The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
UnionType.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: UnionType.cs
6 // Authors: Francisco Ortin - francisco.ortin@gmail.com
7 // Description:
8 // Represents a disjunction set type.
9 // Inheritance: TypeExpression.
10 // Implements Composite pattern [Composite].
11 // --------------------------------------------------------------------------
12 // Create date: 05-04-2007
13 // Modification date: 05-04-2007
15 //visto
16 using System;
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Text;
20 using System.Text.RegularExpressions;
21 
22 using AST;
23 using ErrorManagement;
24 using Tools;
25 using TypeSystem.Constraints;
26 using TypeSystem.Operations;
27 
28 namespace TypeSystem {
36  public class UnionType : TypeExpression {
37 
38  #region Fields
39  private List<TypeExpression> typeSet = new List<TypeExpression>();
43  #endregion
44 
45  #region Properties
46 
50  public int Count {
51  get { return this.typeSet.Count; }
52  }
53 
57  public IList<TypeExpression> TypeSet {
58  get { return this.typeSet; }
59  }
60 
61  #endregion
62 
63  #region Constructor
64 
68  public UnionType() { }
70  this.AddType(te);
71  this.BuildFullName();
72  }
73  #endregion
74 
75  #region AddType
76  public bool AddType(TypeExpression type) {
82  bool added = true;
83  UnionType union = type as UnionType;
84  if (union != null) {
85  foreach (TypeExpression t in union.typeSet)
86  added = this.AddType(t) && added;
87  this.ValidTypeExpression = false;
88  return added;
89  }
90  TypeExpression existingType = typeSet.Find(type.Equals);
91  if (existingType == null) {
92  this.typeSet.Add(type);
93  this.ValidTypeExpression = false;
94  return true;
95  }
96  return false;
97  }
98  #endregion
99 
100  #region ToString()
101 
106  public override string BuildTypeExpressionString(int depthLevel) {
107  if (this.ValidTypeExpression) return this.typeExpression;
108  if (depthLevel <= 0) return this.FullName;
109 
110  StringBuilder aux = new StringBuilder();
111  aux.Append("\\/(");
112  for (int i = 0; i < this.typeSet.Count; i++) {
113  aux.AppendFormat("{0}", this.typeSet[i].BuildTypeExpressionString(depthLevel - 1));
114  if (i < this.typeSet.Count - 1)
115  aux.AppendFormat(" ,");
116  }
117  aux.Append(")");
118  this.typeExpression = aux.ToString();
119  this.ValidTypeExpression = true;
120  return this.typeExpression;
121  }
122  #endregion
123 
124  #region BuildFullName()
125  public override void BuildFullName() {
129  StringBuilder aux = new StringBuilder();
130  aux.Append("\\/(");
131  for (int i = 0; i < this.typeSet.Count; i++) {
132  aux.AppendFormat("{0}", this.typeSet[i].FullName);
133  if (i < this.typeSet.Count - 1)
134  aux.AppendFormat(" ,");
135  }
136  aux.Append(")");
137  this.fullName = aux.ToString();
138  }
139  #endregion
140 
141  // WriteType inference
142 
143  #region Dispatcher
144  public override object AcceptOperation(TypeSystemOperation op, object arg) { return op.Exec(this, arg); }
145  #endregion
146 
147  #region Assignment ANULADA
148  //public override TypeExpression Assignment(TypeExpression operand, AssignmentOperator op, MethodType methodAnalyzed, SortOfUnification unification, TypeExpression actualImplicitObject, Location location) {
162  // // * If the unification is incremental, we must add it to the union typeset
163  // if (unification == SortOfUnification.Incremental) {
164  // this.typeSet.Add(operand);
165  // return this;
166  // }
167  // foreach (TypeExpression type in this.typeSet)
168  // if (operand.PromotionLevel(type) == -1) {
169  // ErrorManager.Instance.NotifyError(new AssignmentError(operand.FullName, this.fullName, location));
170  // return null;
171  // }
172  // return this;
173  //}
174  #endregion
175 
176  #region Equivalent() ANULADA
177  //public override bool Equivalent(TypeExpression typeParam) {
183  // foreach (TypeExpression type in this.typeSet)
184  // if (!type.Equivalent(typeParam))
185  // return false;
186  // return true;
187  //}
188  #endregion
189 
190  #region Dot() ANULADA
191  //public override TypeExpression Dot(string field, MethodType methodAnalyzed, IList<TypeExpression> previousDot, Location loc) {
204  // // * Infinite loop detection
205  // if (previousDot.Contains(this))
206  // return new UnionType();
207  // previousDot.Add(this);
208 
209  // // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
210  // if (this.IsFreshVariable() && methodAnalyzed != null) {
211  // // * A constraint is added to the method analyzed
212  // DotConstraint constraint = new DotConstraint(this, field, loc);
213  // methodAnalyzed.AddConstraint(constraint);
214  // return constraint.ReturnType;
215  // }
216  // TypeExpression returnType = null;
217  // foreach (TypeExpression type in this.typeSet) {
218  // TypeExpression ret;
219  // if (!this.IsDynamic) {
220  // // * Static Behaviour: All the types must accept the attribute access
221  // ret = type.Dot(field, methodAnalyzed, previousDot, loc);
222  // if (ret == null) {
223  // return null;
224  // }
225  // }
226  // else
227  // // * Dynamic Behaviour: Only one type must accept the attribute access
228  // ret = type.Dot(field, previousDot);
229  // if (ret != null)
230  // returnType = UnionType.collect(returnType, ret);
231  // }
232  // // * If it is a dynamic union, one type must accept the attribute access
233  // if (returnType == null)
234  // ErrorManager.Instance.NotifyError(new NoTypeHasMember(this.FullName, field, loc));
235  // return returnType;
236  //}
245  //public override TypeExpression Dot(string memberName, IList<TypeExpression> previousDot) {
246  // // * Infinite loop detection
247  // if (previousDot.Contains(this))
248  // return null;
249  // previousDot.Add(this);
250 
251  // TypeExpression returnType = null;
252  // foreach (TypeExpression type in this.typeSet) {
253  // TypeExpression ret = type.Dot(memberName, previousDot);
254  // if (ret == null && !this.isDynamic)
255  // return null;
256  // if (ret != null)
257  // returnType = UnionType.collect(returnType, ret);
258  // }
259  // return returnType;
260  //}
261  #endregion
262 
263  #region Bracket() ANULADA
264  //public override TypeExpression Bracket(TypeExpression index, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
275  // // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
276  // if (this.IsFreshVariable() && methodAnalyzed != null) {
277  // // * A constraint is added to the method analyzed
278  // SquareBracketConstraint constraint = new SquareBracketConstraint(this, index, loc);
279  // methodAnalyzed.AddConstraint(constraint);
280  // return constraint.ReturnType;
281  // }
282  // TypeExpression returnType = null;
283  // foreach (TypeExpression type in this.typeSet) {
284  // TypeExpression ret = type.Bracket(index, methodAnalyzed, !this.IsDynamic && showErrorMessage, loc);
285  // if (ret == null && !this.IsDynamic)
286  // return null;
287  // if (ret != null)
288  // returnType = UnionType.collect(returnType, ret);
289  // }
290  // return returnType;
291  //}
292  #endregion
293 
294  #region Parenthesis() ANULADA
295  //public override TypeExpression Parenthesis(TypeExpression actualImplicitObject, TypeExpression[] arguments, MethodType methodAnalyzed,
308  // SortOfUnification activeSortOfUnification, Location loc) {
309  // // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
310  // if (this.IsFreshVariable() && methodAnalyzed != null) {
311  // // * A constraint is added to the method analyzed
312  // ParenthesisConstraint constraint = new ParenthesisConstraint(this, actualImplicitObject, arguments, activeSortOfUnification, loc);
313  // methodAnalyzed.AddConstraint(constraint);
314  // return constraint.ReturnType;
315  // }
316  // TypeExpression returnType = null;
317  // foreach (TypeExpression type in this.typeSet) {
318  // TypeExpression ret = type.Parenthesis(actualImplicitObject, arguments, methodAnalyzed, activeSortOfUnification, loc);
319  // if (ret == null && this.isDynamic)
320  // return null;
321  // returnType = UnionType.collect(returnType, ret);
322  // }
323  // return returnType;
324  //}
325  #endregion
326 
327  #region Arithmetic() ANULADA
328  /*
340  public override TypeExpression Arithmetic(TypeExpression operand, Enum op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
341  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
342  if (this.IsFreshVariable() && methodAnalyzed != null) {
343  // * A constraint is added to the method analyzed
344  ArithmeticConstraint constraint = new ArithmeticConstraint(this, operand, op, loc);
345  methodAnalyzed.AddConstraint(constraint);
346  return constraint.ReturnType;
347  }
348  TypeExpression returnType = null;
349  foreach (TypeExpression type in this.typeSet) {
350  TypeExpression ret = type.Arithmetic(operand, op, methodAnalyzed, !this.IsDynamic && showErrorMessage, loc);
351  if (ret == null && !this.IsDynamic)
352  return null;
353  if (ret != null)
354  returnType = UnionType.collect(returnType, ret);
355  }
356  // * If there has been some errors, they have not been shown because the type is dynamic, we show it
357  if (showErrorMessage && this.IsDynamic && returnType == null)
358  ErrorManager.Instance.NotifyError(new NoTypeAcceptsOperation(this.FullName, op.ToString(), operand.FullName, loc));
359  return returnType;
360  }
361  */
362  /* ANULADA
373  public override TypeExpression Arithmetic(UnaryOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
374  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
375  if (this.IsFreshVariable() && methodAnalyzed != null) {
376  // * A constraint is added to the method analyzed
377  ArithmeticConstraint constraint = new ArithmeticConstraint(this, op, loc);
378  methodAnalyzed.AddConstraint(constraint);
379  return constraint.ReturnType;
380  }
381  TypeExpression returnType = null;
382  foreach (TypeExpression type in this.typeSet) {
383  TypeExpression ret = type.Arithmetic(op, methodAnalyzed, !this.IsDynamic && showErrorMessage, loc);
384  if (ret == null && !this.IsDynamic)
385  return null;
386  if (ret != null)
387  returnType = UnionType.collect(returnType, ret);
388  }
389  // * If there has been some errors, they have not been shown because the type is dynamic, we show it
390  if (showErrorMessage && this.IsDynamic && returnType == null)
391  ErrorManager.Instance.NotifyError(new NoTypeAcceptsOperation(this.FullName, op.ToString(), loc));
392  return returnType;
393  } */
394  #endregion
395 
396  #region Relational()ANULADA
397  /*
409  public override TypeExpression Relational(TypeExpression operand, RelationalOperator op, MethodType methodAnalyzed, bool showErrorMessage, Location loc) {
410  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
411  if (this.IsFreshVariable() && methodAnalyzed != null) {
412  // * A constraint is added to the method analyzed
413  RelationalConstraint constraint = new RelationalConstraint(this, operand, op, loc);
414  methodAnalyzed.AddConstraint(constraint);
415  return constraint.ReturnType;
416  }
417  bool oneCorrectType = false;
418  foreach (TypeExpression type in this.typeSet) {
419  TypeExpression ret = type.Relational(operand, op, methodAnalyzed, !this.IsDynamic && showErrorMessage, loc);
420  if (ret == null && !this.IsDynamic)
421  return null;
422  if (ret != null)
423  oneCorrectType = true;
424  }
425  // * If there has been some errors, they have not been shown because the type is dynamic, we show it
426  if (showErrorMessage && this.IsDynamic && !oneCorrectType)
427  ErrorManager.Instance.NotifyError(new NoTypeAcceptsOperation(this.FullName, op.ToString(), operand.FullName, loc));
428  return BoolType.Instance;
429  }*/
430 
431  #endregion
432 
433  // WriteType Promotion
434 
435  #region PromotionLevel() ANULADA
436  //public override int PromotionLevel(TypeExpression superType) {
446  // // * Checks recursion
447  // if (recursivePromotionLevelDetection.Contains(this))
448  // return 0;
449  // recursivePromotionLevelDetection.Add(this);
450  // // * Static Behaviour: Takes the addition of all the promotion level
451  // // * Dynamic Behaviour: Takes the addition of all the promotion level
452  // int promotionLevel = this.IsDynamic ? Int32.MaxValue : 0;
453  // foreach (TypeExpression subType in this.typeSet) {
454  // int aux = subType.PromotionLevel(superType);
455  // if (this.IsDynamic && aux != -1) {
456  // // * Dynamic
457  // promotionLevel = Math.Min(promotionLevel, aux);
458  // if (promotionLevel == 0)
459  // break;
460  // }
461  // if (!this.IsDynamic) {
462  // // * Static
463  // if (aux == -1) {
464  // promotionLevel = -1;
465  // break;
466  // }
467  // promotionLevel += aux;
468  // }
469  // }
470  // if (this.IsDynamic && promotionLevel == Int32.MaxValue)
471  // // * No promotion at all
472  // promotionLevel = -1;
473  // // * Clears the loop detection for futures method calls
474  // recursivePromotionLevelDetection.Clear();
475  // return promotionLevel;
476  //}
478  //private static IList<UnionType> recursivePromotionLevelDetection = new List<UnionType>();
479  #endregion
480 
481  #region Promotion()
482  //public override TypeExpression Promotion(TypeExpression type, MethodType methodAnalyzed, Location loc) {
493  // return this.Promotion(type, AssignmentOperator.Assign, methodAnalyzed, loc);
494  //}
495  //public override TypeExpression Promotion(TypeExpression type, Enum op, MethodType methodAnalyzed, Location loc) {
496  // return this.InternalPromotion(type, op, methodAnalyzed, loc);
497  //}
498  //private TypeExpression InternalPromotion(TypeExpression superType, Enum op, MethodType methodAnalyzed, Location loc) {
499  // if (this.IsFreshVariable() && methodAnalyzed != null) {
500  // // * A constraint is added to the method analyzed
501  // PromotionConstraint constraint = new PromotionConstraint(this, superType, op, loc);
502  // methodAnalyzed.AddConstraint(constraint);
503  // return superType;
504  // }
505  // // * Static Behaviour: All the types in typeset must promote
506  // // * Dynamic Behaviour: One of the types in typeset must promote
507  // int aux = 0;
508  // UnionType dynamicUnionType = new UnionType();
509  // dynamicUnionType.isDynamic = true;
510  // foreach (TypeExpression subType in this.typeSet) {
511  // if (this.IsDynamic) {
512  // // * Dynamic
513  // if (subType.IsFreshVariable())
514  // dynamicUnionType.AddType(subType);
515  // else {
516  // aux = (int) subType.AcceptOperation(new PromotionLevelOperation(superType));
517  // if (aux != -1)
518  // return superType;
519  // }
520  // }
521  // if (!this.IsDynamic) {
522  // // * Static
523  // aux = (int)subType.AcceptOperation(new PromotionLevelOperation(superType));
524  // if (aux == -1) {
525  // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, superType.FullName, op.ToString(), loc));
526  // return null;
527  // }
528  // }
529  // }
530  // if (dynamicUnionType.Count != 0) {
531  // // * If the union type is dynamic and no type in the type set promotes, then we generate a constraint with one promotion grouping the fresh types in the type set
532  // PromotionConstraint constraint = new PromotionConstraint(dynamicUnionType, superType, op, loc);
533  // methodAnalyzed.AddConstraint(constraint);
534  // return superType;
535  // }
536  // if (this.IsDynamic && aux == -1) {
537  // // * No promotion at all
538  // ErrorManager.Instance.NotifyError(new TypePromotionError(this.FullName, superType.FullName, op.ToString(), loc));
539  // return null;
540  // }
541  // return superType;
542  //}
543  #endregion
544 
545  #region SuperType()
546  public int SuperType(TypeExpression subType) {
555  // * Takes the lower promotion level (except -1)
556  int lowerLevelOfPromotion = Int32.MaxValue;
557  foreach (TypeExpression superType in this.typeSet) {
558  int aux = (int) subType.AcceptOperation(new PromotionLevelOperation(superType), null);
559  if (aux != -1 && aux < lowerLevelOfPromotion)
560  lowerLevelOfPromotion = aux;
561  }
562  return lowerLevelOfPromotion == Int32.MaxValue ? -1 : lowerLevelOfPromotion;
563  }
564 
565  #endregion
566 
567  // WriteType Unification
568 
569  #region Unify
570  public override bool Unify(TypeExpression te, SortOfUnification unification, IList<Pair<TypeExpression, TypeExpression>> previouslyUnified) {
578  // * Infinite recursion detection
579  Pair<TypeExpression, TypeExpression> pair = new Pair<TypeExpression, TypeExpression>(this, te);
580  if (previouslyUnified.Contains(pair))
581  return true;
582  previouslyUnified.Add(pair);
583 
584  // * First checks that all the expression are equivalent. Otherwise substitutions could be
585  // partially applied to type variables that should finally not be unified
586  bool equivalent = true;
587  foreach (TypeExpression type in this.typeSet)
588  if (!(equivalent = (bool)type.AcceptOperation(new EquivalentOperation(te), null)))
589  break;
590  if (!equivalent)
591  return false;
592  // * Lets unify, incrementing type variables with union types
593  foreach (TypeExpression type in this.typeSet)
594  // * Unification of union types is incremental
595  te.Unify(type, SortOfUnification.Incremental, previouslyUnified);
596  // * Clears the type expression cache
597  this.ValidTypeExpression = false;
598  te.ValidTypeExpression = false;
599  return true;
600  }
601  #endregion
602 
603  #region HasTypeVariables()
604  public override bool HasTypeVariables() {
610  if (this.validHasTypeVariables)
611  return this.hasTypeVariablesCache;
612  bool toReturn = false;
613  foreach (TypeExpression type in this.typeSet)
614  if (type.HasTypeVariables()) {
615  toReturn = true;
616  break;
617  }
618  this.validHasTypeVariables = true;
619  return this.hasTypeVariablesCache = toReturn;
620  }
621  #endregion
622 
623  #region IsFreshVariable()
624 
630  public override bool IsFreshVariable() {
631  foreach (TypeExpression type in this.typeSet)
632  if (!type.IsFreshVariable())
633  return false;
634  return true;
635  }
636  #endregion
637 
638  #region CloneTypeVariables()
639  public override TypeExpression CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses, IList<ClassType> clonedClasses) {
650  if (!this.HasTypeVariables())
651  return this;
652  UnionType newType = (UnionType)this.MemberwiseClone();
653  IList<TypeExpression> oldTypeSet = this.typeSet;
654  newType.typeSet = new List<TypeExpression>();
655  foreach (TypeExpression oldType in oldTypeSet)
656  newType.typeSet.Add(oldType.CloneTypeVariables(typeVariableMappings, equivalenceClasses, clonedClasses));
657  newType.ValidTypeExpression = false;
658  return newType;
659  }
660  #endregion
661 
662  // Helper Methods
663 
664  #region collect()
665 
672  public static TypeExpression collect(TypeExpression collection, TypeExpression newType)
673  {
674  if (newType == null)
675  return collection;
676  if (collection == null)
677  return newType;
678  UnionType union = collection as UnionType;
679  if (collection != null)
680  union = new UnionType(collection);
681  if (newType is UnionType)
682  {
683  // both types are unions
684  union.AddType(newType);
685  return union;
686  }
687  if (union.AddType(newType))
688  return union;
689  // * If two elements are the same, retuns the first
690  return collection;
691  }
692 
693 
694  #endregion
695 
696  #region UpdateEquivalenceClass()
697  public override void UpdateEquivalenceClass(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<TypeExpression> previouslyUpdated) {
704  // * Checks infinite loops
705  if (previouslyUpdated.Contains(this))
706  return;
707  previouslyUpdated.Add(this);
708 
709  // * Updates the equivalence class
710  if (!this.HasTypeVariables())
711  return;
712  foreach (TypeExpression type in this.typeSet)
713  if (type.HasTypeVariables()) {
714  type.UpdateEquivalenceClass(typeVariableMappings, previouslyUpdated);
715  type.ValidTypeExpression = false;
716  }
717  this.ValidTypeExpression = false;
718  }
719  #endregion
720 
721  #region ReplaceTypeVariables()
722  public override void ReplaceTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings) {
728  if (!this.HasTypeVariables())
729  return;
730  for (int i = 0; i < this.typeSet.Count; i++) {
731  TypeVariable typeVariable = this.typeSet[i] as TypeVariable;
732  if (typeVariable == null) {
733  if (this.typeSet[i].HasTypeVariables())
734  this.typeSet[i].ReplaceTypeVariables(typeVariableMappings);
735  }
736  else if (typeVariableMappings.ContainsKey(typeVariable))
737  this.typeSet[i] = typeVariableMappings[typeVariable];
738  this.ValidTypeExpression = false;
739  }
740  }
741  #endregion
742 
743  #region Freeze()
744  public override TypeExpression Freeze() {
751  return this.Freeze(new List<TypeExpression>());
752  }
753 
754  public TypeExpression Freeze(IList<TypeExpression> evaluated)
755  {
756  if (!this.HasTypeVariables())
757  return this;
758  UnionType newUnionType = new UnionType();
759  foreach (TypeExpression type in this.TypeSet)
760  {
761  if(evaluated.Contains(type))
762  continue;
763  else
764  evaluated.Add(type);
765  if (type is TypeVariable)
766  newUnionType.AddType(((TypeVariable)type).Freeze(evaluated));
767  else
768  {
769  if (type is UnionType)
770  newUnionType.AddType(((UnionType) type).Freeze(evaluated));
771  else
772  newUnionType.AddType(type.Freeze());
773  }
774  }
775  return newUnionType;
776  }
777  #endregion
778 
779  // SSA
780 
781  #region Clone()
782  internal override TypeExpression Clone(IDictionary<int, TypeVariable> clonedTypeVariables, IList<EquivalenceClass> equivalenceClasses, MethodType methodAnalyzed) {
793  if (!this.HasTypeVariables())
794  return this;
795  UnionType newUnionType = (UnionType)this.MemberwiseClone();
796  newUnionType.typeSet = new List<TypeExpression>();
797  // * Clones all the types in the union
798  foreach (TypeExpression type in this.typeSet)
799  newUnionType.typeSet.Add(type.Clone(clonedTypeVariables, equivalenceClasses, methodAnalyzed));
800  return newUnionType;
801  }
802  #endregion
803 
804  // Loop Detection
805 
806  #region Remove()
807  public override bool Remove(TypeVariable toRemove) {
814  int i;
815  for (i = 0; i < this.typeSet.Count; i++) {
816  TypeVariable typeVariable = this.typeSet[i] as TypeVariable;
817  if (typeVariable != null && typeVariable.Variable == toRemove.Variable) {
818  this.typeSet.RemoveAt(i);
819  this.ValidTypeExpression = false;
820  break;
821  }
822  if (this.typeSet[i].Remove(toRemove)) {
823  this.typeSet[i].ValidTypeExpression = false;
824  break;
825  }
826  }
827  return i < this.typeSet.Count;
828  }
829  #endregion
830 
831  // Code Generation
832 
833  #region ILType()
834 
839  public override string ILType() {
840  return "object";
841  }
842 
843  #endregion
844 
845  #region IsValueType()
846 
851  public override bool IsValueType() {
852  return IsValueType(new List<TypeExpression>());
853  }
854 
855  public bool IsValueType(IList<TypeExpression> evaluated)
856  {
857  foreach (TypeExpression type in this.TypeSet)
858  {
859  if (evaluated.Contains(type))
860  continue;
861  else
862  evaluated.Add(type);
863  // * If Dynamic, a value type is enough);
864  if (this.IsDynamic)
865  {
866  if (type is UnionType)
867  if(((UnionType)type).IsValueType(evaluated))
868  return true;
869  if (type is TypeVariable)
870  if(((TypeVariable)type).IsValueType(evaluated))
871  return true;
872  if (type.IsValueType())
873  return true;
874  }
875  // * If Static, all types must be value types
876  else
877  {
878  if (type is UnionType)
879  {
880  if (!((UnionType) type).IsValueType(evaluated))
881  return false;
882  }
883  else if (type is TypeVariable)
884  {
885  if (!((TypeVariable) type).IsValueType(evaluated))
886  return false;
887  }
888  else if (!type.IsValueType())
889  return false;
890  }
891  }
892  if (this.IsDynamic)
893  // * None is value type
894  return false;
895  // * Static: All are value types
896  return true;
897  }
898 
899  #endregion
900 
901  #region HasFreshVariable()
902 
908  public override bool HasFreshVariable() {
909  foreach (TypeExpression type in this.typeSet)
910  if (type.IsFreshVariable())
911  return true;
912  return false;
913  }
914  #endregion
915 
916  #region HasIntersectionVariable()
917 
923  public override bool HasIntersectionVariable() {
924  foreach (TypeExpression type in this.typeSet)
925  if (TypeExpression.Is<IntersectionType>(type))
926  return true;
927  return false;
928  }
929  #endregion
930 
931  #region ContainsValueType()
932 
938  public bool ContainsValueType(TypeExpression type) {
939  for (int i = 0; i < this.typeSet.Count; i++) {
940  if (this.typeSet[i].IsValueType()) {
941  if (this.typeSet[i] == type)
942  return true;
943  }
944  }
945  return false;
946  }
947 
948  #endregion
949 
950  #region ContainsDifferentReturns()
951 
956  public bool ContainsDifferentReturns() {
957  TypeExpression current = null;
958  TypeExpression aux = null;
959 
960  for (int i = 0; i < this.typeSet.Count; i++) {
961  if (this.typeSet[i] is TypeVariable)
962  return true;
963  MethodType mt = null;
964  if ((mt = TypeExpression.As<MethodType>(this.typeSet[i])) != null) {
965  current = mt.Return;
966  if (aux != null && aux != current) // This comparation searches differents value types.
967  return true;
968  aux = current;
969  }
970  }
971  return false;
972  }
973 
974  #endregion
975 
976  }
977 }
int SuperType(TypeExpression subType)
Check if the type can make an assignment operation.
Definition: UnionType.cs:554
override void ReplaceTypeVariables(IDictionary< TypeVariable, TypeVariable > typeVariableMappings)
Replaces type variables substituting the old type variables for the new ones.
Definition: UnionType.cs:727
override bool Unify(TypeExpression te, SortOfUnification unification, IList< Pair< TypeExpression, TypeExpression >> previouslyUnified)
This method unifies two type expressions (this and te)
Definition: UnionType.cs:577
Its operation AcceptOperation() returns an integer value that indicates a promotion level between two...
int Count
Gets the number of type expressions
Definition: UnionType.cs:50
override bool HasFreshVariable()
To know if it is a type variable with no substitution
Definition: UnionType.cs:908
Representa a union type.
Definition: UnionType.cs:36
override bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
Definition: UnionType.cs:851
UnionType(TypeExpression te)
Definition: UnionType.cs:69
bool validHasTypeVariables
To cache the result of the HasTypeVariables method
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
bool ContainsDifferentReturns()
Returns true if the union type contains method types and this methods returns differents types...
Definition: UnionType.cs:956
Abstract class that represents all different types.
UnionType()
Constructor of Intersection
Definition: UnionType.cs:68
override TypeExpression Freeze()
WriteType variable may change its type&#39;s substitution (e.g., field type variables) This method return...
Definition: UnionType.cs:750
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
override object AcceptOperation(TypeSystemOperation op, object arg)
Definition: UnionType.cs:144
override void BuildFullName()
Creates/Updates the full name of the type expression
Definition: UnionType.cs:128
No location information is provided within this class, because the exec methods invoked by the proper...
override bool HasIntersectionVariable()
To know if it has a element with intersection.
Definition: UnionType.cs:923
bool IsValueType(IList< TypeExpression > evaluated)
Definition: UnionType.cs:855
virtual bool IsFreshVariable()
To know if it is a type variable with no substitution
abstract bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
override bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
Definition: UnionType.cs:609
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...
Definition: UnionType.cs:649
override string ILType()
Gets the string type to use in IL code.
Definition: UnionType.cs:839
override bool Remove(TypeVariable toRemove)
When loops are detected, it is necesary to suppress a new extra variable returned in the return type ...
Definition: UnionType.cs:813
virtual bool IsDynamic
Indicates if the type has been set as dynamic
virtual bool HasTypeVariables()
To know if the type expression has some type variables and requieres unification The default implemen...
bool AddType(TypeExpression type)
Adds a new type expression.
Definition: UnionType.cs:81
override bool IsFreshVariable()
To know if it is a type variable with no substitution
Definition: UnionType.cs:630
Representa a method type.
Definition: MethodType.cs:37
override string BuildTypeExpressionString(int depthLevel)
Returns the type expression like a string
Definition: UnionType.cs:106
IList< TypeExpression > TypeSet
Gets the list of type expressions
Definition: UnionType.cs:57
bool ContainsValueType(TypeExpression type)
Returns true if the union type contains the specified value type.
Definition: UnionType.cs:938
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...
Definition: UnionType.cs:703
TypeExpression Freeze(IList< TypeExpression > evaluated)
Definition: UnionType.cs:754
Representa an intersection type.
static TypeExpression collect(TypeExpression collection, TypeExpression newType)
This static helper method group a newType into a union collection of types.
Definition: UnionType.cs:672
virtual object AcceptOperation(TypeSystemOperation op, object arg)