Class: Stone::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/stone/type.rb

Overview

Value object representing a type in the Stone language. Each type (Int, Bool, String, etc.) is a singleton instance registered in TypeRegistry. Access via Stone::Type::Int, Stone::Type::Bool, etc.

Direct Known Subclasses

Union

Defined Under Namespace

Classes: Union

Constant Summary collapse

PRIMITIVE_SIZES =

Size in bytes for primitive types, used for union payload sizing

{
  "Int" => 8,
  "Bool" => 1,
  "String" => 8,  # pointer size
  "Null" => 0,
  "Type" => 8,    # pointer size
  "FieldList" => 8  # pointer size
}.freeze
PRIMITIVE_ALIGNMENTS =

Alignment requirements for primitive types

{
  "Int" => 8,
  "Bool" => 1,
  "String" => 8,  # pointer alignment
  "Null" => 1,
  "Type" => 8,    # pointer alignment
  "FieldList" => 8  # pointer alignment
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, llvm_type:, property_types: {}, fields: nil, **options) ⇒ Type



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stone/type.rb', line 30

def initialize(name:, llvm_type:, property_types: {}, fields: nil, **options)
  @name = name
  @llvm_type = llvm_type
  @property_types = property_types
  @fields = fields
  @primitive = options[:primitive] || false
  @min = options[:min]
  @max = options[:max]
  @param_types = options[:param_types]
  @return_type = options[:return_type]
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



27
28
29
# File 'lib/stone/type.rb', line 27

def fields
  @fields
end

#llvm_typeObject (readonly)

Returns the value of attribute llvm_type.



27
28
29
# File 'lib/stone/type.rb', line 27

def llvm_type
  @llvm_type
end

#maxObject (readonly)

Returns the value of attribute max.



27
28
29
# File 'lib/stone/type.rb', line 27

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



27
28
29
# File 'lib/stone/type.rb', line 27

def min
  @min
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/stone/type.rb', line 27

def name
  @name
end

#param_typesObject (readonly)

Returns the value of attribute param_types.



27
28
29
# File 'lib/stone/type.rb', line 27

def param_types
  @param_types
end

#property_typesObject

Returns the value of attribute property_types.



28
29
30
# File 'lib/stone/type.rb', line 28

def property_types
  @property_types
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



27
28
29
# File 'lib/stone/type.rb', line 27

def return_type
  @return_type
end

Class Method Details

.function(param_types:, return_type:) ⇒ Object



157
158
159
160
161
162
# File 'lib/stone/type.rb', line 157

def self.function(param_types:, return_type:)
  param_names = param_types.map(&:name).join(", ")
  return_name = return_type.function? ? "(#{return_type.name})" : return_type.name
  name = "(#{param_names}) -> #{return_name}"
  new(name:, llvm_type: nil, param_types:, return_type:)
end

.primitive(name:, llvm_type:, property_types: {}, min: nil, max: nil) ⇒ Object



149
150
151
# File 'lib/stone/type.rb', line 149

def self.primitive(name:, llvm_type:, property_types: {}, min: nil, max: nil)
  new(name:, llvm_type:, property_types:, primitive: true, min:, max:)
end

.record(name:, fields:, llvm_type:) ⇒ Object



153
154
155
# File 'lib/stone/type.rb', line 153

def self.record(name:, fields:, llvm_type:)
  new(name:, llvm_type:, fields:, primitive: false)
end

.union(alternatives:) ⇒ Object



164
165
166
167
# File 'lib/stone/type.rb', line 164

def self.union(alternatives:)
  union = Union.new(alternatives:)
  union.alternatives.length == 1 ? union.alternatives.first : union
end

Instance Method Details

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



132
133
134
# File 'lib/stone/type.rb', line 132

def ==(other)
  other.is_a?(self.class) && other.name == name
end

#alignmentObject



72
73
74
75
76
77
78
# File 'lib/stone/type.rb', line 72

def alignment
  return PRIMITIVE_ALIGNMENTS[@name] if primitive? && PRIMITIVE_ALIGNMENTS.key?(@name)
  # Records are stored as pointers in unions
  return 8 if record?  # pointer alignment

  8  # default pointer alignment
end

#as_StringObject



128
129
130
# File 'lib/stone/type.rb', line 128

def as_String
  name
end

#compatible_with?(other) ⇒ Boolean



110
111
112
113
114
115
# File 'lib/stone/type.rb', line 110

def compatible_with?(other)
  return true if self == other
  return other.alternatives.any? { |alt| compatible_with?(alt) } if other.union?

  function? && function_compatible_with?(other)
end

#function?Boolean



94
95
96
# File 'lib/stone/type.rb', line 94

def function?
  @param_types.is_a?(Array)
end

#hashObject



137
138
139
# File 'lib/stone/type.rb', line 137

def hash
  name.hash
end

#inspectObject



145
146
147
# File 'lib/stone/type.rb', line 145

def inspect
  "#<Stone::Type:#{name}>"
end

#non_null_typeObject



106
107
108
# File 'lib/stone/type.rb', line 106

def non_null_type
  self
end

#nullable?Boolean



102
103
104
# File 'lib/stone/type.rb', line 102

def nullable?
  false
end

#payload_llvm_typeObject



84
85
86
87
88
89
90
91
92
# File 'lib/stone/type.rb', line 84

def payload_llvm_type
  case @name
  when "Null" then LLVM::Type.pointer
  when "Int" then LLVM::Int64.type
  when "Bool" then LLVM::Int1.type
  when "String" then LLVM::Type.pointer
  else LLVM::Type.pointer  # records and other pointer types
  end
end

#pointer_type?Boolean



80
81
82
# File 'lib/stone/type.rb', line 80

def pointer_type?
  %w[String Null].include?(@name) || record?
end

#primitive?Boolean



55
56
57
# File 'lib/stone/type.rb', line 55

def primitive?
  @primitive
end

#property_return_type(property_name) ⇒ Object



42
43
44
# File 'lib/stone/type.rb', line 42

def property_return_type(property_name)
  @property_types[property_name] || field_type(property_name)
end

#record?Boolean



59
60
61
# File 'lib/stone/type.rb', line 59

def record?
  !@primitive && !@fields.nil?
end

#size_bytesObject



63
64
65
66
67
68
69
70
# File 'lib/stone/type.rb', line 63

def size_bytes
  return PRIMITIVE_SIZES[@name] if primitive? && PRIMITIVE_SIZES.key?(@name)
  # Records are stored as pointers in unions (to avoid infinite recursion with recursive types)
  return 8 if record?  # pointer size
  return 8 if function?  # function pointer

  8  # default
end

#to_sObject



141
142
143
# File 'lib/stone/type.rb', line 141

def to_s
  name
end

#union?Boolean



98
99
100
# File 'lib/stone/type.rb', line 98

def union?
  false
end