{
ec.Mark (loc, true);
DoEmit (ec);
- }
+ }
+
+ //
+ // This routine must be overrided in derived classes and make copies
+ // of all the data that might be modified if resolved
+ //
+ protected virtual void CloneTo (CloneContext clonectx, Statement target)
+ {
+ throw new InternalErrorException ("{0} does not implement Statement.CloneTo", this.GetType ());
+ }
+
+ public Statement Clone (CloneContext clonectx)
+ {
+ Statement s = (Statement) this.MemberwiseClone ();
+ CloneTo (clonectx, s);
+ return s;
+ }
+
+ public Statement PerformClone ()
+ {
+ CloneContext clonectx = new CloneContext ();
+
+ return Clone (clonectx);
+ }
+
}
+ //
+ // This class is used during the Statement.Clone operation
+ // to remap objects that have been cloned.
+ //
+ // Since blocks are cloned by Block.Clone, we need a way for
+ // expressions that must reference the block to be cloned
+ // pointing to the new cloned block.
+ //
+ public class CloneContext {
+ Hashtable block_map = new Hashtable ();
+ Hashtable variable_map;
+
+ public void AddBlockMap (Block from, Block to)
+ {
+ if (block_map.Contains (from))
+ return;
+ block_map [from] = to;
+ }
+
+ public Block LookupBlock (Block from)
+ {
+ Block result = (Block) block_map [from];
+
+ if (result == null){
+ result = (Block) from.Clone (this);
+ block_map [from] = result;
+ }
+
+ return result;
+ }
+
+ ///
+ /// Remaps block to cloned copy if one exists.
+ ///
+ public Block RemapBlockCopy (Block from)
+ {
+ Block mapped_to = (Block)block_map[from];
+ if (mapped_to == null)
+ return from;
+
+ return mapped_to;
+ }
+
+ public void AddVariableMap (LocalInfo from, LocalInfo to)
+ {
+ if (variable_map == null)
+ variable_map = new Hashtable ();
+
+ if (variable_map.Contains (from))
+ return;
+ variable_map [from] = to;
+ }
+
+ public LocalInfo LookupVariable (LocalInfo from)
+ {
+ LocalInfo result = (LocalInfo) variable_map [from];
+
+ if (result == null)
+ throw new Exception ("LookupVariable: looking up a variable that has not been registered yet");
+
+ return result;
+ }
+ }
+
public sealed class EmptyStatement : Statement {
private EmptyStatement () {}
protected override void DoEmit (EmitContext ec)
{
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement target)
+ {
+ // nothing needed.
+ }
}
public class If : Statement {
bool is_true_ret;
- public If (Expression expr, Statement trueStatement, Location l)
+ public If (Expression expr, Statement true_statement, Location l)
{
this.expr = expr;
- TrueStatement = trueStatement;
+ TrueStatement = true_statement;
loc = l;
}
public If (Expression expr,
- Statement trueStatement,
- Statement falseStatement,
+ Statement true_statement,
+ Statement false_statement,
Location l)
{
this.expr = expr;
- TrueStatement = trueStatement;
- FalseStatement = falseStatement;
+ TrueStatement = true_statement;
+ FalseStatement = false_statement;
loc = l;
}
ok &= TrueStatement.Resolve (ec);
- is_true_ret = ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable;
+ is_true_ret = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
ec.CurrentBranching.CreateSibling ();
ig.MarkLabel (false_target);
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ If target = (If) t;
+
+ target.expr = expr.Clone (clonectx);
+ target.TrueStatement = TrueStatement.Clone (clonectx);
+ if (FalseStatement != null)
+ target.FalseStatement = FalseStatement.Clone (clonectx);
+ }
}
public class Do : Statement {
public Expression expr;
- public readonly Statement EmbeddedStatement;
+ public Statement EmbeddedStatement;
bool infinite;
- public Do (Statement statement, Expression boolExpr, Location l)
+ public Do (Statement statement, Expression bool_expr, Location l)
{
- expr = boolExpr;
+ expr = bool_expr;
EmbeddedStatement = statement;
loc = l;
}
ec.StartFlowBranching (FlowBranching.BranchingType.Loop, loc);
- bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable;
+ bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
if (!EmbeddedStatement.Resolve (ec))
ok = false;
ec.EndFlowBranching ();
- if (ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable && !was_unreachable)
+ if (ec.CurrentBranching.CurrentUsageVector.IsUnreachable && !was_unreachable)
Report.Warning (162, 2, expr.Location, "Unreachable code detected");
expr = Expression.ResolveBoolean (ec, expr, loc);
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Do target = (Do) t;
+
+ target.EmbeddedStatement = EmbeddedStatement.Clone (clonectx);
+ target.expr = expr.Clone (clonectx);
+ }
}
public class While : Statement {
public Expression expr;
- public readonly Statement Statement;
+ public Statement Statement;
bool infinite, empty;
- public While (Expression boolExpr, Statement statement, Location l)
+ public While (Expression bool_expr, Statement statement, Location l)
{
- this.expr = boolExpr;
+ this.expr = bool_expr;
Statement = statement;
loc = l;
}
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ While target = (While) t;
+
+ target.expr = expr.Clone (clonectx);
+ target.Statement = Statement.Clone (clonectx);
+ }
}
public class For : Statement {
Expression Test;
- readonly Statement InitStatement;
- readonly Statement Increment;
- public readonly Statement Statement;
+ Statement InitStatement;
+ Statement Increment;
+ public Statement Statement;
bool infinite, empty;
- public For (Statement initStatement,
+ public For (Statement init_statement,
Expression test,
Statement increment,
Statement statement,
Location l)
{
- InitStatement = initStatement;
+ InitStatement = init_statement;
Test = test;
Increment = increment;
Statement = statement;
if (!infinite)
ec.CurrentBranching.CreateSibling ();
- bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable;
+ bool was_unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
ec.StartFlowBranching (FlowBranching.BranchingType.Embedded, loc);
if (!Statement.Resolve (ec))
ec.EndFlowBranching ();
if (Increment != null){
- if (ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable) {
+ if (ec.CurrentBranching.CurrentUsageVector.IsUnreachable) {
if (!Increment.ResolveUnreachable (ec, !was_unreachable))
ok = false;
} else {
ec.LoopBegin = old_begin;
ec.LoopEnd = old_end;
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ For target = (For) t;
+
+ if (InitStatement != null)
+ target.InitStatement = InitStatement.Clone (clonectx);
+ if (Test != null)
+ target.Test = Test.Clone (clonectx);
+ if (Increment != null)
+ target.Increment = Increment.Clone (clonectx);
+ target.Statement = Statement.Clone (clonectx);
+ }
}
public class StatementExpression : Statement {
{
return "StatementExpression (" + expr + ")";
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ StatementExpression target = (StatementExpression) t;
+
+ target.expr = (ExpressionStatement) expr.Clone (clonectx);
+ }
}
/// <summary>
/// Implements the return statement
/// </summary>
public class Return : Statement {
- public Expression Expr;
+ Expression Expr;
+ bool unwind_protect;
public Return (Expression expr, Location l)
{
Expr = expr;
loc = l;
}
-
- bool unwind_protect;
-
- public override bool Resolve (EmitContext ec)
+
+ bool DoResolve (EmitContext ec)
{
+ if (Expr == null) {
+ if (ec.ReturnType == TypeManager.void_type)
+ return true;
+
+ Error (126, "An object of a type convertible to `{0}' is required " +
+ "for the return statement",
+ TypeManager.CSharpName (ec.ReturnType));
+ return false;
+ }
+
AnonymousContainer am = ec.CurrentAnonymousMethod;
if ((am != null) && am.IsIterator && ec.InIterator) {
Report.Error (1622, loc, "Cannot return a value from iterators. Use the yield return " +
- "statement to return a value, or yield break to end the iteration");
- return false;
+ "statement to return a value, or yield break to end the iteration");
}
- if (ec.ReturnType == null){
- if (Expr != null){
- if (ec.CurrentAnonymousMethod != null){
- Report.Error (1662, loc,
- "Cannot convert anonymous method block to delegate type `{0}' because some of the return types in the block are not implicitly convertible to the delegate return type",
- ec.CurrentAnonymousMethod.GetSignatureForError ());
- }
- Error (127, "A return keyword must not be followed by any expression when method returns void");
- return false;
- }
- } else {
- if (Expr == null){
- Error (126, "An object of a type convertible to `{0}' is required " +
- "for the return statement",
- TypeManager.CSharpName (ec.ReturnType));
- return false;
- }
+ if (am == null && ec.ReturnType == TypeManager.void_type) {
+ MemberCore mc = ec.ResolveContext as MemberCore;
+ Report.Error (127, loc, "`{0}': A return keyword must not be followed by any expression when method returns void",
+ mc.GetSignatureForError ());
+ }
- Expr = Expr.Resolve (ec);
- if (Expr == null)
- return false;
+ Expr = Expr.Resolve (ec);
+ if (Expr == null)
+ return false;
- if (Expr.Type != ec.ReturnType) {
+ if (Expr.Type != ec.ReturnType) {
+ if (ec.InferReturnType) {
+ ec.ReturnType = Expr.Type;
+ } else {
Expr = Convert.ImplicitConversionRequired (
ec, Expr, ec.ReturnType, loc);
- if (Expr == null)
+
+ if (Expr == null) {
+ if (am != null) {
+ Report.Error (1662, loc,
+ "Cannot convert `{0}' to delegate type `{1}' because some of the return types in the block are not implicitly convertible to the delegate return type",
+ am.ContainerType, am.GetSignatureForError ());
+ }
return false;
+ }
}
}
- int errors = Report.Errors;
+ return true;
+ }
+
+ public override bool Resolve (EmitContext ec)
+ {
+ if (!DoResolve (ec))
+ return false;
+
unwind_protect = ec.CurrentBranching.AddReturnOrigin (ec.CurrentBranching.CurrentUsageVector, loc);
if (unwind_protect)
ec.NeedReturnLabel ();
- ec.CurrentBranching.CurrentUsageVector.Return ();
- return errors == Report.Errors;
+ ec.CurrentBranching.CurrentUsageVector.Goto ();
+ return true;
}
protected override void DoEmit (EmitContext ec)
else
ec.ig.Emit (OpCodes.Ret);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Return target = (Return) t;
+ // It's null for simple return;
+ if (Expr != null)
+ target.Expr = Expr.Clone (clonectx);
+ }
}
public class Goto : Statement {
}
if (!ec.Switch.GotDefault){
- Report.Error (159, loc, "No such label `default:' within the scope of the goto statement");
+ FlowBranchingBlock.Error_UnknownLabel (loc, "default");
return;
}
ec.ig.Emit (OpCodes.Br, ec.Switch.DefaultTarget);
sl = (SwitchLabel) ec.Switch.Elements [val];
if (sl == null){
- Report.Error (159, loc, "No such label `case {0}:' within the scope of the goto statement", c.GetValue () == null ? "null" : val.ToString ());
+ FlowBranchingBlock.Error_UnknownLabel (loc, "case " +
+ (c.GetValue () == null ? "null" : val.ToString ()));
return false;
}
{
ec.ig.Emit (OpCodes.Br, sl.GetILLabelCode (ec));
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ GotoCase target = (GotoCase) t;
+
+ target.expr = expr.Clone (clonectx);
+ target.sl = sl.Clone (clonectx);
+ }
}
public class Throw : Statement {
public override bool Resolve (EmitContext ec)
{
- ec.CurrentBranching.CurrentUsageVector.Throw ();
+ ec.CurrentBranching.CurrentUsageVector.Goto ();
if (expr != null){
expr = expr.Resolve (ec);
ec.ig.Emit (OpCodes.Throw);
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Throw target = (Throw) t;
+
+ target.expr = expr.Clone (clonectx);
+ }
}
public class Break : Statement {
{
ec.ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, ec.LoopEnd);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ // nothing needed
+ }
}
public class Continue : Statement {
{
ec.ig.Emit (unwind_protect ? OpCodes.Leave : OpCodes.Br, ec.LoopBegin);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ // nothing needed.
+ }
}
public abstract class Variable
public abstract void EmitAddressOf (EmitContext ec);
}
+ public interface IKnownVariable {
+ Block Block { get; }
+ Location Location { get; }
+ }
+
//
// The information about a user-perceived local variable
//
- public class LocalInfo {
+ public class LocalInfo : IKnownVariable {
public Expression Type;
public Type VariableType;
ec.DefineLocalVariable (name, builder);
}
- public bool IsThisAssigned (EmitContext ec, Location loc)
+ public bool IsThisAssigned (EmitContext ec)
{
if (VariableInfo == null)
throw new Exception ();
if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (VariableInfo))
return true;
- return VariableInfo.TypeInfo.IsFullyInitialized (ec.CurrentBranching, VariableInfo, loc);
+ return VariableInfo.TypeInfo.IsFullyInitialized (ec.CurrentBranching, VariableInfo, ec.loc);
}
public bool IsAssigned (EmitContext ec)
public bool Resolve (EmitContext ec)
{
if (VariableType == null) {
- TypeExpr texpr = Type.ResolveAsTypeTerminal (ec, false);
+ TypeExpr texpr = Type.ResolveAsContextualType (ec, false);
if (texpr == null)
return false;
VariableType = texpr.Type;
}
+ if (TypeManager.IsGenericParameter (VariableType))
+ return true;
+
if (VariableType == TypeManager.void_type) {
Expression.Error_VoidInvalidInTheContext (Location);
return false;
}
public bool IsCaptured {
- get {
- return (flags & Flags.Captured) != 0;
- }
-
- set {
- flags |= Flags.Captured;
- }
+ get { return (flags & Flags.Captured) != 0; }
+ set { flags |= Flags.Captured; }
}
public bool IsConstant {
- get {
- return (flags & Flags.IsConstant) != 0;
- }
- set {
- flags |= Flags.IsConstant;
- }
+ get { return (flags & Flags.IsConstant) != 0; }
+ set { flags |= Flags.IsConstant; }
}
public bool AddressTaken {
- get {
- return (flags & Flags.AddressTaken) != 0;
- }
-
- set {
- flags |= Flags.AddressTaken;
- }
+ get { return (flags & Flags.AddressTaken) != 0; }
+ set { flags |= Flags.AddressTaken; }
}
public bool CompilerGenerated {
- get {
- return (flags & Flags.CompilerGenerated) != 0;
- }
-
- set {
- flags |= Flags.CompilerGenerated;
- }
+ get { return (flags & Flags.CompilerGenerated) != 0; }
+ set { flags |= Flags.CompilerGenerated; }
}
public override string ToString ()
}
public bool Used {
- get {
- return (flags & Flags.Used) != 0;
- }
- set {
- flags = value ? (flags | Flags.Used) : (unchecked (flags & ~Flags.Used));
- }
+ get { return (flags & Flags.Used) != 0; }
+ set { flags = value ? (flags | Flags.Used) : (unchecked (flags & ~Flags.Used)); }
}
public bool ReadOnly {
- get {
- return (flags & Flags.ReadOnly) != 0;
- }
+ get { return (flags & Flags.ReadOnly) != 0; }
}
public void SetReadOnlyContext (ReadOnlyContext context)
// allocated in a pinned slot with DeclareLocal.
//
public bool Pinned {
- get {
- return (flags & Flags.Pinned) != 0;
- }
- set {
- flags = value ? (flags | Flags.Pinned) : (flags & ~Flags.Pinned);
- }
+ get { return (flags & Flags.Pinned) != 0; }
+ set { flags = value ? (flags | Flags.Pinned) : (flags & ~Flags.Pinned); }
}
public bool IsThis {
- get {
- return (flags & Flags.IsThis) != 0;
- }
- set {
- flags = value ? (flags | Flags.IsThis) : (flags & ~Flags.IsThis);
- }
+ get { return (flags & Flags.IsThis) != 0; }
+ set { flags = value ? (flags | Flags.IsThis) : (flags & ~Flags.IsThis); }
+ }
+
+ Block IKnownVariable.Block {
+ get { return Block; }
+ }
+
+ Location IKnownVariable.Location {
+ get { return Location; }
}
protected class LocalVariable : Variable
ec.ig.Emit (OpCodes.Ldloca, builder);
}
}
+
+ public LocalInfo Clone (CloneContext clonectx)
+ {
+ //
+ // Variables in anonymous block are not resolved yet
+ //
+ if (VariableType == null)
+ return new LocalInfo (Type.Clone (clonectx), Name, clonectx.LookupBlock (Block), Location);
+
+ //
+ // Variables in method block are resolved
+ //
+ LocalInfo li = new LocalInfo (null, Name, clonectx.LookupBlock (Block), Location);
+ li.VariableType = VariableType;
+ return li;
+ }
}
-
+
/// <summary>
/// Block represents a C# block.
/// </summary>
public readonly Location StartLocation;
public Location EndLocation = Location.Null;
- public readonly ToplevelBlock Toplevel;
+ public ExplicitBlock Explicit;
+ public ToplevelBlock Toplevel;
[Flags]
- public enum Flags : ushort {
- Implicit = 1,
- Unchecked = 2,
- BlockUsed = 4,
- VariablesInitialized = 8,
- HasRet = 16,
- IsDestructor = 32,
- IsToplevel = 64,
- Unsafe = 128,
- HasVarargs = 256, // Used in ToplevelBlock
- IsIterator = 512
-
+ public enum Flags : byte {
+ Unchecked = 1,
+ BlockUsed = 2,
+ VariablesInitialized = 4,
+ HasRet = 8,
+ IsDestructor = 16,
+ Unsafe = 32,
+ HasVarargs = 64, // Used in ToplevelBlock
+ IsIterator = 128
}
protected Flags flags;
- public bool Implicit {
- get { return (flags & Flags.Implicit) != 0; }
- }
-
public bool Unchecked {
get { return (flags & Flags.Unchecked) != 0; }
set { flags |= Flags.Unchecked; }
protected static int id;
int this_id;
+
+ int assignable_slots;
+ protected ScopeInfo scope_info;
+ bool unreachable_shown;
+ bool unreachable;
public Block (Block parent)
: this (parent, (Flags) 0, Location.Null, Location.Null)
public Block (Block parent, Flags flags, Location start, Location end)
{
- if (parent != null)
+ if (parent != null) {
parent.AddChild (this);
+
+ // the appropriate constructors will fixup these fields
+ Toplevel = parent.Toplevel;
+ Explicit = parent.Explicit;
+ }
this.Parent = parent;
this.flags = flags;
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.known_variables == null)
- parent.known_variables = new Hashtable ();
- // share with parent
- known_variables = parent.known_variables;
- }
}
public Block CreateSwitchBlock (Location start)
{
- Block new_block = new Block (this, start, start);
+ // FIXME: should this be implicit?
+ Block new_block = new ExplicitBlock (this, start, start);
new_block.switch_block = this;
return new_block;
}
protected static void Error_158 (string name, Location loc)
{
Report.Error (158, loc, "The label `{0}' shadows another label " +
- "by the same name in a contained scope.", name);
+ "by the same name in a contained scope", name);
}
/// <summary>
Block cur = this;
while (cur != null) {
- if (cur.DoLookupLabel (name) != null) {
- Report.Error (140, target.loc,
- "The label `{0}' is a duplicate", name);
+ LabeledStatement s = cur.DoLookupLabel (name);
+ if (s != null) {
+ Report.SymbolRelatedToPreviousError (s.loc, s.Name);
+ Report.Error (140, target.loc, "The label `{0}' is a duplicate", name);
return false;
}
- if (!Implicit)
+ if (this == Explicit)
break;
cur = cur.Parent;
if (s == null)
continue;
+ Report.SymbolRelatedToPreviousError (s.loc, s.Name);
Error_158 (name, target.loc);
return false;
}
return null;
foreach (Block child in children) {
- if (!child.Implicit)
+ if (Explicit != child.Explicit)
continue;
s = child.LookupLabel (name);
return null;
}
- Hashtable known_variables;
-
- // <summary>
- // 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>
- void AddKnownVariable (string name, LocalInfo info)
- {
- if (known_variables == null)
- known_variables = new Hashtable ();
-
- known_variables [name] = info;
- }
-
- LocalInfo GetKnownVariableInfo (string name, bool recurse)
- {
- if (known_variables != null) {
- LocalInfo vi = (LocalInfo) known_variables [name];
- if (vi != null)
- return vi;
- }
-
- if (!recurse || (children == null))
- return null;
-
- foreach (Block block in children) {
- LocalInfo vi = block.GetKnownVariableInfo (name, true);
- if (vi != null)
- return vi;
- }
-
- return null;
- }
-
public bool CheckInvariantMeaningInBlock (string name, Expression e, Location loc)
{
Block b = this;
- LocalInfo kvi = b.GetKnownVariableInfo (name, true);
+ IKnownVariable kvi = b.Explicit.GetKnownVariable (name);
while (kvi == null) {
- while (b.Implicit)
- b = b.Parent;
- b = b.Parent;
+ b = b.Explicit.Parent;
if (b == null)
return true;
- kvi = b.GetKnownVariableInfo (name, false);
+ kvi = b.Explicit.GetKnownVariable (name);
}
if (kvi.Block == b)
return true;
// Is kvi.Block nested inside 'b'
- if (b.known_variables != kvi.Block.known_variables) {
+ if (b.Explicit != kvi.Block.Explicit) {
//
// If a variable by the same name it defined in a nested block of this
// block, we violate the invariant meaning in a block.
// an indirect check that depends on AddVariable doing its
// part in maintaining the invariant-meaning-in-block property.
//
- if (e is LocalVariableReference || (e is Constant && b.GetLocalInfo (name) != null))
+ if (e is VariableReference || (e is Constant && b.GetLocalInfo (name) != null))
return true;
//
return false;
}
- public bool CheckError136_InParents (string name, Location loc)
- {
- for (Block b = Parent; b != null; b = b.Parent) {
- if (!b.DoCheckError136 (name, "parent or current", loc))
- return false;
- }
-
- for (Block b = Toplevel.ContainerBlock; b != null; b = b.Toplevel.ContainerBlock) {
- if (!b.CheckError136_InParents (name, loc))
- return false;
- }
-
- return true;
- }
-
- public bool CheckError136_InChildren (string name, Location loc)
- {
- if (!DoCheckError136_InChildren (name, loc))
- return false;
-
- Block b = this;
- while (b.Implicit) {
- if (!b.Parent.DoCheckError136_InChildren (name, loc))
- return false;
- b = b.Parent;
- }
-
- return true;
- }
-
- protected bool DoCheckError136_InChildren (string name, Location loc)
- {
- if (!DoCheckError136 (name, "child", loc))
- return false;
-
- if (AnonymousChildren != null) {
- foreach (ToplevelBlock child in AnonymousChildren) {
- if (!child.DoCheckError136_InChildren (name, loc))
- return false;
- }
- }
-
- if (children != null) {
- foreach (Block child in children) {
- if (!child.DoCheckError136_InChildren (name, loc))
- return false;
- }
- }
-
- return true;
- }
-
- public bool CheckError136 (string name, string scope, bool check_parents,
- bool check_children, Location loc)
- {
- if (!DoCheckError136 (name, scope, loc))
- return false;
-
- if (check_parents) {
- if (!CheckError136_InParents (name, loc))
- return false;
- }
-
- if (check_children) {
- if (!CheckError136_InChildren (name, loc))
- return false;
- }
-
- for (Block c = Toplevel.ContainerBlock; c != null; c = c.Toplevel.ContainerBlock) {
- if (!c.DoCheckError136 (name, "parent or current", loc))
- return false;
- }
-
- return true;
- }
-
- protected bool DoCheckError136 (string name, string scope, Location loc)
- {
- LocalInfo vi = GetKnownVariableInfo (name, false);
- if (vi != null) {
- Report.SymbolRelatedToPreviousError (vi.Location, name);
- Error_AlreadyDeclared (loc, name, scope != null ? scope : "child");
- return false;
- }
-
- int idx;
- Parameter p = Toplevel.Parameters.GetParameterByName (name, out idx);
- if (p != null) {
- Report.SymbolRelatedToPreviousError (p.Location, name);
- Error_AlreadyDeclared (
- loc, name, scope != null ? scope : "method argument");
- return false;
- }
-
- return true;
- }
-
public LocalInfo AddVariable (Expression type, string name, Location l)
{
LocalInfo vi = GetLocalInfo (name);
if (vi != null) {
Report.SymbolRelatedToPreviousError (vi.Location, name);
- if (known_variables == vi.Block.known_variables)
- Report.Error (128, l,
- "A local variable named `{0}' is already defined in this scope", name);
+ if (Explicit == vi.Block.Explicit)
+ Error_AlreadyDeclared (l, name, null);
else
Error_AlreadyDeclared (l, name, "parent");
return null;
}
- if (!CheckError136 (name, null, true, true, l))
+ ToplevelParameterInfo pi = Toplevel.GetParameterInfo (name);
+ if (pi != null) {
+ Report.SymbolRelatedToPreviousError (pi.Location, name);
+ Error_AlreadyDeclared (loc, name,
+ pi.Block == Toplevel ? "method argument" : "parent or current");
+ return null;
+ }
+
+ if (Toplevel.GenericMethod != null) {
+ foreach (TypeParameter tp in Toplevel.GenericMethod.CurrentTypeParameters) {
+ if (tp.Name == name) {
+ Report.SymbolRelatedToPreviousError (tp);
+ Error_AlreadyDeclaredTypeParameter (loc, name);
+ return null;
+ }
+ }
+ }
+
+ IKnownVariable kvi = Explicit.GetKnownVariable (name);
+ if (kvi != null) {
+ Report.SymbolRelatedToPreviousError (kvi.Location, name);
+ Error_AlreadyDeclared (l, name, "child");
return null;
+ }
vi = new LocalInfo (type, name, this, l);
- Variables.Add (name, vi);
- AddKnownVariable (name, vi);
+ AddVariable (vi);
if ((flags & Flags.VariablesInitialized) != 0)
- throw new Exception ();
+ throw new InternalErrorException ("block has already been resolved");
return vi;
}
+
+ protected virtual void AddVariable (LocalInfo li)
+ {
+ Variables.Add (li.Name, li);
+ Explicit.AddKnownVariable (li.Name, li);
+ }
- void Error_AlreadyDeclared (Location loc, string var, string reason)
+ protected virtual void Error_AlreadyDeclared (Location loc, string var, string reason)
{
+ if (reason == null) {
+ Error_AlreadyDeclared (loc, var);
+ return;
+ }
+
Report.Error (136, loc, "A local variable named `{0}' cannot be declared " +
"in this scope because it would give a different meaning " +
"to `{0}', which is already used in a `{1}' scope " +
"to denote something else", var, reason);
}
+ protected virtual void Error_AlreadyDeclared (Location loc, string name)
+ {
+ Report.Error (128, loc,
+ "A local variable named `{0}' is already defined in this scope", name);
+ }
+
+ protected virtual void Error_AlreadyDeclaredTypeParameter (Location loc, string name)
+ {
+ GenericMethod.Error_ParameterNameCollision (loc, name, "local variable");
+ }
+
public bool AddConstant (Expression type, string name, Expression value, Location l)
{
if (AddVariable (type, name, l) == null)
flags |= Flags.IsDestructor;
}
- VariableMap param_map, local_map;
-
- public VariableMap ParameterMap {
+ public int AssignableSlots {
get {
if ((flags & Flags.VariablesInitialized) == 0)
throw new Exception ("Variables have not been initialized yet");
-
- return param_map;
+ return assignable_slots;
}
}
- public VariableMap LocalMap {
- get {
- if ((flags & Flags.VariablesInitialized) == 0)
- throw new Exception ("Variables have not been initialized yet");
-
- return local_map;
- }
+ public ScopeInfo ScopeInfo {
+ get { return scope_info; }
}
- public ScopeInfo ScopeInfo;
-
public ScopeInfo CreateScopeInfo ()
{
- if (ScopeInfo == null)
- ScopeInfo = ScopeInfo.CreateScope (this);
+ if (scope_info == null)
+ scope_info = ScopeInfo.CreateScope (this);
- return ScopeInfo;
+ return scope_info;
}
public ArrayList AnonymousChildren {
anonymous_children.Add (b);
}
- /// <summary>
- /// Emits the variable declarations and labels.
- /// </summary>
- /// <remarks>
- /// tc: is our typecontainer (to resolve type references)
- /// ig: is the code generator:
- /// </remarks>
- public void ResolveMeta (ToplevelBlock toplevel, EmitContext ec, Parameters ip)
+ void DoResolveConstants (EmitContext ec)
{
- Report.Debug (64, "BLOCK RESOLVE META", this, Parent, toplevel);
-
- // If some parent block was unsafe, we remain unsafe even if this block
- // isn't explicitly marked as such.
- using (ec.With (EmitContext.Flags.InUnsafe, ec.InUnsafe | Unsafe)) {
- //
- // Compute the VariableMap's.
- //
- // Unfortunately, we don't know the type when adding variables with
- // AddVariable(), so we need to compute this info here.
- //
-
- LocalInfo[] locals;
- if (variables != null) {
- foreach (LocalInfo li in variables.Values)
- li.Resolve (ec);
-
- locals = new LocalInfo [variables.Count];
- variables.Values.CopyTo (locals, 0);
- } else
- locals = new LocalInfo [0];
+ if (constants == null)
+ return;
- if (Parent != null)
- local_map = new VariableMap (Parent.LocalMap, locals);
- else
- local_map = new VariableMap (locals);
+ if (variables == null)
+ throw new InternalErrorException ("cannot happen");
- param_map = new VariableMap (ip);
- flags |= Flags.VariablesInitialized;
+ foreach (DictionaryEntry de in variables) {
+ string name = (string) de.Key;
+ LocalInfo vi = (LocalInfo) de.Value;
+ Type variable_type = vi.VariableType;
- //
- // Process this block variables
- //
- if (variables != null) {
- foreach (DictionaryEntry de in variables) {
- string name = (string) de.Key;
- LocalInfo vi = (LocalInfo) de.Value;
- Type variable_type = vi.VariableType;
+ if (variable_type == null)
+ continue;
- if (variable_type == null)
- continue;
+ Expression cv = (Expression) constants [name];
+ if (cv == null)
+ continue;
- if (variable_type.IsPointer) {
- //
- // Am not really convinced that this test is required (Microsoft does it)
- // but the fact is that you would not be able to use the pointer variable
- // *anyways*
- //
- if (!TypeManager.VerifyUnManaged (TypeManager.GetElementType (variable_type),
- vi.Location))
- continue;
- }
+ // Don't let 'const int Foo = Foo;' succeed.
+ // Removing the name from 'constants' ensures that we get a LocalVariableReference below,
+ // which in turn causes the 'must be constant' error to be triggered.
+ constants.Remove (name);
- if (constants == null)
- continue;
+ if (!Const.IsConstantTypeValid (variable_type)) {
+ Const.Error_InvalidConstantType (variable_type, loc);
+ continue;
+ }
- Expression cv = (Expression) constants [name];
- if (cv == null)
- continue;
+ ec.CurrentBlock = this;
+ Expression e;
+ using (ec.With (EmitContext.Flags.ConstantCheckState, (flags & Flags.Unchecked) == 0)) {
+ e = cv.Resolve (ec);
+ }
+ if (e == null)
+ continue;
- // Don't let 'const int Foo = Foo;' succeed.
- // Removing the name from 'constants' ensures that we get a LocalVariableReference below,
- // which in turn causes the 'must be constant' error to be triggered.
- constants.Remove (name);
+ Constant ce = e as Constant;
+ if (ce == null) {
+ Const.Error_ExpressionMustBeConstant (vi.Location, name);
+ continue;
+ }
- if (!Const.IsConstantTypeValid (variable_type)) {
- Const.Error_InvalidConstantType (variable_type, loc);
- continue;
- }
+ e = ce.ConvertImplicitly (variable_type);
+ if (e == null) {
+ if (!variable_type.IsValueType && variable_type != TypeManager.string_type && !ce.IsDefaultValue)
+ Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
+ else
+ ce.Error_ValueCannotBeConverted (null, vi.Location, variable_type, false);
+ continue;
+ }
- using (ec.With (EmitContext.Flags.ConstantCheckState, (flags & Flags.Unchecked) == 0)) {
- ec.CurrentBlock = this;
- Expression e = cv.Resolve (ec);
- if (e == null)
- continue;
+ constants.Add (name, e);
+ vi.IsConstant = true;
+ }
+ }
- Constant ce = e as Constant;
- if (ce == null) {
- Const.Error_ExpressionMustBeConstant (vi.Location, name);
- continue;
- }
+ protected void ResolveMeta (EmitContext ec, int offset)
+ {
+ Report.Debug (64, "BLOCK RESOLVE META", this, Parent);
- e = ce.ConvertImplicitly (variable_type);
- if (e == null) {
- if (!variable_type.IsValueType && variable_type != TypeManager.string_type && !ce.IsDefaultValue)
- Const.Error_ConstantCanBeInitializedWithNullOnly (vi.Location, vi.Name);
- else
- ce.Error_ValueCannotBeConverted (null, vi.Location, variable_type, false);
- continue;
- }
+ // If some parent block was unsafe, we remain unsafe even if this block
+ // isn't explicitly marked as such.
+ using (ec.With (EmitContext.Flags.InUnsafe, ec.InUnsafe | Unsafe)) {
+ flags |= Flags.VariablesInitialized;
- constants.Add (name, e);
- vi.IsConstant = true;
- }
+ if (variables != null) {
+ foreach (LocalInfo li in variables.Values) {
+ if (!li.Resolve (ec))
+ continue;
+ li.VariableInfo = new VariableInfo (li, offset);
+ offset += li.VariableInfo.Length;
}
}
+ assignable_slots = offset;
- //
- // Now, handle the children
- //
- if (children != null) {
- foreach (Block b in children)
- b.ResolveMeta (toplevel, ec, ip);
- }
+ DoResolveConstants (ec);
+
+ if (children == null)
+ return;
+ foreach (Block b in children)
+ b.ResolveMeta (ec, offset);
}
}
{
string name;
- if ((variables != null) && (RootContext.WarningLevel >= 3)) {
+ if ((variables != null) && (Report.WarningLevel >= 3)) {
foreach (DictionaryEntry de in variables){
LocalInfo vi = (LocalInfo) de.Value;
}
}
- bool unreachable_shown;
- bool unreachable;
-
private void CheckPossibleMistakenEmptyStatement (Statement s)
{
Statement body;
for (int ix = 0; ix < statement_count; ix++){
Statement s = (Statement) statements [ix];
// Check possible empty statement (CS0642)
- if (RootContext.WarningLevel >= 3 &&
+ if (Report.WarningLevel >= 3 &&
ix + 1 < statement_count &&
statements [ix + 1] is Block)
CheckPossibleMistakenEmptyStatement (s);
//
if (!s.Resolve (ec)) {
+ if (ec.IsInProbingMode)
+ return false;
+
ok = false;
statements [ix] = EmptyStatement.Value;
continue;
num_statements = ix + 1;
- unreachable = ec.CurrentBranching.CurrentUsageVector.Reachability.IsUnreachable;
+ unreachable = ec.CurrentBranching.CurrentUsageVector.IsUnreachable;
if (unreachable && s is LabeledStatement)
throw new InternalErrorException ("should not happen");
}
Report.Debug (4, "RESOLVE BLOCK DONE", StartLocation,
ec.CurrentBranching, statement_count, num_statements);
+ if (!ok)
+ return false;
+
while (ec.CurrentBranching is FlowBranchingLabeled)
ec.EndFlowBranching ();
// If we're a non-static `struct' constructor which doesn't have an
// initializer, then we must initialize all of the struct's fields.
- if ((flags & Flags.IsToplevel) != 0 &&
- !Toplevel.IsThisAssigned (ec) &&
- !vector.Reachability.AlwaysThrows)
+ if (this == Toplevel && !Toplevel.IsThisAssigned (ec) && !vector.IsUnreachable)
ok = false;
- if ((labels != null) && (RootContext.WarningLevel >= 2)) {
+ if ((labels != null) && (Report.WarningLevel >= 2)) {
foreach (LabeledStatement label in labels.Values)
if (!label.HasBeenReferenced)
Report.Warning (164, 2, label.loc,
Report.Debug (4, "RESOLVE BLOCK DONE #2", StartLocation, vector);
- if (vector.Reachability.IsUnreachable)
+ if (vector.IsUnreachable)
flags |= Flags.HasRet;
if (ok && (errors == Report.Errors)) {
- if (RootContext.WarningLevel >= 3)
- UsageWarning (vector);
+ UsageWarning (vector);
}
return ok;
{
for (int ix = 0; ix < num_statements; ix++){
Statement s = (Statement) statements [ix];
-
- // Check whether we are the last statement in a
- // top-level block.
-
- if (((Parent == null) || Implicit) && (ix+1 == num_statements) && !(s is Block))
- ec.IsLastStatement = true;
- else
- ec.IsLastStatement = false;
-
s.Emit (ec);
}
}
ec.CurrentBlock = this;
bool emit_debug_info = (CodeGen.SymbolWriter != null);
- bool is_lexical_block = !Implicit && (Parent != null);
+ bool is_lexical_block = this == Explicit && Parent != null;
if (emit_debug_info) {
if (is_lexical_block)
ec.CurrentBlock = prev_block;
}
- //
- // Returns true if we ar ea child of `b'.
- //
- public bool IsChildOf (Block b)
+ public override string ToString ()
{
- Block current = this;
-
- do {
- if (current.Parent == b)
- return true;
- current = current.Parent;
- } while (current != null);
- return false;
+ return String.Format ("{0} ({1}:{2})", GetType (),ID, StartLocation);
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Block target = (Block) t;
+
+ clonectx.AddBlockMap (this, target);
+
+ //target.Toplevel = (ToplevelBlock) clonectx.LookupBlock (Toplevel);
+ target.Explicit = (ExplicitBlock) clonectx.LookupBlock (Explicit);
+ if (Parent != null)
+ target.Parent = clonectx.RemapBlockCopy (Parent);
+
+ if (variables != null){
+ target.variables = new Hashtable ();
+
+ foreach (DictionaryEntry de in variables){
+ LocalInfo newlocal = ((LocalInfo) de.Value).Clone (clonectx);
+ target.variables [de.Key] = newlocal;
+ clonectx.AddVariableMap ((LocalInfo) de.Value, newlocal);
+ }
+ }
+
+ target.statements = new ArrayList (statements.Count);
+ foreach (Statement s in statements)
+ target.statements.Add (s.Clone (clonectx));
+
+ if (target.children != null){
+ target.children = new ArrayList (children.Count);
+ foreach (Block b in children){
+ target.children.Add (clonectx.LookupBlock (b));
+ }
+ }
+
+ //
+ // TODO: labels, switch_block, constants (?), anonymous_children
+ //
+ }
+ }
+
+ public class ExplicitBlock : Block {
+ public ExplicitBlock (Block parent, Location start, Location end)
+ : this (parent, (Flags) 0, start, end)
+ {
+ }
+
+ public ExplicitBlock (Block parent, Flags flags, Location start, Location end)
+ : base (parent, flags, start, end)
+ {
+ this.Explicit = this;
+ }
+
+ Hashtable known_variables;
+
+ // <summary>
+ // 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>
+ internal void AddKnownVariable (string name, IKnownVariable info)
+ {
+ if (known_variables == null)
+ known_variables = new Hashtable ();
+
+ known_variables [name] = info;
+
+ if (Parent != null)
+ Parent.Explicit.AddKnownVariable (name, info);
+ }
+
+ internal IKnownVariable GetKnownVariable (string name)
+ {
+ return known_variables == null ? null : (IKnownVariable) known_variables [name];
+ }
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ ExplicitBlock target = (ExplicitBlock) t;
+ target.known_variables = null;
+ base.CloneTo (clonectx, t);
+ }
+ }
+
+ public class ToplevelParameterInfo : IKnownVariable {
+ public readonly ToplevelBlock Block;
+ public readonly int Index;
+ public VariableInfo VariableInfo;
+
+ Block IKnownVariable.Block {
+ get { return Block; }
+ }
+ public Parameter Parameter {
+ get { return Block.Parameters [Index]; }
+ }
+ public Location Location {
+ get { return Parameter.Location; }
}
- public override string ToString ()
+ public ToplevelParameterInfo (ToplevelBlock block, int idx)
{
- return String.Format ("{0} ({1}:{2})", GetType (),ID, StartLocation);
+ this.Block = block;
+ this.Index = idx;
}
}
// In particular, this was introduced when the support for Anonymous
// Methods was implemented.
//
- public class ToplevelBlock : Block {
- //
- // Pointer to the host of this anonymous method, or null
- // if we are the topmost block
- //
- Block container;
- ToplevelBlock child;
+ public class ToplevelBlock : ExplicitBlock {
GenericMethod generic;
FlowBranchingToplevel top_level_branching;
AnonymousContainer anonymous_container;
RootScopeInfo root_scope;
+ Parameters parameters;
+ ToplevelParameterInfo[] parameter_info;
public bool HasVarargs {
get { return (flags & Flags.HasVarargs) != 0; }
//
// The parameters for the block.
//
- Parameters parameters;
public Parameters Parameters {
get { return parameters; }
}
public bool CompleteContexts (EmitContext ec)
{
- Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS", this,
- container, root_scope);
+ Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS", this, Parent, root_scope);
if (root_scope != null)
root_scope.LinkScopes ();
- if ((container == null) && (root_scope != null)) {
- Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS #1", this,
- root_scope);
+ if (Parent == null && root_scope != null) {
+ Report.Debug (64, "TOPLEVEL COMPLETE CONTEXTS #1", this, root_scope);
if (root_scope.DefineType () == null)
return false;
}
public ToplevelBlock Container {
- get { return container != null ? container.Toplevel : null; }
- }
-
- public Block ContainerBlock {
- get { return container; }
+ get { return Parent == null ? null : Parent.Toplevel; }
}
public AnonymousContainer AnonymousContainer {
set { anonymous_container = value; }
}
- //
- // Parent is only used by anonymous blocks to link back to their
- // parents
- //
- public ToplevelBlock (Block container, Parameters parameters, Location start) :
- this (container, (Flags) 0, parameters, start)
+ public ToplevelBlock (Block parent, Parameters parameters, Location start) :
+ this (parent, (Flags) 0, parameters, start)
{
}
- public ToplevelBlock (Block container, Parameters parameters, GenericMethod generic,
- Location start) :
- this (container, parameters, start)
+ public ToplevelBlock (Block parent, Parameters parameters, GenericMethod generic, Location start) :
+ this (parent, parameters, start)
{
this.generic = generic;
}
{
}
- public ToplevelBlock (Block container, Flags flags, Parameters parameters, Location start) :
- base (null, flags | Flags.IsToplevel, start, Location.Null)
+ // We use 'Parent' to hook up to the containing block, but don't want to register the current block as a child.
+ // So, we use a two-stage setup -- first pass a null parent to the base constructor, and then override 'Parent'.
+ public ToplevelBlock (Block parent, Flags flags, Parameters parameters, Location start) :
+ base (null, flags, start, Location.Null)
{
+ this.Toplevel = this;
+
this.parameters = parameters == null ? Parameters.EmptyReadOnlyParameters : parameters;
- this.container = container;
+ this.Parent = parent;
+ if (parent != null)
+ parent.AddAnonymousChild (this);
+
+ if (this.parameters.Count != 0)
+ ProcessParameters ();
}
public ToplevelBlock (Location loc) : this (null, (Flags) 0, null, loc)
{
}
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ ToplevelBlock target = (ToplevelBlock) t;
+ base.CloneTo (clonectx, t);
+
+ if (parameters.Count != 0)
+ target.parameter_info = new ToplevelParameterInfo [parameters.Count];
+ for (int i = 0; i < parameters.Count; ++i)
+ target.parameter_info [i] = new ToplevelParameterInfo (target, i);
+ }
+
public bool CheckError158 (string name, Location loc)
{
if (AnonymousChildren != null) {
return true;
}
+ public virtual Expression GetTransparentIdentifier (string name)
+ {
+ return null;
+ }
+
+ void ProcessParameters ()
+ {
+ int n = parameters.Count;
+ parameter_info = new ToplevelParameterInfo [n];
+ for (int i = 0; i < n; ++i) {
+ parameter_info [i] = new ToplevelParameterInfo (this, i);
+
+ Parameter p = parameters [i];
+ if (p == null)
+ continue;
+
+ string name = p.Name;
+ LocalInfo vi = GetLocalInfo (name);
+ if (vi != null) {
+ Report.SymbolRelatedToPreviousError (vi.Location, name);
+ Error_AlreadyDeclared (loc, name, "parent or current");
+ continue;
+ }
+
+ ToplevelParameterInfo pi = Parent == null ? null : Parent.Toplevel.GetParameterInfo (name);
+ if (pi != null) {
+ Report.SymbolRelatedToPreviousError (pi.Location, name);
+ Error_AlreadyDeclared (loc, name, "parent or current");
+ continue;
+ }
+
+ AddKnownVariable (name, parameter_info [i]);
+ }
+
+ // mark this block as "used" so that we create local declarations in a sub-block
+ // FIXME: This appears to uncover a lot of bugs
+ //this.Use ();
+ }
+
bool DoCheckError158 (string name, Location loc)
{
LabeledStatement s = LookupLabel (name);
if (s != null) {
+ Report.SymbolRelatedToPreviousError (s.loc, s.Name);
Error_158 (name, loc);
return false;
}
root_scope = new RootScopeInfo (
this, host, generic, StartLocation);
- ScopeInfo = root_scope;
+ if (scope_info != null)
+ throw new InternalErrorException ();
+
+ scope_info = root_scope;
return root_scope;
}
public void CreateIteratorHost (RootScopeInfo root)
{
- Report.Debug (64, "CREATE ITERATOR HOST", this, root,
- container, root_scope);
+ Report.Debug (64, "CREATE ITERATOR HOST", this, root, Parent, root_scope);
- if ((container != null) || (root_scope != null))
+ if (Parent != null || root_scope != null)
throw new InternalErrorException ();
- ScopeInfo = root_scope = root;
+ scope_info = root_scope = root;
}
public RootScopeInfo RootScope {
//
public void ReParent (ToplevelBlock new_parent)
{
- container = new_parent;
+ if ((flags & Flags.VariablesInitialized) != 0)
+ throw new InternalErrorException ("block has already been resolved");
+
Parent = new_parent;
- new_parent.child = this;
}
//
//
public ParameterReference GetParameterReference (string name, Location loc)
{
- Parameter par;
- int idx;
-
- for (ToplevelBlock t = this; t != null; t = t.Container) {
- Parameters pars = t.Parameters;
- par = pars.GetParameterByName (name, out idx);
- if (par != null)
- return new ParameterReference (par, this, idx, loc);
- }
- return null;
+ ToplevelParameterInfo p = GetParameterInfo (name);
+ return p == null ? null : new ParameterReference (this, p, loc);
}
- //
- // Whether the parameter named `name' is local to this block,
- // or false, if the parameter belongs to an encompassing block.
- //
- public bool IsLocalParameter (string name)
- {
- return Parameters.GetParameterByName (name) != null;
- }
-
- //
- // Whether the `name' is a parameter reference
- //
- public bool IsParameterReference (string name)
+ public ToplevelParameterInfo GetParameterInfo (string name)
{
+ int idx;
for (ToplevelBlock t = this; t != null; t = t.Container) {
- if (t.IsLocalParameter (name))
- return true;
+ Parameter par = t.Parameters.GetParameterByName (name, out idx);
+ if (par != null)
+ return t.parameter_info [idx];
}
- return false;
+ return null;
}
LocalInfo this_variable = null;
public bool IsThisAssigned (EmitContext ec)
{
- return this_variable == null || this_variable.IsThisAssigned (ec, loc);
+ return this_variable == null || this_variable.IsThisAssigned (ec);
}
public bool ResolveMeta (EmitContext ec, Parameters ip)
{
int errors = Report.Errors;
+ int orig_count = parameters.Count;
if (top_level_branching != null)
return true;
if (ip != null)
parameters = ip;
- if (!IsIterator && (container != null) && (parameters != null)) {
- foreach (Parameter p in parameters.FixedParameters) {
- if (!CheckError136_InParents (p.Name, loc))
- return false;
- }
- }
+ // Assert: orig_count != parameter.Count => orig_count == 0
+ if (orig_count != 0 && orig_count != parameters.Count)
+ throw new InternalErrorException ("parameter information mismatch");
+
+ int offset = Parent == null ? 0 : Parent.AssignableSlots;
+
+ for (int i = 0; i < orig_count; ++i) {
+ Parameter.Modifier mod = parameters.ParameterModifier (i);
+
+ if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
+ continue;
- ResolveMeta (this, ec, ip);
+ VariableInfo vi = new VariableInfo (ip, i, offset);
+ parameter_info [i].VariableInfo = vi;
+ offset += vi.Length;
+ }
- if (child != null)
- child.ResolveMeta (this, ec, ip);
+ ResolveMeta (ec, offset);
top_level_branching = ec.StartFlowBranching (this);
return Report.Errors == errors;
}
+ // <summary>
+ // Check whether all `out' parameters have been assigned.
+ // </summary>
+ public void CheckOutParameters (FlowBranching.UsageVector vector, Location loc)
+ {
+ if (vector.IsUnreachable)
+ return;
+
+ int n = parameter_info == null ? 0 : parameter_info.Length;
+
+ for (int i = 0; i < n; i++) {
+ VariableInfo var = parameter_info [i].VariableInfo;
+
+ if (var == null)
+ continue;
+
+ if (vector.IsAssigned (var, false))
+ continue;
+
+ Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
+ var.Name);
+ }
+ }
+
public override void EmitMeta (EmitContext ec)
{
base.EmitMeta (ec);
{
flags |= Flags.IsIterator;
- Block block = new Block (this);
+ Block block = new ExplicitBlock (this, StartLocation, EndLocation);
foreach (Statement stmt in statements)
block.AddStatement (stmt);
- statements = new ArrayList ();
+ statements.Clear ();
statements.Add (new MoveNextStatement (iterator, block));
}
return true;
}
- public void Erorr_AlreadyOccurs (Type switchType, SwitchLabel collisionWith)
+ public void Erorr_AlreadyOccurs (Type switch_type, SwitchLabel collision_with)
{
string label;
if (converted == null)
label = "default";
else if (converted == NullStringCase)
label = "null";
- else if (TypeManager.IsEnumType (switchType))
- label = TypeManager.CSharpEnumValue (switchType, converted);
+ else if (TypeManager.IsEnumType (switch_type))
+ label = TypeManager.CSharpEnumValue (switch_type, converted);
else
label = converted.ToString ();
- Report.SymbolRelatedToPreviousError (collisionWith.loc, null);
+ Report.SymbolRelatedToPreviousError (collision_with.loc, null);
Report.Error (152, loc, "The label `case {0}:' already occurs in this switch statement", label);
}
+
+ public SwitchLabel Clone (CloneContext clonectx)
+ {
+ return new SwitchLabel (label.Clone (clonectx), loc);
+ }
}
public class SwitchSection {
Labels = labels;
Block = block;
}
+
+ public SwitchSection Clone (CloneContext clonectx)
+ {
+ ArrayList cloned_labels = new ArrayList ();
+
+ foreach (SwitchLabel sl in cloned_labels)
+ cloned_labels.Add (sl.Clone (clonectx));
+
+ return new SwitchSection (cloned_labels, clonectx.LookupBlock (Block));
+ }
}
public class Switch : Statement {
- public readonly ArrayList Sections;
+ public ArrayList Sections;
public Expression Expr;
/// <summary>
//
Expression SwitchGoverningType (EmitContext ec, Expression expr)
{
- Type t = expr.Type;
+ Type t = TypeManager.DropGenericTypeArguments (expr.Type);
if (t == TypeManager.byte_type ||
t == TypeManager.sbyte_type ||
// structure used to hold blocks of keys while calculating table switch
class KeyBlock : IComparable
{
- public KeyBlock (long _nFirst)
+ public KeyBlock (long _first)
{
- nFirst = nLast = _nFirst;
+ first = last = _first;
}
- public long nFirst;
- public long nLast;
- public ArrayList rgKeys = null;
+ public long first;
+ public long last;
+ public ArrayList element_keys = null;
// how many items are in the bucket
public int Size = 1;
public int Length
{
- get { return (int) (nLast - nFirst + 1); }
+ get { return (int) (last - first + 1); }
}
- public static long TotalLength (KeyBlock kbFirst, KeyBlock kbLast)
+ public static long TotalLength (KeyBlock kb_first, KeyBlock kb_last)
{
- return kbLast.nLast - kbFirst.nFirst + 1;
+ return kb_last.last - kb_first.first + 1;
}
public int CompareTo (object obj)
{
int nLength = Length;
int nLengthOther = kb.Length;
if (nLengthOther == nLength)
- return (int) (kb.nFirst - nFirst);
+ return (int) (kb.first - first);
return nLength - nLengthOther;
}
}
/// <returns></returns>
void TableSwitchEmit (EmitContext ec, LocalBuilder val)
{
- int cElements = Elements.Count;
- object [] rgKeys = new object [cElements];
- Elements.Keys.CopyTo (rgKeys, 0);
- Array.Sort (rgKeys);
+ int element_count = Elements.Count;
+ object [] element_keys = new object [element_count];
+ Elements.Keys.CopyTo (element_keys, 0);
+ Array.Sort (element_keys);
// initialize the block list with one element per key
- ArrayList rgKeyBlocks = new ArrayList ();
- foreach (object key in rgKeys)
- rgKeyBlocks.Add (new KeyBlock (System.Convert.ToInt64 (key)));
+ ArrayList key_blocks = new ArrayList ();
+ foreach (object key in element_keys)
+ key_blocks.Add (new KeyBlock (System.Convert.ToInt64 (key)));
- KeyBlock kbCurr;
+ KeyBlock current_kb;
// iteratively merge the blocks while they are at least half full
// there's probably a really cool way to do this with a tree...
- while (rgKeyBlocks.Count > 1)
+ while (key_blocks.Count > 1)
{
- ArrayList rgKeyBlocksNew = new ArrayList ();
- kbCurr = (KeyBlock) rgKeyBlocks [0];
- for (int ikb = 1; ikb < rgKeyBlocks.Count; ikb++)
+ ArrayList key_blocks_new = new ArrayList ();
+ current_kb = (KeyBlock) key_blocks [0];
+ for (int ikb = 1; ikb < key_blocks.Count; ikb++)
{
- KeyBlock kb = (KeyBlock) rgKeyBlocks [ikb];
- if ((kbCurr.Size + kb.Size) * 2 >= KeyBlock.TotalLength (kbCurr, kb))
+ KeyBlock kb = (KeyBlock) key_blocks [ikb];
+ if ((current_kb.Size + kb.Size) * 2 >= KeyBlock.TotalLength (current_kb, kb))
{
// merge blocks
- kbCurr.nLast = kb.nLast;
- kbCurr.Size += kb.Size;
+ current_kb.last = kb.last;
+ current_kb.Size += kb.Size;
}
else
{
// start a new block
- rgKeyBlocksNew.Add (kbCurr);
- kbCurr = kb;
+ key_blocks_new.Add (current_kb);
+ current_kb = kb;
}
}
- rgKeyBlocksNew.Add (kbCurr);
- if (rgKeyBlocks.Count == rgKeyBlocksNew.Count)
+ key_blocks_new.Add (current_kb);
+ if (key_blocks.Count == key_blocks_new.Count)
break;
- rgKeyBlocks = rgKeyBlocksNew;
+ key_blocks = key_blocks_new;
}
// initialize the key lists
- foreach (KeyBlock kb in rgKeyBlocks)
- kb.rgKeys = new ArrayList ();
+ foreach (KeyBlock kb in key_blocks)
+ kb.element_keys = new ArrayList ();
// fill the key lists
int iBlockCurr = 0;
- if (rgKeyBlocks.Count > 0) {
- kbCurr = (KeyBlock) rgKeyBlocks [0];
- foreach (object key in rgKeys)
+ if (key_blocks.Count > 0) {
+ current_kb = (KeyBlock) key_blocks [0];
+ foreach (object key in element_keys)
{
- bool fNextBlock = (key is UInt64) ? (ulong) key > (ulong) kbCurr.nLast :
- System.Convert.ToInt64 (key) > kbCurr.nLast;
- if (fNextBlock)
- kbCurr = (KeyBlock) rgKeyBlocks [++iBlockCurr];
- kbCurr.rgKeys.Add (key);
+ bool next_block = (key is UInt64) ? (ulong) key > (ulong) current_kb.last :
+ System.Convert.ToInt64 (key) > current_kb.last;
+ if (next_block)
+ current_kb = (KeyBlock) key_blocks [++iBlockCurr];
+ current_kb.element_keys.Add (key);
}
}
// sort the blocks so we can tackle the largest ones first
- rgKeyBlocks.Sort ();
+ key_blocks.Sort ();
// okay now we can start...
ILGenerator ig = ec.ig;
- Label lblEnd = ig.DefineLabel (); // at the end ;-)
- Label lblDefault = ig.DefineLabel ();
+ Label lbl_end = ig.DefineLabel (); // at the end ;-)
+ Label lbl_default = ig.DefineLabel ();
- Type typeKeys = null;
- if (rgKeys.Length > 0)
- typeKeys = rgKeys [0].GetType (); // used for conversions
+ Type type_keys = null;
+ if (element_keys.Length > 0)
+ type_keys = element_keys [0].GetType (); // used for conversions
Type compare_type;
else
compare_type = SwitchType;
- for (int iBlock = rgKeyBlocks.Count - 1; iBlock >= 0; --iBlock)
+ for (int iBlock = key_blocks.Count - 1; iBlock >= 0; --iBlock)
{
- KeyBlock kb = ((KeyBlock) rgKeyBlocks [iBlock]);
- lblDefault = (iBlock == 0) ? DefaultTarget : ig.DefineLabel ();
+ KeyBlock kb = ((KeyBlock) key_blocks [iBlock]);
+ lbl_default = (iBlock == 0) ? DefaultTarget : ig.DefineLabel ();
if (kb.Length <= 2)
{
- foreach (object key in kb.rgKeys)
+ foreach (object key in kb.element_keys)
{
ig.Emit (OpCodes.Ldloc, val);
EmitObjectInteger (ig, key);
// check block range (could be > 2^31)
ig.Emit (OpCodes.Ldloc, val);
- EmitObjectInteger (ig, System.Convert.ChangeType (kb.nFirst, typeKeys));
- ig.Emit (OpCodes.Blt, lblDefault);
+ EmitObjectInteger (ig, System.Convert.ChangeType (kb.first, type_keys));
+ ig.Emit (OpCodes.Blt, lbl_default);
ig.Emit (OpCodes.Ldloc, val);
- EmitObjectInteger (ig, System.Convert.ChangeType (kb.nLast, typeKeys));
- ig.Emit (OpCodes.Bgt, lblDefault);
+ EmitObjectInteger (ig, System.Convert.ChangeType (kb.last, type_keys));
+ ig.Emit (OpCodes.Bgt, lbl_default);
// normalize range
ig.Emit (OpCodes.Ldloc, val);
- if (kb.nFirst != 0)
+ if (kb.first != 0)
{
- EmitObjectInteger (ig, System.Convert.ChangeType (kb.nFirst, typeKeys));
+ EmitObjectInteger (ig, System.Convert.ChangeType (kb.first, type_keys));
ig.Emit (OpCodes.Sub);
}
ig.Emit (OpCodes.Conv_I4); // assumes < 2^31 labels!
{
// normalize range
ig.Emit (OpCodes.Ldloc, val);
- int nFirst = (int) kb.nFirst;
- if (nFirst > 0)
+ int first = (int) kb.first;
+ if (first > 0)
{
- IntConstant.EmitInt (ig, nFirst);
+ IntConstant.EmitInt (ig, first);
ig.Emit (OpCodes.Sub);
}
- else if (nFirst < 0)
+ else if (first < 0)
{
- IntConstant.EmitInt (ig, -nFirst);
+ IntConstant.EmitInt (ig, -first);
ig.Emit (OpCodes.Add);
}
}
// first, build the list of labels for the switch
int iKey = 0;
int cJumps = kb.Length;
- Label [] rgLabels = new Label [cJumps];
+ Label [] switch_labels = new Label [cJumps];
for (int iJump = 0; iJump < cJumps; iJump++)
{
- object key = kb.rgKeys [iKey];
- if (System.Convert.ToInt64 (key) == kb.nFirst + iJump)
+ object key = kb.element_keys [iKey];
+ if (System.Convert.ToInt64 (key) == kb.first + iJump)
{
SwitchLabel sl = (SwitchLabel) Elements [key];
- rgLabels [iJump] = sl.GetILLabel (ec);
+ switch_labels [iJump] = sl.GetILLabel (ec);
iKey++;
}
else
- rgLabels [iJump] = lblDefault;
+ switch_labels [iJump] = lbl_default;
}
// emit the switch opcode
- ig.Emit (OpCodes.Switch, rgLabels);
+ ig.Emit (OpCodes.Switch, switch_labels);
}
// mark the default for this block
if (iBlock != 0)
- ig.MarkLabel (lblDefault);
+ ig.MarkLabel (lbl_default);
}
// TODO: find the default case and emit it here,
// make sure to mark other labels in the default section
// the last default just goes to the end
- ig.Emit (OpCodes.Br, lblDefault);
+ ig.Emit (OpCodes.Br, lbl_default);
// now emit the code for the sections
- bool fFoundDefault = false;
- bool fFoundNull = false;
+ bool found_default = false;
+ bool found_null = false;
foreach (SwitchSection ss in Sections)
{
foreach (SwitchLabel sl in ss.Labels)
if (sl.Converted == SwitchLabel.NullStringCase)
- fFoundNull = true;
+ found_null = true;
}
foreach (SwitchSection ss in Sections)
if (sl.Converted == SwitchLabel.NullStringCase)
ig.MarkLabel (null_target);
else if (sl.Label == null) {
- ig.MarkLabel (lblDefault);
- fFoundDefault = true;
- if (!fFoundNull)
+ ig.MarkLabel (lbl_default);
+ found_default = true;
+ if (!found_null)
ig.MarkLabel (null_target);
}
}
ss.Block.Emit (ec);
}
- if (!fFoundDefault) {
- ig.MarkLabel (lblDefault);
+ if (!found_default) {
+ ig.MarkLabel (lbl_default);
+ if (HaveUnwrap && !found_null) {
+ ig.MarkLabel (null_target);
+ }
}
- ig.MarkLabel (lblEnd);
+
+ ig.MarkLabel (lbl_end);
}
//
// This simple emit switch works, but does not take advantage of the
SwitchType = new_expr.Type;
if (RootContext.Version == LanguageVersion.ISO_1 && SwitchType == TypeManager.bool_type) {
- Report.FeatureIsNotISO1 (loc, "switch expression of boolean type");
+ Report.FeatureIsNotAvailable (loc, "switch expression of boolean type");
return false;
}
ec.CurrentBranching.CreateSibling (
null, FlowBranching.SiblingType.SwitchSection);
- FlowBranching.Reachability reachability = ec.EndFlowBranching ();
+ ec.EndFlowBranching ();
ec.Switch = old_switch;
- Report.Debug (1, "END OF SWITCH BLOCK", loc, ec.CurrentBranching,
- reachability);
+ Report.Debug (1, "END OF SWITCH BLOCK", loc, ec.CurrentBranching);
return true;
}
ec.LoopEnd = old_end;
ec.Switch = old_switch;
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Switch target = (Switch) t;
+
+ target.Expr = Expr.Clone (clonectx);
+ target.Sections = new ArrayList ();
+ foreach (SwitchSection ss in Sections){
+ target.Sections.Add (ss.Clone (clonectx));
+ }
+ }
}
public abstract class ExceptionStatement : Statement
FlowBranchingException branching = ec.StartFlowBranching (this);
bool ok = Statement.Resolve (ec);
- if (!ok) {
- ec.KillFlowBranching ();
- return false;
- }
ResolveFinally (branching);
- FlowBranching.Reachability reachability = ec.EndFlowBranching ();
- if (!reachability.AlwaysReturns) {
- // Unfortunately, System.Reflection.Emit automatically emits
- // a leave to the end of the finally block.
- // This is a problem if `returns' is true since we may jump
- // to a point after the end of the method.
- // As a workaround, emit an explicit ret here.
- ec.NeedReturnLabel ();
- }
+ ec.EndFlowBranching ();
+
+ // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
+ // So, ensure there's some IL code after the finally block.
+ ec.NeedReturnLabel ();
// Avoid creating libraries that reference the internal
// mcs NullType:
temp = new TemporaryVariable (t, loc);
temp.Resolve (ec);
- return true;
+ return ok;
}
protected override void DoEmit (EmitContext ec)
temp.Emit (ec);
ec.ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Lock target = (Lock) t;
+
+ target.expr = expr.Clone (clonectx);
+ target.Statement = Statement.Clone (clonectx);
+ }
}
public class Unchecked : Statement {
- public readonly Block Block;
+ public Block Block;
public Unchecked (Block b)
{
using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
Block.Emit (ec);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Unchecked target = (Unchecked) t;
+
+ target.Block = clonectx.LookupBlock (Block);
+ }
}
public class Checked : Statement {
- public readonly Block Block;
+ public Block Block;
public Checked (Block b)
{
using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
Block.Emit (ec);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Checked target = (Checked) t;
+
+ target.Block = clonectx.LookupBlock (Block);
+ }
}
public class Unsafe : Statement {
- public readonly Block Block;
+ public Block Block;
public Unsafe (Block b)
{
using (ec.With (EmitContext.Flags.InUnsafe, true))
Block.Emit (ec);
}
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Unsafe target = (Unsafe) t;
+
+ target.Block = clonectx.LookupBlock (Block);
+ }
}
//
foreach (Pair p in declarators){
LocalInfo vi = (LocalInfo) p.First;
Expression e = (Expression) p.Second;
-
+
vi.VariableInfo.SetAssigned (ec);
vi.SetReadOnlyContext (LocalInfo.ReadOnlyContext.Fixed);
ec, array_ptr, vi.VariableType, loc);
if (converted == null)
return false;
+
+ //
+ // fixed (T* e_ptr = (e == null || e.Length == 0) ? null : converted [0])
+ //
+ converted = new Conditional (new Binary (Binary.Operator.LogicalOr,
+ new Binary (Binary.Operator.Equality, e, new NullConstant (loc)),
+ new Binary (Binary.Operator.Equality, new MemberAccess (e, "Length"), new IntConstant (0, loc))),
+ NullPointer.Null,
+ converted);
+
+ converted = converted.Resolve (ec);
data [i] = new ExpressionEmitter (converted, vi);
i++;
}
// Case 4: fixed buffer
- FieldExpr fe = e as FieldExpr;
- if (fe != null) {
- IFixedBuffer ff = AttributeTester.GetFixedBuffer (fe.FieldInfo);
- if (ff != null) {
- Expression fixed_buffer_ptr = new FixedBufferPtr (fe, ff.ElementType, loc);
-
- Expression converted = Convert.ImplicitConversionRequired (
- ec, fixed_buffer_ptr, vi.VariableType, loc);
- if (converted == null)
- return false;
-
- data [i] = new ExpressionEmitter (converted, vi);
- i++;
-
- continue;
- }
+ FixedBufferPtr fixed_buffer_ptr = e as FixedBufferPtr;
+ if (fixed_buffer_ptr != null) {
+ data [i++] = new ExpressionEmitter (fixed_buffer_ptr, vi);
+ continue;
}
//
}
ec.StartFlowBranching (FlowBranching.BranchingType.Conditional, loc);
+ bool ok = statement.Resolve (ec);
+ bool flow_unreachable = ec.EndFlowBranching ();
+ has_ret = flow_unreachable;
- if (!statement.Resolve (ec)) {
- ec.KillFlowBranching ();
- return false;
- }
-
- FlowBranching.Reachability reachability = ec.EndFlowBranching ();
- has_ret = reachability.IsUnreachable;
-
- return true;
+ return ok;
}
protected override void DoEmit (EmitContext ec)
data [i].EmitExit (ec);
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Fixed target = (Fixed) t;
+
+ target.type = type.Clone (clonectx);
+ target.declarators = new ArrayList (declarators.Count);
+ foreach (Pair p in declarators) {
+ LocalInfo vi = (LocalInfo) p.First;
+ Expression e = (Expression) p.Second;
+
+ target.declarators.Add (
+ new Pair (clonectx.LookupVariable (vi), e.Clone (clonectx)));
+ }
+
+ target.statement = statement.Clone (clonectx);
+ }
}
public class Catch : Statement {
public readonly string Name;
- public readonly Block Block;
- public readonly Block VarBlock;
+ public Block Block;
+ public Block VarBlock;
Expression type_expr;
Type type;
return true;
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Catch target = (Catch) t;
+
+ if (type_expr != null)
+ target.type_expr = type_expr.Clone (clonectx);
+ if (VarBlock != null)
+ target.VarBlock = clonectx.LookupBlock (VarBlock);
+ target.Block = clonectx.LookupBlock (Block);
+ }
}
public class Try : ExceptionStatement {
- public readonly Block Fini, Block;
- public readonly ArrayList Specific;
- public readonly Catch General;
+ public Block Fini, Block;
+ public ArrayList Specific;
+ public Catch General;
bool need_exc_block;
Report.Debug (1, "START OF CATCH BLOCKS", vector);
- Type[] prevCatches = new Type [Specific.Count];
+ Type[] prev_catches = new Type [Specific.Count];
int last_index = 0;
foreach (Catch c in Specific){
ec.CurrentBranching.CreateSibling (
if (!c.Resolve (ec))
return false;
- Type resolvedType = c.CatchType;
+ Type resolved_type = c.CatchType;
for (int ii = 0; ii < last_index; ++ii) {
- if (resolvedType == prevCatches [ii] || resolvedType.IsSubclassOf (prevCatches [ii])) {
- Report.Error (160, c.loc, "A previous catch clause already catches all exceptions of this or a super type `{0}'", prevCatches [ii].FullName);
+ if (resolved_type == prev_catches [ii] || resolved_type.IsSubclassOf (prev_catches [ii])) {
+ Report.Error (160, c.loc, "A previous catch clause already catches all exceptions of this or a super type `{0}'", prev_catches [ii].FullName);
return false;
}
}
- prevCatches [last_index++] = resolvedType;
+ prev_catches [last_index++] = resolved_type;
need_exc_block = true;
}
} else
emit_finally = Fini != null;
- FlowBranching.Reachability reachability = ec.EndFlowBranching ();
+ ec.EndFlowBranching ();
- FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
+ // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
+ // So, ensure there's some IL code after the finally block.
+ ec.NeedReturnLabel ();
- Report.Debug (1, "END OF TRY", ec.CurrentBranching, reachability, vector, f_vector);
+ FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
- if (!reachability.AlwaysReturns) {
- // Unfortunately, System.Reflection.Emit automatically emits
- // a leave to the end of the finally block. This is a problem
- // if `returns' is true since we may jump to a point after the
- // end of the method.
- // As a workaround, emit an explicit ret here.
- ec.NeedReturnLabel ();
- }
+ Report.Debug (1, "END OF TRY", ec.CurrentBranching, vector, f_vector);
return ok;
}
return General != null || Specific.Count > 0;
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Try target = (Try) t;
+
+ target.Block = clonectx.LookupBlock (Block);
+ if (Fini != null)
+ target.Fini = clonectx.LookupBlock (Fini);
+ if (General != null)
+ target.General = (Catch) General.Clone (clonectx);
+ if (Specific != null){
+ target.Specific = new ArrayList ();
+ foreach (Catch c in Specific)
+ target.Specific.Add (c.Clone (clonectx));
+ }
+ }
}
public class Using : ExceptionStatement {
Type expr_type;
Expression [] resolved_vars;
Expression [] converted_vars;
- ExpressionStatement [] assign;
+ Expression [] assign;
TemporaryVariable local_copy;
public Using (object expression_or_block, Statement stmt, Location l)
//
bool ResolveLocalVariableDecls (EmitContext ec)
{
- int i = 0;
-
- TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
- if (texpr == null)
- return false;
-
- expr_type = texpr.Type;
-
- //
- // The type must be an IDisposable or an implicit conversion
- // must exist.
- //
- converted_vars = new Expression [var_list.Count];
- resolved_vars = new Expression [var_list.Count];
- assign = new ExpressionStatement [var_list.Count];
-
- bool need_conv = !TypeManager.ImplementsInterface (
- expr_type, TypeManager.idisposable_type);
+ resolved_vars = new Expression[var_list.Count];
+ assign = new Expression [var_list.Count];
+ converted_vars = new Expression[var_list.Count];
- foreach (DictionaryEntry e in var_list){
+ for (int i = 0; i < assign.Length; ++i) {
+ DictionaryEntry e = (DictionaryEntry) var_list [i];
Expression var = (Expression) e.Key;
+ Expression new_expr = (Expression) e.Value;
- var = var.ResolveLValue (ec, new EmptyExpression (), loc);
- if (var == null)
+ Expression a = new Assign (var, new_expr, loc);
+ a = a.Resolve (ec);
+ if (a == null)
return false;
resolved_vars [i] = var;
+ assign [i] = a;
- if (!need_conv) {
- i++;
+ if (TypeManager.ImplementsInterface (a.Type, TypeManager.idisposable_type)) {
+ converted_vars [i] = var;
continue;
}
- converted_vars [i] = Convert.ImplicitConversion (
- ec, var, TypeManager.idisposable_type, loc);
-
- if (converted_vars [i] == null) {
- Error_IsNotConvertibleToIDisposable ();
+ a = Convert.ImplicitConversionStandard (ec, a, TypeManager.idisposable_type, var.Location);
+ if (a == null) {
+ Error_IsNotConvertibleToIDisposable (var);
return false;
}
- i++;
- }
-
- i = 0;
- foreach (DictionaryEntry e in var_list){
- Expression var = resolved_vars [i];
- Expression new_expr = (Expression) e.Value;
- Expression a;
-
- a = new Assign (var, new_expr, loc);
- a = a.Resolve (ec);
- if (a == null)
- return false;
-
- if (!need_conv)
- converted_vars [i] = var;
- assign [i] = (ExpressionStatement) a;
- i++;
+ converted_vars [i] = a;
}
return true;
}
- void Error_IsNotConvertibleToIDisposable ()
+ static void Error_IsNotConvertibleToIDisposable (Expression expr)
{
- Report.Error (1674, loc, "`{0}': type used in a using statement must be implicitly convertible to `System.IDisposable'",
- TypeManager.CSharpName (expr_type));
+ Report.SymbolRelatedToPreviousError (expr.Type);
+ Report.Error (1674, expr.Location, "`{0}': type used in a using statement must be implicitly convertible to `System.IDisposable'",
+ expr.GetSignatureForError ());
}
bool ResolveExpression (EmitContext ec)
{
if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
if (Convert.ImplicitConversion (ec, expr, TypeManager.idisposable_type, loc) == null) {
- Error_IsNotConvertibleToIDisposable ();
+ Error_IsNotConvertibleToIDisposable (expr);
return false;
}
}
int i = 0;
for (i = 0; i < assign.Length; i++) {
- assign [i].EmitStatement (ec);
+ ExpressionStatement es = assign [i] as ExpressionStatement;
+
+ if (es != null)
+ es.EmitStatement (ec);
+ else {
+ assign [i].Emit (ec);
+ ig.Emit (OpCodes.Pop);
+ }
if (emit_finally)
ig.BeginExceptionBlock ();
bool ok = Statement.Resolve (ec);
- if (!ok) {
- ec.KillFlowBranching ();
- return false;
- }
-
ResolveFinally (branching);
- FlowBranching.Reachability reachability = ec.EndFlowBranching ();
- if (!reachability.AlwaysReturns) {
- // Unfortunately, System.Reflection.Emit automatically emits a leave
- // to the end of the finally block. This is a problem if `returns'
- // is true since we may jump to a point after the end of the method.
- // As a workaround, emit an explicit ret here.
- ec.NeedReturnLabel ();
- }
+ ec.EndFlowBranching ();
- return true;
+ // System.Reflection.Emit automatically emits a 'leave' to the end of the finally block.
+ // So, ensure there's some IL code after the finally block.
+ ec.NeedReturnLabel ();
+
+ return ok;
}
protected override void DoEmit (EmitContext ec)
else if (expression_or_block is Expression)
EmitExpressionFinally (ec);
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Using target = (Using) t;
+
+ if (expression_or_block is Expression)
+ target.expression_or_block = ((Expression) expression_or_block).Clone (clonectx);
+ else
+ target.expression_or_block = ((Statement) expression_or_block).Clone (clonectx);
+
+ target.Statement = Statement.Clone (clonectx);
+ }
}
/// <summary>
if (expr == null)
return false;
- Constant c = expr as Constant;
- if (c != null && c.GetValue () == null) {
+ if (expr.Type == TypeManager.null_type) {
Report.Error (186, loc, "Use of null is not valid in this context");
return false;
}
- TypeExpr texpr = type.ResolveAsTypeTerminal (ec, false);
- if (texpr == null)
- return false;
-
- Type var_type = texpr.Type;
-
if (expr.eclass == ExprClass.MethodGroup || expr is AnonymousMethodExpression) {
Report.Error (446, expr.Location, "Foreach statement cannot operate on a `{0}'",
expr.ExprClassName);
}
if (expr.Type.IsArray) {
- array = new ArrayForeach (var_type, variable, expr, statement, loc);
+ array = new ArrayForeach (type, variable, expr, statement, loc);
return array.Resolve (ec);
- } else {
- collection = new CollectionForeach (
- var_type, variable, expr, statement, loc);
- return collection.Resolve (ec);
}
+
+ collection = new CollectionForeach (type, variable, expr, statement, loc);
+ return collection.Resolve (ec);
}
protected override void DoEmit (EmitContext ec)
Expression variable, expr, conv;
Statement statement;
Type array_type;
- Type var_type;
+ Expression var_type;
TemporaryVariable[] lengths;
ArrayCounter[] counter;
int rank;
TemporaryVariable copy;
Expression access;
- public ArrayForeach (Type var_type, Expression var,
+ public ArrayForeach (Expression var_type, Expression var,
Expression expr, Statement stmt, Location l)
{
this.var_type = var_type;
if (access == null)
return false;
- conv = Convert.ExplicitConversion (ec, access, var_type, loc);
+ VarExpr ve = var_type as VarExpr;
+ if (ve != null) {
+ // Infer implicitly typed local variable from foreach array type
+ var_type = new TypeExpression (access.Type, ve.Location);
+ }
+
+ var_type = var_type.ResolveAsTypeTerminal (ec, false);
+ if (var_type == null)
+ return false;
+
+ conv = Convert.ExplicitConversion (ec, access, var_type.Type, loc);
if (conv == null)
return false;
MethodGroupExpr get_enumerator;
PropertyExpr get_current;
MethodInfo move_next;
- Type var_type, enumerator_type;
+ Expression var_type;
+ Type enumerator_type;
bool is_disposable;
bool enumerator_found;
- public CollectionForeach (Type var_type, Expression var,
+ public CollectionForeach (Expression var_type, Expression var,
Expression expr, Statement stmt, Location l)
{
this.var_type = var_type;
TypeManager.CSharpName (expr.Type));
}
+ bool IsOverride (MethodInfo m)
+ {
+ m = (MethodInfo) TypeManager.DropGenericMethodArguments (m);
+
+ if (!m.IsVirtual || ((m.Attributes & MethodAttributes.NewSlot) != 0))
+ return false;
+ if (m is MethodBuilder)
+ return true;
+
+ MethodInfo base_method = m.GetBaseDefinition ();
+ return base_method != m;
+ }
+
bool TryType (EmitContext ec, Type t)
{
MethodGroupExpr mg = Expression.MemberLookup (
if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
continue;
- if (TypeManager.IsOverride (mi))
+ if (IsOverride (mi))
continue;
enumerator_found = true;
if (!TypeManager.IsGenericType (mi.ReturnType))
continue;
+ MethodBase mb = TypeManager.DropGenericMethodArguments (mi);
Report.SymbolRelatedToPreviousError (t);
Report.Error(1640, loc, "foreach statement cannot operate on variables of type `{0}' " +
- "because it contains multiple implementation of `{1}'. Try casting to a specific implementation",
- TypeManager.CSharpName (t), TypeManager.CSharpSignature (mi));
+ "because it contains multiple implementation of `{1}'. Try casting to a specific implementation",
+ TypeManager.CSharpName (t), TypeManager.CSharpSignature (mb));
return false;
}
// Always prefer generics enumerators
if (!TypeManager.IsGenericType (mi.ReturnType)) {
+ if (TypeManager.ImplementsInterface (mi.DeclaringType, result.DeclaringType) ||
+ TypeManager.ImplementsInterface (result.DeclaringType, mi.DeclaringType))
+ continue;
+
Report.SymbolRelatedToPreviousError (result);
Report.SymbolRelatedToPreviousError (mi);
Report.Warning (278, 2, loc, "`{0}' contains ambiguous implementation of `{1}' pattern. Method `{2}' is ambiguous with method `{3}'",
get_current = tmp_get_cur;
enumerator_type = tmp_enumerator_type;
MethodInfo[] mi = new MethodInfo[] { (MethodInfo) result };
- get_enumerator = new MethodGroupExpr (mi, loc);
+ get_enumerator = new MethodGroupExpr (mi, enumerator_type, loc);
if (t != expr.Type) {
expr = Convert.ExplicitConversion (
//
// Now try to find the method in the interfaces
//
- while (t != null){
- Type [] ifaces = t.GetInterfaces ();
-
- foreach (Type i in ifaces){
- if (TryType (ec, i))
- return true;
- }
-
- //
- // Since TypeBuilder.GetInterfaces only returns the interface
- // types for this type, we have to keep looping, but once
- // we hit a non-TypeBuilder (ie, a Type), then we know we are
- // done, because it returns all the types
- //
- if ((t is TypeBuilder))
- t = t.BaseType;
- else
- break;
+ Type [] ifaces = TypeManager.GetInterfaces (t);
+ foreach (Type i in ifaces){
+ if (TryType (ec, i))
+ return true;
}
return false;
return false;
}
+ VarExpr ve = var_type as VarExpr;
+ if (ve != null) {
+ // Infer implicitly typed local variable from foreach enumerable type
+ var_type = new TypeExpression (get_current.PropertyInfo.PropertyType, var_type.Location);
+ }
+
+ var_type = var_type.ResolveAsTypeTerminal (ec, false);
+ if (var_type == null)
+ return false;
+
enumerator = new TemporaryVariable (enumerator_type, loc);
enumerator.Resolve (ec);
- init = new Invocation (get_enumerator, new ArrayList ());
+ init = new Invocation (get_enumerator, null);
init = init.Resolve (ec);
if (init == null)
return false;
Expression move_next_expr;
{
MemberInfo[] mi = new MemberInfo[] { move_next };
- MethodGroupExpr mg = new MethodGroupExpr (mi, loc);
+ MethodGroupExpr mg = new MethodGroupExpr (mi, var_type.Type, loc);
mg.InstanceExpression = enumerator;
- move_next_expr = new Invocation (mg, new ArrayList ());
+ move_next_expr = new Invocation (mg, null);
}
get_current.InstanceExpression = enumerator;
Statement block = new CollectionForeachStatement (
- var_type, variable, get_current, statement, loc);
+ var_type.Type, variable, get_current, statement, loc);
loop = new While (move_next_expr, block, loc);
statement.Emit (ec);
}
}
+
+ protected override void CloneTo (CloneContext clonectx, Statement t)
+ {
+ Foreach target = (Foreach) t;
+
+ target.type = type.Clone (clonectx);
+ target.variable = variable.Clone (clonectx);
+ target.expr = expr.Clone (clonectx);
+ target.statement = statement.Clone (clonectx);
+ }
}
}