mewmew / uc

A compiler for the µC language.
57 stars 5 forks source link

semantic: Forward references #50

Closed mewmew closed 8 years ago

mewmew commented 8 years ago

Consider adding support for forward references.

C89 and the first edition of C90 had support for implicit function declarations (support for which was removed in the second revision of implicit declarations), as indicated by the following extract from the C89 draft.

If the expression that precedes the parenthesized argument list in a function call consists solely of an identifier, and if no declaration is visible for this identifier, the identifier is implicitly declared exactly as if, in the innermost block containing the function call, the declaration

extern int  identifier();

appeared.30

Furthermore, clang and gcc supports implicit forward declarations.

u@x1 ~/D/g> clang -o g g.c
g.c:2:9: warning: implicit declaration of function 'foo' is invalid in C99
      [-Wimplicit-function-declaration]
        return foo();
u@x1 ~/D/g> clang -std=c89 -o g g.c
u@x1 ~/D/g>

Contents of g.c:

int main() {
    return foo();
}

int foo() {
    return 42;
}
mewmew commented 8 years ago

Note, if support for forward references is to be added, it will not rely on the implicit forward reference semantics of C89, but rather the semantics used by the C# compiler, which parses source files using two-pass parsing. The first pass will simply parse file scope (i.e. global) declarations but skip function bodies. The second pass parses the full function bodies, having the global scope of declarations available during its semantic analysis phase, as further outlined in outlined in [1].

Our intention is to implement support for forward references (but not implicit references as defined by C89) by iterating over the global declarations and adding them to the file scope, before doing semantic analysis of the function bodies. Initially, this will be done without skipping function bodies during parsing, and thus requires only a single-pass parser. A future version of the compiler may use a two-pass parser to reduce memory usage when compiling several larger translation units.

To the best of our knowledge, adding support for forward references should not invalidate any currently valid C programs, but rather introduce a superset of C for which source files may be written using forward references without ambiguity.

mewmew commented 8 years ago

The semantic analysis is implemented with support for forward references, as discussed in this issue. Support for implicit references has been explicitly left out.