Author: cesar
Date: 2005-02-27 17:20:23 -0500 (Sun, 27 Feb 2005)
New Revision: 41267
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Block.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
Log:
2005-02-27 Cesar Lopez Nataren <[EMAIL PROTECTED]>
* VariableStatement.cs (PopulateContext): Do not add the symbol to
the context if already defined in current context.
* CodeGenerator.cs: Renamed VariableDefined to
variable_defined_in_current_scope.
* TypeManager.cs: Reimplemented it, We use an IdentificationTable
instead of a simple Hashtable so we can handle scopes
properly. Cleaner api.
* VariableDeclaration.cs: Use new CodeGenerator api. (EmitDecl)
Add the builders to the TypeManager.
* Block.cs: Use new TypeManager api.
* FunctionDeclaration.cs: Use new TypeManager api. Take care of
scopes in the TypeManger on Emit.
* IdentificationTable.cs: (Binder) Changed the type of value from
AST to object so I can use this symbol table
anywhere. (IdentificationTable) Add current_locals stack and added
method InCurrentScope.
* expression.cs: Use new api from TypeManager, Add casts where neeeded.
This solves an infinite loop we were falling into when calling
IdentificationTable.CurrentLocals when repeated declarations where
inside function declarations.
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Block.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Block.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/Block.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -145,7 +145,7 @@
// in function's body.
//
string name = ((FunctionDeclaration)
e).func_obj.name;
- AST binding = context.Get
(Symbol.CreateSymbol (name));
+ AST binding = (AST) context.Get
(Symbol.CreateSymbol (name));
if (binding == null) {
ocurrences.Add (name, i);
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/ChangeLog
2005-02-27 22:20:23 UTC (rev 41267)
@@ -1,5 +1,33 @@
2005-02-27 Cesar Lopez Nataren <[EMAIL PROTECTED]>
+ * VariableStatement.cs (PopulateContext): Do not add the symbol to
+ the context if already defined in current context.
+
+ * CodeGenerator.cs: Renamed VariableDefined to
variable_defined_in_current_scope.
+
+ * TypeManager.cs: Reimplemented it, We use an IdentificationTable
+ instead of a simple Hashtable so we can handle scopes
+ properly. Cleaner api.
+
+ * VariableDeclaration.cs: Use new CodeGenerator api. (EmitDecl)
+ Add the builders to the TypeManager.
+
+ * Block.cs: Use new TypeManager api.
+
+ * FunctionDeclaration.cs: Use new TypeManager api. Take care of
+ scopes in the TypeManger on Emit.
+
+ * IdentificationTable.cs: (Binder) Changed the type of value from
+ AST to object so I can use this symbol table
+ anywhere. (IdentificationTable) Add current_locals stack and added
+ method InCurrentScope.
+
+ * expression.cs: Use new api from TypeManager, Add casts where neeeded.
+
+ This solves an infinite loop we were falling into when calling
+ IdentificationTable.CurrentLocals when repeated declarations where
+ inside function declarations.
+
* ast.cs: Added protected field ig to class Function.
* Block.cs: Emit function's body in a later phase since free
ocurrences of functions - at the same level of it - can be handled
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/CodeGenerator.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -488,9 +488,9 @@
ig.Emit (OpCodes.Callvirt, iact_obj.GetMethod
("GetDefaultThisObject"));
}
- internal static FieldInfo VariableDefined (string id,
EmitContext ec)
+ internal static object variable_defined_in_current_scope
(string id)
{
- return ec.type_builder.GetField (id,
BindingFlags.Public | BindingFlags.Static);
+ return TypeManager.defined_in_current_scope (id);
}
}
}
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/FunctionDeclaration.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -95,11 +95,11 @@
MethodBuilder method_builder = type.DefineMethod
(full_name, func_obj.attr, HandleReturnType,
func_obj.params_types ());
- MethodBuilder tmp = TypeManager.GetMethod (name);
+ MethodBuilder tmp = (MethodBuilder) TypeManager.Get
(name);
if (tmp == null)
- TypeManager.AddMethod (name, method_builder);
+ TypeManager.Add (name, method_builder);
else
- TypeManager.SetMethod (name, method_builder);
+ TypeManager.Set (name, method_builder);
set_custom_attr (method_builder);
this.ig = method_builder.GetILGenerator ();
@@ -109,18 +109,20 @@
FieldAttributes.Public |
FieldAttributes.Static);
else {
local_func = ig.DeclareLocal (typeof
(Microsoft.JScript.ScriptFunction));
- TypeManager.AddLocal (name, local_func);
+ TypeManager.Add (name, local_func);
}
build_closure (ec, full_name);
}
internal override void Emit (EmitContext ec)
{
+ TypeManager.BeginScope ();
ILGenerator old_ig = ec.ig;
ec.ig = this.ig;
func_obj.body.Emit (ec);
this.ig.Emit (OpCodes.Ret);
ec.ig = old_ig;
+ TypeManager.EndScope ();
}
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/IdentificationTable.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -80,7 +80,7 @@
/// Associates a symbol to its declaring object.
/// </summary>
internal class Binder {
- AST value;
+ object value;
Symbol prev_top;
/// <remarks>
@@ -89,7 +89,7 @@
/// </remarks>
Binder tail;
- internal AST Value {
+ internal object Value {
get { return value; }
set { this.value = value; }
}
@@ -102,7 +102,7 @@
get { return prev_top; }
}
- internal Binder (AST value, Symbol prev_top, Binder tail)
+ internal Binder (object value, Symbol prev_top, Binder tail)
{
this.value = value;
this.prev_top = prev_top;
@@ -119,8 +119,12 @@
private Symbol top;
private Binder marks;
+ Stack current_locals;
+
internal IdentificationTable ()
{
+ current_locals = new Stack ();
+ current_locals.Push (new Hashtable ());
}
internal bool Contains (Symbol key)
@@ -132,7 +136,7 @@
/// <summary>
/// Gets the object associated to the symbol in the table
/// </summary>
- internal AST Get (Symbol key)
+ internal object Get (Symbol key)
{
Binder e = (Binder) dict [key];
@@ -145,8 +149,8 @@
/// <summary>
/// Bind a key
/// </summary>
- internal void Enter (Symbol key, AST value)
- {
+ internal void Enter (Symbol key, object value)
+ {
Binder e = (Binder) dict [key];
/// <remarks>
@@ -169,6 +173,7 @@
//
top = key;
}
+ ((Hashtable) current_locals.Peek ()).Add (key.Value,
"");
}
/// <summary>
@@ -191,6 +196,8 @@
{
marks = new Binder (null, top, marks);
top = null;
+
+ current_locals.Push (new Hashtable ());
}
/// <summary>
@@ -230,6 +237,8 @@
// delete the latest scope mark
//
marks = marks.Tail;
+
+ current_locals.Pop ();
}
internal AST [] CurrentLocals {
@@ -250,6 +259,12 @@
}
}
+ internal bool InCurrentScope (Symbol id)
+ {
+ Hashtable hash = (Hashtable) current_locals.Peek ();
+ return hash.ContainsKey (id.Value) && hash [id.Value]
== "";
+ }
+
internal void BuildGlobalEnv ()
{
//
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/TypeManager.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -16,76 +16,44 @@
internal class TypeManager {
- enum ManagedType {
- Method,
- Local
- }
-
- enum Operation {
- Add,
- Get,
- Set
- }
+ static IdentificationTable infos;
- static Hashtable methods;
- static Hashtable locals;
-
static TypeManager ()
{
- methods = new Hashtable ();
- locals = new Hashtable ();
+ infos = new IdentificationTable ();
}
- internal static void AddMethod (string name, MethodBuilder
builder)
+ internal static void BeginScope ()
{
- Operate (Operation.Add, ManagedType.Method, name,
builder);
+ infos.BeginScope ();
}
- internal static MethodBuilder GetMethod (string name)
+ internal static void EndScope ()
{
- return (MethodBuilder) Operate (Operation.Get,
ManagedType.Method, name, null);
+ infos.EndScope ();
}
- internal static void SetMethod (string name, MethodBuilder
builder)
+ internal static void Add (string name, object o)
{
- Operate (Operation.Set, ManagedType.Method, name,
builder);
+ infos.Enter (Symbol.CreateSymbol (name), o);
}
- internal static void AddLocal (string name, LocalBuilder
loc_builder)
+ internal static object Get (string name)
{
- Operate (Operation.Add, ManagedType.Local, name,
loc_builder);
+ return infos.Get (Symbol.CreateSymbol (name));
}
- internal static LocalBuilder GetLocal (string name)
+ internal static void Set (string name, object o)
{
- return (LocalBuilder) Operate (Operation.Get,
ManagedType.Local, name, null);
+ object obj = Get (name);
+ obj = o;
}
- static object Operate (Operation op, ManagedType managed_type,
string name, object obj)
+ internal static object defined_in_current_scope (string id)
{
- switch (managed_type) {
- case ManagedType.Method:
- if (op == Operation.Add) {
- methods.Add (name, obj);
- return null;
- } else if (op == Operation.Get)
- return methods [name];
- else if (op == Operation.Set) {
- methods [name] = obj;
- return null;
- }
- break;
- case ManagedType.Local:
- if (op == Operation.Add) {
- locals.Add (name, obj);
- return null;
- } else if (op == Operation.Get)
- return locals [name];
- break;
- default:
- return null;
- }
- throw new Exception ("Operate, invalid arguments were
supplied.");
+ if (infos.InCurrentScope (Symbol.CreateSymbol (id)))
+ return Get (id);
+ return null;
}
}
}
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableDeclaration.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -79,7 +79,8 @@
internal void EmitDecl (EmitContext ec)
{
object var;
- if ((var = CodeGenerator.VariableDefined (id, ec)) !=
null) {
+
+ if ((var =
CodeGenerator.variable_defined_in_current_scope (id)) != null) {
Type t = var.GetType ();
if (t == typeof (FieldBuilder))
field_info = (FieldBuilder) var;
@@ -87,7 +88,7 @@
local_builder = (LocalBuilder) var;
return;
}
-
+
ILGenerator ig = ec.ig;
if (parent == null || (parent.GetType () != typeof
(FunctionDeclaration)
&& parent.GetType () != typeof
(FunctionExpression))) {
@@ -95,9 +96,12 @@
TypeBuilder type_builder = ec.type_builder;
field_builder = type_builder.DefineField (id,
this.type, FieldAttributes.Public | FieldAttributes.Static);
+ TypeManager.Add (id, field_builder);
field_info = field_builder;
- } else
+ } else {
local_builder = ig.DeclareLocal (type);
+ TypeManager.Add (id, local_builder);
+ }
}
internal override void Emit (EmitContext ec)
Modified:
trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/VariableStatement.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -65,7 +65,6 @@
internal void EmitVariableDecls (EmitContext ec)
{
int n = var_decls.Count;
-
for (int i = 0; i < n; i++)
((VariableDeclaration) var_decls [i]).EmitDecl
(ec);
}
@@ -82,10 +81,14 @@
{
VariableDeclaration tmp_decl;
int n = var_decls.Count;
+ Symbol id;
for (int i = 0; i < n; i++) {
tmp_decl = (VariableDeclaration) var_decls [i];
- context.Enter (Symbol.CreateSymbol
(tmp_decl.id), tmp_decl);
+ id = Symbol.CreateSymbol (tmp_decl.id);
+ if (context.InCurrentScope (id))
+ continue;
+ context.Enter (id, tmp_decl);
}
}
Modified: trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
===================================================================
--- trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
2005-02-27 22:12:07 UTC (rev 41266)
+++ trunk/mcs/class/Microsoft.JScript/Microsoft.JScript/expression.cs
2005-02-27 22:20:23 UTC (rev 41267)
@@ -586,7 +586,7 @@
EmitInvoke (ec);
}
} else if (bind_type == typeof (FunctionDeclaration) ||
bind_type == typeof (FunctionExpression)) {
- MethodBuilder method = TypeManager.GetMethod
((binding as Function).func_obj.name);
+ MethodBuilder method = (MethodBuilder)
TypeManager.Get ((binding as Function).func_obj.name);
emit_func_call (method, ec);
return;
} else if (binding is MemberInfo) {
@@ -1038,8 +1038,8 @@
void load_script_func (EmitContext ec, FunctionDeclaration
binding)
{
- if (InFunction && (TypeManager.GetLocal
(binding.func_obj.name) != null))
- ec.ig.Emit (OpCodes.Ldloc, TypeManager.GetLocal
(binding.func_obj.name));
+ if (InFunction && (TypeManager.Get
(binding.func_obj.name) != null))
+ ec.ig.Emit (OpCodes.Ldloc, (LocalBuilder)
TypeManager.Get (binding.func_obj.name));
else {
TypeBuilder type = ec.type_builder;
FieldInfo method = type.GetField
(binding.func_obj.name);
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches