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 public static void ErrorIsInaccesible (IMemberContext rc, string member, Location loc)
244 rc.Module.Compiler.Report.Error (122, loc, "`{0}' is inaccessible due to its protection level", member);
247 public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name)
249 rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name);
252 public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, TypeSpec type, Location loc, string name)
254 rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
255 name, type.GetSignatureForError ());
258 protected virtual void Error_InvalidExpressionStatement (Report report, Location loc)
260 report.Error (201, loc, "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement");
263 public void Error_InvalidExpressionStatement (BlockContext bc)
265 Error_InvalidExpressionStatement (bc.Report, loc);
268 public void Error_InvalidExpressionStatement (Report report)
270 Error_InvalidExpressionStatement (report, loc);
273 public static void Error_VoidInvalidInTheContext (Location loc, Report Report)
275 Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
278 public virtual void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
280 Error_ValueCannotBeConvertedCore (ec, loc, target, expl);
283 protected void Error_ValueCannotBeConvertedCore (ResolveContext ec, Location loc, TypeSpec target, bool expl)
285 // The error was already reported as CS1660
286 if (type == InternalType.AnonymousMethod)
289 if (type == InternalType.ErrorType || target == InternalType.ErrorType)
292 if (type.MemberDefinition.DeclaringAssembly.IsMissing ||
293 target.MemberDefinition.DeclaringAssembly.IsMissing)
296 string from_type = type.GetSignatureForError ();
297 string to_type = target.GetSignatureForError ();
298 if (from_type == to_type) {
299 from_type = type.GetSignatureForErrorIncludingAssemblyName ();
300 to_type = target.GetSignatureForErrorIncludingAssemblyName ();
304 ec.Report.Error (30, loc, "Cannot convert type `{0}' to `{1}'",
309 ec.Report.DisableReporting ();
310 bool expl_exists = Convert.ExplicitConversion (ec, this, target, Location.Null) != null;
311 ec.Report.EnableReporting ();
314 ec.Report.Error (266, loc,
315 "Cannot implicitly convert type `{0}' to `{1}'. An explicit conversion exists (are you missing a cast?)",
318 ec.Report.Error (29, loc, "Cannot implicitly convert type `{0}' to `{1}'",
323 public void Error_TypeArgumentsCannotBeUsed (IMemberContext context, MemberSpec member, Location loc)
325 // Better message for possible generic expressions
326 if (member != null && (member.Kind & MemberKind.GenericMask) != 0) {
327 var report = context.Module.Compiler.Report;
328 report.SymbolRelatedToPreviousError (member);
329 if (member is TypeSpec)
330 member = ((TypeSpec) member).GetDefinition ();
332 member = ((MethodSpec) member).GetGenericMethodDefinition ();
334 string name = member.Kind == MemberKind.Method ? "method" : "type";
335 if (member.IsGeneric) {
336 report.Error (305, loc, "Using the generic {0} `{1}' requires `{2}' type argument(s)",
337 name, member.GetSignatureForError (), member.Arity.ToString ());
339 report.Error (308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
340 name, member.GetSignatureForError ());
343 Error_TypeArgumentsCannotBeUsed (context, ExprClassName, GetSignatureForError (), loc);
347 public static void Error_TypeArgumentsCannotBeUsed (IMemberContext context, string exprType, string name, Location loc)
349 context.Module.Compiler.Report.Error (307, loc, "The {0} `{1}' cannot be used with type arguments",
353 public virtual void Error_TypeDoesNotContainDefinition (ResolveContext ec, TypeSpec type, string name)
355 Error_TypeDoesNotContainDefinition (ec, loc, type, name);
358 public static void Error_TypeDoesNotContainDefinition (ResolveContext ec, Location loc, TypeSpec type, string name)
360 ec.Report.SymbolRelatedToPreviousError (type);
361 ec.Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
362 type.GetSignatureForError (), name);
365 public virtual void Error_ValueAssignment (ResolveContext rc, Expression rhs)
367 if (rhs == EmptyExpression.LValueMemberAccess || rhs == EmptyExpression.LValueMemberOutAccess) {
368 // Already reported as CS1612
369 } else if (rhs == EmptyExpression.OutAccess) {
370 rc.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
372 rc.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
376 protected void Error_VoidPointerOperation (ResolveContext rc)
378 rc.Report.Error (242, loc, "The operation in question is undefined on void pointers");
381 public static void Warning_UnreachableExpression (ResolveContext rc, Location loc)
383 rc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
386 public ResolveFlags ExprClassToResolveFlags {
390 case ExprClass.Namespace:
391 return ResolveFlags.Type;
393 case ExprClass.MethodGroup:
394 return ResolveFlags.MethodGroup;
396 case ExprClass.TypeParameter:
397 return ResolveFlags.TypeParameter;
399 case ExprClass.Value:
400 case ExprClass.Variable:
401 case ExprClass.PropertyAccess:
402 case ExprClass.EventAccess:
403 case ExprClass.IndexerAccess:
404 return ResolveFlags.VariableOrValue;
407 throw new InternalErrorException (loc.ToString () + " " + GetType () + " ExprClass is Invalid after resolve");
413 // Implements identical simple name and type-name resolution
415 public Expression ProbeIdenticalTypeName (ResolveContext rc, Expression left, SimpleName name)
418 if (t.Kind == MemberKind.InternalCompilerType || t is ElementTypeSpec || t.Arity > 0)
421 // 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
422 // a constant, field, property, local variable, or parameter with the same type as the meaning of E as a type-name
424 if (left is MemberExpr || left is VariableReference) {
425 var identical_type = rc.LookupNamespaceOrType (name.Name, 0, LookupMode.Probing, loc) as TypeExpr;
426 if (identical_type != null && identical_type.Type == left.Type)
427 return identical_type;
433 public virtual string GetSignatureForError ()
435 return type.GetDefinition ().GetSignatureForError ();
438 public static bool IsNeverNull (Expression expr)
440 if (expr is This || expr is New || expr is ArrayCreation || expr is DelegateCreation || expr is ConditionalMemberAccess)
443 var c = expr as Constant;
447 var tc = expr as TypeCast;
449 return IsNeverNull (tc.Child);
454 protected static bool IsNullPropagatingValid (TypeSpec type)
457 case MemberKind.Struct:
458 return type.IsNullableType;
459 case MemberKind.Enum:
460 case MemberKind.Void:
461 case MemberKind.PointerType:
463 case MemberKind.InternalCompilerType:
464 return type.BuiltinType == BuiltinTypeSpec.Type.Dynamic;
465 case MemberKind.TypeParameter:
466 return !((TypeParameterSpec) type).IsValueType;
472 public virtual bool HasConditionalAccess ()
477 protected TypeSpec LiftMemberType (ResolveContext rc, TypeSpec type)
479 var tps = type as TypeParameterSpec;
480 if (tps != null && !(tps.IsReferenceType || tps.IsValueType)) {
481 Error_OperatorCannotBeApplied (rc, loc, "?", type);
484 return TypeSpec.IsValueType (type) && !type.IsNullableType ?
485 Nullable.NullableInfo.MakeType (rc.Module, type) :
490 /// Resolves an expression and performs semantic analysis on it.
494 /// Currently Resolve wraps DoResolve to perform sanity
495 /// checking and assertion checking on what we expect from Resolve.
497 public Expression Resolve (ResolveContext ec, ResolveFlags flags)
499 if (eclass != ExprClass.Unresolved) {
500 if ((flags & ExprClassToResolveFlags) == 0) {
501 Error_UnexpectedKind (ec, flags, loc);
515 if ((flags & e.ExprClassToResolveFlags) == 0) {
516 e.Error_UnexpectedKind (ec, flags, loc);
521 throw new InternalErrorException ("Expression `{0}' didn't set its type in DoResolve", e.GetType ());
524 } catch (Exception ex) {
525 if (loc.IsNull || ec.Module.Compiler.Settings.BreakOnInternalError || ex is CompletionResult || ec.Report.IsDisabled || ex is FatalException ||
526 ec.Report.Printer is NullReportPrinter)
529 ec.Report.Error (584, loc, "Internal compiler error: {0}", ex.Message);
530 return ErrorExpression.Instance; // TODO: Add location
535 /// Resolves an expression and performs semantic analysis on it.
537 public Expression Resolve (ResolveContext rc)
539 return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup);
543 /// Resolves an expression for LValue assignment
547 /// Currently ResolveLValue wraps DoResolveLValue to perform sanity
548 /// checking and assertion checking on what we expect from Resolve
550 public Expression ResolveLValue (ResolveContext ec, Expression right_side)
552 int errors = ec.Report.Errors;
553 bool out_access = right_side == EmptyExpression.OutAccess;
555 Expression e = DoResolveLValue (ec, right_side);
557 if (e != null && out_access && !(e is IMemoryLocation)) {
558 // FIXME: There's no problem with correctness, the 'Expr = null' handles that.
559 // Enabling this 'throw' will "only" result in deleting useless code elsewhere,
561 //throw new InternalErrorException ("ResolveLValue didn't return an IMemoryLocation: " +
562 // e.GetType () + " " + e.GetSignatureForError ());
567 if (errors == ec.Report.Errors) {
568 Error_ValueAssignment (ec, right_side);
573 if (e.eclass == ExprClass.Unresolved)
574 throw new Exception ("Expression " + e + " ExprClass is Invalid after resolve");
576 if ((e.type == null) && !(e is GenericTypeExpr))
577 throw new Exception ("Expression " + e + " did not set its type after Resolve");
582 public Constant ResolveLabelConstant (ResolveContext rc)
584 var expr = Resolve (rc);
588 Constant c = expr as Constant;
590 if (expr.type != InternalType.ErrorType)
591 rc.Report.Error (150, expr.StartLocation, "A constant value is expected");
599 public virtual void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
601 if (Attribute.IsValidArgumentType (parameterType)) {
602 rc.Module.Compiler.Report.Error (182, loc,
603 "An attribute argument must be a constant expression, typeof expression or array creation expression");
605 rc.Module.Compiler.Report.Error (181, loc,
606 "Attribute constructor parameter has type `{0}', which is not a valid attribute parameter type",
607 targetType.GetSignatureForError ());
612 /// Emits the code for the expression
616 /// The Emit method is invoked to generate the code
617 /// for the expression.
619 public abstract void Emit (EmitContext ec);
622 // Emit code to branch to @target if this expression is equivalent to @on_true.
623 // The default implementation is to emit the value, and then emit a brtrue or brfalse.
624 // Subclasses can provide more efficient implementations, but those MUST be equivalent,
625 // including the use of conditional branches. Note also that a branch MUST be emitted
626 public virtual void EmitBranchable (EmitContext ec, Label target, bool on_true)
629 ec.Emit (on_true ? OpCodes.Brtrue : OpCodes.Brfalse, target);
632 // Emit this expression for its side effects, not for its value.
633 // The default implementation is to emit the value, and then throw it away.
634 // Subclasses can provide more efficient implementations, but those MUST be equivalent
635 public virtual void EmitSideEffect (EmitContext ec)
638 ec.Emit (OpCodes.Pop);
642 // Emits the expression into temporary field variable. The method
643 // should be used for await expressions only
645 public virtual Expression EmitToField (EmitContext ec)
648 // This is the await prepare Emit method. When emitting code like
649 // a + b we emit code like
655 // For await a + await b we have to interfere the flow to keep the
656 // stack clean because await yields from the expression. The emit
659 // a = a.EmitToField () // a is changed to temporary field access
660 // b = b.EmitToField ()
666 // The idea is to emit expression and leave the stack empty with
667 // result value still available.
669 // Expressions should override this default implementation when
670 // optimized version can be provided (e.g. FieldExpr)
673 // We can optimize for side-effect free expressions, they can be
674 // emitted out of order
676 if (IsSideEffectFree)
679 bool needs_temporary = ContainsEmitWithAwait ();
680 if (!needs_temporary)
683 // Emit original code
684 var field = EmitToFieldSource (ec);
687 // Store the result to temporary field when we
688 // cannot load `this' directly
690 field = ec.GetTemporaryField (type);
691 if (needs_temporary) {
693 // Create temporary local (we cannot load `this' before Emit)
695 var temp = ec.GetTemporaryLocal (type);
696 ec.Emit (OpCodes.Stloc, temp);
699 ec.Emit (OpCodes.Ldloc, temp);
700 field.EmitAssignFromStack (ec);
702 ec.FreeTemporaryLocal (temp, type);
704 field.EmitAssignFromStack (ec);
711 protected virtual FieldExpr EmitToFieldSource (EmitContext ec)
714 // Default implementation calls Emit method
720 protected static void EmitExpressionsList (EmitContext ec, List<Expression> expressions)
722 if (ec.HasSet (BuilderContext.Options.AsyncBody)) {
723 bool contains_await = false;
725 for (int i = 1; i < expressions.Count; ++i) {
726 if (expressions[i].ContainsEmitWithAwait ()) {
727 contains_await = true;
732 if (contains_await) {
733 for (int i = 0; i < expressions.Count; ++i) {
734 expressions[i] = expressions[i].EmitToField (ec);
739 for (int i = 0; i < expressions.Count; ++i) {
740 expressions[i].Emit (ec);
745 /// Protected constructor. Only derivate types should
746 /// be able to be created
749 protected Expression ()
754 /// Returns a fully formed expression after a MemberLookup
757 static Expression ExprClassFromMemberInfo (MemberSpec spec, Location loc)
759 if (spec is EventSpec)
760 return new EventExpr ((EventSpec) spec, loc);
761 if (spec is ConstSpec)
762 return new ConstantExpr ((ConstSpec) spec, loc);
763 if (spec is FieldSpec)
764 return new FieldExpr ((FieldSpec) spec, loc);
765 if (spec is PropertySpec)
766 return new PropertyExpr ((PropertySpec) spec, loc);
767 if (spec is TypeSpec)
768 return new TypeExpression (((TypeSpec) spec), loc);
773 public static MethodSpec ConstructorLookup (ResolveContext rc, TypeSpec type, ref Arguments args, Location loc)
775 var ctors = MemberCache.FindMembers (type, Constructor.ConstructorName, true);
778 case MemberKind.Struct:
779 // Every struct has implicit default constructor if not provided by user
783 rc.Report.SymbolRelatedToPreviousError (type);
784 // Report meaningful error for struct as they always have default ctor in C# context
785 OverloadResolver.Error_ConstructorMismatch (rc, type, args == null ? 0 : args.Count, loc);
787 case MemberKind.MissingType:
788 case MemberKind.InternalCompilerType:
789 // LAMESPEC: dynamic is not really object
790 // if (type.BuiltinType == BuiltinTypeSpec.Type.Object)
794 rc.Report.SymbolRelatedToPreviousError (type);
795 rc.Report.Error (143, loc, "The class `{0}' has no constructors defined",
796 type.GetSignatureForError ());
803 if (args == null && type.IsStruct) {
804 bool includes_empty = false;
805 foreach (MethodSpec ctor in ctors) {
806 if (ctor.Parameters.IsEmpty) {
807 includes_empty = true;
815 var r = new OverloadResolver (ctors, OverloadResolver.Restrictions.NoBaseMembers, loc);
816 if (!rc.HasSet (ResolveContext.Options.BaseInitializer)) {
817 r.InstanceQualifier = new ConstructorInstanceQualifier (type);
820 return r.ResolveMember<MethodSpec> (rc, ref args);
824 public enum MemberLookupRestrictions
830 EmptyArguments = 1 << 4,
831 IgnoreArity = 1 << 5,
832 IgnoreAmbiguity = 1 << 6,
833 NameOfExcluded = 1 << 7,
834 DontSetConditionalAccess = 1 << 8
838 // Lookup type `queried_type' for code in class `container_type' with a qualifier of
839 // `qualifier_type' or null to lookup members in the current class.
841 public static Expression MemberLookup (IMemberContext rc, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
843 var members = MemberCache.FindMembers (queried_type, name, false);
845 if (members != null) {
848 expr = MemberLookupToExpression (rc, members, errorMode, queried_type, name, arity, restrictions, loc);
852 if (members [0].DeclaringType.BaseType == null)
855 members = MemberCache.FindMembers (members [0].DeclaringType.BaseType, name, false);
856 } while (members != null);
859 var tps = queried_type as TypeParameterSpec;
861 members = MemberCache.FindInterfaceMembers (tps, name);
863 return MemberLookupToExpression (rc, members, errorMode, queried_type, name, arity, restrictions, loc);
869 public static Expression MemberLookupToExpression (IMemberContext rc, IList<MemberSpec> members, bool errorMode, TypeSpec queried_type, string name, int arity, MemberLookupRestrictions restrictions, Location loc)
871 MemberSpec non_method = null;
872 MemberSpec ambig_non_method = null;
874 for (int i = 0; i < members.Count; ++i) {
875 var member = members [i];
877 // HACK: for events because +=/-= can appear at same class only, should use OverrideToBase there
878 if ((member.Modifiers & Modifiers.OVERRIDE) != 0 && member.Kind != MemberKind.Event)
881 if ((member.Modifiers & Modifiers.BACKING_FIELD) != 0 || member.Kind == MemberKind.Operator)
884 if ((arity > 0 || (restrictions & MemberLookupRestrictions.ExactArity) != 0) && member.Arity != arity)
888 if (!member.IsAccessible (rc))
892 // With runtime binder we can have a situation where queried type is inaccessible
893 // because it came via dynamic object, the check about inconsisted accessibility
894 // had no effect as the type was unknown during compilation
897 // private class N { }
899 // public dynamic Foo ()
905 if (rc.Module.Compiler.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
909 if ((restrictions & MemberLookupRestrictions.InvocableOnly) != 0) {
910 if (member is MethodSpec) {
911 return new MethodGroupExpr (members, queried_type, loc);
914 if (!Invocation.IsMemberInvocable (member))
918 if (non_method == null || member is MethodSpec || non_method.IsNotCSharpCompatible) {
920 } else if (!errorMode && !member.IsNotCSharpCompatible) {
922 // Interface members that are hidden by class members are removed from the set when T is a type parameter and
923 // T has both an effective base class other than object and a non-empty effective interface set.
925 // The spec has more complex rules but we simply remove all members declared in an interface declaration.
927 var tps = queried_type as TypeParameterSpec;
928 if (tps != null && tps.HasTypeConstraint) {
929 if (non_method.DeclaringType.IsClass && member.DeclaringType.IsInterface)
932 if (non_method.DeclaringType.IsInterface && member.DeclaringType.IsInterface) {
938 ambig_non_method = member;
942 if (non_method != null) {
943 if (ambig_non_method != null && rc != null && (restrictions & MemberLookupRestrictions.IgnoreAmbiguity) == 0) {
944 var report = rc.Module.Compiler.Report;
945 report.SymbolRelatedToPreviousError (non_method);
946 report.SymbolRelatedToPreviousError (ambig_non_method);
947 report.Error (229, loc, "Ambiguity between `{0}' and `{1}'",
948 non_method.GetSignatureForError (), ambig_non_method.GetSignatureForError ());
951 if (non_method is MethodSpec)
952 return new MethodGroupExpr (members, queried_type, loc);
954 return ExprClassFromMemberInfo (non_method, loc);
960 protected static void Error_NamedArgument (NamedArgument na, Report Report)
962 Report.Error (1742, na.Location, "An element access expression cannot use named argument");
965 protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc)
967 throw new NotImplementedException ();
970 public virtual void Error_OperatorCannotBeApplied (ResolveContext rc, Location loc, string oper, TypeSpec t)
972 if (t == InternalType.ErrorType)
975 rc.Report.Error (23, loc, "The `{0}' operator cannot be applied to operand of type `{1}'",
976 oper, t.GetSignatureForError ());
979 protected void Error_PointerInsideExpressionTree (ResolveContext ec)
981 ec.Report.Error (1944, loc, "An expression tree cannot contain an unsafe pointer operation");
984 protected void Error_NullShortCircuitInsideExpressionTree (ResolveContext rc)
986 rc.Report.Error (8072, loc, "An expression tree cannot contain a null propagating operator");
989 protected void Error_NullPropagatingLValue (ResolveContext rc)
991 rc.Report.Error (-1030, loc, "The left-hand side of an assignment cannot contain a null propagating operator");
994 public virtual void FlowAnalysis (FlowAnalysisContext fc)
999 // Special version of flow analysis for expressions which can return different
1000 // on-true and on-false result. Used by &&, ||, ?: expressions
1002 public virtual void FlowAnalysisConditional (FlowAnalysisContext fc)
1005 fc.DefiniteAssignmentOnTrue = fc.DefiniteAssignmentOnFalse = fc.DefiniteAssignment;
1009 /// Returns an expression that can be used to invoke operator true
1010 /// on the expression if it exists.
1012 protected static Expression GetOperatorTrue (ResolveContext ec, Expression e, Location loc)
1014 return GetOperatorTrueOrFalse (ec, e, true, loc);
1018 /// Returns an expression that can be used to invoke operator false
1019 /// on the expression if it exists.
1021 protected static Expression GetOperatorFalse (ResolveContext ec, Expression e, Location loc)
1023 return GetOperatorTrueOrFalse (ec, e, false, loc);
1026 static Expression GetOperatorTrueOrFalse (ResolveContext ec, Expression e, bool is_true, Location loc)
1028 var op = is_true ? Operator.OpType.True : Operator.OpType.False;
1030 if (type.IsNullableType)
1031 type = Nullable.NullableInfo.GetUnderlyingType (type);
1033 var methods = MemberCache.GetUserOperator (type, op, false);
1034 if (methods == null)
1037 Arguments arguments = new Arguments (1);
1038 arguments.Add (new Argument (e));
1040 var res = new OverloadResolver (methods, OverloadResolver.Restrictions.BaseMembersIncluded | OverloadResolver.Restrictions.NoBaseMembers, loc);
1041 var oper = res.ResolveOperator (ec, ref arguments);
1046 return new UserOperatorCall (oper, arguments, null, loc);
1049 public virtual string ExprClassName
1053 case ExprClass.Unresolved:
1054 return "Unresolved";
1055 case ExprClass.Value:
1057 case ExprClass.Variable:
1059 case ExprClass.Namespace:
1061 case ExprClass.Type:
1063 case ExprClass.MethodGroup:
1064 return "method group";
1065 case ExprClass.PropertyAccess:
1066 return "property access";
1067 case ExprClass.EventAccess:
1068 return "event access";
1069 case ExprClass.IndexerAccess:
1070 return "indexer access";
1071 case ExprClass.Nothing:
1073 case ExprClass.TypeParameter:
1074 return "type parameter";
1076 throw new Exception ("Should not happen");
1081 /// Reports that we were expecting `expr' to be of class `expected'
1083 public static void Error_UnexpectedKind (IMemberContext ctx, Expression memberExpr, string expected, string was, Location loc)
1085 var name = memberExpr.GetSignatureForError ();
1087 ctx.Module.Compiler.Report.Error (118, loc, "`{0}' is a `{1}' but a `{2}' was expected", name, was, expected);
1090 public virtual void Error_UnexpectedKind (ResolveContext ec, ResolveFlags flags, Location loc)
1092 string [] valid = new string [4];
1095 if ((flags & ResolveFlags.VariableOrValue) != 0) {
1096 valid [count++] = "variable";
1097 valid [count++] = "value";
1100 if ((flags & ResolveFlags.Type) != 0)
1101 valid [count++] = "type";
1103 if ((flags & ResolveFlags.MethodGroup) != 0)
1104 valid [count++] = "method group";
1107 valid [count++] = "unknown";
1109 StringBuilder sb = new StringBuilder (valid [0]);
1110 for (int i = 1; i < count - 1; i++) {
1112 sb.Append (valid [i]);
1115 sb.Append ("' or `");
1116 sb.Append (valid [count - 1]);
1119 ec.Report.Error (119, loc,
1120 "Expression denotes a `{0}', where a `{1}' was expected", ExprClassName, sb.ToString ());
1123 public static void UnsafeError (ResolveContext ec, Location loc)
1125 UnsafeError (ec.Report, loc);
1128 public static void UnsafeError (Report Report, Location loc)
1130 Report.Error (214, loc, "Pointers and fixed size buffers may only be used in an unsafe context");
1133 public static void UnsafeInsideIteratorError (ResolveContext rc, Location loc)
1135 UnsafeInsideIteratorError (rc.Report, loc);
1138 public static void UnsafeInsideIteratorError (Report report, Location loc)
1140 report.Error (1629, loc, "Unsafe code may not appear in iterators");
1144 // Converts `source' to an int, uint, long or ulong.
1146 protected Expression ConvertExpressionToArrayIndex (ResolveContext ec, Expression source, bool pointerArray = false)
1148 var btypes = ec.BuiltinTypes;
1150 if (source.type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
1151 Arguments args = new Arguments (1);
1152 args.Add (new Argument (source));
1153 return new DynamicConversion (btypes.Int, CSharpBinderFlags.ConvertArrayIndex, args, source.loc).Resolve (ec);
1156 Expression converted;
1158 using (ec.Set (ResolveContext.Options.CheckedScope)) {
1159 converted = Convert.ImplicitConversion (ec, source, btypes.Int, source.loc);
1160 if (converted == null)
1161 converted = Convert.ImplicitConversion (ec, source, btypes.UInt, source.loc);
1162 if (converted == null)
1163 converted = Convert.ImplicitConversion (ec, source, btypes.Long, source.loc);
1164 if (converted == null)
1165 converted = Convert.ImplicitConversion (ec, source, btypes.ULong, source.loc);
1167 if (converted == null) {
1168 source.Error_ValueCannotBeConverted (ec, btypes.Int, false);
1177 // Only positive constants are allowed at compile time
1179 Constant c = converted as Constant;
1180 if (c != null && c.IsNegative)
1181 Error_NegativeArrayIndex (ec, source.loc);
1183 // No conversion needed to array index
1184 if (converted.Type.BuiltinType == BuiltinTypeSpec.Type.Int)
1187 return new ArrayIndexCast (converted, btypes.Int).Resolve (ec);
1190 public Expression MakePointerAccess (ResolveContext rc, TypeSpec type, Arguments args)
1192 if (args.Count != 1){
1193 rc.Report.Error (196, loc, "A pointer must be indexed by only one value");
1198 if (arg is NamedArgument)
1199 Error_NamedArgument ((NamedArgument) arg, rc.Report);
1201 var index = arg.Expr.Resolve (rc);
1205 index = ConvertExpressionToArrayIndex (rc, index, true);
1207 Expression p = new PointerArithmetic (Binary.Operator.Addition, this, index, type, loc);
1208 return new Indirection (p, loc);
1212 // Derived classes implement this method by cloning the fields that
1213 // could become altered during the Resolve stage
1215 // Only expressions that are created for the parser need to implement
1218 protected virtual void CloneTo (CloneContext clonectx, Expression target)
1220 throw new NotImplementedException (
1222 "CloneTo not implemented for expression {0}", this.GetType ()));
1226 // Clones an expression created by the parser.
1228 // We only support expressions created by the parser so far, not
1229 // expressions that have been resolved (many more classes would need
1230 // to implement CloneTo).
1232 // This infrastructure is here merely for Lambda expressions which
1233 // compile the same code using different type values for the same
1234 // arguments to find the correct overload
1236 public virtual Expression Clone (CloneContext clonectx)
1238 Expression cloned = (Expression) MemberwiseClone ();
1239 CloneTo (clonectx, cloned);
1245 // Implementation of expression to expression tree conversion
1247 public abstract Expression CreateExpressionTree (ResolveContext ec);
1249 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, Arguments args)
1251 return CreateExpressionFactoryCall (ec, name, null, args, loc);
1254 protected Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args)
1256 return CreateExpressionFactoryCall (ec, name, typeArguments, args, loc);
1259 public static Expression CreateExpressionFactoryCall (ResolveContext ec, string name, TypeArguments typeArguments, Arguments args, Location loc)
1261 return new Invocation (new MemberAccess (CreateExpressionTypeExpression (ec, loc), name, typeArguments, loc), args);
1264 protected static TypeExpr CreateExpressionTypeExpression (ResolveContext ec, Location loc)
1266 var t = ec.Module.PredefinedTypes.Expression.Resolve ();
1270 return new TypeExpression (t, loc);
1274 // Implemented by all expressions which support conversion from
1275 // compiler expression to invokable runtime expression. Used by
1276 // dynamic C# binder.
1278 public virtual SLE.Expression MakeExpression (BuilderContext ctx)
1280 throw new NotImplementedException ("MakeExpression for " + GetType ());
1283 public virtual object Accept (StructuralVisitor visitor)
1285 return visitor.Visit (this);
1290 /// This is just a base class for expressions that can
1291 /// appear on statements (invocations, object creation,
1292 /// assignments, post/pre increment and decrement). The idea
1293 /// being that they would support an extra Emition interface that
1294 /// does not leave a result on the stack.
1296 public abstract class ExpressionStatement : Expression
1298 public virtual void MarkReachable (Reachability rc)
1302 public virtual ExpressionStatement ResolveStatement (BlockContext ec)
1304 Expression e = Resolve (ec);
1308 ExpressionStatement es = e as ExpressionStatement;
1309 if (es == null || e is AnonymousMethodBody) {
1310 var reduced = e as IReducedExpressionStatement;
1311 if (reduced != null) {
1312 return EmptyExpressionStatement.Instance;
1315 Error_InvalidExpressionStatement (ec);
1319 // This is quite expensive warning, try to limit the damage
1321 if (MemberAccess.IsValidDotExpression (e.Type) && !(e is Assign || e is Await)) {
1322 WarningAsyncWithoutWait (ec, e);
1328 static void WarningAsyncWithoutWait (BlockContext bc, Expression e)
1330 if (bc.CurrentAnonymousMethod is AsyncInitializer) {
1331 var awaiter = new AwaitStatement.AwaitableMemberAccess (e) {
1336 // Need to do full resolve because GetAwaiter can be extension method
1337 // available only in this context
1339 var mg = awaiter.Resolve (bc) as MethodGroupExpr;
1343 var arguments = new Arguments (0);
1344 mg = mg.OverloadResolve (bc, ref arguments, null, OverloadResolver.Restrictions.ProbingOnly);
1349 // Use same check rules as for real await
1351 var awaiter_definition = bc.Module.GetAwaiter (mg.BestCandidateReturnType);
1352 if (!awaiter_definition.IsValidPattern || !awaiter_definition.INotifyCompletion)
1355 bc.Report.Warning (4014, 1, e.Location,
1356 "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator");
1360 var inv = e as Invocation;
1361 if (inv != null && inv.MethodGroup != null && inv.MethodGroup.BestCandidate.IsAsync) {
1362 // The warning won't be reported for imported methods to maintain warning compatiblity with csc
1363 bc.Report.Warning (4014, 1, e.Location,
1364 "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator or calling `Wait' method");
1370 /// Requests the expression to be emitted in a `statement'
1371 /// context. This means that no new value is left on the
1372 /// stack after invoking this method (constrasted with
1373 /// Emit that will always leave a value on the stack).
1375 public abstract void EmitStatement (EmitContext ec);
1377 public override void EmitSideEffect (EmitContext ec)
1383 interface IReducedExpressionStatement
1388 /// This kind of cast is used to encapsulate the child
1389 /// whose type is child.Type into an expression that is
1390 /// reported to return "return_type". This is used to encapsulate
1391 /// expressions which have compatible types, but need to be dealt
1392 /// at higher levels with.
1394 /// For example, a "byte" expression could be encapsulated in one
1395 /// of these as an "unsigned int". The type for the expression
1396 /// would be "unsigned int".
1399 public abstract class TypeCast : Expression
1401 protected readonly Expression child;
1403 protected TypeCast (Expression child, TypeSpec return_type)
1405 eclass = child.eclass;
1406 loc = child.Location;
1411 public Expression Child {
1417 public override bool ContainsEmitWithAwait ()
1419 return child.ContainsEmitWithAwait ();
1422 public override Expression CreateExpressionTree (ResolveContext ec)
1424 Arguments args = new Arguments (2);
1425 args.Add (new Argument (child.CreateExpressionTree (ec)));
1426 args.Add (new Argument (new TypeOf (type, loc)));
1428 if (type.IsPointer || child.Type.IsPointer)
1429 Error_PointerInsideExpressionTree (ec);
1431 return CreateExpressionFactoryCall (ec, ec.HasSet (ResolveContext.Options.CheckedScope) ? "ConvertChecked" : "Convert", args);
1434 protected override Expression DoResolve (ResolveContext ec)
1436 // This should never be invoked, we are born in fully
1437 // initialized state.
1442 public override void Emit (EmitContext ec)
1447 public override void FlowAnalysis (FlowAnalysisContext fc)
1449 child.FlowAnalysis (fc);
1452 public override SLE.Expression MakeExpression (BuilderContext ctx)
1455 return base.MakeExpression (ctx);
1457 return ctx.HasSet (BuilderContext.Options.CheckedScope) ?
1458 SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type.GetMetaInfo ()) :
1459 SLE.Expression.Convert (child.MakeExpression (ctx), type.GetMetaInfo ());
1463 protected override void CloneTo (CloneContext clonectx, Expression t)
1468 public override bool IsNull {
1469 get { return child.IsNull; }
1473 public class EmptyCast : TypeCast {
1474 EmptyCast (Expression child, TypeSpec target_type)
1475 : base (child, target_type)
1479 public static Expression Create (Expression child, TypeSpec type)
1481 Constant c = child as Constant;
1483 var enum_constant = c as EnumConstant;
1484 if (enum_constant != null)
1485 c = enum_constant.Child;
1487 if (!(c is ReducedExpression.ReducedConstantExpression)) {
1491 var res = c.ConvertImplicitly (type);
1497 EmptyCast e = child as EmptyCast;
1499 return new EmptyCast (e.child, type);
1501 return new EmptyCast (child, type);
1504 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1506 child.EmitBranchable (ec, label, on_true);
1509 public override void EmitSideEffect (EmitContext ec)
1511 child.EmitSideEffect (ec);
1516 // Used for predefined type user operator (no obsolete check, etc.)
1518 public class OperatorCast : TypeCast
1520 readonly MethodSpec conversion_operator;
1522 public OperatorCast (Expression expr, TypeSpec target_type)
1523 : this (expr, target_type, target_type, false)
1527 public OperatorCast (Expression expr, TypeSpec target_type, bool find_explicit)
1528 : this (expr, target_type, target_type, find_explicit)
1532 public OperatorCast (Expression expr, TypeSpec declaringType, TypeSpec returnType, bool isExplicit)
1533 : base (expr, returnType)
1535 var op = isExplicit ? Operator.OpType.Explicit : Operator.OpType.Implicit;
1536 var mi = MemberCache.GetUserOperator (declaringType, op, true);
1539 foreach (MethodSpec oper in mi) {
1540 if (oper.ReturnType != returnType)
1543 if (oper.Parameters.Types[0] == expr.Type) {
1544 conversion_operator = oper;
1550 throw new InternalErrorException ("Missing predefined user operator between `{0}' and `{1}'",
1551 returnType.GetSignatureForError (), expr.Type.GetSignatureForError ());
1554 public override void Emit (EmitContext ec)
1557 ec.Emit (OpCodes.Call, conversion_operator);
1562 // Constant specialization of EmptyCast.
1563 // We need to special case this since an empty cast of
1564 // a constant is still a constant.
1566 public class EmptyConstantCast : Constant
1568 public readonly Constant child;
1570 public EmptyConstantCast (Constant child, TypeSpec type)
1571 : base (child.Location)
1574 throw new ArgumentNullException ("child");
1577 this.eclass = child.eclass;
1581 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1583 if (child.Type == target_type)
1586 // FIXME: check that 'type' can be converted to 'target_type' first
1587 return child.ConvertExplicitly (in_checked_context, target_type);
1590 public override Expression CreateExpressionTree (ResolveContext ec)
1592 Arguments args = Arguments.CreateForExpressionTree (ec, null,
1593 child.CreateExpressionTree (ec),
1594 new TypeOf (type, loc));
1597 Error_PointerInsideExpressionTree (ec);
1599 return CreateExpressionFactoryCall (ec, "Convert", args);
1602 public override bool IsDefaultValue {
1603 get { return child.IsDefaultValue; }
1606 public override bool IsNegative {
1607 get { return child.IsNegative; }
1610 public override bool IsNull {
1611 get { return child.IsNull; }
1614 public override bool IsOneInteger {
1615 get { return child.IsOneInteger; }
1618 public override bool IsSideEffectFree {
1620 return child.IsSideEffectFree;
1624 public override bool IsZeroInteger {
1625 get { return child.IsZeroInteger; }
1628 public override void Emit (EmitContext ec)
1633 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1635 child.EmitBranchable (ec, label, on_true);
1637 // Only to make verifier happy
1638 if (TypeManager.IsGenericParameter (type) && child.IsNull)
1639 ec.Emit (OpCodes.Unbox_Any, type);
1642 public override void EmitSideEffect (EmitContext ec)
1644 child.EmitSideEffect (ec);
1647 public override object GetValue ()
1649 return child.GetValue ();
1652 public override string GetValueAsLiteral ()
1654 return child.GetValueAsLiteral ();
1657 public override long GetValueAsLong ()
1659 return child.GetValueAsLong ();
1662 public override Constant ConvertImplicitly (TypeSpec target_type)
1664 if (type == target_type)
1667 // FIXME: Do we need to check user conversions?
1668 if (!Convert.ImplicitStandardConversionExists (this, target_type))
1671 return child.ConvertImplicitly (target_type);
1676 /// This class is used to wrap literals which belong inside Enums
1678 public class EnumConstant : Constant
1680 public Constant Child;
1682 public EnumConstant (Constant child, TypeSpec enum_type)
1683 : base (child.Location)
1687 this.eclass = ExprClass.Value;
1688 this.type = enum_type;
1691 protected EnumConstant (Location loc)
1696 public override void Emit (EmitContext ec)
1701 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1703 Child.EncodeAttributeValue (rc, enc, Child.Type, parameterType);
1706 public override void EmitBranchable (EmitContext ec, Label label, bool on_true)
1708 Child.EmitBranchable (ec, label, on_true);
1711 public override void EmitSideEffect (EmitContext ec)
1713 Child.EmitSideEffect (ec);
1716 public override string GetSignatureForError()
1718 return Type.GetSignatureForError ();
1721 public override object GetValue ()
1723 return Child.GetValue ();
1727 public override object GetTypedValue ()
1730 // The method can be used in dynamic context only (on closed types)
1732 // System.Enum.ToObject cannot be called on dynamic types
1733 // EnumBuilder has to be used, but we cannot use EnumBuilder
1734 // because it does not properly support generics
1736 return System.Enum.ToObject (type.GetMetaInfo (), Child.GetValue ());
1740 public override string GetValueAsLiteral ()
1742 return Child.GetValueAsLiteral ();
1745 public override long GetValueAsLong ()
1747 return Child.GetValueAsLong ();
1750 public EnumConstant Increment()
1752 return new EnumConstant (((IntegralConstant) Child).Increment (), type);
1755 public override bool IsDefaultValue {
1757 return Child.IsDefaultValue;
1761 public override bool IsSideEffectFree {
1763 return Child.IsSideEffectFree;
1767 public override bool IsZeroInteger {
1768 get { return Child.IsZeroInteger; }
1771 public override bool IsNegative {
1773 return Child.IsNegative;
1777 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
1779 if (Child.Type == target_type)
1782 return Child.ConvertExplicitly (in_checked_context, target_type);
1785 public override Constant ConvertImplicitly (TypeSpec type)
1787 if (this.type == type) {
1791 if (!Convert.ImplicitStandardConversionExists (this, type)){
1795 return Child.ConvertImplicitly (type);
1800 /// This kind of cast is used to encapsulate Value Types in objects.
1802 /// The effect of it is to box the value type emitted by the previous
1805 public class BoxedCast : TypeCast {
1807 public BoxedCast (Expression expr, TypeSpec target_type)
1808 : base (expr, target_type)
1810 eclass = ExprClass.Value;
1813 protected override Expression DoResolve (ResolveContext ec)
1815 // This should never be invoked, we are born in fully
1816 // initialized state.
1821 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
1823 // Only boxing to object type is supported
1824 if (targetType.BuiltinType != BuiltinTypeSpec.Type.Object) {
1825 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
1829 enc.Encode (child.Type);
1830 child.EncodeAttributeValue (rc, enc, child.Type, parameterType);
1833 public override void Emit (EmitContext ec)
1837 ec.Emit (OpCodes.Box, child.Type);
1840 public override void EmitSideEffect (EmitContext ec)
1842 // boxing is side-effectful, since it involves runtime checks, except when boxing to Object or ValueType
1843 // so, we need to emit the box+pop instructions in most cases
1844 if (child.Type.IsStruct &&
1845 (type.BuiltinType == BuiltinTypeSpec.Type.Object || type.BuiltinType == BuiltinTypeSpec.Type.ValueType))
1846 child.EmitSideEffect (ec);
1848 base.EmitSideEffect (ec);
1852 public class UnboxCast : TypeCast {
1853 public UnboxCast (Expression expr, TypeSpec return_type)
1854 : base (expr, return_type)
1858 protected override Expression DoResolve (ResolveContext ec)
1860 // This should never be invoked, we are born in fully
1861 // initialized state.
1866 public override void Emit (EmitContext ec)
1870 ec.Emit (OpCodes.Unbox_Any, type);
1875 /// This is used to perform explicit numeric conversions.
1877 /// Explicit numeric conversions might trigger exceptions in a checked
1878 /// context, so they should generate the conv.ovf opcodes instead of
1881 public class ConvCast : TypeCast {
1882 public enum Mode : byte {
1883 I1_U1, I1_U2, I1_U4, I1_U8, I1_CH,
1885 I2_I1, I2_U1, I2_U2, I2_U4, I2_U8, I2_CH,
1886 U2_I1, U2_U1, U2_I2, U2_CH,
1887 I4_I1, I4_U1, I4_I2, I4_U2, I4_U4, I4_U8, I4_CH,
1888 U4_I1, U4_U1, U4_I2, U4_U2, U4_I4, U4_CH,
1889 I8_I1, I8_U1, I8_I2, I8_U2, I8_I4, I8_U4, I8_U8, I8_CH, I8_I,
1890 U8_I1, U8_U1, U8_I2, U8_U2, U8_I4, U8_U4, U8_I8, U8_CH, U8_I,
1891 CH_I1, CH_U1, CH_I2,
1892 R4_I1, R4_U1, R4_I2, R4_U2, R4_I4, R4_U4, R4_I8, R4_U8, R4_CH,
1893 R8_I1, R8_U1, R8_I2, R8_U2, R8_I4, R8_U4, R8_I8, R8_U8, R8_CH, R8_R4,
1899 public ConvCast (Expression child, TypeSpec return_type, Mode m)
1900 : base (child, return_type)
1905 protected override Expression DoResolve (ResolveContext ec)
1907 // This should never be invoked, we are born in fully
1908 // initialized state.
1913 public override string ToString ()
1915 return String.Format ("ConvCast ({0}, {1})", mode, child);
1918 public override void Emit (EmitContext ec)
1924 public static void Emit (EmitContext ec, Mode mode)
1926 if (ec.HasSet (EmitContext.Options.CheckedScope)) {
1928 case Mode.I1_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1929 case Mode.I1_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1930 case Mode.I1_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1931 case Mode.I1_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1932 case Mode.I1_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1934 case Mode.U1_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1935 case Mode.U1_CH: /* nothing */ break;
1937 case Mode.I2_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1938 case Mode.I2_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1939 case Mode.I2_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1940 case Mode.I2_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1941 case Mode.I2_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1942 case Mode.I2_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1944 case Mode.U2_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1945 case Mode.U2_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1946 case Mode.U2_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1947 case Mode.U2_CH: /* nothing */ break;
1949 case Mode.I4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1950 case Mode.I4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1951 case Mode.I4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1952 case Mode.I4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1953 case Mode.I4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1954 case Mode.I4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1955 case Mode.I4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1957 case Mode.U4_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1958 case Mode.U4_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1959 case Mode.U4_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1960 case Mode.U4_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1961 case Mode.U4_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1962 case Mode.U4_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1964 case Mode.I8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1965 case Mode.I8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1966 case Mode.I8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1967 case Mode.I8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1968 case Mode.I8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1969 case Mode.I8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1970 case Mode.I8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1971 case Mode.I8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1972 case Mode.I8_I: ec.Emit (OpCodes.Conv_Ovf_U); break;
1974 case Mode.U8_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1975 case Mode.U8_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1976 case Mode.U8_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1977 case Mode.U8_U2: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1978 case Mode.U8_I4: ec.Emit (OpCodes.Conv_Ovf_I4_Un); break;
1979 case Mode.U8_U4: ec.Emit (OpCodes.Conv_Ovf_U4_Un); break;
1980 case Mode.U8_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
1981 case Mode.U8_CH: ec.Emit (OpCodes.Conv_Ovf_U2_Un); break;
1982 case Mode.U8_I: ec.Emit (OpCodes.Conv_Ovf_U_Un); break;
1984 case Mode.CH_I1: ec.Emit (OpCodes.Conv_Ovf_I1_Un); break;
1985 case Mode.CH_U1: ec.Emit (OpCodes.Conv_Ovf_U1_Un); break;
1986 case Mode.CH_I2: ec.Emit (OpCodes.Conv_Ovf_I2_Un); break;
1988 case Mode.R4_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1989 case Mode.R4_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
1990 case Mode.R4_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
1991 case Mode.R4_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1992 case Mode.R4_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
1993 case Mode.R4_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
1994 case Mode.R4_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
1995 case Mode.R4_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
1996 case Mode.R4_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
1998 case Mode.R8_I1: ec.Emit (OpCodes.Conv_Ovf_I1); break;
1999 case Mode.R8_U1: ec.Emit (OpCodes.Conv_Ovf_U1); break;
2000 case Mode.R8_I2: ec.Emit (OpCodes.Conv_Ovf_I2); break;
2001 case Mode.R8_U2: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2002 case Mode.R8_I4: ec.Emit (OpCodes.Conv_Ovf_I4); break;
2003 case Mode.R8_U4: ec.Emit (OpCodes.Conv_Ovf_U4); break;
2004 case Mode.R8_I8: ec.Emit (OpCodes.Conv_Ovf_I8); break;
2005 case Mode.R8_U8: ec.Emit (OpCodes.Conv_Ovf_U8); break;
2006 case Mode.R8_CH: ec.Emit (OpCodes.Conv_Ovf_U2); break;
2007 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2009 case Mode.I_I8: ec.Emit (OpCodes.Conv_Ovf_I8_Un); break;
2013 case Mode.I1_U1: ec.Emit (OpCodes.Conv_U1); break;
2014 case Mode.I1_U2: ec.Emit (OpCodes.Conv_U2); break;
2015 case Mode.I1_U4: ec.Emit (OpCodes.Conv_U4); break;
2016 case Mode.I1_U8: ec.Emit (OpCodes.Conv_I8); break;
2017 case Mode.I1_CH: ec.Emit (OpCodes.Conv_U2); break;
2019 case Mode.U1_I1: ec.Emit (OpCodes.Conv_I1); break;
2020 case Mode.U1_CH: ec.Emit (OpCodes.Conv_U2); break;
2022 case Mode.I2_I1: ec.Emit (OpCodes.Conv_I1); break;
2023 case Mode.I2_U1: ec.Emit (OpCodes.Conv_U1); break;
2024 case Mode.I2_U2: ec.Emit (OpCodes.Conv_U2); break;
2025 case Mode.I2_U4: ec.Emit (OpCodes.Conv_U4); break;
2026 case Mode.I2_U8: ec.Emit (OpCodes.Conv_I8); break;
2027 case Mode.I2_CH: ec.Emit (OpCodes.Conv_U2); break;
2029 case Mode.U2_I1: ec.Emit (OpCodes.Conv_I1); break;
2030 case Mode.U2_U1: ec.Emit (OpCodes.Conv_U1); break;
2031 case Mode.U2_I2: ec.Emit (OpCodes.Conv_I2); break;
2032 case Mode.U2_CH: /* nothing */ break;
2034 case Mode.I4_I1: ec.Emit (OpCodes.Conv_I1); break;
2035 case Mode.I4_U1: ec.Emit (OpCodes.Conv_U1); break;
2036 case Mode.I4_I2: ec.Emit (OpCodes.Conv_I2); break;
2037 case Mode.I4_U4: /* nothing */ break;
2038 case Mode.I4_U2: ec.Emit (OpCodes.Conv_U2); break;
2039 case Mode.I4_U8: ec.Emit (OpCodes.Conv_I8); break;
2040 case Mode.I4_CH: ec.Emit (OpCodes.Conv_U2); break;
2042 case Mode.U4_I1: ec.Emit (OpCodes.Conv_I1); break;
2043 case Mode.U4_U1: ec.Emit (OpCodes.Conv_U1); break;
2044 case Mode.U4_I2: ec.Emit (OpCodes.Conv_I2); break;
2045 case Mode.U4_U2: ec.Emit (OpCodes.Conv_U2); break;
2046 case Mode.U4_I4: /* nothing */ break;
2047 case Mode.U4_CH: ec.Emit (OpCodes.Conv_U2); break;
2049 case Mode.I8_I1: ec.Emit (OpCodes.Conv_I1); break;
2050 case Mode.I8_U1: ec.Emit (OpCodes.Conv_U1); break;
2051 case Mode.I8_I2: ec.Emit (OpCodes.Conv_I2); break;
2052 case Mode.I8_U2: ec.Emit (OpCodes.Conv_U2); break;
2053 case Mode.I8_I4: ec.Emit (OpCodes.Conv_I4); break;
2054 case Mode.I8_U4: ec.Emit (OpCodes.Conv_U4); break;
2055 case Mode.I8_U8: /* nothing */ break;
2056 case Mode.I8_CH: ec.Emit (OpCodes.Conv_U2); break;
2057 case Mode.I8_I: ec.Emit (OpCodes.Conv_U); break;
2059 case Mode.U8_I1: ec.Emit (OpCodes.Conv_I1); break;
2060 case Mode.U8_U1: ec.Emit (OpCodes.Conv_U1); break;
2061 case Mode.U8_I2: ec.Emit (OpCodes.Conv_I2); break;
2062 case Mode.U8_U2: ec.Emit (OpCodes.Conv_U2); break;
2063 case Mode.U8_I4: ec.Emit (OpCodes.Conv_I4); break;
2064 case Mode.U8_U4: ec.Emit (OpCodes.Conv_U4); break;
2065 case Mode.U8_I8: /* nothing */ break;
2066 case Mode.U8_CH: ec.Emit (OpCodes.Conv_U2); break;
2067 case Mode.U8_I: ec.Emit (OpCodes.Conv_U); break;
2069 case Mode.CH_I1: ec.Emit (OpCodes.Conv_I1); break;
2070 case Mode.CH_U1: ec.Emit (OpCodes.Conv_U1); break;
2071 case Mode.CH_I2: ec.Emit (OpCodes.Conv_I2); break;
2073 case Mode.R4_I1: ec.Emit (OpCodes.Conv_I1); break;
2074 case Mode.R4_U1: ec.Emit (OpCodes.Conv_U1); break;
2075 case Mode.R4_I2: ec.Emit (OpCodes.Conv_I2); break;
2076 case Mode.R4_U2: ec.Emit (OpCodes.Conv_U2); break;
2077 case Mode.R4_I4: ec.Emit (OpCodes.Conv_I4); break;
2078 case Mode.R4_U4: ec.Emit (OpCodes.Conv_U4); break;
2079 case Mode.R4_I8: ec.Emit (OpCodes.Conv_I8); break;
2080 case Mode.R4_U8: ec.Emit (OpCodes.Conv_U8); break;
2081 case Mode.R4_CH: ec.Emit (OpCodes.Conv_U2); break;
2083 case Mode.R8_I1: ec.Emit (OpCodes.Conv_I1); break;
2084 case Mode.R8_U1: ec.Emit (OpCodes.Conv_U1); break;
2085 case Mode.R8_I2: ec.Emit (OpCodes.Conv_I2); break;
2086 case Mode.R8_U2: ec.Emit (OpCodes.Conv_U2); break;
2087 case Mode.R8_I4: ec.Emit (OpCodes.Conv_I4); break;
2088 case Mode.R8_U4: ec.Emit (OpCodes.Conv_U4); break;
2089 case Mode.R8_I8: ec.Emit (OpCodes.Conv_I8); break;
2090 case Mode.R8_U8: ec.Emit (OpCodes.Conv_U8); break;
2091 case Mode.R8_CH: ec.Emit (OpCodes.Conv_U2); break;
2092 case Mode.R8_R4: ec.Emit (OpCodes.Conv_R4); break;
2094 case Mode.I_I8: ec.Emit (OpCodes.Conv_U8); break;
2100 class OpcodeCast : TypeCast
2104 public OpcodeCast (Expression child, TypeSpec return_type, OpCode op)
2105 : base (child, return_type)
2110 protected override Expression DoResolve (ResolveContext ec)
2112 // This should never be invoked, we are born in fully
2113 // initialized state.
2118 public override void Emit (EmitContext ec)
2124 public TypeSpec UnderlyingType {
2125 get { return child.Type; }
2130 // Opcode casts expression with 2 opcodes but only
2131 // single expression tree node
2133 class OpcodeCastDuplex : OpcodeCast
2135 readonly OpCode second;
2137 public OpcodeCastDuplex (Expression child, TypeSpec returnType, OpCode first, OpCode second)
2138 : base (child, returnType, first)
2140 this.second = second;
2143 public override void Emit (EmitContext ec)
2151 /// This kind of cast is used to encapsulate a child and cast it
2152 /// to the class requested
2154 public sealed class ClassCast : TypeCast {
2155 readonly bool forced;
2157 public ClassCast (Expression child, TypeSpec return_type)
2158 : base (child, return_type)
2162 public ClassCast (Expression child, TypeSpec return_type, bool forced)
2163 : base (child, return_type)
2165 this.forced = forced;
2168 public override void Emit (EmitContext ec)
2172 bool gen = TypeManager.IsGenericParameter (child.Type);
2174 ec.Emit (OpCodes.Box, child.Type);
2176 if (type.IsGenericParameter) {
2177 ec.Emit (OpCodes.Unbox_Any, type);
2184 ec.Emit (OpCodes.Castclass, type);
2189 // Created during resolving pahse when an expression is wrapped or constantified
2190 // and original expression can be used later (e.g. for expression trees)
2192 public class ReducedExpression : Expression
2194 public class ReducedConstantExpression : EmptyConstantCast
2196 readonly Expression orig_expr;
2198 public ReducedConstantExpression (Constant expr, Expression orig_expr)
2199 : base (expr, expr.Type)
2201 this.orig_expr = orig_expr;
2204 public Expression OriginalExpression {
2210 public override Constant ConvertImplicitly (TypeSpec target_type)
2212 Constant c = base.ConvertImplicitly (target_type);
2214 c = new ReducedConstantExpression (c, orig_expr);
2219 public override Expression CreateExpressionTree (ResolveContext ec)
2221 return orig_expr.CreateExpressionTree (ec);
2224 public override Constant ConvertExplicitly (bool in_checked_context, TypeSpec target_type)
2226 Constant c = base.ConvertExplicitly (in_checked_context, target_type);
2228 c = new ReducedConstantExpression (c, orig_expr);
2232 public override void EncodeAttributeValue (IMemberContext rc, AttributeEncoder enc, TypeSpec targetType, TypeSpec parameterType)
2235 // LAMESPEC: Reduced conditional expression is allowed as an attribute argument
2237 if (orig_expr is Conditional)
2238 child.EncodeAttributeValue (rc, enc, targetType,parameterType);
2240 base.EncodeAttributeValue (rc, enc, targetType, parameterType);
2244 sealed class ReducedConstantStatement : ReducedConstantExpression, IReducedExpressionStatement
2246 public ReducedConstantStatement (Constant expr, Expression origExpr)
2247 : base (expr, origExpr)
2252 sealed class ReducedExpressionStatement : ExpressionStatement
2254 readonly Expression orig_expr;
2255 readonly ExpressionStatement stm;
2257 public ReducedExpressionStatement (ExpressionStatement stm, Expression orig)
2259 this.orig_expr = orig;
2261 this.eclass = stm.eclass;
2262 this.type = stm.Type;
2264 this.loc = orig.Location;
2267 public override bool ContainsEmitWithAwait ()
2269 return stm.ContainsEmitWithAwait ();
2272 public override Expression CreateExpressionTree (ResolveContext ec)
2274 return orig_expr.CreateExpressionTree (ec);
2277 protected override Expression DoResolve (ResolveContext ec)
2282 public override void Emit (EmitContext ec)
2287 public override void EmitStatement (EmitContext ec)
2289 stm.EmitStatement (ec);
2292 public override void FlowAnalysis (FlowAnalysisContext fc)
2294 stm.FlowAnalysis (fc);
2298 readonly Expression expr, orig_expr;
2300 private ReducedExpression (Expression expr, Expression orig_expr)
2303 this.eclass = expr.eclass;
2304 this.type = expr.Type;
2305 this.orig_expr = orig_expr;
2306 this.loc = orig_expr.Location;
2311 public override bool IsSideEffectFree {
2313 return expr.IsSideEffectFree;
2317 public Expression OriginalExpression {
2325 public override bool ContainsEmitWithAwait ()
2327 return expr.ContainsEmitWithAwait ();
2331 // Creates fully resolved expression switcher
2333 public static Constant Create (Constant expr, Expression originalExpr)
2335 if (expr.eclass == ExprClass.Unresolved)
2336 throw new ArgumentException ("Unresolved expression");
2338 if (originalExpr is ExpressionStatement)
2339 return new ReducedConstantStatement (expr, originalExpr);
2341 return new ReducedConstantExpression (expr, originalExpr);
2344 public static ExpressionStatement Create (ExpressionStatement s, Expression orig)
2346 return new ReducedExpressionStatement (s, orig);
2349 public static Expression Create (Expression expr, Expression original_expr)
2351 return Create (expr, original_expr, true);
2355 // Creates unresolved reduce expression. The original expression has to be
2356 // already resolved. Created expression is constant based based on `expr'
2357 // value unless canBeConstant is used
2359 public static Expression Create (Expression expr, Expression original_expr, bool canBeConstant)
2361 if (canBeConstant) {
2362 Constant c = expr as Constant;
2364 return Create (c, original_expr);
2367 ExpressionStatement s = expr as ExpressionStatement;
2369 return Create (s, original_expr);
2371 if (expr.eclass == ExprClass.Unresolved)
2372 throw new ArgumentException ("Unresolved expression");
2374 return new ReducedExpression (expr, original_expr);
2377 public override Expression CreateExpressionTree (ResolveContext ec)
2379 return orig_expr.CreateExpressionTree (ec);
2382 protected override Expression DoResolve (ResolveContext ec)
2387 public override void Emit (EmitContext ec)
2392 public override Expression EmitToField (EmitContext ec)
2394 return expr.EmitToField(ec);
2397 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2399 expr.EmitBranchable (ec, target, on_true);
2402 public override void FlowAnalysis (FlowAnalysisContext fc)
2404 expr.FlowAnalysis (fc);
2407 public override SLE.Expression MakeExpression (BuilderContext ctx)
2409 return orig_expr.MakeExpression (ctx);
2414 // Standard composite pattern
2416 public abstract class CompositeExpression : Expression
2418 protected Expression expr;
2420 protected CompositeExpression (Expression expr)
2423 this.loc = expr.Location;
2426 public override bool ContainsEmitWithAwait ()
2428 return expr.ContainsEmitWithAwait ();
2431 public override Expression CreateExpressionTree (ResolveContext rc)
2433 return expr.CreateExpressionTree (rc);
2436 public Expression Child {
2437 get { return expr; }
2440 protected override Expression DoResolve (ResolveContext rc)
2442 expr = expr.Resolve (rc);
2447 eclass = expr.eclass;
2451 public override void Emit (EmitContext ec)
2456 public override bool IsNull {
2457 get { return expr.IsNull; }
2462 // Base of expressions used only to narrow resolve flow
2464 public abstract class ShimExpression : Expression
2466 protected Expression expr;
2468 protected ShimExpression (Expression expr)
2473 public Expression Expr {
2479 protected override void CloneTo (CloneContext clonectx, Expression t)
2484 ShimExpression target = (ShimExpression) t;
2485 target.expr = expr.Clone (clonectx);
2488 public override bool ContainsEmitWithAwait ()
2490 return expr.ContainsEmitWithAwait ();
2493 public override Expression CreateExpressionTree (ResolveContext ec)
2495 throw new NotSupportedException ("ET");
2498 public override void Emit (EmitContext ec)
2500 throw new InternalErrorException ("Missing Resolve call");
2504 public class UnreachableExpression : Expression
2506 public UnreachableExpression (Expression expr)
2508 this.loc = expr.Location;
2511 public override Expression CreateExpressionTree (ResolveContext ec)
2514 throw new NotImplementedException ();
2517 protected override Expression DoResolve (ResolveContext rc)
2519 throw new NotSupportedException ();
2522 public override void FlowAnalysis (FlowAnalysisContext fc)
2524 fc.Report.Warning (429, 4, loc, "Unreachable expression code detected");
2527 public override void Emit (EmitContext ec)
2531 public override void EmitBranchable (EmitContext ec, Label target, bool on_true)
2537 // Unresolved type name expressions
2539 public abstract class ATypeNameExpression : FullNamedExpression
2542 protected TypeArguments targs;
2544 protected ATypeNameExpression (string name, Location l)
2550 protected ATypeNameExpression (string name, TypeArguments targs, Location l)
2557 protected ATypeNameExpression (string name, int arity, Location l)
2558 : this (name, new UnboundTypeArguments (arity, l), l)
2566 return targs == null ? 0 : targs.Count;
2570 public bool HasTypeArguments {
2572 return targs != null && !targs.IsEmpty;
2576 public string Name {
2585 public TypeArguments TypeArguments {
2593 public override bool Equals (object obj)
2595 ATypeNameExpression atne = obj as ATypeNameExpression;
2596 return atne != null && atne.Name == Name &&
2597 (targs == null || targs.Equals (atne.targs));
2600 public override int GetHashCode ()
2602 return Name.GetHashCode ();
2605 // TODO: Move it to MemberCore
2606 public static string GetMemberType (MemberCore mc)
2612 if (mc is FieldBase)
2614 if (mc is MethodCore)
2616 if (mc is EnumMember)
2624 public override string GetSignatureForError ()
2626 if (targs != null) {
2627 return Name + "<" + targs.GetSignatureForError () + ">";
2633 public abstract Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restriction);
2637 /// SimpleName expressions are formed of a single word and only happen at the beginning
2638 /// of a dotted-name.
2640 public class SimpleName : ATypeNameExpression
2642 public SimpleName (string name, Location l)
2647 public SimpleName (string name, TypeArguments args, Location l)
2648 : base (name, args, l)
2652 public SimpleName (string name, int arity, Location l)
2653 : base (name, arity, l)
2657 public SimpleName GetMethodGroup ()
2659 return new SimpleName (Name, targs, loc);
2662 protected override Expression DoResolve (ResolveContext rc)
2664 return SimpleNameResolve (rc, null);
2667 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
2669 return SimpleNameResolve (ec, right_side);
2672 public void Error_NameDoesNotExist (ResolveContext rc)
2674 rc.Report.Error (103, loc, "The name `{0}' does not exist in the current context", Name);
2677 protected virtual void Error_TypeOrNamespaceNotFound (IMemberContext ctx)
2679 if (ctx.CurrentType != null) {
2680 var member = MemberLookup (ctx, false, ctx.CurrentType, Name, 0, MemberLookupRestrictions.ExactArity, loc) as MemberExpr;
2681 if (member != null) {
2682 Error_UnexpectedKind (ctx, member, "type", member.KindName, loc);
2687 var report = ctx.Module.Compiler.Report;
2689 var retval = ctx.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2690 if (retval != null) {
2691 report.SymbolRelatedToPreviousError (retval.Type);
2692 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
2696 retval = ctx.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2697 if (retval != null) {
2698 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, loc);
2702 var ns_candidates = ctx.Module.GlobalRootNamespace.FindTypeNamespaces (ctx, Name, Arity);
2703 if (ns_candidates != null) {
2704 if (ctx is UsingAliasNamespace.AliasContext) {
2705 report.Error (246, loc,
2706 "The type or namespace name `{1}' could not be found. Consider using fully qualified name `{0}.{1}'",
2707 ns_candidates[0], Name);
2709 string usings = string.Join ("' or `", ns_candidates.ToArray ());
2710 report.Error (246, loc,
2711 "The type or namespace name `{0}' could not be found. Are you missing `{1}' using directive?",
2715 report.Error (246, loc,
2716 "The type or namespace name `{0}' could not be found. Are you missing an assembly reference?",
2721 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
2723 FullNamedExpression fne = mc.LookupNamespaceOrType (Name, Arity, LookupMode.Normal, loc);
2726 if (fne.Type != null && Arity > 0) {
2727 if (HasTypeArguments) {
2728 GenericTypeExpr ct = new GenericTypeExpr (fne.Type, targs, loc);
2729 if (ct.ResolveAsType (mc) == null)
2735 targs.Resolve (mc, allowUnboundTypeArguments);
2737 return new GenericOpenTypeExpr (fne.Type, loc);
2741 // dynamic namespace is ignored when dynamic is allowed (does not apply to types)
2743 if (!(fne is NamespaceExpression))
2747 if (Arity == 0 && Name == "dynamic" && !(mc is NamespaceContainer) && mc.Module.Compiler.Settings.Version > LanguageVersion.V_3) {
2748 if (!mc.Module.PredefinedAttributes.Dynamic.IsDefined) {
2749 mc.Module.Compiler.Report.Error (1980, Location,
2750 "Dynamic keyword requires `{0}' to be defined. Are you missing System.Core.dll assembly reference?",
2751 mc.Module.PredefinedAttributes.Dynamic.GetSignatureForError ());
2754 fne = new DynamicTypeExpr (loc);
2755 fne.ResolveAsType (mc);
2761 Error_TypeOrNamespaceNotFound (mc);
2765 public bool IsPossibleTypeOrNamespace (IMemberContext mc)
2767 return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) != null;
2770 public bool IsPossibleType (IMemberContext mc)
2772 return mc.LookupNamespaceOrType (Name, Arity, LookupMode.Probing, loc) is TypeExpr;
2775 public override Expression LookupNameExpression (ResolveContext rc, MemberLookupRestrictions restrictions)
2777 int lookup_arity = Arity;
2778 bool errorMode = false;
2780 Block current_block = rc.CurrentBlock;
2781 INamedBlockVariable variable = null;
2782 bool variable_found = false;
2786 // Stage 1: binding to local variables or parameters
2788 // LAMESPEC: It should take invocableOnly into account but that would break csc compatibility
2790 if (current_block != null && lookup_arity == 0) {
2791 if (current_block.ParametersBlock.TopBlock.GetLocalName (Name, current_block.Original, ref variable)) {
2792 if (!variable.IsDeclared) {
2793 // We found local name in accessible block but it's not
2794 // initialized yet, maybe the user wanted to bind to something else
2796 variable_found = true;
2798 e = variable.CreateReferenceExpression (rc, loc);
2801 Error_TypeArgumentsCannotBeUsed (rc, "variable", Name, loc);
2810 // Stage 2: Lookup members if we are inside a type up to top level type for nested types
2812 TypeSpec member_type = rc.CurrentType;
2813 for (; member_type != null; member_type = member_type.DeclaringType) {
2814 e = MemberLookup (rc, errorMode, member_type, Name, lookup_arity, restrictions, loc);
2818 var me = e as MemberExpr;
2820 // The name matches a type, defer to ResolveAsTypeStep
2828 if (variable != null) {
2829 if (me is FieldExpr || me is ConstantExpr || me is EventExpr || me is PropertyExpr) {
2830 rc.Report.Error (844, loc,
2831 "A local variable `{0}' cannot be used before it is declared. Consider renaming the local variable when it hides the member `{1}'",
2832 Name, me.GetSignatureForError ());
2836 } else if (me is MethodGroupExpr || me is PropertyExpr || me is IndexerExpr) {
2837 // Leave it to overload resolution to report correct error
2839 // TODO: rc.Report.SymbolRelatedToPreviousError ()
2840 ErrorIsInaccesible (rc, me.GetSignatureForError (), loc);
2844 // MemberLookup does not check accessors availability, this is actually needed for properties only
2846 var pe = me as PropertyExpr;
2849 // Break as there is no other overload available anyway
2850 if ((restrictions & MemberLookupRestrictions.ReadAccess) != 0) {
2851 if (!pe.PropertyInfo.HasGet || !pe.PropertyInfo.Get.IsAccessible (rc))
2854 pe.Getter = pe.PropertyInfo.Get;
2856 if (!pe.PropertyInfo.HasSet) {
2857 if (rc.HasSet (ResolveContext.Options.ConstructorScope) && pe.IsAutoPropertyAccess &&
2858 pe.PropertyInfo.DeclaringType == rc.CurrentType && pe.IsStatic == rc.IsStatic) {
2859 var p = (Property) pe.PropertyInfo.MemberDefinition;
2860 return new FieldExpr (p.BackingField, loc);
2863 variable_found = true;
2867 if (!pe.PropertyInfo.Set.IsAccessible (rc)) {
2868 variable_found = true;
2872 pe.Setter = pe.PropertyInfo.Set;
2877 // TODO: It's used by EventExpr -> FieldExpr transformation only
2878 // TODO: Should go to MemberAccess
2879 me = me.ResolveMemberAccess (rc, null, null);
2882 targs.Resolve (rc, false);
2883 me.SetTypeArguments (rc, targs);
2889 var expr = NamespaceContainer.LookupStaticUsings (rc, Name, Arity, loc);
2892 targs.Resolve (rc, false);
2894 var me = expr as MemberExpr;
2896 me.SetTypeArguments (rc, targs);
2902 // Stage 3: Lookup nested types, namespaces and type parameters in the context
2904 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0 && !variable_found) {
2905 if (IsPossibleTypeOrNamespace (rc)) {
2906 return ResolveAsTypeOrNamespace (rc, false);
2910 if ((restrictions & MemberLookupRestrictions.NameOfExcluded) == 0 && Name == "nameof")
2911 return new NameOf (this);
2914 if (variable_found) {
2915 rc.Report.Error (841, loc, "A local variable `{0}' cannot be used before it is declared", Name);
2918 var tparams = rc.CurrentTypeParameters;
2919 if (tparams != null) {
2920 if (tparams.Find (Name) != null) {
2921 Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2926 var ct = rc.CurrentType;
2928 if (ct.MemberDefinition.TypeParametersCount > 0) {
2929 foreach (var ctp in ct.MemberDefinition.TypeParameters) {
2930 if (ctp.Name == Name) {
2931 Error_TypeArgumentsCannotBeUsed (rc, "type parameter", Name, loc);
2937 ct = ct.DeclaringType;
2938 } while (ct != null);
2941 if ((restrictions & MemberLookupRestrictions.InvocableOnly) == 0) {
2942 e = rc.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
2944 rc.Report.SymbolRelatedToPreviousError (e.Type);
2945 ErrorIsInaccesible (rc, e.GetSignatureForError (), loc);
2949 var me = MemberLookup (rc, false, rc.CurrentType, Name, Arity, restrictions & ~MemberLookupRestrictions.InvocableOnly, loc) as MemberExpr;
2951 Error_UnexpectedKind (rc, me, "method group", me.KindName, loc);
2952 return ErrorExpression.Instance;
2956 e = rc.LookupNamespaceOrType (Name, -System.Math.Max (1, Arity), LookupMode.Probing, loc);
2958 if (e.Type.Arity != Arity && (restrictions & MemberLookupRestrictions.IgnoreArity) == 0) {
2959 Error_TypeArgumentsCannotBeUsed (rc, e.Type, loc);
2963 if (e is TypeExpr) {
2964 // TypeExpression does not have correct location
2965 if (e is TypeExpression)
2966 e = new TypeExpression (e.Type, loc);
2972 Error_NameDoesNotExist (rc);
2975 return ErrorExpression.Instance;
2978 if (rc.Module.Evaluator != null) {
2979 var fi = rc.Module.Evaluator.LookupField (Name);
2981 return new FieldExpr (fi.Item1, loc);
2989 Expression SimpleNameResolve (ResolveContext ec, Expression right_side)
2991 Expression e = LookupNameExpression (ec, right_side == null ? MemberLookupRestrictions.ReadAccess : MemberLookupRestrictions.None);
2996 if (e is FullNamedExpression && e.eclass != ExprClass.Unresolved) {
2997 Error_UnexpectedKind (ec, e, "variable", e.ExprClassName, loc);
3001 if (right_side != null) {
3002 e = e.ResolveLValue (ec, right_side);
3010 public override object Accept (StructuralVisitor visitor)
3012 return visitor.Visit (this);
3017 /// Represents a namespace or a type. The name of the class was inspired by
3018 /// section 10.8.1 (Fully Qualified Names).
3020 public abstract class FullNamedExpression : Expression
3022 protected override void CloneTo (CloneContext clonectx, Expression target)
3024 // Do nothing, most unresolved type expressions cannot be
3025 // resolved to different type
3028 public override bool ContainsEmitWithAwait ()
3033 public override Expression CreateExpressionTree (ResolveContext ec)
3035 throw new NotSupportedException ("ET");
3038 public abstract FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments);
3041 // This is used to resolve the expression as a type, a null
3042 // value will be returned if the expression is not a type
3045 public override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3047 FullNamedExpression fne = ResolveAsTypeOrNamespace (mc, allowUnboundTypeArguments);
3052 TypeExpr te = fne as TypeExpr;
3054 Error_UnexpectedKind (mc, fne, "type", fne.ExprClassName, loc);
3062 var dep = type.GetMissingDependencies ();
3064 ImportedTypeDefinition.Error_MissingDependency (mc, dep, loc);
3067 if (type.Kind == MemberKind.Void) {
3068 mc.Module.Compiler.Report.Error (673, loc, "System.Void cannot be used from C#. Consider using `void'");
3072 // Obsolete checks cannot be done when resolving base context as they
3073 // require type dependencies to be set but we are in process of resolving them
3075 if (mc is ResolveContext) {
3076 var oa = type.GetAttributeObsolete ();
3077 if (oa != null && !mc.IsObsolete)
3078 AttributeTester.Report_ObsoleteMessage (oa, type.GetSignatureForError (), fne.Location, mc.Module.Compiler.Report);
3085 public override void Emit (EmitContext ec)
3087 throw new InternalErrorException ("FullNamedExpression `{0}' found in resolved tree",
3088 GetSignatureForError ());
3093 /// Expression that evaluates to a type
3095 public abstract class TypeExpr : FullNamedExpression
3097 public sealed override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3103 protected sealed override Expression DoResolve (ResolveContext ec)
3109 public override bool Equals (object obj)
3111 TypeExpr tobj = obj as TypeExpr;
3115 return Type == tobj.Type;
3118 public override int GetHashCode ()
3120 return Type.GetHashCode ();
3125 /// Fully resolved Expression that already evaluated to a type
3127 public class TypeExpression : TypeExpr
3129 public TypeExpression (TypeSpec t, Location l)
3132 eclass = ExprClass.Type;
3136 public sealed override TypeSpec ResolveAsType (IMemberContext mc, bool allowUnboundTypeArguments = false)
3142 public class NamespaceExpression : FullNamedExpression
3144 readonly Namespace ns;
3146 public NamespaceExpression (Namespace ns, Location loc)
3149 this.Type = InternalType.Namespace;
3150 this.eclass = ExprClass.Namespace;
3154 public Namespace Namespace {
3160 protected override Expression DoResolve (ResolveContext rc)
3162 throw new NotImplementedException ();
3165 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc, bool allowUnboundTypeArguments)
3170 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
3172 var retval = Namespace.LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
3173 if (retval != null) {
3174 // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.MemberDefinition);
3175 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
3179 retval = Namespace.LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
3180 if (retval != null) {
3181 Error_TypeArgumentsCannotBeUsed (ctx, retval, loc);
3186 if (arity > 0 && Namespace.TryGetNamespace (name, out ns)) {
3187 Error_TypeArgumentsCannotBeUsed (ctx, ExprClassName, ns.GetSignatureForError (), loc);
3191 string assembly = null;
3192 string possible_name = Namespace.GetSignatureForError () + "." + name;
3194 // Only assembly unique name should be added
3195 switch (possible_name) {
3196 case "System.Drawing":
3197 case "System.Web.Services":
3200 case "System.Configuration":
3201 case "System.Data.Services":
3202 case "System.DirectoryServices":
3204 case "System.Net.Http":
3205 case "System.Numerics":
3206 case "System.Runtime.Caching":
3207 case "System.ServiceModel":
3208 case "System.Transactions":
3209 case "System.Web.Routing":
3210 case "System.Xml.Linq":
3212 assembly = possible_name;
3216 case "System.Linq.Expressions":
3217 assembly = "System.Core";
3220 case "System.Windows.Forms":
3221 case "System.Windows.Forms.Layout":
3222 assembly = "System.Windows.Forms";
3226 assembly = assembly == null ? "an" : "`" + assembly + "'";
3228 if (Namespace is GlobalRootNamespace) {
3229 ctx.Module.Compiler.Report.Error (400, loc,
3230 "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
3233 ctx.Module.Compiler.Report.Error (234, loc,
3234 "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
3235 name, GetSignatureForError (), assembly);
3239 public override string GetSignatureForError ()
3241 return ns.GetSignatureForError ();
3244 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
3246 return ns.LookupTypeOrNamespace (ctx, name, arity, mode, loc);
3249 public override string ToString ()
3251 return Namespace.Name;
3256 /// This class denotes an expression which evaluates to a member
3257 /// of a struct or a class.
3259 public abstract class MemberExpr : Expression, OverloadResolver.IInstanceQualifier
3261 protected bool conditional_access_receiver;
3264 // An instance expression associated with this member, if it's a
3265 // non-static member
3267 public Expression InstanceExpression;
3270 /// The name of this member.
3272 public abstract string Name {
3277 // When base.member is used
3279 public bool IsBase {
3280 get { return InstanceExpression is BaseThis; }
3284 /// Whether this is an instance member.
3286 public abstract bool IsInstance {
3291 /// Whether this is a static member.
3293 public abstract bool IsStatic {
3297 public abstract string KindName {
3301 public bool ConditionalAccess { get; set; }
3303 protected abstract TypeSpec DeclaringType {
3307 TypeSpec OverloadResolver.IInstanceQualifier.InstanceType {
3309 return InstanceExpression.Type;
3314 // Converts best base candidate for virtual method starting from QueriedBaseType
3316 protected MethodSpec CandidateToBaseOverride (ResolveContext rc, MethodSpec method)
3319 // Only when base.member is used and method is virtual
3325 // Overload resulution works on virtual or non-virtual members only (no overrides). That
3326 // means for base.member access we have to find the closest match after we found best candidate
3328 if ((method.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL | Modifiers.OVERRIDE)) != 0) {
3330 // The method could already be what we are looking for
3332 TypeSpec[] targs = null;
3333 if (method.DeclaringType != InstanceExpression.Type) {
3335 // Candidate can have inflated MVAR parameters and we need to find
3336 // base match for original definition not inflated parameter types
3338 var parameters = method.Parameters;
3339 if (method.Arity > 0) {
3340 parameters = ((IParametersMember) method.MemberDefinition).Parameters;
3341 var inflated = method.DeclaringType as InflatedTypeSpec;
3342 if (inflated != null) {
3343 parameters = parameters.Inflate (inflated.CreateLocalInflator (rc));
3347 var filter = new MemberFilter (method.Name, method.Arity, MemberKind.Method, parameters, null);
3348 var base_override = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as MethodSpec;
3349 if (base_override != null && base_override.DeclaringType != method.DeclaringType) {
3350 if (base_override.IsGeneric)
3351 targs = method.TypeArguments;
3353 method = base_override;
3358 // When base access is used inside anonymous method/iterator/etc we need to
3359 // get back to the context of original type. We do it by emiting proxy
3360 // method in original class and rewriting base call to this compiler
3361 // generated method call which does the actual base invocation. This may
3362 // introduce redundant storey but with `this' only but it's tricky to avoid
3363 // at this stage as we don't know what expressions follow base
3365 // TODO: It's needed only when the method with base call is moved to a storey
3367 if (rc.CurrentAnonymousMethod != null) {
3368 if (targs == null && method.IsGeneric) {
3369 targs = method.TypeArguments;
3370 method = method.GetGenericMethodDefinition ();
3373 if (method.Parameters.HasArglist)
3374 throw new NotImplementedException ("__arglist base call proxy");
3376 method = rc.CurrentMemberDefinition.Parent.PartialContainer.CreateHoistedBaseCallProxy (rc, method);
3378 // Ideally this should apply to any proxy rewrite but in the case of unary mutators on
3379 // get/set member expressions second call would fail to proxy because left expression
3380 // would be of 'this' and not 'base' because we share InstanceExpression for get/set
3381 // FIXME: The async check is another hack but will probably fail with mutators
3382 if (rc.CurrentType.IsStruct || rc.CurrentAnonymousMethod.Storey is AsyncTaskStorey)
3383 InstanceExpression = new This (loc).Resolve (rc);
3387 method = method.MakeGenericMethod (rc, targs);
3391 // Only base will allow this invocation to happen.
3393 if (method.IsAbstract) {
3394 rc.Report.SymbolRelatedToPreviousError (method);
3395 Error_CannotCallAbstractBase (rc, method.GetSignatureForError ());
3401 protected void CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3403 if (InstanceExpression == null)
3406 if ((member.Modifiers & Modifiers.PROTECTED) != 0 && !(InstanceExpression is This)) {
3407 if (!CheckProtectedMemberAccess (rc, member, InstanceExpression.Type)) {
3408 Error_ProtectedMemberAccess (rc, member, InstanceExpression.Type, loc);
3413 bool OverloadResolver.IInstanceQualifier.CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
3415 if (InstanceExpression == null)
3418 return InstanceExpression is This || CheckProtectedMemberAccess (rc, member, InstanceExpression.Type);
3421 public static bool CheckProtectedMemberAccess<T> (ResolveContext rc, T member, TypeSpec qualifier) where T : MemberSpec
3423 var ct = rc.CurrentType;
3424 if (ct == qualifier)
3427 if ((member.Modifiers & Modifiers.INTERNAL) != 0 && member.DeclaringType.MemberDefinition.IsInternalAsPublic (ct.MemberDefinition.DeclaringAssembly))
3430 qualifier = qualifier.GetDefinition ();
3431 if (ct != qualifier && !IsSameOrBaseQualifier (ct, qualifier)) {
3438 public override bool ContainsEmitWithAwait ()
3440 return InstanceExpression != null && InstanceExpression.ContainsEmitWithAwait ();
3443 public override bool HasConditionalAccess ()
3445 return ConditionalAccess || (InstanceExpression != null && InstanceExpression.HasConditionalAccess ());
3448 static bool IsSameOrBaseQualifier (TypeSpec type, TypeSpec qtype)
3451 type = type.GetDefinition ();
3453 if (type == qtype || TypeManager.IsFamilyAccessible (qtype, type))
3456 type = type.DeclaringType;
3457 } while (type != null);
3462 protected void DoBestMemberChecks<T> (ResolveContext rc, T member) where T : MemberSpec, IInterfaceMemberSpec
3464 if (InstanceExpression != null) {
3465 InstanceExpression = InstanceExpression.Resolve (rc);
3466 CheckProtectedMemberAccess (rc, member);
3469 if (member.MemberType.IsPointer) {
3470 if (rc.CurrentIterator != null) {
3471 UnsafeInsideIteratorError (rc, loc);
3472 } else if (!rc.IsUnsafe) {
3473 UnsafeError (rc, loc);
3477 var dep = member.GetMissingDependencies ();
3479 ImportedTypeDefinition.Error_MissingDependency (rc, dep, loc);
3482 member.CheckObsoleteness (rc, loc);
3484 if (!(member is FieldSpec))
3485 member.MemberDefinition.SetIsUsed ();
3488 protected virtual void Error_CannotCallAbstractBase (ResolveContext rc, string name)
3490 rc.Report.Error (205, loc, "Cannot call an abstract base member `{0}'", name);
3493 public static void Error_ProtectedMemberAccess (ResolveContext rc, MemberSpec member, TypeSpec qualifier, Location loc)
3495 rc.Report.SymbolRelatedToPreviousError (member);
3496 rc.Report.Error (1540, loc,
3497 "Cannot access protected member `{0}' via a qualifier of type `{1}'. The qualifier must be of type `{2}' or derived from it",
3498 member.GetSignatureForError (), qualifier.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3501 public override void FlowAnalysis (FlowAnalysisContext fc)
3503 if (InstanceExpression != null) {
3504 InstanceExpression.FlowAnalysis (fc);
3508 protected void ResolveConditionalAccessReceiver (ResolveContext rc)
3510 if (!rc.HasSet (ResolveContext.Options.DontSetConditionalAccessReceiver) && HasConditionalAccess ()) {
3511 conditional_access_receiver = true;
3515 public bool ResolveInstanceExpression (ResolveContext rc, Expression rhs)
3517 if (!ResolveInstanceExpressionCore (rc, rhs))
3521 // Check intermediate value modification which won't have any effect
3523 if (rhs != null && TypeSpec.IsValueType (InstanceExpression.Type)) {
3524 var fexpr = InstanceExpression as FieldExpr;
3525 if (fexpr != null) {
3526 if (!fexpr.Spec.IsReadOnly || rc.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
3529 if (fexpr.IsStatic) {
3530 rc.Report.Error (1650, loc, "Fields of static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
3531 fexpr.GetSignatureForError ());
3533 rc.Report.Error (1648, loc, "Members of readonly field `{0}' cannot be modified (except in a constructor or a variable initializer)",
3534 fexpr.GetSignatureForError ());
3540 if (InstanceExpression is PropertyExpr || InstanceExpression is IndexerExpr || InstanceExpression is Invocation) {
3541 if (rc.CurrentInitializerVariable != null) {
3542 rc.Report.Error (1918, loc, "Members of value type `{0}' cannot be assigned using a property `{1}' object initializer",
3543 InstanceExpression.Type.GetSignatureForError (), InstanceExpression.GetSignatureForError ());
3545 rc.Report.Error (1612, loc,
3546 "Cannot modify a value type return value of `{0}'. Consider storing the value in a temporary variable",
3547 InstanceExpression.GetSignatureForError ());
3553 var lvr = InstanceExpression as LocalVariableReference;
3556 if (!lvr.local_info.IsReadonly)
3559 rc.Report.Error (1654, loc, "Cannot assign to members of `{0}' because it is a `{1}'",
3560 InstanceExpression.GetSignatureForError (), lvr.local_info.GetReadOnlyContext ());
3567 bool ResolveInstanceExpressionCore (ResolveContext rc, Expression rhs)
3570 if (InstanceExpression != null) {
3571 if (InstanceExpression is TypeExpr) {
3572 var t = InstanceExpression.Type;
3574 t.CheckObsoleteness (rc, loc);
3576 t = t.DeclaringType;
3577 } while (t != null);
3579 var runtime_expr = InstanceExpression as RuntimeValueExpression;
3580 if (runtime_expr == null || !runtime_expr.IsSuggestionOnly) {
3581 rc.Report.Error (176, loc,
3582 "Static member `{0}' cannot be accessed with an instance reference, qualify it with a type name instead",
3583 GetSignatureForError ());
3587 InstanceExpression = null;
3593 if (InstanceExpression == null || InstanceExpression is TypeExpr) {
3594 if (InstanceExpression != null || !This.IsThisAvailable (rc, true)) {
3595 if (rc.HasSet (ResolveContext.Options.FieldInitializerScope)) {
3596 rc.Report.Error (236, loc,
3597 "A field initializer cannot reference the nonstatic field, method, or property `{0}'",
3598 GetSignatureForError ());
3600 var fe = this as FieldExpr;
3601 if (fe != null && fe.Spec.MemberDefinition is PrimaryConstructorField) {
3602 if (rc.HasSet (ResolveContext.Options.BaseInitializer)) {
3603 rc.Report.Error (9005, loc, "Constructor initializer cannot access primary constructor parameters");
3605 rc.Report.Error (9006, loc, "An object reference is required to access primary constructor parameter `{0}'",
3609 rc.Report.Error (120, loc,
3610 "An object reference is required to access non-static member `{0}'",
3611 GetSignatureForError ());
3615 InstanceExpression = new CompilerGeneratedThis (rc.CurrentType, loc).Resolve (rc);
3619 if (!TypeManager.IsFamilyAccessible (rc.CurrentType, DeclaringType)) {
3620 rc.Report.Error (38, loc,
3621 "Cannot access a nonstatic member of outer type `{0}' via nested type `{1}'",
3622 DeclaringType.GetSignatureForError (), rc.CurrentType.GetSignatureForError ());
3625 InstanceExpression = new This (loc).Resolve (rc);
3629 var me = InstanceExpression as MemberExpr;
3631 me.ResolveInstanceExpressionCore (rc, rhs);
3633 var fe = me as FieldExpr;
3634 if (fe != null && fe.IsMarshalByRefAccess (rc)) {
3635 rc.Report.SymbolRelatedToPreviousError (me.DeclaringType);
3636 rc.Report.Warning (1690, 1, loc,
3637 "Cannot call methods, properties, or indexers on `{0}' because it is a value type member of a marshal-by-reference class",
3638 me.GetSignatureForError ());
3645 // Additional checks for l-value member access
3648 if (InstanceExpression is UnboxCast) {
3649 rc.Report.Error (445, InstanceExpression.Location, "Cannot modify the result of an unboxing conversion");
3656 public virtual MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
3658 if (left != null && !ConditionalAccess && !ec.HasSet (ResolveContext.Options.NameOfScope) && left.IsNull && TypeSpec.IsReferenceType (left.Type)) {
3659 ec.Report.Warning (1720, 1, left.Location,
3660 "Expression will always cause a `{0}'", "System.NullReferenceException");
3663 InstanceExpression = left;
3667 public virtual void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
3672 protected void EmitInstance (EmitContext ec, bool prepare_for_load)
3674 var inst = new InstanceEmitter (InstanceExpression, TypeSpec.IsValueType (InstanceExpression.Type));
3675 inst.Emit (ec, ConditionalAccess);
3677 if (prepare_for_load)
3678 ec.Emit (OpCodes.Dup);
3681 public abstract void SetTypeArguments (ResolveContext ec, TypeArguments ta);
3684 public class ExtensionMethodCandidates
3686 readonly NamespaceContainer container;
3687 readonly IList<MethodSpec> methods;
3689 readonly IMemberContext context;
3691 public ExtensionMethodCandidates (IMemberContext context, IList<MethodSpec> methods, NamespaceContainer nsContainer, int lookupIndex)
3693 this.context = context;
3694 this.methods = methods;
3695 this.container = nsContainer;
3696 this.index = lookupIndex;
3699 public NamespaceContainer Container {
3705 public IMemberContext Context {
3711 public int LookupIndex {
3717 public IList<MethodSpec> Methods {
3725 // Represents a group of extension method candidates for whole namespace
3727 class ExtensionMethodGroupExpr : MethodGroupExpr, OverloadResolver.IErrorHandler
3729 ExtensionMethodCandidates candidates;
3730 public Expression ExtensionExpression;
3732 public ExtensionMethodGroupExpr (ExtensionMethodCandidates candidates, Expression extensionExpr, Location loc)
3733 : base (candidates.Methods.Cast<MemberSpec>().ToList (), extensionExpr.Type, loc)
3735 this.candidates = candidates;
3736 this.ExtensionExpression = extensionExpr;
3739 public override bool IsStatic {
3740 get { return true; }
3744 // For extension methodgroup we are not looking for base members but parent
3745 // namespace extension methods
3747 public override IList<MemberSpec> GetBaseMembers (TypeSpec type)
3749 // TODO: candidates are null only when doing error reporting, that's
3750 // incorrect. We have to discover same extension methods in error mode
3751 if (candidates == null)
3754 int arity = type_arguments == null ? 0 : type_arguments.Count;
3756 candidates = candidates.Container.LookupExtensionMethod (candidates.Context, Name, arity, candidates.LookupIndex);
3757 if (candidates == null)
3760 return candidates.Methods.Cast<MemberSpec> ().ToList ();
3763 public static bool IsExtensionTypeCompatible (TypeSpec argType, TypeSpec extensionType)
3766 // Indentity, implicit reference or boxing conversion must exist for the extension parameter
3768 // LAMESPEC: or implicit type parameter conversion
3770 return argType == extensionType ||
3771 TypeSpecComparer.IsEqual (argType, extensionType) ||
3772 Convert.ImplicitReferenceConversionExists (argType, extensionType, false) ||
3773 Convert.ImplicitBoxingConversion (null, argType, extensionType) != null;
3776 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
3778 rc.Report.Error (8093, expr.Location, "An argument to nameof operator cannot be extension method group");
3781 public override MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
3783 // We are already here
3787 public override MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments arguments, OverloadResolver.IErrorHandler ehandler, OverloadResolver.Restrictions restr)
3789 if (arguments == null)
3790 arguments = new Arguments (1);
3792 ExtensionExpression = ExtensionExpression.Resolve (ec);
3793 if (ExtensionExpression == null)
3796 var cand = candidates;
3797 var atype = ConditionalAccess ? Argument.AType.ExtensionTypeConditionalAccess : Argument.AType.ExtensionType;
3798 arguments.Insert (0, new Argument (ExtensionExpression, atype));
3799 var res = base.OverloadResolve (ec, ref arguments, ehandler ?? this, restr);
3801 // Restore candidates in case we are running in probing mode
3804 // Store resolved argument and restore original arguments
3806 // Clean-up modified arguments for error reporting
3807 arguments.RemoveAt (0);
3811 var me = ExtensionExpression as MemberExpr;
3813 me.ResolveInstanceExpression (ec, null);
3814 var fe = me as FieldExpr;
3816 fe.Spec.MemberDefinition.SetIsUsed ();
3819 InstanceExpression = null;
3823 #region IErrorHandler Members
3825 bool OverloadResolver.IErrorHandler.AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous)
3830 bool OverloadResolver.IErrorHandler.ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument arg, int index)
3832 rc.Report.SymbolRelatedToPreviousError (best);
3835 rc.Report.Error (1929, loc,
3836 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' requires an instance of type `{3}'",
3837 queried_type.GetSignatureForError (), Name, best.GetSignatureForError (), ((MethodSpec)best).Parameters.ExtensionMethodType.GetSignatureForError ());
3839 rc.Report.Error (1928, loc,
3840 "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments",
3841 queried_type.GetSignatureForError (), Name, best.GetSignatureForError ());
3847 bool OverloadResolver.IErrorHandler.NoArgumentMatch (ResolveContext rc, MemberSpec best)
3852 bool OverloadResolver.IErrorHandler.TypeInferenceFailed (ResolveContext rc, MemberSpec best)
3861 /// MethodGroupExpr represents a group of method candidates which
3862 /// can be resolved to the best method overload
3864 public class MethodGroupExpr : MemberExpr, OverloadResolver.IBaseMembersProvider
3866 static readonly MemberSpec[] Excluded = new MemberSpec[0];
3868 protected IList<MemberSpec> Methods;
3869 MethodSpec best_candidate;
3870 TypeSpec best_candidate_return;
3871 protected TypeArguments type_arguments;
3873 SimpleName simple_name;
3874 protected TypeSpec queried_type;
3876 public MethodGroupExpr (IList<MemberSpec> mi, TypeSpec type, Location loc)
3880 this.type = InternalType.MethodGroup;
3882 eclass = ExprClass.MethodGroup;
3883 queried_type = type;
3886 public MethodGroupExpr (MethodSpec m, TypeSpec type, Location loc)
3887 : this (new MemberSpec[] { m }, type, loc)
3893 public MethodSpec BestCandidate {
3895 return best_candidate;
3899 public TypeSpec BestCandidateReturnType {
3901 return best_candidate_return;
3905 public IList<MemberSpec> Candidates {
3911 protected override TypeSpec DeclaringType {
3913 return queried_type;
3917 public bool IsConditionallyExcluded {
3919 return Methods == Excluded;
3923 public override bool IsInstance {
3925 if (best_candidate != null)
3926 return !best_candidate.IsStatic;
3932 public override bool IsSideEffectFree {
3934 return InstanceExpression == null || InstanceExpression.IsSideEffectFree;
3938 public override bool IsStatic {
3940 if (best_candidate != null)
3941 return best_candidate.IsStatic;
3947 public override string KindName {
3948 get { return "method"; }
3951 public override string Name {
3953 if (best_candidate != null)
3954 return best_candidate.Name;
3957 return Methods.First ().Name;
3964 // When best candidate is already know this factory can be used
3965 // to avoid expensive overload resolution to be called
3967 // NOTE: InstanceExpression has to be set manually
3969 public static MethodGroupExpr CreatePredefined (MethodSpec best, TypeSpec queriedType, Location loc)
3971 return new MethodGroupExpr (best, queriedType, loc) {
3972 best_candidate = best,
3973 best_candidate_return = best.ReturnType
3977 public override string GetSignatureForError ()
3979 if (best_candidate != null)
3980 return best_candidate.GetSignatureForError ();
3982 return Methods.First ().GetSignatureForError ();
3985 public override Expression CreateExpressionTree (ResolveContext ec)
3987 if (best_candidate == null) {
3988 ec.Report.Error (1953, loc, "An expression tree cannot contain an expression with method group");
3992 if (IsConditionallyExcluded)
3993 ec.Report.Error (765, loc,
3994 "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree");
3996 if (ConditionalAccess)
3997 Error_NullShortCircuitInsideExpressionTree (ec);
3999 return new TypeOfMethod (best_candidate, loc);
4002 protected override Expression DoResolve (ResolveContext ec)
4004 this.eclass = ExprClass.MethodGroup;
4006 if (InstanceExpression != null) {
4007 InstanceExpression = InstanceExpression.Resolve (ec);
4008 if (InstanceExpression == null)
4015 public override void Emit (EmitContext ec)
4017 throw new NotSupportedException ();
4020 public void EmitCall (EmitContext ec, Arguments arguments, bool statement)
4022 var call = new CallEmitter ();
4023 call.InstanceExpression = InstanceExpression;
4024 call.ConditionalAccess = ConditionalAccess;
4027 call.EmitStatement (ec, best_candidate, arguments, loc);
4029 call.Emit (ec, best_candidate, arguments, loc);
4032 public void EmitCall (EmitContext ec, Arguments arguments, TypeSpec conditionalAccessReceiver, bool statement)
4034 var ca = ec.ConditionalAccess;
4035 ec.ConditionalAccess = new ConditionalAccessContext (conditionalAccessReceiver, ec.DefineLabel ()) {
4036 Statement = statement
4039 EmitCall (ec, arguments, statement);
4041 ec.CloseConditionalAccess (!statement && best_candidate_return != conditionalAccessReceiver && conditionalAccessReceiver.IsNullableType ? conditionalAccessReceiver : null);
4042 ec.ConditionalAccess = ca;
4045 public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
4047 if (target != InternalType.ErrorType) {
4048 ec.Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
4049 Name, target.GetSignatureForError ());
4053 public bool HasAccessibleCandidate (ResolveContext rc)
4055 foreach (var candidate in Candidates) {
4056 if (candidate.IsAccessible (rc))
4063 public static bool IsExtensionMethodArgument (Expression expr)
4066 // LAMESPEC: No details about which expressions are not allowed
4068 return !(expr is TypeExpr) && !(expr is BaseThis);
4072 /// Find the Applicable Function Members (7.4.2.1)
4074 /// me: Method Group expression with the members to select.
4075 /// it might contain constructors or methods (or anything
4076 /// that maps to a method).
4078 /// Arguments: ArrayList containing resolved Argument objects.
4080 /// loc: The location if we want an error to be reported, or a Null
4081 /// location for "probing" purposes.
4083 /// Returns: The MethodBase (either a ConstructorInfo or a MethodInfo)
4084 /// that is the best match of me on Arguments.
4087 public virtual MethodGroupExpr OverloadResolve (ResolveContext ec, ref Arguments args, OverloadResolver.IErrorHandler cerrors, OverloadResolver.Restrictions restr)
4089 // TODO: causes issues with probing mode, remove explicit Kind check
4090 if (best_candidate != null && best_candidate.Kind == MemberKind.Destructor)
4093 var r = new OverloadResolver (Methods, type_arguments, restr, loc);
4094 if ((restr & OverloadResolver.Restrictions.NoBaseMembers) == 0) {
4095 r.BaseMembersProvider = this;
4096 r.InstanceQualifier = this;
4099 if (cerrors != null)
4100 r.CustomErrors = cerrors;
4102 // TODO: When in probing mode do IsApplicable only and when called again do VerifyArguments for full error reporting
4103 best_candidate = r.ResolveMember<MethodSpec> (ec, ref args);
4104 if (best_candidate == null) {
4105 if (!r.BestCandidateIsDynamic)
4108 if (simple_name != null && ec.IsStatic)
4109 InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4114 // Overload resolver had to create a new method group, all checks bellow have already been executed
4115 if (r.BestCandidateNewMethodGroup != null)
4116 return r.BestCandidateNewMethodGroup;
4118 if (best_candidate.Kind == MemberKind.Method && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0) {
4119 if (InstanceExpression != null) {
4120 if (best_candidate.IsExtensionMethod && args[0].Expr == InstanceExpression) {
4121 InstanceExpression = null;
4123 if (simple_name != null && best_candidate.IsStatic) {
4124 InstanceExpression = ProbeIdenticalTypeName (ec, InstanceExpression, simple_name);
4127 InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup | ResolveFlags.Type);
4131 ResolveInstanceExpression (ec, null);
4134 var base_override = CandidateToBaseOverride (ec, best_candidate);
4135 if (base_override == best_candidate) {
4136 best_candidate_return = r.BestCandidateReturnType;
4138 best_candidate = base_override;
4139 best_candidate_return = best_candidate.ReturnType;
4142 if (best_candidate.IsGeneric && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0 && TypeParameterSpec.HasAnyTypeParameterConstrained (best_candidate.GenericDefinition)) {
4143 ConstraintChecker cc = new ConstraintChecker (ec);
4144 cc.CheckAll (best_candidate.GetGenericMethodDefinition (), best_candidate.TypeArguments, best_candidate.Constraints, loc);
4148 // Additional check for possible imported base override method which
4149 // could not be done during IsOverrideMethodBaseTypeAccessible
4151 if (best_candidate.IsVirtual && (best_candidate.DeclaringType.Modifiers & Modifiers.PROTECTED) != 0 &&
4152 best_candidate.MemberDefinition.IsImported && !best_candidate.DeclaringType.IsAccessible (ec)) {
4153 ec.Report.SymbolRelatedToPreviousError (best_candidate);
4154 ErrorIsInaccesible (ec, best_candidate.GetSignatureForError (), loc);
4157 // Speed up the check by not doing it on disallowed targets
4158 if (best_candidate_return.Kind == MemberKind.Void && best_candidate.IsConditionallyExcluded (ec))
4164 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
4166 var fe = left as FieldExpr;
4169 // Using method-group on struct fields makes the struct assigned. I am not sure
4170 // why but that's what .net does
4172 fe.Spec.MemberDefinition.SetIsAssigned ();
4175 simple_name = original;
4176 return base.ResolveMemberAccess (ec, left, original);
4179 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
4181 if (!HasAccessibleCandidate (rc)) {
4182 ErrorIsInaccesible (rc, expr.GetSignatureForError (), loc);
4185 if (expr.HasTypeArguments) {
4186 rc.Report.Error (8084, expr.Location, "An argument to nameof operator cannot be method group with type arguments");
4190 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
4192 type_arguments = ta;
4195 #region IBaseMembersProvider Members
4197 public virtual IList<MemberSpec> GetBaseMembers (TypeSpec type)
4199 var baseType = type.BaseType;
4201 IList<MemberSpec> members = baseType == null ? null : MemberCache.FindMembers (baseType, Methods [0].Name, false);
4203 if (members == null && !type.IsInterface) {
4204 var tps = queried_type as TypeParameterSpec;
4206 members = MemberCache.FindInterfaceMembers (tps, Methods [0].Name);
4212 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4214 if (queried_type == member.DeclaringType)
4217 return MemberCache.FindMember (queried_type, new MemberFilter ((MethodSpec) member),
4218 BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as IParametersMember;
4222 // Extension methods lookup after ordinary methods candidates failed to apply
4224 public virtual MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4226 if (InstanceExpression == null || InstanceExpression.eclass == ExprClass.Type)
4229 if (!IsExtensionMethodArgument (InstanceExpression))
4232 int arity = type_arguments == null ? 0 : type_arguments.Count;
4233 var methods = rc.LookupExtensionMethod (Methods[0].Name, arity);
4234 if (methods == null)
4237 var emg = new ExtensionMethodGroupExpr (methods, InstanceExpression, loc);
4238 emg.SetTypeArguments (rc, type_arguments);
4239 emg.ConditionalAccess = ConditionalAccess;
4246 struct ConstructorInstanceQualifier : OverloadResolver.IInstanceQualifier
4248 public ConstructorInstanceQualifier (TypeSpec type)
4251 InstanceType = type;
4254 public TypeSpec InstanceType { get; private set; }
4256 public bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member)
4258 return MemberExpr.CheckProtectedMemberAccess (rc, member, InstanceType);
4262 public struct OverloadResolver
4265 public enum Restrictions
4269 ProbingOnly = 1 << 1,
4270 CovariantDelegate = 1 << 2,
4271 NoBaseMembers = 1 << 3,
4272 BaseMembersIncluded = 1 << 4,
4273 GetEnumeratorLookup = 1 << 5
4276 public interface IBaseMembersProvider
4278 IList<MemberSpec> GetBaseMembers (TypeSpec baseType);
4279 IParametersMember GetOverrideMemberParameters (MemberSpec member);
4280 MethodGroupExpr LookupExtensionMethod (ResolveContext rc);
4283 public interface IErrorHandler
4285 bool AmbiguousCandidates (ResolveContext rc, MemberSpec best, MemberSpec ambiguous);
4286 bool ArgumentMismatch (ResolveContext rc, MemberSpec best, Argument a, int index);
4287 bool NoArgumentMatch (ResolveContext rc, MemberSpec best);
4288 bool TypeInferenceFailed (ResolveContext rc, MemberSpec best);
4291 public interface IInstanceQualifier
4293 TypeSpec InstanceType { get; }
4294 bool CheckProtectedMemberAccess (ResolveContext rc, MemberSpec member);
4297 sealed class NoBaseMembers : IBaseMembersProvider
4299 public static readonly IBaseMembersProvider Instance = new NoBaseMembers ();
4301 public IList<MemberSpec> GetBaseMembers (TypeSpec baseType)
4306 public IParametersMember GetOverrideMemberParameters (MemberSpec member)
4311 public MethodGroupExpr LookupExtensionMethod (ResolveContext rc)
4317 struct AmbiguousCandidate
4319 public readonly MemberSpec Member;
4320 public readonly bool Expanded;
4321 public readonly AParametersCollection Parameters;
4323 public AmbiguousCandidate (MemberSpec member, AParametersCollection parameters, bool expanded)
4326 Parameters = parameters;
4327 Expanded = expanded;
4332 IList<MemberSpec> members;
4333 TypeArguments type_arguments;
4334 IBaseMembersProvider base_provider;
4335 IErrorHandler custom_errors;
4336 IInstanceQualifier instance_qualifier;
4337 Restrictions restrictions;
4338 MethodGroupExpr best_candidate_extension_group;
4339 TypeSpec best_candidate_return_type;
4341 SessionReportPrinter lambda_conv_msgs;
4343 public OverloadResolver (IList<MemberSpec> members, Restrictions restrictions, Location loc)
4344 : this (members, null, restrictions, loc)
4348 public OverloadResolver (IList<MemberSpec> members, TypeArguments targs, Restrictions restrictions, Location loc)
4351 if (members == null || members.Count == 0)
4352 throw new ArgumentException ("empty members set");
4354 this.members = members;
4356 type_arguments = targs;
4357 this.restrictions = restrictions;
4358 if (IsDelegateInvoke)
4359 this.restrictions |= Restrictions.NoBaseMembers;
4361 base_provider = NoBaseMembers.Instance;
4366 public IBaseMembersProvider BaseMembersProvider {
4368 return base_provider;
4371 base_provider = value;
4375 public bool BestCandidateIsDynamic { get; set; }
4378 // Best candidate was found in newly created MethodGroupExpr, used by extension methods
4380 public MethodGroupExpr BestCandidateNewMethodGroup {
4382 return best_candidate_extension_group;
4387 // Return type can be different between best candidate and closest override
4389 public TypeSpec BestCandidateReturnType {
4391 return best_candidate_return_type;
4395 public IErrorHandler CustomErrors {
4397 return custom_errors;
4400 custom_errors = value;
4404 TypeSpec DelegateType {
4406 if ((restrictions & Restrictions.DelegateInvoke) == 0)
4407 throw new InternalErrorException ("Not running in delegate mode", loc);
4409 return members [0].DeclaringType;
4413 public IInstanceQualifier InstanceQualifier {
4415 return instance_qualifier;
4418 instance_qualifier = value;
4422 bool IsProbingOnly {
4424 return (restrictions & Restrictions.ProbingOnly) != 0;
4428 bool IsDelegateInvoke {
4430 return (restrictions & Restrictions.DelegateInvoke) != 0;
4437 // 7.4.3.3 Better conversion from expression
4438 // Returns : 1 if a->p is better,
4439 // 2 if a->q is better,
4440 // 0 if neither is better
4442 static int BetterExpressionConversion (ResolveContext ec, Argument a, TypeSpec p, TypeSpec q)
4444 TypeSpec argument_type = a.Type;
4447 // Exactly matching Expression phase
4451 // If argument is an anonymous function
4453 if (argument_type == InternalType.AnonymousMethod && ec.Module.Compiler.Settings.Version > LanguageVersion.ISO_2) {
4455 // p and q are delegate types or expression tree types
4457 if (p.IsExpressionTreeType || q.IsExpressionTreeType) {
4458 if (q.MemberDefinition != p.MemberDefinition) {
4463 // Uwrap delegate from Expression<T>
4465 q = TypeManager.GetTypeArguments (q) [0];
4466 p = TypeManager.GetTypeArguments (p) [0];
4469 var p_m = Delegate.GetInvokeMethod (p);
4470 var q_m = Delegate.GetInvokeMethod (q);
4473 // With identical parameter lists
4475 if (!TypeSpecComparer.Equals (p_m.Parameters.Types, q_m.Parameters.Types))
4483 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
4485 if (p.Kind == MemberKind.Void) {
4486 return q.Kind != MemberKind.Void ? 2 : 0;
4490 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
4492 if (q.Kind == MemberKind.Void) {
4493 return p.Kind != MemberKind.Void ? 1 : 0;
4496 var am = (AnonymousMethodExpression)a.Expr;
4499 // When anonymous method is an asynchronous, and P has a return type Task<Y1>, and Q has a return type Task<Y2>
4500 // better conversion is performed between underlying types Y1 and Y2
4502 if (p.IsGenericTask || q.IsGenericTask) {
4503 if (am.Block.IsAsync && p.IsGenericTask && q.IsGenericTask) {
4504 q = q.TypeArguments [0];
4505 p = p.TypeArguments [0];
4511 // An inferred return type X exists for E in the context of the parameter list, and
4512 // an identity conversion exists from X to the return type of D
4514 var inferred_type = am.InferReturnType (ec, null, orig_q);
4515 if (inferred_type != null) {
4516 if (inferred_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
4517 inferred_type = ec.BuiltinTypes.Object;
4519 if (inferred_type == p)
4522 if (inferred_type == q)
4528 if (argument_type == p)
4531 if (argument_type == q)
4534 return IsBetterConversionTarget (ec, p, q);
4537 static int IsBetterConversionTarget (ResolveContext rc, TypeSpec p, TypeSpec q)
4539 if ((p.Kind == MemberKind.Delegate || p.IsExpressionTreeType) && (q.Kind == MemberKind.Delegate || q.IsExpressionTreeType)) {
4541 if (p.Kind != MemberKind.Delegate) {
4542 p = TypeManager.GetTypeArguments (p) [0];
4545 if (q.Kind != MemberKind.Delegate) {
4546 q = TypeManager.GetTypeArguments (q) [0];
4549 var p_m = Delegate.GetInvokeMethod (p);
4550 var q_m = Delegate.GetInvokeMethod (q);
4556 // if p is void returning, and q has a return type Y, then C2 is the better conversion.
4558 if (p.Kind == MemberKind.Void) {
4559 return q.Kind != MemberKind.Void ? 2 : 0;
4563 // if p has a return type Y, and q is void returning, then C1 is the better conversion.
4565 if (q.Kind == MemberKind.Void) {
4566 return p.Kind != MemberKind.Void ? 1 : 0;
4569 return IsBetterConversionTarget (rc, p, q);
4572 if (p.IsGenericTask && q.IsGenericTask) {
4573 q = q.TypeArguments [0];
4574 p = p.TypeArguments [0];
4575 return IsBetterConversionTarget (rc, p, q);
4579 if (p.IsNullableType) {
4580 p = Nullable.NullableInfo.GetUnderlyingType (p);
4581 if (!BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (p))
4582 return BetterTypeConversionImplicitConversion (rc, p_orig, q);
4585 // Spec expects implicit conversion check between p and q, q and p
4586 // to be done before nullable unwrapping but that's expensive operation.
4588 // Extra manual tweak is needed because BetterTypeConversion works on
4596 if (q.IsNullableType) {
4597 q = Nullable.NullableInfo.GetUnderlyingType (q);
4598 if (!BuiltinTypeSpec.IsPrimitiveTypeOrDecimal (q))
4599 return BetterTypeConversionImplicitConversion (rc, p_orig, q_orig);
4605 return BetterTypeConversion (rc, p, q);
4609 // 7.4.3.4 Better conversion from type
4611 public static int BetterTypeConversion (ResolveContext ec, TypeSpec p, TypeSpec q)
4613 if (p == null || q == null)
4614 throw new InternalErrorException ("BetterTypeConversion got a null conversion");
4616 switch (p.BuiltinType) {
4617 case BuiltinTypeSpec.Type.Int:
4618 if (q.BuiltinType == BuiltinTypeSpec.Type.UInt || q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4621 case BuiltinTypeSpec.Type.Long:
4622 if (q.BuiltinType == BuiltinTypeSpec.Type.ULong)
4625 case BuiltinTypeSpec.Type.SByte:
4626 switch (q.BuiltinType) {
4627 case BuiltinTypeSpec.Type.Byte:
4628 case BuiltinTypeSpec.Type.UShort:
4629 case BuiltinTypeSpec.Type.UInt:
4630 case BuiltinTypeSpec.Type.ULong:
4634 case BuiltinTypeSpec.Type.Short:
4635 switch (q.BuiltinType) {
4636 case BuiltinTypeSpec.Type.UShort:
4637 case BuiltinTypeSpec.Type.UInt:
4638 case BuiltinTypeSpec.Type.ULong:
4642 case BuiltinTypeSpec.Type.Dynamic:
4643 // LAMESPEC: Dynamic conversions is not considered
4644 p = ec.Module.Compiler.BuiltinTypes.Object;
4648 switch (q.BuiltinType) {
4649 case BuiltinTypeSpec.Type.Int:
4650 if (p.BuiltinType == BuiltinTypeSpec.Type.UInt || p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4653 case BuiltinTypeSpec.Type.Long:
4654 if (p.BuiltinType == BuiltinTypeSpec.Type.ULong)
4657 case BuiltinTypeSpec.Type.SByte:
4658 switch (p.BuiltinType) {
4659 case BuiltinTypeSpec.Type.Byte:
4660 case BuiltinTypeSpec.Type.UShort:
4661 case BuiltinTypeSpec.Type.UInt:
4662 case BuiltinTypeSpec.Type.ULong:
4666 case BuiltinTypeSpec.Type.Short:
4667 switch (p.BuiltinType) {
4668 case BuiltinTypeSpec.Type.UShort:
4669 case BuiltinTypeSpec.Type.UInt:
4670 case BuiltinTypeSpec.Type.ULong:
4674 case BuiltinTypeSpec.Type.Dynamic:
4675 // LAMESPEC: Dynamic conversions is not considered
4676 q = ec.Module.Compiler.BuiltinTypes.Object;
4680 return BetterTypeConversionImplicitConversion (ec, p, q);
4683 static int BetterTypeConversionImplicitConversion (ResolveContext rc, TypeSpec p, TypeSpec q)
4685 // TODO: this is expensive
4686 Expression p_tmp = new EmptyExpression (p);
4687 Expression q_tmp = new EmptyExpression (q);
4689 bool p_to_q = Convert.ImplicitConversionExists (rc, p_tmp, q);
4690 bool q_to_p = Convert.ImplicitConversionExists (rc, q_tmp, p);
4692 if (p_to_q && !q_to_p)
4695 if (q_to_p && !p_to_q)
4702 /// Determines "Better function" between candidate
4703 /// and the current best match
4706 /// Returns a boolean indicating :
4707 /// false if candidate ain't better
4708 /// true if candidate is better than the current best match
4710 bool BetterFunction (ResolveContext ec, Arguments args, MemberSpec candidate, AParametersCollection cparam, bool candidate_params,
4711 MemberSpec best, AParametersCollection bparam, bool best_params)
4713 AParametersCollection candidate_pd = ((IParametersMember) candidate).Parameters;
4714 AParametersCollection best_pd = ((IParametersMember) best).Parameters;
4716 int candidate_better_count = 0;
4717 int best_better_count = 0;
4719 bool are_equivalent = true;
4720 int args_count = args == null ? 0 : args.Count;
4724 for (int c_idx = 0, b_idx = 0; j < args_count; ++j, ++c_idx, ++b_idx) {
4727 // Default arguments are ignored for better decision
4728 if (a.IsDefaultArgument)
4732 // When comparing named argument the parameter type index has to be looked up
4733 // in original parameter set (override version for virtual members)
4735 NamedArgument na = a as NamedArgument;
4737 int idx = cparam.GetParameterIndexByName (na.Name);
4738 ct = candidate_pd.Types[idx];
4739 if (candidate_params && candidate_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4740 ct = TypeManager.GetElementType (ct);
4742 idx = bparam.GetParameterIndexByName (na.Name);
4743 bt = best_pd.Types[idx];
4744 if (best_params && best_pd.FixedParameters[idx].ModFlags == Parameter.Modifier.PARAMS)
4745 bt = TypeManager.GetElementType (bt);
4747 ct = candidate_pd.Types[c_idx];
4748 bt = best_pd.Types[b_idx];
4750 if (candidate_params && candidate_pd.FixedParameters[c_idx].ModFlags == Parameter.Modifier.PARAMS) {
4751 ct = TypeManager.GetElementType (ct);
4755 if (best_params && best_pd.FixedParameters[b_idx].ModFlags == Parameter.Modifier.PARAMS) {
4756 bt = TypeManager.GetElementType (bt);
4761 if (TypeSpecComparer.IsEqual (ct, bt))
4764 are_equivalent = false;
4765 int result = BetterExpressionConversion (ec, a, ct, bt);
4767 // for each argument, the conversion to 'ct' should be no worse than
4768 // the conversion to 'bt'.
4771 // No optional parameters tie breaking rules for delegates overload resolution
4773 if ((restrictions & Restrictions.CovariantDelegate) != 0)
4776 ++best_better_count;
4780 // for at least one argument, the conversion to 'ct' should be better than
4781 // the conversion to 'bt'.
4783 ++candidate_better_count;
4786 if (candidate_better_count != 0 && best_better_count == 0)
4789 if (best_better_count > 0 && candidate_better_count == 0)
4793 // LAMESPEC: Tie-breaking rules for not equivalent parameter types
4795 if (!are_equivalent) {
4796 while (j < args_count && !args [j++].IsDefaultArgument) ;
4799 // A candidate with no default parameters is still better when there
4800 // is no better expression conversion and does not have more parameters
4802 if (candidate_pd.Count < best_pd.Count) {
4803 if (candidate_params)
4806 if (!candidate_pd.FixedParameters [j - 1].HasDefaultValue)
4809 if (best_pd.FixedParameters [j].HasDefaultValue)
4812 } else if (candidate_pd.Count == best_pd.Count) {
4813 if (candidate_params)
4816 if (!candidate_pd.FixedParameters [j - 1].HasDefaultValue && best_pd.FixedParameters [j - 1].HasDefaultValue)
4819 if (candidate_pd.FixedParameters [j - 1].HasDefaultValue && best_pd.HasParams)
4827 // If candidate is applicable in its normal form and best has a params array and is applicable
4828 // only in its expanded form, then candidate is better
4830 if (candidate_params != best_params)
4831 return !candidate_params;
4834 // We have not reached end of parameters list due to params or used default parameters
4836 bool defaults_ambiguity = false;
4837 while (j < candidate_pd.Count && j < best_pd.Count) {
4838 var cand_param = candidate_pd.FixedParameters [j];
4839 var best_param = best_pd.FixedParameters [j];
4841 if (cand_param.HasDefaultValue != best_param.HasDefaultValue && (!candidate_pd.HasParams || !best_pd.HasParams))
4842 return cand_param.HasDefaultValue;
4844 defaults_ambiguity = true;
4845 if (candidate_pd.Count == best_pd.Count) {
4849 // void Foo (int i = 0) is better than void Foo (params int[]) for Foo ()
4850 // void Foo (string[] s, string value = null) is better than Foo (string s, params string[]) for Foo (null) or Foo ()
4852 if (cand_param.HasDefaultValue) {
4861 // Neither is better when not all arguments are provided
4863 // void Foo (string s, int i = 0) <-> Foo (string s, int i = 0, int i2 = 0)
4864 // void Foo (string s, int i = 0) <-> Foo (string s, byte i = 0)
4865 // void Foo (string s, params int[]) <-> Foo (string s, params byte[])
4870 if (candidate_pd.Count != best_pd.Count) {
4871 if (defaults_ambiguity && best_pd.Count - 1 == j)
4872 return best_pd.HasParams;
4874 return candidate_pd.Count < best_pd.Count;
4878 // One is a non-generic method and second is a generic method, then non-generic is better
4880 if (best.IsGeneric != candidate.IsGeneric)
4881 return best.IsGeneric;
4884 // Both methods have the same number of parameters, and the parameters have equal types
4885 // Pick the "more specific" signature using rules over original (non-inflated) types
4887 var candidate_def_pd = ((IParametersMember) candidate.MemberDefinition).Parameters;
4888 var best_def_pd = ((IParametersMember) best.MemberDefinition).Parameters;
4890 bool specific_at_least_once = false;
4891 for (j = 0; j < args_count; ++j) {
4892 NamedArgument na = args_count == 0 ? null : args [j] as NamedArgument;
4894 ct = candidate_def_pd.Types[cparam.GetParameterIndexByName (na.Name)];
4895 bt = best_def_pd.Types[bparam.GetParameterIndexByName (na.Name)];
4897 ct = candidate_def_pd.Types[j];
4898 bt = best_def_pd.Types[j];
4903 TypeSpec specific = MoreSpecific (ct, bt);
4907 specific_at_least_once = true;
4910 if (specific_at_least_once)
4916 static bool CheckInflatedArguments (MethodSpec ms)
4918 if (!TypeParameterSpec.HasAnyTypeParameterTypeConstrained (ms.GenericDefinition))
4921 // Setup constraint checker for probing only
4922 ConstraintChecker cc = new ConstraintChecker (null);
4924 var mp = ms.Parameters.Types;
4925 for (int i = 0; i < mp.Length; ++i) {
4926 var type = mp[i] as InflatedTypeSpec;
4930 var targs = type.TypeArguments;
4931 if (targs.Length == 0)
4934 // TODO: Checking inflated MVAR arguments should be enough
4935 if (!cc.CheckAll (type.GetDefinition (), targs, type.Constraints, Location.Null))
4942 public static void Error_ConstructorMismatch (ResolveContext rc, TypeSpec type, int argCount, Location loc)
4944 rc.Report.Error (1729, loc,
4945 "The type `{0}' does not contain a constructor that takes `{1}' arguments",
4946 type.GetSignatureForError (), argCount.ToString ());
4950 // Determines if the candidate method is applicable to the given set of arguments
4951 // There could be two different set of parameters for same candidate where one
4952 // is the closest override for default values and named arguments checks and second
4953 // one being the virtual base for the parameter types and modifiers.
4955 // A return value rates candidate method compatibility,
4957 // 0 = the best, int.MaxValue = the worst
4959 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)
4962 // Each step has allocated 10 values, it can overflow for
4963 // more than 10 arguments but that's ok as it's used for
4964 // better error reporting only
4966 const int ArgumentCountMismatch = 1000000000;
4967 const int NamedArgumentsMismatch = 100000000;
4968 const int DefaultArgumentMismatch = 10000000;
4969 const int UnexpectedTypeArguments = 1000000;
4970 const int TypeArgumentsMismatch = 100000;
4971 const int InflatedTypesMismatch = 10000;
4973 // Parameters of most-derived type used mainly for named and optional parameters
4974 var pd = pm.Parameters;
4976 // Used for params modifier only, that's legacy of C# 1.0 which uses base type for
4977 // params modifier instead of most-derived type
4978 var cpd = ((IParametersMember) candidate).Parameters;
4979 int param_count = pd.Count;
4980 int optional_count = 0;
4982 Arguments orig_args = arguments;
4984 if (arg_count != param_count) {
4986 // No arguments expansion when doing exact match for delegates
4988 if ((restrictions & Restrictions.CovariantDelegate) == 0) {
4989 for (int i = 0; i < pd.Count; ++i) {
4990 if (pd.FixedParameters[i].HasDefaultValue) {
4991 optional_count = pd.Count - i;
4997 if (optional_count != 0) {
4998 // Readjust expected number when params used
4999 if (cpd.HasParams) {
5001 if (arg_count < param_count)
5003 } else if (arg_count > param_count) {
5004 int args_gap = System.Math.Abs (arg_count - param_count);
5005 return ArgumentCountMismatch + args_gap;
5006 } else if (arg_count < param_count - optional_count) {
5007 int args_gap = System.Math.Abs (param_count - optional_count - arg_count);
5008 return ArgumentCountMismatch + args_gap;
5010 } else if (arg_count != param_count) {
5011 int args_gap = System.Math.Abs (arg_count - param_count);
5013 return ArgumentCountMismatch + args_gap;
5014 if (arg_count < param_count - 1)
5015 return ArgumentCountMismatch + args_gap;
5018 // Resize to fit optional arguments
5019 if (optional_count != 0) {
5020 if (arguments == null) {
5021 arguments = new Arguments (optional_count);
5023 // Have to create a new container, so the next run can do same
5024 var resized = new Arguments (param_count);
5025 resized.AddRange (arguments);
5026 arguments = resized;
5029 for (int i = arg_count; i < param_count; ++i)
5030 arguments.Add (null);
5034 if (arg_count > 0) {
5036 // Shuffle named arguments to the right positions if there are any
5038 if (arguments[arg_count - 1] is NamedArgument) {
5039 arg_count = arguments.Count;
5041 for (int i = 0; i < arg_count; ++i) {
5042 bool arg_moved = false;
5044 NamedArgument na = arguments[i] as NamedArgument;
5048 int index = pd.GetParameterIndexByName (na.Name);
5050 // Named parameter not found
5052 return NamedArgumentsMismatch - i;
5054 // already reordered
5059 if (index >= param_count) {
5060 // When using parameters which should not be available to the user
5061 if ((cpd.FixedParameters[index].ModFlags & Parameter.Modifier.PARAMS) == 0)
5064 arguments.Add (null);
5068 if (index == arg_count)
5069 return NamedArgumentsMismatch - i - 1;
5071 temp = arguments [index];
5073 // The slot has been taken by positional argument
5074 if (temp != null && !(temp is NamedArgument))
5075 return NamedArgumentsMismatch - i - 1;
5079 arguments = arguments.MarkOrderedArgument (na);
5083 if (arguments == orig_args) {
5084 arguments = new Arguments (orig_args.Count);
5085 arguments.AddRange (orig_args);
5088 arguments[index] = arguments[i];
5089 arguments[i] = temp;
5096 arg_count = arguments.Count;
5098 } else if (arguments != null) {
5099 arg_count = arguments.Count;
5103 // Don't do any expensive checks when the candidate cannot succeed
5105 if (arg_count != param_count && !cpd.HasParams)
5106 return DefaultArgumentMismatch - System.Math.Abs (param_count - arg_count);
5108 var dep = candidate.GetMissingDependencies ();
5110 ImportedTypeDefinition.Error_MissingDependency (ec, dep, loc);
5115 // 1. Handle generic method using type arguments when specified or type inference
5118 var ms = candidate as MethodSpec;
5119 if (ms != null && ms.IsGeneric) {
5120 if (type_arguments != null) {
5121 var g_args_count = ms.Arity;
5122 if (g_args_count != type_arguments.Count)
5123 return TypeArgumentsMismatch - System.Math.Abs (type_arguments.Count - g_args_count);
5125 if (type_arguments.Arguments != null)
5126 ms = ms.MakeGenericMethod (ec, type_arguments.Arguments);
5129 // Deploy custom error reporting for infered anonymous expression or lambda methods. When
5130 // probing lambda methods keep all errors reported in separate set and once we are done and no best
5131 // candidate was found use the set to report more details about what was wrong with lambda body.
5132 // The general idea is to distinguish between code errors and errors caused by
5133 // trial-and-error type inference
5135 if (lambda_conv_msgs == null) {
5136 for (int i = 0; i < arg_count; i++) {
5137 Argument a = arguments[i];
5141 var am = a.Expr as AnonymousMethodExpression;
5143 if (lambda_conv_msgs == null)
5144 lambda_conv_msgs = new SessionReportPrinter ();
5146 am.TypeInferenceReportPrinter = lambda_conv_msgs;
5151 var ti = new TypeInference (arguments);
5152 TypeSpec[] i_args = ti.InferMethodArguments (ec, ms);
5155 return TypeArgumentsMismatch - ti.InferenceScore;
5158 // Clear any error messages when the result was success
5160 if (lambda_conv_msgs != null)
5161 lambda_conv_msgs.ClearSession ();
5163 if (i_args.Length != 0) {
5165 for (int i = 0; i < i_args.Length; ++i) {
5166 var ta = i_args [i];
5167 if (!ta.IsAccessible (ec))
5168 return TypeArgumentsMismatch - i;
5172 ms = ms.MakeGenericMethod (ec, i_args);
5177 // Type arguments constraints have to match for the method to be applicable
5179 if (!CheckInflatedArguments (ms)) {
5181 return InflatedTypesMismatch;
5185 // We have a generic return type and at same time the method is override which
5186 // means we have to also inflate override return type in case the candidate is
5187 // best candidate and override return type is different to base return type.
5189 // virtual Foo<T, object> with override Foo<T, dynamic>
5191 if (candidate != pm) {
5192 MethodSpec override_ms = (MethodSpec) pm;
5193 var inflator = new TypeParameterInflator (ec, ms.DeclaringType, override_ms.GenericDefinition.TypeParameters, ms.TypeArguments);
5194 returnType = inflator.Inflate (returnType);
5196 returnType = ms.ReturnType;
5203 if (type_arguments != null)
5204 return UnexpectedTypeArguments;
5210 // 2. Each argument has to be implicitly convertible to method parameter
5212 Parameter.Modifier p_mod = 0;
5215 for (int i = 0; i < arg_count; i++) {
5216 Argument a = arguments[i];
5218 var fp = pd.FixedParameters[i];
5219 if (!fp.HasDefaultValue) {
5220 arguments = orig_args;
5221 return arg_count * 2 + 2;
5225 // Get the default value expression, we can use the same expression
5226 // if the type matches
5228 Expression e = fp.DefaultValue;
5230 e = ResolveDefaultValueArgument (ec, ptypes[i], e, loc);
5232 // Restore for possible error reporting
5233 for (int ii = i; ii < arg_count; ++ii)
5234 arguments.RemoveAt (i);
5236 return (arg_count - i) * 2 + 1;
5240 if ((fp.ModFlags & Parameter.Modifier.CallerMask) != 0) {
5242 // LAMESPEC: Attributes can be mixed together with build-in priority
5244 if ((fp.ModFlags & Parameter.Modifier.CallerLineNumber) != 0) {
5245 e = new IntLiteral (ec.BuiltinTypes, loc.Row, loc);
5246 } else if ((fp.ModFlags & Parameter.Modifier.CallerFilePath) != 0) {
5247 e = new StringLiteral (ec.BuiltinTypes, loc.SourceFile.GetFullPathName (ec.Module.Compiler.Settings.PathMap), loc);
5248 } else if (ec.MemberContext.CurrentMemberDefinition != null) {
5249 e = new StringLiteral (ec.BuiltinTypes, ec.MemberContext.CurrentMemberDefinition.GetCallerMemberName (), loc);
5253 arguments[i] = new Argument (e, Argument.AType.Default);
5257 if (p_mod != Parameter.Modifier.PARAMS) {
5258 p_mod = (pd.FixedParameters[i].ModFlags & ~Parameter.Modifier.PARAMS) | (cpd.FixedParameters[i].ModFlags & Parameter.Modifier.PARAMS);
5260 } else if (!params_expanded_form) {
5261 params_expanded_form = true;
5262 pt = ((ElementTypeSpec) pt).Element;
5268 if (!params_expanded_form) {
5269 if (a.IsExtensionType) {
5270 if (ExtensionMethodGroupExpr.IsExtensionTypeCompatible (a.Type, pt)) {
5275 score = IsArgumentCompatible (ec, a, p_mod, pt);
5278 dynamicArgument = true;
5283 // It can be applicable in expanded form (when not doing exact match like for delegates)
5285 if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0 && (restrictions & Restrictions.CovariantDelegate) == 0) {
5286 if (!params_expanded_form) {
5287 pt = ((ElementTypeSpec) pt).Element;
5291 score = IsArgumentCompatible (ec, a, Parameter.Modifier.NONE, pt);
5294 params_expanded_form = true;
5295 dynamicArgument = true;
5296 } else if (score == 0 || arg_count > pd.Count) {
5297 params_expanded_form = true;
5302 if (params_expanded_form)
5304 return (arg_count - i) * 2 + score;
5309 // Restore original arguments for dynamic binder to keep the intention of original source code
5311 if (dynamicArgument)
5312 arguments = orig_args;
5317 public static Expression ResolveDefaultValueArgument (ResolveContext ec, TypeSpec ptype, Expression e, Location loc)
5319 if (e is Constant && e.Type == ptype)
5323 // LAMESPEC: No idea what the exact rules are for System.Reflection.Missing.Value instead of null
5325 if (e == EmptyExpression.MissingValue && (ptype.BuiltinType == BuiltinTypeSpec.Type.Object || ptype.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) {
5326 e = new MemberAccess (new MemberAccess (new MemberAccess (
5327 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Reflection", loc), "Missing", loc), "Value", loc);
5328 } else if (e is Constant) {
5330 // Handles int to int? conversions, DefaultParameterValue check
5332 e = Convert.ImplicitConversionStandard (ec, e, ptype, loc);
5336 e = new DefaultValueExpression (new TypeExpression (ptype, loc), loc);
5339 return e.Resolve (ec);
5343 // Tests argument compatibility with the parameter
5344 // The possible return values are
5346 // 1 - modifier mismatch
5347 // 2 - type mismatch
5348 // -1 - dynamic binding required
5350 int IsArgumentCompatible (ResolveContext ec, Argument argument, Parameter.Modifier param_mod, TypeSpec parameter)
5353 // Types have to be identical when ref or out modifer
5354 // is used and argument is not of dynamic type
5356 if (((argument.Modifier | param_mod) & Parameter.Modifier.RefOutMask) != 0) {
5357 var arg_type = argument.Type;
5359 if ((argument.Modifier & Parameter.Modifier.RefOutMask) != (param_mod & Parameter.Modifier.RefOutMask)) {
5361 // Using dynamic for ref/out parameter can still succeed at runtime
5363 if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5369 if (arg_type != parameter) {
5370 if (arg_type == InternalType.VarOutType)
5374 // Do full equality check after quick path
5376 if (!TypeSpecComparer.IsEqual (arg_type, parameter)) {
5378 // Using dynamic for ref/out parameter can still succeed at runtime
5380 if (arg_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (argument.Modifier & Parameter.Modifier.RefOutMask) == 0 && (restrictions & Restrictions.CovariantDelegate) == 0)
5388 if (argument.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic && (restrictions & Restrictions.CovariantDelegate) == 0)
5392 // Use implicit conversion in all modes to return same candidates when the expression
5393 // is used as argument or delegate conversion
5395 if (!Convert.ImplicitConversionExists (ec, argument.Expr, parameter)) {
5396 return parameter.IsDelegate && argument.Expr is AnonymousMethodExpression ? 2 : 3;
5403 static TypeSpec MoreSpecific (TypeSpec p, TypeSpec q)
5405 if (TypeManager.IsGenericParameter (p) && !TypeManager.IsGenericParameter (q))
5407 if (!TypeManager.IsGenericParameter (p) && TypeManager.IsGenericParameter (q))
5410 var ac_p = p as ArrayContainer;
5412 var ac_q = q as ArrayContainer;
5416 TypeSpec specific = MoreSpecific (ac_p.Element, ac_q.Element);
5417 if (specific == ac_p.Element)
5419 if (specific == ac_q.Element)
5421 } else if (p.IsGeneric && q.IsGeneric) {
5422 var pargs = TypeManager.GetTypeArguments (p);
5423 var qargs = TypeManager.GetTypeArguments (q);
5425 bool p_specific_at_least_once = false;
5426 bool q_specific_at_least_once = false;
5428 for (int i = 0; i < pargs.Length; i++) {
5429 TypeSpec specific = MoreSpecific (pargs[i], qargs[i]);
5430 if (specific == pargs[i])
5431 p_specific_at_least_once = true;
5432 if (specific == qargs[i])
5433 q_specific_at_least_once = true;
5436 if (p_specific_at_least_once && !q_specific_at_least_once)
5438 if (!p_specific_at_least_once && q_specific_at_least_once)
5446 // Find the best method from candidate list
5448 public T ResolveMember<T> (ResolveContext rc, ref Arguments args) where T : MemberSpec, IParametersMember
5450 List<AmbiguousCandidate> ambiguous_candidates = null;
5452 MemberSpec best_candidate;
5453 Arguments best_candidate_args = null;
5454 bool best_candidate_params = false;
5455 bool best_candidate_dynamic = false;
5456 int best_candidate_rate;
5457 IParametersMember best_parameter_member = null;
5459 int args_count = args != null ? args.Count : 0;
5461 Arguments candidate_args = args;
5462 bool error_mode = false;
5463 MemberSpec invocable_member = null;
5464 int applicable_candidates = 0;
5467 best_candidate = null;
5468 best_candidate_rate = int.MaxValue;
5470 var type_members = members;
5472 for (int i = 0; i < type_members.Count; ++i) {
5473 var member = type_members[i];
5476 // Methods in a base class are not candidates if any method in a derived
5477 // class is applicable
5479 if ((member.Modifiers & Modifiers.OVERRIDE) != 0)
5483 if (!member.IsAccessible (rc))
5486 if (rc.IsRuntimeBinder && !member.DeclaringType.IsAccessible (rc))
5489 if ((member.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5490 instance_qualifier != null && !instance_qualifier.CheckProtectedMemberAccess (rc, member)) {
5495 IParametersMember pm = member as IParametersMember;
5498 // Will use it later to report ambiguity between best method and invocable member
5500 if (Invocation.IsMemberInvocable (member))
5501 invocable_member = member;
5507 // Overload resolution is looking for base member but using parameter names
5508 // and default values from the closest member. That means to do expensive lookup
5509 // for the closest override for virtual or abstract members
5511 if ((member.Modifiers & (Modifiers.VIRTUAL | Modifiers.ABSTRACT)) != 0) {
5512 var override_params = base_provider.GetOverrideMemberParameters (member);
5513 if (override_params != null)
5514 pm = override_params;
5518 // Check if the member candidate is applicable
5520 bool params_expanded_form = false;
5521 bool dynamic_argument = false;
5522 TypeSpec rt = pm.MemberType;
5523 int candidate_rate = IsApplicable (rc, ref candidate_args, args_count, ref member, pm, ref params_expanded_form, ref dynamic_argument, ref rt, error_mode);
5525 if (lambda_conv_msgs != null)
5526 lambda_conv_msgs.EndSession ();
5529 // How does it score compare to others
5531 if (candidate_rate < best_candidate_rate) {
5533 // Fatal error (missing dependency), cannot continue
5534 if (candidate_rate < 0)
5537 applicable_candidates = 1;
5538 if ((restrictions & Restrictions.GetEnumeratorLookup) != 0 && candidate_args.Count != 0) {
5539 // Only parameterless methods are considered
5541 best_candidate_rate = candidate_rate;
5542 best_candidate = member;
5543 best_candidate_args = candidate_args;
5544 best_candidate_params = params_expanded_form;
5545 best_candidate_dynamic = dynamic_argument;
5546 best_parameter_member = pm;
5547 best_candidate_return_type = rt;
5549 } else if (candidate_rate == 0) {
5551 // The member look is done per type for most operations but sometimes
5552 // it's not possible like for binary operators overload because they
5553 // are unioned between 2 sides
5555 if ((restrictions & Restrictions.BaseMembersIncluded) != 0) {
5556 if (TypeSpec.IsBaseClass (best_candidate.DeclaringType, member.DeclaringType, true))
5560 ++applicable_candidates;
5562 if (best_candidate.DeclaringType.IsInterface && member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5564 // We pack all interface members into top level type which makes the overload resolution
5565 // more complicated for interfaces. We compensate it by removing methods with same
5566 // signature when building the cache hence this path should not really be hit often
5569 // interface IA { void Foo (int arg); }
5570 // interface IB : IA { void Foo (params int[] args); }
5572 // IB::Foo is the best overload when calling IB.Foo (1)
5575 if (ambiguous_candidates != null) {
5576 foreach (var amb_cand in ambiguous_candidates) {
5577 if (member.DeclaringType.ImplementsInterface (best_candidate.DeclaringType, false)) {
5586 ambiguous_candidates = null;
5589 // Is the new candidate better
5590 is_better = BetterFunction (rc, candidate_args, member, pm.Parameters, params_expanded_form, best_candidate, best_parameter_member.Parameters, best_candidate_params);
5594 best_candidate = member;
5595 best_candidate_args = candidate_args;
5596 best_candidate_params = params_expanded_form;
5597 best_candidate_dynamic = dynamic_argument;
5598 best_parameter_member = pm;
5599 best_candidate_return_type = rt;
5601 // It's not better but any other found later could be but we are not sure yet
5602 if (ambiguous_candidates == null)
5603 ambiguous_candidates = new List<AmbiguousCandidate> ();
5605 ambiguous_candidates.Add (new AmbiguousCandidate (member, pm.Parameters, params_expanded_form));
5609 // Restore expanded arguments
5610 candidate_args = args;
5612 } while (best_candidate_rate != 0 && (type_members = base_provider.GetBaseMembers (type_members[0].DeclaringType)) != null);
5615 // We've found exact match
5617 if (best_candidate_rate == 0)
5621 // Try extension methods lookup when no ordinary method match was found and provider enables it
5624 var emg = base_provider.LookupExtensionMethod (rc);
5626 emg = emg.OverloadResolve (rc, ref args, null, restrictions);
5628 best_candidate_extension_group = emg;
5629 return (T) (MemberSpec) emg.BestCandidate;
5634 // Don't run expensive error reporting mode for probing
5641 if (lambda_conv_msgs != null && !lambda_conv_msgs.IsEmpty)
5644 lambda_conv_msgs = null;
5649 // No best member match found, report an error
5651 if (best_candidate_rate != 0 || error_mode) {
5652 ReportOverloadError (rc, best_candidate, best_parameter_member, best_candidate_args, best_candidate_params);
5656 if (best_candidate_dynamic) {
5657 if (args[0].IsExtensionType) {
5658 rc.Report.Error (1973, loc,
5659 "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",
5660 args [0].Type.GetSignatureForError (), best_candidate.Name, best_candidate.GetSignatureForError ());
5664 // Check type constraints only when explicit type arguments are used
5666 if (applicable_candidates == 1 && best_candidate.IsGeneric && type_arguments != null) {
5667 MethodSpec bc = best_candidate as MethodSpec;
5668 if (bc != null && TypeParameterSpec.HasAnyTypeParameterConstrained (bc.GenericDefinition)) {
5669 ConstraintChecker cc = new ConstraintChecker (rc);
5670 cc.CheckAll (bc.GetGenericMethodDefinition (), bc.TypeArguments, bc.Constraints, loc);
5674 BestCandidateIsDynamic = true;
5679 // These flags indicates we are running delegate probing conversion. No need to
5680 // do more expensive checks
5682 if ((restrictions & (Restrictions.ProbingOnly | Restrictions.CovariantDelegate)) == (Restrictions.CovariantDelegate | Restrictions.ProbingOnly))
5683 return (T) best_candidate;
5685 if (ambiguous_candidates != null) {
5687 // Now check that there are no ambiguities i.e the selected method
5688 // should be better than all the others
5690 for (int ix = 0; ix < ambiguous_candidates.Count; ix++) {
5691 var candidate = ambiguous_candidates [ix];
5693 if (!BetterFunction (rc, best_candidate_args, best_candidate, best_parameter_member.Parameters, best_candidate_params, candidate.Member, candidate.Parameters, candidate.Expanded)) {
5694 var ambiguous = candidate.Member;
5695 if (custom_errors == null || !custom_errors.AmbiguousCandidates (rc, best_candidate, ambiguous)) {
5696 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5697 rc.Report.SymbolRelatedToPreviousError (ambiguous);
5698 rc.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'",
5699 best_candidate.GetSignatureForError (), ambiguous.GetSignatureForError ());
5702 return (T) best_candidate;
5707 if (invocable_member != null && !IsProbingOnly) {
5708 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5709 rc.Report.SymbolRelatedToPreviousError (invocable_member);
5710 rc.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and invocable non-method `{1}'. Using method group",
5711 best_candidate.GetSignatureForError (), invocable_member.GetSignatureForError ());
5715 // And now check if the arguments are all
5716 // compatible, perform conversions if
5717 // necessary etc. and return if everything is
5720 if (!VerifyArguments (rc, ref best_candidate_args, best_candidate, best_parameter_member, best_candidate_params))
5723 if (best_candidate == null)
5727 // Don't run possibly expensive checks in probing mode
5729 if (!IsProbingOnly && !rc.IsInProbingMode) {
5731 // Check ObsoleteAttribute on the best method
5733 best_candidate.CheckObsoleteness (rc, loc);
5735 best_candidate.MemberDefinition.SetIsUsed ();
5738 args = best_candidate_args;
5739 return (T) best_candidate;
5742 public MethodSpec ResolveOperator (ResolveContext rc, ref Arguments args)
5744 return ResolveMember<MethodSpec> (rc, ref args);
5747 void ReportArgumentMismatch (ResolveContext ec, int idx, MemberSpec method,
5748 Argument a, AParametersCollection expected_par, TypeSpec paramType)
5750 if (custom_errors != null && custom_errors.ArgumentMismatch (ec, method, a, idx))
5753 if (a.Type == InternalType.ErrorType)
5756 if (a is CollectionElementInitializer.ElementInitializerArgument) {
5757 ec.Report.SymbolRelatedToPreviousError (method);
5758 if ((expected_par.FixedParameters[idx].ModFlags & Parameter.Modifier.RefOutMask) != 0) {
5759 ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have `ref' or `out' modifier",
5760 TypeManager.CSharpSignature (method));
5763 ec.Report.Error (1950, loc, "The best overloaded collection initalizer method `{0}' has some invalid arguments",
5764 TypeManager.CSharpSignature (method));
5765 } else if (IsDelegateInvoke) {
5766 ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments",
5767 DelegateType.GetSignatureForError ());
5769 ec.Report.SymbolRelatedToPreviousError (method);
5770 ec.Report.Error (1502, loc, "The best overloaded method match for `{0}' has some invalid arguments",
5771 method.GetSignatureForError ());
5774 Parameter.Modifier mod = idx >= expected_par.Count ? 0 : expected_par.FixedParameters[idx].ModFlags;
5776 string index = (idx + 1).ToString ();
5777 if (((mod & Parameter.Modifier.RefOutMask) ^ (a.Modifier & Parameter.Modifier.RefOutMask)) != 0) {
5778 if ((mod & Parameter.Modifier.RefOutMask) == 0)
5779 ec.Report.Error (1615, a.Expr.Location, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
5780 index, Parameter.GetModifierSignature (a.Modifier));
5782 ec.Report.Error (1620, a.Expr.Location, "Argument `#{0}' is missing `{1}' modifier",
5783 index, Parameter.GetModifierSignature (mod));
5785 string p1 = a.GetSignatureForError ();
5786 string p2 = paramType.GetSignatureForError ();
5789 p1 = a.Type.GetSignatureForErrorIncludingAssemblyName ();
5790 p2 = paramType.GetSignatureForErrorIncludingAssemblyName ();
5793 if ((mod & Parameter.Modifier.RefOutMask) != 0) {
5794 p1 = Parameter.GetModifierSignature (a.Modifier) + " " + p1;
5795 p2 = Parameter.GetModifierSignature (a.Modifier) + " " + p2;
5798 ec.Report.Error (1503, a.Expr.Location,
5799 "Argument `#{0}' cannot convert `{1}' expression to type `{2}'", index, p1, p2);
5804 // We have failed to find exact match so we return error info about the closest match
5806 void ReportOverloadError (ResolveContext rc, MemberSpec best_candidate, IParametersMember pm, Arguments args, bool params_expanded)
5808 int ta_count = type_arguments == null ? 0 : type_arguments.Count;
5809 int arg_count = args == null ? 0 : args.Count;
5811 if (ta_count != best_candidate.Arity && (ta_count > 0 || ((IParametersMember) best_candidate).Parameters.IsEmpty)) {
5812 var mg = new MethodGroupExpr (new [] { best_candidate }, best_candidate.DeclaringType, loc);
5813 mg.Error_TypeArgumentsCannotBeUsed (rc, best_candidate, loc);
5817 if (lambda_conv_msgs != null && lambda_conv_msgs.Merge (rc.Report.Printer)) {
5822 if ((best_candidate.Modifiers & (Modifiers.PROTECTED | Modifiers.STATIC)) == Modifiers.PROTECTED &&
5823 InstanceQualifier != null && !InstanceQualifier.CheckProtectedMemberAccess (rc, best_candidate)) {
5824 MemberExpr.Error_ProtectedMemberAccess (rc, best_candidate, InstanceQualifier.InstanceType, loc);
5828 // For candidates which match on parameters count report more details about incorrect arguments
5831 if (pm.Parameters.Count == arg_count || params_expanded || HasUnfilledParams (best_candidate, pm, args)) {
5832 // Reject any inaccessible member
5833 if (!best_candidate.IsAccessible (rc) || !best_candidate.DeclaringType.IsAccessible (rc)) {
5834 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5835 Expression.ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
5839 var ms = best_candidate as MethodSpec;
5840 if (ms != null && ms.IsGeneric) {
5841 bool constr_ok = true;
5842 if (ms.TypeArguments != null)
5843 constr_ok = new ConstraintChecker (rc.MemberContext).CheckAll (ms.GetGenericMethodDefinition (), ms.TypeArguments, ms.Constraints, loc);
5845 if (ta_count == 0 && ms.TypeArguments == null) {
5846 if (custom_errors != null && custom_errors.TypeInferenceFailed (rc, best_candidate))
5850 rc.Report.Error (411, loc,
5851 "The type arguments for method `{0}' cannot be inferred from the usage. Try specifying the type arguments explicitly",
5852 ms.GetGenericMethodDefinition ().GetSignatureForError ());
5859 VerifyArguments (rc, ref args, best_candidate, pm, params_expanded);
5865 // We failed to find any method with correct argument count, report best candidate
5867 if (custom_errors != null && custom_errors.NoArgumentMatch (rc, best_candidate))
5870 if (best_candidate.Kind == MemberKind.Constructor) {
5871 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5872 Error_ConstructorMismatch (rc, best_candidate.DeclaringType, arg_count, loc);
5873 } else if (IsDelegateInvoke) {
5874 rc.Report.SymbolRelatedToPreviousError (DelegateType);
5875 rc.Report.Error (1593, loc, "Delegate `{0}' does not take `{1}' arguments",
5876 DelegateType.GetSignatureForError (), arg_count.ToString ());
5878 string name = best_candidate.Kind == MemberKind.Indexer ? "this" : best_candidate.Name;
5879 rc.Report.SymbolRelatedToPreviousError (best_candidate);
5880 rc.Report.Error (1501, loc, "No overload for method `{0}' takes `{1}' arguments",
5881 name, arg_count.ToString ());
5885 static bool HasUnfilledParams (MemberSpec best_candidate, IParametersMember pm, Arguments args)
5887 var p = ((IParametersMember)best_candidate).Parameters;
5892 for (int i = p.Count - 1; i != 0; --i) {
5893 var fp = p.FixedParameters [i];
5894 if ((fp.ModFlags & Parameter.Modifier.PARAMS) == 0)
5904 foreach (var arg in args) {
5905 var na = arg as NamedArgument;
5909 if (na.Name == name) {
5918 return args.Count + 1 == pm.Parameters.Count;
5921 bool VerifyArguments (ResolveContext ec, ref Arguments args, MemberSpec member, IParametersMember pm, bool chose_params_expanded)
5923 var pd = pm.Parameters;
5924 var cpd = ((IParametersMember) member).Parameters;
5925 var ptypes = cpd.Types;
5927 Parameter.Modifier p_mod = 0;
5929 int a_idx = 0, a_pos = 0;
5931 ArrayInitializer params_initializers = null;
5932 bool has_unsafe_arg = pm.MemberType.IsPointer;
5933 int arg_count = args == null ? 0 : args.Count;
5935 for (; a_idx < arg_count; a_idx++, ++a_pos) {
5940 if (p_mod != Parameter.Modifier.PARAMS) {
5941 p_mod = cpd.FixedParameters [a_idx].ModFlags;
5943 has_unsafe_arg |= pt.IsPointer;
5945 if (p_mod == Parameter.Modifier.PARAMS) {
5946 if (chose_params_expanded) {
5947 params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location);
5948 pt = TypeManager.GetElementType (pt);
5954 // Types have to be identical when ref or out modifer is used
5956 if (((a.Modifier | p_mod) & Parameter.Modifier.RefOutMask) != 0) {
5957 if ((a.Modifier & Parameter.Modifier.RefOutMask) != (p_mod & Parameter.Modifier.RefOutMask))
5960 var arg_type = a.Type;
5964 if (arg_type == InternalType.VarOutType) {
5966 // Set underlying variable type based on parameter type
5968 ((DeclarationExpression)a.Expr).Variable.Type = pt;
5972 if (!TypeSpecComparer.IsEqual (arg_type, pt))
5976 NamedArgument na = a as NamedArgument;
5978 int name_index = pd.GetParameterIndexByName (na.Name);
5979 if (name_index < 0 || name_index >= pd.Count) {
5980 if (IsDelegateInvoke) {
5981 ec.Report.SymbolRelatedToPreviousError (DelegateType);
5982 ec.Report.Error (1746, na.Location,
5983 "The delegate `{0}' does not contain a parameter named `{1}'",
5984 DelegateType.GetSignatureForError (), na.Name);
5986 ec.Report.SymbolRelatedToPreviousError (member);
5987 ec.Report.Error (1739, na.Location,
5988 "The best overloaded method match for `{0}' does not contain a parameter named `{1}'",
5989 TypeManager.CSharpSignature (member), na.Name);
5991 } else if (args[name_index] != a && args[name_index] != null) {
5992 if (IsDelegateInvoke)
5993 ec.Report.SymbolRelatedToPreviousError (DelegateType);
5995 ec.Report.SymbolRelatedToPreviousError (member);
5997 ec.Report.Error (1744, na.Location,
5998 "Named argument `{0}' cannot be used for a parameter which has positional argument specified",
6003 if (a.Expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
6006 if ((restrictions & Restrictions.CovariantDelegate) != 0 && !Delegate.IsTypeCovariant (ec, a.Expr.Type, pt)) {
6007 if (a.IsExtensionType) {
6008 // TODO: Should report better message type, something similar to CS1928/1929 instead of
6009 // CS1061 but that still better than confusing CS0123
6010 var ma = new MemberAccess (a.Expr, member.Name, loc);
6011 ma.Error_TypeDoesNotContainDefinition (ec, a.Expr.Type, ma.Name);
6013 custom_errors.NoArgumentMatch (ec, member);
6019 if (a.IsExtensionType) {
6020 if (a.Expr.Type == pt || TypeSpecComparer.IsEqual (a.Expr.Type, pt)) {
6023 conv = Convert.ImplicitReferenceConversion (a.Expr, pt, false);
6025 conv = Convert.ImplicitBoxingConversion (a.Expr, a.Expr.Type, pt);
6028 conv = Convert.ImplicitConversion (ec, a.Expr, pt, loc);
6035 // Convert params arguments to an array initializer
6037 if (params_initializers != null) {
6038 // we choose to use 'a.Expr' rather than 'conv' so that
6039 // we don't hide the kind of expression we have (esp. CompoundAssign.Helper)
6040 params_initializers.Add (a.Expr);
6041 args.RemoveAt (a_idx--);
6047 // Update the argument with the implicit conversion
6051 if (a_idx != arg_count) {
6053 // Convert all var out argument to error type for less confusing error reporting
6054 // when no matching overload is found
6056 for (; a_idx < arg_count; a_idx++) {
6057 var arg = args [a_idx];
6061 if (arg.Type == InternalType.VarOutType) {
6062 ((DeclarationExpression)arg.Expr).Variable.Type = InternalType.ErrorType;
6066 ReportArgumentMismatch (ec, a_pos, member, a, pd, pt);
6071 // Fill not provided arguments required by params modifier
6073 if (params_initializers == null && arg_count + 1 == pd.Count) {
6075 args = new Arguments (1);
6077 pt = ptypes[pd.Count - 1];
6078 pt = TypeManager.GetElementType (pt);
6079 has_unsafe_arg |= pt.IsPointer;
6080 params_initializers = new ArrayInitializer (0, loc);
6084 // Append an array argument with all params arguments
6086 if (params_initializers != null) {
6087 args.Add (new Argument (
6088 new ArrayCreation (new TypeExpression (pt, loc), params_initializers, loc).Resolve (ec)));
6092 if (has_unsafe_arg) {
6093 if (ec.CurrentIterator != null) {
6094 Expression.UnsafeInsideIteratorError (ec, loc);
6095 } else if (!ec.IsUnsafe) {
6096 Expression.UnsafeError (ec, loc);
6101 // We could infer inaccesible type arguments
6103 if (type_arguments == null && member.IsGeneric) {
6104 var ms = (MethodSpec) member;
6105 foreach (var ta in ms.TypeArguments) {
6106 if (!ta.IsAccessible (ec)) {
6107 ec.Report.SymbolRelatedToPreviousError (ta);
6108 Expression.ErrorIsInaccesible (ec, member.GetSignatureForError (), loc);
6118 public class ConstantExpr : MemberExpr
6120 readonly ConstSpec constant;
6122 public ConstantExpr (ConstSpec constant, Location loc)
6124 this.constant = constant;
6128 public override string Name {
6129 get { throw new NotImplementedException (); }
6132 public override string KindName {
6133 get { return "constant"; }
6136 public override bool IsInstance {
6137 get { return !IsStatic; }
6140 public override bool IsStatic {
6141 get { return true; }
6144 protected override TypeSpec DeclaringType {
6145 get { return constant.DeclaringType; }
6148 public override Expression CreateExpressionTree (ResolveContext ec)
6150 throw new NotSupportedException ("ET");
6153 protected override Expression DoResolve (ResolveContext rc)
6155 ResolveInstanceExpression (rc, null);
6156 DoBestMemberChecks (rc, constant);
6158 if (rc.HasSet (ResolveContext.Options.NameOfScope)) {
6159 eclass = ExprClass.Value;
6160 type = constant.MemberType;
6164 var c = constant.GetConstant (rc);
6166 // Creates reference expression to the constant value
6167 return Constant.CreateConstantFromValue (constant.MemberType, c.GetValue (), loc);
6170 public override void Emit (EmitContext ec)
6172 throw new NotSupportedException ();
6175 public override string GetSignatureForError ()
6177 return constant.GetSignatureForError ();
6180 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
6182 constant.CheckObsoleteness (rc, expr.Location);
6185 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6187 Error_TypeArgumentsCannotBeUsed (ec, "constant", GetSignatureForError (), loc);
6192 // Fully resolved expression that references a Field
6194 public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference
6196 protected FieldSpec spec;
6197 VariableInfo variable_info;
6199 LocalTemporary temp;
6202 protected FieldExpr (Location l)
6207 public FieldExpr (FieldSpec spec, Location loc)
6212 type = spec.MemberType;
6215 public FieldExpr (FieldBase fi, Location l)
6222 public override string Name {
6228 public bool IsHoisted {
6230 IVariableReference hv = InstanceExpression as IVariableReference;
6231 return hv != null && hv.IsHoisted;
6235 public override bool IsInstance {
6237 return !spec.IsStatic;
6241 public override bool IsStatic {
6243 return spec.IsStatic;
6247 public override string KindName {
6248 get { return "field"; }
6251 public FieldSpec Spec {
6257 protected override TypeSpec DeclaringType {
6259 return spec.DeclaringType;
6263 public VariableInfo VariableInfo {
6265 return variable_info;
6271 public override string GetSignatureForError ()
6273 return spec.GetSignatureForError ();
6276 public bool IsMarshalByRefAccess (ResolveContext rc)
6278 // Checks possible ldflda of field access expression
6279 return !spec.IsStatic && TypeSpec.IsValueType (spec.MemberType) && !(InstanceExpression is This) &&
6280 rc.Module.PredefinedTypes.MarshalByRefObject.Define () &&
6281 TypeSpec.IsBaseClass (spec.DeclaringType, rc.Module.PredefinedTypes.MarshalByRefObject.TypeSpec, false);
6284 public void SetHasAddressTaken ()
6286 IVariableReference vr = InstanceExpression as IVariableReference;
6288 vr.SetHasAddressTaken ();
6292 protected override void CloneTo (CloneContext clonectx, Expression target)
6294 var t = (FieldExpr) target;
6296 if (InstanceExpression != null)
6297 t.InstanceExpression = InstanceExpression.Clone (clonectx);
6300 public override Expression CreateExpressionTree (ResolveContext ec)
6302 if (ConditionalAccess) {
6303 Error_NullShortCircuitInsideExpressionTree (ec);
6306 return CreateExpressionTree (ec, true);
6309 public Expression CreateExpressionTree (ResolveContext ec, bool convertInstance)
6312 Expression instance;
6314 if (InstanceExpression == null) {
6315 instance = new NullLiteral (loc);
6316 } else if (convertInstance) {
6317 instance = InstanceExpression.CreateExpressionTree (ec);
6319 args = new Arguments (1);
6320 args.Add (new Argument (InstanceExpression));
6321 instance = CreateExpressionFactoryCall (ec, "Constant", args);
6324 args = Arguments.CreateForExpressionTree (ec, null,
6326 CreateTypeOfExpression ());
6328 return CreateExpressionFactoryCall (ec, "Field", args);
6331 public Expression CreateTypeOfExpression ()
6333 return new TypeOfField (spec, loc);
6336 protected override Expression DoResolve (ResolveContext ec)
6338 spec.MemberDefinition.SetIsUsed ();
6340 return DoResolve (ec, null);
6343 Expression DoResolve (ResolveContext ec, Expression rhs)
6345 bool lvalue_instance = rhs != null && IsInstance && spec.DeclaringType.IsStruct;
6348 ResolveConditionalAccessReceiver (ec);
6350 if (ResolveInstanceExpression (ec, rhs)) {
6351 // Resolve the field's instance expression while flow analysis is turned
6352 // off: when accessing a field "a.b", we must check whether the field
6353 // "a.b" is initialized, not whether the whole struct "a" is initialized.
6355 if (lvalue_instance) {
6356 bool out_access = rhs == EmptyExpression.OutAccess || rhs == EmptyExpression.LValueMemberOutAccess;
6358 Expression right_side =
6359 out_access ? EmptyExpression.LValueMemberOutAccess : EmptyExpression.LValueMemberAccess;
6361 InstanceExpression = InstanceExpression.ResolveLValue (ec, right_side);
6363 InstanceExpression = InstanceExpression.Resolve (ec, ResolveFlags.VariableOrValue);
6366 if (InstanceExpression == null)
6370 DoBestMemberChecks (ec, spec);
6372 if (conditional_access_receiver)
6373 ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, false);
6376 var fb = spec as FixedFieldSpec;
6377 IVariableReference var = InstanceExpression as IVariableReference;
6380 IFixedExpression fe = InstanceExpression as IFixedExpression;
6381 if (!ec.HasSet (ResolveContext.Options.FixedInitializerScope) && (fe == null || !fe.IsFixed)) {
6382 ec.Report.Error (1666, loc, "You cannot use fixed size buffers contained in unfixed expressions. Try using the fixed statement");
6385 if (InstanceExpression.eclass != ExprClass.Variable) {
6386 ec.Report.SymbolRelatedToPreviousError (spec);
6387 ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields",
6388 TypeManager.GetFullNameSignature (spec));
6389 } else if (var != null && var.IsHoisted) {
6390 AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc);
6393 return new FixedBufferPtr (this, fb.ElementType, loc).Resolve (ec);
6397 // Set flow-analysis variable info for struct member access. It will be check later
6398 // for precise error reporting
6400 if (var != null && var.VariableInfo != null && InstanceExpression.Type.IsStruct) {
6401 variable_info = var.VariableInfo.GetStructFieldInfo (Name);
6404 if (conditional_access_receiver)
6405 type = LiftMemberType (ec, type);
6407 if (ConditionalAccess && InstanceExpression != null && InstanceExpression.IsNull)
6408 return Constant.CreateConstantFromValue (type, null, loc);
6410 eclass = ExprClass.Variable;
6414 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
6416 spec.CheckObsoleteness (rc, expr.Location);
6419 public void SetFieldAssigned (FlowAnalysisContext fc)
6424 bool lvalue_instance = spec.DeclaringType.IsStruct;
6425 if (lvalue_instance) {
6426 var var = InstanceExpression as IVariableReference;
6427 if (var != null && var.VariableInfo != null) {
6428 fc.SetStructFieldAssigned (var.VariableInfo, Name);
6432 var fe = InstanceExpression as FieldExpr;
6434 Expression instance;
6437 instance = fe.InstanceExpression;
6438 var fe_instance = instance as FieldExpr;
6439 if ((fe_instance != null && !fe_instance.IsStatic) || instance is LocalVariableReference) {
6440 if (TypeSpec.IsReferenceType (fe.Type) && instance.Type.IsStruct) {
6441 var var = InstanceExpression as IVariableReference;
6442 if (var != null && var.VariableInfo == null) {
6443 var var_inst = instance as IVariableReference;
6444 if (var_inst == null || (var_inst.VariableInfo != null && !fc.IsDefinitelyAssigned (var_inst.VariableInfo)))
6445 fc.Report.Warning (1060, 1, fe.loc, "Use of possibly unassigned field `{0}'", fe.Name);
6449 if (fe_instance != null) {
6458 if (instance != null && TypeSpec.IsReferenceType (instance.Type))
6459 instance.FlowAnalysis (fc);
6461 if (TypeSpec.IsReferenceType (InstanceExpression.Type))
6462 InstanceExpression.FlowAnalysis (fc);
6466 Expression Error_AssignToReadonly (ResolveContext rc, Expression right_side)
6468 // The return value is always null. Returning a value simplifies calling code.
6470 if (right_side == EmptyExpression.OutAccess) {
6472 rc.Report.Error (199, loc, "A static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6473 GetSignatureForError ());
6475 rc.Report.Error (192, loc, "A readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6476 GetSignatureForError ());
6482 if (right_side == EmptyExpression.LValueMemberAccess) {
6483 // Already reported as CS1648/CS1650
6487 if (right_side == EmptyExpression.LValueMemberOutAccess) {
6489 rc.Report.Error (1651, loc, "Fields of static readonly field `{0}' cannot be passed ref or out (except in a static constructor)",
6490 GetSignatureForError ());
6492 rc.Report.Error (1649, loc, "Members of readonly field `{0}' cannot be passed ref or out (except in a constructor)",
6493 GetSignatureForError ());
6499 rc.Report.Error (198, loc, "A static readonly field `{0}' cannot be assigned to (except in a static constructor or a variable initializer)",
6500 GetSignatureForError ());
6502 rc.Report.Error (191, loc, "A readonly field `{0}' cannot be assigned to (except in a constructor or a variable initializer)",
6503 GetSignatureForError ());
6509 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
6511 if (HasConditionalAccess ())
6512 Error_NullPropagatingLValue (ec);
6514 if (spec is FixedFieldSpec) {
6515 // It could be much better error message but we want to be error compatible
6516 Error_ValueAssignment (ec, right_side);
6519 Expression e = DoResolve (ec, right_side);
6524 spec.MemberDefinition.SetIsAssigned ();
6526 if ((right_side == EmptyExpression.UnaryAddress || right_side == EmptyExpression.OutAccess) &&
6527 (spec.Modifiers & Modifiers.VOLATILE) != 0) {
6528 ec.Report.Warning (420, 1, loc,
6529 "`{0}': A volatile field references will not be treated as volatile",
6530 spec.GetSignatureForError ());
6533 if (spec.IsReadOnly) {
6534 // InitOnly fields can only be assigned in constructors or initializers
6535 if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope))
6536 return Error_AssignToReadonly (ec, right_side);
6538 if (ec.HasSet (ResolveContext.Options.ConstructorScope)) {
6540 // InitOnly fields cannot be assigned-to in a different constructor from their declaring type
6541 if (ec.CurrentMemberDefinition.Parent.PartialContainer.Definition != spec.DeclaringType.GetDefinition ())
6542 return Error_AssignToReadonly (ec, right_side);
6543 // static InitOnly fields cannot be assigned-to in an instance constructor
6544 if (IsStatic && !ec.IsStatic)
6545 return Error_AssignToReadonly (ec, right_side);
6546 // instance constructors can't modify InitOnly fields of other instances of the same type
6547 if (!IsStatic && !(InstanceExpression is This))
6548 return Error_AssignToReadonly (ec, right_side);
6552 if (right_side == EmptyExpression.OutAccess && IsMarshalByRefAccess (ec)) {
6553 ec.Report.SymbolRelatedToPreviousError (spec.DeclaringType);
6554 ec.Report.Warning (197, 1, loc,
6555 "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",
6556 GetSignatureForError ());
6559 eclass = ExprClass.Variable;
6563 public override void FlowAnalysis (FlowAnalysisContext fc)
6565 var var = InstanceExpression as IVariableReference;
6567 var vi = var.VariableInfo;
6568 if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, Name)) {
6569 fc.Report.Error (170, loc, "Use of possibly unassigned field `{0}'", Name);
6573 if (TypeSpec.IsValueType (InstanceExpression.Type)) {
6574 var le = SkipLeftValueTypeAccess (InstanceExpression);
6576 le.FlowAnalysis (fc);
6582 var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
6584 base.FlowAnalysis (fc);
6586 if (conditional_access_receiver)
6587 fc.DefiniteAssignment = da;
6590 static Expression SkipLeftValueTypeAccess (Expression expr)
6592 if (!TypeSpec.IsValueType (expr.Type))
6595 if (expr is VariableReference)
6598 var fe = expr as FieldExpr;
6602 if (fe.InstanceExpression == null)
6605 return SkipLeftValueTypeAccess (fe.InstanceExpression);
6608 public override int GetHashCode ()
6610 return spec.GetHashCode ();
6613 public bool IsFixed {
6616 // A variable of the form V.I is fixed when V is a fixed variable of a struct type
6618 IVariableReference variable = InstanceExpression as IVariableReference;
6619 if (variable != null)
6620 return InstanceExpression.Type.IsStruct && variable.IsFixed;
6622 IFixedExpression fe = InstanceExpression as IFixedExpression;
6623 return fe != null && fe.IsFixed;
6627 public override bool Equals (object obj)
6629 FieldExpr fe = obj as FieldExpr;
6633 if (spec != fe.spec)
6636 if (InstanceExpression == null || fe.InstanceExpression == null)
6639 return InstanceExpression.Equals (fe.InstanceExpression);
6642 public void Emit (EmitContext ec, bool leave_copy)
6644 bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6648 ec.Emit (OpCodes.Volatile);
6650 ec.Emit (OpCodes.Ldsfld, spec);
6652 var ca = ec.ConditionalAccess;
6655 if (conditional_access_receiver)
6656 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
6658 EmitInstance (ec, false);
6661 // Optimization for build-in types
6662 if (type.IsStruct && type == ec.CurrentType && InstanceExpression.Type == type) {
6663 ec.EmitLoadFromPtr (type);
6665 var ff = spec as FixedFieldSpec;
6667 ec.Emit (OpCodes.Ldflda, spec);
6668 ec.Emit (OpCodes.Ldflda, ff.Element);
6671 ec.Emit (OpCodes.Volatile);
6673 ec.Emit (OpCodes.Ldfld, spec);
6677 if (conditional_access_receiver) {
6678 ec.CloseConditionalAccess (type.IsNullableType && type != spec.MemberType ? type : null);
6679 ec.ConditionalAccess = ca;
6684 ec.Emit (OpCodes.Dup);
6686 temp = new LocalTemporary (this.Type);
6692 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
6694 bool has_await_source = ec.HasSet (BuilderContext.Options.AsyncBody) && source.ContainsEmitWithAwait ();
6695 if (isCompound && !(source is DynamicExpressionStatement) && !has_await_source) {
6700 if (ConditionalAccess)
6701 throw new NotImplementedException ("null operator assignment");
6703 if (has_await_source)
6704 source = source.EmitToField (ec);
6706 EmitInstance (ec, prepared);
6711 if (leave_copy || ec.NotifyEvaluatorOnStore) {
6712 ec.Emit (OpCodes.Dup);
6714 temp = new LocalTemporary (this.Type);
6719 if ((spec.Modifiers & Modifiers.VOLATILE) != 0)
6720 ec.Emit (OpCodes.Volatile);
6722 spec.MemberDefinition.SetIsAssigned ();
6725 ec.Emit (OpCodes.Stsfld, spec);
6727 ec.Emit (OpCodes.Stfld, spec);
6729 if (ec.NotifyEvaluatorOnStore) {
6731 throw new NotImplementedException ("instance field write");
6734 ec.Emit (OpCodes.Dup);
6736 ec.Module.Evaluator.EmitValueChangedCallback (ec, Name, type, loc);
6747 // Emits store to field with prepared values on stack
6749 public void EmitAssignFromStack (EmitContext ec)
6752 ec.Emit (OpCodes.Stsfld, spec);
6754 ec.Emit (OpCodes.Stfld, spec);
6758 public override void Emit (EmitContext ec)
6763 public override void EmitSideEffect (EmitContext ec)
6765 bool is_volatile = (spec.Modifiers & Modifiers.VOLATILE) != 0;
6767 if (is_volatile) // || is_marshal_by_ref ())
6768 base.EmitSideEffect (ec);
6771 public virtual void AddressOf (EmitContext ec, AddressOp mode)
6773 if ((mode & AddressOp.Store) != 0)
6774 spec.MemberDefinition.SetIsAssigned ();
6775 if ((mode & AddressOp.Load) != 0)
6776 spec.MemberDefinition.SetIsUsed ();
6779 // Handle initonly fields specially: make a copy and then
6780 // get the address of the copy.
6783 if (spec.IsReadOnly){
6785 if (ec.HasSet (EmitContext.Options.ConstructorScope) && spec.DeclaringType == ec.CurrentType) {
6797 var temp = ec.GetTemporaryLocal (type);
6798 ec.Emit (OpCodes.Stloc, temp);
6799 ec.Emit (OpCodes.Ldloca, temp);
6805 ec.Emit (OpCodes.Ldsflda, spec);
6808 EmitInstance (ec, false);
6809 ec.Emit (OpCodes.Ldflda, spec);
6813 public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
6815 return MakeExpression (ctx);
6818 public override SLE.Expression MakeExpression (BuilderContext ctx)
6821 return base.MakeExpression (ctx);
6823 return SLE.Expression.Field (
6824 IsStatic ? null : InstanceExpression.MakeExpression (ctx),
6825 spec.GetMetaInfo ());
6829 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
6831 Error_TypeArgumentsCannotBeUsed (ec, "field", GetSignatureForError (), loc);
6837 // Expression that evaluates to a Property.
6839 // This is not an LValue because we need to re-write the expression. We
6840 // can not take data from the stack and store it.
6842 sealed class PropertyExpr : PropertyOrIndexerExpr<PropertySpec>
6844 Arguments arguments;
6845 FieldExpr backing_field;
6847 public PropertyExpr (PropertySpec spec, Location l)
6850 best_candidate = spec;
6851 type = spec.MemberType;
6856 protected override Arguments Arguments {
6865 protected override TypeSpec DeclaringType {
6867 return best_candidate.DeclaringType;
6871 public override string Name {
6873 return best_candidate.Name;
6877 public bool IsAutoPropertyAccess {
6879 var prop = best_candidate.MemberDefinition as Property;
6880 return prop != null && prop.BackingField != null;
6884 public override bool IsInstance {
6890 public override bool IsStatic {
6892 return best_candidate.IsStatic;
6896 public override string KindName {
6897 get { return "property"; }
6900 public PropertySpec PropertyInfo {
6902 return best_candidate;
6908 public override MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
6910 if (best_candidate == null || !(best_candidate.IsStatic || InstanceExpression is This))
6913 var args_count = arguments == null ? 0 : arguments.Count;
6914 if (args_count != body.Parameters.Count && args_count == 0)
6917 var mg = MethodGroupExpr.CreatePredefined (best_candidate.Get, DeclaringType, loc);
6918 mg.InstanceExpression = InstanceExpression;
6923 public static PropertyExpr CreatePredefined (PropertySpec spec, Location loc)
6925 return new PropertyExpr (spec, loc) {
6931 public override Expression CreateExpressionTree (ResolveContext ec)
6933 if (ConditionalAccess) {
6934 Error_NullShortCircuitInsideExpressionTree (ec);
6938 if (IsSingleDimensionalArrayLength ()) {
6939 args = new Arguments (1);
6940 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6941 return CreateExpressionFactoryCall (ec, "ArrayLength", args);
6944 args = new Arguments (2);
6945 if (InstanceExpression == null)
6946 args.Add (new Argument (new NullLiteral (loc)));
6948 args.Add (new Argument (InstanceExpression.CreateExpressionTree (ec)));
6949 args.Add (new Argument (new TypeOfMethod (Getter, loc)));
6950 return CreateExpressionFactoryCall (ec, "Property", args);
6953 public Expression CreateSetterTypeOfExpression (ResolveContext rc)
6955 DoResolveLValue (rc, null);
6956 return new TypeOfMethod (Setter, loc);
6959 public override string GetSignatureForError ()
6961 return best_candidate.GetSignatureForError ();
6964 public override SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
6967 return base.MakeExpression (ctx);
6969 return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Setter.GetMetaInfo ());
6973 public override SLE.Expression MakeExpression (BuilderContext ctx)
6976 return base.MakeExpression (ctx);
6978 return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) Getter.GetMetaInfo ());
6982 void Error_PropertyNotValid (ResolveContext ec)
6984 ec.Report.SymbolRelatedToPreviousError (best_candidate);
6985 ec.Report.Error (1546, loc, "Property or event `{0}' is not supported by the C# language",
6986 GetSignatureForError ());
6989 bool IsSingleDimensionalArrayLength ()
6991 if (best_candidate.DeclaringType.BuiltinType != BuiltinTypeSpec.Type.Array || !best_candidate.HasGet || Name != "Length")
6994 ArrayContainer ac = InstanceExpression.Type as ArrayContainer;
6995 return ac != null && ac.Rank == 1;
6998 public override void Emit (EmitContext ec, bool leave_copy)
7001 // Special case: length of single dimension array property is turned into ldlen
7003 if (IsSingleDimensionalArrayLength ()) {
7004 if (conditional_access_receiver) {
7005 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7008 EmitInstance (ec, false);
7010 ec.Emit (OpCodes.Ldlen);
7011 ec.Emit (OpCodes.Conv_I4);
7013 if (conditional_access_receiver) {
7014 ec.CloseConditionalAccess (type);
7020 base.Emit (ec, leave_copy);
7023 public override void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
7025 if (backing_field != null) {
7026 backing_field.EmitAssign (ec, source, leave_copy, false);
7031 LocalTemporary await_source_arg = null;
7033 if (isCompound && !(source is DynamicExpressionStatement)) {
7034 emitting_compound_assignment = true;
7037 if (has_await_arguments) {
7038 await_source_arg = new LocalTemporary (Type);
7039 await_source_arg.Store (ec);
7041 args = new Arguments (1);
7042 args.Add (new Argument (await_source_arg));
7045 temp = await_source_arg;
7048 has_await_arguments = false;
7053 ec.Emit (OpCodes.Dup);
7054 temp = new LocalTemporary (this.Type);
7059 args = arguments ?? new Arguments (1);
7063 temp = new LocalTemporary (this.Type);
7065 args.Add (new Argument (temp));
7067 args.Add (new Argument (source));
7071 emitting_compound_assignment = false;
7073 var call = new CallEmitter ();
7074 call.InstanceExpression = InstanceExpression;
7076 call.InstanceExpressionOnStack = true;
7078 if (ConditionalAccess) {
7079 call.ConditionalAccess = true;
7083 call.Emit (ec, Setter, args, loc);
7085 call.EmitStatement (ec, Setter, args, loc);
7092 if (await_source_arg != null) {
7093 await_source_arg.Release (ec);
7097 public override void FlowAnalysis (FlowAnalysisContext fc)
7099 var prop = best_candidate.MemberDefinition as Property;
7100 if (prop != null && prop.BackingField != null) {
7101 var var = InstanceExpression as IVariableReference;
7103 var vi = var.VariableInfo;
7104 if (vi != null && !fc.IsStructFieldDefinitelyAssigned (vi, prop.BackingField.Name)) {
7105 fc.Report.Error (8079, loc, "Use of possibly unassigned auto-implemented property `{0}'", Name);
7109 if (TypeSpec.IsValueType (InstanceExpression.Type) && InstanceExpression is VariableReference)
7114 var da = conditional_access_receiver ? fc.BranchDefiniteAssignment () : null;
7116 base.FlowAnalysis (fc);
7118 if (conditional_access_receiver)
7119 fc.DefiniteAssignment = da;
7122 protected override Expression OverloadResolve (ResolveContext rc, Expression right_side)
7124 eclass = ExprClass.PropertyAccess;
7126 if (best_candidate.IsNotCSharpCompatible) {
7127 Error_PropertyNotValid (rc);
7130 ResolveInstanceExpression (rc, right_side);
7132 if ((best_candidate.Modifiers & (Modifiers.ABSTRACT | Modifiers.VIRTUAL)) != 0 && best_candidate.DeclaringType != InstanceExpression.Type) {
7133 var filter = new MemberFilter (best_candidate.Name, 0, MemberKind.Property, null, null);
7134 var p = MemberCache.FindMember (InstanceExpression.Type, filter, BindingRestriction.InstanceOnly | BindingRestriction.OverrideOnly) as PropertySpec;
7136 type = p.MemberType;
7140 DoBestMemberChecks (rc, best_candidate);
7142 // Handling of com-imported properties with any number of default property parameters
7143 if (best_candidate.HasGet && !best_candidate.Get.Parameters.IsEmpty) {
7144 var p = best_candidate.Get.Parameters;
7145 arguments = new Arguments (p.Count);
7146 for (int i = 0; i < p.Count; ++i) {
7147 arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7149 } else if (best_candidate.HasSet && best_candidate.Set.Parameters.Count > 1) {
7150 var p = best_candidate.Set.Parameters;
7151 arguments = new Arguments (p.Count - 1);
7152 for (int i = 0; i < p.Count - 1; ++i) {
7153 arguments.Add (new Argument (OverloadResolver.ResolveDefaultValueArgument (rc, p.Types [i], p.FixedParameters [i].DefaultValue, loc)));
7160 protected override bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7162 if (!rc.HasSet (ResolveContext.Options.ConstructorScope))
7165 var prop = best_candidate.MemberDefinition as Property;
7166 if (prop == null || prop.Parent.PartialContainer != rc.CurrentMemberDefinition.Parent.PartialContainer) {
7167 var ps = MemberCache.FindMember (rc.CurrentType, MemberFilter.Property (best_candidate.Name, best_candidate.MemberType), BindingRestriction.DeclaredOnly) as PropertySpec;
7171 prop = (Property)ps.MemberDefinition;
7174 var spec = prop.BackingField;
7178 if (rc.IsStatic != spec.IsStatic)
7181 if (!spec.IsStatic && (!(InstanceExpression is This) || InstanceExpression is BaseThis))
7184 backing_field = new FieldExpr (prop.BackingField, loc);
7185 backing_field.ResolveLValue (rc, rhs);
7189 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
7191 if (!best_candidate.IsAccessible (rc))
7192 ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), expr.Location);
7194 best_candidate.CheckObsoleteness (rc, expr.Location);
7197 public void SetBackingFieldAssigned (FlowAnalysisContext fc)
7199 if (backing_field != null) {
7200 backing_field.SetFieldAssigned (fc);
7204 if (!IsAutoPropertyAccess)
7207 var prop = best_candidate.MemberDefinition as Property;
7208 if (prop != null && prop.BackingField != null) {
7209 bool lvalue_instance = best_candidate.DeclaringType.IsStruct;
7210 if (lvalue_instance) {
7211 var var = InstanceExpression as IVariableReference;
7212 if (var != null && var.VariableInfo != null) {
7213 fc.SetStructFieldAssigned (var.VariableInfo, prop.BackingField.Name);
7219 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7221 Error_TypeArgumentsCannotBeUsed (ec, "property", GetSignatureForError (), loc);
7225 abstract class PropertyOrIndexerExpr<T> : MemberExpr, IDynamicAssign where T : PropertySpec
7227 // getter and setter can be different for base calls
7228 MethodSpec getter, setter;
7229 protected T best_candidate;
7231 protected LocalTemporary temp;
7232 protected bool emitting_compound_assignment;
7233 protected bool has_await_arguments;
7235 protected PropertyOrIndexerExpr (Location l)
7242 protected abstract Arguments Arguments { get; set; }
7244 public MethodSpec Getter {
7253 public MethodSpec Setter {
7264 protected override Expression DoResolve (ResolveContext ec)
7266 if (eclass == ExprClass.Unresolved) {
7267 ResolveConditionalAccessReceiver (ec);
7269 var expr = OverloadResolve (ec, null);
7274 using (ec.With (ResolveContext.Options.DontSetConditionalAccessReceiver, conditional_access_receiver))
7275 return expr.Resolve (ec);
7278 if (conditional_access_receiver) {
7279 type = LiftMemberType (ec, type);
7283 if (!ResolveGetter (ec))
7289 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
7291 if (HasConditionalAccess ())
7292 Error_NullPropagatingLValue (rc);
7294 if (right_side == EmptyExpression.OutAccess) {
7295 // TODO: best_candidate can be null at this point
7296 INamedBlockVariable variable = null;
7297 if (best_candidate != null && rc.CurrentBlock.ParametersBlock.TopBlock.GetLocalName (best_candidate.Name, rc.CurrentBlock, ref variable) && variable is Linq.RangeVariable) {
7298 rc.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter",
7299 best_candidate.Name);
7301 right_side.DoResolveLValue (rc, this);
7306 if (eclass == ExprClass.Unresolved) {
7307 var expr = OverloadResolve (rc, right_side);
7312 return expr.ResolveLValue (rc, right_side);
7314 ResolveInstanceExpression (rc, right_side);
7317 if (!best_candidate.HasSet) {
7318 if (ResolveAutopropertyAssignment (rc, right_side))
7321 rc.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read-only)",
7322 GetSignatureForError ());
7326 if (!best_candidate.Set.IsAccessible (rc) || !best_candidate.Set.DeclaringType.IsAccessible (rc)) {
7327 if (best_candidate.HasDifferentAccessibility) {
7328 rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7329 rc.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible",
7330 GetSignatureForError ());
7332 rc.Report.SymbolRelatedToPreviousError (best_candidate.Set);
7333 ErrorIsInaccesible (rc, best_candidate.GetSignatureForError (), loc);
7337 if (best_candidate.HasDifferentAccessibility)
7338 CheckProtectedMemberAccess (rc, best_candidate.Set);
7340 setter = CandidateToBaseOverride (rc, best_candidate.Set);
7344 void EmitConditionalAccess (EmitContext ec, ref CallEmitter call, MethodSpec method, Arguments arguments)
7346 var ca = ec.ConditionalAccess;
7347 ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7349 call.Emit (ec, method, arguments, loc);
7351 ec.CloseConditionalAccess (method.ReturnType != type && type.IsNullableType ? type : null);
7352 ec.ConditionalAccess = ca;
7356 // Implements the IAssignMethod interface for assignments
7358 public virtual void Emit (EmitContext ec, bool leave_copy)
7360 var call = new CallEmitter ();
7361 call.ConditionalAccess = ConditionalAccess;
7362 call.InstanceExpression = InstanceExpression;
7363 if (has_await_arguments)
7364 call.HasAwaitArguments = true;
7366 call.DuplicateArguments = emitting_compound_assignment;
7368 if (conditional_access_receiver)
7369 EmitConditionalAccess (ec, ref call, Getter, Arguments);
7371 call.Emit (ec, Getter, Arguments, loc);
7373 if (call.HasAwaitArguments) {
7374 InstanceExpression = call.InstanceExpression;
7375 Arguments = call.EmittedArguments;
7376 has_await_arguments = true;
7380 ec.Emit (OpCodes.Dup);
7381 temp = new LocalTemporary (Type);
7386 public abstract void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound);
7388 public override void Emit (EmitContext ec)
7393 protected override FieldExpr EmitToFieldSource (EmitContext ec)
7395 has_await_arguments = true;
7400 public abstract SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
7402 protected abstract Expression OverloadResolve (ResolveContext rc, Expression right_side);
7404 bool ResolveGetter (ResolveContext rc)
7406 if (!best_candidate.HasGet) {
7407 if (InstanceExpression != EmptyExpression.Null) {
7408 rc.Report.SymbolRelatedToPreviousError (best_candidate);
7409 rc.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
7410 best_candidate.GetSignatureForError ());
7413 } else if (!best_candidate.Get.IsAccessible (rc) || !best_candidate.Get.DeclaringType.IsAccessible (rc)) {
7414 if (best_candidate.HasDifferentAccessibility) {
7415 rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7416 rc.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible",
7417 TypeManager.CSharpSignature (best_candidate));
7419 rc.Report.SymbolRelatedToPreviousError (best_candidate.Get);
7420 ErrorIsInaccesible (rc, best_candidate.Get.GetSignatureForError (), loc);
7424 if (best_candidate.HasDifferentAccessibility) {
7425 CheckProtectedMemberAccess (rc, best_candidate.Get);
7428 getter = CandidateToBaseOverride (rc, best_candidate.Get);
7432 protected virtual bool ResolveAutopropertyAssignment (ResolveContext rc, Expression rhs)
7439 /// Fully resolved expression that evaluates to an Event
7441 public class EventExpr : MemberExpr, IAssignMethod
7443 readonly EventSpec spec;
7446 public EventExpr (EventSpec spec, Location loc)
7454 protected override TypeSpec DeclaringType {
7456 return spec.DeclaringType;
7460 public override string Name {
7466 public override bool IsInstance {
7468 return !spec.IsStatic;
7472 public override bool IsStatic {
7474 return spec.IsStatic;
7478 public override string KindName {
7479 get { return "event"; }
7482 public MethodSpec Operator {
7490 public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, SimpleName original)
7493 // If the event is local to this class and we are not lhs of +=/-= we transform ourselves into a FieldExpr
7495 if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7496 if (spec.BackingField != null &&
7497 (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition))) {
7499 spec.MemberDefinition.SetIsUsed ();
7501 spec.CheckObsoleteness (ec, loc);
7503 if ((spec.Modifiers & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0)
7504 Error_AssignmentEventOnly (ec);
7506 FieldExpr ml = new FieldExpr (spec.BackingField, loc);
7508 InstanceExpression = null;
7510 return ml.ResolveMemberAccess (ec, left, original);
7514 return base.ResolveMemberAccess (ec, left, original);
7517 public override Expression CreateExpressionTree (ResolveContext ec)
7519 throw new NotSupportedException ("ET");
7522 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7524 if (right_side == EmptyExpression.EventAddition) {
7525 op = spec.AccessorAdd;
7526 } else if (right_side == EmptyExpression.EventSubtraction) {
7527 op = spec.AccessorRemove;
7531 Error_AssignmentEventOnly (ec);
7535 if (HasConditionalAccess ())
7536 Error_NullPropagatingLValue (ec);
7538 op = CandidateToBaseOverride (ec, op);
7542 protected override Expression DoResolve (ResolveContext ec)
7544 eclass = ExprClass.EventAccess;
7545 type = spec.MemberType;
7547 ResolveInstanceExpression (ec, null);
7549 if (!ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) {
7550 Error_AssignmentEventOnly (ec);
7553 DoBestMemberChecks (ec, spec);
7557 public override void Emit (EmitContext ec)
7559 throw new NotSupportedException ();
7560 //Error_CannotAssign ();
7563 #region IAssignMethod Members
7565 public void Emit (EmitContext ec, bool leave_copy)
7567 throw new NotImplementedException ();
7570 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
7572 if (leave_copy || !isCompound)
7573 throw new NotImplementedException ("EventExpr::EmitAssign");
7575 Arguments args = new Arguments (1);
7576 args.Add (new Argument (source));
7578 // TODO: Wrong, needs receiver
7579 // if (NullShortCircuit) {
7580 // ec.ConditionalAccess = new ConditionalAccessContext (type, ec.DefineLabel ());
7583 var call = new CallEmitter ();
7584 call.InstanceExpression = InstanceExpression;
7585 call.ConditionalAccess = ConditionalAccess;
7586 call.EmitStatement (ec, op, args, loc);
7588 // if (NullShortCircuit)
7589 // ec.CloseConditionalAccess (null);
7594 void Error_AssignmentEventOnly (ResolveContext ec)
7596 if (spec.DeclaringType == ec.CurrentType || TypeManager.IsNestedChildOf (ec.CurrentType, spec.DeclaringType.MemberDefinition)) {
7597 ec.Report.Error (79, loc,
7598 "The event `{0}' can only appear on the left hand side of `+=' or `-=' operator",
7599 GetSignatureForError ());
7601 ec.Report.Error (70, loc,
7602 "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'",
7603 GetSignatureForError (), spec.DeclaringType.GetSignatureForError ());
7607 protected override void Error_CannotCallAbstractBase (ResolveContext rc, string name)
7609 name = name.Substring (0, name.LastIndexOf ('.'));
7610 base.Error_CannotCallAbstractBase (rc, name);
7613 public override string GetSignatureForError ()
7615 return TypeManager.CSharpSignature (spec);
7618 public override void ResolveNameOf (ResolveContext rc, ATypeNameExpression expr)
7620 spec.CheckObsoleteness (rc, expr.Location);
7623 public override void SetTypeArguments (ResolveContext ec, TypeArguments ta)
7625 Error_TypeArgumentsCannotBeUsed (ec, "event", GetSignatureForError (), loc);
7629 public class TemporaryVariableReference : VariableReference
7631 public class Declarator : Statement
7633 TemporaryVariableReference variable;
7635 public Declarator (TemporaryVariableReference variable)
7637 this.variable = variable;
7641 protected override void DoEmit (EmitContext ec)
7643 variable.li.CreateBuilder (ec);
7646 public override void Emit (EmitContext ec)
7648 // Don't create sequence point
7652 protected override bool DoFlowAnalysis (FlowAnalysisContext fc)
7657 protected override void CloneTo (CloneContext clonectx, Statement target)
7665 public TemporaryVariableReference (LocalVariable li, Location loc)
7668 this.type = li.Type;
7672 public override bool IsLockedByStatement {
7680 public LocalVariable LocalInfo {
7686 public static TemporaryVariableReference Create (TypeSpec type, Block block, Location loc, bool writeToSymbolFile = false)
7688 var li = LocalVariable.CreateCompilerGenerated (type, block, loc, writeToSymbolFile);
7689 return new TemporaryVariableReference (li, loc);
7692 protected override Expression DoResolve (ResolveContext ec)
7694 eclass = ExprClass.Variable;
7697 // Don't capture temporary variables except when using
7698 // state machine redirection and block yields
7700 if (ec.CurrentAnonymousMethod is StateMachineInitializer &&
7701 (ec.CurrentBlock.Explicit.HasYield || ec.CurrentBlock.Explicit.HasAwait) &&
7702 ec.IsVariableCapturingRequired) {
7703 AnonymousMethodStorey storey = li.Block.Explicit.CreateAnonymousMethodStorey (ec);
7704 storey.CaptureLocalVariable (ec, li);
7710 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
7712 return Resolve (ec);
7715 public override void Emit (EmitContext ec)
7717 li.CreateBuilder (ec);
7722 public void EmitAssign (EmitContext ec, Expression source)
7724 li.CreateBuilder (ec);
7726 EmitAssign (ec, source, false, false);
7729 public override HoistedVariable GetHoistedVariable (AnonymousExpression ae)
7731 return li.HoistedVariant;
7734 public override bool IsFixed {
7735 get { return true; }
7738 public override bool IsRef {
7739 get { return false; }
7742 public override string Name {
7743 get { throw new NotImplementedException (); }
7746 public override void SetHasAddressTaken ()
7748 throw new NotImplementedException ();
7751 protected override ILocalVariable Variable {
7755 public override VariableInfo VariableInfo {
7756 get { return null; }
7761 /// Handles `var' contextual keyword; var becomes a keyword only
7762 /// if no type called var exists in a variable scope
7764 class VarExpr : SimpleName
7766 public VarExpr (Location loc)
7771 public bool InferType (ResolveContext ec, Expression right_side)
7774 throw new InternalErrorException ("An implicitly typed local variable could not be redefined");
7776 type = right_side.Type;
7777 if (type == InternalType.NullLiteral || type.Kind == MemberKind.Void || type == InternalType.AnonymousMethod || type == InternalType.MethodGroup) {
7778 ec.Report.Error (815, loc,
7779 "An implicitly typed local variable declaration cannot be initialized with `{0}'",
7780 type.GetSignatureForError ());
7781 type = InternalType.ErrorType;
7785 eclass = ExprClass.Variable;
7789 protected override void Error_TypeOrNamespaceNotFound (IMemberContext ec)
7791 if (ec.Module.Compiler.Settings.Version < LanguageVersion.V_3)
7792 base.Error_TypeOrNamespaceNotFound (ec);
7794 ec.Module.Compiler.Report.Error (825, loc, "The contextual keyword `var' may only appear within a local variable declaration");