Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use System.Exception | |
| 3 | use Ghul.Disposable | |
| 4 | ||
| 5 | use IO.Std | |
| 6 | ||
| 7 | use Logging | |
| 8 | use Source | |
| 9 | ||
| 10 | use IR.Values | |
| 11 | use IR.VALUE_CONVERTER | |
| 12 | use IR.VALUE_BOXER | |
| 13 | ||
| 14 | use Semantic.LEAST_UPPER_BOUND_MAP | |
| 15 | use Semantic.Types.Type | |
| 16 | ||
| 17 | use Syntax.Trees.Definitions.PRAGMA | |
| 18 | ||
| 19 | use Ghul.Pipes | |
| 20 | ||
| 21 | ||
| 22 | // Name and reference walks: calls, identifiers, presence tests, unwraps, references, null and default values. | |
| 23 | partial COMPILE_EXPRESSIONS is | |
| 24 | pre(call: Trees.Expressions.CALL) -> bool => true | |
| 25 | visit(call: Trees.Expressions.CALL) is | |
| 26 | let mark = _logger.mark() | |
| 27 | ||
| 28 | try | |
| 29 | super.pre(call) | |
| 30 | ||
| 31 | // Push the call's expected type into the callee | |
| 32 | // expression. A unit-variant `Option.NONE` sitting at | |
| 33 | // call.function reads it in visit_member and asks | |
| 34 | // OWNER_CONSTRAINT_SPECIALIZER for the specialised | |
| 35 | // owner — the same binding the resolve_constructor | |
| 36 | // path performs for the parenthesised form. Non-variant | |
| 37 | // callees ignore the field. | |
| 38 | // | |
| 39 | // A callee that takes its own type from its context is | |
| 40 | // the exception: the call's expected type is what the | |
| 41 | // callee returns, not what it is, so pushing it here | |
| 42 | // would have `cast(v)` in callee position target the | |
| 43 | // result type and then report that the result cannot be | |
| 44 | // called. Such a callee is left until the arguments have | |
| 45 | // been walked and the whole function type is known. | |
| 46 | let defer_callee = call.function.awaits_context_type | |
| 47 | ||
| 48 | if call.expected_type? /\ !defer_callee then | |
| 49 | call.function.set_expected_type(call.expected_type, call.expected_type_error_message) | |
| 50 | fi | |
| 51 | ||
| 52 | // Mark the callee so a reflected TYPE_GROUP resolves to its | |
| 53 | // generic member for construction rather than collapsing to | |
| 54 | // the (often non-constructible) arity-0 member. | |
| 55 | call.function.mark_call_target() | |
| 56 | ||
| 57 | // Function walks outside the speculation level that | |
| 58 | // `visit_call` rolls back during constraint-push | |
| 59 | // retry, so diagnostics from sub-walks of the function | |
| 60 | // expression aren't scrubbed alongside first-pass | |
| 61 | // overload noise. | |
| 62 | if !defer_callee then | |
| 63 | call.function.walk(self) | |
| 64 | fi | |
| 65 | ||
| 66 | RETRY_SITE_STATS.note("references.call_arguments") | |
| 67 | _logger.speculate() | |
| 68 | ||
| 69 | // Snapshot the pre-argument narrowing facts so an | |
| 70 | // overload-retry re-walk resets to what the first walk | |
| 71 | // saw; on success the final walk's facts are kept. | |
| 72 | let use flow_speculation = _flow.speculate_then_commit() | |
| 73 | ||
| 74 | call.arguments.walk(self) | |
| 75 | ||
| 76 | if defer_callee then | |
| 77 | _walk_deferred_callee(call) | |
| 78 | fi | |
| 79 | ||
| 80 | _calls.visit_call(call) | |
| 81 | ||
| 82 | // A terminal Pipe consumer (`.count()`, ...) over a | |
| 83 | // fusible chain lowers to an inline driving loop instead | |
| 84 | // of building the pipe objects and iterating them. | |
| 85 | // Both checks below read the call the source wrote rather | |
| 86 | // than whatever it lowered to. Fusing changes how the | |
| 87 | // callback is reached, not whether it can store, and the | |
| 88 | // fused loop is not a call shape either of them | |
| 89 | // recognises: asking them about it silently answers that | |
| 90 | // nothing was called, which loses the crossing a narrowed | |
| 91 | // field is relying on. | |
| 92 | let as_written = call.value | |
| 93 | ||
| 94 | _undisposed_sources.check_call(call, as_written) | |
| 95 | ||
| 96 | let fused = _recognize_consumer_fusion(call) | |
| 97 | ||
| 98 | if fused? then | |
| 99 | call.compile_expressions_state.value = fused | |
| 100 | fi | |
| 101 | ||
| 102 | _check_pure_slots(call.location, as_written, call.arguments) | |
| 103 | _note_call(call.location, as_written) | |
| 104 | ||
| 105 | _logger.commit() | |
| 106 | catch e: Exception | |
| 107 | call.compile_expressions_state.value = DUMMY(Semantic.Types.ERROR(), call.location) | |
| 108 | _logger.release(mark) | |
| 109 | ||
| 110 | _logger.exception(call.location, e, "exception compiling call") | |
| 111 | yrt | |
| 112 | si | |
| 113 | ||
| 114 | // A callee whose own type comes from its context - `cast(v)` | |
| 115 | // with the target elided - can only take it from the call it | |
| 116 | // heads, and the call determines it completely: the arguments | |
| 117 | // give the parameter types and the call's own expected type | |
| 118 | // gives the return type. That is why such a callee is walked | |
| 119 | // here rather than before the arguments: the type it is waiting | |
| 120 | // for is not known until they have been. With no expected type | |
| 121 | // on the call there is no return type to supply, so the callee | |
| 122 | // walks unaided and reports that it cannot infer one. | |
| 123 | _walk_deferred_callee(call: Trees.Expressions.CALL) is | |
| 124 | let function_type = _try_get_callee_function_type(call) | |
| 125 | ||
| 126 | if function_type? then | |
| 127 | call.function.set_expected_type( | |
| 128 | function_type, | |
| 129 | "{{0}} is not assignable to {{1}}" | |
| 130 | ) | |
| 131 | fi | |
| 132 | ||
| 133 | call.function.walk(self) | |
| 134 | si | |
| 135 | ||
| 136 | // The function type a call determines for its own callee: | |
| 137 | // parameters from the walked arguments, return type from the | |
| 138 | // call's expected type. Null when either is unavailable. | |
| 139 | _try_get_callee_function_type(call: Trees.Expressions.CALL) -> Semantic.Types.Type? is | |
| 140 | let return_type = call.expected_type | |
| 141 | ||
| 142 | if !return_type? then | |
| 143 | return null | |
| 144 | fi | |
| 145 | ||
| 146 | let types = Collections.LIST[Semantic.Types.Type]() | |
| 147 | ||
| 148 | for a in call.arguments.expressions do | |
| 149 | let value = a.value | |
| 150 | ||
| 151 | if !value? then | |
| 152 | return null | |
| 153 | fi | |
| 154 | ||
| 155 | let type = value.type | |
| 156 | ||
| 157 | if !type? \/ type.is_error \/ !type.is_settled then | |
| 158 | return null | |
| 159 | fi | |
| 160 | ||
| 161 | types.add(type) | |
| 162 | od | |
| 163 | ||
| 164 | types.add(return_type) | |
| 165 | ||
| 166 | return _innate_symbol_lookup.get_function_type(types) | |
| 167 | si | |
| 168 | ||
| 169 | visit(identifier: Trees.Expressions.IDENTIFIER) is | |
| 170 | let mark = _logger.mark() | |
| 171 | ||
| 172 | try | |
| 173 | _access.visit_identifier(identifier) | |
| 174 | _note_call(identifier.location, identifier.value) | |
| 175 | ||
| 176 | catch ex: Exception | |
| 177 | _logger.exception(identifier.location, ex, "something went wrong with identifier") | |
| 178 | ||
| 179 | finally | |
| 180 | _logger.release(mark) | |
| 181 | yrt | |
| 182 | si | |
| 183 | ||
| 184 | visit(has_value: Trees.Expressions.HAS_VALUE) is | |
| 185 | // Stamp the log position the test is walked at, so facts | |
| 186 | // formed from it skip the calls that ran before it. | |
| 187 | _flow.note_test_site(has_value) | |
| 188 | ||
| 189 | _access.visit_has_value(has_value) | |
| 190 | _note_call(has_value.location, has_value.value) | |
| 191 | si | |
| 192 | ||
| 193 | visit(unwrap: Trees.Expressions.UNWRAP) is | |
| 194 | _access.visit_unwrap(unwrap) | |
| 195 | _note_call(unwrap.location, unwrap.value) | |
| 196 | si | |
| 197 | ||
| 198 | // An operand of `ref` is an address, not a value read. Suppress | |
| 199 | // the operand's definite-assignment check by recording its target | |
| 200 | // for the identifier load to skip, and reset the write flag so it | |
| 201 | // is re-derived from scratch this walk (robust to speculative | |
| 202 | // retry and inference iteration, which the flag does not otherwise | |
| 203 | // participate in). The resolved call does the real read-check and | |
| 204 | // assignment in `note_reference_arguments`. | |
| 205 | pre(reference: Trees.Expressions.REFERENCE) -> bool is | |
| 206 | reference.writes_target = false | |
| 207 | _reference_operand_target = try_get_narrowing_target(reference.left) | |
| 208 | ||
| 209 | return false | |
| 210 | si | |
| 211 | ||
| 212 | visit(reference: Trees.Expressions.REFERENCE) is | |
| 213 | IoC.CONTAINER.instance.capability_checker.check( | |
| 214 | Semantic.Capability.REFERENCE, reference.location, "references") | |
| 215 | ||
| 216 | _reference_operand_target = null | |
| 217 | _access.visit_reference(reference) | |
| 218 | si | |
| 219 | ||
| 220 | // True while walking the operand of the `ref` that writes to | |
| 221 | // `symbol` — consulted by the identifier load to skip the | |
| 222 | // definite-assignment check on an address-of operand. | |
| 223 | is_reference_operand_target(symbol: Semantic.Symbols.Symbol) -> bool => | |
| 224 | _reference_operand_target? /\ symbol == _reference_operand_target | |
| 225 | ||
| 226 | // Definite assignment for `ref` arguments, run once the call is | |
| 227 | // resolved so the matched parameter's direction is known. A slot | |
| 228 | // the callee reads (any by-ref except pure `out`) requires the | |
| 229 | // target to be assigned already; a slot it writes (any by-ref | |
| 230 | // except pure `in`) assigns it. The write flag is recorded on the | |
| 231 | // REFERENCE so the condition analyzer can carry that assignment | |
| 232 | // onto the branch edges a `ref` in a condition reaches. | |
| 233 | note_reference_arguments(call: Trees.Expressions.CALL, function: Semantic.Symbols.Function) is | |
| 234 | if call.argument_names? then | |
| 235 | return | |
| 236 | fi | |
| 237 | ||
| 238 | for i in 0..call.arguments.count do | |
| 239 | let expr = call.arguments.expressions[i] | |
| 240 | ||
| 241 | if !isa Trees.Expressions.REFERENCE(expr) then | |
| 242 | continue | |
| 243 | fi | |
| 244 | ||
| 245 | let reference = expr | |
| 246 | let target = try_get_narrowing_target(reference.left) | |
| 247 | ||
| 248 | if !target? then | |
| 249 | continue | |
| 250 | fi | |
| 251 | ||
| 252 | if | |
| 253 | function.argument_reads(i) /\ | |
| 254 | !_build_flags.no_warn_definite_assignment /\ | |
| 255 | isa Semantic.Symbols.Variable(target) /\ | |
| 256 | _flow.is_tracked(target) /\ | |
| 257 | !_flow.is_assigned(target) | |
| 258 | then | |
| 259 | _logger.warn(reference.location, "definite-assignment", "{target.name} may be used before it is assigned", target.location, "variable declared here") | |
| 260 | fi | |
| 261 | ||
| 262 | reference.writes_target = function.argument_writes(i) | |
| 263 | ||
| 264 | if reference.writes_target then | |
| 265 | _flow.mark_assigned(target) | |
| 266 | fi | |
| 267 | od | |
| 268 | si | |
| 269 | ||
| 270 | visit(`null: Trees.Expressions.NULL) is | |
| 271 | // For a value-type `T?` target (NULLABLE[T]) `null` is the | |
| 272 | // empty Nullable — a zeroed value, not a reference. Lower | |
| 273 | // it as an IR.Values.DEFAULT; a plain ldnull would be | |
| 274 | // invalid IL against a value-type slot. Reference-type | |
| 275 | // targets keep the ordinary null reference. | |
| 276 | if let `null.expected_type? /\ expected_type.is_value_type /\ expected_type.is_optional then | |
| 277 | `null.compile_expressions_state.value = IR.Values.DEFAULT(expected_type) | |
| 278 | else | |
| 279 | `null.compile_expressions_state.value = NULL(Semantic.Types.NULL()) | |
| 280 | fi | |
| 281 | ||
| 282 | si | |
| 283 | ||
| 284 | visit(`default: Trees.Expressions.DEFAULT) is | |
| 285 | _literals.visit_default(`default) | |
| 286 | ||
| 287 | // Non-optional-by-default: `default` reaching a non-optional | |
| 288 | // reference slot resolves to null at runtime, the same hole | |
| 289 | // as a bare `null` literal. Same warning category and flag | |
| 290 | // as visit(NULL) — one migration, one flip. | |
| 291 | if | |
| 292 | !_build_flags.no_warn_non_optional /\ | |
| 293 | `default.value? /\ | |
| 294 | _is_non_optional_reference(`default.value.type) | |
| 295 | then | |
| 296 | let target = `default.value!.type | |
| 297 | _logger.warn(`default.location, "non-optional", "default where non-optional {target} expected") | |
| 298 | fi | |
| 299 | si | |
| 300 | ||
| 301 | si | |
| 302 | si |