Package com.tonic.analysis.typeinference
Class TypeSet
- java.lang.Object
-
- com.tonic.analysis.typeinference.TypeSet
-
public class TypeSet extends Object
A union of the types a value may hold, flagged complete when the members are exact and incomplete when subtypes may also occur.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddType(IRType type)Adds a type, ignoring null.static TypeSetallSubtypesOf(IRType type)Creates an incomplete set rooted at the given type, standing for it and all its subtypes.booleancontains(IRType type)Tests membership.booleanequals(Object o)IRTypegetAnyType()Returns an arbitrary member to stand in for the set.IRTypegetLeastUpperBound()Computes the least upper bound, collapsing any mix of reference types to Object.IRTypegetSingleType()Returns the sole member of a singleton set.Set<IRType>getTypes()inthashCode()booleanisComplete()booleanisEmpty()booleanisSingleton()TypeSetjoin(TypeSet other)Unions two sets; the result is complete only when both operands are.TypeSetmeet(TypeSet other)Intersects two sets; the result is complete when either operand is.voidremoveType(IRType type)Removes a type.voidsetIncomplete()Marks the set as possibly including subtypes of its members.intsize()StringtoString()
-
-
-
Constructor Detail
-
TypeSet
public TypeSet()
Creates an empty, complete type set.
-
TypeSet
public TypeSet(IRType singleType)
Creates a set holding one type, or an empty set if it is null.- Parameters:
singleType- the sole member, may be null
-
TypeSet
public TypeSet(Collection<? extends IRType> types)
Creates a set holding the given types.- Parameters:
types- the initial members
-
-
Method Detail
-
allSubtypesOf
public static TypeSet allSubtypesOf(IRType type)
Creates an incomplete set rooted at the given type, standing for it and all its subtypes.- Parameters:
type- the root type- Returns:
- the incomplete set
-
addType
public void addType(IRType type)
Adds a type, ignoring null.- Parameters:
type- the type to add, may be null
-
removeType
public void removeType(IRType type)
Removes a type.- Parameters:
type- the type to remove
-
size
public int size()
- Returns:
- the number of members
-
isEmpty
public boolean isEmpty()
- Returns:
- true when there are no members
-
isSingleton
public boolean isSingleton()
- Returns:
- true when there is exactly one member
-
getSingleType
public IRType getSingleType()
Returns the sole member of a singleton set.- Returns:
- the only member
- Throws:
IllegalStateException- if the set does not hold exactly one type
-
getAnyType
public IRType getAnyType()
Returns an arbitrary member to stand in for the set.- Returns:
- a member, or null when the set is empty
-
contains
public boolean contains(IRType type)
Tests membership.- Parameters:
type- the type to look for- Returns:
- true when the type is a member
-
isComplete
public boolean isComplete()
- Returns:
- true when the members are exact, false when subtypes may also occur
-
setIncomplete
public void setIncomplete()
Marks the set as possibly including subtypes of its members.
-
join
public TypeSet join(TypeSet other)
Unions two sets; the result is complete only when both operands are.- Parameters:
other- the set to union with- Returns:
- a new set holding the union
-
meet
public TypeSet meet(TypeSet other)
Intersects two sets; the result is complete when either operand is.- Parameters:
other- the set to intersect with- Returns:
- a new set holding the intersection
-
getLeastUpperBound
public IRType getLeastUpperBound()
Computes the least upper bound, collapsing any mix of reference types to Object.- Returns:
- the sole member when singleton, Object when all members are references, null when the set is empty or mixes primitives
-
-