The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
VerbosePromotionOperation.cs
Go to the documentation of this file.
1 using ErrorManagement;
2 using System;
3 using TypeSystem.Constraints;
4 using DynVarManagement;
5 namespace TypeSystem.Operations {
10  internal class VerbosePromotionOperation : PromotionOperation {
11 
12  #region Fields
13  private TypeExpression to;
14  private Enum op;
15  private MethodType methodAnalyzed;
16  private Location location;
17  #endregion
18 
19  #region Constructor
20  internal VerbosePromotionOperation (TypeExpression to, Enum op, MethodType methodAnalyzed, Location location) {
21  this.to = to;
22  this.op = op;
23  this.methodAnalyzed = methodAnalyzed;
24  this.location = location;
25  }
26  #endregion
27  #region Kind of promotion information
28  public override KindOfPromotion KindOfPromotion {
29  get { return KindOfPromotion.Verbose; }
30  }
31  #endregion
32  #region ClassTypeProxy --> ...
33  public override object Exec(ClassTypeProxy from, object arg) {
34  return from.RealType.AcceptOperation(this, arg);
35  }
36  #endregion
37 
38  #region FieldType --> ...
39 
40  public override object Exec(FieldType from, object arg) {
41  if (from.FieldTypeExpression != null) {
42  return from.FieldTypeExpression.AcceptOperation(this, arg);
43  }
44  return null;
45  }
46  #endregion
47 
48  #region Promotion -->
49  public override object Exec(TypeExpression from, object arg) {
50  if ((int)from.AcceptOperation (new PromotionLevelOperation(this.to), arg) == -1) {
51  return ReportError(from);
52  }
53  return to;
54  }
55  #endregion
56 
57  public override object Exec(UnionType from, object arg) {
58  return this.InternalPromotion(from, arg);
59  }
60  #region InternalPromotion
61  private TypeExpression InternalPromotion(UnionType from, object arg) {
62  if (from.IsFreshVariable() && this.methodAnalyzed != null) {
63  // * A constraint is added to the method analyzed
64  PromotionConstraint constraint = new PromotionConstraint(from, this.to, this.op, this.location);
65  this.methodAnalyzed.AddConstraint(constraint);
66  return this.to;
67  }
68  // * Static Behaviour: All the types in typeset must promote
69  // * Dynamic Behaviour: One of the types in typeset must promote
70  int aux = 0;
71  UnionType dynamicUnionType = new UnionType();
72  dynamicUnionType.IsDynamic = true;
73  foreach (TypeExpression subType in from.TypeSet) {
74  if (from.IsDynamic) {
75  // * Dynamic
76  if (subType.IsFreshVariable())
77  dynamicUnionType.AddType(subType);
78  else {
79  aux = (int) subType.AcceptOperation(new PromotionLevelOperation(this.to), arg);
80  if (aux != -1)
81  return this.to;
82  }
83  } else { // * !from.IsDynamic, so it is static
84  aux = (int)subType.AcceptOperation(new PromotionLevelOperation(this.to), arg);
85  if (aux == -1)
86  return (TypeExpression)ReportError(from);
87  }
88  }
89  if (dynamicUnionType.Count != 0) { // * 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
90  PromotionConstraint constraint = new PromotionConstraint(dynamicUnionType, this.to, this.op, this.location);
91  this.methodAnalyzed.AddConstraint(constraint);
92  return this.to;
93  }
94  if (from.IsDynamic && aux == -1) {
95  // * No promotion at all
96  return (TypeExpression)ReportError(from);
97 
98  }
99  return this.to;
100  }
101  #endregion
102 
103  public override object Exec(TypeVariable from, object arg) {
104  if (from.Substitution != null) {
105  DynVarOptions.Instance.AssignDynamism(from.Substitution, from.IsDynamic);
106  return from.Substitution.AcceptOperation(this, arg);
107  }
108  if (this.methodAnalyzed != null) {
109  // * A constraint is added to the method analyzed
110  PromotionConstraint constraint = new PromotionConstraint(from , this.to, this.op, this.location);
111  this.methodAnalyzed.AddConstraint(constraint);
112  return constraint.ReturnType;
113  }
114  return ReportError(from);
115  }
116 
117 
118  public override object ReportError(TypeExpression from) {
119  ErrorManager.Instance.NotifyError(new TypePromotionError(from.fullName, this.to.FullName, this.op.ToString(), this.location));
120  return null;
121  }
122  }
123 }
It represents constraints of the form op1 <= op2 (op1 is a subtype of op2)
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
Represents a error produced when the type promotion can not be applied to specified expressions...
KindOfPromotion
This class implements factory method pattern ( virtual constructor). Role: Factory ...