2 using CodeGeneration.Operations;
3 using TypeSystem.Operations;
8 using System.Collections.Generic;
9 using CodeGeneration.ExceptionManagement;
10 namespace CodeGeneration.NewOperations
18 protected VisitorILCodeGeneration<T> visitor;
20 protected T codeGenerator;
26 public CGUnaryOperation(VisitorILCodeGeneration<T> visitor,
object obj,
int indent,
UnaryExpression node)
28 this.codeGenerator = visitor.codeGenerator;
29 this.visitor = visitor;
35 public override object Exec(
TypeExpression typeExpression,
object arg)
38 this.Exec(typeExpression as UnionType, arg);
40 this.Exec(typeExpression as TypeVariable, arg);
42 this.Exec(((PropertyType)typeExpression).PropertyTypeExpression, arg);
44 this.Exec(((FieldType)typeExpression).FieldTypeExpression, arg);
45 else if (IsValueType(typeExpression))
47 node.Operand.Accept(visitor, obj);
53 public override object Exec(TypeVariable teLeft,
object arg)
56 return this.Exec(GenerateAllUnionTypes(), arg);
58 return this.Exec(teLeft.Substitution, arg);
61 public override object Exec(UnionType teLeft,
object arg)
63 node.Operand.Accept(visitor, obj);
67 String finalLabel = this.codeGenerator.NewLabel;
68 String nextLabel =
"";
70 List<TypeExpression> typeSet =
new List<TypeExpression>();
71 for (
int i = 0; i < teLeft.TypeSet.Count; i++)
72 typeSet.AddRange(GetTypes(teLeft.
TypeSet[i]));
73 typeSet =
new List<TypeExpression>(
new HashSet<TypeExpression>(typeSet));
75 for (
int i = 0; i < typeSet.Count; i++)
77 if (!String.IsNullOrEmpty(nextLabel))
78 this.codeGenerator.WriteLabel(indent, nextLabel);
79 if (i != typeSet.Count - 1)
81 nextLabel = this.codeGenerator.NewLabel;
82 this.codeGenerator.dup(indent);
83 this.codeGenerator.isinst(indent, typeSet[i]);
84 this.codeGenerator.brfalse(indent, nextLabel);
86 this.codeGenerator.UnboxAny(indent, typeSet[i]);
88 this.codeGenerator.Box(indent, typeSet[i]);
89 if (i != typeSet.Count - 1)
90 this.codeGenerator.br(indent, finalLabel);
92 this.codeGenerator.WriteLabel(indent, finalLabel);
99 private UnionType GenerateAllUnionTypes()
101 UnionType unions =
new UnionType();
104 unions.AddType(CharType.Instance);
105 unions.AddType(IntType.Instance);
106 unions.AddType(DoubleType.Instance);
109 unions.AddType(BoolType.Instance);
113 private void GenerateOperator()
115 switch (node.Operator)
117 case UnaryOperator.Not:
118 this.codeGenerator.ldc(this.indent,
false);
119 this.codeGenerator.ceq(this.indent);
121 case UnaryOperator.BitwiseNot:
122 this.codeGenerator.not(this.indent);
124 case UnaryOperator.Minus:
125 this.codeGenerator.neg(this.indent);
127 case UnaryOperator.Plus:
134 private List<TypeExpression> GetTypes(
TypeExpression typeExpression)
136 List<TypeExpression> typeSet =
new List<TypeExpression>();
137 if (IsValueType(typeExpression) || typeExpression is
StringType)
138 typeSet.Add(typeExpression);
139 else if (typeExpression is TypeVariable)
141 if (((TypeVariable)typeExpression).Substitution != null)
142 typeSet.AddRange(GetTypes(((TypeVariable)typeExpression).Substitution));
144 else if (typeExpression is UnionType)
146 UnionType
union = typeExpression as UnionType;
147 foreach (var expression
in union.TypeSet)
148 typeSet.AddRange(GetTypes(expression));
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 double type.
Encapsulates a unary expression of our programming language.
UnaryOperator
Unary operators
Representa a property type.
IList< TypeExpression > TypeSet
Gets the list of type expressions
TypeExpression Substitution
Gets the substitution; null if it does not exist