The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
CGCastOperation.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using AST;
4 using CodeGeneration;
5 using TypeSystem.Operations;
6 using TypeSystem;
7 using ErrorManagement;
8 
9 namespace CodeGeneration.NewOperations {
13  internal class CGCastOperation <T>:TypeSystemOperation where T: ILCodeGenerator {
14 
18  private T codeGenerator;
22  private int indent;
23 
24 
25  public CGCastOperation(T codeGenerator, int indent) {
26  this.codeGenerator = codeGenerator;
27  this.indent = indent;
28  }
29 
30  public override object Exec(ClassType ct, object arg) {
31  this.codeGenerator.castclass(this.indent, ct);
32  return null;
33  }
34  public override object Exec(IntType it, object arg) {
35  this.codeGenerator.convToInt(this.indent);
36  return null;
37  }
38  public override object Exec(CharType ct, object arg)
39  {
40  this.codeGenerator.convToChar(this.indent);
41  return null;
42  }
43 
44  public override object Exec(DoubleType dt, object arg) {
45  this.codeGenerator.convToDouble(this.indent);
46  return null;
47  }
48 
49  public override object Exec(TypeVariable tv, object arg)
50  {
51  if (arg == null)
52  return this.ReportError(tv);
53  if (tv.Substitution == null)
54  return this.Exec(GenerateAllUnionTypes(), arg);
55  return this.Exec(tv.Substitution, arg);
56  }
57 
58  public override object Exec(TypeExpression te, object arg)
59  {
60  if (arg == null)
61  return this.ReportError(te);
62  if (te is UnionType)
63  this.Exec(te as UnionType, arg);
64  else if (te is TypeVariable)
65  this.Exec(te as TypeVariable, arg);
66  else if (te is PropertyType)
67  this.Exec(((PropertyType)te).PropertyTypeExpression, arg);
68  else if (te is FieldType)
69  this.Exec(((FieldType)te).FieldTypeExpression, arg);
70  else if (te is StringType)
71  this.codeGenerator.castclass(indent, StringType.Instance);
72  else if (IsValueType(te))
73  {
74  this.codeGenerator.UnboxAny(indent, te);
75  ((TypeExpression) arg).AcceptOperation(this, null);
76  }
77 
78  return null;
79  }
80 
81  private static UnionType GenerateAllUnionTypes()
82  {
83  UnionType unions = new UnionType();
84  unions.AddType(CharType.Instance);
85  unions.AddType(IntType.Instance);
86  unions.AddType(DoubleType.Instance);
87  return unions;
88  }
89 
90  public override object Exec(UnionType tv, object arg)
91  {
92  if (arg == null)
93  return this.ReportError(tv);
94  List<TypeExpression> typeSet = new List<TypeExpression>();
95  for (int i = 0; i < tv.TypeSet.Count; i++)
96  typeSet.AddRange(GetTypes(tv.TypeSet[i]));
97  typeSet = new List<TypeExpression>(new HashSet<TypeExpression>(typeSet));
98 
99  String finalLabel = this.codeGenerator.NewLabel;
100  String nextLabel = "";
101  for (int i = 0; i < typeSet.Count; i++)
102  {
103  if (!String.IsNullOrEmpty(nextLabel))
104  this.codeGenerator.WriteLabel(indent, nextLabel);
105  if (i != typeSet.Count - 1)
106  {
107  nextLabel = this.codeGenerator.NewLabel;
108  this.codeGenerator.dup(indent);
109  this.codeGenerator.isinst(indent, typeSet[i]);
110  this.codeGenerator.brfalse(indent, nextLabel);
111  }
112  this.codeGenerator.UnboxAny(indent, typeSet[i]);
113  ((TypeExpression)arg).AcceptOperation(this, arg);
114  if (i != typeSet.Count - 1)
115  this.codeGenerator.br(indent, finalLabel);
116  }
117  this.codeGenerator.WriteLabel(indent, finalLabel);
118  return null;
119  }
120 
121  private List<TypeExpression> GetTypes(TypeExpression typeExpression)
122  {
123  List<TypeExpression> typeSet = new List<TypeExpression>();
124  if (IsValueType(typeExpression) || typeExpression is StringType)
125  typeSet.Add(typeExpression);
126  else if (typeExpression is TypeVariable)
127  {
128  if (((TypeVariable)typeExpression).Substitution != null)
129  typeSet.AddRange(GetTypes(((TypeVariable)typeExpression).Substitution));
130  }
131  else if (typeExpression is UnionType)
132  {
133  UnionType union = typeExpression as UnionType;
134  foreach (var expression in union.TypeSet)
135  typeSet.AddRange(GetTypes(expression));
136  }
137  return typeSet;
138  }
139 
140  public override object ReportError(TypeExpression tE)
141  {
142  ErrorManager.Instance.NotifyError(new CodeGenerationError("No se ha definido la operación solicitada"));
143  return null;
144  }
145 
146  #region Static methods, Candidates to implement as Operations
147 
148  public static bool IsValueType(TypeExpression exp)
149  {
150  if (exp is BoolType)
151  return true;
152  if (exp is CharType)
153  return true;
154  if (exp is IntType)
155  return true;
156  if (exp is DoubleType)
157  return true;
158  return false;
159  }
160 
161  public static bool ChechBox(TypeExpression te)
162  {
163  if ((te is TypeVariable) && ((TypeVariable)te).Substitution == null)
164  return true;
165  if ((te is TypeVariable) && ((TypeVariable)te).Substitution is UnionType)
166  return true;
167  if (te is PropertyType)
168  return ChechBox(((PropertyType)te).PropertyTypeExpression);
169  if (te is FieldType)
170  return ChechBox(((FieldType)te).FieldTypeExpression);
171  return false;
172  }
173 
174  public static bool IsInternallyAnObject(TypeExpression typeExpression)
175  {
176  if (typeExpression is UnionType)
177  return true;
178  else if (typeExpression is TypeVariable)
179  {
180  if (((TypeVariable)typeExpression).Substitution == null)
181  return true;
182  else
183  return !IsValueType(((TypeVariable)typeExpression).Substitution);
184  }
185  else if (typeExpression is PropertyType)
186  return !IsValueType(((PropertyType)typeExpression).PropertyTypeExpression);
187  else if (typeExpression is FieldType)
188  return !IsValueType(((FieldType)typeExpression).FieldTypeExpression);
189  else if (typeExpression is StringType)
190  return true;
191  return false;
192  }
193  #endregion
194  }
195 }
Representa a union type.
Definition: UnionType.cs:36
Represent a string type.
Definition: StringType.cs:36
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
Represent a character type.
Definition: CharType.cs:38
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
Represent a integer type.
Definition: IntType.cs:37
Represents a error produced when a MethodType has class information and tries to assign other class i...
Represents a double type.
Definition: DoubleType.cs:36
Representa a property type.
Definition: PropertyType.cs:34
IList< TypeExpression > TypeSet
Gets the list of type expressions
Definition: UnionType.cs:57
TypeExpression Substitution
Gets the substitution; null if it does not exist
Represent a bool type.
Definition: BoolType.cs:36
Represents a class type.
Definition: ClassType.cs:35
Representa a field type.
Definition: FieldType.cs:37