X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Fecore.cs;h=336bd0be58b5ac78caaaeb36840a2634eedac569;hb=3e476138366f3dd57de9c1c65961adb3e1321e31;hp=9f19b28015e32bfd8c27a7b1b353ef6a1568f207;hpb=639e0b6e13a5989392936e7a2f2089694ada2ae1;p=mono.git diff --git a/mcs/mcs/ecore.cs b/mcs/mcs/ecore.cs index 9f19b28015e..336bd0be58b 100644 --- a/mcs/mcs/ecore.cs +++ b/mcs/mcs/ecore.cs @@ -10,18 +10,16 @@ // // +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; +using System.Reflection.Emit; +using System.Text; +using SLE = System.Linq.Expressions; +using System.Linq; + namespace Mono.CSharp { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Diagnostics; - using System.Reflection; - using System.Reflection.Emit; - using System.Text; - -#if NET_4_0 - using SLE = System.Linq.Expressions; -#endif /// /// The ExprClass class contains the is used to pass the @@ -151,28 +149,26 @@ namespace Mono.CSharp { return TypeManager.CSharpName (type); } - public static bool IsAccessorAccessible (Type invocation_type, MethodInfo mi, out bool must_do_cs1540_check) + public static bool IsAccessorAccessible (Type invocation_type, MethodSpec mi, out bool must_do_cs1540_check) { - MethodAttributes ma = mi.Attributes & MethodAttributes.MemberAccessMask; + var ma = mi.Modifiers & Modifiers.AccessibilityMask; must_do_cs1540_check = false; // by default we do not check for this - if (ma == MethodAttributes.Public) + if (ma == Modifiers.PUBLIC) return true; // // If only accessible to the current class or children // - if (ma == MethodAttributes.Private) + if (ma == Modifiers.PRIVATE) return TypeManager.IsPrivateAccessible (invocation_type, mi.DeclaringType) || TypeManager.IsNestedChildOf (invocation_type, mi.DeclaringType); - if (TypeManager.IsThisOrFriendAssembly (invocation_type.Assembly, mi.DeclaringType.Assembly)) { - if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem) - return true; - } else { - if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem) - return false; + if ((ma & Modifiers.INTERNAL) != 0) { + var b = TypeManager.IsThisOrFriendAssembly (invocation_type.Assembly, mi.DeclaringType.Assembly); + if (b || ma == Modifiers.INTERNAL) + return b; } // Family and FamANDAssem require that we derive. @@ -333,6 +329,17 @@ namespace Mono.CSharp { } + public void Error_ExpressionMustBeConstant (ResolveContext rc, Location loc, string e_name) + { + rc.Report.Error (133, loc, "The expression being assigned to `{0}' must be constant", e_name); + } + + public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, Type type, Location loc, string name) + { + rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null", + name, TypeManager.CSharpName (type)); + } + public static void Error_InvalidExpressionStatement (Report Report, Location loc) { Report.Error (201, loc, "Only assignment, call, increment, decrement, and new object " + @@ -506,24 +513,6 @@ namespace Mono.CSharp { return Resolve (rc, ResolveFlags.VariableOrValue | ResolveFlags.MethodGroup); } - public Constant ResolveAsConstant (ResolveContext ec, MemberCore mc) - { - Expression e = Resolve (ec); - if (e == null) - return null; - - Constant c = e as Constant; - if (c != null) - return c; - - if (type != null && TypeManager.IsReferenceType (type)) - Const.Error_ConstantCanBeInitializedWithNullOnly (type, loc, mc.GetSignatureForError (), ec.Report); - else - Const.Error_ExpressionMustBeConstant (loc, mc.GetSignatureForError (), ec.Report); - - return null; - } - /// /// Resolves an expression for LValue assignment /// @@ -612,14 +601,15 @@ namespace Mono.CSharp { public static Expression ExprClassFromMemberInfo (Type container_type, MemberInfo mi, Location loc) { if (mi is EventInfo) - return new EventExpr ((EventInfo) mi, loc); + return new EventExpr (Import.CreateEvent ((EventInfo) mi), loc); else if (mi is FieldInfo) { FieldInfo fi = (FieldInfo) mi; - if (fi.IsLiteral || (fi.IsInitOnly && fi.FieldType == TypeManager.decimal_type)) - return new ConstantExpr (fi, loc); - return new FieldExpr (fi, loc); + var spec = Import.CreateField (fi); + if (spec is ConstSpec) + return new ConstantExpr ((ConstSpec) spec, loc); + return new FieldExpr (spec, loc); } else if (mi is PropertyInfo) - return new PropertyExpr (container_type, (PropertyInfo) mi, loc); + return new PropertyExpr (container_type, Import.CreateProperty ((PropertyInfo) mi), loc); else if (mi is Type) { return new TypeExpression ((System.Type) mi, loc); } @@ -684,21 +674,21 @@ namespace Mono.CSharp { if (mi.Length > 1) { bool is_interface = qualifier_type != null && qualifier_type.IsInterface; - ArrayList methods = new ArrayList (2); - ArrayList non_methods = null; + var methods = new List (2); + List non_methods = null; - foreach (MemberInfo m in mi) { + foreach (var m in mi) { if (m is MethodBase) { - methods.Add (m); + methods.Add (Import.CreateMethod ((MethodBase) m)); continue; } if (non_methods == null) - non_methods = new ArrayList (2); + non_methods = new List (2); bool is_candidate = true; for (int i = 0; i < non_methods.Count; ++i) { - MemberInfo n_m = (MemberInfo) non_methods [i]; + MemberInfo n_m = non_methods [i]; if (n_m.DeclaringType.IsInterface && TypeManager.ImplementsInterface (m.DeclaringType, n_m.DeclaringType)) { non_methods.Remove (n_m); --i; @@ -714,11 +704,11 @@ namespace Mono.CSharp { } if (methods.Count == 0 && non_methods != null && non_methods.Count > 1) { - ctx.Report.SymbolRelatedToPreviousError ((MemberInfo)non_methods [1]); - ctx.Report.SymbolRelatedToPreviousError ((MemberInfo)non_methods [0]); + ctx.Report.SymbolRelatedToPreviousError (non_methods [1]); + ctx.Report.SymbolRelatedToPreviousError (non_methods [0]); ctx.Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'", - TypeManager.GetFullNameSignature ((MemberInfo)non_methods [1]), - TypeManager.GetFullNameSignature ((MemberInfo)non_methods [0])); + TypeManager.GetFullNameSignature (non_methods [1]), + TypeManager.GetFullNameSignature (non_methods [0])); return null; } @@ -726,23 +716,23 @@ namespace Mono.CSharp { return ExprClassFromMemberInfo (container_type, (MemberInfo)non_methods [0], loc); if (non_methods != null && non_methods.Count > 0) { - MethodBase method = (MethodBase) methods [0]; + var method = methods [0]; MemberInfo non_method = (MemberInfo) non_methods [0]; if (method.DeclaringType == non_method.DeclaringType) { // Cannot happen with C# code, but is valid in IL - ctx.Report.SymbolRelatedToPreviousError (method); + ctx.Report.SymbolRelatedToPreviousError (method.MetaInfo); ctx.Report.SymbolRelatedToPreviousError (non_method); ctx.Report.Error (229, loc, "Ambiguity between `{0}' and `{1}'", TypeManager.GetFullNameSignature (non_method), - TypeManager.CSharpSignature (method)); + TypeManager.CSharpSignature (method.MetaInfo)); return null; } if (is_interface) { - ctx.Report.SymbolRelatedToPreviousError (method); + ctx.Report.SymbolRelatedToPreviousError (method.MetaInfo); ctx.Report.SymbolRelatedToPreviousError (non_method); ctx.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and non-method `{1}'. Using method `{0}'", - TypeManager.CSharpSignature (method), TypeManager.GetFullNameSignature (non_method)); + TypeManager.CSharpSignature (method.MetaInfo), TypeManager.GetFullNameSignature (non_method)); } } @@ -750,7 +740,7 @@ namespace Mono.CSharp { } if (mi [0] is MethodBase) - return new MethodGroupExpr (mi, queried_type, loc); + return new MethodGroupExpr (mi.Select (l => Import.CreateMethod ((MethodBase) l)).ToArray (), queried_type, loc); return ExprClassFromMemberInfo (container_type, mi [0], loc); } @@ -887,13 +877,16 @@ namespace Mono.CSharp { protected virtual Expression Error_MemberLookupFailed (ResolveContext ec, Type type, MemberInfo[] members) { + List methods = new List (); for (int i = 0; i < members.Length; ++i) { if (!(members [i] is MethodBase)) return null; + + methods.Add (Import.CreateMethod (members[i] as MethodBase)); } // By default propagate the closest candidates upwards - return new MethodGroupExpr (members, type, loc, true); + return new MethodGroupExpr (methods.ToArray (), type, loc, true); } protected virtual void Error_NegativeArrayIndex (ResolveContext ec, Location loc) @@ -1254,7 +1247,7 @@ namespace Mono.CSharp { { TypeExpr texpr = TypeManager.expression_type_expr; if (texpr == null) { - Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Linq.Expressions", "Expression", Kind.Class, true); + Type t = TypeManager.CoreLookupType (ec.Compiler, "System.Linq.Expressions", "Expression", MemberKind.Class, true); if (t == null) return null; @@ -1264,7 +1257,6 @@ namespace Mono.CSharp { return texpr; } -#if NET_4_0 // // Implemented by all expressions which support conversion from // compiler expression to invokable runtime expression. Used by @@ -1274,7 +1266,6 @@ namespace Mono.CSharp { { throw new NotImplementedException ("MakeExpression for " + GetType ()); } -#endif public virtual void MutateHoistedGenericType (AnonymousMethodStorey storey) { @@ -1372,14 +1363,12 @@ namespace Mono.CSharp { return child.GetAttributableValue (ec, value_type, out value); } -#if NET_4_0 public override SLE.Expression MakeExpression (BuilderContext ctx) { return ctx.HasSet (BuilderContext.Options.CheckedScope) ? SLE.Expression.ConvertChecked (child.MakeExpression (ctx), type) : SLE.Expression.Convert (child.MakeExpression (ctx), type); } -#endif public override void MutateHoistedGenericType (AnonymousMethodStorey storey) { @@ -1499,7 +1488,7 @@ namespace Mono.CSharp { /// public class CastFromDecimal : TypeCast { - static IDictionary operators; + static Dictionary operators; public CastFromDecimal (Expression child, Type return_type) : base (child, return_type) @@ -1514,11 +1503,11 @@ namespace Mono.CSharp { public Expression Resolve () { if (operators == null) { - MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type, - TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method, - BindingFlags.Static | BindingFlags.Public, "op_Explicit", null); + MemberInfo[] all_oper = TypeManager.MemberLookup (TypeManager.decimal_type, + TypeManager.decimal_type, TypeManager.decimal_type, MemberTypes.Method, + BindingFlags.Static | BindingFlags.Public, "op_Explicit", null); - operators = new System.Collections.Specialized.HybridDictionary (); + operators = new Dictionary (ReferenceEquality.Default); foreach (MethodInfo oper in all_oper) { AParametersCollection pd = TypeManager.GetParameterData (oper); if (pd.Types [0] == TypeManager.decimal_type) @@ -1526,7 +1515,7 @@ namespace Mono.CSharp { } } - return operators.Contains (type) ? this : null; + return operators.ContainsKey (type) ? this : null; } public override void Emit (EmitContext ec) @@ -1534,7 +1523,7 @@ namespace Mono.CSharp { ILGenerator ig = ec.ig; child.Emit (ec); - ig.Emit (OpCodes.Call, (MethodInfo)operators [type]); + ig.Emit (OpCodes.Call, operators [type]); } } @@ -1601,6 +1590,10 @@ namespace Mono.CSharp { public override bool IsNull { get { return child.IsNull; } } + + public override bool IsOneInteger { + get { return child.IsOneInteger; } + } public override bool IsZeroInteger { get { return child.IsZeroInteger; } @@ -2305,12 +2298,10 @@ namespace Mono.CSharp { expr.EmitBranchable (ec, target, on_true); } -#if NET_4_0 public override SLE.Expression MakeExpression (BuilderContext ctx) { return orig_expr.MakeExpression (ctx); } -#endif public override void MutateHoistedGenericType (AnonymousMethodStorey storey) { @@ -2845,7 +2836,7 @@ namespace Mono.CSharp { if (RootContext.EvalMode){ FieldInfo fi = Evaluator.LookupField (Name); if (fi != null) - return new FieldExpr (fi, loc).Resolve (ec); + return new FieldExpr (Import.CreateField (fi), loc).Resolve (ec); } if (almost_matched != null) @@ -3261,7 +3252,7 @@ namespace Mono.CSharp { public Expression ExtensionExpression; Argument extension_argument; - public ExtensionMethodGroupExpr (ArrayList list, NamespaceEntry n, Type extensionType, Location l) + public ExtensionMethodGroupExpr (List list, NamespaceEntry n, Type extensionType, Location l) : base (list, extensionType, l) { this.namespace_entry = n; @@ -3327,13 +3318,13 @@ namespace Mono.CSharp { { public interface IErrorHandler { - bool AmbiguousCall (ResolveContext ec, MethodBase ambiguous); - bool NoExactMatch (ResolveContext ec, MethodBase method); + bool AmbiguousCall (ResolveContext ec, MethodSpec ambiguous); + bool NoExactMatch (ResolveContext ec, MethodSpec method); } - public IErrorHandler CustomErrorHandler; - public MethodBase [] Methods; - MethodBase best_candidate; + public IErrorHandler CustomErrorHandler; + public MethodSpec [] Methods; + MethodSpec best_candidate; // TODO: make private public TypeArguments type_arguments; bool identical_type_name; @@ -3341,31 +3332,31 @@ namespace Mono.CSharp { Type delegate_type; Type queried_type; - public MethodGroupExpr (MemberInfo [] mi, Type type, Location l) + public MethodGroupExpr (MethodSpec [] mi, Type type, Location l) : this (type, l) { - Methods = new MethodBase [mi.Length]; + Methods = new MethodSpec[mi.Length]; mi.CopyTo (Methods, 0); } - public MethodGroupExpr (MemberInfo[] mi, Type type, Location l, bool inacessibleCandidatesOnly) + public MethodGroupExpr (MethodSpec[] mi, Type type, Location l, bool inacessibleCandidatesOnly) : this (mi, type, l) { has_inaccessible_candidates_only = inacessibleCandidatesOnly; } - public MethodGroupExpr (ArrayList list, Type type, Location l) + public MethodGroupExpr (List list, Type type, Location l) : this (type, l) { try { - Methods = (MethodBase[])list.ToArray (typeof (MethodBase)); + Methods = list.ToArray (); } catch { - foreach (MemberInfo m in list){ - if (!(m is MethodBase)){ - Console.WriteLine ("Name " + m.Name); - Console.WriteLine ("Found a: " + m.GetType ().FullName); - } - } + //foreach (MemberInfo m in list){ + // if (!(m is MethodBase)){ + // Console.WriteLine ("Name " + m.Name); + // Console.WriteLine ("Found a: " + m.GetType ().FullName); + // } + //} throw; } @@ -3386,6 +3377,12 @@ namespace Mono.CSharp { } } + public MethodSpec BestCandidate { + get { + return best_candidate; + } + } + public Type DelegateType { set { delegate_type = value; @@ -3401,9 +3398,9 @@ namespace Mono.CSharp { public override string GetSignatureForError () { if (best_candidate != null) - return TypeManager.CSharpSignature (best_candidate); + return TypeManager.CSharpSignature (best_candidate.MetaInfo); - return TypeManager.CSharpSignature (Methods [0]); + return TypeManager.CSharpSignature (Methods [0].MetaInfo); } public override string Name { @@ -3417,7 +3414,7 @@ namespace Mono.CSharp { if (best_candidate != null) return !best_candidate.IsStatic; - foreach (MethodBase mb in Methods) + foreach (var mb in Methods) if (!mb.IsStatic) return true; @@ -3430,22 +3427,17 @@ namespace Mono.CSharp { if (best_candidate != null) return best_candidate.IsStatic; - foreach (MethodBase mb in Methods) + foreach (var mb in Methods) if (mb.IsStatic) return true; return false; } } - - public static explicit operator ConstructorInfo (MethodGroupExpr mg) - { - return (ConstructorInfo)mg.best_candidate; - } - public static explicit operator MethodInfo (MethodGroupExpr mg) + public static explicit operator MethodSpec (MethodGroupExpr mg) { - return (MethodInfo)mg.best_candidate; + return mg.best_candidate; } // @@ -3557,11 +3549,11 @@ namespace Mono.CSharp { /// true if candidate is better than the current best match /// static bool BetterFunction (ResolveContext ec, Arguments args, int argument_count, - MethodBase candidate, bool candidate_params, - MethodBase best, bool best_params) + MethodSpec candidate, bool candidate_params, + MethodSpec best, bool best_params) { - AParametersCollection candidate_pd = TypeManager.GetParameterData (candidate); - AParametersCollection best_pd = TypeManager.GetParameterData (best); + AParametersCollection candidate_pd = candidate.Parameters; + AParametersCollection best_pd = best.Parameters; bool better_at_least_one = false; bool same = true; @@ -3587,8 +3579,8 @@ namespace Mono.CSharp { bt = TypeManager.GetElementType (bt); --b_idx; } - - if (ct == bt) + + if (TypeManager.IsEqual (ct, bt)) continue; same = false; @@ -3623,10 +3615,10 @@ namespace Mono.CSharp { // // The two methods have equal parameter types. Now apply tie-breaking rules // - if (TypeManager.IsGenericMethod (best)) { - if (!TypeManager.IsGenericMethod (candidate)) + if (best.IsGenericMethod) { + if (!candidate.IsGenericMethod) return true; - } else if (TypeManager.IsGenericMethod (candidate)) { + } else if (candidate.IsGenericMethod) { return false; } @@ -3714,7 +3706,7 @@ namespace Mono.CSharp { return null; } - IMethodData md = TypeManager.GetMethod (best_candidate); + IMethodData md = TypeManager.GetMethod (best_candidate.MetaInfo); if (md != null && md.IsExcluded ()) ec.Report.Error (765, loc, "Partial methods with only a defining declaration or removed conditional methods cannot be used in an expression tree"); @@ -3752,23 +3744,23 @@ namespace Mono.CSharp { Invocation.EmitCall (ec, IsBase, InstanceExpression, best_candidate, arguments, loc); } - void Error_AmbiguousCall (ResolveContext ec, MethodBase ambiguous) + void Error_AmbiguousCall (ResolveContext ec, MethodSpec ambiguous) { if (CustomErrorHandler != null && CustomErrorHandler.AmbiguousCall (ec, ambiguous)) return; - ec.Report.SymbolRelatedToPreviousError (best_candidate); + ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo); ec.Report.Error (121, loc, "The call is ambiguous between the following methods or properties: `{0}' and `{1}'", - TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (best_candidate)); + TypeManager.CSharpSignature (ambiguous), TypeManager.CSharpSignature (best_candidate.MetaInfo)); } - protected virtual void Error_InvalidArguments (ResolveContext ec, Location loc, int idx, MethodBase method, + protected virtual void Error_InvalidArguments (ResolveContext ec, Location loc, int idx, MethodSpec method, Argument a, AParametersCollection expected_par, Type paramType) { ExtensionMethodGroupExpr emg = this as ExtensionMethodGroupExpr; if (a is CollectionElementInitializer.ElementInitializerArgument) { - ec.Report.SymbolRelatedToPreviousError (method); + ec.Report.SymbolRelatedToPreviousError (method.MetaInfo); if ((expected_par.FixedParameters [idx].ModFlags & Parameter.Modifier.ISBYREF) != 0) { ec.Report.Error (1954, loc, "The best overloaded collection initalizer method `{0}' cannot have 'ref', or `out' modifier", TypeManager.CSharpSignature (method)); @@ -3780,7 +3772,7 @@ namespace Mono.CSharp { ec.Report.Error (1594, loc, "Delegate `{0}' has some invalid arguments", TypeManager.CSharpName (method.DeclaringType)); } else { - ec.Report.SymbolRelatedToPreviousError (method); + ec.Report.SymbolRelatedToPreviousError (method.MetaInfo); if (emg != null) { ec.Report.Error (1928, loc, "Type `{0}' does not contain a member `{1}' and the best extension method overload `{2}' has some invalid arguments", @@ -3835,7 +3827,7 @@ namespace Mono.CSharp { Name, arg_count.ToString ()); } - protected virtual int GetApplicableParametersCount (MethodBase method, AParametersCollection parameters) + protected virtual int GetApplicableParametersCount (MethodSpec method, AParametersCollection parameters) { return parameters.Count; } @@ -3854,11 +3846,11 @@ namespace Mono.CSharp { /// 0 = the best, int.MaxValue = the worst /// public int IsApplicable (ResolveContext ec, - ref Arguments arguments, int arg_count, ref MethodBase method, ref bool params_expanded_form) + ref Arguments arguments, int arg_count, ref MethodSpec method, ref bool params_expanded_form) { - MethodBase candidate = method; + var candidate = method; - AParametersCollection pd = TypeManager.GetParameterData (candidate); + AParametersCollection pd = candidate.Parameters; int param_count = GetApplicableParametersCount (candidate, pd); int optional_count = 0; @@ -3870,7 +3862,7 @@ namespace Mono.CSharp { } } - int args_gap = Math.Abs (arg_count - param_count); + int args_gap = System.Math.Abs (arg_count - param_count); if (optional_count != 0) { if (args_gap > optional_count) return int.MaxValue - 10000 + args_gap - optional_count; @@ -3956,26 +3948,25 @@ namespace Mono.CSharp { // // 1. Handle generic method using type arguments when specified or type inference // - if (TypeManager.IsGenericMethod (candidate)) { + if (candidate.IsGenericMethod) { if (type_arguments != null) { Type [] g_args = candidate.GetGenericArguments (); if (g_args.Length != type_arguments.Count) - return int.MaxValue - 20000 + Math.Abs (type_arguments.Count - g_args.Length); + return int.MaxValue - 20000 + System.Math.Abs (type_arguments.Count - g_args.Length); - // TODO: Don't create new method, create Parameters only - method = ((MethodInfo) candidate).MakeGenericMethod (type_arguments.Arguments); + method = candidate.Inflate (type_arguments.Arguments); candidate = method; - pd = TypeManager.GetParameterData (candidate); + pd = candidate.Parameters; } else { int score = TypeManager.InferTypeArguments (ec, arguments, ref candidate); if (score != 0) return score - 20000; - if (TypeManager.IsGenericMethodDefinition (candidate)) + if (TypeManager.IsGenericMethodDefinition (candidate.MetaInfo)) throw new InternalErrorException ("A generic method `{0}' definition took part in overload resolution", - TypeManager.CSharpSignature (candidate)); + TypeManager.CSharpSignature (candidate.MetaInfo)); - pd = TypeManager.GetParameterData (candidate); + pd = candidate.Parameters; } } else { if (type_arguments != null) @@ -4103,10 +4094,10 @@ namespace Mono.CSharp { if (mg2 == null) return mg1; - - ArrayList all = new ArrayList (mg1.Methods); - foreach (MethodBase m in mg2.Methods){ - if (!TypeManager.ArrayContainsMethod (mg1.Methods, m, false)) + + var all = new List (mg1.Methods); + foreach (var m in mg2.Methods){ + if (!TypeManager.ArrayContainsMethod (mg1.Methods.Select (l => l.MetaInfo).ToArray (), m.MetaInfo, false)) all.Add (m); } @@ -4160,13 +4151,11 @@ namespace Mono.CSharp { { base.MutateHoistedGenericType (storey); - MethodInfo mi = best_candidate as MethodInfo; - if (mi != null) { - best_candidate = storey.MutateGenericMethod (mi); - return; + if (best_candidate.IsConstructor) { + storey.MutateConstructor (best_candidate); + } else { + storey.MutateGenericMethod (best_candidate); } - - best_candidate = storey.MutateConstructor ((ConstructorInfo) this); } /// @@ -4190,8 +4179,8 @@ namespace Mono.CSharp { { bool method_params = false; Type applicable_type = null; - ArrayList candidates = new ArrayList (2); - ArrayList candidate_overrides = null; + var candidates = new List (2); + List candidate_overrides = null; // // Used to keep a map between the candidate @@ -4200,8 +4189,8 @@ namespace Mono.CSharp { // // false is normal form, true is expanded form // - Dictionary candidate_to_form = null; - Dictionary candidates_expanded = null; + Dictionary candidate_to_form = null; + Dictionary candidates_expanded = null; Arguments candidate_args = Arguments; int arg_count = Arguments != null ? Arguments.Count : 0; @@ -4226,16 +4215,20 @@ namespace Mono.CSharp { // with non-C# libraries which change the visibility of overrides (#75636) // int j = 0; + MethodBase mb = null; for (int i = 0; i < Methods.Length; ++i) { - MethodBase m = Methods [i]; + var m = Methods [i]; + mb = m.MetaInfo; if (TypeManager.IsOverride (m)) { if (candidate_overrides == null) - candidate_overrides = new ArrayList (); + candidate_overrides = new List (); candidate_overrides.Add (m); - m = TypeManager.TryGetBaseDefinition (m); + mb = TypeManager.TryGetBaseDefinition (mb); + if (mb != null && Array.Exists (Methods, l => l.MetaInfo == mb)) + continue; } - if (m != null) - Methods [j++] = m; + if (mb != null) + Methods [j++] = Import.CreateMethod (mb); } nmethods = j; } @@ -4274,14 +4267,14 @@ namespace Mono.CSharp { if (params_expanded_form) { if (candidate_to_form == null) - candidate_to_form = new Dictionary (4, ReferenceEquality.Default); - MethodBase candidate = Methods [i]; + candidate_to_form = new Dictionary (4, ReferenceEquality.Default); + var candidate = Methods [i]; candidate_to_form [candidate] = candidate; } if (candidate_args != Arguments) { if (candidates_expanded == null) - candidates_expanded = new Dictionary (4, ReferenceEquality.Default); + candidates_expanded = new Dictionary (4, ReferenceEquality.Default); candidates_expanded.Add (Methods [i], candidate_args); candidate_args = Arguments; @@ -4386,7 +4379,7 @@ namespace Mono.CSharp { int j = finalized; // where to put the next finalized candidate int k = finalized; // where to put the next undiscarded candidate for (int i = finalized; i < candidate_top; ++i) { - MethodBase candidate = (MethodBase) candidates [i]; + var candidate = candidates [i]; Type decl_type = candidate.DeclaringType; if (decl_type == applicable_type) { @@ -4419,7 +4412,7 @@ namespace Mono.CSharp { // Now we actually find the best method // - best_candidate = (MethodBase) candidates [0]; + best_candidate = candidates [0]; method_params = candidate_to_form != null && candidate_to_form.ContainsKey (best_candidate); // @@ -4432,9 +4425,9 @@ namespace Mono.CSharp { } for (int ix = 1; ix < candidate_top; ix++) { - MethodBase candidate = (MethodBase) candidates [ix]; + var candidate = candidates [ix]; - if (candidate == best_candidate) + if (candidate.MetaInfo == best_candidate.MetaInfo) continue; bool cand_params = candidate_to_form != null && candidate_to_form.ContainsKey (candidate); @@ -4450,11 +4443,11 @@ namespace Mono.CSharp { // Now check that there are no ambiguities i.e the selected method // should be better than all the others // - MethodBase ambiguous = null; + MethodSpec ambiguous = null; for (int ix = 1; ix < candidate_top; ix++) { - MethodBase candidate = (MethodBase) candidates [ix]; + var candidate = candidates [ix]; - if (candidate == best_candidate) + if (candidate.MetaInfo == best_candidate.MetaInfo) continue; bool cand_params = candidate_to_form != null && candidate_to_form.ContainsKey (candidate); @@ -4463,7 +4456,7 @@ namespace Mono.CSharp { candidate, cand_params)) { if (!may_fail) - ec.Report.SymbolRelatedToPreviousError (candidate); + ec.Report.SymbolRelatedToPreviousError (candidate.MetaInfo); ambiguous = candidate; } } @@ -4484,29 +4477,29 @@ namespace Mono.CSharp { if (candidate_overrides != null) { Type[] gen_args = null; bool gen_override = false; - if (TypeManager.IsGenericMethod (best_candidate)) - gen_args = TypeManager.GetGenericArguments (best_candidate); + if (best_candidate.IsGenericMethod) + gen_args = TypeManager.GetGenericArguments (best_candidate.MetaInfo); - foreach (MethodBase candidate in candidate_overrides) { - if (TypeManager.IsGenericMethod (candidate)) { + foreach (var candidate in candidate_overrides) { + if (candidate.IsGenericMethod) { if (gen_args == null) continue; - if (gen_args.Length != TypeManager.GetGenericArguments (candidate).Length) + if (gen_args.Length != TypeManager.GetGenericArguments (candidate.MetaInfo).Length) continue; } else { if (gen_args != null) continue; } - if (IsOverride (candidate, best_candidate)) { + if (IsOverride (candidate.MetaInfo, best_candidate.MetaInfo)) { gen_override = true; best_candidate = candidate; } } if (gen_override && gen_args != null) { - best_candidate = ((MethodInfo) best_candidate).MakeGenericMethod (gen_args); + best_candidate = best_candidate.Inflate (gen_args); } } } @@ -4526,7 +4519,7 @@ namespace Mono.CSharp { MethodBase the_method = TypeManager.DropGenericMethodArguments (best_candidate); if (TypeManager.IsGenericMethodDefinition (the_method) && - !ConstraintChecker.CheckConstraints (ec, the_method, best_candidate, loc)) + !ConstraintChecker.CheckConstraints (ec, the_method, best_candidate.MetaInfo, loc)) return null; // @@ -4538,37 +4531,37 @@ namespace Mono.CSharp { IMethodData data = TypeManager.GetMethod (the_method); if (data != null) - data.SetMemberIsUsed (); + data.SetIsUsed (); Arguments = candidate_args; return this; } - bool NoExactMatch (ResolveContext ec, ref Arguments Arguments, IDictionary candidate_to_form) + bool NoExactMatch (ResolveContext ec, ref Arguments Arguments, IDictionary candidate_to_form) { - AParametersCollection pd = TypeManager.GetParameterData (best_candidate); + AParametersCollection pd = best_candidate.Parameters; int arg_count = Arguments == null ? 0 : Arguments.Count; if (arg_count == pd.Count || pd.HasParams) { - if (TypeManager.IsGenericMethodDefinition (best_candidate)) { + if (TypeManager.IsGenericMethodDefinition (best_candidate.MetaInfo)) { if (type_arguments == null) { ec.Report.Error (411, loc, "The type arguments for method `{0}' cannot be inferred from " + "the usage. Try specifying the type arguments explicitly", - TypeManager.CSharpSignature (best_candidate)); + TypeManager.CSharpSignature (best_candidate.MetaInfo)); return true; } - Type[] g_args = TypeManager.GetGenericArguments (best_candidate); + Type[] g_args = TypeManager.GetGenericArguments (best_candidate.MetaInfo); if (type_arguments.Count != g_args.Length) { - ec.Report.SymbolRelatedToPreviousError (best_candidate); + ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo); ec.Report.Error (305, loc, "Using the generic method `{0}' requires `{1}' type argument(s)", - TypeManager.CSharpSignature (best_candidate), + TypeManager.CSharpSignature (best_candidate.MetaInfo), g_args.Length.ToString ()); return true; } } else { - if (type_arguments != null && !TypeManager.IsGenericMethod (best_candidate)) { + if (type_arguments != null && !best_candidate.IsGenericMethod) { Error_TypeArgumentsCannotBeUsed (ec.Report, loc); return true; } @@ -4581,9 +4574,9 @@ namespace Mono.CSharp { // base class (CS1540). If the qualifier_type is a base of the // ec.CurrentType and the lookup succeeds with the latter one, // then we are in this situation. - Error_CannotAccessProtected (ec, loc, best_candidate, queried_type, ec.CurrentType); + Error_CannotAccessProtected (ec, loc, best_candidate.MetaInfo, queried_type, ec.CurrentType); } else { - ec.Report.SymbolRelatedToPreviousError (best_candidate); + ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo); ErrorIsInaccesible (loc, GetSignatureForError (), ec.Report); } } @@ -4605,11 +4598,11 @@ namespace Mono.CSharp { } public bool VerifyArgumentsCompat (ResolveContext ec, ref Arguments arguments, - int arg_count, MethodBase method, + int arg_count, MethodSpec method, bool chose_params_expanded, bool may_fail, Location loc) { - AParametersCollection pd = TypeManager.GetParameterData (method); + AParametersCollection pd = method.Parameters; int param_count = GetApplicableParametersCount (method, pd); int errors = ec.Report.Errors; @@ -4617,8 +4610,8 @@ namespace Mono.CSharp { Type pt = null; int a_idx = 0, a_pos = 0; Argument a = null; - ArrayList params_initializers = null; - bool has_unsafe_arg = method is MethodInfo ? ((MethodInfo) method).ReturnType.IsPointer : false; + ArrayInitializer params_initializers = null; + bool has_unsafe_arg = method.ReturnType.IsPointer; for (; a_idx < arg_count; a_idx++, ++a_pos) { a = arguments [a_idx]; @@ -4629,7 +4622,7 @@ namespace Mono.CSharp { if (p_mod == Parameter.Modifier.PARAMS) { if (chose_params_expanded) { - params_initializers = new ArrayList (arg_count - a_idx); + params_initializers = new ArrayInitializer (arg_count - a_idx, a.Expr.Location); pt = TypeManager.GetElementType (pt); } } @@ -4657,16 +4650,16 @@ namespace Mono.CSharp { "The delegate `{0}' does not contain a parameter named `{1}'", TypeManager.CSharpName (DeclaringType), na.Name); } else { - ec.Report.SymbolRelatedToPreviousError (best_candidate); + ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo); ec.Report.Error (1739, na.Location, "The best overloaded method match for `{0}' does not contain a parameter named `{1}'", - TypeManager.CSharpSignature (method), na.Name); + TypeManager.CSharpSignature (method.MetaInfo), na.Name); } } else if (arguments[name_index] != a) { if (DeclaringType != null && TypeManager.IsDelegateType (DeclaringType)) ec.Report.SymbolRelatedToPreviousError (DeclaringType); else - ec.Report.SymbolRelatedToPreviousError (best_candidate); + ec.Report.SymbolRelatedToPreviousError (best_candidate.MetaInfo); ec.Report.Error (1744, na.Location, "Named argument `{0}' cannot be used for a parameter which has positional argument specified", @@ -4721,7 +4714,7 @@ namespace Mono.CSharp { pt = pd.Types [param_count - 1]; pt = TypeManager.GetElementType (pt); has_unsafe_arg |= pt.IsPointer; - params_initializers = new ArrayList (0); + params_initializers = new ArrayInitializer (0, loc); } // @@ -4729,8 +4722,7 @@ namespace Mono.CSharp { // if (params_initializers != null) { arguments.Add (new Argument ( - new ArrayCreation (new TypeExpression (pt, loc), "[]", - params_initializers, loc).Resolve (ec))); + new ArrayCreation (new TypeExpression (pt, loc), "[]", params_initializers, loc).Resolve (ec))); arg_count++; } @@ -4752,9 +4744,9 @@ namespace Mono.CSharp { public class ConstantExpr : MemberExpr { - FieldInfo constant; + ConstSpec constant; - public ConstantExpr (FieldInfo constant, Location loc) + public ConstantExpr (ConstSpec constant, Location loc) { this.constant = constant; this.loc = loc; @@ -4769,33 +4761,13 @@ namespace Mono.CSharp { } public override bool IsStatic { - get { return constant.IsStatic; } + get { return true; } } public override Type DeclaringType { get { return constant.DeclaringType; } } - public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, Location loc, SimpleName original) - { - constant = TypeManager.GetGenericFieldDefinition (constant); - - IConstant ic = TypeManager.GetConstant (constant); - if (ic == null) { - if (constant.IsLiteral) { - ic = new ExternalConstant (constant, ec.Compiler); - } else { - ic = ExternalConstant.CreateDecimal (constant, ec); - // HACK: decimal field was not resolved as constant - if (ic == null) - return new FieldExpr (constant, loc).ResolveMemberAccess (ec, left, loc, original); - } - TypeManager.RegisterConstant (constant, ic); - } - - return base.ResolveMemberAccess (ec, left, loc, original); - } - public override Expression CreateExpressionTree (ResolveContext ec) { throw new NotSupportedException ("ET"); @@ -4803,13 +4775,19 @@ namespace Mono.CSharp { protected override Expression DoResolve (ResolveContext rc) { - IConstant ic = TypeManager.GetConstant (constant); - if (ic.ResolveValue ()) { - if (!rc.IsObsolete) - ic.CheckObsoleteness (loc); + constant.MemberDefinition.SetIsUsed (); + + if (!rc.IsObsolete) { + var oa = constant.GetObsoleteAttribute (); + if (oa != null) + AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (constant.MetaInfo), loc, rc.Report); } - return ic.CreateConstantReference (rc, loc); + // Constants are resolved on-demand + var c = constant.Value.Resolve (rc) as Constant; + + // Creates reference expression to the constant value + return Constant.CreateConstant (rc, c.Type, c.GetValue (), loc); } public override void Emit (EmitContext ec) @@ -4819,7 +4797,7 @@ namespace Mono.CSharp { public override string GetSignatureForError () { - return TypeManager.GetFullNameSignature (constant); + return TypeManager.GetFullNameSignature (constant.MetaInfo); } } @@ -4827,7 +4805,7 @@ namespace Mono.CSharp { /// Fully resolved expression that evaluates to a Field /// public class FieldExpr : MemberExpr, IDynamicAssign, IMemoryLocation, IVariableReference { - public FieldInfo FieldInfo; + protected FieldSpec spec; readonly Type constructed_generic_type; VariableInfo variable_info; @@ -4838,15 +4816,22 @@ namespace Mono.CSharp { { loc = l; } + + public FieldExpr (FieldSpec spec, Location loc) + { + this.spec = spec; + this.loc = loc; + + type = TypeManager.TypeToCoreType (spec.FieldType); + } - public FieldExpr (FieldInfo fi, Location l) + public FieldExpr (FieldBase fi, Location l) + : this (fi.Spec, l) { - FieldInfo = fi; - type = TypeManager.TypeToCoreType (fi.FieldType); loc = l; } - public FieldExpr (FieldInfo fi, Type genericType, Location l) + public FieldExpr (Field fi, Type genericType, Location l) : this (fi, l) { if (TypeManager.IsGenericTypeDefinition (genericType)) @@ -4856,31 +4841,37 @@ namespace Mono.CSharp { public override string Name { get { - return FieldInfo.Name; + return spec.Name; } } public override bool IsInstance { get { - return !FieldInfo.IsStatic; + return !spec.IsStatic; } } public override bool IsStatic { get { - return FieldInfo.IsStatic; + return spec.IsStatic; + } + } + + public FieldSpec Spec { + get { + return spec; } } public override Type DeclaringType { get { - return FieldInfo.DeclaringType; + return spec.MetaInfo.DeclaringType; } } public override string GetSignatureForError () { - return TypeManager.GetFullNameSignature (FieldInfo); + return TypeManager.GetFullNameSignature (spec.MetaInfo); } public VariableInfo VariableInfo { @@ -4892,7 +4883,7 @@ namespace Mono.CSharp { public override MemberExpr ResolveMemberAccess (ResolveContext ec, Expression left, Location loc, SimpleName original) { - FieldInfo fi = TypeManager.GetGenericFieldDefinition (FieldInfo); + FieldInfo fi = TypeManager.GetGenericFieldDefinition (spec.MetaInfo); Type t = fi.FieldType; if (t.IsPointer && !ec.IsUnsafe) { @@ -4927,7 +4918,7 @@ namespace Mono.CSharp { public Expression CreateTypeOfExpression () { - return new TypeOfField (GetConstructedFieldInfo (), loc); + return new TypeOfField (Import.CreateField (GetConstructedFieldInfo ()), loc); } protected override Expression DoResolve (ResolveContext ec) @@ -4937,7 +4928,7 @@ namespace Mono.CSharp { Expression DoResolve (ResolveContext ec, bool lvalue_instance, bool out_access) { - if (!FieldInfo.IsStatic){ + if (!IsStatic){ if (InstanceExpression == null){ // // This can happen when referencing an instance field using @@ -4976,17 +4967,17 @@ namespace Mono.CSharp { } if (!ec.IsObsolete) { - FieldBase f = TypeManager.GetField (FieldInfo); + FieldBase f = TypeManager.GetField (spec.MetaInfo); if (f != null) { f.CheckObsoleteness (loc); } else { - ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (FieldInfo); + ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo); if (oa != null) - AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (FieldInfo), loc, ec.Report); + AttributeTester.Report_ObsoleteMessage (oa, TypeManager.GetFullNameSignature (spec.MetaInfo), loc, ec.Report); } } - IFixedBuffer fb = AttributeTester.GetFixedBuffer (FieldInfo); + var fb = spec as FixedFieldSpec; IVariableReference var = InstanceExpression as IVariableReference; if (fb != null) { @@ -4996,9 +4987,9 @@ namespace Mono.CSharp { } if (InstanceExpression.eclass != ExprClass.Variable) { - ec.Report.SymbolRelatedToPreviousError (FieldInfo); + ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo); ec.Report.Error (1708, loc, "`{0}': Fixed size buffers can only be accessed through locals or fields", - TypeManager.GetFullNameSignature (FieldInfo)); + TypeManager.GetFullNameSignature (spec.MetaInfo)); } else if (var != null && var.IsHoisted) { AnonymousMethodExpression.Error_AddressOfCapturedVar (ec, var, loc); } @@ -5013,10 +5004,10 @@ namespace Mono.CSharp { return this; VariableInfo vi = var.VariableInfo; - if (!vi.IsFieldAssigned (ec, FieldInfo.Name, loc)) + if (!vi.IsFieldAssigned (ec, Name, loc)) return null; - variable_info = vi.GetSubStruct (FieldInfo.Name); + variable_info = vi.GetSubStruct (Name); return this; } @@ -5061,9 +5052,9 @@ namespace Mono.CSharp { { IVariableReference var = InstanceExpression as IVariableReference; if (var != null && var.VariableInfo != null) - var.VariableInfo.SetFieldAssigned (ec, FieldInfo.Name); + var.VariableInfo.SetFieldAssigned (ec, Name); - bool lvalue_instance = !FieldInfo.IsStatic && TypeManager.IsValueType (FieldInfo.DeclaringType); + bool lvalue_instance = !spec.IsStatic && TypeManager.IsValueType (spec.MetaInfo.DeclaringType); bool out_access = right_side == EmptyExpression.OutAccess.Instance || right_side == EmptyExpression.LValueMemberOutAccess; Expression e = DoResolve (ec, lvalue_instance, out_access); @@ -5071,7 +5062,7 @@ namespace Mono.CSharp { if (e == null) return null; - FieldBase fb = TypeManager.GetField (FieldInfo); + FieldBase fb = TypeManager.GetField (spec.MetaInfo); if (fb != null) { fb.SetAssigned (); @@ -5083,7 +5074,7 @@ namespace Mono.CSharp { } } - if (FieldInfo.IsInitOnly) { + if (spec.IsReadOnly) { // InitOnly fields can only be assigned in constructors or initializers if (!ec.HasAny (ResolveContext.Options.FieldInitializerScope | ResolveContext.Options.ConstructorScope)) return Report_AssignToReadonly (ec, right_side); @@ -5092,7 +5083,7 @@ namespace Mono.CSharp { Type ctype = ec.CurrentType; // InitOnly fields cannot be assigned-to in a different constructor from their declaring type - if (!TypeManager.IsEqual (ctype, FieldInfo.DeclaringType)) + if (!TypeManager.IsEqual (ctype, DeclaringType)) return Report_AssignToReadonly (ec, right_side); // static InitOnly fields cannot be assigned-to in an instance constructor if (IsStatic && !ec.IsStatic) @@ -5131,7 +5122,7 @@ namespace Mono.CSharp { public override int GetHashCode () { - return FieldInfo.GetHashCode (); + return spec.GetHashCode (); } public bool IsFixed { @@ -5161,7 +5152,7 @@ namespace Mono.CSharp { if (fe == null) return false; - if (FieldInfo != fe.FieldInfo) + if (spec.MetaInfo != fe.spec.MetaInfo) return false; if (InstanceExpression == null || fe.InstanceExpression == null) @@ -5175,15 +5166,15 @@ namespace Mono.CSharp { ILGenerator ig = ec.ig; bool is_volatile = false; - FieldBase f = TypeManager.GetField (FieldInfo); + var f = TypeManager.GetField (spec.MetaInfo); if (f != null){ if ((f.ModFlags & Modifiers.VOLATILE) != 0) is_volatile = true; - f.SetMemberIsUsed (); + f.SetIsUsed (); } - if (FieldInfo.IsStatic){ + if (IsStatic){ if (is_volatile) ig.Emit (OpCodes.Volatile); @@ -5193,10 +5184,10 @@ namespace Mono.CSharp { EmitInstance (ec, false); // Optimization for build-in types - if (TypeManager.IsStruct (type) && TypeManager.IsEqual (type, ec.MemberContext.CurrentType)) { + if (TypeManager.IsStruct (type) && TypeManager.IsEqual (type, ec.MemberContext.CurrentType) && TypeManager.IsEqual (InstanceExpression.Type, type)) { LoadFromPtr (ig, type); } else { - IFixedBuffer ff = AttributeTester.GetFixedBuffer (FieldInfo); + var ff = spec as FixedFieldSpec; if (ff != null) { ig.Emit (OpCodes.Ldflda, GetConstructedFieldInfo ()); ig.Emit (OpCodes.Ldflda, ff.Element); @@ -5211,7 +5202,7 @@ namespace Mono.CSharp { if (leave_copy) { ec.ig.Emit (OpCodes.Dup); - if (!FieldInfo.IsStatic) { + if (!IsStatic) { temp = new LocalTemporary (this.Type); temp.Store (ec); } @@ -5220,8 +5211,8 @@ namespace Mono.CSharp { public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load) { - FieldAttributes fa = FieldInfo.Attributes; - bool is_static = (fa & FieldAttributes.Static) != 0; + //FieldAttributes fa = FieldInfo.Attributes; + //bool is_static = (fa & FieldAttributes.Static) != 0; ILGenerator ig = ec.ig; prepared = prepare_for_load; @@ -5230,13 +5221,13 @@ namespace Mono.CSharp { source.Emit (ec); if (leave_copy) { ec.ig.Emit (OpCodes.Dup); - if (!FieldInfo.IsStatic) { + if (!IsStatic) { temp = new LocalTemporary (this.Type); temp.Store (ec); } } - FieldBase f = TypeManager.GetField (FieldInfo); + FieldBase f = TypeManager.GetField (spec.MetaInfo); if (f != null){ if ((f.ModFlags & Modifiers.VOLATILE) != 0) ig.Emit (OpCodes.Volatile); @@ -5244,7 +5235,7 @@ namespace Mono.CSharp { f.SetAssigned (); } - if (is_static) + if (IsStatic) ig.Emit (OpCodes.Stsfld, GetConstructedFieldInfo ()); else ig.Emit (OpCodes.Stfld, GetConstructedFieldInfo ()); @@ -5263,7 +5254,7 @@ namespace Mono.CSharp { public override void EmitSideEffect (EmitContext ec) { - FieldBase f = TypeManager.GetField (FieldInfo); + FieldBase f = TypeManager.GetField (spec.MetaInfo); bool is_volatile = f != null && (f.ModFlags & Modifiers.VOLATILE) != 0; if (is_volatile || is_marshal_by_ref ()) @@ -5281,12 +5272,12 @@ namespace Mono.CSharp { { ILGenerator ig = ec.ig; - FieldBase f = TypeManager.GetField (FieldInfo); + FieldBase f = TypeManager.GetField (spec.MetaInfo); if (f != null){ if ((mode & AddressOp.Store) != 0) f.SetAssigned (); if ((mode & AddressOp.Load) != 0) - f.SetMemberIsUsed (); + f.SetIsUsed (); } // @@ -5294,10 +5285,10 @@ namespace Mono.CSharp { // get the address of the copy. // bool need_copy; - if (FieldInfo.IsInitOnly){ + if (spec.IsReadOnly){ need_copy = true; if (ec.HasSet (EmitContext.Options.ConstructorScope)){ - if (FieldInfo.IsStatic){ + if (IsStatic){ if (ec.IsStatic) need_copy = false; } else @@ -5316,7 +5307,7 @@ namespace Mono.CSharp { } - if (FieldInfo.IsStatic){ + if (IsStatic){ ig.Emit (OpCodes.Ldsflda, GetConstructedFieldInfo ()); } else { if (!prepared) @@ -5328,12 +5319,11 @@ namespace Mono.CSharp { FieldInfo GetConstructedFieldInfo () { if (constructed_generic_type == null) - return FieldInfo; + return spec.MetaInfo; - return TypeBuilder.GetField (constructed_generic_type, FieldInfo); + return TypeBuilder.GetField (constructed_generic_type, spec.MetaInfo); } -#if NET_4_0 public SLE.Expression MakeAssignExpression (BuilderContext ctx) { return MakeExpression (ctx); @@ -5341,13 +5331,12 @@ namespace Mono.CSharp { public override SLE.Expression MakeExpression (BuilderContext ctx) { - return SLE.Expression.Field (InstanceExpression.MakeExpression (ctx), FieldInfo); + return SLE.Expression.Field (InstanceExpression.MakeExpression (ctx), spec.MetaInfo); } -#endif public override void MutateHoistedGenericType (AnonymousMethodStorey storey) { - FieldInfo = storey.MutateField (FieldInfo); + storey.MutateField (spec); base.MutateHoistedGenericType (storey); } } @@ -5362,8 +5351,8 @@ namespace Mono.CSharp { /// public class PropertyExpr : MemberExpr, IDynamicAssign { - public readonly PropertyInfo PropertyInfo; - MethodInfo getter, setter; + PropertySpec spec; + MethodSpec getter, setter; bool is_static; TypeArguments targs; @@ -5371,20 +5360,19 @@ namespace Mono.CSharp { LocalTemporary temp; bool prepared; - public PropertyExpr (Type container_type, PropertyInfo pi, Location l) + public PropertyExpr (Type container_type, PropertySpec spec, Location l) { - PropertyInfo = pi; - is_static = false; + this.spec = spec; loc = l; - type = TypeManager.TypeToCoreType (pi.PropertyType); + type = TypeManager.TypeToCoreType (spec.PropertyType); ResolveAccessors (container_type); } public override string Name { get { - return PropertyInfo.Name; + return spec.Name; } } @@ -5430,13 +5418,13 @@ namespace Mono.CSharp { public override Type DeclaringType { get { - return PropertyInfo.DeclaringType; + return spec.DeclaringType; } } public override string GetSignatureForError () { - return TypeManager.GetFullNameSignature (PropertyInfo); + return TypeManager.GetFullNameSignature (spec.MetaInfo); } void FindAccessors (Type invocation_type) @@ -5445,11 +5433,11 @@ namespace Mono.CSharp { BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly; - Type current = PropertyInfo.DeclaringType; + Type current = spec.DeclaringType; for (; current != null; current = current.BaseType) { MemberInfo[] group = TypeManager.MemberLookup ( invocation_type, invocation_type, current, - MemberTypes.Property, flags, PropertyInfo.Name, null); + MemberTypes.Property, flags, spec.Name, null); if (group == null) continue; @@ -5460,13 +5448,19 @@ namespace Mono.CSharp { PropertyInfo pi = (PropertyInfo) group [0]; - if (getter == null) - getter = pi.GetGetMethod (true); + if (getter == null) { + var m = pi.GetGetMethod (true); + if (m != null) + getter = Import.CreateMethod (m); + } - if (setter == null) - setter = pi.GetSetMethod (true); + if (setter == null) { + var m = pi.GetSetMethod (true); + if (m != null) + setter = Import.CreateMethod (m); + } - MethodInfo accessor = getter != null ? getter : setter; + var accessor = getter != null ? getter : setter; if (!accessor.IsVirtual) return; @@ -5486,7 +5480,7 @@ namespace Mono.CSharp { MethodBase the_getter = TypeManager.DropGenericMethodArguments (getter); IMethodData md = TypeManager.GetMethod (the_getter); if (md != null) - md.SetMemberIsUsed (); + md.SetIsUsed (); is_static = getter.IsStatic; } @@ -5495,23 +5489,21 @@ namespace Mono.CSharp { MethodBase the_setter = TypeManager.DropGenericMethodArguments (setter); IMethodData md = TypeManager.GetMethod (the_setter); if (md != null) - md.SetMemberIsUsed (); + md.SetIsUsed (); is_static = setter.IsStatic; } } -#if NET_4_0 public SLE.Expression MakeAssignExpression (BuilderContext ctx) { - return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), setter); + return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) setter.MetaInfo); } public override SLE.Expression MakeExpression (BuilderContext ctx) { - return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), getter); + return SLE.Expression.Property (InstanceExpression.MakeExpression (ctx), (MethodInfo) getter.MetaInfo); } -#endif public override void MutateHoistedGenericType (AnonymousMethodStorey storey) { @@ -5520,9 +5512,15 @@ namespace Mono.CSharp { type = storey.MutateType (type); if (getter != null) - getter = storey.MutateGenericMethod (getter); + storey.MutateGenericMethod (getter); if (setter != null) - setter = storey.MutateGenericMethod (setter); + storey.MutateGenericMethod (setter); + } + + public PropertyInfo PropertyInfo { + get { + return spec.MetaInfo; + } } bool InstanceResolve (ResolveContext ec, bool lvalue_instance, bool must_do_cs1540_check) @@ -5550,30 +5548,30 @@ namespace Mono.CSharp { !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.CurrentType) && !TypeManager.IsNestedChildOf (ec.CurrentType, InstanceExpression.Type) && !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.CurrentType)) { - ec.Report.SymbolRelatedToPreviousError (PropertyInfo); - Error_CannotAccessProtected (ec, loc, PropertyInfo, InstanceExpression.Type, ec.CurrentType); + ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo); + Error_CannotAccessProtected (ec, loc, spec.MetaInfo, InstanceExpression.Type, ec.CurrentType); return false; } return true; } - void Error_PropertyNotFound (ResolveContext ec, MethodInfo mi, bool getter) + void Error_PropertyNotFound (ResolveContext ec, MethodSpec mi, bool getter) { // TODO: correctly we should compare arguments but it will lead to bigger changes - if (mi is MethodBuilder) { - Error_TypeDoesNotContainDefinition (ec, loc, PropertyInfo.DeclaringType, Name); + if (mi.MetaInfo is MethodBuilder) { + Error_TypeDoesNotContainDefinition (ec, loc, spec.DeclaringType, Name); return; } StringBuilder sig = new StringBuilder (TypeManager.CSharpName (mi.DeclaringType)); sig.Append ('.'); - AParametersCollection iparams = TypeManager.GetParameterData (mi); + AParametersCollection iparams = mi.Parameters; sig.Append (getter ? "get_" : "set_"); sig.Append (Name); sig.Append (iparams.GetSignatureForError ()); - ec.Report.SymbolRelatedToPreviousError (mi); + ec.Report.SymbolRelatedToPreviousError (mi.MetaInfo); ec.Report.Error (1546, loc, "Property `{0}' is not supported by the C# language. Try to call the accessor method `{1}' directly", Name, sig.ToString ()); } @@ -5581,7 +5579,7 @@ namespace Mono.CSharp { public bool IsAccessibleFrom (Type invocation_type, bool lvalue) { bool dummy; - MethodInfo accessor = lvalue ? setter : getter; + var accessor = lvalue ? setter : getter; if (accessor == null && lvalue) accessor = getter; return accessor != null && IsAccessorAccessible (invocation_type, accessor, out dummy); @@ -5628,19 +5626,19 @@ namespace Mono.CSharp { // Only base will allow this invocation to happen. // if (IsBase && getter.IsAbstract) { - Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (PropertyInfo)); + Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (spec.MetaInfo)); } - if (PropertyInfo.PropertyType.IsPointer && !ec.IsUnsafe){ + if (spec.PropertyType.IsPointer && !ec.IsUnsafe){ UnsafeError (ec, loc); } if (!ec.IsObsolete) { - PropertyBase pb = TypeManager.GetProperty (PropertyInfo); + PropertyBase pb = TypeManager.GetProperty (spec.MetaInfo); if (pb != null) { pb.CheckObsoleteness (loc); } else { - ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (PropertyInfo); + ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo); if (oa != null) AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report); } @@ -5654,9 +5652,9 @@ namespace Mono.CSharp { eclass = ExprClass.PropertyAccess; if (right_side == EmptyExpression.OutAccess.Instance) { - if (ec.CurrentBlock.Toplevel.GetParameterReference (PropertyInfo.Name, loc) is MemberAccess) { + if (ec.CurrentBlock.Toplevel.GetParameterReference (spec.Name, loc) is MemberAccess) { ec.Report.Error (1939, loc, "A range variable `{0}' may not be passes as `ref' or `out' parameter", - PropertyInfo.Name); + spec.Name); } else { right_side.DoResolveLValue (ec, this); } @@ -5677,9 +5675,9 @@ namespace Mono.CSharp { if (getter == null) return null; - if (ec.CurrentBlock.Toplevel.GetParameterReference (PropertyInfo.Name, loc) is MemberAccess) { + if (ec.CurrentBlock.Toplevel.GetParameterReference (spec.Name, loc) is MemberAccess) { ec.Report.Error (1947, loc, "A range variable `{0}' cannot be assigned to. Consider using `let' clause to store the value", - PropertyInfo.Name); + spec.Name); } else { ec.Report.Error (200, loc, "Property or indexer `{0}' cannot be assigned to (it is read only)", GetSignatureForError ()); @@ -5692,46 +5690,46 @@ namespace Mono.CSharp { return null; } - if (TypeManager.GetParameterData (setter).Count != 1){ + if (setter.Parameters.Count != 1){ Error_PropertyNotFound (ec, setter, false); return null; } bool must_do_cs1540_check; if (!IsAccessorAccessible (ec.CurrentType, setter, out must_do_cs1540_check)) { - PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter) as PropertyBase.PropertyMethod; + PropertyBase.PropertyMethod pm = TypeManager.GetMethod (setter.MetaInfo) as PropertyBase.PropertyMethod; if (pm != null && pm.HasCustomAccessModifier) { ec.Report.SymbolRelatedToPreviousError (pm); ec.Report.Error (272, loc, "The property or indexer `{0}' cannot be used in this context because the set accessor is inaccessible", TypeManager.CSharpSignature (setter)); } else { - ec.Report.SymbolRelatedToPreviousError (setter); + ec.Report.SymbolRelatedToPreviousError (setter.MetaInfo); ErrorIsInaccesible (loc, TypeManager.CSharpSignature (setter), ec.Report); } return null; } - if (!InstanceResolve (ec, TypeManager.IsStruct (PropertyInfo.DeclaringType), must_do_cs1540_check)) + if (!InstanceResolve (ec, TypeManager.IsStruct (spec.DeclaringType), must_do_cs1540_check)) return null; // // Only base will allow this invocation to happen. // if (IsBase && setter.IsAbstract){ - Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (PropertyInfo)); + Error_CannotCallAbstractBase (ec, TypeManager.GetFullNameSignature (spec.MetaInfo)); } - if (PropertyInfo.PropertyType.IsPointer && !ec.IsUnsafe) { + if (spec.PropertyType.IsPointer && !ec.IsUnsafe) { UnsafeError (ec, loc); } if (!ec.IsObsolete) { - PropertyBase pb = TypeManager.GetProperty (PropertyInfo); + PropertyBase pb = TypeManager.GetProperty (spec.MetaInfo); if (pb != null) { pb.CheckObsoleteness (loc); } else { - ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (PropertyInfo); + ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (spec.MetaInfo); if (oa != null) AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report); } @@ -5813,7 +5811,7 @@ namespace Mono.CSharp { } if (getter != null) { - if (TypeManager.GetParameterData (getter).Count != 0) { + if (!getter.Parameters.IsEmpty) { Error_PropertyNotFound (ec, getter, true); return false; } @@ -5831,21 +5829,21 @@ namespace Mono.CSharp { if (InstanceExpression != EmptyExpression.Null) { ec.Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor", - TypeManager.GetFullNameSignature (PropertyInfo)); + TypeManager.GetFullNameSignature (spec.MetaInfo)); return false; } } if (getter != null && !IsAccessorAccessible (ec.CurrentType, getter, out must_do_cs1540_check)) { - PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter) as PropertyBase.PropertyMethod; + PropertyBase.PropertyMethod pm = TypeManager.GetMethod (getter.MetaInfo) as PropertyBase.PropertyMethod; if (pm != null && pm.HasCustomAccessModifier) { ec.Report.SymbolRelatedToPreviousError (pm); ec.Report.Error (271, loc, "The property or indexer `{0}' cannot be used in this context because the get accessor is inaccessible", - TypeManager.CSharpSignature (getter)); + TypeManager.CSharpSignature (getter.MetaInfo)); } else { - ec.Report.SymbolRelatedToPreviousError (getter); - ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter), ec.Report); + ec.Report.SymbolRelatedToPreviousError (getter.MetaInfo); + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (getter.MetaInfo), ec.Report); } return false; @@ -5863,51 +5861,37 @@ namespace Mono.CSharp { /// /// Fully resolved expression that evaluates to an Event /// - public class EventExpr : MemberExpr { - public readonly EventInfo EventInfo; - - bool is_static; - MethodInfo add_accessor, remove_accessor; + public class EventExpr : MemberExpr + { + readonly EventSpec spec; - public EventExpr (EventInfo ei, Location loc) + public EventExpr (EventSpec spec, Location loc) { - EventInfo = ei; + this.spec = spec; this.loc = loc; - - add_accessor = TypeManager.GetAddMethod (ei); - remove_accessor = TypeManager.GetRemoveMethod (ei); - if (add_accessor.IsStatic || remove_accessor.IsStatic) - is_static = true; - - if (EventInfo is MyEventBuilder){ - MyEventBuilder eb = (MyEventBuilder) EventInfo; - type = eb.EventType; - eb.SetUsed (); - } else - type = EventInfo.EventHandlerType; } public override string Name { get { - return EventInfo.Name; + return spec.Name; } } public override bool IsInstance { get { - return !is_static; + return !spec.IsStatic; } } public override bool IsStatic { get { - return is_static; + return spec.IsStatic; } } public override Type DeclaringType { get { - return EventInfo.DeclaringType; + return spec.DeclaringType; } } @@ -5924,20 +5908,21 @@ namespace Mono.CSharp { // If the event is local to this class, we transform ourselves into a FieldExpr // - if (EventInfo.DeclaringType == ec.CurrentType || - TypeManager.IsNestedChildOf(ec.CurrentType, EventInfo.DeclaringType)) { + if (spec.DeclaringType == ec.CurrentType || + TypeManager.IsNestedChildOf(ec.CurrentType, spec.DeclaringType)) { // TODO: Breaks dynamic binder as currect context fields are imported and not compiled - EventField mi = TypeManager.GetEventField (EventInfo); + EventField mi = TypeManager.GetEventField (spec.MetaInfo).MemberDefinition as EventField; - if (mi != null) { + if (mi != null && mi.HasBackingField) { + mi.SetIsUsed (); if (!ec.IsObsolete) mi.CheckObsoleteness (loc); if ((mi.ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN)) != 0 && !ec.HasSet (ResolveContext.Options.CompoundAssignmentScope)) Error_AssignmentEventOnly (ec); - FieldExpr ml = new FieldExpr (mi.BackingField.FieldBuilder, loc); + FieldExpr ml = new FieldExpr (mi.BackingField, loc); InstanceExpression = null; @@ -5953,7 +5938,7 @@ namespace Mono.CSharp { bool InstanceResolve (ResolveContext ec, bool must_do_cs1540_check) { - if (is_static) { + if (IsStatic) { InstanceExpression = null; return true; } @@ -5967,8 +5952,8 @@ namespace Mono.CSharp { if (InstanceExpression == null) return false; - if (IsBase && add_accessor.IsAbstract) { - Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature(add_accessor)); + if (IsBase && spec.IsAbstract) { + Error_CannotCallAbstractBase (ec, TypeManager.CSharpSignature(spec.MetaInfo)); return false; } @@ -5982,8 +5967,8 @@ namespace Mono.CSharp { !TypeManager.IsInstantiationOfSameGenericType (InstanceExpression.Type, ec.CurrentType) && !TypeManager.IsNestedChildOf (ec.CurrentType, InstanceExpression.Type) && !TypeManager.IsSubclassOf (InstanceExpression.Type, ec.CurrentType)) { - ec.Report.SymbolRelatedToPreviousError (EventInfo); - ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo), ec.Report); + ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo); + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (spec.MetaInfo), ec.Report); return false; } @@ -5993,8 +5978,8 @@ namespace Mono.CSharp { public bool IsAccessibleFrom (Type invocation_type) { bool dummy; - return IsAccessorAccessible (invocation_type, add_accessor, out dummy) && - IsAccessorAccessible (invocation_type, remove_accessor, out dummy); + return IsAccessorAccessible (invocation_type, spec.AccessorAdd, out dummy) && + IsAccessorAccessible (invocation_type, spec.AccessorRemove, out dummy); } public override Expression CreateExpressionTree (ResolveContext ec) @@ -6014,10 +5999,10 @@ namespace Mono.CSharp { eclass = ExprClass.EventAccess; bool must_do_cs1540_check; - if (!(IsAccessorAccessible (ec.CurrentType, add_accessor, out must_do_cs1540_check) && - IsAccessorAccessible (ec.CurrentType, remove_accessor, out must_do_cs1540_check))) { - ec.Report.SymbolRelatedToPreviousError (EventInfo); - ErrorIsInaccesible (loc, TypeManager.CSharpSignature (EventInfo), ec.Report); + if (!(IsAccessorAccessible (ec.CurrentType, spec.AccessorAdd, out must_do_cs1540_check) && + IsAccessorAccessible (ec.CurrentType, spec.AccessorRemove, out must_do_cs1540_check))) { + ec.Report.SymbolRelatedToPreviousError (spec.MetaInfo); + ErrorIsInaccesible (loc, TypeManager.CSharpSignature (spec.MetaInfo), ec.Report); return null; } @@ -6030,15 +6015,13 @@ namespace Mono.CSharp { } if (!ec.IsObsolete) { - EventField ev = TypeManager.GetEventField (EventInfo); - if (ev != null) { - ev.CheckObsoleteness (loc); - } else { - ObsoleteAttribute oa = AttributeTester.GetMemberObsoleteAttribute (EventInfo); - if (oa != null) - AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report); - } + var oa = spec.GetObsoleteAttribute (); + if (oa != null) + AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, ec.Report); } + + spec.MemberDefinition.SetIsUsed (); + type = spec.EventType; return this; } @@ -6053,19 +6036,21 @@ namespace Mono.CSharp { { ec.Report.Error (70, loc, "The event `{0}' can only appear on the left hand side of += or -= when used outside of the type `{1}'", - GetSignatureForError (), TypeManager.CSharpName (EventInfo.DeclaringType)); + GetSignatureForError (), TypeManager.CSharpName (spec.DeclaringType)); } public override string GetSignatureForError () { - return TypeManager.CSharpSignature (EventInfo); + return TypeManager.CSharpSignature (spec.MetaInfo); } public void EmitAddOrRemove (EmitContext ec, bool is_add, Expression source) { Arguments args = new Arguments (1); args.Add (new Argument (source)); - Invocation.EmitCall (ec, IsBase, InstanceExpression, is_add ? add_accessor : remove_accessor, args, loc); + Invocation.EmitCall (ec, IsBase, InstanceExpression, + is_add ? spec.AccessorAdd : spec.AccessorRemove, + args, loc); } }