2004-01-16 Cesar Lopez Nataren <cesar@ciencias.unam.mx>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / VariableDeclaration.cs
1 //
2 // VariableDeclaration.cs: The AST representation of a VariableDeclaration.
3 //
4 // Author:
5 //      Cesar Octavio Lopez Nataren
6 //
7 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
8 //
9
10 using System;
11 using System.Text;
12 using System.Reflection;
13 using System.Reflection.Emit;
14
15 namespace Microsoft.JScript {
16
17         public abstract class Decl : AST {
18                 internal FieldInfo field_info;
19                 internal LocalBuilder local_builder;
20         }
21
22         public class VariableDeclaration : Decl {
23
24                 internal string id;
25                 internal Type type;
26                 internal string type_annot;
27                 internal AST val;
28
29                 internal VariableDeclaration (AST parent, string id, string t, AST init)
30                 {
31                         this.parent = parent;
32                         this.id = id;
33
34                         if (t == null)
35                                 this.type = typeof (System.Object);
36                         else {
37                                 this.type_annot = t;
38                                 // FIXME: resolve the type annotations
39                                 this.type = typeof (System.Object);
40                         }
41                         this.val = init;
42                 }
43
44                 public override string ToString ()
45                 {
46                         StringBuilder sb = new StringBuilder ();
47
48                         sb.Append (id);
49                         sb.Append (":" + type_annot);
50                         sb.Append (" = ");
51
52                         if (val != null)
53                                 sb.Append (val.ToString ());
54
55                         return sb.ToString ();
56                 }
57
58                 internal override void Emit (EmitContext ec)
59                 {
60                         if (parent == null) {
61                                 FieldBuilder field;
62                                 TypeBuilder type  = ec.type_builder;
63                                 
64                                 field = type.DefineField (id, this.type,
65                                                           FieldAttributes.Public |
66                                                           FieldAttributes.Static);
67                                 
68                                 field_info = CodeGenerator.assembly_builder.GetType ("JScript 0").GetField (id);
69
70                                 if (val != null) {
71                                         val.Emit (ec);
72                                         ec.gc_ig.Emit (OpCodes.Stsfld, field);
73                                 }
74                         } else {
75                                 ILGenerator ig = ec.ig;
76                                 local_builder = ig.DeclareLocal (type);
77
78                                 if (val != null) {
79                                         val.Emit (ec);
80                                         ig.Emit (OpCodes.Stloc, local_builder);
81                                 }
82                         }
83                 }
84
85                 internal override bool Resolve (IdentificationTable context)
86                 {
87                         bool r = true;
88                         if (val != null)
89                                 if (val is Exp) 
90                                         r = ((Exp) val).Resolve (context, false);
91                                 else
92                                         r = val.Resolve (context);
93                         context.Enter (id, this);
94                         return r;
95                 }
96         }
97 }