Module: MilkTea::Lowering::Utils

Included in:
MilkTea::Lowerer
Defined in:
lib/milk_tea/core/lowering/utils.rb

Instance Method Summary collapse

Instance Method Details

#addressable_storage_expression?(expression) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


827
828
829
# File 'lib/milk_tea/core/lowering/utils.rb', line 827

def bind_let_else_local?(statement)
  !let_else_discards_binding?(statement)
end

#c_local_name(name) ⇒ Object



1139
1140
1141
1142
1143
1144
# File 'lib/milk_tea/core/lowering/utils.rb', line 1139

def c_local_name(name)
  return "value" unless name
  return "#{name}_" if c_reserved_identifier?(name)

  name
end

#c_reserved_identifier?(identifier) ⇒ Boolean

Returns:

  • (Boolean)


1146
1147
1148
1149
1150
1151
1152
1153
1154
# File 'lib/milk_tea/core/lowering/utils.rb', line 1146

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



1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/milk_tea/core/lowering/utils.rb', line 1023

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

Returns:

  • (Boolean)


475
476
477
# File 'lib/milk_tea/core/lowering/utils.rb', line 475

def callable_type?(type)
  type.is_a?(Types::Function) || proc_type?(type)
end

#cfg_block_always_terminates?(statements) ⇒ Boolean

Returns:

  • (Boolean)


645
646
647
# File 'lib/milk_tea/core/lowering/utils.rb', line 645

def cfg_block_always_terminates?(statements)
  ControlFlow::Termination.block_always_terminates?(statements, ignore_name: ->(_name) { false })
end

#char_array_text_type?(type) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


1161
1162
1163
1164
1165
1166
1167
1168
# File 'lib/milk_tea/core/lowering/utils.rb', line 1161

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



739
740
741
# File 'lib/milk_tea/core/lowering/utils.rb', line 739

def cleanup_statements(local_defers, outer_defers)
  local_defers.reverse.flat_map(&:itself) + outer_defers.reverse.flat_map(&:itself)
end

#collection_loop_item_value(iterable_ref, iterable_type, index_ref, element_type) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/milk_tea/core/lowering/utils.rb', line 158

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_stop_value(iterable_ref, iterable_type) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/milk_tea/core/lowering/utils.rb', line 167

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

#compile_time_builtin_function_type(name, arguments, env) ⇒ Object

Raises:



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/milk_tea/core/lowering/utils.rb', line 390

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



408
409
410
# File 'lib/milk_tea/core/lowering/utils.rb', line 408

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



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# File 'lib/milk_tea/core/lowering/utils.rb', line 649

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



682
683
684
685
686
687
688
689
690
691
692
693
694
695
# File 'lib/milk_tea/core/lowering/utils.rb', line 682

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

Returns:

  • (Boolean)


782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/milk_tea/core/lowering/utils.rb', line 782

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

Returns:

  • (Boolean)


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/milk_tea/core/lowering/utils.rb', line 479

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

Returns:

  • (Boolean)


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/milk_tea/core/lowering/utils.rb', line 495

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

#cstr_list_trackable_type?(type) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Raises:



552
553
554
555
556
557
558
# File 'lib/milk_tea/core/lowering/utils.rb', line 552

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



815
816
817
818
819
820
821
# File 'lib/milk_tea/core/lowering/utils.rb', line 815

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_envObject



807
808
809
# File 'lib/milk_tea/core/lowering/utils.rb', line 807

def empty_env
  { scopes: [{}], counter: { value: 0 } }
end

#enum_member_c_name(type, member_name) ⇒ Object



1078
1079
1080
# File 'lib/milk_tea/core/lowering/utils.rb', line 1078

def enum_member_c_name(type, member_name)
  "#{c_type_name(type)}_#{member_name}"
end

#env_with_refinements(env, refinements) ⇒ Object



560
561
562
563
564
# File 'lib/milk_tea/core/lowering/utils.rb', line 560

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



1095
1096
1097
1098
1099
# File 'lib/milk_tea/core/lowering/utils.rb', line 1095

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



599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/milk_tea/core/lowering/utils.rb', line 599

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

Returns:

  • (Boolean)


1062
1063
1064
# File 'lib/milk_tea/core/lowering/utils.rb', line 1062

def forward_declarable_external_opaque?(type)
  type.external && opaque_forward_declarable?(type)
end

#fresh_c_temp_name(env, prefix) ⇒ Object



1156
1157
1158
1159
# File 'lib/milk_tea/core/lowering/utils.rb', line 1156

