The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
IntersectionType.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: IntersectionType.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Description: //
9 // Represents a disjunction set type. //
10 // Inheritance: TypeExpression. //
11 // Implements Composite pattern [Composite]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 27-02-2007 //
14 // Modification date: 21-03-2007 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Reflection;
20 using System.Text;
21 using System.Text.RegularExpressions;
22 
23 using AST;
24 using ErrorManagement;
25 using Tools;
26 using TypeSystem.Operations;
27 
28 namespace TypeSystem {
37  //VISTO
39  #region Fields
40 
44  protected List<TypeExpression> typeSet = new List<TypeExpression>();
45 
46  #endregion
47 
48  #region Properties
49 
53  public int Count {
54  get { return this.typeSet.Count; }
55  }
56 
60  public IList<TypeExpression> TypeSet {
61  get { return this.typeSet; }
62  }
63  #endregion
64 
65  #region Constructor
66 
70  public IntersectionType() { }
72  this.AddType(te);
73  this.BuildFullName();
74  }
75  #endregion
76 
77  #region Dispatcher
78  public override object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
79  #endregion
80 
81  #region AddType
82  public bool AddType(TypeExpression type) {
88  bool added = true;
89  IntersectionType intersection = TypeExpression.As<IntersectionType>(type);
90  if (intersection != null) {
91  foreach (TypeExpression t in intersection.typeSet)
92  added = added && this.AddType(t);
93  this.ValidTypeExpression = false;
94  return added;
95  }
96  Predicate<TypeExpression> predicate = delegate(TypeExpression te2) {
97  return (bool)te2.AcceptOperation(new EquivalentOperation(type), null);
98  };
99  if (this.typeSet.Find(predicate)== null) {
100  this.typeSet.Add(type);
101  this.ValidTypeExpression = false;
102  return true;
103  }
104  return false;
105  }
106  #endregion
107 
108  #region ToString()
109 
114  public override string BuildTypeExpressionString(int depthLevel) {
115  if (this.ValidTypeExpression) return this.typeExpression;
116  if (depthLevel <= 0) return this.FullName;
117 
118  StringBuilder aux = new StringBuilder();
119  aux.Append("/\\(");
120  for (int i = 0; i < this.typeSet.Count; i++) {
121  aux.AppendFormat("{0}", this.typeSet[i].BuildTypeExpressionString(depthLevel-1));
122  if (i < this.typeSet.Count - 1)
123  aux.AppendFormat(" ,");
124  }
125  aux.Append(")");
126  this.typeExpression = aux.ToString();
127  this.ValidTypeExpression = true;
128  return this.typeExpression;
129  }
130  #endregion
131 
132  #region BuildFullName()
133  public override void BuildFullName() {
137  StringBuilder aux = new StringBuilder();
138  aux.Append("/\\(");
139  for (int i = 0; i < this.typeSet.Count; i++) {
140  aux.AppendFormat("{0}", this.typeSet[i].FullName);
141  if (i < this.typeSet.Count - 1)
142  aux.AppendFormat(" ,");
143  }
144  aux.Append(")");
145  this.fullName = aux.ToString();
146  }
147  #endregion
148 
149  // WriteType inference
150 
151  #region overloadResolution()
152  public TypeExpression overloadResolution(TypeExpression[] arguments, Location location) {
161  int aux;
162  int min = -1, index = -1, minNumFreeVariables = Int32.MaxValue;
163 
164  // * We create a dictionary of <index,promotionValue> to remember the promotion values (they could have
165  // repeated values because of type variables)
166  Dictionary<int, int> promotionValues = new Dictionary<int, int>();
167 
168  if (this.typeSet.Count == 0) {
169  System.Diagnostics.Debug.Assert(false, "There should be no empty intersection types.");
170  return null;
171  }
172 
173  for (int i = 0; i < this.typeSet.Count; i++) {
174  MethodType mt = TypeExpression.As<MethodType>(this.typeSet[i]);
175  if (mt == null)
176  ErrorManager.Instance.NotifyError(new OperationNotAllowedError("()", mt.FullName, location));
177  aux = mt.Promotion(arguments, location);
178  if (aux != -1)
179  if ((min >= aux) || (min == -1)) {
180  min = aux;
181  index = i;
182  promotionValues[index] = min;
183  }
184  }
185  // * No method is suitable
186  if (index == -1) {
187  ErrorManager.Instance.NotifyError(new UnknownMemberError(location));
188  return null;
189  }
190  index = -1;
191  // * Gets the min number of free variables
192  foreach (KeyValuePair<int, int> pair in promotionValues)
193  if (pair.Value == min) {
194  aux = ((MethodType)this.typeSet[pair.Key]).GetNumberFreeVariables();
195  if (aux < minNumFreeVariables)
196  minNumFreeVariables = aux;
197  }
198  // * Assigns a union of all the best methods
199  TypeExpression bestMethods=null;
200  foreach (KeyValuePair<int, int> pair in promotionValues)
201  if (pair.Value == min && ((MethodType)this.typeSet[pair.Key]).GetNumberFreeVariables() == minNumFreeVariables)
202  bestMethods = UnionType.collect(bestMethods, this.typeSet[pair.Key]);
203  // * We've got'em
204  return bestMethods;
205  }
206  #endregion
207 
208  #region Parenthesis() ANULADA
209 
222  //public override TypeExpression Parenthesis(TypeExpression actualImplicitObject, TypeExpression[] arguments, MethodType methodAnalyzed,
223  // SortOfUnification activeSortOfUnification, Location loc) {
224  // TypeExpression method = overloadResolution(arguments, loc);
225  // if (method == null)
226  // return null;
227  // return method.Parenthesis(actualImplicitObject, arguments, methodAnalyzed, activeSortOfUnification, loc);
228  //}
229 
230  #endregion
231 
232  // WriteType Unification
233 
234  #region Unify
235  public override bool Unify(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified) {
243  // * Infinite recursion detection
244  Pair<TypeExpression,TypeExpression> pair = new Pair<TypeExpression, TypeExpression>(this, te);
245  if (previouslyUnified.Contains(pair))
246  return true;
247  previouslyUnified.Add(pair);
248 
249  // * Clears the type expression cache
250  this.ValidTypeExpression = false;
251  te.ValidTypeExpression = false;
252  // TODO
253  return false;
254  }
255  #endregion
256 
257 
258  #region HasTypeVariables()
259  public override bool HasTypeVariables() {
265  if (this.validHasTypeVariables)
266  return this.hasTypeVariablesCache;
267  bool toReturn = false;
268  foreach (TypeExpression type in this.typeSet)
269  if (type.HasTypeVariables()) {
270  toReturn = true;
271  break;
272  }
273  this.validHasTypeVariables = true;
274  return this.hasTypeVariablesCache = toReturn;
275  }
276  #endregion
277 
278  #region CloneTypeVariables()
279  public override TypeExpression CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses, IList<ClassType> clonedClasses) {
290  if (!this.HasTypeVariables())
291  return this;
292  IntersectionType newType = (IntersectionType)this.MemberwiseClone();
293  IList<TypeExpression> oldTypeSet = this.typeSet;
294  newType.typeSet = new List<TypeExpression>();
295  foreach (TypeExpression oldType in oldTypeSet)
296  newType.typeSet.Add(oldType.CloneTypeVariables(typeVariableMappings, equivalenceClasses, clonedClasses));
297  newType.ValidTypeExpression = false;
298  return newType;
299  }
300  #endregion
301 
302  // SSA
303 
304  #region Clone()
305  internal override TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
316  if (!this.HasTypeVariables())
317  return this;
318  IntersectionType newIntersectionType = (IntersectionType)this.MemberwiseClone();
319  newIntersectionType.typeSet = new List<TypeExpression>();
320  // * Clones all the types in the union
321  foreach (TypeExpression type in this.typeSet)
322  newIntersectionType.typeSet.Add(type.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed));
323  return newIntersectionType;
324  }
325  #endregion
326 
327  #region IsValueType()
328 
333  public override bool IsValueType()
334  {
335  throw new Exception("The method or operation is not implemented.");
336  }
337 
338  #endregion
339 
340 
341  }
342 }
TypeExpression overloadResolution(TypeExpression[] arguments, Location location)
A public method for being used as a global overload resolution process
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
Check if the type can make a method operation.
Represents a error produced when tries to make an operation not allowed for the specified type...
IList< TypeExpression > TypeSet
Gets the list of type expressions
override object AcceptOperation(TypeSystemOperation op, object arg)
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.
Definition: Location.cs:24
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
IntersectionType()
Constructor of Intersection
override string BuildTypeExpressionString(int depthLevel)
Returns the type expression like a string
Abstract class that represents all different types.
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
No location information is provided within this class, because the exec methods invoked by the proper...
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
IntersectionType(TypeExpression te)
bool AddType(TypeExpression type)
Adds a new type expression.
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 void BuildFullName()
Creates/Updates the full name of the type expression
List< TypeExpression > typeSet
Stores a set of type expression
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
int Count
Gets the number of type expressions
Representa an intersection type.
Represents a error produced when the attribute identifier is not defined.