The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
CodeGenerator.cs
Go to the documentation of this file.
1 // -------------------------------------------------------------------------- //
3 // Project rROTOR //
4 // -------------------------------------------------------------------------- //
5 // File: CodeGenerator.cs //
6 // Authors: Cristina Gonzalez Muņoz - cristi.gm@gmail.com //
7 // Francisco Ortin - francisco.ortin@gmail.com //
8 // Daniel Zapico - daniel.zapico.rodriguez@gmail.com //
9 // Description: //
10 // This class encapsulates the IL instruction used to generate the code.
11 // TODO: Maybe the comment above is wrong, the idea of this class is to add //
12 //functionality to the generated code not specifically IL code. In fact
13 // children of this class are code generators of MSIL, CLR, rRotor and C#
14 // Implements Factory method [Abstract Product].
15 // -------------------------------------------------------------------------- //
16 // Create date: 28-05-2007 //
17 // Modification date: in progress //
19 
20 using System;
21 using System.Collections.Generic;
22 using System.IO;
23 using System.Text;
24 using AST;
25 using CodeGeneration.ExceptionManagement;
26 using TypeSystem;
27 
28 namespace CodeGeneration {
32  abstract public class CodeGenerator {
33 
34 
35  #region Fields
36 
40  protected TextWriter output;
41 
46 
50  protected int currentLabel;
51 
55  protected Dictionary<string, DynamicExceptionManager> exceptions;
56 
57  #endregion
58 
59  #region Properties
60 
64  public abstract string NewLabel {
65  get;
66  }
67 
68  #endregion
69 
70  #region Constructors
71 
76  public CodeGenerator(TextWriter writer) {
77  this.output = writer;
78  this.currentLocalVars = new StringBuilder();
79  this.currentLabel = 0;
80  this.exceptions = new Dictionary<string, DynamicExceptionManager>();
81  }
82 
83  #endregion
84 
85  public abstract void InitialComment();
86 
87  #region WriteComment()
88 
94  public abstract void Comment(string msg);
101  public abstract void Comment(int indent, string msg);
102 
103 
104  #endregion
105 
106  #region WriteLabel()
107 
112  public abstract void WriteLabel(int indent, string label);
113 
114  #endregion
115 
116  internal abstract void WriteConstructorHeader(int indent);
117 
118  internal abstract void WriteStaticConstructorHeader(int indent);
119 
120 
121  /* #region WriteLine()
122 
127  public abstract void WriteLine();
128 
129  #endregion
130 
131  #region WriteIndentation()
132 
137  protected abstract void WriteIndentation(int lenght);
138 
139  #endregion
140 
141  #region Write ()
142  //Helper writes the formatted mesage in the file. Using format and obj
143  // to build the resulting string.
144  public virtual void Write(string format, params Object[] obj) {
145  this.output.Write(format, obj);
146  }
147  #endregion
148  */
149  #region WriteType()
150 
155  public abstract void WriteType(TypeExpression type);
156  #endregion
157 
158  #region WriteHeader()
159 
164  public abstract void WriteHeader(string fileName);
165 
166  #endregion
167 
168  #region WriteNamespaceHeader()
169  public abstract void WriteNamespaceHeader(int indent, string name);
175 
176  #endregion
177 
178  #region WriteLNClassHeader()
179 
186  public abstract void WriteLNClassHeader(int indent, string name, ClassType type);
187  #endregion
188 
189  #region WriteEndOfClass()
190 
196  public abstract void WriteEndOfClass(int indent, string name);
197 
198  #endregion
199 
200  #region WriteLNInterfaceHeader()
201 
208  public abstract void WriteInterfaceHeader(int indent, string name, InterfaceType type);
209 
210  #endregion
211 
212  #region WriteLNEndOfInterface()
213 
219  public abstract void WriteEndOfInterface(int indent, string name);
220  #endregion
221 
222  #region WriteLNMethodHeader()
223  public abstract void WriteLNMethodHeader(int indent, string name, MethodType type);
230  #endregion
231 
232  #region WriteEndOfMethod()
233 
239  public abstract void WriteEndOfMethod(int indent, string name);
240 
241  #endregion
242 
243  #region WriteParams()
244  public abstract void WriteParams(MethodType memberType, AST.CompoundExpression arguments);
250  #endregion
251 
252  #region WriteField()
253 
261  public abstract void WriteField(int indent, string name, FieldType type, bool constantField);
262 
267  public abstract void WriteLNFieldInitialization(TypeExpression type);
268 
273  public virtual void WriteLNFieldInitialization(string init) {
274  this.output.WriteLine(init);
275  }
276 
277  #endregion
278 
279  #region WriteEndOfField()
280 
284  public abstract void WriteEndOfField();
285  #endregion
286 
287  #region WriteStartBlock()
288 
293  public abstract void WriteStartBlock(int indent);
294 
295  #endregion
296 
297  #region WriteLNEndOfBlock()
298 
303  public abstract void WriteEndOfBlock(int indent);
304 
305  #endregion
306 
307  #region AddLocalVariable();
308  public abstract void AddLocalVariable(string name, TypeExpression type);
309 
310  #endregion
311 
312  #region WriteLocalVariable()
313 
318  public abstract void WriteLocalVariable(int indent);
319 
320  #endregion
321 
322  #region WriteAuxiliarLocalVariable()
323 
330  public abstract void WriteAuxiliarLocalVariable(int indent, string id, string type);
331 
332  #endregion
333 
334  #region WriteCodeOfExceptions()
335 
340  public virtual void WriteCodeOfExceptions() {
341  foreach (string key in this.exceptions.Keys)
343  }
349 
350  #endregion
351 
352  #region WriteEntryPoint()
353 
358  public abstract void WriteEntryPoint(int indent);
359 
360 
361  #endregion
362 
363 
364  #region WriteEntryPoint()
365 
366  public virtual void WriteEntryPoint() {
367  this.WriteEntryPoint();
368  }
369  #endregion
370 
371 
372  #region TryDirective ()
373 
374  public abstract void WriteTryDirective(int indent);
375 
376  public abstract void WriteOpenBraceTry(int indent);
377 
378  public abstract void WriteCloseBraceTry(int indent);
379 
380  #endregion
381 
382  #region Catch()
383 
384  public abstract void WriteCatch(int indent, String type, String var);
385 
386  public abstract void WriteOpenBraceCatch(int indent);
387 
388  public abstract void WriteCloseBraceCatch(int indent);
389 
390  #endregion
391 
392  #region Finally()
393 
394  public abstract void WriteFinally(int indent);
395 
396  public abstract void WriteOpenBraceFinally(int indent);
397 
398  public abstract void WriteCloseBraceFinally(int indent);
399  #endregion
400  //#region RuntimeFreshTypeExpressionPromotion()
401 
407  //public abstract void RuntimeFreshTypeExpressionPromotion(int indent, TypeExpression typeExpression);
408  //#endregion
409 
410  #region RuntimeIsInstruction()
411 
418 
419  #endregion
420 
421  #region WriteThrowException
422 
428  public abstract void WriteThrowException(int indent, DynamicExceptionManager dynException);
429 
436  public abstract void WriteThrowException(int indent, string ex, string[] msg);
437 
438  #endregion
439 
440  #region Box and Unbox
441 
442  #region Box
443 
449  public abstract void Box(int indent, TypeExpression type);
455  public abstract void BoxIfNeeded(int indent, TypeExpression type);
456  #endregion
457 
458  #region UnboxAny
459 
460  // Unbox <token>
461  // Revert a boxed value type instance from the object form to its value type form. <token> specifies the value type being converted and must be a valid TypeDef or TypeRef token.
462  // This instruction takes an object reference from the stack and puts a managed pointer to the value type instance on the stack.
463 
469  public abstract void UnboxAny(int indent, TypeExpression type);
470 
471  #endregion
472 
473  #region Unbox
474 
475  // Unbox <token>
476  // Revert a boxed value type instance from the object form to its value type form. <token> specifies the value type being converted and must be a valid TypeDef or TypeRef token.
477  // This instruction takes an object reference from the stack and puts a managed pointer to the value type instance on the stack.
478 
484  public abstract void Unbox(int indent, TypeExpression type);
485  #endregion
486 
487  #endregion
488 
489  #region CallVirt <token>
490  // CallVirt <token>
491  // Call the virtual method specified by <token>.
492 
501  public abstract void CallVirt(int indent, MethodType memberType, TypeExpression klass, string member, AST.CompoundExpression arguments);
502 
511  public abstract void CallVirt(int indent, PropertyType memberType, TypeExpression obj, string member, bool setProperty);
512 
522  public abstract void CallVirt(int indent, string methodType, string result, string klass, string memberName, string[] args);
523 
524  #endregion
525 
526  #region call <token>
527  // call <token>
528  // Call a nonvirtual method.
529 
537  public abstract void Call(int indent, MethodType memberType, TypeExpression obj, string member);
538 
547 
548  public abstract void Call(int indent, PropertyType memberType, TypeExpression obj, string member, bool setProperty);
549 
559  public abstract void Call(int indent, string methodType, string result, string klass, string memberName, string[] args);
560 
561  #endregion
562 
563  #region Close()
564 
568  public void Close() {
569  this.output.Close();
570  }
571 
572  #endregion
573  #region MakeCall()
574 
575  public virtual TypeExpression MakeCall(int indent, InvocationExpression node, Object o, MethodType actualMethodCalled, FieldAccessExpression fieldAccessExpression, object arg) {
576  // shortcut evaluation. Null pointed error suprimed
577  if (o is SynthesizedAttributes && ((SynthesizedAttributes)o).IdentifierExpressionMode == IdentifierMode.Instance)
578  // * 1.1.1 The implicit parameter is an object (not a class)
579  this.CallVirt(indent, actualMethodCalled, actualMethodCalled.MemberInfo.Class, fieldAccessExpression.FieldName.Identifier, node.Arguments);
580  else if (actualMethodCalled.MemberInfo.Modifiers.Contains(Modifier.Abstract))
581  this.CallVirt(indent, actualMethodCalled, actualMethodCalled.MemberInfo.Class, fieldAccessExpression.FieldName.Identifier, node.Arguments);
582  else
583  // * 1.1.2 The implicit parameter is a class (not an object)
584  this.Call(indent, actualMethodCalled, actualMethodCalled.MemberInfo.Class, actualMethodCalled.MemberInfo.MemberIdentifier);
585 
586  return actualMethodCalled.Return;
587  }
588 
589  #endregion
590 
591  }
592 }
abstract void WriteEndOfClass(int indent, string name)
Writes the class termination
abstract void BoxIfNeeded(int indent, TypeExpression type)
Makes sure to convert a type to an object
abstract void WriteLNMethodHeader(int indent, string name, MethodType type)
Writes the method header.
abstract void WriteCodeOfExceptionsTemplateMethod(DynamicExceptionManager d)
Algorithm used in WriteCodeException
abstract void WriteLocalVariable(int indent)
Writes the information of local variables.
Encapsulates the expression to access a field.
virtual void WriteLNFieldInitialization(string init)
Writes the field inicialization expression.
abstract void WriteParams(MethodType memberType, AST.CompoundExpression arguments)
Writes the parameters of a method
abstract void Comment(string msg)
Writes the specified message stored in msg in a comment wrapped in the proper format of the target la...
abstract void WriteEndOfInterface(int indent, string name)
Writes the interface termination
void Close()
Close the writer
abstract void WriteNamespaceHeader(int indent, string name)
Writes the namespace header.
abstract void WriteOpenBraceCatch(int indent)
abstract void WriteFinally(int indent)
Encapsulates a invocation expression.
abstract void Unbox(int indent, TypeExpression type)
Writes the Unbox instruction
abstract string NewLabel
Gets the value of the current label
abstract void WriteStartBlock(int indent)
Writes the class inicialization
virtual void WriteCodeOfExceptions()
Implements Template Method Pattern Writes the intermediate code for each exceptions to include ...
abstract void AddLocalVariable(string name, TypeExpression type)
This class encapsulates the IL code to generate a dynamic exception.
Represents a interface type.
abstract void InitialComment()
abstract void Box(int indent, TypeExpression type)
Writes the Box instruction
Abstract class that represents all different types.
abstract void WriteThrowException(int indent, DynamicExceptionManager dynException)
Checks if the type variable on the stack is a specified type or can be to promotion. Indentation levelThe type expression to promote to
abstract void WriteCatch(int indent, String type, String var)
abstract void WriteCloseBraceTry(int indent)
System.Text.StringBuilder StringBuilder
Definition: CSharpParser.cs:4
This class encapsulates the instruction used to generate the code.
abstract void Call(int indent, MethodType memberType, TypeExpression obj, string member)
Writes the call instruction.
virtual TypeExpression MakeCall(int indent, InvocationExpression node, Object o, MethodType actualMethodCalled, FieldAccessExpression fieldAccessExpression, object arg)
abstract void WriteInterfaceHeader(int indent, string name, InterfaceType type)
Writes the interface header
abstract void WriteOpenBraceFinally(int indent)
IdentifierMode
Represents how to use the identifier
CodeGenerator(TextWriter writer)
Constructor of CodeGenerator.
abstract void WriteHeader(string fileName)
Writes the header of the object code file.
CompoundExpression Arguments
Gets the arguments to invoke in the expression
TextWriter output
Writer to write the intermediate code.
abstract void WriteOpenBraceTry(int indent)
abstract void WriteLNClassHeader(int indent, string name, ClassType type)
Writes the class header.
abstract void WriteType(TypeExpression type)
Writes the current type information
Modifier
Indicates differents modifiers to use in class (only public, internal or static), fields or methods...
abstract void WriteEndOfField()
Writes the field termination
abstract void WriteTryDirective(int indent)
Representa a property type.
Definition: PropertyType.cs:34
abstract void WriteAuxiliarLocalVariable(int indent, string id, string type)
Writes the information of local variables.
Representa a method type.
Definition: MethodType.cs:37
abstract void WriteEndOfMethod(int indent, string name)
Writes the method termination
abstract void UnboxAny(int indent, TypeExpression type)
Writes the Unbox instruction
abstract void WriteCloseBraceFinally(int indent)
Dictionary< string, DynamicExceptionManager > exceptions
List of exceptions that is necessary to include in intermediate code file.
StringBuilder currentLocalVars
Stores the current information about local variables.
This class encapsulates several inherited attributes used in code generation process.
abstract void CallVirt(int indent, MethodType memberType, TypeExpression klass, string member, AST.CompoundExpression arguments)
Writes the CallVirt instruction.
abstract void WriteEndOfBlock(int indent)
Writes the termination token
string Identifier
Gets the name name.
Represents a class type.
Definition: ClassType.cs:35
Representa a field type.
Definition: FieldType.cs:37
SingleIdentifierExpression FieldName
Gets the name to the field.
abstract void WriteField(int indent, string name, FieldType type, bool constantField)
Writes the field header.
abstract void WriteLNFieldInitialization(TypeExpression type)
Writes the field inicialization.
abstract void WriteLabel(int indent, string label)
Writes the specified label.
abstract void WriteCloseBraceCatch(int indent)
int currentLabel
Represents the value of the current label.