def fresh_c_temp_name(env, prefix)
  env[:counter][:value] += 1
  "__mt_#{prefix}_#{env[:counter][:value]}"
end

#fresh_proc_symbolObject



548
549
550
# File 'lib/milk_tea/core/lowering/utils.rb', line 548

def fresh_proc_symbol
  @synthetic_proc_counter += 1
end

#function_binding_c_name(binding, module_name:, receiver_type: nil) ⇒ Object



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/milk_tea/core/lowering/utils.rb', line 1082

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

Returns:

  • (Boolean)


1074
1075
1076
# File 'lib/milk_tea/core/lowering/utils.rb', line 1074

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.



1127
1128
1129
# File 'lib/milk_tea/core/lowering/utils.rb', line 1127

def generic_type_argument_suffix(type_arguments)
  sanitize_identifier(type_arguments.join('_'))
end

#if_expression_branch_compatible?(actual_type, expected_type) ⇒ Boolean

Returns:

  • (Boolean)


669
670
671
672
673
674
675
676
# File 'lib/milk_tea/core/lowering/utils.rb', line 669

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



1105
1106
1107
1108
1109
1110
# File 'lib/milk_tea/core/lowering/utils.rb', line 1105

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

Raises:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/milk_tea/core/lowering/utils.rb', line 211

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 receiver_type.is_a?(Types::StringView)
    return @ctx.types.fetch("ubyte")
  end

  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

Raises:



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/milk_tea/core/lowering/utils.rb', line 1007

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

Raises:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/milk_tea/core/lowering/utils.rb', line 188

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

Raises:



985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/milk_tea/core/lowering/utils.rb', line 985

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



968
969
970
971
972
# File 'lib/milk_tea/core/lowering/utils.rb', line 968

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



974
975
976
977
978
979
980
981
982
983
# File 'lib/milk_tea/core/lowering/utils.rb', line 974

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

Raises:



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

Returns:

  • (Boolean)


207
208
209
# File 'lib/milk_tea/core/lowering/utils.rb', line 207

def integer_type?(type)
  type.is_a?(Types::Primitive) && type.integer?
end

#integer_type_argument?(argument) ⇒ Boolean

Returns:

  • (Boolean)


1070
1071
1072
# File 'lib/milk_tea/core/lowering/utils.rb', line 1070

def integer_type_argument?(argument)
  argument.is_a?(Types::LiteralTypeArg) && argument.value.is_a?(Integer)
end

#let_else_binding_projection(type) ⇒ Object



853
854
855
856
857
858
# File 'lib/milk_tea/core/lowering/utils.rb', line 853

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

Returns:

  • (Boolean)


823
824
825
# File 'lib/milk_tea/core/lowering/utils.rb', line 823

def let_else_discards_binding?(statement)
  statement.is_a?(AST::LocalDecl) && statement.else_body && statement.name == "_"
end

#let_else_error_type(type) ⇒ Object



847
848
849
850
851
# File 'lib/milk_tea/core/lowering/utils.rb', line 847

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

Raises:



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
# File 'lib/milk_tea/core/lowering/utils.rb', line 878

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



831
832
833
834
835
836
837
# File 'lib/milk_tea/core/lowering/utils.rb', line 831

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



839
840
841
842
843
844
845
# File 'lib/milk_tea/core/lowering/utils.rb', line 839

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, substitute_const_value: false) ⇒ Object



471
472
473
# File 'lib/milk_tea/core/lowering/utils.rb', line 471

def local_binding(type:, linkage_name:, mutable:, pointer:, storage_type: nil, projection: nil, cstr_backed: false, cstr_list_backed: false, const_value: nil, substitute_const_value: false)
  { type:, storage_type: storage_type || type, linkage_name:, mutable:, pointer:, projection:, cstr_backed:, cstr_list_backed:, const_value:, substitute_const_value: }
end

#lookup_value(name, env) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/milk_tea/core/lowering/utils.rb', line 308

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



743
744
745
# File 'lib/milk_tea/core/lowering/utils.rb', line 743

def loop_exit_break(label = nil)
  { kind: :break, label: }
end

#loop_exit_continue(label = nil) ⇒ Object



747
748
749
# File 'lib/milk_tea/core/lowering/utils.rb', line 747

def loop_exit_continue(label = nil)
  { kind: :continue, label: }
end

#loop_exit_label(label) ⇒ Object



751
752
753
# File 'lib/milk_tea/core/lowering/utils.rb', line 751

