// WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { if (te == null) { return(false); } bool success = this.addToMyEquivalenceClass(te, unification, previouslyUnified); // * Clears the type expression cache this.ValidTypeExpression = te.ValidTypeExpression = false; return(success); }
/// <summary> /// Tries to unify the constraints of a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">The location where method is called</param> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { TypeExpression result = (TypeExpression)this.FirstOperand.AcceptOperation(new CastOperation(this.CastType, methodAnalyzed, this.Location), null); if (result == null && showInvocationMessage) { ErrorManager.Instance.NotifyError(new ConstraintError(location)); return(null); } // * If no error exists, we return the casttype return(this.CastType); }
/// <summary> /// Constructor /// </summary> /// <param name="alpha">The type expression to be modified</param> /// <param name="beta">The type to be added</param> /// <param name="sortOfUnification">The kind of unification used in the assignment</param> public FieldTypeAssignmentConstraint(FieldType alpha, TypeExpression beta, SortOfUnification sortOfUnification) { this.alpha = alpha; this.beta = beta; if (sortOfUnification == SortOfUnification.Equivalent) { // * Equivalence is translated into override when fields are assigned this.sortOfUnification = SortOfUnification.Override; } else { this.sortOfUnification = sortOfUnification; } }
/// <summary> /// Tries to unify the constraints of a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">The location of the method call.</param> /// <returns>If the unification has been satisfied</returns> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { TypeExpression result = (TypeExpression)this.FirstOperand.Exec(new BracketOperation(this.SecondOperand, methodAnalyzed, true, this.Location)); if (result == null && showInvocationMessage) { ErrorManager.Instance.NotifyError(new ConstraintError(location)); return(null); } // * If no error exists, we unify the return type variable with the actual result this.ReturnType.Unify(result, SortOfUnification.Equivalent, new List <Pair <TypeExpression, TypeExpression> >()); this.ReturnType.ValidTypeExpression = this.ValidTypeExpression = false; return(this.ReturnType); }
// WriteType Promotion #region PromotionLevel() ANULADA ///// <summary> ///// Returns a value that indicates a promotion level. ///// </summary> ///// <param name="type">WriteType to promotion.</param> ///// <returns>Returns a promotion value.</returns> //public override int PromotionLevel(TypeExpression type) { // // * Bool type and type variable // if (TypeExpression.As<BoolType>(type)!=null) // return 0; // // * WriteType variable // TypeVariable typeVariable = type as TypeVariable; // if (typeVariable != null && typeVariable.Substitution==null) // // * A free variable is complete promotion // return 0; // // * Union type // UnionType unionType = TypeExpression.As<UnionType>(type); // if (unionType != null) // return unionType.SuperType(this); // // * Field type and bounded type variable // FieldType fieldType = TypeExpression.As<FieldType>(type); // if (fieldType != null) // return this.PromotionLevel(fieldType.FieldTypeExpression); // // * Use the BCL object oriented approach // return this.BCLType.PromotionLevel(type); //} #endregion // WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { BoolType bt = te as BoolType; if (bt != null) { return(true); } if (te is TypeVariable && unification != SortOfUnification.Incremental) { // * No incremental unification is commutative return(te.Unify(this, unification, previouslyUnified)); } return(false); }
/// <summary> /// Adds a type variable to the object's equivalence class /// </summary> /// <param name="typeExpression">The type variable to add</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the type variable has been actually added</returns> /// EN esta clase se le ha cambiado su visibilidad internal bool addToMyEquivalenceClass(TypeExpression typeExpression, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { // * It the type variable does not have a equivalence class, we create it if (this.equivalenceClass == null) { this.equivalenceClass = new EquivalenceClass(this); } bool added = this.equivalenceClass.add(typeExpression, unification, previouslyUnified); this.ValidTypeExpression = false; if (typeExpression is TypeVariable) { typeExpression.ValidTypeExpression = false; } return(added); }
// WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { // * Infinite recursion detection Pair <TypeExpression, TypeExpression> pair = new Pair <TypeExpression, TypeExpression>(this, te); if (previouslyUnified.Contains(pair)) { return(true); } previouslyUnified.Add(pair); bool success = false; ArrayType at = te as ArrayType; if (at != null) { success = this.arrayType.Unify(at.arrayType, unification, previouslyUnified); } else if (te is TypeVariable) { TypeVariable typeVariable = (TypeVariable)te; if (unification != SortOfUnification.Incremental) { // * Incremental is commutative success = typeVariable.Unify(this, unification, previouslyUnified); } else // * Array(var) should unify to Var=Array(int) { if (typeVariable.Substitution != null) { success = this.Unify(typeVariable.Substitution, unification, previouslyUnified); } else { success = false; } } } else if (te is UnionType) { success = te.Unify(this, unification, previouslyUnified); } // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; return(success); }
/// <summary> /// Assigns all the attributes in two this references, generating the appropriate constraints. /// </summary> /// <param name="classType">The type of this</param> /// <param name="typeOfThis1">The first value of this' type</param> /// <param name="typeOfThis2">The second value of this' type</param> /// <param name="methodAnalyzed">Method being analyzed</param> /// <param name="unification">WriteType of unification</param> /// <param name="actualImplicitObject">The actual implicit object</param> /// <summary> public static void AssignAttributes(UserType classType, TypeExpression typeOfThis1, TypeExpression typeOfThis2, MethodType methodAnalyzed, SortOfUnification unification, TypeExpression actualImplicitObject, Location location) { if (methodAnalyzed == null) { return; } foreach (KeyValuePair <string, AccessModifier> pair in classType.Fields) { TypeExpression fieldType = pair.Value.Type, fieldType1 = getFieldType(typeOfThis1, pair.Key), fieldType2 = getFieldType(typeOfThis2, pair.Key); TypeExpression unionType = UnionType.collect(fieldType1, fieldType2); fieldType.AcceptOperation(new AssignmentOperation(unionType, AssignmentOperator.Assign, methodAnalyzed, unification, actualImplicitObject, location), null); } }
/// <summary> /// Check if the type can make a method operation. /// </summary> /// <param name="actualImplicitObject">The actual implicit object employed to pass the message</param> /// <param name="arguments">Arguments of the method.</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> /// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Parenthesis(TypeExpression actualImplicitObject, TypeExpression[] arguments, MethodType methodAnalyzed, // SortOfUnification activeSortOfUnification, Location loc) { // TypeExpression method = overloadResolution(arguments, loc); // if (method == null) // return null; // return method.Parenthesis(actualImplicitObject, arguments, methodAnalyzed, activeSortOfUnification, loc); //} #endregion // WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { // * Infinite recursion detection Pair <TypeExpression, TypeExpression> pair = new Pair <TypeExpression, TypeExpression>(this, te); if (previouslyUnified.Contains(pair)) { return(true); } previouslyUnified.Add(pair); // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; // TODO return(false); }
/// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { InterfaceType it = TypeExpression.As <InterfaceType>(te); if (it != null) { bool success = (bool)this.AcceptOperation(new EquivalentOperation(it), null); // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; return(success); } if (te is TypeVariable && unification != SortOfUnification.Incremental) { // * No incremental unification is commutative return(te.Unify(this, unification, previouslyUnified)); } return(false); }
// WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { DoubleType dt = te as DoubleType; if (dt != null) { return(true); } if (te is TypeVariable && unification != SortOfUnification.Incremental) { // * No incremental unification is commutative return(te.Unify(this, unification, previouslyUnified)); } if (te is FieldType && unification == SortOfUnification.Equivalent) { return(((FieldType)te).FieldTypeExpression.Unify(this, unification, previouslyUnified)); } return(false); }
/// Assigns all the attributes in many this references, generating the appropriate constraints. /// </summary> /// <param name="classType">The type of this</param> /// <param name="typesOfThisAfterCases">The types of this after each case</param> /// <param name="methodAnalyzed">Method being analyzed</param> /// <param name="unification">WriteType of unification</param> /// <param name="actualImplicitObject">The actual implicit object</param> public static void AssignAttributes(UserType classType, IList <TypeExpression> typesOfThisAfterCases, MethodType methodAnalyzed, SortOfUnification unification, TypeExpression actualImplicitObject, Location location) { if (methodAnalyzed == null) { return; } foreach (KeyValuePair <string, AccessModifier> pair in classType.Fields) { TypeExpression fieldType = pair.Value.Type; TypeExpression unionType = null; foreach (TypeExpression typeOfThis in typesOfThisAfterCases) { TypeExpression eachFieldType = getFieldType(typeOfThis, pair.Key); unionType = UnionType.collect(unionType, eachFieldType); } fieldType.AcceptOperation(new AssignmentOperation(unionType, AssignmentOperator.Assign, methodAnalyzed, unification, actualImplicitObject, location), null); } }
/// <summary> /// Checks the constraints of a attribute assigment in a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">Location of the method call</param> /// <returns>The return type expression</returns> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { if (this.alpha.Substitution == null) { this.alpha.AcceptOperation(new AssignmentOperation(this.beta, AssignmentOperator.Assign, methodAnalyzed, this.sortOfUnification, actualImplicitObject, location), null); return(this.alpha); } FieldType fieldType = this.alpha.Substitution as FieldType; if (fieldType == null) { ErrorManager.Instance.NotifyError(new UnknownMemberError(this.memberName, location)); return(null); } SortOfUnification unification = activeSortOfUnification == SortOfUnification.Incremental ? SortOfUnification.Incremental: this.sortOfUnification; fieldType.AcceptOperation(new AssignmentOperation(this.beta, AssignmentOperator.Assign, methodAnalyzed, unification, actualImplicitObject, location), null); return(fieldType); }
/// <summary> /// Tries to unify the constraints of a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">Location of the method call</param> /// <returns>If the unification has been satisfied</returns> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { TypeExpression result; if (this.op != null) { //result = this.FirstOperand.Promotion(this.SecondOperand, (Enum)this.Operator, methodAnalyzed, this.Location); result = (TypeExpression)this.FirstOperand.AcceptOperation(PromotionOperation.Create(this.SecondOperand, (Enum)this.Operator, methodAnalyzed, this.Location), null); } else { result = (TypeExpression)this.FirstOperand.AcceptOperation(PromotionOperation.Create(this.SecondOperand, methodAnalyzed, this.Location), null); } if (result == null && showInvocationMessage) { ErrorManager.Instance.NotifyError(new ConstraintError(location)); return(null); } // * If no error exists, we return the supertype return(this.SecondOperand); }
/// <summary> /// Tries to unify the constraints of a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">The location where the location is called</param> /// <returns>If the unification has been satisfied</returns> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { // * If the actual implicit object is a field, we take its field's type FieldType implicitObjectAsField = TypeExpression.As <FieldType>(this.actualImplicitObject); ClassType implicitObjectAsClass; TypeExpression implicitObject = this.ActualImplicitObject; if (implicitObjectAsField != null) { implicitObject = implicitObjectAsField.FieldTypeExpression; implicitObjectAsClass = TypeExpression.As <ClassType>(implicitObjectAsField.FieldTypeExpression); } else { implicitObjectAsClass = TypeExpression.As <ClassType>(this.actualImplicitObject); } // * If the actual implicit object is concrete, so it is the saved implicit object if (ClassType.IsConcreteType(actualImplicitObject) != null && implicitObjectAsClass != null) { implicitObjectAsClass.ConcreteType = true; } // * If the unification in the method call is incremental, this is the one to be used SortOfUnification unification = activeSortOfUnification == SortOfUnification.Incremental ? SortOfUnification.Incremental : this.sortOfUnification; // * Checks the parenthesis operation TypeExpression result = (TypeExpression)this.FirstOperand.AcceptOperation(new ParenthesisOperation(implicitObject, this.Parameters, methodAnalyzed, unification, this.Location), null); if (result == null && showInvocationMessage) { ErrorManager.Instance.NotifyError(new ConstraintError(location)); return(null); } // * If no error exists, we unify the return type variable with the actual result this.ReturnType.Unify(result, SortOfUnification.Equivalent, new List <Pair <TypeExpression, TypeExpression> >()); this.ReturnType.ValidTypeExpression = this.ValidTypeExpression = false; return(this.ReturnType); }
///// <summary> ///// Check if the type can make an operation of field access. ///// Generates an error if the attribute does not exist. ///// Generates a constraint in case it is applied to a free variable. ///// </summary> ///// <param name="field">Field to access.</param> ///// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> ///// <param name="previousDot">To detect infinite loops. The types that have been previously passed the dot message. Used for union types.</param> ///// <param name="fileName">File name.</param> ///// <param name="line">Line number.</param> ///// <param name="column">Column number.</param> ///// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Dot(string field, MethodType methodAnalyzed, IList<TypeExpression> previousDot, Location loc) { // if (this.fieldType != null) { // // * If the field type is a dynamic union type, so it is the union // UnionType unionType = As<UnionType>(this.fieldType); // if (unionType != null) // DynVarOptions.Instance.AssignDynamism(this.fieldType, this.IsDynamic); // return this.fieldType.Dot(field, methodAnalyzed, previousDot, loc); // } // return null; //} ///// <summary> ///// Tries to find a attribute. ///// No error is generated if the attribute does not exist. ///// It does not generate a constraint in case it is applied to a free variable. ///// </summary> ///// <param name="memberName">Member to access.</param> ///// <param name="previousDot">To detect infinite loops. The types that have been previously passed the dot message. Used for union types.</param> ///// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Dot(string memberName, IList<TypeExpression> previousDot) { // if (this.fieldType != null) // return this.fieldType.Dot(memberName, previousDot); // return null; //} #endregion //#region Bracket() anulada ///// <summary> ///// Check if the type can make an array operation. ///// </summary> ///// <param name="index">TypeExpression of the index.</param> ///// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> ///// <param name="showErrorMessage">Indicates if an error message should be shown (used for dynamic types)</param> ///// <param name="fileName">File name.</param> ///// <param name="line">Line number.</param> ///// <param name="column">Column number.</param> ///// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Bracket(TypeExpression index, MethodType methodAnalyzed, bool showErrorMessage, Location loc) { // if (this.fieldType != null) // return this.fieldType.Bracket(index, methodAnalyzed, showErrorMessage, loc); // return null; //} //#endregion #region Assignment() ANULADA /// <summary> /// Check if the type can make an assignment operation. /// </summary> /// <param name="operand">WriteType expression of the operand of binary expression.</param> /// <param name="op">Operator.</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="actualImplicitObject">Only suitable when the assignment is executed as a constraint of a method call. In that case, /// this parameter represents the actual object used to pass the message; null otherwise.</param> /// <param name="location"> Location of the element</param> /// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification, // TypeExpression actualImplicitObject, Location location) { // // * We check if a constraint must be generated. Is it an assignment of the implicit object's field? // bool found = false; // // * In case it has free variables and the reference used is this, we add a constraint to the method // if (this.HasTypeVariables() && methodAnalyzed != null && ClassType.IsConcreteType(actualImplicitObject) == null) { // // * They should be the same exact (sub)classes. This represent the same instance, not another instance of the same class. // ClassType methodSuperClass = (ClassType)methodAnalyzed.MemberInfo.Class; // while (!(found = (this.MemberInfo.Class == methodSuperClass)) && methodSuperClass != null) // methodSuperClass = methodSuperClass.BaseClass; // if (found) { // // * An assignment constraint is added, postponing the type inference // // * If an actual implicit object is used, we take its field's type // FieldType fieldType = this; // ClassType thisType = TypeExpression.As<ClassType>(actualImplicitObject); // if (thisType == null) { // FieldType field = TypeExpression.As<FieldType>(actualImplicitObject); // if (field != null) // thisType = TypeExpression.As<ClassType>(field.FieldTypeExpression); // } // if (thisType != null) // fieldType = (FieldType)thisType.Fields[this.MemberInfo.MemberIdentifier].WriteType; // methodAnalyzed.AddConstraint(new FieldTypeAssignmentConstraint(fieldType, operand, unification)); // methodAnalyzed.ValidTypeExpression = false; // return this.fieldType; // } // } // if (!found && this.fieldType != null) // return this.fieldType.Assignment(operand, op, null, unification, actualImplicitObject, location); // return null; //} #endregion #region Arithmetic() >NUL>C> /* * /// <summary> * /// Check if the type can make an arithmetic operation. * /// </summary> * /// <param name="operand">WriteType expression of the operand of binary expression.</param> * /// <param name="op">Operator.</param> * /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> * /// <param name="showErrorMessage">Indicates if an error message should be shown (used for dynamic types)</param> * /// <param name="fileName">File name.</param> * /// <param name="line">Line number.</param> * /// <param name="column">Column number.</param> * /// <returns>WriteType obtained with the operation.</returns> * public override TypeExpression Arithmetic(TypeExpression operand, Enum op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) { * if (this.fieldType != null) * return this.fieldType.Arithmetic(operand, op, methodAnalyzed, showErrorMessage, loc); * return null; * } * * /// <summary> * /// Check if the type can make an arithmetic operation. * /// </summary> * /// <param name="op">Operator.</param> * /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> * /// <param name="showErrorMessage">Indicates if an error message should be shown (used for dynamic types)</param> * /// <param name="fileName">File name.</param> * /// <param name="line">Line number.</param> * /// <param name="column">Column number.</param> * /// <returns>WriteType obtained with the operation.</returns> * public override TypeExpression Arithmetic(UnaryOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) { * return this.fieldType.Arithmetic(op, methodAnalyzed, showErrorMessage, loc); * } */ #endregion #region Relational()ANULADA /* /// <summary> * /// Check if the type can make an relational operation. * /// </summary> * /// <param name="operand">WriteType expression of the operand of binary expression.</param> * /// <param name="op">Operator.</param> * /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> * /// <param name="showErrorMessage">Indicates if an error message should be shown (used for dynamic types)</param> * /// <param name="fileName">File name.</param> * /// <param name="line">Line number.</param> * /// <param name="column">Column number.</param> * /// <returns>WriteType obtained with the operation.</returns> * public override TypeExpression Relational(TypeExpression operand, RelationalOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) { * if (this.fieldType != null) * return this.fieldType.Relational(operand, op, methodAnalyzed, showErrorMessage, loc); * return null; * } * */ #endregion #region PromotionLevl()--># /// <summary> /// Returns a value that indicates a promotion level. /// </summary> /// <param name="type">WriteType to promotion.</param> /// <returns>Returns a promotion value.</returns> //public override int PromotionLevel(TypeExpression type) { // if (this.fieldType != null) // return this.fieldType.PromotionLevel(type); // return -1; //} /// <summary> #endregion #region Promotion ANULADA /// Requires the implicit object to be a subtype of the type parameter /// </summary> /// <param name="type">WriteType to promotion.</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="op">An optional operator to report error messages.</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> ///// <returns>The supertype; null if there has been some error.</returns> //public override TypeExpression Promotion(TypeExpression type, Enum op, MethodType methodAnalyzed, Location loc) { // if (this.fieldType != null) // return this.fieldType.Promotion(type, op, methodAnalyzed, loc); // return null; //} #endregion #region Cast() ANULADA /// <summary> /// Tells if the type can be cast to the casttype /// </summary> /// <param name="castType">The expected type</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> ///// <returns>The returned type expression</returns> //public override TypeExpression Cast(TypeExpression castType, MethodType methodAnalyzed, Location loc) { // if (this.fieldType != null) // return this.fieldType.Cast(castType, methodAnalyzed, loc); // return null; //} #endregion // WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { FieldType ft = te as FieldType; if (ft != null) { bool success = this.fieldType.Unify(ft.fieldType, unification, previouslyUnified); if (success) // * Dynamic type { DynVarOptions.Instance.AssignDynamism(this, ft.isDynamic); } // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; return(success); } if (te is TypeVariable && unification != SortOfUnification.Incremental) { // * No incremental unification is commutative return(te.Unify(this, unification, previouslyUnified)); } return(false); }
// WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { // * Infinite recursion detection Pair <TypeExpression, TypeExpression> pair = new Pair <TypeExpression, TypeExpression>(this, te); if (previouslyUnified.Contains(pair)) { return(true); } previouslyUnified.Add(pair); // * First checks that all the expression are equivalent. Otherwise substitutions could be // partially applied to type variables that should finally not be unified bool equivalent = true; foreach (TypeExpression type in this.typeSet) { if (!(equivalent = (bool)type.AcceptOperation(new EquivalentOperation(te), null))) { break; } } if (!equivalent) { return(false); } // * Lets unify, incrementing type variables with union types foreach (TypeExpression type in this.typeSet) { // * Unification of union types is incremental te.Unify(type, SortOfUnification.Incremental, previouslyUnified); } // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; return(true); }
// WriteType Promotion #region PromotionLevel() ANULADA //public virtual int PromotionLevel(TypeExpression type) { // return -1; //} #endregion #region Promotion() ANULADA /// <summary> /// Requires the implicit object to be a subtype of the type parameter /// </summary> /// <param name="type">WriteType to promotion.</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="op">An optional operator to report error messages.</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> /// <returns>The supertype; null if there has been some error.</returns> //public virtual TypeExpression Promotion(TypeExpression type, MethodType methodAnalyzed, Location location) { // if ((int) this.AcceptOperation(new PromotionLevelOperation(type)) == -1) { // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, type.FullName, location)); // return null; // } // return type; //} //public virtual TypeExpression Promotion(TypeExpression type, Enum op, MethodType methodAnalyzed, Location location) { // if ((int)this.AcceptOperation( new PromotionLevelOperation(type)) == -1) { // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, type.FullName, op.ToString(), location)); // return null; // } // return type; //} #endregion #region Cast() ANULADA /// <summary> /// Tells if the type can be cast to the casttype /// </summary> /// <param name="castType">The expected type</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> /// <returns>The returned type expression</returns> //public virtual TypeExpression Cast(TypeExpression castType, MethodType methodAnalyzed, Location loc) { // if (castType == null) // return null; // if (((int)castType.AcceptOperation(new PromotionLevelOperation(this)) != -1) || ((int)this.AcceptOperation(new PromotionLevelOperation(castType)) != -1)) // return castType; // ErrorManager.Instance.NotifyError(new TypeCastError(this.FullName, castType.FullName, loc)); // return null; //} #endregion #region EqualsForOverload() ANULADA /// <summary> /// Used to not repeat methods in overload /// </summary> /// <param name="typeExpression">The other type expression</param> /// <returns>If both represent the same type</returns> //public virtual bool EqualsForOverload(object typeExpression) { // // * By default, we use the equals comparison // return this.Equals(typeExpression); //} #endregion // WriteType Unification #region Unify() /// <summary> /// Tries to unify the type expression of this and the parameter /// </summary> /// <param name="te">The type expression to be unified together with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If both type expressionas could be unfied</returns> public abstract bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified);
// WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { return(this.RealType.Unify(te, unification, previouslyUnified)); }
/// <summary> /// Costructor of the assignment operation. /// </summary> /// <param name="rightOperand">TypeExpression to be assigned to the stored firstOperand operand.</param> /// <param name="op">Kind o</param> /// <param name="methodAnalyzed">The Actual Method Being Analysed</param> /// <param name="unification">Kind of unification to use: Equivalent, Incremental and Override.</param> /// <param name="actualImplicitObject">The actual "this" objet we are visiting.</param> /// <param name="location">The location (file, line, column) of text being analyzed.</param> public AssignmentOperation(TypeExpression rightOperand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification, TypeExpression actualImplicitObject, Location location) { this.rightOperand = rightOperand; this.op = op; this.methodAnalyzed = methodAnalyzed; this.unification = unification; this.actualImplicitObject = actualImplicitObject; this.location = location; }
/// <summary> /// Tries to unify the constraints of a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">The location of the method call</param> /// <returns>If the unification has been satisfied</returns> public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location) { foreach (ConstraintList constraint in this.ConstraintLists) { TypeExpression result = constraint.Check(methodAnalyzed, actualImplicitObject, false, activeSortOfUnification, location); if (result != null) { return(result); } } if (showInvocationMessage) { ErrorManager.Instance.NotifyError(new ConstraintError(location)); } return(null); }
// Loop Detection // Helper Methods #region methodCall() /// <summary> /// This method does the type inference in a method call including unification. /// It requires that a) the method to be invoked has been previously analyzed with this visitor /// b) The formalMethod parameter is the result of the overload resolution /// </summary> /// <param name="actualImplicitObject">The actual implicit object</param> /// <param name="formalMethod">The formal method to be called</param> /// <param name="args">The ordered types of the actual parameters</param> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="fileName">File name.</param> /// <param name="line">Line number.</param> /// <param name="column">Column number.</param> /// <returns>The type expression of the returned value</returns> public static TypeExpression methodCall(TypeExpression actualImplicitObject, MethodType formalMethod, TypeExpression[] args, MethodType methodAnalyzed, SortOfUnification activeSortOfUnification, Location location) { UserType userType = formalMethod.MemberInfo.Class; MethodType actualMethod = null; // * We must create a new type with type variables for the object's attributes (the formal implicit object) IDictionary <TypeVariable, TypeVariable> typeVariableMappings = new Dictionary <TypeVariable, TypeVariable>(); // * If the method is an instance one and the actual object is not this, we create a new implicit object to unify if (!formalMethod.MemberInfo.hasModifier(Modifier.Static) && actualImplicitObject != null && actualImplicitObject != methodAnalyzed.memberInfo.Class) { // * Unifies the implicit objects (actual and formal) UserType formalImplicitObject = (UserType)userType.CloneType(typeVariableMappings); if (!actualImplicitObject.Unify(formalImplicitObject, SortOfUnification.Equivalent, new List <Pair <TypeExpression, TypeExpression> >())) { // * If the formal implicit object already has substitution (fields declararion with assignments), we override it with a union type formalImplicitObject.Unify(actualImplicitObject, SortOfUnification.Override, new List <Pair <TypeExpression, TypeExpression> >()); } actualImplicitObject.ValidTypeExpression = false; } // * If "this" is the actual implicit object, the return type is the original return type of the method TypeExpression originalReturnType = formalMethod.Return; if (formalMethod.HasTypeVariables() || formalMethod.Constraints.Count > 0) { // * We must also generate a method with fresh variables (formal method) // when it has parameters with type variables or constraints formalMethod = formalMethod.CloneMethodType(typeVariableMappings); } // * If the method has type variables... if (formalMethod.HasTypeVariables()) { // * We create the actual method: // 1.- The actual return type TypeVariable actualReturnType = TypeVariable.NewTypeVariable; // 2.- The actual method actualMethod = new MethodType(actualReturnType); // 3.- The actual parameters foreach (TypeExpression arg in args) { actualMethod.AddParameter(arg); } // * Unifies both methods if (!actualMethod.Unify(formalMethod, SortOfUnification.Equivalent, new List <Pair <TypeExpression, TypeExpression> >())) { ErrorManager.Instance.NotifyError(new UnificationError(actualMethod.FullName, location)); return(null); } } // * Otherwise, arguments promotion must be checked else { if (args.Length != formalMethod.ParameterListCount) { ErrorManager.Instance.NotifyError(new ArgumentNumberError(formalMethod.FullName, args.Length, location)); return(null); } for (int i = 0; i < args.Length; i++) { if (args[i].AcceptOperation(PromotionOperation.Create(formalMethod.paramList[i], methodAnalyzed, location), null) == null) { return(null); } } } // * Method constraints satisfaction if (formalMethod.Constraints.Count > 0) { formalMethod.Constraints.Check(methodAnalyzed, actualImplicitObject, true, activeSortOfUnification, location); } // * The returned type is the the actual method if there has been a unification and // in case the method is a instance method, a concrete object has been used (not this) or // a different implicit object (the this reference is changed in the SSA algorithm) if (actualMethod != null && (formalMethod.MemberInfo.hasModifier(Modifier.Static) || ClassType.IsConcreteType(actualImplicitObject) != null || actualImplicitObject != formalMethod.MemberInfo.Class)) { TypeVariable returnType = (TypeVariable)actualMethod.Return; if (returnType.Substitution != null) { return(returnType.EquivalenceClass.Substitution); } return(returnType); } // * The original returned type if there has been no unification or the implicit object is "this" return(originalReturnType); }
/// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { return(false); }
/// <summary> /// To add a new type to the class equivalence. /// </summary> /// <param name="te">The type to be added</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the substitution has been correctly applied</returns> public bool add(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { TypeVariable tv = te as TypeVariable; if (tv != null) // * Another type variable // * Tries to add its substitution { if (tv.Substitution != null) // * If it has a substitution { if (!this.add(tv.EquivalenceClass.Substitution, unification, previouslyUnified)) // * We try to add it to ourselves { return(false); // * Both susbstitutions are not the same } } // * If no error, we add it to the equivalence class this.typeVariables[tv.Variable] = tv; if (tv.EquivalenceClass != null) // * The parameter already has a equivalence class { foreach (KeyValuePair <int, TypeVariable> pair in tv.EquivalenceClass.TypeVariables) { if (!this.typeVariables.ContainsKey(pair.Key)) { // * We recursively add all the element of the equivalence class this.typeVariables[pair.Key] = pair.Value; this.add(pair.Value, unification, previouslyUnified); } } } // * Finally, we update the equivalence class of tv tv.EquivalenceClass = this; return(true); } // * te is not a type variable if (this.substitution != null) { // * A substitution already exists if (unification == SortOfUnification.Equivalent) { // * They must be equivalent if (!(bool)this.substitution.AcceptOperation(new EquivalentOperation(te), null)) { return(false); } if (te.HasTypeVariables()) { // var1=Array(int) must be unified to Array(var1) return(te.Unify(this.substitution, unification, previouslyUnified)); } return(true); } if (unification == SortOfUnification.Incremental) { // * The incremental behaviour implies a union of all the types this.substitution = UnionType.collect(this.substitution, te); return(true); } // * Override unification (the susbstitution is overridden) this.substitution = te; return(true); } // * We set the type as a susbstitution substitution = te; return(true); }
/// <summary> ///// Check if the type can make an assignment operation. ///// </summary> ///// <param name="operand">WriteType expression of the operand of binary expression.</param> ///// <param name="op">Operator.</param> ///// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> ///// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> ///// <param name="actualImplicitObject">Only suitable when the assignment is executed as a constraint of a method call. In that case, ///// this parameter represents the actual object used to pass the message; null otherwise.</param> ///// <param name="fileName">File name.</param> ///// <param name="line">Line number.</param> ///// <param name="column">Column number.</param> ///// <returns>WriteType obtained with the operation.</returns> //public override TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification, // TypeExpression actualImplicitObject, Location location) { // if (op == AssignmentOperator.Assign) // return operand; // else // ErrorManager.Instance.NotifyError(new AssignmentError(operand.FullName, this.FullName, location)); // return null; //} #endregion #region Arithmetic() ANULADA /* * /// <summary> * /// Check if the type can make an arithmetic operation. * /// </summary> * /// <param name="operand">WriteType expression of the operand of binary expression.</param> * /// <param name="op">Operator.</param> * /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> * /// <param name="showErrorMessage">Indicates if an error message should be shown (used for dynamic types)</param> * /// <param name="fileName">File name.</param> * /// <param name="line">Line number.</param> * /// <param name="column">Column number.</param> * /// <returns>WriteType obtained with the operation.</returns> * public override TypeExpression Arithmetic(TypeExpression operand, Enum op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) { * if (op.Equals(ArithmeticOperator.Plus) && operand.Equivalent(StringType.Instance)) * return StringType.Instance; * if (showErrorMessage) * ErrorManager.Instance.NotifyError(new TypePromotionError(operand.FullName, this.FullName, op.ToString(), loc)); * return null; * } */ #endregion // WriteType Promotion #region PromotionLevel() ANULADA /// <summary> /// Returns a value that indicates a promotion level. /// </summary> /// <param name="type">WriteType to promotion.</param> /// <returns>Returns a promotion value.</returns> //public override int PromotionLevel(TypeExpression type) { // // * Built-in types: no promotion, except string // if (type is BoolType || type is CharType || type is DoubleType || type is IntType || type is VoidType) // return -1; // // * BCL Value Types (structs): No promotion // BCLClassType bclClass = TypeExpression.As<BCLClassType>(type); // if (bclClass != null) { // if (bclClass.TypeInfo.IsValueType) // return -1; // // * Correct promotion to classes that are not value types // return 0; // } // // * WriteType variable // TypeVariable typeVariable = type as TypeVariable; // if (typeVariable != null) { // if (typeVariable.Substitution != null) // // * If the variable is bounded, the promotion is the one of its substitution // return this.PromotionLevel(typeVariable.EquivalenceClass.Substitution); // // * A free variable is complete promotion // return 0; // } // // * Union type // UnionType unionType = TypeExpression.As<UnionType>(type); // if (unionType != null) // return unionType.SuperType(this); // // * Field type and bounded type variable // FieldType fieldType = TypeExpression.As<FieldType>(type); // if (fieldType != null) // return this.PromotionLevel(fieldType.FieldTypeExpression); // // * Correct Promotion // return 0; //} #endregion // WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="te">The expression to be unfied with this</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { throw new NotImplementedException("NullType.Unify() Not implemented"); }
/// <summary> /// Checks the satisfaction of a constraint in a method call /// </summary> /// <param name="methodAnalyzed">The method that is being analyzed when the operation is performed.</param> /// <param name="actualImplicitObject">Only suitable in an assignment constraint. It represents the actual object used to pass the message.</param> /// <param name="showInvocationMessage">To show the invocation line and column in case an error exists</param> /// <param name="activeSortOfUnification">The active sort of unification used (Equivalent is the default /// one and Incremental is used in the SSA bodies of the while, for and do statements)</param> /// <param name="location">The location where the method is called</param> /// <returns>The return type expression</returns> public abstract TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage, SortOfUnification activeSortOfUnification, Location location);
// WriteType Promotion #region PromotionLevel() /// <summary> /// Returns a value thdat indicates a promotion level. /// </summary> /// <param name="type">WriteType to promotion.</param> /// <returns>Returns a promotion value.</returns> //public override int PromotionLevel(TypeExpression type) { // int aux, less = -1; // // * The same type // if (this == type) // return 0; // // * Equivalent types // if ((bool)this.AcceptOperation(new EquivalentOperation(type))) // return 0; // // * Field type and bounded type variable // FieldType fieldType = TypeExpression.As<FieldType>(type); // if (fieldType != null) // return this.PromotionLevel(fieldType.FieldTypeExpression); // // * WriteType variable // TypeVariable typeVariable = type as TypeVariable; // if (typeVariable != null) { // if (typeVariable.Substitution != null) // // * If the variable is bounded, the promotion is the one of its substitution // return this.PromotionLevel(typeVariable.EquivalenceClass.Substitution); // // * A free variable is complete promotion // return 0; // } // // * Inheritance // if (this.BaseClass == null) // // * Object only promotes to object // return -1; // if ((bool)this.baseClass.AcceptOperation(new EquivalentOperation(type))) // return 1; // else { // aux = this.baseClass.PromotionLevel(type); // if (aux != -1) // return aux + 1; // } // // * Interfaces // if (this.interfaceList.Count != 0) { // for (int i = 0; i < this.interfaceList.Count; i++) { // if ((bool)this.interfaceList[i].AcceptOperation( new EquivalentOperation(type))) { // if ((less > 1) || (less == -1)) // less = 1; // } // else { // aux = this.interfaceList[i].PromotionLevel(type); // if (aux != -1) { // if ((less > (aux + 1)) || (less == -1)) // less = aux + 1; // } // } // } // } // if (less != -1) // return less; // // * Union type // UnionType unionType = TypeExpression.As<UnionType>(type); // if (unionType != null) // return unionType.SuperType(this); // // * No promotion // return -1; //} #endregion // WriteType Unification #region Unify /// <summary> /// This method unifies two type expressions (this and te) /// </summary> /// <param name="te">The expression to be unfied with this</param> /// <param name="unification">Indicates if the kind of unification (equivalent, incremental or override).</param> /// <param name="previouslyUnified">To detect infinite loops. The previously unified pairs of type expressions.</param> /// <returns>If the unification was successful</returns> public override bool Unify(TypeExpression te, SortOfUnification unification, IList <Pair <TypeExpression, TypeExpression> > previouslyUnified) { // * Infinite recursion detection Pair <TypeExpression, TypeExpression> pair = new Pair <TypeExpression, TypeExpression>(this, te); if (previouslyUnified.Contains(pair)) { return(true); } previouslyUnified.Add(pair); ClassType ct = te as ClassType; bool success = true; // * Class WriteType if (ct != null) { // * Inheritance is taken into account if ((int)ct.AcceptOperation(new PromotionLevelOperation(this), null) == -1) { return(false); } // * Walk upward in the tree till find the correct class while (!(bool)ct.AcceptOperation(new EquivalentOperation(this), null)) { ct = ct.baseClass; } // * Now we unify the fields foreach (string key in this.Fields.Keys) { FieldType thisField = (FieldType)this.Fields[key].Type, teField = (FieldType)ct.Fields[key].Type; if (thisField.FieldTypeExpression is ClassTypeProxy || teField.FieldTypeExpression is ClassTypeProxy) { success = thisField.FieldTypeExpression.FullName.Equals(teField.FieldTypeExpression.FullName); } else if (!(thisField.Unify(teField, unification, previouslyUnified))) { success = false; } if (!success) { break; } } if (success && this.baseClass != null) { // * The same with the base class this.baseClass.Unify(ct.baseClass, unification, previouslyUnified); } // * If one of the classes is a concrete type, so it is the other if (success) { this.ConcreteType = ct.ConcreteType = this.ConcreteType || ct.ConcreteType; } } // * WriteType variable else if (te is TypeVariable) { TypeVariable typeVariable = (TypeVariable)te; if (unification != SortOfUnification.Incremental) { // * Incremental is commutative success = typeVariable.Unify(this, unification, previouslyUnified); } // * Incremental unification (not commutative) else if (typeVariable.Substitution != null) { // * Class(var) should unify to Var=Class(int) success = this.Unify(typeVariable.Substitution, unification, previouslyUnified); } else { success = false; } } // * Union WriteType else if (te is UnionType) { success = te.Unify(this, unification, previouslyUnified); } // * Class WriteType Proxy else if (te is ClassTypeProxy) { success = this.Unify(((ClassTypeProxy)te).RealType, unification, previouslyUnified); } else if (te is FieldType) { success = this.Unify(((FieldType)te).FieldTypeExpression, unification, previouslyUnified); } else { success = false; } // * Clears the type expression cache this.ValidTypeExpression = false; te.ValidTypeExpression = false; return(success); }
/// <summary> /// Constructor for method call constraints /// </summary> /// <param name="firstOperand">WriteType of the first operand</param> /// <param name="actualImplicitObject">The actual implicit object used to pass the message</param> /// <param name="parameters">The set of actual parameters</param> /// <param name="sortOfUnification">The kind of unification used in the method call</param> /// <param name="location">The original location where the constraint has been generated</param> public ParenthesisConstraint(TypeExpression firstOperand, TypeExpression actualImplicitObject, TypeExpression[] parameters, SortOfUnification sortOfUnification, Location location) : base(firstOperand, location) { this.actualImplicitObject = actualImplicitObject; this.parameters = parameters; this.sortOfUnification = sortOfUnification; }
public ParenthesisOperation(TypeExpression actualImplicitObject, TypeExpression[] arguments, MethodType methodAnalyzed, SortOfUnification activeSortOfUnification, Location location) { this.actualImplicitObject = actualImplicitObject; this.arguments = arguments; this.methodAnalyzed = methodAnalyzed; this.activeSortOfUnification = activeSortOfUnification; this.location = location; }