19 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
24 using ErrorManagement;
25 using TypeSystem.Constraints;
27 using TypeSystem.Operations;
29 namespace TypeSystem {
48 private List<TypeExpression> paramList =
new List<TypeExpression>();
68 private int numberFreeVariablesCache;
73 private bool validNumberFreeVariables;
79 private bool isTypeInferred =
false;
88 private IDictionary<MethodType, TypeVariable> observerCollection;
95 return paramList[index];
101 get {
return this.ret; }
104 this.ValidTypeExpression = this.memberInfo.Class.ValidTypeExpression =
false;
112 get {
return this.paramList.Count; }
119 get {
return this.memberInfo; }
121 if (this.memberInfo == null) {
122 this.memberInfo = value;
133 get {
return this.constraints; }
140 get {
return this.astNode; }
141 set { this.astNode = value; }
147 internal override bool ValidTypeExpression {
152 this.BuildFullName();
155 this.MemberInfo.Class.ValidTypeExpression =
false;
165 get {
return this.isTypeInferred; }
177 this.BuildFullName();
185 #region AddParameter()
191 this.paramList.Add(paramType);
195 #region GetParameter()
203 if ((index >= 0) && (index < this.paramList.Count))
204 return this.paramList[index];
210 #region AddConstraint()
236 this.constraints.Add(constraint);
240 #region AddConstraint()
246 this.constraints.Constraints.RemoveAt(index);
250 #region BuildTypeExpressionString()
256 if (this.ValidTypeExpression)
return this.typeExpression;
257 if (depthLevel <= 0)
return this.FullName;
262 tE.AppendFormat(
"Method({0}, {1},", this.MemberInfo.Class.FullName, this.MemberInfo.MemberIdentifier);
265 for (
int i = 0; i < this.MemberInfo.Modifiers.Count - 1; i++)
266 tE.AppendFormat(
" {0} x",
this.MemberInfo.Modifiers[i]);
267 tE.AppendFormat(
" {0}", this.MemberInfo.Modifiers[this.MemberInfo.Modifiers.Count - 1]);
274 if (this.paramList.Count > 0)
275 for (
int i = 0; i < this.paramList.Count; i++) {
276 tE.AppendFormat(
"{0}", this.paramList[i].BuildTypeExpressionString(depthLevel - 1));
277 if (i < this.paramList.Count - 1)
283 if (this.ret != null)
284 tE.Append(this.ret.BuildTypeExpressionString(depthLevel - 1));
289 for (
int i = 0; i < this.Constraints.Count; i++) {
290 tE.Append(this.Constraints[i].ToString());
295 this.ValidTypeExpression =
true;
296 return this.typeExpression = tE.ToString();
300 #region BuildFullName()
306 if (this.memberInfo != null)
307 fN.AppendFormat(
"{0}.{1}:", this.MemberInfo.Class.FullName, this.MemberInfo.MemberIdentifier);
309 if (this.paramList.Count > 0)
310 for (
int i = 0; i < this.paramList.Count; i++) {
311 fN.AppendFormat(
"{0}", this.paramList[i].FullName);
312 if (i < this.paramList.Count - 1)
315 else fN.Append(
"void");
317 if (this.ret != null)
318 fN.Append(this.ret.FullName);
319 else fN.Append(
"void");
321 this.fullName = fN.ToString();
327 #region Parenthesis() ANULADA
407 #region Promotion (Overloaded)
422 if (arguments == null)
423 return this.paramList.Count == 0 ? 0 : -1;
426 if (arguments.GetLength(0) != this.ParameterListCount)
430 for (
int i = 0; i < this.ParameterListCount; i++) {
441 #region EqualsForOverload() NO ANULADA
452 #region GetNumberFreeVariables()
459 if (this.validNumberFreeVariables)
460 return this.numberFreeVariablesCache;
463 if (type.HasTypeVariables())
465 this.validNumberFreeVariables =
true;
466 return this.numberFreeVariablesCache = number++;
483 if (mt.ParameterListCount !=
this.ParameterListCount)
486 for (
int i = 0; i < this.ParameterListCount; i++)
487 if (!this.paramList[i].
Unify(mt.GetParameter(i), unification, previouslyUnified)) {
492 success = this.Return.Unify(mt.Return, unification, previouslyUnified);
494 this.ValidTypeExpression =
false;
495 te.ValidTypeExpression =
false;
500 return te.Unify(
this, unification, previouslyUnified);
506 #region HasTypeVariables()
514 return this.hasTypeVariablesCache;
515 bool toReturn = this.ret.HasTypeVariables();
518 if (type.HasTypeVariables()) {
522 this.validHasTypeVariables =
true;
523 return this.hasTypeVariablesCache = toReturn;
527 #region CloneMethodType()
528 public MethodType
CloneMethodType(IDictionary<TypeVariable, TypeVariable> typeVariableMappings) {
540 List<TypeExpression> oldParameterList = this.paramList;
541 methodType.paramList =
new List<TypeExpression>();
543 IList<EquivalenceClass> equivalenceClasses =
new List<EquivalenceClass>();
544 for (
int i = 0; i < this.paramList.Count; i++)
548 methodType.paramList.Add(oldParameterList[i].CloneTypeVariables(typeVariableMappings, equivalenceClasses,
new List<ClassType>()));
550 methodType.ret = this.ret.CloneTypeVariables(typeVariableMappings, equivalenceClasses,
new List<ClassType>());
552 AccessModifier accessModifier = (AccessModifier)this.
MemberInfo.
Clone();
553 accessModifier.Type = methodType;
554 methodType.MemberInfo = accessModifier;
562 foreach (EquivalenceClass equivalenceClass
in equivalenceClasses)
563 equivalenceClass.UpdateEquivalenceClass(typeVariableMappings);
564 methodType.ValidTypeExpression =
false;
573 internal override TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
612 UserType userType = formalMethod.MemberInfo.Class;
613 MethodType actualMethod = null;
615 IDictionary<TypeVariable, TypeVariable> typeVariableMappings =
new Dictionary<TypeVariable, TypeVariable>();
618 if (!formalMethod.
MemberInfo.
hasModifier(
Modifier.Static) && actualImplicitObject != null && actualImplicitObject != methodAnalyzed.memberInfo.Class) {
621 if (!actualImplicitObject.
Unify(formalImplicitObject,
SortOfUnification.Equivalent,
new List<Pair<TypeExpression, TypeExpression>>()))
623 formalImplicitObject.
Unify(actualImplicitObject,
SortOfUnification.Override,
new List<Pair<TypeExpression, TypeExpression>>());
624 actualImplicitObject.ValidTypeExpression =
false;
641 actualMethod =
new MethodType(actualReturnType);
644 actualMethod.AddParameter(arg);
647 if (!actualMethod.
Unify(formalMethod,
SortOfUnification.Equivalent,
new List<Pair<TypeExpression, TypeExpression>>())) {
658 for (
int i = 0; i < args.Length; i++)
665 formalMethod.Constraints.Check(methodAnalyzed, actualImplicitObject,
true, activeSortOfUnification, location);
672 ClassType.IsConcreteType(actualImplicitObject) != null ||
676 return returnType.EquivalenceClass.Substitution;
680 return originalReturnType;
685 #region AddInferredTypeObserver()
693 if (this.observerCollection == null)
694 this.observerCollection =
new Dictionary<MethodType, TypeVariable>();
695 this.observerCollection[method] = returnType;
700 #region TypeInferred()
706 this.isTypeInferred =
true;
708 if (this.observerCollection != null) {
709 foreach (KeyValuePair<MethodType, TypeVariable> pair
in this.observerCollection) {
711 pair.Key.Return = UnionType.collect(pair.Key.Return, this.Return);
714 this.Return.Remove(pair.Value);
717 pair.Value.Unify(this.Return, SortOfUnification.Equivalent,
new List<Pair<TypeExpression, TypeExpression>>());
724 #region IsValueType()
731 return this.ret.IsValueType();
bool IsTypeInferred
Its true when the type of the method has been completely inferred. Used to detect loops in the abstra...
static TypeVariable NewTypeVariable
Gets a new identify to the variable type
override void BuildFullName()
Creates/Updates the full name of the type expression
MethodDeclaration ASTNode
The AST node set by the VisitorTypeDefinition and used in the VisitorTypeInferece for abstract interp...
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
This method unifies two type expressions (this and te)
Represents a error produced when a MethodType has class information and tries to assign other class i...
bool validHasTypeVariables
To cache the result of the HasTypeVariables method
This class encapsulates a location in a specific file. Implements an Inmutable pattern. So it can be used in any context, that is his internal fields never change.
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
int GetNumberFreeVariables()
To know the number of parameters that has type variables. This is used for overload resolution...
override string BuildTypeExpressionString(int depthLevel)
Creates the type expression and the full name string.
Implemeents a double dispatcher pattern Role:
string MemberIdentifier
Gets the attribute name
void RemoveConstraint(int index)
Removes a constraint to the method.
Represents a error produced when the argument and parameter number is different.
bool hasModifier(Modifier mod)
To know if a modifier is supported
AccessModifier MemberInfo
Gets or sets the attribute information of method type
bool validTypeExpression
To implement a type expression cache
Abstract class that represents all different types.
Represents a generic type expression
System.Text.StringBuilder StringBuilder
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
int ParameterListCount
Gets the number of parameters
void AddConstraint(Constraint constraint)
Adds a new constraint to the method.
TypeExpression Return
Gets type expression method return
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
Encapsulates a declaration of a concrete method.
int Promotion(TypeExpression[] arguments, Location location)
Gets the promotion value of the method.
void addInferredTypeObserver(MethodType method, TypeVariable returnType)
Adds a new observer that listens to the event of inferred types
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
MethodType CloneMethodType(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
This method returns a new method type, creating new type variables for each parametern and the return...
virtual bool EqualsForOverload(TypeExpression typeExpression)
Used to not repeat methods in overload
ConstraintList Constraints
A set of constraints to be satisfied
Modifier
Indicates differents modifiers to use in class (only public, internal or static), fields or methods...
override object AcceptOperation(TypeSystemOperation op, object arg)
static TypeExpression methodCall(TypeExpression actualImplicitObject, MethodType formalMethod, TypeExpression[] args, MethodType methodAnalyzed, SortOfUnification activeSortOfUnification, Location location)
This method does the type inference in a method call including unification. It requires that a) the m...
string typeExpression
Represents the type by a debug string Note: WriteType expression is the longest recursive representat...
UserType Class
Gets or sets the class type reference
Representa a method type.
override Constraint CloneTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings, IList< EquivalenceClass > equivalenceClasses)
Method that clones each type variable of a constraint. Equivalence classes are not cloned (but includ...
List< Modifier > Modifiers
Gets the modifiers of the element
TypeExpression Substitution
Gets the substitution; null if it does not exist
Association Class between ClassType and MethodType (or Fields). Represents the access modifier inform...
MethodType(TypeExpression ret)
Constructor of MethodType
void TypeInferred()
This method is used to control loops (recursion)
Represents a error produced when the attribute identifier is not defined.
abstract bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Requires the implicit object to be a subtype of the type parameter
TypeExpression GetParameter(int index)
Gets the type expression of the specific parameter
Represents a class or interface type.
int Count
The number of constraints
TypeExpression getParam(int index)
void AddParameter(TypeExpression paramType)
Adds a new parameter type.
virtual TypeExpression CloneType(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
This method creates a new type, creating new type variables for type expression. It these type variab...
Representa a class attribute (fields or methods).