Inflate custom site container delegates correctly.
[mono.git] / mcs / mcs / attribute.cs
index 77d8c143f506a879c85ce688e853f4c759401ce6..8330f5000763fa5d98ef0641cd9d210cf3aa5a0c 100644 (file)
@@ -178,11 +178,9 @@ namespace Mono.CSharp {
                                name.Name);
                }
 
-               public static void Error_AttributeArgumentNotValid (IMemberContext rc, Location loc)
+               public static void Error_AttributeArgumentIsDynamic (IMemberContext context, Location loc)
                {
-                       rc.Compiler.Report.Error (182, loc,
-                                     "An attribute argument must be a constant expression, typeof " +
-                                     "expression or array creation expression");
+                       context.Compiler.Report.Error (1982, loc, "An attribute argument cannot be dynamic expression");
                }
                
                public void Error_MissingGuidAttribute ()
@@ -310,8 +308,8 @@ namespace Mono.CSharp {
 
                public bool HasSecurityAttribute {
                        get {
-                               PredefinedAttribute pa = PredefinedAttributes.Get.Security;
-                               return pa.IsDefined && TypeManager.IsSubclassOf (type, pa.Type);
+                               PredefinedAttribute pa = context.Compiler.PredefinedAttributes.Security;
+                               return pa.IsDefined && TypeSpec.IsBaseClass (type, pa.Type, false);
                        }
                }
 
@@ -339,7 +337,7 @@ namespace Mono.CSharp {
 
                void ApplyModuleCharSet (ResolveContext rc)
                {
-                       if (Type != PredefinedAttributes.Get.DllImport)
+                       if (Type != context.Compiler.PredefinedAttributes.DllImport)
                                return;
 
                        if (!RootContext.ToplevelTypes.HasDefaultCharSet)
@@ -355,7 +353,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       var char_set = Import.ImportType (typeof (CharSet));
+                       var char_set = rc.Compiler.MetaImporter.ImportType (typeof (CharSet));  // TODO: typeof
                        NamedArguments.Add (new NamedArgument (CharSetEnumMember, loc,
                                Constant.CreateConstant (rc, char_set, RootContext.ToplevelTypes.DefaultCharSet, Location)));
                }
@@ -419,25 +417,12 @@ namespace Mono.CSharp {
                                bool dynamic;
                                PosArguments.Resolve (ec, out dynamic);
                                if (dynamic) {
-                                       Error_AttributeArgumentNotValid (ec, loc);
+                                       Error_AttributeArgumentIsDynamic (ec.MemberContext, loc);
                                        return null;
                                }
                        }
 
-                       MethodGroupExpr mg = MemberLookupFinal (ec, ec.CurrentType,
-                               Type, ConstructorInfo.ConstructorName, 0, MemberKind.Constructor,
-                               BindingRestriction.AccessibleOnly | BindingRestriction.DeclaredOnly,
-                               Location) as MethodGroupExpr;
-
-                       if (mg == null)
-                               throw new NotImplementedException ();
-
-                       mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
-                       if (mg == null)
-                               return null;
-                       
-                       var constructor = (MethodSpec) mg;
-                       return constructor;
+                       return ConstructorLookup (ec, Type, ref PosArguments, loc);
                }
 
                protected virtual bool ResolveNamedArguments (ResolveContext ec)
@@ -458,20 +443,14 @@ namespace Mono.CSharp {
 
                                a.Resolve (ec);
 
-                               Expression member = Expression.MemberLookup (ec.Compiler,
-                                       ec.CurrentType, Type, name, 0,
-                                       MemberKind.All,
-                                       BindingRestriction.AccessibleOnly,
-                                       Location);
+                               Expression member = Expression.MemberLookup (ec, ec.CurrentType, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
 
                                if (member == null) {
-                                       member = Expression.MemberLookup (ec.Compiler, ec.CurrentType, Type, name, 0,
-                                               MemberKind.All, BindingRestriction.None,
-                                               Location);
+                                       member = Expression.MemberLookup (null, ec.CurrentType, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
 
                                        if (member != null) {
-                                               ec.Report.SymbolRelatedToPreviousError (member.Type);
-                                               Expression.ErrorIsInaccesible (Location, member.GetSignatureForError (), ec.Report);
+                                               // TODO: ec.Report.SymbolRelatedToPreviousError (member);
+                                               Expression.ErrorIsInaccesible (ec, member.GetSignatureForError (), loc);
                                                return false;
                                        }
                                }
@@ -491,7 +470,7 @@ namespace Mono.CSharp {
                                if (member is PropertyExpr) {
                                        var pi = ((PropertyExpr) member).PropertyInfo;
 
-                                       if (!pi.HasSet || !pi.HasGet || pi.IsStatic) {
+                                       if (!pi.HasSet || !pi.HasGet || pi.IsStatic || !pi.Get.IsPublic || !pi.Set.IsPublic) {
                                                ec.Report.SymbolRelatedToPreviousError (pi);
                                                Error_InvalidNamedArgument (ec, a);
                                                return false;
@@ -504,10 +483,11 @@ namespace Mono.CSharp {
                                        }
 
                                        obsolete_attr = pi.GetAttributeObsolete ();
+                                       pi.MemberDefinition.SetIsAssigned ();
                                } else {
                                        var fi = ((FieldExpr) member).Spec;
 
-                                       if (fi.IsReadOnly || fi.IsStatic) {
+                                       if (fi.IsReadOnly || fi.IsStatic || !fi.IsPublic) {
                                                Error_InvalidNamedArgument (ec, a);
                                                return false;
                                        }
@@ -519,6 +499,7 @@ namespace Mono.CSharp {
                                        }
 
                                        obsolete_attr = fi.GetAttributeObsolete ();
+                                       fi.MemberDefinition.SetIsAssigned ();
                                }
 
                                if (obsolete_attr != null && !context.IsObsolete)
@@ -541,7 +522,7 @@ namespace Mono.CSharp {
                public string GetValidTargets ()
                {
                        StringBuilder sb = new StringBuilder ();
-                       AttributeTargets targets = Type.GetAttributeUsage (PredefinedAttributes.Get.AttributeUsage).ValidOn;
+                       AttributeTargets targets = Type.GetAttributeUsage (context.Compiler.PredefinedAttributes.AttributeUsage).ValidOn;
 
                        if ((targets & AttributeTargets.Assembly) != 0)
                                sb.Append ("assembly, ");
@@ -651,10 +632,16 @@ namespace Mono.CSharp {
                /// </summary>
                public ObsoleteAttribute GetObsoleteAttribute ()
                {
-                       if (!arg_resolved)
+                       if (!arg_resolved) {
+                               // corlib only case when obsolete is used before is resolved
+                               var c = type.MemberDefinition as Class;
+                               if (c != null && !c.HasMembersDefined)
+                                       c.Define ();
+                               
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve ();
+                       }
 
                        if (resolve_error)
                                return null;
@@ -1006,7 +993,7 @@ namespace Mono.CSharp {
 
                public bool IsInternalMethodImplAttribute {
                        get {
-                               if (Type != PredefinedAttributes.Get.MethodImpl)
+                               if (Type != context.Compiler.PredefinedAttributes.MethodImpl)
                                        return false;
 
                                MethodImplOptions options;
@@ -1061,7 +1048,9 @@ namespace Mono.CSharp {
                        if (ctor == null)
                                return;
 
-                       AttributeUsageAttribute usage_attr = Type.GetAttributeUsage (PredefinedAttributes.Get.AttributeUsage);
+                       var predefined = context.Compiler.PredefinedAttributes;
+
+                       AttributeUsageAttribute usage_attr = Type.GetAttributeUsage (predefined.AttributeUsage);
                        if ((usage_attr.ValidOn & Target) == 0) {
                                Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
                                              "It is valid on `{1}' declarations only",
@@ -1069,19 +1058,12 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       var predefined = PredefinedAttributes.Get;
-
                        AttributeEncoder encoder = new AttributeEncoder (false);
 
                        if (PosArguments != null) {
                                var param_types = ctor.Parameters.Types;
                                for (int j = 0; j < PosArguments.Count; ++j) {
                                        var pt = param_types[j];
-                                       if (!IsValidArgumentType (pt)) {
-                                               Error_AttributeArgumentNotValid (context, loc);
-                                               return;
-                                       }
-
                                        var arg_expr = PosArguments[j].Expr;
                                        if (j == 0) {
                                                if (Type == predefined.IndexerName || Type == predefined.Conditional) {
@@ -1137,7 +1119,6 @@ namespace Mono.CSharp {
                                foreach (Attributable target in targets)
                                        target.ApplyAttributeBuilder (this, ctor, cdata, predefined);
                        } catch (Exception e) {
-                               var w = e;
                                Error_AttributeEmitError (e.Message);
                                return;
                        }
@@ -1415,6 +1396,14 @@ namespace Mono.CSharp {
 
        public struct AttributeEncoder
        {
+               [Flags]
+               public enum EncodedTypeProperties
+               {
+                       None = 0,
+                       DynamicType = 1,
+                       TypeParameter = 1 << 1
+               }
+
                public readonly BinaryWriter Stream;
 
                public AttributeEncoder (bool empty)
@@ -1439,7 +1428,7 @@ namespace Mono.CSharp {
                        Stream.Write (buf);
                }
 
-               public void Encode (TypeSpec type)
+               public EncodedTypeProperties Encode (TypeSpec type)
                {
                        if (type == TypeManager.bool_type) {
                                Stream.Write ((byte) 0x02);
@@ -1476,20 +1465,19 @@ namespace Mono.CSharp {
                                EncodeTypeName (type);
                        } else if (type.IsArray) {
                                Stream.Write ((byte) 0x1D);
-                               Encode (TypeManager.GetElementType (type));
-                       } else {
-                               throw new NotImplementedException (type.ToString ());
+                               return Encode (TypeManager.GetElementType (type));
+                       } else if (type == InternalType.Dynamic) {
+                               Stream.Write ((byte) 0x51);
+                               return EncodedTypeProperties.DynamicType;
                        }
+
+                       return EncodedTypeProperties.None;
                }
 
-               public bool EncodeTypeName (TypeSpec type)
+               public void EncodeTypeName (TypeSpec type)
                {
-//                     if (TypeManager.ContainsGenericParameters (type) && !TypeManager.IsGenericTypeDefinition (type))
-//                             return false;
-
                        var old_type = type.GetMetaInfo ();
                        Encode (type.MemberDefinition.IsImported ? old_type.AssemblyQualifiedName : old_type.FullName);
-                       return true;
                }
 
                void WriteCompressedValue (int value)
@@ -1572,7 +1560,7 @@ namespace Mono.CSharp {
 
                public static void VerifyModulesClsCompliance (CompilerContext ctx)
                {
-                       Module[] modules = GlobalRootNamespace.Instance.Modules;
+                       Module[] modules = ctx.GlobalRootNamespace.Modules;
                        if (modules == null)
                                return;
 
@@ -1671,9 +1659,7 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute StructLayout;
                public readonly PredefinedAttribute FieldOffset;
 
-               public static PredefinedAttributes Get = new PredefinedAttributes ();
-
-               private PredefinedAttributes ()
+               public PredefinedAttributes ()
                {
                        ParamArray = new PredefinedAttribute ("System", "ParamArrayAttribute");
                        Out = new PredefinedAttribute ("System.Runtime.InteropServices", "OutAttribute");
@@ -1725,11 +1711,6 @@ namespace Mono.CSharp {
                                ((PredefinedAttribute) fi.GetValue (this)).Initialize (ctx, true);
                        }
                }
-
-               public static void Reset ()
-               {
-                       Get = new PredefinedAttributes ();
-               }
        }
 
        public class PredefinedAttribute