Appearance
| 1 | namespace Compiler is | |
| 2 | use Collections.LIST | |
| 3 | use Semantic.Symbols | |
| 4 | ||
| 5 | // The report a --cells or --session build writes beside its output, | |
| 6 | // as JSON: one entry a cell, its namespace name and the names a | |
| 7 | // later cell can import from it. | |
| 8 | class CELL_EXPORTS(cells: LIST[CELL_EXPORT] public) | |
| 9 | ||
| 10 | // One cell's exportable names. | |
| 11 | class CELL_EXPORT(cell: string public, exports: LIST[string] public) | |
| 12 | ||
| 13 | // What a --cells or --session build tells the session host about each | |
| 14 | // cell: the names a later cell can import. The .NET host reads these | |
| 15 | // off each cell's assembly; a host with no assemblies to read - a | |
| 16 | // wasm engine, a browser - has the compiler report them instead. The | |
| 17 | // shape is the manifest's, so the manifest can carry the same fields | |
| 18 | // when it exists. | |
| 19 | class CELL_EXPORT_REPORT is | |
| 20 | // The report for the cells this build registered: for each cell | |
| 21 | // namespace, the names a later cell can write after `use cellK.` - | |
| 22 | // the types declared directly in the namespace, the global | |
| 23 | // functions and the global variables. Public names always; | |
| 24 | // underscore names too, since a later cell of the same session | |
| 25 | // reaches them as a later part of one file could. The entry a | |
| 26 | // host calls itself is left out, and so is anything the compiler | |
| 27 | // generated: a `$` in a name marks it. | |
| 28 | build(symbol_table: Semantic.SYMBOL_TABLE, cell_names: LIST[string]) -> CELL_EXPORTS static is | |
| 29 | let cells = LIST[CELL_EXPORT]() | |
| 30 | ||
| 31 | for name in cell_names do | |
| 32 | let cell = cast NAMESPACE?(symbol_table.global_scope.find_direct(name)) | |
| 33 | ||
| 34 | if !cell? then | |
| 35 | continue | |
| 36 | fi | |
| 37 | ||
| 38 | let exports = LIST[string]() | |
| 39 | ||
| 40 | for symbol in cell.symbols do | |
| 41 | // The cell's global functions and variables sit in | |
| 42 | // the synthesised `$globals` class rather than the | |
| 43 | // namespace itself, so its members are the cell's as | |
| 44 | // much as the namespace's own symbols are. | |
| 45 | if isa Classy(symbol) /\ symbol.name =~ "$globals" then | |
| 46 | for member in (cast Classy(symbol)).symbols do | |
| 47 | if _is_export(member) then | |
| 48 | _add_once(exports, member.name) | |
| 49 | fi | |
| 50 | od | |
| 51 | elif _is_export(symbol) then | |
| 52 | _add_once(exports, symbol.name) | |
| 53 | fi | |
| 54 | od | |
| 55 | ||
| 56 | exports.sort((a: string, b: string) -> int => string.compare_ordinal(a, b)) | |
| 57 | ||
| 58 | cells.add(CELL_EXPORT(name, exports)) | |
| 59 | od | |
| 60 | ||
| 61 | return CELL_EXPORTS(cells) | |
| 62 | si | |
| 63 | ||
| 64 | _is_export(symbol: Symbol) -> bool static is | |
| 65 | if symbol.name.length == 0 \/ | |
| 66 | symbol.name.contains('$') \/ | |
| 67 | symbol.name =~ "entry" \/ | |
| 68 | symbol.name =~ "_entry" | |
| 69 | then | |
| 70 | return false | |
| 71 | fi | |
| 72 | ||
| 73 | if isa GLOBAL_FUNCTION(symbol) then | |
| 74 | return true | |
| 75 | fi | |
| 76 | ||
| 77 | if isa GLOBAL_VARIABLE(symbol) then | |
| 78 | return true | |
| 79 | fi | |
| 80 | ||
| 81 | // A name overloaded across arities or argument types is one | |
| 82 | // import: the groups hold the declarations, the name is | |
| 83 | // what a later cell writes. | |
| 84 | if isa TYPE_GROUP(symbol) then | |
| 85 | return true | |
| 86 | fi | |
| 87 | ||
| 88 | if isa FUNCTION_GROUP(symbol) then | |
| 89 | return true | |
| 90 | fi | |
| 91 | ||
| 92 | // Types reach a later cell by name; a union's variants sit in | |
| 93 | // the union's own scope and do not appear here, and a nested | |
| 94 | // namespace is not something `use cellK.name` imports. | |
| 95 | return isa Classy(symbol) | |
| 96 | si | |
| 97 | ||
| 98 | _add_once(exports: LIST[string], name: string) static is | |
| 99 | if !exports.contains(name) then | |
| 100 | exports.add(name) | |
| 101 | fi | |
| 102 | si | |
| 103 | ||
| 104 | // The report as JSON, written directly: the shape is fixed and | |
| 105 | // small, and a protocol writer that reflects over .NET types has | |
| 106 | // no counterpart on the targets whose hosts need this file most. | |
| 107 | // Cell names are identifiers and export names are user names, so | |
| 108 | // the only escapes a name can carry are quote and backslash. | |
| 109 | write(report: CELL_EXPORTS) -> string static is | |
| 110 | let text = System.Text.StringBuilder() | |
| 111 | ||
| 112 | text.append("{{\"cells\":[") | |
| 113 | ||
| 114 | for i in 0..report.cells.count do | |
| 115 | if i > 0 then | |
| 116 | text.append(',') | |
| 117 | fi | |
| 118 | ||
| 119 | let cell = report.cells[i] | |
| 120 | ||
| 121 | text.append("{{\"cell\":") | |
| 122 | _append_string(text, cell.cell) | |
| 123 | text.append(",\"exports\":[") | |
| 124 | ||
| 125 | for j in 0..cell.exports.count do | |
| 126 | if j > 0 then | |
| 127 | text.append(',') | |
| 128 | fi | |
| 129 | ||
| 130 | _append_string(text, cell.exports[j]) | |
| 131 | od | |
| 132 | ||
| 133 | text.append("]}}") | |
| 134 | od | |
| 135 | ||
| 136 | text.append("]}}") | |
| 137 | ||
| 138 | return text.to_string() | |
| 139 | si | |
| 140 | ||
| 141 | _append_string(text: System.Text.StringBuilder, value: string) static is | |
| 142 | text.append('"') | |
| 143 | ||
| 144 | for c in value do | |
| 145 | if c == '\"' then | |
| 146 | text.append("\\\"") | |
| 147 | elif c == '\\' then | |
| 148 | text.append("\\\\") | |
| 149 | elif c < ' ' then | |
| 150 | _append_escaped(text, c) | |
| 151 | else | |
| 152 | text.append(c) | |
| 153 | fi | |
| 154 | od | |
| 155 | ||
| 156 | text.append('"') | |
| 157 | si | |
| 158 | ||
| 159 | _append_escaped(text: System.Text.StringBuilder, c: char) static is | |
| 160 | let hex = "0123456789abcdef" | |
| 161 | let code = cast int(c) | |
| 162 | ||
| 163 | text.append('\\') | |
| 164 | text.append('u') | |
| 165 | text.append(hex[code >> 12 & 0xF]) | |
| 166 | text.append(hex[code >> 8 & 0xF]) | |
| 167 | text.append(hex[code >> 4 & 0xF]) | |
| 168 | text.append(hex[code & 0xF]) | |
| 169 | si | |
| 170 | si | |
| 171 | si |