def loop_exit_label(label)
  { kind: :label, label: }
end

#loop_exit_statement(target, local_defers:, outer_defers:) ⇒ Object



755
756
757
758
759
760
761
762
763
764
765
766
767
768
# File 'lib/milk_tea/core/lowering/utils.rb', line 755

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



701
702
703
704
705
706
707
708
# File 'lib/milk_tea/core/lowering/utils.rb', line 701

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



1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
# File 'lib/milk_tea/core/lowering/utils.rb', line 1230

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



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
# File 'lib/milk_tea/core/lowering/utils.rb', line 1181

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



911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/milk_tea/core/lowering/utils.rb', line 911

def lower_bound_identifier(binding, expected_type: nil)
  storage_type = binding[:storage_type]
  visible_type = binding[:type]
  projection = binding[:projection]

  if binding[:substitute_const_value] && !binding[:const_value].nil?
    return lower_const_value_literal(visible_type, binding[:const_value])
  end

  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



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/milk_tea/core/lowering/utils.rb', line 354

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 Array
    return nil unless array_type?(type)

    element_type = type.arguments.first
    elements = value.map { |element| lower_compile_time_literal(element, element_type) }
    return nil if elements.any?(&:nil?)

    IR::ArrayLiteral.new(type:, elements:)
  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



799
800
801
# File 'lib/milk_tea/core/lowering/utils.rb', line 799

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



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/milk_tea/core/lowering/utils.rb', line 175

def lower_fatal_statement(message, 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: message.inspect, value: message, cstring: false))],
      ),
      env:,
      expected_type: @ctx.types.fetch("void"),
    ),
  )
end

#lower_loop_exit(target, local_defers, outer_defers) ⇒ Object



770
771
772
773
774
775
776
777
778
779
780
# File 'lib/milk_tea/core/lowering/utils.rb', line 770

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



328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/milk_tea/core/lowering/utils.rb', line 328

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

  rewritten = rewrite_static_storage_initializer(expression)
  if (imported_analysis = static_storage_imported_analysis(expression))
    with_analysis_context(imported_analysis) do
      lower_expression(rewritten, env:, expected_type: expected_type)
    end
  else
    lower_expression(rewritten, env:, expected_type: expected_type)
  end
end

#merge_refinements(existing, incoming) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/milk_tea/core/lowering/utils.rb', line 586

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



1135
1136
1137
# File 'lib/milk_tea/core/lowering/utils.rb', line 1135

def module_c_prefix(module_name)
  sanitize_identifier(module_name.to_s.tr('.', '_'))
end

#module_function_c_name(module_name, name, type_arguments: []) ⇒ Object



1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'lib/milk_tea/core/lowering/utils.rb', line 1112

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



1131
1132
1133
# File 'lib/milk_tea/core/lowering/utils.rb', line 1131

def module_value_c_name(module_name, name)
  "#{module_c_prefix(module_name)}_#{name}"
end

#nested_loop_flow(current_loop_flow, local_defers) ⇒ Object



710
711
712
713
714
715
716
717
718
719
# File 'lib/milk_tea/core/lowering/utils.rb', line 710

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



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/milk_tea/core/lowering/utils.rb', line 627

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_typeObject



697
698
699
# File 'lib/milk_tea/core/lowering/utils.rb', line 697

def null_type
  @null_type ||= Types::Null.new
end

#nullable_candidate?(type) ⇒ Boolean

Returns:

  • (Boolean)


678
679
680
# File 'lib/milk_tea/core/lowering/utils.rb', line 678

def nullable_candidate?(type)
  !ref_type?(type) && type != @ctx.types.fetch("void")
end

#nullable_some_literal(nullable_type, value) ⇒ Object



1257
1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/milk_tea/core/lowering/utils.rb', line 1257

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



1052
1053
1054
# File 'lib/milk_tea/core/lowering/utils.rb', line 1052

def opaque_c_type_name(type)
  type.linkage_name || c_type_name(type)
end

#opaque_forward_declarable?(type) ⇒ Boolean

Returns:

  • (Boolean)


1056
1057
1058
1059
1060
# File 'lib/milk_tea/core/lowering/utils.rb', line 1056

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

Returns:

  • (Boolean)


860
861
862
863
864
865
866
867
# File 'lib/milk_tea/core/lowering/utils.rb', line 860

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

Returns:

  • (Boolean)


1267
1268
1269
# File 'lib/milk_tea/core/lowering/utils.rb', line 1267

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



