2005-10-11 Cesar Lopez Nataren <cnataren@novell.com>
authorCésar Natarén <cesar@mono-cvs.ximian.com>
Tue, 11 Oct 2005 22:14:11 +0000 (22:14 -0000)
committerCésar Natarén <cesar@mono-cvs.ximian.com>
Tue, 11 Oct 2005 22:14:11 +0000 (22:14 -0000)
* Add support for multiple files compilation. This involved addig
an extra pass into the AST for ensuring the resolve process, this
pass is named PopulateContext. The AST nodes that implement
ICanModifyContext call PopulateContext.

* IdentificationTable.cs: Added class Environment, which is a
symbol table which takes into account namespace info, this is for
package statement future implementation.

svn path=/trunk/mcs/; revision=51640

44 files changed:
mcs/class/Microsoft.JScript/Microsoft.JScript.Vsa/ChangeLog
mcs/class/Microsoft.JScript/Microsoft.JScript.Vsa/VsaEngine.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ASTList.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ArrayLiteral.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/BitwiseBinary.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Block.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/DebugBreak.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Decompiler.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Enum.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Equality.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Eval.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ForIn.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/FormalParameterList.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionExpression.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionObject.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Import.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/In.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/InstanceOf.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Literal.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/NumericBinary.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/NumericUnary.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Package.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Parser.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Plus.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/PostOrPrefixOperator.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Print.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Relational.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ScriptBlock.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/SemanticAnalizer.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Statement.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/StrictEquality.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/StringLiteral.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Throw.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/Try.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/TypeOf.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/With.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/ast.cs
mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs

index d83e2bb3dc5a1c48e92d8e8c1f6dee4123602b67..df2592b0a3dc850c25b0e2093d0405cb51374c96 100644 (file)
@@ -1,3 +1,7 @@
+2005-10-11  Cesar Lopez Nataren  <cnataren@novell.com>
+
+       * VsaEngine.cs: Parse all the files.
+
 2005-09-13  Cesar Lopez Nataren  <cnataren@novell.com>
 
        * VsaEngine.cs: Override method Compile and SetOption from BaseVsaEngine,
index 2ae89769529a237b597ae3423b3002002a68e242..2e3db77b058b55251a98f5c1e6d61ffad5228b01 100644 (file)
@@ -76,10 +76,10 @@ namespace Microsoft.JScript.Vsa {
                                        throw new Exception ("FIXME: VsaItemType.AppGlobal");
                        }
                        Parser parser = new Parser (code_items);
