The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
EquivalentOperation.cs
Go to the documentation of this file.
1 using TypeSystem.Operations;
2 using TypeSystem;
3 using ErrorManagement;
4 using AST;
5 using Tools;
6 using System.Collections.Generic;
7 using TypeSystem.Constraints;
8 using System;
9 using DynVarManagement;
10 namespace TypeSystem.Operations {
16  #region Fields
18  #endregion
19 
20  #region Constructor
22  this.operand2 = operand2;
23  }
24  #endregion
25 
26  #region Array.Equivalent(...)
27  public override object Exec(ArrayType operand1, object arg) {
28  // * Is it an array?
29  ArrayType arrayType = TypeExpression.As<ArrayType>(this.operand2);
30  if (arrayType != null)
31  return operand1.ArrayTypeExpression.AcceptOperation(new EquivalentOperation(arrayType.ArrayTypeExpression), arg);
32 
33  // * It can be a System.Array
34  BCLClassType bclClassType = TypeExpression.As<BCLClassType>(this.operand2);
35 
36  if (bclClassType != null && bclClassType.TypeInfo.IsArray)
37  {
38  TypeExpression elementType = TypeTable.Instance.GetType(bclClassType.TypeInfo.GetElementType().FullName, new Location());
39  return elementType.AcceptOperation(new EquivalentOperation(operand1), arg);
40  }
41 
42  return false;
43  }
44 
45  #endregion
46 
47  #region BCLClassType.Equivalent(...)
48  public override object Exec(BCLClassType operand1, object arg) {
49  if (this.operand2 == null)
50  return false;
51 
52  if (operand1.FullName.Equals(this.operand2.FullName))
53  return true;
54 
55  if ( (bool)BCLClassType.BCLtoTypeSystemMapping.ContainsKey(operand1.FullName) && (bool)BCLClassType.BCLtoTypeSystemMapping[operand1.FullName].AcceptOperation(new EquivalentOperation(operand2), null))
56  return true;
57 
58  if (operand1.TypeInfo.IsArray) {
59  Type elementType = operand1.TypeInfo.GetElementType();
60 
61  if (this.operand2 is ArrayType)
62  return new BCLClassType(elementType.FullName, elementType).AcceptOperation(new EquivalentOperation(( (ArrayType)this.operand2 ).ArrayTypeExpression), null);
63 
64  BCLClassType bclType = TypeExpression.As<BCLClassType>(this.operand2);
65  if (bclType != null && bclType.TypeInfo.IsArray) {
66  TypeExpression thisArrayType = TypeTable.Instance.GetType(operand1.TypeInfo.GetElementType().FullName, new Location()),
67  paramArrayType = TypeTable.Instance.GetType(bclType.TypeInfo.GetElementType().FullName, new Location());
68  return thisArrayType.AcceptOperation(new EquivalentOperation(paramArrayType), null);
69  }
70  }
71  return false;
72  }
73  #endregion
74 
75 
76  #region ClassTypeProxy(...)
77 
78  public override object Exec(ClassTypeProxy operand1, object arg) {
79  return operand1.RealType.AcceptOperation(this, arg);
80  }
81  #endregion
82 
83  #region FieldType (...)
84 
85  public override object Exec(FieldType operand1, object arg) {
86  FieldType fieldType = this.operand2 as FieldType;
87  if (fieldType != null)
88  return operand1.FieldTypeExpression.AcceptOperation(new EquivalentOperation(fieldType.FieldTypeExpression), arg);
89  return operand1.FieldTypeExpression.AcceptOperation(this, arg);
90  }
91  #endregion
92 
93  #region MethodType.Equivalent(...)
94 
95  public override object Exec(MethodType operand1, object arg) {
96  if (operand1 == this.operand2)
97  return true;
98 
99  TypeVariable typeVariable = this.operand2 as TypeVariable;
100  if (typeVariable != null)
101  return typeVariable.AcceptOperation(new EquivalentOperation(operand1), arg);
102 
103  MethodType method = this.operand2 as MethodType;
104  // * It must be a method
105  if (method == null)
106  return false;
107  // * Same name
108  if (!operand1.MemberInfo.MemberIdentifier.Equals(method.MemberInfo.MemberIdentifier))
109  return false;
110  // * Same class
111  if ( !(bool)operand1.MemberInfo.Class.AcceptOperation(new EquivalentOperation(method.MemberInfo.Class), null) )
112  return false;
113  // * Same signature
114  if (operand1.ParameterListCount != method.ParameterListCount)
115  return false;
116  for (int i = 0; i < operand1.ParameterListCount; i++)
117  if ( !(bool)operand1.GetParameter(i).AcceptOperation(new EquivalentOperation(method.GetParameter(i)), null) )
118  return false;
119  return true;
120  }
121  #endregion
122 
123  #region TypeExpression.Equivalent
124  public override object Exec(TypeExpression operand1, object arg) {
125  if (this.operand2 == operand1)
126  return true;
127  TypeVariable typeVariable = this.operand2 as TypeVariable;
128  if (typeVariable != null)
129  return typeVariable.AcceptOperation(new EquivalentOperation(operand1), arg);
130  FieldType fieldType = this.operand2 as FieldType;
131  if (fieldType != null)
132  return fieldType.AcceptOperation(this, arg);
133  UnionType unionType = this.operand2 as UnionType;
134  if (unionType != null)
135  return this.operand2.FullName.Contains(operand1.FullName);
136  return operand1.FullName.Equals(this.operand2.FullName);
137  }
138 
139  #endregion
140 
141  #region TypeVariable.Equivalent
142  public override object Exec(TypeVariable operand1, object arg) {
143  if (operand1.Substitution != null) {
144  DynVarOptions.Instance.AssignDynamism(operand1.Substitution, operand1.IsDynamic);
145  // * If the variable is bounded, the equivalence is the one of its substitution
146  return operand1.EquivalenceClass.Substitution.AcceptOperation(this, arg);
147  }
148  // * A free variable is equivalent to any type
149  return true;
150  }
151  #endregion
152 
153  #region UnionType.Equivalent
154  public override object Exec(UnionType operand1, object arg) {
155  foreach (TypeExpression type in operand1.TypeSet)
156  if (!(bool)type.AcceptOperation(this, arg))
157  return false;
158 
159  return true;
160  }
161  #endregion
162  public override object ReportError(TypeExpression tE) {
163  //if (this.showErrorMessage)
164  System.Diagnostics.Debug.Assert(false, "Called Report Error in Equivalent operation. Revisite your code");
165  return null;
166  }
167  }
168 }
Representa a union type.
Definition: UnionType.cs:36
Representa an array type.
Definition: ArrayType.cs:35
static TypeTable Instance
Gets the unique instance of TypeTable
Definition: TypeTable.cs:57
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...
override object Exec(MethodType operand1, object arg)
override object Exec(TypeExpression operand1, object arg)
string MemberIdentifier
Gets the attribute name
override object Exec(BCLClassType operand1, object arg)
AccessModifier MemberInfo
Gets or sets the attribute information of method type
Definition: MethodType.cs:118
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
TypeExpression GetType(string name, Location location)
Gets the type associated to the name specified in the argument.
Definition: TypeTable.cs:117
Represents a proxy of a class type. It implements the unfold operatations of theoretical type systes...
virtual string FullName
Gets the full name of the type Note: WriteType expression is the longest recursive representation of ...
int ParameterListCount
Gets the number of parameters
Definition: MethodType.cs:111
No location information is provided within this class, because the exec methods invoked by the proper...
Type TypeInfo
Returns the real introspective type
Definition: BCLClassType.cs:36
Implementation of a table of types.
Definition: TypeTable.cs:30
override object Exec(UnionType operand1, object arg)
TypeExpression FieldTypeExpression
Gets the field type.
Definition: FieldType.cs:58
TypeExpression ArrayTypeExpression
Gets the array type.
Definition: ArrayType.cs:55
override object Exec(FieldType operand1, object arg)
override object ReportError(TypeExpression tE)
override object Exec(ClassTypeProxy operand1, object arg)
Representa a method type.
Definition: MethodType.cs:37
TypeExpression Substitution
Gets the substitution; null if it does not exist
override object Exec(TypeVariable operand1, object arg)
Representa a field type.
Definition: FieldType.cs:37
override object Exec(ArrayType operand1, object arg)
virtual object AcceptOperation(TypeSystemOperation op, object arg)