crashappsec / libcon4m

Base Compiler and Runtime Support for con4m
Apache License 2.0
0 stars 0 forks source link

Typechecking pass done #21

Closed viega closed 3 months ago

viega commented 3 months ago

The second pass is done enough to move on to code generation. It:

  1. Type checks everything, including non-declared symbols.
  2. Builds full control flow graphs that contain execution-ordered defs/uses.
  3. Adds in auto-declared variables; $result for functions, $i for loops, and $last for for loops.
  4. I redid the way that generic types are done so that they implicitly track what known container types can / cannot match. This, plus the willingness to generate type-cased calls will allow me to avoid the problem of having to declare param types when the only thing you do to it is index it, for instance (which has always been the case in the Nim c4m). I don't think there is anything at all that needs a written type for disambiguation at the moment, and very little will require dynamic checks.

There are some things I'm leaving for later:

  1. I have not done the walk of the CFG yet to generate error messages about use-before-def problems, returns on every branch, etc. But the graph is otherwise completely built.
  2. While I erase $result from function scopes if it isn't used, I'm not yet erasing the loop variables when they're not used (I intend to deal when doing codegen).
  3. Because of that lack of erasure, I don't yet warn when something like $i is ambiguous. However, there is a disambiguation scheme; if you label loops, label$i and, if appropriate, label$last are available. Again, I was planning on adding these warnings in code gen.
  4. Most of the builtin operations (like addition, etc) are essentially converted into magic (polymorphic) function calls, like __add__. I am not yet statically binding where possible; this too I expect to do at code gen (generating type casing when static binding isn't possible).
  5. Though I wasn't planning on re-doing constant folding until later, I didn't go ahead and do it :)
  6. There are probably plenty of bugs / issues to shake out, including a few already on my list... most significantly, I keep forgetting to check, but I think I might have forgotten to make functions from imported modules available outside the module (which won't be much work, but will be natural to handle in codegen).