Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Logging; | |
| 3 | use Trees; | |
| 4 | use Source; | |
| 5 | ||
| 6 | // Registers an innate for each intrinsic the compilation declares in | |
| 7 | // source, so the assembly that provides the intrinsics can be built by | |
| 8 | // a compiler that otherwise reflects them. | |
| 9 | // | |
| 10 | // The declaration itself stays where it is written and is emitted | |
| 11 | // normally — that emitted method, carrying its INTRINSIC_ATTRIBUTE, is | |
| 12 | // what every other compilation reflects. What it cannot do is serve | |
| 13 | // this compilation: a call has to lower to an opcode, and that is a | |
| 14 | // property of the symbol's class, not a flag. So a matching innate is | |
| 15 | // registered in `Ghul`, where the language looks for it. | |
| 16 | // | |
| 17 | // Runs once signatures are resolved, because the innate takes its own | |
| 18 | // from the declaration. | |
| 19 | class REGISTER_SOURCE_INTRINSICS: ScopedVisitor is | |
| 20 | _namespaces: Semantic.NAMESPACES; | |
| 21 | ||
| 22 | init( | |
| 23 | logger: Logger, | |
| 24 | symbol_table: Semantic.SYMBOL_TABLE, | |
| 25 | namespaces: Semantic.NAMESPACES | |
| 26 | ) | |
| 27 | is | |
| 28 | super.init(logger, symbol_table, namespaces); | |
| 29 | ||
| 30 | _namespaces = namespaces; | |
| 31 | si | |
| 32 | ||
| 33 | apply(root: Trees.Node) is | |
| 34 | root.walk(self); | |
| 35 | si | |
| 36 | ||
| 37 | visit(function: Definitions.FUNCTION) is | |
| 38 | super.visit(function); | |
| 39 | ||
| 40 | let symbol = symbol_for(function); | |
| 41 | ||
| 42 | if !symbol? \/ !isa Semantic.Symbols.Function(symbol) then | |
| 43 | return; | |
| 44 | fi | |
| 45 | ||
| 46 | let declaration = cast Semantic.Symbols.Function(symbol); | |
| 47 | ||
| 48 | if !declaration.intrinsic_operation? \/ declaration.is_innate then | |
| 49 | return; | |
| 50 | fi | |
| 51 | ||
| 52 | let target = _namespaces.try_find_namespace(".Ghul"); | |
| 53 | ||
| 54 | if !target? then | |
| 55 | return; | |
| 56 | fi | |
| 57 | ||
| 58 | let intrinsic = | |
| 59 | cast Semantic.Symbols.Function( | |
| 60 | target.declare_innate( | |
| 61 | declaration.location, | |
| 62 | declaration.name, | |
| 63 | declaration.intrinsic_operation!, | |
| 64 | target, | |
| 65 | null | |
| 66 | ) | |
| 67 | ); | |
| 68 | ||
| 69 | intrinsic.arguments = declaration.arguments; | |
| 70 | intrinsic.argument_names = declaration.argument_names; | |
| 71 | intrinsic.return_type = declaration.return_type; | |
| 72 | ||
| 73 | if declaration.is_generic then | |
| 74 | intrinsic.is_generic = true; | |
| 75 | intrinsic.generic_arguments = declaration.generic_arguments; | |
| 76 | intrinsic.generic_argument_names = declaration.generic_argument_names; | |
| 77 | intrinsic.generic_argument_constraint_kinds = declaration.generic_argument_constraint_kinds; | |
| 78 | intrinsic.generic_argument_has_constructor_constraint = declaration.generic_argument_has_constructor_constraint; | |
| 79 | fi | |
| 80 | si | |
| 81 | si | |
| 82 | si |