2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Microsoft.JScript / Microsoft.JScript / VariableDeclaration.cs
index 19b0b611c20a130c0f38101097a0bec0f7b2cd04..fc612ec8f0ece675427cfd1b084a35c3704a088d 100644 (file)
 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
 //
 
-namespace Microsoft.JScript.Tmp
-{
-       using System.Text;
-
-       public class VariableDeclaration : Statement
-       {
-               private string id;
-               private string type;
-               private AST assignExp;
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 
-               internal VariableDeclaration ()
-               {}
+using System;
+using System.Text;
+using System.Reflection;
+using System.Reflection.Emit;
 
+namespace Microsoft.JScript {
+       
+       public class VariableDeclaration : AST {
 
-               public string Id {
-                       get { return id; }
-                       set { id = value; }
-               }
+               internal string id;
+               internal Type type;
+               internal string type_annot = String.Empty;
+               internal AST val;
 
-               
-               public string Type {
-                       get { return type; }
-                       set { type = value; }
-               }
+               internal FieldInfo field_info;
+               internal LocalBuilder local_builder;
 
-
-               internal new object Visit (Visitor v, object args)
+               internal VariableDeclaration (AST parent, string id, string t, AST init)
                {
-                       return v.VisitVariableDeclaration (this, args);
-               }
-
+                       this.parent = parent;
+                       this.id = id;
 
+                       if (t == null)
+                               this.type = typeof (System.Object);
+                       else {
+                               this.type_annot = t;
+                               // FIXME: resolve the type annotations
+                               this.type = typeof (System.Object);
+                       }
+                       this.val = init;
+               }
+               
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ();
 
-                       // FIXME: we must add the string 
-                       // representation of assignExp, too.
-                       sb.Append (Id);
-                       
+                       sb.Append (id);
+                       if (type_annot != String.Empty) {                               
+                               sb.Append (":" + type_annot);
+                               sb.Append (" = ");
+                       }
+
+                       if (val != null)
+                               sb.Append (val.ToString ());
+
                        return sb.ToString ();
                }
+
+               internal override void Emit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       if (parent == null || (parent.GetType () != typeof (FunctionDeclaration)
+                                              && parent.GetType () != typeof (FunctionExpression))) {
+                               FieldBuilder field_builder;
+                               TypeBuilder type  = ec.type_builder;
+                               
+                               field_builder = type.DefineField (id, this.type, FieldAttributes.Public | FieldAttributes.Static);
+                               field_info = field_builder;
+
+                               if (val != null) {
+                                       val.Emit (ec);
+                                       ig.Emit (OpCodes.Stsfld, field_builder);
+                               }
+                       } else {
+                               local_builder = ig.DeclareLocal (type);
+                               
+                               if (val != null) {
+                                       val.Emit (ec);
+                                       ig.Emit (OpCodes.Stloc, local_builder);
+                               }
+                       }
+               }
+
+               internal override bool Resolve (IdentificationTable context)
+               {
+                       bool r = true;
+                       if (val != null)
+                               r = val.Resolve (context);
+                       context.Enter (id, this);
+                       return r;
+               }
        }
 }