Module: MilkTea::LowererUtils
- Included in:
- Lowerer
- Defined in:
- lib/milk_tea/core/lowering/utils.rb
Instance Method Summary collapse
- #addressable_storage_expression?(expression) ⇒ Boolean
- #array_element_type(type) ⇒ Object
- #array_length(type) ⇒ Object
- #array_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
- #array_type?(type) ⇒ Boolean
- #bind_let_else_local?(statement) ⇒ Boolean
- #c_local_name(name) ⇒ Object
- #c_reserved_identifier?(identifier) ⇒ Boolean
- #c_type_name(type) ⇒ Object
- #callable_type?(type) ⇒ Boolean
- #cfg_block_always_terminates?(statements) ⇒ Boolean
- #char_array_text_type?(type) ⇒ Boolean
- #cleanup_safe_return_expression?(expression) ⇒ Boolean
- #cleanup_statements(local_defers, outer_defers) ⇒ Object
- #collection_loop_binding_type(iterable_type, element_type) ⇒ Object
- #collection_loop_item_value(iterable_ref, iterable_type, index_ref, element_type) ⇒ Object
- #collection_loop_ref_element_type?(type) ⇒ Boolean
- #collection_loop_stop_value(iterable_ref, iterable_type) ⇒ Object
- #collection_loop_type(type) ⇒ Object
- #compile_time_builtin_function_type(name, arguments, env) ⇒ Object
- #compile_time_builtin_specialization_function_type(callee) ⇒ Object
- #conditional_common_type(then_type, else_type) ⇒ Object
- #conditional_null_common_type(null_type, other_type) ⇒ Object
- #contains_label_target?(statements, label) ⇒ Boolean
- #contains_proc_storage_type?(type, visited = Set.new) ⇒ Boolean
- #contains_task_type?(type, visited = Set.new) ⇒ Boolean
- #contains_type_var?(type) ⇒ Boolean
- #cstr_list_trackable_type?(type) ⇒ Boolean
- #cstr_trackable_type?(type) ⇒ Boolean
- #current_actual_scope(scopes) ⇒ Object
- #duplicate_env(env) ⇒ Object
- #empty_env ⇒ Object
- #enum_member_c_name(type, member_name) ⇒ Object
- #env_with_refinements(env, refinements) ⇒ Object
- #external_function_c_name(binding) ⇒ Object
- #flow_refinements(expression, truthy:, env:) ⇒ Object
- #forward_declarable_external_opaque?(type) ⇒ Boolean
- #fresh_c_temp_name(env, prefix) ⇒ Object
- #fresh_proc_symbol ⇒ Object
- #function_binding_c_name(binding, module_name:, receiver_type: nil) ⇒ Object
- #generic_integer_type_argument?(argument) ⇒ Boolean
-
#generic_type_argument_suffix(type_arguments) ⇒ Object
Joins resolved generic type arguments into the instance-name suffix used by both the free-function (
module_function_c_name) and method (function_binding_c_name) paths, keeping the scheme consistent. - #if_expression_branch_compatible?(actual_type, expected_type) ⇒ Boolean
- #imported_value_c_name(imported_module, name) ⇒ Object
- #infer_field_receiver_type(receiver_expression, env:) ⇒ Object
- #infer_index_result_type(receiver_type, index_type) ⇒ Object
- #infer_method_receiver_type(receiver_expression, env:, member_name: nil) ⇒ Object
- #infer_option_propagation_details(storage_type, env:, allow_void_success:) ⇒ Object
- #infer_range_loop_type(expression, env:) ⇒ Object
- #infer_result_propagation_details(storage_type, env:, allow_void_success:) ⇒ Object
- #infer_result_propagation_type(expression, env:) ⇒ Object
- #infer_result_propagation_types(expression, env:, allow_void_success: false) ⇒ Object
- #infer_value_type(handle_expression, env:) ⇒ Object
- #integer_type?(type) ⇒ Boolean
- #integer_type_argument?(argument) ⇒ Boolean
- #let_else_binding_projection(type) ⇒ Object
- #let_else_discard_binding_syntax?(statement) ⇒ Boolean
- #let_else_error_type(type) ⇒ Object
- #let_else_failure_condition(storage_expr, storage_type) ⇒ Object
- #let_else_storage_c_name(statement, env) ⇒ Object
- #let_else_success_type(type) ⇒ Object
- #local_binding(type:, linkage_name:, mutable:, pointer:, storage_type: nil, projection: nil, cstr_backed: false, cstr_list_backed: false, const_value: nil) ⇒ Object
- #lookup_value(name, env) ⇒ Object
- #loop_exit_break(label = nil) ⇒ Object
- #loop_exit_continue(label = nil) ⇒ Object
- #loop_exit_label(label) ⇒ Object
- #loop_exit_statement(target, local_defers:, outer_defers:) ⇒ Object
- #loop_flow(break_target:, continue_target:, break_defers: [], continue_defers: []) ⇒ Object
- #lower_assignment_binding_target(binding) ⇒ Object
- #lower_assignment_target(expression, env:) ⇒ Object
- #lower_bound_identifier(binding, expected_type: nil) ⇒ Object
- #lower_compile_time_literal(value, type) ⇒ Object
- #lower_defer_cleanup_body(statements, env:, return_type:) ⇒ Object
- #lower_fatal_statement(message, env:) ⇒ Object
- #lower_loop_exit(target, local_defers, outer_defers) ⇒ Object
- #lower_range_match_condition(range_pattern, scrutinee_ir, bool_type, env:) ⇒ Object
- #lower_static_storage_initializer(expression, env:, expected_type: nil) ⇒ Object
- #merge_refinements(existing, incoming) ⇒ Object
- #module_c_prefix(module_name) ⇒ Object
- #module_function_c_name(module_name, name, type_arguments: []) ⇒ Object
- #module_value_c_name(module_name, name) ⇒ Object
- #nested_loop_flow(current_loop_flow, local_defers) ⇒ Object
- #null_test_refinements(expression, truthy:, env:) ⇒ Object
- #null_type ⇒ Object
- #nullable_candidate?(type) ⇒ Boolean
- #nullable_some_literal(nullable_type, value) ⇒ Object
- #opaque_c_type_name(type) ⇒ Object
- #opaque_forward_declarable?(type) ⇒ Boolean
- #option_let_else_type?(type) ⇒ Boolean
- #pointer_like_type?(type) ⇒ Boolean
- #pointer_to(type) ⇒ Object
- #proc_env_pointer_type ⇒ Object
- #proc_invoke_function_type(proc_type) ⇒ Object
- #proc_release_function_type ⇒ Object
- #proc_retain_function_type ⇒ Object
- #range_end_of(iterable) ⇒ Object
- #range_iterable?(expression) ⇒ Boolean
- #range_start_of(iterable) ⇒ Object
- #read_call?(expression) ⇒ Boolean
- #result_let_else_type?(type) ⇒ Boolean
- #rewrite_static_storage_initializer(expression) ⇒ Object
- #sanitize_identifier(text) ⇒ Object
- #scopes_with_refinements(scopes, refinements) ⇒ Object
- #simd_type?(type) ⇒ Boolean
- #snapshot_env(env) ⇒ Object
- #stored_ref_supported_type?(type, visited = {}) ⇒ Boolean
- #str_buffer_capacity(type) ⇒ Object
- #str_buffer_storage_capacity(type) ⇒ Object
- #str_buffer_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
- #str_buffer_type?(type) ⇒ Boolean
- #struct_contains_string_field?(type) ⇒ Boolean
- #suppress_format_releases_for_assignment(cleanups, target_type) ⇒ Object
- #switch_loop_flow(current_loop_flow, local_defers) ⇒ Object
- #switch_loop_target(target) ⇒ Object
- #terminating_ir_statement?(statement) ⇒ Boolean
- #validate_generic_type!(name, arguments) ⇒ Object
- #value_c_name(name) ⇒ Object
- #variant_binding_projection_expression(storage_expr, storage_type, arm_name, field_name, field_type) ⇒ Object
- #variant_match_arm_name_from_pattern(pattern) ⇒ Object
- #wildcard_arm_pattern?(expression) ⇒ Boolean
- #with_analysis_context(analysis) ⇒ Object
- #wrap_nullable_field_value(field_type, lowered_value, env) ⇒ Object
Instance Method Details
#addressable_storage_expression?(expression) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 110 def addressable_storage_expression?(expression) case expression when AST::Identifier true when AST::MemberAccess, AST::IndexAccess addressable_storage_expression?(expression.receiver) when AST::Call read_call?(expression) else false end end |
#array_element_type(type) ⇒ Object
48 49 50 51 52 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 48 def array_element_type(type) return unless array_type?(type) type.arguments.first end |
#array_length(type) ⇒ Object
87 88 89 90 91 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 87 def array_length(type) return unless array_type?(type) type.arguments[1].value end |
#array_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
54 55 56 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 54 def array_to_span_compatible?(actual_type, expected_type) array_type?(actual_type) && expected_type.is_a?(Types::Span) && array_element_type(actual_type) == expected_type.element_type end |
#array_type?(type) ⇒ Boolean
39 40 41 42 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 39 def array_type?(type) type.is_a?(Types::GenericInstance) && type.name == "array" && type.arguments.length == 2 && type.arguments[1].is_a?(Types::LiteralTypeArg) end |
#bind_let_else_local?(statement) ⇒ Boolean
791 792 793 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 791 def bind_let_else_local?(statement) !let_else_discard_binding_syntax?(statement) end |
#c_local_name(name) ⇒ Object
1099 1100 1101 1102 1103 1104 1105 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1099 def c_local_name(name) return "value" unless name identifier = sanitize_identifier(name) return "#{identifier}_" if c_reserved_identifier?(identifier) identifier end |
#c_reserved_identifier?(identifier) ⇒ Boolean
1107 1108 1109 1110 1111 1112 1113 1114 1115 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1107 def c_reserved_identifier?(identifier) %w[ auto break case char const continue default do double else enum extern float for goto if inline int long register restrict return short signed sizeof static struct switch typedef union unsigned void volatile while _Alignas _Alignof _Atomic _Bool _Complex _Generic _Imaginary _Noreturn _Static_assert _Thread_local ].include?(identifier) end |
#c_type_name(type) ⇒ Object
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 983 def c_type_name(type) if type.is_a?(Types::Nullable) return "nullable_#{c_type_name(type.base)}" end if type.respond_to?(:linkage_name) && type.linkage_name return type.linkage_name end if type.is_a?(Types::GenericInstance) base = if type.respond_to?(:module_name) && type.module_name&.start_with?("std.c.") type.name elsif type.respond_to?(:module_name) && !type.module_name.nil? "#{module_c_prefix(type.module_name)}_#{type.name}" else type.name end return "#{base}_#{sanitize_identifier(type.arguments.join('_'))}" end return type.name if type.respond_to?(:module_name) && type.module_name&.start_with?("std.c.") base = (type.respond_to?(:module_name) && type.module_name) ? "#{module_c_prefix(type.module_name)}_#{type.name}" : type.name return base unless type.is_a?(Types::StructInstance) || type.is_a?(Types::VariantInstance) "#{base}_#{sanitize_identifier(type.arguments.join('_'))}" end |
#callable_type?(type) ⇒ Boolean
439 440 441 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 439 def callable_type?(type) type.is_a?(Types::Function) || proc_type?(type) end |
#cfg_block_always_terminates?(statements) ⇒ Boolean
609 610 611 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 609 def cfg_block_always_terminates?(statements) ControlFlow::Termination.block_always_terminates?(statements, ignore_name: ->(_name) { false }) end |
#char_array_text_type?(type) ⇒ Boolean
93 94 95 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 93 def char_array_text_type?(type) array_type?(type) && array_element_type(type) == @ctx.types.fetch("char") end |
#cleanup_safe_return_expression?(expression) ⇒ Boolean
1122 1123 1124 1125 1126 1127 1128 1129 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1122 def cleanup_safe_return_expression?(expression) case expression when AST::IntegerLiteral, AST::FloatLiteral, AST::StringLiteral, AST::BooleanLiteral, AST::NullLiteral true else false end end |
#cleanup_statements(local_defers, outer_defers) ⇒ Object
703 704 705 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 703 def cleanup_statements(local_defers, outer_defers) local_defers.reverse.flat_map(&:itself) + outer_defers.reverse.flat_map(&:itself) end |
#collection_loop_binding_type(iterable_type, element_type) ⇒ Object
161 162 163 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 161 def collection_loop_binding_type(iterable_type, element_type) super end |
#collection_loop_item_value(iterable_ref, iterable_type, index_ref, element_type) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 169 def collection_loop_item_value(iterable_ref, iterable_type, index_ref, element_type) if array_type?(iterable_type) IR::Index.new(receiver: iterable_ref, index: index_ref, type: element_type) else data_ref = IR::Member.new(receiver: iterable_ref, member: "data", type: pointer_to(element_type)) IR::Index.new(receiver: data_ref, index: index_ref, type: element_type) end end |
#collection_loop_ref_element_type?(type) ⇒ Boolean
165 166 167 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 165 def collection_loop_ref_element_type?(type) super end |
#collection_loop_stop_value(iterable_ref, iterable_type) ⇒ Object
178 179 180 181 182 183 184 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 178 def collection_loop_stop_value(iterable_ref, iterable_type) if array_type?(iterable_type) IR::IntegerLiteral.new(value: array_length(iterable_type), type: @ctx.types.fetch("ptr_uint")) else IR::Member.new(receiver: iterable_ref, member: "len", type: @ctx.types.fetch("ptr_uint")) end end |
#collection_loop_type(type) ⇒ Object
157 158 159 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 157 def collection_loop_type(type) super end |
#compile_time_builtin_function_type(name, arguments, env) ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 354 def compile_time_builtin_function_type(name, arguments, env) return_type = case name when "field_of" @ctx.types.fetch("field_handle") when "callable_of" @ctx.types.fetch("callable_handle") when "has_attribute" @ctx.types.fetch("bool") when "attribute_of" @ctx.types.fetch("attribute_handle") else nil end raise LoweringError.new("unsupported compile-time builtin #{name}", line: 0, column: 0, path: @ctx.current_analysis_path) unless return_type Types::Registry.function(name, params: [], return_type: return_type) end |
#compile_time_builtin_specialization_function_type(callee) ⇒ Object
372 373 374 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 372 def compile_time_builtin_specialization_function_type(callee) Types::Registry.function("attribute_arg", params: [], return_type: resolve_type_ref(callee.arguments.fetch(0).value)) end |
#conditional_common_type(then_type, else_type) ⇒ Object
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 613 def conditional_common_type(then_type, else_type) return then_type if then_type == else_type numeric_type = common_numeric_type(then_type, else_type) return numeric_type if numeric_type if (nullable_type = conditional_null_common_type(then_type, else_type)) return nullable_type end if (nullable_type = conditional_null_common_type(else_type, then_type)) return nullable_type end return then_type if then_type.is_a?(Types::Nullable) && else_type == then_type.base return else_type if else_type.is_a?(Types::Nullable) && then_type == else_type.base nil end |
#conditional_null_common_type(null_type, other_type) ⇒ Object
646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 646 def conditional_null_common_type(null_type, other_type) return unless null_type.is_a?(Types::Null) if other_type.is_a?(Types::Nullable) return other_type if null_type.target_type.nil? || null_type.target_type == other_type.base return nil end return unless nullable_candidate?(other_type) return if null_type.target_type && null_type.target_type != other_type Types::Registry.nullable(other_type) end |
#contains_label_target?(statements, label) ⇒ Boolean
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 746 def contains_label_target?(statements, label) statements.any? do |statement| case statement when IR::GotoStmt statement.label == label when IR::BlockStmt, IR::WhileStmt, IR::ForStmt contains_label_target?(statement.body, label) when IR::IfStmt contains_label_target?(statement.then_body, label) || (statement.else_body && contains_label_target?(statement.else_body, label)) when IR::SwitchStmt statement.cases.any? { |switch_case| contains_label_target?(switch_case.body, label) } else false end end end |
#contains_proc_storage_type?(type, visited = Set.new) ⇒ Boolean
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 443 def contains_proc_storage_type?(type, visited = Set.new) return false if visited.include?(type.object_id) case type when Types::Proc true when Types::Struct, Types::StructInstance visited.add(type.object_id) type.fields.each_value.any? { |field_type| contains_proc_storage_type?(field_type, visited) } when Types::Nullable contains_proc_storage_type?(type.base, visited) else false end end |
#contains_task_type?(type, visited = Set.new) ⇒ Boolean
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 459 def contains_task_type?(type, visited = Set.new) return false if visited.include?(type.object_id) case type when Types::Task true when Types::Struct, Types::StructInstance, Types::Union, Types::GenericStructDefinition, Types::VariantArmPayload visited.add(type.object_id) type.fields.each_value.any? { |ft| contains_task_type?(ft, visited) } when Types::VariantInstance type.arguments.any? { |arg| contains_task_type?(arg, visited) } when Types::Variant, Types::GenericVariantDefinition visited.add(type.object_id) type.arms.each_value.any? do |arm_fields| arm_fields.each_value.any? { |ft| contains_task_type?(ft, visited) } end when Types::GenericInstance type.arguments.any? { |arg| contains_task_type?(arg, visited) } when Types::Nullable contains_task_type?(type.base, visited) else false end end |
#contains_type_var?(type) ⇒ Boolean
250 251 252 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 250 def contains_type_var?(type) super end |
#cstr_list_trackable_type?(type) ⇒ Boolean
76 77 78 79 80 81 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 76 def cstr_list_trackable_type?(type) return false unless array_type?(type) element_type = array_element_type(type) element_type == @ctx.types.fetch("str") || element_type == @ctx.types.fetch("cstr") end |
#cstr_trackable_type?(type) ⇒ Boolean
58 59 60 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 58 def cstr_trackable_type?(type) type == @ctx.types.fetch("str") || type == @ctx.types.fetch("cstr") end |
#current_actual_scope(scopes) ⇒ Object
516 517 518 519 520 521 522 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 516 def current_actual_scope(scopes) scopes.reverse_each do |scope| return scope unless scope.is_a?(FlowScope) end raise LoweringError.new("missing lexical scope", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#duplicate_env(env) ⇒ Object
779 780 781 782 783 784 785 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 779 def duplicate_env(env) duplicated = env.dup duplicated[:scopes] = env[:scopes].map(&:dup) + [{}] duplicated[:counter] = env[:counter] duplicated.delete(:prepared_expression_cleanups) duplicated end |
#empty_env ⇒ Object
771 772 773 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 771 def empty_env { scopes: [{}], counter: { value: 0 } } end |
#enum_member_c_name(type, member_name) ⇒ Object
1038 1039 1040 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1038 def enum_member_c_name(type, member_name) "#{c_type_name(type)}_#{member_name}" end |
#env_with_refinements(env, refinements) ⇒ Object
524 525 526 527 528 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 524 def env_with_refinements(env, refinements) updated = env.dup updated[:scopes] = scopes_with_refinements(env[:scopes], refinements) updated end |
#external_function_c_name(binding) ⇒ Object
1055 1056 1057 1058 1059 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1055 def external_function_c_name(binding) return binding.ast.mapping.value if binding.external && binding.ast.is_a?(AST::ExternFunctionDecl) && binding.ast.mapping binding.name end |
#flow_refinements(expression, truthy:, env:) ⇒ Object
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 563 def flow_refinements(expression, truthy:, env:) case expression when AST::UnaryOp return flow_refinements(expression.operand, truthy: !truthy, env:) if expression.operator == "not" when AST::BinaryOp case expression.operator when "and" if truthy left_truthy = flow_refinements(expression.left, truthy: true, env:) right_env = env_with_refinements(env, left_truthy) right_truthy = flow_refinements(expression.right, truthy: true, env: right_env) return merge_refinements(left_truthy, right_truthy) end when "or" unless truthy left_falsy = flow_refinements(expression.left, truthy: false, env:) right_env = env_with_refinements(env, left_falsy) right_falsy = flow_refinements(expression.right, truthy: false, env: right_env) return merge_refinements(left_falsy, right_falsy) end when "==", "!=" return null_test_refinements(expression, truthy:, env:) end end {} end |
#forward_declarable_external_opaque?(type) ⇒ Boolean
1022 1023 1024 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1022 def forward_declarable_external_opaque?(type) type.external && opaque_forward_declarable?(type) end |
#fresh_c_temp_name(env, prefix) ⇒ Object
1117 1118 1119 1120 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1117 def fresh_c_temp_name(env, prefix) env[:counter][:value] += 1 "__mt_#{prefix}_#{env[:counter][:value]}" end |
#fresh_proc_symbol ⇒ Object
512 513 514 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 512 def fresh_proc_symbol @synthetic_proc_counter += 1 end |
#function_binding_c_name(binding, module_name:, receiver_type: nil) ⇒ Object
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1042 def function_binding_c_name(binding, module_name:, receiver_type: nil) if receiver_type.nil? && binding.name == "main" && binding.type_arguments.empty? return binding.async ? module_function_c_name(module_name, "__async_main") : module_function_c_name(module_name, "main") end if receiver_type base = "#{c_type_name(receiver_type)}_#{binding.name}" base = "#{base}_static" if binding.type.receiver_type.nil? return binding.type_arguments.empty? ? base : "#{base}__#{generic_type_argument_suffix(binding.type_arguments)}" end module_function_c_name(module_name, binding.name, type_arguments: binding.type_arguments) end |
#generic_integer_type_argument?(argument) ⇒ Boolean
1034 1035 1036 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1034 def generic_integer_type_argument?(argument) integer_type_argument?(argument) || argument.is_a?(Types::TypeVar) end |
#generic_type_argument_suffix(type_arguments) ⇒ Object
Joins resolved generic type arguments into the instance-name suffix used
by both the free-function (module_function_c_name) and method
(function_binding_c_name) paths, keeping the scheme consistent.
1087 1088 1089 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1087 def generic_type_argument_suffix(type_arguments) sanitize_identifier(type_arguments.join('_')) end |
#if_expression_branch_compatible?(actual_type, expected_type) ⇒ Boolean
633 634 635 636 637 638 639 640 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 633 def if_expression_branch_compatible?(actual_type, expected_type) return true if actual_type == expected_type return true if null_assignable_to?(actual_type, expected_type) return true if expected_type.is_a?(Types::Nullable) && actual_type == expected_type.base return true if common_numeric_type(actual_type, expected_type) == expected_type false end |
#imported_value_c_name(imported_module, name) ⇒ Object
1065 1066 1067 1068 1069 1070 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1065 def imported_value_c_name(imported_module, name) imported_analysis = analysis_for_module(imported_module.name) return name if imported_analysis.module_kind == :raw_module module_value_c_name(imported_module.name, name) end |
#infer_field_receiver_type(receiver_expression, env:) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 149 def infer_field_receiver_type(receiver_expression, env:) receiver_type = infer_expression_type(receiver_expression, env:) return referenced_type(receiver_type) if ref_type?(receiver_type) return pointee_type(receiver_type) if pointer_type?(receiver_type) receiver_type end |
#infer_index_result_type(receiver_type, index_type) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 222 def infer_index_result_type(receiver_type, index_type) raise LoweringError.new("index must be an integer type, got #{index_type}", line: 0, column: 0, path: @ctx.current_analysis_path) unless integer_type?(index_type) receiver_type = referenced_type(receiver_type) if ref_type?(receiver_type) if array_type?(receiver_type) return array_element_type(receiver_type) end if receiver_type.is_a?(Types::Span) return receiver_type.element_type end if receiver_type.is_a?(Types::SoA) return receiver_type.element_type end if simd_type?(receiver_type) return receiver_type.element_type end if pointer_type?(receiver_type) return pointee_type(receiver_type) end raise LoweringError.new("cannot index #{receiver_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#infer_method_receiver_type(receiver_expression, env:, member_name: nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 135 def infer_method_receiver_type(receiver_expression, env:, member_name: nil) receiver_type = infer_expression_type(receiver_expression, env:) receiver_type = referenced_type(receiver_type) if ref_type?(receiver_type) if pointer_type?(receiver_type) dispatch_receiver_type = method_dispatch_receiver_type(receiver_type) return receiver_type if member_name && (@method_definitions.key?([receiver_type, member_name]) || @method_definitions.key?([dispatch_receiver_type, member_name]) || @method_definitions.key?([receiver_type, "static:#{member_name}"]) || @method_definitions.key?([dispatch_receiver_type, "static:#{member_name}"])) return pointee_type(receiver_type) end receiver_type end |
#infer_option_propagation_details(storage_type, env:, allow_void_success:) ⇒ Object
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 967 def infer_option_propagation_details(storage_type, env:, allow_void_success:) success_type = let_else_success_type(storage_type) raise LoweringError.new("propagation requires a non-void Option success type", line: 0, column: 0, path: @ctx.current_analysis_path) if success_type == @ctx.types.fetch("void") && !allow_void_success context = env[:return_context] raise LoweringError.new("propagation is only allowed inside function and proc bodies", line: 0, column: 0, path: @ctx.current_analysis_path) unless context raise LoweringError.new("propagation is not allowed inside defer blocks", line: 0, column: 0, path: @ctx.current_analysis_path) unless context[:allow_return] return_type = context[:return_type] unless option_let_else_type?(return_type) raise LoweringError.new("propagation requires enclosing function/proc to return Option[_], got #{return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end [storage_type, success_type, return_type, nil] end |
#infer_range_loop_type(expression, env:) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 199 def infer_range_loop_type(expression, env:) start_expr = range_start_of(expression) stop_expr = range_end_of(expression) start_type = infer_expression_type(start_expr, env:) stop_type = infer_expression_type(stop_expr, env:) if start_type != stop_type if start_expr.is_a?(AST::IntegerLiteral) start_type = infer_expression_type(start_expr, env:, expected_type: stop_type) elsif stop_expr.is_a?(AST::IntegerLiteral) stop_type = infer_expression_type(stop_expr, env:, expected_type: start_type) end end raise LoweringError.new("range bounds must use matching integer types, got #{start_type} and #{stop_type}", line: 0, column: 0, path: @ctx.current_analysis_path) unless start_type == stop_type start_type end |
#infer_result_propagation_details(storage_type, env:, allow_void_success:) ⇒ Object
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 945 def infer_result_propagation_details(storage_type, env:, allow_void_success:) success_type = let_else_success_type(storage_type) error_type = let_else_error_type(storage_type) raise LoweringError.new("propagation requires a non-void Result success type", line: 0, column: 0, path: @ctx.current_analysis_path) if success_type == @ctx.types.fetch("void") && !allow_void_success context = env[:return_context] raise LoweringError.new("propagation is only allowed inside function and proc bodies", line: 0, column: 0, path: @ctx.current_analysis_path) unless context raise LoweringError.new("propagation is not allowed inside defer blocks", line: 0, column: 0, path: @ctx.current_analysis_path) unless context[:allow_return] return_type = context[:return_type] unless result_let_else_type?(return_type) raise LoweringError.new("propagation requires enclosing function/proc to return Result[_, #{error_type}], got #{return_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end return_error_type = let_else_error_type(return_type) unless return_error_type == error_type raise LoweringError.new("propagation error type #{error_type} must match enclosing Result error type #{return_error_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end [storage_type, success_type, return_type, error_type] end |
#infer_result_propagation_type(expression, env:) ⇒ Object
928 929 930 931 932 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 928 def infer_result_propagation_type(expression, env:) _storage_type, success_type, = infer_result_propagation_types(expression, env:) success_type end |
#infer_result_propagation_types(expression, env:, allow_void_success: false) ⇒ Object
934 935 936 937 938 939 940 941 942 943 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 934 def infer_result_propagation_types(expression, env:, allow_void_success: false) storage_type = infer_expression_type(expression.operand, env:) if result_let_else_type?(storage_type) infer_result_propagation_details(storage_type, env:, allow_void_success:) elsif option_let_else_type?(storage_type) infer_option_propagation_details(storage_type, env:, allow_void_success:) else raise LoweringError.new("propagation expects Result[T, E] or Option[T], got #{storage_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#infer_value_type(handle_expression, env:) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 127 def infer_value_type(handle_expression, env:) handle_type = infer_expression_type(handle_expression, env:) return referenced_type(handle_type) if ref_type?(handle_type) return pointee_type(handle_type) if pointer_type?(handle_type) raise LoweringError.new("read expects ref[...] or ptr[...], got #{handle_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#integer_type?(type) ⇒ Boolean
218 219 220 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 218 def integer_type?(type) type.is_a?(Types::Primitive) && type.integer? end |
#integer_type_argument?(argument) ⇒ Boolean
1030 1031 1032 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1030 def integer_type_argument?(argument) argument.is_a?(Types::LiteralTypeArg) && argument.value.is_a?(Integer) end |
#let_else_binding_projection(type) ⇒ Object
817 818 819 820 821 822 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 817 def let_else_binding_projection(type) return :result_success_value if result_let_else_type?(type) return :option_some_value if option_let_else_type?(type) nil end |
#let_else_discard_binding_syntax?(statement) ⇒ Boolean
787 788 789 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 787 def let_else_discard_binding_syntax?(statement) statement.is_a?(AST::LocalDecl) && statement.else_body && statement.name == "_" end |
#let_else_error_type(type) ⇒ Object
811 812 813 814 815 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 811 def let_else_error_type(type) return unless result_let_else_type?(type) type.arm("failure").fetch("error") end |
#let_else_failure_condition(storage_expr, storage_type) ⇒ Object
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 842 def let_else_failure_condition(storage_expr, storage_type) if storage_type.is_a?(Types::Nullable) return IR::Binary.new( operator: "==", left: storage_expr, right: IR::NullLiteral.new(type: storage_type), type: @ctx.types.fetch("bool"), ) end if result_let_else_type?(storage_type) kind_type = @ctx.types.fetch("int") return IR::Binary.new( operator: "==", left: IR::Member.new(receiver: storage_expr, member: "kind", type: kind_type), right: IR::Name.new(name: "#{c_type_name(storage_type)}_kind_failure", type: kind_type, pointer: false), type: @ctx.types.fetch("bool"), ) end if option_let_else_type?(storage_type) kind_type = @ctx.types.fetch("int") return IR::Binary.new( operator: "==", left: IR::Member.new(receiver: storage_expr, member: "kind", type: kind_type), right: IR::Name.new(name: "#{c_type_name(storage_type)}_kind_none", type: kind_type, pointer: false), type: @ctx.types.fetch("bool"), ) end raise LoweringError.new("unsupported let-else storage type #{storage_type}", line: 0, column: 0, path: @ctx.current_analysis_path) end |
#let_else_storage_c_name(statement, env) ⇒ Object
795 796 797 798 799 800 801 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 795 def let_else_storage_c_name(statement, env) return fresh_c_temp_name(env, "let_else_discard") if let_else_discard_binding_syntax?(statement) return fresh_c_temp_name(env, "_") if statement.name == "_" c_local_name(statement.name) end |
#let_else_success_type(type) ⇒ Object
803 804 805 806 807 808 809 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 803 def let_else_success_type(type) return type.base if type.is_a?(Types::Nullable) return type.arm("some").fetch("value") if option_let_else_type?(type) return unless result_let_else_type?(type) type.arm("success").fetch("value") end |
#local_binding(type:, linkage_name:, mutable:, pointer:, storage_type: nil, projection: nil, cstr_backed: false, cstr_list_backed: false, const_value: nil) ⇒ Object
435 436 437 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 435 def local_binding(type:, linkage_name:, mutable:, pointer:, storage_type: nil, projection: nil, cstr_backed: false, cstr_list_backed: false, const_value: nil) { type:, storage_type: storage_type || type, linkage_name:, mutable:, pointer:, projection:, cstr_backed:, cstr_list_backed:, const_value: } end |
#lookup_value(name, env) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 298 def lookup_value(name, env) env[:scopes].reverse_each do |scope| return scope[name] if scope.key?(name) end if @ctx.values.key?(name) binding = @ctx.values.fetch(name) { type: binding.type, storage_type: binding.storage_type, linkage_name: value_c_name(name), mutable: binding.mutable, pointer: false, cstr_backed: cstr_trackable_type?(binding.type) && binding.const_value.is_a?(String), cstr_list_backed: false, const_value: binding.const_value, } end end |
#loop_exit_break(label = nil) ⇒ Object
707 708 709 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 707 def loop_exit_break(label = nil) { kind: :break, label: } end |
#loop_exit_continue(label = nil) ⇒ Object
711 712 713 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 711 def loop_exit_continue(label = nil) { kind: :continue, label: } end |
#loop_exit_label(label) ⇒ Object
715 716 717 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 715 def loop_exit_label(label) { kind: :label, label: } end |
#loop_exit_statement(target, local_defers:, outer_defers:) ⇒ Object
719 720 721 722 723 724 725 726 727 728 729 730 731 732 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 719 def loop_exit_statement(target, local_defers:, outer_defers:) case target[:kind] when :break IR::BreakStmt.new when :continue IR::ContinueStmt.new when :label return IR::GotoStmt.new(label: target[:label]) if target[:label] IR::GotoStmt.new(label: target[:label]) else raise LoweringError.new("unsupported loop exit target #{target.inspect}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#loop_flow(break_target:, continue_target:, break_defers: [], continue_defers: []) ⇒ Object
665 666 667 668 669 670 671 672 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 665 def loop_flow(break_target:, continue_target:, break_defers: [], continue_defers: []) { break_target:, continue_target:, break_defers:, continue_defers:, } end |
#lower_assignment_binding_target(binding) ⇒ Object
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1191 def lower_assignment_binding_target(binding) storage_type = binding[:storage_type] visible_type = binding[:type] storage_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) case binding[:projection] when :result_success_value variant_binding_projection_expression(storage_ref, storage_type, "success", "value", visible_type) when :option_some_value variant_binding_projection_expression(storage_ref, storage_type, "some", "value", visible_type) else if visible_type == storage_type || (storage_type.is_a?(Types::Nullable) && storage_type.base == visible_type) IR::Name.new(name: binding[:linkage_name], type: visible_type, pointer: binding[:pointer]) else storage_ref end end end |
#lower_assignment_target(expression, env:) ⇒ Object
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1142 def lower_assignment_target(expression, env:) case expression when AST::Identifier binding = lookup_value(expression.name, env) lower_assignment_binding_target(binding) when AST::MemberAccess if expression.receiver.is_a?(AST::IndexAccess) base_type = infer_expression_type(expression.receiver.receiver, env:) if base_type.is_a?(Types::SoA) soa_base = lower_expression(expression.receiver.receiver, env:) index = lower_expression(expression.receiver.index, env:) field_type = base_type.fields[expression.member] target_type = infer_expression_type(expression, env:) return IR::Index.new( receiver: IR::Member.new(receiver: soa_base, member: expression.member, type: field_type), index:, type: target_type, ) end end receiver_type = infer_expression_type(expression.receiver, env:) receiver = lower_expression(expression.receiver, env:) type = infer_expression_type(expression, env:) IR::Member.new(receiver:, member: member_c_name(receiver_type, expression.member), type:) when AST::IndexAccess receiver_type = infer_expression_type(expression.receiver, env:) receiver = lower_expression(expression.receiver, env:) index = lower_expression(expression.index, env:) type = infer_expression_type(expression, env:) if array_type?(receiver_type) IR::CheckedIndex.new(receiver:, index:, receiver_type:, type:) elsif receiver_type.is_a?(Types::Span) IR::CheckedSpanIndex.new(receiver:, index:, receiver_type:, type:) else IR::Index.new(receiver:, index:, type:) end when AST::Call if read_call?(expression) type = infer_expression_type(expression, env:) operand = lower_expression(expression.arguments.first.value, env:) return IR::Unary.new(operator: "*", operand:, type:) end raise LoweringError.new("unsupported assignment target #{expression.class.name}", line: 0, column: 0, path: @ctx.current_analysis_path) else raise LoweringError.new("unsupported assignment target #{expression.class.name}", line: 0, column: 0, path: @ctx.current_analysis_path) end end |
#lower_bound_identifier(binding, expected_type: nil) ⇒ Object
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 875 def lower_bound_identifier(binding, expected_type: nil) storage_type = binding[:storage_type] visible_type = binding[:type] projection = binding[:projection] if projection == :result_success_value local_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return variant_binding_projection_expression(local_ref, storage_type, "success", "value", visible_type) end if projection == :result_failure_error local_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return variant_binding_projection_expression(local_ref, storage_type, "failure", "error", visible_type) end if projection == :option_some_value local_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return variant_binding_projection_expression(local_ref, storage_type, "some", "value", visible_type) end return IR::Name.new(name: binding[:linkage_name], type: visible_type, pointer: binding[:pointer]) if visible_type == storage_type if storage_type.is_a?(Types::Nullable) && storage_type.base == visible_type name = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return name if pointer_like_type?(storage_type.base) return name if expected_type == storage_type return IR::Member.new(receiver: name, member: "value", type: visible_type) end if result_let_else_type?(storage_type) && let_else_success_type(storage_type) == visible_type local_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return variant_binding_projection_expression(local_ref, storage_type, "success", "value", visible_type) end if option_let_else_type?(storage_type) && let_else_success_type(storage_type) == visible_type local_ref = IR::Name.new(name: binding[:linkage_name], type: storage_type, pointer: binding[:pointer]) return variant_binding_projection_expression(local_ref, storage_type, "some", "value", visible_type) end IR::Name.new(name: binding[:linkage_name], type: visible_type, pointer: binding[:pointer]) end |
#lower_compile_time_literal(value, type) ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 326 def lower_compile_time_literal(value, type) case value when true, false return IR::BooleanLiteral.new(value:, type:) if type.is_a?(Types::Primitive) && type.boolean? when Integer return IR::IntegerLiteral.new(value:, type:) if type.is_a?(Types::Primitive) && type.integer? return IR::FloatLiteral.new(value: value.to_f, type:) if type.is_a?(Types::Primitive) && type.float? when Float return IR::FloatLiteral.new(value:, type:) if type.is_a?(Types::Primitive) && type.float? when String if type == @ctx.types.fetch("str") || type == @ctx.types.fetch("cstr") return IR::StringLiteral.new(value:, type:, cstring: type == @ctx.types.fetch("cstr")) end when Hash return nil unless type.is_a?(Types::Struct) fields = value.map do |name, field_value| field_type = type.field(name) return nil unless field_type lowered = lower_compile_time_literal(field_value, field_type) return nil unless lowered IR::AggregateField.new(name:, value: lowered) end return IR::AggregateLiteral.new(type:, fields:) end nil end |
#lower_defer_cleanup_body(statements, env:, return_type:) ⇒ Object
763 764 765 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 763 def lower_defer_cleanup_body(statements, env:, return_type:) lower_block(statements, env:, active_defers: [], return_type:, loop_flow: nil, allow_return: false) end |
#lower_fatal_statement(message, env:) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 186 def lower_fatal_statement(, env:) IR::ExpressionStmt.new( expression: lower_expression( AST::Call.new( callee: AST::Identifier.new(name: "fatal"), arguments: [AST::Argument.new(name: nil, value: AST::StringLiteral.new(lexeme: .inspect, value: , cstring: false))], ), env:, expected_type: @ctx.types.fetch("void"), ), ) end |
#lower_loop_exit(target, local_defers, outer_defers) ⇒ Object
734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 734 def lower_loop_exit(target, local_defers, outer_defers) cleanup = cleanup_statements(local_defers, outer_defers) if cleanup.empty? [loop_exit_statement(target, local_defers:, outer_defers:)] else label = target[:label] raise LoweringError.new("structured loop exits with cleanup are unsupported", line: 0, column: 0, path: @ctx.current_analysis_path) unless label cleanup + [IR::GotoStmt.new(label:)] end end |
#lower_range_match_condition(range_pattern, scrutinee_ir, bool_type, env:) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 9 def lower_range_match_condition(range_pattern, scrutinee_ir, bool_type, env:) start_ir = lower_expression(range_pattern.start_expr, env:, expected_type: nil) end_ir = lower_expression(range_pattern.end_expr, env:, expected_type: nil) ge = IR::Binary.new(operator: ">=", left: scrutinee_ir, right: start_ir, type: bool_type) le = IR::Binary.new(operator: "<=", left: scrutinee_ir, right: end_ir, type: bool_type) IR::Binary.new(operator: "and", left: ge, right: le, type: bool_type) end |
#lower_static_storage_initializer(expression, env:, expected_type: nil) ⇒ Object
318 319 320 321 322 323 324 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 318 def lower_static_storage_initializer(expression, env:, expected_type: nil) if expected_type && (literal = lower_compile_time_literal(compile_time_const_value(expression, env:), expected_type)) return literal end lower_expression(rewrite_static_storage_initializer(expression), env:, expected_type: expected_type) end |
#merge_refinements(existing, incoming) ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 550 def merge_refinements(existing, incoming) merged = existing.dup incoming.each do |name, refined_type| if merged.key?(name) && merged[name] != refined_type merged.delete(name) else merged[name] = refined_type end end merged end |
#module_c_prefix(module_name) ⇒ Object
1095 1096 1097 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1095 def module_c_prefix(module_name) sanitize_identifier(module_name.to_s.tr('.', '_')) end |
#module_function_c_name(module_name, name, type_arguments: []) ⇒ Object
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1072 def module_function_c_name(module_name, name, type_arguments: []) base = "#{module_c_prefix(module_name)}_#{name}" return base if type_arguments.empty? # A double-underscore separates a generic function instance's type # arguments so it cannot collide with a distinct regular function whose # name happens to be `<name>_<typearg>` (e.g. the instance # `expect_equal[str]` vs the function `expect_equal_str`). The method path # in `function_binding_c_name` uses the same scheme for consistency. "#{base}__#{generic_type_argument_suffix(type_arguments)}" end |
#module_value_c_name(module_name, name) ⇒ Object
1091 1092 1093 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1091 def module_value_c_name(module_name, name) "#{module_c_prefix(module_name)}_#{name}" end |
#nested_loop_flow(current_loop_flow, local_defers) ⇒ Object
674 675 676 677 678 679 680 681 682 683 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 674 def nested_loop_flow(current_loop_flow, local_defers) return nil unless current_loop_flow loop_flow( break_target: current_loop_flow[:break_target], continue_target: current_loop_flow[:continue_target], break_defers: current_loop_flow[:break_defers] + local_defers, continue_defers: current_loop_flow[:continue_defers] + local_defers, ) end |
#null_test_refinements(expression, truthy:, env:) ⇒ Object
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 591 def null_test_refinements(expression, truthy:, env:) identifier_expression = nil if expression.left.is_a?(AST::Identifier) && expression.right.is_a?(AST::NullLiteral) identifier_expression = expression.left elsif expression.left.is_a?(AST::NullLiteral) && expression.right.is_a?(AST::Identifier) identifier_expression = expression.right else return {} end binding = lookup_value(identifier_expression.name, env) return {} unless binding && binding[:storage_type].is_a?(Types::Nullable) null_result = expression.operator == "==" ? truthy : !truthy refined_type = null_result ? null_type : binding[:storage_type].base { identifier_expression.name => refined_type } end |
#null_type ⇒ Object
661 662 663 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 661 def null_type @null_type ||= Types::Null.new end |
#nullable_candidate?(type) ⇒ Boolean
642 643 644 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 642 def nullable_candidate?(type) !ref_type?(type) && type != @ctx.types.fetch("void") end |
#nullable_some_literal(nullable_type, value) ⇒ Object
1218 1219 1220 1221 1222 1223 1224 1225 1226 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1218 def nullable_some_literal(nullable_type, value) IR::AggregateLiteral.new( type: nullable_type, fields: [ IR::AggregateField.new(name: "has_value", value: IR::BooleanLiteral.new(value: true, type: @ctx.types.fetch("bool"))), IR::AggregateField.new(name: "value", value:), ], ) end |
#opaque_c_type_name(type) ⇒ Object
1012 1013 1014 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1012 def opaque_c_type_name(type) type.linkage_name || c_type_name(type) end |
#opaque_forward_declarable?(type) ⇒ Boolean
1016 1017 1018 1019 1020 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1016 def opaque_forward_declarable?(type) return false unless opaque_c_type_name(type).match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/) !type.external || type.linkage_name.nil? end |
#option_let_else_type?(type) ⇒ Boolean
824 825 826 827 828 829 830 831 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 824 def option_let_else_type?(type) return false unless type.is_a?(Types::Variant) some_fields = type.arm("some") none_fields = type.arm("none") some_fields && some_fields.length == 1 && some_fields.key?("value") && none_fields && none_fields.empty? end |
#pointer_like_type?(type) ⇒ Boolean
1228 1229 1230 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1228 def pointer_like_type?(type) pointer_type?(type) || own_type?(type) || (type.is_a?(Types::Primitive) && type.name == "cstr") || type.is_a?(Types::Function) || type.is_a?(Types::Proc) || type.is_a?(Types::Opaque) end |
#pointer_to(type) ⇒ Object
285 286 287 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 285 def pointer_to(type) Types::Registry.generic_instance("ptr", [type]) end |
#proc_env_pointer_type ⇒ Object
484 485 486 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 484 def proc_env_pointer_type @proc_env_pointer_type ||= pointer_to(@ctx.types.fetch("void")) end |
#proc_invoke_function_type(proc_type) ⇒ Object
488 489 490 491 492 493 494 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 488 def proc_invoke_function_type(proc_type) Types::Registry.function( nil, params: [Types::Registry.parameter("env", proc_env_pointer_type), *proc_type.params], return_type: proc_type.return_type, ) end |
#proc_release_function_type ⇒ Object
496 497 498 499 500 501 502 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 496 def proc_release_function_type @proc_release_function_type ||= Types::Registry.function( nil, params: [Types::Registry.parameter("env", proc_env_pointer_type)], return_type: @ctx.types.fetch("void"), ) end |
#proc_retain_function_type ⇒ Object
504 505 506 507 508 509 510 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 504 def proc_retain_function_type @proc_retain_function_type ||= Types::Registry.function( nil, params: [Types::Registry.parameter("env", proc_env_pointer_type)], return_type: @ctx.types.fetch("void"), ) end |
#range_end_of(iterable) ⇒ Object
21 22 23 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 21 def range_end_of(iterable) iterable.end_expr end |
#range_iterable?(expression) ⇒ Boolean
5 6 7 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 5 def range_iterable?(expression) range_expr?(expression) end |
#range_start_of(iterable) ⇒ Object
17 18 19 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 17 def range_start_of(iterable) iterable.start_expr end |
#read_call?(expression) ⇒ Boolean
123 124 125 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 123 def read_call?(expression) expression.is_a?(AST::Call) && expression.callee.is_a?(AST::Identifier) && expression.callee.name == "read" end |
#result_let_else_type?(type) ⇒ Boolean
833 834 835 836 837 838 839 840 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 833 def result_let_else_type?(type) return false unless type.is_a?(Types::Variant) success_fields = type.arm("success") failure_fields = type.arm("failure") success_fields && success_fields.length == 1 && success_fields.key?("value") && failure_fields && failure_fields.length == 1 && failure_fields.key?("error") end |
#rewrite_static_storage_initializer(expression) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 376 def rewrite_static_storage_initializer(expression) case expression when AST::Identifier binding = @ctx.values[expression.name] if binding&.kind == :const declaration = const_declaration_for_module(@ctx.module_name, expression.name) return rewrite_static_storage_initializer(declaration.value) end expression when AST::MemberAccess if expression.receiver.is_a?(AST::Identifier) && @ctx.imports.key?(expression.receiver.name) imported_module = @ctx.imports.fetch(expression.receiver.name) if (binding = imported_module.values[expression.member])&.kind == :const imported_analysis = analysis_for_module(imported_module.name) declaration = const_declaration_for_module(imported_module.name, expression.member) return with_analysis_context(imported_analysis) do rewrite_static_storage_initializer(declaration.value) end end end AST::MemberAccess.new( receiver: rewrite_static_storage_initializer(expression.receiver), member: expression.member, ) when AST::UnaryOp AST::UnaryOp.new(operator: expression.operator, operand: rewrite_static_storage_initializer(expression.operand)) when AST::BinaryOp AST::BinaryOp.new( operator: expression.operator, left: rewrite_static_storage_initializer(expression.left), right: rewrite_static_storage_initializer(expression.right), ) when AST::IfExpr AST::IfExpr.new( condition: rewrite_static_storage_initializer(expression.condition), then_expression: rewrite_static_storage_initializer(expression.then_expression), else_expression: rewrite_static_storage_initializer(expression.else_expression), ) when AST::UnsafeExpr AST::UnsafeExpr.new(expression: rewrite_static_storage_initializer(expression.expression)) when AST::Call AST::Call.new( callee: rewrite_static_storage_initializer(expression.callee), arguments: expression.arguments.map do |argument| AST::Argument.new(name: argument.name, value: rewrite_static_storage_initializer(argument.value)) end, ) when AST::Specialization AST::Specialization.new( callee: rewrite_static_storage_initializer(expression.callee), arguments: expression.arguments.map { |argument| AST::TypeArgument.new(value: argument.value) }, ) else expression end end |
#sanitize_identifier(text) ⇒ Object
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1131 def sanitize_identifier(text) return "value" unless text cache = (@sanitize_identifier_cache ||= {}) cached = cache[text] return cached if cached identifier = text.gsub(/[^A-Za-z0-9_]+/, "_").gsub(/_+/, "_").sub(/_+$/, "").sub(/^_+/, "") cache[text] = identifier.empty? ? "value" : identifier end |
#scopes_with_refinements(scopes, refinements) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 530 def scopes_with_refinements(scopes, refinements) return scopes if refinements.nil? || refinements.empty? base_scopes = scopes.last.is_a?(FlowScope) ? scopes[0...-1] : scopes merged_refinements = scopes.last.is_a?(FlowScope) ? scopes.last.each_with_object({}) { |(name, binding), result| result[name] = binding[:type] } : {} merged_refinements = merge_refinements(merged_refinements, refinements) flow_scope = FlowScope.new merged_refinements.each do |name, refined_type| binding = lookup_value(name, { scopes: base_scopes }) next unless binding flow_scope[name] = binding.merge(type: refined_type) end return base_scopes if flow_scope.empty? base_scopes + [flow_scope] end |
#simd_type?(type) ⇒ Boolean
44 45 46 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 44 def simd_type?(type) type.is_a?(Types::Simd) end |
#snapshot_env(env) ⇒ Object
775 776 777 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 775 def snapshot_env(env) { scopes: env[:scopes].map(&:dup), counter: env[:counter] } end |
#stored_ref_supported_type?(type, visited = {}) ⇒ Boolean
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 255 def stored_ref_supported_type?(type, visited = {}) return true unless type visit_key = [type.class, type.object_id] return true if visited[visit_key] visited[visit_key] = true case type when Types::Nullable stored_ref_supported_type?(type.base, visited) when Types::GenericInstance if ref_type?(type) lt = ref_lifetime(type) return !!lt # lifetime-ref is supported by default; bare ref is not end type.arguments.all? { |argument| argument.is_a?(Types::LiteralTypeArg) || stored_ref_supported_type?(argument, visited) } when Types::Span stored_ref_supported_type?(type.element_type, visited) when Types::Task stored_ref_supported_type?(type.result_type, visited) when Types::StructInstance, Types::VariantInstance type.arguments.all? { |argument| stored_ref_supported_type?(argument, visited) } when Types::Proc, Types::Function callable_param_ref_supported?(type) else !contains_ref_type?(type) end end |
#str_buffer_capacity(type) ⇒ Object
102 103 104 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 102 def str_buffer_capacity(type) type.arguments.first.value end |
#str_buffer_storage_capacity(type) ⇒ Object
106 107 108 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 106 def str_buffer_storage_capacity(type) str_buffer_capacity(type) + 1 end |
#str_buffer_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
83 84 85 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 83 def str_buffer_to_span_compatible?(actual_type, expected_type) str_buffer_type?(actual_type) && expected_type.is_a?(Types::Span) && expected_type.element_type == @ctx.types.fetch("char") end |
#str_buffer_type?(type) ⇒ Boolean
97 98 99 100 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 97 def str_buffer_type?(type) type.is_a?(Types::GenericInstance) && type.name == "str_buffer" && type.arguments.length == 1 && type.arguments.first.is_a?(Types::LiteralTypeArg) && type.arguments.first.value.is_a?(Integer) end |
#struct_contains_string_field?(type) ⇒ Boolean
62 63 64 65 66 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 62 def struct_contains_string_field?(type) return false unless type.is_a?(Types::Struct) type.fields.any? { |_name, field_type| cstr_trackable_type?(field_type) || struct_contains_string_field?(field_type) } end |
#suppress_format_releases_for_assignment(cleanups, target_type) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 68 def suppress_format_releases_for_assignment(cleanups, target_type) return cleanups unless cstr_trackable_type?(target_type) || struct_contains_string_field?(target_type) cleanups.reject do |items| items.any? { |stmt| stmt.is_a?(IR::ExpressionStmt) && stmt.expression.is_a?(IR::Call) && stmt.expression.callee == "mt_format_str_release" } end end |
#switch_loop_flow(current_loop_flow, local_defers) ⇒ Object
691 692 693 694 695 696 697 698 699 700 701 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 691 def switch_loop_flow(current_loop_flow, local_defers) nested = nested_loop_flow(current_loop_flow, local_defers) return nil unless nested loop_flow( break_target: switch_loop_target(nested[:break_target]), continue_target: switch_loop_target(nested[:continue_target]), break_defers: nested[:break_defers], continue_defers: nested[:continue_defers], ) end |
#switch_loop_target(target) ⇒ Object
685 686 687 688 689 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 685 def switch_loop_target(target) return target unless target && target[:label] loop_exit_label(target[:label]) end |
#terminating_ir_statement?(statement) ⇒ Boolean
767 768 769 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 767 def terminating_ir_statement?(statement) statement.is_a?(IR::ReturnStmt) || statement.is_a?(IR::GotoStmt) end |
#validate_generic_type!(name, arguments) ⇒ Object
1026 1027 1028 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1026 def validate_generic_type!(name, arguments) super(name, arguments) { |msg| raise LoweringError.new(msg, line: 0, column: 0, path: @ctx.current_analysis_path) } end |
#value_c_name(name) ⇒ Object
1061 1062 1063 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1061 def value_c_name(name) module_value_c_name(@ctx.module_name, name) end |
#variant_binding_projection_expression(storage_expr, storage_type, arm_name, field_name, field_type) ⇒ Object
916 917 918 919 920 921 922 923 924 925 926 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 916 def variant_binding_projection_expression(storage_expr, storage_type, arm_name, field_name, field_type) payload_type = Types::VariantArmPayload.new(storage_type, arm_name, storage_type.arm(arm_name)) data_expr = IR::Member.new(receiver: storage_expr, member: "data", type: nil) arm_expr = IR::Member.new(receiver: data_expr, member: arm_name, type: payload_type) member_expr = IR::Member.new(receiver: arm_expr, member: field_name, type: field_type) if type_reaches_target?(field_type, payload_type.variant_type) return IR::Unary.new(operator: "*", operand: member_expr, type: field_type) end member_expr end |
#variant_match_arm_name_from_pattern(pattern) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 29 def variant_match_arm_name_from_pattern(pattern) callee = case pattern when AST::Call pattern.callee else pattern end callee.is_a?(AST::MemberAccess) ? callee.member : nil end |
#wildcard_arm_pattern?(expression) ⇒ Boolean
25 26 27 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 25 def wildcard_arm_pattern?(expression) expression.is_a?(AST::Identifier) && expression.name == "_" end |
#with_analysis_context(analysis) ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 289 def with_analysis_context(analysis) saved = @ctx.save @ctx.install(analysis) @ctx.module_prefix = module_c_prefix(@ctx.module_name) yield ensure @ctx.restore(saved) end |
#wrap_nullable_field_value(field_type, lowered_value, env) ⇒ Object
1210 1211 1212 1213 1214 1215 1216 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1210 def wrap_nullable_field_value(field_type, lowered_value, env) return lowered_value unless field_type.is_a?(Types::Nullable) return lowered_value if pointer_like_type?(field_type.base) return lowered_value if lowered_value.type.is_a?(Types::Nullable) nullable_some_literal(field_type, lowered_value) end |