295
296
297
# File 'lib/milk_tea/core/lowering/utils.rb', line 295

def pointer_to(type)
  Types::Registry.generic_instance("ptr", [type])
end

#proc_env_pointer_typeObject



520
521
522
# File 'lib/milk_tea/core/lowering/utils.rb', line 520

def proc_env_pointer_type
  @proc_env_pointer_type ||= pointer_to(@ctx.types.fetch("void"))
end

#proc_invoke_function_type(proc_type) ⇒ Object



524
525
526
527
528
529
530
# File 'lib/milk_tea/core/lowering/utils.rb', line 524

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_typeObject



532
533
534
535
536
537
538
# File 'lib/milk_tea/core/lowering/utils.rb', line 532

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_typeObject



540
541
542
543
544
545
546
# File 'lib/milk_tea/core/lowering/utils.rb', line 540

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_index_result_type(receiver_type) ⇒ Object

Raises:



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/milk_tea/core/lowering/utils.rb', line 243

def range_index_result_type(receiver_type)
  receiver_type = referenced_type(receiver_type) if ref_type?(receiver_type)

  if receiver_type.is_a?(Types::StringView)
    return @ctx.types.fetch("str")
  end

  if array_type?(receiver_type)
    return Types::Span.new(array_element_type(receiver_type))
  end

  if receiver_type.is_a?(Types::Span)
    return receiver_type
  end

  raise LoweringError.new("cannot range-index #{receiver_type}; expected str, array[T, N], or span[T]", line: 0, column: 0, path: @ctx.current_analysis_path)
end

#range_iterable?(expression) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


869
870
871
872
873
874
875
876
# File 'lib/milk_tea/core/lowering/utils.rb', line 869

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



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/milk_tea/core/lowering/utils.rb', line 412

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



1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
# File 'lib/milk_tea/core/lowering/utils.rb', line 1170

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



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/milk_tea/core/lowering/utils.rb', line 566

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

Returns:

  • (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



811
812
813
# File 'lib/milk_tea/core/lowering/utils.rb', line 811

def snapshot_env(env)
  { scopes: env[:scopes].map(&:dup), counter: env[:counter] }
end

#span_slice_callee(span_type) ⇒ Object



261
262
263
# File 'lib/milk_tea/core/lowering/utils.rb', line 261

def span_slice_callee(span_type)
  "mt_span_slice_#{sanitize_identifier(span_type.element_type.to_s)}"
end

#static_storage_imported_analysis(expression) ⇒ Object



343
344
345
346
347
348
349
350
351
352
# File 'lib/milk_tea/core/lowering/utils.rb', line 343

def static_storage_imported_analysis(expression)
  return nil unless expression.is_a?(AST::MemberAccess)
  return nil unless expression.receiver.is_a?(AST::Identifier)
  return nil unless @ctx.imports.key?(expression.receiver.name)

  imported_module = @ctx.imports.fetch(expression.receiver.name)
  return nil unless imported_module.values[expression.member]&.kind == :const

  analysis_for_module(imported_module.name)
end

#stored_ref_supported_type?(type, visited = {}) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/milk_tea/core/lowering/utils.rb', line 265

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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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



727
728
729
730
731
732
733
734
735
736
737
# File 'lib/milk_tea/core/lowering/utils.rb', line 727

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



721
722
723
724
725
# File 'lib/milk_tea/core/lowering/utils.rb', line 721

def switch_loop_target(target)
  return target unless target && target[:label]

  loop_exit_label(target[:label])
end

#terminating_ir_statement?(statement) ⇒ Boolean

Returns:

  • (Boolean)


803
804
805
# File 'lib/milk_tea/core/lowering/utils.rb', line 803

def terminating_ir_statement?(statement)
  statement.is_a?(IR::ReturnStmt) || statement.is_a?(IR::GotoStmt)
end

#validate_generic_type!(name, arguments) ⇒ Object



1066
1067
1068
# File 'lib/milk_tea/core/lowering/utils.rb', line 1066

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



1101
1102
1103
# File 'lib/milk_tea/core/lowering/utils.rb', line 1101

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



956
957
958
959
960
961
962
963
964
965
966
# File 'lib/milk_tea/core/lowering/utils.rb', line 956

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

Returns:

  • (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



299
300
301
302
303
304
305
306
# File 'lib/milk_tea/core/lowering/utils.rb', line 299

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



1249
1250
1251
1252
1253
1254
1255
# File 'lib/milk_tea/core/lowering/utils.rb', line 1249

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