The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
ParenthesisConstraint.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: ParenthesisConstraint.cs
6 // Author: Francisco Ortin - francisco.ortin@gmail.com
7 // Description:
8 // Method invocation constraint to be executed each time the associated method
9 // has been called.
10 // It represents constraints of the form:
11 // ret := op1(implicitObj, param*)
12 // where op1 is a method, implicitObj is the actual object used to pass the
13 // message and param* is a sequence of parameters
14 // Implements Composite pattern [Leaf].
15 // Implements Command pattern [Concrete Command].
16 // Implements Memento pattern [Memento].
17 // --------------------------------------------------------------------------
18 // Create date: 26-04-2007
19 // Modification date: 26-04-2007
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Text;
25 
26 using AST;
27 using ErrorManagement;
28 using Tools;
29 using TypeSystem.Operations;
30 
31 namespace TypeSystem.Constraints {
39 
40  #region Fields
41  private TypeExpression actualImplicitObject;
45 
49  private TypeExpression[] parameters;
50 
54  private SortOfUnification sortOfUnification;
55  #endregion
56 
57  #region Properties
58  public TypeExpression ActualImplicitObject {
62  get { return this.actualImplicitObject; }
63  }
64 
68  public TypeExpression[] Parameters {
69  get { return this.parameters; }
70  }
71  #endregion
72 
73 
74  #region Constructor
75  public ParenthesisConstraint(TypeExpression firstOperand, TypeExpression actualImplicitObject, TypeExpression[] parameters, SortOfUnification sortOfUnification, Location location)
84  : base(firstOperand, location) {
85  this.actualImplicitObject = actualImplicitObject;
86  this.parameters = parameters;
87  this.sortOfUnification = sortOfUnification;
88  }
98  private ParenthesisConstraint(TypeExpression firstOperand, TypeExpression actualImplicitObject, TypeExpression[] parameters,
99  TypeVariable returnType, SortOfUnification sortOfUnification, Location location)
100  : base(firstOperand, returnType, location) {
101  this.actualImplicitObject = actualImplicitObject;
102  this.parameters = parameters;
103  this.sortOfUnification = sortOfUnification;
104  }
105  #endregion
106 
107  #region BuildTypeExpressionString()
108  protected override string BuildTypeExpressionString() {
109  StringBuilder sb = new StringBuilder();
110  sb.AppendFormat("{0}:={1}({2}", this.ReturnType.FullName, this.FirstOperand.FullName, this.ActualImplicitObject.FullName);
111  foreach (TypeExpression parameter in this.Parameters)
112  sb.AppendFormat(",{0}", parameter.FullName);
113  sb.Append(")");
114  return sb.ToString();
115  }
116  #endregion
117 
118  #region CloneTypeVariables()
119  public override Constraint CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses) {
128  TypeExpression newFirstOperand = this.FirstOperand.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
129  TypeExpression newActualImplicitObject = null;
130  if (this.ActualImplicitObject != null)
131  newActualImplicitObject = this.ActualImplicitObject.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
132  TypeVariable newReturnType = (TypeVariable)this.ReturnType.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
133  TypeExpression[] newParameters = new TypeExpression[this.parameters.Length];
134  for (int i = 0; i < this.parameters.Length; i++)
135  newParameters[i] = this.parameters[i].CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
136  return new ParenthesisConstraint(newFirstOperand, newActualImplicitObject, newParameters, newReturnType, this.sortOfUnification, this.Location);
137  }
138  #endregion
139 
140  #region Check()
141  public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage,
152  SortOfUnification activeSortOfUnification, Location location) {
153  // * If the actual implicit object is a field, we take its field's type
154  FieldType implicitObjectAsField = TypeExpression.As<FieldType>(this.actualImplicitObject);
155  ClassType implicitObjectAsClass;
156  TypeExpression implicitObject=this.ActualImplicitObject;
157  if (implicitObjectAsField != null) {
158  implicitObject = implicitObjectAsField.FieldTypeExpression;
159  implicitObjectAsClass = TypeExpression.As<ClassType>(implicitObjectAsField.FieldTypeExpression);
160  }
161  else
162  implicitObjectAsClass = TypeExpression.As<ClassType>(this.actualImplicitObject);
163  // * If the actual implicit object is concrete, so it is the saved implicit object
164  if (ClassType.IsConcreteType(actualImplicitObject) != null && implicitObjectAsClass != null)
165  implicitObjectAsClass.ConcreteType = true;
166 
167  // * If the unification in the method call is incremental, this is the one to be used
168  SortOfUnification unification = activeSortOfUnification == SortOfUnification.Incremental ? SortOfUnification.Incremental : this.sortOfUnification;
169  // * Checks the parenthesis operation
170  TypeExpression result = (TypeExpression)this.FirstOperand.AcceptOperation(new ParenthesisOperation(implicitObject, this.Parameters, methodAnalyzed, unification, this.Location), null);
171  if (result == null && showInvocationMessage) {
172  ErrorManager.Instance.NotifyError(new ConstraintError(location));
173  return null;
174  }
175  // * If no error exists, we unify the return type variable with the actual result
176  this.ReturnType.Unify(result, SortOfUnification.Equivalent, new List<Pair<TypeExpression, TypeExpression>>());
177  this.ReturnType.ValidTypeExpression = this.ValidTypeExpression = false;
178  return this.ReturnType;
179  }
180  #endregion
181 
182 
183  }
184 }
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...
Represents a error produced when a constraint has not been satisfied
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.
Definition: Location.cs:24
It represents constraints of the form: ret := op1(implicitObj, param*) where op1 is a method...
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
bool ConcreteType
Indicates if the class holds a concrete type. Opposite to an abstract type, a concrete type holds the...
Definition: ClassType.cs:64
Implements Double Dispatch Pattern Role:
Representa a method type.
Definition: MethodType.cs:37
Represents a class type.
Definition: ClassType.cs:35
Representa a field type.
Definition: FieldType.cs:37