| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | struct LibraryExport { |
| 6 | internal string |
| 7 | external string |
| 8 | origin reader.NodeId = reader.no_node |
| 9 | } |
| 10 | |
| 11 | struct LibraryIdentity { |
| 12 | token u8 = 1 |
| 13 | } |
| 14 | |
| 15 | @[heap] |
| 16 | struct LibraryDeclaration { |
| 17 | identity &LibraryIdentity |
| 18 | name LibraryName |
| 19 | source_path string |
| 20 | imports []ImportSet |
| 21 | exports []LibraryExport |
| 22 | code &Code |
| 23 | forms []reader.NodeId |
| 24 | } |
| 25 | |
| 26 | fn (mut b Builder) library_export_binding(id reader.NodeId) !string { |
| 27 | name := b.code.variable(id)! |
| 28 | if !isnil(b.code.generated) { |
| 29 | if alias := b.code.generated.aliases[name] { |
| 30 | if alias.use_site && isnil(alias.use_scope) { return alias.use_name } |
| 31 | } |
| 32 | } |
| 33 | return name |
| 34 | } |
| 35 | |
| 36 | fn (mut b Builder) library_export(id reader.NodeId) !LibraryExport { |
| 37 | if b.code.node(id)!.kind == .symbol { |
| 38 | return LibraryExport{ internal: b.library_export_binding(id)!, external: external_identifier_key(b.code.datum_name(id)!), origin: b.code.source_origin(id) } |
| 39 | } |
| 40 | parts := b.code.nodes(id)! |
| 41 | if parts.len != 3 || b.library_identifier(parts[0])! != 'rename' { |
| 42 | return failure('library export needs an identifier or (rename internal external)') |
| 43 | } |
| 44 | return LibraryExport{ internal: b.library_export_binding(parts[1])!, external: external_identifier_key(b.library_identifier(parts[2])!), origin: b.code.source_origin(parts[1]) } |
| 45 | } |
| 46 | |
| 47 | fn (mut b Builder) library_declaration(nodes []reader.NodeId) !&LibraryDeclaration { |
| 48 | check_arity('define-library', nodes.len - 1, 1, -1)! |
| 49 | name := b.library_name(nodes[1])! |
| 50 | mut imports := []ImportSet{} |
| 51 | mut exports := []LibraryExport{} |
| 52 | mut bodies := []reader.NodeId{} |
| 53 | mut pending := [BodyCursor{ items: unsafe { nodes[2..] } }] |
| 54 | group_base := if isnil(b.code.graphs) { 0 } else { b.code.graphs.groups.len } |
| 55 | if !isnil(b.code.graphs) { b.code.graphs.groups << reader.no_node } |
| 56 | defer { |
| 57 | if !isnil(b.code.graphs) { b.code.graphs.groups.trim(group_base) } |
| 58 | } |
| 59 | for pending.len > 0 { |
| 60 | if !isnil(b.code.graphs) { |
| 61 | for b.code.graphs.groups.len < group_base + pending.len { |
| 62 | b.code.graphs.groups << reader.no_node |
| 63 | } |
| 64 | } |
| 65 | last := pending.len - 1 |
| 66 | if pending[last].index == pending[last].items.len { |
| 67 | pending.pop() |
| 68 | if !isnil(b.code.graphs) { b.code.graphs.groups.pop() } |
| 69 | continue |
| 70 | } |
| 71 | declaration := pending[last].items[pending[last].index] |
| 72 | pending[last].index++ |
| 73 | b.collect_library_declaration(declaration, mut imports, mut exports, mut bodies, mut pending) or { |
| 74 | return b.code.source_failure(declaration, err) |
| 75 | } |
| 76 | } |
| 77 | // Retain only the referenced generated subtrees and aliases. The enclosing |
| 78 | // compilation may now discard its expansion arena before this library is |
| 79 | // ever imported. Original reader nodes remain shared immutable source data. |
| 80 | mut code := &Code{ doc: b.code.doc, source_path: b.code.source_path, syntax_seed: b.code.syntax_seed, sources: b.code.sources } |
| 81 | if !isnil(b.code.graphs) { |
| 82 | code.enable_datum_graphs() |
| 83 | code.graphs.literals = b.code.child_graph_literal_owner() |
| 84 | } |
| 85 | mut importer := &SourceImporter{ parent: b.code, code: code } |
| 86 | defer { |
| 87 | importer.release_scratch() |
| 88 | unsafe { free(importer) } |
| 89 | } |
| 90 | mut forms := filled_storage(bodies.len, reader.NodeId(0)) |
| 91 | for i, id in bodies { container_store_validated(mut forms, i, importer.import_form(id)!) } |
| 92 | return &LibraryDeclaration{ identity: &LibraryIdentity{}, name: name, source_path: b.code.source_file(nodes[0]), imports: imports, exports: exports, code: code, forms: forms } |
| 93 | } |
| 94 | |
| 95 | fn (mut b Builder) collect_library_declaration(declaration reader.NodeId, mut imports []ImportSet, mut exports []LibraryExport, mut bodies []reader.NodeId, mut pending []BodyCursor) ! { |
| 96 | parts := b.code.nodes(declaration)! |
| 97 | if parts.len == 0 { return failure('library declaration must be nonempty') } |
| 98 | head := b.library_identifier(parts[0])! |
| 99 | match head { |
| 100 | 'import' { |
| 101 | for id in parts[1..] { imports << b.import_set(id)! } |
| 102 | } |
| 103 | 'export' { |
| 104 | for id in parts[1..] { exports << b.library_export(id)! } |
| 105 | } |
| 106 | 'include', 'include-ci' { |
| 107 | bodies << b.static_forms(head, parts, declaration)! |
| 108 | } |
| 109 | 'include-library-declarations', 'cond-expand' { |
| 110 | b.code.enter_syntax_group(declaration)! |
| 111 | pending << BodyCursor{ items: b.static_forms(head, parts, declaration)! } |
| 112 | } |
| 113 | 'begin' { |
| 114 | for id in parts[1..] { bodies << id } |
| 115 | } |
| 116 | else { return failure('unsupported library declaration ${head}') } |
| 117 | } |
| 118 | } |
| 119 | |