class Ikra::Types::UnionType
Represents a possibly polymorphic type consisting of 0..* instances of {RubyType}. Only primitive types or class types are allowed for these inner types, but not union types.
Attributes
@return [Set<RubyType>] Inner types
Public Class Methods
# File lib/types/types/union_type.rb, line 215 def create_bool return new(PrimitiveType::Bool) end
# File lib/types/types/union_type.rb, line 211 def create_float return new(PrimitiveType::Float) end
# File lib/types/types/union_type.rb, line 207 def create_int return new(PrimitiveType::Int) end
# File lib/types/types/union_type.rb, line 223 def create_nil return new(PrimitiveType::Nil) end
# File lib/types/types/union_type.rb, line 227 def create_unknown return new(UnknownType::Instance) end
# File lib/types/types/union_type.rb, line 219 def create_void return new(PrimitiveType::Void) end
# File lib/types/types/union_type.rb, line 31 def initialize(*types) # Check arguments types.each do |type| if type.is_union_type? raise AssertionError.new( "Union type not allowed as inner type of union type") end end @types = Set.new(types) end
# File lib/types/types/union_type.rb, line 231 def parameter_hash_to_s(hash) return hash.map do |name, type| name.to_s + ": " + type.to_s end.join(", ") end
Public Instance Methods
Alias for {#include_all?}
# File lib/types/types/union_type.rb, line 198 def <=(union_type) return union_type.include_all?(self) end
# File lib/types/types/union_type.rb, line 21 def ==(other) return other.class == self.class && other.types == self.types end
Adds a singleton type to this union type. @return True if the type was extended
# File lib/types/types/union_type.rb, line 158 def add(singleton_type) if singleton_type.is_union_type? raise AssertionError.new("Singleton type expected") end if singleton_type == PrimitiveType::Int && include?(PrimitiveType::Float) # Special rule: Coerce int to float return false elsif singleton_type == PrimitiveType::Float && include?(PrimitiveType::Int) # Special rule: Coerce int to float @types.delete(PrimitiveType::Int) @types.add(singleton_type) return true else is_expanded = !@types.include?(singleton_type) @types.add(singleton_type) return is_expanded end end
# File lib/types/types/union_type.rb, line 75 def c_size if is_singleton? return @types.first.c_size else return "sizeof(union_t)" end end
Removes all type information.
# File lib/types/types/union_type.rb, line 48 def clear! @types.clear end
# File lib/types/types/union_type.rb, line 25 def dup result = super result.types = @types.dup return result end
# File lib/types/types/union_type.rb, line 43 def empty? return @types.empty? end
Merges all inner types of the argument into this union type. @param [UnionType] union_type The other union type @return [Bool] true if the argument added new inner types to this union type
# File lib/types/types/union_type.rb, line 133 def expand(union_type) if not union_type.is_union_type? raise AssertionError.new( "Cannot expand with non-union type: #{union_type}") end is_expanded = false for type in union_type is_expanded = is_expanded | add(type) end return is_expanded end
Merges all inner types of the argument into this union type. @param [UnionType] union_type The other union type @return [UnionType] self
# File lib/types/types/union_type.rb, line 151 def expand_return_type(union_type) expand(union_type) return self end
Determines if this union type contains a specific singleton type. @param [RubyType] singleton_type
The other type @return [Bool] true if the type is included
# File lib/types/types/union_type.rb, line 181 def include?(singleton_type) if singleton_type.is_union_type? raise AssertionError.new("Union type can never be included in union type") end @types.include?(singleton_type) end
# File lib/types/types/union_type.rb, line 189 def include_all?(union_type) if not union_type.is_union_type? raise AssertionError.new("Union type expected") end return (union_type.types - @types).size == 0 end
# File lib/types/types/union_type.rb, line 99 def is_primitive? if is_singleton? return @types.first.is_primitive? else return false end end
# File lib/types/types/union_type.rb, line 115 def is_singleton? return @types.size == 1 end
# File lib/types/types/union_type.rb, line 107 def is_union_type? return true end
Removes all singleton types contained in `union_type` from this type.
# File lib/types/types/union_type.rb, line 53 def remove!(union_type) if union_type.is_singleton? raise AssertionError.new("Union type expected") else @types.delete_if do |type| union_type.include?(type) end end end
Returns the single inner type or raises an error if this union type contains more than one inner type. @return [RubyType] Inner type
# File lib/types/types/union_type.rb, line 121 def singleton_type if !is_singleton? raise AssertionError.new( "Union type is not singleton (found #{@types.size} types)") end return @types.first end
# File lib/types/types/union_type.rb, line 63 def size return @types.size end
# File lib/types/types/union_type.rb, line 67 def to_c_type if is_singleton? return @types.first.to_c_type else return "union_t" end end
# File lib/types/types/union_type.rb, line 91 def to_ffi_type if is_singleton? return @types.first.to_ffi_type else raise NotImplementedError.new end end
# File lib/types/types/union_type.rb, line 83 def to_ruby_type if is_singleton? return @types.first.to_ruby_type else raise NotImplementedError.new end end
# File lib/types/types/union_type.rb, line 202 def to_s return "U{#{@types.to_a.join(", ")}}" end
# File lib/types/types/union_type.rb, line 111 def to_union_type return self end