Module: MilkTea::Lowering::Declarations
- Included in:
- MilkTea::Lowerer
- Defined in:
- lib/milk_tea/core/lowering/declarations.rb
Instance Method Summary collapse
-
#ensure_registered_storage_type_types(type, seen = {}) ⇒ Object
Register C struct typedefs for every tuple type reachable from a module-storage type (consts and globals).
- #expanded_declarations ⇒ Object
- #lower_const_value_literal(type, const_value) ⇒ Object
- #lower_constants ⇒ Object
- #lower_enums ⇒ Object
- #lower_globals ⇒ Object
- #lower_imported_external_opaques ⇒ Object
- #lower_opaques ⇒ Object
- #lower_static_assert(statement, env:) ⇒ Object
- #lower_static_asserts ⇒ Object
- #lower_struct_decl(decl, qualified_name, results) ⇒ Object
- #lower_structs ⇒ Object
- #lower_unions ⇒ Object
- #lower_variants ⇒ Object
- #static_initializer_ir_has_call?(node) ⇒ Boolean
Instance Method Details
#ensure_registered_storage_type_types(type, seen = {}) ⇒ Object
Register C struct typedefs for every tuple type reachable from a
module-storage type (consts and globals). ensure_tuple_struct is
normally called while lowering tuple expressions, so a tuple that
only appears in static storage never gets its mt_tuple_... typedef
emitted; the C compiler then fails with "unknown type name".
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 87 def ensure_registered_storage_type_types(type, seen = {}) return if type.nil? || seen[type] seen[type] = true if type.is_a?(Types::Tuple) ensure_tuple_struct(type) type.element_types.each { |element_type| ensure_registered_storage_type_types(element_type, seen) } end case type when Types::Nullable ensure_registered_storage_type_types(type.base, seen) when Types::Span ensure_registered_storage_type_types(type.element_type, seen) when Types::Task ensure_registered_storage_type_types(type.result_type, seen) when Types::GenericInstance, Types::StructInstance, Types::VariantInstance type.arguments.each do |argument| ensure_registered_storage_type_types(argument, seen) unless argument.is_a?(Types::LiteralTypeArg) end end if type.respond_to?(:fields) && type.fields type.fields.each_value { |field_type| ensure_registered_storage_type_types(field_type, seen) } end if type.respond_to?(:arms) type.arms.each_value do |arm_fields| arm_fields.each_value { |field_type| ensure_registered_storage_type_types(field_type, seen) } end end end |
#expanded_declarations ⇒ Object
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 @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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 119 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_constants ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 22 def lower_constants .grep(AST::ConstDecl).filter_map do |decl| type = @ctx.values.fetch(decl.name).type const_value = @ctx.values.fetch(decl.name).const_value ensure_registered_storage_type_types(type) next if type == Types::BUILTIN_TYPE_META_TYPE if !const_value.nil? && (decl.value.is_a?(AST::Call) || decl.value.is_a?(AST::Specialization)) value = lower_const_value_literal(type, const_value) elsif !const_value.nil? && (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, path: @ctx.current_analysis_path) else value = lower_static_storage_initializer(decl.value, env: empty_env, expected_type: type) if (decl.value.is_a?(AST::Call) || decl.value.is_a?(AST::Specialization)) && static_initializer_ir_has_call?(value) raise LoweringError.new("constant #{decl.name} initializer is not a compile-time value", line: decl.line, column: decl.column, path: @ctx.current_analysis_path) end 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_enums ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 259 def lower_enums .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_globals ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 166 def lower_globals .filter_map do |decl| next unless decl.is_a?(AST::VarDecl) || decl.is_a?(AST::EventDecl) type = @ctx.values.fetch(decl.name).type ensure_registered_storage_type_types(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_opaques ⇒ Object
194 195 196 197 198 199 200 201 202 203 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 194 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_opaques ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 182 def lower_opaques .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
211 212 213 214 215 216 217 218 219 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 211 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., env:, expected_type: @ctx.types.fetch("str")), ) end |
#lower_static_asserts ⇒ Object
205 206 207 208 209 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 205 def lower_static_asserts .grep(AST::StaticAssert).map do |statement| lower_static_assert(statement, env: empty_env) end end |
#lower_struct_decl(decl, qualified_name, results) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 231 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_structs ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 221 def lower_structs .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_unions ⇒ Object
249 250 251 252 253 254 255 256 257 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 249 def lower_unions .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_variants ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 285 def lower_variants .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 |
#static_initializer_ir_has_call?(node) ⇒ Boolean
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 |
# File 'lib/milk_tea/core/lowering/declarations.rb', line 51 def static_initializer_ir_has_call?(node) case node when IR::Call true when IR::AggregateLiteral node.fields.any? { |field| static_initializer_ir_has_call?(field.value) } when IR::ArrayLiteral node.elements.any? { |element| static_initializer_ir_has_call?(element) } when IR::VariantLiteral node.fields.any? { |field| static_initializer_ir_has_call?(field.value) } when IR::Binary static_initializer_ir_has_call?(node.left) || static_initializer_ir_has_call?(node.right) when IR::Unary static_initializer_ir_has_call?(node.operand) when IR::Conditional static_initializer_ir_has_call?(node.condition) || static_initializer_ir_has_call?(node.then_expression) || static_initializer_ir_has_call?(node.else_expression) when IR::Cast static_initializer_ir_has_call?(node.expression) when IR::AddressOf static_initializer_ir_has_call?(node.expression) when IR::Member static_initializer_ir_has_call?(node.receiver) when IR::Index, IR::CheckedIndex, IR::CheckedSpanIndex, IR::NullableIndex, IR::NullableSpanIndex static_initializer_ir_has_call?(node.receiver) || static_initializer_ir_has_call?(node.index) when IR::ReinterpretExpr static_initializer_ir_has_call?(node.expression) else false end end |