The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
VisitorCodeGeneration2.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: VisitorCodeGeneration2.cs //
6 // Author: Cristina Gonzalez Muñoz - cristi.gm@gmail.com //
7 // Daniel Zapico Rodríguez - daniel.zapico.rodriguez@gmail.com //
8 // Description: //
9 // This class walks the AST to obtain the field and localinit directives. //
10 // Inheritance: VisitorAdapter //
11 // Implements Visitor pattern [Concrete Visitor]. //
12 // -------------------------------------------------------------------------- //
13 // Create date: 11-06-2007 //
14 // Modification date: 11-06-2007 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 using AST;
21 using TypeSystem;
22 
23 namespace CodeGeneration {
32  #region Fields
33 
37  private List<FieldDefinition>[] fields;
38 
42  private List<IdDeclaration> decls;
43 
47  private const string auxiliarVar = "v_array_temp_";
52  private const string auxiliarValue = "V_"; //this is the syntax of VS
53 
57  private static int currentAuxiliarSuffix = 0;
58 
59  public int CurrentAuxiliarSuffix {
60  get { return currentAuxiliarSuffix; }
61  set { currentAuxiliarSuffix = value; }
62  }
63 
64  #endregion
65 
66  #region Constructor
67 
72  this.fields = new List<FieldDefinition>[2];
73  this.fields[0] = new List<FieldDefinition>();
74  this.fields[1] = new List<FieldDefinition>();
75  this.decls = new List<IdDeclaration>();
76  TemporalVariablesTable.Instance.Clear();
77  }
78 
79  #endregion
80 
81  // Declarations
82 
83  #region Visit(ClassDefinition node, Object obj)
84 
85  public override Object Visit(ClassDefinition node, Object obj) {
86  for (int i = 0; i < node.MemberCount; i++) {
87  node.GetMemberElement(i).Accept(this, obj);
88  }
89  return this.fields;
90  }
91 
92  #endregion
93 
94  //#region Visit(MethodDefinition node, Object obj)
95 
96  public override Object Visit(InvocationExpression node, Object obj) {
97  node.Identifier.Accept(this, null);
98  if (node.FrozenTypeExpression.IsValueType()) {
100  IdDeclaration id = new IdDeclaration(sid, node.ILTypeExpression.ILType(), node.Location);
101  id.TypeExpr = node.ExpressionType ?? node.ILTypeExpression;
102  if (TemporalVariablesTable.Instance.SearchId(id.FullName) == null) { // it's a new inserction
103  TemporalVariablesTable.Instance.Insert(id.FullName, id.ILName);
104  decls.Add(id);
105  }
106  }
107  node.Arguments.Accept(this, obj);
108  return null;
109  }
110  #region Visit(CastExpression node, Object obj)
111 
112  public override Object Visit(CastExpression node, Object obj) {
113  node.Expression.Accept(this, obj);
114 
117  IdDeclaration id = new IdDeclaration(sId, node.Expression.ExpressionType.ILType(), node.Location);
118  id.TypeExpr = node.Expression.ExpressionType;
119  // temporal variables of the same type are forbidden
120  if (TemporalVariablesTable.Instance.SearchId(id.FullName) == null) { // it's a new inserction
121  TemporalVariablesTable.Instance.Insert(id.FullName, id.ILName);
122  decls.Add(id);
123  }
124  }
125  return null;
126  }
127  #endregion
128  public override Object Visit(FieldAccessExpression node, Object obj) {
131  IdDeclaration id = new IdDeclaration(sId, node.Expression.ExpressionType.ILType(), node.Location);
132  id.TypeExpr = node.Expression.ExpressionType;
133  // temporal variables of the same type are forbidden
134  if (TemporalVariablesTable.Instance.SearchId(id.FullName) == null) { // it's a new inserction
135  TemporalVariablesTable.Instance.Insert(id.FullName, id.ILName);
136  decls.Add(id);
137  }
138  }
139  node.FieldName.Accept(this, obj);
140  return null;
141  }
142 
143 
144  public override Object Visit(MethodDefinition node, Object obj) {
146  node.Body.Accept(this, obj);
147  return this.decls;
148  }
149 
150 
151  #region Visit(ConstructorDefinition node, Object obj)
152 
153  public override Object Visit(ConstructorDefinition node, Object obj) {
154  this.CurrentAuxiliarSuffix = 0;
155  node.Body.Accept(this, obj);
156  return this.decls;
157  }
158 
159  #endregion
160 
161  // Fields
162 
163  #region Visit(FieldDefinition node, Object obj)
164 
165  public override Object Visit(FieldDefinition node, Object obj) {
166  node.Init.Accept(this, obj);
167  if ((((FieldType)node.TypeExpr).MemberInfo.ModifierMask & Modifier.Static) != 0)
168  this.fields[1].Add(node);
169  else
170  this.fields[0].Add(node);
171  return null;
172  }
173 
174 
175 
176  //public override Object Visit(ConstantDefinition node, Object obj) {
177  // Object obj = node.Init.Accept(this, obj);
178  // if (cnode is node.
179  // if ((((FieldType)node.TypeExpr).MemberInfo.ModifierMask & Modifier.Static) != 0)
180  // this.fields[1].Add(node);
181  //}
182 
183  #endregion
184 
185  // Local Variables
186 
187  #region Visit(IdDeclaration node, Object obj)
188 
189  public override Object Visit(IdDeclaration node, Object obj) {
190  this.decls.Add(node);
191  return null;
192  }
193 
194  #endregion
195 
196  #region Visit(Definition node, Object obj)
197 
198  public override Object Visit(Definition node, Object obj) {
199  this.decls.Add(node);
200  node.Init.Accept(this, obj);
201  return null;
202  }
203 
204  #endregion
205 
206  // Expressions
207 
208  #region Visit(NewArrayExpression node, Object obj)
209 
210  public override Object Visit(NewArrayExpression node, Object obj) {
211  string tmpName = auxiliarVar + CurrentAuxiliarSuffix++;
213  IdDeclaration id = new IdDeclaration(sId, -1, node.TypeInfo, node.Location);
214  id.TypeExpr = node.ILTypeExpression;
215 
216  if (TemporalVariablesTable.Instance.SearchId(id.FullName) == null) { // it's a new inserction
217  TemporalVariablesTable.Instance.Insert(id.FullName, id.ILName);
218  }
219  node.Identifier = id.Identifier;
220  //decls.Add(id);
221  this.decls.Add(id);
222  if (node.Size != null)
223  node.Size.Accept(this, obj);
224  if (node.Init != null)
225  node.Init.Accept(this, obj);
226  return this.decls;
227  }
228 
229  #endregion
230 
231  // Literals
232 
233  #region Visit(BoolLiteralExpression node, Object obj)
234 
235  public override Object Visit(BoolLiteralExpression node, Object obj) {
236  return Convert.ToString(node.BoolValue);
237  }
238 
239  #endregion
240 
241  #region Visit(CharLiteralExpression node, Object obj)
242 
243  public override Object Visit(CharLiteralExpression node, Object obj) {
244  return Convert.ToString(Convert.ToUInt16(node.CharValue));
245  }
246 
247  #endregion
248 
249  #region Visit(DoubleLiteralExpression node, Object obj)
250 
251  public override Object Visit(DoubleLiteralExpression node, Object obj) {
252  return node.ILValue;
253  }
254 
255  #endregion
256 
257  #region Visit(IntLiteralExpression node, Object obj)
258 
259  public override Object Visit(IntLiteralExpression node, Object obj) {
260  return node.ILValue;
261  }
262 
263  #endregion
264 
265  #region Visit(StringLiteralExpression node, Object obj)
266 
267  public override Object Visit(StringLiteralExpression node, Object obj) {
268  return node.StringValue;
269  }
270 
271  #endregion
272 
273  public override void AddExceptionCode() {
274  //this.codeGenerator.WriteCodeOfExceptions();
275  System.Diagnostics.Debug.Assert(false, "no debería llegar aqú); } public override void Close() { System.Diagnostics.Debug.Assert(false, "no debería llegar aqú"); } } } ");
276  }
277  public override void Close() {
278  System.Diagnostics.Debug.Assert(false, "no debería llegar aqú); } } } ");
279  }
280  }
281 }
282 
override Object Visit(IntLiteralExpression node, Object obj)
Encapsulates a definition of a concrete method.
override void Close()
Class used to generate de intermediate code We implement explicitly covariance in this attribute by m...
Encapsulates a string literal expression.
Encapsulates the expression to access a field.
Encapsulates a invocation expression.
Encapsulates a boolean literal expression.
Encapsulates a cast expression.
Encapsulates a definition of a concrete field.
TypeExpression ExpressionType
Gets or sets the type of the expression.
Definition: Expression.cs:71
This class walks the AST to to obtain intermediate code Thisis a layer adapter class and is the base ...
override Object Visit(BoolLiteralExpression node, Object obj)
Location Location
Definition: AstNode.cs:45
Encapsulates a definition.
Definition: Definition.cs:36
override Object Visit(DoubleLiteralExpression node, Object obj)
override Object Visit(StringLiteralExpression node, Object obj)
string TypeInfo
Gets the name of the new type
string SearchId(string type)
Searches the temporal variable.whose type is represented in type
Encapsulates a identifier expression of our programming language.
override Object Visit(NewArrayExpression node, Object obj)
override Object Visit(ConstructorDefinition node, Object obj)
Implementation of a table of variables. thist tables search for an id according to its string type re...
This class walks the AST to obtain the field and localinit directives.
abstract bool IsValueType()
True if type expression is a ValueType. Otherwise, false.
override Object Visit(FieldAccessExpression node, Object obj)
CompoundExpression Init
Gets or sets the array initialization
virtual string ILType()
Gets the type name to use in IL code.
Encapsulates a new array expression.
Encapsulates a definition of a concrete constructor.
override Object Visit(ClassDefinition node, Object obj)
Encapsulates a string literal expression.
override Object Visit(IdDeclaration node, Object obj)
Encapsulates a declaration.
Modifier
Indicates differents modifiers to use in class (only public, internal or static), fields or methods...
Expression Size
Gets or sets the array size
Encapsulates a integer literal expression.
override Object Visit(MethodDefinition node, Object obj)
override Object Visit(InvocationExpression node, Object obj)
override Object Visit(CharLiteralExpression node, Object obj)
VisitorCodeGeneration2()
Constructor of VisitorCodeGeneration2
override Object Visit(CastExpression node, Object obj)
Representa a field type.
Definition: FieldType.cs:37
override Object Visit(Definition node, Object obj)
TypeExpression FrozenTypeExpression
WriteType variable may change its type&#39;s substitution (e.g., field type variables) This attribute sav...
virtual TypeExpression ILTypeExpression
Gets the type expression to use in code generation.
Definition: Expression.cs:98
Expression Expression
Gets the expression to access a field.
Encapsulates a definition of a concrete class.
Expression Expression
Gets the expression to convert.
override Object Visit(FieldDefinition node, Object obj)
TypeExpression TypeExpr
Gets or sets the type of the declaration
Definition: Declaration.cs:63
static TemporalVariablesTable Instance
Gets the unique instance of TemporalVariablesTable