Module: MilkTea::Lowering::Declarations

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

Instance Method Summary collapse

Instance Method Details

#expanded_declarationsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/milk_tea/core/lowering/declarations.rb', line 6

def expanded_declarations
  @ctx.ast.declarations.flat_map do |decl|
    case decl
    when AST::WhenStmt
      val = compile_time_const_value(decl.discriminant)
      next [decl] if val.nil?

      chosen = decl.branches.find { |b| val == compile_time_const_value(b.pattern) }
      body = chosen ? chosen.body : (decl.else_body || [])
      body
    else
      [decl]
    end
  end
end

#lower_const_value_literal(type, const_value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/milk_tea/core/lowering/declarations.rb', line 47

def lower_const_value_literal(type, const_value)
  case const_value
  when Integer
    value = if type.respond_to?(:integer_width) && (width = type.integer_width) && width < 64
      mask = (1 << width) - 1
      masked = const_value & mask
      if type.respond_to?(:unsigned_integer?) && !type.unsigned_integer? && masked >= (1 << (width - 1))
        masked - (1 << width)
      else
        masked
      end
    else
      const_value
    end
    IR::IntegerLiteral.new(value:, type:)
  when Float
    IR::FloatLiteral.new(value: const_value, type:)
  when String
    IR::StringLiteral.new(value: const_value, type:, cstring: false)
  when TrueClass, FalseClass
    IR::BooleanLiteral.new(value: const_value, type:)
  when Array
    element_type = type.respond_to?(:arguments) ? type.arguments.first : @ctx.types["int"]
    elements = const_value.map do |element|
      lower_const_value_literal(element_type, element)
    end
    IR::ArrayLiteral.new(type:, elements:)
  when Hash
    fields = const_value.map do |name, field_value|
      field_type = type.field(name)
      raise LoweringError.new("constant struct field #{name} not found in #{type}", line: 0, column: 0, path: @ctx.current_analysis_path) unless field_type
      IR::AggregateField.new(name:, value: lower_const_value_literal(field_type, field_value))
    end
    IR::AggregateLiteral.new(type:, fields:)
  when CompileTime::VariantValue
    arm_field_types = type.respond_to?(:arm) ? (type.arm(const_value.arm) || {}) : {}
    fields = const_value.fields.map do |field_name, field_value|
      field_type = arm_field_types[field_name]
      raise LoweringError.new("constant variant arm #{const_value.arm} field #{field_name} not found in #{type}", line: 0, column: 0, path: @ctx.current_analysis_path) unless field_type
      IR::AggregateField.new(name: field_name, value: lower_const_value_literal(field_type, field_value))
    end
    IR::VariantLiteral.new(type:, arm_name: const_value.arm, fields:)
  else
    raise LoweringError.new("unsupported const value type #{const_value.class}", line: 0, column: 0, path: @ctx.current_analysis_path)
  end
end

#lower_constantsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/milk_tea/core/lowering/declarations.rb', line 22

def lower_constants
  expanded_declarations.grep(AST::ConstDecl).filter_map do |decl|
    type = @ctx.values.fetch(decl.name).type
    const_value = @ctx.values.fetch(decl.name).const_value

    next if type == Types::BUILTIN_TYPE_META_TYPE

    if const_value && (decl.value.is_a?(AST::Call) || decl.value.is_a?(AST::Specialization))
      value = lower_const_value_literal(type, const_value)
    elsif const_value && (decl.block_body || decl.value.is_a?(AST::ExpressionList))
      if const_value.is_a?(Array) && const_value.empty? && decl.value.is_a?(AST::ExpressionList) && !decl.value.elements.empty?
        value = lower_static_storage_initializer(decl.value, env: empty_env, expected_type: type)
      else
        value = lower_const_value_literal(type, const_value)
      end
    elsif decl.block_body || decl.value.is_a?(AST::ExpressionList)
      raise LoweringError.new("constant #{decl.name} has no compile-time value", line: decl.line, column: decl.column)
    else
      value = lower_static_storage_initializer(decl.value, env: empty_env, expected_type: type)
    end

    IR::Constant.new(name: decl.name, linkage_name: value_c_name(decl.name), type:, value:, line: decl.line, path: @ctx.current_analysis_path)
  end
end

#lower_enumsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/milk_tea/core/lowering/declarations.rb', line 186

def lower_enums
  expanded_declarations.filter_map do |decl|
    case decl
    when AST::EnumDecl, AST::FlagsDecl
      enum_type = @ctx.types.fetch(decl.name)
      backing_type = enum_type.backing_type
      members = decl.members.map do |member|
        value = if member.value
                  lower_expression(member.value, env: empty_env, expected_type: backing_type)
                else
                  IR::IntegerLiteral.new(value: enum_type.member_value(member.name), type: backing_type)
                end
        IR::EnumMember.new(name: member.name, linkage_name: enum_member_c_name(enum_type, member.name), value:)
      end

      IR::EnumDecl.new(
        name: decl.name,
        linkage_name: c_type_name(enum_type),
        backing_type:,
        members:,
        flags: decl.is_a?(AST::FlagsDecl),
      )
    end
  end
end

#lower_globalsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/milk_tea/core/lowering/declarations.rb', line 94

