Module: MilkTea::Lowering::Resolve
- Included in:
- MilkTea::Lowerer
- Defined in:
- lib/milk_tea/core/lowering/resolve.rb
Defined Under Namespace
Classes: ConstFnLowerEvaluator
Constant Summary collapse
- PASS_THROUGH_BUILTINS =
{ "fatal" => :fatal, "ref_of" => :ref_of, "const_ptr_of" => :const_ptr_of, "read" => :read, "ptr_of" => :ptr_of, "get" => :get, }.freeze
- COMPILE_TIME_BUILTINS =
%w[ field_of callable_of has_attribute attribute_of ].freeze
Instance Method Summary collapse
- #aggregate_arithmetic_result_type(operator, left_type, right_type) ⇒ Object
- #aggregate_scalar_result(aggregate_type, scalar_type) ⇒ Object
- #analysis_for_module(module_name) ⇒ Object
- #attribute_binding_supports_target?(binding, target) ⇒ Boolean
- #attribute_target_kind(target) ⇒ Object
- #binary_right_env(expression, env) ⇒ Object
- #binding_cstr_backed?(binding) ⇒ Boolean
- #binding_cstr_list_backed?(binding) ⇒ Boolean
- #build_direct_function_proc_invoke_function(source_expression, function_c_name, function_type, proc_type, invoke_c_name) ⇒ Object
- #builtin_attribute_binding(name) ⇒ Object
- #cast_expression(expression, target_type) ⇒ Object
- #collect_type_substitutions(pattern_type, actual_type, substitutions, function_name) ⇒ Object
- #compile_time_const_value(expression, env: nil) ⇒ Object
- #compile_time_numeric_const_expression?(expression, env: nil) ⇒ Boolean
- #const_declaration_for_module(module_name, name) ⇒ Object
- #contextual_numeric_compatibility?(expression, actual_type, expected_type, env:, external_numeric: false, contextual_int_to_float: false) ⇒ Boolean
- #copy_cstr_metadata!(target_env, source_env) ⇒ Object
- #cstr_backed_expression?(expression, env) ⇒ Boolean
- #cstr_backed_storage_value?(type, expression, env) ⇒ Boolean
- #cstr_list_backed_expression?(expression, env) ⇒ Boolean
- #cstr_list_backed_storage_value?(type, expression, env) ⇒ Boolean
- #cstr_metadata_exit_envs_for_if_statement(statement, env) ⇒ Object
- #current_type_params ⇒ Object
- #direct_function_identity_expression?(expression, env) ⇒ Boolean
- #direct_function_to_proc_contextual_compatibility?(expression, actual_type, env:, expected_type:) ⇒ Boolean
- #each_non_raw_module_analysis(&block) ⇒ Object
- #each_raw_module_analysis(&block) ⇒ Object
- #evaluate_attribute_arg_call(arguments, env:) ⇒ Object
- #evaluate_attribute_of_call(arguments, env:) ⇒ Object
- #evaluate_attributes_of_call(arguments, env:) ⇒ Object
- #evaluate_callable_of_call(arguments) ⇒ Object
- #evaluate_compile_time_call(expression, env:) ⇒ Object
- #evaluate_const_function_body_lower(func, arguments) ⇒ Object
- #evaluate_field_of_call(arguments, env:) ⇒ Object
- #evaluate_fields_of_call(arguments, env:) ⇒ Object
- #evaluate_has_attribute_call(arguments, env:) ⇒ Object
- #evaluate_members_of_call(arguments, env:) ⇒ Object
- #evaluate_reflection_target_argument(expression, env:) ⇒ Object
- #evaluate_type_returning_call(expression, env:) ⇒ Object
- #exact_compile_time_numeric_compatibility?(actual_type, expression, expected_type, env: nil) ⇒ Boolean
- #external_numeric_assignment_target?(expression, env:) ⇒ Boolean
- #extract_type_callee_info(expression) ⇒ Object
- #ffi_external_layout_root_type(type) ⇒ Object
- #find_attribute_application(target, binding) ⇒ Object
- #find_struct_decl_by_name(declarations, name) ⇒ Object
- #float_literal_expression?(expression) ⇒ Boolean
- #foreign_identity_projection_expression(expression, target_type) ⇒ Object
- #function_type_for_name(name) ⇒ Object
- #harmonize_binary_float_literal_types(left_expression, right_expression, left_type, right_type, env:) ⇒ Object
- #harmonize_binary_integer_literal_types(left_expression, right_expression, left_type, right_type, env:) ⇒ Object
- #imports_for_module(module_name) ⇒ Object
- #infer_binary_operand_types(expression, env:, expected_type: nil) ⇒ Object
- #infer_expression_type(expression, env:, expected_type: nil) ⇒ Object
- #infer_field_handle_member_type(expression) ⇒ Object
- #infer_function_type_arguments(binding, arguments, env, receiver_type: nil) ⇒ Object
- #infer_member_handle_member_type(expression) ⇒ Object
- #infer_null_literal_type(expression, expected_type) ⇒ Object
- #infer_receiver_type_substitutions(binding, receiver_type) ⇒ Object
- #instantiate_function_binding(binding, type_arguments) ⇒ Object
- #instantiate_function_binding_with_receiver(binding, explicit_type_arguments, receiver_type: nil) ⇒ Object
- #integer_literal_expression?(expression) ⇒ Boolean
- #interface_implementation_key(type) ⇒ Object
- #literal_type_argument_name_candidate?(type_ref) ⇒ Boolean
- #lower_array_to_span_expression(expression, target_type) ⇒ Object
- #lower_fn_to_proc_expression(source_expression, source_function, env:, expected_type:) ⇒ Object
- #lower_str_buffer_to_span_expression(expression, target_type) ⇒ Object
- #merge_cstr_metadata_after_if_statement!(statement, env) ⇒ Object
- #method_analysis_key(method_ast) ⇒ Object
- #methods_receiver_type_argument_names!(type_ref) ⇒ Object
- #pointer_arithmetic_result_type(operator, left_type, right_type) ⇒ Object
- #pointer_lowered_method_receiver?(callee_type, callee_binding) ⇒ Boolean
- #pointer_lowered_sync_method_receiver?(binding) ⇒ Boolean
- #promoted_binary_operand_type(operator, left_type, right_type) ⇒ Object
- #propagating_expected_type(operator, expected_type) ⇒ Object
- #receiver_type_uses_pointer_lowering?(type) ⇒ Boolean
- #record_external_layout_assertion(source_type, target_type) ⇒ Object
- #reflection_identifier_name(expression) ⇒ Object
- #reflection_positional_arguments?(arguments, expected_length) ⇒ Boolean
- #reflection_type_from_expression(expression, env:) ⇒ Object
- #reinterpret_expression(expression, target_type) ⇒ Object
- #replace_binding_cstr_metadata!(name, env, cstr_backed:, cstr_list_backed:) ⇒ Object
- #resolve_attribute_name_argument(expression) ⇒ Object
- #resolve_callable_handle_argument(expression) ⇒ Object
- #resolve_callee(callee, env, arguments: nil) ⇒ Object
- #resolve_current_module_const_value(name) ⇒ Object
- #resolve_default_specialization(expression, env:) ⇒ Object
- #resolve_equal_specialization(expression, env:) ⇒ Object
- #resolve_explicit_associated_binding(target_type, method_name, requirement_message:) {|method_binding, method_analysis, method_entry_receiver_type| ... } ⇒ Object
- #resolve_explicit_default_binding(target_type, context:) ⇒ Object
- #resolve_explicit_equal_binding(target_type, context:) ⇒ Object
- #resolve_explicit_format_append_binding(target_type, context:) ⇒ Object
- #resolve_explicit_format_binding(target_type, context:) ⇒ Object
- #resolve_explicit_format_len_binding(target_type, context:) ⇒ Object
- #resolve_explicit_hash_binding(target_type, context:) ⇒ Object
- #resolve_explicit_instance_binding(target_type, method_name, requirement_message:) {|method_binding, method_analysis, method_entry_receiver_type| ... } ⇒ Object
- #resolve_explicit_order_binding(target_type, context:) ⇒ Object
- #resolve_expression_callee(callee, env) ⇒ Object
-
#resolve_field_handle_type_ref(parts) ⇒ Object
Resolves a bare dotted reflection type ref
field.type(wherefieldis a compile-timefield_handlebound in the active inline-for env) to the field's concrete type. - #resolve_hash_specialization(expression, env:) ⇒ Object
- #resolve_identifier_callee(callee, env, arguments) ⇒ Object
- #resolve_imported_module_const_value(import_name, value_name) ⇒ Object
- #resolve_interface_ref(interface_ref) ⇒ Object
- #resolve_member_access_callee(callee, env, arguments) ⇒ Object
- #resolve_named_generic_type(parts) ⇒ Object
- #resolve_named_generic_type_for_analysis(analysis, parts) ⇒ Object
- #resolve_named_literal_type_argument(type_ref) ⇒ Object
- #resolve_nested_type_ref(parts) ⇒ Object
- #resolve_order_specialization(expression, env:) ⇒ Object
- #resolve_specialization_callee(callee, env) ⇒ Object
- #resolve_specialization_type_arguments(expression) ⇒ Object
- #resolve_specialized_callable_binding(expression, env:) ⇒ Object
- #resolve_struct_handle_argument(expression, env:) ⇒ Object
- #resolve_type_argument(argument, type_params: current_type_params) ⇒ Object
- #resolve_type_argument_ref(type_ref, type_params:) ⇒ Object
- #resolve_type_expression(expression) ⇒ Object
- #resolve_type_member(type, name) ⇒ Object
- #resolve_type_member_const_value(expression) ⇒ Object
- #resolve_type_ref(type_ref, type_params: current_type_params) ⇒ Object
- #resolve_type_ref_for_analysis(type_ref, analysis, type_params: current_type_params) ⇒ Object
- #resolved_attribute_applications_for_target(target) ⇒ Object
- #same_attribute_binding?(left, right) ⇒ Boolean
- #simulate_cstr_metadata_block(statements, env:) ⇒ Object
- #specialize_function_binding(binding, arguments, env, receiver_type: nil) ⇒ Object
- #struct_handle_for_type(type) ⇒ Object
- #substitute_type(type, substitutions) ⇒ Object
- #substitute_value_binding(binding, substitutions) ⇒ Object
- #trackable_binding_names(env) ⇒ Object
- #try_resolve_type_ref(type_ref, type_params:) ⇒ Object
- #type_contains_array_storage?(type) ⇒ Boolean
- #type_implements_interface?(type, interface) ⇒ Boolean
- #types_for_module(module_name) ⇒ Object
- #update_cstr_metadata_for_assignment!(statement, prepared_value, env) ⇒ Object
- #validate_function_type_param_constraints!(binding, substitutions) ⇒ Object
- #validate_methods_receiver_type_arguments!(type_ref, generic_type) ⇒ Object
- #value_param_declared_type(name) ⇒ Object
- #value_param_literal(name, expected_type = nil) ⇒ Object
- #value_param_substitution(name) ⇒ Object
Instance Method Details
#aggregate_arithmetic_result_type(operator, left_type, right_type) ⇒ Object
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1123 def aggregate_arithmetic_result_type(operator, left_type, right_type) if left_type.is_a?(Types::Vector) && right_type.is_a?(Types::Vector) && left_type.name == right_type.name return left_type end if left_type.is_a?(Types::Matrix) && right_type.is_a?(Types::Matrix) && left_type.name == right_type.name return left_type end if left_type.is_a?(Types::Quaternion) && right_type.is_a?(Types::Quaternion) return left_type end scalar_result = aggregate_scalar_result(left_type, right_type) return scalar_result if scalar_result case operator when "+", "-" nil when "*", "/" aggregate_scalar_result(right_type, left_type) else nil end end |
#aggregate_scalar_result(aggregate_type, scalar_type) ⇒ Object
1147 1148 1149 1150 1151 1152 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1147 def aggregate_scalar_result(aggregate_type, scalar_type) return nil unless aggregate_type.is_a?(Types::Vector) || aggregate_type.is_a?(Types::Matrix) return nil unless scalar_type.is_a?(Types::Primitive) && scalar_type.numeric? aggregate_type end |
#analysis_for_module(module_name) ⇒ Object
2349 2350 2351 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2349 def analysis_for_module(module_name) @program.analyses_by_module_name.fetch(module_name) end |
#attribute_binding_supports_target?(binding, target) ⇒ Boolean
1934 1935 1936 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1934 def attribute_binding_supports_target?(binding, target) binding && target && binding.targets.include?(attribute_target_kind(target)) end |
#attribute_target_kind(target) ⇒ Object
1938 1939 1940 1941 1942 1943 1944 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1938 def attribute_target_kind(target) case target when Types::StructHandle then :struct when Types::FieldHandle then :field when Types::CallableHandle then :callable end end |
#binary_right_env(expression, env) ⇒ Object
962 963 964 965 966 967 968 969 970 971 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 962 def binary_right_env(expression, env) case expression.operator when "and" env_with_refinements(env, flow_refinements(expression.left, truthy: true, env:)) when "or" env_with_refinements(env, flow_refinements(expression.left, truthy: false, env:)) else env end end |
#binding_cstr_backed?(binding) ⇒ Boolean
381 382 383 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 381 def binding_cstr_backed?(binding) binding && binding[:cstr_backed] end |
#binding_cstr_list_backed?(binding) ⇒ Boolean
385 386 387 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 385 def binding_cstr_list_backed?(binding) binding && binding[:cstr_list_backed] end |
#build_direct_function_proc_invoke_function(source_expression, function_c_name, function_type, proc_type, invoke_c_name) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 77 def build_direct_function_proc_invoke_function(source_expression, function_c_name, function_type, proc_type, invoke_c_name) env = empty_env params = [IR::Param.new(name: "env", linkage_name: "__mt_proc_env", type: proc_env_pointer_type, pointer: false)] parameter_setup = [] call_arguments = [] proc_type.params.each_with_index do |param, index| linkage_name = c_local_name(param.name || "arg#{index}") if array_type?(param.type) input_linkage_name = "#{linkage_name}_input" params << IR::Param.new(name: param.name || "arg#{index}", linkage_name: input_linkage_name, type: param.type, pointer: false) env[:scopes].last[param.name || "arg#{index}"] = local_binding(type: param.type, linkage_name:, mutable: param.mutable, pointer: false) parameter_setup << IR::LocalDecl.new( name: param.name || "arg#{index}", linkage_name:, type: param.type, value: IR::Name.new(name: input_linkage_name, type: param.type, pointer: false), ) call_arguments << IR::Name.new(name: linkage_name, type: param.type, pointer: false) else env[:scopes].last[param.name || "arg#{index}"] = local_binding(type: param.type, linkage_name:, mutable: param.mutable, pointer: false) params << IR::Param.new(name: param.name || "arg#{index}", linkage_name:, type: param.type, pointer: false) call_arguments << IR::Name.new(name: linkage_name, type: param.type, pointer: false) end end call = IR::Call.new(callee: function_c_name, arguments: call_arguments, type: proc_type.return_type) body = if proc_type.return_type == @ctx.types.fetch("void") parameter_setup + [IR::ExpressionStmt.new(expression: call), IR::ReturnStmt.new(value: nil)] else parameter_setup + [IR::ReturnStmt.new(value: call)] end IR::Function.new(name: invoke_c_name, linkage_name: invoke_c_name, params:, return_type: proc_type.return_type, body:, entry_point: false) end |
#builtin_attribute_binding(name) ⇒ Object
1975 1976 1977 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1975 def builtin_attribute_binding(name) Bindings.builtin_attribute_binding(name, @ctx.types) end |
#cast_expression(expression, target_type) ⇒ Object
1031 1032 1033 1034 1035 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1031 def cast_expression(expression, target_type) return expression if expression.type == target_type IR::Cast.new(target_type:, expression:, type: target_type) end |
#collect_type_substitutions(pattern_type, actual_type, substitutions, function_name) ⇒ Object
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2244 def collect_type_substitutions(pattern_type, actual_type, substitutions, function_name) case pattern_type when Types::TypeVar existing = substitutions[pattern_type.name] if existing && existing != actual_type raise LoweringError.new("conflicting type argument #{pattern_type.name} for function #{function_name}: got #{existing} and #{actual_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions[pattern_type.name] ||= actual_type when Types::Nullable candidate = actual_type.is_a?(Types::Nullable) ? actual_type.base : actual_type collect_type_substitutions(pattern_type.base, candidate, substitutions, function_name) when Types::GenericInstance if ref_type?(pattern_type) && !ref_type?(actual_type) collect_type_substitutions(referenced_type(pattern_type), actual_type, substitutions, function_name) return end if own_type?(actual_type) && (mutable_pointer_type?(pattern_type) || const_pointer_type?(pattern_type)) collect_type_substitutions(pointee_type(pattern_type), owned_referent_type(actual_type), substitutions, function_name) return end return unless actual_type.is_a?(Types::GenericInstance) return unless actual_type.name == pattern_type.name && actual_type.arguments.length == pattern_type.arguments.length pattern_type.arguments.zip(actual_type.arguments).each do |expected_argument, actual_argument| next if expected_argument.is_a?(Types::LiteralTypeArg) collect_type_substitutions(expected_argument, actual_argument, substitutions, function_name) end when Types::Span return unless actual_type.is_a?(Types::Span) collect_type_substitutions(pattern_type.element_type, actual_type.element_type, substitutions, function_name) when Types::Task return unless actual_type.is_a?(Types::Task) collect_type_substitutions(pattern_type.result_type, actual_type.result_type, substitutions, function_name) when Types::Proc if task_root_proc_type?(pattern_type) && actual_type.is_a?(Types::Task) collect_type_substitutions(pattern_type.return_type, actual_type, substitutions, function_name) return end actual_params = case actual_type when Types::Proc return unless actual_type.params.length == pattern_type.params.length actual_type.params when Types::Function return if actual_type.receiver_type || actual_type.variadic return unless actual_type.params.length == pattern_type.params.length return unless actual_type.params.zip(pattern_type.params).all? { |actual_param, expected_param| actual_param.mutable == expected_param.mutable } actual_type.params else return end pattern_type.params.zip(actual_params).each do |expected_param, actual_param| collect_type_substitutions(expected_param.type, actual_param.type, substitutions, function_name) end collect_type_substitutions(pattern_type.return_type, actual_type.return_type, substitutions, function_name) when Types::StructInstance return unless actual_type.is_a?(Types::StructInstance) return unless actual_type.definition == pattern_type.definition && actual_type.arguments.length == pattern_type.arguments.length pattern_type.arguments.zip(actual_type.arguments).each do |expected_argument, actual_argument| collect_type_substitutions(expected_argument, actual_argument, substitutions, function_name) end when Types::VariantInstance return unless actual_type.is_a?(Types::VariantInstance) return unless actual_type.definition == pattern_type.definition && actual_type.arguments.length == pattern_type.arguments.length pattern_type.arguments.zip(actual_type.arguments).each do |expected_argument, actual_argument| collect_type_substitutions(expected_argument, actual_argument, substitutions, function_name) end when Types::Function return unless actual_type.is_a?(Types::Function) return unless actual_type.params.length == pattern_type.params.length pattern_type.params.zip(actual_type.params).each do |expected_param, actual_param| collect_type_substitutions(expected_param.type, actual_param.type, substitutions, function_name) end collect_type_substitutions(pattern_type.return_type, actual_type.return_type, substitutions, function_name) end end |
#compile_time_const_value(expression, env: nil) ⇒ Object
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1535 def compile_time_const_value(expression, env: nil) CompileTime.evaluate( expression, resolve_identifier: lambda do |identifier_expression| if env binding = lookup_value(identifier_expression.name, env) return binding[:const_value] unless binding&.fetch(:const_value, nil).nil? end value = resolve_current_module_const_value(identifier_expression.name) return value if value # Resolve a bare type-parameter name (e.g. `T`) to its substituted # concrete type so `inline if T == int` folds during lowering too. current_type_params[identifier_expression.name] || @ctx.types[identifier_expression.name] end, resolve_member_access: lambda do |member_access_expression| if (receiver_value = CompileTime.evaluate( member_access_expression.receiver, resolve_identifier: lambda do |identifier_expression| if env binding = lookup_value(identifier_expression.name, env) return binding[:const_value] unless binding&.fetch(:const_value, nil).nil? end resolve_current_module_const_value(identifier_expression.name) end, resolve_member_access: lambda { |ma| nil }, resolve_type_ref: lambda { |tr| resolve_type_ref(tr) }, resolve_call: lambda { |ce| evaluate_compile_time_call(ce, env:) }, )) case receiver_value when Types::FieldHandle case member_access_expression.member when "name" then next receiver_value.field_name when "type" then next receiver_value.struct_handle.struct_type.field(receiver_value.field_name) end when Types::MemberHandle case member_access_expression.member when "name" then next receiver_value.member_name when "value" then next receiver_value.member_value end end end value = if member_access_expression.receiver.is_a?(AST::Identifier) resolve_imported_module_const_value(member_access_expression.receiver.name, member_access_expression.member) end next value unless value.nil? resolve_type_member_const_value(member_access_expression) end, resolve_type_ref: lambda do |type_ref| resolve_type_ref(type_ref) end, resolve_call: lambda do |call_expression| evaluate_compile_time_call(call_expression, env:) end, ) end |
#compile_time_numeric_const_expression?(expression, env: nil) ⇒ Boolean
1530 1531 1532 1533 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1530 def compile_time_numeric_const_expression?(expression, env: nil) value = compile_time_const_value(expression, env:) value.is_a?(Integer) || value.is_a?(Float) end |
#const_declaration_for_module(module_name, name) ⇒ Object
2371 2372 2373 2374 2375 2376 2377 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2371 def const_declaration_for_module(module_name, name) analysis = @program.analyses_by_module_name.fetch(module_name) declaration = analysis.ast.declarations.find { |decl| decl.is_a?(AST::ConstDecl) && decl.name == name } raise LoweringError.new("unknown constant #{analysis.module_name}.#{name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless declaration declaration end |
#contextual_numeric_compatibility?(expression, actual_type, expected_type, env:, external_numeric: false, contextual_int_to_float: false) ⇒ Boolean
163 164 165 166 167 168 169 170 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 163 def contextual_numeric_compatibility?(expression, actual_type, expected_type, env:, external_numeric: false, contextual_int_to_float: false) return true if exact_compile_time_numeric_compatibility?(actual_type, expression, expected_type, env:) return true if integer_to_char_compatibility?(actual_type, expected_type) return true if external_numeric && external_numeric_compatibility?(actual_type, expected_type) return true if contextual_int_to_float && contextual_int_to_float_compatibility?(actual_type, expected_type) false end |
#copy_cstr_metadata!(target_env, source_env) ⇒ Object
344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 344 def (target_env, source_env) trackable_binding_names(target_env).each do |name| binding = lookup_value(name, target_env) source_binding = lookup_value(name, source_env) next unless binding && source_binding ( name, target_env, cstr_backed: binding_cstr_backed?(source_binding), cstr_list_backed: binding_cstr_list_backed?(source_binding), ) end end |
#cstr_backed_expression?(expression, env) ⇒ Boolean
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 172 def cstr_backed_expression?(expression, env) return true if infer_expression_type(expression, env:) == @ctx.types.fetch("cstr") case expression when AST::StringLiteral true when AST::Identifier binding_cstr_backed?(lookup_value(expression.name, env)) when AST::IfExpr then_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: true, env:)) else_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: false, env:)) cstr_backed_expression?(expression.then_expression, then_env) && cstr_backed_expression?(expression.else_expression, else_env) when AST::UnsafeExpr cstr_backed_expression?(expression.expression, env) else false end rescue LoweringError false end |
#cstr_backed_storage_value?(type, expression, env) ⇒ Boolean
220 221 222 223 224 225 226 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 220 def cstr_backed_storage_value?(type, expression, env) return false unless expression return true if type == @ctx.types.fetch("cstr") return false unless type == @ctx.types.fetch("str") cstr_backed_expression?(expression, env) end |
#cstr_list_backed_expression?(expression, env) ⇒ Boolean
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 194 def cstr_list_backed_expression?(expression, env) actual_type = infer_expression_type(expression, env:) return false unless array_type?(actual_type) element_type = array_element_type(actual_type) return false unless element_type == @ctx.types.fetch("str") || element_type == @ctx.types.fetch("cstr") case expression when AST::Identifier binding_cstr_list_backed?(lookup_value(expression.name, env)) when AST::Call expression.arguments.all? { |argument| cstr_backed_expression?(argument.value, env) } when AST::IfExpr then_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: true, env:)) else_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: false, env:)) cstr_list_backed_expression?(expression.then_expression, then_env) && cstr_list_backed_expression?(expression.else_expression, else_env) when AST::UnsafeExpr cstr_list_backed_expression?(expression.expression, env) else false end rescue LoweringError false end |
#cstr_list_backed_storage_value?(type, expression, env) ⇒ Boolean
228 229 230 231 232 233 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 228 def cstr_list_backed_storage_value?(type, expression, env) return false unless expression return false unless cstr_list_trackable_type?(type) cstr_list_backed_expression?(expression, env) end |
#cstr_metadata_exit_envs_for_if_statement(statement, env) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 274 def (statement, env) false_refinements = {} exit_envs = [] statement.branches.each do |branch| branch_env = env_with_refinements(env, false_refinements) true_refinements = merge_refinements(false_refinements, flow_refinements(branch.condition, truthy: true, env: branch_env)) simulated = (branch.body, env: env_with_refinements(env, true_refinements)) exit_envs << simulated if simulated false_refinements = merge_refinements(false_refinements, flow_refinements(branch.condition, truthy: false, env: branch_env)) end if statement.else_body simulated = (statement.else_body, env: env_with_refinements(env, false_refinements)) exit_envs << simulated if simulated else exit_envs << env end exit_envs end |
#current_type_params ⇒ Object
2388 2389 2390 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2388 def current_type_params @ctx.current_type_substitutions || {} end |
#direct_function_identity_expression?(expression, env) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 27 def direct_function_identity_expression?(expression, env) case expression when AST::Identifier return false if lookup_value(expression.name, env) return false unless @ctx.functions.key?(expression.name) binding = @ctx.functions.fetch(expression.name) !binding.type_params.any? && !foreign_function_binding?(binding) when AST::MemberAccess return false unless expression.receiver.is_a?(AST::Identifier) && @ctx.imports.key?(expression.receiver.name) imported_module = @ctx.imports.fetch(expression.receiver.name) return false unless imported_module.functions.key?(expression.member) binding = imported_module.functions.fetch(expression.member) !binding.type_params.any? && !foreign_function_binding?(binding) when AST::Specialization callable_resolution = resolve_specialized_callable_binding(expression, env:) return false unless callable_resolution callable_kind, binding, = callable_resolution callable_kind == :function && !foreign_function_binding?(binding) else false end end |
#direct_function_to_proc_contextual_compatibility?(expression, actual_type, env:, expected_type:) ⇒ Boolean
20 21 22 23 24 25 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 20 def direct_function_to_proc_contextual_compatibility?(expression, actual_type, env:, expected_type:) return false unless actual_type.is_a?(Types::Function) && proc_type?(expected_type) return false unless direct_function_identity_expression?(expression, env) function_type_matches_proc_type?(actual_type, expected_type) end |
#each_non_raw_module_analysis(&block) ⇒ Object
2358 2359 2360 2361 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2358 def each_non_raw_module_analysis(&block) return @program.analyses_by_module_name.each_value.reject { |a| a.module_kind == :raw_module }.each unless block @program.analyses_by_module_name.each_value { |a| block.call(a) unless a.module_kind == :raw_module } end |
#each_raw_module_analysis(&block) ⇒ Object
2353 2354 2355 2356 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2353 def each_raw_module_analysis(&block) return @program.analyses_by_module_name.each_value.select { |a| a.module_kind == :raw_module }.each unless block @program.analyses_by_module_name.each_value { |a| block.call(a) if a.module_kind == :raw_module } end |
#evaluate_attribute_arg_call(arguments, env:) ⇒ Object
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1773 def evaluate_attribute_arg_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 2) attribute_handle = compile_time_const_value(arguments.first.value, env:) return nil unless attribute_handle.is_a?(Types::AttributeHandle) param_name = reflection_identifier_name(arguments[1].value) return nil unless param_name && attribute_handle.argument_values attribute_handle.argument_values[param_name] end |
#evaluate_attribute_of_call(arguments, env:) ⇒ Object
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1754 def evaluate_attribute_of_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 2) target = evaluate_reflection_target_argument(arguments.first.value, env:) binding = resolve_attribute_name_argument(arguments[1].value) return nil unless attribute_binding_supports_target?(binding, target) application = find_attribute_application(target, binding) return nil unless application Types::AttributeHandle.new( binding.name, binding.module_name, target, binding.params, application.argument_values, ) end |
#evaluate_attributes_of_call(arguments, env:) ⇒ Object
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1707 def evaluate_attributes_of_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 1) || reflection_positional_arguments?(arguments, 2) target = evaluate_reflection_target_argument(arguments.first.value, env:) return nil unless target if arguments.length == 2 attribute_binding = resolve_attribute_name_argument(arguments[1].value) application = find_attribute_application(target, attribute_binding) return [] unless application [Types::AttributeHandle.new( attribute_binding.name, attribute_binding.module_name, target, attribute_binding.params, application.argument_values, )] else resolved_attribute_applications_for_target(target).map do |application| Types::AttributeHandle.new( application.binding.name, application.binding.module_name, target, application.binding.params, application.argument_values, ) end end end |
#evaluate_callable_of_call(arguments) ⇒ Object
1738 1739 1740 1741 1742 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1738 def evaluate_callable_of_call(arguments) return nil unless reflection_positional_arguments?(arguments, 1) resolve_callable_handle_argument(arguments.first.value) end |
#evaluate_compile_time_call(expression, env:) ⇒ Object
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1595 def evaluate_compile_time_call(expression, env:) case expression.callee when AST::Identifier case expression.callee.name when "field_of" evaluate_field_of_call(expression.arguments, env:) when "fields_of" evaluate_fields_of_call(expression.arguments, env:) when "callable_of" evaluate_callable_of_call(expression.arguments) when "has_attribute" evaluate_has_attribute_call(expression.arguments, env:) when "attribute_of" evaluate_attribute_of_call(expression.arguments, env:) when "members_of" evaluate_members_of_call(expression.arguments, env:) when "attributes_of" evaluate_attributes_of_call(expression.arguments, env:) else func = @ctx.functions[expression.callee.name] if func&.ast&.respond_to?(:const) && func.ast.const evaluate_const_function_body_lower(func, expression.arguments) else evaluate_type_returning_call(expression, env:) end end when AST::Specialization if expression.callee.callee.is_a?(AST::Identifier) && expression.callee.callee.name == "attribute_arg" evaluate_attribute_arg_call(expression.arguments, env:) else callee_name = expression.callee.callee.is_a?(AST::Identifier) ? expression.callee.callee.name : nil if callee_name == "array" values = [] expression.arguments.each do |argument| val = compile_time_const_value(argument.value, env:) return nil unless val values << val end values elsif callee_name func = @ctx.functions[callee_name] if func&.ast&.respond_to?(:const) && func.ast.const evaluate_const_function_body_lower(func, expression.arguments) else evaluate_type_returning_call(expression, env:) end else evaluate_type_returning_call(expression, env:) end end end end |
#evaluate_const_function_body_lower(func, arguments) ⇒ Object
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1785 def evaluate_const_function_body_lower(func, arguments) return nil unless func.ast.params.length == arguments.length initial_vars = {} func.ast.params.each_with_index do |param, idx| arg_expr = arguments[idx].value arg_value = compile_time_const_value(arg_expr, env: empty_env) return nil unless arg_value initial_vars[param.name] = arg_value end evaluator = ConstFnLowerEvaluator.new(self) ctx = CompileTime::BlockContext.new(evaluator, initial_variables: initial_vars) result = ctx.evaluate_block(func.ast.body, scopes: nil) result.is_a?(CompileTime::ReturnOutcome) ? result.value : result end |
#evaluate_field_of_call(arguments, env:) ⇒ Object
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1675 def evaluate_field_of_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 2) struct_handle = resolve_struct_handle_argument(arguments.first.value, env:) return nil unless struct_handle field_name = reflection_identifier_name(arguments[1].value) return nil unless field_name CompileTime::Reflection.core_field_handle(struct_handle, field_name) end |
#evaluate_fields_of_call(arguments, env:) ⇒ Object
1687 1688 1689 1690 1691 1692 1693 1694 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1687 def evaluate_fields_of_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 1) struct_handle = resolve_struct_handle_argument(arguments.first.value, env:) return nil unless struct_handle CompileTime::Reflection.core_field_handles(struct_handle) end |
#evaluate_has_attribute_call(arguments, env:) ⇒ Object
1744 1745 1746 1747 1748 1749 1750 1751 1752 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1744 def evaluate_has_attribute_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 2) target = evaluate_reflection_target_argument(arguments.first.value, env:) binding = resolve_attribute_name_argument(arguments[1].value) return nil unless attribute_binding_supports_target?(binding, target) !find_attribute_application(target, binding).nil? end |
#evaluate_members_of_call(arguments, env:) ⇒ Object
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1696 def evaluate_members_of_call(arguments, env:) return nil unless reflection_positional_arguments?(arguments, 1) type = resolve_type_expression(arguments.first.value) return nil unless type return nil unless type.is_a?(Types::Enum) || type.is_a?(Types::Flags) CompileTime::Reflection.core_member_handles(type) end |
#evaluate_reflection_target_argument(expression, env:) ⇒ Object
1821 1822 1823 1824 1825 1826 1827 1828 1829 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1821 def evaluate_reflection_target_argument(expression, env:) struct_handle = resolve_struct_handle_argument(expression, env:) return struct_handle if struct_handle value = compile_time_const_value(expression, env:) return value if value.is_a?(Types::FieldHandle) || value.is_a?(Types::CallableHandle) nil end |
#evaluate_type_returning_call(expression, env:) ⇒ Object
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1648 def evaluate_type_returning_call(expression, env:) callee_name, type_args = extract_type_callee_info(expression) return nil unless callee_name CompileTime::Reflection.core_evaluate_type_returning( callee_name, type_args, evaluate_value: ->(v) { compile_time_const_value(v, env:) }, resolve_type_ref: ->(tr) { resolve_type_ref(tr) }, pointer_to: ->(t) { pointer_to(t) }, const_pointer_to: ->(t) { const_pointer_to(t) }, top_level_functions: ->(name) { nil }, evaluate_type_returning_function_body: nil, ) end |
#exact_compile_time_numeric_compatibility?(actual_type, expression, expected_type, env: nil) ⇒ Boolean
389 390 391 392 393 394 395 396 397 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 389 def exact_compile_time_numeric_compatibility?(actual_type, expression, expected_type, env: nil) return false unless expected_type.is_a?(Types::Primitive) && expected_type.numeric? return false if actual_type.is_a?(Types::EnumBase) value = compile_time_const_value(expression, env:) return false unless value.is_a?(Numeric) numeric_constant_fits_type?(value, expected_type) end |
#external_numeric_assignment_target?(expression, env:) ⇒ Boolean
399 400 401 402 403 404 405 406 407 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 399 def external_numeric_assignment_target?(expression, env:) case expression when AST::MemberAccess receiver_type = infer_field_receiver_type(expression.receiver, env:) receiver_type.respond_to?(:external) && receiver_type.external else false end end |
#extract_type_callee_info(expression) ⇒ Object
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1663 def extract_type_callee_info(expression) if expression.is_a?(AST::Call) && expression.callee.is_a?(AST::Identifier) [expression.callee.name, nil] elsif expression.is_a?(AST::Specialization) if expression.callee.is_a?(AST::Identifier) [expression.callee.name, expression.arguments] elsif expression.callee.is_a?(AST::Specialization) && expression.callee.callee.is_a?(AST::Identifier) [expression.callee.callee.name, expression.callee.arguments] end end end |
#ffi_external_layout_root_type(type) ⇒ Object
1110 1111 1112 1113 1114 1115 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1110 def ffi_external_layout_root_type(type) type = type.base while type.is_a?(Types::Nullable) return pointee_type(type) if pointer_type?(type) type end |
#find_attribute_application(target, binding) ⇒ Object
1965 1966 1967 1968 1969 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1965 def find_attribute_application(target, binding) resolved_attribute_applications_for_target(target).find do |application| same_attribute_binding?(application.binding, binding) end end |
#find_struct_decl_by_name(declarations, name) ⇒ Object
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1878 def find_struct_decl_by_name(declarations, name) declarations.each do |decl| next unless decl.is_a?(AST::StructDecl) return decl if decl.name == name if decl.nested_types&.any? found = find_struct_decl_by_name(decl.nested_types, name) return found if found end end nil end |
#float_literal_expression?(expression) ⇒ Boolean
985 986 987 988 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 985 def float_literal_expression?(expression) expression.is_a?(AST::FloatLiteral) || (expression.is_a?(AST::UnaryOp) && ["+", "-"].include?(expression.operator) && float_literal_expression?(expression.operand)) end |
#foreign_identity_projection_expression(expression, target_type) ⇒ Object
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1072 def foreign_identity_projection_expression(expression, target_type) return expression if expression.type == target_type return cast_expression(expression, target_type) if foreign_identity_projection_cast_compatible?(expression.type, target_type) if foreign_identity_projection_reinterpret_compatible?(expression.type, target_type) record_external_layout_assertion(expression.type, target_type) return reinterpret_expression(expression, target_type) end nil end |
#function_type_for_name(name) ⇒ Object
1195 1196 1197 1198 1199 1200 1201 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1195 def function_type_for_name(name) binding = @ctx.functions.fetch(name) raise LoweringError.new("generic function #{name} cannot be used as a value", line: 0, column: 0, path: @ctx.current_analysis_path) if binding.type_params.any? raise LoweringError.new("foreign function #{name} cannot be used as a value", line: 0, column: 0, path: @ctx.current_analysis_path) if foreign_function_binding?(binding) binding.type end |
#harmonize_binary_float_literal_types(left_expression, right_expression, left_type, right_type, env:) ⇒ Object
973 974 975 976 977 978 979 980 981 982 983 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 973 def harmonize_binary_float_literal_types(left_expression, right_expression, left_type, right_type, env:) if float_literal_expression?(left_expression) && right_type.is_a?(Types::Primitive) && right_type.float? left_type = infer_expression_type(left_expression, env:, expected_type: right_type) end if float_literal_expression?(right_expression) && left_type.is_a?(Types::Primitive) && left_type.float? right_type = infer_expression_type(right_expression, env:, expected_type: left_type) end [left_type, right_type] end |
#harmonize_binary_integer_literal_types(left_expression, right_expression, left_type, right_type, env:) ⇒ Object
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 990 def harmonize_binary_integer_literal_types(left_expression, right_expression, left_type, right_type, env:) if integer_literal_expression?(left_expression) && right_type.is_a?(Types::Primitive) && right_type.integer? if exact_compile_time_numeric_compatibility?(left_type, left_expression, right_type, env:) left_type = infer_expression_type(left_expression, env:, expected_type: right_type) end end if integer_literal_expression?(right_expression) && left_type.is_a?(Types::Primitive) && left_type.integer? if exact_compile_time_numeric_compatibility?(right_type, right_expression, left_type, env:) right_type = infer_expression_type(right_expression, env:, expected_type: left_type) end end [left_type, right_type] end |
#imports_for_module(module_name) ⇒ Object
2367 2368 2369 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2367 def imports_for_module(module_name) @program.analyses_by_module_name.fetch(module_name).imports end |
#infer_binary_operand_types(expression, env:, expected_type: nil) ⇒ Object
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 943 def infer_binary_operand_types(expression, env:, expected_type: nil) propagated_type = propagating_expected_type(expression.operator, expected_type) left_type = infer_expression_type(expression.left, env:, expected_type: propagated_type) right_env = binary_right_env(expression, env) right_expected_type = case expression.operator when "<<", ">>" propagated_type || left_type when "+", "-", "*", "/", "%" propagated_type || left_type when "|", "&", "^" left_type else left_type end right_type = infer_expression_type(expression.right, env: right_env, expected_type: right_expected_type) left_type, right_type = harmonize_binary_float_literal_types(expression.left, expression.right, left_type, right_type, env: right_env) harmonize_binary_integer_literal_types(expression.left, expression.right, left_type, right_type, env: right_env) end |
#infer_expression_type(expression, env:, expected_type: nil) ⇒ Object
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 700 def infer_expression_type(expression, env:, expected_type: nil) if !@bypass_sema_type_cache && expected_type.nil? && !expression.is_a?(AST::Identifier) && (id = @ctx.ast.node_ids[expression.object_id]) && (resolved = @ctx.resolved_expr_types[id]) return resolved end case expression when AST::AwaitExpr task_type = infer_expression_type(expression.expression, env:) raise LoweringError.new("await requires a Task value, got #{task_type}", line: 0, column: 0, path: @ctx.current_analysis_path) unless task_type.is_a?(Types::Task) task_type.result_type when AST::IntegerLiteral if expected_type.is_a?(Types::Primitive) && expected_type.integer? expected_type else @ctx.types.fetch("int") end when AST::CharLiteral @ctx.types.fetch("ubyte") when AST::FloatLiteral if expression.lexeme.end_with?("f") @ctx.types.fetch("float") elsif expression.lexeme.end_with?("d") @ctx.types.fetch("double") elsif expected_type.is_a?(Types::Primitive) && expected_type.float? expected_type else @ctx.types.fetch("float") end when AST::SizeofExpr, AST::AlignofExpr, AST::OffsetofExpr @ctx.types.fetch("ptr_uint") when AST::StringLiteral @ctx.types.fetch(expression.cstring ? "cstr" : "str") when AST::FormatString @ctx.types.fetch("str") when AST::BooleanLiteral @ctx.types.fetch("bool") when AST::NullLiteral infer_null_literal_type(expression, expected_type) when AST::Identifier binding = lookup_value(expression.name, env) return binding[:type] if binding if (value_param_type = value_param_declared_type(expression.name)) return value_param_type end return function_type_for_name(expression.name) if @ctx.functions.key?(expression.name) raise LoweringError.new("unknown identifier #{expression.name}", line: expression.line, column: expression.column, path: @ctx.current_analysis_path) when AST::MemberAccess if (type_expr = resolve_type_expression(expression.receiver)) member_type = resolve_type_member(type_expr, expression.member) return member_type if member_type dispatch_receiver_type = method_dispatch_receiver_type(type_expr) method_entry_receiver_type = type_expr method_entry = @method_definitions[[type_expr, expression.member]] method_entry ||= @method_definitions[[type_expr, "static:#{expression.member}"]] unless method_entry || dispatch_receiver_type == type_expr method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, expression.member]] method_entry ||= @method_definitions[[dispatch_receiver_type, "static:#{expression.member}"]] end if method_entry method_analysis, method_ast = method_entry method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) return method_binding.type if method_binding.type.receiver_type.nil? end end if expression.receiver.is_a?(AST::Identifier) && @ctx.imports.key?(expression.receiver.name) imported_module = @ctx.imports.fetch(expression.receiver.name) return imported_module.values.fetch(expression.member).type if imported_module.values.key?(expression.member) return imported_module.functions.fetch(expression.member).type if imported_module.functions.key?(expression.member) end receiver_type = infer_field_receiver_type(expression.receiver, env:) if (event_type = event_member_from_owner_type(receiver_type, expression.member)) return event_type end if receiver_type == @ctx.types["field_handle"] return infer_field_handle_member_type(expression) end if receiver_type == @ctx.types["member_handle"] return infer_member_handle_member_type(expression) end return receiver_type.field(expression.member) if receiver_type.respond_to?(:field) raise LoweringError.new("unknown member #{expression.member}", line: expression.line, column: expression.column, path: @ctx.current_analysis_path) when AST::IndexAccess receiver_type = infer_expression_type(expression.receiver, env:) if expression.index.is_a?(AST::RangeExpr) return range_index_result_type(receiver_type) end index_type = infer_expression_type(expression.index, env:) infer_index_result_type(receiver_type, index_type) when AST::UnaryOp return infer_result_propagation_type(expression, env:) if expression.operator == "?" operand_type = infer_expression_type(expression.operand, env:, expected_type:) case expression.operator when "not" @ctx.types.fetch("bool") else operand_type end when AST::BinaryOp left_type, right_type = infer_binary_operand_types(expression, env:, expected_type: expected_type) case expression.operator when "and", "or", "<", "<=", ">", ">=", "==", "!=" @ctx.types.fetch("bool") when "+", "-", "*", "/" aggregate_arithmetic_result_type(expression.operator, left_type, right_type) || pointer_arithmetic_result_type(expression.operator, left_type, right_type) || common_numeric_type(left_type, right_type) || left_type when "%" common_integer_type(left_type, right_type) || left_type else left_type end when AST::IfExpr then_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: true, env:)) else_env = env_with_refinements(env, flow_refinements(expression.condition, truthy: false, env:)) then_type = infer_expression_type(expression.then_expression, env: then_env, expected_type: expected_type) else_type = infer_expression_type(expression.else_expression, env: else_env, expected_type: expected_type) if expected_type && if_expression_branch_compatible?(then_type, expected_type) && if_expression_branch_compatible?(else_type, expected_type) return expected_type end conditional_common_type(then_type, else_type) || raise(LoweringError, "if expression branches require compatible types, got #{then_type} and #{else_type}") when AST::MatchExpr scrutinee_type = infer_expression_type(expression.expression, env:) arm_types = expression.arms.map do |arm| arm_env = duplicate_env(env) if scrutinee_type.is_a?(Types::Variant) && arm.binding_name && !wildcard_arm_pattern?(arm.pattern) arm_name = variant_match_arm_name_from_pattern(arm.pattern) if arm_name && scrutinee_type.has_payload?(arm_name) fields = scrutinee_type.arm(arm_name) payload_type = Types::VariantArmPayload.new(scrutinee_type, arm_name, fields) arm_env[:scopes].last[arm.binding_name] = local_binding(type: payload_type, linkage_name: c_local_name(arm.binding_name), mutable: false, pointer: false) end end infer_expression_type(arm.value, env: arm_env, expected_type: expected_type) end if expected_type && arm_types.all? { |arm_type| if_expression_branch_compatible?(arm_type, expected_type) } return expected_type end common_type = arm_types.first arm_types.drop(1).each do |arm_type| common_type = conditional_common_type(common_type, arm_type) || raise(LoweringError, "match expression arms require compatible types, got #{common_type} and #{arm_type}") end common_type when AST::UnsafeExpr infer_expression_type(expression.expression, env:, expected_type:) when AST::ProcExpr resolve_type_ref(AST::ProcType.new(params: expression.params, return_type: expression.return_type)) when AST::Call kind, _callee_name, _receiver, callee_type = resolve_callee(expression.callee, env, arguments: expression.arguments) case kind when :function, :method, :associated_method, :callable_value, :str_buffer_clear, :str_buffer_assign, :str_buffer_append, :str_buffer_assign_format, :str_buffer_append_format, :str_buffer_len, :str_buffer_capacity, :str_buffer_as_str, :str_buffer_as_cstr, :event_subscribe, :event_subscribe_once, :event_unsubscribe, :event_emit, :event_wait, :compile_time_builtin, :reinterpret, :zero, :hash, :equal, :order, :dyn_method callee_type.return_type when :struct_literal, :struct_with, :array, :simd, :variant_arm_ctor, :adapt callee_type when :ref_of argument_type = infer_expression_type(expression.arguments.fetch(0).value, env:) Types::Registry.generic_instance("ref", [argument_type]) when :const_ptr_of argument_type = infer_expression_type(expression.arguments.fetch(0).value, env:) Types::Registry.generic_instance("const_ptr", [argument_type]) when :read infer_value_type(expression.arguments.fetch(0).value, env:) when :ptr_of argument_type = infer_expression_type(expression.arguments.fetch(0).value, env:) if ref_type?(argument_type) Types::Registry.generic_instance("ptr", [referenced_type(argument_type)]) else Types::Registry.generic_instance("ptr", [infer_expression_type(expression.arguments.fetch(0).value, env:, expected_type: expected_type && pointer_type?(expected_type) ? pointee_type(expected_type) : nil)]) end when :array_as_span callee_type when :fatal @ctx.types.fetch("void") when :get receiver_type = infer_expression_type(expression.arguments.fetch(0).value, env:) elem_type = if array_type?(receiver_type) array_element_type(receiver_type) else receiver_type.element_type end Types::Registry.nullable(Types::Registry.generic_instance("ptr", [elem_type])) when :atomic_load, :atomic_add, :atomic_sub, :atomic_exchange, :atomic_store, :atomic_compare_exchange, :simd_lane_with callee_type.return_type else raise LoweringError.new("unsupported call kind #{kind}", line: 0, column: 0, path: @ctx.current_analysis_path) end when AST::PrefixCast resolve_type_ref(expression.target_type) when AST::Specialization if expression.callee.is_a?(AST::Identifier) && expression.callee.name == "zero" _, _, _, function_type = resolve_callee(expression, env, arguments: []) function_type.return_type elsif expression.callee.is_a?(AST::Identifier) && expression.callee.name == "default" resolve_default_specialization(expression, env:).target_type elsif (callable_resolution = resolve_specialized_callable_binding(expression, env:)) callable_kind, function_binding, = callable_resolution raise LoweringError.new("specialized method must be called", line: 0, column: 0, path: @ctx.current_analysis_path) if callable_kind == :method function_binding.type else raise LoweringError.new("unsupported specialization", line: 0, column: 0, path: @ctx.current_analysis_path) end when AST::RangeExpr raise LoweringError.new("range expression is not valid in this context; use it as a for-loop iterable", line: 0, column: 0, path: @ctx.current_analysis_path) when AST::ExpressionList names = [] element_types = [] expression.elements.each do |element| if element.is_a?(AST::Argument) names << element.name element_types << infer_expression_type(element.value, env:) else names << nil element_types << infer_expression_type(element, env:) end end has_named = names.any? Types::Registry.tuple(element_types, field_names: has_named ? names : nil) when AST::DetachExpr Types::Handle.new else raise LoweringError.new("unsupported expression type #{expression.class.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#infer_field_handle_member_type(expression) ⇒ Object
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2556 def infer_field_handle_member_type(expression) case expression.member when "name" then @ctx.types["str"] when "type" handle = compile_time_const_value(expression.receiver, env: nil) return @error_type unless handle.is_a?(Types::FieldHandle) resolve_type_ref(handle.field_declaration.type) else @error_type end end |
#infer_function_type_arguments(binding, arguments, env, receiver_type: nil) ⇒ Object
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2084 def infer_function_type_arguments(binding, arguments, env, receiver_type: nil) expected_params = binding.type.params unless call_arity_matches?(binding.type, arguments.length) raise LoweringError.new((binding.type, binding.name, arguments.length), line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions = infer_receiver_type_substitutions(binding, receiver_type) expected_params.each_with_index do |parameter, index| argument = arguments.fetch(index) candidate_type = substitute_type(parameter.type, substitutions) expected_argument_type = if callable_type?(candidate_type) candidate_type elsif contains_type_var?(candidate_type) nil else candidate_type end actual_type = infer_expression_type(argument.value, env:, expected_type: expected_argument_type) collect_type_substitutions(parameter.type, actual_type, substitutions, binding.name) end binding.type_params.map do |name| inferred = substitutions[name] raise LoweringError.new("cannot infer type argument #{name} for function #{binding.name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless inferred inferred end end |
#infer_member_handle_member_type(expression) ⇒ Object
2569 2570 2571 2572 2573 2574 2575 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2569 def infer_member_handle_member_type(expression) case expression.member when "name" then @ctx.types["str"] when "value" then @ctx.types["int"] else @error_type end end |
#infer_null_literal_type(expression, expected_type) ⇒ Object
1117 1118 1119 1120 1121 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1117 def infer_null_literal_type(expression, expected_type) return Types::Null.new(resolve_type_ref(expression.type)) if expression.type expected_type || null_type end |
#infer_receiver_type_substitutions(binding, receiver_type) ⇒ Object
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2158 def infer_receiver_type_substitutions(binding, receiver_type) declared_receiver_type = binding.declared_receiver_type return {} unless declared_receiver_type case declared_receiver_type when Types::Nullable unless receiver_type.is_a?(Types::Nullable) raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end infer_receiver_type_substitutions( binding.with(declared_receiver_type: declared_receiver_type.base), receiver_type.base, ) when Types::StructInstance return {} unless declared_receiver_type.definition.is_a?(Types::GenericStructDefinition) unless receiver_type.is_a?(Types::StructInstance) && receiver_type.definition == declared_receiver_type.definition raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end declared_receiver_type.definition.type_params.zip(receiver_type.arguments).to_h when Types::VariantInstance return {} unless declared_receiver_type.definition.is_a?(Types::GenericVariantDefinition) unless receiver_type.is_a?(Types::VariantInstance) && receiver_type.definition == declared_receiver_type.definition raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end declared_receiver_type.definition.type_params.zip(receiver_type.arguments).to_h when Types::GenericInstance unless receiver_type.is_a?(Types::GenericInstance) && receiver_type.name == declared_receiver_type.name && receiver_type.arguments.length == declared_receiver_type.arguments.length raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end declared_receiver_type.arguments.zip(receiver_type.arguments).each_with_object({}) do |(declared_argument, actual_argument), substitutions| if declared_argument.is_a?(Types::TypeVar) substitutions[declared_argument.name] = actual_argument elsif declared_argument != actual_argument raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end when Types::Span return {} unless receiver_type.is_a?(Types::Span) substitutions = {} if declared_receiver_type.element_type.is_a?(Types::TypeVar) substitutions[declared_receiver_type.element_type.name] = receiver_type.element_type elsif declared_receiver_type.element_type != receiver_type.element_type raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions when Types::Task return {} unless receiver_type.is_a?(Types::Task) substitutions = {} if declared_receiver_type.result_type.is_a?(Types::TypeVar) substitutions[declared_receiver_type.result_type.name] = receiver_type.result_type elsif declared_receiver_type.result_type != receiver_type.result_type raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions when Types::SoA return {} unless receiver_type.is_a?(Types::SoA) substitutions = {} if declared_receiver_type.element_type.is_a?(Types::TypeVar) substitutions[declared_receiver_type.element_type.name] = receiver_type.element_type elsif declared_receiver_type.element_type != receiver_type.element_type raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions when Types::Simd return {} unless receiver_type.is_a?(Types::Simd) substitutions = {} if declared_receiver_type.element_type.is_a?(Types::TypeVar) substitutions[declared_receiver_type.element_type.name] = receiver_type.element_type elsif declared_receiver_type.element_type != receiver_type.element_type raise LoweringError.new("cannot use method #{binding.name} with receiver #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions else {} end end |
#instantiate_function_binding(binding, type_arguments) ⇒ Object
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2015 def instantiate_function_binding(binding, type_arguments) if binding.type_params.empty? raise LoweringError.new("function #{binding.name} is not generic and cannot be specialized", line: 0, column: 0, path: @ctx.current_analysis_path) end unless binding.type_params.length == type_arguments.length raise LoweringError.new("function #{binding.name} expects #{binding.type_params.length} type arguments, got #{type_arguments.length}", line: 0, column: 0, path: @ctx.current_analysis_path) end if type_arguments.any? { |type_argument| contains_ref_type?(type_argument) } raise LoweringError.new("generic function #{binding.name} cannot be instantiated with ref types", line: 0, column: 0, path: @ctx.current_analysis_path) end key = type_arguments.freeze return binding.instances.fetch(key) if binding.instances.key?(key) substitutions = binding.type_params.zip(type_arguments).to_h validate_function_type_param_constraints!(binding, substitutions) instance = Bindings::FunctionBinding.new( name: binding.name, type: substitute_type(binding.type, substitutions), body_params: binding.body_params.map { |param| substitute_value_binding(param, substitutions) }, body_return_type: substitute_type(binding.body_return_type, substitutions), ast: binding.ast, external: binding.external, async: binding.async, type_params: [].freeze, type_param_constraints: {}.freeze, instances: {}, type_arguments: key, owner: binding.owner, specialization_owner: nil, type_substitutions: substitutions.freeze, declared_receiver_type: binding.declared_receiver_type ? substitute_type(binding.declared_receiver_type, substitutions) : nil, ) binding.instances[key] = instance end |
#instantiate_function_binding_with_receiver(binding, explicit_type_arguments, receiver_type: nil) ⇒ Object
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1987 def instantiate_function_binding_with_receiver(binding, explicit_type_arguments, receiver_type: nil) if binding.type_params.empty? raise LoweringError.new("function #{binding.name} is not generic and cannot be specialized", line: 0, column: 0, path: @ctx.current_analysis_path) end receiver_substitutions = infer_receiver_type_substitutions(binding, receiver_type) remaining_type_params = binding.type_params.reject { |name| receiver_substitutions.key?(name) } unless remaining_type_params.length == explicit_type_arguments.length raise LoweringError.new("function #{binding.name} expects #{remaining_type_params.length} type arguments, got #{explicit_type_arguments.length}", line: 0, column: 0, path: @ctx.current_analysis_path) end substitutions = receiver_substitutions.dup remaining_type_params.zip(explicit_type_arguments).each do |name, type_argument| raise LoweringError.new("generic function #{binding.name} cannot be instantiated with ref types", line: 0, column: 0, path: @ctx.current_analysis_path) if contains_ref_type?(type_argument) substitutions[name] = type_argument end type_arguments = binding.type_params.map do |name| inferred = substitutions[name] raise LoweringError.new("cannot infer type argument #{name} for function #{binding.name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless inferred inferred end instantiate_function_binding(binding, type_arguments) end |
#integer_literal_expression?(expression) ⇒ Boolean
1006 1007 1008 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1006 def integer_literal_expression?(expression) expression.is_a?(AST::IntegerLiteral) end |
#interface_implementation_key(type) ⇒ Object
2067 2068 2069 2070 2071 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2067 def interface_implementation_key(type) return type.definition if type.is_a?(Types::StructInstance) type end |
#literal_type_argument_name_candidate?(type_ref) ⇒ Boolean
1486 1487 1488 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1486 def literal_type_argument_name_candidate?(type_ref) type_ref.arguments.empty? && !type_ref.nullable end |
#lower_array_to_span_expression(expression, target_type) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 113 def lower_array_to_span_expression(expression, target_type) array_type = expression.type array_type = referenced_type(array_type) if ref_type?(array_type) IR::AggregateLiteral.new( type: target_type, fields: [ IR::AggregateField.new( name: "data", value: IR::AddressOf.new( expression: IR::Index.new( receiver: expression, index: IR::IntegerLiteral.new(value: 0, type: @ctx.types.fetch("ptr_uint")), type: target_type.element_type, ), type: pointer_to(target_type.element_type), ), ), IR::AggregateField.new( name: "len", value: IR::IntegerLiteral.new(value: array_length(array_type), type: @ctx.types.fetch("ptr_uint")), ), ], ) end |
#lower_fn_to_proc_expression(source_expression, source_function, env:, expected_type:) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 54 def lower_fn_to_proc_expression(source_expression, source_function, env:, expected_type:) raise LoweringError.new("function-to-proc coercion requires a direct function name", line: 0, column: 0, path: @ctx.current_analysis_path) unless source_function.is_a?(IR::Name) proc_id = fresh_proc_symbol invoke_c_name = "#{@ctx.module_prefix}__proc_#{proc_id}__invoke" release_linkage_name = "#{@ctx.module_prefix}__proc_#{proc_id}__release" retain_c_name = "#{@ctx.module_prefix}__proc_#{proc_id}__retain" @artifacts.synthetic_functions << build_direct_function_proc_invoke_function(source_expression, source_function.name, source_function.type, expected_type, invoke_c_name) @artifacts.synthetic_functions << build_proc_noop_release_function(release_linkage_name) @artifacts.synthetic_functions << build_proc_noop_retain_function(retain_c_name) IR::AggregateLiteral.new( type: expected_type, fields: [ IR::AggregateField.new(name: "env", value: IR::NullLiteral.new(type: proc_env_pointer_type)), IR::AggregateField.new(name: "invoke", value: IR::Name.new(name: invoke_c_name, type: proc_invoke_function_type(expected_type), pointer: false)), IR::AggregateField.new(name: "release", value: IR::Name.new(name: release_linkage_name, type: proc_release_function_type, pointer: false)), IR::AggregateField.new(name: "retain", value: IR::Name.new(name: retain_c_name, type: proc_retain_function_type, pointer: false)), ], ) end |
#lower_str_buffer_to_span_expression(expression, target_type) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 139 def lower_str_buffer_to_span_expression(expression, target_type) IR::AggregateLiteral.new( type: target_type, fields: [ IR::AggregateField.new( name: "data", value: IR::Call.new( callee: "mt_str_buffer_prepare_write", arguments: [ lower_str_buffer_data_pointer_from_lowered(expression), IR::IntegerLiteral.new(value: str_buffer_capacity(expression.type), type: @ctx.types.fetch("ptr_uint")), lower_str_buffer_dirty_pointer_from_lowered(expression), ], type: pointer_to(target_type.element_type), ), ), IR::AggregateField.new( name: "len", value: IR::IntegerLiteral.new(value: str_buffer_storage_capacity(expression.type), type: @ctx.types.fetch("ptr_uint")), ), ], ) end |
#merge_cstr_metadata_after_if_statement!(statement, env) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 257 def (statement, env) exit_envs = (statement, env) return if exit_envs.empty? trackable_binding_names(env).each do |name| binding = lookup_value(name, env) next unless binding ( name, env, cstr_backed: cstr_trackable_type?(binding[:type]) && exit_envs.all? { |exit_env| binding_cstr_backed?(lookup_value(name, exit_env)) }, cstr_list_backed: cstr_list_trackable_type?(binding[:type]) && exit_envs.all? { |exit_env| binding_cstr_list_backed?(lookup_value(name, exit_env)) }, ) end end |
#method_analysis_key(method_ast) ⇒ Object
1378 1379 1380 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1378 def method_analysis_key(method_ast) method_ast.kind == :static ? "static:#{method_ast.name}" : method_ast.name end |
#methods_receiver_type_argument_names!(type_ref) ⇒ Object
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2144 def methods_receiver_type_argument_names!(type_ref) names = type_ref.arguments.map do |argument| value = argument.value next unless value.is_a?(AST::TypeRef) next unless value.arguments.empty? && !value.nullable && value.name.parts.length == 1 value.name.parts.first end raise LoweringError.new("extending target #{type_ref} must use the receiver type parameters directly", line: 0, column: 0, path: @ctx.current_analysis_path) if names.any?(&:nil?) names end |
#pointer_arithmetic_result_type(operator, left_type, right_type) ⇒ Object
1154 1155 1156 1157 1158 1159 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1154 def pointer_arithmetic_result_type(operator, left_type, right_type) return left_type if pointer_type?(left_type) && integer_type?(right_type) && (operator == "+" || operator == "-") return right_type if operator == "+" && integer_type?(left_type) && pointer_type?(right_type) nil end |
#pointer_lowered_method_receiver?(callee_type, callee_binding) ⇒ Boolean
1043 1044 1045 1046 1047 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1043 def pointer_lowered_method_receiver?(callee_type, callee_binding) return true if callee_type.receiver_editable receiver_type_uses_pointer_lowering?(callee_type.receiver_type) && !callee_binding&.async end |
#pointer_lowered_sync_method_receiver?(binding) ⇒ Boolean
1037 1038 1039 1040 1041 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1037 def pointer_lowered_sync_method_receiver?(binding) return false if binding.async pointer_lowered_method_receiver?(binding.type, binding) end |
#promoted_binary_operand_type(operator, left_type, right_type) ⇒ Object
1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1022 def promoted_binary_operand_type(operator, left_type, right_type) case operator when "+", "-", "*", "/", "<", "<=", ">", ">=", "==", "!=" common_numeric_type(left_type, right_type) when "%" common_integer_type(left_type, right_type) end end |
#propagating_expected_type(operator, expected_type) ⇒ Object
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1010 def propagating_expected_type(operator, expected_type) case operator when "+", "-", "*", "/", "%", "<<", ">>" return expected_type if expected_type.is_a?(Types::Primitive) && expected_type.numeric? when "|", "&", "^" return expected_type if expected_type.is_a?(Types::Primitive) && expected_type.integer? return expected_type if expected_type.is_a?(Types::Flags) end nil end |
#receiver_type_uses_pointer_lowering?(type) ⇒ Boolean
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1049 def receiver_type_uses_pointer_lowering?(type) case type when Types::Nullable receiver_type_uses_pointer_lowering?(type.base) when Types::Struct, Types::StructInstance type_contains_array_storage?(type) else false end end |
#record_external_layout_assertion(source_type, target_type) ⇒ Object
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1084 def record_external_layout_assertion(source_type, target_type) source_root = ffi_external_layout_root_type(source_type) target_root = ffi_external_layout_root_type(target_type) return unless source_root && target_root return unless source_root.external && target_root.external return if source_root.module_name == target_root.module_name pair_key = [[source_root.module_name, source_root.name], [target_root.module_name, target_root.name]].sort.freeze return if @artifacts.emitted_external_layout_pairs[pair_key] @artifacts.emitted_external_layout_pairs[pair_key] = true @artifacts.external_layout_assertions << IR::StaticAssert.new( condition: IR::Binary.new( operator: "==", left: IR::SizeofExpr.new(target_type: source_root, type: @ctx.types.fetch("ptr_uint")), right: IR::SizeofExpr.new(target_type: target_root, type: @ctx.types.fetch("ptr_uint")), type: @ctx.types.fetch("bool"), ), message: IR::StringLiteral.new( value: "FFI layout mismatch: #{source_root} vs #{target_root}", type: @ctx.types.fetch("str"), cstring: false, ), ) end |
#reflection_identifier_name(expression) ⇒ Object
1930 1931 1932 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1930 def reflection_identifier_name(expression) expression.is_a?(AST::Identifier) ? expression.name : nil end |
#reflection_positional_arguments?(arguments, expected_length) ⇒ Boolean
1831 1832 1833 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1831 def reflection_positional_arguments?(arguments, expected_length) arguments.length == expected_length && arguments.none?(&:name) end |
#reflection_type_from_expression(expression, env:) ⇒ Object
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1842 def reflection_type_from_expression(expression, env:) case expression when AST::Identifier return nil if env && lookup_value(expression.name, env) current_type_params[expression.name] || @ctx.types[expression.name] when AST::MemberAccess return nil unless expression.receiver.is_a?(AST::Identifier) if @ctx.imports.key?(expression.receiver.name) imported_module = @ctx.imports[expression.receiver.name] return nil if imported_module.private_type?(expression.member) return imported_module.types[expression.member] end parent_type = @ctx.types[expression.receiver.name] return parent_type.nested_types[expression.member] if parent_type.respond_to?(:nested_types) && parent_type.nested_types.key?(expression.member) nil else nil end end |
#reinterpret_expression(expression, target_type) ⇒ Object
1066 1067 1068 1069 1070 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1066 def reinterpret_expression(expression, target_type) return expression if expression.type == target_type IR::ReinterpretExpr.new(target_type:, source_type: expression.type, expression:, type: target_type) end |
#replace_binding_cstr_metadata!(name, env, cstr_backed:, cstr_list_backed:) ⇒ Object
359 360 361 362 363 364 365 366 367 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 359 def (name, env, cstr_backed:, cstr_list_backed:) env[:scopes].reverse_each do |scope| next if scope.is_a?(FlowScope) next unless scope.key?(name) scope[name] = scope.fetch(name).merge(cstr_backed:, cstr_list_backed:) return end end |
#resolve_attribute_name_argument(expression) ⇒ Object
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1913 def resolve_attribute_name_argument(expression) case expression when AST::Identifier @ctx.attributes[expression.name] || builtin_attribute_binding(expression.name) when AST::MemberAccess return nil unless expression.receiver.is_a?(AST::Identifier) imported_module = @ctx.imports[expression.receiver.name] return nil unless imported_module return nil if imported_module.private_attribute?(expression.member) imported_module.attributes[expression.member] else nil end end |
#resolve_callable_handle_argument(expression) ⇒ Object
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1890 def resolve_callable_handle_argument(expression) case expression when AST::Identifier binding = @ctx.functions[expression.name] return nil unless binding&.ast Types::CallableHandle.new(expression.name, binding.ast) when AST::MemberAccess return nil unless expression.receiver.is_a?(AST::Identifier) imported_module = @ctx.imports[expression.receiver.name] return nil unless imported_module return nil if imported_module.private_function?(expression.member) binding = imported_module.functions[expression.member] return nil unless binding&.ast Types::CallableHandle.new("#{expression.receiver.name}.#{expression.member}", binding.ast) else nil end end |
#resolve_callee(callee, env, arguments: nil) ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 409 def resolve_callee(callee, env, arguments: nil) case callee when AST::Identifier resolve_identifier_callee(callee, env, arguments) when AST::MemberAccess resolve_member_access_callee(callee, env, arguments) when AST::Specialization resolve_specialization_callee(callee, env) else resolve_expression_callee(callee, env) end end |
#resolve_current_module_const_value(name) ⇒ Object
1503 1504 1505 1506 1507 1508 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1503 def resolve_current_module_const_value(name) binding = @ctx.values[name] return unless binding&.kind == :const binding.const_value end |
#resolve_default_specialization(expression, env:) ⇒ Object
1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1255 def resolve_default_specialization(expression, env:) target_type = resolve_type_ref(expression.arguments.fetch(0).value) explicit_default = resolve_explicit_default_binding(target_type, context: "default[#{target_type}]") raise LoweringError.new("default[#{target_type}] requires associated function #{target_type}.default()", line: 0, column: 0, path: @ctx.current_analysis_path) unless explicit_default DefaultResolution.new(target_type:, binding: explicit_default.binding, callee_name: explicit_default.callee_name) end |
#resolve_equal_specialization(expression, env:) ⇒ Object
1272 1273 1274 1275 1276 1277 1278 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1272 def resolve_equal_specialization(expression, env:) target_type = resolve_type_ref(expression.arguments.fetch(0).value) explicit_equal = resolve_explicit_equal_binding(target_type, context: "equal[#{target_type}]") raise LoweringError.new("equal[#{target_type}] requires associated function #{target_type}.equal(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> bool", line: 0, column: 0, path: @ctx.current_analysis_path) unless explicit_equal EqualResolution.new(target_type:, binding: explicit_equal.binding, callee_name: explicit_equal.callee_name) end |
#resolve_explicit_associated_binding(target_type, method_name, requirement_message:) {|method_binding, method_analysis, method_entry_receiver_type| ... } ⇒ Object
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1382 def resolve_explicit_associated_binding(target_type, method_name, requirement_message:) dispatch_receiver_type = method_dispatch_receiver_type(target_type) method_entry_receiver_type = target_type static_method_name = "static:#{method_name}" method_entry = @method_definitions[[target_type, static_method_name]] unless method_entry || dispatch_receiver_type == target_type method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, static_method_name]] end return nil unless method_entry method_analysis, method_ast = method_entry method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) raise LoweringError.new(, line: 0, column: 0, path: @ctx.current_analysis_path) unless method_binding.type.receiver_type.nil? method_binding = instantiate_function_binding_with_receiver(method_binding, [], receiver_type: target_type) if method_binding.type_params.any? yield method_binding, method_analysis, method_entry_receiver_type callee_name = if method_binding.external external_function_c_name(method_binding) else function_binding_c_name(method_binding, module_name: method_analysis.module_name, receiver_type: method_entry_receiver_type) end case method_name when "default" ExplicitDefaultBinding.new(binding: method_binding, callee_name:) when "hash" ExplicitHashBinding.new(binding: method_binding, callee_name:) when "equal" ExplicitEqualBinding.new(binding: method_binding, callee_name:) when "order" ExplicitOrderBinding.new(binding: method_binding, callee_name:) else raise LoweringError.new("unsupported associated hook #{method_name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#resolve_explicit_default_binding(target_type, context:) ⇒ Object
1288 1289 1290 1291 1292 1293 1294 1295 1296 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1288 def resolve_explicit_default_binding(target_type, context:) = "#{context} requires associated function #{target_type}.default()" resolve_explicit_associated_binding(target_type, "default", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| raise LoweringError.new("#{context} requires #{target_type}.default() to take 0 arguments", line: 0, column: 0, path: @ctx.current_analysis_path) unless method_binding.type.params.empty? unless method_binding.type.return_type == target_type raise LoweringError.new("#{context} requires #{target_type}.default() to return #{target_type}, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_explicit_equal_binding(target_type, context:) ⇒ Object
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1310 def resolve_explicit_equal_binding(target_type, context:) = "#{context} requires associated function #{target_type}.equal(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> bool" resolve_explicit_associated_binding(target_type, "equal", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| expected_param_types = [const_pointer_to(target_type), const_pointer_to(target_type)] unless method_binding.type.params.map(&:type) == expected_param_types raise LoweringError.new("#{context} requires #{target_type}.equal(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> bool", line: 0, column: 0, path: @ctx.current_analysis_path) end unless method_binding.type.return_type == @ctx.types.fetch("bool") raise LoweringError.new("#{context} requires #{target_type}.equal(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> bool, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_explicit_format_append_binding(target_type, context:) ⇒ Object
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1365 def resolve_explicit_format_append_binding(target_type, context:) = "#{context} requires method #{target_type}.append_format(output: ref[std.string.String]) -> void" resolve_explicit_instance_binding(target_type, "append_format", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| raise LoweringError.new("#{context} requires #{target_type}.append_format() to be non-editable", line: 0, column: 0, path: @ctx.current_analysis_path) if method_binding.type.receiver_editable unless method_binding.type.params.length == 1 && string_builder_ref_type?(method_binding.type.params.first.type) raise LoweringError.new("#{context} requires #{target_type}.append_format(output: ref[std.string.String]) -> void", line: 0, column: 0, path: @ctx.current_analysis_path) end unless method_binding.type.return_type == @ctx.types.fetch("void") raise LoweringError.new("#{context} requires #{target_type}.append_format(output: ref[std.string.String]) -> void, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_explicit_format_binding(target_type, context:) ⇒ Object
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1336 def resolve_explicit_format_binding(target_type, context:) length_binding = resolve_explicit_format_len_binding(target_type, context:) append_binding = resolve_explicit_format_append_binding(target_type, context:) return ExplicitFormatBinding.new( length_binding: length_binding.fetch(:binding), length_callee_name: length_binding.fetch(:callee_name), append_binding: append_binding.fetch(:binding), append_callee_name: append_binding.fetch(:callee_name), ) if length_binding && append_binding if length_binding || append_binding raise LoweringError.new("#{context} requires methods #{target_type}.format_len() -> ptr_uint and #{target_type}.append_format(output: ref[std.string.String]) -> void", line: 0, column: 0, path: @ctx.current_analysis_path) end nil end |
#resolve_explicit_format_len_binding(target_type, context:) ⇒ Object
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1354 def resolve_explicit_format_len_binding(target_type, context:) = "#{context} requires method #{target_type}.format_len() -> ptr_uint" resolve_explicit_instance_binding(target_type, "format_len", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| raise LoweringError.new("#{context} requires #{target_type}.format_len() to take 0 arguments", line: 0, column: 0, path: @ctx.current_analysis_path) unless method_binding.type.params.empty? raise LoweringError.new("#{context} requires #{target_type}.format_len() to be non-editable", line: 0, column: 0, path: @ctx.current_analysis_path) if method_binding.type.receiver_editable unless method_binding.type.return_type == @ctx.types.fetch("ptr_uint") raise LoweringError.new("#{context} requires #{target_type}.format_len() -> ptr_uint, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_explicit_hash_binding(target_type, context:) ⇒ Object
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1298 def resolve_explicit_hash_binding(target_type, context:) = "#{context} requires associated function #{target_type}.hash(value: const_ptr[#{target_type}]) -> uint" resolve_explicit_associated_binding(target_type, "hash", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| unless method_binding.type.params.map(&:type) == [const_pointer_to(target_type)] raise LoweringError.new("#{context} requires #{target_type}.hash(value: const_ptr[#{target_type}]) -> uint", line: 0, column: 0, path: @ctx.current_analysis_path) end unless method_binding.type.return_type == @ctx.types.fetch("uint") raise LoweringError.new("#{context} requires #{target_type}.hash(value: const_ptr[#{target_type}]) -> uint, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_explicit_instance_binding(target_type, method_name, requirement_message:) {|method_binding, method_analysis, method_entry_receiver_type| ... } ⇒ Object
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1420 def resolve_explicit_instance_binding(target_type, method_name, requirement_message:) dispatch_receiver_type = method_dispatch_receiver_type(target_type) method_entry_receiver_type = target_type method_entry = @method_definitions[[target_type, method_name]] unless method_entry || dispatch_receiver_type == target_type method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, method_name]] end return nil unless method_entry method_analysis, method_ast = method_entry method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) raise LoweringError.new(, line: 0, column: 0, path: @ctx.current_analysis_path) if method_binding.type.receiver_type.nil? method_binding = instantiate_function_binding_with_receiver(method_binding, [], receiver_type: target_type) if method_binding.type_params.any? yield method_binding, method_analysis, method_entry_receiver_type callee_name = if method_binding.external external_function_c_name(method_binding) else function_binding_c_name(method_binding, module_name: method_analysis.module_name, receiver_type: method_entry_receiver_type) end { binding: method_binding, callee_name: callee_name, } end |
#resolve_explicit_order_binding(target_type, context:) ⇒ Object
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1323 def resolve_explicit_order_binding(target_type, context:) = "#{context} requires associated function #{target_type}.order(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> int" resolve_explicit_associated_binding(target_type, "order", requirement_message:) do |method_binding, _method_analysis, _method_entry_receiver_type| expected_param_types = [const_pointer_to(target_type), const_pointer_to(target_type)] unless method_binding.type.params.map(&:type) == expected_param_types raise LoweringError.new("#{context} requires #{target_type}.order(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> int", line: 0, column: 0, path: @ctx.current_analysis_path) end unless method_binding.type.return_type == @ctx.types.fetch("int") raise LoweringError.new("#{context} requires #{target_type}.order(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> int, got #{method_binding.type.return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#resolve_expression_callee(callee, env) ⇒ Object
693 694 695 696 697 698 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 693 def resolve_expression_callee(callee, env) callee_type = infer_expression_type(callee, env:) return [:callable_value, nil, nil, callee_type, nil] if callable_type?(callee_type) raise LoweringError.new("unsupported callee #{callee.class.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#resolve_field_handle_type_ref(parts) ⇒ Object
Resolves a bare dotted reflection type ref field.type (where field is
a compile-time field_handle bound in the active inline-for env) to the
field's concrete type. Mirrors the sema-side resolve_compile_time_type_ref.
2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2543 def resolve_field_handle_type_ref(parts) return nil unless parts.length == 2 && parts.last == "type" binding = lookup_value(parts.first, @type_resolution_env) handle = binding && binding[:const_value] return nil unless handle.is_a?(Types::FieldHandle) # Use the struct's already-resolved field type (module-independent) rather # than re-resolving the field's declared TypeRef, which would look up a # user struct name in this (std) module's scope and fail. handle.struct_handle.struct_type.field(handle.field_name) end |
#resolve_hash_specialization(expression, env:) ⇒ Object
1264 1265 1266 1267 1268 1269 1270 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1264 def resolve_hash_specialization(expression, env:) target_type = resolve_type_ref(expression.arguments.fetch(0).value) explicit_hash = resolve_explicit_hash_binding(target_type, context: "hash[#{target_type}]") raise LoweringError.new("hash[#{target_type}] requires associated function #{target_type}.hash(value: const_ptr[#{target_type}]) -> uint", line: 0, column: 0, path: @ctx.current_analysis_path) unless explicit_hash HashResolution.new(target_type:, binding: explicit_hash.binding, callee_name: explicit_hash.callee_name) end |
#resolve_identifier_callee(callee, env, arguments) ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 422 def resolve_identifier_callee(callee, env, arguments) if (binding = lookup_value(callee.name, env)) return [:callable_value, nil, nil, binding[:type], nil] if callable_type?(binding[:type]) raise LoweringError.new("#{callee.name} is not callable", line: 0, column: 0, path: @ctx.current_analysis_path) end if @ctx.functions.key?(callee.name) binding = specialize_function_binding(@ctx.functions.fetch(callee.name), arguments, env) callee_name = if binding.external external_function_c_name(binding) else function_binding_c_name(binding, module_name: @ctx.module_name) end return [:function, callee_name, nil, binding.type, binding] end if (kind = PASS_THROUGH_BUILTINS[callee.name]) return [kind, nil, nil, nil] end if COMPILE_TIME_BUILTINS.include?(callee.name) return [:compile_time_builtin, callee.name, nil, compile_time_builtin_function_type(callee.name, arguments, env)] end type = lookup_named_type(callee.name) if type.is_a?(Types::Struct) || type.is_a?(Types::StringView) || task_type?(type) || type.is_a?(Types::Vector) || type.is_a?(Types::Matrix) || type.is_a?(Types::Quaternion) return [:struct_literal, nil, nil, type] end if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) raise LoweringError.new("generic type #{callee.name} requires type arguments", line: 0, column: 0, path: @ctx.current_analysis_path) end emit_fn = @artifacts.emitted_declarations.find { |d| d.is_a?(IR::Function) && d.name == callee.name } if emit_fn return [:function, emit_fn.linkage_name, nil, emit_fn.return_type, nil] end raise LoweringError.new("unknown callee #{callee.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#resolve_imported_module_const_value(import_name, value_name) ⇒ Object
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1510 def resolve_imported_module_const_value(import_name, value_name) imported_module = @ctx.imports[import_name] return unless imported_module if imported_module.private_value?(value_name) raise LoweringError.new("#{import_name}.#{value_name} is private to module #{imported_module.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end binding = imported_module.values[value_name] return unless binding&.kind == :const binding.const_value end |
#resolve_interface_ref(interface_ref) ⇒ Object
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2577 def resolve_interface_ref(interface_ref) parts = interface_ref.parts interface = if parts.length == 1 @ctx.interfaces[parts.first] elsif parts.length == 2 && @ctx.imports.key?(parts.first) @ctx.imports.fetch(parts.first).interfaces[parts.last] end raise LoweringError.new("unknown interface #{interface_ref}", line: 0, column: 0, path: @ctx.current_analysis_path) unless interface if interface_ref.type_arguments.any? raise LoweringError.new("interface #{interface.name} is not generic", line: 0, column: 0, path: @ctx.current_analysis_path) unless interface.respond_to?(:instantiate) type_args = interface_ref.type_arguments.map { |arg| resolve_type_ref(arg) } interface.instantiate(type_args) else interface end end |
#resolve_member_access_callee(callee, env, arguments) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 464 def resolve_member_access_callee(callee, env, arguments) if callee.receiver.is_a?(AST::Identifier) && @ctx.imports.key?(callee.receiver.name) imported_module = @ctx.imports.fetch(callee.receiver.name) if imported_module.functions.key?(callee.member) binding = specialize_function_binding(imported_module.functions.fetch(callee.member), arguments, env) unless binding.owner binding = binding.with(owner: imported_module.respond_to?(:analysis) ? imported_module.analysis : imported_module) end return [:function, function_binding_c_name(binding, module_name: imported_module.name), nil, binding.type, binding] unless binding.external return [:function, external_function_c_name(binding), nil, binding.type, binding] end imported_type = imported_module.types[callee.member] if imported_type.is_a?(Types::GenericStructDefinition) || imported_type.is_a?(Types::GenericVariantDefinition) raise LoweringError.new("generic type #{callee.receiver.name}.#{callee.member} requires type arguments", line: 0, column: 0, path: @ctx.current_analysis_path) end if imported_type.is_a?(Types::Struct) || imported_type.is_a?(Types::StringView) || task_type?(imported_type) || imported_type.is_a?(Types::Vector) || imported_type.is_a?(Types::Matrix) || imported_type.is_a?(Types::Quaternion) return [:struct_literal, nil, nil, imported_module.types.fetch(callee.member)] end if imported_type.is_a?(Types::Variant) && imported_type.arm_names.include?(callee.member) arm_name = callee.member return [:variant_arm_ctor, nil, nil, imported_type, [imported_type, arm_name]] end end if (type_expr = resolve_type_expression(callee.receiver)) if type_expr.is_a?(Types::Variant) && type_expr.arm_names.include?(callee.member) arm_name = callee.member return [:variant_arm_ctor, nil, nil, type_expr, [type_expr, arm_name]] end if type_expr.respond_to?(:nested_types) && type_expr.nested_types.key?(callee.member) return [:struct_literal, nil, nil, type_expr.nested_types[callee.member]] end dispatch_receiver_type = method_dispatch_receiver_type(type_expr) method_entry_receiver_type = type_expr method_entry = @method_definitions[[type_expr, callee.member]] method_entry ||= @method_definitions[[type_expr, "static:#{callee.member}"]] unless method_entry || dispatch_receiver_type == type_expr method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, callee.member]] method_entry ||= @method_definitions[[dispatch_receiver_type, "static:#{callee.member}"]] end if method_entry method_analysis, method_ast = method_entry method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) if method_binding.type.receiver_type.nil? method_binding = specialize_function_binding(method_binding, arguments, env, receiver_type: type_expr) if method_binding.type_params.any? return [:associated_method, function_binding_c_name(method_binding, module_name: method_analysis.module_name, receiver_type: method_entry_receiver_type), nil, method_binding.type, method_binding] end end raise LoweringError.new("unknown associated function #{type_expr}.#{callee.member}", line: 0, column: 0, path: @ctx.current_analysis_path) end resolved_receiver_type = infer_method_receiver_type(callee.receiver, env:, member_name: callee.member) if dyn_type?(resolved_receiver_type) interface = resolved_receiver_type.interface_binding method_binding = interface.methods[callee.member] raise LoweringError.new("no method '#{callee.member}' on interface #{interface.name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless method_binding return [:dyn_method, nil, callee.receiver, method_binding, nil] end dispatch_receiver_type = method_dispatch_receiver_type(resolved_receiver_type) method_entry_receiver_type = resolved_receiver_type method_entry = @method_definitions[[resolved_receiver_type, callee.member]] unless method_entry || dispatch_receiver_type == resolved_receiver_type method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, callee.member]] end if method_entry method_analysis, method_ast = method_entry method_analysis_key = method_ast.kind == :static ? "static:#{method_ast.name}" : method_ast.name method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key) method_binding = specialize_function_binding(method_binding, arguments, env, receiver_type: resolved_receiver_type) return [ :method, function_binding_c_name(method_binding, module_name: method_analysis.module_name, receiver_type: method_entry_receiver_type), callee.receiver, method_binding.type, method_binding, ] end if callee.member == "with" && struct_with_target_type?(resolved_receiver_type) return [:struct_with, nil, callee.receiver, resolved_receiver_type] end if (precomputed = @ctx.resolved_call_kinds[@ctx.ast.node_ids[callee.object_id]]) case precomputed when :str_buffer_clear, :str_buffer_assign, :str_buffer_append, :str_buffer_assign_format, :str_buffer_append_format, :str_buffer_len, :str_buffer_capacity, :str_buffer_as_str, :str_buffer_as_cstr return [precomputed, nil, callee.receiver, str_buffer_method_type(precomputed, resolved_receiver_type)] when :event_subscribe, :event_subscribe_once, :event_unsubscribe, :event_emit, :event_wait event_type = infer_expression_type(callee.receiver, env:) return [precomputed, nil, callee.receiver, event_method_type(precomputed, event_type)] when :atomic_load, :atomic_store, :atomic_add, :atomic_sub, :atomic_exchange, :atomic_compare_exchange elem = atomic_element_type(resolved_receiver_type) ret = case precomputed when :atomic_load, :atomic_add, :atomic_sub, :atomic_exchange then elem when :atomic_store then @ctx.types.fetch("void") when :atomic_compare_exchange then @ctx.types.fetch("bool") end return [precomputed, nil, callee.receiver, Types::Registry.function(nil, params: [], return_type: ret)] when :simd_lane_with return [precomputed, nil, callee.receiver, Types::Registry.function(nil, params: [], return_type: resolved_receiver_type)] end end if (str_buffer_method = str_buffer_method_kind(resolved_receiver_type, callee.member)) return [str_buffer_method, nil, callee.receiver, str_buffer_method_type(str_buffer_method, resolved_receiver_type)] end if (event_method = event_method_kind(resolved_receiver_type, callee.member)) event_type = infer_expression_type(callee.receiver, env:) return [event_method, nil, callee.receiver, event_method_type(event_method, event_type)] end if (atomic_method = atomic_method_kind(resolved_receiver_type, callee.member)) elem = atomic_element_type(resolved_receiver_type) ret = case atomic_method when :atomic_load, :atomic_add, :atomic_sub, :atomic_exchange then elem when :atomic_store then @ctx.types.fetch("void") when :atomic_compare_exchange then @ctx.types.fetch("bool") end return [atomic_method, nil, callee.receiver, Types::Registry.function(nil, params: [], return_type: ret)] end if (simd_method = simd_method_kind(resolved_receiver_type, callee.member)) ret = case simd_method when :simd_lane_with then resolved_receiver_type end return [simd_method, nil, callee.receiver, Types::Registry.function(nil, params: [], return_type: ret)] end field_receiver_type = infer_field_receiver_type(callee.receiver, env:) if array_type?(field_receiver_type) && callee.member == "as_span" return [:array_as_span, nil, callee.receiver, Types::Registry.span(array_element_type(field_receiver_type))] end member_type = field_receiver_type.respond_to?(:field) ? field_receiver_type.field(callee.member) : nil return [:callable_value, nil, nil, member_type, nil] if callable_type?(member_type) raise LoweringError.new("unknown callee #{callee.receiver}.#{callee.member}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#resolve_named_generic_type(parts) ⇒ Object
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2524 def resolve_named_generic_type(parts) if parts.length == 1 type = @ctx.types[parts.first] return type if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) elsif parts.length >= 2 type = resolve_nested_type_ref(parts) return type if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) if @ctx.imports.key?(parts.first) type = @ctx.imports.fetch(parts.first).types[parts.last] return type if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) end end nil end |
#resolve_named_generic_type_for_analysis(analysis, parts) ⇒ Object
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2115 def resolve_named_generic_type_for_analysis(analysis, parts) if parts.length == 1 type = analysis.types[parts.first] return type if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) elsif parts.length == 2 && analysis.imports.key?(parts.first) type = analysis.imports.fetch(parts.first).types[parts.last] return type if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) end nil end |
#resolve_named_literal_type_argument(type_ref) ⇒ Object
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1490 def resolve_named_literal_type_argument(type_ref) value = case type_ref.name.parts.length when 1 resolve_current_module_const_value(type_ref.name.parts.first) when 2 resolve_imported_module_const_value(type_ref.name.parts.first, type_ref.name.parts.last) end return unless value.is_a?(Integer) || value.is_a?(Float) Types::LiteralTypeArg.new(value) end |
#resolve_nested_type_ref(parts) ⇒ Object
2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2512 def resolve_nested_type_ref(parts) current = @ctx.types[parts.first] return nil unless current.is_a?(Types::Struct) || current.is_a?(Types::GenericStructDefinition) parts[1..].each do |part| nested = current.respond_to?(:nested_types) ? current.nested_types[part] : nil return nil unless nested current = nested end current end |
#resolve_order_specialization(expression, env:) ⇒ Object
1280 1281 1282 1283 1284 1285 1286 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1280 def resolve_order_specialization(expression, env:) target_type = resolve_type_ref(expression.arguments.fetch(0).value) explicit_order = resolve_explicit_order_binding(target_type, context: "order[#{target_type}]") raise LoweringError.new("order[#{target_type}] requires associated function #{target_type}.order(left: const_ptr[#{target_type}], right: const_ptr[#{target_type}]) -> int", line: 0, column: 0, path: @ctx.current_analysis_path) unless explicit_order OrderResolution.new(target_type:, binding: explicit_order.binding, callee_name: explicit_order.callee_name) end |
#resolve_specialization_callee(callee, env) ⇒ Object
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 615 def resolve_specialization_callee(callee, env) if callee.callee.is_a?(AST::Identifier) case callee.callee.name when "reinterpret" target_type = resolve_type_ref(callee.arguments.fetch(0).value) return [:reinterpret, nil, nil, Types::Registry.function("reinterpret", params: [Types::Registry.parameter("value", target_type)], return_type: target_type)] when "array" array_type = resolve_type_ref(AST::TypeRef.new(name: AST::QualifiedName.new(parts: ["array"]), arguments: callee.arguments, nullable: false)) return [:array, nil, nil, array_type] when "simd" simd_type = resolve_type_ref(AST::TypeRef.new(name: AST::QualifiedName.new(parts: ["simd"]), arguments: callee.arguments, nullable: false)) return [:simd, nil, nil, simd_type] when "span" span_type = resolve_type_ref(AST::TypeRef.new(name: AST::QualifiedName.new(parts: ["span"]), arguments: callee.arguments, nullable: false)) return [:struct_literal, nil, nil, span_type] when "zero" target_type = resolve_type_ref(callee.arguments.fetch(0).value) return [:zero, nil, nil, Types::Registry.function("zero", params: [], return_type: target_type)] when "hash" resolution = resolve_hash_specialization(callee, env:) return [:hash, resolution.callee_name, nil, Types::Registry.function("hash", params: [Types::Registry.parameter("value", resolution.target_type)], return_type: @ctx.types.fetch("uint")), resolution.binding] when "equal" resolution = resolve_equal_specialization(callee, env:) params = [ Types::Registry.parameter("left", resolution.target_type), Types::Registry.parameter("right", resolution.target_type), ] return [:equal, resolution.callee_name, nil, Types::Registry.function("equal", params:, return_type: @ctx.types.fetch("bool")), resolution.binding] when "order" resolution = resolve_order_specialization(callee, env:) params = [ Types::Registry.parameter("left", resolution.target_type), Types::Registry.parameter("right", resolution.target_type), ] return [:order, resolution.callee_name, nil, Types::Registry.function("order", params:, return_type: @ctx.types.fetch("int")), resolution.binding] when "attribute_arg" return [:compile_time_builtin, "attribute_arg", nil, compile_time_builtin_specialization_function_type(callee)] when "adapt" raise LoweringError.new("adapt requires exactly one type argument", line: 0, column: 0, path: @ctx.current_analysis_path) unless callee.arguments.length == 1 type_arg = callee.arguments.first.value raise LoweringError.new("adapt type argument must be a type", line: 0, column: 0, path: @ctx.current_analysis_path) unless type_arg.is_a?(AST::TypeRef) parts = type_arg.name.parts type_args = type_arg.arguments.map { |a| a.value } interface = resolve_interface_ref(AST::QualifiedName.new(parts:, type_arguments: type_args)) dyn_type = Types::Dyn.new(interface, interface.respond_to?(:type_arguments) ? (interface.type_arguments || []) : []) return [:adapt, nil, nil, dyn_type, interface] end end if (callable_resolution = resolve_specialized_callable_binding(callee, env:)) callable_kind, function_binding, receiver, method_entry_receiver_type = callable_resolution if callable_kind == :method return [ :method, function_binding_c_name(function_binding, module_name: function_binding.owner.module_name, receiver_type: method_entry_receiver_type), receiver, function_binding.type, function_binding, ] end if function_binding.external return [:function, external_function_c_name(function_binding), nil, function_binding.type, function_binding] end return [:function, function_binding_c_name(function_binding, module_name: function_binding.owner.module_name), nil, function_binding.type, function_binding] end if (type_ref = type_ref_from_specialization(callee)) specialized_type = resolve_type_ref(type_ref) return [:struct_literal, nil, nil, specialized_type] if specialized_type.is_a?(Types::Struct) || task_type?(specialized_type) || specialized_type.is_a?(Types::Vector) || specialized_type.is_a?(Types::Matrix) || specialized_type.is_a?(Types::Quaternion) || specialized_type.is_a?(Types::Simd) end raise LoweringError.new("unsupported specialization callee", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#resolve_specialization_type_arguments(expression) ⇒ Object
1449 1450 1451 1452 1453 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1449 def resolve_specialization_type_arguments(expression) expression.arguments.map do |argument| resolve_type_argument(argument.value) end end |
#resolve_specialized_callable_binding(expression, env:) ⇒ Object
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1203 def resolve_specialized_callable_binding(expression, env:) callable_kind = :function receiver = nil receiver_type = nil binding = case expression.callee when AST::Identifier @ctx.functions[expression.callee.name] when AST::MemberAccess if expression.callee.receiver.is_a?(AST::Identifier) && @ctx.imports.key?(expression.callee.receiver.name) @ctx.imports.fetch(expression.callee.receiver.name).functions[expression.callee.member] elsif (type_expr = resolve_type_expression(expression.callee.receiver)) dispatch_receiver_type = method_dispatch_receiver_type(type_expr) method_entry_receiver_type = type_expr method_entry = @method_definitions[[type_expr, expression.callee.member]] method_entry ||= @method_definitions[[type_expr, "static:#{expression.callee.member}"]] unless method_entry || dispatch_receiver_type == type_expr method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, expression.callee.member]] method_entry ||= @method_definitions[[dispatch_receiver_type, "static:#{expression.callee.member}"]] end if method_entry method_analysis, method_ast = method_entry method_binding = method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) if method_binding.type.receiver_type.nil? receiver_type = type_expr method_binding end end else resolved_receiver_type = infer_method_receiver_type(expression.callee.receiver, env:, member_name: expression.callee.member) dispatch_receiver_type = method_dispatch_receiver_type(resolved_receiver_type) method_entry_receiver_type = resolved_receiver_type method_entry = @method_definitions[[resolved_receiver_type, expression.callee.member]] unless method_entry || dispatch_receiver_type == resolved_receiver_type method_entry_receiver_type = dispatch_receiver_type method_entry = @method_definitions[[dispatch_receiver_type, expression.callee.member]] end if method_entry method_analysis, method_ast = method_entry callable_kind = :method receiver = expression.callee.receiver receiver_type = resolved_receiver_type method_analysis.methods.fetch(method_entry_receiver_type).fetch(method_analysis_key(method_ast)) end end end return nil unless binding type_arguments = resolve_specialization_type_arguments(expression) [callable_kind, instantiate_function_binding_with_receiver(binding, type_arguments, receiver_type:), receiver, method_entry_receiver_type] end |
#resolve_struct_handle_argument(expression, env:) ⇒ Object
1835 1836 1837 1838 1839 1840 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1835 def resolve_struct_handle_argument(expression, env:) type = reflection_type_from_expression(expression, env:) return nil unless type struct_handle_for_type(type) end |
#resolve_type_argument(argument, type_params: current_type_params) ⇒ Object
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1455 def resolve_type_argument(argument, type_params: current_type_params) case argument when AST::TypeRef resolve_type_argument_ref(argument, type_params:) when AST::FunctionType, AST::ProcType, AST::TupleType resolve_type_ref(argument, type_params:) when AST::IntegerLiteral, AST::FloatLiteral Types::LiteralTypeArg.new(argument.value) else raise LoweringError.new("unsupported type argument #{argument.class.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#resolve_type_argument_ref(type_ref, type_params:) ⇒ Object
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1468 def resolve_type_argument_ref(type_ref, type_params:) return resolve_type_ref(type_ref, type_params:) unless literal_type_argument_name_candidate?(type_ref) result = try_resolve_type_ref(type_ref, type_params:) return result if result literal_type_argument = resolve_named_literal_type_argument(type_ref) return literal_type_argument if literal_type_argument resolve_type_ref(type_ref, type_params:) end |
#resolve_type_expression(expression) ⇒ Object
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1161 def resolve_type_expression(expression) case expression when AST::Identifier return current_type_params[expression.name] if current_type_params.key?(expression.name) lookup_named_type(expression.name) when AST::MemberAccess return nil unless expression.receiver.is_a?(AST::Identifier) if @ctx.imports.key?(expression.receiver.name) return @ctx.imports.fetch(expression.receiver.name).types[expression.member] end parent_type = @ctx.types[expression.receiver.name] return parent_type.nested_types[expression.member] if parent_type.respond_to?(:nested_types) && parent_type.nested_types.key?(expression.member) nil when AST::Specialization type_ref = type_ref_from_specialization(expression) return nil unless type_ref resolve_type_ref(type_ref) end end |
#resolve_type_member(type, name) ⇒ Object
1186 1187 1188 1189 1190 1191 1192 1193 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1186 def resolve_type_member(type, name) case type when Types::Enum, Types::Flags type.member(name) when Types::Variant type if type.arm_names.include?(name) end end |
#resolve_type_member_const_value(expression) ⇒ Object
1523 1524 1525 1526 1527 1528 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1523 def resolve_type_member_const_value(expression) type = resolve_type_expression(expression.receiver) return unless type.is_a?(Types::EnumBase) type.member_value(expression.member) end |
#resolve_type_ref(type_ref, type_params: current_type_params) ⇒ Object
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2411 def resolve_type_ref(type_ref, type_params: current_type_params) if type_ref.is_a?(AST::FunctionType) params = type_ref.params.map do |param| Types::Registry.parameter(param.name, resolve_type_ref(param.type, type_params:)) end return Types::Registry.function(nil, params:, return_type: resolve_type_ref(type_ref.return_type, type_params:)) end if type_ref.is_a?(AST::ProcType) params = type_ref.params.map do |param| Types::Registry.parameter(param.name, resolve_type_ref(param.type, type_params:)) end return Types::Registry.proc(params:, return_type: resolve_type_ref(type_ref.return_type, type_params:)) end if type_ref.is_a?(AST::DynType) interface = resolve_interface_ref(type_ref.interface) raise LoweringError.new("generic interface requires type arguments", line: 0, column: 0, path: @ctx.current_analysis_path) if interface.respond_to?(:instantiate) type_arguments = interface.respond_to?(:type_arguments) ? (interface.type_arguments || []) : [] return Types::Dyn.new(interface, type_arguments) end if type_ref.is_a?(AST::TupleType) names = [] element_types = [] type_ref.element_types.each do |et| if et.is_a?(AST::Argument) names << et.name element_types << resolve_type_ref(et.value, type_params:) else names << nil element_types << resolve_type_ref(et, type_params:) end end has_named = names.any? return Types::Registry.tuple(element_types, field_names: has_named ? names : nil) end parts = type_ref.name.parts base = if type_ref.arguments.any? name = parts.join(".") args = type_ref.arguments.map { |argument| resolve_type_argument(argument.value, type_params:) } if name != "ref" && args.any? { |argument| contains_ref_type?(argument) && !stored_ref_supported_type?(argument) } raise LoweringError.new("ref types cannot be nested inside #{name}", line: 0, column: 0, path: @ctx.current_analysis_path) end if name == "Task" validate_generic_type!(name, args) Types::Registry.task(args.fetch(0)) elsif (generic_type = resolve_named_generic_type(parts)) generic_type.instantiate(args) elsif name == "span" Types::Registry.span(args.fetch(0)) elsif name == "SoA" validate_generic_type!(name, args) Types::Registry.soa(args.fetch(0), count: args.fetch(1).value) elsif name == "simd" validate_generic_type!(name, args) Types::Registry.simd(args.fetch(0), lane_count: args.fetch(1).value) else validate_generic_type!(name, args) args = [type_ref.lifetime] + args if name == "ref" && type_ref.lifetime Types::Registry.generic_instance(name, args) end elsif parts.length == 1 && type_params.key?(parts.first) type_params.fetch(parts.first) elsif parts.length == 1 type = lookup_named_type(parts.first) raise LoweringError.new("unknown type #{parts.first}", line: 0, column: 0, path: @ctx.current_analysis_path) unless type raise LoweringError.new("generic type #{parts.first} requires type arguments", line: 0, column: 0, path: @ctx.current_analysis_path) if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) type elsif parts.length >= 2 type = resolve_nested_type_ref(parts) unless type if @ctx.imports.key?(parts.first) imported_module = @ctx.imports.fetch(parts.first) if imported_module.private_type?(parts.last) raise LoweringError.new("#{parts.first}.#{parts.last} is private to module #{imported_module.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end type = imported_module.types[parts.last] raise LoweringError.new("unknown type #{type_ref.name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless type raise LoweringError.new("generic type #{type_ref.name} requires type arguments", line: 0, column: 0, path: @ctx.current_analysis_path) if type.is_a?(Types::GenericStructDefinition) || type.is_a?(Types::GenericVariantDefinition) elsif @type_resolution_env && (field_type = resolve_field_handle_type_ref(parts)) type = field_type else raise LoweringError.new("unknown type #{type_ref.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end type else raise LoweringError.new("unknown type #{type_ref.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end raise LoweringError.new("ref types are non-null and cannot be nullable", line: 0, column: 0, path: @ctx.current_analysis_path) if type_ref.nullable && ref_type?(base) type_ref.nullable ? Types::Registry.nullable(base) : base end |
#resolve_type_ref_for_analysis(type_ref, analysis, type_params: current_type_params) ⇒ Object
2379 2380 2381 2382 2383 2384 2385 2386 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2379 def resolve_type_ref_for_analysis(type_ref, analysis, type_params: current_type_params) saved = @ctx.save @ctx.install(analysis) @ctx.module_prefix = module_c_prefix(@ctx.module_name) resolve_type_ref(type_ref, type_params:) ensure @ctx.restore(saved) end |
#resolved_attribute_applications_for_target(target) ⇒ Object
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1946 def resolved_attribute_applications_for_target(target) target_id = case target when Types::StructHandle then target.declaration.object_id when Types::FieldHandle then target.field_declaration.object_id when Types::CallableHandle then target.declaration.object_id end return [] unless target_id applications = @ctx.attribute_applications[target_id] return applications if applications @ctx.imports.each_value do |imported_module| applications = imported_module.attribute_applications[target_id] return applications if applications end [] end |
#same_attribute_binding?(left, right) ⇒ Boolean
1971 1972 1973 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1971 def same_attribute_binding?(left, right) left.name == right.name && left.module_name == right.module_name end |
#simulate_cstr_metadata_block(statements, env:) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 296 def (statements, env:) simulated_env = duplicate_env(env) statements.each do |statement| case statement when AST::LocalDecl storage_type = if statement.else_body infer_expression_type(statement.value, env: simulated_env) elsif statement.type resolve_type_ref(statement.type) else infer_expression_type(statement.value, env: simulated_env) end type = if statement.else_body statement.type ? resolve_type_ref(statement.type) : let_else_success_type(storage_type) else storage_type end unless let_else_discards_binding?(statement) current_actual_scope(simulated_env[:scopes])[statement.name] = local_binding( type:, storage_type:, linkage_name: c_local_name(statement.name), mutable: statement.kind == :var, pointer: false, projection: statement.else_body ? let_else_binding_projection(storage_type) : nil, cstr_backed: cstr_backed_storage_value?(storage_type, statement.value, simulated_env), cstr_list_backed: cstr_list_backed_storage_value?(storage_type, statement.value, simulated_env), const_value: statement.else_body ? nil : statement.kind == :let && statement.value ? compile_time_const_value(statement.value, env: simulated_env) : nil, ) end when AST::Assignment (statement, statement.value, simulated_env) when AST::IfStmt (statement, simulated_env) when AST::UnsafeStmt nested_env = (statement.body, env: simulated_env) return nil unless nested_env (simulated_env, nested_env) when AST::ReturnStmt, AST::BreakStmt, AST::ContinueStmt return nil end end simulated_env end |
#specialize_function_binding(binding, arguments, env, receiver_type: nil) ⇒ Object
1979 1980 1981 1982 1983 1984 1985 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1979 def specialize_function_binding(binding, arguments, env, receiver_type: nil) return binding if binding.type_params.empty? raise LoweringError.new("generic function #{binding.name} must be called", line: 0, column: 0, path: @ctx.current_analysis_path) unless arguments type_arguments = infer_function_type_arguments(binding, arguments, env, receiver_type:) instantiate_function_binding(binding, type_arguments) end |
#struct_handle_for_type(type) ⇒ Object
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1866 def struct_handle_for_type(type) base_type = type.is_a?(Types::StructInstance) ? type.definition : type return nil unless base_type.is_a?(Types::Struct) || base_type.is_a?(Types::GenericStructDefinition) return nil unless base_type.respond_to?(:module_name) analysis = analysis_for_module(base_type.module_name) declaration = find_struct_decl_by_name(analysis.ast.declarations, base_type.name) return nil unless declaration Types::StructHandle.new(base_type, declaration) end |
#substitute_type(type, substitutions) ⇒ Object
2345 2346 2347 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2345 def substitute_type(type, substitutions) Types::SubstituteTypeVisitor.new(substitutions).apply(type) end |
#substitute_value_binding(binding, substitutions) ⇒ Object
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2333 def substitute_value_binding(binding, substitutions) Bindings::ValueBinding.new( id: binding.id, name: binding.name, storage_type: substitute_type(binding.storage_type, substitutions), flow_type: binding.flow_type ? substitute_type(binding.flow_type, substitutions) : nil, mutable: binding.mutable, kind: binding.kind, const_value: binding.const_value, ) end |
#trackable_binding_names(env) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 369 def trackable_binding_names(env) env[:scopes].each_with_object([]) do |scope, names| next if scope.is_a?(FlowScope) scope.each do |name, binding| next unless cstr_trackable_type?(binding[:type]) || cstr_list_trackable_type?(binding[:type]) names << name unless names.include?(name) end end end |
#try_resolve_type_ref(type_ref, type_params:) ⇒ Object
1480 1481 1482 1483 1484 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1480 def try_resolve_type_ref(type_ref, type_params:) resolve_type_ref(type_ref, type_params:) rescue LoweringError nil end |
#type_contains_array_storage?(type) ⇒ Boolean
1060 1061 1062 1063 1064 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 1060 def type_contains_array_storage?(type) visitor = Types::ContainsArrayStorageVisitor.new visitor.visit(type) visitor.found? end |
#type_implements_interface?(type, interface) ⇒ Boolean
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2073 def type_implements_interface?(type, interface) key = interface_implementation_key(type) return true if @ctx.implemented_interfaces.fetch(key, []).include?(interface) @ctx.imports.each_value do |module_binding| return true if module_binding.implemented_interfaces.fetch(key, []).include?(interface) end false end |
#types_for_module(module_name) ⇒ Object
2363 2364 2365 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2363 def types_for_module(module_name) @program.analyses_by_module_name.fetch(module_name).types end |
#update_cstr_metadata_for_assignment!(statement, prepared_value, env) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 235 def (statement, prepared_value, env) if statement.target.is_a?(AST::Identifier) binding = lookup_value(statement.target.name, env) return unless binding ( statement.target.name, env, cstr_backed: statement.operator == "=" ? cstr_backed_storage_value?(binding[:type], prepared_value, env) : false, cstr_list_backed: statement.operator == "=" ? cstr_list_backed_storage_value?(binding[:type], prepared_value, env) : false, ) return end return unless statement.target.is_a?(AST::IndexAccess) && statement.target.receiver.is_a?(AST::Identifier) binding = lookup_value(statement.target.receiver.name, env) return unless binding && cstr_list_trackable_type?(binding[:type]) (statement.target.receiver.name, env, cstr_backed: binding_cstr_backed?(binding), cstr_list_backed: false) end |
#validate_function_type_param_constraints!(binding, substitutions) ⇒ Object
2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2053 def validate_function_type_param_constraints!(binding, substitutions) binding.type_param_constraints.each do |name, constraints| actual_type = substitutions[name] raise LoweringError.new("cannot infer type argument #{name} for function #{binding.name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless actual_type constraints.interfaces.each do |interface| next if type_implements_interface?(actual_type, interface) raise LoweringError.new("type #{actual_type} does not implement interface #{interface.name} for function #{binding.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end end |
#validate_methods_receiver_type_arguments!(type_ref, generic_type) ⇒ Object
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2127 def validate_methods_receiver_type_arguments!(type_ref, generic_type) names = type_ref.arguments.map do |argument| value = argument.value next unless value.is_a?(AST::TypeRef) next unless value.arguments.empty? && !value.nullable && value.name.parts.length == 1 value.name.parts.first end expected_names = generic_type.type_params unless names == expected_names raise LoweringError.new("extending target #{type_ref} must use the receiver type parameters directly", line: 0, column: 0, path: @ctx.current_analysis_path) end expected_names end |
#value_param_declared_type(name) ⇒ Object
2397 2398 2399 2400 2401 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2397 def value_param_declared_type(name) return nil unless value_param_substitution(name) @ctx.current_value_type_params&.[](name) end |
#value_param_literal(name, expected_type = nil) ⇒ Object
2403 2404 2405 2406 2407 2408 2409 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2403 def value_param_literal(name, expected_type = nil) substitution = value_param_substitution(name) return nil unless substitution && substitution.value.is_a?(Integer) type = @ctx.current_value_type_params&.[](name) || expected_type || @ctx.types.fetch("int") IR::IntegerLiteral.new(value: substitution.value, type:) end |
#value_param_substitution(name) ⇒ Object
2392 2393 2394 2395 |
# File 'lib/milk_tea/core/lowering/resolve.rb', line 2392 def value_param_substitution(name) substitution = current_type_params[name] substitution.is_a?(Types::LiteralTypeArg) ? substitution : nil end |