Class: Stone::AST::RecordInstantiation
- Inherits:
-
Expression
- Object
- Stone::AST
- Expression
- Stone::AST::RecordInstantiation
- Defined in:
- lib/stone/ast/record_instantiation.rb
Instance Attribute Summary collapse
-
#field_values ⇒ Object
readonly
Returns the value of attribute field_values.
-
#record_definition ⇒ Object
readonly
Returns the value of attribute record_definition.
-
#record_type_name ⇒ Object
readonly
Returns the value of attribute record_type_name.
Attributes inherited from Stone::AST
Instance Method Summary collapse
-
#initialize(record_type_name, field_values) ⇒ RecordInstantiation
constructor
A new instance of RecordInstantiation.
- #to_llir(builder, mod, scope = Stone::Scope.top_level) ⇒ Object
- #to_s ⇒ Object
- #type(_context = nil) ⇒ Object
Constructor Details
#initialize(record_type_name, field_values) ⇒ RecordInstantiation
Returns a new instance of RecordInstantiation.
12 13 14 15 16 17 |
# File 'lib/stone/ast/record_instantiation.rb', line 12 def initialize(record_type_name, field_values) @name = :record_instantiation @record_type_name = record_type_name @field_values = field_values @record_definition = nil end |
Instance Attribute Details
#field_values ⇒ Object (readonly)
Returns the value of attribute field_values.
10 11 12 |
# File 'lib/stone/ast/record_instantiation.rb', line 10 def field_values @field_values end |
#record_definition ⇒ Object (readonly)
Returns the value of attribute record_definition.
10 11 12 |
# File 'lib/stone/ast/record_instantiation.rb', line 10 def record_definition @record_definition end |
#record_type_name ⇒ Object (readonly)
Returns the value of attribute record_type_name.
10 11 12 |
# File 'lib/stone/ast/record_instantiation.rb', line 10 def record_type_name @record_type_name end |
Instance Method Details
#to_llir(builder, mod, scope = Stone::Scope.top_level) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/stone/ast/record_instantiation.rb', line 19 def to_llir(builder, mod, scope = Stone::Scope.top_level) record_def = mod.record_types[@record_type_name] fail "Unknown record type: #{@record_type_name}" unless record_def # Store record definition for later access @record_definition = record_def # Verify field count matches if @field_values.size != record_def.fields.size fail Stone::ArityError, "wrong number of arguments for #{@record_type_name} (given #{@field_values.size}, expected #{record_def.fields.size})" end # Evaluate each field value llvm_values = @field_values.map { |field_ast| field_ast.to_llir(builder, mod, scope) } # Convert struct values to pointers for record-typed fields llvm_values = convert_structs_to_pointers(builder, mod, record_def, llvm_values) # Create struct value create_struct(record_def.llvm_type(mod), llvm_values, builder) end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/stone/ast/record_instantiation.rb', line 41 def to_s "#{@record_type_name}(#{@field_values.map(&:to_s).join(', ')})" end |