Module: MilkTea::LowererDeclarations

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

Instance Method Summary collapse

Instance Method Details

#expanded_declarationsObject



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

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



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

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:)
  else
    IR::IntegerLiteral.new(value: 0, type:)
  end
end

#lower_constantsObject



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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/milk_tea/core/lowering/declarations.rb', line 177

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/milk_tea/core/lowering/declarations.rb', line 85

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



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

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_one_struct(decl, qualified_name, results) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/milk_tea/core/lowering/declarations.rb', line 149

def lower_one_struct(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_one_struct(nested, "#{qualified_name}.#{nested.name}", results)
  end
end

#lower_opaquesObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/milk_tea/core/lowering/declarations.rb', line 100

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:



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

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



123
124
125
126
127
# File 'lib/milk_tea/core/lowering/declarations.rb', line 123

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

#lower_structsObject



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

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

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

#lower_unionsObject



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

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



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/milk_tea/core/lowering/declarations.rb', line 203

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