//
// Author:
// Miguel de Icaza (miguel@ximian.com)
-// Martin Baulig (martin@gnome.org)
+// Martin Baulig (martin@ximian.com)
//
// (C) 2001, 2002, 2003 Ximian, Inc.
// (C) 2003, 2004 Novell, Inc.
// in unreachable code, for instance.
//
+ if (warn && (RootContext.WarningLevel >= 2))
+ Report.Warning (162, loc, "Unreachable code detected");
+
ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
bool ok = Resolve (ec);
ec.KillFlowBranching ();
- if (!ok)
- return false;
-
- if (warn && (RootContext.WarningLevel >= 2))
- Report.Warning (162, loc, "Unreachable code detected");
- return true;
+ return ok;
}
protected void CheckObsolete (Type type)
return false;
}
+ Assign ass = expr as Assign;
+ if (ass != null && ass.Source is Constant) {
+ Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+ }
+
//
// Dead code elimination
//
public override bool Resolve (EmitContext ec)
{
- expr = expr.ResolveStatement (ec);
+ if (expr != null)
+ expr = expr.ResolveStatement (ec);
return expr != null;
}
return false;
}
+ if (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;
+ }
+
Expr = Expr.Resolve (ec);
if (Expr == null)
return false;
label.AddUsageVector (ec.CurrentBranching.CurrentUsageVector);
ec.CurrentBranching.CurrentUsageVector.Goto ();
+ label.AddReference ();
return true;
}
bool defined;
bool referenced;
Label label;
+ ILGenerator ig;
FlowBranching.UsageVector vectors;
{
if (defined)
return label;
+ ig = ec.ig;
label = ec.ig.DefineLabel ();
defined = true;
{
ec.CurrentBranching.Label (vectors);
- referenced = true;
-
return true;
}
protected override void DoEmit (EmitContext ec)
{
+ if (ig != null && ig != ec.ig) {
+ Report.Error (1632, "Control cannot leave body of anonymous method");
+ return;
+ }
LabelTarget (ec);
ec.ig.MarkLabel (label);
}
+
+ public void AddReference ()
+ {
+ referenced = true;
+ }
}
public override bool Resolve (EmitContext ec)
{
- bool in_catch = ec.CurrentBranching.InCatch ();
ec.CurrentBranching.CurrentUsageVector.Throw ();
if (expr != null){
ExprClass eclass = expr.eclass;
if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
- eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
+ eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
expr.Error_UnexpectedKind ("value, variable, property or indexer access ", loc);
return false;
}
Type t = expr.Type;
if ((t != TypeManager.exception_type) &&
- !t.IsSubclassOf (TypeManager.exception_type) &&
- !(expr is NullLiteral)) {
+ !t.IsSubclassOf (TypeManager.exception_type) &&
+ !(expr is NullLiteral)) {
Error (155,
- "The type caught or thrown must be derived " +
- "from System.Exception");
+ "The type caught or thrown must be derived " +
+ "from System.Exception");
return false;
}
- } else if (!in_catch) {
- Error (156,
- "A throw statement with no argument is only " +
- "allowed in a catch clause");
+ return true;
+ }
+
+ if (ec.CurrentBranching.InFinally (true)) {
+ Error (724, "A throw statement with no argument is only allowed in a catch clause nested inside of the innermost catch clause");
return false;
}
+ if (!ec.CurrentBranching.InCatch ()) {
+ Error (156, "A throw statement with no argument is only allowed in a catch clause");
+ return false;
+ }
return true;
}
// </summary>
public LocalInfo ThisVariable {
get {
- if (this_variable != null)
- return this_variable;
- else if (Parent != null)
- return Parent.ThisVariable;
- else
- return null;
+ for (Block b = this; b != null; b = b.Parent) {
+ if (b.this_variable != null)
+ return b.this_variable;
+ }
+
+ return null;
}
}
/// </remarks>
public void ResolveMeta (ToplevelBlock toplevel, EmitContext ec, InternalParameters ip)
{
- ILGenerator ig = ec.ig;
-
bool old_unsafe = ec.InUnsafe;
// If some parent block was unsafe, we remain unsafe even if this block
Report.Debug (4, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
- bool unreachable = false;
+ bool unreachable = unreachable_shown;
int statement_count = statements.Count;
for (int ix = 0; ix < statement_count; ix++){
else
s.loc = Location.Null;
- statements [ix] = EmptyStatement.Value;
- continue;
+ if (ok && !(s is Block)) {
+ statements [ix] = EmptyStatement.Value;
+ continue;
+ }
}
if (s.Resolve (ec) == false) {
public override bool ResolveUnreachable (EmitContext ec, bool warn)
{
unreachable_shown = true;
- return base.ResolveUnreachable (ec, warn);
+
+ if (warn && (RootContext.WarningLevel >= 2))
+ Report.Warning (162, loc, "Unreachable code detected");
+
+ if (Implicit)
+ return Resolve (ec);
+
+ ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
+ bool ok = Resolve (ec);
+ ec.KillFlowBranching ();
+
+ return ok;
}
protected override void DoEmit (EmitContext ec)
// Check whether we are the last statement in a
// top-level block.
- if ((Parent == null) && (ix+1 == num_statements))
+ if (((Parent == null) || Implicit) && (ix+1 == num_statements) && !(s is Block))
ec.IsLastStatement = true;
else
ec.IsLastStatement = false;
if (emit_debug_info) {
if (is_lexical_block)
- ec.ig.BeginScope ();
+ ec.BeginScope ();
if (variables != null) {
foreach (DictionaryEntry de in variables) {
ec.Mark (EndLocation, true);
if (emit_debug_info && is_lexical_block)
- ec.ig.EndScope ();
+ ec.EndScope ();
ec.CurrentBlock = prev_block;
}
static int did = 0;
- int my_id = did++;
-
public void RegisterCaptureContext (CaptureContext cc)
{
if (allowed_types == null){
allowed_types = new Type [] {
+ TypeManager.int32_type,
+ TypeManager.uint32_type,
TypeManager.sbyte_type,
TypeManager.byte_type,
TypeManager.short_type,
TypeManager.ushort_type,
- TypeManager.int32_type,
- TypeManager.uint32_type,
TypeManager.int64_type,
TypeManager.uint64_type,
TypeManager.char_type,
if (e == null)
continue;
+ //
+ // Ignore over-worked ImplicitUserConversions that do
+ // an implicit conversion in addition to the user conversion.
+ //
+ if (e is UserCast){
+ UserCast ue = e as UserCast;
+
+ if (ue.Source != Expr)
+ e = null;
+ }
+
if (converted != null){
- Report.Error (-12, loc, "More than one conversion to an integral " +
- " type exists for type `" +
- TypeManager.CSharpName (Expr.Type)+"'");
+ Report.ExtraInformation (
+ loc,
+ String.Format ("reason: more than one conversion to an integral type exist for type {0}",
+ TypeManager.CSharpName (Expr.Type)));
return null;
- } else
+ } else {
converted = e;
+ }
}
return converted;
}
ArrayList declarators;
Statement statement;
Type expr_type;
- FixedData[] data;
+ Emitter[] data;
bool has_ret;
- struct FixedData {
- public bool is_object;
- public LocalInfo vi;
- public Expression expr;
- public Expression converted;
- }
+ abstract class Emitter
+ {
+ protected LocalInfo vi;
+ protected Expression converted;
+
+ protected Emitter (Expression expr, LocalInfo li)
+ {
+ converted = expr;
+ vi = li;
+ }
+
+ public abstract void Emit (EmitContext ec);
+ public abstract void EmitExit (ILGenerator ig);
+ }
+
+ class ExpressionEmitter: Emitter {
+ public ExpressionEmitter (Expression converted, LocalInfo li) :
+ base (converted, li)
+ {
+ }
+
+ public override void Emit (EmitContext ec) {
+ //
+ // Store pointer in pinned location
+ //
+ converted.Emit (ec);
+ ec.ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+ }
+
+ public override void EmitExit (ILGenerator ig)
+ {
+ ig.Emit (OpCodes.Ldc_I4_0);
+ ig.Emit (OpCodes.Conv_U);
+ ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+ }
+ }
+
+ class StringEmitter: Emitter {
+ LocalBuilder pinned_string;
+ Location loc;
+
+ public StringEmitter (Expression expr, LocalInfo li, Location loc):
+ base (expr, li)
+ {
+ this.loc = loc;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+ pinned_string = TypeManager.DeclareLocalPinned (ig, TypeManager.string_type);
+
+ converted.Emit (ec);
+ ig.Emit (OpCodes.Stloc, pinned_string);
+
+ Expression sptr = new StringPtr (pinned_string, loc);
+ converted = Convert.ImplicitConversionRequired (
+ ec, sptr, vi.VariableType, loc);
+
+ if (converted == null)
+ return;
+
+ converted.Emit (ec);
+ ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
+ }
+
+ public override void EmitExit(ILGenerator ig)
+ {
+ ig.Emit (OpCodes.Ldnull);
+ ig.Emit (OpCodes.Stloc, pinned_string);
+ }
+ }
public Fixed (Expression type, ArrayList decls, Statement stmt, Location l)
{
return false;
}
- data = new FixedData [declarators.Count];
+ data = new Emitter [declarators.Count];
if (!expr_type.IsPointer){
Report.Error (209, loc, "Variables in a fixed statement must be pointers");
if (!TypeManager.VerifyUnManaged (child.Type, loc))
return false;
- data [i].is_object = true;
- data [i].expr = e;
- data [i].converted = null;
- data [i].vi = vi;
+ data [i] = new ExpressionEmitter (e, vi);
i++;
continue;
// and T* is implicitly convertible to the
// pointer type given in the fixed statement.
//
- ArrayPtr array_ptr = new ArrayPtr (e, loc);
+ ArrayPtr array_ptr = new ArrayPtr (e, array_type, loc);
Expression converted = Convert.ImplicitConversionRequired (
ec, array_ptr, vi.VariableType, loc);
if (converted == null)
return false;
- data [i].is_object = false;
- data [i].expr = e;
- data [i].converted = converted;
- data [i].vi = vi;
+ data [i] = new ExpressionEmitter (converted, vi);
i++;
continue;
// Case 3: string
//
if (e.Type == TypeManager.string_type){
- data [i].is_object = false;
- data [i].expr = e;
- data [i].converted = null;
- data [i].vi = vi;
+ data [i] = new StringEmitter (e, vi, loc);
i++;
continue;
}
+ // 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;
+ }
+ }
+
//
// For other cases, flag a `this is already fixed expression'
//
protected override void DoEmit (EmitContext ec)
{
- ILGenerator ig = ec.ig;
-
- LocalBuilder [] clear_list = new LocalBuilder [data.Length];
-
for (int i = 0; i < data.Length; i++) {
- LocalInfo vi = data [i].vi;
-
- //
- // Case 1: & object.
- //
- if (data [i].is_object) {
- //
- // Store pointer in pinned location
- //
- data [i].expr.Emit (ec);
- ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
- clear_list [i] = vi.LocalBuilder;
- continue;
- }
-
- //
- // Case 2: Array
- //
- if (data [i].expr.Type.IsArray){
- //
- // Store pointer in pinned location
- //
- data [i].converted.Emit (ec);
-
- ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
- clear_list [i] = vi.LocalBuilder;
- continue;
- }
-
- //
- // Case 3: string
- //
- if (data [i].expr.Type == TypeManager.string_type){
- LocalBuilder pinned_string = TypeManager.DeclareLocalPinned (ig, TypeManager.string_type);
- clear_list [i] = pinned_string;
-
- data [i].expr.Emit (ec);
- ig.Emit (OpCodes.Stloc, pinned_string);
-
- Expression sptr = new StringPtr (pinned_string, loc);
- Expression converted = Convert.ImplicitConversionRequired (
- ec, sptr, vi.VariableType, loc);
-
- if (converted == null)
- continue;
-
- converted.Emit (ec);
- ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
- }
+ data [i].Emit (ec);
}
statement.Emit (ec);
if (has_ret)
return;
+ ILGenerator ig = ec.ig;
+
//
// Clear the pinned variable
//
for (int i = 0; i < data.Length; i++) {
- if (data [i].is_object || data [i].expr.Type.IsArray) {
- ig.Emit (OpCodes.Ldc_I4_0);
- ig.Emit (OpCodes.Conv_U);
- ig.Emit (OpCodes.Stloc, clear_list [i]);
- } else if (data [i].expr.Type == TypeManager.string_type){
- ig.Emit (OpCodes.Ldnull);
- ig.Emit (OpCodes.Stloc, clear_list [i]);
- }
+ data [i].EmitExit (ig);
}
}
}
ILGenerator ig = ec.ig;
int i = assign.Length;
- foreach (DictionaryEntry e in var_list){
+ for (int ii = 0; ii < var_list.Count; ++ii){
Expression var = resolved_vars [--i];
Label skip = ig.DefineLabel ();
void EmitExpressionFinally (EmitContext ec)
{
ILGenerator ig = ec.ig;
- Label skip = ig.DefineLabel ();
- ig.Emit (OpCodes.Ldloc, local_copy);
- ig.Emit (OpCodes.Brfalse, skip);
- ig.Emit (OpCodes.Ldloc, local_copy);
- ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
- ig.MarkLabel (skip);
+ if (!local_copy.LocalType.IsValueType) {
+ Label skip = ig.DefineLabel ();
+ ig.Emit (OpCodes.Ldloc, local_copy);
+ ig.Emit (OpCodes.Brfalse, skip);
+ ig.Emit (OpCodes.Ldloc, local_copy);
+ ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+ ig.MarkLabel (skip);
+ } else {
+ Expression ml = Expression.MemberLookup(ec, TypeManager.idisposable_type, local_copy.LocalType, "Dispose", Mono.CSharp.Location.Null);
+
+ if (!(ml is MethodGroupExpr)) {
+ ig.Emit (OpCodes.Ldloc, local_copy);
+ ig.Emit (OpCodes.Box, local_copy.LocalType);
+ ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+ } else {
+ MethodInfo mi = null;
+
+ foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
+ if (TypeManager.GetArgumentTypes (mk).Length == 0) {
+ mi = mk;
+ break;
+ }
+ }
+
+ if (mi == null) {
+ Report.Error(-100, Mono.CSharp.Location.Null, "Internal error: No Dispose method which takes 0 parameters.");
+ return;
+ }
+
+ ig.Emit (OpCodes.Ldloca, local_copy);
+ ig.Emit (OpCodes.Call, mi);
+ }
+ }
}
public override bool Resolve (EmitContext ec)
if (expr == null)
return false;
+ if (expr is NullLiteral) {
+ Report.Error (186, expr.Location, "Use of null is not valid in this context");
+ return false;
+ }
+
TypeExpr texpr = type.ResolveAsTypeTerminal (ec, false);
if (texpr == null)
return false;
return false;
}
ForeachHelperMethods hm = (ForeachHelperMethods) criteria;
- EmitContext ec = hm.ec;
// Check whether GetEnumerator is public
if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
ig.Emit (OpCodes.Brfalse, end_try);
if (ec.InIterator)
- enumerator.EmitThis (ig);
+ ig.Emit (OpCodes.Ldarg_0);
+
enumerator.EmitCall (ig, hm.get_current);
if (ec.InIterator){
ig.MarkLabel (loop);
if (ec.InIterator)
- ec.EmitThis ();
+ ig.Emit (OpCodes.Ldarg_0);
copy.EmitThis (ig);
copy.EmitLoad (ig);
}
if (ec.InIterator)
- ec.EmitThis ();
+ ig.Emit (OpCodes.Ldarg_0);
+
copy.EmitThis (ig);
copy.EmitLoad (ig);
for (dim = 0; dim < rank; dim++){