18 using System.Collections.Generic;
22 using ErrorManagement;
23 using TypeSystem.Constraints;
25 using DynVarManagement;
26 using TypeSystem.Operations;
28 namespace TypeSystem {
43 private static int lastVariable = 0;
72 TypeTable.Instance.AddVarType(typeVariable);
73 TypeVariable.lastTypeVariable = typeVariable;
82 get {
return lastTypeVariable; }
89 get {
return equivalenceClass; }
90 set { equivalenceClass = value; }
97 get {
return this.variable; }
105 if (CheckRecursion())
107 if (this.equivalenceClass != null)
108 return this.equivalenceClass.Substitution;
116 private bool CheckRecursion()
118 IList<TypeExpression> teList =
new List<TypeExpression>();
119 if (this.equivalenceClass == null)
122 if (substitution == null)
126 return CheckRecursion(substitution as UnionType, teList);
128 return CheckRecursion(substitution as TypeVariable, teList);
133 private bool CheckRecursion(UnionType ut, IList<TypeExpression> teList)
135 foreach (var typeSet
in ut.TypeSet)
137 if (typeSet is TypeVariable && CheckRecursion(typeSet as TypeVariable, teList))
143 private bool CheckRecursion(TypeVariable tv, IList<TypeExpression> teList)
145 if (teList.Contains(tv))
149 if (tv.equivalenceClass == null)
152 if (substitution == null)
155 if (substitution is UnionType)
156 return CheckRecursion(substitution as UnionType,teList);
157 else if (substitution is TypeVariable)
158 return CheckRecursion(substitution as TypeVariable,teList);
171 this.BuildFullName();
172 return this.fullName;
182 this.isDynamic = value;
184 this.Substitution.IsDynamic = value;
196 this.variable = variable;
197 this.BuildFullName();
199 this.ValidTypeExpression =
true;
204 #region addToMyEquivalenceClass()
215 if (this.equivalenceClass == null)
217 bool added = this.equivalenceClass.add(
typeExpression, unification, previouslyUnified);
218 this.ValidTypeExpression =
false;
219 if (typeExpression is TypeVariable)
220 typeExpression.ValidTypeExpression =
false;
226 #region BuildTypeExpressionString()
232 if (this.ValidTypeExpression)
return this.typeExpression;
233 if (depthLevel <= 0)
return this.FullName;
235 if (equivalenceClass == null)
236 return this.typeExpression =
"Var(" + this.variable +
")";
237 string substitutionString, eqClassString;
238 eqClassString = equivalenceClass.ToString();
240 substitutionString =
"fresh variable";
242 substitutionString = equivalenceClass.Substitution.BuildTypeExpressionString(depthLevel - 1);
243 this.ValidTypeExpression =
true;
244 return this.typeExpression = String.Format(
"[Var({0})={1}={2}]", this.variable, eqClassString, substitutionString);
248 #region BuildFullName()
253 if (equivalenceClass == null) {
254 this.fullName =
"Var(" + this.variable +
")";
257 string eqClassString = equivalenceClass.ToString();
259 this.fullName = String.Format(
"[Var({0})={1}]", this.variable, eqClassString);
261 this.fullName = String.Format(
"[Var({0})={1}={2}]", this.variable, eqClassString, equivalenceClass.Substitution.FullName);
268 this.validTypeExpression =
false;
269 return this.typeExpression = this.BuildTypeExpressionString(1);
274 #region Equivalent() ANULADA
296 return op.Exec(
this, arg);
300 #region Dot() ANULADA
340 #region Parenthesis() ANULADA
371 #region Assignment() ANULADA
400 #region Bracket() ANULADA
430 #region Arithmetic RELATIONQL() ANULADA
516 #region PromotionLevel() ANULADA
534 #region Promotion() ANULADA
575 #region Cast() ANULADA
600 #region EqualsForOverload() ANULADA
624 bool success = this.addToMyEquivalenceClass(te, unification, previouslyUnified);
626 this.ValidTypeExpression = te.ValidTypeExpression =
false;
631 #region HasTypeVariables()
642 #region IsFressVariable()
648 return this.Substitution == null;
663 IList<EquivalenceClass> equivalenceClasses =
new List<EquivalenceClass>();
664 TypeVariable newTypeVariable = (TypeVariable)this.
CloneTypeVariables(typeVariableMappings, equivalenceClasses,
new List<ClassType>());
671 equivalenceClass.UpdateEquivalenceClass(typeVariableMappings);
672 newTypeVariable.ValidTypeExpression =
false;
674 return newTypeVariable;
678 #region CloneTypeVariables()
679 public override TypeExpression CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses, IList<ClassType> clonedClasses) {
692 if (typeVariableMappings.ContainsKey(
this))
693 return typeVariableMappings[
this];
695 newOne.IsDynamic = this.IsDynamic;
697 typeVariableMappings[
this] = newOne;
699 if (this.
EquivalenceClass != null && !equivalenceClasses.Contains(
this.equivalenceClass))
700 equivalenceClasses.Add(
this.equivalenceClass);
703 TypeExpression clonedSubstitution = this.Substitution.CloneTypeVariables(typeVariableMappings, equivalenceClasses, clonedClasses);
704 newOne.addToMyEquivalenceClass(clonedSubstitution, SortOfUnification.Equivalent,
new List<Pair<TypeExpression, TypeExpression>>());
707 newOne.equivalenceClass = this.equivalenceClass;
708 newOne.ValidTypeExpression =
false;
714 #region UpdateEquivalenceClass()
715 public override void UpdateEquivalenceClass(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<TypeExpression> previouslyUpdated) {
723 if (previouslyUpdated.Contains(
this))
725 previouslyUpdated.Add(
this);
730 this.Substitution.UpdateEquivalenceClass(typeVariableMappings, previouslyUpdated);
731 this.ValidTypeExpression =
false;
746 return this.Substitution.Freeze();
754 return ((TypeVariable)this.
Substitution).Freeze(evaluated);
755 else if (this.Substitution is UnionType)
756 return ((UnionType)this.Substitution).
Freeze(evaluated);
757 return this.Substitution.Freeze();
765 internal override TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses,
MethodType methodAnalyzed) {
776 if (clonedTypeVariables.ContainsKey(
this.variable))
778 return clonedTypeVariables[this.variable];
782 newTypeVariable.variable = TypeVariable.NewTypeVariable.Variable;
784 clonedTypeVariables[this.variable] = newTypeVariable;
786 if (newTypeVariable.equivalenceClass != null)
787 newTypeVariable.equivalenceClass.CloneTypeVariables(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
788 newTypeVariable.BuildFullName();
789 newTypeVariable.BuildTypeExpressionString(2);
790 return newTypeVariable;
792 if (methodAnalyzed != null) {
795 methodAnalyzed.AddConstraint(constraint);
797 clonedTypeVariables[this.variable] = constraint.ReturnType;
799 if (this.equivalenceClass != null)
800 this.equivalenceClass.CloneTypeVariables(clonedTypeVariables, equivalenceClasses, methodAnalyzed);
801 return constraint.ReturnType;
807 #region Equals&GetHashCode()
808 public override bool Equals(
object obj) {
812 if (typeVariable == null)
814 return this.variable == typeVariable.variable;
817 return this.variable;
831 bool ret = this.equivalenceClass.Remove(toRemove);
832 this.ValidTypeExpression =
false;
840 public override string ILType() {
847 return this.Substitution.ILType();
853 #region IsValueType()
860 return this.Substitution.IsValueType();
869 return ((UnionType) this.
Substitution).IsValueType(evaluated);
871 return ((TypeVariable) this.Substitution).
IsValueType(evaluated);
872 return this.Substitution.IsValueType();
bool IsValueType(IList< TypeExpression > evaluated)
static TypeVariable NewTypeVariable
Gets a new identify to the variable type
override int GetHashCode()
override TypeExpression CloneTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< EquivalenceClass > equivalenceClasses, IList< ClassType > clonedClasses)
Method that clones each type variable of a type expression. Equivalence classes are not cloned (but i...
override string FullName
The full name in type variables is calculated just in time
TypeExpression Substitution
A equivalence class could be bounded to a unique no variable type
override void BuildFullName()
Creates/Updates the full name of the type expression
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
override string ILType()
Gets the type name to use in IL code.
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
override bool IsFreshVariable()
To know if it is a type variable with no substitution
static TypeVariable LastTypeVariable
Gets the last type variable created
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
override string ToString()
Abstract class that represents all different types.
Represents a generic type expression
override bool Equals(object obj)
override bool Remove(TypeVariable toRemove)
When loops are detected, it is necesary to suppress a new extra variable returned in the return type ...
override bool IsDynamic
Indicates if the type has been set as dynamic
string typeExpression
Represents the type by a debug string Note: WriteType expression is the longest recursive representat...
Representa a method type.
override object AcceptOperation(TypeSystemOperation op, object arg)
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Check if the type can make a method operation.
override void UpdateEquivalenceClass(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< TypeExpression > previouslyUpdated)
Replaces the equivalence class of type variables substituting the old type variables for the new ones...
const int MAX_DEPTH_LEVEL_TYPE_EXPRESSION
In order to avoid stack overflow in the construction of typeexpression string (ToString), we set a maximum level of depth
TypeExpression Substitution
Gets the substitution; null if it does not exist
override TypeExpression CloneType(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
This method creates a new type variable. It these type variables where bounded to types or other type...
TypeExpression Freeze(IList< TypeExpression > evaluated)
override string BuildTypeExpressionString(int depthLevel)
The type expression following the following convention: variable={equivalenceclass}=substitution ...
EquivalenceClass EquivalenceClass
The equivalence class of the type variable (see the dragon book)
int Variable
Concrete value for the variable type
override TypeExpression Freeze()
WriteType variable may change its type's substitution (e.g., field type variables) This method return...