harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

declaration_solver.txt (4766B)


      1 First thing harec does after parsing the inputs is making sure all the global
      2 declarations are valid. A constraint unique to this compilation step is that
      3 unlike with sequential bindings in a compound expression, there is no explicit
      4 order in which the declarations should be traversed to validate them, the
      5 correct traversal order is given by the contents of declarations themselves. To
      6 make things even more complex, interdependent declarations may reside in
      7 different subunits, so there is quite a bit of scope juggling involved to
      8 achieve correct scope shadowing.
      9 
     10 Declarations come in five flavours - functions, constants, type aliases, global
     11 variables and enum fields.
     12 
     13 First, the constants defined upon harec invocation are checked and put into a
     14 dedicated scope. Then the imports for each subunit are loaded. This step requires
     15 the command line defines to already be in place. After the imports of a
     16 subunit are loaded, all of its declarations except enums are marked
     17 incomplete and put into the unit scope. Duplicate declarations are caught and
     18 reported at this step. Enum types are treated separately from enum values
     19 in this algorithm. Enum types never have dependencies and can be completed
     20 on the spot. Enum values are put into a special scope that is created for each
     21 enum type and marked incomplete.
     22 
     23 At this point the dedicated scope for defines is reparented on the unit
     24 scope, shadowing the declarations from the source files.
     25 
     26 Next, aliases of enum types are detected and taken care of. Because an enum
     27 alias only depends on the underlying enum type, its entire dependency tree
     28 is known immediately when it is detected, so it can be completed right away.
     29 For values of enum aliases we need to cover three possible configurations:
     30     (a) An alias to an enum whose values are already completed at this stage.
     31 	This happens when the underlying enum was imported from another module.
     32 	This is the easiest case to handle, we can just copy it's values to the
     33 	alias immediately.
     34     (b) An enum alias whose underlying enum is defined in the same unit this
     35 	case is handled by the general resolution algorithm described below.
     36 
     37 With everything but the declarations in place, the core part of the algorithm
     38 is started:
     39 
     40 For each incomplete declaration:
     41     (1) Save previous enum and subunit context, and load subunit context for
     42         current declaration.
     43     (2) If this declaration is marked as in-progress, error out because the
     44         declaration part of a dependency cycle, otherwise mark this
     45         declaration as in progress.
     46     (3) Disambiguate between different declaration flavours:
     47         - for functions, constants and globals and enum fields:
     48             Check and evaluate as if all the dependencies are already resolved.
     49             For enum fields, the relevant enum context has to be loaded and
     50             implicit values need to be taken care of.
     51 
     52             If an incomplete dependency is encountered along the way, first
     53             resolve that dependency recursively and then continue with
     54             resolution. When the check is done, insert the declaration into the
     55             unit scope. Declaration is now considered complete.
     56         - for type aliases:
     57             Types have two distinct properties that both have to be computed
     58             before a type is complete, their dimensions and their
     59             representation. The approach taken can be summarized as follows:
     60 
     61             (a) Compute secondary type's dimensions by traversing just enough
     62                 of its constituent types to be able to do so.  If an incomplete
     63                 type alias is encountered, first compute that type's dimensions
     64                 recursively and then continue.
     65             (b) Insert the complete alias into the type store and into the unit
     66                 scope.
     67             (c) Compute secondary type's representation by traversing all of
     68                 its constituent types and repeating this entire process on
     69                 those that are still incomplete.
     70 
     71             A valid type alias never references itself during (a), because such
     72             a type would not be storable in memory.  Types may reference
     73             themselves during (c). Such types are called self-referential
     74             types. Self-references during (c) require no special treatment,
     75             because from step (b) onwards the type we are currently declaring
     76             can be treated as a regular complete type.
     77     (4) Remove the in progress mark
     78     (5) Restore the saved subunit and enum context
     79 
     80 At this point all the incomplete declarations in the unit scope are shadowed by
     81 their complete counterparts. From here on, no special considerations regarding
     82 incomplete declarations apply and check can proceed accordingly.