Class: Stone::Type
- Inherits:
-
Object
- Object
- Stone::Type
- 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
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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#llvm_type ⇒ Object
readonly
Returns the value of attribute llvm_type.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#param_types ⇒ Object
readonly
Returns the value of attribute param_types.
-
#property_types ⇒ Object
Returns the value of attribute property_types.
-
#return_type ⇒ Object
readonly
Returns the value of attribute return_type.
Class Method Summary collapse
- .function(param_types:, return_type:) ⇒ Object
- .primitive(name:, llvm_type:, property_types: {}, min: nil, max: nil) ⇒ Object
- .record(name:, fields:, llvm_type:) ⇒ Object
- .union(alternatives:) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #alignment ⇒ Object
- #as_String ⇒ Object
- #compatible_with?(other) ⇒ Boolean
- #function? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name:, llvm_type:, property_types: {}, fields: nil, **options) ⇒ Type
constructor
A new instance of Type.
- #inspect ⇒ Object
- #non_null_type ⇒ Object
- #nullable? ⇒ Boolean
- #payload_llvm_type ⇒ Object
- #pointer_type? ⇒ Boolean
- #primitive? ⇒ Boolean
- #property_return_type(property_name) ⇒ Object
- #record? ⇒ Boolean
- #size_bytes ⇒ Object
- #to_s ⇒ Object
- #union? ⇒ Boolean
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, **) @name = name @llvm_type = llvm_type @property_types = property_types @fields = fields @primitive = [:primitive] || false @min = [:min] @max = [:max] @param_types = [:param_types] @return_type = [:return_type] end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
27 28 29 |
# File 'lib/stone/type.rb', line 27 def fields @fields end |
#llvm_type ⇒ Object (readonly)
Returns the value of attribute llvm_type.
27 28 29 |
# File 'lib/stone/type.rb', line 27 def llvm_type @llvm_type end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
27 28 29 |
# File 'lib/stone/type.rb', line 27 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
27 28 29 |
# File 'lib/stone/type.rb', line 27 def min @min end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/stone/type.rb', line 27 def name @name end |
#param_types ⇒ Object (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_types ⇒ Object
Returns the value of attribute property_types.
28 29 30 |
# File 'lib/stone/type.rb', line 28 def property_types @property_types end |
#return_type ⇒ Object (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 |
#alignment ⇒ Object
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_String ⇒ Object
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 |
#hash ⇒ Object
137 138 139 |
# File 'lib/stone/type.rb', line 137 def hash name.hash end |
#inspect ⇒ Object
145 146 147 |
# File 'lib/stone/type.rb', line 145 def inspect "#<Stone::Type:#{name}>" end |
#non_null_type ⇒ Object
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_type ⇒ Object
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_bytes ⇒ Object
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_s ⇒ Object
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 |