-                       ScriptBlock block = (ScriptBlock) parser.ParseAll ();
-                       if (block != null) {
-                               SemanticAnalyser.Run (block, (Assembly []) GetOption ("assemblies"));
-                               CodeGenerator.Run ((string) GetOption ("first_source"), block);
+                       ScriptBlock [] blocks = parser.ParseAll ();
+                       if (blocks != null) {
+                               SemanticAnalyser.Run (blocks, (Assembly []) GetOption ("assemblies"));
+                               CodeGenerator.Run ((string) GetOption ("first_source"), blocks);
                                Console.WriteLine ("Compilation succeeded");
                        }
                        return false;
index 176ff2bd0a0469a06f8949120627f5c98bc1cf27..696e3542715037a87311d437ecd37ad24ffb56a8 100644 (file)
@@ -55,7 +55,7 @@ namespace Microsoft.JScript {
                        get { return elems.Count; }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
index 904f7db9126569e7cea892bbe88c2a5f168448a6..d53adfbea6ed0ac528856b5ac44f082904422a8f 100644 (file)
@@ -53,12 +53,12 @@ namespace Microsoft.JScript {
                        this.location = location;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                         bool r = true;
                         foreach (AST ast in elems.elems)
                                 if (ast != null)
-                                        r &= ast.Resolve (context);
+                                        r &= ast.Resolve (env);
                        return r;
                }
 
index b4a240f3fd24b161ae9e745d5bb8b5e477ae067d..b8735c6b4d291b9257b0b714c3d9d5e1a4935a29 100644 (file)
@@ -71,12 +71,12 @@ namespace Microsoft.JScript {
                        return (uint) num1 >> num2;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        throw new NotImplementedException ();
                }
index 47e3fcd5aa641208367b7785d0000ab889f95544..5d3d443b0e22f3a54e91f339a86659fbe5bb3bc8 100644 (file)
@@ -33,16 +33,14 @@ using System;
 
 namespace Microsoft.JScript {
 
-       public class Block : AST {
+       public class Block : AST, ICanModifyContext {
 
                internal ArrayList elems;
-               private Hashtable ocurrences;
 
                internal Block (AST parent, Location location)
                        : base (parent, location)
                {
                        elems = new ArrayList ();
-                       ocurrences = new Hashtable ();
                }
 
                internal void Add (AST e)
@@ -51,44 +49,63 @@ namespace Microsoft.JScript {
                                elems.Add (e);
                }
 
-               internal override void Emit (EmitContext ec)
+               void ICanModifyContext.EmitDecls (EmitContext ec)
                {
-                       int i, n = elems.Count;
-                       object e;
-
                        //
-                       // Emit variable declarations first
-                       // because of posible free occurrences inside
-                       // a method. 
+                       // Emit variable declarations and function's closure first
+                       // because of posible free occurrences inside a method. 
                        //
-                       for (i = 0; i < n; i++) {
-                               e = elems [i];
-                               if (e is VariableStatement)
-                                       ((VariableStatement) e).EmitVariableDecls (ec);
-                       }
+                       foreach (AST ast in elems)
+                               if (ast is FunctionDeclaration)
+                                       ((FunctionDeclaration) ast).create_closure (ec);
+                               else if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+               }
+
+               internal override void Emit (EmitContext ec)
+               {
+                       int n = elems.Count;
+                       object e;
 
-                       //
-                       // Emit the function closure before any
-                       // expression because the ScriptFunction and
-                       // field created must be set properly before
-                       // any use. The body gets emitted later.
-                       //                      
-                       for (i = 0; i < n; i++) {
-                               e = elems [i];
-                               if (e is FunctionDeclaration)
-                                       ((FunctionDeclaration) e).create_closure (ec);
-                       }
-                       
                        //
                        // Emit the rest of expressions and statements.
                        //
-                       for (i = 0; i < n; i++) {
+                       for (int i = 0; i < n; i++) {
                                e = elems [i];
                                ((AST) e).Emit (ec);
                        }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       AST ast;
+                       for (int i = 0; i < elems.Count; i++) {
+                               ast = (AST) elems [i];
+                               if (ast is FunctionDeclaration) {
+                                       string name = ((FunctionDeclaration) ast).func_obj.name;
+                                       AST binding = (AST) env.Get (ns, Symbol.CreateSymbol (name));
+
+                                       if (binding == null)
+                                               SemanticAnalyser.Ocurrences.Enter (ns, Symbol.CreateSymbol (name), new DeleteInfo (i, this));
+                                       else {
+                                               DeleteInfo delete_info = (DeleteInfo) SemanticAnalyser.Ocurrences.Get (ns, Symbol.CreateSymbol (name));
+                                               if (delete_info != null) {
+                                                       delete_info.Block.elems.RemoveAt (delete_info.Index);
+                                                       SemanticAnalyser.Ocurrences.Remove (ns, Symbol.CreateSymbol (name));
+                                                       if (delete_info.Block == this)
+                                                               if (delete_info.Index < i)
+                                                                       i--;
+
+                                                       SemanticAnalyser.Ocurrences.Enter (ns, Symbol.CreateSymbol (name), new DeleteInfo (i, this));
+                                               }
+                                       }
+                               }
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+                       }
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        AST e;
                        bool no_effect;
@@ -100,56 +117,35 @@ namespace Microsoft.JScript {
                        else
                                no_effect = false;
                        
-                       for (i = 0; i < elems.Count; i++) {
-                               e = (AST) elems [i];
-                               //
-                               // Add the variables to the symbol
-                               // tables. If a variable declaration
-                               // has an initializer we postpone the
-                               // resolve process of the initializer
-                               // until we have collected all the
-                               // variable declarations. 
-                               //
-                               if (e is VariableStatement)
-                                       (e as VariableStatement).PopulateContext (context);
-                               else if (e is FunctionDeclaration) {
-                                       //
-                                       // In the case of function
-                                       // declarations we add
-                                       // function's name to the
-                                       // table but we resolve its
-                                       // body until later, as free
-                                       // variables can be referenced
-                                       // in function's body.
-                                       //
-                                       string name = ((FunctionDeclaration) e).func_obj.name;
-                                       AST binding = (AST) context.Get (Symbol.CreateSymbol (name));
-
-                                       if (binding == null) {
-                                               ocurrences.Add (name, i);
-                                               context.Enter (Symbol.CreateSymbol (((FunctionDeclaration) e).func_obj.name), new FunctionDeclaration ());
-                                       } else {
-                                               Console.WriteLine ("warning: JS1111: '{0}' has already been defined.", name);
-                                               if (!(binding is FunctionDeclaration))
-                                                       throw new Exception ("error JS5040: '" + ((VariableDeclaration) binding).id + "' it's read only.");
-                                               int k = (int) ocurrences [name];
-                                               elems.RemoveAt (k);
-                                               if (k < i)
-                                                       i -= 1;
-                                               ocurrences [name] = i;
-                                       }
-                               }
-                       }                       
                        n = elems.Count;
                        
                        for (i = 0; i < n; i++) {
                                e = (AST) elems [i];
                                if (e is Exp) 
-                                       r &= ((Exp) e).Resolve (context, no_effect);
-                               else
-                                       r &= e.Resolve (context);
+                                       r &= ((Exp) e).Resolve (env, no_effect);
+                               else 
+                                       r &= e.Resolve (env);
                        }
-                       return r;                       
+                       return r;
+               }
+       }
+
+       internal class DeleteInfo {
+               private int index;
+               private Block block;
+
+               internal DeleteInfo (int index, Block block)
+               {
+                       this.index = index;
+                       this.block = block;
+               }
+
+               internal int Index {
+                       get { return index; }
+               }
+
+               internal Block Block {
+                       get { return block; }
                }
        }
 }
index 4a3c66c762e428c0870a584fc72bddc307ba3132..2e08b16ece79ef7d8e4e8e058e1f3e6a57337710 100644 (file)
@@ -1,3 +1,14 @@
+2005-10-11  Cesar Lopez Nataren  <cnataren@novell.com>
+
+       * Add support for multiple files compilation. This involved addig
+       an extra pass into the AST for ensuring the resolve process, this
+       pass is named PopulateContext. The AST nodes that implement
+       ICanModifyContext call PopulateContext.
+
+       * IdentificationTable.cs: Added class Environment, which is a
+       symbol table which takes into account namespace info, this is for
+       package statement future implementation.
+
 2005-09-30  Cesar Lopez Nataren  <cnataren@novell.com>
 
        * expression.cs (Expression::Resolve): check if we have an
index ee13b93a4583616a430e6c3c92a88e6774590aa8..0a57a1e7b02d54acfa7a07f21d23947ade8b4151 100644 (file)
@@ -35,7 +35,6 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Threading;
 using Microsoft.JScript.Vsa;
-using System.Runtime.CompilerServices;
 using System.Collections;
 
 namespace Microsoft.JScript {
@@ -45,23 +44,10 @@ namespace Microsoft.JScript {
                internal TypeBuilder type_builder;
                internal ILGenerator ig;
                internal ModuleBuilder mod_builder;
+               internal MethodBuilder global_code;
 
                internal Label LoopBegin, LoopEnd;
 
-               internal EmitContext (TypeBuilder type)
-               {
-                       type_builder = type;
-
-                       if (type_builder != null) {
-                               MethodBuilder global_code =  type_builder.DefineMethod (
-                                                                       "Global Code",
-                                                                       MethodAttributes.Public,
-                                                                       typeof (System.Object),
-                                                                       new Type [] {});
-                               ig = global_code.GetILGenerator ();
-                       }
-               }
-
                internal EmitContext (TypeBuilder type_builder, ModuleBuilder mod_builder, ILGenerator ig)
                {
                        this.type_builder = type_builder;
@@ -80,6 +66,14 @@ namespace Microsoft.JScript {
                internal static AssemblyName assembly_name;
                internal static AssemblyBuilder assembly_builder;
                internal static ModuleBuilder module_builder;
+               private static int next_type = 0;
+               private static ArrayList global_types = new ArrayList ();
+               private static Hashtable global_methods = new Hashtable ();
+               private static Hashtable source_file_to_type = new Hashtable ();
+
+               private static string NextType {
+                       get { return "JScript " + next_type++; }
+               }
 
                internal static string Basename (string name)
                {
@@ -146,75 +140,21 @@ namespace Microsoft.JScript {
                        assembly_builder.Save (CodeGenerator.Basename (target_name));
                }
 
-               internal static void Emit (AST prog)
+               internal static void EmitDecls (ScriptBlock prog)
                {
                        if (prog == null)
                                return;
 
-                       TypeBuilder type_builder;
-                       type_builder = module_builder.DefineType ("JScript 0", TypeAttributes.Public);
-
-                       type_builder.SetParent (typeof (GlobalScope));
-                       type_builder.SetCustomAttribute (new CustomAttributeBuilder
-                                                        (typeof (CompilerGlobalScopeAttribute).GetConstructor (new Type [] {}), new object [] {}));
+                       string next_type = CodeGenerator.NextType;
 
-                       EmitContext ec = new EmitContext (type_builder);
-                       ec.mod_builder = module_builder;
-                       ILGenerator global_code = ec.ig;
+                       prog.InitTypeBuilder (module_builder, next_type);
+                       prog.InitGlobalCode ();
 
-                       emit_default_script_constructor (ec);
-                       emit_default_init_global_code (global_code);
-                       prog.Emit (ec);
-                       emit_default_end_global_code (global_code);
-                       ec.type_builder.CreateType ();
-
-                       //
-                       // Build the default 'JScript Main' class
-                       //
-                       ec.type_builder = module_builder.DefineType ("JScript Main");
-                       emit_jscript_main (ec.type_builder);
-                       ec.type_builder.CreateType ();
-               }
-
-               internal static void emit_default_init_global_code (ILGenerator ig)
-               {
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Call,
-                               typeof (VsaEngine).GetMethod ("PushScriptObject",
-                                                             new Type [] { typeof (ScriptObject)}));
-               }
-
-               internal static void emit_default_end_global_code (ILGenerator ig)
-               {
-                       ig.Emit (OpCodes.Ldnull);
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
-                       ig.Emit (OpCodes.Call, typeof (VsaEngine).GetMethod ("PopScriptObject"));
-                       ig.Emit (OpCodes.Pop);
-                       ig.Emit (OpCodes.Ret);          
-               }
-
-               internal static void emit_default_script_constructor (EmitContext ec)
-               {
-                       ConstructorBuilder cons_builder;
-                       TypeBuilder tb = ec.type_builder;
-                       cons_builder = tb.DefineConstructor (MethodAttributes.Public,
-                                                            CallingConventions.Standard,
-                                                            new Type [] { typeof (GlobalScope) });
+                       global_types.Add (next_type);
+                       global_methods.Add (next_type, prog.GlobalCode);
+                       source_file_to_type.Add (prog.Location.SourceName, next_type);
 
-                       ILGenerator ig = cons_builder.GetILGenerator ();
-                       ig.Emit (OpCodes.Ldarg_0);
-                       ig.Emit (OpCodes.Ldarg_1);
-                       ig.Emit (OpCodes.Dup);
-                       ig.Emit (OpCodes.Ldfld,
-                                typeof (ScriptObject).GetField ("engine"));
-                       
-                       ig.Emit (OpCodes.Call, 
-                                typeof (GlobalScope).GetConstructor (new Type [] {typeof (GlobalScope), 
-                                                                                  typeof (VsaEngine)}));
-                       ig.Emit (OpCodes.Ret);
+                       prog.EmitDecls (module_builder);
                }
 
                internal static void emit_jscript_main (TypeBuilder tb)
@@ -265,24 +205,48 @@ namespace Microsoft.JScript {
                                                               new Type [] {typeof (bool), 
                                                                            typeof (string [])}));      
                        ig.Emit (OpCodes.Stloc_0);
-                       ig.Emit (OpCodes.Ldloc_0);
-                       
-                       ig.Emit (OpCodes.Newobj,
-                                assembly_builder.GetType ("JScript 0").GetConstructor (
-                                                                       new Type [] {typeof (GlobalScope)})); 
-                       ig.Emit (OpCodes.Call, 
-                                assembly_builder.GetType ("JScript 0").GetMethod (
-                                                                          "Global Code", new Type [] {}));
-                       ig.Emit (OpCodes.Pop);
+
+                       foreach (string type_name in global_types) {
+                               ig.Emit (OpCodes.Ldloc_0);
+                               ig.Emit (OpCodes.Newobj, assembly_builder.GetType (type_name).GetConstructor (
+                                                                             new Type [] {typeof (GlobalScope)})); 
+                               ig.Emit (OpCodes.Call, (MethodInfo) global_methods [type_name]);
+                               ig.Emit (OpCodes.Pop);
+                       }
                        ig.Emit (OpCodes.Ret);
 
                        assembly_builder.SetEntryPoint (method);
                }
 
-               public static void Run (string file_name, AST prog)
+               public static void Run (string file_name, ScriptBlock [] blocks)
                {
                        CodeGenerator.Init (file_name);
-                       CodeGenerator.Emit (prog);
+
+                       //
+                       // Emit first all the declarations (function and variables)
+                       //
+                       foreach (ScriptBlock script_block in blocks)
+                               CodeGenerator.EmitDecls (script_block);
+
+                       //
+                       // emit everything that's not a declaration
+                       //
+                       foreach (ScriptBlock script_block in blocks)
+                               script_block.Emit ();
+
+                       //
+                       // Create the types ('JScript N')
+                       //
+                       foreach (ScriptBlock script_block in blocks)
+                               script_block.CreateType ();
+                       
+                       //
+                       // Build the default 'JScript Main' class
+                       //
+                       TypeBuilder main_type_builder = module_builder.DefineType ("JScript Main");
+                       emit_jscript_main (main_type_builder);
+                       main_type_builder.CreateType ();
+
                        CodeGenerator.Save (trim_extension (file_name) + ".exe");
                }
 
@@ -316,6 +280,13 @@ namespace Microsoft.JScript {
                                        ig.Emit (OpCodes.Conv_R8);
                                        ig.Emit (OpCodes.Blt, lbl);
                                        break;
+
+                               default:
+                                       ast.Emit (ec);
+                                       ig.Emit (OpCodes.Ldc_I4_1);
+                                       ig.Emit (OpCodes.Call, typeof (Convert).GetMethod ("ToBoolean", new Type [] {typeof (object), typeof (bool)}));
+                                       ig.Emit (OpCodes.Brfalse, lbl);
+                                       break;
                                }
                        }
                }
@@ -347,9 +318,9 @@ namespace Microsoft.JScript {
                {
                        Type type = ast.GetType ();
 
-                       if (type == typeof (Expression)) {  
-                               Expression exp = ast as Expression;                             
-                               AST last_exp = last_exp = (AST) exp.exprs [exp.exprs.Count - 1];
+                       if (type == typeof (Expression)) {
+                               Expression exp = ast as Expression;
+                               AST last_exp = (AST) exp.exprs [exp.exprs.Count - 1];
                                if (exp.exprs.Count >= 2)
                                        exp.Emit (ec);
                                fall_true (ec, last_exp, lbl);
@@ -357,6 +328,8 @@ namespace Microsoft.JScript {
                                ft_binary_recursion (ec, ast, lbl);
                        else if (type == typeof (Equality) || type == typeof (StrictEquality))
                                ft_emit_equality (ec, ast, lbl);
+                       else if (type == typeof (Relational))
+                               ft_emit_relational (ec, (Relational) ast, lbl);
                        else
                                emit_default_case (ec, ast, OpCodes.Brfalse, lbl);
                }
@@ -367,13 +340,68 @@ namespace Microsoft.JScript {
                        Relational r = ast as Relational;
                        r.Emit (ec);
 
+                       OpCode opcode;
+
                        switch (r.op) {
                        case JSToken.LessThan:
-                               ig.Emit (OpCodes.Ldc_I4_0);
-                               ig.Emit (OpCodes.Conv_R8);
-                               ig.Emit (OpCodes.Blt, lbl);
+                               opcode = OpCodes.Blt;
+                               break;
+
+                       case JSToken.GreaterThan:
+                               opcode = OpCodes.Bgt;
+                               break;
+
+                       case JSToken.LessThanEqual:
+                               opcode = OpCodes.Ble;
+                               break;
+
+                       case JSToken.GreaterThanEqual:
+                               opcode = OpCodes.Bge;
+                               break;
+
+                       default:
+                               throw new Exception ("unexpected token");
+                       }
+
+                       ig.Emit (OpCodes.Ldc_I4_0);
+                       ig.Emit (OpCodes.Conv_R8);
+                       ig.Emit (opcode, lbl);
+               }
+
+               static void ft_emit_relational (EmitContext ec, Relational re, Label lbl)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       re.Emit (ec);
+                       JSToken op = re.op;
+                       
+                       OpCode opcode;
+
+                       switch (op) {
+                       case JSToken.LessThan:
+                               opcode = OpCodes.Bge_Un;
                                break;
+
+                       case JSToken.GreaterThan:
+                               opcode = OpCodes.Ble_Un;
+                               break;
+
+                       case JSToken.LessThanEqual:
+                               opcode = OpCodes.Bgt_Un;
+                               break;
+
+                       case JSToken.GreaterThanEqual:
+                               opcode = OpCodes.Blt_Un;
+                               break;
+
+                       default:
+                               Console.WriteLine (re.Location.LineNumber);
+                               throw new NotImplementedException ();
                        }
+
+                       ig.Emit (OpCodes.Ldc_I4_0);
+                       ig.Emit (OpCodes.Conv_R8);
+                       ig.Emit (opcode, lbl);
                }
 
                static void ff_binary_recursion (EmitContext ec, AST ast, Label lbl)
@@ -432,10 +460,12 @@ namespace Microsoft.JScript {
                                        ff_emit_equality_cond (ec, last_exp, lbl);
                                else {
                                        Console.WriteLine ("fall_false, last_exp.GetType () == {0}", last_exp);
-                                       throw new Exception ("uknown type: " + last_exp.GetType ().ToString ());
+                                       throw new Exception ("unknown type: " + last_exp.GetType ().ToString ());
                                }
                        } else if (type == typeof (Binary))
                                ff_binary_recursion (ec, ast, lbl);
+                       else if (type == typeof (Relational))
+                               ff_emit_relational (ec, ast, lbl);
                        else 
                                emit_default_case (ec, ast, OpCodes.Brtrue, lbl);
                }
@@ -678,6 +708,7 @@ namespace Microsoft.JScript {
                                Console.WriteLine (re.Location.LineNumber);
                                throw new NotImplementedException ();
                        }
+
                        ig.Emit (opcode, true_case);
                        ig.Emit (OpCodes.Ldc_I4_0);
                        ig.Emit (OpCodes.Br, box_to_bool);
@@ -686,5 +717,10 @@ namespace Microsoft.JScript {
                        ig.MarkLabel (box_to_bool);
                        ig.Emit (OpCodes.Box, typeof (bool));
                }
+
+               internal static string GetTypeName (string srcName)
+               {
+                       return (string) source_file_to_type [srcName];
+               }
        }
 }
index 77ef70b7834d98e5101f1fc5b05c38d6bf6e9993..9554eef5e803ed4d51e1e0cce245b0bf2672f028 100644 (file)
@@ -38,7 +38,7 @@ namespace Microsoft.JScript {
                {
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
index 84a879a72747aad46b7014cb25bd1570ff7b45a8..b4ccaff0c710692e4dced0dcbb05513dc127d2e8 100644 (file)
@@ -247,7 +247,6 @@ internal class Decompiler {
                        newCapacity = minimalCapacity;
 
                char [] tmp = new char [newCapacity];
-               // System.arraycopy (sourceBuffer, 0, tmp, 0, sourceTop);
                Array.Copy (sourceBuffer, 0, tmp, 0, sourceTop);
                sourceBuffer = tmp;
        }
index 2b61365c4bcf828c855b7f752a954b06e224c786..bbd7d54a290ae53d0670cf87aeb8e3cede9540d6 100644 (file)
@@ -88,7 +88,7 @@ namespace Microsoft.JScript {
                        return sb.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }                                       
index 33bedcdd7a6753bc987017605110d12222994de4..5fd334f5ca8c2c5e6dc4a0657138685d89868852 100644 (file)
@@ -143,20 +143,20 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (left != null)
-                               r &= left.Resolve (context);
+                               r &= left.Resolve (env);
                        if (right != null)
-                               r &= right.Resolve (context);
+                               r &= right.Resolve (env);
                        return r;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index d69d1d8e24c6ba2006801fdc3aa0bbc1a89cf486..8d22ef98718bfae54fa9b6627dcab6882e408891 100644 (file)
@@ -52,7 +52,7 @@ namespace Microsoft.JScript {
                }
 #endif
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
index ff222fa97b989ebc096ccb3f8fb0d2ccf0d94949..7c2f08d058840344727d6a735e0289a9fc6ee5ce 100644 (file)
@@ -34,7 +34,7 @@ using System.Reflection.Emit;
 
 namespace Microsoft.JScript {
 
-       public class ForIn : AST {
+       public class ForIn : AST, ICanModifyContext {
 
                AST lhs, obj, body;
                
@@ -51,19 +51,37 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (lhs is ICanModifyContext)
+                               ((ICanModifyContext) lhs).PopulateContext (env, ns);
+
+                       if (body is ICanModifyContext)
+                               ((ICanModifyContext) body).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (lhs is ICanModifyContext)
+                               ((ICanModifyContext) lhs).EmitDecls (ec);
+
+                       if (body is ICanModifyContext)
+                               ((ICanModifyContext) body).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (lhs != null)
                                if (lhs is VariableStatement)
-                                       ((VariableStatement) lhs).PopulateContext (context);
+                                       ((ICanModifyContext) lhs).PopulateContext (env, String.Empty);
                                else
-                                       r &= lhs.Resolve (context);
+                                       r &= lhs.Resolve (env);
                        if (obj != null)
-                               r &= obj.Resolve (context);
+                               r &= obj.Resolve (env);
 
                        if (body != null)
-                               r &= body.Resolve (context);
+                               r &= body.Resolve (env);
                        return r;
                }
 
@@ -73,7 +91,6 @@ namespace Microsoft.JScript {
                        
                        if (lhs is VariableStatement) {
                                VariableStatement stm = (VariableStatement) lhs;
-                               stm.EmitVariableDecls (ec);
                                ig = ec.ig;
                                ig.Emit (OpCodes.Ldnull);
                                object var = TypeManager.Get (((VariableDeclaration) stm.var_decls [0]).id);
index 47d27198cd863dfefef9a72186ed31c5e0726d95..fb088254f40eb335238ddf42486a7e3571e228a0 100644 (file)
@@ -62,10 +62,10 @@ namespace Microsoft.JScript {
                internal override void Emit (EmitContext ec)
                {
                }
-       
-               internal override bool Resolve (IdentificationTable context)
+
+               internal override bool Resolve (Environment env)
                {
-                       context.Enter (Symbol.CreateSymbol (id), this);
+                       env.Enter (String.Empty, Symbol.CreateSymbol (id), this);
                        return true;
                }
        }
@@ -83,7 +83,7 @@ namespace Microsoft.JScript {
                internal void Add (string id, string type_annot, Location location)
                {
                        FormalParam p = new FormalParam (id, type_annot, location);
-                       ids.Add (p); 
+                       ids.Add (p);
                }
 
                public override string ToString ()
@@ -96,7 +96,7 @@ namespace Microsoft.JScript {
                        return sb.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        FormalParam f;
                        int i, n = ids.Count;
@@ -104,7 +104,7 @@ namespace Microsoft.JScript {
                        for (i = 0; i < n; i++) {
                                f = (FormalParam) ids [i];
                                f.pos = (byte) (i + 2);
-                               f.Resolve (context);
+                               f.Resolve (env);
                        }
                        return true;
                } 
index 82eeda16e88e04b0a032a29f3a7af743a703a4c3..7a15d0c7f3b08a98e5650910a320128c4a676c36 100644 (file)
@@ -38,7 +38,7 @@ using System.Collections;
 
 namespace Microsoft.JScript {
        
-       public class FunctionDeclaration : Function {
+       public class FunctionDeclaration : Function, ICanModifyContext {
 
                private int lexical_depth;
 
@@ -123,6 +123,8 @@ namespace Microsoft.JScript {
                        TypeManager.BeginScope ();
                        ILGenerator old_ig = ec.ig;
                        ec.ig = this.ig;
+
+                       ((ICanModifyContext) func_obj.body).EmitDecls (ec);
                        func_obj.body.Emit (ec);
 
                        string func_name = func_obj.name;
@@ -148,8 +150,7 @@ namespace Microsoft.JScript {
                {
                        ILGenerator ig = ec.ig;
                        string name = func_obj.name;
-
-                       Type t = ec.mod_builder.GetType ("JScript 0");
+                       Type t = ec.mod_builder.GetType (CodeGenerator.GetTypeName (Location.SourceName));
                        ig.Emit (OpCodes.Ldtoken, t);
                        ig.Emit (OpCodes.Ldstr, name);
                        ig.Emit (OpCodes.Ldstr, full_name);
@@ -225,32 +226,60 @@ namespace Microsoft.JScript {
                                emit_return_local_field (ig, ctr_info, n);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
                {
-                       set_function_type ();
-
                        //
-                       // outer scope has already added the symbol to the
-                       // table but not the correct binding.
+                       // In the case of function
+                       // declarations we add
+                       // function's name to the
+                       // table but we resolve its
+                       // body until later, as free
+                       // variables can be referenced
+                       // in function's body.
                        //
-                       FunctionDeclaration func_decl = (FunctionDeclaration) context.Get (Symbol.CreateSymbol (func_obj.name));
-                       func_decl.Init (this.parent, this.func_obj.name, this.func_obj.parameters, this.func_obj.type_annot, this.func_obj.body);
+                       string name = func_obj.name;
+                       AST binding = (AST) env.Get (String.Empty, Symbol.CreateSymbol (name));
+
+                       if (binding == null)
+                               env.Enter (String.Empty, Symbol.CreateSymbol (name), this);
+                       else if (binding is FunctionDeclaration || binding is VariableDeclaration) {
+                               Console.WriteLine ("{0}({1},0) : warning JS1111: '{2}' is already defined",
+                                          Location.SourceName, Location.LineNumber, name);
+                       }
+
+                       if (binding is VariableDeclaration) {
+                               VariableDeclaration var_decl = (VariableDeclaration) binding;
+                               string error_msg = Location.SourceName + "(" + var_decl.Location.LineNumber + ",0) : "
+                                       + "error JS5040: '" + var_decl.id + "' is read-only";
+                               throw new Exception (error_msg);
+                       }
+               }
 
-                       context.BeginScope ();
-                       lexical_depth = context.depth;
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       ((ICanModifyContext) func_obj.body).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
+               {
+                       set_function_type ();
+                       env.BeginScope (String.Empty);
+                       lexical_depth = env.Depth (String.Empty);
+
+                       ((ICanModifyContext) func_obj).PopulateContext (env, String.Empty);
 
                        FormalParameterList p = func_obj.parameters;
 
                        if (p != null)
-                               p.Resolve (context);
-
+                               p.Resolve (env);
+                       
                        Block body = func_obj.body;
 
                        if (body != null)
-                               body.Resolve (context);
+                               body.Resolve (env);
 
-                       locals = context.CurrentLocals;
-                       context.EndScope ();
+                       locals = env.CurrentLocals (String.Empty);
+                       env.EndScope (String.Empty);
                        return true;
                }               
        }
index 761cfaaa7a2e425d39dc3505f78a78ba92ad5b7e..e6c95ab6b5368f7a0e03b8cff3db0440e2fa52d1 100644 (file)
@@ -35,7 +35,7 @@ using Microsoft.JScript.Vsa;
 
 namespace Microsoft.JScript {
 
-       public class FunctionExpression : Function {
+       public class FunctionExpression : Function, ICanModifyContext {
 
                internal LocalBuilder local_script_func;
                internal FieldBuilder field;
@@ -43,6 +43,7 @@ namespace Microsoft.JScript {
                internal FunctionExpression (AST parent, string name, Location location)
                        : this (parent, name, null, String.Empty, null, location)
                {
+                       this.location = location;
                }
 
                internal FunctionExpression (AST parent, string name, 
@@ -67,23 +68,57 @@ namespace Microsoft.JScript {
                        return fun;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       //
+                       // In the case of function
+                       // expressions we add
+                       // function's name to the
+                       // table but we resolve its
+                       // body until later, as free
+                       // variables can be referenced
+                       // in function's body.
+                       //
+                       string name = func_obj.name;
+                       AST binding = null;
+
+                       if (name != null && name != String.Empty)
+                               binding = (AST) env.Get (ns, Symbol.CreateSymbol (name));
+
+                       if (binding == null)
+                               env.Enter (ns, Symbol.CreateSymbol (name), new FunctionDeclaration ());
+                       else {
+                               Console.WriteLine ("repeated functions are not handled yet");
+                               throw new NotImplementedException ();
+                       }
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       ((ICanModifyContext) func_obj.body).EmitDecls (ec);
+               }
+               
+               internal override bool Resolve (Environment env)
                {
                        set_function_type ();
+
                        if (func_obj.name != null && func_obj.name != String.Empty)
-                               context.Enter (Symbol.CreateSymbol (func_obj.name), this);
-                       context.BeginScope ();
+                               env.Enter (String.Empty, Symbol.CreateSymbol (func_obj.name), this);
+                       env.BeginScope (String.Empty);
+
+                       ((ICanModifyContext) func_obj).PopulateContext (env, String.Empty);
+                       
                        FormalParameterList p = func_obj.parameters;
 
                        if (p != null)
-                               p.Resolve (context);
+                               p.Resolve (env);
 
                        Block body = func_obj.body;
                        if (body != null)
-                               body.Resolve (context);
+                               body.Resolve (env);
 
-                       locals = context.CurrentLocals;
-                       context.EndScope ();
+                       locals = env.CurrentLocals (String.Empty);
+                       env.EndScope (String.Empty);
                        return true;
                }
 
@@ -141,7 +176,7 @@ namespace Microsoft.JScript {
                        ILGenerator ig = ec.ig;
                        string name = func_obj.name;
 
-                       Type t = ec.mod_builder.GetType ("JScript 0");
+                       Type t = ec.mod_builder.GetType (CodeGenerator.GetTypeName (Location.SourceName));
                        ig.Emit (OpCodes.Ldtoken, t);
                        
                        if (name != String.Empty)
index eefe5cf12b1988156a070cfe80334908d7ff1a7f..270dd70ad6af9d5125965c0fe11c266b85b7a8f0 100644 (file)
@@ -35,7 +35,7 @@ using System.Collections;
 
 namespace Microsoft.JScript {
 
-       public class FunctionObject : ScriptFunction {
+       public class FunctionObject : ScriptFunction, ICanModifyContext {
 
                internal string type_annot;
                internal Block body;
@@ -119,5 +119,15 @@ namespace Microsoft.JScript {
                                return types;
                        }
                }
+
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       ((ICanModifyContext) body).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       ((ICanModifyContext) body).EmitDecls (ec);
+               }               
        }
 }
index 10f1ebc40e54014ee9a60cc0b620fdc1c68f2066..518b88acc9c4a99d260b6018a74e2ba1a44c3080 100644 (file)
@@ -8,7 +8,7 @@
 //     Cesar Lopez Nataren (cnataren@novell.com)
 //
 // (C) 2003, Cesar Lopez Nataren
-// (C) 2005 Novell Inc.
+// Copyright (C) 2005 Novell Inc (http://novell.com)
 //
 
 //
@@ -190,7 +190,7 @@ namespace Microsoft.JScript {
                /// Delete symbol from the table
                /// </summary>
                internal void Remove (Symbol key)
-               {                       
+               {
                        Binder e = (Binder) dict [key];
                        if (e != null)
                                if (e.Tail != null)
@@ -292,19 +292,181 @@ namespace Microsoft.JScript {
                        ((Hashtable) locals.Peek ()).Add (name, o);
                }
 
+               internal DictionaryEntry [] LocalsAtDepth (int depth)
+               {
+                       object [] hashes = new object [locals.Count];
+                       locals.CopyTo (hashes, 0);
+                       Hashtable hash = (Hashtable) hashes [locals.Count - depth - 1];
+                       DictionaryEntry [] _locals = new DictionaryEntry [hash.Count];
+                       hash.CopyTo (_locals, 0);
+                       return _locals;
+               }
+       }
+
+
+       //
+       // Lookup table with namespace context.
+       // 
+       internal class Environment {
+
+               private Hashtable namespaces;
+
+               internal Environment ()
+               {
+                       namespaces = new Hashtable ();
+
+                       //
+                       // global variables identification table
+                       //
+                       namespaces.Add (String.Empty, new IdentificationTable ());
+               }
+
+               internal Environment (ScriptBlock [] blocks)
+                       : this ()
+               {
+                       //
+                       // FIXME: when we implement the package stm. For each package
+                       // name we must add a identification table that will store
+                       // id's defined inside it.
+                       //
+
+                       //
+                       // integrate the builtin functions, objects, ...
+                       //
+                       BuildGlobalEnv ();
+               }
+
+               //
+               // retrieves the identification table for the ns namespace
+               //
+               internal IdentificationTable GetIdentificationTable (string ns)
+               {
+                       return (IdentificationTable) namespaces [ns];
+               }
+
+               internal bool Contains (string ns, Symbol key)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               return false;
+
+                       return symbols.Contains (key);
+               }
+
+               internal object Get (string ns, Symbol key)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               return null;
+
+                       return symbols.Get (key);
+               }
+
+               internal void Enter (string ns, Symbol key, object value)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+                       
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       symbols.Enter (key, value);
+               }
+
+               internal void Remove (string ns, Symbol key)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+                       
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       symbols.Remove (key);
+               }
+
+               internal void BeginScope (string ns)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       symbols.BeginScope ();
+               }
+
+               internal void BeginScope (string ns, bool catchScope)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       symbols.BeginScope (catchScope);
+               }
+
+               internal void EndScope (string ns)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       symbols.EndScope ();
+               }
+
+               internal object [] CurrentLocals (string ns)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       return symbols.CurrentLocals;
+               }
+
+               internal int Depth (string ns)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       return symbols.depth;
+               }
+
+               internal bool InCurrentScope (string ns, Symbol id) 
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       return symbols.InCurrentScope (id);
+               }
+
+               internal bool CatchScope (string ns)
+               {
+                       IdentificationTable symbols = (IdentificationTable) namespaces [ns];
+
+                       if (symbols == null)
+                               throw new Exception (ns + " does not exist");
+
+                       return symbols.CatchScope;
+               }
+
                internal void BuildGlobalEnv ()
                {
                        //
                        // built in print function
                        //
                        if (SemanticAnalyser.print)
-                               Enter (Symbol.CreateSymbol ("print"), new BuiltIn ("print", false, true));
+                               Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("print"), new BuiltIn ("print", false, true));
 
                        /* value properties of the Global Object */
-                       Enter (Symbol.CreateSymbol ("NaN"), new BuiltIn ("NaN", false, false));
-                       Enter (Symbol.CreateSymbol ("Infinity"), new BuiltIn ("Infinity", false, false));
-                       Enter (Symbol.CreateSymbol ("undefined"), new BuiltIn ("undefined", false, false));
-                       Enter (Symbol.CreateSymbol ("null"), new BuiltIn ("null", false, false));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("NaN"), new BuiltIn ("NaN", false, false));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Infinity"), new BuiltIn ("Infinity", false, false));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("undefined"), new BuiltIn ("undefined", false, false));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("null"), new BuiltIn ("null", false, false));
                        
                        /* function properties of the Global Object */
                        object [] custom_attrs;
@@ -314,38 +476,28 @@ namespace Microsoft.JScript {
                                custom_attrs = mi.GetCustomAttributes (typeof (JSFunctionAttribute), false);
                                foreach (JSFunctionAttribute attr in custom_attrs)
                                        if (attr.IsBuiltIn)
-                                               Enter (Symbol.CreateSymbol (mi.Name), new BuiltIn (SemanticAnalyser.ImplementationName (attr.BuiltIn.ToString ()), false, true));
+                                               Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol (mi.Name), new BuiltIn (SemanticAnalyser.ImplementationName (attr.BuiltIn.ToString ()), false, true));
                        }
 
                        /* built in objects */
-                       Enter (Symbol.CreateSymbol ("Object"), new BuiltIn ("Object", true, true));
-                       Enter (Symbol.CreateSymbol ("Function"), new BuiltIn ("Function", true, true));
-                       Enter (Symbol.CreateSymbol ("Array"), new BuiltIn ("Array", true, true));
-                       Enter (Symbol.CreateSymbol ("String"), new BuiltIn ("String", true, true));
-                       Enter (Symbol.CreateSymbol ("Boolean"), new BuiltIn ("Boolean", true, true));
-                       Enter (Symbol.CreateSymbol ("Number"), new BuiltIn ("Number", true, true));
-                       Enter (Symbol.CreateSymbol ("Math"), new BuiltIn ("Math", false, false));
-                       Enter (Symbol.CreateSymbol ("Date"), new BuiltIn ("Date", true, true));
-                       Enter (Symbol.CreateSymbol ("RegExp"), new BuiltIn ("RegExp", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Object"), new BuiltIn ("Object", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Function"), new BuiltIn ("Function", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Array"), new BuiltIn ("Array", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("String"), new BuiltIn ("String", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Boolean"), new BuiltIn ("Boolean", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Number"), new BuiltIn ("Number", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Math"), new BuiltIn ("Math", false, false));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Date"), new BuiltIn ("Date", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("RegExp"), new BuiltIn ("RegExp", true, true));
 
                        /* built in Error objects */
-                       Enter (Symbol.CreateSymbol ("Error"), new BuiltIn ("Error", true, true));
-                       Enter (Symbol.CreateSymbol ("EvalError"), new BuiltIn ("EvalError", true, true));
-                       Enter (Symbol.CreateSymbol ("RangeError"), new BuiltIn ("RangeError", true, true));
-                       Enter (Symbol.CreateSymbol ("ReferenceError"), new BuiltIn ("ReferenceError", true, true));
-                       Enter (Symbol.CreateSymbol ("SyntaxError"), new BuiltIn ("SyntaxError", true, true));
-                       Enter (Symbol.CreateSymbol ("TypeError"), new BuiltIn ("TypeError", true, true));
-                       Enter (Symbol.CreateSymbol ("URIError"), new BuiltIn ("URIError", true, true));
-               }
-
-               internal DictionaryEntry [] LocalsAtDepth (int depth)
-               {
-                       object [] hashes = new object [locals.Count];
-                       locals.CopyTo (hashes, 0);
-                       Hashtable hash = (Hashtable) hashes [locals.Count - depth - 1];
-                       DictionaryEntry [] _locals = new DictionaryEntry [hash.Count];
-                       hash.CopyTo (_locals, 0);
-                       return _locals;
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("Error"), new BuiltIn ("Error", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("EvalError"), new BuiltIn ("EvalError", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("RangeError"), new BuiltIn ("RangeError", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("ReferenceError"), new BuiltIn ("ReferenceError", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("SyntaxError"), new BuiltIn ("SyntaxError", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("TypeError"), new BuiltIn ("TypeError", true, true));
+                       Enter (SemanticAnalyser.GlobalNamespace, Symbol.CreateSymbol ("URIError"), new BuiltIn ("URIError", true, true));
                }
        }
 }
index 59aedab22221e12ebf1d8f1d59e4688e1b59a4a6..015ae8e02f123e90d0797c39a39021ce74834239 100644 (file)
@@ -4,7 +4,7 @@
 // Author: Cesar Octavio Lopez Nataren
 //
 // (C) 2003, Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
-// (C) 2005 Copyright , Novell Inc (http://novell.com)
+// Copyright (C) 2005 , Novell Inc (http://novell.com)
 //
 
 //
@@ -48,7 +48,7 @@ namespace Microsoft.JScript {
                {
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        if (InFunction) {
                                string err = location.SourceName + "(" + location.LineNumber + ",0) : " +
index 486ceca326d50532f5a42d0f8a43204b89175073..6375a65f0806186b3af8542897b8ba85fb239c87 100644 (file)
@@ -48,12 +48,12 @@ namespace Microsoft.JScript {
                        return LateBinding.HasObjectProperty (obj, key);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        throw new NotImplementedException ();
                }
index 0bd3aabf50483c7b15ef9314681b03764157e8bf..62249fe1e1ef3ab1d05d2aa7380bca8a67a32f28 100644 (file)
@@ -67,12 +67,12 @@ namespace Microsoft.JScript {
                                throw new JScriptException (JSError.TypeMismatch);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        throw new NotImplementedException ();
                }
index bbfdbb35675aa2d691caa58273c21b38836815ba..8bc658cef019604d6e9ba1d594742399bc329df7 100644 (file)
@@ -44,7 +44,7 @@ namespace Microsoft.JScript {
                {
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        return true;
                }
@@ -65,15 +65,15 @@ namespace Microsoft.JScript {
                {
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        return true;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
        }
 
@@ -86,7 +86,7 @@ namespace Microsoft.JScript {
                        this.Value = val;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
                        return true;
@@ -296,17 +296,17 @@ namespace Microsoft.JScript {
                        this.elems = elems;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        foreach (AST ast in elems)
-                               r &= ast.Resolve (context);
+                               r &= ast.Resolve (env);
                        return r;
                }
 
@@ -345,9 +345,9 @@ namespace Microsoft.JScript {
                                property_name = obj.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
-                       return exp.Resolve (context);
+                       return exp.Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
@@ -381,7 +381,7 @@ namespace Microsoft.JScript {
                        this.flags = flags;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        return true;
                }
index 979127d4bdafad171f25a353d3e2b45bbb633404..de6ad3def4dac423c751222501c4953efef06b16 100644 (file)
@@ -67,15 +67,15 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index a27fcdf2c4d2e4cd07757608f58e029ed31c016a..70e8755d82e21a57c6db7887e9804cf307d57525 100644 (file)
@@ -64,12 +64,12 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        throw new NotImplementedException ();
                }
index 20f04a2146cdaaeef37e2fb5dc35dc9218ef7962..901b11ef85fc16c519f4eacee7f38d1702de7bec 100644 (file)
@@ -46,7 +46,7 @@ namespace Microsoft.JScript {
                public static void JScriptPackage (string rootName, VsaEngine engine)
                {}
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
index 7e73ed41a768f5a82ebfc9651636d2f64c37d991..d091d7757786e73b9f853ea599b22b5970b0c827 100644 (file)
@@ -31,6 +31,7 @@
 //
 
 using System.Collections;
+using Microsoft.Vsa;
 using System.IO;
 using System;
 
@@ -86,7 +87,6 @@ namespace Microsoft.JScript {
 
                TokenStream ts;
                bool ok; // did the parse encounter an error?
-               ScriptBlock current_script_or_fn;
                int nesting_of_function;
                int nesting_of_with;
                bool allow_member_expr_as_function_name;
@@ -96,20 +96,21 @@ namespace Microsoft.JScript {
                internal Parser ()
                {
                }
-
+               
                internal Parser (ArrayList code_items)
                {
                        this.code_items = code_items;
                }
-
-               internal AST ParseAll ()
+               
+               internal ScriptBlock [] ParseAll ()
                {
-                       ScriptBlock block = new ScriptBlock ();
+                       int i = 0, n = code_items.Count;
+                       ScriptBlock [] blocks = new ScriptBlock [n];
 
                        foreach (VsaCodeItem item in code_items)
-                               block.Add (Parse (item.SourceText, item.Name, 0));
+                               blocks [i++] = Parse (item.SourceText, item.Name, 0);
 
-                       return block;
+                       return blocks;
                }
 
                internal Decompiler CreateDecompiler ()
@@ -135,10 +136,11 @@ namespace Microsoft.JScript {
                /// </summary>
                ///
                /// <remarks>
-               ///   return an AST representing the parsed program.
-               ///    If the parse fails, null will be returned.
+               ///   return an ScriptBlock representing the parsed program
+               ///   that corresponds to a source file.
+               ///   If the parse fails, null will be returned.
                /// </remarks>
-               internal AST Parse (string source_string, string source_location, int line_number)
+               internal ScriptBlock Parse (string source_string, string source_location, int line_number)
                {
                        ts = new TokenStream (null, source_string, source_location, line_number);
                        try {
@@ -178,10 +180,10 @@ namespace Microsoft.JScript {
                        throw new ParserException ();
                }
        
-               AST Parse ()
+               ScriptBlock Parse ()
                {
                        decompiler = CreateDecompiler ();
-                       current_script_or_fn = new ScriptBlock (new Location (ts.SourceName, ts.LineNumber));
+                       ScriptBlock current_script_or_fn = new ScriptBlock (new Location (ts.SourceName, ts.LineNumber));
                        decompiler.GetCurrentOffset ();
                        decompiler.AddToken (Token.SCRIPT);
                        ok = true;
@@ -232,6 +234,7 @@ namespace Microsoft.JScript {
                {
                        ++nesting_of_function;
                        Block pn = new Block (parent, new Location (ts.SourceName, ts.LineNumber));
+
                        try {
                                int tt;
                                while ((tt = ts.PeekToken ()) > Token.EOF && tt != Token.RC) {
@@ -312,7 +315,6 @@ namespace Microsoft.JScript {
                        if (name != "")
                                decompiler.AddName (name);
 
-                       ScriptBlock saved_script_or_fn = current_script_or_fn;
                        int saved_nesting_of_with = nesting_of_with;
                        nesting_of_with = 0;
 
@@ -354,7 +356,6 @@ namespace Microsoft.JScript {
                                                decompiler.AddEOL (Token.SEMI);
                                }
                        } finally {
-                               current_script_or_fn = saved_script_or_fn;
                                nesting_of_with = saved_nesting_of_with;
                        }
 
@@ -372,6 +373,7 @@ namespace Microsoft.JScript {
                                // FIXME
                                pn = fn;
                                pn = new Assign (null, member_expr, pn, JSToken.Assign, false, new Location (ts.SourceName, ts.LineNumber));
+
                                // FIXME, research about createExprStatement
                                if (ft != FunctionType.Expression)
                                        ;
@@ -495,9 +497,7 @@ namespace Microsoft.JScript {
 
                        if (tt == Token.IF) {
                                skip_semi = true;
-
                                decompiler.AddToken (Token.IF);
-
                                AST cond = Condition (parent);
 
                                decompiler.AddEOL (Token.LC);
index 24bb52faf9e961b086dbe4b422a9b52263ccc8e6..1088c1e811287e78d5b15c0773f726abbba091c6 100644 (file)
@@ -58,12 +58,12 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        throw new NotImplementedException ();
                }
index a079abe4fe862d4121d931dd36b0d1e88dd7d9ab..428f757bd56bfd14d1500093a6caa0662ebfdc24 100644 (file)
@@ -65,17 +65,17 @@ namespace Microsoft.JScript {
                                return value - 1;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
-                       return operand.Resolve (context);
+                       return operand.Resolve (env);
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        if (operand is Exp)
-                               return ((Exp) operand).Resolve (context, no_effect);
+                               return ((Exp) operand).Resolve (env, no_effect);
                        else
-                               return operand.Resolve (context);
+                               return operand.Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index 752d54cfb1b89dea8dcbab9cf5354a4b38ef9326..a27a930b02a56a5bcb0f10c9df3e8fea6e605705 100644 (file)
@@ -51,7 +51,7 @@ namespace Microsoft.JScript {
                        return exp.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
index 5ee5b6591276098f2273e8567e7430cb3ce0fb68..08d7b2aaef09ffed08ccda5aad7fde7e2eafde53 100644 (file)
@@ -71,21 +71,21 @@ namespace Microsoft.JScript {
                        }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        if (left != null)
-                               left.Resolve (context);
+                               left.Resolve (env);
 
                        if (right != null)
-                               right.Resolve (context);
+                               right.Resolve (env);
 
                        return true;                    
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index 7d45cac1c1106bce341290b994d323ebe67270b6..86cde4cea9e2883e3783349a3cd1e36b825a3465 100644 (file)
@@ -1,9 +1,10 @@
 //
-// ScriptBlock.cs:
+// ScriptBlock.cs: Represents a file, which maps to a 'JScript N' class in the assembly.
 //
 // Author: Cesar Octavio Lopez Nataren
 //
 // (C) Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
+// Copyright (C) 2005 Novell Inc (http://novell.com)
 //
 
 //
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+using System;
+using System.Reflection;
+using Microsoft.JScript.Vsa;
+using System.Reflection.Emit;
+using System.Runtime.CompilerServices;
+
 namespace Microsoft.JScript {
 
-       public class ScriptBlock : AST {
+       public class ScriptBlock : AST, ICanModifyContext {
 
+               private TypeBuilder type_builder;
+               private MethodBuilder global_code;
+               private ILGenerator global_code_ig;
+               private EmitContext base_emit_context;
                internal Block src_elems;
+               
+               internal MethodBuilder GlobalCode {
+                       get { return global_code; }
+               }
+
+               internal TypeBuilder TypeBuilder {
+                       get { return type_builder; }
+               }
 
                internal ScriptBlock ()
                        : base (null, null)
@@ -50,14 +69,101 @@ namespace Microsoft.JScript {
                        src_elems.Add (e);
                }
 
+               internal void EmitDecls (ModuleBuilder mb)
+               {
+                       base_emit_context = new EmitContext (type_builder, mb, global_code_ig);
+                       EmitInitGlobalCode ();
+                       ((ICanModifyContext) src_elems).EmitDecls (base_emit_context);
+               }
+
+               internal void Emit ()
+               {
+                       Emit (base_emit_context);
+                       EmitEndGlobalCode ();
+               }
+
                internal override void Emit (EmitContext ec)
                {
+                       EmitTypeCtr ();
                        src_elems.Emit (ec);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       ((ICanModifyContext) src_elems).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+               }
+
+               internal override bool Resolve (Environment env)
+               {
+                       return src_elems.Resolve (env);
+               }
+
+
+               internal void InitTypeBuilder (ModuleBuilder moduleBuilder, string next_type)
+               { 
+                       type_builder = moduleBuilder.DefineType (next_type, TypeAttributes.Public);
+                       type_builder.SetParent (typeof (GlobalScope));
+                       type_builder.SetCustomAttribute (new CustomAttributeBuilder
+                                                        (typeof (CompilerGlobalScopeAttribute).GetConstructor (new Type [] {}), new object [] {}));
+               }
+
+               internal void CreateType ()
+               {
+                       type_builder.CreateType ();
+               }
+
+               internal void EmitTypeCtr ()
+               {
+                       ConstructorBuilder cons_builder;
+                       cons_builder = type_builder.DefineConstructor (MethodAttributes.Public,
+                                                              CallingConventions.Standard,
+                                                              new Type [] { typeof (GlobalScope) });
+                       ILGenerator ig = cons_builder.GetILGenerator ();
+                       ig.Emit (OpCodes.Ldarg_0);
+                       ig.Emit (OpCodes.Ldarg_1);
+                       ig.Emit (OpCodes.Dup);
+                       ig.Emit (OpCodes.Ldfld,
+                                typeof (ScriptObject).GetField ("engine"));
+                       
+                       ig.Emit (OpCodes.Call, 
+                                typeof (GlobalScope).GetConstructor (new Type [] {typeof (GlobalScope), 
+                                                                                  typeof (VsaEngine)}));
+                       ig.Emit (OpCodes.Ret);
+               }
+
+               internal void InitGlobalCode ()
                {
-                       return src_elems.Resolve (context);
+                       global_code = type_builder.DefineMethod ("Global Code", MethodAttributes.Public,
+                                                        typeof (System.Object), new Type [] {});
+                       global_code_ig = global_code.GetILGenerator ();
+               }
+
+               private void EmitInitGlobalCode ()
+               {
+                       ILGenerator ig = global_code_ig;
+
+                       ig.Emit (OpCodes.Ldarg_0);
+                       ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
+                       ig.Emit (OpCodes.Ldarg_0);
+                       ig.Emit (OpCodes.Call,
+                               typeof (VsaEngine).GetMethod ("PushScriptObject",
+                                                             new Type [] { typeof (ScriptObject)}));
+               }
+
+               private void EmitEndGlobalCode ()
+               {
+                       ILGenerator ig = global_code_ig;
+
+                       ig.Emit (OpCodes.Ldnull);
+                       ig.Emit (OpCodes.Ldarg_0);
+                       ig.Emit (OpCodes.Ldfld, typeof (ScriptObject).GetField ("engine"));
+                       ig.Emit (OpCodes.Call, typeof (VsaEngine).GetMethod ("PopScriptObject"));
+                       ig.Emit (OpCodes.Pop);
+                       ig.Emit (OpCodes.Ret);
                }
        }
 }
index d1ed6b2149a8e159e984c6c2a141666151f41c8e..834c8250ef73adf979a9c7f3bbc8ddff8edfd86d 100644 (file)
@@ -39,7 +39,7 @@ namespace Microsoft.JScript {
 
                internal static bool print = true;
                internal static bool allow_member_expr_as_function_name;
-               static IdentificationTable context;
+               static Environment env;
                static IdentificationTable label_set;
 
                private static Hashtable obj_ctrs;
@@ -47,6 +47,7 @@ namespace Microsoft.JScript {
                internal static Hashtable methods_with_eval = new Hashtable ();
                internal static Hashtable methods_with_outter_scope_refs = new Hashtable ();
                internal static Hashtable methods_with_vars_used_nested = new Hashtable ();
+               internal static Environment Ocurrences = new Environment ();
 
                //
                // Type to GlobalObject
@@ -56,6 +57,11 @@ namespace Microsoft.JScript {
                internal static bool NoFast = true;
                private static Assembly [] assemblies;
 
+               private static readonly string global_namespace = String.Empty;
+               internal static string GlobalNamespace {
+                       get { return global_namespace; }
+               }
+
                static SemanticAnalyser ()
                {
                        label_set = new IdentificationTable ();
@@ -133,14 +139,20 @@ namespace Microsoft.JScript {
                        return name.Substring (i + 1);
                }
 
-               internal static bool Run (ScriptBlock prog, Assembly [] ref_items)
+               internal static bool Run (ScriptBlock [] blocks, Assembly [] ref_items)
                {
-                       assemblies = ref_items;                 
+                       assemblies = ref_items;
                        ComputeNamespaces ();
+                       env = new Environment (blocks);
+                       bool r = true;
+
+                       foreach (ScriptBlock script_block in blocks)
+                               ((ICanModifyContext) script_block).PopulateContext (env, String.Empty);
 
-                       context = new IdentificationTable ();
-                       context.BuildGlobalEnv ();
-                       return prog.Resolve (context);
+                       foreach (ScriptBlock script_block in blocks)
+                               r &= script_block.Resolve (env);
+
+                       return r;
                }
 
                static int anon_method_counter = -1;
index d58d9084c0ef49d5f846889b9cb6f062fe332135..a318ed4f758fe663e22909ad36b1cf0d4a82819d 100644 (file)
@@ -5,6 +5,7 @@
 //     Cesar Octavio Lopez Nataren
 //
 // (C) 2003, 2004 Cesar Octavio Lopez Nataren, <cesar@ciencias.unam.mx>
+// Copyright (C) 2005 Novell Inc (http://novell.com)
 //
 
 //
@@ -36,7 +37,15 @@ using System.Collections;
 
 namespace Microsoft.JScript {
 
-       internal class If : AST {
+       internal interface ICanModifyContext {
+               //
+               // Populate the symbol table before resolving references
+               //
+               void PopulateContext (Environment env, string ns);
+               void EmitDecls (EmitContext ec);
+       }
+
+       internal class If : AST, ICanModifyContext {
 
                internal AST cond, true_stm, false_stm;
 
@@ -61,26 +70,44 @@ namespace Microsoft.JScript {
                        
                        return sb.ToString ();
                }
+
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (true_stm is ICanModifyContext)
+                               ((ICanModifyContext) true_stm).PopulateContext (env, ns);
+
+                       if (false_stm is ICanModifyContext)
+                               ((ICanModifyContext) false_stm).PopulateContext (env, ns);
+               }
                
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (true_stm is ICanModifyContext)
+                               ((ICanModifyContext) true_stm).EmitDecls (ec);
+
+                       if (false_stm is ICanModifyContext)
+                               ((ICanModifyContext) false_stm).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
 
                        if (cond != null)
                                if (cond is Exp)
-                                       r &= ((Exp) cond).Resolve (context, false);
+                                       r &= ((Exp) cond).Resolve (env, false);
 
                        if (true_stm != null)
                                if (true_stm is Exp)
-                                       r &= ((Exp) true_stm).Resolve (context, true);
+                                       r &= ((Exp) true_stm).Resolve (env, true);
                                else
-                                       r &= true_stm.Resolve (context);                        
+                                       r &= true_stm.Resolve (env);
 
                        if (false_stm != null)
                                if (false_stm is Exp)
-                                       r &= ((Exp) false_stm).Resolve (context, true);
+                                       r &= ((Exp) false_stm).Resolve (env, true);
                                else
-                                       r &= false_stm.Resolve (context);                       
+                                       r &= false_stm.Resolve (env);
                        return r;
                }
 
@@ -131,7 +158,7 @@ namespace Microsoft.JScript {
                        this.label += label;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                 {
                         if (!InLoop)
                                 throw new Exception ("A continue can't be outside a iteration stm");
@@ -158,7 +185,7 @@ namespace Microsoft.JScript {
                        this.label += label;
                }
 
-                internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                 {
                         if (!InLoop && !InSwitch)
                                throw new Exception ("A break statement can't be outside a switch or iteration stm");
@@ -214,13 +241,13 @@ namespace Microsoft.JScript {
                                return String.Empty;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        if (!InFunction)
                                throw new Exception ("error JS1018: 'return' statement outside of function");
                        if (expression != null) {
                                OnNotVoidReturn (null);
-                               return expression.Resolve (context);
+                               return expression.Resolve (env);
                        } else 
                                return true;
                }
@@ -245,7 +272,7 @@ namespace Microsoft.JScript {
                }
        }
 
-       internal class DoWhile : AST {
+       internal class DoWhile : AST, ICanModifyContext {
 
                AST stm, exp;
 
@@ -261,14 +288,26 @@ namespace Microsoft.JScript {
                        this.exp = exp;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
 
                        if (stm != null)
-                               r &= stm.Resolve (context);
+                               r &= stm.Resolve (env);
                        if (exp != null)
-                               r &= exp.Resolve (context);
+                               r &= exp.Resolve (env);
                        return r;
                }
 
@@ -301,7 +340,7 @@ namespace Microsoft.JScript {
        }
 
 
-       internal class While : AST {            
+       internal class While : AST, ICanModifyContext {
                AST exp, stm;
 
                internal While (Location location)
@@ -316,16 +355,28 @@ namespace Microsoft.JScript {
                        this.stm = stm;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).EmitDecls (ec);
+               }
+               
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (exp != null)
                                if (exp is Exp)
-                                       r &= ((Exp) exp).Resolve (context, false);
+                                       r &= ((Exp) exp).Resolve (env, false);
                                else 
-                                       r &= exp.Resolve (context);
+                                       r &= exp.Resolve (env);
                        if (stm != null)
-                               r &= stm.Resolve (context);
+                               r &= stm.Resolve (env);
                        return r;
                }
 
@@ -364,7 +415,7 @@ namespace Microsoft.JScript {
                }
        }
 
-       internal class For : AST {
+       internal class For : AST, ICanModifyContext {
                
                AST [] exprs = new AST [3];
                AST stms;
@@ -378,17 +429,34 @@ namespace Microsoft.JScript {
                        stms = body;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       foreach (AST ast in exprs)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+
+                       if (stms is ICanModifyContext)
+                               ((ICanModifyContext) stms).PopulateContext (env, ns);
+               }
+               
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       foreach (AST ast in exprs)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+
+                       if (stms is ICanModifyContext)
+                               ((ICanModifyContext) stms).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
 
                        foreach (AST ast in exprs)
-                               if (ast is VariableStatement)
-                                       ((VariableStatement) ast).PopulateContext (context);
-                               else
-                                       r &= ast.Resolve (context);
+                               r &= ast.Resolve (env);
                        if (stms != null)
-                               r &= stms.Resolve (context);
+                               r &= stms.Resolve (env);
                        return true;
                }
 
@@ -404,13 +472,9 @@ namespace Microsoft.JScript {
 
                        /* emit init expr */
                        tmp = exprs [0];
-                       if (tmp != null) {
-                               if (tmp is VariableStatement) {
-                                       VariableStatement stm = (VariableStatement) tmp;
-                                       stm.EmitVariableDecls (ec);
-                               }
+                       if (tmp != null)
                                tmp.Emit (ec);
-                       }
+
                         ec.LoopBegin = ig.DefineLabel ();
                        ec.LoopEnd = ig.DefineLabel ();
                        
@@ -454,7 +518,7 @@ namespace Microsoft.JScript {
                }
        }
 
-       internal class Switch : AST {          
+       internal class Switch : AST, ICanModifyContext {
 
                internal AST exp;
                internal ArrayList case_clauses;
@@ -477,20 +541,50 @@ namespace Microsoft.JScript {
                                sec_case_clauses.Add (clause);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       foreach (AST ast in case_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+
+                       foreach (AST ast in default_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+
+                       foreach (AST ast in sec_case_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+               }
+               
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       foreach (AST ast in case_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+
+                       foreach (AST ast in default_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+
+                       foreach (AST ast in sec_case_clauses)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (exp != null)
-                               r &= exp.Resolve (context);
+                               r &= exp.Resolve (env);
                        if (case_clauses != null)
                                foreach (Clause c in case_clauses)
-                                       r &= c.Resolve (context);
+                                       r &= c.Resolve (env);
                        if (default_clauses != null)
                                foreach (AST dc in default_clauses)
-                                       r &= dc.Resolve (context);
+                                       r &= dc.Resolve (env);
                        if (sec_case_clauses != null)
                                foreach (Clause sc in sec_case_clauses)
-                                       r &= sc.Resolve (context);
+                                       r &= sc.Resolve (env);
                        return r;
                }
 
@@ -542,7 +636,7 @@ namespace Microsoft.JScript {
                }
        }
 
-       internal class Clause : AST {
+       internal class Clause : AST, ICanModifyContext {
                internal AST exp;
                internal ArrayList stm_list;
                internal Label matched_block;
@@ -558,13 +652,28 @@ namespace Microsoft.JScript {
                        stm_list.Add (stm);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       foreach (AST ast in stm_list)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+               }
+               
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       foreach (AST ast in stm_list)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (exp != null)
-                               r &= exp.Resolve (context);
+                               r &= exp.Resolve (env);
                        foreach (AST ast in stm_list)
-                               r &= ast.Resolve (context);
+                               r &= ast.Resolve (env);
                        return r;                       
                }
 
@@ -589,7 +698,7 @@ namespace Microsoft.JScript {
                }       
        }
 
-       internal class Catch : AST {
+       internal class Catch : AST, ICanModifyContext {
                internal string id;
                internal AST catch_cond;
                internal AST stms;
@@ -605,11 +714,23 @@ namespace Microsoft.JScript {
                        this.stms = stms;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (stms is ICanModifyContext)
+                               ((ICanModifyContext) stms).PopulateContext (env, ns);
+               }
+               
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (stms is ICanModifyContext)
+                               ((ICanModifyContext) stms).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (stms != null)
-                               r &= stms.Resolve (context);
+                               r &= stms.Resolve (env);
                        return r;
                }
 
@@ -642,7 +763,7 @@ namespace Microsoft.JScript {
                }
        }
 
-       internal class Labelled : AST {
+       internal class Labelled : AST, ICanModifyContext {
                string name;
                Label init_addrs; 
                Label end_addrs;
@@ -671,7 +792,19 @@ namespace Microsoft.JScript {
                        this.location = location;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).PopulateContext (env, ns);
+               }
+               
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        try {
                                SemanticAnalyser.AddLabel (name, this);
@@ -679,7 +812,7 @@ namespace Microsoft.JScript {
                                throw new Exception ("error JS1025: Label redefined");
                        }
                        if (stm != null)
-                               stm.Resolve (context);
+                               stm.Resolve (env);
                        SemanticAnalyser.RemoveLabel (name);
                        return true;
                }
index 6b1212a6387fbbc92522da13ee408d340f796e6a..0423db252e12ec828b2283dca7dc02aec0498848 100644 (file)
@@ -92,20 +92,20 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (left != null)
-                               r &= left.Resolve (context);
+                               r &= left.Resolve (env);
                        if (right != null)
-                               r &= right.Resolve (context);
+                               r &= right.Resolve (env);
                        return r;
                }
                
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index f1b0d7c7bea59944a9d4c8b15df2c6a58e6554e5..970de2c27c51d92ea024baa80a8cf6dbf161d5d3 100644 (file)
@@ -45,12 +45,12 @@ namespace Microsoft.JScript {
                        str = s;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        return true;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
                        return true;
index 02751ce2d7145026aac6bf58b7188b51f8c2ce55..5a35887d5a1ac5ea106926b93be6b50e7152011c 100644 (file)
@@ -47,9 +47,9 @@ namespace Microsoft.JScript {
                        return new JScriptException (Convert.ToString (value));
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
-                       return expression.Resolve (context);
+                       return expression.Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index 3f21ba90c6c77bfb9293e9330832d5c127c970af..a87c1b4d973d10911d4c2c1759624c84263a7b7a 100644 (file)
@@ -35,7 +35,7 @@ using System.Collections;
 
 namespace Microsoft.JScript {
 
-       public sealed class Try : AST {
+       public sealed class Try : AST, ICanModifyContext {
 
                internal FieldBuilder field_info;
                internal LocalBuilder local_builder;
@@ -69,22 +69,22 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (guarded_block != null)
-                               r &= guarded_block.Resolve (context);
-                       
+                               r &= guarded_block.Resolve (env);
+
                        if (catch_blocks != null && catch_blocks.Count > 0) {
                                foreach (Catch c in catch_blocks) {
-                                       context.BeginScope (true);
-                                       context.Enter (Symbol.CreateSymbol (c.id), c);
-                                       r &= c.Resolve (context);
-                                       context.EndScope ();
+                                       env.BeginScope (String.Empty, true);
+                                       env.Enter (String.Empty, Symbol.CreateSymbol (c.id), c);
+                                       r &= c.Resolve (env);
+                                       env.EndScope (String.Empty);
                                }
                        }
                        if (finally_block != null)
-                               r &= finally_block.Resolve (context);
+                               r &= finally_block.Resolve (env);
                        return r;
                }
 
@@ -107,5 +107,32 @@ namespace Microsoft.JScript {
                        }
                        ig.EndExceptionBlock ();
                }
+
+
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (guarded_block is ICanModifyContext)
+                               ((ICanModifyContext) guarded_block).PopulateContext (env, ns);
+
+                       foreach (AST ast in catch_blocks)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).PopulateContext (env, ns);
+
+                       if (finally_block is ICanModifyContext)
+                               ((ICanModifyContext) finally_block).PopulateContext (env, ns);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (guarded_block is ICanModifyContext)
+                               ((ICanModifyContext) guarded_block).EmitDecls (ec);
+
+                       foreach (AST ast in catch_blocks)
+                               if (ast is ICanModifyContext)
+                                       ((ICanModifyContext) ast).EmitDecls (ec);
+
+                       if (finally_block is ICanModifyContext)
+                               ((ICanModifyContext) finally_block).EmitDecls (ec);
+               }
        }
 }
index b9ae4c04a9a6695d439700d9fc80774f14e5b547..8038a5c9707af276199e4be2cb204fdd1caeed77 100644 (file)
@@ -71,15 +71,15 @@ namespace Microsoft.JScript {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        throw new NotImplementedException ();
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
index a66c76e5182c3ea3efdbf3acaa2ca36bcf0ffb4d..03b1c6fd2c75d7bb15e2d302b6a9061649aa481f 100644 (file)
@@ -35,7 +35,7 @@ using System.Reflection.Emit;
 
 namespace Microsoft.JScript {
        
-       internal class VariableDeclaration : AST {
+       internal class VariableDeclaration : AST, ICanModifyContext {
 
                internal string id;
                internal Type type;
@@ -79,7 +79,15 @@ namespace Microsoft.JScript {
                        return "var " + sb.ToString ();
                }
 
-               internal void EmitDecl (EmitContext ec)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       Symbol _id = Symbol.CreateSymbol (id);
+
+                       if (!env.InCurrentScope (ns, _id))
+                               env.Enter (ns, _id, this);
+               }
+
+               void ICanModifyContext.EmitDecls (EmitContext ec)
                {
                        object var;
                        
@@ -127,12 +135,12 @@ namespace Microsoft.JScript {
                        }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (val != null)
-                               r = val.Resolve (context);
-                       lexical_depth = context.depth;
+                               r = val.Resolve (env);
+                       lexical_depth = env.Depth (String.Empty);
                        func_decl = GetContainerFunction;
                        return r;
                }
index 61caaa7634d25ff37220b661ea3cc24b7c4c88f0..07fce9d44ebf728ce37ff7ef70687b531bbd6c33 100644 (file)
@@ -34,7 +34,7 @@ using System;
 
 namespace Microsoft.JScript {
 
-       internal class VariableStatement : AST {
+       internal class VariableStatement : AST, ICanModifyContext {
 
                internal ArrayList var_decls;
 
@@ -61,11 +61,10 @@ namespace Microsoft.JScript {
                        return sb.ToString ();
                }
 
-               internal void EmitVariableDecls (EmitContext ec)
+               void ICanModifyContext.EmitDecls (EmitContext ec)
                {
-                       int n = var_decls.Count;
-                       for (int i = 0; i < n; i++)
-                               ((VariableDeclaration) var_decls [i]).EmitDecl (ec);
+                       foreach (VariableDeclaration var_decl in var_decls)
+                               ((ICanModifyContext) var_decl).EmitDecls (ec);
                }
 
                internal override void Emit (EmitContext ec)
@@ -76,22 +75,13 @@ namespace Microsoft.JScript {
                                ((VariableDeclaration) var_decls [i]).Emit (ec);
                }
 
-               internal void PopulateContext (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
                {
-                       VariableDeclaration tmp_decl;
-                       int n = var_decls.Count;
-                       Symbol id;
-
-                       for (int i = 0; i < n; i++) {
-                               tmp_decl = (VariableDeclaration) var_decls [i];
-                               id = Symbol.CreateSymbol (tmp_decl.id);
-                               if (context.InCurrentScope (id))
-                                       continue;
-                               context.Enter (id, tmp_decl);
-                       }
+                       foreach (VariableDeclaration var_decl in var_decls)
+                               ((ICanModifyContext) var_decl).PopulateContext (env, ns);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        VariableDeclaration tmp_decl;
                        int n = var_decls.Count;
@@ -99,7 +89,7 @@ namespace Microsoft.JScript {
 
                        for (int i = 0; i < n; i++) {
                                tmp_decl = (VariableDeclaration) var_decls [i];
-                               r &= tmp_decl.Resolve (context);
+                               r &= tmp_decl.Resolve (env);
                        }
                        return r;
                }
index 630f7ae0f6da7796820266bc9be2de7cf4ba8c88..5defc3e587ab552e1c5c3bffc59244fd81ee23f8 100644 (file)
@@ -34,7 +34,7 @@ using System.Reflection.Emit;
 
 namespace Microsoft.JScript {
 
-       public class With : AST {
+       public class With : AST, ICanModifyContext {
 
                AST exp, stm;
 
@@ -51,13 +51,25 @@ namespace Microsoft.JScript {
                        return withObj;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               void ICanModifyContext.PopulateContext (Environment env, string ns)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).PopulateContext (env, ns);
+               }
+               
+               void ICanModifyContext.EmitDecls (EmitContext ec)
+               {
+                       if (stm is ICanModifyContext)
+                               ((ICanModifyContext) stm).EmitDecls (ec);
+               }
+
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (exp != null)
-                               r &= exp.Resolve (context);
+                               r &= exp.Resolve (env);
                        if (stm != null)
-                               r &= stm.Resolve (context);
+                               r &= stm.Resolve (env);
                        return r;
                }
 
index 3cc3c4ba6cd0263ca3b7b8af8381d1320ecf8d56..ae6c4d9e13eef6e5de871497acceee4f58b8a7fb 100644 (file)
@@ -54,12 +54,12 @@ namespace Microsoft.JScript {
                // Here the actual IL code generation happens.
                //
                internal abstract void Emit (EmitContext ec);
-
+                       
                //
                // Perform type checks and associates expressions
                // with their declarations
                //
-               internal abstract bool Resolve (IdentificationTable context);
+               internal abstract bool Resolve (Environment env);
 
                internal bool InLoop {
                        get {
index 5f3b73f3fa553c2fc684ee3701bbc89693c3efea..4de83a5e4fb752c111ae91380dd2fb5e612738b4 100644 (file)
@@ -40,7 +40,7 @@ namespace Microsoft.JScript {
        
        public abstract class Exp : AST {
                internal bool no_effect;
-               internal abstract bool Resolve (IdentificationTable context, bool no_effect);
+               internal abstract bool Resolve (Environment env, bool no_effect);
 
                internal Exp (AST parent, Location location)
                        : base (parent, location)
@@ -72,7 +72,7 @@ namespace Microsoft.JScript {
                        return sb.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = false;                
 
@@ -80,19 +80,19 @@ namespace Microsoft.JScript {
                                Binary bin = (Binary) operand;
                                if (oper == JSToken.Delete && bin.AccessField)
                                        this.deletable = bin.IsDeletable (out isCtr);
-                               bin.Resolve (context);
+                               bin.Resolve (env);
                        } else if (operand is Exp) {
                                if (oper != JSToken.Increment && oper != JSToken.Decrement)
-                                       r = ((Exp) operand).Resolve (context, no_effect);
+                                       r = ((Exp) operand).Resolve (env, no_effect);
                        } else 
-                               r = operand.Resolve (context);
+                               r = operand.Resolve (env);
                        return r;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
@@ -269,7 +269,7 @@ namespace Microsoft.JScript {
                        get { return late_bind; }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool found = true;
 
@@ -279,30 +279,30 @@ namespace Microsoft.JScript {
                                        if (!found)
                                                late_bind = true;
                                } else
-                                       found &= left.Resolve (context);
+                                       found &= left.Resolve (env);
                        if (right != null)
                                if (op == JSToken.AccessField && right is IAccessible) {
                                        found &= ((IAccessible) right).ResolveFieldAccess (left);
                                        if (!found)
                                                late_bind = true;
                                } else
-                                       found &= right.Resolve (context);
+                                       found &= right.Resolve (env);
                        return found;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
                
-               public bool ResolveAssign (IdentificationTable context, AST right_side)
+               public bool ResolveAssign (Environment env, AST right_side)
                {
                        if (op == JSToken.LeftBracket || op == JSToken.AccessField) {
                                this.no_effect = false;
                                this.assign = true;
                                this.right_side = right_side;
-                               return Resolve (context);
+                               return Resolve (env);
                        } else 
                                throw new Exception (location.SourceName + " (" + location.LineNumber + ",0): error JS5008: Illegal assignment");
                }
@@ -659,22 +659,22 @@ namespace Microsoft.JScript {
                        return sb.ToString ();
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
                        if (cond_exp != null)
-                               r &= cond_exp.Resolve (context);
+                               r &= cond_exp.Resolve (env);
                        if (true_exp != null)
-                               r &= true_exp.Resolve (context);
+                               r &= true_exp.Resolve (env);
                        if (false_exp != null)
-                               r &= false_exp.Resolve (context);
+                               r &= false_exp.Resolve (env);
                        return r;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
@@ -734,14 +734,14 @@ namespace Microsoft.JScript {
                        args.Add (arg);
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
 
                        if (member_exp != null) {
                                if (member_exp is Identifier) {
-                                       member_exp.Resolve (context);
-                                       binding = context.Get ((member_exp as Identifier).name);
+                                       member_exp.Resolve (env);
+                                       binding = env.Get (String.Empty, (member_exp as Identifier).name);
                                        bind_type = binding.GetType ();
 
                                        if (bind_type == typeof (BuiltIn)) {
@@ -762,19 +762,19 @@ namespace Microsoft.JScript {
                                                }
                                        }
                                } else if (member_exp is Binary) {
-                                       member_exp.Resolve (context);
+                                       member_exp.Resolve (env);
                                        Binary bin = (Binary) member_exp;
                                        if (bin.AccessField)
                                                binding = bin.Binding;
                                        if (binding is MethodInfo)
                                                NeedThis ((MethodInfo) binding);
                                } else
-                                       r &= member_exp.Resolve (context);
+                                       r &= member_exp.Resolve (env);
 
                                args.BoundToMethod = binding;
 
                                if (args != null)
-                                       r &= args.Resolve (context);
+                                       r &= args.Resolve (env);
                        } else
                                throw new Exception ("Call.Resolve, member_exp can't be null");
                        return r;
@@ -785,10 +785,10 @@ namespace Microsoft.JScript {
                        need_this = SemanticAnalyser.Needs (JSFunctionAttributeEnum.HasThisObject, method);
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
@@ -830,7 +830,7 @@ namespace Microsoft.JScript {
                        } else if (bind_type == typeof (FunctionDeclaration) || bind_type == typeof (FunctionExpression)) {
                                Function function = binding as Function;
                                MethodBuilder method = (MethodBuilder) TypeManager.Get (function.func_obj.name);
-                               
+
                                if (SemanticAnalyser.MethodContainsEval (function.func_obj.name) ||
                                    SemanticAnalyser.MethodReferenceOutterScopeVar (function.func_obj.name) ||
                                    SemanticAnalyser.MethodVarsUsedNested (function.func_obj.name)) {
@@ -1243,44 +1243,50 @@ namespace Microsoft.JScript {
                        return name.Value;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
-                       bool contained = context.Contains (this.name);
+                       bool contained = env.Contains (String.Empty, this.name);
                        if (contained) {
-                               binding = (AST) context.Get (this.name);
+                               binding = (AST) env.Get (String.Empty, this.name);
                                if (binding is VariableDeclaration) {
                                        VariableDeclaration decl = (VariableDeclaration) binding;
-                                       lexical_difference = context.depth - decl.lexical_depth;
+                                       lexical_difference = env.Depth (String.Empty) - decl.lexical_depth;
                                        no_field = decl.lexical_depth != 0;
-                                       if (no_field && lexical_difference > 0 && !context.CatchScope) {
+
+                                       if (no_field && lexical_difference > 0 && !env.CatchScope (String.Empty)) {
                                                Function container_func = GetContainerFunction;
                                                SemanticAnalyser.AddMethodReferenceOutterScopeVar (container_func.func_obj.name, decl);
-                                               context.Enter (Symbol.CreateSymbol (decl.id), decl);
+                                               env.Enter (String.Empty, Symbol.CreateSymbol (decl.id), decl);
                                                SemanticAnalyser.AddMethodVarsUsedNested (decl.GetContainerFunction.func_obj.name, decl);
                                        }
                                }
                        } else if (SemanticAnalyser.NoFast) {
                                undeclared = true;
                                Console.WriteLine ("warning JS1135: Variable '" + name + "' has not been declared");
-                       } else
+                       } else {
+                               //
+                               // Identifier not into stand-alone JScript, we'll search into 
+                               // referenced assemblies and namespaces 
+                               //
                                throw new Exception (location.SourceName + "(" + location.LineNumber + 
                                             ",0) : error JS1135: Variable '" + name + "' has not been declared");
+                       }
                        return true;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
-               public bool ResolveAssign (IdentificationTable context, AST right_side)
+               public bool ResolveAssign (Environment env, AST right_side)
                {
                        this.assign = true;
                        this.no_effect = false;
                        this.right_side = right_side;
                        if (name.Value != String.Empty)                 
-                               return Resolve (context);
+                               return Resolve (env);
                        return true;
                }
 
@@ -1644,7 +1650,7 @@ namespace Microsoft.JScript {
                        }
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        AST tmp;
                        bool r = true;
@@ -1674,8 +1680,9 @@ namespace Microsoft.JScript {
                                tmp = (AST) elems [i];
                                if (tmp != null)
                                        if (tmp is Exp)
-                                               r &= ((Exp) tmp).Resolve (context, false);
-                                       r &= tmp.Resolve (context);
+                                               r &= ((Exp) tmp).Resolve (env, false);
+                                       else
+                                               r &= tmp.Resolve (env);
                        }
                        return r;
                }
@@ -1881,7 +1888,7 @@ namespace Microsoft.JScript {
                        } else return String.Empty;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        int i, n;
                        object e;
@@ -1893,26 +1900,26 @@ namespace Microsoft.JScript {
                                e = exprs [i];
                                if (e is Exp)
                                        if (e is Assign)
-                                               r &= ((Assign) e).Resolve (context);
+                                               r &= ((Assign) e).Resolve (env);
                                        else
-                                               r &= ((Exp) e).Resolve (context, true);
+                                               r &= ((Exp) e).Resolve (env, true);
                        }
                        e = exprs [n];
                        if (e is Exp)
                                if (e is Assign)
-                                       r &= ((Assign) e).Resolve (context);
+                                       r &= ((Assign) e).Resolve (env);
                                else
-                                       r &= ((Exp) e).Resolve (context, no_effect);
+                                       r &= ((Exp) e).Resolve (env, no_effect);
                        else 
-                               ((AST) e).Resolve (context);
+                               ((AST) e).Resolve (env);
 
                        return r;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        this.no_effect = no_effect;
-                       return Resolve (context);
+                       return Resolve (env);
                }
 
                internal override void Emit (EmitContext ec)
@@ -1942,22 +1949,22 @@ namespace Microsoft.JScript {
                // after calling Resolve, left contains all the 
                // information about the assignment
                //
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {                                               
                        bool r;
 
                        if (left is IAssignable)
-                               r = ((IAssignable) left).ResolveAssign (context, right);
+                               r = ((IAssignable) left).ResolveAssign (env, right);
                        else
                                throw new Exception (location.SourceName + " (" + location.LineNumber + ",0): error JS5008: Illegal assignment");
                        if (right is Exp)
-                               r &=((Exp) right).Resolve (context, false);
+                               r &=((Exp) right).Resolve (env, false);
                        else
-                               r &= right.Resolve (context);
+                               r &= right.Resolve (env);
                        return r;
                }
 
-               internal override bool Resolve (IdentificationTable context, bool no_effect)
+               internal override bool Resolve (Environment env, bool no_effect)
                {
                        return true;
                }
@@ -1967,7 +1974,7 @@ namespace Microsoft.JScript {
                        if (op == JSToken.Assign) {
                                if (is_embedded) {
                                        Console.WriteLine ("embedded assignments not supported yet");
-                                       Environment.Exit (-1);
+                                       System.Environment.Exit (-1);
                                } 
                                left.Emit (ec);
                        } else {
@@ -2120,7 +2127,7 @@ namespace Microsoft.JScript {
                        args.Add (arg);
                }
                
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        bool r = true;
 
@@ -2128,11 +2135,11 @@ namespace Microsoft.JScript {
                                Identifier id = (Identifier) exp;
                                late_bind = !SemanticAnalyser.is_js_object (id.name.Value);
                        } 
-                       exp.Resolve (context);
+                       exp.Resolve (env);
 
                        if (args != null) {
                                args.InNew = true;
-                               r &= args.Resolve (context);
+                               r &= args.Resolve (env);
                        }
                        return r;
                }
@@ -2236,7 +2243,7 @@ namespace Microsoft.JScript {
        }
        
        internal interface IAssignable {
-               bool ResolveAssign (IdentificationTable context, AST right_side);
+               bool ResolveAssign (Environment env, AST right_side);
        }
 
        internal class BuiltIn : AST {
@@ -2252,7 +2259,7 @@ namespace Microsoft.JScript {
                        this.allowed_as_func = allowed_as_func;
                }
 
-               internal override bool Resolve (IdentificationTable context)
+               internal override bool Resolve (Environment env)
                {
                        return true;
                }