Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / mcs / attribute.cs
index 355948bb5a9051bd44a27134aeb8628e1133a34b..4fbc993a55a882591eb2192cfbe79fa163027afe 100644 (file)
@@ -8,6 +8,7 @@
 //
 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
 // Copyright 2003-2008 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
@@ -21,10 +22,12 @@ using System.IO;
 
 #if STATIC
 using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
+using BadImageFormat = IKVM.Reflection.BadImageFormatException;
 using IKVM.Reflection;
 using IKVM.Reflection.Emit;
 #else
 using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
+using BadImageFormat = System.BadImageFormatException;
 using System.Reflection;
 using System.Reflection.Emit;
 #endif
@@ -81,18 +84,19 @@ namespace Mono.CSharp {
                public abstract string[] ValidAttributeTargets { get; }
        };
 
-       public class Attribute : Expression
+       public class Attribute
        {
                public readonly string ExplicitTarget;
                public AttributeTargets Target;
                readonly ATypeNameExpression expression;
 
-               Arguments PosArguments;
-               Arguments NamedArguments;
+               Arguments pos_args, named_args;
 
                bool resolve_error;
                bool arg_resolved;
                readonly bool nameEscaped;
+               readonly Location loc;
+               public TypeSpec Type;   
 
                //
                // An attribute can be attached to multiple targets (e.g. multiple fields)
@@ -114,14 +118,44 @@ namespace Mono.CSharp {
                {
                        this.expression = expr;
                        if (args != null) {
-                               PosArguments = args[0];
-                               NamedArguments = args[1];
+                               pos_args = args[0];
+                               named_args = args[1];
                        }
                        this.loc = loc;
                        ExplicitTarget = target;
                        this.nameEscaped = nameEscaped;
                }
 
+               public Location Location {
+                       get {
+                               return loc;
+                       }
+               }
+
+               public Arguments NamedArguments {
+                       get {
+                               return named_args;
+                       }
+               }
+
+               public Arguments PositionalArguments {
+                       get {
+                               return pos_args;
+                       }
+               }
+
+               public bool ResolveError {
+                       get {
+                               return resolve_error;
+                       }
+               }
+
+               public ATypeNameExpression TypeExpression {
+                       get {
+                               return expression;
+                       }
+               }
+
                void AddModuleCharSet (ResolveContext rc)
                {
                        const string dll_import_char_set = "CharSet";
@@ -137,7 +171,7 @@ namespace Mono.CSharp {
                        }
 
                        if (NamedArguments == null)
-                               NamedArguments = new Arguments (1);
+                               named_args = new Arguments (1);
 
                        var value = Constant.CreateConstant (rc.Module.PredefinedTypes.CharSet.TypeSpec, rc.Module.DefaultCharSet, Location);
                        NamedArguments.Add (new NamedArgument (dll_import_char_set, loc, value));
@@ -146,8 +180,8 @@ namespace Mono.CSharp {
                public Attribute Clone ()
                {
                        Attribute a = new Attribute (ExplicitTarget, expression, null, loc, nameEscaped);
-                       a.PosArguments = PosArguments;
-                       a.NamedArguments = NamedArguments;
+                       a.pos_args = pos_args;
+                       a.named_args = NamedArguments;
                        return a;
                }
 
@@ -165,7 +199,7 @@ namespace Mono.CSharp {
                        }
 
                        // When re-attaching global attributes
-                       if (context is NamespaceEntry) {
+                       if (context is NamespaceContainer) {
                                this.targets[0] = target;
                                this.context = context;
                                return;
@@ -245,37 +279,28 @@ namespace Mono.CSharp {
                        }
                }
 
-               TypeSpec ResolvePossibleAttributeType (ATypeNameExpression expr, ref bool is_attr)
-               {
-                       TypeExpr te = expr.ResolveAsTypeTerminal (context, false);
-                       if (te == null)
-                               return null;
-
-                       TypeSpec t = te.Type;
-                       if (t.IsAttribute) {
-                               is_attr = true;
-                       } else {
-                               Report.SymbolRelatedToPreviousError (t);
-                               Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
-                       }
-                       return t;
-               }
-
                /// <summary>
                ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
                /// </summary>
-               void ResolveAttributeType ()
+               void ResolveAttributeType (bool comparisonOnly)
                {
                        SessionReportPrinter resolve_printer = new SessionReportPrinter ();
-                       ReportPrinter prev_recorder = context.Module.Compiler.Report.SetPrinter (resolve_printer);
+                       ReportPrinter prev_recorder = Report.SetPrinter (resolve_printer);
 
                        bool t1_is_attr = false;
                        bool t2_is_attr = false;
                        TypeSpec t1, t2;
                        ATypeNameExpression expanded = null;
 
+                       // TODO: Additional warnings such as CS0436 are swallowed because we don't
+                       // print on success
+
                        try {
-                               t1 = ResolvePossibleAttributeType (expression, ref t1_is_attr);
+                               t1 = expression.ResolveAsType (context);
+                               if (t1 != null)
+                                       t1_is_attr = t1.IsAttribute;
+
+                               resolve_printer.EndSession ();
 
                                if (nameEscaped) {
                                        t2 = null;
@@ -283,18 +308,21 @@ namespace Mono.CSharp {
                                        expanded = (ATypeNameExpression) expression.Clone (null);
                                        expanded.Name += "Attribute";
 
-                                       t2 = ResolvePossibleAttributeType (expanded, ref t2_is_attr);
+                                       t2 = expanded.ResolveAsType (context);
+                                       if (t2 != null)
+                                               t2_is_attr = t2.IsAttribute;
                                }
-
-                               resolve_printer.EndSession ();
                        } finally {
                                context.Module.Compiler.Report.SetPrinter (prev_recorder);
                        }
 
-                       if (t1_is_attr && t2_is_attr) {
-                               Report.Error (1614, Location, "`{0}' is ambiguous between `{1}' and `{2}'. Use either `@{0}' or `{0}Attribute'",
-                                       GetSignatureForError (), expression.GetSignatureForError (), expanded.GetSignatureForError ());
-                               resolve_error = true;
+                       if (t1_is_attr && t2_is_attr && t1 != t2) {
+                               if (!comparisonOnly) {
+                                       Report.Error (1614, Location, "`{0}' is ambiguous between `{1}' and `{2}'. Use either `@{0}' or `{0}Attribute'",
+                                               GetSignatureForError (), expression.GetSignatureForError (), expanded.GetSignatureForError ());
+                                       resolve_error = true;
+                               }
+
                                return;
                        }
 
@@ -308,18 +336,36 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       resolve_printer.Merge (prev_recorder);
+                       if (comparisonOnly)
+                               return;
+
                        resolve_error = true;
+
+                       if (t1 != null) {
+                               resolve_printer.Merge (prev_recorder);
+
+                               Report.SymbolRelatedToPreviousError (t1);
+                               Report.Error (616, Location, "`{0}': is not an attribute class", t1.GetSignatureForError ());
+                               return;
+                       }
+
+                       if (t2 != null) {
+                               Report.SymbolRelatedToPreviousError (t2);
+                               Report.Error (616, Location, "`{0}': is not an attribute class", t2.GetSignatureForError ());
+                               return;
+                       }
+
+                       resolve_printer.Merge (prev_recorder);
                }
 
-               public TypeSpec ResolveType ()
+               public TypeSpec ResolveTypeForComparison ()
                {
                        if (Type == null && !resolve_error)
-                               ResolveAttributeType ();
+                               ResolveAttributeType (true);
                        return Type;
                }
 
-               public override string GetSignatureForError ()
+               public string GetSignatureForError ()
                {
                        if (Type != null)
                                return TypeManager.CSharpName (Type);
@@ -330,7 +376,7 @@ namespace Mono.CSharp {
                public bool HasSecurityAttribute {
                        get {
                                PredefinedAttribute pa = context.Module.PredefinedAttributes.Security;
-                               return pa.IsDefined && TypeSpec.IsBaseClass (type, pa.TypeSpec, false);
+                               return pa.IsDefined && TypeSpec.IsBaseClass (Type, pa.TypeSpec, false);
                        }
                }
 
@@ -391,7 +437,7 @@ namespace Mono.CSharp {
                        arg_resolved = true;
 
                        if (Type == null) {
-                               ResolveAttributeType ();
+                               ResolveAttributeType (false);
                                if (Type == null)
                                        return null;
                        }
@@ -410,14 +456,14 @@ namespace Mono.CSharp {
 
                        MethodSpec ctor;
                        // Try if the attribute is simple and has been resolved before
-                       if (PosArguments != null || !context.Module.AttributeConstructorCache.TryGetValue (Type, out ctor)) {
+                       if (pos_args != null || !context.Module.AttributeConstructorCache.TryGetValue (Type, out ctor)) {
                                rc = CreateResolveContext ();
                                ctor = ResolveConstructor (rc);
                                if (ctor == null) {
                                        return null;
                                }
 
-                               if (PosArguments == null && ctor.Parameters.IsEmpty)
+                               if (pos_args == null && ctor.Parameters.IsEmpty)
                                        context.Module.AttributeConstructorCache.Add (Type, ctor);
                        }
 
@@ -425,7 +471,7 @@ namespace Mono.CSharp {
                        // Add [module: DefaultCharSet] to all DllImport import attributes
                        //
                        var module = context.Module;
-                       if (Type == module.PredefinedAttributes.DllImport && module.HasDefaultCharSet) {
+                       if ((Type == module.PredefinedAttributes.DllImport || Type == module.PredefinedAttributes.UnmanagedFunctionPointer) && module.HasDefaultCharSet) {
                                if (rc == null)
                                        rc = CreateResolveContext ();
 
@@ -446,16 +492,16 @@ namespace Mono.CSharp {
 
                MethodSpec ResolveConstructor (ResolveContext ec)
                {
-                       if (PosArguments != null) {
+                       if (pos_args != null) {
                                bool dynamic;
-                               PosArguments.Resolve (ec, out dynamic);
+                               pos_args.Resolve (ec, out dynamic);
                                if (dynamic) {
                                        Error_AttributeArgumentIsDynamic (ec.MemberContext, loc);
                                        return null;
                                }
                        }
 
-                       return ConstructorLookup (ec, Type, ref PosArguments, loc);
+                       return Expression.ConstructorLookup (ec, Type, ref pos_args, loc);
                }
 
                bool ResolveNamedArguments (ResolveContext ec)
@@ -476,10 +522,10 @@ namespace Mono.CSharp {
 
                                a.Resolve (ec);
 
-                               Expression member = Expression.MemberLookup (ec, false, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
+                               Expression member = Expression.MemberLookup (ec, false, Type, name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
 
                                if (member == null) {
-                                       member = Expression.MemberLookup (ec, true, Type, name, 0, MemberLookupRestrictions.ExactArity, loc);
+                                       member = Expression.MemberLookup (ec, true, Type, name, 0, Expression.MemberLookupRestrictions.ExactArity, loc);
 
                                        if (member != null) {
                                                // TODO: ec.Report.SymbolRelatedToPreviousError (member);
@@ -615,7 +661,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return DefaultUsageAttribute;
 
-                       AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)((Constant) PosArguments [0].Expr).GetValue ());
+                       AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets) ((Constant) pos_args[0].Expr).GetValue ());
 
                        var field = GetNamedValue ("AllowMultiple") as BoolConstant;
                        if (field != null)
@@ -638,10 +684,10 @@ namespace Mono.CSharp {
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve ();
 
-                       if (resolve_error)
+                       if (resolve_error || pos_args.Count != 1 || !(pos_args[0].Expr is Constant))
                                return null;
 
-                       return ((Constant) PosArguments [0].Expr).GetValue () as string;
+                       return ((Constant) pos_args[0].Expr).GetValue () as string;
                }
 
                /// <summary>
@@ -657,7 +703,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return null;
 
-                       return ((Constant) PosArguments[0].Expr).GetValue () as string;
+                       return ((Constant) pos_args[0].Expr).GetValue () as string;
                }
 
                /// <summary>
@@ -667,7 +713,7 @@ namespace Mono.CSharp {
                {
                        if (!arg_resolved) {
                                // corlib only case when obsolete is used before is resolved
-                               var c = type.MemberDefinition as Class;
+                               var c = Type.MemberDefinition as Class;
                                if (c != null && !c.HasMembersDefined)
                                        c.Define ();
                                
@@ -679,14 +725,14 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return null;
 
-                       if (PosArguments == null)
+                       if (pos_args == null)
                                return new ObsoleteAttribute ();
 
-                       string msg = ((Constant) PosArguments[0].Expr).GetValue () as string;
-                       if (PosArguments.Count == 1)
+                       string msg = ((Constant) pos_args[0].Expr).GetValue () as string;
+                       if (pos_args.Count == 1)
                                return new ObsoleteAttribute (msg);
 
-                       return new ObsoleteAttribute (msg, ((BoolConstant) PosArguments[1].Expr).Value);
+                       return new ObsoleteAttribute (msg, ((BoolConstant) pos_args[1].Expr).Value);
                }
 
                /// <summary>
@@ -704,7 +750,7 @@ namespace Mono.CSharp {
                        if (resolve_error)
                                return false;
 
-                       return ((BoolConstant) PosArguments[0].Expr).Value;
+                       return ((BoolConstant) pos_args[0].Expr).Value;
                }
 
                public TypeSpec GetCoClassAttributeValue ()
@@ -790,7 +836,7 @@ namespace Mono.CSharp {
 
                System.Security.Permissions.SecurityAction GetSecurityActionValue ()
                {
-                       return (SecurityAction) ((Constant) PosArguments[0].Expr).GetValue ();
+                       return (SecurityAction) ((Constant) pos_args[0].Expr).GetValue ();
                }
 
                /// <summary>
@@ -800,9 +846,9 @@ namespace Mono.CSharp {
                public void ExtractSecurityPermissionSet (MethodSpec ctor, ref SecurityType permissions)
                {
 #if STATIC
-                       object[] values = new object [PosArguments.Count];
+                       object[] values = new object[pos_args.Count];
                        for (int i = 0; i < values.Length; ++i)
-                               values [i] = ((Constant) PosArguments [i].Expr).GetValue ();
+                               values[i] = ((Constant) pos_args[i].Expr).GetValue ();
 
                        PropertyInfo[] prop;
                        object[] prop_values;
@@ -843,7 +889,7 @@ namespace Mono.CSharp {
 
                public CharSet GetCharSetValue ()
                {
-                       return (CharSet)System.Enum.Parse (typeof (CharSet), ((Constant) PosArguments [0].Expr).GetValue ().ToString ());
+                       return (CharSet) System.Enum.Parse (typeof (CharSet), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                }
 
                public bool HasField (string fieldName)
@@ -863,16 +909,21 @@ namespace Mono.CSharp {
                // Returns true for MethodImplAttribute with MethodImplOptions.InternalCall value
                // 
                public bool IsInternalCall ()
+               {
+                       return (GetMethodImplOptions () & MethodImplOptions.InternalCall) != 0;
+               }
+
+               public MethodImplOptions GetMethodImplOptions ()
                {
                        MethodImplOptions options = 0;
-                       if (PosArguments.Count == 1) {
-                               options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                       if (pos_args.Count == 1) {
+                               options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                        } else if (HasField ("Value")) {
                                var named = GetNamedValue ("Value");
                                options = (MethodImplOptions) System.Enum.Parse (typeof (MethodImplOptions), named.GetValue ().ToString ());
                        }
 
-                       return (options & MethodImplOptions.InternalCall) != 0;
+                       return options;
                }
 
                //
@@ -880,19 +931,19 @@ namespace Mono.CSharp {
                // 
                public bool IsExplicitLayoutKind ()
                {
-                       if (PosArguments == null || PosArguments.Count != 1)
+                       if (pos_args == null || pos_args.Count != 1)
                                return false;
 
-                       var value = (LayoutKind) System.Enum.Parse (typeof (LayoutKind), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                       var value = (LayoutKind) System.Enum.Parse (typeof (LayoutKind), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                        return value == LayoutKind.Explicit;
                }
 
                public Expression GetParameterDefaultValue ()
                {
-                       if (PosArguments == null)
+                       if (pos_args == null)
                                return null;
 
-                       return PosArguments[0].Expr;
+                       return pos_args[0].Expr;
                }
 
                public override bool Equals (object obj)
@@ -906,7 +957,7 @@ namespace Mono.CSharp {
 
                public override int GetHashCode ()
                {
-                       return type.GetHashCode () ^ Target.GetHashCode ();
+                       return Type.GetHashCode () ^ Target.GetHashCode ();
                }
 
                /// <summary>
@@ -929,22 +980,23 @@ namespace Mono.CSharp {
                        }
 
                        byte[] cdata;
-                       if (PosArguments == null && named_values == null) {
+                       if (pos_args == null && named_values == null) {
                                cdata = AttributeEncoder.Empty;
                        } else {
                                AttributeEncoder encoder = new AttributeEncoder ();
 
-                               if (PosArguments != null) {
+                               if (pos_args != null) {
                                        var param_types = ctor.Parameters.Types;
-                                       for (int j = 0; j < PosArguments.Count; ++j) {
+                                       for (int j = 0; j < pos_args.Count; ++j) {
                                                var pt = param_types[j];
-                                               var arg_expr = PosArguments[j].Expr;
+                                               var arg_expr = pos_args[j].Expr;
                                                if (j == 0) {
-                                                       if (Type == predefined.IndexerName || Type == predefined.Conditional) {
-                                                               string v = ((StringConstant) arg_expr).Value;
-                                                               if (!Tokenizer.IsValidIdentifier (v) || Tokenizer.IsKeyword (v)) {
+                                                       if ((Type == predefined.IndexerName || Type == predefined.Conditional) && arg_expr is Constant) {
+                                                               string v = ((Constant) arg_expr).GetValue () as string;
+                                                               if (!Tokenizer.IsValidIdentifier (v) || (Type == predefined.IndexerName && Tokenizer.IsKeyword (v))) {
                                                                        context.Module.Compiler.Report.Error (633, arg_expr.Location,
                                                                                "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
+                                                                       return;
                                                                }
                                                        } else if (Type == predefined.Guid) {
                                                                try {
@@ -961,17 +1013,17 @@ namespace Mono.CSharp {
                                                                                "System.AttributeUsage");
                                                                }
                                                        } else if (Type == predefined.MarshalAs) {
-                                                               if (PosArguments.Count == 1) {
-                                                                       var u_type = (UnmanagedType) System.Enum.Parse (typeof (UnmanagedType), ((Constant) PosArguments[0].Expr).GetValue ().ToString ());
+                                                               if (pos_args.Count == 1) {
+                                                                       var u_type = (UnmanagedType) System.Enum.Parse (typeof (UnmanagedType), ((Constant) pos_args[0].Expr).GetValue ().ToString ());
                                                                        if (u_type == UnmanagedType.ByValArray && !(Owner is FieldBase)) {
                                                                                Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
                                                                        }
                                                                }
                                                        } else if (Type == predefined.DllImport) {
-                                                               if (PosArguments.Count == 1) {
-                                                                       var value = ((Constant) PosArguments[0].Expr).GetValue () as string;
+                                                               if (pos_args.Count == 1 && pos_args[0].Expr is Constant) {
+                                                                       var value = ((Constant) pos_args[0].Expr).GetValue () as string;
                                                                        if (string.IsNullOrEmpty (value))
-                                                                               Error_AttributeEmitError ("DllName cannot be empty");
+                                                                               Error_AttributeEmitError ("DllName cannot be empty or null");
                                                                }
                                                        } else if (Type == predefined.MethodImpl && pt.BuiltinType == BuiltinTypeSpec.Type.Short &&
                                                                !System.Enum.IsDefined (typeof (MethodImplOptions), ((Constant) arg_expr).GetValue ().ToString ())) {
@@ -1007,6 +1059,9 @@ namespace Mono.CSharp {
                                foreach (Attributable target in targets)
                                        target.ApplyAttributeBuilder (this, ctor, cdata, predefined);
                        } catch (Exception e) {
+                               if (e is BadImageFormat && Report.Errors > 0)
+                                       return;
+
                                Error_AttributeEmitError (e.Message);
                                return;
                        }
@@ -1029,8 +1084,8 @@ namespace Mono.CSharp {
 
                        // Here we are testing attribute arguments for array usage (error 3016)
                        if (Owner.IsClsComplianceRequired ()) {
-                               if (PosArguments != null)
-                                       PosArguments.CheckArrayAsAttribute (context.Module.Compiler);
+                               if (pos_args != null)
+                                       pos_args.CheckArrayAsAttribute (context.Module.Compiler);
                        
                                if (NamedArguments == null)
                                        return;
@@ -1041,10 +1096,10 @@ namespace Mono.CSharp {
 
                private Expression GetValue () 
                {
-                       if (PosArguments == null || PosArguments.Count < 1)
+                       if (pos_args == null || pos_args.Count < 1)
                                return null;
 
-                       return PosArguments [0].Expr;
+                       return pos_args[0].Expr;
                }
 
                public string GetString () 
@@ -1070,21 +1125,6 @@ namespace Mono.CSharp {
                                return null;
                        return e.TypeArgument;
                }
-
-               public override Expression CreateExpressionTree (ResolveContext ec)
-               {
-                       throw new NotSupportedException ("ET");
-               }
-
-               protected override Expression DoResolve (ResolveContext ec)
-               {
-                       throw new NotImplementedException ();
-               }
-
-               public override void Emit (EmitContext ec)
-               {
-                       throw new NotImplementedException ();
-               }
        }
        
        public class Attributes
@@ -1140,7 +1180,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public void ConvertGlobalAttributes (TypeContainer member, NamespaceEntry currentNamespace, bool isGlobal)
+               public void ConvertGlobalAttributes (TypeContainer member, NamespaceContainer currentNamespace, bool isGlobal)
                {
                        var member_explicit_targets = member.ValidAttributeTargets;
                        for (int i = 0; i < Attrs.Count; ++i) {
@@ -1165,6 +1205,16 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool HasResolveError()
+               {
+                       foreach (var a in Attrs) {
+                               if (a.ResolveError)
+                                       return true;
+                       }
+
+                       return false;
+               }
+
                public Attribute Search (PredefinedAttribute t)
                {
                        return Search (null, t);
@@ -1176,7 +1226,7 @@ namespace Mono.CSharp {
                                if (explicitTarget != null && a.ExplicitTarget != explicitTarget)
                                        continue;
 
-                               if (a.ResolveType () == t)
+                               if (a.ResolveTypeForComparison () == t)
                                        return a;
                        }
                        return null;
@@ -1190,7 +1240,7 @@ namespace Mono.CSharp {
                        List<Attribute> ar = null;
 
                        foreach (Attribute a in Attrs) {
-                               if (a.ResolveType () == t) {
+                               if (a.ResolveTypeForComparison () == t) {
                                        if (ar == null)
                                                ar = new List<Attribute> (Attrs.Count);
                                        ar.Add (a);
@@ -1429,6 +1479,12 @@ namespace Mono.CSharp {
                        Encode (type.MemberDefinition.IsImported ? old_type.AssemblyQualifiedName : old_type.FullName);
                }
 
+               public void EncodeTypeName (TypeContainer type)
+               {
+                       Encode (type.GetSignatureForMetadata ());
+               }
+
+
                //
                // Encodes single property named argument per call
                //
@@ -1515,56 +1571,6 @@ namespace Mono.CSharp {
        /// </summary>
        static class AttributeTester
        {
-               public enum Result {
-                       Ok,
-                       RefOutArrayError,
-                       ArrayArrayError
-               }
-
-               /// <summary>
-               /// Returns true if parameters of two compared methods are CLS-Compliant.
-               /// It tests differing only in ref or out, or in array rank.
-               /// </summary>
-               public static Result AreOverloadedMethodParamsClsCompliant (AParametersCollection pa, AParametersCollection pb) 
-               {
-                       TypeSpec [] types_a = pa.Types;
-                       TypeSpec [] types_b = pb.Types;
-                       if (types_a == null || types_b == null)
-                               return Result.Ok;
-
-                       if (types_a.Length != types_b.Length)
-                               return Result.Ok;
-
-                       Result result = Result.Ok;
-                       for (int i = 0; i < types_b.Length; ++i) {
-                               TypeSpec aType = types_a [i];
-                               TypeSpec bType = types_b [i];
-
-                               var ac_a = aType as ArrayContainer;
-                               var ac_b = aType as ArrayContainer;
-
-                               if (ac_a != null && ac_b != null) {
-                                       if (ac_a.Rank != ac_b.Rank && ac_a.Element == ac_b.Element) {
-                                               result = Result.RefOutArrayError;
-                                               continue;
-                                       }
-
-                                       if (ac_a.Element.IsArray || ac_b.Element.IsArray) {
-                                               result = Result.ArrayArrayError;
-                                               continue;
-                                       }
-                               }
-
-                               if (aType != bType)
-                                       return Result.Ok;
-
-                               const Parameter.Modifier out_ref_mod = (Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
-                               if ((pa.FixedParameters[i].ModFlags & out_ref_mod) != (pb.FixedParameters[i].ModFlags & out_ref_mod))
-                                       result = Result.RefOutArrayError;
-                       }
-                       return result;
-               }
-
                /// <summary>
                /// Common method for Obsolete error/warning reporting.
                /// </summary>
@@ -1615,8 +1621,6 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute DefaultParameterValue;
                public readonly PredefinedAttribute OptionalParameter;
                public readonly PredefinedAttribute UnverifiableCode;
-
-               // New in .NET 2.0
                public readonly PredefinedAttribute DefaultCharset;
                public readonly PredefinedAttribute TypeForwarder;
                public readonly PredefinedAttribute FixedBuffer;
@@ -1625,6 +1629,8 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute RuntimeCompatibility;
                public readonly PredefinedAttribute DebuggerHidden;
                public readonly PredefinedAttribute UnsafeValueType;
+               public readonly PredefinedAttribute UnmanagedFunctionPointer;
+               public readonly PredefinedDebuggerBrowsableAttribute DebuggerBrowsable;
 
                // New in .NET 3.5
                public readonly PredefinedAttribute Extension;
@@ -1632,6 +1638,10 @@ namespace Mono.CSharp {
                // New in .NET 4.0
                public readonly PredefinedDynamicAttribute Dynamic;
 
+               // New in .NET 4.5
+               public readonly PredefinedStateMachineAttribute AsyncStateMachine;
+               public readonly PredefinedStateMachineAttribute IteratorStateMachine;
+
                //
                // Optional types which are used as types and for member lookup
                //
@@ -1639,13 +1649,16 @@ namespace Mono.CSharp {
                public readonly PredefinedDecimalAttribute DecimalConstant;
                public readonly PredefinedAttribute StructLayout;
                public readonly PredefinedAttribute FieldOffset;
+               public readonly PredefinedAttribute CallerMemberNameAttribute;
+               public readonly PredefinedAttribute CallerLineNumberAttribute;
+               public readonly PredefinedAttribute CallerFilePathAttribute;
 
                public PredefinedAttributes (ModuleContainer module)
                {
                        ParamArray = new PredefinedAttribute (module, "System", "ParamArrayAttribute");
                        Out = new PredefinedAttribute (module, "System.Runtime.InteropServices", "OutAttribute");
-                       ParamArray.Resolve (Location.Null);
-                       Out.Resolve (Location.Null);
+                       ParamArray.Resolve ();
+                       Out.Resolve ();
 
                        Obsolete = new PredefinedAttribute (module, "System", "ObsoleteAttribute");
                        DllImport = new PredefinedAttribute (module, "System.Runtime.InteropServices", "DllImportAttribute");
@@ -1678,6 +1691,8 @@ namespace Mono.CSharp {
                        RuntimeCompatibility = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
                        DebuggerHidden = new PredefinedAttribute (module, "System.Diagnostics", "DebuggerHiddenAttribute");
                        UnsafeValueType = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "UnsafeValueTypeAttribute");
+                       UnmanagedFunctionPointer = new PredefinedAttribute (module, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
+                       DebuggerBrowsable = new PredefinedDebuggerBrowsableAttribute (module, "System.Diagnostics", "DebuggerBrowsableAttribute");
 
                        Extension = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "ExtensionAttribute");
 
@@ -1688,6 +1703,15 @@ namespace Mono.CSharp {
                        StructLayout = new PredefinedAttribute (module, "System.Runtime.InteropServices", "StructLayoutAttribute");
                        FieldOffset = new PredefinedAttribute (module, "System.Runtime.InteropServices", "FieldOffsetAttribute");
 
+                       AsyncStateMachine = new PredefinedStateMachineAttribute (module, "System.Runtime.CompilerServices", "AsyncStateMachineAttribute");
+                       IteratorStateMachine = new PredefinedStateMachineAttribute (module, "System.Runtime.CompilerServices", "IteratorStateMachineAttribute") {
+                               IsIterator = true
+                       };
+
+                       CallerMemberNameAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerMemberNameAttribute");
+                       CallerLineNumberAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerLineNumberAttribute");
+                       CallerFilePathAttribute = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CallerFilePathAttribute");
+
                        // TODO: Should define only attributes which are used for comparison
                        const System.Reflection.BindingFlags all_fields = System.Reflection.BindingFlags.Public |
                                System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly;
@@ -1761,22 +1785,12 @@ namespace Mono.CSharp {
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (FieldBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                public void EmitAttribute (TypeBuilder builder)
                {
                        if (ResolveBuilder ())
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (TypeBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                public void EmitAttribute (AssemblyBuilder builder)
                {
                        if (ResolveBuilder ())
@@ -1795,11 +1809,6 @@ namespace Mono.CSharp {
                                builder.SetCustomAttribute (GetCtorMetaInfo (), AttributeEncoder.Empty);
                }
 
-               public void EmitAttribute (ParameterBuilder builder, AttributeEncoder argsEncoded)
-               {
-                       builder.SetCustomAttribute (GetCtorMetaInfo (), argsEncoded.ToArray ());
-               }
-
                ConstructorInfo GetCtorMetaInfo ()
                {
                        return (ConstructorInfo) ctor.GetMetaInfo ();
@@ -1821,6 +1830,27 @@ namespace Mono.CSharp {
                }
        }
 
+       public class PredefinedDebuggerBrowsableAttribute : PredefinedAttribute
+       {
+               public PredefinedDebuggerBrowsableAttribute (ModuleContainer module, string ns, string name)
+                       : base (module, ns, name)
+               {
+               }
+
+               public void EmitAttribute (FieldBuilder builder, System.Diagnostics.DebuggerBrowsableState state)
+               {
+                       var ctor = module.PredefinedMembers.DebuggerBrowsableAttributeCtor.Get ();
+                       if (ctor == null)
+                               return;
+
+                       AttributeEncoder encoder = new AttributeEncoder ();
+                       encoder.Encode ((int) state);
+                       encoder.EncodeEmptyNamedArguments ();
+
+                       builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
+               }
+       }
+
        public class PredefinedDecimalAttribute : PredefinedAttribute
        {
                public PredefinedDecimalAttribute (ModuleContainer module, string ns, string name)
@@ -1865,6 +1895,34 @@ namespace Mono.CSharp {
                }
        }
 
+       public class PredefinedStateMachineAttribute : PredefinedAttribute
+       {
+               public PredefinedStateMachineAttribute (ModuleContainer module, string ns, string name)
+                       : base (module, ns, name)
+               {
+               }
+
+               public bool IsIterator { get; set; }
+
+               public void EmitAttribute (MethodBuilder builder, StateMachine type)
+               {
+                       var predefined_ctor = IsIterator ?
+                               module.PredefinedMembers.IteratorStateMachineAttributeCtor :
+                               module.PredefinedMembers.AsyncStateMachineAttributeCtor;
+
+                       var ctor = predefined_ctor.Get ();
+
+                       if (ctor == null)
+                               return;
+
+                       AttributeEncoder encoder = new AttributeEncoder ();
+                       encoder.EncodeTypeName (type);
+                       encoder.EncodeEmptyNamedArguments ();
+
+                       builder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
+               }
+       }
+
        public class PredefinedDynamicAttribute : PredefinedAttribute
        {
                MethodSpec tctor;