//
// Author:
// Miguel de Icaza (miguel@ximian.com)
+// Marek Safar (marek.safar@seznam.cz)
//
// (C) 2001, 2002, 2003 Ximian, Inc.
//
set { type = value; }
}
- public Location Location {
+ public virtual Location Location {
get { return loc; }
}
Report.Error (error, loc, s);
}
- /// <summary>
- /// Utility wrapper routine for Warning, just to beautify the code
- /// </summary>
- public void Warning (int code, string format, params object[] args)
- {
- Report.Warning (code, loc, format, args);
- }
-
// Not nice but we have broken hierarchy
public virtual void CheckMarshallByRefAccess (Type container) {}
- /// <summary>
- /// Tests presence of ObsoleteAttribute and report proper error
- /// </summary>
- protected void CheckObsoleteAttribute (Type type)
+ public virtual bool GetAttributableValue (Type valueType, out object value)
{
- ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
- if (obsolete_attr == null)
- return;
-
- AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
+ Attribute.Error_AttributeArgumentNotValid (loc);
+ value = null;
+ return false;
}
public virtual string GetSignatureForError ()
// This is used if the expression should be resolved as a type or namespace name.
// the default implementation fails.
//
- public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+ public virtual FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
return null;
}
// value will be returned if the expression is not a type
// reference
//
- public TypeExpr ResolveAsTypeTerminal (EmitContext ec, bool silent)
+ public virtual TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
+ {
+ TypeExpr te = ResolveAsBaseTerminal (ec, silent);
+ if (te == null)
+ return null;
+
+ ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (te.Type);
+ if (obsolete_attr != null && !ec.IsInObsoleteScope) {
+ AttributeTester.Report_ObsoleteMessage (obsolete_attr, GetSignatureForError (), Location);
+ }
+ return te;
+ }
+
+ public TypeExpr ResolveAsBaseTerminal (IResolveContext ec, bool silent)
{
int errors = Report.Errors;
FullNamedExpression fne = ResolveAsTypeStep (ec, silent);
- if (fne == null)
+ if (fne == null){
+ if (!silent && errors == Report.Errors)
+ Report.Error (118, loc, "Expecting a type.");
return null;
+ }
if (fne.eclass != ExprClass.Type) {
if (!silent && errors == Report.Errors)
TypeExpr te = fne as TypeExpr;
- if (!te.CheckAccessLevel (ec.DeclSpace)) {
+ if (!te.CheckAccessLevel (ec.DeclContainer)) {
ErrorIsInaccesible (loc, TypeManager.CSharpName (te.Type));
return null;
}
+ te.loc = loc;
return te;
}
Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", name);
}
- public virtual void Error_ValueCannotBeConverted (Location loc, Type t)
+ protected static void Error_CannotAccessProtected (Location loc, MemberInfo m, Type qualifier, Type container)
{
- Convert.Error_CannotImplicitConversion (loc, Type, t);
+ Report.Error (1540, loc, "Cannot access protected member `{0}' via a qualifier of type `{1}';"
+ + " the qualifier must be of type `{2}' (or derived from it)",
+ TypeManager.GetFullNameSignature (m),
+ TypeManager.CSharpName (qualifier),
+ TypeManager.CSharpName (container));
+
+ }
+
+ public virtual void Error_ValueCannotBeConverted (Location loc, Type target, bool expl)
+ {
+ if (Type.Name == target.Name){
+ Report.ExtraInformation (loc,
+ String.Format (
+ "The type {0} has two conflicting definitions, one comes from {1} and the other from {2}",
+ Type.Name, Type.Assembly.FullName, target.Assembly.FullName));
+
+ }
+
+ if (expl) {
+ Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
+ GetSignatureForError (), TypeManager.CSharpName (target));
+ return;
+ }
+
+ Expression e = (this is EnumConstant) ? ((EnumConstant)this).Child : this;
+ bool b = Convert.ExplicitNumericConversion (e, target) != null;
+
+ if (b || Convert.ExplicitReferenceConversionExists (Type, target) || Convert.ExplicitUnsafe (e, target) != null) {
+ Report.Error (266, loc, "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
+ TypeManager.CSharpName (Type), TypeManager.CSharpName (target));
+ return;
+ }
+
+ if (Type != TypeManager.string_type && this is Constant && !(this is NullCast)) {
+ Report.Error (31, loc, "Constant value `{0}' cannot be converted to a `{1}'",
+ GetSignatureForError (), TypeManager.CSharpName (target));
+ return;
+ }
+
+ Report.Error (29, loc, "Cannot implicitly convert type {0} to `{1}'",
+ Type == TypeManager.anonymous_method_type ?
+ "anonymous method" : "`" + GetSignatureForError () + "'",
+ TypeManager.CSharpName (target));
}
protected static void Error_TypeDoesNotContainDefinition (Location loc, Type type, string name)
TypeManager.CSharpName (type), name);
}
- ResolveFlags ExprClassToResolveFlags ()
+ ResolveFlags ExprClassToResolveFlags
{
- switch (eclass) {
- case ExprClass.Type:
- case ExprClass.Namespace:
- return ResolveFlags.Type;
-
- case ExprClass.MethodGroup:
- return ResolveFlags.MethodGroup;
-
- case ExprClass.Value:
- case ExprClass.Variable:
- case ExprClass.PropertyAccess:
- case ExprClass.EventAccess:
- case ExprClass.IndexerAccess:
- return ResolveFlags.VariableOrValue;
-
- default:
- throw new Exception ("Expression " + GetType () +
- " ExprClass is Invalid after resolve");
+ get {
+ switch (eclass) {
+ case ExprClass.Type:
+ case ExprClass.Namespace:
+ return ResolveFlags.Type;
+
+ case ExprClass.MethodGroup:
+ return ResolveFlags.MethodGroup;
+
+ case ExprClass.Value:
+ case ExprClass.Variable:
+ case ExprClass.PropertyAccess:
+ case ExprClass.EventAccess:
+ case ExprClass.IndexerAccess:
+ return ResolveFlags.VariableOrValue;
+
+ default:
+ throw new Exception ("Expression " + GetType () +
+ " ExprClass is Invalid after resolve");
+ }
}
-
}
/// <summary>
ec.OmitStructFlowAnalysis = true;
Expression e;
- bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
- if (this is SimpleName)
+ if (this is SimpleName) {
+ bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
e = ((SimpleName) this).DoResolve (ec, intermediate);
-
+ }
else
e = DoResolve (ec);
if (e == null)
return null;
- if ((flags & e.ExprClassToResolveFlags ()) == 0) {
+ if ((flags & e.ExprClassToResolveFlags) == 0) {
e.Error_UnexpectedKind (flags, loc);
return null;
}
public Constant ResolveAsConstant (EmitContext ec, MemberCore mc)
{
Expression e = Resolve (ec);
- if (e != null) {
- Constant c = e as Constant;
- if (c != null)
- return c;
-
- EmptyCast empty = e as EmptyCast;
- if (empty != null) {
- c = empty.Child as Constant;
- if (c != null) {
- // TODO: not sure about this maybe there is easier way how to use EmptyCast
- if (e.Type.IsEnum)
- c.Type = e.Type;
-
- return c;
- }
- }
- }
+ if (e == null)
+ return null;
+
+ Constant c = e as Constant;
+ if (c != null)
+ return c;
Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError ());
return null;
public static Constant Constantify (object v, Type t)
{
if (t == TypeManager.int32_type)
- return new IntConstant ((int) v);
+ return new IntConstant ((int) v, Location.Null);
else if (t == TypeManager.uint32_type)
- return new UIntConstant ((uint) v);
+ return new UIntConstant ((uint) v, Location.Null);
else if (t == TypeManager.int64_type)
- return new LongConstant ((long) v);
+ return new LongConstant ((long) v, Location.Null);
else if (t == TypeManager.uint64_type)
- return new ULongConstant ((ulong) v);
+ return new ULongConstant ((ulong) v, Location.Null);
else if (t == TypeManager.float_type)
- return new FloatConstant ((float) v);
+ return new FloatConstant ((float) v, Location.Null);
else if (t == TypeManager.double_type)
- return new DoubleConstant ((double) v);
+ return new DoubleConstant ((double) v, Location.Null);
else if (t == TypeManager.string_type)
- return new StringConstant ((string) v);
+ return new StringConstant ((string) v, Location.Null);
else if (t == TypeManager.short_type)
- return new ShortConstant ((short)v);
+ return new ShortConstant ((short)v, Location.Null);
else if (t == TypeManager.ushort_type)
- return new UShortConstant ((ushort)v);
+ return new UShortConstant ((ushort)v, Location.Null);
else if (t == TypeManager.sbyte_type)
- return new SByteConstant (((sbyte)v));
+ return new SByteConstant ((sbyte)v, Location.Null);
else if (t == TypeManager.byte_type)
- return new ByteConstant ((byte)v);
+ return new ByteConstant ((byte)v, Location.Null);
else if (t == TypeManager.char_type)
- return new CharConstant ((char)v);
+ return new CharConstant ((char)v, Location.Null);
else if (t == TypeManager.bool_type)
- return new BoolConstant ((bool) v);
+ return new BoolConstant ((bool) v, Location.Null);
else if (t == TypeManager.decimal_type)
- return new DecimalConstant ((decimal) v);
+ return new DecimalConstant ((decimal) v, Location.Null);
else if (TypeManager.IsEnumType (t)){
Type real_type = TypeManager.TypeToCoreType (v.GetType ());
if (real_type == t)
return new EnumConstant (e, t);
} else if (v == null && !TypeManager.IsValueType (t))
- return NullLiteral.Null;
+ return new NullLiteral (Location.Null);
else
throw new Exception ("Unknown type for constant (" + t +
"), details: " + v);
/// Returns a fully formed expression after a MemberLookup
/// </summary>
///
- // TODO: This can be heavily cached
- public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
+ public static Expression ExprClassFromMemberInfo (Type containerType, MemberInfo mi, Location loc)
{
if (mi is EventInfo)
return new EventExpr ((EventInfo) mi, loc);
else if (mi is FieldInfo)
return new FieldExpr ((FieldInfo) mi, loc);
else if (mi is PropertyInfo)
- return new PropertyExpr (ec, (PropertyInfo) mi, loc);
+ return new PropertyExpr (containerType, (PropertyInfo) mi, loc);
else if (mi is Type){
return new TypeExpression ((System.Type) mi, loc);
}
// FIXME: Potential optimization, have a static ArrayList
//
- public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
+ public static Expression MemberLookup (Type container_type, Type queried_type, string name,
MemberTypes mt, BindingFlags bf, Location loc)
{
- return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
+ return MemberLookup (container_type, null, queried_type, name, mt, bf, loc);
}
//
// `qualifier_type' or null to lookup members in the current class.
//
- public static Expression MemberLookup (EmitContext ec, Type container_type,
+ public static Expression MemberLookup (Type container_type,
Type qualifier_type, Type queried_type,
string name, MemberTypes mt,
BindingFlags bf, Location loc)
if (count > 1)
return null;
- return ExprClassFromMemberInfo (ec, mi [0], loc);
+ return ExprClassFromMemberInfo (container_type, mi [0], loc);
}
public const MemberTypes AllMemberTypes =
BindingFlags.Static |
BindingFlags.Instance;
- public static Expression MemberLookup (EmitContext ec, Type queried_type,
+ public static Expression MemberLookup (Type container_type, Type queried_type,
string name, Location loc)
{
- return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+ return MemberLookup (container_type, null, queried_type, name,
AllMemberTypes, AllBindingFlags, loc);
}
- public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
+ public static Expression MemberLookup (Type container_type, Type qualifier_type,
Type queried_type, string name, Location loc)
{
- return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
+ return MemberLookup (container_type, qualifier_type, queried_type,
name, AllMemberTypes, AllBindingFlags, loc);
}
public static Expression MethodLookup (EmitContext ec, Type queried_type,
string name, Location loc)
{
- return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
+ return MemberLookup (ec.ContainerType, null, queried_type, name,
MemberTypes.Method, AllBindingFlags, loc);
}
int errors = Report.Errors;
- e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
+ e = MemberLookup (ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
if (e == null && errors == Report.Errors)
// No errors were reported by MemberLookup, but there was an error.
- MemberLookupFailed (ec, qualifier_type, queried_type, name, null, true, loc);
+ MemberLookupFailed (ec.ContainerType, qualifier_type, queried_type, name, null, true, loc);
return e;
}
- public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
+ public static void MemberLookupFailed (Type container_type, Type qualifier_type,
Type queried_type, string name,
string class_name, bool complain_if_none_found,
Location loc)
if (qualifier_type == null) {
Report.Error (38, loc, "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
TypeManager.CSharpName (m.DeclaringType),
- TypeManager.CSharpName (ec.ContainerType));
+ TypeManager.CSharpName (container_type));
- } else if (qualifier_type != ec.ContainerType &&
- TypeManager.IsNestedFamilyAccessible (ec.ContainerType, declaring_type)) {
+ } else if (qualifier_type != container_type &&
+ TypeManager.IsNestedFamilyAccessible (container_type, declaring_type)) {
// Although a derived class can access protected members of
// its base class it cannot do so through an instance of the
// base class (CS1540). If the qualifier_type is a base of the
// ec.ContainerType and the lookup succeeds with the latter one,
// then we are in this situation.
- Report.Error (1540, loc,
- "Cannot access protected member `{0}' via a qualifier of type `{1}';"
- + " the qualifier must be of type `{2}' (or derived from it)",
- TypeManager.GetFullNameSignature (m),
- TypeManager.CSharpName (qualifier_type),
- TypeManager.CSharpName (ec.ContainerType));
+ Error_CannotAccessProtected (loc, m, qualifier_type, container_type);
} else {
ErrorIsInaccesible (loc, TypeManager.GetFullNameSignature (m));
}
BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null);
if (name == ".ctor" && ml.Count == 0)
{
- Report.Error (143, loc, String.Format ("The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type)));
+ Report.Error (143, loc, "The type `{0}' has no constructors defined", TypeManager.CSharpName (queried_type));
return;
}
//
// If no implicit conversion to bool exists, try using `operator true'
//
- Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
- if (operator_true == null){
- Report.Error (31, loc, "Can not convert the expression to a boolean");
+ converted = Expression.GetOperatorTrue (ec, e, loc);
+ if (converted == null){
+ e.Error_ValueCannotBeConverted (loc, TypeManager.bool_type, false);
return null;
}
- return operator_true;
+ return converted;
}
- string ExprClassName ()
- {
- switch (eclass){
- case ExprClass.Invalid:
- return "Invalid";
- case ExprClass.Value:
- return "value";
- case ExprClass.Variable:
- return "variable";
- case ExprClass.Namespace:
- return "namespace";
- case ExprClass.Type:
- return "type";
- case ExprClass.MethodGroup:
- return "method group";
- case ExprClass.PropertyAccess:
- return "property access";
- case ExprClass.EventAccess:
- return "event access";
- case ExprClass.IndexerAccess:
- return "indexer access";
- case ExprClass.Nothing:
- return "null";
- }
- throw new Exception ("Should not happen");
+ public virtual string ExprClassName
+ {
+ get {
+ switch (eclass){
+ case ExprClass.Invalid:
+ return "Invalid";
+ case ExprClass.Value:
+ return "value";
+ case ExprClass.Variable:
+ return "variable";
+ case ExprClass.Namespace:
+ return "namespace";
+ case ExprClass.Type:
+ return "type";
+ case ExprClass.MethodGroup:
+ return "method group";
+ case ExprClass.PropertyAccess:
+ return "property access";
+ case ExprClass.EventAccess:
+ return "event access";
+ case ExprClass.IndexerAccess:
+ return "indexer access";
+ case ExprClass.Nothing:
+ return "null";
+ }
+ throw new Exception ("Should not happen");
+ }
}
/// <summary>
/// Reports that we were expecting `expr' to be of class `expected'
/// </summary>
- public void Error_UnexpectedKind (EmitContext ec, string expected, Location loc)
+ public void Error_UnexpectedKind (DeclSpace ds, string expected, Location loc)
{
- Error_UnexpectedKind (ec, expected, ExprClassName (), loc);
+ Error_UnexpectedKind (ds, expected, ExprClassName, loc);
}
- public void Error_UnexpectedKind (EmitContext ec, string expected, string was, Location loc)
+ public void Error_UnexpectedKind (DeclSpace ds, string expected, string was, Location loc)
{
string name = GetSignatureForError ();
- if (ec != null)
- name = ec.DeclSpace.GetSignatureForError () + '.' + name;
+ if (ds != null)
+ name = ds.GetSignatureForError () + '.' + name;
Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected",
name, was, expected);
}
Report.Error (119, loc,
- "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName (), sb);
+ "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
}
public static void UnsafeError (Location loc)
if (target == null){
target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
if (target == null)
- Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
+ source.Error_ValueCannotBeConverted (loc, TypeManager.int32_type, false);
}
}
}
///
/// </summary>
public class EmptyCast : Expression {
- protected Expression child;
-
- public Expression Child {
- get {
- return child;
- }
- }
+ protected readonly Expression child;
public EmptyCast (Expression child, Type return_type)
{
{
child.Emit (ec);
}
+
+ public override bool GetAttributableValue (Type valueType, out object value)
+ {
+ return child.GetAttributableValue (valueType, out value);
+ }
+
}
/// <summary>
/// This is a numeric cast to a Decimal
MethodInfo conversion_operator;
- public CastToDecimal (EmitContext ec, Expression child)
- : this (ec, child, false)
+ public CastToDecimal (Expression child)
+ : this (child, false)
{
}
- public CastToDecimal (EmitContext ec, Expression child, bool find_explicit)
+ public CastToDecimal (Expression child, bool find_explicit)
: base (child, TypeManager.decimal_type)
{
- conversion_operator = GetConversionOperator (ec, find_explicit);
+ conversion_operator = GetConversionOperator (find_explicit);
if (conversion_operator == null)
- Convert.Error_CannotImplicitConversion (loc, child.Type, type);
+ throw new InternalErrorException ("Outer conversion routine is out of sync");
}
// Returns the implicit operator that converts from
// 'child.Type' to System.Decimal.
- MethodInfo GetConversionOperator (EmitContext ec, bool find_explicit)
+ MethodInfo GetConversionOperator (bool find_explicit)
{
- string operator_name = "op_Implicit";
-
- if (find_explicit)
- operator_name = "op_Explicit";
+ string operator_name = find_explicit ? "op_Explicit" : "op_Implicit";
- MethodGroupExpr opers = Expression.MethodLookup (
- ec, type, operator_name, loc) as MethodGroupExpr;
+ MemberInfo [] mi = TypeManager.MemberLookup (type, type, type, MemberTypes.Method,
+ BindingFlags.Static | BindingFlags.Public, operator_name, null);
- if (opers == null)
- Convert.Error_CannotImplicitConversion (loc, child.Type, type);
-
- foreach (MethodInfo oper in opers.Methods) {
+ foreach (MethodInfo oper in mi) {
ParameterData pd = TypeManager.GetParameterData (oper);
if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
ig.Emit (OpCodes.Call, conversion_operator);
}
}
+
/// <summary>
/// This is an explicit numeric cast from a Decimal
/// </summary>
public class CastFromDecimal : EmptyCast
{
- MethodInfo conversion_operator;
- public CastFromDecimal (EmitContext ec, Expression child, Type return_type)
+ static IDictionary operators;
+
+ public CastFromDecimal (Expression child, Type return_type)
: base (child, return_type)
{
if (child.Type != TypeManager.decimal_type)
throw new InternalErrorException (
"The expected type is Decimal, instead it is " + child.Type.FullName);
-
- conversion_operator = GetConversionOperator (ec);
- if (conversion_operator == null)
- Convert.Error_CannotImplicitConversion (loc, child.Type, type);
}
// Returns the explicit operator that converts from an
// express of type System.Decimal to 'type'.
- MethodInfo GetConversionOperator (EmitContext ec)
- {
- MethodGroupExpr opers = Expression.MethodLookup (
- ec, child.Type, "op_Explicit", loc) as MethodGroupExpr;
-
- if (opers == null)
- Convert.Error_CannotImplicitConversion (loc, child.Type, type);
-
- foreach (MethodInfo oper in opers.Methods) {
- ParameterData pd = TypeManager.GetParameterData (oper);
-
- if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
- return oper;
+ public Expression Resolve ()
+ {
+ if (operators == null) {
+ MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type,
+ TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method,
+ BindingFlags.Static | BindingFlags.Public, "op_Explicit", null);
+
+ operators = new System.Collections.Specialized.HybridDictionary ();
+ foreach (MethodInfo oper in all_oper) {
+ ParameterData pd = TypeManager.GetParameterData (oper);
+ if (pd.ParameterType (0) == TypeManager.decimal_type)
+ operators.Add (oper.ReturnType, oper);
+ }
}
- return null;
+ return operators.Contains (type) ? this : null;
}
+
public override void Emit (EmitContext ec)
{
ILGenerator ig = ec.ig;
child.Emit (ec);
- ig.Emit (OpCodes.Call, conversion_operator);
+ ig.Emit (OpCodes.Call, (MethodInfo)operators [type]);
}
}
// a NullLiteral is still a Constant
//
public class NullCast : Constant {
- protected Expression child;
+ public Constant child;
- public NullCast (Expression child, Type return_type)
+ public NullCast (Constant child, Type return_type):
+ base (Location.Null)
{
eclass = child.eclass;
type = return_type;
public override bool IsDefaultValue {
get {
- throw new NotImplementedException ();
+ return true;
}
}
return false;
}
}
+
+ public override Constant Reduce (bool inCheckedContext, Type target_type)
+ {
+ if (type == target_type)
+ return child.Reduce (inCheckedContext, target_type);
+
+ return null;
+ }
+
}
public class EnumConstant : Constant {
public Constant Child;
- public EnumConstant (Constant child, Type enum_type)
+ public EnumConstant (Constant child, Type enum_type):
+ base (child.Location)
{
eclass = child.eclass;
this.Child = child;
Child.Emit (ec);
}
+ public override bool GetAttributableValue (Type valueType, out object value)
+ {
+ value = GetTypedValue ();
+ return true;
+ }
+
+ public override string GetSignatureForError()
+ {
+ return TypeManager.CSharpName (Type);
+ }
+
public override object GetValue ()
{
return Child.GetValue ();
return System.Enum.ToObject (type, Child.GetValue ());
}
- public override void Error_ValueCannotBeConverted (Location loc, Type t)
- {
- Convert.Error_CannotImplicitConversion (loc, Type, t);
- }
-
public override string AsString ()
{
return Child.AsString ();
}
}
+ public override Constant Reduce(bool inCheckedContext, Type target_type)
+ {
+ if (Child.Type == target_type)
+ return Child;
+
+ return Child.Reduce (inCheckedContext, target_type);
+ }
+
public override Constant ToType (Type type, Location loc)
{
if (Type == type) {
return this;
}
- if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, this, type)){
- Error_ValueCannotBeConverted (loc, type);
+ if (!Convert.ImplicitStandardConversionExists (this, type)){
+ Error_ValueCannotBeConverted (loc, type, false);
return null;
}
return this;
}
+ public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
+ {
+ if (right_side == EmptyExpression.LValueMemberAccess)
+ Report.Error (445, loc, "Cannot modify the result of an unboxing conversion");
+ return base.DoResolveLValue (ec, right_side);
+ }
+
public override void Emit (EmitContext ec)
{
Type t = type;
}
Mode mode;
- bool checked_state;
- public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
+ public ConvCast (Expression child, Type return_type, Mode m)
: base (child, return_type)
{
- checked_state = ec.CheckState;
mode = m;
}
base.Emit (ec);
- if (checked_state){
+ if (ec.CheckState){
switch (mode){
case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
{
return resolved_to != null && resolved_to.Type != null &&
resolved_to.Type.Name == Name &&
- (ec.DeclSpace.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
+ (ec.DeclContainer.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
}
public override Expression DoResolve (EmitContext ec)
return SimpleNameResolve (ec, null, intermediate);
}
- public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+ public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
int errors = Report.Errors;
- FullNamedExpression fne = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false);
+ FullNamedExpression fne = ec.DeclContainer.LookupType (Name, loc, /*ignore_cs0104=*/ false);
if (fne != null)
return fne;
if (silent || errors != Report.Errors)
return null;
- MemberCore mc = ec.DeclSpace.GetDefinition (Name);
+ MemberCore mc = ec.DeclContainer.GetDefinition (Name);
if (mc != null) {
- Error_UnexpectedKind (ec, "type", GetMemberType (mc), loc);
- } else {
- NamespaceEntry.Error_NamespaceNotFound (loc, Name);
+ Error_UnexpectedKind (ec.DeclContainer, "type", GetMemberType (mc), loc);
+ return null;
}
+ string ns = ec.DeclContainer.NamespaceEntry.NS.Name;
+ string fullname = (ns.Length > 0) ? ns + "." + Name : Name;
+ foreach (Assembly a in RootNamespace.Global.Assemblies) {
+ Type type = a.GetType (fullname);
+ if (type != null) {
+ Report.SymbolRelatedToPreviousError (type);
+ Expression.ErrorIsInaccesible (loc, fullname);
+ return null;
+ }
+ }
+
+ NamespaceEntry.Error_NamespaceNotFound (loc, Name);
return null;
}
return "indexer";
if (mc is FieldBase)
return "field";
+ if (mc is MethodCore)
+ return "method";
+ if (mc is EnumMember)
+ return "enum";
return "type";
}
// Stage 2: Lookup members
//
- DeclSpace lookup_ds = ec.DeclSpace;
+ DeclSpace lookup_ds = ec.DeclContainer;
Type almost_matched_type = null;
ArrayList almost_matched = null;
do {
if (lookup_ds.TypeBuilder == null)
break;
- e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
+ e = MemberLookup (ec.ContainerType, lookup_ds.TypeBuilder, Name, loc);
if (e != null)
break;
} while (lookup_ds != null);
if (e == null && ec.ContainerType != null)
- e = MemberLookup (ec, ec.ContainerType, Name, loc);
+ e = MemberLookup (ec.ContainerType, ec.ContainerType, Name, loc);
if (e == null) {
if (almost_matched == null && almostMatchedMembers.Count > 0) {
almost_matched_type = ec.ContainerType;
almost_matched = (ArrayList) almostMatchedMembers.Clone ();
}
- e = ResolveAsTypeStep (ec, false);
+ e = ResolveAsTypeStep (ec, true);
}
if (e == null) {
almostMatchedMembers = almost_matched;
if (almost_matched_type == null)
almost_matched_type = ec.ContainerType;
- MemberLookupFailed (ec, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclSpace.Name, true, loc);
+ MemberLookupFailed (ec.ContainerType, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclContainer.Name, true, loc);
return null;
}
if (!me.IsStatic &&
(!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
Error_ObjectRefRequired (ec, loc, me.GetSignatureForError ());
- return null;
+ return EmptyExpression.Null;
}
//
Error (103, "The name `" + Name +
"' does not exist in the class `" +
- ec.DeclSpace.Name + "'");
+ ec.DeclContainer.Name + "'");
}
public override string ToString ()
/// section 10.8.1 (Fully Qualified Names).
/// </summary>
public abstract class FullNamedExpression : Expression {
- public override FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+ public override FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
return this;
}
}
/// <summary>
- /// Fully resolved expression that evaluates to a type
+ /// Expression that evaluates to a type
/// </summary>
public abstract class TypeExpr : FullNamedExpression {
- override public FullNamedExpression ResolveAsTypeStep (EmitContext ec, bool silent)
+ override public FullNamedExpression ResolveAsTypeStep (IResolveContext ec, bool silent)
{
TypeExpr t = DoResolveAsTypeStep (ec);
if (t == null)
return true;
}
- public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
-
- public virtual Type ResolveType (EmitContext ec)
- {
- TypeExpr t = ResolveAsTypeTerminal (ec, false);
- if (t == null)
- return null;
-
- return t.Type;
- }
+ protected abstract TypeExpr DoResolveAsTypeStep (IResolveContext ec);
public abstract string Name {
get;
}
}
+ /// <summary>
+ /// Fully resolved Expression that already evaluated to a type
+ /// </summary>
public class TypeExpression : TypeExpr {
public TypeExpression (Type t, Location l)
{
loc = l;
}
- public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+ protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+ {
+ return this;
+ }
+
+ public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
{
return this;
}
/// by the parser to setup the core types. A TypeLookupExpression is always
/// classified as a type.
/// </summary>
- public class TypeLookupExpression : TypeExpr {
- string name;
+ public sealed class TypeLookupExpression : TypeExpr {
+ readonly string name;
public TypeLookupExpression (string name)
{
this.name = name;
+ eclass = ExprClass.Type;
}
- static readonly char [] dot_array = { '.' };
- public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+ public override TypeExpr ResolveAsTypeTerminal (IResolveContext ec, bool silent)
{
- if (type != null)
- return this;
+ // It's null for corlib compilation only
+ if (type == null)
+ return DoResolveAsTypeStep (ec);
+ return this;
+ }
+
+ static readonly char [] dot_array = { '.' };
+ protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
+ {
// If name is of the form `N.I', first lookup `N', then search a member `I' in it.
string rest = null;
string lookup_name = name;
lookup_name = name.Substring (0, pos);
}
- FullNamedExpression resolved = Namespace.Root.Lookup (ec.DeclSpace, lookup_name, Location.Null);
+ FullNamedExpression resolved = RootNamespace.Global.Lookup (ec.DeclContainer, lookup_name, Location.Null);
if (resolved != null && rest != null) {
// Now handle the rest of the the name.
Namespace ns = resolved as Namespace;
element = elements [i++];
lookup_name += "." + element;
- resolved = ns.Lookup (ec.DeclSpace, element, Location.Null);
+ resolved = ns.Lookup (ec.DeclContainer, element, Location.Null);
}
if (resolved != null && resolved is TypeExpr) {
Type t = ((TypeExpr) resolved).Type;
while (t != null) {
- if (!ec.DeclSpace.CheckAccessLevel (t)) {
+ if (!ec.DeclContainer.CheckAccessLevel (t)) {
resolved = null;
lookup_name = t.FullName;
break;
}
if (!(resolved is TypeExpr)) {
- resolved.Error_UnexpectedKind (ec, "type", loc);
+ resolved.Error_UnexpectedKind (ec.DeclContainer, "type", loc);
return null;
}
- type = ((TypeExpr) resolved).ResolveType (ec);
+ type = resolved.Type;
return this;
}
get { return texpr.FullName; }
}
- public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
+ protected override TypeExpr DoResolveAsTypeStep (IResolveContext ec)
{
- Type type = texpr.ResolveType (ec);
- if (type == null)
- return null;
-
- return new TypeExpression (type, loc);
+ return texpr;
}
public override bool CheckAccessLevel (DeclSpace ds)
"with an instance reference, qualify it with a type name instead", name);
}
-
// TODO: possible optimalization
// Cache resolved constant result in FieldBuilder <-> expression map
public virtual Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
if (left is TypeExpr) {
if (!IsStatic) {
- SimpleName.Error_ObjectRefRequired (ec, loc, Name);
+ SimpleName.Error_ObjectRefRequired (ec, loc, GetSignatureForError ());
return null;
}
if (InstanceExpression is IMemoryLocation) {
((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
} else {
- LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type);
+ LocalTemporary t = new LocalTemporary (InstanceExpression.Type);
InstanceExpression.Emit (ec);
t.Store (ec);
t.AddressOf (ec, AddressOp.Store);
public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
SimpleName original)
{
- if (FieldInfo.IsLiteral) {
+ Type t = FieldInfo.FieldType;
+
+ if (FieldInfo.IsLiteral || (FieldInfo.IsInitOnly && t == TypeManager.decimal_type)) {
IConstant ic = TypeManager.GetConstant (FieldInfo);
if (ic == null) {
- ic = new ExternalConstant (FieldInfo);
+ if (FieldInfo.IsLiteral) {
+ ic = new ExternalConstant (FieldInfo);
+ } else {
+ ic = ExternalConstant.CreateDecimal (FieldInfo);
+ if (ic == null) {
+ return base.ResolveMemberAccess (ec, left, loc, original);
+ }
+ }
TypeManager.RegisterConstant (FieldInfo, ic);
}
return null;
}
- if (ic.ResolveValue ())
- ic.CheckObsoleteness (loc);
+ if (ic.ResolveValue ()) {
+ if (!ec.IsInObsoleteScope)
+ ic.CheckObsoleteness (loc);
+ }
return ic.Value;
}
-
- bool is_emitted = FieldInfo is FieldBuilder;
- Type t = FieldInfo.FieldType;
-
- //
- // Decimal constants cannot be encoded in the constant blob, and thus are marked
- // as IsInitOnly ('readonly' in C# parlance). We get its value from the
- // DecimalConstantAttribute metadata.
- //
- //TODO: incorporare in GetContant otherwise we miss all error checks + obsoleteness check
- if (FieldInfo.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
- object[] attrs = FieldInfo.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
- if (attrs.Length == 1)
- return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
- }
if (t.IsPointer && !ec.InUnsafe) {
UnsafeError (loc);
}
override public Expression DoResolve (EmitContext ec)
+ {
+ return DoResolve (ec, false);
+ }
+
+ Expression DoResolve (EmitContext ec, bool lvalue_instance)
{
if (ec.InRefOutArgumentResolving && FieldInfo.IsInitOnly && !ec.IsConstructor && FieldInfo.FieldType.IsValueType) {
if (FieldInfo.FieldType is TypeBuilder) {
// Resolve the field's instance expression while flow analysis is turned
// off: when accessing a field "a.b", we must check whether the field
// "a.b" is initialized, not whether the whole struct "a" is initialized.
- InstanceExpression = InstanceExpression.Resolve (
- ec, ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis);
+
+ if (lvalue_instance) {
+ bool old_do_flow_analysis = ec.DoFlowAnalysis;
+ ec.DoFlowAnalysis = false;
+ InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
+ ec.DoFlowAnalysis = old_do_flow_analysis;
+ } else {
+ ResolveFlags rf = ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis;
+ InstanceExpression = InstanceExpression.Resolve (ec, rf);
+ }
+
if (InstanceExpression == null)
return null;
}
- if (!in_initializer) {
+ if (!in_initializer && !ec.IsFieldInitializer) {
ObsoleteAttribute oa;
FieldBase f = TypeManager.GetField (FieldInfo);
if (f != null) {
- f.CheckObsoleteness (loc);
+ if (!ec.IsInObsoleteScope)
+ f.CheckObsoleteness (loc);
// To be sure that type is external because we do not register generated fields
} else if (!(FieldInfo.DeclaringType is TypeBuilder)) {
return this;
}
- void Report_AssignToReadonly (bool is_instance)
+ void Report_AssignToReadonly (Expression right_side)
{
+ int code;
string msg;
-
- if (is_instance)
- msg = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)";
- else
+ bool need_error_sig = false;
+ if (right_side == EmptyExpression.LValueMemberAccess) {
+ if (IsStatic) {
+ code = 1650;
+ msg = "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)";
+ } else {
+ code = 1648;
+ msg = "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)";
+ }
+ need_error_sig = true;
+ } else if (IsStatic) {
+ code = 198;
msg = "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)";
+ } else {
+ code = 191;
+ msg = "A readonly field cannot be assigned to (except in a constructor or a variable initializer)";
+ }
- Report.Error (is_instance ? 191 : 198, loc, msg);
+ if (need_error_sig)
+ Report.Error (code, loc, msg, GetSignatureForError ());
+ else
+ Report.Error (code, loc, msg);
}
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
if ((var != null) && (var.VariableInfo != null))
var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
- Expression e = DoResolve (ec);
+ bool lvalue_instance = !FieldInfo.IsStatic && FieldInfo.DeclaringType.IsValueType;
+
+ Expression e = DoResolve (ec, lvalue_instance);
if (e == null)
return null;
- if (!FieldInfo.IsStatic && (InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation))) {
- Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
- InstanceExpression.GetSignatureForError ());
- return null;
- }
-
FieldBase fb = TypeManager.GetField (FieldInfo);
if (fb != null)
fb.SetAssigned ();
return this;
//
- // InitOnly fields can only be assigned in constructors
+ // InitOnly fields can only be assigned in constructors or initializers
//
+ if (ec.IsFieldInitializer)
+ return this;
+
if (ec.IsConstructor){
if (IsStatic && !ec.IsStatic)
- Report_AssignToReadonly (false);
+ Report_AssignToReadonly (right_side);
if (ec.ContainerType == FieldInfo.DeclaringType)
return this;
}
- Report_AssignToReadonly (!IsStatic);
+ Report_AssignToReadonly (right_side);
return null;
}
ILGenerator ig = ec.ig;
bool is_volatile = false;
- if (FieldInfo is FieldBuilder){
- FieldBase f = TypeManager.GetField (FieldInfo);
- if (f != null){
- if ((f.ModFlags & Modifiers.VOLATILE) != 0)
- is_volatile = true;
-
- f.SetMemberIsUsed ();
- }
- }
+ FieldBase f = TypeManager.GetField (FieldInfo);
+ if (f != null){
+ if ((f.ModFlags & Modifiers.VOLATILE) != 0)
+ is_volatile = true;
+
+ f.SetMemberIsUsed ();
+ }
if (FieldInfo.IsStatic){
if (is_volatile)
if (leave_copy) {
ec.ig.Emit (OpCodes.Dup);
if (!FieldInfo.IsStatic) {
- temp = new LocalTemporary (ec, this.Type);
+ temp = new LocalTemporary (this.Type);
temp.Store (ec);
}
}
prepared = prepare_for_load;
if (is_readonly && !ec.IsConstructor){
- Report_AssignToReadonly (!is_static);
+ Report_AssignToReadonly (source);
return;
}
if (leave_copy) {
ec.ig.Emit (OpCodes.Dup);
if (!FieldInfo.IsStatic) {
- temp = new LocalTemporary (ec, this.Type);
+ temp = new LocalTemporary (this.Type);
temp.Store (ec);
}
}
internal static PtrHashtable AccessorTable = new PtrHashtable ();
- public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
+ public PropertyExpr (Type containerType, PropertyInfo pi, Location l)
{
PropertyInfo = pi;
eclass = ExprClass.PropertyAccess;
type = TypeManager.TypeToCoreType (pi.PropertyType);
- ResolveAccessors (ec);
+ ResolveAccessors (containerType);
}
public override string Name {
void FindAccessors (Type invocation_type)
{
- BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
+ const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
// We also perform the permission checking here, as the PropertyInfo does not
// hold the information for the accessibility of its setter/getter
//
- void ResolveAccessors (EmitContext ec)
+ // TODO: can use TypeManager.GetProperty to boost performance
+ void ResolveAccessors (Type containerType)
{
- FindAccessors (ec.ContainerType);
+ FindAccessors (containerType);
if (getter != null) {
IMethodData md = TypeManager.GetMethod (getter);
}
}
- bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
+ bool InstanceResolve (EmitContext ec, bool lvalue_instance, bool must_do_cs1540_check)
{
if (is_static) {
InstanceExpression = null;
return false;
}
- InstanceExpression = InstanceExpression.DoResolve (ec);
+ if (lvalue_instance)
+ InstanceExpression = InstanceExpression.ResolveLValue (ec, EmptyExpression.LValueMemberAccess, loc);
+ else
+ InstanceExpression = InstanceExpression.DoResolve (ec);
if (InstanceExpression == null)
return false;
InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
- if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
- if ((InstanceExpression.Type != ec.ContainerType) &&
- ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
- Report.Error (1540, loc, "Cannot access protected member `" +
- PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
- "' via a qualifier of type `" +
- TypeManager.CSharpName (InstanceExpression.Type) +
- "'; the qualifier must be of type `" +
- TypeManager.CSharpName (ec.ContainerType) +
- "' (or derived from it)");
- return false;
- }
+ if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
+ InstanceExpression.Type != ec.ContainerType &&
+ ec.ContainerType.IsSubclassOf (PropertyInfo.DeclaringType) &&
+ !InstanceExpression.Type.IsSubclassOf (ec.ContainerType)) {
+ Error_CannotAccessProtected (loc, PropertyInfo, InstanceExpression.Type, ec.ContainerType);
+ return false;
}
return true;
return this;
if (getter != null){
- if (TypeManager.GetArgumentTypes (getter).Length != 0){
+ if (TypeManager.GetParameterData (getter).Count != 0){
Error_PropertyNotFound (getter, true);
return null;
}
return null;
}
- if (!InstanceResolve (ec, must_do_cs1540_check))
+ if (!InstanceResolve (ec, false, must_do_cs1540_check))
return null;
//
override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
{
+ if (right_side == EmptyExpression.OutAccess) {
+ Report.Error (206, loc, "A property or indexer `{0}' may not be passed as an out or ref parameter",
+ GetSignatureForError ());
+ return null;
+ }
+
+ if (right_side == EmptyExpression.LValueMemberAccess) {
+ Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
+ GetSignatureForError ());
+ return null;
+ }
+
if (setter == null){
//
// The following condition happens if the PropertyExpr was
//
if (getter == null)
return null;
-
- Report.Error (200, loc, " Property or indexer `{0}' cannot be assigned to (it is read only)",
- TypeManager.GetFullNameSignature (PropertyInfo));
+ Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)",
+ GetSignatureForError ());
return null;
}
- if (TypeManager.GetArgumentTypes (setter).Length != 1){
+ if (TypeManager.GetParameterData (setter).Count != 1){
Error_PropertyNotFound (setter, false);
return null;
}
return null;
}
- if (!InstanceResolve (ec, must_do_cs1540_check))
+ if (!InstanceResolve (ec, PropertyInfo.DeclaringType.IsValueType, must_do_cs1540_check))
return null;
//
return null;
}
- //
- // Check that we are not making changes to a temporary memory location
- //
- if (InstanceExpression != null && InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation)) {
- Report.Error (1612, loc, "Cannot modify the return value of `{0}' because it is not a variable",
- InstanceExpression.GetSignatureForError ());
- return null;
- }
-
return this;
}
public void Emit (EmitContext ec, bool leave_copy)
{
- if (!prepared)
- EmitInstance (ec, false);
-
//
// Special case: length of single dimension array property is turned into ldlen
//
// support invoking GetArrayRank, so test for that case first
//
if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
+ if (!prepared)
+ EmitInstance (ec, false);
ec.ig.Emit (OpCodes.Ldlen);
ec.ig.Emit (OpCodes.Conv_I4);
return;
}
}
- Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
+ Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, getter, null, loc, prepared, false);
- if (!leave_copy)
- return;
-
- ec.ig.Emit (OpCodes.Dup);
- if (!is_static) {
- temp = new LocalTemporary (ec, this.Type);
- temp.Store (ec);
+ if (leave_copy) {
+ ec.ig.Emit (OpCodes.Dup);
+ if (!is_static) {
+ temp = new LocalTemporary (this.Type);
+ temp.Store (ec);
+ }
}
}
//
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
{
+ Expression my_source = source;
+
prepared = prepare_for_load;
- EmitInstance (ec, prepare_for_load);
-
- source.Emit (ec);
- if (leave_copy) {
- ec.ig.Emit (OpCodes.Dup);
+ if (prepared) {
+ source.Emit (ec);
+ if (leave_copy) {
+ ec.ig.Emit (OpCodes.Dup);
+ if (!is_static) {
+ temp = new LocalTemporary (this.Type);
+ temp.Store (ec);
+ }
+ }
+ } else if (leave_copy) {
+ source.Emit (ec);
if (!is_static) {
- temp = new LocalTemporary (ec, this.Type);
+ temp = new LocalTemporary (this.Type);
temp.Store (ec);
}
+ my_source = temp;
}
ArrayList args = new ArrayList (1);
- args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
+ args.Add (new Argument (my_source, Argument.AType.Expression));
- Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
+ Invocation.EmitCall (ec, IsBase, IsStatic, InstanceExpression, setter, args, loc, false, prepared);
if (temp != null)
temp.Emit (ec);
MemberInfo mi = TypeManager.GetPrivateFieldOfEvent (EventInfo);
if (mi != null) {
- MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec, mi, loc);
+ MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec.ContainerType, mi, loc);
if (ml == null) {
Report.Error (-200, loc, "Internal error!!");
// This is using the same mechanism as the CS1540 check in PropertyExpr.
// However, in the Event case, we reported a CS0122 instead.
//
- if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
- if ((InstanceExpression.Type != ec.ContainerType) &&
- ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
- ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
- return false;
- }
+ if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null &&
+ InstanceExpression.Type != ec.ContainerType &&
+ ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
+ ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo));
+ return false;
}
return true;
ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);
}
}
+
+
+ public class TemporaryVariable : Expression, IMemoryLocation
+ {
+ LocalInfo li;
+
+ public TemporaryVariable (Type type, Location loc)
+ {
+ this.type = type;
+ this.loc = loc;
+ eclass = ExprClass.Value;
+ }
+
+ public override Expression DoResolve (EmitContext ec)
+ {
+ if (li != null)
+ return this;
+
+ TypeExpr te = new TypeExpression (type, loc);
+ li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
+ if (!li.Resolve (ec))
+ return null;
+
+ AnonymousContainer am = ec.CurrentAnonymousMethod;
+ if ((am != null) && am.IsIterator)
+ ec.CaptureVariable (li);
+
+ return this;
+ }
+
+ public override void Emit (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+
+ if (li.FieldBuilder != null) {
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldfld, li.FieldBuilder);
+ } else {
+ ig.Emit (OpCodes.Ldloc, li.LocalBuilder);
+ }
+ }
+
+ public void EmitLoadAddress (EmitContext ec)
+ {
+ ILGenerator ig = ec.ig;
+
+ if (li.FieldBuilder != null) {
+ ig.Emit (OpCodes.Ldarg_0);
+ ig.Emit (OpCodes.Ldflda, li.FieldBuilder);
+ } else {
+ ig.Emit (OpCodes.Ldloca, li.LocalBuilder);
+ }
+ }
+
+ public void Store (EmitContext ec, Expression right_side)
+ {
+ if (li.FieldBuilder != null)
+ ec.ig.Emit (OpCodes.Ldarg_0);
+
+ right_side.Emit (ec);
+ if (li.FieldBuilder != null) {
+ ec.ig.Emit (OpCodes.Stfld, li.FieldBuilder);
+ } else {
+ ec.ig.Emit (OpCodes.Stloc, li.LocalBuilder);
+ }
+ }
+
+ public void EmitThis (EmitContext ec)
+ {
+ if (li.FieldBuilder != null) {
+ ec.ig.Emit (OpCodes.Ldarg_0);
+ }
+ }
+
+ public void EmitStore (ILGenerator ig)
+ {
+ if (li.FieldBuilder != null)
+ ig.Emit (OpCodes.Stfld, li.FieldBuilder);
+ else
+ ig.Emit (OpCodes.Stloc, li.LocalBuilder);
+ }
+
+ public void AddressOf (EmitContext ec, AddressOp mode)
+ {
+ EmitLoadAddress (ec);
+ }
+ }
+
}