|
| 1 | +# data model |
| 2 | + |
| 3 | +This chapter describes how functions, types, templates and values work in this |
| 4 | +language. |
| 5 | + |
| 6 | +Here are a couple of rules in this language: |
| 7 | + - Types are first class values, and can be passed to functions or type templates. |
| 8 | + - All function and type definitions are always templates. |
| 9 | + - A type definition is an alternate representation of a function that returns a type. |
| 10 | + - Multiple type and function definition with the same name form a overload set. |
| 11 | + - Type and function definitions may be modified until the whole overload set is frozen. |
| 12 | + - An overload set is frozen when a type or function is instantiated. |
| 13 | + - Functions and types are lazilly evaluated to delay freezing; functions and |
| 14 | + global variables are recursively evaluated when they are exported, after |
| 15 | + processing the last statement of a file. |
| 16 | + |
| 17 | +## Templates |
| 18 | + |
| 19 | +Function and type definitions are always templates in this language. |
| 20 | + |
| 21 | + |
| 22 | +## Overload sets |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +## Compilation Order |
| 28 | + |
| 29 | +The compilation order is important when we are describing how the compiler |
| 30 | +knows which types are visible and complete. |
| 31 | + |
| 32 | +Compilation starts after the prologue-scan which will read every source file |
| 33 | +in the repository and each imported repository. |
| 34 | + |
| 35 | +Each file is then compiled in random order while satisfying that all imported |
| 36 | +modules of a file are compiled first. In incremental/language server mode, |
| 37 | +files may trigger recompilation of files and their dependent files. |
| 38 | + |
| 39 | +Each file is compiled statement-by-statement, as-if they executed in a |
| 40 | +interpreter, for example: |
| 41 | + - type-template definition. |
| 42 | + - function-template definition. |
| 43 | + - global variable definition. |
| 44 | + - executable statements |
| 45 | + |
| 46 | +> [!NOTE] |
| 47 | +> All types and function declarations are templates in this language. |
| 48 | +
|
| 49 | +At this time function and type declarations remain in their "modifiable" state, |
| 50 | +this means: |
| 51 | + - Other versions of type and functions can be added to the overload set. |
| 52 | + - It is possible to modify types by removing and adding members to types, |
| 53 | + as-if types are mutable values. |
| 54 | + |
| 55 | +During the statement-by-statement compilation there may also be normal code |
| 56 | +statments at file scope that will be executed that for example can make these |
| 57 | +modifications of types. These code statements can cause types and functions |
| 58 | +to transition to "frozen" and "instantiated" state. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Functions |
| 63 | + |
| 64 | +A function is a piece of code to execute, its identity is the memory address to |
| 65 | +the start of a function, together with a function's signature. |
| 66 | + |
| 67 | +It is possible for code to be shared by multiple functions, by de-duplication of |
| 68 | +identical code being generated. |
| 69 | + |
| 70 | +> [!NOTE] |
| 71 | +> A function address is not unique between functions, |
| 72 | +
|
| 73 | + |
| 74 | +### Function Definition |
| 75 | + |
| 76 | +A function definition consists of: |
| 77 | + - A fully qualified name |
| 78 | + - A function signature |
| 79 | + - Attributes |
| 80 | + - A block of code |
| 81 | + |
| 82 | +A function definition creates a function-template which is part of an overload set |
| 83 | +which shares the fully qualified name with other function definitions. |
| 84 | + |
| 85 | +### Compilation Order |
| 86 | + |
| 87 | +A function definition during compilation has the following states: |
| 88 | + 1. Modifiable: each function definition is modifiable during compilation. |
| 89 | + 2. Frozen: all function definitions of a overload set is frozen when the |
| 90 | + first reference to a function by that name is being compiled. This will |
| 91 | + in turn freeze each type-template mentioned in the signature of each |
| 92 | + function definition of that overload set. |
| 93 | + 3. Instantiation: when a function call is compiled the proper template in |
| 94 | + the overload set is matched, and then the function is uniquely instantiated |
| 95 | + for this call, with potential constant values. Only when a function is |
| 96 | + instantiated will calls in the code block be compiled and trigger freeze |
| 97 | + and instantiation of other functions. |
| 98 | + |
| 99 | +The compiler is guaranteed to lazy freeze or instantiate functions only when |
| 100 | +those functions are: |
| 101 | + - Called directly in the file-scope, in-order from top to bottom. |
| 102 | + - When functions are explicitly exported to be part of the executable linkage. |
| 103 | + This happens after the last line of the file-scope is processed. |
| 104 | + - When a global variable is explicitly exported to be part of the executable linkage. |
| 105 | + This happens after the last line of the file-scope is processed. |
| 106 | + - A function called `main()` is implicitly exported, this happens as-if |
| 107 | + This happens after the last line of the file-scope is processed. |
| 108 | + |
0 commit comments