* VariableDeclaration.cs: Deleted Decl class, field_info and
[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 class VariableDeclaration : AST {
18
19                 internal string id;
20                 internal Type type;
21                 internal string type_annot;
22                 internal AST val;
23
24                 internal FieldInfo field_info;
25                 internal LocalBuilder local_builder;
26
27                 internal VariableDeclaration (AST parent, string id, string t, AST init)
28                 {
29                         this.parent = parent;
30                         this.id = id;
31
32                         if (t == null)
33                                 this.type = typeof (System.Object);
34                         else {
35                                 this.type_annot = t;
36                                 // FIXME: resolve the type annotations
37                                 this.type = typeof (System.Object);
38                         }
39                         this.val = init;
40                 }
41
42                 public override string ToString ()
43                 {
44                         StringBuilder sb = new StringBuilder ();
45
46                         sb.Append (id);
47                         sb.Append (":" + type_annot);
48                         sb.Append (" = ");
49
50                         if (val != null)
51                                 sb.Append (val.ToString ());
52
53                         return sb.ToString ();
54                 }
55
56                 internal override void Emit (EmitContext ec)
57                 {
58                         ILGenerator ig = ec.ig;
59
60                         if (parent == null) {                           
61                                 FieldBuilder field_builder;
62                                 TypeBuilder type  = ec.type_builder;
63                                 
64                                 field_builder = type.DefineField (id, this.type, FieldAttributes.Public | FieldAttributes.Static);
65                                 field_info = field_builder;
66
67                                 if (val != null) {
68                                         val.Emit (ec);
69                                         ig.Emit (OpCodes.Stsfld, field_builder);
70                                 }
71                         } else {
72                                 local_builder = ig.DeclareLocal (type);
73                                 
74                                 if (val != null) {
75                                         val.Emit (ec);
76                                         ig.Emit (OpCodes.Stloc, local_builder);
77                                 }
78                         }
79                 }
80
81                 internal override bool Resolve (IdentificationTable context)
82                 {
83                         bool r = true;
84                         if (val != null)
85                                 r = val.Resolve (context);
86                         context.Enter (id, this);
87                         return r;
88                 }
89         }
90 }