2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) 2001, 2002, 2003 Ximian, Inc.
11 namespace Mono.CSharp {
13 using System.Collections;
14 using System.Diagnostics;
15 using System.Reflection;
16 using System.Reflection.Emit;
20 /// The ExprClass class contains the is used to pass the
21 /// classification of an expression (value, variable, namespace,
22 /// type, method group, property access, event access, indexer access,
25 public enum ExprClass : byte {
40 /// This is used to tell Resolve in which types of expressions we're
44 public enum ResolveFlags {
45 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
48 // Returns a type expression.
51 // Returns a method group.
54 // Mask of all the expression class flags.
57 // Disable control flow analysis while resolving the expression.
58 // This is used when resolving the instance expression of a field expression.
59 DisableFlowAnalysis = 8,
61 // Set if this is resolving the first part of a MemberAccess.
66 // This is just as a hint to AddressOf of what will be done with the
69 public enum AddressOp {
76 /// This interface is implemented by variables
78 public interface IMemoryLocation {
80 /// The AddressOf method should generate code that loads
81 /// the address of the object and leaves it on the stack.
83 /// The `mode' argument is used to notify the expression
84 /// of whether this will be used to read from the address or
85 /// write to the address.
87 /// This is just a hint that can be used to provide good error
88 /// reporting, and should have no other side effects.
90 void AddressOf (EmitContext ec, AddressOp mode);
94 /// We are either a namespace or a type.
95 /// If we're a type, `IsType' is true and we may use `Type' to get
96 /// a TypeExpr representing that type.
98 public interface IAlias {
107 TypeExpr ResolveAsType (EmitContext ec);
111 /// This interface is implemented by variables
113 public interface IVariable {
114 VariableInfo VariableInfo {
118 bool VerifyFixed (bool is_expression);
122 /// Base class for expressions
124 public abstract class Expression {
125 public ExprClass eclass;
127 protected Location loc;
139 public Location Location {
146 /// Utility wrapper routine for Error, just to beautify the code
148 public void Error (int error, string s)
150 if (!Location.IsNull (loc))
151 Report.Error (error, loc, s);
153 Report.Error (error, s);
157 /// Utility wrapper routine for Warning, just to beautify the code
159 public void Warning (int code, string format, params object[] args)
161 Report.Warning (code, loc, format, args);
164 // Not nice but we have broken hierarchy
165 public virtual void CheckMarshallByRefAccess (Type container) {}
168 /// Tests presence of ObsoleteAttribute and report proper error
170 protected void CheckObsoleteAttribute (Type type)
172 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
173 if (obsolete_attr == null)
176 AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
179 public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check)
181 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
183 must_do_cs1540_check = false; // by default we do not check for this
186 // If only accessible to the current class or children
188 if (ma == MethodAttributes.Private)
189 return invocation_type == mi.DeclaringType ||
190 TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType);
192 if (mi.DeclaringType.Assembly == invocation_type.Assembly) {
193 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
196 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
200 // Family and FamANDAssem require that we derive.
201 // FamORAssem requires that we derive if in different assemblies.
202 if (ma == MethodAttributes.Family ||
203 ma == MethodAttributes.FamANDAssem ||
204 ma == MethodAttributes.FamORAssem) {
205 if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
208 if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
209 must_do_cs1540_check = true;
218 /// Performs semantic analysis on the Expression
222 /// The Resolve method is invoked to perform the semantic analysis
225 /// The return value is an expression (it can be the
226 /// same expression in some cases) or a new
227 /// expression that better represents this node.
229 /// For example, optimizations of Unary (LiteralInt)
230 /// would return a new LiteralInt with a negated
233 /// If there is an error during semantic analysis,
234 /// then an error should be reported (using Report)
235 /// and a null value should be returned.
237 /// There are two side effects expected from calling
238 /// Resolve(): the the field variable "eclass" should
239 /// be set to any value of the enumeration
240 /// `ExprClass' and the type variable should be set
241 /// to a valid type (this is the type of the
244 public abstract Expression DoResolve (EmitContext ec);
246 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
252 // This is used if the expression should be resolved as a type or namespace name.
253 // the default implementation fails.
255 public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec)
261 // This is used to resolve the expression as a type, a null
262 // value will be returned if the expression is not a type
265 public TypeExpr ResolveAsTypeTerminal (EmitContext ec, bool silent)
267 int errors = Report.Errors;
269 FullNamedExpression fne = ResolveAsTypeStep (ec);
272 if (!silent && errors == Report.Errors)
273 Report.Error (246, Location, "Cannot find type '{0}'", ToString ());
277 if (fne.eclass != ExprClass.Type) {
278 if (!silent && errors == Report.Errors)
279 Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected",
280 fne.FullName, fne.ExprClassName ());
284 TypeExpr te = fne as TypeExpr;
286 if (!te.CheckAccessLevel (ec.DeclSpace)) {
287 Report.Error (122, Location, "'{0}' is inaccessible due to its protection level", te.Name);
294 ResolveFlags ExprClassToResolveFlags ()
298 case ExprClass.Namespace:
299 return ResolveFlags.Type;
301 case ExprClass.MethodGroup:
302 return ResolveFlags.MethodGroup;
304 case ExprClass.Value:
305 case ExprClass.Variable:
306 case ExprClass.PropertyAccess:
307 case ExprClass.EventAccess:
308 case ExprClass.IndexerAccess:
309 return ResolveFlags.VariableOrValue;
312 throw new Exception ("Expression " + GetType () +
313 " ExprClass is Invalid after resolve");
319 /// Resolves an expression and performs semantic analysis on it.
323 /// Currently Resolve wraps DoResolve to perform sanity
324 /// checking and assertion checking on what we expect from Resolve.
326 public Expression Resolve (EmitContext ec, ResolveFlags flags)
328 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
329 return ResolveAsTypeStep (ec);
331 bool old_do_flow_analysis = ec.DoFlowAnalysis;
332 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
333 ec.DoFlowAnalysis = false;
336 bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
337 if (this is SimpleName)
338 e = ((SimpleName) this).DoResolve (ec, intermediate);
343 ec.DoFlowAnalysis = old_do_flow_analysis;
348 if ((flags & e.ExprClassToResolveFlags ()) == 0) {
349 e.Error_UnexpectedKind (flags, loc);
353 if (e.type == null && !(e is Namespace)) {
354 throw new Exception (
355 "Expression " + e.GetType () +
356 " did not set its type after Resolve\n" +
357 "called from: " + this.GetType ());
364 /// Resolves an expression and performs semantic analysis on it.
366 public Expression Resolve (EmitContext ec)
368 Expression e = Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
370 if (e != null && e.eclass == ExprClass.MethodGroup && RootContext.Version == LanguageVersion.ISO_1) {
371 ((MethodGroupExpr) e).ReportUsageError ();
378 /// Resolves an expression for LValue assignment
382 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
383 /// checking and assertion checking on what we expect from Resolve
385 public Expression ResolveLValue (EmitContext ec, Expression right_side, Location loc)
387 int errors = Report.Errors;
388 Expression e = DoResolveLValue (ec, right_side);
391 if (errors == Report.Errors)
392 Report.Error (131, loc, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer");
397 if (e.eclass == ExprClass.Invalid)
398 throw new Exception ("Expression " + e +
399 " ExprClass is Invalid after resolve");
401 if (e.eclass == ExprClass.MethodGroup) {
402 ((MethodGroupExpr) e).ReportUsageError ();
407 throw new Exception ("Expression " + e +
408 " did not set its type after Resolve");
415 /// Emits the code for the expression
419 /// The Emit method is invoked to generate the code
420 /// for the expression.
422 public abstract void Emit (EmitContext ec);
424 public virtual void EmitBranchable (EmitContext ec, Label target, bool onTrue)
427 ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
431 /// Protected constructor. Only derivate types should
432 /// be able to be created
435 protected Expression ()
437 eclass = ExprClass.Invalid;
442 /// Returns a literalized version of a literal FieldInfo
446 /// The possible return values are:
447 /// IntConstant, UIntConstant
448 /// LongLiteral, ULongConstant
449 /// FloatConstant, DoubleConstant
452 /// The value returned is already resolved.
454 public static Constant Constantify (object v, Type t)
456 if (t == TypeManager.int32_type)
457 return new IntConstant ((int) v);
458 else if (t == TypeManager.uint32_type)
459 return new UIntConstant ((uint) v);
460 else if (t == TypeManager.int64_type)
461 return new LongConstant ((long) v);
462 else if (t == TypeManager.uint64_type)
463 return new ULongConstant ((ulong) v);
464 else if (t == TypeManager.float_type)
465 return new FloatConstant ((float) v);
466 else if (t == TypeManager.double_type)
467 return new DoubleConstant ((double) v);
468 else if (t == TypeManager.string_type)
469 return new StringConstant ((string) v);
470 else if (t == TypeManager.short_type)
471 return new ShortConstant ((short)v);
472 else if (t == TypeManager.ushort_type)
473 return new UShortConstant ((ushort)v);
474 else if (t == TypeManager.sbyte_type)
475 return new SByteConstant (((sbyte)v));
476 else if (t == TypeManager.byte_type)
477 return new ByteConstant ((byte)v);
478 else if (t == TypeManager.char_type)
479 return new CharConstant ((char)v);
480 else if (t == TypeManager.bool_type)
481 return new BoolConstant ((bool) v);
482 else if (t == TypeManager.decimal_type)
483 return new DecimalConstant ((decimal) v);
484 else if (TypeManager.IsEnumType (t)){
485 Type real_type = TypeManager.TypeToCoreType (v.GetType ());
487 real_type = System.Enum.GetUnderlyingType (real_type);
489 Constant e = Constantify (v, real_type);
491 return new EnumConstant (e, t);
492 } else if (v == null && !TypeManager.IsValueType (t))
493 return NullLiteral.Null;
495 throw new Exception ("Unknown type for constant (" + t +
500 /// Returns a fully formed expression after a MemberLookup
502 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
505 return new EventExpr ((EventInfo) mi, loc);
506 else if (mi is FieldInfo)
507 return new FieldExpr ((FieldInfo) mi, loc);
508 else if (mi is PropertyInfo)
509 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
510 else if (mi is Type){
511 return new TypeExpression ((System.Type) mi, loc);
517 protected static ArrayList almostMatchedMembers = new ArrayList (4);
520 // FIXME: Probably implement a cache for (t,name,current_access_set)?
522 // This code could use some optimizations, but we need to do some
523 // measurements. For example, we could use a delegate to `flag' when
524 // something can not any longer be a method-group (because it is something
528 // If the return value is an Array, then it is an array of
531 // If the return value is an MemberInfo, it is anything, but a Method
535 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
536 // the arguments here and have MemberLookup return only the methods that
537 // match the argument count/type, unlike we are doing now (we delay this
540 // This is so we can catch correctly attempts to invoke instance methods
541 // from a static body (scan for error 120 in ResolveSimpleName).
544 // FIXME: Potential optimization, have a static ArrayList
547 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
548 MemberTypes mt, BindingFlags bf, Location loc)
550 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
554 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
555 // `qualifier_type' or null to lookup members in the current class.
558 public static Expression MemberLookup (EmitContext ec, Type container_type,
559 Type qualifier_type, Type queried_type,
560 string name, MemberTypes mt,
561 BindingFlags bf, Location loc)
563 almostMatchedMembers.Clear ();
565 MemberInfo [] mi = TypeManager.MemberLookup (container_type, qualifier_type,
566 queried_type, mt, bf, name, almostMatchedMembers);
571 int count = mi.Length;
573 if (mi [0] is MethodBase)
574 return new MethodGroupExpr (mi, loc);
579 return ExprClassFromMemberInfo (ec, mi [0], loc);
582 public const MemberTypes AllMemberTypes =
583 MemberTypes.Constructor |
587 MemberTypes.NestedType |
588 MemberTypes.Property;
590 public const BindingFlags AllBindingFlags =
591 BindingFlags.Public |
592 BindingFlags.Static |
593 BindingFlags.Instance;
595 public static Expression MemberLookup (EmitContext ec, Type queried_type,
596 string name, Location loc)
598 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
599 AllMemberTypes, AllBindingFlags, loc);
602 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
603 Type queried_type, string name, Location loc)
605 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
606 name, AllMemberTypes, AllBindingFlags, loc);
609 public static Expression MethodLookup (EmitContext ec, Type queried_type,
610 string name, Location loc)
612 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
613 MemberTypes.Method, AllBindingFlags, loc);
617 /// This is a wrapper for MemberLookup that is not used to "probe", but
618 /// to find a final definition. If the final definition is not found, we
619 /// look for private members and display a useful debugging message if we
622 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
623 Type queried_type, string name, Location loc)
625 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
626 AllMemberTypes, AllBindingFlags, loc);
629 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
630 Type queried_type, string name,
631 MemberTypes mt, BindingFlags bf,
636 int errors = Report.Errors;
638 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type, name, mt, bf, loc);
640 if (e == null && errors == Report.Errors)
641 // No errors were reported by MemberLookup, but there was an error.
642 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, true, loc);
647 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
648 Type queried_type, string name,
649 string class_name, bool complain_if_none_found,
652 if (almostMatchedMembers.Count != 0) {
653 for (int i = 0; i < almostMatchedMembers.Count; ++i) {
654 MemberInfo m = (MemberInfo) almostMatchedMembers [i];
655 for (int j = 0; j < i; ++j) {
656 if (m == almostMatchedMembers [j]) {
664 Type declaring_type = m.DeclaringType;
666 Report.SymbolRelatedToPreviousError (m);
667 if (qualifier_type == null) {
668 Report.Error (38, loc,
669 "Cannot access non-static member `{0}' via nested type `{1}'",
670 TypeManager.GetFullNameSignature (m),
671 TypeManager.CSharpName (ec.ContainerType));
673 } else if (qualifier_type != ec.ContainerType &&
674 TypeManager.IsNestedFamilyAccessible (ec.ContainerType, declaring_type)) {
675 // Although a derived class can access protected members of
676 // its base class it cannot do so through an instance of the
677 // base class (CS1540). If the qualifier_type is a base of the
678 // ec.ContainerType and the lookup succeeds with the latter one,
679 // then we are in this situation.
680 Report.Error (1540, loc,
681 "Cannot access protected member `{0}' via a qualifier of type `{1}';"
682 + " the qualifier must be of type `{2}' (or derived from it)",
683 TypeManager.GetFullNameSignature (m),
684 TypeManager.CSharpName (qualifier_type),
685 TypeManager.CSharpName (ec.ContainerType));
687 Report.Error (122, loc,
688 "'{0}' is inaccessible due to its protection level",
689 TypeManager.GetFullNameSignature (m));
692 almostMatchedMembers.Clear ();
696 object lookup = TypeManager.MemberLookup (queried_type, null, queried_type,
697 AllMemberTypes, AllBindingFlags |
698 BindingFlags.NonPublic, name, null);
700 if (lookup == null) {
701 if (!complain_if_none_found)
704 if (class_name != null)
705 Report.Error (103, loc, "The name `" + name + "' could not be " +
706 "found in `" + class_name + "'");
709 117, loc, "`" + queried_type + "' does not contain a " +
710 "definition for `" + name + "'");
714 if (name == ".ctor" && TypeManager.FindMembers (qualifier_type, MemberTypes.Constructor,
715 BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null).Count == 0)
717 Report.Error (143, loc, String.Format ("The type '{0}' has no constructors defined", TypeManager.CSharpName (queried_type)));
721 if (qualifier_type != null) {
722 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name);
724 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name);
729 /// Returns an expression that can be used to invoke operator true
730 /// on the expression if it exists.
732 static public StaticCallExpr GetOperatorTrue (EmitContext ec, Expression e, Location loc)
734 return GetOperatorTrueOrFalse (ec, e, true, loc);
738 /// Returns an expression that can be used to invoke operator false
739 /// on the expression if it exists.
741 static public StaticCallExpr GetOperatorFalse (EmitContext ec, Expression e, Location loc)
743 return GetOperatorTrueOrFalse (ec, e, false, loc);
746 static StaticCallExpr GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
749 Expression operator_group;
751 operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
752 if (operator_group == null)
755 ArrayList arguments = new ArrayList ();
756 arguments.Add (new Argument (e, Argument.AType.Expression));
757 method = Invocation.OverloadResolve (
758 ec, (MethodGroupExpr) operator_group, arguments, false, loc);
763 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
767 /// Resolves the expression `e' into a boolean expression: either through
768 /// an implicit conversion, or through an `operator true' invocation
770 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
776 if (e.Type == TypeManager.bool_type)
779 Expression converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, Location.Null);
781 if (converted != null)
785 // If no implicit conversion to bool exists, try using `operator true'
787 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
788 if (operator_true == null){
789 Report.Error (31, loc, "Can not convert the expression to a boolean");
792 return operator_true;
795 public string ExprClassName ()
798 case ExprClass.Invalid:
800 case ExprClass.Value:
802 case ExprClass.Variable:
804 case ExprClass.Namespace:
808 case ExprClass.MethodGroup:
809 return "method group";
810 case ExprClass.PropertyAccess:
811 return "property access";
812 case ExprClass.EventAccess:
813 return "event access";
814 case ExprClass.IndexerAccess:
815 return "indexer access";
816 case ExprClass.Nothing:
819 throw new Exception ("Should not happen");
823 /// Reports that we were expecting `expr' to be of class `expected'
825 public void Error_UnexpectedKind (string expected, Location loc)
827 Report.Error (118, loc,
828 "Expression denotes a '{0}', where a '{1}' was expected", ExprClassName (), expected);
831 public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
833 string [] valid = new string [4];
836 if ((flags & ResolveFlags.VariableOrValue) != 0) {
837 valid [count++] = "variable";
838 valid [count++] = "value";
841 if ((flags & ResolveFlags.Type) != 0)
842 valid [count++] = "type";
844 if ((flags & ResolveFlags.MethodGroup) != 0)
845 valid [count++] = "method group";
848 valid [count++] = "unknown";
850 StringBuilder sb = new StringBuilder (valid [0]);
851 for (int i = 1; i < count - 1; i++) {
853 sb.Append (valid [i]);
856 sb.Append ("' or '");
857 sb.Append (valid [count - 1]);
860 Report.Error (119, loc,
861 "Expression denotes a '{0}', where a '{1}' was expected", ExprClassName (), sb);
864 static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
866 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
867 TypeManager.CSharpName (t));
870 public static void UnsafeError (Location loc)
872 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
876 /// Converts the IntConstant, UIntConstant, LongConstant or
877 /// ULongConstant into the integral target_type. Notice
878 /// that we do not return an `Expression' we do return
879 /// a boxed integral type.
881 /// FIXME: Since I added the new constants, we need to
882 /// also support conversions from CharConstant, ByteConstant,
883 /// SByteConstant, UShortConstant, ShortConstant
885 /// This is used by the switch statement, so the domain
886 /// of work is restricted to the literals above, and the
887 /// targets are int32, uint32, char, byte, sbyte, ushort,
888 /// short, uint64 and int64
890 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
892 if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, c, target_type)){
893 Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
899 if (c.Type == target_type)
900 return ((Constant) c).GetValue ();
903 // Make into one of the literals we handle, we dont really care
904 // about this value as we will just return a few limited types
906 if (c is EnumConstant)
907 c = ((EnumConstant)c).WidenToCompilerConstant ();
909 if (c is IntConstant){
910 int v = ((IntConstant) c).Value;
912 if (target_type == TypeManager.uint32_type){
915 } else if (target_type == TypeManager.char_type){
916 if (v >= Char.MinValue && v <= Char.MaxValue)
918 } else if (target_type == TypeManager.byte_type){
919 if (v >= Byte.MinValue && v <= Byte.MaxValue)
921 } else if (target_type == TypeManager.sbyte_type){
922 if (v >= SByte.MinValue && v <= SByte.MaxValue)
924 } else if (target_type == TypeManager.short_type){
925 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
927 } else if (target_type == TypeManager.ushort_type){
928 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
930 } else if (target_type == TypeManager.int64_type)
932 else if (target_type == TypeManager.uint64_type){
938 } else if (c is UIntConstant){
939 uint v = ((UIntConstant) c).Value;
941 if (target_type == TypeManager.int32_type){
942 if (v <= Int32.MaxValue)
944 } else if (target_type == TypeManager.char_type){
945 if (v >= Char.MinValue && v <= Char.MaxValue)
947 } else if (target_type == TypeManager.byte_type){
948 if (v <= Byte.MaxValue)
950 } else if (target_type == TypeManager.sbyte_type){
951 if (v <= SByte.MaxValue)
953 } else if (target_type == TypeManager.short_type){
954 if (v <= UInt16.MaxValue)
956 } else if (target_type == TypeManager.ushort_type){
957 if (v <= UInt16.MaxValue)
959 } else if (target_type == TypeManager.int64_type)
961 else if (target_type == TypeManager.uint64_type)
964 } else if (c is LongConstant){
965 long v = ((LongConstant) c).Value;
967 if (target_type == TypeManager.int32_type){
968 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
970 } else if (target_type == TypeManager.uint32_type){
971 if (v >= 0 && v <= UInt32.MaxValue)
973 } else if (target_type == TypeManager.char_type){
974 if (v >= Char.MinValue && v <= Char.MaxValue)
976 } else if (target_type == TypeManager.byte_type){
977 if (v >= Byte.MinValue && v <= Byte.MaxValue)
979 } else if (target_type == TypeManager.sbyte_type){
980 if (v >= SByte.MinValue && v <= SByte.MaxValue)
982 } else if (target_type == TypeManager.short_type){
983 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
985 } else if (target_type == TypeManager.ushort_type){
986 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
988 } else if (target_type == TypeManager.uint64_type){
993 } else if (c is ULongConstant){
994 ulong v = ((ULongConstant) c).Value;
996 if (target_type == TypeManager.int32_type){
997 if (v <= Int32.MaxValue)
999 } else if (target_type == TypeManager.uint32_type){
1000 if (v <= UInt32.MaxValue)
1002 } else if (target_type == TypeManager.char_type){
1003 if (v >= Char.MinValue && v <= Char.MaxValue)
1005 } else if (target_type == TypeManager.byte_type){
1006 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1008 } else if (target_type == TypeManager.sbyte_type){
1009 if (v <= (int) SByte.MaxValue)
1011 } else if (target_type == TypeManager.short_type){
1012 if (v <= UInt16.MaxValue)
1014 } else if (target_type == TypeManager.ushort_type){
1015 if (v <= UInt16.MaxValue)
1017 } else if (target_type == TypeManager.int64_type){
1018 if (v <= Int64.MaxValue)
1022 } else if (c is ByteConstant){
1023 byte v = ((ByteConstant) c).Value;
1025 if (target_type == TypeManager.int32_type)
1027 else if (target_type == TypeManager.uint32_type)
1029 else if (target_type == TypeManager.char_type)
1031 else if (target_type == TypeManager.sbyte_type){
1032 if (v <= SByte.MaxValue)
1034 } else if (target_type == TypeManager.short_type)
1036 else if (target_type == TypeManager.ushort_type)
1038 else if (target_type == TypeManager.int64_type)
1040 else if (target_type == TypeManager.uint64_type)
1043 } else if (c is SByteConstant){
1044 sbyte v = ((SByteConstant) c).Value;
1046 if (target_type == TypeManager.int32_type)
1048 else if (target_type == TypeManager.uint32_type){
1051 } else if (target_type == TypeManager.char_type){
1054 } else if (target_type == TypeManager.byte_type){
1057 } else if (target_type == TypeManager.short_type)
1059 else if (target_type == TypeManager.ushort_type){
1062 } else if (target_type == TypeManager.int64_type)
1064 else if (target_type == TypeManager.uint64_type){
1069 } else if (c is ShortConstant){
1070 short v = ((ShortConstant) c).Value;
1072 if (target_type == TypeManager.int32_type){
1074 } else if (target_type == TypeManager.uint32_type){
1077 } else if (target_type == TypeManager.char_type){
1080 } else if (target_type == TypeManager.byte_type){
1081 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1083 } else if (target_type == TypeManager.sbyte_type){
1084 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1086 } else if (target_type == TypeManager.ushort_type){
1089 } else if (target_type == TypeManager.int64_type)
1091 else if (target_type == TypeManager.uint64_type)
1095 } else if (c is UShortConstant){
1096 ushort v = ((UShortConstant) c).Value;
1098 if (target_type == TypeManager.int32_type)
1100 else if (target_type == TypeManager.uint32_type)
1102 else if (target_type == TypeManager.char_type){
1103 if (v >= Char.MinValue && v <= Char.MaxValue)
1105 } else if (target_type == TypeManager.byte_type){
1106 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1108 } else if (target_type == TypeManager.sbyte_type){
1109 if (v <= SByte.MaxValue)
1111 } else if (target_type == TypeManager.short_type){
1112 if (v <= Int16.MaxValue)
1114 } else if (target_type == TypeManager.int64_type)
1116 else if (target_type == TypeManager.uint64_type)
1120 } else if (c is CharConstant){
1121 char v = ((CharConstant) c).Value;
1123 if (target_type == TypeManager.int32_type)
1125 else if (target_type == TypeManager.uint32_type)
1127 else if (target_type == TypeManager.byte_type){
1128 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1130 } else if (target_type == TypeManager.sbyte_type){
1131 if (v <= SByte.MaxValue)
1133 } else if (target_type == TypeManager.short_type){
1134 if (v <= Int16.MaxValue)
1136 } else if (target_type == TypeManager.ushort_type)
1138 else if (target_type == TypeManager.int64_type)
1140 else if (target_type == TypeManager.uint64_type)
1145 Error_ConstantValueCannotBeConverted (loc, s, target_type);
1150 // Load the object from the pointer.
1152 public static void LoadFromPtr (ILGenerator ig, Type t)
1154 if (t == TypeManager.int32_type)
1155 ig.Emit (OpCodes.Ldind_I4);
1156 else if (t == TypeManager.uint32_type)
1157 ig.Emit (OpCodes.Ldind_U4);
1158 else if (t == TypeManager.short_type)
1159 ig.Emit (OpCodes.Ldind_I2);
1160 else if (t == TypeManager.ushort_type)
1161 ig.Emit (OpCodes.Ldind_U2);
1162 else if (t == TypeManager.char_type)
1163 ig.Emit (OpCodes.Ldind_U2);
1164 else if (t == TypeManager.byte_type)
1165 ig.Emit (OpCodes.Ldind_U1);
1166 else if (t == TypeManager.sbyte_type)
1167 ig.Emit (OpCodes.Ldind_I1);
1168 else if (t == TypeManager.uint64_type)
1169 ig.Emit (OpCodes.Ldind_I8);
1170 else if (t == TypeManager.int64_type)
1171 ig.Emit (OpCodes.Ldind_I8);
1172 else if (t == TypeManager.float_type)
1173 ig.Emit (OpCodes.Ldind_R4);
1174 else if (t == TypeManager.double_type)
1175 ig.Emit (OpCodes.Ldind_R8);
1176 else if (t == TypeManager.bool_type)
1177 ig.Emit (OpCodes.Ldind_I1);
1178 else if (t == TypeManager.intptr_type)
1179 ig.Emit (OpCodes.Ldind_I);
1180 else if (TypeManager.IsEnumType (t)) {
1181 if (t == TypeManager.enum_type)
1182 ig.Emit (OpCodes.Ldind_Ref);
1184 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1185 } else if (t.IsValueType)
1186 ig.Emit (OpCodes.Ldobj, t);
1187 else if (t.IsPointer)
1188 ig.Emit (OpCodes.Ldind_I);
1190 ig.Emit (OpCodes.Ldind_Ref);
1194 // The stack contains the pointer and the value of type `type'
1196 public static void StoreFromPtr (ILGenerator ig, Type type)
1198 if (TypeManager.IsEnumType (type))
1199 type = TypeManager.EnumToUnderlying (type);
1200 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1201 ig.Emit (OpCodes.Stind_I4);
1202 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1203 ig.Emit (OpCodes.Stind_I8);
1204 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1205 type == TypeManager.ushort_type)
1206 ig.Emit (OpCodes.Stind_I2);
1207 else if (type == TypeManager.float_type)
1208 ig.Emit (OpCodes.Stind_R4);
1209 else if (type == TypeManager.double_type)
1210 ig.Emit (OpCodes.Stind_R8);
1211 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1212 type == TypeManager.bool_type)
1213 ig.Emit (OpCodes.Stind_I1);
1214 else if (type == TypeManager.intptr_type)
1215 ig.Emit (OpCodes.Stind_I);
1216 else if (type.IsValueType)
1217 ig.Emit (OpCodes.Stobj, type);
1219 ig.Emit (OpCodes.Stind_Ref);
1223 // Returns the size of type `t' if known, otherwise, 0
1225 public static int GetTypeSize (Type t)
1227 t = TypeManager.TypeToCoreType (t);
1228 if (t == TypeManager.int32_type ||
1229 t == TypeManager.uint32_type ||
1230 t == TypeManager.float_type)
1232 else if (t == TypeManager.int64_type ||
1233 t == TypeManager.uint64_type ||
1234 t == TypeManager.double_type)
1236 else if (t == TypeManager.byte_type ||
1237 t == TypeManager.sbyte_type ||
1238 t == TypeManager.bool_type)
1240 else if (t == TypeManager.short_type ||
1241 t == TypeManager.char_type ||
1242 t == TypeManager.ushort_type)
1244 else if (t == TypeManager.decimal_type)
1250 public static void Error_NegativeArrayIndex (Location loc)
1252 Report.Error (248, loc, "Cannot create an array with a negative size");
1256 // Converts `source' to an int, uint, long or ulong.
1258 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1262 bool old_checked = ec.CheckState;
1263 ec.CheckState = true;
1265 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1266 if (target == null){
1267 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1268 if (target == null){
1269 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1270 if (target == null){
1271 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1273 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1277 ec.CheckState = old_checked;
1280 // Only positive constants are allowed at compile time
1282 if (target is Constant){
1283 if (target is IntConstant){
1284 if (((IntConstant) target).Value < 0){
1285 Error_NegativeArrayIndex (loc);
1290 if (target is LongConstant){
1291 if (((LongConstant) target).Value < 0){
1292 Error_NegativeArrayIndex (loc);
1305 /// This is just a base class for expressions that can
1306 /// appear on statements (invocations, object creation,
1307 /// assignments, post/pre increment and decrement). The idea
1308 /// being that they would support an extra Emition interface that
1309 /// does not leave a result on the stack.
1311 public abstract class ExpressionStatement : Expression {
1313 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1315 Expression e = Resolve (ec);
1319 ExpressionStatement es = e as ExpressionStatement;
1321 Error (201, "Only assignment, call, increment, decrement and new object " +
1322 "expressions can be used as a statement");
1328 /// Requests the expression to be emitted in a `statement'
1329 /// context. This means that no new value is left on the
1330 /// stack after invoking this method (constrasted with
1331 /// Emit that will always leave a value on the stack).
1333 public abstract void EmitStatement (EmitContext ec);
1337 /// This kind of cast is used to encapsulate the child
1338 /// whose type is child.Type into an expression that is
1339 /// reported to return "return_type". This is used to encapsulate
1340 /// expressions which have compatible types, but need to be dealt
1341 /// at higher levels with.
1343 /// For example, a "byte" expression could be encapsulated in one
1344 /// of these as an "unsigned int". The type for the expression
1345 /// would be "unsigned int".
1348 public class EmptyCast : Expression {
1349 protected Expression child;
1351 public Expression Child {
1357 public EmptyCast (Expression child, Type return_type)
1359 eclass = child.eclass;
1360 loc = child.Location;
1365 public override Expression DoResolve (EmitContext ec)
1367 // This should never be invoked, we are born in fully
1368 // initialized state.
1373 public override void Emit (EmitContext ec)
1379 /// This is a numeric cast to a Decimal
1381 public class CastToDecimal : EmptyCast {
1383 MethodInfo conversion_operator;
1385 public CastToDecimal (EmitContext ec, Expression child)
1386 : this (ec, child, false)
1390 public CastToDecimal (EmitContext ec, Expression child, bool find_explicit)
1391 : base (child, TypeManager.decimal_type)
1393 conversion_operator = GetConversionOperator (ec, find_explicit);
1395 if (conversion_operator == null)
1396 Convert.Error_CannotImplicitConversion (loc, child.Type, type);
1399 // Returns the implicit operator that converts from
1400 // 'child.Type' to System.Decimal.
1401 MethodInfo GetConversionOperator (EmitContext ec, bool find_explicit)
1403 string operator_name = "op_Implicit";
1406 operator_name = "op_Explicit";
1408 MethodGroupExpr opers = Expression.MethodLookup (
1409 ec, type, operator_name, loc) as MethodGroupExpr;
1412 Convert.Error_CannotImplicitConversion (loc, child.Type, type);
1414 foreach (MethodInfo oper in opers.Methods) {
1415 ParameterData pd = TypeManager.GetParameterData (oper);
1417 if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
1423 public override void Emit (EmitContext ec)
1425 ILGenerator ig = ec.ig;
1428 ig.Emit (OpCodes.Call, conversion_operator);
1432 /// This is an explicit numeric cast from a Decimal
1434 public class CastFromDecimal : EmptyCast
1436 MethodInfo conversion_operator;
1437 public CastFromDecimal (EmitContext ec, Expression child, Type return_type)
1438 : base (child, return_type)
1440 if (child.Type != TypeManager.decimal_type)
1441 throw new InternalErrorException (
1442 "The expected type is Decimal, instead it is " + child.Type.FullName);
1444 conversion_operator = GetConversionOperator (ec);
1445 if (conversion_operator == null)
1446 Convert.Error_CannotImplicitConversion (loc, child.Type, type);
1449 // Returns the explicit operator that converts from an
1450 // express of type System.Decimal to 'type'.
1451 MethodInfo GetConversionOperator (EmitContext ec)
1453 MethodGroupExpr opers = Expression.MethodLookup (
1454 ec, child.Type, "op_Explicit", loc) as MethodGroupExpr;
1457 Convert.Error_CannotImplicitConversion (loc, child.Type, type);
1459 foreach (MethodInfo oper in opers.Methods) {
1460 ParameterData pd = TypeManager.GetParameterData (oper);
1462 if (pd.ParameterType (0) == child.Type && oper.ReturnType == type)
1468 public override void Emit (EmitContext ec)
1470 ILGenerator ig = ec.ig;
1473 ig.Emit (OpCodes.Call, conversion_operator);
1478 // We need to special case this since an empty cast of
1479 // a NullLiteral is still a Constant
1481 public class NullCast : Constant {
1482 protected Expression child;
1484 public NullCast (Expression child, Type return_type)
1486 eclass = child.eclass;
1491 override public string AsString ()
1496 public override object GetValue ()
1501 public override Expression DoResolve (EmitContext ec)
1503 // This should never be invoked, we are born in fully
1504 // initialized state.
1509 public override void Emit (EmitContext ec)
1514 public override bool IsDefaultValue {
1516 throw new NotImplementedException ();
1520 public override bool IsNegative {
1529 /// This class is used to wrap literals which belong inside Enums
1531 public class EnumConstant : Constant {
1532 public Constant Child;
1534 public EnumConstant (Constant child, Type enum_type)
1536 eclass = child.eclass;
1541 public override Expression DoResolve (EmitContext ec)
1543 // This should never be invoked, we are born in fully
1544 // initialized state.
1549 public override void Emit (EmitContext ec)
1554 public override object GetValue ()
1556 return Child.GetValue ();
1559 public object GetValueAsEnumType ()
1561 return System.Enum.ToObject (type, Child.GetValue ());
1565 // Converts from one of the valid underlying types for an enumeration
1566 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1567 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1569 public Constant WidenToCompilerConstant ()
1571 Type t = TypeManager.EnumToUnderlying (Child.Type);
1572 object v = ((Constant) Child).GetValue ();;
1574 if (t == TypeManager.int32_type)
1575 return new IntConstant ((int) v);
1576 if (t == TypeManager.uint32_type)
1577 return new UIntConstant ((uint) v);
1578 if (t == TypeManager.int64_type)
1579 return new LongConstant ((long) v);
1580 if (t == TypeManager.uint64_type)
1581 return new ULongConstant ((ulong) v);
1582 if (t == TypeManager.short_type)
1583 return new ShortConstant ((short) v);
1584 if (t == TypeManager.ushort_type)
1585 return new UShortConstant ((ushort) v);
1586 if (t == TypeManager.byte_type)
1587 return new ByteConstant ((byte) v);
1588 if (t == TypeManager.sbyte_type)
1589 return new SByteConstant ((sbyte) v);
1591 throw new Exception ("Invalid enumeration underlying type: " + t);
1595 // Extracts the value in the enumeration on its native representation
1597 public object GetPlainValue ()
1599 Type t = TypeManager.EnumToUnderlying (Child.Type);
1600 object v = ((Constant) Child).GetValue ();;
1602 if (t == TypeManager.int32_type)
1604 if (t == TypeManager.uint32_type)
1606 if (t == TypeManager.int64_type)
1608 if (t == TypeManager.uint64_type)
1610 if (t == TypeManager.short_type)
1612 if (t == TypeManager.ushort_type)
1614 if (t == TypeManager.byte_type)
1616 if (t == TypeManager.sbyte_type)
1622 public override string AsString ()
1624 return Child.AsString ();
1627 public override DoubleConstant ConvertToDouble ()
1629 return Child.ConvertToDouble ();
1632 public override FloatConstant ConvertToFloat ()
1634 return Child.ConvertToFloat ();
1637 public override ULongConstant ConvertToULong ()
1639 return Child.ConvertToULong ();
1642 public override LongConstant ConvertToLong ()
1644 return Child.ConvertToLong ();
1647 public override UIntConstant ConvertToUInt ()
1649 return Child.ConvertToUInt ();
1652 public override IntConstant ConvertToInt ()
1654 return Child.ConvertToInt ();
1657 public override bool IsDefaultValue {
1659 return Child.IsDefaultValue;
1663 public override bool IsZeroInteger {
1664 get { return Child.IsZeroInteger; }
1667 public override bool IsNegative {
1669 return Child.IsNegative;
1675 /// This kind of cast is used to encapsulate Value Types in objects.
1677 /// The effect of it is to box the value type emitted by the previous
1680 public class BoxedCast : EmptyCast {
1682 public BoxedCast (Expression expr)
1683 : base (expr, TypeManager.object_type)
1685 eclass = ExprClass.Value;
1688 public BoxedCast (Expression expr, Type target_type)
1689 : base (expr, target_type)
1691 eclass = ExprClass.Value;
1694 public override Expression DoResolve (EmitContext ec)
1696 // This should never be invoked, we are born in fully
1697 // initialized state.
1702 public override void Emit (EmitContext ec)
1706 ec.ig.Emit (OpCodes.Box, child.Type);
1710 public class UnboxCast : EmptyCast {
1711 public UnboxCast (Expression expr, Type return_type)
1712 : base (expr, return_type)
1716 public override Expression DoResolve (EmitContext ec)
1718 // This should never be invoked, we are born in fully
1719 // initialized state.
1724 public override void Emit (EmitContext ec)
1727 ILGenerator ig = ec.ig;
1730 ig.Emit (OpCodes.Unbox, t);
1732 LoadFromPtr (ig, t);
1737 /// This is used to perform explicit numeric conversions.
1739 /// Explicit numeric conversions might trigger exceptions in a checked
1740 /// context, so they should generate the conv.ovf opcodes instead of
1743 public class ConvCast : EmptyCast {
1744 public enum Mode : byte {
1745 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1747 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1748 U2_I1, U2_U1, U2_I2, U2_CH,
1749 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1750 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1751 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1752 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1753 CH_I1, CH_U1, CH_I2,
1754 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1755 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1761 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1762 : base (child, return_type)
1764 checked_state = ec.CheckState;
1768 public override Expression DoResolve (EmitContext ec)
1770 // This should never be invoked, we are born in fully
1771 // initialized state.
1776 public override string ToString ()
1778 return String.Format ("ConvCast ({0}, {1})", mode, child);
1781 public override void Emit (EmitContext ec)
1783 ILGenerator ig = ec.ig;
1789 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1790 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1791 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1792 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1793 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1795 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1796 case Mode.U1_CH: /* nothing */ break;
1798 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1799 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1800 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1801 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1802 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1803 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1805 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1806 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1807 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1808 case Mode.U2_CH: /* nothing */ break;
1810 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1811 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1812 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1813 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1814 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1815 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1816 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1818 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1819 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1820 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1821 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1822 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1823 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1825 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1826 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1827 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1828 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1829 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1830 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1831 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1832 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1834 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1835 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1836 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1837 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1838 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1839 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1840 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1841 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1843 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1844 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1845 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1847 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1848 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1849 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1850 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1851 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1852 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1853 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1854 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1855 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1857 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1858 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1859 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1860 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1861 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1862 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1863 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1864 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1865 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1866 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1870 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1871 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1872 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1873 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1874 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1876 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1877 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1879 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1880 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1881 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1882 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1883 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1884 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1886 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1887 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1888 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1889 case Mode.U2_CH: /* nothing */ break;
1891 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1892 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1893 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1894 case Mode.I4_U4: /* nothing */ break;
1895 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1896 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1897 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1899 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1900 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1901 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1902 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1903 case Mode.U4_I4: /* nothing */ break;
1904 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1906 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1907 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1908 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1909 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1910 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1911 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1912 case Mode.I8_U8: /* nothing */ break;
1913 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1915 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1916 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1917 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1918 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1919 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1920 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1921 case Mode.U8_I8: /* nothing */ break;
1922 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1924 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1925 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1926 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1928 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1929 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1930 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1931 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1932 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1933 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1934 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1935 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1936 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1938 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1939 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1940 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1941 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1942 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1943 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1944 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1945 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1946 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1947 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1953 public class OpcodeCast : EmptyCast {
1957 public OpcodeCast (Expression child, Type return_type, OpCode op)
1958 : base (child, return_type)
1962 second_valid = false;
1965 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1966 : base (child, return_type)
1971 second_valid = true;
1974 public override Expression DoResolve (EmitContext ec)
1976 // This should never be invoked, we are born in fully
1977 // initialized state.
1982 public override void Emit (EmitContext ec)
1993 /// This kind of cast is used to encapsulate a child and cast it
1994 /// to the class requested
1996 public class ClassCast : EmptyCast {
1997 public ClassCast (Expression child, Type return_type)
1998 : base (child, return_type)
2003 public override Expression DoResolve (EmitContext ec)
2005 // This should never be invoked, we are born in fully
2006 // initialized state.
2011 public override void Emit (EmitContext ec)
2015 ec.ig.Emit (OpCodes.Castclass, type);
2021 /// SimpleName expressions are formed of a single word and only happen at the beginning
2022 /// of a dotted-name.
2024 public class SimpleName : Expression {
2028 public SimpleName (string name, Location l)
2034 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
2036 if (ec.IsFieldInitializer)
2039 "A field initializer cannot reference the non-static field, " +
2040 "method or property `"+name+"'");
2044 "An object reference is required " +
2045 "for the non-static field `"+name+"'");
2048 public bool IdenticalNameAndTypeName (EmitContext ec, Expression resolved_to, Location loc)
2050 return resolved_to != null && resolved_to.Type != null &&
2051 resolved_to.Type.Name == Name &&
2052 (ec.DeclSpace.LookupType (Name, loc, /* ignore_cs0104 = */ true) != null);
2055 public override Expression DoResolve (EmitContext ec)
2057 return SimpleNameResolve (ec, null, false);
2060 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
2062 return SimpleNameResolve (ec, right_side, false);
2066 public Expression DoResolve (EmitContext ec, bool intermediate)
2068 return SimpleNameResolve (ec, null, intermediate);
2071 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2073 int errors = Report.Errors;
2074 FullNamedExpression dt = ec.DeclSpace.LookupType (Name, loc, /*ignore_cs0104=*/ false);
2075 if (Report.Errors != errors)
2081 Expression SimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2087 Expression e = DoSimpleNameResolve (ec, right_side, intermediate);
2091 if (ec.CurrentBlock == null || ec.CurrentBlock.CheckInvariantMeaningInBlock (Name, e, Location))
2098 /// 7.5.2: Simple Names.
2100 /// Local Variables and Parameters are handled at
2101 /// parse time, so they never occur as SimpleNames.
2103 /// The `intermediate' flag is used by MemberAccess only
2104 /// and it is used to inform us that it is ok for us to
2105 /// avoid the static check, because MemberAccess might end
2106 /// up resolving the Name as a Type name and the access as
2107 /// a static type access.
2109 /// ie: Type Type; .... { Type.GetType (""); }
2111 /// Type is both an instance variable and a Type; Type.GetType
2112 /// is the static method not an instance method of type.
2114 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool intermediate)
2116 Expression e = null;
2119 // Stage 1: Performed by the parser (binding to locals or parameters).
2121 Block current_block = ec.CurrentBlock;
2122 if (current_block != null){
2123 LocalInfo vi = current_block.GetLocalInfo (Name);
2127 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2129 if (right_side != null)
2130 return var.ResolveLValue (ec, right_side, loc);
2132 return var.Resolve (ec);
2135 ParameterReference pref = current_block.Toplevel.GetParameterReference (Name, loc);
2137 if (right_side != null)
2138 return pref.ResolveLValue (ec, right_side, loc);
2140 return pref.Resolve (ec);
2145 // Stage 2: Lookup members
2148 DeclSpace lookup_ds = ec.DeclSpace;
2149 Type almost_matched_type = null;
2150 ArrayList almost_matched = null;
2152 if (lookup_ds.TypeBuilder == null)
2155 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2159 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2160 almost_matched_type = lookup_ds.TypeBuilder;
2161 almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2164 lookup_ds =lookup_ds.Parent;
2165 } while (lookup_ds != null);
2167 if (e == null && ec.ContainerType != null)
2168 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2171 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2172 almost_matched_type = ec.ContainerType;
2173 almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2175 e = ResolveAsTypeStep (ec);
2179 if (almost_matched != null)
2180 almostMatchedMembers = almost_matched;
2181 if (almost_matched_type == null)
2182 almost_matched_type = ec.ContainerType;
2183 MemberLookupFailed (ec, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclSpace.Name, true, loc);
2190 if (e is MemberExpr) {
2191 MemberExpr me = (MemberExpr) e;
2194 if (me.IsInstance) {
2195 if (ec.IsStatic || ec.IsFieldInitializer) {
2197 // Note that an MemberExpr can be both IsInstance and IsStatic.
2198 // An unresolved MethodGroupExpr can contain both kinds of methods
2199 // and each predicate is true if the MethodGroupExpr contains
2200 // at least one of that kind of method.
2204 (!intermediate || !IdenticalNameAndTypeName (ec, me, loc))) {
2205 Error_ObjectRefRequired (ec, loc, Name);
2210 // Pass the buck to MemberAccess and Invocation.
2212 left = EmptyExpression.Null;
2214 left = ec.GetThis (loc);
2217 left = new TypeExpression (ec.ContainerType, loc);
2220 e = me.ResolveMemberAccess (ec, left, loc, null);
2224 me = e as MemberExpr;
2229 TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2230 me.InstanceExpression.Type != me.DeclaringType &&
2231 !me.InstanceExpression.Type.IsSubclassOf (me.DeclaringType) &&
2232 (!intermediate || !IdenticalNameAndTypeName (ec, e, loc))) {
2233 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2234 "outer type `" + me.DeclaringType + "' via nested type `" +
2235 me.InstanceExpression.Type + "'");
2239 return (right_side != null)
2240 ? me.DoResolveLValue (ec, right_side)
2241 : me.DoResolve (ec);
2247 public override void Emit (EmitContext ec)
2250 // If this is ever reached, then we failed to
2251 // find the name as a namespace
2254 Error (103, "The name `" + Name +
2255 "' does not exist in the class `" +
2256 ec.DeclSpace.Name + "'");
2259 public override string ToString ()
2266 /// Represents a namespace or a type. The name of the class was inspired by
2267 /// section 10.8.1 (Fully Qualified Names).
2269 public abstract class FullNamedExpression : Expression {
2270 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2275 public abstract string FullName {
2281 /// Fully resolved expression that evaluates to a type
2283 public abstract class TypeExpr : FullNamedExpression {
2284 override public FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2286 TypeExpr t = DoResolveAsTypeStep (ec);
2290 eclass = ExprClass.Type;
2294 override public Expression DoResolve (EmitContext ec)
2296 return ResolveAsTypeTerminal (ec, false);
2299 override public void Emit (EmitContext ec)
2301 throw new Exception ("Should never be called");
2304 public virtual bool CheckAccessLevel (DeclSpace ds)
2306 return ds.CheckAccessLevel (Type);
2309 public virtual bool AsAccessible (DeclSpace ds, int flags)
2311 return ds.AsAccessible (Type, flags);
2314 public virtual bool IsClass {
2315 get { return Type.IsClass; }
2318 public virtual bool IsValueType {
2319 get { return Type.IsValueType; }
2322 public virtual bool IsInterface {
2323 get { return Type.IsInterface; }
2326 public virtual bool IsSealed {
2327 get { return Type.IsSealed; }
2330 public virtual bool CanInheritFrom ()
2332 if (Type == TypeManager.enum_type ||
2333 (Type == TypeManager.value_type && RootContext.StdLib) ||
2334 Type == TypeManager.multicast_delegate_type ||
2335 Type == TypeManager.delegate_type ||
2336 Type == TypeManager.array_type)
2342 public abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
2344 public virtual Type ResolveType (EmitContext ec)
2346 TypeExpr t = ResolveAsTypeTerminal (ec, false);
2353 public abstract string Name {
2357 public override bool Equals (object obj)
2359 TypeExpr tobj = obj as TypeExpr;
2363 return Type == tobj.Type;
2366 public override int GetHashCode ()
2368 return Type.GetHashCode ();
2371 public override string ToString ()
2377 public class TypeExpression : TypeExpr {
2378 public TypeExpression (Type t, Location l)
2381 eclass = ExprClass.Type;
2385 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2390 public override string Name {
2392 return Type.ToString ();
2396 public override string FullName {
2398 return Type.FullName;
2404 /// Used to create types from a fully qualified name. These are just used
2405 /// by the parser to setup the core types. A TypeLookupExpression is always
2406 /// classified as a type.
2408 public class TypeLookupExpression : TypeExpr {
2411 public TypeLookupExpression (string name)
2416 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2419 FullNamedExpression t = ec.DeclSpace.LookupType (name, Location.Null, /*ignore_cs0104=*/ false);
2421 Report.Error (246, loc, "Cannot find type `" + name + "'");
2424 if (!(t is TypeExpr)) {
2425 Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected",
2426 t.FullName, t.ExprClassName ());
2430 type = ((TypeExpr) t).ResolveType (ec);
2436 public override string Name {
2442 public override string FullName {
2449 public class TypeAliasExpression : TypeExpr {
2452 public TypeAliasExpression (TypeExpr texpr, Location l)
2455 loc = texpr.Location;
2457 eclass = ExprClass.Type;
2460 public override string Name {
2461 get { return texpr.Name; }
2464 public override string FullName {
2465 get { return texpr.FullName; }
2468 public override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2470 Type type = texpr.ResolveType (ec);
2474 return new TypeExpression (type, loc);
2477 public override bool CheckAccessLevel (DeclSpace ds)
2479 return texpr.CheckAccessLevel (ds);
2482 public override bool AsAccessible (DeclSpace ds, int flags)
2484 return texpr.AsAccessible (ds, flags);
2487 public override bool IsClass {
2488 get { return texpr.IsClass; }
2491 public override bool IsValueType {
2492 get { return texpr.IsValueType; }
2495 public override bool IsInterface {
2496 get { return texpr.IsInterface; }
2499 public override bool IsSealed {
2500 get { return texpr.IsSealed; }
2505 /// This class denotes an expression which evaluates to a member
2506 /// of a struct or a class.
2508 public abstract class MemberExpr : Expression
2511 /// The name of this member.
2513 public abstract string Name {
2518 /// Whether this is an instance member.
2520 public abstract bool IsInstance {
2525 /// Whether this is a static member.
2527 public abstract bool IsStatic {
2532 /// The type which declares this member.
2534 public abstract Type DeclaringType {
2539 /// The instance expression associated with this member, if it's a
2540 /// non-static member.
2542 public Expression InstanceExpression;
2544 public static void error176 (Location loc, string name)
2546 Report.Error (176, loc, "Static member `" + name + "' cannot be accessed " +
2547 "with an instance reference, qualify with a type name instead");
2551 // TODO: possible optimalization
2552 // Cache resolved constant result in FieldBuilder <-> expression map
2553 public virtual Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
2554 SimpleName original)
2558 // original == null || original.Resolve (...) ==> left
2561 if (left is TypeExpr) {
2563 SimpleName.Error_ObjectRefRequired (ec, loc, Name);
2571 if (original != null && original.IdenticalNameAndTypeName (ec, left, loc))
2574 error176 (loc, Name);
2578 InstanceExpression = left;
2583 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
2588 if (InstanceExpression == EmptyExpression.Null) {
2589 SimpleName.Error_ObjectRefRequired (ec, loc, Name);
2593 if (InstanceExpression.Type.IsValueType) {
2594 if (InstanceExpression is IMemoryLocation) {
2595 ((IMemoryLocation) InstanceExpression).AddressOf (ec, AddressOp.LoadStore);
2597 LocalTemporary t = new LocalTemporary (ec, InstanceExpression.Type);
2598 InstanceExpression.Emit (ec);
2600 t.AddressOf (ec, AddressOp.Store);
2603 InstanceExpression.Emit (ec);
2605 if (prepare_for_load)
2606 ec.ig.Emit (OpCodes.Dup);
2611 /// MethodGroup Expression.
2613 /// This is a fully resolved expression that evaluates to a type
2615 public class MethodGroupExpr : MemberExpr {
2616 public MethodBase [] Methods;
2617 bool identical_type_name = false;
2620 public MethodGroupExpr (MemberInfo [] mi, Location l)
2622 Methods = new MethodBase [mi.Length];
2623 mi.CopyTo (Methods, 0);
2624 eclass = ExprClass.MethodGroup;
2625 type = TypeManager.object_type;
2629 public MethodGroupExpr (ArrayList list, Location l)
2631 Methods = new MethodBase [list.Count];
2634 list.CopyTo (Methods, 0);
2636 foreach (MemberInfo m in list){
2637 if (!(m is MethodBase)){
2638 Console.WriteLine ("Name " + m.Name);
2639 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2646 eclass = ExprClass.MethodGroup;
2647 type = TypeManager.object_type;
2650 public override Type DeclaringType {
2653 // The methods are arranged in this order:
2654 // derived type -> base type
2656 return Methods [0].DeclaringType;
2660 public bool IdenticalTypeName {
2662 return identical_type_name;
2666 identical_type_name = value;
2670 public bool IsBase {
2679 public override string Name {
2681 return Methods [0].Name;
2685 public override bool IsInstance {
2687 foreach (MethodBase mb in Methods)
2695 public override bool IsStatic {
2697 foreach (MethodBase mb in Methods)
2705 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
2706 SimpleName original)
2708 if (!(left is TypeExpr) &&
2709 original != null && original.IdenticalNameAndTypeName (ec, left, loc))
2710 IdenticalTypeName = true;
2712 return base.ResolveMemberAccess (ec, left, loc, original);
2715 override public Expression DoResolve (EmitContext ec)
2718 InstanceExpression = null;
2720 if (InstanceExpression != null) {
2721 InstanceExpression = InstanceExpression.DoResolve (ec);
2722 if (InstanceExpression == null)
2729 public void ReportUsageError ()
2731 Report.Error (654, loc, "Method `" + DeclaringType + "." +
2732 Name + "()' is referenced without parentheses");
2735 override public void Emit (EmitContext ec)
2737 ReportUsageError ();
2740 bool RemoveMethods (bool keep_static)
2742 ArrayList smethods = new ArrayList ();
2744 foreach (MethodBase mb in Methods){
2745 if (mb.IsStatic == keep_static)
2749 if (smethods.Count == 0)
2752 Methods = new MethodBase [smethods.Count];
2753 smethods.CopyTo (Methods, 0);
2759 /// Removes any instance methods from the MethodGroup, returns
2760 /// false if the resulting set is empty.
2762 public bool RemoveInstanceMethods ()
2764 return RemoveMethods (true);
2768 /// Removes any static methods from the MethodGroup, returns
2769 /// false if the resulting set is empty.
2771 public bool RemoveStaticMethods ()
2773 return RemoveMethods (false);
2778 /// Fully resolved expression that evaluates to a Field
2780 public class FieldExpr : MemberExpr, IAssignMethod, IMemoryLocation, IVariable {
2781 public readonly FieldInfo FieldInfo;
2782 VariableInfo variable_info;
2784 LocalTemporary temp;
2786 bool in_initializer;
2788 public FieldExpr (FieldInfo fi, Location l, bool in_initializer):
2791 this.in_initializer = in_initializer;
2794 public FieldExpr (FieldInfo fi, Location l)
2797 eclass = ExprClass.Variable;
2798 type = fi.FieldType;
2802 public override string Name {
2804 return FieldInfo.Name;
2808 public override bool IsInstance {
2810 return !FieldInfo.IsStatic;
2814 public override bool IsStatic {
2816 return FieldInfo.IsStatic;
2820 public override Type DeclaringType {
2822 return FieldInfo.DeclaringType;
2826 public VariableInfo VariableInfo {
2828 return variable_info;
2832 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
2833 SimpleName original)
2835 bool left_is_type = left is TypeExpr;
2837 Type decl_type = FieldInfo.DeclaringType;
2839 bool is_emitted = FieldInfo is FieldBuilder;
2840 Type t = FieldInfo.FieldType;
2843 Const c = TypeManager.LookupConstant ((FieldBuilder) FieldInfo);
2847 if (!c.LookupConstantValue (out o))
2850 c.SetMemberIsUsed ();
2851 object real_value = ((Constant) c.Expr).GetValue ();
2853 Expression exp = Constantify (real_value, t);
2855 if (!left_is_type &&
2856 (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
2857 Report.SymbolRelatedToPreviousError (c);
2858 error176 (loc, c.GetSignatureForError ());
2867 // Decimal constants cannot be encoded in the constant blob, and thus are marked
2868 // as IsInitOnly ('readonly' in C# parlance). We get its value from the
2869 // DecimalConstantAttribute metadata.
2871 if (FieldInfo.IsInitOnly && !is_emitted && t == TypeManager.decimal_type) {
2872 object[] attrs = FieldInfo.GetCustomAttributes (TypeManager.decimal_constant_attribute_type, false);
2873 if (attrs.Length == 1)
2874 return new DecimalConstant (((System.Runtime.CompilerServices.DecimalConstantAttribute) attrs [0]).Value);
2877 if (FieldInfo.IsLiteral) {
2881 o = TypeManager.GetValue ((FieldBuilder) FieldInfo);
2883 o = FieldInfo.GetValue (FieldInfo);
2885 if (decl_type.IsSubclassOf (TypeManager.enum_type)) {
2886 if (!left_is_type &&
2887 (original == null || !original.IdenticalNameAndTypeName (ec, left, loc))) {
2888 error176 (loc, FieldInfo.Name);
2892 Expression enum_member = MemberLookup (
2893 ec, decl_type, "value__", MemberTypes.Field,
2894 AllBindingFlags | BindingFlags.NonPublic, loc);
2896 Enum en = TypeManager.LookupEnum (decl_type);
2900 c = Constantify (o, en.UnderlyingType);
2902 c = Constantify (o, enum_member.Type);
2904 return new EnumConstant (c, decl_type);
2907 Expression exp = Constantify (o, t);
2909 if (!left_is_type) {
2910 error176 (loc, FieldInfo.Name);
2917 if (t.IsPointer && !ec.InUnsafe) {
2922 return base.ResolveMemberAccess (ec, left, loc, original);
2925 override public Expression DoResolve (EmitContext ec)
2927 if (ec.InRefOutArgumentResolving && FieldInfo.IsInitOnly && !ec.IsConstructor && FieldInfo.FieldType.IsValueType) {
2928 if (FieldInfo.FieldType is TypeBuilder) {
2929 if (FieldInfo.IsStatic)
2930 Report.Error (1651, loc, "Members of readonly static field '{0}.{1}' cannot be passed ref or out (except in a constructor)",
2931 TypeManager.CSharpName (DeclaringType), Name);
2933 Report.Error (1649, loc, "Members of readonly field '{0}.{1}' cannot be passed ref or out (except in a constructor)",
2934 TypeManager.CSharpName (DeclaringType), Name);
2936 if (FieldInfo.IsStatic)
2937 Report.Error (199, loc, "A static readonly field '{0}' cannot be passed ref or out (except in a static constructor)",
2940 Report.Error (192, loc, "A readonly field '{0}' cannot be passed ref or out (except in a constructor)",
2946 if (!FieldInfo.IsStatic){
2947 if (InstanceExpression == null){
2949 // This can happen when referencing an instance field using
2950 // a fully qualified type expression: TypeName.InstanceField = xxx
2952 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2956 // Resolve the field's instance expression while flow analysis is turned
2957 // off: when accessing a field "a.b", we must check whether the field
2958 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2959 InstanceExpression = InstanceExpression.Resolve (
2960 ec, ResolveFlags.VariableOrValue | ResolveFlags.DisableFlowAnalysis);
2961 if (InstanceExpression == null)
2965 if (!in_initializer) {
2966 ObsoleteAttribute oa;
2967 FieldBase f = TypeManager.GetField (FieldInfo);
2969 oa = f.GetObsoleteAttribute (f.Parent);
2971 AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
2973 // To be sure that type is external because we do not register generated fields
2974 } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {
2975 oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
2977 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
2981 AnonymousContainer am = ec.CurrentAnonymousMethod;
2983 if (!FieldInfo.IsStatic){
2984 if (!am.IsIterator && (ec.TypeContainer is Struct)){
2985 Report.Error (1673, loc, "Can not reference instance variables in anonymous methods hosted in structs");
2988 if ((am.ContainerAnonymousMethod == null) && (InstanceExpression is This))
2989 ec.CaptureField (this);
2993 // If the instance expression is a local variable or parameter.
2994 IVariable var = InstanceExpression as IVariable;
2995 if ((var == null) || (var.VariableInfo == null))
2998 VariableInfo vi = var.VariableInfo;
2999 if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
3002 variable_info = vi.GetSubStruct (FieldInfo.Name);
3006 void Report_AssignToReadonly (bool is_instance)
3011 msg = "Readonly field can not be assigned outside " +
3012 "of constructor or variable initializer";
3014 msg = "A static readonly field can only be assigned in " +
3015 "a static constructor";
3017 Report.Error (is_instance ? 191 : 198, loc, msg);
3020 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3022 IVariable var = InstanceExpression as IVariable;
3023 if ((var != null) && (var.VariableInfo != null))
3024 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
3026 Expression e = DoResolve (ec);
3031 if (!FieldInfo.IsStatic && (InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation))) {
3032 // FIXME: Provide better error reporting.
3033 Error (1612, "Cannot modify expression because it is not a variable.");
3037 FieldBase fb = TypeManager.GetField (FieldInfo);
3041 if (!FieldInfo.IsInitOnly)
3045 // InitOnly fields can only be assigned in constructors
3048 if (ec.IsConstructor){
3049 if (IsStatic && !ec.IsStatic)
3050 Report_AssignToReadonly (false);
3052 if (ec.ContainerType == FieldInfo.DeclaringType)
3056 Report_AssignToReadonly (!IsStatic);
3061 public override void CheckMarshallByRefAccess (Type container)
3063 if (!IsStatic && Type.IsValueType && !container.IsSubclassOf (TypeManager.mbr_type) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
3064 Report.SymbolRelatedToPreviousError (DeclaringType);
3065 Report.Error (1690, loc, "Cannot call '{0}' method, property, or indexer because it is a value type member of a marshal-by-reference class", Name);
3069 public bool VerifyFixed (bool is_expression)
3071 IVariable variable = InstanceExpression as IVariable;
3072 if ((variable == null) || !variable.VerifyFixed (true))
3078 public override int GetHashCode()
3080 return FieldInfo.GetHashCode ();
3083 public override bool Equals (object obj)
3085 FieldExpr fe = obj as FieldExpr;
3089 if (FieldInfo != fe.FieldInfo)
3092 if (InstanceExpression == null || fe.InstanceExpression == null)
3095 return InstanceExpression.Equals (fe.InstanceExpression);
3098 public void Emit (EmitContext ec, bool leave_copy)
3100 ILGenerator ig = ec.ig;
3101 bool is_volatile = false;
3103 if (FieldInfo is FieldBuilder){
3104 FieldBase f = TypeManager.GetField (FieldInfo);
3106 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3109 f.SetMemberIsUsed ();
3113 if (FieldInfo.IsStatic){
3115 ig.Emit (OpCodes.Volatile);
3117 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3120 EmitInstance (ec, false);
3123 ig.Emit (OpCodes.Volatile);
3125 IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo);
3128 ig.Emit (OpCodes.Ldflda, FieldInfo);
3129 ig.Emit (OpCodes.Ldflda, ff.Element);
3132 ig.Emit (OpCodes.Ldfld, FieldInfo);
3137 ec.ig.Emit (OpCodes.Dup);
3138 if (!FieldInfo.IsStatic) {
3139 temp = new LocalTemporary (ec, this.Type);
3145 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3147 FieldAttributes fa = FieldInfo.Attributes;
3148 bool is_static = (fa & FieldAttributes.Static) != 0;
3149 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3150 ILGenerator ig = ec.ig;
3151 prepared = prepare_for_load;
3153 if (is_readonly && !ec.IsConstructor){
3154 Report_AssignToReadonly (!is_static);
3158 EmitInstance (ec, prepare_for_load);
3162 ec.ig.Emit (OpCodes.Dup);
3163 if (!FieldInfo.IsStatic) {
3164 temp = new LocalTemporary (ec, this.Type);
3169 if (FieldInfo is FieldBuilder){
3170 FieldBase f = TypeManager.GetField (FieldInfo);
3172 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3173 ig.Emit (OpCodes.Volatile);
3175 f.status |= Field.Status.ASSIGNED;
3180 ig.Emit (OpCodes.Stsfld, FieldInfo);
3182 ig.Emit (OpCodes.Stfld, FieldInfo);
3188 public override void Emit (EmitContext ec)
3193 public void AddressOf (EmitContext ec, AddressOp mode)
3195 ILGenerator ig = ec.ig;
3197 if (FieldInfo is FieldBuilder){
3198 FieldBase f = TypeManager.GetField (FieldInfo);
3200 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
3201 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
3205 if ((mode & AddressOp.Store) != 0)
3206 f.status |= Field.Status.ASSIGNED;
3207 if ((mode & AddressOp.Load) != 0)
3208 f.SetMemberIsUsed ();
3213 // Handle initonly fields specially: make a copy and then
3214 // get the address of the copy.
3217 if (FieldInfo.IsInitOnly){
3219 if (ec.IsConstructor){
3220 if (FieldInfo.IsStatic){
3232 local = ig.DeclareLocal (type);
3233 ig.Emit (OpCodes.Stloc, local);
3234 ig.Emit (OpCodes.Ldloca, local);
3239 if (FieldInfo.IsStatic){
3240 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3242 EmitInstance (ec, false);
3243 ig.Emit (OpCodes.Ldflda, FieldInfo);
3249 // A FieldExpr whose address can not be taken
3251 public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
3252 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
3256 public new void AddressOf (EmitContext ec, AddressOp mode)
3258 Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
3263 /// Expression that evaluates to a Property. The Assign class
3264 /// might set the `Value' expression if we are in an assignment.
3266 /// This is not an LValue because we need to re-write the expression, we
3267 /// can not take data from the stack and store it.
3269 public class PropertyExpr : MemberExpr, IAssignMethod {
3270 public readonly PropertyInfo PropertyInfo;
3273 // This is set externally by the `BaseAccess' class
3276 MethodInfo getter, setter;
3281 LocalTemporary temp;
3284 internal static PtrHashtable AccessorTable = new PtrHashtable ();
3286 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
3289 eclass = ExprClass.PropertyAccess;
3293 type = TypeManager.TypeToCoreType (pi.PropertyType);
3295 ResolveAccessors (ec);
3298 public override string Name {
3300 return PropertyInfo.Name;
3304 public override bool IsInstance {
3310 public override bool IsStatic {
3316 public override Type DeclaringType {
3318 return PropertyInfo.DeclaringType;
3322 public bool VerifyAssignable ()
3324 if (setter == null) {
3325 Report.Error (200, loc,
3326 "The property `" + PropertyInfo.Name +
3327 "' can not be assigned to, as it has not set accessor");
3334 void FindAccessors (Type invocation_type)
3336 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
3337 BindingFlags.Static | BindingFlags.Instance |
3338 BindingFlags.DeclaredOnly;
3340 Type current = PropertyInfo.DeclaringType;
3341 for (; current != null; current = current.BaseType) {
3342 MemberInfo[] group = TypeManager.MemberLookup (
3343 invocation_type, invocation_type, current,
3344 MemberTypes.Property, flags, PropertyInfo.Name, null);
3349 if (group.Length != 1)
3350 // Oooops, can this ever happen ?
3353 PropertyInfo pi = (PropertyInfo) group [0];
3356 getter = pi.GetGetMethod (true);
3359 setter = pi.GetSetMethod (true);
3361 MethodInfo accessor = getter != null ? getter : setter;
3363 if (!accessor.IsVirtual)
3369 // We also perform the permission checking here, as the PropertyInfo does not
3370 // hold the information for the accessibility of its setter/getter
3372 void ResolveAccessors (EmitContext ec)
3374 FindAccessors (ec.ContainerType);
3376 if (getter != null) {
3377 IMethodData md = TypeManager.GetMethod (getter);
3379 md.SetMemberIsUsed ();
3381 AccessorTable [getter] = PropertyInfo;
3382 is_static = getter.IsStatic;
3385 if (setter != null) {
3386 IMethodData md = TypeManager.GetMethod (setter);
3388 md.SetMemberIsUsed ();
3390 AccessorTable [setter] = PropertyInfo;
3391 is_static = setter.IsStatic;
3395 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
3398 InstanceExpression = null;
3402 if (InstanceExpression == null) {
3403 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
3407 InstanceExpression = InstanceExpression.DoResolve (ec);
3408 if (InstanceExpression == null)
3411 InstanceExpression.CheckMarshallByRefAccess (ec.ContainerType);
3413 if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
3414 if ((InstanceExpression.Type != ec.ContainerType) &&
3415 ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
3416 Report.Error (1540, loc, "Cannot access protected member `" +
3417 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
3418 "' via a qualifier of type `" +
3419 TypeManager.CSharpName (InstanceExpression.Type) +
3420 "'; the qualifier must be of type `" +
3421 TypeManager.CSharpName (ec.ContainerType) +
3422 "' (or derived from it)");
3430 override public Expression DoResolve (EmitContext ec)
3435 if (getter != null){
3436 if (TypeManager.GetArgumentTypes (getter).Length != 0){
3438 117, loc, "`{0}' does not contain a " +
3439 "definition for `{1}'.", getter.DeclaringType,
3445 if (getter == null){
3447 // The following condition happens if the PropertyExpr was
3448 // created, but is invalid (ie, the property is inaccessible),
3449 // and we did not want to embed the knowledge about this in
3450 // the caller routine. This only avoids double error reporting.
3455 if (InstanceExpression != EmptyExpression.Null) {
3456 Report.Error (154, loc,
3457 "The property `" + PropertyInfo.Name +
3458 "' can not be used in " +
3459 "this context because it lacks a get accessor");
3464 bool must_do_cs1540_check = false;
3465 if (getter != null &&
3466 !IsAccessorAccessible (ec.ContainerType, getter, out must_do_cs1540_check)) {
3467 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod;
3468 if (pm != null && pm.HasCustomAccessModifier) {
3469 Report.SymbolRelatedToPreviousError (pm);
3470 Report.Error (271, loc, "The property or indexer '{0}' cannot be used in this context because the get accessor is inaccessible",
3471 TypeManager.CSharpSignature (getter));
3474 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3475 TypeManager.CSharpSignature (getter));
3479 if (!InstanceResolve (ec, must_do_cs1540_check))
3483 // Only base will allow this invocation to happen.
3485 if (IsBase && getter.IsAbstract){
3486 Report.Error (205, loc, "Cannot call an abstract base property: " +
3487 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3491 if (PropertyInfo.PropertyType.IsPointer && !ec.InUnsafe){
3501 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3503 if (setter == null){
3505 // The following condition happens if the PropertyExpr was
3506 // created, but is invalid (ie, the property is inaccessible),
3507 // and we did not want to embed the knowledge about this in
3508 // the caller routine. This only avoids double error reporting.
3513 // TODO: Print better property name
3514 Report.Error (200, loc, "Property or indexer '{0}' cannot be assigned to -- it is read only",
3519 if (TypeManager.GetArgumentTypes (setter).Length != 1){
3521 117, loc, "`{0}' does not contain a " +
3522 "definition for `{1}'.", getter.DeclaringType,
3527 bool must_do_cs1540_check;
3528 if (!IsAccessorAccessible (ec.ContainerType, setter, out must_do_cs1540_check)) {
3529 PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod;
3530 if (pm != null && pm.HasCustomAccessModifier) {
3531 Report.SymbolRelatedToPreviousError (pm);
3532 Report.Error (272, loc, "The property or indexer '{0}' cannot be used in this context because the set accessor is inaccessible",
3533 TypeManager.CSharpSignature (setter));
3536 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3537 TypeManager.CSharpSignature (setter));
3541 if (!InstanceResolve (ec, must_do_cs1540_check))
3545 // Only base will allow this invocation to happen.
3547 if (IsBase && setter.IsAbstract){
3548 Report.Error (205, loc, "Cannot call an abstract base property: " +
3549 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3554 // Check that we are not making changes to a temporary memory location
3556 if (InstanceExpression != null && InstanceExpression.Type.IsValueType && !(InstanceExpression is IMemoryLocation)) {
3557 // FIXME: Provide better error reporting.
3558 Error (1612, "Cannot modify expression because it is not a variable.");
3565 public override void Emit (EmitContext ec)
3570 public void Emit (EmitContext ec, bool leave_copy)
3573 EmitInstance (ec, false);
3576 // Special case: length of single dimension array property is turned into ldlen
3578 if ((getter == TypeManager.system_int_array_get_length) ||
3579 (getter == TypeManager.int_array_get_length)){
3580 Type iet = InstanceExpression.Type;
3583 // System.Array.Length can be called, but the Type does not
3584 // support invoking GetArrayRank, so test for that case first
3586 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
3587 ec.ig.Emit (OpCodes.Ldlen);
3588 ec.ig.Emit (OpCodes.Conv_I4);
3593 Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
3598 ec.ig.Emit (OpCodes.Dup);
3600 temp = new LocalTemporary (ec, this.Type);
3606 // Implements the IAssignMethod interface for assignments
3608 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3610 prepared = prepare_for_load;
3612 EmitInstance (ec, prepare_for_load);
3616 ec.ig.Emit (OpCodes.Dup);
3618 temp = new LocalTemporary (ec, this.Type);
3623 ArrayList args = new ArrayList (1);
3624 args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
3626 Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
3634 /// Fully resolved expression that evaluates to an Event
3636 public class EventExpr : MemberExpr {
3637 public readonly EventInfo EventInfo;
3640 MethodInfo add_accessor, remove_accessor;
3642 public EventExpr (EventInfo ei, Location loc)
3646 eclass = ExprClass.EventAccess;
3648 add_accessor = TypeManager.GetAddMethod (ei);
3649 remove_accessor = TypeManager.GetRemoveMethod (ei);
3651 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3654 if (EventInfo is MyEventBuilder){
3655 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3656 type = eb.EventType;
3659 type = EventInfo.EventHandlerType;
3662 public override string Name {
3664 return EventInfo.Name;
3668 public override bool IsInstance {
3674 public override bool IsStatic {
3680 public override Type DeclaringType {
3682 return EventInfo.DeclaringType;
3686 public override Expression ResolveMemberAccess (EmitContext ec, Expression left, Location loc,
3687 SimpleName original)
3690 // If the event is local to this class, we transform ourselves into a FieldExpr
3693 if (EventInfo.DeclaringType == ec.ContainerType ||
3694 TypeManager.IsNestedChildOf(ec.ContainerType, EventInfo.DeclaringType)) {
3695 MemberInfo mi = TypeManager.GetPrivateFieldOfEvent (EventInfo);
3698 MemberExpr ml = (MemberExpr) ExprClassFromMemberInfo (ec, mi, loc);
3701 Report.Error (-200, loc, "Internal error!!");
3705 InstanceExpression = null;
3707 return ml.ResolveMemberAccess (ec, left, loc, original);
3711 return base.ResolveMemberAccess (ec, left, loc, original);
3715 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
3718 InstanceExpression = null;
3722 if (InstanceExpression == null) {
3723 SimpleName.Error_ObjectRefRequired (ec, loc, EventInfo.Name);
3727 InstanceExpression = InstanceExpression.DoResolve (ec);
3728 if (InstanceExpression == null)
3732 // This is using the same mechanism as the CS1540 check in PropertyExpr.
3733 // However, in the Event case, we reported a CS0122 instead.
3735 if (must_do_cs1540_check && InstanceExpression != EmptyExpression.Null) {
3736 if ((InstanceExpression.Type != ec.ContainerType) &&
3737 ec.ContainerType.IsSubclassOf (InstanceExpression.Type)) {
3738 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3739 DeclaringType.Name + "." + EventInfo.Name);
3748 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3750 return DoResolve (ec);
3753 public override Expression DoResolve (EmitContext ec)
3755 bool must_do_cs1540_check;
3756 if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check) &&
3757 IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) {
3759 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3760 DeclaringType.Name + "." + EventInfo.Name);
3764 if (!InstanceResolve (ec, must_do_cs1540_check))
3770 public override void Emit (EmitContext ec)
3772 if (InstanceExpression is This)
3773 Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=, try calling the actual delegate", Name);
3775 Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
3776 "(except on the defining type)", Name);
3779 public void EmitAddOrRemove (EmitContext ec, Expression source)
3781 BinaryDelegate source_del = (BinaryDelegate) source;
3782 Expression handler = source_del.Right;
3784 Argument arg = new Argument (handler, Argument.AType.Expression);
3785 ArrayList args = new ArrayList ();
3789 if (source_del.IsAddition)
3790 Invocation.EmitCall (
3791 ec, false, IsStatic, InstanceExpression, add_accessor, args, loc);
3793 Invocation.EmitCall (
3794 ec, false, IsStatic, InstanceExpression, remove_accessor, args, loc);