The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
SquareBracketOperation.cs
Go to the documentation of this file.
1 using TypeSystem;
2 using AST;
3 using ErrorManagement;
4 using System;
5 using TypeSystem.Constraints;
6 using DynVarManagement;
7 
8 namespace TypeSystem.Operations {
13  public class SquareBracketOperation : TypeSystemOperation {
14 
15  #region Fields
16  protected TypeExpression index; //* type expression that indexes the type that is using the bracket
17  protected MethodType methodAnalyzed;
18  protected bool showErrorMessage;
19  protected Location location;
20 #endregion
21 
22  #region Constructor
23  public SquareBracketOperation(TypeExpression index, MethodType methodAnalyzed, bool showErrorMessage, Location location) {
24  this.index = index;
25  this.methodAnalyzed = methodAnalyzed;
26  this.showErrorMessage = showErrorMessage;
27  this.location = location;
28  }
29  // * We simply check if the index is promotable to an Integer.
30  #endregion
31 
32 
33  #region ArrayType []
34  public override object Exec(ArrayType a, object arg) {
35  if (this.index.AcceptOperation(PromotionOperation.Create(IntType.Instance, ArrayOperator.Indexer, this.methodAnalyzed, this.location), arg) != null)
36  return a.ArrayTypeExpression;
37  return null;
38  }
39  #endregion
40 
41  #region TypeVariable []
42  public override object Exec(TypeVariable a, object arg) { // this is a likely array
43 
44  TypeExpression subtitutions = a.Substitution;
45  if (subtitutions != null) {
46  DynVarOptions.Instance.AssignDynamism(subtitutions, a.IsDynamic);
47  return subtitutions.AcceptOperation(this, arg);
48  }
49  if (this.methodAnalyzed != null) {
50  // * A bracket constraint is added to the method analyzed
51  SquareBracketConstraint bracketConstraint = new SquareBracketConstraint(a, this.index, this.location);
52  this.methodAnalyzed.AddConstraint(bracketConstraint);
53  // * Also a promotion constriaint of the index to IntType
54  //index.Promotion(IntType.Instance, ArrayOperator.Indexer, methodAnalyzed, fileName, line, column);
55  return bracketConstraint.ReturnType;
56  }
57  // We are at this point the operation is invalid, report the error.?
58  return ReportError(a);
59  }
60  #endregion
61 
62  #region PropertyType []
63  public override object Exec(PropertyType a, object arg) {
64  if (a.PropertyTypeExpression!= null)
65  return a.PropertyTypeExpression.AcceptOperation(this, arg);
66  return null;
67  }
68  #endregion
69 
70  #region BCLClassType []
71  public override object Exec(BCLClassType a, object arg) {
72  // * Brackets are allowed if it is an array nb
73  if (a.TypeInfo.IsArray)
74  return TypeTable.Instance.GetType(a.TypeInfo.GetElementType().FullName, this.location);
75  // * Brackets are allowed if it is an indexer
76  if (a.Methods.ContainsKey("get_Item")) {
77  MethodType method = a.Methods["get_Item"].Type as MethodType;
78  if (method != null && method.ParameterListCount == 1) // && method.GetParameter(0).Equivalent(IntType.Instance))
79  return method.Return;
80  }
81  // if we are in these point the [] is not applicable simply raise an error.
82  return ReportError(a);
83  }
84  #endregion
85 
86  #region UnionType []
87  public override object Exec(UnionType a, object arg) {
88  // * If all the types in typeset generate a constraint, we simply generate one constraint using the whole union type
89  if (a.IsFreshVariable() && this.methodAnalyzed != null) {
90  // * A constraint is added to the method analyzed
91  SquareBracketConstraint constraint = new SquareBracketConstraint(a, this.index, this.location);
92  this.methodAnalyzed.AddConstraint(constraint);
93  return constraint.ReturnType;
94  }
95  TypeExpression returnType = null;
96  foreach (TypeExpression type in a.TypeSet) {
97  TypeExpression ret = (TypeExpression)type.AcceptOperation(new SquareBracketOperation(this.index, this.methodAnalyzed, !a.IsDynamic && this.showErrorMessage, this.location), arg);
98  if (ret == null && !a.IsDynamic)
99  return null;
100  if (ret != null)
101  returnType = UnionType.collect(returnType, ret);
102  }
103  return returnType;
104  }
105 
106  #endregion
107 
108  #region FieldType []
109  public override object Exec(FieldType a, object arg) {
110  if (a.FieldTypeExpression != null)
111  return a.FieldTypeExpression.AcceptOperation(this, arg);
112  return null;
113  }
114  #endregion
115 
116 
117  #region Report Errors
118  // in our case we only notify operations not allowed
119  // for other pruposes invoke explicitly another kind of error
120 
121 //TODO: Hacer una versión más específica del error
122  public override object ReportError(TypeExpression tE) {
123  if (this.showErrorMessage)
124  ErrorManager.Instance.NotifyError(new OperationNotAllowedError("[]", tE.FullName, this.location));
125  return null;
126  }
127  #endregion
128  }
129 
130 }
Representa a union type.
Definition: UnionType.cs:36
Represents a error produced when tries to make an operation not allowed for the specified type...
override object Exec(TypeVariable a, object arg)
Representa an array type.
Definition: ArrayType.cs:35
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
override object Exec(BCLClassType a, object arg)
Dictionary< string, AccessModifier > Methods
Gets the method list
Definition: UserType.cs:96
ArrayOperator
Array operators
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
override object Exec(ArrayType a, object arg)
static PromotionOperation Create(TypeExpression type, MethodType methodAnalyzed, Location location)
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
Represent a integer type.
Definition: IntType.cs:37
Type TypeInfo
Returns the real introspective type
Definition: BCLClassType.cs:36
SquareBracketOperation(TypeExpression index, MethodType methodAnalyzed, bool showErrorMessage, Location location)
override object ReportError(TypeExpression tE)
override object Exec(PropertyType a, object arg)
Implements bracket operation of a type expression. Implments double dispatcher pattern.
TypeExpression FieldTypeExpression
Gets the field type.
Definition: FieldType.cs:58
virtual bool IsDynamic
Indicates if the type has been set as dynamic
override object Exec(UnionType a, object arg)
override bool IsFreshVariable()
To know if it is a type variable with no substitution
Definition: UnionType.cs:630
Representa a property type.
Definition: PropertyType.cs:34
Representa a method type.
Definition: MethodType.cs:37
static IntType Instance
Gets the unique instance of IntType
Definition: IntType.cs:57
It represents constraints of the form: ret := ret := op1 [ op2 ]
TypeExpression PropertyTypeExpression
Gets the property type.
Definition: PropertyType.cs:64
Representa a field type.
Definition: FieldType.cs:37
virtual object AcceptOperation(TypeSystemOperation op, object arg)
override object Exec(FieldType a, object arg)