2 // ecore.cs: Core of the Expression representation for the intermediate tree.
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright 2001, 2002, 2003 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 // Copyright 2011-2012 Xamarin Inc.
15 using System.Collections.Generic;
17 using SLE = System.Linq.Expressions;
21 using IKVM.Reflection;
22 using IKVM.Reflection.Emit;
24 using System.Reflection;
25 using System.Reflection.Emit;
28 namespace Mono.CSharp {
31 /// The ExprClass class contains the is used to pass the
32 /// classification of an expression (value, variable, namespace,
33 /// type, method group, property access, event access, indexer access,
36 public enum ExprClass : byte {
52 /// This is used to tell Resolve in which types of expressions we're
56 public enum ResolveFlags {
57 // Returns Value, Variable, PropertyAccess, EventAccess or IndexerAccess.
60 // Returns a type expression.
63 // Returns a method group.
66 TypeParameter = 1 << 3,
68 // Mask of all the expression class flags.
69 MaskExprClass = VariableOrValue | Type | MethodGroup | TypeParameter,
73 // This is just as a hint to AddressOf of what will be done with the
76 public enum AddressOp {
83 /// This interface is implemented by variables
85 public interface IMemoryLocation {
87 /// The AddressOf method should generate code that loads
88 /// the address of the object and leaves it on the stack.
90 /// The `mode' argument is used to notify the expression
91 /// of whether this will be used to read from the address or
92 /// write to the address.
94 /// This is just a hint that can be used to provide good error
95 /// reporting, and should have no other side effects.
97 void AddressOf (EmitContext ec, AddressOp mode);
101 // An expressions resolved as a direct variable reference
103 public interface IVariableReference : IFixedExpression
105 bool IsHoisted { get; }
107 VariableInfo VariableInfo { get; }
109 void SetHasAddressTaken ();
113 // Implemented by an expression which could be or is always
116 public interface IFixedExpression
118 bool IsFixed { get; }
121 public interface IExpressionCleanup
123 void EmitCleanup (EmitContext ec);
127 /// Base class for expressions
129 public abstract class Expression
131 public ExprClass eclass;
132 protected TypeSpec type;
133 protected Location loc;
135 public TypeSpec Type {
137 set { type = value; }
140 public virtual bool IsSideEffectFree {
146 public Location Location {
150 public virtual bool IsNull {
157 // Used to workaround parser limitation where we cannot get
158 // start of statement expression location
160 public virtual Location StartLocation {
166 public virtual MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
169 // Return method-group expression when the expression can be used as
170 // lambda replacement. A good example is array sorting where instead of
173 // Array.Sort (s, (a, b) => String.Compare (a, b));
175 // we can use method group directly
177 // Array.Sort (s, String.Compare);
179 // Correct overload will be used because we do the reduction after
180 // best candidate was found.
186 // Returns true when the expression during Emit phase breaks stack
187 // by using await expression
189 public virtual bool ContainsEmitWithAwait ()
195 /// Performs semantic analysis on the Expression
199 /// The Resolve method is invoked to perform the semantic analysis
202 /// The return value is an expression (it can be the
203 /// same expression in some cases) or a new
204 /// expression that better represents this node.
206 /// For example, optimizations of Unary (LiteralInt)
207 /// would return a new LiteralInt with a negated
210 /// If there is an error during semantic analysis,
211 /// then an error should be reported (using Report)
212 /// and a null value should be returned.
214 /// There are two side effects expected from calling
215 /// Resolve(): the the field variable "eclass" should
216 /// be set to any value of the enumeration
217 /// `ExprClass' and the type variable should be set
218 /// to a valid type (this is the type of the
221 protected abstract Expression DoResolve (ResolveContext rc);
223 public virtual Expression DoResolveLValue (ResolveContext rc, Expression right_side)
229 // This is used if the expression should be resolved as a type or namespace name.
230 // the default implementation fails.
232 public virtual TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
234 var rc = mc as ResolveContext ?? new ResolveContext (mc);
235 Expression e = Resolve (rc);
237 e.Error_UnexpectedKind (rc, ResolveFlags.Type, loc);
242 protected void CheckExpressionVariable (ResolveContext rc)
244 if (rc.HasAny (ResolveContext.Options.BaseInitializer | ResolveContext.Options.FieldInitializerScope)) {
245 rc.Report.Error (8200, loc, "Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers");
246 } else if (rc.HasSet (ResolveContext.Options.QueryClauseScope)) {
247 rc.Report.Error (8201, loc, "Out variable and pattern variable declarations are not allowed within a query clause");
251 public static void ErrorIsInaccesible (IMemberContext rc, string member, Location loc)
253 rc.Module.Compiler.Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", member);
256 public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name)
258 rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
261 public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, TypeSpec type, Location loc, string name)
263 rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
264 name, type.GetSignatureForError ());
267 protected virtual void Error_InvalidExpressionStatement (Report report, Location loc)
269 report.Error (201, loc, "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement");
272 public void Error_InvalidExpressionStatement (BlockContext bc)
274 Error_InvalidExpressionStatement (bc.Report, loc);
277 public void Error_InvalidExpressionStatement (Report report)
279 Error_InvalidExpressionStatement (report, loc);
282 public static void Error_VoidInvalidInTheContext (Location loc, Report Report)
284 Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
287 public virtual void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
289 Error_ValueCannotBeConvertedCore (ec, loc, target, expl);
292 protected void Error_ValueCannotBeConvertedCore (ResolveContext ec, Location loc, TypeSpec target, bool expl)
294 // The error was already reported as CS1660
295 if (type == InternalType.AnonymousMethod)
298 if (type == InternalType.ErrorType || target == InternalType.ErrorType)
301 if (type.MemberDefinition.DeclaringAssembly.IsMissing ||
302 target.MemberDefinition.DeclaringAssembly.IsMissing)
305 string from_type = type.GetSignatureForError ();
306 string to_type = target.GetSignatureForError ();
307 if (from_type == to_type) {
308 from_type = type.GetSignatureForErrorIncludingAssemblyName ();
309 to_type = target.GetSignatureForErrorIncludingAssemblyName ();
313 ec.Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
318 ec.Report.DisableReporting ();
319 bool expl_exists = Convert.ExplicitConversion (ec, this, target, Location.Null) != null;
320 ec.Report.EnableReporting ();
323 ec.Report.Error (266, loc,
324 "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
327 ec.Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
332 public void Error_TypeArgumentsCannotBeUsed (IMemberContext context, MemberSpec member, Location loc)
334 // Better message for possible generic expressions
335 if (member != null && (member.Kind & MemberKind.GenericMask) != 0) {
336 var report = context.Module.Compiler.Report;
337 report.SymbolRelatedToPreviousError (member);
338 if (member is TypeSpec)
339 member = ((TypeSpec) member).GetDefinition ();
341 member = ((MethodSpec) member).GetGenericMethodDefinition ();
343 string name = member.Kind == MemberKind.Method ? "method" : "type";
344 if (member.IsGeneric) {
345 report.Error (305, loc, "Using the generic {0} `{1}' requires `{2}' type argument(s)",
346 name, member.GetSignatureForError (), member.Arity.ToString ());
348 report.Error (308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
349 name, member.GetSignatureForError ());
352 Error_TypeArgumentsCannotBeUsed (context, ExprClassName, GetSignatureForError (), loc);
356 public static void Error_TypeArgumentsCannotBeUsed (IMemberContext context, string exprType, string name, Location loc)
358 context.Module.Compiler.Report.Error (307, loc, "The {0} `{1}' cannot be used with type arguments",
362 public virtual void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
364 Error_TypeDoesNotContainDefinition (ec, loc, type, name);
367 public static void Error_TypeDoesNotContainDefinition (ResolveContext ec, Location loc, TypeSpec type, string name)
369 ec.Report.SymbolRelatedToPreviousError (type);
370 ec.Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
371 type.GetSignatureForError (), name);
374 public virtual void Error_ValueAssignment (ResolveContext rc, Expression rhs)
376 if (rhs == EmptyExpression.LValueMemberAccess || rhs == EmptyExpression.LValueMemberOutAccess) {
377 // Already reported as CS1612
378 } else if (rhs == EmptyExpression.OutAccess) {
379 rc.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
381 rc.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
385 protected void Error_VoidPointerOperation (ResolveContext rc)
387 rc.Report.Error (242, loc, "The operation in question is undefined on void pointers");
390 public static void Warning_UnreachableExpression (ResolveContext rc, Location loc)
392 rc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
395 public ResolveFlags ExprClassToResolveFlags {
399 case ExprClass.Namespace:
400 return ResolveFlags.Type;
402 case ExprClass.MethodGroup:
403 return ResolveFlags.MethodGroup;
405 case ExprClass.TypeParameter:
406 return ResolveFlags.TypeParameter;
408 case ExprClass.Value:
409 case ExprClass.Variable:
410 case ExprClass.PropertyAccess:
411 case ExprClass.EventAccess:
412 case ExprClass.IndexerAccess:
413 return ResolveFlags.VariableOrValue;
416 throw new InternalErrorException (loc.ToString () + " " + GetType () + " ExprClass is Invalid after resolve");
422 // Implements identical simple name and type-name resolution
424 public Expression ProbeIdenticalTypeName (ResolveContext rc, Expression left, SimpleName name)
427 if (t.Kind == MemberKind.InternalCompilerType || t is ElementTypeSpec || t.Arity > 0)
430 // In a member access of the form E.I, if E is a single identifier, and if the meaning of E as a simple-name is
431 // a constant, field, property, local variable, or parameter with the same type as the meaning of E as a type-name
433 if (left is MemberExpr || left is VariableReference) {
434 var identical_type = rc.LookupNamespaceOrType (name.Name, 0, LookupMode.Probing, loc) as TypeExpr;
435 if (identical_type != null && identical_type.Type == left.Type)
436 return identical_type;
442 public virtual string GetSignatureForError ()
444 return type.GetDefinition ().GetSignatureForError ();
447 public static bool IsNeverNull (Expression expr)
449 if (expr is This || expr is New || expr is ArrayCreation || expr is DelegateCreation || expr is ConditionalMemberAccess)
452 var c = expr as Constant;
456 var tc = expr as TypeCast;
458 return IsNeverNull (tc.Child);
463 protected static bool IsNullPropagatingValid (TypeSpec type)
466 case MemberKind.Struct:
467 return type.IsNullableType;
468 case MemberKind.Enum:
469 case MemberKind.Void:
470 case MemberKind.PointerType:
472 case MemberKind.InternalCompilerType:
473 return type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
474 case MemberKind.TypeParameter:
475 return !((TypeParameterSpec) type).IsValueType;
481 public virtual bool HasConditionalAccess ()
486 protected TypeSpec LiftMemberType (ResolveContext rc, TypeSpec type)
488 var tps = type as TypeParameterSpec;
489 if (tps != null && !(tps.IsReferenceType || tps.IsValueType)) {
490 Error_OperatorCannotBeApplied (rc, loc, "?", type);
493 return TypeSpec.IsValueType (type) && !type.IsNullableType ?
494 Nullable.NullableInfo.MakeType (rc.Module, type) :
499 /// Resolves an expression and performs semantic analysis on it.
503 /// Currently Resolve wraps DoResolve to perform sanity
504 /// checking and assertion checking on what we expect from Resolve.
506 public Expression Resolve (ResolveContext ec, ResolveFlags flags)
508 if (eclass != ExprClass.Unresolved) {
509 if ((flags & ExprClassToResolveFlags) == 0) {
510 Error_UnexpectedKind (ec, flags, loc);
524 if ((flags & e.ExprClassToResolveFlags) == 0) {
525 e.Error_UnexpectedKind (ec, flags, loc);
530 throw new InternalErrorException ("Expression `{0}' didn't set its type in DoResolve", e.GetType ());
533 } catch (Exception ex) {
534 if (loc.IsNull || ec.Module.Compiler.Settings.BreakOnInternalError || ex is CompletionResult || ec.Report.IsDisabled || ex is FatalException ||
535 ec.Report.Printer is NullReportPrinter)
538 ec.Report.Error (584, loc, "Internal compiler error: {0}", ex.Message);
539 return ErrorExpression.Instance; // TODO: Add location
544 /// Resolves an expression and performs semantic analysis on it.
546 public Expression Resolve (ResolveContext rc)
548 return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
552 /// Resolves an expression for LValue assignment
556 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
557 /// checking and assertion checking on what we expect from Resolve
559 public Expression ResolveLValue (ResolveContext ec, Expression right_side)
561 int errors = ec.Report.Errors;
562 bool out_access = right_side == EmptyExpression.OutAccess;
564 Expression e = DoResolveLValue (ec, right_side);
566 if (e != null && out_access && !(e is IMemoryLocation)) {
567 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
568 // Enabling this 'throw' will "only" result in deleting useless code elsewhere,
570 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
571 // e.GetType () + " " + e.GetSignatureForError ());
576 if (errors == ec.Report.Errors) {
577 Error_ValueAssignment (ec, right_side);
582 if (e.eclass == ExprClass.Unresolved)
583 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
585 if ((e.type == null) && !(e is GenericTypeExpr))
586 throw new Exception ("Expression " + e + " did not set its type after Resolve");
591 public Constant ResolveLabelConstant (ResolveContext rc)
593 var expr = Resolve (rc);
597 Constant c = expr as Constant;
599 if (expr.type != InternalType.ErrorType)
600 rc.Report.Error (150, expr.StartLocation, "A constant value is expected");
608 public virtual void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
610 if (Attribute.IsValidArgumentType (parameterType)) {
611 rc.Module.Compiler.Report.Error (182, loc,
612 "An attribute argument must be a constant expression, typeof expression or array creation expression");
614 rc.Module.Compiler.Report.Error (181, loc,
615 "Attribute constructor parameter has type `{0}', which is not a valid attribute parameter type",
616 targetType.GetSignatureForError ());
621 /// Emits the code for the expression
625 /// The Emit method is invoked to generate the code
626 /// for the expression.
628 public abstract void Emit (EmitContext ec);
631 // Emit code to branch to @target if this expression is equivalent to @on_true.
632 // The default implementation is to emit the value, and then emit a brtrue or brfalse.
633 // Subclasses can provide more efficient implementations, but those MUST be equivalent,
634 // including the use of conditional branches. Note also that a branch MUST be emitted
635 public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
638 ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
641 // Emit this expression for its side effects, not for its value.
642 // The default implementation is to emit the value, and then throw it away.
643 // Subclasses can provide more efficient implementations, but those MUST be equivalent
644 public virtual void EmitSideEffect (EmitContext ec)
647 ec.Emit (OpCodes.Pop);
650 public virtual void EmitPrepare (EmitContext ec)
655 // Emits the expression into temporary field variable. The method
656 // should be used for await expressions only
658 public virtual Expression EmitToField (EmitContext ec)
661 // This is the await prepare Emit method. When emitting code like
662 // a + b we emit code like
668 // For await a + await b we have to interfere the flow to keep the
669 // stack clean because await yields from the expression. The emit
672 // a = a.EmitToField () // a is changed to temporary field access
673 // b = b.EmitToField ()
679 // The idea is to emit expression and leave the stack empty with
680 // result value still available.
682 // Expressions should override this default implementation when
683 // optimized version can be provided (e.g. FieldExpr)
686 // We can optimize for side-effect free expressions, they can be
687 // emitted out of order
689 if (IsSideEffectFree)
692 bool needs_temporary = ContainsEmitWithAwait ();
693 if (!needs_temporary)
696 // Emit original code
697 var field = EmitToFieldSource (ec);
700 // Store the result to temporary field when we
701 // cannot load `this' directly
703 field = ec.GetTemporaryField (type);
704 if (needs_temporary) {
706 // Create temporary local (we cannot load `this' before Emit)
708 var temp = ec.GetTemporaryLocal (type);
709 ec.Emit (OpCodes.Stloc, temp);
712 ec.Emit (OpCodes.Ldloc, temp);
713 field.EmitAssignFromStack (ec);
715 ec.FreeTemporaryLocal (temp, type);
717 field.EmitAssignFromStack (ec);
724 protected virtual FieldExpr EmitToFieldSource (EmitContext ec)
727 // Default implementation calls Emit method
733 protected static void EmitExpressionsList (EmitContext ec, List<Expression> expressions)
735 if (ec.HasSet (BuilderContext.Options.AsyncBody)) {
736 bool contains_await = false;
738 for (int i = 1; i < expressions.Count; ++i) {
739 if (expressions[i].ContainsEmitWithAwait ()) {
740 contains_await = true;
745 if (contains_await) {
746 for (int i = 0; i < expressions.Count; ++i) {
747 expressions[i] = expressions[i].EmitToField (ec);
752 for (int i = 0; i < expressions.Count; ++i) {
753 expressions[i].Emit (ec);
758 /// Protected constructor. Only derivate types should
759 /// be able to be created
762 protected Expression ()
767 /// Returns a fully formed expression after a MemberLookup
770 static Expression ExprClassFromMemberInfo (MemberSpec spec, Location loc)
772 if (spec is EventSpec)
773 return new EventExpr ((EventSpec) spec, loc);
774 if (spec is ConstSpec)
775 return new ConstantExpr ((ConstSpec) spec, loc);
776 if (spec is FieldSpec)
777 return new FieldExpr ((FieldSpec) spec, loc);
778 if (spec is PropertySpec)
779 return new PropertyExpr ((PropertySpec) spec, loc);
780 if (spec is TypeSpec)
781 return new TypeExpression (((TypeSpec) spec), loc);
786 public static MethodSpec ConstructorLookup (ResolveContext rc, TypeSpec type, ref Arguments args, Location loc)
788 var ctors = MemberCache.FindMembers (type, Constructor.ConstructorName, true);
791 case MemberKind.Struct:
792 // Every struct has implicit default constructor if not provided by user
796 rc.Report.SymbolRelatedToPreviousError (type);
797 // Report meaningful error for struct as they always have default ctor in C# context
798 OverloadResolver.Error_ConstructorMismatch (rc, type, args == null ? 0 : args.Count, loc);
800 case MemberKind.MissingType:
801 case MemberKind.InternalCompilerType:
802 // LAMESPEC: dynamic is not really object
803 // if (type.BuiltinType == BuiltinTypeSpec.Type.Object)
807 rc.Report.SymbolRelatedToPreviousError (type);
808 rc.Report.Error (143, loc, "The class `{0}' has no constructors defined",
809 type.GetSignatureForError ());
816 if (args == null && type.IsStruct) {
817 bool includes_empty = false;
818 foreach (MethodSpec ctor in ctors) {
819 if (ctor.Parameters.IsEmpty) {
820 includes_empty = true;
828 var r = new OverloadResolver (ctors, OverloadResolver.Restrictions.NoBaseMembers, loc);
829 if (!rc.HasSet (ResolveContext.Options.BaseInitializer)) {
830 r.InstanceQualifier = new ConstructorInstanceQualifier (type);
833 return r.ResolveMember<MethodSpec> (rc, ref args);
837 public enum MemberLookupRestrictions
843 EmptyArguments = 1 << 4,
844 IgnoreArity = 1 << 5,
845 IgnoreAmbiguity = 1 << 6,
846 NameOfExcluded = 1 << 7,
847 DontSetConditionalAccess = 1 << 8
851 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
852 // `qualifier_type' or null to lookup members in the current class.
854 public static Expression MemberLookup (IMemberContext rc, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
856 var members = MemberCache.FindMembers (queried_type, name, false);
858 if (members != null) {
861 expr = MemberLookupToExpression (rc, members, errorMode, queried_type, name, arity, restrictions, loc);
865 if (members [0].DeclaringType.BaseType == null)
868 members = MemberCache.FindMembers (members [0].DeclaringType.BaseType, name, false);
869 } while (members != null);
872 var tps = queried_type as TypeParameterSpec;
874 members = MemberCache.FindInterfaceMembers (tps, name);
876 return MemberLookupToExpression (rc, members, errorMode, queried_type, name, arity, restrictions, loc);
879 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0) {
880 var ntuple = queried_type as NamedTupleSpec;
881 if (ntuple != null) {
882 var ms = ntuple.FindElement (rc, name, loc);
884 return ExprClassFromMemberInfo (ms, loc);
891 public static Expression MemberLookupToExpression (IMemberContext rc, IList<MemberSpec> members, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
893 MemberSpec non_method = null;
894 MemberSpec ambig_non_method = null;
896 for (int i = 0; i < members.Count; ++i) {
897 var member = members [i];
899 // HACK: for events because +=/-= can appear at same class only, should use OverrideToBase there
900 if ((member.Modifiers & Modifiers.OVERRIDE) != 0 && member.Kind != MemberKind.Event)
903 if ((member.Modifiers & Modifiers.BACKING_FIELD) != 0 || member.Kind == MemberKind.Operator)
906 if ((arity > 0 || (restrictions & MemberLookupRestrictions.ExactArity) != 0) && member.Arity != arity)
910 if (!member.IsAccessible (rc))
914 // With runtime binder we can have a situation where queried type is inaccessible
915 // because it came via dynamic object, the check about inconsisted accessibility
916 // had no effect as the type was unknown during compilation
919 // private class N { }
921 // public dynamic Foo ()
927 if (rc.Module.Compiler.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
931 if ((restrictions & MemberLookupRestrictions.InvocableOnly) != 0) {
932 if (member is MethodSpec) {
933 return new MethodGroupExpr (members, queried_type, loc);
936 if (!Invocation.IsMemberInvocable (member))
940 if (non_method == null || member is MethodSpec || non_method.IsNotCSharpCompatible) {
942 } else if (!errorMode && !member.IsNotCSharpCompatible) {
944 // Interface members that are hidden by class members are removed from the set when T is a type parameter and
945 // T has both an effective base class other than object and a non-empty effective interface set.
947 // The spec has more complex rules but we simply remove all members declared in an interface declaration.
949 var tps = queried_type as TypeParameterSpec;
950 if (tps != null && tps.HasTypeConstraint) {
951 if (non_method.DeclaringType.IsClass && member.DeclaringType.IsInterface)
954 if (non_method.DeclaringType.IsInterface && member.DeclaringType.IsInterface) {
960 ambig_non_method = member;
964 if (non_method != null) {
965 if (ambig_non_method != null && rc != null && (restrictions & MemberLookupRestrictions.IgnoreAmbiguity) == 0) {
966 var report = rc.Module.Compiler.Report;
967 report.SymbolRelatedToPreviousError (non_method);
968 report.SymbolRelatedToPreviousError (ambig_non_method);
969 report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
970 non_method.GetSignatureForError (), ambig_non_method.GetSignatureForError ());
973 if (non_method is MethodSpec)
974 return new MethodGroupExpr (members, queried_type, loc);
976 return ExprClassFromMemberInfo (non_method, loc);
982 protected static void Error_NamedArgument (NamedArgument na, Report Report)
984 Report.Error (1742, na.Location, "An element access expression cannot use named argument");
987 protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
989 throw new NotImplementedException ();
992 public virtual void Error_OperatorCannotBeApplied (ResolveContext rc, Location loc, string oper, TypeSpec t)
994 if (t == InternalType.ErrorType)
997 rc.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
998 oper, t.GetSignatureForError ());
1001 protected void Error_PointerInsideExpressionTree (ResolveContext ec)
1003 ec.Report.Error (1944, loc, "An expression tree cannot contain an unsafe pointer operation");
1006 protected void Error_NullShortCircuitInsideExpressionTree (ResolveContext rc)
1008 rc.Report.Error (8072, loc, "An expression tree cannot contain a null propagating operator");
1011 protected void Error_NullPropagatingLValue (ResolveContext rc)
1013 rc.Report.Error (-1030, loc, "The left-hand side of an assignment cannot contain a null propagating operator");
1016 public virtual void FlowAnalysis (FlowAnalysisContext fc)
1020 public virtual Reachability MarkReachable (Reachability rc)
1026 // Special version of flow analysis for expressions which can return different
1027 // on-true and on-false result. Used by &&, ||, ?: expressions
1029 public virtual void FlowAnalysisConditional (FlowAnalysisContext fc)
1032 fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment;
1036 /// Returns an expression that can be used to invoke operator true
1037 /// on the expression if it exists.
1039 protected static Expression GetOperatorTrue (ResolveContext ec, Expression e, Location loc)
1041 return GetOperatorTrueOrFalse (ec, e, true, loc);
1045 /// Returns an expression that can be used to invoke operator false
1046 /// on the expression if it exists.
1048 protected static Expression GetOperatorFalse (ResolveContext ec, Expression e, Location loc)
1050 return GetOperatorTrueOrFalse (ec, e, false, loc);
1053 static Expression GetOperatorTrueOrFalse (ResolveContext ec, Expression e, bool is_true, Location loc)
1055 var op = is_true ? Operator.OpType.True : Operator.OpType.False;
1057 if (type.IsNullableType)
1058 type = Nullable.NullableInfo.GetUnderlyingType (type);
1060 var methods = MemberCache.GetUserOperator (type, op, false);
1061 if (methods == null)
1064 Arguments arguments = new Arguments (1);
1065 arguments.Add (new Argument (e));
1067 var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
1068 var oper = res.ResolveOperator (ec, ref arguments);
1073 return new UserOperatorCall (oper, arguments, null, loc);
1076 public virtual string ExprClassName
1080 case ExprClass.Unresolved:
1081 return "Unresolved";
1082 case ExprClass.Value:
1084 case ExprClass.Variable:
1086 case ExprClass.Namespace:
1088 case ExprClass.Type:
1090 case ExprClass.MethodGroup:
1091 return "method group";
1092 case ExprClass.PropertyAccess:
1093 return "property access";
1094 case ExprClass.EventAccess:
1095 return "event access";
1096 case ExprClass.IndexerAccess:
1097 return "indexer access";
1098 case ExprClass.Nothing:
1100 case ExprClass.TypeParameter:
1101 return "type parameter";
1103 throw new Exception ("Should not happen");
1108 /// Reports that we were expecting `expr' to be of class `expected'
1110 public static void Error_UnexpectedKind (IMemberContext ctx, Expression memberExpr, string expected, string was, Location loc)
1112 var name = memberExpr.GetSignatureForError ();
1114 ctx.Module.Compiler.Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected", name, was, expected);
1117 public virtual void Error_UnexpectedKind (ResolveContext ec, ResolveFlags flags, Location loc)
1119 string [] valid = new string [4];
1122 if ((flags & ResolveFlags.VariableOrValue) != 0) {
1123 valid [count++] = "variable";
1124 valid [count++] = "value";
1127 if ((flags & ResolveFlags.Type) != 0)
1128 valid [count++] = "type";
1130 if ((flags & ResolveFlags.MethodGroup) != 0)
1131 valid [count++] = "method group";
1134 valid [count++] = "unknown";
1136 StringBuilder sb = new StringBuilder (valid [0]);
1137 for (int i = 1; i < count - 1; i++) {
1139 sb.Append (valid [i]);
1142 sb.Append ("' or `");
1143 sb.Append (valid [count - 1]);
1146 ec.Report.Error (119, loc,
1147 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
1150 public static void UnsafeError (ResolveContext ec, Location loc)
1152 UnsafeError (ec.Report, loc);
1155 public static void UnsafeError (Report Report, Location loc)
1157 Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
1160 public static void UnsafeInsideIteratorError (ResolveContext rc, Location loc)
1162 UnsafeInsideIteratorError (rc.Report, loc);
1165 public static void UnsafeInsideIteratorError (Report report, Location loc)
1167 report.Error (1629, loc, "Unsafe code may not appear in iterators");
1171 // Converts `source' to an int, uint, long or ulong.
1173 protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source, bool pointerArray = false)
1175 var btypes = ec.BuiltinTypes;
1177 if (source.type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1178 Arguments args = new Arguments (1);
1179 args.Add (new Argument (source));
1180 return new DynamicConversion (btypes.Int, CSharpBinderFlags.ConvertArrayIndex, args, source.loc).Resolve (ec);
1183 Expression converted;
1185 using (ec.Set (ResolveContext.Options.CheckedScope)) {
1186 converted = Convert.ImplicitConversion (ec, source, btypes.Int, source.loc);
1187 if (converted == null)
1188 converted = Convert.ImplicitConversion (ec, source, btypes.UInt, source.loc);
1189 if (converted == null)
1190 converted = Convert.ImplicitConversion (ec, source, btypes.Long, source.loc);
1191 if (converted == null)
1192 converted = Convert.ImplicitConversion (ec, source, btypes.ULong, source.loc);
1194 if (converted == null) {
1195 source.Error_ValueCannotBeConverted (ec, btypes.Int, false);
1204 // Only positive constants are allowed at compile time
1206 Constant c = converted as Constant;
1207 if (c != null && c.IsNegative)
1208 Error_NegativeArrayIndex (ec, source.loc);
1210 // No conversion needed to array index
1211 if (converted.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
1214 return new ArrayIndexCast (converted, btypes.Int).Resolve (ec);
1217 public Expression MakePointerAccess (ResolveContext rc, TypeSpec type, Arguments args)
1219 if (args.Count != 1){
1220 rc.Report.Error (196, loc, "A pointer must be indexed by only one value");
1225 if (arg is NamedArgument)
1226 Error_NamedArgument ((NamedArgument) arg, rc.Report);
1228 var index = arg.Expr.Resolve (rc);
1232 index = ConvertExpressionToArrayIndex (rc, index, true);
1234 Expression p = new PointerArithmetic (Binary.Operator.Addition, this, index, type, loc);
1235 return new Indirection (p, loc);
1239 // Derived classes implement this method by cloning the fields that
1240 // could become altered during the Resolve stage
1242 // Only expressions that are created for the parser need to implement
1245 protected virtual void CloneTo (CloneContext clonectx, Expression target)
1247 throw new NotImplementedException (
1249 "CloneTo not implemented for expression {0}", this.GetType ()));
1253 // Clones an expression created by the parser.
1255 // We only support expressions created by the parser so far, not
1256 // expressions that have been resolved (many more classes would need
1257 // to implement CloneTo).
1259 // This infrastructure is here merely for Lambda expressions which
1260 // compile the same code using different type values for the same
1261 // arguments to find the correct overload
1263 public virtual Expression Clone (CloneContext clonectx)
1265 Expression cloned = (Expression) MemberwiseClone ();
1266 CloneTo (clonectx, cloned);
1272 // Implementation of expression to expression tree conversion
1274 public abstract Expression CreateExpressionTree (ResolveContext ec);
1276 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, Arguments args)
1278 return CreateExpressionFactoryCall (ec, name, null, args, loc);
1281 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args)
1283 return CreateExpressionFactoryCall (ec, name, typeArguments, args, loc);
1286 public static Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args, Location loc)
1288 return new Invocation (new MemberAccess (CreateExpressionTypeExpression (ec, loc), name, typeArguments, loc), args);
1291 protected static TypeExpr CreateExpressionTypeExpression (ResolveContext ec, Location loc)
1293 var t = ec.Module.PredefinedTypes.Expression.Resolve ();
1297 return new TypeExpression (t, loc);
1301 // Implemented by all expressions which support conversion from
1302 // compiler expression to invokable runtime expression. Used by
1303 // dynamic C# binder.
1305 public virtual SLE.Expression MakeExpression (BuilderContext ctx)
1307 throw new NotImplementedException ("MakeExpression for " + GetType ());
1310 public virtual object Accept (StructuralVisitor visitor)
1312 return visitor.Visit (this);
1317 /// This is just a base class for expressions that can
1318 /// appear on statements (invocations, object creation,
1319 /// assignments, post/pre increment and decrement). The idea
1320 /// being that they would support an extra Emition interface that
1321 /// does not leave a result on the stack.
1323 public abstract class ExpressionStatement : Expression
1325 public virtual ExpressionStatement ResolveStatement (BlockContext ec)
1327 Expression e = Resolve (ec);
1331 ExpressionStatement es = e as ExpressionStatement;
1332 if (es == null || e is AnonymousMethodBody) {
1333 var reduced = e as IReducedExpressionStatement;
1334 if (reduced != null) {
1335 return EmptyExpressionStatement.Instance;
1338 Error_InvalidExpressionStatement (ec);
1342 // This is quite expensive warning, try to limit the damage
1344 if (MemberAccess.IsValidDotExpression (e.Type) && !(e is Assign || e is Await)) {
1345 WarningAsyncWithoutWait (ec, e);
1351 static void WarningAsyncWithoutWait (BlockContext bc, Expression e)
1353 if (bc.CurrentAnonymousMethod is AsyncInitializer) {
1354 var awaiter = new AwaitStatement.AwaitableMemberAccess (e) {
1359 // Need to do full resolve because GetAwaiter can be extension method
1360 // available only in this context
1362 var mg = awaiter.Resolve (bc) as MethodGroupExpr;
1366 var arguments = new Arguments (0);
1367 mg = mg.OverloadResolve (bc, ref arguments, null, OverloadResolver.Restrictions.ProbingOnly);
1372 // Use same check rules as for real await
1374 var awaiter_definition = bc.Module.GetAwaiter (mg.BestCandidateReturnType);
1375 if (!awaiter_definition.IsValidPattern || !awaiter_definition.INotifyCompletion)
1378 bc.Report.Warning (4014, 1, e.Location,
1379 "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator");
1383 var inv = e as Invocation;
1384 if (inv != null && inv.MethodGroup != null && inv.MethodGroup.BestCandidate.IsAsync) {
1385 // The warning won't be reported for imported methods to maintain warning compatiblity with csc
1386 bc.Report.Warning (4014, 1, e.Location,
1387 "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator or calling `Wait' method");
1393 /// Requests the expression to be emitted in a `statement'
1394 /// context. This means that no new value is left on the
1395 /// stack after invoking this method (constrasted with
1396 /// Emit that will always leave a value on the stack).
1398 public abstract void EmitStatement (EmitContext ec);
1400 public override void EmitSideEffect (EmitContext ec)
1406 interface IReducedExpressionStatement
1411 /// This kind of cast is used to encapsulate the child
1412 /// whose type is child.Type into an expression that is
1413 /// reported to return "return_type". This is used to encapsulate
1414 /// expressions which have compatible types, but need to be dealt
1415 /// at higher levels with.
1417 /// For example, a "byte" expression could be encapsulated in one
1418 /// of these as an "unsigned int". The type for the expression
1419 /// would be "unsigned int".
1422 public abstract class TypeCast : Expression
1424 protected readonly Expression child;
1426 protected TypeCast (Expression child, TypeSpec return_type)
1428 eclass = child.eclass;
1429 loc = child.Location;
1434 public Expression Child {
1440 public override bool ContainsEmitWithAwait ()
1442 return child.ContainsEmitWithAwait ();
1445 public override Expression CreateExpressionTree (ResolveContext ec)
1447 Arguments args = new Arguments (2);
1448 args.Add (new Argument (child.CreateExpressionTree (ec)));
1449 args.Add (new Argument (new TypeOf (type, loc)));
1451 if (type.IsPointer || child.Type.IsPointer)
1452 Error_PointerInsideExpressionTree (ec);
1454 return CreateExpressionFactoryCall (ec, ec.HasSet (ResolveContext.Options.CheckedScope) ? "ConvertChecked" : "Convert", args);
1457 protected override Expression DoResolve (ResolveContext ec)
1459 // This should never be invoked, we are born in fully
1460 // initialized state.
1465 public override void Emit (EmitContext ec)
1470 public override void FlowAnalysis (FlowAnalysisContext fc)
1472 child.FlowAnalysis (fc);
1475 public override SLE.Expression MakeExpression (BuilderContext ctx)
1478 return base.MakeExpression (ctx);
1480 return ctx.HasSet (BuilderContext.Options.CheckedScope) ?
1481 SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type.GetMetaInfo ()) :
1482 SLE.Expression.Convert (child.MakeExpression (ctx), type.GetMetaInfo ());
1486 public override Reachability MarkReachable (Reachability rc)
1488 return child.MarkReachable (rc);
1491 protected override void CloneTo (CloneContext clonectx, Expression t)
1496 public override bool IsNull {
1497 get { return child.IsNull; }
1501 public class EmptyCast : TypeCast {
1502 EmptyCast (Expression child, TypeSpec target_type)
1503 : base (child, target_type)
1507 public static Expression Create (Expression child, TypeSpec type)
1509 Constant c = child as Constant;
1511 var enum_constant = c as EnumConstant;
1512 if (enum_constant != null)
1513 c = enum_constant.Child;
1515 if (!(c is ReducedExpression.ReducedConstantExpression)) {
1519 var res = c.ConvertImplicitly (type);
1525 EmptyCast e = child as EmptyCast;
1527 return new EmptyCast (e.child, type);
1529 return new EmptyCast (child, type);
1532 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1534 child.EmitBranchable (ec, label, on_true);
1537 public override void EmitSideEffect (EmitContext ec)
1539 child.EmitSideEffect (ec);
1544 // Used for predefined type user operator (no obsolete check, etc.)
1546 public class OperatorCast : TypeCast
1548 readonly MethodSpec conversion_operator;
1550 public OperatorCast (Expression expr, TypeSpec target_type)
1551 : this (expr, target_type, target_type, false)
1555 public OperatorCast (Expression expr, TypeSpec target_type, bool find_explicit)
1556 : this (expr, target_type, target_type, find_explicit)
1560 public OperatorCast (Expression expr, TypeSpec declaringType, TypeSpec returnType, bool isExplicit)
1561 : base (expr, returnType)
1563 var op = isExplicit ? Operator.OpType.Explicit : Operator.OpType.Implicit;
1564 var mi = MemberCache.GetUserOperator (declaringType, op, true);
1567 foreach (MethodSpec oper in mi) {
1568 if (oper.ReturnType != returnType)
1571 if (oper.Parameters.Types[0] == expr.Type) {
1572 conversion_operator = oper;
1578 throw new InternalErrorException ("Missing predefined user operator between `{0}' and `{1}'",
1579 returnType.GetSignatureForError (), expr.Type.GetSignatureForError ());
1582 public override void Emit (EmitContext ec)
1585 ec.Emit (OpCodes.Call, conversion_operator);
1590 // Constant specialization of EmptyCast.
1591 // We need to special case this since an empty cast of
1592 // a constant is still a constant.
1594 public class EmptyConstantCast : Constant
1596 public readonly Constant child;
1598 public EmptyConstantCast (Constant child, TypeSpec type)
1599 : base (child.Location)
1602 throw new ArgumentNullException ("child");
1605 this.eclass = child.eclass;
1609 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1611 if (child.Type == target_type)
1614 // FIXME: check that 'type' can be converted to 'target_type' first
1615 return child.ConvertExplicitly (in_checked_context, target_type);
1618 public override Expression CreateExpressionTree (ResolveContext ec)
1620 Arguments args = Arguments.CreateForExpressionTree (ec, null,
1621 child.CreateExpressionTree (ec),
1622 new TypeOf (type, loc));
1625 Error_PointerInsideExpressionTree (ec);
1627 return CreateExpressionFactoryCall (ec, "Convert", args);
1630 public override bool IsDefaultValue {
1631 get { return child.IsDefaultValue; }
1634 public override bool IsNegative {
1635 get { return child.IsNegative; }
1638 public override bool IsNull {
1639 get { return child.IsNull; }
1642 public override bool IsOneInteger {
1643 get { return child.IsOneInteger; }
1646 public override bool IsSideEffectFree {
1648 return child.IsSideEffectFree;
1652 public override bool IsZeroInteger {
1653 get { return child.IsZeroInteger; }
1656 public override void Emit (EmitContext ec)
1661 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1663 child.EmitBranchable (ec, label, on_true);
1665 // Only to make verifier happy
1666 if (TypeManager.IsGenericParameter (type) && child.IsNull)
1667 ec.Emit (OpCodes.Unbox_Any, type);
1670 public override void EmitSideEffect (EmitContext ec)
1672 child.EmitSideEffect (ec);
1675 public override object GetValue ()
1677 return child.GetValue ();
1680 public override string GetValueAsLiteral ()
1682 return child.GetValueAsLiteral ();
1685 public override long GetValueAsLong ()
1687 return child.GetValueAsLong ();
1690 public override Constant ConvertImplicitly (TypeSpec target_type)
1692 if (type == target_type)
1695 // FIXME: Do we need to check user conversions?
1696 if (!Convert.ImplicitStandardConversionExists (this, target_type))
1699 return child.ConvertImplicitly (target_type);
1704 /// This class is used to wrap literals which belong inside Enums
1706 public class EnumConstant : Constant
1708 public Constant Child;
1710 public EnumConstant (Constant child, TypeSpec enum_type)
1711 : base (child.Location)
1715 this.eclass = ExprClass.Value;
1716 this.type = enum_type;
1719 protected EnumConstant (Location loc)
1724 public override void Emit (EmitContext ec)
1729 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1731 Child.EncodeAttributeValue (rc, enc, Child.Type, parameterType);
1734 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1736 Child.EmitBranchable (ec, label, on_true);
1739 public override void EmitSideEffect (EmitContext ec)
1741 Child.EmitSideEffect (ec);
1744 public override string GetSignatureForError()
1746 return Type.GetSignatureForError ();
1749 public override object GetValue ()
1751 return Child.GetValue ();
1755 public override object GetTypedValue ()
1758 // The method can be used in dynamic context only (on closed types)
1760 // System.Enum.ToObject cannot be called on dynamic types
1761 // EnumBuilder has to be used, but we cannot use EnumBuilder
1762 // because it does not properly support generics
1764 return System.Enum.ToObject (type.GetMetaInfo (), Child.GetValue ());
1768 public override string GetValueAsLiteral ()
1770 return Child.GetValueAsLiteral ();
1773 public override long GetValueAsLong ()
1775 return Child.GetValueAsLong ();
1778 public EnumConstant Increment()
1780 return new EnumConstant (((IntegralConstant) Child).Increment (), type);
1783 public override bool IsDefaultValue {
1785 return Child.IsDefaultValue;
1789 public override bool IsSideEffectFree {
1791 return Child.IsSideEffectFree;
1795 public override bool IsZeroInteger {
1796 get { return Child.IsZeroInteger; }
1799 public override bool IsNegative {
1801 return Child.IsNegative;
1805 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1807 if (Child.Type == target_type)
1810 return Child.ConvertExplicitly (in_checked_context, target_type);
1813 public override Constant ConvertImplicitly (TypeSpec type)
1815 if (this.type == type) {
1819 if (!Convert.ImplicitStandardConversionExists (this, type)){
1823 return Child.ConvertImplicitly (type);
1828 /// This kind of cast is used to encapsulate Value Types in objects.
1830 /// The effect of it is to box the value type emitted by the previous
1833 public class BoxedCast : TypeCast {
1835 public BoxedCast (Expression expr, TypeSpec target_type)
1836 : base (expr, target_type)
1838 eclass = ExprClass.Value;
1841 protected override Expression DoResolve (ResolveContext ec)
1843 // This should never be invoked, we are born in fully
1844 // initialized state.
1849 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1851 // Only boxing to object type is supported
1852 if (targetType.BuiltinType != BuiltinTypeSpec.Type.Object) {
1853 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
1857 enc.Encode (child.Type);
1858 child.EncodeAttributeValue (rc, enc, child.Type, parameterType);
1861 public override void Emit (EmitContext ec)
1865 ec.Emit (OpCodes.Box, child.Type);
1868 public override void EmitSideEffect (EmitContext ec)
1870 // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
1871 // so, we need to emit the box+pop instructions in most cases
1872 if (child.Type.IsStruct &&
1873 (type.BuiltinType == BuiltinTypeSpec.Type.Object || type.BuiltinType == BuiltinTypeSpec.Type.ValueType))
1874 child.EmitSideEffect (ec);
1876 base.EmitSideEffect (ec);
1880 public class UnboxCast : TypeCast {
1881 public UnboxCast (Expression expr, TypeSpec return_type)
1882 : base (expr, return_type)
1886 protected override Expression DoResolve (ResolveContext ec)
1888 // This should never be invoked, we are born in fully
1889 // initialized state.
1894 public override void Emit (EmitContext ec)
1898 ec.Emit (OpCodes.Unbox_Any, type);
1903 /// This is used to perform explicit numeric conversions.
1905 /// Explicit numeric conversions might trigger exceptions in a checked
1906 /// context, so they should generate the conv.ovf opcodes instead of
1909 public class ConvCast : TypeCast {
1910 public enum Mode : byte {
1911 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1913 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1914 U2_I1, U2_U1, U2_I2, U2_CH,
1915 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1916 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1917 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH, I8_I,
1918 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH, U8_I,
1919 CH_I1, CH_U1, CH_I2,
1920 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1921 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4,
1927 public ConvCast (Expression child, TypeSpec return_type, Mode m)
1928 : base (child, return_type)
1933 protected override Expression DoResolve (ResolveContext ec)
1935 // This should never be invoked, we are born in fully
1936 // initialized state.
1941 public override string ToString ()
1943 return String.Format ("ConvCast ({0}, {1})", mode, child);
1946 public override void Emit (EmitContext ec)
1952 public static void Emit (EmitContext ec, Mode mode)
1954 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
1956 case Mode.I1_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1957 case Mode.I1_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1958 case Mode.I1_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1959 case Mode.I1_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1960 case Mode.I1_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1962 case Mode.U1_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1963 case Mode.U1_CH: /* nothing */ break;
1965 case Mode.I2_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1966 case Mode.I2_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1967 case Mode.I2_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1968 case Mode.I2_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1969 case Mode.I2_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1970 case Mode.I2_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1972 case Mode.U2_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1973 case Mode.U2_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1974 case Mode.U2_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1975 case Mode.U2_CH: /* nothing */ break;
1977 case Mode.I4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1978 case Mode.I4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1979 case Mode.I4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1980 case Mode.I4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1981 case Mode.I4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1982 case Mode.I4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1983 case Mode.I4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1985 case Mode.U4_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1986 case Mode.U4_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1987 case Mode.U4_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1988 case Mode.U4_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1989 case Mode.U4_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1990 case Mode.U4_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1992 case Mode.I8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1993 case Mode.I8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1994 case Mode.I8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1995 case Mode.I8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1996 case Mode.I8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1997 case Mode.I8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1998 case Mode.I8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1999 case Mode.I8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2000 case Mode.I8_I: ec.Emit (OpCodes.Conv_Ovf_U); break;
2002 case Mode.U8_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2003 case Mode.U8_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2004 case Mode.U8_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2005 case Mode.U8_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2006 case Mode.U8_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
2007 case Mode.U8_U4: ec.Emit (OpCodes.Conv_Ovf_U4_Un); break;
2008 case Mode.U8_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2009 case Mode.U8_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
2010 case Mode.U8_I: ec.Emit (OpCodes.Conv_Ovf_U_Un); break;
2012 case Mode.CH_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
2013 case Mode.CH_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
2014 case Mode.CH_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
2016 case Mode.R4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
2017 case Mode.R4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
2018 case Mode.R4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2019 case Mode.R4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2020 case Mode.R4_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2021 case Mode.R4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2022 case Mode.R4_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
2023 case Mode.R4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2024 case Mode.R4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2026 case Mode.R8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
2027 case Mode.R8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
2028 case Mode.R8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2029 case Mode.R8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2030 case Mode.R8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2031 case Mode.R8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2032 case Mode.R8_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
2033 case Mode.R8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2034 case Mode.R8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2035 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2037 case Mode.I_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2041 case Mode.I1_U1: ec.Emit (OpCodes.Conv_U1); break;
2042 case Mode.I1_U2: ec.Emit (OpCodes.Conv_U2); break;
2043 case Mode.I1_U4: ec.Emit (OpCodes.Conv_U4); break;
2044 case Mode.I1_U8: ec.Emit (OpCodes.Conv_I8); break;
2045 case Mode.I1_CH: ec.Emit (OpCodes.Conv_U2); break;
2047 case Mode.U1_I1: ec.Emit (OpCodes.Conv_I1); break;
2048 case Mode.U1_CH: ec.Emit (OpCodes.Conv_U2); break;
2050 case Mode.I2_I1: ec.Emit (OpCodes.Conv_I1); break;
2051 case Mode.I2_U1: ec.Emit (OpCodes.Conv_U1); break;
2052 case Mode.I2_U2: ec.Emit (OpCodes.Conv_U2); break;
2053 case Mode.I2_U4: ec.Emit (OpCodes.Conv_U4); break;
2054 case Mode.I2_U8: ec.Emit (OpCodes.Conv_I8); break;
2055 case Mode.I2_CH: ec.Emit (OpCodes.Conv_U2); break;
2057 case Mode.U2_I1: ec.Emit (OpCodes.Conv_I1); break;
2058 case Mode.U2_U1: ec.Emit (OpCodes.Conv_U1); break;
2059 case Mode.U2_I2: ec.Emit (OpCodes.Conv_I2); break;
2060 case Mode.U2_CH: /* nothing */ break;
2062 case Mode.I4_I1: ec.Emit (OpCodes.Conv_I1); break;
2063 case Mode.I4_U1: ec.Emit (OpCodes.Conv_U1); break;
2064 case Mode.I4_I2: ec.Emit (OpCodes.Conv_I2); break;
2065 case Mode.I4_U4: /* nothing */ break;
2066 case Mode.I4_U2: ec.Emit (OpCodes.Conv_U2); break;
2067 case Mode.I4_U8: ec.Emit (OpCodes.Conv_I8); break;
2068 case Mode.I4_CH: ec.Emit (OpCodes.Conv_U2); break;
2070 case Mode.U4_I1: ec.Emit (OpCodes.Conv_I1); break;
2071 case Mode.U4_U1: ec.Emit (OpCodes.Conv_U1); break;
2072 case Mode.U4_I2: ec.Emit (OpCodes.Conv_I2); break;
2073 case Mode.U4_U2: ec.Emit (OpCodes.Conv_U2); break;
2074 case Mode.U4_I4: /* nothing */ break;
2075 case Mode.U4_CH: ec.Emit (OpCodes.Conv_U2); break;
2077 case Mode.I8_I1: ec.Emit (OpCodes.Conv_I1); break;
2078 case Mode.I8_U1: ec.Emit (OpCodes.Conv_U1); break;
2079 case Mode.I8_I2: ec.Emit (OpCodes.Conv_I2); break;
2080 case Mode.I8_U2: ec.Emit (OpCodes.Conv_U2); break;
2081 case Mode.I8_I4: ec.Emit (OpCodes.Conv_I4); break;
2082 case Mode.I8_U4: ec.Emit (OpCodes.Conv_U4); break;
2083 case Mode.I8_U8: /* nothing */ break;
2084 case Mode.I8_CH: ec.Emit (OpCodes.Conv_U2); break;
2085 case Mode.I8_I: ec.Emit (OpCodes.Conv_U); break;
2087 case Mode.U8_I1: ec.Emit (OpCodes.Conv_I1); break;
2088 case Mode.U8_U1: ec.Emit (OpCodes.Conv_U1); break;
2089 case Mode.U8_I2: ec.Emit (OpCodes.Conv_I2); break;
2090 case Mode.U8_U2: ec.Emit (OpCodes.Conv_U2); break;
2091 case Mode.U8_I4: ec.Emit (OpCodes.Conv_I4); break;
2092 case Mode.U8_U4: ec.Emit (OpCodes.Conv_U4); break;
2093 case Mode.U8_I8: /* nothing */ break;
2094 case Mode.U8_CH: ec.Emit (OpCodes.Conv_U2); break;
2095 case Mode.U8_I: ec.Emit (OpCodes.Conv_U); break;
2097 case Mode.CH_I1: ec.Emit (OpCodes.Conv_I1); break;
2098 case Mode.CH_U1: ec.Emit (OpCodes.Conv_U1); break;
2099 case Mode.CH_I2: ec.Emit (OpCodes.Conv_I2); break;
2101 case Mode.R4_I1: ec.Emit (OpCodes.Conv_I1); break;
2102 case Mode.R4_U1: ec.Emit (OpCodes.Conv_U1); break;
2103 case Mode.R4_I2: ec.Emit (OpCodes.Conv_I2); break;
2104 case Mode.R4_U2: ec.Emit (OpCodes.Conv_U2); break;
2105 case Mode.R4_I4: ec.Emit (OpCodes.Conv_I4); break;
2106 case Mode.R4_U4: ec.Emit (OpCodes.Conv_U4); break;
2107 case Mode.R4_I8: ec.Emit (OpCodes.Conv_I8); break;
2108 case Mode.R4_U8: ec.Emit (OpCodes.Conv_U8); break;
2109 case Mode.R4_CH: ec.Emit (OpCodes.Conv_U2); break;
2111 case Mode.R8_I1: ec.Emit (OpCodes.Conv_I1); break;
2112 case Mode.R8_U1: ec.Emit (OpCodes.Conv_U1); break;
2113 case Mode.R8_I2: ec.Emit (OpCodes.Conv_I2); break;
2114 case Mode.R8_U2: ec.Emit (OpCodes.Conv_U2); break;
2115 case Mode.R8_I4: ec.Emit (OpCodes.Conv_I4); break;
2116 case Mode.R8_U4: ec.Emit (OpCodes.Conv_U4); break;
2117 case Mode.R8_I8: ec.Emit (OpCodes.Conv_I8); break;
2118 case Mode.R8_U8: ec.Emit (OpCodes.Conv_U8); break;
2119 case Mode.R8_CH: ec.Emit (OpCodes.Conv_U2); break;
2120 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2122 case Mode.I_I8: ec.Emit (OpCodes.Conv_U8); break;
2128 class OpcodeCast : TypeCast
2132 public OpcodeCast (Expression child, TypeSpec return_type, OpCode op)
2133 : base (child, return_type)
2138 protected override Expression DoResolve (ResolveContext ec)
2140 // This should never be invoked, we are born in fully
2141 // initialized state.
2146 public override void Emit (EmitContext ec)
2152 public TypeSpec UnderlyingType {
2153 get { return child.Type; }
2158 // Opcode casts expression with 2 opcodes but only
2159 // single expression tree node
2161 class OpcodeCastDuplex : OpcodeCast
2163 readonly OpCode second;
2165 public OpcodeCastDuplex (Expression child, TypeSpec returnType, OpCode first, OpCode second)
2166 : base (child, returnType, first)
2168 this.second = second;
2171 public override void Emit (EmitContext ec)
2179 /// This kind of cast is used to encapsulate a child and cast it
2180 /// to the class requested
2182 public sealed class ClassCast : TypeCast {
2183 readonly bool forced;
2185 public ClassCast (Expression child, TypeSpec return_type)
2186 : base (child, return_type)
2190 public ClassCast (Expression child, TypeSpec return_type, bool forced)
2191 : base (child, return_type)
2193 this.forced = forced;
2196 public override void Emit (EmitContext ec)
2200 bool gen = TypeManager.IsGenericParameter (child.Type);
2202 ec.Emit (OpCodes.Box, child.Type);
2204 if (type.IsGenericParameter) {
2205 ec.Emit (OpCodes.Unbox_Any, type);
2212 ec.Emit (OpCodes.Castclass, type);
2217 // Created during resolving pahse when an expression is wrapped or constantified
2218 // and original expression can be used later (e.g. for expression trees)
2220 public class ReducedExpression : Expression
2222 public class ReducedConstantExpression : EmptyConstantCast
2224 readonly Expression orig_expr;
2226 public ReducedConstantExpression (Constant expr, Expression orig_expr)
2227 : base (expr, expr.Type)
2229 this.orig_expr = orig_expr;
2232 public Expression OriginalExpression {
2238 public override Constant ConvertImplicitly (TypeSpec target_type)
2240 Constant c = base.ConvertImplicitly (target_type);
2242 c = new ReducedConstantExpression (c, orig_expr);
2247 public override Expression CreateExpressionTree (ResolveContext ec)
2249 return orig_expr.CreateExpressionTree (ec);
2252 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
2254 Constant c = base.ConvertExplicitly (in_checked_context, target_type);
2256 c = new ReducedConstantExpression (c, orig_expr);
2260 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
2263 // LAMESPEC: Reduced conditional expression is allowed as an attribute argument
2265 if (orig_expr is Conditional)
2266 child.EncodeAttributeValue (rc, enc, targetType,parameterType);
2268 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
2272 sealed class ReducedConstantStatement : ReducedConstantExpression, IReducedExpressionStatement
2274 public ReducedConstantStatement (Constant expr, Expression origExpr)
2275 : base (expr, origExpr)
2280 sealed class ReducedExpressionStatement : ExpressionStatement
2282 readonly Expression orig_expr;
2283 readonly ExpressionStatement stm;
2285 public ReducedExpressionStatement (ExpressionStatement stm, Expression orig)
2287 this.orig_expr = orig;
2289 this.eclass = stm.eclass;
2290 this.type = stm.Type;
2292 this.loc = orig.Location;
2295 public override bool ContainsEmitWithAwait ()
2297 return stm.ContainsEmitWithAwait ();
2300 public override Expression CreateExpressionTree (ResolveContext ec)
2302 return orig_expr.CreateExpressionTree (ec);
2305 protected override Expression DoResolve (ResolveContext ec)
2310 public override void Emit (EmitContext ec)
2315 public override void EmitStatement (EmitContext ec)
2317 stm.EmitStatement (ec);
2320 public override void FlowAnalysis (FlowAnalysisContext fc)
2322 stm.FlowAnalysis (fc);
2326 readonly Expression expr, orig_expr;
2328 private ReducedExpression (Expression expr, Expression orig_expr)
2331 this.eclass = expr.eclass;
2332 this.type = expr.Type;
2333 this.orig_expr = orig_expr;
2334 this.loc = orig_expr.Location;
2339 public override bool IsSideEffectFree {
2341 return expr.IsSideEffectFree;
2345 public Expression OriginalExpression {
2353 public override bool ContainsEmitWithAwait ()
2355 return expr.ContainsEmitWithAwait ();
2359 // Creates fully resolved expression switcher
2361 public static Constant Create (Constant expr, Expression originalExpr)
2363 if (expr.eclass == ExprClass.Unresolved)
2364 throw new ArgumentException ("Unresolved expression");
2366 if (originalExpr is ExpressionStatement)
2367 return new ReducedConstantStatement (expr, originalExpr);
2369 return new ReducedConstantExpression (expr, originalExpr);
2372 public static ExpressionStatement Create (ExpressionStatement s, Expression orig)
2374 return new ReducedExpressionStatement (s, orig);
2377 public static Expression Create (Expression expr, Expression original_expr)
2379 return Create (expr, original_expr, true);
2383 // Creates unresolved reduce expression. The original expression has to be
2384 // already resolved. Created expression is constant based based on `expr'
2385 // value unless canBeConstant is used
2387 public static Expression Create (Expression expr, Expression original_expr, bool canBeConstant)
2389 if (canBeConstant) {
2390 Constant c = expr as Constant;
2392 return Create (c, original_expr);
2395 ExpressionStatement s = expr as ExpressionStatement;
2397 return Create (s, original_expr);
2399 if (expr.eclass == ExprClass.Unresolved)
2400 throw new ArgumentException ("Unresolved expression");
2402 return new ReducedExpression (expr, original_expr);
2405 public override Expression CreateExpressionTree (ResolveContext ec)
2407 return orig_expr.CreateExpressionTree (ec);
2410 protected override Expression DoResolve (ResolveContext ec)
2415 public override void Emit (EmitContext ec)
2420 public override Expression EmitToField (EmitContext ec)
2422 return expr.EmitToField(ec);
2425 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2427 expr.EmitBranchable (ec, target, on_true);
2430 public override void FlowAnalysis (FlowAnalysisContext fc)
2432 orig_expr.FlowAnalysis (fc);
2435 public override void FlowAnalysisConditional (FlowAnalysisContext fc)
2437 orig_expr.FlowAnalysisConditional (fc);
2440 public override SLE.Expression MakeExpression (BuilderContext ctx)
2442 return orig_expr.MakeExpression (ctx);
2445 public override Reachability MarkReachable (Reachability rc)
2447 return expr.MarkReachable (rc);
2452 // Standard composite pattern
2454 public abstract class CompositeExpression : Expression
2456 protected Expression expr;
2458 protected CompositeExpression (Expression expr)
2461 this.loc = expr.Location;
2464 public override bool ContainsEmitWithAwait ()
2466 return expr.ContainsEmitWithAwait ();
2469 public override Expression CreateExpressionTree (ResolveContext rc)
2471 return expr.CreateExpressionTree (rc);
2474 public Expression Child {
2475 get { return expr; }
2478 protected override Expression DoResolve (ResolveContext rc)
2480 expr = expr.Resolve (rc);
2485 eclass = expr.eclass;
2489 public override void Emit (EmitContext ec)
2494 public override bool IsNull {
2495 get { return expr.IsNull; }
2500 // Base of expressions used only to narrow resolve flow
2502 public abstract class ShimExpression : Expression
2504 protected Expression expr;
2506 protected ShimExpression (Expression expr)
2511 public Expression Expr {
2517 protected override void CloneTo (CloneContext clonectx, Expression t)
2522 ShimExpression target = (ShimExpression) t;
2523 target.expr = expr.Clone (clonectx);
2526 public override bool ContainsEmitWithAwait ()
2528 return expr.ContainsEmitWithAwait ();
2531 public override Expression CreateExpressionTree (ResolveContext ec)
2533 throw new NotSupportedException ("ET");
2536 public override void Emit (EmitContext ec)
2538 throw new InternalErrorException ("Missing Resolve call");
2542 public class UnreachableExpression : Expression
2544 public UnreachableExpression (Expression expr)
2546 this.loc = expr.Location;
2549 public override Expression CreateExpressionTree (ResolveContext ec)
2552 throw new NotImplementedException ();
2555 protected override Expression DoResolve (ResolveContext rc)
2557 throw new NotSupportedException ();
2560 public override void FlowAnalysis (FlowAnalysisContext fc)
2562 fc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
2565 public override void Emit (EmitContext ec)
2569 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2575 // Unresolved type name expressions
2577 public abstract class ATypeNameExpression : FullNamedExpression
2580 protected TypeArguments targs;
2582 protected ATypeNameExpression (string name, Location l)
2588 protected ATypeNameExpression (string name, TypeArguments targs, Location l)
2595 protected ATypeNameExpression (string name, int arity, Location l)
2596 : this (name, new UnboundTypeArguments (arity, l), l)
2604 return targs == null ? 0 : targs.Count;
2608 public bool HasTypeArguments {
2610 return targs != null && !targs.IsEmpty;
2614 public string Name {
2623 public TypeArguments TypeArguments {
2631 public override bool Equals (object obj)
2633 ATypeNameExpression atne = obj as ATypeNameExpression;
2634 return atne != null && atne.Name == Name &&
2635 (targs == null || targs.Equals (atne.targs));
2638 public override int GetHashCode ()
2640 return Name.GetHashCode ();
2643 // TODO: Move it to MemberCore
2644 public static string GetMemberType (MemberCore mc)
2650 if (mc is FieldBase)
2652 if (mc is MethodCore)
2654 if (mc is EnumMember)
2662 public override string GetSignatureForError ()
2664 if (targs != null) {
2665 return Name + "<" + targs.GetSignatureForError () + ">";
2671 public abstract Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restriction);
2675 /// SimpleName expressions are formed of a single word and only happen at the beginning
2676 /// of a dotted-name.
2678 public class SimpleName : ATypeNameExpression
2680 public SimpleName (string name, Location l)
2685 public SimpleName (string name, TypeArguments args, Location l)
2686 : base (name, args, l)
2690 public SimpleName (string name, int arity, Location l)
2691 : base (name, arity, l)
2695 public SimpleName GetMethodGroup ()
2697 return new SimpleName (Name, targs, loc);
2700 protected override Expression DoResolve (ResolveContext rc)
2702 return SimpleNameResolve (rc, null);
2705 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
2707 return SimpleNameResolve (ec, right_side);
2710 public void Error_NameDoesNotExist (ResolveContext rc)
2712 rc.Report.Error (103, loc, "The name `{0}' does not exist in the current context", Name);
2715 protected virtual void Error_TypeOrNamespaceNotFound (IMemberContext ctx)
2717 if (ctx.CurrentType != null) {
2718 var member = MemberLookup (ctx, false, ctx.CurrentType, Name, 0, MemberLookupRestrictions.ExactArity, loc) as MemberExpr;
2719 if (member != null) {
2720 Error_UnexpectedKind (ctx, member, "type", member.KindName, loc);
2725 var report = ctx.Module.Compiler.Report;
2727 var retval = ctx.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2728 if (retval != null) {
2729 report.SymbolRelatedToPreviousError (retval.Type);
2730 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
2734 retval = ctx.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2735 if (retval != null) {
2736 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, loc);
2740 var ns_candidates = ctx.Module.GlobalRootNamespace.FindTypeNamespaces (ctx, Name, Arity);
2741 if (ns_candidates != null) {
2742 if (ctx is UsingAliasNamespace.AliasContext) {
2743 report.Error (246, loc,
2744 "The type or namespace name `{1}' could not be found. Consider using fully qualified name `{0}.{1}'",
2745 ns_candidates[0], Name);
2747 string usings = string.Join ("' or `", ns_candidates.ToArray ());
2748 report.Error (246, loc,
2749 "The type or namespace name `{0}' could not be found. Are you missing `{1}' using directive?",
2753 report.Error (246, loc,
2754 "The type or namespace name `{0}' could not be found. Are you missing an assembly reference?",
2759 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
2761 FullNamedExpression fne = mc.LookupNamespaceOrType (Name, Arity, LookupMode.Normal, loc);
2764 if (fne.Type != null && Arity > 0) {
2765 if (HasTypeArguments) {
2766 GenericTypeExpr ct = new GenericTypeExpr (fne.Type, targs, loc);
2767 if (ct.ResolveAsType (mc) == null)
2773 targs.Resolve (mc, allowUnboundTypeArguments);
2775 return new GenericOpenTypeExpr (fne.Type, loc);
2779 // dynamic namespace is ignored when dynamic is allowed (does not apply to types)
2781 if (!(fne is NamespaceExpression))
2785 if (Arity == 0 && Name == "dynamic" && !(mc is NamespaceContainer) && mc.Module.Compiler.Settings.Version > LanguageVersion.V_3) {
2786 if (!mc.Module.PredefinedAttributes.Dynamic.IsDefined) {
2787 mc.Module.Compiler.Report.Error (1980, Location,
2788 "Dynamic keyword requires `{0}' to be defined. Are you missing System.Core.dll assembly reference?",
2789 mc.Module.PredefinedAttributes.Dynamic.GetSignatureForError ());
2792 fne = new DynamicTypeExpr (loc);
2793 fne.ResolveAsType (mc);
2799 Error_TypeOrNamespaceNotFound (mc);
2803 bool IsPossibleTypeOrNamespace (IMemberContext mc)
2806 // Has to ignore static usings because we are looking for any member not just type
2809 return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing | LookupMode.IgnoreStaticUsing, loc) != null;
2812 public bool IsPossibleType (IMemberContext mc)
2814 return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) is TypeExpr;
2817 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
2819 int lookup_arity = Arity;
2820 bool errorMode = false;
2822 Block current_block = rc.CurrentBlock;
2823 INamedBlockVariable variable = null;
2824 bool variable_found = false;
2828 // Stage 1: binding to local variables or parameters
2830 // LAMESPEC: It should take invocableOnly into account but that would break csc compatibility
2832 if (current_block != null && lookup_arity == 0) {
2833 if (current_block.ParametersBlock.TopBlock.GetLocalName (Name, current_block.Original, ref variable)) {
2834 if (!variable.IsDeclared) {
2835 // We found local name in accessible block but it's not
2836 // initialized yet, maybe the user wanted to bind to something else
2838 variable_found = true;
2840 e = variable.CreateReferenceExpression (rc, loc);
2843 Error_TypeArgumentsCannotBeUsed (rc, "variable", Name, loc);
2852 // Stage 2: Lookup members if we are inside a type up to top level type for nested types
2854 TypeSpec member_type = rc.CurrentType;
2855 for (; member_type != null; member_type = member_type.DeclaringType) {
2856 e = MemberLookup (rc, errorMode, member_type, Name, lookup_arity, restrictions, loc);
2860 var me = e as MemberExpr;
2862 // The name matches a type, defer to ResolveAsTypeStep
2870 if (variable != null) {
2871 if (me is FieldExpr || me is ConstantExpr || me is EventExpr || me is PropertyExpr) {
2872 rc.Report.Error (844, loc,
2873 "A local variable `{0}' cannot be used before it is declared. Consider renaming the local variable when it hides the member `{1}'",
2874 Name, me.GetSignatureForError ());
2878 } else if (me is MethodGroupExpr || me is PropertyExpr || me is IndexerExpr) {
2879 // Leave it to overload resolution to report correct error
2881 // TODO: rc.Report.SymbolRelatedToPreviousError ()
2882 ErrorIsInaccesible (rc, me.GetSignatureForError (), loc);
2886 // MemberLookup does not check accessors availability, this is actually needed for properties only
2888 var pe = me as PropertyExpr;
2891 // Break as there is no other overload available anyway
2892 if ((restrictions & MemberLookupRestrictions.ReadAccess) != 0) {
2893 if (!pe.PropertyInfo.HasGet || !pe.PropertyInfo.Get.IsAccessible (rc))
2896 pe.Getter = pe.PropertyInfo.Get;
2898 if (!pe.PropertyInfo.HasSet) {
2899 if (rc.HasSet (ResolveContext.Options.ConstructorScope) && pe.IsAutoPropertyAccess &&
2900 pe.PropertyInfo.DeclaringType == rc.CurrentType && pe.IsStatic == rc.IsStatic) {
2901 var p = (Property) pe.PropertyInfo.MemberDefinition;
2902 return new FieldExpr (p.BackingField, loc);
2905 variable_found = true;
2909 if (!pe.PropertyInfo.Set.IsAccessible (rc)) {
2910 variable_found = true;
2914 pe.Setter = pe.PropertyInfo.Set;
2919 // TODO: It's used by EventExpr -> FieldExpr transformation only
2920 // TODO: Should go to MemberAccess
2921 me = me.ResolveMemberAccess (rc, null, null);
2924 targs.Resolve (rc, false);
2925 me.SetTypeArguments (rc, targs);
2932 // Stage 3: Lookup nested types, namespaces and type parameters in the context
2934 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0 && !variable_found) {
2935 if (IsPossibleTypeOrNamespace (rc)) {
2936 return ResolveAsTypeOrNamespace (rc, false);
2940 var expr = NamespaceContainer.LookupStaticUsings (rc, Name, Arity, loc);
2943 targs.Resolve (rc, false);
2945 var me = expr as MemberExpr;
2947 me.SetTypeArguments (rc, targs);
2952 if ((restrictions & MemberLookupRestrictions.NameOfExcluded) == 0 && Name == "nameof")
2953 return new NameOf (this);
2956 if (variable_found) {
2957 rc.Report.Error (841, loc, "A local variable `{0}' cannot be used before it is declared", Name);
2960 var tparams = rc.CurrentTypeParameters;
2961 if (tparams != null) {
2962 if (tparams.Find (Name) != null) {
2963 Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2968 var ct = rc.CurrentType;
2970 if (ct.MemberDefinition.TypeParametersCount > 0) {
2971 foreach (var ctp in ct.MemberDefinition.TypeParameters) {
2972 if (ctp.Name == Name) {
2973 Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2979 ct = ct.DeclaringType;
2980 } while (ct != null);
2983 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0) {
2984 e = rc.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2986 rc.Report.SymbolRelatedToPreviousError (e.Type);
2987 ErrorIsInaccesible (rc, e.GetSignatureForError (), loc);
2991 var me = MemberLookup (rc, false, rc.CurrentType, Name, Arity, restrictions & ~MemberLookupRestrictions.InvocableOnly, loc) as MemberExpr;
2993 Error_UnexpectedKind (rc, me, "method group", me.KindName, loc);
2994 return ErrorExpression.Instance;
2998 e = rc.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
3000 if (e.Type.Arity != Arity && (restrictions & MemberLookupRestrictions.IgnoreArity) == 0) {
3001 Error_TypeArgumentsCannotBeUsed (rc, e.Type, loc);
3005 if (e is TypeExpr) {
3006 // TypeExpression does not have correct location
3007 if (e is TypeExpression)
3008 e = new TypeExpression (e.Type, loc);
3014 Error_NameDoesNotExist (rc);
3017 return ErrorExpression.Instance;
3020 if (rc.Module.Evaluator != null) {
3021 var fi = rc.Module.Evaluator.LookupField (Name);
3023 return new FieldExpr (fi.Item1, loc);
3031 Expression SimpleNameResolve (ResolveContext ec, Expression right_side)
3033 Expression e = LookupNameExpression (ec, right_side == null ? MemberLookupRestrictions.ReadAccess : MemberLookupRestrictions.None);
3038 if (e is FullNamedExpression && e.eclass != ExprClass.Unresolved) {
3039 Error_UnexpectedKind (ec, e, "variable", e.ExprClassName, loc);
3043 if (right_side != null) {
3044 e = e.ResolveLValue (ec, right_side);
3052 public override object Accept (StructuralVisitor visitor)
3054 return visitor.Visit (this);
3059 /// Represents a namespace or a type. The name of the class was inspired by
3060 /// section 10.8.1 (Fully Qualified Names).
3062 public abstract class FullNamedExpression : Expression
3064 protected override void CloneTo (CloneContext clonectx, Expression target)
3066 // Do nothing, most unresolved type expressions cannot be
3067 // resolved to different type
3070 public override bool ContainsEmitWithAwait ()
3075 public override Expression CreateExpressionTree (ResolveContext ec)
3077 throw new NotSupportedException ("ET");
3080 public abstract FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments);
3083 // This is used to resolve the expression as a type, a null
3084 // value will be returned if the expression is not a type
3087 public override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3089 FullNamedExpression fne = ResolveAsTypeOrNamespace (mc, allowUnboundTypeArguments);
3094 TypeExpr te = fne as TypeExpr;
3096 Error_UnexpectedKind (mc, fne, "type", fne.ExprClassName, loc);
3104 var dep = type.GetMissingDependencies ();
3106 ImportedTypeDefinition.Error_MissingDependency (mc, dep, loc);
3109 if (type.Kind == MemberKind.Void) {
3110 mc.Module.Compiler.Report.Error (673, loc, "System.Void cannot be used from C#. Consider using `void'");
3114 // Obsolete checks cannot be done when resolving base context as they
3115 // require type dependencies to be set but we are in process of resolving them
3117 if (mc is ResolveContext) {
3118 var oa = type.GetAttributeObsolete ();
3119 if (oa != null && !mc.IsObsolete)
3120 AttributeTester.Report_ObsoleteMessage (oa, type.GetSignatureForError (), fne.Location, mc.Module.Compiler.Report);
3127 public override void Emit (EmitContext ec)
3129 throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
3130 GetSignatureForError ());
3135 /// Expression that evaluates to a type
3137 public abstract class TypeExpr : FullNamedExpression
3139 public sealed override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3145 protected sealed override Expression DoResolve (ResolveContext ec)
3151 public override bool Equals (object obj)
3153 TypeExpr tobj = obj as TypeExpr;
3157 return Type == tobj.Type;
3160 public override int GetHashCode ()
3162 return Type.GetHashCode ();
3167 /// Fully resolved Expression that already evaluated to a type
3169 public class TypeExpression : TypeExpr
3171 public TypeExpression (TypeSpec t, Location l)
3174 eclass = ExprClass.Type;
3178 public sealed override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3184 public class NamespaceExpression : FullNamedExpression
3186 readonly Namespace ns;
3188 public NamespaceExpression (Namespace ns, Location loc)
3191 this.Type = InternalType.Namespace;
3192 this.eclass = ExprClass.Namespace;
3196 public Namespace Namespace {
3202 protected override Expression DoResolve (ResolveContext rc)
3204 throw new NotImplementedException ();
3207 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3212 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
3214 var retval = Namespace.LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
3215 if (retval != null) {
3216 // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.MemberDefinition);
3217 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
3221 retval = Namespace.LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
3222 if (retval != null) {
3223 Error_TypeArgumentsCannotBeUsed (ctx, retval, loc);
3228 if (arity > 0 && Namespace.TryGetNamespace (name, out ns)) {
3229 Error_TypeArgumentsCannotBeUsed (ctx, ExprClassName, ns.GetSignatureForError (), loc);
3233 string assembly = null;
3234 string possible_name = Namespace.GetSignatureForError () + "." + name;
3236 // Only assembly unique name should be added
3237 switch (possible_name) {
3238 case "System.Drawing":
3239 case "System.Web.Services":
3242 case "System.Configuration":
3243 case "System.Data.Services":
3244 case "System.DirectoryServices":
3246 case "System.Net.Http":
3247 case "System.Numerics":
3248 case "System.Runtime.Caching":
3249 case "System.ServiceModel":
3250 case "System.Transactions":
3251 case "System.Web.Routing":
3252 case "System.Xml.Linq":
3254 assembly = possible_name;
3258 case "System.Linq.Expressions":
3259 assembly = "System.Core";
3262 case "System.Windows.Forms":
3263 case "System.Windows.Forms.Layout":
3264 assembly = "System.Windows.Forms";
3268 assembly = assembly == null ? "an" : "`" + assembly + "'";
3270 if (Namespace is GlobalRootNamespace) {
3271 ctx.Module.Compiler.Report.Error (400, loc,
3272 "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
3275 ctx.Module.Compiler.Report.Error (234, loc,
3276 "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
3277 name, GetSignatureForError (), assembly);
3281 public override string GetSignatureForError ()
3283 return ns.GetSignatureForError ();
3286 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
3288 return ns.LookupTypeOrNamespace (ctx, name, arity, mode, loc);
3291 public override string ToString ()
3293 return Namespace.Name;
3298 /// This class denotes an expression which evaluates to a member
3299 /// of a struct or a class.
3301 public abstract class MemberExpr : Expression, OverloadResolver.IInstanceQualifier
3303 protected bool conditional_access_receiver;
3306 // An instance expression associated with this member, if it's a
3307 // non-static member
3309 public Expression InstanceExpression;
3312 /// The name of this member.
3314 public abstract string Name {
3319 // When base.member is used
3321 public bool IsBase {
3322 get { return InstanceExpression is BaseThis; }
3326 /// Whether this is an instance member.
3328 public abstract bool IsInstance {
3333 /// Whether this is a static member.
3335 public abstract bool IsStatic {
3339 public abstract string KindName {
3343 public bool ConditionalAccess { get; set; }
3345 protected abstract TypeSpec DeclaringType {
3349 TypeSpec OverloadResolver.IInstanceQualifier.InstanceType {
3351 return InstanceExpression.Type;
3356 // Converts best base candidate for virtual method starting from QueriedBaseType
3358 protected MethodSpec CandidateToBaseOverride (ResolveContext rc, MethodSpec method)
3361 // Only when base.member is used and method is virtual
3367 // Overload resulution works on virtual or non-virtual members only (no overrides). That
3368 // means for base.member access we have to find the closest match after we found best candidate
3370 if ((method.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
3372 // The method could already be what we are looking for
3374 TypeSpec[] targs = null;
3375 if (method.DeclaringType != InstanceExpression.Type) {
3377 // Candidate can have inflated MVAR parameters and we need to find
3378 // base match for original definition not inflated parameter types
3380 var parameters = method.Parameters;
3381 if (method.Arity > 0) {
3382 parameters = ((IParametersMember) method.MemberDefinition).Parameters;
3383 var inflated = method.DeclaringType as InflatedTypeSpec;
3384 if (inflated != null) {
3385 parameters = parameters.Inflate (inflated.CreateLocalInflator (rc));
3389 var filter = new MemberFilter (method.Name, method.Arity, MemberKind.Method, parameters, null);
3390 var base_override = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as MethodSpec;
3391 if (base_override != null && base_override.DeclaringType != method.DeclaringType) {
3392 if (base_override.IsGeneric)
3393 targs = method.TypeArguments;
3395 method = base_override;
3400 // When base access is used inside anonymous method/iterator/etc we need to
3401 // get back to the context of original type. We do it by emiting proxy
3402 // method in original class and rewriting base call to this compiler
3403 // generated method call which does the actual base invocation. This may
3404 // introduce redundant storey but with `this' only but it's tricky to avoid
3405 // at this stage as we don't know what expressions follow base
3407 // TODO: It's needed only when the method with base call is moved to a storey
3409 if (rc.CurrentAnonymousMethod != null) {
3410 if (targs == null && method.IsGeneric) {
3411 targs = method.TypeArguments;
3412 method = method.GetGenericMethodDefinition ();
3415 if (method.Parameters.HasArglist)
3416 throw new NotImplementedException ("__arglist base call proxy");
3418 method = rc.CurrentMemberDefinition.Parent.PartialContainer.CreateHoistedBaseCallProxy (rc, method);
3420 // Ideally this should apply to any proxy rewrite but in the case of unary mutators on
3421 // get/set member expressions second call would fail to proxy because left expression
3422 // would be of 'this' and not 'base' because we share InstanceExpression for get/set
3423 // FIXME: The async check is another hack but will probably fail with mutators
3424 if (rc.CurrentType.IsStruct || rc.CurrentAnonymousMethod.Storey is AsyncTaskStorey)
3425 InstanceExpression = new This (loc).Resolve (rc);
3429 method = method.MakeGenericMethod (rc, targs);
3433 // Only base will allow this invocation to happen.
3435 if (method.IsAbstract) {
3436 rc.Report.SymbolRelatedToPreviousError (method);
3437 Error_CannotCallAbstractBase (rc, method.GetSignatureForError ());
3443 protected void CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3445 if (InstanceExpression == null)
3448 if ((member.Modifiers & Modifiers.PROTECTED) != 0 && !(InstanceExpression is This)) {
3449 if (!CheckProtectedMemberAccess (rc, member, InstanceExpression.Type)) {
3450 Error_ProtectedMemberAccess (rc, member, InstanceExpression.Type, loc);
3455 bool OverloadResolver.IInstanceQualifier.CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3457 if (InstanceExpression == null)
3460 return InstanceExpression is This || CheckProtectedMemberAccess (rc, member, InstanceExpression.Type);
3463 public static bool CheckProtectedMemberAccess<T> (ResolveContext rc, T member, TypeSpec qualifier) where T : MemberSpec
3465 var ct = rc.CurrentType;
3466 if (ct == qualifier)
3469 if ((member.Modifiers & Modifiers.INTERNAL) != 0 && member.DeclaringType.MemberDefinition.IsInternalAsPublic (ct.MemberDefinition.DeclaringAssembly))
3472 qualifier = qualifier.GetDefinition ();
3473 if (ct != qualifier && !IsSameOrBaseQualifier (ct, qualifier)) {
3480 public override bool ContainsEmitWithAwait ()
3482 return InstanceExpression != null && InstanceExpression.ContainsEmitWithAwait ();
3485 public override bool HasConditionalAccess ()
3487 return ConditionalAccess || (InstanceExpression != null && InstanceExpression.HasConditionalAccess ());
3490 static bool IsSameOrBaseQualifier (TypeSpec type, TypeSpec qtype)
3493 type = type.GetDefinition ();
3495 if (type == qtype || TypeManager.IsFamilyAccessible (qtype, type))
3498 type = type.DeclaringType;
3499 } while (type != null);
3504 protected void DoBestMemberChecks<T> (ResolveContext rc, T member) where T : MemberSpec, IInterfaceMemberSpec
3506 if (InstanceExpression != null) {
3507 InstanceExpression = InstanceExpression.Resolve (rc);
3508 CheckProtectedMemberAccess (rc, member);
3511 if (member.MemberType.IsPointer) {
3512 if (rc.CurrentIterator != null) {
3513 UnsafeInsideIteratorError (rc, loc);
3514 } else if (!rc.IsUnsafe) {
3515 UnsafeError (rc, loc);
3519 var dep = member.GetMissingDependencies ();
3521 ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
3524 member.CheckObsoleteness (rc, loc);
3526 if (!(member is FieldSpec))
3527 member.MemberDefinition.SetIsUsed ();
3530 protected virtual void Error_CannotCallAbstractBase (ResolveContext rc, string name)
3532 rc.Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
3535 public static void Error_ProtectedMemberAccess (ResolveContext rc, MemberSpec member, TypeSpec qualifier, Location loc)
3537 rc.Report.SymbolRelatedToPreviousError (member);
3538 rc.Report.Error (1540, loc,
3539 "Cannot access protected member `{0}' via a qualifier of type `{1}'. The qualifier must be of type `{2}' or derived from it",
3540 member.GetSignatureForError (), qualifier.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3543 public override void FlowAnalysis (FlowAnalysisContext fc)
3545 if (InstanceExpression != null) {
3546 InstanceExpression.FlowAnalysis (fc);
3550 protected void ResolveConditionalAccessReceiver (ResolveContext rc)
3552 if (!rc.HasSet (ResolveContext.Options.DontSetConditionalAccessReceiver) && HasConditionalAccess ()) {
3553 conditional_access_receiver = true;
3557 public bool ResolveInstanceExpression (ResolveContext rc, Expression rhs)
3559 if (!ResolveInstanceExpressionCore (rc, rhs))
3563 // Check intermediate value modification which won't have any effect
3565 if (rhs != null && TypeSpec.IsValueType (InstanceExpression.Type)) {
3566 var fexpr = InstanceExpression as FieldExpr;
3567 if (fexpr != null) {
3568 if (!fexpr.Spec.IsReadOnly || rc.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
3571 if (fexpr.IsStatic) {
3572 rc.Report.Error (1650, loc, "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
3573 fexpr.GetSignatureForError ());
3575 rc.Report.Error (1648, loc, "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
3576 fexpr.GetSignatureForError ());
3582 if (InstanceExpression is PropertyExpr || InstanceExpression is IndexerExpr || InstanceExpression is Invocation) {
3583 if (rc.CurrentInitializerVariable != null) {
3584 rc.Report.Error (1918, loc, "Members of value type `{0}' cannot be assigned using a property `{1}' object initializer",
3585 InstanceExpression.Type.GetSignatureForError (), InstanceExpression.GetSignatureForError ());
3587 rc.Report.Error (1612, loc,
3588 "Cannot modify a value type return value of `{0}'. Consider storing the value in a temporary variable",
3589 InstanceExpression.GetSignatureForError ());
3595 var lvr = InstanceExpression as LocalVariableReference;
3598 if (!lvr.local_info.IsReadonly)
3601 rc.Report.Error (1654, loc, "Cannot assign to members of `{0}' because it is a `{1}'",
3602 InstanceExpression.GetSignatureForError (), lvr.local_info.GetReadOnlyContext ());
3609 bool ResolveInstanceExpressionCore (ResolveContext rc, Expression rhs)
3612 if (InstanceExpression != null) {
3613 if (InstanceExpression is TypeExpr) {
3614 var t = InstanceExpression.Type;
3616 t.CheckObsoleteness (rc, loc);
3618 t = t.DeclaringType;
3619 } while (t != null);
3621 var runtime_expr = InstanceExpression as RuntimeValueExpression;
3622 if (runtime_expr == null || !runtime_expr.IsSuggestionOnly) {
3623 rc.Report.Error (176, loc,
3624 "Static member `{0}' cannot be accessed with an instance reference, qualify it with a type name instead",
3625 GetSignatureForError ());
3629 InstanceExpression = null;
3635 if (InstanceExpression == null || InstanceExpression is TypeExpr) {
3636 if (InstanceExpression != null || !This.IsThisAvailable (rc, true)) {
3637 if (rc.HasSet (ResolveContext.Options.FieldInitializerScope)) {
3638 rc.Report.Error (236, loc,
3639 "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
3640 GetSignatureForError ());
3642 var fe = this as FieldExpr;
3643 if (fe != null && fe.Spec.MemberDefinition is PrimaryConstructorField) {
3644 if (rc.HasSet (ResolveContext.Options.BaseInitializer)) {
3645 rc.Report.Error (9005, loc, "Constructor initializer cannot access primary constructor parameters");
3647 rc.Report.Error (9006, loc, "An object reference is required to access primary constructor parameter `{0}'",
3651 rc.Report.Error (120, loc,
3652 "An object reference is required to access non-static member `{0}'",
3653 GetSignatureForError ());
3657 InstanceExpression = new CompilerGeneratedThis (rc.CurrentType, loc).Resolve (rc);
3661 if (!TypeManager.IsFamilyAccessible (rc.CurrentType, DeclaringType)) {
3662 rc.Report.Error (38, loc,
3663 "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
3664 DeclaringType.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3667 InstanceExpression = new This (loc).Resolve (rc);
3671 var me = InstanceExpression as MemberExpr;
3673 me.ResolveInstanceExpressionCore (rc, rhs);
3675 var fe = me as FieldExpr;
3676 if (fe != null && fe.IsMarshalByRefAccess (rc)) {
3677 rc.Report.SymbolRelatedToPreviousError (me.DeclaringType);
3678 rc.Report.Warning (1690, 1, loc,
3679 "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
3680 me.GetSignatureForError ());
3687 // Additional checks for l-value member access
3690 if (InstanceExpression is UnboxCast) {
3691 rc.Report.Error (445, InstanceExpression.Location, "Cannot modify the result of an unboxing conversion");
3698 public virtual MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
3700 if (left != null && !ConditionalAccess && !ec.HasSet (ResolveContext.Options.NameOfScope) && left.IsNull && TypeSpec.IsReferenceType (left.Type)) {
3701 ec.Report.Warning (1720, 1, left.Location,
3702 "Expression will always cause a `{0}'", "System.NullReferenceException");
3705 InstanceExpression = left;
3709 public virtual void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
3714 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
3716 var inst = new InstanceEmitter (InstanceExpression, TypeSpec.IsValueType (InstanceExpression.Type));
3717 inst.Emit (ec, ConditionalAccess);
3719 if (prepare_for_load)
3720 ec.Emit (OpCodes.Dup);
3723 public abstract void SetTypeArguments (ResolveContext ec, TypeArguments ta);
3726 public class ExtensionMethodCandidates
3728 readonly NamespaceContainer container;
3729 readonly IList<MethodSpec> methods;
3731 readonly IMemberContext context;
3733 public ExtensionMethodCandidates (IMemberContext context, IList<MethodSpec> methods, NamespaceContainer nsContainer, int lookupIndex)
3735 this.context = context;
3736 this.methods = methods;
3737 this.container = nsContainer;
3738 this.index = lookupIndex;
3741 public NamespaceContainer Container {
3747 public IMemberContext Context {
3753 public int LookupIndex {
3759 public IList<MethodSpec> Methods {
3767 // Represents a group of extension method candidates for whole namespace
3769 class ExtensionMethodGroupExpr : MethodGroupExpr, OverloadResolver.IErrorHandler
3771 ExtensionMethodCandidates candidates;
3772 public Expression ExtensionExpression;
3774 public ExtensionMethodGroupExpr (ExtensionMethodCandidates candidates, Expression extensionExpr, Location loc)
3775 : base (candidates.Methods.Cast<MemberSpec>().ToList (), extensionExpr.Type, loc)
3777 this.candidates = candidates;
3778 this.ExtensionExpression = extensionExpr;
3781 public override bool IsStatic {
3782 get { return true; }
3786 // For extension methodgroup we are not looking for base members but parent
3787 // namespace extension methods
3789 public override IList<MemberSpec> GetBaseMembers (TypeSpec type)
3791 // TODO: candidates are null only when doing error reporting, that's
3792 // incorrect. We have to discover same extension methods in error mode
3793 if (candidates == null)
3796 int arity = type_arguments == null ? 0 : type_arguments.Count;
3798 candidates = candidates.Container.LookupExtensionMethod (candidates.Context, Name, arity, candidates.LookupIndex);
3799 if (candidates == null)
3802 return candidates.Methods.Cast<MemberSpec> ().ToList ();
3805 public static bool IsExtensionTypeCompatible (TypeSpec argType, TypeSpec extensionType)
3808 // Indentity, implicit reference or boxing conversion must exist for the extension parameter
3810 // LAMESPEC: or implicit type parameter conversion
3812 return argType == extensionType ||
3813 TypeSpecComparer.IsEqual (argType, extensionType) ||
3814 Convert.ImplicitReferenceConversionExists (argType, extensionType, false) ||
3815 Convert.ImplicitBoxingConversion (null, argType, extensionType) != null;
3818 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
3820 rc.Report.Error (8093, expr.Location, "An argument to nameof operator cannot be extension method group");
3823 public override MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
3825 // We are already here
3829 public override MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments arguments, OverloadResolver.IErrorHandler ehandler, OverloadResolver.Restrictions restr)
3831 if (arguments == null)
3832 arguments = new Arguments (1);
3834 ExtensionExpression = ExtensionExpression.Resolve (ec);
3835 if (ExtensionExpression == null)
3838 var cand = candidates;
3839 var atype = ConditionalAccess ? Argument.AType.ExtensionTypeConditionalAccess : Argument.AType.ExtensionType;
3840 arguments.Insert (0, new Argument (ExtensionExpression, atype));
3841 var res = base.OverloadResolve (ec, ref arguments, ehandler ?? this, restr);
3843 // Restore candidates in case we are running in probing mode
3846 // Store resolved argument and restore original arguments
3848 // Clean-up modified arguments for error reporting
3849 arguments.RemoveAt (0);
3853 var me = ExtensionExpression as MemberExpr;
3855 me.ResolveInstanceExpression (ec, null);
3856 var fe = me as FieldExpr;
3858 fe.Spec.MemberDefinition.SetIsUsed ();
3861 InstanceExpression = null;
3865 #region IErrorHandler Members
3867 bool OverloadResolver.IErrorHandler.AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous)
3872 bool OverloadResolver.IErrorHandler.ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument arg, int index)
3874 rc.Report.SymbolRelatedToPreviousError (best);
3877 rc.Report.Error (1929, loc,
3878 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' requires an instance of type `{3}'",
3879 queried_type.GetSignatureForError (), Name, best.GetSignatureForError (), ((MethodSpec)best).Parameters.ExtensionMethodType.GetSignatureForError ());
3881 rc.Report.Error (1928, loc,
3882 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments",
3883 queried_type.GetSignatureForError (), Name, best.GetSignatureForError ());
3889 bool OverloadResolver.IErrorHandler.NoArgumentMatch (ResolveContext rc, MemberSpec best)
3894 bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best)
3903 /// MethodGroupExpr represents a group of method candidates which
3904 /// can be resolved to the best method overload
3906 public class MethodGroupExpr : MemberExpr, OverloadResolver.IBaseMembersProvider
3908 static readonly MemberSpec[] Excluded = new MemberSpec[0];
3910 protected IList<MemberSpec> Methods;
3911 MethodSpec best_candidate;
3912 TypeSpec best_candidate_return;
3913 protected TypeArguments type_arguments;
3915 SimpleName simple_name;
3916 protected TypeSpec queried_type;
3918 public MethodGroupExpr (IList<MemberSpec> mi, TypeSpec type, Location loc)
3922 this.type = InternalType.MethodGroup;
3924 eclass = ExprClass.MethodGroup;
3925 queried_type = type;
3928 public MethodGroupExpr (MethodSpec m, TypeSpec type, Location loc)
3929 : this (new MemberSpec[] { m }, type, loc)
3935 public MethodSpec BestCandidate {
3937 return best_candidate;
3941 public TypeSpec BestCandidateReturnType {
3943 return best_candidate_return;
3947 public IList<MemberSpec> Candidates {
3953 protected override TypeSpec DeclaringType {
3955 return queried_type;
3959 public bool IsConditionallyExcluded {
3961 return Methods == Excluded;
3965 public override bool IsInstance {
3967 if (best_candidate != null)
3968 return !best_candidate.IsStatic;
3974 public override bool IsSideEffectFree {
3976 return InstanceExpression == null || InstanceExpression.IsSideEffectFree;
3980 public override bool IsStatic {
3982 if (best_candidate != null)
3983 return best_candidate.IsStatic;
3989 public override string KindName {
3990 get { return "method"; }
3993 public override string Name {
3995 if (best_candidate != null)
3996 return best_candidate.Name;
3999 return Methods.First ().Name;
4006 // When best candidate is already know this factory can be used
4007 // to avoid expensive overload resolution to be called
4009 // NOTE: InstanceExpression has to be set manually
4011 public static MethodGroupExpr CreatePredefined (MethodSpec best, TypeSpec queriedType, Location loc)
4013 return new MethodGroupExpr (best, queriedType, loc) {
4014 best_candidate = best,
4015 best_candidate_return = best.ReturnType
4019 public override string GetSignatureForError ()
4021 if (best_candidate != null)
4022 return best_candidate.GetSignatureForError ();
4024 return Methods.First ().GetSignatureForError ();
4027 public override Expression CreateExpressionTree (ResolveContext ec)
4029 if (best_candidate == null) {
4030 ec.Report.Error (1953, loc, "An expression tree cannot contain an expression with method group");
4034 if (IsConditionallyExcluded)
4035 ec.Report.Error (765, loc,
4036 "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
4038 if (ConditionalAccess)
4039 Error_NullShortCircuitInsideExpressionTree (ec);
4041 return new TypeOfMethod (best_candidate, loc);
4044 protected override Expression DoResolve (ResolveContext ec)
4046 this.eclass = ExprClass.MethodGroup;
4048 if (InstanceExpression != null) {
4049 InstanceExpression = InstanceExpression.Resolve (ec);
4050 if (InstanceExpression == null)
4057 public override void Emit (EmitContext ec)
4059 throw new NotSupportedException ();
4062 public void EmitCall (EmitContext ec, Arguments arguments, bool statement)
4064 var call = new CallEmitter ();
4065 call.InstanceExpression = InstanceExpression;
4066 call.ConditionalAccess = ConditionalAccess;
4069 call.EmitStatement (ec, best_candidate, arguments, loc);
4071 call.Emit (ec, best_candidate, arguments, loc);
4074 public void EmitCall (EmitContext ec, Arguments arguments, TypeSpec conditionalAccessReceiver, bool statement)
4076 var ca = ec.ConditionalAccess;
4077 ec.ConditionalAccess = new ConditionalAccessContext (conditionalAccessReceiver, ec.DefineLabel ()) {
4078 Statement = statement
4081 EmitCall (ec, arguments, statement);
4083 ec.CloseConditionalAccess (!statement && best_candidate_return != conditionalAccessReceiver && conditionalAccessReceiver.IsNullableType ? conditionalAccessReceiver : null);
4084 ec.ConditionalAccess = ca;
4087 public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
4089 if (target != InternalType.ErrorType) {
4090 ec.Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
4091 Name, target.GetSignatureForError ());
4095 public bool HasAccessibleCandidate (ResolveContext rc)
4097 foreach (var candidate in Candidates) {
4098 if (candidate.IsAccessible (rc))
4105 public static bool IsExtensionMethodArgument (Expression expr)
4108 // LAMESPEC: No details about which expressions are not allowed
4110 return !(expr is TypeExpr) && !(expr is BaseThis);
4114 /// Find the Applicable Function Members (7.4.2.1)
4116 /// me: Method Group expression with the members to select.
4117 /// it might contain constructors or methods (or anything
4118 /// that maps to a method).
4120 /// Arguments: ArrayList containing resolved Argument objects.
4122 /// loc: The location if we want an error to be reported, or a Null
4123 /// location for "probing" purposes.
4125 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4126 /// that is the best match of me on Arguments.
4129 public virtual MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments args, OverloadResolver.IErrorHandler cerrors, OverloadResolver.Restrictions restr)
4131 // TODO: causes issues with probing mode, remove explicit Kind check
4132 if (best_candidate != null && best_candidate.Kind == MemberKind.Destructor)
4135 var r = new OverloadResolver (Methods, type_arguments, restr, loc);
4136 if ((restr & OverloadResolver.Restrictions.NoBaseMembers) == 0) {
4137 r.BaseMembersProvider = this;
4138 r.InstanceQualifier = this;
4141 if (cerrors != null)
4142 r.CustomErrors = cerrors;
4144 // TODO: When in probing mode do IsApplicable only and when called again do VerifyArguments for full error reporting
4145 best_candidate = r.ResolveMember<MethodSpec> (ec, ref args);
4146 if (best_candidate == null) {
4147 if (!r.BestCandidateIsDynamic)
4150 if (simple_name != null && ec.IsStatic)
4151 InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4156 // Overload resolver had to create a new method group, all checks bellow have already been executed
4157 if (r.BestCandidateNewMethodGroup != null)
4158 return r.BestCandidateNewMethodGroup;
4160 if (best_candidate.Kind == MemberKind.Method && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0) {
4161 if (InstanceExpression != null) {
4162 if (best_candidate.IsExtensionMethod && args[0].Expr == InstanceExpression) {
4163 InstanceExpression = null;
4165 if (simple_name != null && best_candidate.IsStatic) {
4166 InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4169 InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup | ResolveFlags.Type);
4173 ResolveInstanceExpression (ec, null);
4176 var base_override = CandidateToBaseOverride (ec, best_candidate);
4177 if (base_override == best_candidate) {
4178 best_candidate_return = r.BestCandidateReturnType;
4180 best_candidate = base_override;
4181 best_candidate_return = best_candidate.ReturnType;
4184 if (best_candidate.IsGeneric && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0 && TypeParameterSpec.HasAnyTypeParameterConstrained (best_candidate.GenericDefinition)) {
4185 ConstraintChecker cc = new ConstraintChecker (ec);
4186 cc.CheckAll (best_candidate.GetGenericMethodDefinition (), best_candidate.TypeArguments, best_candidate.Constraints, loc);
4190 // Additional check for possible imported base override method which
4191 // could not be done during IsOverrideMethodBaseTypeAccessible
4193 if (best_candidate.IsVirtual && (best_candidate.DeclaringType.Modifiers & Modifiers.PROTECTED) != 0 &&
4194 best_candidate.MemberDefinition.IsImported && !best_candidate.DeclaringType.IsAccessible (ec)) {
4195 ec.Report.SymbolRelatedToPreviousError (best_candidate);
4196 ErrorIsInaccesible (ec, best_candidate.GetSignatureForError (), loc);
4199 // Speed up the check by not doing it on disallowed targets
4200 if (best_candidate_return.Kind == MemberKind.Void && best_candidate.IsConditionallyExcluded (ec))
4206 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
4208 var fe = left as FieldExpr;
4211 // Using method-group on struct fields makes the struct assigned. I am not sure
4212 // why but that's what .net does
4214 fe.Spec.MemberDefinition.SetIsAssigned ();
4217 simple_name = original;
4218 return base.ResolveMemberAccess (ec, left, original);
4221 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
4223 if (!HasAccessibleCandidate (rc)) {
4224 ErrorIsInaccesible (rc, expr.GetSignatureForError (), loc);
4227 if (expr.HasTypeArguments) {
4228 rc.Report.Error (8084, expr.Location, "An argument to nameof operator cannot be method group with type arguments");
4232 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
4234 type_arguments = ta;
4237 #region IBaseMembersProvider Members
4239 public virtual IList<MemberSpec> GetBaseMembers (TypeSpec type)
4241 var baseType = type.BaseType;
4243 IList<MemberSpec> members = baseType == null ? null : MemberCache.FindMembers (baseType, Methods [0].Name, false);
4245 if (members == null && !type.IsInterface) {
4246 var tps = queried_type as TypeParameterSpec;
4248 members = MemberCache.FindInterfaceMembers (tps, Methods [0].Name);
4254 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4256 if (queried_type == member.DeclaringType)
4259 return MemberCache.FindMember (queried_type, new MemberFilter ((MethodSpec) member),
4260 BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as IParametersMember;
4264 // Extension methods lookup after ordinary methods candidates failed to apply
4266 public virtual MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4268 if (InstanceExpression == null || InstanceExpression.eclass == ExprClass.Type)
4271 if (!IsExtensionMethodArgument (InstanceExpression))
4274 int arity = type_arguments == null ? 0 : type_arguments.Count;
4275 var methods = rc.LookupExtensionMethod (Methods[0].Name, arity);
4276 if (methods == null)
4279 var emg = new ExtensionMethodGroupExpr (methods, InstanceExpression, loc);
4280 emg.SetTypeArguments (rc, type_arguments);
4281 emg.ConditionalAccess = ConditionalAccess;
4288 struct ConstructorInstanceQualifier : OverloadResolver.IInstanceQualifier
4290 public ConstructorInstanceQualifier (TypeSpec type)
4293 InstanceType = type;
4296 public TypeSpec InstanceType { get; private set; }
4298 public bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
4300 return MemberExpr.CheckProtectedMemberAccess (rc, member, InstanceType);
4304 public struct OverloadResolver
4307 public enum Restrictions
4311 ProbingOnly = 1 << 1,
4312 CovariantDelegate = 1 << 2,
4313 NoBaseMembers = 1 << 3,
4314 BaseMembersIncluded = 1 << 4,
4315 GetEnumeratorLookup = 1 << 5
4318 public interface IBaseMembersProvider
4320 IList<MemberSpec> GetBaseMembers (TypeSpec baseType);
4321 IParametersMember GetOverrideMemberParameters (MemberSpec member);
4322 MethodGroupExpr LookupExtensionMethod (ResolveContext rc);
4325 public interface IErrorHandler
4327 bool AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous);
4328 bool ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument a, int index);
4329 bool NoArgumentMatch (ResolveContext rc, MemberSpec best);
4330 bool TypeInferenceFailed (ResolveContext rc, MemberSpec best);
4333 public interface IInstanceQualifier
4335 TypeSpec InstanceType { get; }
4336 bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member);
4339 sealed class NoBaseMembers : IBaseMembersProvider
4341 public static readonly IBaseMembersProvider Instance = new NoBaseMembers ();
4343 public IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
4348 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4353 public MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4359 struct AmbiguousCandidate
4361 public readonly MemberSpec Member;
4362 public readonly bool Expanded;
4363 public readonly AParametersCollection Parameters;
4365 public AmbiguousCandidate (MemberSpec member, AParametersCollection parameters, bool expanded)
4368 Parameters = parameters;
4369 Expanded = expanded;
4374 IList<MemberSpec> members;
4375 TypeArguments type_arguments;
4376 IBaseMembersProvider base_provider;
4377 IErrorHandler custom_errors;
4378 IInstanceQualifier instance_qualifier;
4379 Restrictions restrictions;
4380 MethodGroupExpr best_candidate_extension_group;
4381 TypeSpec best_candidate_return_type;
4383 SessionReportPrinter lambda_conv_msgs;
4385 public OverloadResolver (IList<MemberSpec> members, Restrictions restrictions, Location loc)
4386 : this (members, null, restrictions, loc)
4390 public OverloadResolver (IList<MemberSpec> members, TypeArguments targs, Restrictions restrictions, Location loc)
4393 if (members == null || members.Count == 0)
4394 throw new ArgumentException ("empty members set");
4396 this.members = members;
4398 type_arguments = targs;
4399 this.restrictions = restrictions;
4400 if (IsDelegateInvoke)
4401 this.restrictions |= Restrictions.NoBaseMembers;
4403 base_provider = NoBaseMembers.Instance;
4408 public IBaseMembersProvider BaseMembersProvider {
4410 return base_provider;
4413 base_provider = value;
4417 public bool BestCandidateIsDynamic { get; set; }
4420 // Best candidate was found in newly created MethodGroupExpr, used by extension methods
4422 public MethodGroupExpr BestCandidateNewMethodGroup {
4424 return best_candidate_extension_group;
4429 // Return type can be different between best candidate and closest override
4431 public TypeSpec BestCandidateReturnType {
4433 return best_candidate_return_type;
4437 public IErrorHandler CustomErrors {
4439 return custom_errors;
4442 custom_errors = value;
4446 TypeSpec DelegateType {
4448 if ((restrictions & Restrictions.DelegateInvoke) == 0)
4449 throw new InternalErrorException ("Not running in delegate mode", loc);
4451 return members [0].DeclaringType;
4455 public IInstanceQualifier InstanceQualifier {
4457 return instance_qualifier;
4460 instance_qualifier = value;
4464 bool IsProbingOnly {
4466 return (restrictions & Restrictions.ProbingOnly) != 0;
4470 bool IsDelegateInvoke {
4472 return (restrictions & Restrictions.DelegateInvoke) != 0;
4479 // 7.4.3.3 Better conversion from expression
4480 // Returns : 1 if a->p is better,
4481 // 2 if a->q is better,
4482 // 0 if neither is better
4484 static int BetterExpressionConversion (ResolveContext ec, Argument a, TypeSpec p, TypeSpec q)
4486 TypeSpec argument_type = a.Type;
4489 // Exactly matching Expression phase
4493 // If argument is an anonymous function
4495 if (argument_type == InternalType.AnonymousMethod && ec.Module.Compiler.Settings.Version > LanguageVersion.ISO_2) {
4497 // p and q are delegate types or expression tree types
4499 if (p.IsExpressionTreeType || q.IsExpressionTreeType) {
4500 if (q.MemberDefinition != p.MemberDefinition) {
4505 // Uwrap delegate from Expression<T>
4507 q = TypeManager.GetTypeArguments (q) [0];
4508 p = TypeManager.GetTypeArguments (p) [0];
4511 var p_m = Delegate.GetInvokeMethod (p);
4512 var q_m = Delegate.GetInvokeMethod (q);
4515 // With identical parameter lists
4517 if (!TypeSpecComparer.Equals (p_m.Parameters.Types, q_m.Parameters.Types))
4525 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
4527 if (p.Kind == MemberKind.Void) {
4528 return q.Kind != MemberKind.Void ? 2 : 0;
4532 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
4534 if (q.Kind == MemberKind.Void) {
4535 return p.Kind != MemberKind.Void ? 1 : 0;
4538 var am = (AnonymousMethodExpression)a.Expr;
4541 // When anonymous method is an asynchronous, and P has a return type Task<Y1>, and Q has a return type Task<Y2>
4542 // better conversion is performed between underlying types Y1 and Y2
4544 if (p.IsGenericTask || q.IsGenericTask) {
4545 if (am.Block.IsAsync && p.IsGenericTask && q.IsGenericTask) {
4546 q = q.TypeArguments [0];
4547 p = p.TypeArguments [0];
4553 // An inferred return type X exists for E in the context of the parameter list, and
4554 // an identity conversion exists from X to the return type of D
4556 var inferred_type = am.InferReturnType (ec, null, orig_q);
4557 if (inferred_type != null) {
4558 if (inferred_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
4559 inferred_type = ec.BuiltinTypes.Object;
4561 if (inferred_type == p)
4564 if (inferred_type == q)
4570 if (argument_type == p)
4573 if (argument_type == q)
4576 return IsBetterConversionTarget (ec, p, q);
4579 static int IsBetterConversionTarget (ResolveContext rc, TypeSpec p, TypeSpec q)
4581 if ((p.Kind == MemberKind.Delegate || p.IsExpressionTreeType) && (q.Kind == MemberKind.Delegate || q.IsExpressionTreeType)) {
4583 if (p.Kind != MemberKind.Delegate) {
4584 p = TypeManager.GetTypeArguments (p) [0];
4587 if (q.Kind != MemberKind.Delegate) {
4588 q = TypeManager.GetTypeArguments (q) [0];
4591 var p_m = Delegate.GetInvokeMethod (p);
4592 var q_m = Delegate.GetInvokeMethod (q);
4598 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
4600 if (p.Kind == MemberKind.Void) {
4601 return q.Kind != MemberKind.Void ? 2 : 0;
4605 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
4607 if (q.Kind == MemberKind.Void) {
4608 return p.Kind != MemberKind.Void ? 1 : 0;
4611 return IsBetterConversionTarget (rc, p, q);
4614 if (p.IsGenericTask && q.IsGenericTask) {
4615 q = q.TypeArguments [0];
4616 p = p.TypeArguments [0];
4617 return IsBetterConversionTarget (rc, p, q);
4621 if (p.IsNullableType) {
4622 p = Nullable.NullableInfo.GetUnderlyingType (p);
4623 if (!BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (p))
4624 return BetterTypeConversionImplicitConversion (rc, p_orig, q);
4627 // Spec expects implicit conversion check between p and q, q and p
4628 // to be done before nullable unwrapping but that's expensive operation.
4630 // Extra manual tweak is needed because BetterTypeConversion works on
4638 if (q.IsNullableType) {
4639 q = Nullable.NullableInfo.GetUnderlyingType (q);
4640 if (!BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (q))
4641 return BetterTypeConversionImplicitConversion (rc, p_orig, q_orig);
4647 return BetterTypeConversion (rc, p, q);
4651 // 7.4.3.4 Better conversion from type
4653 public static int BetterTypeConversion (ResolveContext ec, TypeSpec p, TypeSpec q)
4655 if (p == null || q == null)
4656 throw new InternalErrorException ("BetterTypeConversion got a null conversion");
4658 switch (p.BuiltinType) {
4659 case BuiltinTypeSpec.Type.Int:
4660 if (q.BuiltinType == BuiltinTypeSpec.Type.UInt || q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4663 case BuiltinTypeSpec.Type.Long:
4664 if (q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4667 case BuiltinTypeSpec.Type.SByte:
4668 switch (q.BuiltinType) {
4669 case BuiltinTypeSpec.Type.Byte:
4670 case BuiltinTypeSpec.Type.UShort:
4671 case BuiltinTypeSpec.Type.UInt:
4672 case BuiltinTypeSpec.Type.ULong:
4676 case BuiltinTypeSpec.Type.Short:
4677 switch (q.BuiltinType) {
4678 case BuiltinTypeSpec.Type.UShort:
4679 case BuiltinTypeSpec.Type.UInt:
4680 case BuiltinTypeSpec.Type.ULong:
4684 case BuiltinTypeSpec.Type.Dynamic:
4685 // LAMESPEC: Dynamic conversions is not considered
4686 p = ec.Module.Compiler.BuiltinTypes.Object;
4690 switch (q.BuiltinType) {
4691 case BuiltinTypeSpec.Type.Int:
4692 if (p.BuiltinType == BuiltinTypeSpec.Type.UInt || p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4695 case BuiltinTypeSpec.Type.Long:
4696 if (p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4699 case BuiltinTypeSpec.Type.SByte:
4700 switch (p.BuiltinType) {
4701 case BuiltinTypeSpec.Type.Byte:
4702 case BuiltinTypeSpec.Type.UShort:
4703 case BuiltinTypeSpec.Type.UInt:
4704 case BuiltinTypeSpec.Type.ULong:
4708 case BuiltinTypeSpec.Type.Short:
4709 switch (p.BuiltinType) {
4710 case BuiltinTypeSpec.Type.UShort:
4711 case BuiltinTypeSpec.Type.UInt:
4712 case BuiltinTypeSpec.Type.ULong:
4716 case BuiltinTypeSpec.Type.Dynamic:
4717 // LAMESPEC: Dynamic conversions is not considered
4718 q = ec.Module.Compiler.BuiltinTypes.Object;
4722 return BetterTypeConversionImplicitConversion (ec, p, q);
4725 static int BetterTypeConversionImplicitConversion (ResolveContext rc, TypeSpec p, TypeSpec q)
4727 // TODO: this is expensive
4728 Expression p_tmp = new EmptyExpression (p);
4729 Expression q_tmp = new EmptyExpression (q);
4731 bool p_to_q = Convert.ImplicitConversionExists (rc, p_tmp, q);
4732 bool q_to_p = Convert.ImplicitConversionExists (rc, q_tmp, p);
4734 if (p_to_q && !q_to_p)
4737 if (q_to_p && !p_to_q)
4744 /// Determines "Better function" between candidate
4745 /// and the current best match
4748 /// Returns a boolean indicating :
4749 /// false if candidate ain't better
4750 /// true if candidate is better than the current best match
4752 bool BetterFunction (ResolveContext ec, Arguments args, MemberSpec candidate, AParametersCollection cparam, bool candidate_params,
4753 MemberSpec best, AParametersCollection bparam, bool best_params)
4755 AParametersCollection candidate_pd = ((IParametersMember) candidate).Parameters;
4756 AParametersCollection best_pd = ((IParametersMember) best).Parameters;
4758 int candidate_better_count = 0;
4759 int best_better_count = 0;
4761 bool are_equivalent = true;
4762 int args_count = args == null ? 0 : args.Count;
4766 for (int c_idx = 0, b_idx = 0; j < args_count; ++j, ++c_idx, ++b_idx) {
4769 // Default arguments are ignored for better decision
4770 if (a.IsDefaultArgument)
4774 // When comparing named argument the parameter type index has to be looked up
4775 // in original parameter set (override version for virtual members)
4777 NamedArgument na = a as NamedArgument;
4779 int idx = cparam.GetParameterIndexByName (na.Name);
4780 ct = candidate_pd.Types[idx];
4781 if (candidate_params && candidate_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4782 ct = TypeManager.GetElementType (ct);
4784 idx = bparam.GetParameterIndexByName (na.Name);
4785 bt = best_pd.Types[idx];
4786 if (best_params && best_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4787 bt = TypeManager.GetElementType (bt);
4789 ct = candidate_pd.Types[c_idx];
4790 bt = best_pd.Types[b_idx];
4792 if (candidate_params && candidate_pd.FixedParameters[c_idx].ModFlags == Parameter.Modifier.PARAMS) {
4793 ct = TypeManager.GetElementType (ct);
4797 if (best_params && best_pd.FixedParameters[b_idx].ModFlags == Parameter.Modifier.PARAMS) {
4798 bt = TypeManager.GetElementType (bt);
4803 if (TypeSpecComparer.IsEqual (ct, bt))
4806 are_equivalent = false;
4807 int result = BetterExpressionConversion (ec, a, ct, bt);
4809 // for each argument, the conversion to 'ct' should be no worse than
4810 // the conversion to 'bt'.
4813 // No optional parameters tie breaking rules for delegates overload resolution
4815 if ((restrictions & Restrictions.CovariantDelegate) != 0)
4818 ++best_better_count;
4822 // for at least one argument, the conversion to 'ct' should be better than
4823 // the conversion to 'bt'.
4825 ++candidate_better_count;
4828 if (candidate_better_count != 0 && best_better_count == 0)
4831 if (best_better_count > 0 && candidate_better_count == 0)
4835 // LAMESPEC: Tie-breaking rules for not equivalent parameter types
4837 if (!are_equivalent) {
4838 while (j < args_count && !args [j++].IsDefaultArgument) ;
4841 // A candidate with no default parameters is still better when there
4842 // is no better expression conversion and does not have more parameters
4844 if (candidate_pd.Count < best_pd.Count) {
4845 if (candidate_params)
4848 if (!candidate_pd.FixedParameters [j - 1].HasDefaultValue)
4851 if (best_pd.FixedParameters [j].HasDefaultValue)
4854 } else if (candidate_pd.Count == best_pd.Count) {
4855 if (candidate_params)
4858 if (!candidate_pd.FixedParameters [j - 1].HasDefaultValue && best_pd.FixedParameters [j - 1].HasDefaultValue)
4861 if (candidate_pd.FixedParameters [j - 1].HasDefaultValue && best_pd.HasParams)
4869 // If candidate is applicable in its normal form and best has a params array and is applicable
4870 // only in its expanded form, then candidate is better
4872 if (candidate_params != best_params)
4873 return !candidate_params;
4876 // We have not reached end of parameters list due to params or used default parameters
4878 bool defaults_ambiguity = false;
4879 while (j < candidate_pd.Count && j < best_pd.Count) {
4880 var cand_param = candidate_pd.FixedParameters [j];
4881 var best_param = best_pd.FixedParameters [j];
4883 if (cand_param.HasDefaultValue != best_param.HasDefaultValue && (!candidate_pd.HasParams || !best_pd.HasParams))
4884 return cand_param.HasDefaultValue;
4886 defaults_ambiguity = true;
4887 if (candidate_pd.Count == best_pd.Count) {
4891 // void Foo (int i = 0) is better than void Foo (params int[]) for Foo ()
4892 // void Foo (string[] s, string value = null) is better than Foo (string s, params string[]) for Foo (null) or Foo ()
4894 if (cand_param.HasDefaultValue) {
4903 // Neither is better when not all arguments are provided
4905 // void Foo (string s, int i = 0) <-> Foo (string s, int i = 0, int i2 = 0)
4906 // void Foo (string s, int i = 0) <-> Foo (string s, byte i = 0)
4907 // void Foo (string s, params int[]) <-> Foo (string s, params byte[])
4912 if (candidate_pd.Count != best_pd.Count) {
4913 if (defaults_ambiguity && best_pd.Count - 1 == j)
4914 return best_pd.HasParams;
4916 return candidate_pd.Count < best_pd.Count;
4920 // One is a non-generic method and second is a generic method, then non-generic is better
4922 if (best.IsGeneric != candidate.IsGeneric)
4923 return best.IsGeneric;
4926 // Both methods have the same number of parameters, and the parameters have equal types
4927 // Pick the "more specific" signature using rules over original (non-inflated) types
4929 var candidate_def_pd = ((IParametersMember) candidate.MemberDefinition).Parameters;
4930 var best_def_pd = ((IParametersMember) best.MemberDefinition).Parameters;
4932 bool specific_at_least_once = false;
4933 for (j = 0; j < args_count; ++j) {
4934 NamedArgument na = args_count == 0 ? null : args [j] as NamedArgument;
4936 ct = candidate_def_pd.Types[cparam.GetParameterIndexByName (na.Name)];
4937 bt = best_def_pd.Types[bparam.GetParameterIndexByName (na.Name)];
4939 ct = candidate_def_pd.Types[j];
4940 bt = best_def_pd.Types[j];
4945 TypeSpec specific = MoreSpecific (ct, bt);
4949 specific_at_least_once = true;
4952 if (specific_at_least_once)
4958 static bool CheckInflatedArguments (MethodSpec ms)
4960 if (!TypeParameterSpec.HasAnyTypeParameterTypeConstrained (ms.GenericDefinition))
4963 // Setup constraint checker for probing only
4964 ConstraintChecker cc = new ConstraintChecker (null);
4966 var mp = ms.Parameters.Types;
4967 for (int i = 0; i < mp.Length; ++i) {
4968 var type = mp[i] as InflatedTypeSpec;
4972 var targs = type.TypeArguments;
4973 if (targs.Length == 0)
4976 // TODO: Checking inflated MVAR arguments should be enough
4977 if (!cc.CheckAll (type.GetDefinition (), targs, type.Constraints, Location.Null))
4984 public static void Error_ConstructorMismatch (ResolveContext rc, TypeSpec type, int argCount, Location loc)
4986 rc.Report.Error (1729, loc,
4987 "The type `{0}' does not contain a constructor that takes `{1}' arguments",
4988 type.GetSignatureForError (), argCount.ToString ());
4992 // Determines if the candidate method is applicable to the given set of arguments
4993 // There could be two different set of parameters for same candidate where one
4994 // is the closest override for default values and named arguments checks and second
4995 // one being the virtual base for the parameter types and modifiers.
4997 // A return value rates candidate method compatibility,
4999 // 0 = the best, int.MaxValue = the worst
5001 int IsApplicable (ResolveContext ec, ref Arguments arguments, int arg_count, ref MemberSpec candidate, IParametersMember pm, ref bool params_expanded_form, ref bool dynamicArgument, ref TypeSpec returnType, bool errorMode)
5004 // Each step has allocated 10 values, it can overflow for
5005 // more than 10 arguments but that's ok as it's used for
5006 // better error reporting only
5008 const int ArgumentCountMismatch = 1000000000;
5009 const int NamedArgumentsMismatch = 100000000;
5010 const int DefaultArgumentMismatch = 10000000;
5011 const int UnexpectedTypeArguments = 1000000;
5012 const int TypeArgumentsMismatch = 100000;
5013 const int InflatedTypesMismatch = 10000;
5015 // Parameters of most-derived type used mainly for named and optional parameters
5016 var pd = pm.Parameters;
5018 // Used for params modifier only, that's legacy of C# 1.0 which uses base type for
5019 // params modifier instead of most-derived type
5020 var cpd = ((IParametersMember) candidate).Parameters;
5021 int param_count = pd.Count;
5022 int optional_count = 0;
5024 Arguments orig_args = arguments;
5026 if (arg_count != param_count) {
5028 // No arguments expansion when doing exact match for delegates
5030 if ((restrictions & Restrictions.CovariantDelegate) == 0) {
5031 for (int i = 0; i < pd.Count; ++i) {
5032 if (pd.FixedParameters[i].HasDefaultValue) {
5033 optional_count = pd.Count - i;
5039 if (optional_count != 0) {
5040 // Readjust expected number when params used
5041 if (cpd.HasParams) {
5043 if (arg_count < param_count)
5045 } else if (arg_count > param_count) {
5046 int args_gap = System.Math.Abs (arg_count - param_count);
5047 return ArgumentCountMismatch + args_gap;
5048 } else if (arg_count < param_count - optional_count) {
5049 int args_gap = System.Math.Abs (param_count - optional_count - arg_count);
5050 return ArgumentCountMismatch + args_gap;
5052 } else if (arg_count != param_count) {
5053 int args_gap = System.Math.Abs (arg_count - param_count);
5055 return ArgumentCountMismatch + args_gap;
5056 if (arg_count < param_count - 1)
5057 return ArgumentCountMismatch + args_gap;
5060 // Resize to fit optional arguments
5061 if (optional_count != 0) {
5062 if (arguments == null) {
5063 arguments = new Arguments (optional_count);
5065 // Have to create a new container, so the next run can do same
5066 var resized = new Arguments (param_count);
5067 resized.AddRange (arguments);
5068 arguments = resized;
5071 for (int i = arg_count; i < param_count; ++i)
5072 arguments.Add (null);
5076 if (arg_count > 0) {
5078 // Shuffle named arguments to the right positions if there are any
5080 if (arguments[arg_count - 1] is NamedArgument) {
5081 arg_count = arguments.Count;
5083 for (int i = 0; i < arg_count; ++i) {
5084 bool arg_moved = false;
5086 NamedArgument na = arguments[i] as NamedArgument;
5090 int index = pd.GetParameterIndexByName (na.Name);
5092 // Named parameter not found
5094 return NamedArgumentsMismatch - i;
5096 // already reordered
5101 if (index >= param_count) {
5102 // When using parameters which should not be available to the user
5103 if ((cpd.FixedParameters[index].ModFlags & Parameter.Modifier.PARAMS) == 0)
5106 arguments.Add (null);
5110 if (index == arg_count)
5111 return NamedArgumentsMismatch - i - 1;
5113 temp = arguments [index];
5115 // The slot has been taken by positional argument
5116 if (temp != null && !(temp is NamedArgument))
5117 return NamedArgumentsMismatch - i - 1;
5121 arguments = arguments.MarkOrderedArgument (na);
5125 if (arguments == orig_args) {
5126 arguments = new Arguments (orig_args.Count);
5127 arguments.AddRange (orig_args);
5130 arguments[index] = arguments[i];
5131 arguments[i] = temp;
5138 arg_count = arguments.Count;
5140 } else if (arguments != null) {
5141 arg_count = arguments.Count;
5145 // Don't do any expensive checks when the candidate cannot succeed
5147 if (arg_count != param_count && !cpd.HasParams)
5148 return DefaultArgumentMismatch - System.Math.Abs (param_count - arg_count);
5150 var dep = candidate.GetMissingDependencies ();
5152 ImportedTypeDefinition.Error_MissingDependency (ec, dep, loc);
5157 // 1. Handle generic method using type arguments when specified or type inference
5160 var ms = candidate as MethodSpec;
5161 if (ms != null && ms.IsGeneric) {
5162 if (type_arguments != null) {
5163 var g_args_count = ms.Arity;
5164 if (g_args_count != type_arguments.Count)
5165 return TypeArgumentsMismatch - System.Math.Abs (type_arguments.Count - g_args_count);
5167 if (type_arguments.Arguments != null)
5168 ms = ms.MakeGenericMethod (ec, type_arguments.Arguments);
5171 // Deploy custom error reporting for infered anonymous expression or lambda methods. When
5172 // probing lambda methods keep all errors reported in separate set and once we are done and no best
5173 // candidate was found use the set to report more details about what was wrong with lambda body.
5174 // The general idea is to distinguish between code errors and errors caused by
5175 // trial-and-error type inference
5177 if (lambda_conv_msgs == null) {
5178 for (int i = 0; i < arg_count; i++) {
5179 Argument a = arguments[i];
5183 var am = a.Expr as AnonymousMethodExpression;
5185 if (lambda_conv_msgs == null)
5186 lambda_conv_msgs = new SessionReportPrinter ();
5188 am.TypeInferenceReportPrinter = lambda_conv_msgs;
5193 var ti = new TypeInference (arguments);
5194 TypeSpec[] i_args = ti.InferMethodArguments (ec, ms);
5197 return TypeArgumentsMismatch - ti.InferenceScore;
5200 // Clear any error messages when the result was success
5202 if (lambda_conv_msgs != null)
5203 lambda_conv_msgs.ClearSession ();
5205 if (i_args.Length != 0) {
5207 for (int i = 0; i < i_args.Length; ++i) {
5208 var ta = i_args [i];
5209 if (!ta.IsAccessible (ec))
5210 return TypeArgumentsMismatch - i;
5214 ms = ms.MakeGenericMethod (ec, i_args);
5219 // Type arguments constraints have to match for the method to be applicable
5221 if (!CheckInflatedArguments (ms)) {
5223 return InflatedTypesMismatch;
5227 // We have a generic return type and at same time the method is override which
5228 // means we have to also inflate override return type in case the candidate is
5229 // best candidate and override return type is different to base return type.
5231 // virtual Foo<T, object> with override Foo<T, dynamic>
5233 if (candidate != pm) {
5234 MethodSpec override_ms = (MethodSpec) pm;
5235 var inflator = new TypeParameterInflator (ec, ms.DeclaringType, override_ms.GenericDefinition.TypeParameters, ms.TypeArguments);
5236 returnType = inflator.Inflate (returnType);
5238 returnType = ms.ReturnType;
5245 if (type_arguments != null)
5246 return UnexpectedTypeArguments;
5252 // 2. Each argument has to be implicitly convertible to method parameter
5254 Parameter.Modifier p_mod = 0;
5257 for (int i = 0; i < arg_count; i++) {
5258 Argument a = arguments[i];
5260 var fp = pd.FixedParameters[i];
5261 if (!fp.HasDefaultValue) {
5262 arguments = orig_args;
5263 return arg_count * 2 + 2;
5267 // Get the default value expression, we can use the same expression
5268 // if the type matches
5270 Expression e = fp.DefaultValue;
5272 e = ResolveDefaultValueArgument (ec, ptypes[i], e, loc);
5274 // Restore for possible error reporting
5275 for (int ii = i; ii < arg_count; ++ii)
5276 arguments.RemoveAt (i);
5278 return (arg_count - i) * 2 + 1;
5282 if ((fp.ModFlags & Parameter.Modifier.CallerMask) != 0) {
5284 // LAMESPEC: Attributes can be mixed together with build-in priority
5286 if ((fp.ModFlags & Parameter.Modifier.CallerLineNumber) != 0) {
5287 e = new IntLiteral (ec.BuiltinTypes, loc.Row, loc);
5288 } else if ((fp.ModFlags & Parameter.Modifier.CallerFilePath) != 0) {
5289 e = new StringLiteral (ec.BuiltinTypes, loc.SourceFile.GetFullPathName (ec.Module.Compiler.Settings.PathMap), loc);
5290 } else if (ec.MemberContext.CurrentMemberDefinition != null) {
5291 e = new StringLiteral (ec.BuiltinTypes, ec.MemberContext.CurrentMemberDefinition.GetCallerMemberName (), loc);
5295 arguments[i] = new Argument (e, Argument.AType.Default);
5299 if (p_mod != Parameter.Modifier.PARAMS) {
5300 p_mod = (pd.FixedParameters[i].ModFlags & ~Parameter.Modifier.PARAMS) | (cpd.FixedParameters[i].ModFlags & Parameter.Modifier.PARAMS);
5302 } else if (!params_expanded_form) {
5303 params_expanded_form = true;
5304 pt = ((ElementTypeSpec) pt).Element;
5310 if (!params_expanded_form) {
5311 if (a.IsExtensionType) {
5312 if (ExtensionMethodGroupExpr.IsExtensionTypeCompatible (a.Type, pt)) {
5317 score = IsArgumentCompatible (ec, a, p_mod, pt);
5320 dynamicArgument = true;
5325 // It can be applicable in expanded form (when not doing exact match like for delegates)
5327 if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0 && (restrictions & Restrictions.CovariantDelegate) == 0) {
5328 if (!params_expanded_form) {
5329 pt = ((ElementTypeSpec) pt).Element;
5333 score = IsArgumentCompatible (ec, a, Parameter.Modifier.NONE, pt);
5336 params_expanded_form = true;
5337 dynamicArgument = true;
5338 } else if (score == 0 || arg_count > pd.Count) {
5339 params_expanded_form = true;
5344 if (params_expanded_form)
5346 return (arg_count - i) * 2 + score;
5351 // Restore original arguments for dynamic binder to keep the intention of original source code
5353 if (dynamicArgument)
5354 arguments = orig_args;
5359 public static Expression ResolveDefaultValueArgument (ResolveContext ec, TypeSpec ptype, Expression e, Location loc)
5361 if (e is Constant && e.Type == ptype)
5365 // LAMESPEC: No idea what the exact rules are for System.Reflection.Missing.Value instead of null
5367 if (e == EmptyExpression.MissingValue && (ptype.BuiltinType == BuiltinTypeSpec.Type.Object || ptype.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) {
5368 e = new MemberAccess (new MemberAccess (new MemberAccess (
5369 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Reflection", loc), "Missing", loc), "Value", loc);
5370 } else if (e is Constant) {
5372 // Handles int to int? conversions, DefaultParameterValue check
5374 e = Convert.ImplicitConversionStandard (ec, e, ptype, loc);
5378 e = new DefaultValueExpression (new TypeExpression (ptype, loc), loc);
5381 return e.Resolve (ec);
5385 // Tests argument compatibility with the parameter
5386 // The possible return values are
5388 // 1 - modifier mismatch
5389 // 2 - type mismatch
5390 // -1 - dynamic binding required
5392 int IsArgumentCompatible (ResolveContext ec, Argument argument, Parameter.Modifier param_mod, TypeSpec parameter)
5395 // Types have to be identical when ref or out modifer
5396 // is used and argument is not of dynamic type
5398 if (((argument.Modifier | param_mod) & Parameter.Modifier.RefOutMask) != 0) {
5399 var arg_type = argument.Type;
5401 if ((argument.Modifier & Parameter.Modifier.RefOutMask) != (param_mod & Parameter.Modifier.RefOutMask)) {
5403 // Using dynamic for ref/out parameter can still succeed at runtime
5405 if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5411 if (arg_type != parameter) {
5412 if (arg_type == InternalType.VarOutType)
5416 // Do full equality check after quick path
5418 if (!TypeSpecComparer.IsEqual (arg_type, parameter)) {
5420 // Using dynamic for ref/out parameter can still succeed at runtime
5422 if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5430 if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (restrictions & Restrictions.CovariantDelegate) == 0)
5434 // Use implicit conversion in all modes to return same candidates when the expression
5435 // is used as argument or delegate conversion
5437 if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter)) {
5438 return parameter.IsDelegate && argument.Expr is AnonymousMethodExpression ? 2 : 3;
5445 static TypeSpec MoreSpecific (TypeSpec p, TypeSpec q)
5447 if (p.IsGenericParameter != q.IsGenericParameter)
5448 return p.IsGenericParameter ? q : p;
5450 var ac_p = p as ArrayContainer;
5452 var ac_q = q as ArrayContainer;
5456 TypeSpec specific = MoreSpecific (ac_p.Element, ac_q.Element);
5457 if (specific == ac_p.Element)
5459 if (specific == ac_q.Element)
5465 if (p.IsGeneric && q.IsGeneric) {
5466 var pargs = p.TypeArguments;
5467 var qargs = q.TypeArguments;
5469 bool p_specific_at_least_once = false;
5470 bool q_specific_at_least_once = false;
5472 for (int i = 0; i < pargs.Length; i++) {
5473 TypeSpec specific = MoreSpecific (pargs [i], qargs [i]);
5474 if (specific == pargs [i])
5475 p_specific_at_least_once = true;
5476 if (specific == qargs [i])
5477 q_specific_at_least_once = true;
5480 if (p_specific_at_least_once && !q_specific_at_least_once)
5482 if (!p_specific_at_least_once && q_specific_at_least_once)
5490 // Find the best method from candidate list
5492 public T ResolveMember<T> (ResolveContext rc, ref Arguments args) where T : MemberSpec, IParametersMember
5494 List<AmbiguousCandidate> ambiguous_candidates = null;
5496 MemberSpec best_candidate;
5497 Arguments best_candidate_args = null;
5498 bool best_candidate_params = false;
5499 bool best_candidate_dynamic = false;
5500 int best_candidate_rate;
5501 IParametersMember best_parameter_member = null;
5503 int args_count = args != null ? args.Count : 0;
5505 Arguments candidate_args = args;
5506 bool error_mode = false;
5507 MemberSpec invocable_member = null;
5508 int applicable_candidates = 0;
5511 best_candidate = null;
5512 best_candidate_rate = int.MaxValue;
5514 var type_members = members;
5516 for (int i = 0; i < type_members.Count; ++i) {
5517 var member = type_members[i];
5520 // Methods in a base class are not candidates if any method in a derived
5521 // class is applicable
5523 if ((member.Modifiers & Modifiers.OVERRIDE) != 0)
5527 if (!member.IsAccessible (rc))
5530 if (rc.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
5533 if ((member.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5534 instance_qualifier != null && !instance_qualifier.CheckProtectedMemberAccess (rc, member)) {
5539 IParametersMember pm = member as IParametersMember;
5542 // Will use it later to report ambiguity between best method and invocable member
5544 if (Invocation.IsMemberInvocable (member))
5545 invocable_member = member;
5551 // Overload resolution is looking for base member but using parameter names
5552 // and default values from the closest member. That means to do expensive lookup
5553 // for the closest override for virtual or abstract members
5555 if ((member.Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5556 var override_params = base_provider.GetOverrideMemberParameters (member);
5557 if (override_params != null)
5558 pm = override_params;
5562 // Check if the member candidate is applicable
5564 bool params_expanded_form = false;
5565 bool dynamic_argument = false;
5566 TypeSpec rt = pm.MemberType;
5567 int candidate_rate = IsApplicable (rc, ref candidate_args, args_count, ref member, pm, ref params_expanded_form, ref dynamic_argument, ref rt, error_mode);
5569 if (lambda_conv_msgs != null)
5570 lambda_conv_msgs.EndSession ();
5573 // How does it score compare to others
5575 if (candidate_rate < best_candidate_rate) {
5577 // Fatal error (missing dependency), cannot continue
5578 if (candidate_rate < 0)
5581 applicable_candidates = 1;
5582 if ((restrictions & Restrictions.GetEnumeratorLookup) != 0 && candidate_args.Count != 0) {
5583 // Only parameterless methods are considered
5585 best_candidate_rate = candidate_rate;
5586 best_candidate = member;
5587 best_candidate_args = candidate_args;
5588 best_candidate_params = params_expanded_form;
5589 best_candidate_dynamic = dynamic_argument;
5590 best_parameter_member = pm;
5591 best_candidate_return_type = rt;
5593 } else if (candidate_rate == 0) {
5595 // The member look is done per type for most operations but sometimes
5596 // it's not possible like for binary operators overload because they
5597 // are unioned between 2 sides
5599 if ((restrictions & Restrictions.BaseMembersIncluded) != 0) {
5600 if (TypeSpec.IsBaseClass (best_candidate.DeclaringType, member.DeclaringType, true))
5604 ++applicable_candidates;
5606 if (best_candidate.DeclaringType.IsInterface && member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5608 // We pack all interface members into top level type which makes the overload resolution
5609 // more complicated for interfaces. We compensate it by removing methods with same
5610 // signature when building the cache hence this path should not really be hit often
5613 // interface IA { void Foo (int arg); }
5614 // interface IB : IA { void Foo (params int[] args); }
5616 // IB::Foo is the best overload when calling IB.Foo (1)
5619 if (ambiguous_candidates != null) {
5620 foreach (var amb_cand in ambiguous_candidates) {
5621 if (member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5630 ambiguous_candidates = null;
5633 // Is the new candidate better
5634 is_better = BetterFunction (rc, candidate_args, member, pm.Parameters, params_expanded_form, best_candidate, best_parameter_member.Parameters, best_candidate_params);
5638 best_candidate = member;
5639 best_candidate_args = candidate_args;
5640 best_candidate_params = params_expanded_form;
5641 best_candidate_dynamic = dynamic_argument;
5642 best_parameter_member = pm;
5643 best_candidate_return_type = rt;
5645 // It's not better but any other found later could be but we are not sure yet
5646 if (ambiguous_candidates == null)
5647 ambiguous_candidates = new List<AmbiguousCandidate> ();
5649 ambiguous_candidates.Add (new AmbiguousCandidate (member, pm.Parameters, params_expanded_form));
5653 // Restore expanded arguments
5654 candidate_args = args;
5656 } while (best_candidate_rate != 0 && (type_members = base_provider.GetBaseMembers (type_members[0].DeclaringType)) != null);
5659 // We've found exact match
5661 if (best_candidate_rate == 0)
5665 // Try extension methods lookup when no ordinary method match was found and provider enables it
5668 var emg = base_provider.LookupExtensionMethod (rc);
5670 emg = emg.OverloadResolve (rc, ref args, null, restrictions);
5672 best_candidate_extension_group = emg;
5673 return (T) (MemberSpec) emg.BestCandidate;
5678 // Don't run expensive error reporting mode for probing
5685 if (lambda_conv_msgs != null && !lambda_conv_msgs.IsEmpty)
5688 lambda_conv_msgs = null;
5693 // No best member match found, report an error
5695 if (best_candidate_rate != 0 || error_mode) {
5696 ReportOverloadError (rc, best_candidate, best_parameter_member, best_candidate_args, best_candidate_params);
5700 if (best_candidate_dynamic) {
5701 if (args[0].IsExtensionType) {
5702 rc.Report.Error (1973, loc,
5703 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' cannot be dynamically dispatched. Consider calling the method without the extension method syntax",
5704 args [0].Type.GetSignatureForError (), best_candidate.Name, best_candidate.GetSignatureForError ());
5708 // Check type constraints only when explicit type arguments are used
5710 if (applicable_candidates == 1 && best_candidate.IsGeneric && type_arguments != null) {
5711 MethodSpec bc = best_candidate as MethodSpec;
5712 if (bc != null && TypeParameterSpec.HasAnyTypeParameterConstrained (bc.GenericDefinition)) {
5713 ConstraintChecker cc = new ConstraintChecker (rc);
5714 cc.CheckAll (bc.GetGenericMethodDefinition (), bc.TypeArguments, bc.Constraints, loc);
5718 BestCandidateIsDynamic = true;
5723 // These flags indicates we are running delegate probing conversion. No need to
5724 // do more expensive checks
5726 if ((restrictions & (Restrictions.ProbingOnly | Restrictions.CovariantDelegate)) == (Restrictions.CovariantDelegate | Restrictions.ProbingOnly))
5727 return (T) best_candidate;
5729 if (ambiguous_candidates != null) {
5731 // Now check that there are no ambiguities i.e the selected method
5732 // should be better than all the others
5734 for (int ix = 0; ix < ambiguous_candidates.Count; ix++) {
5735 var candidate = ambiguous_candidates [ix];
5737 if (!BetterFunction (rc, best_candidate_args, best_candidate, best_parameter_member.Parameters, best_candidate_params, candidate.Member, candidate.Parameters, candidate.Expanded)) {
5738 var ambiguous = candidate.Member;
5739 if (custom_errors == null || !custom_errors.AmbiguousCandidates (rc, best_candidate, ambiguous)) {
5740 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5741 rc.Report.SymbolRelatedToPreviousError (ambiguous);
5742 rc.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
5743 best_candidate.GetSignatureForError (), ambiguous.GetSignatureForError ());
5746 return (T) best_candidate;
5751 if (invocable_member != null && !IsProbingOnly) {
5752 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5753 rc.Report.SymbolRelatedToPreviousError (invocable_member);
5754 rc.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and invocable non-method `{1}'. Using method group",
5755 best_candidate.GetSignatureForError (), invocable_member.GetSignatureForError ());
5759 // And now check if the arguments are all
5760 // compatible, perform conversions if
5761 // necessary etc. and return if everything is
5764 if (!VerifyArguments (rc, ref best_candidate_args, best_candidate, best_parameter_member, best_candidate_params))
5767 if (best_candidate == null)
5771 // Don't run possibly expensive checks in probing mode
5773 if (!IsProbingOnly && !rc.IsInProbingMode) {
5775 // Check ObsoleteAttribute on the best method
5777 best_candidate.CheckObsoleteness (rc, loc);
5779 best_candidate.MemberDefinition.SetIsUsed ();
5782 args = best_candidate_args;
5783 return (T) best_candidate;
5786 public MethodSpec ResolveOperator (ResolveContext rc, ref Arguments args)
5788 return ResolveMember<MethodSpec> (rc, ref args);
5791 void ReportArgumentMismatch (ResolveContext ec, int idx, MemberSpec method,
5792 Argument a, AParametersCollection expected_par, TypeSpec paramType)
5794 if (custom_errors != null && custom_errors.ArgumentMismatch (ec, method, a, idx))
5797 if (a.Type == InternalType.ErrorType)
5800 if (a is CollectionElementInitializer.ElementInitializerArgument) {
5801 ec.Report.SymbolRelatedToPreviousError (method);
5802 if ((expected_par.FixedParameters[idx].ModFlags & Parameter.Modifier.RefOutMask) != 0) {
5803 ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have `ref' or `out' modifier",
5804 TypeManager.CSharpSignature (method));
5807 ec.Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
5808 TypeManager.CSharpSignature (method));
5809 } else if (IsDelegateInvoke) {
5810 ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
5811 DelegateType.GetSignatureForError ());
5813 ec.Report.SymbolRelatedToPreviousError (method);
5814 ec.Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
5815 method.GetSignatureForError ());
5818 Parameter.Modifier mod = idx >= expected_par.Count ? 0 : expected_par.FixedParameters[idx].ModFlags;
5820 string index = (idx + 1).ToString ();
5821 if (((mod & Parameter.Modifier.RefOutMask) ^ (a.Modifier & Parameter.Modifier.RefOutMask)) != 0) {
5822 if ((mod & Parameter.Modifier.RefOutMask) == 0)
5823 ec.Report.Error (1615, a.Expr.Location, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
5824 index, Parameter.GetModifierSignature (a.Modifier));
5826 ec.Report.Error (1620, a.Expr.Location, "Argument `#{0}' is missing `{1}' modifier",
5827 index, Parameter.GetModifierSignature (mod));
5829 string p1 = a.GetSignatureForError ();
5830 string p2 = paramType.GetSignatureForError ();
5833 p1 = a.Type.GetSignatureForErrorIncludingAssemblyName ();
5834 p2 = paramType.GetSignatureForErrorIncludingAssemblyName ();
5837 if ((mod & Parameter.Modifier.RefOutMask) != 0) {
5838 p1 = Parameter.GetModifierSignature (a.Modifier) + " " + p1;
5839 p2 = Parameter.GetModifierSignature (a.Modifier) + " " + p2;
5842 ec.Report.Error (1503, a.Expr.Location,
5843 "Argument `#{0}' cannot convert `{1}' expression to type `{2}'", index, p1, p2);
5848 // We have failed to find exact match so we return error info about the closest match
5850 void ReportOverloadError (ResolveContext rc, MemberSpec best_candidate, IParametersMember pm, Arguments args, bool params_expanded)
5852 int ta_count = type_arguments == null ? 0 : type_arguments.Count;
5853 int arg_count = args == null ? 0 : args.Count;
5855 if (ta_count != best_candidate.Arity && (ta_count > 0 || ((IParametersMember) best_candidate).Parameters.IsEmpty)) {
5856 var mg = new MethodGroupExpr (new [] { best_candidate }, best_candidate.DeclaringType, loc);
5857 mg.Error_TypeArgumentsCannotBeUsed (rc, best_candidate, loc);
5861 if (lambda_conv_msgs != null && lambda_conv_msgs.Merge (rc.Report.Printer)) {
5866 if ((best_candidate.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5867 InstanceQualifier != null && !InstanceQualifier.CheckProtectedMemberAccess (rc, best_candidate)) {
5868 MemberExpr.Error_ProtectedMemberAccess (rc, best_candidate, InstanceQualifier.InstanceType, loc);
5872 // For candidates which match on parameters count report more details about incorrect arguments
5875 if (pm.Parameters.Count == arg_count || params_expanded || HasUnfilledParams (best_candidate, pm, args)) {
5876 // Reject any inaccessible member
5877 if (!best_candidate.IsAccessible (rc) || !best_candidate.DeclaringType.IsAccessible (rc)) {
5878 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5879 Expression.ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
5883 var ms = best_candidate as MethodSpec;
5884 if (ms != null && ms.IsGeneric) {
5885 bool constr_ok = true;
5886 if (ms.TypeArguments != null)
5887 constr_ok = new ConstraintChecker (rc.MemberContext).CheckAll (ms.GetGenericMethodDefinition (), ms.TypeArguments, ms.Constraints, loc);
5889 if (ta_count == 0 && ms.TypeArguments == null) {
5890 if (custom_errors != null && custom_errors.TypeInferenceFailed (rc, best_candidate))
5894 rc.Report.Error (411, loc,
5895 "The type arguments for method `{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly",
5896 ms.GetGenericMethodDefinition ().GetSignatureForError ());
5903 VerifyArguments (rc, ref args, best_candidate, pm, params_expanded);
5909 // We failed to find any method with correct argument count, report best candidate
5911 if (custom_errors != null && custom_errors.NoArgumentMatch (rc, best_candidate))
5914 if (best_candidate.Kind == MemberKind.Constructor) {
5915 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5916 Error_ConstructorMismatch (rc, best_candidate.DeclaringType, arg_count, loc);
5917 } else if (IsDelegateInvoke) {
5918 rc.Report.SymbolRelatedToPreviousError (DelegateType);
5919 rc.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
5920 DelegateType.GetSignatureForError (), arg_count.ToString ());
5922 string name = best_candidate.Kind == MemberKind.Indexer ? "this" : best_candidate.Name;
5923 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5924 rc.Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
5925 name, arg_count.ToString ());
5929 static bool HasUnfilledParams (MemberSpec best_candidate, IParametersMember pm, Arguments args)
5931 var p = ((IParametersMember)best_candidate).Parameters;
5936 for (int i = p.Count - 1; i != 0; --i) {
5937 var fp = p.FixedParameters [i];
5938 if ((fp.ModFlags & Parameter.Modifier.PARAMS) == 0)
5948 foreach (var arg in args) {
5949 var na = arg as NamedArgument;
5953 if (na.Name == name) {
5962 return args.Count + 1 == pm.Parameters.Count;
5965 bool VerifyArguments (ResolveContext ec, ref Arguments args, MemberSpec member, IParametersMember pm, bool chose_params_expanded)
5967 var pd = pm.Parameters;
5968 var cpd = ((IParametersMember) member).Parameters;
5969 var ptypes = cpd.Types;
5971 Parameter.Modifier p_mod = 0;
5973 int a_idx = 0, a_pos = 0;
5975 ArrayInitializer params_initializers = null;
5976 bool has_unsafe_arg = pm.MemberType.IsPointer;
5977 int arg_count = args == null ? 0 : args.Count;
5979 for (; a_idx < arg_count; a_idx++, ++a_pos) {
5984 if (p_mod != Parameter.Modifier.PARAMS) {
5985 p_mod = cpd.FixedParameters [a_idx].ModFlags;
5987 has_unsafe_arg |= pt.IsPointer;
5989 if (p_mod == Parameter.Modifier.PARAMS) {
5990 if (chose_params_expanded) {
5991 params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
5992 pt = TypeManager.GetElementType (pt);
5998 // Types have to be identical when ref or out modifer is used
6000 if (((a.Modifier | p_mod) & Parameter.Modifier.RefOutMask) != 0) {
6001 if ((a.Modifier & Parameter.Modifier.RefOutMask) != (p_mod & Parameter.Modifier.RefOutMask))
6004 var arg_type = a.Type;
6008 if (arg_type == InternalType.VarOutType) {
6010 // Set underlying variable type based on parameter type
6012 ((DeclarationExpression)a.Expr).Variable.Type = pt;
6016 if (!TypeSpecComparer.IsEqual (arg_type, pt))
6020 NamedArgument na = a as NamedArgument;
6022 int name_index = pd.GetParameterIndexByName (na.Name);
6023 if (name_index < 0 || name_index >= pd.Count) {
6024 if (IsDelegateInvoke) {
6025 ec.Report.SymbolRelatedToPreviousError (DelegateType);
6026 ec.Report.Error (1746, na.Location,
6027 "The delegate `{0}' does not contain a parameter named `{1}'",
6028 DelegateType.GetSignatureForError (), na.Name);
6030 ec.Report.SymbolRelatedToPreviousError (member);
6031 ec.Report.Error (1739, na.Location,
6032 "The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
6033 TypeManager.CSharpSignature (member), na.Name);
6035 } else if (args[name_index] != a && args[name_index] != null) {
6036 if (IsDelegateInvoke)
6037 ec.Report.SymbolRelatedToPreviousError (DelegateType);
6039 ec.Report.SymbolRelatedToPreviousError (member);
6041 ec.Report.Error (1744, na.Location,
6042 "Named argument `{0}' cannot be used for a parameter which has positional argument specified",
6047 if (a.Expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
6050 if ((restrictions & Restrictions.CovariantDelegate) != 0 && !Delegate.IsTypeCovariant (ec, a.Expr.Type, pt)) {
6051 if (a.IsExtensionType) {
6052 // TODO: Should report better message type, something similar to CS1928/1929 instead of
6053 // CS1061 but that still better than confusing CS0123
6054 var ma = new MemberAccess (a.Expr, member.Name, loc);
6055 ma.Error_TypeDoesNotContainDefinition (ec, a.Expr.Type, ma.Name);
6057 custom_errors.NoArgumentMatch (ec, member);
6063 if (a.IsExtensionType) {
6064 if (a.Expr.Type == pt || TypeSpecComparer.IsEqual (a.Expr.Type, pt)) {
6067 conv = Convert.ImplicitReferenceConversion (a.Expr, pt, false);
6069 conv = Convert.ImplicitBoxingConversion (a.Expr, a.Expr.Type, pt);
6072 conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
6079 // Convert params arguments to an array initializer
6081 if (params_initializers != null) {
6082 // we choose to use 'a.Expr' rather than 'conv' so that
6083 // we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
6084 params_initializers.Add (a.Expr);
6085 args.RemoveAt (a_idx--);
6091 // Update the argument with the implicit conversion
6095 if (a_idx != arg_count) {
6097 // Convert all var out argument to error type for less confusing error reporting
6098 // when no matching overload is found
6100 for (; a_idx < arg_count; a_idx++) {
6101 var arg = args [a_idx];
6105 if (arg.Type == InternalType.VarOutType) {
6106 ((DeclarationExpression)arg.Expr).Variable.Type = InternalType.ErrorType;
6110 ReportArgumentMismatch (ec, a_pos, member, a, pd, pt);
6115 // Fill not provided arguments required by params modifier
6117 if (params_initializers == null && arg_count + 1 == pd.Count) {
6119 args = new Arguments (1);
6121 pt = ptypes[pd.Count - 1];
6122 pt = TypeManager.GetElementType (pt);
6123 has_unsafe_arg |= pt.IsPointer;
6124 params_initializers = new ArrayInitializer (0, loc);
6128 // Append an array argument with all params arguments
6130 if (params_initializers != null) {
6131 args.Add (new Argument (
6132 new ArrayCreation (new TypeExpression (pt, loc), params_initializers, loc).Resolve (ec)));
6136 if (has_unsafe_arg) {
6137 if (ec.CurrentIterator != null) {
6138 Expression.UnsafeInsideIteratorError (ec, loc);
6139 } else if (!ec.IsUnsafe) {
6140 Expression.UnsafeError (ec, loc);
6145 // We could infer inaccesible type arguments
6147 if (type_arguments == null && member.IsGeneric) {
6148 var ms = (MethodSpec) member;
6149 foreach (var ta in ms.TypeArguments) {
6150 if (!ta.IsAccessible (ec)) {
6151 ec.Report.SymbolRelatedToPreviousError (ta);
6152 Expression.ErrorIsInaccesible (ec, member.GetSignatureForError (), loc);
6162 public class ConstantExpr : MemberExpr
6164 readonly ConstSpec constant;
6166 public ConstantExpr (ConstSpec constant, Location loc)
6168 this.constant = constant;
6172 public override string Name {
6173 get { throw new NotImplementedException (); }
6176 public override string KindName {
6177 get { return "constant"; }
6180 public override bool IsInstance {
6181 get { return !IsStatic; }
6184 public override bool IsStatic {
6185 get { return true; }
6188 protected override TypeSpec DeclaringType {
6189 get { return constant.DeclaringType; }
6192 public override Expression CreateExpressionTree (ResolveContext ec)
6194 throw new NotSupportedException ("ET");
6197 protected override Expression DoResolve (ResolveContext rc)
6199 ResolveInstanceExpression (rc, null);
6200 DoBestMemberChecks (rc, constant);
6202 if (rc.HasSet (ResolveContext.Options.NameOfScope)) {
6203 eclass = ExprClass.Value;
6204 type = constant.MemberType;
6208 var c = constant.GetConstant (rc);
6210 // Creates reference expression to the constant value
6211 return Constant.CreateConstantFromValue (constant.MemberType, c.GetValue (), loc);
6214 public override void Emit (EmitContext ec)
6216 throw new NotSupportedException ();
6219 public override string GetSignatureForError ()
6221 return constant.GetSignatureForError ();
6224 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
6226 constant.CheckObsoleteness (rc, expr.Location);
6229 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6231 Error_TypeArgumentsCannotBeUsed (ec, "constant", GetSignatureForError (), loc);
6236 // Fully resolved expression that references a Field
6238 public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference
6240 protected FieldSpec spec;
6241 VariableInfo variable_info;
6243 LocalTemporary temp;
6246 protected FieldExpr (Location l)
6251 public FieldExpr (FieldSpec spec, Location loc)
6256 type = spec.MemberType;
6259 public FieldExpr (FieldBase fi, Location l)
6266 public override string Name {
6272 public bool IsHoisted {
6274 IVariableReference hv = InstanceExpression as IVariableReference;
6275 return hv != null && hv.IsHoisted;
6279 public override bool IsInstance {
6281 return !spec.IsStatic;
6285 public override bool IsStatic {
6287 return spec.IsStatic;
6291 public override string KindName {
6292 get { return "field"; }
6295 public FieldSpec Spec {
6301 protected override TypeSpec DeclaringType {
6303 return spec.DeclaringType;
6307 public VariableInfo VariableInfo {
6309 return variable_info;
6315 public override string GetSignatureForError ()
6317 return spec.GetSignatureForError ();
6320 public bool IsMarshalByRefAccess (ResolveContext rc)
6322 // Checks possible ldflda of field access expression
6323 return !spec.IsStatic && TypeSpec.IsValueType (spec.MemberType) && !(InstanceExpression is This) &&
6324 rc.Module.PredefinedTypes.MarshalByRefObject.Define () &&
6325 TypeSpec.IsBaseClass (spec.DeclaringType, rc.Module.PredefinedTypes.MarshalByRefObject.TypeSpec, false);
6328 public void SetHasAddressTaken ()
6330 IVariableReference vr = InstanceExpression as IVariableReference;
6332 vr.SetHasAddressTaken ();
6336 protected override void CloneTo (CloneContext clonectx, Expression target)
6338 var t = (FieldExpr) target;
6340 if (InstanceExpression != null)
6341 t.InstanceExpression = InstanceExpression.Clone (clonectx);
6344 public override Expression CreateExpressionTree (ResolveContext ec)
6346 if (ConditionalAccess) {
6347 Error_NullShortCircuitInsideExpressionTree (ec);
6350 return CreateExpressionTree (ec, true);
6353 public Expression CreateExpressionTree (ResolveContext ec, bool convertInstance)
6356 Expression instance;
6358 if (InstanceExpression == null) {
6359 instance = new NullLiteral (loc);
6360 } else if (convertInstance) {
6361 instance = InstanceExpression.CreateExpressionTree (ec);
6363 args = new Arguments (1);
6364 args.Add (new Argument (InstanceExpression));
6365 instance = CreateExpressionFactoryCall (ec, "Constant", args);
6368 args = Arguments.CreateForExpressionTree (ec, null,
6370 CreateTypeOfExpression ());
6372 return CreateExpressionFactoryCall (ec, "Field", args);
6375 public Expression CreateTypeOfExpression ()
6377 return new TypeOfField (spec, loc);
6380 protected override Expression DoResolve (ResolveContext ec)
6382 spec.MemberDefinition.SetIsUsed ();
6384 return DoResolve (ec, null);
6387 Expression DoResolve (ResolveContext ec, Expression rhs)
6389 bool lvalue_instance = rhs != null && IsInstance && spec.DeclaringType.IsStruct;
6392 ResolveConditionalAccessReceiver (ec);
6394 if (ResolveInstanceExpression (ec, rhs)) {
6395 // Resolve the field's instance expression while flow analysis is turned
6396 // off: when accessing a field "a.b", we must check whether the field
6397 // "a.b" is initialized, not whether the whole struct "a" is initialized.
6399 if (lvalue_instance) {
6400 bool out_access = rhs == EmptyExpression.OutAccess || rhs == EmptyExpression.LValueMemberOutAccess;
6402 Expression right_side =
6403 out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
6405 InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side);
6407 InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue);
6410 if (InstanceExpression == null)
6414 DoBestMemberChecks (ec, spec);
6416 if (conditional_access_receiver)
6417 ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false);
6420 var fb = spec as FixedFieldSpec;
6421 IVariableReference var = InstanceExpression as IVariableReference;
6424 IFixedExpression fe = InstanceExpression as IFixedExpression;
6425 if (!ec.HasSet (ResolveContext.Options.FixedInitializerScope) && (fe == null || !fe.IsFixed)) {
6426 ec.Report.Error (1666, loc, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
6429 if (InstanceExpression.eclass != ExprClass.Variable) {
6430 ec.Report.SymbolRelatedToPreviousError (spec);
6431 ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
6432 TypeManager.GetFullNameSignature (spec));
6433 } else if (var != null && var.IsHoisted) {
6434 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc);
6437 return new FixedBufferPtr (this, fb.ElementType, loc).Resolve (ec);
6441 // Set flow-analysis variable info for struct member access. It will be check later
6442 // for precise error reporting
6444 if (var != null && var.VariableInfo != null && InstanceExpression.Type.IsStruct) {
6445 variable_info = var.VariableInfo.GetStructFieldInfo (Name);
6448 if (conditional_access_receiver)
6449 type = LiftMemberType (ec, type);
6451 if (ConditionalAccess && InstanceExpression != null && InstanceExpression.IsNull)
6452 return Constant.CreateConstantFromValue (type, null, loc);
6454 eclass = ExprClass.Variable;
6458 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
6460 spec.CheckObsoleteness (rc, expr.Location);
6463 public void SetFieldAssigned (FlowAnalysisContext fc)
6468 bool lvalue_instance = spec.DeclaringType.IsStruct;
6469 if (lvalue_instance) {
6470 var var = InstanceExpression as IVariableReference;
6471 if (var != null && var.VariableInfo != null) {
6472 fc.SetStructFieldAssigned (var.VariableInfo, Name);
6476 var fe = InstanceExpression as FieldExpr;
6478 Expression instance;
6481 instance = fe.InstanceExpression;
6482 var fe_instance = instance as FieldExpr;
6483 if ((fe_instance != null && !fe_instance.IsStatic) || instance is LocalVariableReference) {
6484 if (TypeSpec.IsReferenceType (fe.Type) && instance.Type.IsStruct) {
6485 var var = InstanceExpression as IVariableReference;
6486 if (var != null && var.VariableInfo == null) {
6487 var var_inst = instance as IVariableReference;
6488 if (var_inst == null || (var_inst.VariableInfo != null && !fc.IsDefinitelyAssigned (var_inst.VariableInfo)))
6489 fc.Report.Warning (1060, 1, fe.loc, "Use of possibly unassigned field `{0}'", fe.Name);
6493 if (fe_instance != null) {
6502 if (instance != null && TypeSpec.IsReferenceType (instance.Type))
6503 instance.FlowAnalysis (fc);
6505 if (TypeSpec.IsReferenceType (InstanceExpression.Type))
6506 InstanceExpression.FlowAnalysis (fc);
6510 Expression Error_AssignToReadonly (ResolveContext rc, Expression right_side)
6512 // The return value is always null. Returning a value simplifies calling code.
6514 if (right_side == EmptyExpression.OutAccess) {
6516 rc.Report.Error (199, loc, "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6517 GetSignatureForError ());
6519 rc.Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6520 GetSignatureForError ());
6526 if (right_side == EmptyExpression.LValueMemberAccess) {
6527 // Already reported as CS1648/CS1650
6531 if (right_side == EmptyExpression.LValueMemberOutAccess) {
6533 rc.Report.Error (1651, loc, "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6534 GetSignatureForError ());
6536 rc.Report.Error (1649, loc, "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6537 GetSignatureForError ());
6543 rc.Report.Error (198, loc, "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
6544 GetSignatureForError ());
6546 rc.Report.Error (191, loc, "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
6547 GetSignatureForError ());
6553 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6555 if (HasConditionalAccess ())
6556 Error_NullPropagatingLValue (ec);
6558 if (spec is FixedFieldSpec) {
6559 // It could be much better error message but we want to be error compatible
6560 Error_ValueAssignment (ec, right_side);
6563 Expression e = DoResolve (ec, right_side);
6568 spec.MemberDefinition.SetIsAssigned ();
6570 if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess) &&
6571 (spec.Modifiers & Modifiers.VOLATILE) != 0) {
6572 ec.Report.Warning (420, 1, loc,
6573 "`{0}': A volatile field references will not be treated as volatile",
6574 spec.GetSignatureForError ());
6577 if (spec.IsReadOnly) {
6578 // InitOnly fields can only be assigned in constructors or initializers
6579 if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
6580 return Error_AssignToReadonly (ec, right_side);
6582 if (ec.HasSet (ResolveContext.Options.ConstructorScope)) {
6584 // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
6585 if (ec.CurrentMemberDefinition.Parent.PartialContainer.Definition != spec.DeclaringType.GetDefinition ())
6586 return Error_AssignToReadonly (ec, right_side);
6587 // static InitOnly fields cannot be assigned-to in an instance constructor
6588 if (IsStatic && !ec.IsStatic)
6589 return Error_AssignToReadonly (ec, right_side);
6590 // instance constructors can't modify InitOnly fields of other instances of the same type
6591 if (!IsStatic && !(InstanceExpression is This))
6592 return Error_AssignToReadonly (ec, right_side);
6596 if (right_side == EmptyExpression.OutAccess && IsMarshalByRefAccess (ec)) {
6597 ec.Report.SymbolRelatedToPreviousError (spec.DeclaringType);
6598 ec.Report.Warning (197, 1, loc,
6599 "Passing `{0}' as ref or out or taking its address may cause a runtime exception because it is a field of a marshal-by-reference class",
6600 GetSignatureForError ());
6603 eclass = ExprClass.Variable;
6607 public override void FlowAnalysis (FlowAnalysisContext fc)
6609 var var = InstanceExpression as IVariableReference;
6611 var vi = var.VariableInfo;
6612 if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, Name)) {
6613 fc.Report.Error (170, loc, "Use of possibly unassigned field `{0}'", Name);
6617 if (TypeSpec.IsValueType (InstanceExpression.Type)) {
6618 var le = SkipLeftValueTypeAccess (InstanceExpression);
6620 le.FlowAnalysis (fc);
6626 var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
6628 base.FlowAnalysis (fc);
6630 if (conditional_access_receiver)
6631 fc.DefiniteAssignment = da;
6634 static Expression SkipLeftValueTypeAccess (Expression expr)
6636 if (!TypeSpec.IsValueType (expr.Type))
6639 if (expr is VariableReference)
6642 var fe = expr as FieldExpr;
6646 if (fe.InstanceExpression == null)
6649 return SkipLeftValueTypeAccess (fe.InstanceExpression);
6652 public override int GetHashCode ()
6654 return spec.GetHashCode ();
6657 public bool IsFixed {
6660 // A variable of the form V.I is fixed when V is a fixed variable of a struct type
6662 IVariableReference variable = InstanceExpression as IVariableReference;
6663 if (variable != null)
6664 return InstanceExpression.Type.IsStruct && variable.IsFixed;
6666 IFixedExpression fe = InstanceExpression as IFixedExpression;
6667 return fe != null && fe.IsFixed;
6671 public override bool Equals (object obj)
6673 FieldExpr fe = obj as FieldExpr;
6677 if (spec != fe.spec)
6680 if (InstanceExpression == null || fe.InstanceExpression == null)
6683 return InstanceExpression.Equals (fe.InstanceExpression);
6686 public void Emit (EmitContext ec, bool leave_copy)
6688 bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6692 ec.Emit (OpCodes.Volatile);
6694 ec.Emit (OpCodes.Ldsfld, spec);
6696 var ca = ec.ConditionalAccess;
6699 if (conditional_access_receiver)
6700 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
6702 EmitInstance (ec, false);
6705 // Optimization for build-in types
6706 if (type.IsStruct && type == ec.CurrentType && InstanceExpression.Type == type) {
6707 ec.EmitLoadFromPtr (type);
6709 var ff = spec as FixedFieldSpec;
6711 ec.Emit (OpCodes.Ldflda, spec);
6712 ec.Emit (OpCodes.Ldflda, ff.Element);
6715 ec.Emit (OpCodes.Volatile);
6717 ec.Emit (OpCodes.Ldfld, spec);
6721 if (conditional_access_receiver) {
6722 ec.CloseConditionalAccess (type.IsNullableType && type != spec.MemberType ? type : null);
6723 ec.ConditionalAccess = ca;
6728 ec.Emit (OpCodes.Dup);
6730 temp = new LocalTemporary (this.Type);
6736 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
6738 bool has_await_source = ec.HasSet (BuilderContext.Options.AsyncBody) && source.ContainsEmitWithAwait ();
6739 if (isCompound && !(source is DynamicExpressionStatement) && !has_await_source) {
6744 if (ConditionalAccess)
6745 throw new NotImplementedException ("null operator assignment");
6747 if (has_await_source)
6748 source = source.EmitToField (ec);
6750 EmitInstance (ec, prepared);
6755 if (leave_copy || ec.NotifyEvaluatorOnStore) {
6756 ec.Emit (OpCodes.Dup);
6758 temp = new LocalTemporary (this.Type);
6763 if ((spec.Modifiers & Modifiers.VOLATILE) != 0)
6764 ec.Emit (OpCodes.Volatile);
6766 spec.MemberDefinition.SetIsAssigned ();
6769 ec.Emit (OpCodes.Stsfld, spec);
6771 ec.Emit (OpCodes.Stfld, spec);
6773 if (ec.NotifyEvaluatorOnStore) {
6775 throw new NotImplementedException ("instance field write");
6778 ec.Emit (OpCodes.Dup);
6780 ec.Module.Evaluator.EmitValueChangedCallback (ec, Name, type, loc);
6791 // Emits store to field with prepared values on stack
6793 public void EmitAssignFromStack (EmitContext ec)
6796 ec.Emit (OpCodes.Stsfld, spec);
6798 ec.Emit (OpCodes.Stfld, spec);
6802 public override void Emit (EmitContext ec)
6807 public override void EmitSideEffect (EmitContext ec)
6809 bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6811 if (is_volatile) // || is_marshal_by_ref ())
6812 base.EmitSideEffect (ec);
6815 public virtual void AddressOf (EmitContext ec, AddressOp mode)
6817 if ((mode & AddressOp.Store) != 0)
6818 spec.MemberDefinition.SetIsAssigned ();
6819 if ((mode & AddressOp.Load) != 0)
6820 spec.MemberDefinition.SetIsUsed ();
6823 // Handle initonly fields specially: make a copy and then
6824 // get the address of the copy.
6827 if (spec.IsReadOnly){
6829 if (ec.HasSet (EmitContext.Options.ConstructorScope) && spec.DeclaringType == ec.CurrentType) {
6841 var temp = ec.GetTemporaryLocal (type);
6842 ec.Emit (OpCodes.Stloc, temp);
6843 ec.Emit (OpCodes.Ldloca, temp);
6849 ec.Emit (OpCodes.Ldsflda, spec);
6852 EmitInstance (ec, false);
6853 ec.Emit (OpCodes.Ldflda, spec);
6857 public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
6859 return MakeExpression (ctx);
6862 public override SLE.Expression MakeExpression (BuilderContext ctx)
6865 return base.MakeExpression (ctx);
6867 return SLE.Expression.Field (
6868 IsStatic ? null : InstanceExpression.MakeExpression (ctx),
6869 spec.GetMetaInfo ());
6873 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6875 Error_TypeArgumentsCannotBeUsed (ec, "field", GetSignatureForError (), loc);
6881 // Expression that evaluates to a Property.
6883 // This is not an LValue because we need to re-write the expression. We
6884 // can not take data from the stack and store it.
6886 sealed class PropertyExpr : PropertyOrIndexerExpr<PropertySpec>
6888 Arguments arguments;
6889 FieldExpr backing_field;
6891 public PropertyExpr (PropertySpec spec, Location l)
6894 best_candidate = spec;
6895 type = spec.MemberType;
6900 protected override Arguments Arguments {
6909 protected override TypeSpec DeclaringType {
6911 return best_candidate.DeclaringType;
6915 public override string Name {
6917 return best_candidate.Name;
6921 public bool IsAutoPropertyAccess {
6923 var prop = best_candidate.MemberDefinition as Property;
6924 return prop != null && prop.BackingField != null;
6928 public override bool IsInstance {
6934 public override bool IsStatic {
6936 return best_candidate.IsStatic;
6940 public override string KindName {
6941 get { return "property"; }
6944 public PropertySpec PropertyInfo {
6946 return best_candidate;
6952 public override MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
6954 if (best_candidate == null || !(best_candidate.IsStatic || InstanceExpression is This))
6957 var args_count = arguments == null ? 0 : arguments.Count;
6958 if (args_count != body.Parameters.Count && args_count == 0)
6961 var mg = MethodGroupExpr.CreatePredefined (best_candidate.Get, DeclaringType, loc);
6962 mg.InstanceExpression = InstanceExpression;
6967 public static PropertyExpr CreatePredefined (PropertySpec spec, Location loc)
6969 return new PropertyExpr (spec, loc) {
6975 public override Expression CreateExpressionTree (ResolveContext ec)
6977 if (ConditionalAccess) {
6978 Error_NullShortCircuitInsideExpressionTree (ec);
6982 if (IsSingleDimensionalArrayLength ()) {
6983 args = new Arguments (1);
6984 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6985 return CreateExpressionFactoryCall (ec, "ArrayLength", args);
6988 args = new Arguments (2);
6989 if (InstanceExpression == null)
6990 args.Add (new Argument (new NullLiteral (loc)));
6992 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6993 args.Add (new Argument (new TypeOfMethod (Getter, loc)));
6994 return CreateExpressionFactoryCall (ec, "Property", args);
6997 public Expression CreateSetterTypeOfExpression (ResolveContext rc)
6999 DoResolveLValue (rc, null);
7000 return new TypeOfMethod (Setter, loc);
7003 public override string GetSignatureForError ()
7005 return best_candidate.GetSignatureForError ();
7008 public override SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
7011 return base.MakeExpression (ctx);
7013 return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Setter.GetMetaInfo ());
7017 public override SLE.Expression MakeExpression (BuilderContext ctx)
7020 return base.MakeExpression (ctx);
7022 return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Getter.GetMetaInfo ());
7026 void Error_PropertyNotValid (ResolveContext ec)
7028 ec.Report.SymbolRelatedToPreviousError (best_candidate);
7029 ec.Report.Error (1546, loc, "Property or event `{0}' is not supported by the C# language",
7030 GetSignatureForError ());
7033 bool IsSingleDimensionalArrayLength ()
7035 if (best_candidate.DeclaringType.BuiltinType != BuiltinTypeSpec.Type.Array || !best_candidate.HasGet || Name != "Length")
7038 ArrayContainer ac = InstanceExpression.Type as ArrayContainer;
7039 return ac != null && ac.Rank == 1;
7042 public override void Emit (EmitContext ec, bool leave_copy)
7045 // Special case: length of single dimension array property is turned into ldlen
7047 if (IsSingleDimensionalArrayLength ()) {
7048 if (conditional_access_receiver) {
7049 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7052 EmitInstance (ec, false);
7054 ec.Emit (OpCodes.Ldlen);
7055 ec.Emit (OpCodes.Conv_I4);
7057 if (conditional_access_receiver) {
7058 ec.CloseConditionalAccess (type);
7064 base.Emit (ec, leave_copy);
7067 public override void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
7069 if (backing_field != null) {
7070 backing_field.EmitAssign (ec, source, leave_copy, false);
7075 LocalTemporary await_source_arg = null;
7077 if (isCompound && !(source is DynamicExpressionStatement)) {
7078 emitting_compound_assignment = true;
7081 if (has_await_arguments) {
7082 await_source_arg = new LocalTemporary (Type);
7083 await_source_arg.Store (ec);
7085 args = new Arguments (1);
7086 args.Add (new Argument (await_source_arg));
7089 temp = await_source_arg;
7092 has_await_arguments = false;
7097 ec.Emit (OpCodes.Dup);
7098 temp = new LocalTemporary (this.Type);
7103 args = arguments ?? new Arguments (1);
7107 temp = new LocalTemporary (this.Type);
7109 args.Add (new Argument (temp));
7111 args.Add (new Argument (source));
7115 emitting_compound_assignment = false;
7117 var call = new CallEmitter ();
7118 call.InstanceExpression = InstanceExpression;
7120 call.InstanceExpressionOnStack = true;
7122 if (ConditionalAccess) {
7123 call.ConditionalAccess = true;
7127 call.Emit (ec, Setter, args, loc);
7129 call.EmitStatement (ec, Setter, args, loc);
7136 if (await_source_arg != null) {
7137 await_source_arg.Release (ec);
7141 public override void FlowAnalysis (FlowAnalysisContext fc)
7143 var prop = best_candidate.MemberDefinition as Property;
7144 if (prop != null && prop.BackingField != null) {
7145 var var = InstanceExpression as IVariableReference;
7147 var vi = var.VariableInfo;
7148 if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, prop.BackingField.Name)) {
7149 fc.Report.Error (8079, loc, "Use of possibly unassigned auto-implemented property `{0}'", Name);
7153 if (TypeSpec.IsValueType (InstanceExpression.Type) && InstanceExpression is VariableReference)
7158 var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
7160 base.FlowAnalysis (fc);
7162 if (conditional_access_receiver)
7163 fc.DefiniteAssignment = da;
7166 protected override Expression OverloadResolve (ResolveContext rc, Expression right_side)
7168 eclass = ExprClass.PropertyAccess;
7170 if (best_candidate.IsNotCSharpCompatible) {
7171 Error_PropertyNotValid (rc);
7174 ResolveInstanceExpression (rc, right_side);
7176 if ((best_candidate.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0 && best_candidate.DeclaringType != InstanceExpression.Type) {
7177 var filter = new MemberFilter (best_candidate.Name, 0, MemberKind.Property, null, null);
7178 var p = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as PropertySpec;
7180 type = p.MemberType;
7184 DoBestMemberChecks (rc, best_candidate);
7186 // Handling of com-imported properties with any number of default property parameters
7187 if (best_candidate.HasGet && !best_candidate.Get.Parameters.IsEmpty) {
7188 var p = best_candidate.Get.Parameters;
7189 arguments = new Arguments (p.Count);
7190 for (int i = 0; i < p.Count; ++i) {
7191 arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7193 } else if (best_candidate.HasSet && best_candidate.Set.Parameters.Count > 1) {
7194 var p = best_candidate.Set.Parameters;
7195 arguments = new Arguments (p.Count - 1);
7196 for (int i = 0; i < p.Count - 1; ++i) {
7197 arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7204 protected override bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7206 if (!rc.HasSet (ResolveContext.Options.ConstructorScope))
7209 var prop = best_candidate.MemberDefinition as Property;
7210 if (prop == null || prop.Parent.PartialContainer != rc.CurrentMemberDefinition.Parent.PartialContainer) {
7211 var ps = MemberCache.FindMember (rc.CurrentType, MemberFilter.Property (best_candidate.Name, best_candidate.MemberType), BindingRestriction.DeclaredOnly) as PropertySpec;
7215 prop = (Property)ps.MemberDefinition;
7218 var spec = prop.BackingField;
7222 if (rc.IsStatic != spec.IsStatic)
7225 if (!spec.IsStatic && (!(InstanceExpression is This) || InstanceExpression is BaseThis))
7228 backing_field = new FieldExpr (prop.BackingField, loc);
7229 backing_field.ResolveLValue (rc, rhs);
7233 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
7235 if (!best_candidate.IsAccessible (rc))
7236 ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), expr.Location);
7238 best_candidate.CheckObsoleteness (rc, expr.Location);
7241 public void SetBackingFieldAssigned (FlowAnalysisContext fc)
7243 if (backing_field != null) {
7244 backing_field.SetFieldAssigned (fc);
7248 if (!IsAutoPropertyAccess)
7251 var prop = best_candidate.MemberDefinition as Property;
7252 if (prop != null && prop.BackingField != null) {
7253 bool lvalue_instance = best_candidate.DeclaringType.IsStruct;
7254 if (lvalue_instance) {
7255 var var = InstanceExpression as IVariableReference;
7256 if (var != null && var.VariableInfo != null) {
7257 fc.SetStructFieldAssigned (var.VariableInfo, prop.BackingField.Name);
7263 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7265 Error_TypeArgumentsCannotBeUsed (ec, "property", GetSignatureForError (), loc);
7269 abstract class PropertyOrIndexerExpr<T> : MemberExpr, IDynamicAssign where T : PropertySpec
7271 // getter and setter can be different for base calls
7272 MethodSpec getter, setter;
7273 protected T best_candidate;
7275 protected LocalTemporary temp;
7276 protected bool emitting_compound_assignment;
7277 protected bool has_await_arguments;
7279 protected PropertyOrIndexerExpr (Location l)
7286 protected abstract Arguments Arguments { get; set; }
7288 public MethodSpec Getter {
7297 public MethodSpec Setter {
7308 protected override Expression DoResolve (ResolveContext ec)
7310 if (eclass == ExprClass.Unresolved) {
7311 ResolveConditionalAccessReceiver (ec);
7313 var expr = OverloadResolve (ec, null);
7318 using (ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, conditional_access_receiver))
7319 return expr.Resolve (ec);
7322 if (conditional_access_receiver) {
7323 type = LiftMemberType (ec, type);
7327 if (!ResolveGetter (ec))
7333 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
7335 if (HasConditionalAccess ())
7336 Error_NullPropagatingLValue (rc);
7338 if (right_side == EmptyExpression.OutAccess) {
7339 // TODO: best_candidate can be null at this point
7340 INamedBlockVariable variable = null;
7341 if (best_candidate != null && rc.CurrentBlock.ParametersBlock.TopBlock.GetLocalName (best_candidate.Name, rc.CurrentBlock, ref variable) && variable is Linq.RangeVariable) {
7342 rc.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
7343 best_candidate.Name);
7345 right_side.DoResolveLValue (rc, this);
7350 if (eclass == ExprClass.Unresolved) {
7351 var expr = OverloadResolve (rc, right_side);
7356 return expr.ResolveLValue (rc, right_side);
7358 ResolveInstanceExpression (rc, right_side);
7361 if (!best_candidate.HasSet) {
7362 if (ResolveAutopropertyAssignment (rc, right_side))
7365 rc.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read-only)",
7366 GetSignatureForError ());
7370 if (!best_candidate.Set.IsAccessible (rc) || !best_candidate.Set.DeclaringType.IsAccessible (rc)) {
7371 if (best_candidate.HasDifferentAccessibility) {
7372 rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7373 rc.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
7374 GetSignatureForError ());
7376 rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7377 ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
7381 if (best_candidate.HasDifferentAccessibility)
7382 CheckProtectedMemberAccess (rc, best_candidate.Set);
7384 setter = CandidateToBaseOverride (rc, best_candidate.Set);
7388 void EmitConditionalAccess (EmitContext ec, ref CallEmitter call, MethodSpec method, Arguments arguments)
7390 var ca = ec.ConditionalAccess;
7391 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7393 call.Emit (ec, method, arguments, loc);
7395 ec.CloseConditionalAccess (method.ReturnType != type && type.IsNullableType ? type : null);
7396 ec.ConditionalAccess = ca;
7400 // Implements the IAssignMethod interface for assignments
7402 public virtual void Emit (EmitContext ec, bool leave_copy)
7404 var call = new CallEmitter ();
7405 call.ConditionalAccess = ConditionalAccess;
7406 call.InstanceExpression = InstanceExpression;
7407 if (has_await_arguments)
7408 call.HasAwaitArguments = true;
7410 call.DuplicateArguments = emitting_compound_assignment;
7412 if (conditional_access_receiver)
7413 EmitConditionalAccess (ec, ref call, Getter, Arguments);
7415 call.Emit (ec, Getter, Arguments, loc);
7417 if (call.HasAwaitArguments) {
7418 InstanceExpression = call.InstanceExpression;
7419 Arguments = call.EmittedArguments;
7420 has_await_arguments = true;
7424 ec.Emit (OpCodes.Dup);
7425 temp = new LocalTemporary (Type);
7430 public abstract void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound);
7432 public override void Emit (EmitContext ec)
7437 protected override FieldExpr EmitToFieldSource (EmitContext ec)
7439 has_await_arguments = true;
7444 public abstract SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
7446 protected abstract Expression OverloadResolve (ResolveContext rc, Expression right_side);
7448 bool ResolveGetter (ResolveContext rc)
7450 if (!best_candidate.HasGet) {
7451 if (InstanceExpression != EmptyExpression.Null) {
7452 rc.Report.SymbolRelatedToPreviousError (best_candidate);
7453 rc.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
7454 best_candidate.GetSignatureForError ());
7457 } else if (!best_candidate.Get.IsAccessible (rc) || !best_candidate.Get.DeclaringType.IsAccessible (rc)) {
7458 if (best_candidate.HasDifferentAccessibility) {
7459 rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7460 rc.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
7461 TypeManager.CSharpSignature (best_candidate));
7463 rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7464 ErrorIsInaccesible (rc, best_candidate.Get.GetSignatureForError (), loc);
7468 if (best_candidate.HasDifferentAccessibility) {
7469 CheckProtectedMemberAccess (rc, best_candidate.Get);
7472 getter = CandidateToBaseOverride (rc, best_candidate.Get);
7476 protected virtual bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7483 /// Fully resolved expression that evaluates to an Event
7485 public class EventExpr : MemberExpr, IAssignMethod
7487 readonly EventSpec spec;
7490 public EventExpr (EventSpec spec, Location loc)
7498 protected override TypeSpec DeclaringType {
7500 return spec.DeclaringType;
7504 public override string Name {
7510 public override bool IsInstance {
7512 return !spec.IsStatic;
7516 public override bool IsStatic {
7518 return spec.IsStatic;
7522 public override string KindName {
7523 get { return "event"; }
7526 public MethodSpec Operator {
7534 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
7537 // If the event is local to this class and we are not lhs of +=/-= we transform ourselves into a FieldExpr
7539 if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7540 if (spec.BackingField != null &&
7541 (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition))) {
7543 spec.MemberDefinition.SetIsUsed ();
7545 spec.CheckObsoleteness (ec, loc);
7547 if ((spec.Modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7548 Error_AssignmentEventOnly (ec);
7550 FieldExpr ml = new FieldExpr (spec.BackingField, loc);
7552 InstanceExpression = null;
7554 return ml.ResolveMemberAccess (ec, left, original);
7558 return base.ResolveMemberAccess (ec, left, original);
7561 public override Expression CreateExpressionTree (ResolveContext ec)
7563 throw new NotSupportedException ("ET");
7566 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7568 if (right_side == EmptyExpression.EventAddition) {
7569 op = spec.AccessorAdd;
7570 } else if (right_side == EmptyExpression.EventSubtraction) {
7571 op = spec.AccessorRemove;
7575 Error_AssignmentEventOnly (ec);
7579 if (HasConditionalAccess ())
7580 Error_NullPropagatingLValue (ec);
7582 op = CandidateToBaseOverride (ec, op);
7586 protected override Expression DoResolve (ResolveContext ec)
7588 eclass = ExprClass.EventAccess;
7589 type = spec.MemberType;
7591 ResolveInstanceExpression (ec, null);
7593 if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7594 Error_AssignmentEventOnly (ec);
7597 DoBestMemberChecks (ec, spec);
7601 public override void Emit (EmitContext ec)
7603 throw new NotSupportedException ();
7604 //Error_CannotAssign ();
7607 #region IAssignMethod Members
7609 public void Emit (EmitContext ec, bool leave_copy)
7611 throw new NotImplementedException ();
7614 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
7616 if (leave_copy || !isCompound)
7617 throw new NotImplementedException ("EventExpr::EmitAssign");
7619 Arguments args = new Arguments (1);
7620 args.Add (new Argument (source));
7622 // TODO: Wrong, needs receiver
7623 // if (NullShortCircuit) {
7624 // ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7627 var call = new CallEmitter ();
7628 call.InstanceExpression = InstanceExpression;
7629 call.ConditionalAccess = ConditionalAccess;
7630 call.EmitStatement (ec, op, args, loc);
7632 // if (NullShortCircuit)
7633 // ec.CloseConditionalAccess (null);
7638 void Error_AssignmentEventOnly (ResolveContext ec)
7640 if (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition)) {
7641 ec.Report.Error (79, loc,
7642 "The event `{0}' can only appear on the left hand side of `+=' or `-=' operator",
7643 GetSignatureForError ());
7645 ec.Report.Error (70, loc,
7646 "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
7647 GetSignatureForError (), spec.DeclaringType.GetSignatureForError ());
7651 protected override void Error_CannotCallAbstractBase (ResolveContext rc, string name)
7653 name = name.Substring (0, name.LastIndexOf ('.'));
7654 base.Error_CannotCallAbstractBase (rc, name);
7657 public override string GetSignatureForError ()
7659 return TypeManager.CSharpSignature (spec);
7662 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
7664 spec.CheckObsoleteness (rc, expr.Location);
7667 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7669 Error_TypeArgumentsCannotBeUsed (ec, "event", GetSignatureForError (), loc);
7673 public class TemporaryVariableReference : VariableReference
7675 public class Declarator : Statement
7677 TemporaryVariableReference variable;
7679 public Declarator (TemporaryVariableReference variable)
7681 this.variable = variable;
7685 protected override void DoEmit (EmitContext ec)
7687 variable.li.CreateBuilder (ec);
7690 public override void Emit (EmitContext ec)
7692 // Don't create sequence point
7696 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
7701 protected override void CloneTo (CloneContext clonectx, Statement target)
7709 public TemporaryVariableReference (LocalVariable li, Location loc)
7712 this.type = li.Type;
7716 public override bool IsLockedByStatement {
7724 public LocalVariable LocalInfo {
7730 public static TemporaryVariableReference Create (TypeSpec type, Block block, Location loc, bool writeToSymbolFile = false)
7732 var li = LocalVariable.CreateCompilerGenerated (type, block, loc, writeToSymbolFile);
7733 return new TemporaryVariableReference (li, loc);
7736 protected override Expression DoResolve (ResolveContext ec)
7738 eclass = ExprClass.Variable;
7741 // Don't capture temporary variables except when using
7742 // state machine redirection and block yields
7744 if (ec.CurrentAnonymousMethod is StateMachineInitializer &&
7745 (ec.CurrentBlock.Explicit.HasYield || ec.CurrentBlock.Explicit.HasAwait) &&
7746 ec.IsVariableCapturingRequired) {
7747 AnonymousMethodStorey storey = li.Block.Explicit.CreateAnonymousMethodStorey (ec);
7748 storey.CaptureLocalVariable (ec, li);
7754 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7756 return Resolve (ec);
7759 public override void Emit (EmitContext ec)
7761 li.CreateBuilder (ec);
7766 public void EmitAssign (EmitContext ec, Expression source)
7768 li.CreateBuilder (ec);
7770 EmitAssign (ec, source, false, false);
7773 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
7775 return li.HoistedVariant;
7778 public override bool IsFixed {
7779 get { return true; }
7782 public override bool IsRef {
7783 get { return false; }
7786 public override string Name {
7787 get { throw new NotImplementedException (); }
7790 public override void SetHasAddressTaken ()
7792 throw new NotImplementedException ();
7795 protected override ILocalVariable Variable {
7799 public override VariableInfo VariableInfo {
7800 get { return null; }
7805 /// Handles `var' contextual keyword; var becomes a keyword only
7806 /// if no type called var exists in a variable scope
7808 class VarExpr : SimpleName
7810 public VarExpr (Location loc)
7815 public bool InferType (ResolveContext ec, Expression rhs)
7818 throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
7822 if (type.Kind == MemberKind.Void || InternalType.HasNoType (type) || (rhs is TupleLiteral && TupleLiteral.ContainsNoTypeElement (type))) {
7823 ec.Report.Error (815, loc,
7824 "An implicitly typed local variable declaration cannot be initialized with `{0}'",
7825 type.GetSignatureForError ());
7826 type = InternalType.ErrorType;
7830 eclass = ExprClass.Variable;
7834 protected override void Error_TypeOrNamespaceNotFound (IMemberContext ec)
7836 if (ec.Module.Compiler.Settings.Version < LanguageVersion.V_3)
7837 base.Error_TypeOrNamespaceNotFound (ec);
7839 ec.Module.Compiler.Report.Error (825, loc, "The contextual keyword `var' may only appear within a local variable declaration");