The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
PromotionConstraint.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: PromotionConstraint.cs
6 // Author: Francisco Ortin - francisco.ortin@gmail.com
7 // Description:
8 // Promotion constraint to be executed each time the associated method
9 // has been called. It represents constraints of the form:
10 // op1 <= op2 (op1 is a subtype of op2)
11 // Implements Composite pattern [Leaf].
12 // Implements Command pattern [Concrete Command].
13 // Implements Memento pattern [Memento].
14 // --------------------------------------------------------------------------
15 // Create date: 05-04-2007
16 // Modification date: 25-04-2007
18 
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22 
23 using ErrorManagement;
24 using TypeSystem.Operations;
25 
26 namespace TypeSystem.Constraints {
32  #region Fields
33  private TypeExpression secondOperand;
37 
42  private object op;
43  #endregion
44 
45  #region Properties
46  public TypeExpression SecondOperand {
50  get { return this.secondOperand; }
51  }
52 
57  public object Operator {
58  get { return this.op; }
59  }
60  #endregion
61 
62 
63  #region Constructor
64  public PromotionConstraint(TypeExpression firstOperand, TypeExpression secondOperand, object op, Location location)
72  : base(firstOperand, location) {
73  this.secondOperand = secondOperand;
74  this.op = op;
75  }
82  public PromotionConstraint(TypeExpression firstOperand, TypeExpression secondOperand, Location location)
83  : this(firstOperand, secondOperand, null, location) { }
84  #endregion
85 
86  #region BuildTypeExpressionString()
87  protected override string BuildTypeExpressionString() {
88  return String.Format("{0} <= {1}", this.FirstOperand.FullName, this.SecondOperand.FullName);
89  }
90  #endregion
91 
92  #region EqualsAndGetHashCode
93  public override bool Equals(object obj) {
94  PromotionConstraint constraint = obj as PromotionConstraint;
95  if (constraint == null)
96  return false;
97  return this.FirstOperand.Equals(constraint.FirstOperand) && this.secondOperand.Equals(constraint.secondOperand);
98  }
99  public override int GetHashCode() {
100  return this.FirstOperand.GetHashCode()*this.secondOperand.GetHashCode();
101  }
102  #endregion
103 
104 
105  #region CloneTypeVariables()
106  public override Constraint CloneTypeVariables(IDictionary<TypeVariable, TypeVariable> typeVariableMappings, IList<EquivalenceClass> equivalenceClasses) {
115  TypeExpression newFirstOperand = this.FirstOperand.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>()),
116  newSecondOperand = this.SecondOperand.CloneTypeVariables(typeVariableMappings, equivalenceClasses, new List<ClassType>());
117  if (this.op != null)
118  return new PromotionConstraint(newFirstOperand, newSecondOperand, this.Operator, this.Location);
119  return new PromotionConstraint(newFirstOperand, newSecondOperand, this.Location);
120  }
121  #endregion
122 
123  #region Check()
124  public override TypeExpression Check(MethodType methodAnalyzed, TypeExpression actualImplicitObject, bool showInvocationMessage,
135  SortOfUnification activeSortOfUnification, Location location) {
136  TypeExpression result;
137  if (this.op!=null)
138  //result = this.FirstOperand.Promotion(this.SecondOperand, (Enum)this.Operator, methodAnalyzed, this.Location);
139  result = (TypeExpression)this.FirstOperand.AcceptOperation(PromotionOperation.Create(this.SecondOperand, (Enum)this.Operator, methodAnalyzed, this.Location), null);
140  else
141  result = (TypeExpression)this.FirstOperand.AcceptOperation(PromotionOperation.Create(this.SecondOperand, methodAnalyzed, this.Location), null);
142  if (result == null && showInvocationMessage) {
143  ErrorManager.Instance.NotifyError(new ConstraintError(location));
144  return null;
145  }
146  // * If no error exists, we return the supertype
147  return this.SecondOperand;
148  }
149  #endregion
150 
151 
152  }
153 }
It represents constraints of the form op1 &lt;= op2 (op1 is a subtype of op2)
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
PromotionConstraint(TypeExpression firstOperand, TypeExpression secondOperand, Location location)
Constructor for promotion constraints without operator
Abstract class that represents all different types.
static PromotionOperation Create(TypeExpression type, MethodType methodAnalyzed, Location location)
Representa a method type.
Definition: MethodType.cs:37
virtual object AcceptOperation(TypeSystemOperation op, object arg)