Author: raja
Date: 2005-03-29 07:27:05 -0500 (Tue, 29 Mar 2005)
New Revision: 42330
Added:
trunk/mcs/errors/cs0136-3.cs
trunk/mcs/errors/cs0136-4.cs
Modified:
trunk/mcs/errors/ChangeLog
trunk/mcs/errors/errors.txt
trunk/mcs/errors/gmcs-expect-no-error
trunk/mcs/mcs/ChangeLog
trunk/mcs/mcs/cs-parser.jay
trunk/mcs/mcs/ecore.cs
trunk/mcs/mcs/statement.cs
Log:
In mcs:
Fix #47991. Remove a TODO.
* statement.cs (Block.Toplevel): Make into a field.
(Block.Parameters): Move into ToplevelBlock.
(Block.known_variables): Rename from child_variable_names.
(Block.Block): Remove variants that take Parameters. Initialize
'Toplevel' with the immediately surrounding toplevel block.
(Block.AddKnownVariable): Rename from AddChildVariableName. Add a
LocalInfo parameter.
(Block.GetKnownVariableInfo): New.
(Block.IsVariableNameUsedInChildBlock): Update.
(Block.IsVariableNameUsedInBlock): New. Checks if a name is used in
the block, even though it may not be in scope.
(Block.AddVariable): Remove Parameters parameter. Use
Toplevel.Parameters instead.
(Block.AddConstant): Remove Parameters parameter.
(Block.GetParameterReference): Update to use Toplevel.Parameters.
(Block.IsParamaterReference): Likewise.
(Block.IsLocalParameter): Likewise. Simplify a lot.
(ToplevelBlock.Parameters): New. Moved from Block.
(ToplevelBlock.ToplevelBlock): Update to changes. Always
initialize Parameters to a non-null value.
* cs-parser.jay: Update to changes.
* ecore.cs (SimpleName.SimpleNameResolve): Emit cs0136 error for
simple names that mean different things in the same block. Use
Block.IsVariableNameUsedInBlock.
In errors:
* cs0136-3.cs, cs0136-4.cs: New tests from #47991.
Modified: trunk/mcs/errors/ChangeLog
===================================================================
--- trunk/mcs/errors/ChangeLog 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/errors/ChangeLog 2005-03-29 12:27:05 UTC (rev 42330)
@@ -1,3 +1,7 @@
+2005-03-29 Raja R Harinath <[EMAIL PROTECTED]>
+
+ * cs0136-3.cs, cs0136-4.cs: New tests from #47991.
+
2005-03-18 Raja R Harinath <[EMAIL PROTECTED]>
* Makefile (DISTFILES): Don't distribute TestRunner.exe.
Added: trunk/mcs/errors/cs0136-3.cs
===================================================================
--- trunk/mcs/errors/cs0136-3.cs 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/errors/cs0136-3.cs 2005-03-29 12:27:05 UTC (rev 42330)
@@ -0,0 +1,11 @@
+// cs0136: 'y' has a different meaning later in the block
+// Line: 8
+
+class X
+{
+ static void y () { }
+ static void Main () {
+ y ();
+ int y = 5;
+ }
+}
Added: trunk/mcs/errors/cs0136-4.cs
===================================================================
--- trunk/mcs/errors/cs0136-4.cs 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/errors/cs0136-4.cs 2005-03-29 12:27:05 UTC (rev 42330)
@@ -0,0 +1,11 @@
+// cs0136: 'y' has a different meaning later in the block
+// Line: 8
+
+class X
+{
+ static int y;
+ static void Main () {
+ y = 10;
+ int y = 5;
+ }
+}
Modified: trunk/mcs/errors/errors.txt
===================================================================
--- trunk/mcs/errors/errors.txt 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/errors/errors.txt 2005-03-29 12:27:05 UTC (rev 42330)
@@ -81,6 +81,11 @@
-27 Attribute resolved to two different types
+-28 (warning) Somehow there's a function marked override, and
+ doesn't override anything. Warn that this method will be
+ ignored during overload resolution. (marked override == in IL,
+ it has a 'virtual', but no 'newslot')
+
----------
Errors that we have allocated that will have corresponding errors in
Modified: trunk/mcs/errors/gmcs-expect-no-error
===================================================================
--- trunk/mcs/errors/gmcs-expect-no-error 2005-03-29 11:42:18 UTC (rev
42329)
+++ trunk/mcs/errors/gmcs-expect-no-error 2005-03-29 12:27:05 UTC (rev
42330)
@@ -12,6 +12,8 @@
cs0108-3.cs
cs0121-3.cs
cs0121-4.cs
+cs0136-3.cs
+cs0136-4.cs
cs0157-5.cs
cs0161-2.cs # new in GMCS
cs0164.cs
Modified: trunk/mcs/mcs/ChangeLog
===================================================================
--- trunk/mcs/mcs/ChangeLog 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/mcs/ChangeLog 2005-03-29 12:27:05 UTC (rev 42330)
@@ -1,3 +1,31 @@
+2005-03-29 Raja R Harinath <[EMAIL PROTECTED]>
+
+ Fix #47991. Remove a TODO.
+ * statement.cs (Block.Toplevel): Make into a field.
+ (Block.Parameters): Move into ToplevelBlock.
+ (Block.known_variables): Rename from child_variable_names.
+ (Block.Block): Remove variants that take Parameters. Initialize
+ 'Toplevel' with the immediately surrounding toplevel block.
+ (Block.AddKnownVariable): Rename from AddChildVariableName. Add a
+ LocalInfo parameter.
+ (Block.GetKnownVariableInfo): New.
+ (Block.IsVariableNameUsedInChildBlock): Update.
+ (Block.IsVariableNameUsedInBlock): New. Checks if a name is used in
+ the block, even though it may not be in scope.
+ (Block.AddVariable): Remove Parameters parameter. Use
+ Toplevel.Parameters instead.
+ (Block.AddConstant): Remove Parameters parameter.
+ (Block.GetParameterReference): Update to use Toplevel.Parameters.
+ (Block.IsParamaterReference): Likewise.
+ (Block.IsLocalParameter): Likewise. Simplify a lot.
+ (ToplevelBlock.Parameters): New. Moved from Block.
+ (ToplevelBlock.ToplevelBlock): Update to changes. Always
+ initialize Parameters to a non-null value.
+ * cs-parser.jay: Update to changes.
+ * ecore.cs (SimpleName.SimpleNameResolve): Emit cs0136 error for
+ simple names that mean different things in the same block. Use
+ Block.IsVariableNameUsedInBlock.
+
2005-03-28 Raja R Harinath <[EMAIL PROTECTED]>
* typemanager.cs (TypeHandle.BaseType): Make into an IMemberContainer.
Modified: trunk/mcs/mcs/cs-parser.jay
===================================================================
--- trunk/mcs/mcs/cs-parser.jay 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/mcs/cs-parser.jay 2005-03-29 12:27:05 UTC (rev 42330)
@@ -3515,8 +3515,7 @@
current_block = new ToplevelBlock ((ToplevelBlock)
top_current_block, current_local_parameters, lexer.Location);
top_current_block = current_block;
} else {
- current_block = new Block (current_block,
current_local_parameters,
- lexer.Location, Location.Null);
+ current_block = new Block (current_block, lexer.Location,
Location.Null);
}
}
opt_statement_list CLOSE_BRACE
@@ -3928,8 +3927,7 @@
LocalInfo vi;
- vi = current_block.AddVariable (
- type, decl.identifier,
current_local_parameters, decl.Location);
+ vi = current_block.AddVariable (type,
decl.identifier, decl.Location);
if (vi == null)
continue;
@@ -4039,7 +4037,7 @@
Location l = lexer.Location;
LocalInfo vi;
- vi = foreach_block.AddVariable ((Expression) $3, (string) $4,
current_local_parameters, l);
+ vi = foreach_block.AddVariable ((Expression) $3, (string) $4,
l);
if (vi != null) {
vi.ReadOnly = true;
@@ -4341,7 +4339,7 @@
Pair p = (Pair) list [i];
LocalInfo v;
- v = current_block.AddVariable (type, (string)
p.First,current_local_parameters, l);
+ v = current_block.AddVariable (type, (string) p.First,
l);
if (v == null)
continue;
@@ -4426,9 +4424,7 @@
foreach (VariableDeclaration decl in var_declarators){
- LocalInfo vi = current_block.AddVariable (
- type, decl.identifier,
- current_local_parameters,
decl.Location);
+ LocalInfo vi = current_block.AddVariable (type,
decl.identifier, decl.Location);
if (vi == null)
continue;
vi.ReadOnly = true;
@@ -4668,7 +4664,7 @@
foreach (VariableDeclaration decl in variable_declarators){
- if (implicit_block.AddVariable (type, decl.identifier,
current_local_parameters, decl.Location) != null) {
+ if (implicit_block.AddVariable (type, decl.identifier,
decl.Location) != null) {
if (decl.expression_or_array_initializer != null){
if (inits == null)
inits = new ArrayList (4);
@@ -4714,8 +4710,7 @@
implicit_block = current_block;
foreach (VariableDeclaration decl in declarators){
- implicit_block.AddConstant (type, decl.identifier, (Expression)
decl.expression_or_array_initializer,
- current_local_parameters,
decl.Location);
+ implicit_block.AddConstant (type, decl.identifier, (Expression)
decl.expression_or_array_initializer, decl.Location);
}
return implicit_block;
Modified: trunk/mcs/mcs/ecore.cs
===================================================================
--- trunk/mcs/mcs/ecore.cs 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/mcs/ecore.cs 2005-03-29 12:27:05 UTC (rev 42330)
@@ -2079,6 +2079,15 @@
"'{0}' has a different
meaning in a child block", Name);
return null;
}
+
+ if (!(e is LocalVariableReference) &&
current_block.IsVariableNameUsedInBlock (Name)) {
+ // Catch some false positives, e.g.,
when a local variable resolves to a constant.
+ LocalInfo vi =
current_block.GetLocalInfo (Name);
+ if (vi == null) {
+ Report.Error (136, Location,
"'{0}' has a different meaning later in the block", Name);
+ return null;
+ }
+ }
}
return e;
Modified: trunk/mcs/mcs/statement.cs
===================================================================
--- trunk/mcs/mcs/statement.cs 2005-03-29 11:42:18 UTC (rev 42329)
+++ trunk/mcs/mcs/statement.cs 2005-03-29 12:27:05 UTC (rev 42330)
@@ -1170,6 +1170,8 @@
public readonly Location StartLocation;
public Location EndLocation = Location.Null;
+ public readonly ToplevelBlock Toplevel;
+
[Flags]
public enum Flags {
Implicit = 1,
@@ -1248,11 +1250,6 @@
//
// Keeps track of constants
Hashtable constants;
-
- //
- // The parameters for the block, this is only needed on the
toplevel block really
- // TODO: move `parameters' into ToplevelBlock
- Parameters parameters;
//
// If this is a switch section, the enclosing switch block.
@@ -1271,42 +1268,33 @@
: this (parent, flags, Location.Null, Location.Null)
{ }
- public Block (Block parent, Flags flags, Parameters parameters)
- : this (parent, flags, parameters, Location.Null,
Location.Null)
- { }
-
public Block (Block parent, Location start, Location end)
: this (parent, (Flags) 0, start, end)
{ }
- public Block (Block parent, Parameters parameters, Location
start, Location end)
- : this (parent, (Flags) 0, parameters, start, end)
- { }
-
public Block (Block parent, Flags flags, Location start,
Location end)
- : this (parent, flags,
Parameters.EmptyReadOnlyParameters, start, end)
- { }
-
- public Block (Block parent, Flags flags, Parameters parameters,
- Location start, Location end)
{
if (parent != null)
parent.AddChild (this);
this.Parent = parent;
this.flags = flags;
- this.parameters = parameters;
this.StartLocation = start;
this.EndLocation = end;
this.loc = start;
this_id = id++;
statements = new ArrayList ();
+ if ((flags & Flags.IsToplevel) != 0)
+ Toplevel = (ToplevelBlock) this;
+ else
+ Toplevel = parent.Toplevel;
+
if (parent != null && Implicit) {
- if (parent.child_variable_names == null)
- parent.child_variable_names = new
Hashtable();
+ if (parent.known_variables == null)
+ parent.known_variables = new Hashtable
();
// share with parent
- child_variable_names =
parent.child_variable_names;
+ known_variables = parent.known_variables;
}
}
@@ -1453,30 +1441,47 @@
}
}
- Hashtable child_variable_names;
+ Hashtable known_variables;
// <summary>
- // Marks a variable with name @name as being used in a child
block.
+ // Marks a variable with name @name as being used in this or
a child block.
// If a variable name has been used in a child block, it's
illegal to
// declare a variable with the same name in the current block.
// </summary>
- public void AddChildVariableName (string name)
+ void AddKnownVariable (string name, LocalInfo info)
{
- if (child_variable_names == null)
- child_variable_names = new Hashtable ();
+ if (known_variables == null)
+ known_variables = new Hashtable ();
- child_variable_names [name] = null;
+ known_variables [name] = info;
}
+ public LocalInfo GetKnownVariableInfo (string name)
+ {
+ if (known_variables == null ||
!known_variables.Contains (name))
+ return null;
+ return (LocalInfo) known_variables [name];
+ }
+
// <summary>
// Checks whether a variable name has already been used in a
child block.
// </summary>
public bool IsVariableNameUsedInChildBlock (string name)
{
- if (child_variable_names == null)
- return false;
+ LocalInfo vi = GetKnownVariableInfo (name);
+ // Cheeky little test that knows that 'known_variables'
is shared between
+ // an implicit block and its enclosing real block.
+ return vi != null && known_variables !=
vi.Block.known_variables;
+ }
- return child_variable_names.Contains (name);
+ // <summary>
+ // Checks whether a variable name has already been used in
this block, possibly by
+ // an implicit block.
+ // </summary>
+ public bool IsVariableNameUsedInBlock (string name)
+ {
+ LocalInfo vi = GetKnownVariableInfo (name);
+ return vi != null && known_variables ==
vi.Block.known_variables;
}
// <summary>
@@ -1503,7 +1508,7 @@
return this_variable;
}
- public LocalInfo AddVariable (Expression type, string name,
Parameters pars, Location l)
+ public LocalInfo AddVariable (Expression type, string name,
Location l)
{
if (variables == null)
variables = new Hashtable ();
@@ -1538,27 +1543,23 @@
return null;
}
- if (pars != null) {
- int idx;
- Parameter p = pars.GetParameterByName (name,
out idx);
- if (p != null) {
- Report.Error (136, l, "A local variable
named `" + name + "' " +
- "cannot be declared in
this scope since it would " +
- "give a different meaning
to `" + name + "', which " +
- "is already used in a
`parent or current' scope to " +
- "denote something else");
- return null;
- }
+ int idx;
+ Parameter p = Toplevel.Parameters.GetParameterByName
(name, out idx);
+ if (p != null) {
+ Report.Error (136, l, "A local variable named
`" + name + "' " +
+ "cannot be declared in this scope
since it would " +
+ "give a different meaning to `" +
name + "', which " +
+ "is already used in a `parent or
current' scope to " +
+ "denote something else");
+ return null;
}
vi = new LocalInfo (type, name, this, l);
variables.Add (name, vi);
- // Mark 'name' as "used by a child block" in every
surrounding block
- if (cur != null)
- for (Block par = cur.Parent; par != null; par =
par.Parent)
- par.AddChildVariableName (name);
+ for (Block b = cur; b != null; b = b.Parent)
+ b.AddKnownVariable (name, vi);
if ((flags & Flags.VariablesInitialized) != 0)
throw new Exception ();
@@ -1567,9 +1568,9 @@
return vi;
}
- public bool AddConstant (Expression type, string name,
Expression value, Parameters pars, Location l)
+ public bool AddConstant (Expression type, string name,
Expression value, Location l)
{
- if (AddVariable (type, name, pars, l) == null)
+ if (AddVariable (type, name, l) == null)
return false;
if (constants == null)
@@ -1637,22 +1638,15 @@
//
public ParameterReference GetParameterReference (string name,
Location loc)
{
- Block b = this;
+ Parameter par;
+ int idx;
- do {
- Parameters pars = b.parameters;
-
- if (pars != null){
- Parameter par;
- int idx;
-
- par = pars.GetParameterByName (name,
out idx);
- if (par != null){
- return new ParameterReference
(pars, this, idx, name, loc);
- }
- }
- b = b.Parent;
- } while (b != null);
+ for (Block b = this; b != null; b = b.Toplevel.Parent) {
+ Parameters pars = b.Toplevel.Parameters;
+ par = pars.GetParameterByName (name, out idx);
+ if (par != null)
+ return new ParameterReference (pars,
this, idx, name, loc);
+ }
return null;
}
@@ -1662,24 +1656,7 @@
//
public bool IsLocalParameter (string name)
{
- Block b = this;
- int toplevel_count = 0;
-
- do {
- if (this is ToplevelBlock)
- toplevel_count++;
-
- Parameters pars = b.parameters;
- if (pars != null){
- if (pars.GetParameterByName (name) !=
null)
- return true;
- return false;
- }
- if (toplevel_count > 0)
- return false;
- b = b.Parent;
- } while (b != null);
- return false;
+ return Toplevel.Parameters.GetParameterByName (name) !=
null;
}
//
@@ -1687,16 +1664,14 @@
//
public bool IsParameterReference (string name)
{
- Block b = this;
+ Parameter par;
+ int idx;
- do {
- Parameters pars = b.parameters;
-
- if (pars != null)
- if (pars.GetParameterByName (name) !=
null)
- return true;
- b = b.Parent;
- } while (b != null);
+ for (Block b = this; b != null; b = b.Toplevel.Parent) {
+ par = b.Toplevel.Parameters.GetParameterByName
(name, out idx);
+ if (par != null)
+ return true;
+ }
return false;
}
@@ -2104,19 +2079,6 @@
ec.CurrentBlock = prev_block;
}
- public ToplevelBlock Toplevel {
- get {
- Block b = this;
- while (b.Parent != null){
- if ((b.flags & Flags.IsToplevel) != 0)
- break;
- b = b.Parent;
- }
-
- return (ToplevelBlock) b;
- }
- }
-
//
// Returns true if we ar ea child of `b'.
//
@@ -2152,7 +2114,11 @@
Hashtable capture_contexts;
static int did = 0;
-
+
+ //
+ // The parameters for the block.
+ //
+ public readonly Parameters Parameters;
public void RegisterCaptureContext (CaptureContext cc)
{
@@ -2182,25 +2148,31 @@
// parents
//
public ToplevelBlock (ToplevelBlock container, Parameters
parameters, Location start) :
- base (null, Flags.IsToplevel, parameters, start,
Location.Null)
+ this (container, (Flags) 0, parameters, start)
{
- Container = container;
}
public ToplevelBlock (Parameters parameters, Location start) :
- base (null, Flags.IsToplevel, parameters, start,
Location.Null)
+ this (null, (Flags) 0, parameters, start)
{
}
public ToplevelBlock (Flags flags, Parameters parameters,
Location start) :
- base (null, flags | Flags.IsToplevel, parameters,
start, Location.Null)
+ this (null, flags, parameters, start)
{
}
- public ToplevelBlock (Location loc) : base (null,
Flags.IsToplevel, loc, loc)
+ public ToplevelBlock (ToplevelBlock container, Flags flags,
Parameters parameters, Location start) :
+ base (null, flags | Flags.IsToplevel, start,
Location.Null)
{
+ Parameters = parameters == null ?
Parameters.EmptyReadOnlyParameters : parameters;
+ Container = container;
}
+ public ToplevelBlock (Location loc) : this (null, (Flags) 0,
null, loc)
+ {
+ }
+
public void SetHaveAnonymousMethods (Location loc,
AnonymousMethod host)
{
if (capture_context == null)
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches