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 /// This interface denotes an expression which evaluates to a member
123 /// of a struct or a class.
125 public interface IMemberExpr
128 /// The name of this member.
135 /// Whether this is an instance member.
142 /// Whether this is a static member.
149 /// The type which declares this member.
156 /// The instance expression associated with this member, if it's a
157 /// non-static member.
159 Expression InstanceExpression {
165 /// Base class for expressions
167 public abstract class Expression {
168 public ExprClass eclass;
170 protected Location loc;
182 public Location Location {
189 /// Utility wrapper routine for Error, just to beautify the code
191 public void Error (int error, string s)
193 if (!Location.IsNull (loc))
194 Report.Error (error, loc, s);
196 Report.Error (error, s);
200 /// Utility wrapper routine for Warning, just to beautify the code
202 public void Warning (int code, string format, params object[] args)
204 Report.Warning (code, loc, format, args);
207 // Not nice but we have broken hierarchy
208 public virtual void CheckMarshallByRefAccess (Type container) {}
211 /// Tests presence of ObsoleteAttribute and report proper error
213 protected void CheckObsoleteAttribute (Type type)
215 ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
216 if (obsolete_attr == null)
219 AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
222 public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check)
224 MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask;
226 must_do_cs1540_check = false; // by default we do not check for this
229 // If only accessible to the current class or children
231 if (ma == MethodAttributes.Private) {
232 Type declaring_type = mi.DeclaringType;
234 if (invocation_type != declaring_type)
235 return TypeManager.IsNestedChildOf (invocation_type, declaring_type);
240 // FamAndAssem requires that we not only derivate, but we are on the
243 if (ma == MethodAttributes.FamANDAssem){
244 return (mi.DeclaringType.Assembly != invocation_type.Assembly);
247 // Assembly and FamORAssem succeed if we're in the same assembly.
248 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
249 if (mi.DeclaringType.Assembly == invocation_type.Assembly)
253 // We already know that we aren't in the same assembly.
254 if (ma == MethodAttributes.Assembly)
257 // Family and FamANDAssem require that we derive.
258 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem) || (ma == MethodAttributes.FamORAssem)){
259 if (!TypeManager.IsNestedFamilyAccessible (invocation_type, mi.DeclaringType))
262 if (!TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType))
263 must_do_cs1540_check = true;
272 /// Performs semantic analysis on the Expression
276 /// The Resolve method is invoked to perform the semantic analysis
279 /// The return value is an expression (it can be the
280 /// same expression in some cases) or a new
281 /// expression that better represents this node.
283 /// For example, optimizations of Unary (LiteralInt)
284 /// would return a new LiteralInt with a negated
287 /// If there is an error during semantic analysis,
288 /// then an error should be reported (using Report)
289 /// and a null value should be returned.
291 /// There are two side effects expected from calling
292 /// Resolve(): the the field variable "eclass" should
293 /// be set to any value of the enumeration
294 /// `ExprClass' and the type variable should be set
295 /// to a valid type (this is the type of the
298 public abstract Expression DoResolve (EmitContext ec);
300 public virtual Expression DoResolveLValue (EmitContext ec, Expression right_side)
306 // This is used if the expression should be resolved as a type or namespace name.
307 // the default implementation fails.
309 public virtual FullNamedExpression ResolveAsTypeStep (EmitContext ec)
315 // This is used to resolve the expression as a type, a null
316 // value will be returned if the expression is not a type
319 public virtual TypeExpr ResolveAsTypeTerminal (EmitContext ec)
321 int errors = Report.Errors;
323 FullNamedExpression fne = ResolveAsTypeStep (ec);
326 if (errors == Report.Errors)
327 Report.Error (246, Location, "Cannot find type '{0}'", ToString ());
331 if (fne.eclass != ExprClass.Type) {
332 if (errors == Report.Errors)
333 Report.Error (118, Location, "'{0}' denotes a '{1}', where a type was expected",
334 fne.FullName, fne.ExprClassName ());
338 TypeExpr te = fne as TypeExpr;
340 if (!te.CheckAccessLevel (ec.DeclSpace)) {
341 Report.Error (122, Location, "'{0}' is inaccessible due to its protection level", te.Name);
349 /// Resolves an expression and performs semantic analysis on it.
353 /// Currently Resolve wraps DoResolve to perform sanity
354 /// checking and assertion checking on what we expect from Resolve.
356 public Expression Resolve (EmitContext ec, ResolveFlags flags)
358 if ((flags & ResolveFlags.MaskExprClass) == ResolveFlags.Type)
359 return ResolveAsTypeStep (ec);
361 bool old_do_flow_analysis = ec.DoFlowAnalysis;
362 if ((flags & ResolveFlags.DisableFlowAnalysis) != 0)
363 ec.DoFlowAnalysis = false;
366 bool intermediate = (flags & ResolveFlags.Intermediate) == ResolveFlags.Intermediate;
367 if (this is SimpleName)
368 e = ((SimpleName) this).DoResolveAllowStatic (ec, intermediate);
373 ec.DoFlowAnalysis = old_do_flow_analysis;
378 if ((e is TypeExpr) || (e is ComposedCast) || (e is Namespace)) {
379 if ((flags & ResolveFlags.Type) == 0) {
380 e.Error_UnexpectedKind (flags, loc);
389 case ExprClass.Namespace:
390 if ((flags & ResolveFlags.VariableOrValue) == 0) {
391 e.Error_UnexpectedKind (flags, loc);
396 case ExprClass.MethodGroup:
397 if (RootContext.Version == LanguageVersion.ISO_1){
398 if ((flags & ResolveFlags.MethodGroup) == 0) {
399 ((MethodGroupExpr) e).ReportUsageError ();
405 case ExprClass.Value:
406 case ExprClass.Variable:
407 case ExprClass.PropertyAccess:
408 case ExprClass.EventAccess:
409 case ExprClass.IndexerAccess:
410 if ((flags & ResolveFlags.VariableOrValue) == 0) {
411 Console.WriteLine ("I got: {0} and {1}", e.GetType (), e);
412 Console.WriteLine ("I am {0} and {1}", this.GetType (), this);
413 FieldInfo fi = ((FieldExpr) e).FieldInfo;
415 Console.WriteLine ("{0} and {1}", fi.DeclaringType, fi.Name);
416 e.Error_UnexpectedKind (flags, loc);
422 throw new Exception ("Expression " + e.GetType () +
423 " ExprClass is Invalid after resolve");
426 if (e.type == null && !(e is Namespace)) {
427 throw new Exception (
428 "Expression " + e.GetType () +
429 " did not set its type after Resolve\n" +
430 "called from: " + this.GetType ());
437 /// Resolves an expression and performs semantic analysis on it.
439 public Expression Resolve (EmitContext ec)
441 return Resolve (ec, ResolveFlags.VariableOrValue);
445 /// Resolves an expression for LValue assignment
449 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
450 /// checking and assertion checking on what we expect from Resolve
452 public Expression ResolveLValue (EmitContext ec, Expression right_side)
454 int errors = Report.Errors;
455 Expression e = DoResolveLValue (ec, right_side);
458 if (errors == Report.Errors)
459 Report.Error (131, Location, "The left-hand side of an assignment or mutating operation must be a variable, property or indexer");
464 if (e.eclass == ExprClass.Invalid)
465 throw new Exception ("Expression " + e +
466 " ExprClass is Invalid after resolve");
468 if (e.eclass == ExprClass.MethodGroup) {
469 ((MethodGroupExpr) e).ReportUsageError ();
473 if ((e.type == null) && !(e is ConstructedType))
474 throw new Exception ("Expression " + e +
475 " did not set its type after Resolve");
482 /// Emits the code for the expression
486 /// The Emit method is invoked to generate the code
487 /// for the expression.
489 public abstract void Emit (EmitContext ec);
491 public virtual void EmitBranchable (EmitContext ec, Label target, bool onTrue)
494 ec.ig.Emit (onTrue ? OpCodes.Brtrue : OpCodes.Brfalse, target);
498 /// Protected constructor. Only derivate types should
499 /// be able to be created
502 protected Expression ()
504 eclass = ExprClass.Invalid;
509 /// Returns a literalized version of a literal FieldInfo
513 /// The possible return values are:
514 /// IntConstant, UIntConstant
515 /// LongLiteral, ULongConstant
516 /// FloatConstant, DoubleConstant
519 /// The value returned is already resolved.
521 public static Constant Constantify (object v, Type t)
523 if (t == TypeManager.int32_type)
524 return new IntConstant ((int) v);
525 else if (t == TypeManager.uint32_type)
526 return new UIntConstant ((uint) v);
527 else if (t == TypeManager.int64_type)
528 return new LongConstant ((long) v);
529 else if (t == TypeManager.uint64_type)
530 return new ULongConstant ((ulong) v);
531 else if (t == TypeManager.float_type)
532 return new FloatConstant ((float) v);
533 else if (t == TypeManager.double_type)
534 return new DoubleConstant ((double) v);
535 else if (t == TypeManager.string_type)
536 return new StringConstant ((string) v);
537 else if (t == TypeManager.short_type)
538 return new ShortConstant ((short)v);
539 else if (t == TypeManager.ushort_type)
540 return new UShortConstant ((ushort)v);
541 else if (t == TypeManager.sbyte_type)
542 return new SByteConstant (((sbyte)v));
543 else if (t == TypeManager.byte_type)
544 return new ByteConstant ((byte)v);
545 else if (t == TypeManager.char_type)
546 return new CharConstant ((char)v);
547 else if (t == TypeManager.bool_type)
548 return new BoolConstant ((bool) v);
549 else if (t == TypeManager.decimal_type)
550 return new DecimalConstant ((decimal) v);
551 else if (TypeManager.IsEnumType (t)){
552 Type real_type = TypeManager.TypeToCoreType (v.GetType ());
554 real_type = System.Enum.GetUnderlyingType (real_type);
556 Constant e = Constantify (v, real_type);
558 return new EnumConstant (e, t);
559 } else if (v == null && !TypeManager.IsValueType (t))
560 return NullLiteral.Null;
562 throw new Exception ("Unknown type for constant (" + t +
567 /// Returns a fully formed expression after a MemberLookup
569 public static Expression ExprClassFromMemberInfo (EmitContext ec, MemberInfo mi, Location loc)
572 return new EventExpr ((EventInfo) mi, loc);
573 else if (mi is FieldInfo)
574 return new FieldExpr ((FieldInfo) mi, loc);
575 else if (mi is PropertyInfo)
576 return new PropertyExpr (ec, (PropertyInfo) mi, loc);
577 else if (mi is Type){
578 return new TypeExpression ((System.Type) mi, loc);
584 protected static ArrayList almostMatchedMembers = new ArrayList (4);
587 // FIXME: Probably implement a cache for (t,name,current_access_set)?
589 // This code could use some optimizations, but we need to do some
590 // measurements. For example, we could use a delegate to `flag' when
591 // something can not any longer be a method-group (because it is something
595 // If the return value is an Array, then it is an array of
598 // If the return value is an MemberInfo, it is anything, but a Method
602 // FIXME: When calling MemberLookup inside an `Invocation', we should pass
603 // the arguments here and have MemberLookup return only the methods that
604 // match the argument count/type, unlike we are doing now (we delay this
607 // This is so we can catch correctly attempts to invoke instance methods
608 // from a static body (scan for error 120 in ResolveSimpleName).
611 // FIXME: Potential optimization, have a static ArrayList
614 public static Expression MemberLookup (EmitContext ec, Type queried_type, string name,
615 MemberTypes mt, BindingFlags bf, Location loc)
617 return MemberLookup (ec, ec.ContainerType, null, queried_type, name, mt, bf, loc);
621 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
622 // `qualifier_type' or null to lookup members in the current class.
625 public static Expression MemberLookup (EmitContext ec, Type container_type,
626 Type qualifier_type, Type queried_type,
627 string name, MemberTypes mt,
628 BindingFlags bf, Location loc)
630 almostMatchedMembers.Clear ();
632 MemberInfo [] mi = TypeManager.MemberLookup (
633 container_type, qualifier_type,queried_type, mt, bf, name,
634 almostMatchedMembers);
639 int count = mi.Length;
641 if (mi [0] is MethodBase)
642 return new MethodGroupExpr (mi, loc);
647 return ExprClassFromMemberInfo (ec, mi [0], loc);
650 public const MemberTypes AllMemberTypes =
651 MemberTypes.Constructor |
655 MemberTypes.NestedType |
656 MemberTypes.Property;
658 public const BindingFlags AllBindingFlags =
659 BindingFlags.Public |
660 BindingFlags.Static |
661 BindingFlags.Instance;
663 public static Expression MemberLookup (EmitContext ec, Type queried_type,
664 string name, Location loc)
666 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
667 AllMemberTypes, AllBindingFlags, loc);
670 public static Expression MemberLookup (EmitContext ec, Type qualifier_type,
671 Type queried_type, string name, Location loc)
673 return MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
674 name, AllMemberTypes, AllBindingFlags, loc);
677 public static Expression MethodLookup (EmitContext ec, Type queried_type,
678 string name, Location loc)
680 return MemberLookup (ec, ec.ContainerType, null, queried_type, name,
681 MemberTypes.Method, AllBindingFlags, loc);
685 /// This is a wrapper for MemberLookup that is not used to "probe", but
686 /// to find a final definition. If the final definition is not found, we
687 /// look for private members and display a useful debugging message if we
690 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
691 Type queried_type, string name,
694 return MemberLookupFinal (ec, qualifier_type, queried_type, name,
695 AllMemberTypes, AllBindingFlags, loc);
698 public static Expression MemberLookupFinal (EmitContext ec, Type qualifier_type,
699 Type queried_type, string name,
700 MemberTypes mt, BindingFlags bf,
705 int errors = Report.Errors;
707 e = MemberLookup (ec, ec.ContainerType, qualifier_type, queried_type,
710 if (e == null && errors == Report.Errors)
711 // No errors were reported by MemberLookup, but there was an error.
712 MemberLookupFailed (ec, qualifier_type, queried_type, name, null, true, loc);
717 public static void MemberLookupFailed (EmitContext ec, Type qualifier_type,
718 Type queried_type, string name,
719 string class_name, bool complain_if_none_found,
722 if (almostMatchedMembers.Count != 0) {
723 if (qualifier_type == null) {
724 foreach (MemberInfo m in almostMatchedMembers)
725 Report.Error (38, loc,
726 "Cannot access non-static member `{0}' via nested type `{1}'",
727 TypeManager.GetFullNameSignature (m),
728 TypeManager.CSharpName (ec.ContainerType));
733 if (qualifier_type != ec.ContainerType) {
734 // Although a derived class can access protected members of
735 // its base class it cannot do so through an instance of the
736 // base class (CS1540). If the qualifier_type is a base of the
737 // ec.ContainerType and the lookup succeeds with the latter one,
738 // then we are in this situation.
739 for (int i = 0; i < almostMatchedMembers.Count; ++i) {
740 MemberInfo m = (MemberInfo) almostMatchedMembers [i];
741 for (int j = 0; j < i; ++j) {
742 if (m == almostMatchedMembers [j]) {
750 Report.SymbolRelatedToPreviousError (m);
751 Report.Error (1540, loc,
752 "Cannot access protected member `{0}' via a qualifier of type `{1}';"
753 + " the qualifier must be of type `{2}' (or derived from it)",
754 TypeManager.GetFullNameSignature (m),
755 TypeManager.CSharpName (qualifier_type),
756 TypeManager.CSharpName (ec.ContainerType));
760 almostMatchedMembers.Clear ();
763 MemberInfo[] mi = TypeManager.MemberLookup (queried_type, null, queried_type,
764 AllMemberTypes, AllBindingFlags |
765 BindingFlags.NonPublic, name, null);
768 if (!complain_if_none_found)
771 if (class_name != null)
772 Report.Error (103, loc, "The name `" + name + "' could not be " +
773 "found in `" + class_name + "'");
776 117, loc, "`" + queried_type + "' does not contain a " +
777 "definition for `" + name + "'");
781 if (TypeManager.MemberLookup (queried_type, null, queried_type,
782 AllMemberTypes, AllBindingFlags |
783 BindingFlags.NonPublic, name, null) == null) {
784 if ((mi.Length == 1) && (mi [0] is Type)) {
785 Type t = (Type) mi [0];
787 Report.Error (305, loc,
788 "Using the generic type `{0}' " +
789 "requires {1} type arguments",
790 TypeManager.GetFullName (t),
791 TypeManager.GetNumberOfTypeArguments (t));
796 if (name == ".ctor" && TypeManager.FindMembers (qualifier_type, MemberTypes.Constructor,
797 BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly, null, null).Count == 0)
799 Report.Error (143, loc, String.Format ("The type '{0}' has no constructors defined", TypeManager.CSharpName (queried_type)));
803 if (qualifier_type != null) {
804 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", TypeManager.CSharpName (qualifier_type) + "." + name);
806 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level", name);
810 static public MemberInfo GetFieldFromEvent (EventExpr event_expr)
812 EventInfo ei = event_expr.EventInfo;
814 return TypeManager.GetPrivateFieldOfEvent (ei);
818 /// Returns an expression that can be used to invoke operator true
819 /// on the expression if it exists.
821 static public Expression GetOperatorTrue (EmitContext ec, Expression e, Location loc)
823 return GetOperatorTrueOrFalse (ec, e, true, loc);
827 /// Returns an expression that can be used to invoke operator false
828 /// on the expression if it exists.
830 static public Expression GetOperatorFalse (EmitContext ec, Expression e, Location loc)
832 return GetOperatorTrueOrFalse (ec, e, false, loc);
835 static Expression GetOperatorTrueOrFalse (EmitContext ec, Expression e, bool is_true, Location loc)
838 Expression operator_group;
840 if (TypeManager.IsNullableType (e.Type))
841 return new Nullable.OperatorTrueOrFalse (e, is_true, loc).Resolve (ec);
843 operator_group = MethodLookup (ec, e.Type, is_true ? "op_True" : "op_False", loc);
844 if (operator_group == null)
847 ArrayList arguments = new ArrayList ();
848 arguments.Add (new Argument (e, Argument.AType.Expression));
849 method = Invocation.OverloadResolve (
850 ec, (MethodGroupExpr) operator_group, arguments, false, loc);
855 return new StaticCallExpr ((MethodInfo) method, arguments, loc);
859 /// Resolves the expression `e' into a boolean expression: either through
860 /// an implicit conversion, or through an `operator true' invocation
862 public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
868 if (e.Type == TypeManager.bool_type)
871 Expression converted = Convert.ImplicitConversion (ec, e, TypeManager.bool_type, new Location (-1));
873 if (converted != null)
877 // If no implicit conversion to bool exists, try using `operator true'
879 Expression operator_true = Expression.GetOperatorTrue (ec, e, loc);
880 if (operator_true == null){
881 Report.Error (31, loc, "Can not convert the expression to a boolean");
884 return operator_true;
887 public string ExprClassName ()
890 case ExprClass.Invalid:
892 case ExprClass.Value:
894 case ExprClass.Variable:
896 case ExprClass.Namespace:
900 case ExprClass.MethodGroup:
901 return "method group";
902 case ExprClass.PropertyAccess:
903 return "property access";
904 case ExprClass.EventAccess:
905 return "event access";
906 case ExprClass.IndexerAccess:
907 return "indexer access";
908 case ExprClass.Nothing:
911 throw new Exception ("Should not happen");
915 /// Reports that we were expecting `expr' to be of class `expected'
917 public void Error_UnexpectedKind (string expected, Location loc)
919 Report.Error (118, loc, "Expression denotes a `" + ExprClassName () +
920 "' where a `" + expected + "' was expected");
923 public void Error_UnexpectedKind (ResolveFlags flags, Location loc)
925 ArrayList valid = new ArrayList (10);
927 if ((flags & ResolveFlags.VariableOrValue) != 0) {
928 valid.Add ("variable");
932 if ((flags & ResolveFlags.Type) != 0)
935 if ((flags & ResolveFlags.MethodGroup) != 0)
936 valid.Add ("method group");
938 if (valid.Count == 0)
939 valid.Add ("unknown");
941 StringBuilder sb = new StringBuilder ();
942 for (int i = 0; i < valid.Count; i++) {
945 else if (i == valid.Count)
947 sb.Append (valid [i]);
950 Report.Error (119, loc, "Expression denotes a `" + ExprClassName () + "' where " +
951 "a `" + sb.ToString () + "' was expected");
954 static public void Error_ConstantValueCannotBeConverted (Location l, string val, Type t)
956 Report.Error (31, l, "Constant value `" + val + "' cannot be converted to " +
957 TypeManager.CSharpName (t));
960 public static void UnsafeError (Location loc)
962 Report.Error (214, loc, "Pointers may only be used in an unsafe context");
966 /// Converts the IntConstant, UIntConstant, LongConstant or
967 /// ULongConstant into the integral target_type. Notice
968 /// that we do not return an `Expression' we do return
969 /// a boxed integral type.
971 /// FIXME: Since I added the new constants, we need to
972 /// also support conversions from CharConstant, ByteConstant,
973 /// SByteConstant, UShortConstant, ShortConstant
975 /// This is used by the switch statement, so the domain
976 /// of work is restricted to the literals above, and the
977 /// targets are int32, uint32, char, byte, sbyte, ushort,
978 /// short, uint64 and int64
980 public static object ConvertIntLiteral (Constant c, Type target_type, Location loc)
982 if (!Convert.ImplicitStandardConversionExists (Convert.ConstantEC, c, target_type)){
983 Convert.Error_CannotImplicitConversion (loc, c.Type, target_type);
989 if (c.Type == target_type)
990 return ((Constant) c).GetValue ();
993 // Make into one of the literals we handle, we dont really care
994 // about this value as we will just return a few limited types
996 if (c is EnumConstant)
997 c = ((EnumConstant)c).WidenToCompilerConstant ();
999 if (c is IntConstant){
1000 int v = ((IntConstant) c).Value;
1002 if (target_type == TypeManager.uint32_type){
1005 } else if (target_type == TypeManager.char_type){
1006 if (v >= Char.MinValue && v <= Char.MaxValue)
1008 } else if (target_type == TypeManager.byte_type){
1009 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1011 } else if (target_type == TypeManager.sbyte_type){
1012 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1014 } else if (target_type == TypeManager.short_type){
1015 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1017 } else if (target_type == TypeManager.ushort_type){
1018 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1020 } else if (target_type == TypeManager.int64_type)
1022 else if (target_type == TypeManager.uint64_type){
1028 } else if (c is UIntConstant){
1029 uint v = ((UIntConstant) c).Value;
1031 if (target_type == TypeManager.int32_type){
1032 if (v <= Int32.MaxValue)
1034 } else if (target_type == TypeManager.char_type){
1035 if (v >= Char.MinValue && v <= Char.MaxValue)
1037 } else if (target_type == TypeManager.byte_type){
1038 if (v <= Byte.MaxValue)
1040 } else if (target_type == TypeManager.sbyte_type){
1041 if (v <= SByte.MaxValue)
1043 } else if (target_type == TypeManager.short_type){
1044 if (v <= UInt16.MaxValue)
1046 } else if (target_type == TypeManager.ushort_type){
1047 if (v <= UInt16.MaxValue)
1049 } else if (target_type == TypeManager.int64_type)
1051 else if (target_type == TypeManager.uint64_type)
1054 } else if (c is LongConstant){
1055 long v = ((LongConstant) c).Value;
1057 if (target_type == TypeManager.int32_type){
1058 if (v >= UInt32.MinValue && v <= UInt32.MaxValue)
1060 } else if (target_type == TypeManager.uint32_type){
1061 if (v >= 0 && v <= UInt32.MaxValue)
1063 } else if (target_type == TypeManager.char_type){
1064 if (v >= Char.MinValue && v <= Char.MaxValue)
1066 } else if (target_type == TypeManager.byte_type){
1067 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1069 } else if (target_type == TypeManager.sbyte_type){
1070 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1072 } else if (target_type == TypeManager.short_type){
1073 if (v >= Int16.MinValue && v <= UInt16.MaxValue)
1075 } else if (target_type == TypeManager.ushort_type){
1076 if (v >= UInt16.MinValue && v <= UInt16.MaxValue)
1078 } else if (target_type == TypeManager.uint64_type){
1083 } else if (c is ULongConstant){
1084 ulong v = ((ULongConstant) c).Value;
1086 if (target_type == TypeManager.int32_type){
1087 if (v <= Int32.MaxValue)
1089 } else if (target_type == TypeManager.uint32_type){
1090 if (v <= UInt32.MaxValue)
1092 } else if (target_type == TypeManager.char_type){
1093 if (v >= Char.MinValue && v <= Char.MaxValue)
1095 } else if (target_type == TypeManager.byte_type){
1096 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1098 } else if (target_type == TypeManager.sbyte_type){
1099 if (v <= (int) SByte.MaxValue)
1101 } else if (target_type == TypeManager.short_type){
1102 if (v <= UInt16.MaxValue)
1104 } else if (target_type == TypeManager.ushort_type){
1105 if (v <= UInt16.MaxValue)
1107 } else if (target_type == TypeManager.int64_type){
1108 if (v <= Int64.MaxValue)
1112 } else if (c is ByteConstant){
1113 byte v = ((ByteConstant) c).Value;
1115 if (target_type == TypeManager.int32_type)
1117 else if (target_type == TypeManager.uint32_type)
1119 else if (target_type == TypeManager.char_type)
1121 else if (target_type == TypeManager.sbyte_type){
1122 if (v <= SByte.MaxValue)
1124 } else if (target_type == TypeManager.short_type)
1126 else if (target_type == TypeManager.ushort_type)
1128 else if (target_type == TypeManager.int64_type)
1130 else if (target_type == TypeManager.uint64_type)
1133 } else if (c is SByteConstant){
1134 sbyte v = ((SByteConstant) c).Value;
1136 if (target_type == TypeManager.int32_type)
1138 else if (target_type == TypeManager.uint32_type){
1141 } else if (target_type == TypeManager.char_type){
1144 } else if (target_type == TypeManager.byte_type){
1147 } else if (target_type == TypeManager.short_type)
1149 else if (target_type == TypeManager.ushort_type){
1152 } else if (target_type == TypeManager.int64_type)
1154 else if (target_type == TypeManager.uint64_type){
1159 } else if (c is ShortConstant){
1160 short v = ((ShortConstant) c).Value;
1162 if (target_type == TypeManager.int32_type){
1164 } else if (target_type == TypeManager.uint32_type){
1167 } else if (target_type == TypeManager.char_type){
1170 } else if (target_type == TypeManager.byte_type){
1171 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1173 } else if (target_type == TypeManager.sbyte_type){
1174 if (v >= SByte.MinValue && v <= SByte.MaxValue)
1176 } else if (target_type == TypeManager.ushort_type){
1179 } else if (target_type == TypeManager.int64_type)
1181 else if (target_type == TypeManager.uint64_type)
1185 } else if (c is UShortConstant){
1186 ushort v = ((UShortConstant) c).Value;
1188 if (target_type == TypeManager.int32_type)
1190 else if (target_type == TypeManager.uint32_type)
1192 else if (target_type == TypeManager.char_type){
1193 if (v >= Char.MinValue && v <= Char.MaxValue)
1195 } else if (target_type == TypeManager.byte_type){
1196 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1198 } else if (target_type == TypeManager.sbyte_type){
1199 if (v <= SByte.MaxValue)
1201 } else if (target_type == TypeManager.short_type){
1202 if (v <= Int16.MaxValue)
1204 } else if (target_type == TypeManager.int64_type)
1206 else if (target_type == TypeManager.uint64_type)
1210 } else if (c is CharConstant){
1211 char v = ((CharConstant) c).Value;
1213 if (target_type == TypeManager.int32_type)
1215 else if (target_type == TypeManager.uint32_type)
1217 else if (target_type == TypeManager.byte_type){
1218 if (v >= Byte.MinValue && v <= Byte.MaxValue)
1220 } else if (target_type == TypeManager.sbyte_type){
1221 if (v <= SByte.MaxValue)
1223 } else if (target_type == TypeManager.short_type){
1224 if (v <= Int16.MaxValue)
1226 } else if (target_type == TypeManager.ushort_type)
1228 else if (target_type == TypeManager.int64_type)
1230 else if (target_type == TypeManager.uint64_type)
1235 Error_ConstantValueCannotBeConverted (loc, s, target_type);
1240 // Load the object from the pointer.
1242 public static void LoadFromPtr (ILGenerator ig, Type t)
1244 if (t == TypeManager.int32_type)
1245 ig.Emit (OpCodes.Ldind_I4);
1246 else if (t == TypeManager.uint32_type)
1247 ig.Emit (OpCodes.Ldind_U4);
1248 else if (t == TypeManager.short_type)
1249 ig.Emit (OpCodes.Ldind_I2);
1250 else if (t == TypeManager.ushort_type)
1251 ig.Emit (OpCodes.Ldind_U2);
1252 else if (t == TypeManager.char_type)
1253 ig.Emit (OpCodes.Ldind_U2);
1254 else if (t == TypeManager.byte_type)
1255 ig.Emit (OpCodes.Ldind_U1);
1256 else if (t == TypeManager.sbyte_type)
1257 ig.Emit (OpCodes.Ldind_I1);
1258 else if (t == TypeManager.uint64_type)
1259 ig.Emit (OpCodes.Ldind_I8);
1260 else if (t == TypeManager.int64_type)
1261 ig.Emit (OpCodes.Ldind_I8);
1262 else if (t == TypeManager.float_type)
1263 ig.Emit (OpCodes.Ldind_R4);
1264 else if (t == TypeManager.double_type)
1265 ig.Emit (OpCodes.Ldind_R8);
1266 else if (t == TypeManager.bool_type)
1267 ig.Emit (OpCodes.Ldind_I1);
1268 else if (t == TypeManager.intptr_type)
1269 ig.Emit (OpCodes.Ldind_I);
1270 else if (TypeManager.IsEnumType (t)) {
1271 if (t == TypeManager.enum_type)
1272 ig.Emit (OpCodes.Ldind_Ref);
1274 LoadFromPtr (ig, TypeManager.EnumToUnderlying (t));
1275 } else if (t.IsValueType || t.IsGenericParameter)
1276 ig.Emit (OpCodes.Ldobj, t);
1277 else if (t.IsPointer)
1278 ig.Emit (OpCodes.Ldind_I);
1280 ig.Emit (OpCodes.Ldind_Ref);
1284 // The stack contains the pointer and the value of type `type'
1286 public static void StoreFromPtr (ILGenerator ig, Type type)
1288 if (TypeManager.IsEnumType (type))
1289 type = TypeManager.EnumToUnderlying (type);
1290 if (type == TypeManager.int32_type || type == TypeManager.uint32_type)
1291 ig.Emit (OpCodes.Stind_I4);
1292 else if (type == TypeManager.int64_type || type == TypeManager.uint64_type)
1293 ig.Emit (OpCodes.Stind_I8);
1294 else if (type == TypeManager.char_type || type == TypeManager.short_type ||
1295 type == TypeManager.ushort_type)
1296 ig.Emit (OpCodes.Stind_I2);
1297 else if (type == TypeManager.float_type)
1298 ig.Emit (OpCodes.Stind_R4);
1299 else if (type == TypeManager.double_type)
1300 ig.Emit (OpCodes.Stind_R8);
1301 else if (type == TypeManager.byte_type || type == TypeManager.sbyte_type ||
1302 type == TypeManager.bool_type)
1303 ig.Emit (OpCodes.Stind_I1);
1304 else if (type == TypeManager.intptr_type)
1305 ig.Emit (OpCodes.Stind_I);
1306 else if (type.IsValueType || type.IsGenericParameter)
1307 ig.Emit (OpCodes.Stobj, type);
1309 ig.Emit (OpCodes.Stind_Ref);
1313 // Returns the size of type `t' if known, otherwise, 0
1315 public static int GetTypeSize (Type t)
1317 t = TypeManager.TypeToCoreType (t);
1318 if (t == TypeManager.int32_type ||
1319 t == TypeManager.uint32_type ||
1320 t == TypeManager.float_type)
1322 else if (t == TypeManager.int64_type ||
1323 t == TypeManager.uint64_type ||
1324 t == TypeManager.double_type)
1326 else if (t == TypeManager.byte_type ||
1327 t == TypeManager.sbyte_type ||
1328 t == TypeManager.bool_type)
1330 else if (t == TypeManager.short_type ||
1331 t == TypeManager.char_type ||
1332 t == TypeManager.ushort_type)
1334 else if (t == TypeManager.decimal_type)
1340 public static void Error_NegativeArrayIndex (Location loc)
1342 Report.Error (248, loc, "Cannot create an array with a negative size");
1346 // Converts `source' to an int, uint, long or ulong.
1348 public Expression ExpressionToArrayArgument (EmitContext ec, Expression source, Location loc)
1352 bool old_checked = ec.CheckState;
1353 ec.CheckState = true;
1355 target = Convert.ImplicitConversion (ec, source, TypeManager.int32_type, loc);
1356 if (target == null){
1357 target = Convert.ImplicitConversion (ec, source, TypeManager.uint32_type, loc);
1358 if (target == null){
1359 target = Convert.ImplicitConversion (ec, source, TypeManager.int64_type, loc);
1360 if (target == null){
1361 target = Convert.ImplicitConversion (ec, source, TypeManager.uint64_type, loc);
1363 Convert.Error_CannotImplicitConversion (loc, source.Type, TypeManager.int32_type);
1367 ec.CheckState = old_checked;
1370 // Only positive constants are allowed at compile time
1372 if (target is Constant){
1373 if (target is IntConstant){
1374 if (((IntConstant) target).Value < 0){
1375 Error_NegativeArrayIndex (loc);
1380 if (target is LongConstant){
1381 if (((LongConstant) target).Value < 0){
1382 Error_NegativeArrayIndex (loc);
1395 /// This is just a base class for expressions that can
1396 /// appear on statements (invocations, object creation,
1397 /// assignments, post/pre increment and decrement). The idea
1398 /// being that they would support an extra Emition interface that
1399 /// does not leave a result on the stack.
1401 public abstract class ExpressionStatement : Expression {
1403 public virtual ExpressionStatement ResolveStatement (EmitContext ec)
1405 Expression e = Resolve (ec);
1409 ExpressionStatement es = e as ExpressionStatement;
1411 Error (201, "Only assignment, call, increment, decrement and new object " +
1412 "expressions can be used as a statement");
1418 /// Requests the expression to be emitted in a `statement'
1419 /// context. This means that no new value is left on the
1420 /// stack after invoking this method (constrasted with
1421 /// Emit that will always leave a value on the stack).
1423 public abstract void EmitStatement (EmitContext ec);
1427 /// This kind of cast is used to encapsulate the child
1428 /// whose type is child.Type into an expression that is
1429 /// reported to return "return_type". This is used to encapsulate
1430 /// expressions which have compatible types, but need to be dealt
1431 /// at higher levels with.
1433 /// For example, a "byte" expression could be encapsulated in one
1434 /// of these as an "unsigned int". The type for the expression
1435 /// would be "unsigned int".
1438 public class EmptyCast : Expression {
1439 protected Expression child;
1441 public Expression Child {
1447 public EmptyCast (Expression child, Type return_type)
1449 eclass = child.eclass;
1450 loc = child.Location;
1455 public override Expression DoResolve (EmitContext ec)
1457 // This should never be invoked, we are born in fully
1458 // initialized state.
1463 public override void Emit (EmitContext ec)
1470 // We need to special case this since an empty cast of
1471 // a NullLiteral is still a Constant
1473 public class NullCast : Constant {
1474 protected Expression child;
1476 public NullCast (Expression child, Type return_type)
1478 eclass = child.eclass;
1483 override public string AsString ()
1488 public override object GetValue ()
1493 public override Expression DoResolve (EmitContext ec)
1495 // This should never be invoked, we are born in fully
1496 // initialized state.
1501 public override void Emit (EmitContext ec)
1506 public override bool IsDefaultValue {
1508 throw new NotImplementedException ();
1512 public override bool IsNegative {
1521 /// This class is used to wrap literals which belong inside Enums
1523 public class EnumConstant : Constant {
1524 public Constant Child;
1526 public EnumConstant (Constant child, Type enum_type)
1528 eclass = child.eclass;
1533 public override Expression DoResolve (EmitContext ec)
1535 // This should never be invoked, we are born in fully
1536 // initialized state.
1541 public override void Emit (EmitContext ec)
1546 public override object GetValue ()
1548 return Child.GetValue ();
1551 public object GetValueAsEnumType ()
1553 return System.Enum.ToObject (type, Child.GetValue ());
1557 // Converts from one of the valid underlying types for an enumeration
1558 // (int32, uint32, int64, uint64, short, ushort, byte, sbyte) to
1559 // one of the internal compiler literals: Int/UInt/Long/ULong Literals.
1561 public Constant WidenToCompilerConstant ()
1563 Type t = TypeManager.EnumToUnderlying (Child.Type);
1564 object v = ((Constant) Child).GetValue ();;
1566 if (t == TypeManager.int32_type)
1567 return new IntConstant ((int) v);
1568 if (t == TypeManager.uint32_type)
1569 return new UIntConstant ((uint) v);
1570 if (t == TypeManager.int64_type)
1571 return new LongConstant ((long) v);
1572 if (t == TypeManager.uint64_type)
1573 return new ULongConstant ((ulong) v);
1574 if (t == TypeManager.short_type)
1575 return new ShortConstant ((short) v);
1576 if (t == TypeManager.ushort_type)
1577 return new UShortConstant ((ushort) v);
1578 if (t == TypeManager.byte_type)
1579 return new ByteConstant ((byte) v);
1580 if (t == TypeManager.sbyte_type)
1581 return new SByteConstant ((sbyte) v);
1583 throw new Exception ("Invalid enumeration underlying type: " + t);
1587 // Extracts the value in the enumeration on its native representation
1589 public object GetPlainValue ()
1591 Type t = TypeManager.EnumToUnderlying (Child.Type);
1592 object v = ((Constant) Child).GetValue ();;
1594 if (t == TypeManager.int32_type)
1596 if (t == TypeManager.uint32_type)
1598 if (t == TypeManager.int64_type)
1600 if (t == TypeManager.uint64_type)
1602 if (t == TypeManager.short_type)
1604 if (t == TypeManager.ushort_type)
1606 if (t == TypeManager.byte_type)
1608 if (t == TypeManager.sbyte_type)
1614 public override string AsString ()
1616 return Child.AsString ();
1619 public override DoubleConstant ConvertToDouble ()
1621 return Child.ConvertToDouble ();
1624 public override FloatConstant ConvertToFloat ()
1626 return Child.ConvertToFloat ();
1629 public override ULongConstant ConvertToULong ()
1631 return Child.ConvertToULong ();
1634 public override LongConstant ConvertToLong ()
1636 return Child.ConvertToLong ();
1639 public override UIntConstant ConvertToUInt ()
1641 return Child.ConvertToUInt ();
1644 public override IntConstant ConvertToInt ()
1646 return Child.ConvertToInt ();
1649 public override bool IsDefaultValue {
1651 return Child.IsDefaultValue;
1655 public override bool IsZeroInteger {
1656 get { return Child.IsZeroInteger; }
1659 public override bool IsNegative {
1661 return Child.IsNegative;
1667 /// This kind of cast is used to encapsulate Value Types in objects.
1669 /// The effect of it is to box the value type emitted by the previous
1672 public class BoxedCast : EmptyCast {
1674 public BoxedCast (Expression expr)
1675 : base (expr, TypeManager.object_type)
1677 eclass = ExprClass.Value;
1680 public BoxedCast (Expression expr, Type target_type)
1681 : base (expr, target_type)
1683 eclass = ExprClass.Value;
1686 public override Expression DoResolve (EmitContext ec)
1688 // This should never be invoked, we are born in fully
1689 // initialized state.
1694 public override void Emit (EmitContext ec)
1698 ec.ig.Emit (OpCodes.Box, child.Type);
1702 public class UnboxCast : EmptyCast {
1703 public UnboxCast (Expression expr, Type return_type)
1704 : base (expr, return_type)
1708 public override Expression DoResolve (EmitContext ec)
1710 // This should never be invoked, we are born in fully
1711 // initialized state.
1716 public override void Emit (EmitContext ec)
1719 ILGenerator ig = ec.ig;
1722 if (t.IsGenericParameter)
1723 ig.Emit (OpCodes.Unbox_Any, t);
1725 ig.Emit (OpCodes.Unbox, t);
1727 LoadFromPtr (ig, t);
1733 /// This is used to perform explicit numeric conversions.
1735 /// Explicit numeric conversions might trigger exceptions in a checked
1736 /// context, so they should generate the conv.ovf opcodes instead of
1739 public class ConvCast : EmptyCast {
1740 public enum Mode : byte {
1741 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1743 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1744 U2_I1, U2_U1, U2_I2, U2_CH,
1745 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1746 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1747 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH,
1748 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH,
1749 CH_I1, CH_U1, CH_I2,
1750 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1751 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4
1757 public ConvCast (EmitContext ec, Expression child, Type return_type, Mode m)
1758 : base (child, return_type)
1760 checked_state = ec.CheckState;
1764 public override Expression DoResolve (EmitContext ec)
1766 // This should never be invoked, we are born in fully
1767 // initialized state.
1772 public override string ToString ()
1774 return String.Format ("ConvCast ({0}, {1})", mode, child);
1777 public override void Emit (EmitContext ec)
1779 ILGenerator ig = ec.ig;
1785 case Mode.I1_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1786 case Mode.I1_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1787 case Mode.I1_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1788 case Mode.I1_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1789 case Mode.I1_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1791 case Mode.U1_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1792 case Mode.U1_CH: /* nothing */ break;
1794 case Mode.I2_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1795 case Mode.I2_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1796 case Mode.I2_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1797 case Mode.I2_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1798 case Mode.I2_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1799 case Mode.I2_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1801 case Mode.U2_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1802 case Mode.U2_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1803 case Mode.U2_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1804 case Mode.U2_CH: /* nothing */ break;
1806 case Mode.I4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1807 case Mode.I4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1808 case Mode.I4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1809 case Mode.I4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1810 case Mode.I4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1811 case Mode.I4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1812 case Mode.I4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1814 case Mode.U4_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1815 case Mode.U4_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1816 case Mode.U4_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1817 case Mode.U4_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1818 case Mode.U4_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1819 case Mode.U4_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1821 case Mode.I8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1822 case Mode.I8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1823 case Mode.I8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1824 case Mode.I8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1825 case Mode.I8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1826 case Mode.I8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1827 case Mode.I8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1828 case Mode.I8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1830 case Mode.U8_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1831 case Mode.U8_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1832 case Mode.U8_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1833 case Mode.U8_U2: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1834 case Mode.U8_I4: ig.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1835 case Mode.U8_U4: ig.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1836 case Mode.U8_I8: ig.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1837 case Mode.U8_CH: ig.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1839 case Mode.CH_I1: ig.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1840 case Mode.CH_U1: ig.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1841 case Mode.CH_I2: ig.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1843 case Mode.R4_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1844 case Mode.R4_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1845 case Mode.R4_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1846 case Mode.R4_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1847 case Mode.R4_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1848 case Mode.R4_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1849 case Mode.R4_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1850 case Mode.R4_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1851 case Mode.R4_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1853 case Mode.R8_I1: ig.Emit (OpCodes.Conv_Ovf_I1); break;
1854 case Mode.R8_U1: ig.Emit (OpCodes.Conv_Ovf_U1); break;
1855 case Mode.R8_I2: ig.Emit (OpCodes.Conv_Ovf_I2); break;
1856 case Mode.R8_U2: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1857 case Mode.R8_I4: ig.Emit (OpCodes.Conv_Ovf_I4); break;
1858 case Mode.R8_U4: ig.Emit (OpCodes.Conv_Ovf_U4); break;
1859 case Mode.R8_I8: ig.Emit (OpCodes.Conv_Ovf_I8); break;
1860 case Mode.R8_U8: ig.Emit (OpCodes.Conv_Ovf_U8); break;
1861 case Mode.R8_CH: ig.Emit (OpCodes.Conv_Ovf_U2); break;
1862 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1866 case Mode.I1_U1: ig.Emit (OpCodes.Conv_U1); break;
1867 case Mode.I1_U2: ig.Emit (OpCodes.Conv_U2); break;
1868 case Mode.I1_U4: ig.Emit (OpCodes.Conv_U4); break;
1869 case Mode.I1_U8: ig.Emit (OpCodes.Conv_I8); break;
1870 case Mode.I1_CH: ig.Emit (OpCodes.Conv_U2); break;
1872 case Mode.U1_I1: ig.Emit (OpCodes.Conv_I1); break;
1873 case Mode.U1_CH: ig.Emit (OpCodes.Conv_U2); break;
1875 case Mode.I2_I1: ig.Emit (OpCodes.Conv_I1); break;
1876 case Mode.I2_U1: ig.Emit (OpCodes.Conv_U1); break;
1877 case Mode.I2_U2: ig.Emit (OpCodes.Conv_U2); break;
1878 case Mode.I2_U4: ig.Emit (OpCodes.Conv_U4); break;
1879 case Mode.I2_U8: ig.Emit (OpCodes.Conv_I8); break;
1880 case Mode.I2_CH: ig.Emit (OpCodes.Conv_U2); break;
1882 case Mode.U2_I1: ig.Emit (OpCodes.Conv_I1); break;
1883 case Mode.U2_U1: ig.Emit (OpCodes.Conv_U1); break;
1884 case Mode.U2_I2: ig.Emit (OpCodes.Conv_I2); break;
1885 case Mode.U2_CH: /* nothing */ break;
1887 case Mode.I4_I1: ig.Emit (OpCodes.Conv_I1); break;
1888 case Mode.I4_U1: ig.Emit (OpCodes.Conv_U1); break;
1889 case Mode.I4_I2: ig.Emit (OpCodes.Conv_I2); break;
1890 case Mode.I4_U4: /* nothing */ break;
1891 case Mode.I4_U2: ig.Emit (OpCodes.Conv_U2); break;
1892 case Mode.I4_U8: ig.Emit (OpCodes.Conv_I8); break;
1893 case Mode.I4_CH: ig.Emit (OpCodes.Conv_U2); break;
1895 case Mode.U4_I1: ig.Emit (OpCodes.Conv_I1); break;
1896 case Mode.U4_U1: ig.Emit (OpCodes.Conv_U1); break;
1897 case Mode.U4_I2: ig.Emit (OpCodes.Conv_I2); break;
1898 case Mode.U4_U2: ig.Emit (OpCodes.Conv_U2); break;
1899 case Mode.U4_I4: /* nothing */ break;
1900 case Mode.U4_CH: ig.Emit (OpCodes.Conv_U2); break;
1902 case Mode.I8_I1: ig.Emit (OpCodes.Conv_I1); break;
1903 case Mode.I8_U1: ig.Emit (OpCodes.Conv_U1); break;
1904 case Mode.I8_I2: ig.Emit (OpCodes.Conv_I2); break;
1905 case Mode.I8_U2: ig.Emit (OpCodes.Conv_U2); break;
1906 case Mode.I8_I4: ig.Emit (OpCodes.Conv_I4); break;
1907 case Mode.I8_U4: ig.Emit (OpCodes.Conv_U4); break;
1908 case Mode.I8_U8: /* nothing */ break;
1909 case Mode.I8_CH: ig.Emit (OpCodes.Conv_U2); break;
1911 case Mode.U8_I1: ig.Emit (OpCodes.Conv_I1); break;
1912 case Mode.U8_U1: ig.Emit (OpCodes.Conv_U1); break;
1913 case Mode.U8_I2: ig.Emit (OpCodes.Conv_I2); break;
1914 case Mode.U8_U2: ig.Emit (OpCodes.Conv_U2); break;
1915 case Mode.U8_I4: ig.Emit (OpCodes.Conv_I4); break;
1916 case Mode.U8_U4: ig.Emit (OpCodes.Conv_U4); break;
1917 case Mode.U8_I8: /* nothing */ break;
1918 case Mode.U8_CH: ig.Emit (OpCodes.Conv_U2); break;
1920 case Mode.CH_I1: ig.Emit (OpCodes.Conv_I1); break;
1921 case Mode.CH_U1: ig.Emit (OpCodes.Conv_U1); break;
1922 case Mode.CH_I2: ig.Emit (OpCodes.Conv_I2); break;
1924 case Mode.R4_I1: ig.Emit (OpCodes.Conv_I1); break;
1925 case Mode.R4_U1: ig.Emit (OpCodes.Conv_U1); break;
1926 case Mode.R4_I2: ig.Emit (OpCodes.Conv_I2); break;
1927 case Mode.R4_U2: ig.Emit (OpCodes.Conv_U2); break;
1928 case Mode.R4_I4: ig.Emit (OpCodes.Conv_I4); break;
1929 case Mode.R4_U4: ig.Emit (OpCodes.Conv_U4); break;
1930 case Mode.R4_I8: ig.Emit (OpCodes.Conv_I8); break;
1931 case Mode.R4_U8: ig.Emit (OpCodes.Conv_U8); break;
1932 case Mode.R4_CH: ig.Emit (OpCodes.Conv_U2); break;
1934 case Mode.R8_I1: ig.Emit (OpCodes.Conv_I1); break;
1935 case Mode.R8_U1: ig.Emit (OpCodes.Conv_U1); break;
1936 case Mode.R8_I2: ig.Emit (OpCodes.Conv_I2); break;
1937 case Mode.R8_U2: ig.Emit (OpCodes.Conv_U2); break;
1938 case Mode.R8_I4: ig.Emit (OpCodes.Conv_I4); break;
1939 case Mode.R8_U4: ig.Emit (OpCodes.Conv_U4); break;
1940 case Mode.R8_I8: ig.Emit (OpCodes.Conv_I8); break;
1941 case Mode.R8_U8: ig.Emit (OpCodes.Conv_U8); break;
1942 case Mode.R8_CH: ig.Emit (OpCodes.Conv_U2); break;
1943 case Mode.R8_R4: ig.Emit (OpCodes.Conv_R4); break;
1949 public class OpcodeCast : EmptyCast {
1953 public OpcodeCast (Expression child, Type return_type, OpCode op)
1954 : base (child, return_type)
1958 second_valid = false;
1961 public OpcodeCast (Expression child, Type return_type, OpCode op, OpCode op2)
1962 : base (child, return_type)
1967 second_valid = true;
1970 public override Expression DoResolve (EmitContext ec)
1972 // This should never be invoked, we are born in fully
1973 // initialized state.
1978 public override void Emit (EmitContext ec)
1989 /// This kind of cast is used to encapsulate a child and cast it
1990 /// to the class requested
1992 public class ClassCast : EmptyCast {
1993 public ClassCast (Expression child, Type return_type)
1994 : base (child, return_type)
1999 public override Expression DoResolve (EmitContext ec)
2001 // This should never be invoked, we are born in fully
2002 // initialized state.
2007 public override void Emit (EmitContext ec)
2011 if (child.Type.IsGenericParameter)
2012 ec.ig.Emit (OpCodes.Box, child.Type);
2014 if (type.IsGenericParameter)
2015 ec.ig.Emit (OpCodes.Unbox_Any, type);
2017 ec.ig.Emit (OpCodes.Castclass, type);
2022 /// SimpleName expressions are formed of a single word and only happen at the beginning
2023 /// of a dotted-name.
2025 public class SimpleName : Expression {
2027 public readonly TypeArguments Arguments;
2029 public SimpleName (string name, Location l)
2035 public SimpleName (string name, TypeArguments args, Location l)
2042 public SimpleName (string name, TypeParameter[] type_params, Location l)
2047 Arguments = new TypeArguments (l);
2048 foreach (TypeParameter type_param in type_params)
2049 Arguments.Add (new TypeParameterExpr (type_param, l));
2052 public static string RemoveGenericArity (string name)
2055 StringBuilder sb = new StringBuilder ();
2056 while (start < name.Length) {
2057 int pos = name.IndexOf ('`', start);
2059 sb.Append (name.Substring (start));
2063 sb.Append (name.Substring (start, pos-start));
2066 while ((pos < name.Length) && Char.IsNumber (name [pos]))
2072 return sb.ToString ();
2075 public SimpleName GetMethodGroup ()
2077 return new SimpleName (RemoveGenericArity (Name), Arguments, loc);
2080 public static void Error_ObjectRefRequired (EmitContext ec, Location l, string name)
2082 if (ec.IsFieldInitializer)
2085 "A field initializer cannot reference the non-static field, " +
2086 "method or property `"+name+"'");
2090 "An object reference is required " +
2091 "for the non-static field `"+name+"'");
2095 // Checks whether we are trying to access an instance
2096 // property, method or field from a static body.
2098 Expression MemberStaticCheck (EmitContext ec, Expression e)
2100 if (e is IMemberExpr){
2101 IMemberExpr member = (IMemberExpr) e;
2103 if (!member.IsStatic){
2104 Error_ObjectRefRequired (ec, loc, Name);
2112 public override Expression DoResolve (EmitContext ec)
2114 return SimpleNameResolve (ec, null, false, false);
2117 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
2119 return SimpleNameResolve (ec, right_side, false, false);
2123 public Expression DoResolveAllowStatic (EmitContext ec, bool intermediate)
2125 return SimpleNameResolve (ec, null, true, intermediate);
2128 private bool IsNestedChild (Type t, Type parent)
2133 while (parent != null) {
2134 if (parent.IsGenericInstance)
2135 parent = parent.GetGenericTypeDefinition ();
2137 if (TypeManager.IsNestedChildOf (t, parent))
2140 parent = parent.BaseType;
2146 FullNamedExpression ResolveNested (EmitContext ec, Type t)
2148 if (!t.IsGenericTypeDefinition)
2151 DeclSpace ds = ec.DeclSpace;
2152 while (ds != null) {
2153 if (IsNestedChild (t, ds.TypeBuilder))
2162 Type[] gen_params = t.GetGenericArguments ();
2164 int arg_count = Arguments != null ? Arguments.Count : 0;
2166 for (; (ds != null) && ds.IsGeneric; ds = ds.Parent) {
2167 if (arg_count + ds.CountTypeParameters == gen_params.Length) {
2168 TypeArguments new_args = new TypeArguments (loc);
2169 foreach (TypeParameter param in ds.TypeParameters)
2170 new_args.Add (new TypeParameterExpr (param, loc));
2172 if (Arguments != null)
2173 new_args.Add (Arguments);
2175 return new ConstructedType (t, new_args, loc);
2182 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2184 FullNamedExpression dt = ec.DeclSpace.LookupGeneric (Name, loc);
2186 return dt.ResolveAsTypeStep (ec);
2188 int errors = Report.Errors;
2189 dt = ec.DeclSpace.LookupType (Name, loc, /*silent=*/ true, /*ignore_cs0104=*/ false);
2190 if (Report.Errors != errors)
2193 if ((dt == null) || (dt.Type == null))
2196 FullNamedExpression nested = ResolveNested (ec, dt.Type);
2198 return nested.ResolveAsTypeStep (ec);
2200 if (Arguments != null) {
2201 ConstructedType ct = new ConstructedType (dt, Arguments, loc);
2202 return ct.ResolveAsTypeStep (ec);
2208 Expression SimpleNameResolve (EmitContext ec, Expression right_side,
2209 bool allow_static, bool intermediate)
2211 Expression e = DoSimpleNameResolve (ec, right_side, allow_static, intermediate);
2215 Block current_block = ec.CurrentBlock;
2216 if (current_block != null){
2217 if (current_block.IsVariableNameUsedInChildBlock (Name)) {
2218 Report.Error (135, Location,
2219 "'{0}' has a different meaning in a child block", Name);
2224 if (e.Type != null && e.Type.IsPointer && !ec.InUnsafe) {
2233 /// 7.5.2: Simple Names.
2235 /// Local Variables and Parameters are handled at
2236 /// parse time, so they never occur as SimpleNames.
2238 /// The `allow_static' flag is used by MemberAccess only
2239 /// and it is used to inform us that it is ok for us to
2240 /// avoid the static check, because MemberAccess might end
2241 /// up resolving the Name as a Type name and the access as
2242 /// a static type access.
2244 /// ie: Type Type; .... { Type.GetType (""); }
2246 /// Type is both an instance variable and a Type; Type.GetType
2247 /// is the static method not an instance method of type.
2249 Expression DoSimpleNameResolve (EmitContext ec, Expression right_side, bool allow_static, bool intermediate)
2251 Expression e = null;
2254 // Stage 1: Performed by the parser (binding to locals or parameters).
2256 Block current_block = ec.CurrentBlock;
2257 if (current_block != null){
2258 LocalInfo vi = current_block.GetLocalInfo (Name);
2262 var = new LocalVariableReference (ec.CurrentBlock, Name, loc);
2264 if (right_side != null)
2265 return var.ResolveLValue (ec, right_side);
2267 return var.Resolve (ec);
2270 ParameterReference pref = current_block.GetParameterReference (Name, loc);
2272 if (right_side != null)
2273 return pref.ResolveLValue (ec, right_side);
2275 return pref.Resolve (ec);
2280 // Stage 2: Lookup members
2283 DeclSpace lookup_ds = ec.DeclSpace;
2284 Type almost_matched_type = null;
2285 ArrayList almost_matched = null;
2287 if (lookup_ds.TypeBuilder == null)
2290 e = MemberLookup (ec, lookup_ds.TypeBuilder, Name, loc);
2294 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2295 almost_matched_type = lookup_ds.TypeBuilder;
2296 almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2299 lookup_ds =lookup_ds.Parent;
2300 } while (lookup_ds != null);
2302 if (e == null && ec.ContainerType != null)
2303 e = MemberLookup (ec, ec.ContainerType, Name, loc);
2306 if (almost_matched == null && almostMatchedMembers.Count > 0) {
2307 almost_matched_type = ec.ContainerType;
2308 almost_matched = (ArrayList) almostMatchedMembers.Clone ();
2310 e = ResolveAsTypeStep (ec);
2314 if (almost_matched != null)
2315 almostMatchedMembers = almost_matched;
2316 if (almost_matched_type == null)
2317 almost_matched_type = ec.ContainerType;
2318 MemberLookupFailed (ec, null, almost_matched_type, ((SimpleName) this).Name, ec.DeclSpace.Name, true, loc);
2325 if (e is IMemberExpr) {
2326 e = MemberAccess.ResolveMemberAccess (ec, e, null, loc, this);
2330 IMemberExpr me = e as IMemberExpr;
2334 if (Arguments != null) {
2335 MethodGroupExpr mg = me as MethodGroupExpr;
2339 return mg.ResolveGeneric (ec, Arguments);
2342 // This fails if ResolveMemberAccess() was unable to decide whether
2343 // it's a field or a type of the same name.
2345 if (!me.IsStatic && (me.InstanceExpression == null))
2349 TypeManager.IsNestedFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2350 me.InstanceExpression.Type != me.DeclaringType &&
2351 !TypeManager.IsFamilyAccessible (me.InstanceExpression.Type, me.DeclaringType) &&
2352 (!intermediate || !MemberAccess.IdenticalNameAndTypeName (ec, this, e, loc))) {
2353 Error (38, "Cannot access nonstatic member `" + me.Name + "' of " +
2354 "outer type `" + me.DeclaringType + "' via nested type `" +
2355 me.InstanceExpression.Type + "'");
2359 return (right_side != null)
2360 ? e.DoResolveLValue (ec, right_side)
2364 if (ec.IsStatic || ec.IsFieldInitializer){
2368 return MemberStaticCheck (ec, e);
2373 public override void Emit (EmitContext ec)
2376 // If this is ever reached, then we failed to
2377 // find the name as a namespace
2380 Error (103, "The name `" + Name +
2381 "' does not exist in the class `" +
2382 ec.DeclSpace.Name + "'");
2385 public override string ToString ()
2392 /// Represents a namespace or a type. The name of the class was inspired by
2393 /// section 10.8.1 (Fully Qualified Names).
2395 public abstract class FullNamedExpression : Expression {
2396 public override FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2401 public abstract string FullName {
2407 /// Fully resolved expression that evaluates to a type
2409 public abstract class TypeExpr : FullNamedExpression {
2410 override public FullNamedExpression ResolveAsTypeStep (EmitContext ec)
2412 TypeExpr t = DoResolveAsTypeStep (ec);
2416 eclass = ExprClass.Type;
2420 override public Expression DoResolve (EmitContext ec)
2422 return ResolveAsTypeTerminal (ec);
2425 override public void Emit (EmitContext ec)
2427 throw new Exception ("Should never be called");
2430 public virtual bool CheckAccessLevel (DeclSpace ds)
2432 return ds.CheckAccessLevel (Type);
2435 public virtual bool AsAccessible (DeclSpace ds, int flags)
2437 return ds.AsAccessible (Type, flags);
2440 public virtual bool IsClass {
2441 get { return Type.IsClass; }
2444 public virtual bool IsValueType {
2445 get { return Type.IsValueType; }
2448 public virtual bool IsInterface {
2449 get { return Type.IsInterface; }
2452 public virtual bool IsSealed {
2453 get { return Type.IsSealed; }
2456 public virtual bool CanInheritFrom ()
2458 if (Type == TypeManager.enum_type ||
2459 (Type == TypeManager.value_type && RootContext.StdLib) ||
2460 Type == TypeManager.multicast_delegate_type ||
2461 Type == TypeManager.delegate_type ||
2462 Type == TypeManager.array_type)
2468 protected abstract TypeExpr DoResolveAsTypeStep (EmitContext ec);
2470 public virtual Type ResolveType (EmitContext ec)
2472 TypeExpr t = ResolveAsTypeTerminal (ec);
2479 public abstract string Name {
2483 public override bool Equals (object obj)
2485 TypeExpr tobj = obj as TypeExpr;
2489 return Type == tobj.Type;
2492 public override int GetHashCode ()
2494 return Type.GetHashCode ();
2497 public override string ToString ()
2503 public class TypeExpression : TypeExpr {
2504 public TypeExpression (Type t, Location l)
2507 eclass = ExprClass.Type;
2511 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2516 public override string Name {
2518 return Type.ToString ();
2522 public override string FullName {
2524 return Type.FullName != null ? Type.FullName : Type.Name;
2530 /// Used to create types from a fully qualified name. These are just used
2531 /// by the parser to setup the core types. A TypeLookupExpression is always
2532 /// classified as a type.
2534 public class TypeLookupExpression : TypeExpr {
2537 public TypeLookupExpression (string name)
2542 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2545 FullNamedExpression t = ec.DeclSpace.LookupType (
2546 name, Location.Null, /*silent=*/ false, /*ignore_cs0104=*/ false);
2549 if (!(t is TypeExpr))
2551 type = ((TypeExpr) t).ResolveType (ec);
2557 public override string Name {
2563 public override string FullName {
2571 /// Represents an "unbound generic type", ie. typeof (Foo<>).
2574 public class UnboundTypeExpression : TypeLookupExpression {
2575 public UnboundTypeExpression (string name)
2580 public class TypeAliasExpression : TypeExpr {
2581 FullNamedExpression alias;
2586 public TypeAliasExpression (FullNamedExpression alias, TypeArguments args, Location l)
2592 eclass = ExprClass.Type;
2594 name = alias.FullName + "<" + args.ToString () + ">";
2596 name = alias.FullName;
2599 public override string Name {
2600 get { return alias.FullName; }
2603 public override string FullName {
2604 get { return name; }
2607 protected override TypeExpr DoResolveAsTypeStep (EmitContext ec)
2609 texpr = alias.ResolveAsTypeTerminal (ec);
2613 Type type = texpr.Type;
2614 int num_args = TypeManager.GetNumberOfTypeArguments (type);
2617 if (num_args == 0) {
2618 Report.Error (308, loc,
2619 "The non-generic type `{0}' cannot " +
2620 "be used with type arguments.",
2621 TypeManager.CSharpName (type));
2625 ConstructedType ctype = new ConstructedType (type, args, loc);
2626 return ctype.ResolveAsTypeTerminal (ec);
2627 } else if (num_args > 0) {
2628 Report.Error (305, loc,
2629 "Using the generic type `{0}' " +
2630 "requires {1} type arguments",
2631 TypeManager.GetFullName (type), num_args);
2635 return new TypeExpression (type, loc);
2638 public override bool CheckAccessLevel (DeclSpace ds)
2640 return texpr.CheckAccessLevel (ds);
2643 public override bool AsAccessible (DeclSpace ds, int flags)
2645 return texpr.AsAccessible (ds, flags);
2648 public override bool IsClass {
2649 get { return texpr.IsClass; }
2652 public override bool IsValueType {
2653 get { return texpr.IsValueType; }
2656 public override bool IsInterface {
2657 get { return texpr.IsInterface; }
2660 public override bool IsSealed {
2661 get { return texpr.IsSealed; }
2666 /// MethodGroup Expression.
2668 /// This is a fully resolved expression that evaluates to a type
2670 public class MethodGroupExpr : Expression, IMemberExpr {
2671 public MethodBase [] Methods;
2672 Expression instance_expression = null;
2673 bool is_explicit_impl = false;
2674 bool has_type_arguments = false;
2675 bool identical_type_name = false;
2678 public MethodGroupExpr (MemberInfo [] mi, Location l)
2680 Methods = new MethodBase [mi.Length];
2681 mi.CopyTo (Methods, 0);
2682 eclass = ExprClass.MethodGroup;
2683 type = TypeManager.object_type;
2687 public MethodGroupExpr (ArrayList list, Location l)
2689 Methods = new MethodBase [list.Count];
2692 list.CopyTo (Methods, 0);
2694 foreach (MemberInfo m in list){
2695 if (!(m is MethodBase)){
2696 Console.WriteLine ("Name " + m.Name);
2697 Console.WriteLine ("Found a: " + m.GetType ().FullName);
2704 eclass = ExprClass.MethodGroup;
2705 type = TypeManager.object_type;
2708 public Type DeclaringType {
2711 // We assume that the top-level type is in the end
2713 return Methods [Methods.Length - 1].DeclaringType;
2714 //return Methods [0].DeclaringType;
2719 // `A method group may have associated an instance expression'
2721 public Expression InstanceExpression {
2723 return instance_expression;
2727 instance_expression = value;
2731 public bool IsExplicitImpl {
2733 return is_explicit_impl;
2737 is_explicit_impl = value;
2741 public bool HasTypeArguments {
2743 return has_type_arguments;
2747 has_type_arguments = value;
2751 public bool IdenticalTypeName {
2753 return identical_type_name;
2757 identical_type_name = value;
2761 public bool IsBase {
2770 public string Name {
2772 //return Methods [0].Name;
2773 return Methods [Methods.Length - 1].Name;
2777 public bool IsInstance {
2779 foreach (MethodBase mb in Methods)
2787 public bool IsStatic {
2789 foreach (MethodBase mb in Methods)
2797 override public Expression DoResolve (EmitContext ec)
2800 instance_expression = null;
2802 if (instance_expression != null) {
2803 instance_expression = instance_expression.DoResolve (ec);
2804 if (instance_expression == null)
2811 public void ReportUsageError ()
2813 Report.Error (654, loc, "Method `" + DeclaringType + "." +
2814 Name + "()' is referenced without parentheses");
2817 override public void Emit (EmitContext ec)
2819 ReportUsageError ();
2822 bool RemoveMethods (bool keep_static)
2824 ArrayList smethods = new ArrayList ();
2826 foreach (MethodBase mb in Methods){
2827 if (mb.IsStatic == keep_static)
2831 if (smethods.Count == 0)
2834 Methods = new MethodBase [smethods.Count];
2835 smethods.CopyTo (Methods, 0);
2841 /// Removes any instance methods from the MethodGroup, returns
2842 /// false if the resulting set is empty.
2844 public bool RemoveInstanceMethods ()
2846 return RemoveMethods (true);
2850 /// Removes any static methods from the MethodGroup, returns
2851 /// false if the resulting set is empty.
2853 public bool RemoveStaticMethods ()
2855 return RemoveMethods (false);
2858 public Expression ResolveGeneric (EmitContext ec, TypeArguments args)
2860 if (args.Resolve (ec) == false)
2863 Type[] atypes = args.Arguments;
2865 int first_count = 0;
2866 MethodInfo first = null;
2868 ArrayList list = new ArrayList ();
2869 foreach (MethodBase mb in Methods) {
2870 MethodInfo mi = mb as MethodInfo;
2871 if ((mi == null) || !mi.HasGenericParameters)
2874 Type[] gen_params = mi.GetGenericArguments ();
2876 if (first == null) {
2878 first_count = gen_params.Length;
2881 if (gen_params.Length != atypes.Length)
2884 list.Add (mi.BindGenericParameters (atypes));
2887 if (list.Count > 0) {
2888 MethodGroupExpr new_mg = new MethodGroupExpr (list, Location);
2889 new_mg.InstanceExpression = InstanceExpression;
2890 new_mg.HasTypeArguments = true;
2896 305, loc, "Using the generic method `{0}' " +
2897 "requires {1} type arguments", Name,
2901 308, loc, "The non-generic method `{0}' " +
2902 "cannot be used with type arguments", Name);
2909 /// Fully resolved expression that evaluates to a Field
2911 public class FieldExpr : Expression, IAssignMethod, IMemoryLocation, IMemberExpr, IVariable {
2912 public readonly FieldInfo FieldInfo;
2913 Expression instance_expr;
2914 VariableInfo variable_info;
2916 LocalTemporary temp;
2918 bool is_field_initializer;
2920 public FieldExpr (FieldInfo fi, Location l)
2923 eclass = ExprClass.Variable;
2924 type = TypeManager.TypeToCoreType (fi.FieldType);
2928 public string Name {
2930 return FieldInfo.Name;
2934 public bool IsInstance {
2936 return !FieldInfo.IsStatic;
2940 public bool IsStatic {
2942 return FieldInfo.IsStatic;
2946 public Type DeclaringType {
2948 return FieldInfo.DeclaringType;
2952 public Expression InstanceExpression {
2954 return instance_expr;
2958 instance_expr = value;
2962 public bool IsFieldInitializer {
2964 return is_field_initializer;
2968 is_field_initializer = value;
2972 public VariableInfo VariableInfo {
2974 return variable_info;
2978 override public Expression DoResolve (EmitContext ec)
2980 if (!FieldInfo.IsStatic){
2981 if (instance_expr == null){
2983 // This can happen when referencing an instance field using
2984 // a fully qualified type expression: TypeName.InstanceField = xxx
2986 SimpleName.Error_ObjectRefRequired (ec, loc, FieldInfo.Name);
2990 // Resolve the field's instance expression while flow analysis is turned
2991 // off: when accessing a field "a.b", we must check whether the field
2992 // "a.b" is initialized, not whether the whole struct "a" is initialized.
2993 instance_expr = instance_expr.Resolve (ec, ResolveFlags.VariableOrValue |
2994 ResolveFlags.DisableFlowAnalysis);
2995 if (instance_expr == null)
2999 ObsoleteAttribute oa;
3000 FieldBase f = TypeManager.GetField (FieldInfo);
3002 oa = f.GetObsoleteAttribute (f.Parent);
3004 AttributeTester.Report_ObsoleteMessage (oa, f.GetSignatureForError (), loc);
3005 // To be sure that type is external because we do not register generated fields
3006 } else if (!(FieldInfo.DeclaringType is TypeBuilder)) {
3007 oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo);
3009 AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc);
3012 if (ec.CurrentAnonymousMethod != null){
3013 if (!FieldInfo.IsStatic){
3014 if (ec.TypeContainer is Struct){
3015 Report.Error (1673, loc, "Can not reference instance variables in anonymous methods hosted in structs");
3018 ec.CaptureField (this);
3022 // If the instance expression is a local variable or parameter.
3023 IVariable var = instance_expr as IVariable;
3024 if ((var == null) || (var.VariableInfo == null))
3027 VariableInfo vi = var.VariableInfo;
3028 if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc))
3031 variable_info = vi.GetSubStruct (FieldInfo.Name);
3035 void Report_AssignToReadonly (bool is_instance)
3040 msg = "Readonly field can not be assigned outside " +
3041 "of constructor or variable initializer";
3043 msg = "A static readonly field can only be assigned in " +
3044 "a static constructor";
3046 Report.Error (is_instance ? 191 : 198, loc, msg);
3049 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3051 IVariable var = instance_expr as IVariable;
3052 if ((var != null) && (var.VariableInfo != null))
3053 var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name);
3055 Expression e = DoResolve (ec);
3060 if (!FieldInfo.IsStatic && (instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation))) {
3061 // FIXME: Provide better error reporting.
3062 Error (1612, "Cannot modify expression because it is not a variable.");
3066 if (!FieldInfo.IsInitOnly)
3069 FieldBase fb = TypeManager.GetField (FieldInfo);
3074 // InitOnly fields can only be assigned in constructors
3077 if (ec.IsConstructor){
3078 if (IsStatic && !ec.IsStatic)
3079 Report_AssignToReadonly (false);
3082 if (!is_field_initializer &&
3083 (ec.TypeContainer.CurrentType != null))
3084 ctype = ec.TypeContainer.CurrentType;
3086 ctype = ec.ContainerType;
3088 if (TypeManager.IsEqual (ctype, FieldInfo.DeclaringType))
3092 Report_AssignToReadonly (!IsStatic);
3097 public override void CheckMarshallByRefAccess (Type container)
3099 if (!IsStatic && Type.IsValueType && !container.IsSubclassOf (TypeManager.mbr_type) && DeclaringType.IsSubclassOf (TypeManager.mbr_type)) {
3100 Report.SymbolRelatedToPreviousError (DeclaringType);
3101 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);
3105 public bool VerifyFixed (bool is_expression)
3107 IVariable variable = instance_expr as IVariable;
3108 if ((variable == null) || !variable.VerifyFixed (true))
3114 public void Emit (EmitContext ec, bool leave_copy)
3116 ILGenerator ig = ec.ig;
3117 bool is_volatile = false;
3119 if (FieldInfo is FieldBuilder){
3120 FieldBase f = TypeManager.GetField (FieldInfo);
3122 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3125 f.status |= Field.Status.USED;
3129 if (FieldInfo.IsStatic){
3131 ig.Emit (OpCodes.Volatile);
3133 ig.Emit (OpCodes.Ldsfld, FieldInfo);
3139 ig.Emit (OpCodes.Volatile);
3141 IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo);
3144 ig.Emit (OpCodes.Ldflda, FieldInfo);
3145 ig.Emit (OpCodes.Ldflda, ff.Element);
3148 ig.Emit (OpCodes.Ldfld, FieldInfo);
3153 ec.ig.Emit (OpCodes.Dup);
3154 if (!FieldInfo.IsStatic) {
3155 temp = new LocalTemporary (ec, this.Type);
3161 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3163 FieldAttributes fa = FieldInfo.Attributes;
3164 bool is_static = (fa & FieldAttributes.Static) != 0;
3165 bool is_readonly = (fa & FieldAttributes.InitOnly) != 0;
3166 ILGenerator ig = ec.ig;
3167 prepared = prepare_for_load;
3169 if (is_readonly && !ec.IsConstructor){
3170 Report_AssignToReadonly (!is_static);
3176 if (prepare_for_load)
3177 ig.Emit (OpCodes.Dup);
3182 ec.ig.Emit (OpCodes.Dup);
3183 if (!FieldInfo.IsStatic) {
3184 temp = new LocalTemporary (ec, this.Type);
3189 if (FieldInfo is FieldBuilder){
3190 FieldBase f = TypeManager.GetField (FieldInfo);
3192 if ((f.ModFlags & Modifiers.VOLATILE) != 0)
3193 ig.Emit (OpCodes.Volatile);
3195 f.status |= Field.Status.ASSIGNED;
3200 ig.Emit (OpCodes.Stsfld, FieldInfo);
3202 ig.Emit (OpCodes.Stfld, FieldInfo);
3208 void EmitInstance (EmitContext ec)
3210 if (instance_expr.Type.IsValueType) {
3211 if (instance_expr is IMemoryLocation) {
3212 ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3214 LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3215 instance_expr.Emit (ec);
3217 t.AddressOf (ec, AddressOp.Store);
3220 instance_expr.Emit (ec);
3223 public override void Emit (EmitContext ec)
3228 public void AddressOf (EmitContext ec, AddressOp mode)
3230 ILGenerator ig = ec.ig;
3232 if (FieldInfo is FieldBuilder){
3233 FieldBase f = TypeManager.GetField (FieldInfo);
3235 if ((f.ModFlags & Modifiers.VOLATILE) != 0){
3236 Error (676, "volatile variable: can not take its address, or pass as ref/out parameter");
3240 if ((mode & AddressOp.Store) != 0)
3241 f.status |= Field.Status.ASSIGNED;
3242 if ((mode & AddressOp.Load) != 0)
3243 f.status |= Field.Status.USED;
3248 // Handle initonly fields specially: make a copy and then
3249 // get the address of the copy.
3252 if (FieldInfo.IsInitOnly){
3254 if (ec.IsConstructor){
3255 if (FieldInfo.IsStatic){
3267 local = ig.DeclareLocal (type);
3268 ig.Emit (OpCodes.Stloc, local);
3269 ig.Emit (OpCodes.Ldloca, local);
3274 if (FieldInfo.IsStatic){
3275 ig.Emit (OpCodes.Ldsflda, FieldInfo);
3278 ig.Emit (OpCodes.Ldflda, FieldInfo);
3284 // A FieldExpr whose address can not be taken
3286 public class FieldExprNoAddress : FieldExpr, IMemoryLocation {
3287 public FieldExprNoAddress (FieldInfo fi, Location loc) : base (fi, loc)
3291 public new void AddressOf (EmitContext ec, AddressOp mode)
3293 Report.Error (-215, "Report this: Taking the address of a remapped parameter not supported");
3298 /// Expression that evaluates to a Property. The Assign class
3299 /// might set the `Value' expression if we are in an assignment.
3301 /// This is not an LValue because we need to re-write the expression, we
3302 /// can not take data from the stack and store it.
3304 public class PropertyExpr : ExpressionStatement, IAssignMethod, IMemberExpr {
3305 public readonly PropertyInfo PropertyInfo;
3308 // This is set externally by the `BaseAccess' class
3311 MethodInfo getter, setter;
3314 Expression instance_expr;
3315 LocalTemporary temp;
3318 internal static PtrHashtable AccessorTable = new PtrHashtable ();
3320 public PropertyExpr (EmitContext ec, PropertyInfo pi, Location l)
3323 eclass = ExprClass.PropertyAccess;
3327 type = TypeManager.TypeToCoreType (pi.PropertyType);
3329 ResolveAccessors (ec);
3332 public string Name {
3334 return PropertyInfo.Name;
3338 public bool IsInstance {
3344 public bool IsStatic {
3350 public Type DeclaringType {
3352 return PropertyInfo.DeclaringType;
3357 // The instance expression associated with this expression
3359 public Expression InstanceExpression {
3361 instance_expr = value;
3365 return instance_expr;
3369 public bool VerifyAssignable ()
3371 if (setter == null) {
3372 Report.Error (200, loc,
3373 "The property `" + PropertyInfo.Name +
3374 "' can not be assigned to, as it has not set accessor");
3381 void FindAccessors (Type invocation_type)
3383 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
3384 BindingFlags.Static | BindingFlags.Instance |
3385 BindingFlags.DeclaredOnly;
3387 Type current = PropertyInfo.DeclaringType;
3388 for (; current != null; current = current.BaseType) {
3389 MemberInfo[] group = TypeManager.MemberLookup (
3390 invocation_type, invocation_type, current,
3391 MemberTypes.Property, flags, PropertyInfo.Name, null);
3396 if (group.Length != 1)
3397 // Oooops, can this ever happen ?
3400 PropertyInfo pi = (PropertyInfo) group [0];
3403 getter = pi.GetGetMethod (true);
3406 setter = pi.GetSetMethod (true);
3408 MethodInfo accessor = getter != null ? getter : setter;
3410 if (!accessor.IsVirtual)
3416 // We also perform the permission checking here, as the PropertyInfo does not
3417 // hold the information for the accessibility of its setter/getter
3419 void ResolveAccessors (EmitContext ec)
3421 FindAccessors (ec.ContainerType);
3423 if (getter != null) {
3424 AccessorTable [getter] = PropertyInfo;
3425 is_static = getter.IsStatic;
3428 if (setter != null) {
3429 AccessorTable [setter] = PropertyInfo;
3430 is_static = setter.IsStatic;
3434 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
3436 if ((instance_expr == null) && ec.IsStatic && !is_static) {
3437 SimpleName.Error_ObjectRefRequired (ec, loc, PropertyInfo.Name);
3441 if (instance_expr != null) {
3442 instance_expr = instance_expr.DoResolve (ec);
3443 if (instance_expr == null)
3446 instance_expr.CheckMarshallByRefAccess (ec.ContainerType);
3449 if (must_do_cs1540_check && (instance_expr != null)) {
3450 if ((instance_expr.Type != ec.ContainerType) &&
3451 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
3452 Report.Error (1540, loc, "Cannot access protected member `" +
3453 PropertyInfo.DeclaringType + "." + PropertyInfo.Name +
3454 "' via a qualifier of type `" +
3455 TypeManager.CSharpName (instance_expr.Type) +
3456 "'; the qualifier must be of type `" +
3457 TypeManager.CSharpName (ec.ContainerType) +
3458 "' (or derived from it)");
3466 override public Expression DoResolve (EmitContext ec)
3468 if (getter != null){
3469 if (TypeManager.GetArgumentTypes (getter).Length != 0){
3471 117, loc, "`{0}' does not contain a " +
3472 "definition for `{1}'.", getter.DeclaringType,
3478 if (getter == null){
3480 // The following condition happens if the PropertyExpr was
3481 // created, but is invalid (ie, the property is inaccessible),
3482 // and we did not want to embed the knowledge about this in
3483 // the caller routine. This only avoids double error reporting.
3488 Report.Error (154, loc,
3489 "The property `" + PropertyInfo.Name +
3490 "' can not be used in " +
3491 "this context because it lacks a get accessor");
3495 bool must_do_cs1540_check;
3496 if (!IsAccessorAccessible (ec.ContainerType, getter, out must_do_cs1540_check)) {
3497 Report.Error (122, loc, "'{0}.get' is inaccessible due to its protection level", PropertyInfo.Name);
3501 if (!InstanceResolve (ec, must_do_cs1540_check))
3505 // Only base will allow this invocation to happen.
3507 if (IsBase && getter.IsAbstract){
3508 Report.Error (205, loc, "Cannot call an abstract base property: " +
3509 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3513 if (PropertyInfo.PropertyType.IsPointer && !ec.InUnsafe){
3521 override public Expression DoResolveLValue (EmitContext ec, Expression right_side)
3523 if (setter == null){
3525 // The following condition happens if the PropertyExpr was
3526 // created, but is invalid (ie, the property is inaccessible),
3527 // and we did not want to embed the knowledge about this in
3528 // the caller routine. This only avoids double error reporting.
3533 // TODO: Print better property name
3534 Report.Error (200, loc, "Property or indexer '{0}' cannot be assigned to -- it is read only",
3539 if (TypeManager.GetArgumentTypes (setter).Length != 1){
3541 117, loc, "`{0}' does not contain a " +
3542 "definition for `{1}'.", getter.DeclaringType,
3547 bool must_do_cs1540_check;
3548 if (!IsAccessorAccessible (ec.ContainerType, setter, out must_do_cs1540_check)) {
3549 Report.Error (122, loc, "'{0}.set' is inaccessible due to its protection level", PropertyInfo.Name);
3553 if (!InstanceResolve (ec, must_do_cs1540_check))
3557 // Only base will allow this invocation to happen.
3559 if (IsBase && setter.IsAbstract){
3560 Report.Error (205, loc, "Cannot call an abstract base property: " +
3561 PropertyInfo.DeclaringType + "." +PropertyInfo.Name);
3566 // Check that we are not making changes to a temporary memory location
3568 if (instance_expr != null && instance_expr.Type.IsValueType && !(instance_expr is IMemoryLocation)) {
3569 // FIXME: Provide better error reporting.
3570 Error (1612, "Cannot modify expression because it is not a variable.");
3579 public override void Emit (EmitContext ec)
3584 void EmitInstance (EmitContext ec)
3589 if (instance_expr.Type.IsValueType) {
3590 if (instance_expr is IMemoryLocation) {
3591 ((IMemoryLocation) instance_expr).AddressOf (ec, AddressOp.LoadStore);
3593 LocalTemporary t = new LocalTemporary (ec, instance_expr.Type);
3594 instance_expr.Emit (ec);
3596 t.AddressOf (ec, AddressOp.Store);
3599 instance_expr.Emit (ec);
3602 ec.ig.Emit (OpCodes.Dup);
3606 public void Emit (EmitContext ec, bool leave_copy)
3612 // Special case: length of single dimension array property is turned into ldlen
3614 if ((getter == TypeManager.system_int_array_get_length) ||
3615 (getter == TypeManager.int_array_get_length)){
3616 Type iet = instance_expr.Type;
3619 // System.Array.Length can be called, but the Type does not
3620 // support invoking GetArrayRank, so test for that case first
3622 if (iet != TypeManager.array_type && (iet.GetArrayRank () == 1)) {
3623 ec.ig.Emit (OpCodes.Ldlen);
3624 ec.ig.Emit (OpCodes.Conv_I4);
3629 Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), getter, null, loc);
3634 ec.ig.Emit (OpCodes.Dup);
3636 temp = new LocalTemporary (ec, this.Type);
3642 // Implements the IAssignMethod interface for assignments
3644 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
3646 prepared = prepare_for_load;
3652 ec.ig.Emit (OpCodes.Dup);
3654 temp = new LocalTemporary (ec, this.Type);
3659 ArrayList args = new ArrayList (1);
3660 args.Add (new Argument (new EmptyAddressOf (), Argument.AType.Expression));
3662 Invocation.EmitCall (ec, IsBase, IsStatic, new EmptyAddressOf (), setter, args, loc);
3668 override public void EmitStatement (EmitContext ec)
3671 ec.ig.Emit (OpCodes.Pop);
3676 /// Fully resolved expression that evaluates to an Event
3678 public class EventExpr : Expression, IMemberExpr {
3679 public readonly EventInfo EventInfo;
3680 Expression instance_expr;
3683 MethodInfo add_accessor, remove_accessor;
3685 public EventExpr (EventInfo ei, Location loc)
3689 eclass = ExprClass.EventAccess;
3691 add_accessor = TypeManager.GetAddMethod (ei);
3692 remove_accessor = TypeManager.GetRemoveMethod (ei);
3694 if (add_accessor.IsStatic || remove_accessor.IsStatic)
3697 if (EventInfo is MyEventBuilder){
3698 MyEventBuilder eb = (MyEventBuilder) EventInfo;
3699 type = eb.EventType;
3702 type = EventInfo.EventHandlerType;
3705 public string Name {
3707 return EventInfo.Name;
3711 public bool IsInstance {
3717 public bool IsStatic {
3723 public Type DeclaringType {
3725 return EventInfo.DeclaringType;
3729 public Expression InstanceExpression {
3731 return instance_expr;
3735 instance_expr = value;
3739 bool InstanceResolve (EmitContext ec, bool must_do_cs1540_check)
3741 if ((instance_expr == null) && ec.IsStatic && !is_static) {
3742 SimpleName.Error_ObjectRefRequired (ec, loc, EventInfo.Name);
3746 if (instance_expr != null) {
3747 instance_expr = instance_expr.DoResolve (ec);
3748 if (instance_expr == null)
3753 // This is using the same mechanism as the CS1540 check in PropertyExpr.
3754 // However, in the Event case, we reported a CS0122 instead.
3756 if (must_do_cs1540_check && (instance_expr != null)) {
3757 if ((instance_expr.Type != ec.ContainerType) &&
3758 ec.ContainerType.IsSubclassOf (instance_expr.Type)) {
3759 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3760 DeclaringType.Name + "." + EventInfo.Name);
3769 public override Expression DoResolveLValue (EmitContext ec, Expression right_side)
3771 return DoResolve (ec);
3774 public override Expression DoResolve (EmitContext ec)
3776 if (instance_expr != null) {
3777 instance_expr = instance_expr.DoResolve (ec);
3778 if (instance_expr == null)
3782 bool must_do_cs1540_check;
3783 if (!(IsAccessorAccessible (ec.ContainerType, add_accessor, out must_do_cs1540_check)
3784 && IsAccessorAccessible (ec.ContainerType, remove_accessor, out must_do_cs1540_check))) {
3786 Report.Error (122, loc, "'{0}' is inaccessible due to its protection level",
3787 DeclaringType.Name + "." + EventInfo.Name);
3791 if (!InstanceResolve (ec, must_do_cs1540_check))
3797 public override void Emit (EmitContext ec)
3799 if (instance_expr is This)
3800 Report.Error (79, loc, "The event `{0}' can only appear on the left hand side of += or -=, try calling the actual delegate", Name);
3802 Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= "+
3803 "(except on the defining type)", Name);
3806 public void EmitAddOrRemove (EmitContext ec, Expression source)
3808 BinaryDelegate source_del = (BinaryDelegate) source;
3809 Expression handler = source_del.Right;
3811 Argument arg = new Argument (handler, Argument.AType.Expression);
3812 ArrayList args = new ArrayList ();
3816 if (source_del.IsAddition)
3817 Invocation.EmitCall (
3818 ec, false, IsStatic, instance_expr, add_accessor, args, loc);
3820 Invocation.EmitCall (
3821 ec, false, IsStatic, instance_expr, remove_accessor, args, loc);