def lower_globals
  expanded_declarations.filter_map do |decl|
    next unless decl.is_a?(AST::VarDecl) || decl.is_a?(AST::EventDecl)

    type = @ctx.values.fetch(decl.name).type
    ensure_event_runtime(type) if type.is_a?(Types::Event)
    value = if decl.is_a?(AST::VarDecl) && decl.value
      lower_static_storage_initializer(decl.value, env: empty_env, expected_type: type)
    else
      IR::ZeroInit.new(type: type)
    end
    IR::Global.new(name: decl.name, linkage_name: value_c_name(decl.name), type:, value:)
  end
end

#lower_imported_external_opaquesObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/milk_tea/core/lowering/declarations.rb', line 121

def lower_imported_external_opaques
  each_raw_module_analysis.flat_map do |analysis|
    analysis.ast.declarations.grep(AST::OpaqueDecl).filter_map do |decl|
      opaque_type = analysis.types.fetch(decl.name)
      next unless forward_declarable_external_opaque?(opaque_type)

      IR::OpaqueDecl.new(name: decl.name, linkage_name: opaque_c_type_name(opaque_type), forward_declarable: true, source_module: analysis.module_name)
    end
  end.uniq { |decl| decl.linkage_name }
end

#lower_opaquesObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/milk_tea/core/lowering/declarations.rb', line 109

def lower_opaques
  expanded_declarations.grep(AST::OpaqueDecl).map do |decl|
    opaque_type = @ctx.opaque_types.fetch(decl.name)
    IR::OpaqueDecl.new(
      name: decl.name,
      linkage_name: opaque_c_type_name(opaque_type),
      forward_declarable: opaque_forward_declarable?(opaque_type),
      source_module: @ctx.module_name,
    )
  end
end

#lower_static_assert(statement, env:) ⇒ Object

Raises:



138
139
140
141
142
143
144
145
146
# File 'lib/milk_tea/core/lowering/declarations.rb', line 138

def lower_static_assert(statement, env:)
  condition_value = compile_time_const_value(statement.condition, env:)
  raise LoweringError.new("static_assert condition must lower to a compile-time bool constant", line: 0, column: 0, path: @ctx.current_analysis_path) unless condition_value == true || condition_value == false

  IR::StaticAssert.new(
    condition: IR::BooleanLiteral.new(value: condition_value, type: @ctx.types.fetch("bool")),
    message: lower_expression(statement.message, env:, expected_type: @ctx.types.fetch("str")),
  )
end

#lower_static_assertsObject



132
133
134
135
136
# File 'lib/milk_tea/core/lowering/declarations.rb', line 132

def lower_static_asserts
  expanded_declarations.grep(AST::StaticAssert).map do |statement|
    lower_static_assert(statement, env: empty_env)
  end
end

#lower_struct_decl(decl, qualified_name, results) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/milk_tea/core/lowering/declarations.rb', line 158

def lower_struct_decl(decl, qualified_name, results)
  struct_type = @ctx.struct_types.fetch(qualified_name)
  fields = decl.fields.map do |field|
    IR::Field.new(name: field.name, type: struct_type.field(field.name))
  end
  decl.events.each do |event_decl|
    event_type = struct_type.event(event_decl.name)
    ensure_event_runtime(event_type)
    fields << IR::Field.new(name: event_type.hidden_field_name, type: event_type)
  end

  results << IR::StructDecl.new(name: decl.name, linkage_name: c_type_name(struct_type), fields:, packed: decl.packed, alignment: decl.alignment, source_module: @ctx.module_name)

  decl.nested_types.each do |nested|
    lower_struct_decl(nested, "#{qualified_name}.#{nested.name}", results)
  end
end

#lower_structsObject



148
149
150
151
152
153
154
155
156
# File 'lib/milk_tea/core/lowering/declarations.rb', line 148

def lower_structs
  expanded_declarations.grep(AST::StructDecl).filter_map do |decl|
    next unless decl.type_params.empty?

    results = []
    lower_struct_decl(decl, decl.name, results)
    results
  end.flatten
end

#lower_unionsObject



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

def lower_unions
  expanded_declarations.grep(AST::UnionDecl).map do |decl|
    union_type = @ctx.union_types.fetch(decl.name)
    fields = decl.fields.map do |field|
      IR::Field.new(name: field.name, type: union_type.field(field.name))
    end
    IR::UnionDecl.new(name: decl.name, linkage_name: c_type_name(union_type), fields:, source_module: @ctx.module_name)
  end
end

#lower_variantsObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/milk_tea/core/lowering/declarations.rb', line 212

def lower_variants
  expanded_declarations.filter_map do |decl|
    next unless decl.is_a?(AST::VariantDecl)

    variant_type = @ctx.types.fetch(decl.name)
    next if variant_type.is_a?(Types::GenericVariantDefinition)

    outer_c = c_type_name(variant_type)
    arms = decl.arms.map do |arm|
      arm_c = "#{outer_c}_#{arm.name}"
      fields = arm.fields.map do |field|
        field_type = variant_type.arm(arm.name).fetch(field.name)
        IR::Field.new(name: field.name, type: field_type)
      end
      IR::VariantArm.new(name: arm.name, linkage_name: arm_c, fields:)
    end
    IR::VariantDecl.new(name: decl.name, linkage_name: outer_c, arms:, source_module: @ctx.module_name)
  end
end