DIP31

From D Wiki
Revision as of 08:06, 17 March 2013 by Deadalnix (talk | contribs) (Created page with "{| class="wikitable" !Title: !'''Defined compile time paradox resolution''' |- |DIP: |31 |- |Version: |1 |- |Status: |Draft |- |Created: |2013-17-27 |- |Last Modified: |2013-...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Title: Defined compile time paradox resolution
DIP: 31
Version: 1
Status: Draft
Created: 2013-17-27
Last Modified: 2013-17-27
Author: Amaury SÉCHET

Abstract

It is easy to introduce paradoxes with compile time feature, by introducing new symbols when they may have been used by other compile time feature. Currently, what happen in completely implementation defined and can change from version to version. This DIP intend to propose a sane way to handle compile time paradoxes.

Rationale

The way compile time paradoxes are handled right now is implementation defined. it may change with any release, and make it impossible to write a front end that handle such features in a consistent way with DMD.

Resolution priority

Resolution priority are defined as follow :

  1. A construct that do not introduce new symbol (except itself).
  2. A construct that may introduce symbol conditionally.
  3. A construct that may introduce unknown symbols.

The obvious feature of priority 2 is mixin, as it may introduce arbitrary symbols. All declarations (variables, fields, functions, method, alias, templates, etc...) are of priority 0, unless they are in a static if, in which case they become of priority 1. Static ifs are of the highest priority it contains.

See sample code for constructs with priority noted as comment.

int i; // 0
void foo() {} // 0

static if(condition1) { // 2
    int j; // 1
    static if(condition2) { // 1
        int k; // 1
    }
} else {
    mixin(str); // 2
}

All symbol of priority 0 are registered in the symbol table directly. Symbols of priority 1 are registered as conditional symbols.

Poison mode

As long as construct of priority 1 or 2 exists, the symbol table is in poison mode. Any attempt to resolve a symbol will create a poison at the corresponding entry in addition to process the regular symbol lookup. Any attempt to register a symbol at a poisoned position is an error.

string foo() {
	return "int foo(int i) { return i; }";
}

mixin(foo()); // Error, foo is poisoned.

However, this work as foo don't need to be resolved before the new overload is mixed in :

string foo() {
	return bar();
}

mixin(bar());

string bar() {
	return "int foo(int i) { return i; }";
}

Order of evaluation

Construct of priority 2 are evaluated in order of appearance in the source. When all constructs of priority 2 are reduced, the compiler start reducing construct of priority 1, in order of appearance.

Example of order of evaluation :

mixin(A); // 1

static if(condition) { // 2
    mixin(B); // 3
    static if(condition2) { ... } // 5
}

static if(condition3) { ... } // 6
mixin(C); // 4

Resolution of conditional symbols

A compile time construct can require the resolution of a conditional symbol. In this case, construct on which the inclusion of that symbol depends on are reduced on a per needed basis. If a construct need to reduce itself, this is an error.

See examples :

bool foo() {
    return true;
}

static if(foo()) { // Error, static if may introduce foo.
    void foo() {}
}
bool foo() {
    return true;
}

mixin(bar()); // 1. bar is conditionally included, static if is evaluated.
              // 3. mixin is processed, i is introduced. If is was resolved before, it is an error as i is poisoned.

static if(foo()) { // 2. static if is reduced.
    string bar() { return "int i;"; }
}

Symbol withing function call are resolved in order of appearance in the function, and function are explored in a depth first manner :

int bar() {
    // 2. resolve symbol present in bar in order of appearance.
}

int foo() {
    return bar() + buzz(); // 1. resolve bar.
                           // 3. resolve buzz.
}

int buzz() {
    // 4. resolve symbol in buzz.
}

static if(foo() > 0) { ... }

Note: The local scope of method isn't in poison mode.

Conclusion

This DIP propose a way to ensure that no compile time construct can change the meaning of previously reduced compile time construct. What happen right now is such situation is completely implementation defined (and may change any time). The proposed solution allow to forward reference symbols within static ifs, so error are reduced to cases where compile time construct change their own meaning and order in the file should not matter most of the time.

Copyright

This document has been placed in the Public Domain.