2 using System.Collections.Generic;
5 using TypeSystem.Operations;
9 namespace CodeGeneration.NewOperations {
18 private T codeGenerator;
25 public CGCastOperation(T codeGenerator,
int indent) {
26 this.codeGenerator = codeGenerator;
30 public override object Exec(
ClassType ct,
object arg) {
31 this.codeGenerator.castclass(this.indent, ct);
34 public override object Exec(
IntType it,
object arg) {
35 this.codeGenerator.convToInt(this.indent);
38 public override object Exec(
CharType ct,
object arg)
40 this.codeGenerator.convToChar(this.indent);
44 public override object Exec(
DoubleType dt,
object arg) {
45 this.codeGenerator.convToDouble(this.indent);
52 return this.ReportError(tv);
54 return this.Exec(GenerateAllUnionTypes(), arg);
55 return this.Exec(tv.Substitution, arg);
61 return this.ReportError(te);
63 this.Exec(te as UnionType, arg);
65 this.Exec(te as TypeVariable, arg);
67 this.Exec(((PropertyType)te).PropertyTypeExpression, arg);
69 this.Exec(((FieldType)te).FieldTypeExpression, arg);
71 this.codeGenerator.castclass(indent, StringType.Instance);
72 else if (IsValueType(te))
74 this.codeGenerator.UnboxAny(indent, te);
81 private static UnionType GenerateAllUnionTypes()
83 UnionType unions =
new UnionType();
84 unions.AddType(CharType.Instance);
85 unions.AddType(IntType.Instance);
86 unions.AddType(DoubleType.Instance);
90 public override object Exec(UnionType tv,
object arg)
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));
99 String finalLabel = this.codeGenerator.NewLabel;
100 String nextLabel =
"";
101 for (
int i = 0; i < typeSet.Count; i++)
103 if (!String.IsNullOrEmpty(nextLabel))
104 this.codeGenerator.WriteLabel(indent, nextLabel);
105 if (i != typeSet.Count - 1)
107 nextLabel = this.codeGenerator.NewLabel;
108 this.codeGenerator.dup(indent);
109 this.codeGenerator.isinst(indent, typeSet[i]);
110 this.codeGenerator.brfalse(indent, nextLabel);
112 this.codeGenerator.UnboxAny(indent, typeSet[i]);
114 if (i != typeSet.Count - 1)
115 this.codeGenerator.br(indent, finalLabel);
117 this.codeGenerator.WriteLabel(indent, finalLabel);
121 private List<TypeExpression> GetTypes(
TypeExpression typeExpression)
123 List<TypeExpression> typeSet =
new List<TypeExpression>();
124 if (IsValueType(typeExpression) || typeExpression is StringType)
125 typeSet.Add(typeExpression);
126 else if (typeExpression is TypeVariable)
128 if (((TypeVariable)typeExpression).Substitution != null)
129 typeSet.AddRange(GetTypes(((TypeVariable)typeExpression).Substitution));
131 else if (typeExpression is UnionType)
133 UnionType
union = typeExpression as UnionType;
134 foreach (var expression
in union.TypeSet)
135 typeSet.AddRange(GetTypes(expression));
142 ErrorManager.Instance.NotifyError(
new CodeGenerationError(
"No se ha definido la operación solicitada"));
146 #region Static methods, Candidates to implement as Operations
163 if ((te is TypeVariable) && ((TypeVariable)te).Substitution == null)
165 if ((te is TypeVariable) && ((TypeVariable)te).Substitution is UnionType)
168 return ChechBox(((PropertyType)te).PropertyTypeExpression);
170 return ChechBox(((FieldType)te).FieldTypeExpression);
174 public static bool IsInternallyAnObject(
TypeExpression typeExpression)
176 if (typeExpression is UnionType)
178 else if (typeExpression is TypeVariable)
180 if (((TypeVariable)typeExpression).Substitution == null)
183 return !IsValueType(((TypeVariable)typeExpression).Substitution);
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)
This class represent the entry wnen sending a message to an operation object derived from TypeExpress...
Represent a character type.
Abstract class that represents all different types.
Represents a generic type expression
Represent a integer type.
Represents a error produced when a MethodType has class information and tries to assign other class i...
Represents a double type.
Representa a property type.
IList< TypeExpression > TypeSet
Gets the list of type expressions
TypeExpression Substitution
Gets the substitution; null if it does not exist