Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace IR.Emitter is | |
| 4 | use Ghul.Pipes | |
| 5 | ||
| 6 | use Semantic.Symbols.Function | |
| 7 | use Semantic.Symbols.FUNCTION_GROUP | |
| 8 | ||
| 9 | use Values.Value | |
| 10 | ||
| 11 | // The string builder runtime operations, as calls on .NET's | |
| 12 | // DefaultInterpolatedStringHandler. | |
| 13 | class CIL_STRING_BUILDER is | |
| 14 | _handler: Semantic.Types.Type? | |
| 15 | ||
| 16 | _constructor: Function? | |
| 17 | _append_literal: Function? | |
| 18 | _append_formatted: Function? | |
| 19 | _append_formatted_alignment: Function? | |
| 20 | _append_formatted_format: Function? | |
| 21 | _append_formatted_alignment_format: Function? | |
| 22 | _to_string_and_clear: Function? | |
| 23 | ||
| 24 | init() is | |
| 25 | si | |
| 26 | ||
| 27 | // The call `runtime` stands for. It is built where the | |
| 28 | // operation was, as far as the positions its values mark go. | |
| 29 | lowered(runtime: Values.RUNTIME) -> Value is | |
| 30 | _materialize() | |
| 31 | ||
| 32 | let locations = IoC.CONTAINER.instance.location_stack | |
| 33 | ||
| 34 | locations.push(runtime.operator_location) | |
| 35 | ||
| 36 | try | |
| 37 | return _call(runtime) | |
| 38 | finally | |
| 39 | locations.pop() | |
| 40 | yrt | |
| 41 | si | |
| 42 | ||
| 43 | _call(runtime: Values.RUNTIME) -> Value is | |
| 44 | let operands = runtime.operands | |
| 45 | let builder = operands[0] | |
| 46 | ||
| 47 | case runtime.operation | |
| 48 | when IR.RuntimeOperation.NEW_STRING_BUILDER then | |
| 49 | return Values.NEW(_handler!, _constructor!, [operands[0], operands[1]]:Value) | |
| 50 | ||
| 51 | when IR.RuntimeOperation.APPEND_TEXT then | |
| 52 | return _on_builder(_append_literal!, builder, [operands[1]]:Value) | |
| 53 | ||
| 54 | when IR.RuntimeOperation.APPEND_VALUE then | |
| 55 | return _on_builder(_specialized(_append_formatted!, runtime), builder, [operands[1]]:Value) | |
| 56 | ||
| 57 | when IR.RuntimeOperation.APPEND_ALIGNED_VALUE then | |
| 58 | return _on_builder(_specialized(_append_formatted_alignment!, runtime), builder, [operands[1], operands[2]]:Value) | |
| 59 | ||
| 60 | when IR.RuntimeOperation.APPEND_FORMATTED_VALUE then | |
| 61 | return _on_builder(_specialized(_append_formatted_format!, runtime), builder, [operands[1], operands[2]]:Value) | |
| 62 | ||
| 63 | when IR.RuntimeOperation.APPEND_ALIGNED_FORMATTED_VALUE then | |
| 64 | return _on_builder(_specialized(_append_formatted_alignment_format!, runtime), builder, [operands[1], operands[2], operands[3]]:Value) | |
| 65 | ||
| 66 | when IR.RuntimeOperation.BUILD_STRING then | |
| 67 | return _on_builder(_to_string_and_clear!, builder, Collections.LIST[Value]()) | |
| 68 | ||
| 69 | else | |
| 70 | throw System.InvalidOperationException("not a string builder operation: {runtime.operation}") | |
| 71 | esac | |
| 72 | si | |
| 73 | ||
| 74 | _specialized(function: Function, runtime: Values.RUNTIME) -> Semantic.Symbols.Symbol => | |
| 75 | function.specialize([runtime.type_argument!]) | |
| 76 | ||
| 77 | _on_builder(member: Semantic.Symbols.Symbol, builder: Value, arguments: Collections.List[Value]) -> Value => | |
| 78 | member.call(Source.LOCATION.internal, builder, arguments, null, IoC.CONTAINER.instance.function_caller) | |
| 79 | ||
| 80 | _materialize() is | |
| 81 | if _handler? then | |
| 82 | return | |
| 83 | fi | |
| 84 | ||
| 85 | let lookup = IoC.CONTAINER.instance.innate_symbol_lookup | |
| 86 | let string_type = lookup.get_string_type() | |
| 87 | let int_type = lookup.get_int_type() | |
| 88 | ||
| 89 | let handler = lookup.get_interpolated_string_handler_type() | |
| 90 | ||
| 91 | _handler = handler | |
| 92 | ||
| 93 | let append_formatted = _group(handler, "append_formatted").functions | |
| 94 | ||
| 95 | // the constructor overload wanted is the only one with 2 arguments: | |
| 96 | _constructor = _group(handler, "init").functions |> filter(f => f.arguments.count == 2) |> only() | |
| 97 | ||
| 98 | // there is only one overload of append_literal: | |
| 99 | _append_literal = _group(handler, "append_literal").functions[0] | |
| 100 | ||
| 101 | // the only overload of append_formatted that is both generic and | |
| 102 | // takes 1 argument: | |
| 103 | _append_formatted = append_formatted |> filter(f => f.is_generic /\ f.arguments.count == 1) |> only() | |
| 104 | ||
| 105 | // there are two overloads of append_formatted with 2 arguments; | |
| 106 | // this is the one whose second argument is an int: | |
| 107 | _append_formatted_alignment = | |
| 108 | append_formatted |> | |
| 109 | filter(f => f.is_generic /\ f.arguments.count == 2 /\ f.arguments[1].type!.compare(int_type) == Semantic.Types.MATCH.SAME) |> | |
| 110 | only() | |
| 111 | ||
| 112 | // and this the one whose second argument is a string (the BCL | |
| 113 | // declares the format parameter as string?, so a match is | |
| 114 | // anything not DIFFERENT): | |
| 115 | _append_formatted_format = | |
| 116 | append_formatted |> | |
| 117 | filter(f => f.is_generic /\ f.arguments.count == 2 /\ f.arguments[1].type!.compare(string_type) != Semantic.Types.MATCH.DIFFERENT) |> | |
| 118 | only() | |
| 119 | ||
| 120 | // there is only one overload of append_formatted with 3 arguments: | |
| 121 | _append_formatted_alignment_format = append_formatted |> filter(f => f.is_generic /\ f.arguments.count == 3) |> only() | |
| 122 | ||
| 123 | // there is only one overload of to_string_and_clear: | |
| 124 | _to_string_and_clear = _group(handler, "to_string_and_clear").functions[0] | |
| 125 | si | |
| 126 | ||
| 127 | _group(type: Semantic.Types.Type, name: string) -> FUNCTION_GROUP => | |
| 128 | cast FUNCTION_GROUP?(type.find_member(name))! | |
| 129 | si | |
| 130 | si |