Module: MilkTea::Lowering::Utils
- Included in:
- MilkTea::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_discards_binding?(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
- #reject_format_releases_for_assignment(cleanups, target_type) ⇒ Object
- #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
- #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
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 111 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
49 50 51 52 53 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 49 def array_element_type(type) return unless array_type?(type) type.arguments.first end |
#array_length(type) ⇒ Object
88 89 90 91 92 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 88 def array_length(type) return unless array_type?(type) type.arguments[1].value end |
#array_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
55 56 57 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 55 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
40 41 42 43 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 40 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
792 793 794 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 792 def bind_let_else_local?(statement) !let_else_discards_binding?(statement) end |
#c_local_name(name) ⇒ Object
1100 1101 1102 1103 1104 1105 1106 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1100 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
1108 1109 1110 1111 1112 1113 1114 1115 1116 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1108 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
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 1011 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 984 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
440 441 442 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 440 def callable_type?(type) type.is_a?(Types::Function) || proc_type?(type) end |
#cfg_block_always_terminates?(statements) ⇒ Boolean
610 611 612 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 610 def cfg_block_always_terminates?(statements) ControlFlow::Termination.block_always_terminates?(statements, ignore_name: ->(_name) { false }) end |
#char_array_text_type?(type) ⇒ Boolean
94 95 96 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 94 def char_array_text_type?(type) array_type?(type) && array_element_type(type) == @ctx.types.fetch("char") end |
#cleanup_safe_return_expression?(expression) ⇒ Boolean
1123 1124 1125 1126 1127 1128 1129 1130 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1123 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
704 705 706 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 704 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
162 163 164 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 162 def collection_loop_binding_type(iterable_type, element_type) super end |
#collection_loop_item_value(iterable_ref, iterable_type, index_ref, element_type) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 170 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
166 167 168 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 166 def collection_loop_ref_element_type?(type) super end |
#collection_loop_stop_value(iterable_ref, iterable_type) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 179 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
158 159 160 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 158 def collection_loop_type(type) super end |
#compile_time_builtin_function_type(name, arguments, env) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 355 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
373 374 375 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 373 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
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 614 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
647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 647 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
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 747 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
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 444 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
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 460 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
251 252 253 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 251 def contains_type_var?(type) super end |
#cstr_list_trackable_type?(type) ⇒ Boolean
77 78 79 80 81 82 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 77 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
59 60 61 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 59 def cstr_trackable_type?(type) type == @ctx.types.fetch("str") || type == @ctx.types.fetch("cstr") end |
#current_actual_scope(scopes) ⇒ Object
517 518 519 520 521 522 523 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 517 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
780 781 782 783 784 785 786 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 780 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
772 773 774 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 772 def empty_env { scopes: [{}], counter: { value: 0 } } end |
#enum_member_c_name(type, member_name) ⇒ Object
1039 1040 1041 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1039 def enum_member_c_name(type, member_name) "#{c_type_name(type)}_#{member_name}" end |
#env_with_refinements(env, refinements) ⇒ Object
525 526 527 528 529 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 525 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
1056 1057 1058 1059 1060 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1056 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
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 564 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
1023 1024 1025 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1023 def forward_declarable_external_opaque?(type) type.external && opaque_forward_declarable?(type) end |
#fresh_c_temp_name(env, prefix) ⇒ Object
1118 1119 1120 1121 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1118 def fresh_c_temp_name(env, prefix) env[:counter][:value] += 1 "__mt_#{prefix}_#{env[:counter][:value]}" end |
#fresh_proc_symbol ⇒ Object
513 514 515 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 513 def fresh_proc_symbol @synthetic_proc_counter += 1 end |
#function_binding_c_name(binding, module_name:, receiver_type: nil) ⇒ Object
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1043 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
1035 1036 1037 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1035 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.
1088 1089 1090 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1088 def generic_type_argument_suffix(type_arguments) sanitize_identifier(type_arguments.join('_')) end |
#if_expression_branch_compatible?(actual_type, expected_type) ⇒ Boolean
634 635 636 637 638 639 640 641 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 634 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
1066 1067 1068 1069 1070 1071 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1066 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
150 151 152 153 154 155 156 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 150 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
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 249 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 223 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
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 136 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
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 968 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
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 200 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
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 946 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
929 930 931 932 933 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 929 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
935 936 937 938 939 940 941 942 943 944 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 935 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
128 129 130 131 132 133 134 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 128 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
219 220 221 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 219 def integer_type?(type) type.is_a?(Types::Primitive) && type.integer? end |
#integer_type_argument?(argument) ⇒ Boolean
1031 1032 1033 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1031 def integer_type_argument?(argument) argument.is_a?(Types::LiteralTypeArg) && argument.value.is_a?(Integer) end |
#let_else_binding_projection(type) ⇒ Object
818 819 820 821 822 823 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 818 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_discards_binding?(statement) ⇒ Boolean
788 789 790 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 788 def let_else_discards_binding?(statement) statement.is_a?(AST::LocalDecl) && statement.else_body && statement.name == "_" end |
#let_else_error_type(type) ⇒ Object
812 813 814 815 816 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 812 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
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 843 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
796 797 798 799 800 801 802 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 796 def let_else_storage_c_name(statement, env) return fresh_c_temp_name(env, "let_else_discard") if let_else_discards_binding?(statement) return fresh_c_temp_name(env, "_") if statement.name == "_" c_local_name(statement.name) end |
#let_else_success_type(type) ⇒ Object
804 805 806 807 808 809 810 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 804 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
436 437 438 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 436 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
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 299 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
708 709 710 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 708 def loop_exit_break(label = nil) { kind: :break, label: } end |
#loop_exit_continue(label = nil) ⇒ Object
712 713 714 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 712 def loop_exit_continue(label = nil) { kind: :continue, label: } end |
#loop_exit_label(label) ⇒ Object
716 717 718 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 716 def loop_exit_label(label) { kind: :label, label: } end |
#loop_exit_statement(target, local_defers:, outer_defers:) ⇒ Object
720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 720 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
666 667 668 669 670 671 672 673 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 666 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
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1192 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
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 1190 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1143 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
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 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 876 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
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 353 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 327 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
764 765 766 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 764 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
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 187 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
735 736 737 738 739 740 741 742 743 744 745 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 735 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
10 11 12 13 14 15 16 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 10 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
319 320 321 322 323 324 325 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 319 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
551 552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 551 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
1096 1097 1098 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1096 def module_c_prefix(module_name) sanitize_identifier(module_name.to_s.tr('.', '_')) end |
#module_function_c_name(module_name, name, type_arguments: []) ⇒ Object
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1073 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
1092 1093 1094 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1092 def module_value_c_name(module_name, name) "#{module_c_prefix(module_name)}_#{name}" end |
#nested_loop_flow(current_loop_flow, local_defers) ⇒ Object
675 676 677 678 679 680 681 682 683 684 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 675 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
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 592 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
662 663 664 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 662 def null_type @null_type ||= Types::Null.new end |
#nullable_candidate?(type) ⇒ Boolean
643 644 645 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 643 def nullable_candidate?(type) !ref_type?(type) && type != @ctx.types.fetch("void") end |
#nullable_some_literal(nullable_type, value) ⇒ Object
1219 1220 1221 1222 1223 1224 1225 1226 1227 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1219 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
1013 1014 1015 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1013 def opaque_c_type_name(type) type.linkage_name || c_type_name(type) end |
#opaque_forward_declarable?(type) ⇒ Boolean
1017 1018 1019 1020 1021 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1017 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
825 826 827 828 829 830 831 832 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 825 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
1229 1230 1231 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1229 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
286 287 288 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 286 def pointer_to(type) Types::Registry.generic_instance("ptr", [type]) end |
#proc_env_pointer_type ⇒ Object
485 486 487 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 485 def proc_env_pointer_type @proc_env_pointer_type ||= pointer_to(@ctx.types.fetch("void")) end |
#proc_invoke_function_type(proc_type) ⇒ Object
489 490 491 492 493 494 495 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 489 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
497 498 499 500 501 502 503 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 497 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
505 506 507 508 509 510 511 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 505 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
22 23 24 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 22 def range_end_of(iterable) iterable.end_expr end |
#range_iterable?(expression) ⇒ Boolean
6 7 8 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 6 def range_iterable?(expression) range_expr?(expression) end |
#range_start_of(iterable) ⇒ Object
18 19 20 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 18 def range_start_of(iterable) iterable.start_expr end |
#read_call?(expression) ⇒ Boolean
124 125 126 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 124 def read_call?(expression) expression.is_a?(AST::Call) && expression.callee.is_a?(AST::Identifier) && expression.callee.name == "read" end |
#reject_format_releases_for_assignment(cleanups, target_type) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 69 def reject_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 |
#result_let_else_type?(type) ⇒ Boolean
834 835 836 837 838 839 840 841 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 834 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
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 434 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 377 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
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1132 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
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 531 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
45 46 47 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 45 def simd_type?(type) type.is_a?(Types::Simd) end |
#snapshot_env(env) ⇒ Object
776 777 778 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 776 def snapshot_env(env) { scopes: env[:scopes].map(&:dup), counter: env[:counter] } end |
#stored_ref_supported_type?(type, visited = {}) ⇒ Boolean
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 284 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 256 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
103 104 105 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 103 def str_buffer_capacity(type) type.arguments.first.value end |
#str_buffer_storage_capacity(type) ⇒ Object
107 108 109 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 107 def str_buffer_storage_capacity(type) str_buffer_capacity(type) + 1 end |
#str_buffer_to_span_compatible?(actual_type, expected_type) ⇒ Boolean
84 85 86 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 84 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
98 99 100 101 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 98 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
63 64 65 66 67 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 63 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 |
#switch_loop_flow(current_loop_flow, local_defers) ⇒ Object
692 693 694 695 696 697 698 699 700 701 702 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 692 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
686 687 688 689 690 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 686 def switch_loop_target(target) return target unless target && target[:label] loop_exit_label(target[:label]) end |
#terminating_ir_statement?(statement) ⇒ Boolean
768 769 770 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 768 def terminating_ir_statement?(statement) statement.is_a?(IR::ReturnStmt) || statement.is_a?(IR::GotoStmt) end |
#validate_generic_type!(name, arguments) ⇒ Object
1027 1028 1029 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1027 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
1062 1063 1064 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1062 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
917 918 919 920 921 922 923 924 925 926 927 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 917 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
30 31 32 33 34 35 36 37 38 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 30 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
26 27 28 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 26 def wildcard_arm_pattern?(expression) expression.is_a?(AST::Identifier) && expression.name == "_" end |
#with_analysis_context(analysis) ⇒ Object
290 291 292 293 294 295 296 297 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 290 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
1211 1212 1213 1214 1215 1216 1217 |
# File 'lib/milk_tea/core/lowering/utils.rb', line 1211 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 |