| ... | ... | @@ -1,74 +0,0 @@ |
| 1 | | # How Semantic Analysis Works |
| 2 | | |
| 3 | | We start with a set of files. Typically the user only has one entry point file, |
| 4 | | which imports the other files they want to use. However, the compiler may |
| 5 | | choose to add more files to the compilation, for example bootstrap.zig which |
| 6 | | contains the code that calls main. |
| 7 | | |
| 8 | | Our goal now is to treat everything that is marked with the `export` keyword |
| 9 | | as a root node, and then parse and semantically analyze as little as possible |
| 10 | | in order to fulfill these exports. |
| 11 | | |
| 12 | | So, some parts of the code very well may have uncaught semantic errors, but as |
| 13 | | long as the code is not referenced in any way, the compiler will not complain |
| 14 | | because the code may as well not exist. This is similar to the fact that code |
| 15 | | excluded from compilation with an `#ifdef` in C is not analyzed. Avoiding |
| 16 | | analyzing unused code will save compilation time - one of Zig's goals. |
| 17 | | |
| 18 | | So, for each file, we iterate over the top level declarations. The set of top |
| 19 | | level declarations are: |
| 20 | | |
| 21 | | * Function Definition |
| 22 | | * Global Variable Declaration |
| 23 | | * Container Declaration (struct or enum) |
| 24 | | * Error Value Declaration |
| 25 | | * Use Declaration |
| 26 | | |
| 27 | | Each of these can have `export` attached to them except for error value |
| 28 | | declarations and use declarations. |
| 29 | | |
| 30 | | When we see a top level declaration during this iteration, we determine its |
| 31 | | unique name identifier within the file. For example, for a function definition, |
| 32 | | the unique name identifier is simply its name. Using this name we add the top |
| 33 | | level declaration to a map. |
| 34 | | |
| 35 | | If the top level declaration is exported, we add it to a set of exported top |
| 36 | | level identifiers. |
| 37 | | |
| 38 | | If the top level declaration is a use declaration, we add it to a set of use |
| 39 | | declarations. |
| 40 | | |
| 41 | | If the top level declaration is an error value declaration, we assign it a value |
| 42 | | and increment the count of error values. |
| 43 | | |
| 44 | | After this preliminary iteration over the top level declarations, we iterate |
| 45 | | over the use declarations and resolve them. To resolve a use declaration, we |
| 46 | | analyze the associated expression, verify that its type is the namespace type, |
| 47 | | and then add all the items from the namespace into the top level declaration |
| 48 | | map for the current file. |
| 49 | | |
| 50 | | To analyze an expression, we recurse the abstract syntax tree of the |
| 51 | | expression. Whenever we must look up a symbol, if the symbol exists already, |
| 52 | | we can use it. Otherwise, we look it up in the top level declaration map. |
| 53 | | If it exists, we can use it. Otherwise, we interrupt resolving this use |
| 54 | | declaration to resolve the next one. If a dependency loop is detected, emit |
| 55 | | an error. If all use declarations are resolved yet the symbol we need still |
| 56 | | does not exist, emit an error. |
| 57 | | |
| 58 | | To analyze an `@import` expression, find the referenced file, parse it, and |
| 59 | | add it to the set of files to perform semantic analysis on. |
| 60 | | |
| 61 | | Proceed through the rest of the use declarations the same way. |
| 62 | | |
| 63 | | If we make it through the use declarations without an error, then we have a |
| 64 | | complete map of all globals that exist in the current file. |
| 65 | | |
| 66 | | Next we iterate over the set of exported top level declarations. |
| 67 | | |
| 68 | | If it's a function definition, add it to the set of exported function |
| 69 | | definitions and resolve the function prototype only. Otherwise, resolve the |
| 70 | | top level declaration completely. This may involve recursively resolving other |
| 71 | | top level declarations that expressions depend on. |
| 72 | | |
| 73 | | Finally, iterate over the set of exported function definitions and analyze the |
| 74 | | bodies. |