Class: MilkTea::Types::GenericStructDefinition

Inherits:
Base
  • Object
show all
Defined in:
lib/milk_tea/core/types/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#accept, #bitwise?, #boolean?, #float?, #integer?, #numeric?, #sendable?, #void?

Constructor Details

#initialize(name, type_params, module_name: nil, external: false, packed: false, alignment: nil, linkage_name: nil, lifetime_params: []) ⇒ GenericStructDefinition

Returns a new instance of GenericStructDefinition.



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
# File 'lib/milk_tea/core/types/types.rb', line 794

def initialize(name, type_params, module_name: nil, external: false, packed: false, alignment: nil, linkage_name: nil, lifetime_params: [])
  @name = name
  @type_params = type_params.freeze
  @type_param_constraints = {}.freeze
  @module_name = module_name
  @external = external
  @packed = packed
  @alignment = alignment
  @linkage_name = linkage_name
  @lifetime_params = lifetime_params.freeze
  @fields = {}
  @events = {}
  @instances = {}
  @ast_declaration = nil
end

Instance Attribute Details

#alignmentObject (readonly)

Returns the value of attribute alignment.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def alignment
  @alignment
end

#ast_declarationObject

Returns the value of attribute ast_declaration.



792
793
794
# File 'lib/milk_tea/core/types/types.rb', line 792

def ast_declaration
  @ast_declaration
end

#externalObject (readonly)

Returns the value of attribute external.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def external
  @external
end

#lifetime_paramsObject (readonly)

Returns the value of attribute lifetime_params.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def lifetime_params
  @lifetime_params
end

#linkage_nameObject (readonly)

Returns the value of attribute linkage_name.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def linkage_name
  @linkage_name
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def module_name
  @module_name
end

#nameObject (readonly)

Returns the value of attribute name.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def name
  @name
end

#packedObject (readonly)

Returns the value of attribute packed.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def packed
  @packed
end

#type_param_constraintsObject (readonly)

Returns the value of attribute type_param_constraints.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def type_param_constraints
  @type_param_constraints
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



791
792
793
# File 'lib/milk_tea/core/types/types.rb', line 791

def type_params
  @type_params
end

Instance Method Details

#childrenObject



899
900
901
# File 'lib/milk_tea/core/types/types.rb', line 899

def children
  fields.values
end

#define_events(events) ⇒ Object



815
816
817
818
# File 'lib/milk_tea/core/types/types.rb', line 815

def define_events(events)
  @events = events.freeze
  self
end

#define_fields(fields) ⇒ Object



810
811
812
813
# File 'lib/milk_tea/core/types/types.rb', line 810

def define_fields(fields)
  @fields = fields.freeze
  self
end

#define_type_param_constraints(type_param_constraints) ⇒ Object



820
821
822
823
# File 'lib/milk_tea/core/types/types.rb', line 820

def define_type_param_constraints(type_param_constraints)
  @type_param_constraints = type_param_constraints.freeze
  self
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


859
860
861
862
863
864
865
866
867
868
# File 'lib/milk_tea/core/types/types.rb', line 859

def eql?(other)
  other.class == self.class &&
    other.name == name &&
    other.type_params == type_params &&
    other.module_name == module_name &&
    other.external == external &&
    other.packed == packed &&
    other.alignment == alignment &&
    other.linkage_name == linkage_name
end

#event(name) ⇒ Object



844
845
846
# File 'lib/milk_tea/core/types/types.rb', line 844

def event(name)
  @events[name]
end

#eventsObject



840
841
842
# File 'lib/milk_tea/core/types/types.rb', line 840

def events
  @events
end

#field(name) ⇒ Object



836
837
838
# File 'lib/milk_tea/core/types/types.rb', line 836

def field(name)
  @fields[name]
end

#field_c_name(name) ⇒ Object



852
853
854
855
856
857
# File 'lib/milk_tea/core/types/types.rb', line 852

def field_c_name(name)
  stripped = name.delete_suffix("_")
  return stripped if stripped != name && MilkTea::KEYWORDS.key?(stripped)

  name
end

#fieldsObject



832
833
834
# File 'lib/milk_tea/core/types/types.rb', line 832

def fields
  @fields
end

#has_events?Boolean

Returns:

  • (Boolean)


848
849
850
# File 'lib/milk_tea/core/types/types.rb', line 848

def has_events?
  !@events.empty?
end

#hashObject



872
873
874
# File 'lib/milk_tea/core/types/types.rb', line 872

def hash
  [self.class, name, type_params, module_name, external, packed, alignment, linkage_name].hash
end

#instantiate(arguments) ⇒ Object

Raises:

  • (ArgumentError)


876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/milk_tea/core/types/types.rb', line 876

def instantiate(arguments)
  raise ArgumentError, "#{name} expects #{type_params.length} type arguments, got #{arguments.length}" unless arguments.length == type_params.length

  key = arguments.dup.freeze
  return @instances[key] if @instances.key?(key)

  substitutions = type_params.zip(arguments).to_h
  lifetime_params.each do |lt|
    substitutions[lt] = arguments.find { |a| a.is_a?(LifetimeRef) && a.name == lt } || LifetimeRef.new(lt)
  end
  instance = StructInstance.new(self, arguments)
  @instances[key] = instance
  instance.define_fields(
    @fields.transform_values { |type| Types.substitute_type_variables(type, substitutions) },
  ).define_events(
    @events.transform_values { |type| Types.substitute_type_variables(type, substitutions) },
  )
end

#set_layout(packed:, alignment:) ⇒ Object



825
826
827
828
829
830
# File 'lib/milk_tea/core/types/types.rb', line 825

def set_layout(packed:, alignment:)
  @packed = packed
  @alignment = alignment
  @instances.each_value { |instance| instance.set_layout(packed:, alignment:) }
  self
end

#to_sObject



895
896
897
# File 'lib/milk_tea/core/types/types.rb', line 895

def to_s
  module_name ? "#{module_name}.#{name}" : name
end