2009-02-09 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / attribute.cs
index 612f299403fd70fa8d4ed4409697e9760ef51b64..da6717218380af100ea14fe489e9feaa02be002f 100644 (file)
@@ -4,10 +4,10 @@
 // Author: Ravi Pratap (ravi@ximian.com)
 //         Marek Safar (marek.safar@seznam.cz)
 //
-// Licensed under the terms of the GNU GPL
-//
-// (C) 2001 Ximian, Inc (http://www.ximian.com)
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
+// Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+// Copyright 2003-2008 Novell, Inc.
 //
 
 using System;
@@ -75,7 +75,8 @@ namespace Mono.CSharp {
                public abstract string[] ValidAttributeTargets { get; }
        };
 
-       public class Attribute {
+       public class Attribute : Expression
+       {
                public readonly string ExplicitTarget;
                public AttributeTargets Target;
 
@@ -84,13 +85,9 @@ namespace Mono.CSharp {
                public readonly Expression LeftExpr;
                public readonly string Identifier;
 
-               readonly ArrayList PosArguments;
+               ArrayList PosArguments;
                ArrayList NamedArguments;
 
-               public readonly Location Location;
-
-               public Type Type;
-
                bool resolve_error;
                readonly bool nameEscaped;
 
@@ -121,11 +118,19 @@ namespace Mono.CSharp {
                                PosArguments = (ArrayList)args [0];
                                NamedArguments = (ArrayList)args [1];                           
                        }
-                       Location = loc;
+                       this.loc = loc;
                        ExplicitTarget = target;
                        this.nameEscaped = nameEscaped;
                }
 
+               public Attribute Clone ()
+               {
+                       Attribute a = new Attribute (ExplicitTarget, LeftExpr, Identifier, null, loc, nameEscaped);
+                       a.PosArguments = PosArguments;
+                       a.NamedArguments = NamedArguments;
+                       return a;
+               }
+
                static Attribute ()
                {
                        Reset ();
@@ -288,7 +293,7 @@ namespace Mono.CSharp {
                        return Type;
                }
 
-               public string GetSignatureForError ()
+               public override string GetSignatureForError ()
                {
                        if (Type != null)
                                return TypeManager.CSharpName (Type);
@@ -296,14 +301,26 @@ namespace Mono.CSharp {
                        return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
                }
 
-               bool IsValidArgumentType (Type t)
+               public bool HasSecurityAttribute {
+                       get {
+                               return TypeManager.security_attr_type != null &&
+                               TypeManager.IsSubclassOf (type, TypeManager.security_attr_type);
+                       }
+               }
+
+               public bool IsValidSecurityAttribute ()
+               {
+                       return HasSecurityAttribute && IsSecurityActionValid (false);
+               }
+
+               static bool IsValidArgumentType (Type t)
                {
                        if (t.IsArray)
-                               t = t.GetElementType ();
+                               t = TypeManager.GetElementType (t);
 
-                       return TypeManager.IsPrimitiveType (t) ||
+                       return t == TypeManager.string_type ||
+                               TypeManager.IsPrimitiveType (t) ||
                                TypeManager.IsEnumType (t) ||
-                               t == TypeManager.string_type ||
                                t == TypeManager.object_type ||
                                t == TypeManager.type_type;
                }
@@ -363,8 +380,12 @@ namespace Mono.CSharp {
                        }
 
                        Attributable owner = Owner;
-                       EmitContext ec = new EmitContext (owner.ResolveContext, owner.ResolveContext.DeclContainer, owner.ResolveContext.DeclContainer,
-                               Location, null, typeof (Attribute), owner.ResolveContext.DeclContainer.ModFlags, false);
+                       DeclSpace ds = owner.ResolveContext as DeclSpace;
+                       if (ds == null)
+                               ds = owner.ResolveContext.DeclContainer;
+                       
+                       EmitContext ec = new EmitContext (owner.ResolveContext, ds, ds,
+                               Location, null, typeof (Attribute), ds.ModFlags, false);
                        ec.IsAnonymousMethodAllowed = false;
 
                        ConstructorInfo ctor = ResolveConstructor (ec);
@@ -381,6 +402,10 @@ namespace Mono.CSharp {
 
                        CustomAttributeBuilder cb;
                        try {
+                               // SRE does not allow private ctor but we want to report all source code errors
+                               if (ctor.IsPrivate)
+                                       return null;
+
                                if (NamedArguments == null) {
                                        cb = new CustomAttributeBuilder (ctor, pos_values);
 
@@ -418,8 +443,8 @@ namespace Mono.CSharp {
                                                return null;
                                }
                        }
-
-                       MethodGroupExpr mg = Expression.MemberLookup (ec.ContainerType,
+                       
+                       MethodGroupExpr mg = MemberLookupFinal (ec, ec.ContainerType,
                                Type, ".ctor", MemberTypes.Constructor,
                                BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
                                Location) as MethodGroupExpr;
@@ -427,66 +452,25 @@ namespace Mono.CSharp {
                        if (mg == null)
                                return null;
 
-                       mg = mg.OverloadResolve (ec, PosArguments, false, Location);
+                       mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
                        if (mg == null)
                                return null;
                        
                        ConstructorInfo constructor = (ConstructorInfo)mg;
-
-                       // TODO: move to OverloadResolve
-                       ObsoleteAttribute oa = AttributeTester.GetMethodObsoleteAttribute (constructor);
-                       if (oa != null && !Owner.ResolveContext.IsInObsoleteScope) {
-                               AttributeTester.Report_ObsoleteMessage (oa, mg.GetSignatureForError (), mg.Location);
-                       }
-
                        if (PosArguments == null) {
                                pos_values = EmptyObject;
                                return constructor;
                        }
 
-                       ParameterData pd = TypeManager.GetParameterData (constructor);
+                       AParametersCollection pd = TypeManager.GetParameterData (constructor);
 
                        int pos_arg_count = PosArguments.Count;
-                       int last_real_param = pd.Count;
-
                        pos_values = new object [pos_arg_count];
-
-                       if (pd.HasParams) {
-                               // When the params is not filled we need to put one
-                               if (last_real_param > pos_arg_count) {
-                                       object [] new_pos_values = new object [pos_arg_count + 1];
-                                       pos_values.CopyTo (new_pos_values, 0);
-                                       new_pos_values [pos_arg_count] = new object [] {} ;
-                                       pos_values = new_pos_values;
-                               }
-                               last_real_param--;
-                       }
-
                        for (int j = 0; j < pos_arg_count; ++j) {
                                Argument a = (Argument) PosArguments [j];
 
-                               if (!a.Expr.GetAttributableValue (a.Type, out pos_values [j]))
+                               if (!a.Expr.GetAttributableValue (ec, a.Type, out pos_values [j]))
                                        return null;
-                               
-                               if (j < last_real_param)
-                                       continue;
-                               
-                               if (j == last_real_param) {
-                                       object [] array = new object [pos_arg_count - last_real_param];
-                                       array [0] = pos_values [j];
-                                       pos_values [j] = array;
-                                       continue;
-                               }
-
-                               object [] params_array = (object []) pos_values [last_real_param];
-                               params_array [j - last_real_param] = pos_values [j];
-                       }
-
-                       // Adjust the size of the pos_values if it had params
-                       if (last_real_param != pos_arg_count) {
-                               object [] new_pos_values = new object [last_real_param + 1];
-                               Array.Copy (pos_values, new_pos_values, last_real_param + 1);
-                               pos_values = new_pos_values;
                        }
 
                        // Here we do the checks which should be done by corlib or by runtime.
@@ -508,7 +492,8 @@ namespace Mono.CSharp {
                        }
 
                        if (Type == TypeManager.indexer_name_type || Type == TypeManager.conditional_attribute_type) {
-                               if (!Tokenizer.IsValidIdentifier ((string)pos_values [0])) {
+                               string v = pos_values [0] as string;
+                               if (!Tokenizer.IsValidIdentifier (v) || Tokenizer.IsKeyword (v)) {
                                        Report.Error (633, ((Argument)PosArguments[0]).Expr.Location,
                                                "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
                                        return null;
@@ -516,13 +501,13 @@ namespace Mono.CSharp {
                        }
 
                        if (Type == TypeManager.methodimpl_attr_type && pos_values.Length == 1 &&
-                               pd.ParameterType (0) == TypeManager.short_type &&
+                               pd.Types [0] == TypeManager.short_type &&
                                !System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0].ToString ())) {
                                Error_AttributeEmitError ("Incorrect argument value.");
                                return null;
                        }
 
-                       return (ConstructorInfo)constructor;
+                       return constructor;
                }
 
                protected virtual bool ResolveNamedArguments (EmitContext ec)
@@ -540,7 +525,7 @@ namespace Mono.CSharp {
                                string member_name = (string) de.Key;
 
                                if (seen_names.Contains(member_name)) {
-                                       Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
+                                       Report.Error(643, Location, "'{0}' duplicate named attribute argument", member_name);
                                        return false;
                                }                               
                                seen_names.Add(member_name);
@@ -593,14 +578,14 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       if (!IsValidArgumentType (pi.PropertyType)) {
+                                       if (!IsValidArgumentType (member.Type)) {
                                                Report.SymbolRelatedToPreviousError (pi);
                                                Error_InvalidNamedAgrumentType (member_name);
                                                return false;
                                        }
 
                                        object value;
-                                       if (!a.Expr.GetAttributableValue (pi.PropertyType, out value))
+                                       if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
                                                return false;
 
                                        PropertyBase pb = TypeManager.GetProperty (pi);
@@ -620,14 +605,14 @@ namespace Mono.CSharp {
                                                return false;
                                        }
 
-                                       if (!IsValidArgumentType (fi.FieldType)) {
+                                       if (!IsValidArgumentType (member.Type)) {
                                                Report.SymbolRelatedToPreviousError (fi);
                                                Error_InvalidNamedAgrumentType (member_name);
                                                return false;
                                        }
 
                                        object value;
-                                       if (!a.Expr.GetAttributableValue (fi.FieldType, out value))
+                                       if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
                                                return false;
 
                                        FieldBase fb = TypeManager.GetField (fi);
@@ -867,11 +852,11 @@ namespace Mono.CSharp {
                        // TODO: we can skip the first item
                        if (((IList) valid_targets).Contains (ExplicitTarget)) {
                                switch (ExplicitTarget) {
-                                       case "return": Target = AttributeTargets.ReturnValue; return true;
-                                       case "param": Target = AttributeTargets.Parameter; return true;
-                                       case "field": Target = AttributeTargets.Field; return true;
-                                       case "method": Target = AttributeTargets.Method; return true;
-                                       case "property": Target = AttributeTargets.Property; return true;
+                               case "return": Target = AttributeTargets.ReturnValue; return true;
+                               case "param": Target = AttributeTargets.Parameter; return true;
+                               case "field": Target = AttributeTargets.Field; return true;
+                               case "method": Target = AttributeTargets.Method; return true;
+                               case "property": Target = AttributeTargets.Property; return true;
                                }
                                throw new InternalErrorException ("Unknown explicit target: " + ExplicitTarget);
                        }
@@ -890,7 +875,7 @@ namespace Mono.CSharp {
                /// <summary>
                /// Tests permitted SecurityAction for assembly or other types
                /// </summary>
-               public bool CheckSecurityActionValidity (bool for_assembly)
+               protected virtual bool IsSecurityActionValid (bool for_assembly)
                {
                        SecurityAction action = GetSecurityActionValue ();
 
@@ -977,9 +962,11 @@ namespace Mono.CSharp {
                                if (prop_info_arr != null) {
                                        for (int i = 0; i < prop_info_arr.Length; ++i) {
                                                PropertyInfo emited_pi = prop_info_arr [i];
-                                               PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name, emited_pi.PropertyType);
+                                               // FIXME: We are missing return type filter
+                                               // TODO: pi can be null
+                                               PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name);
 
-                                               object old_instance = pi.PropertyType.IsEnum ?
+                                               object old_instance = TypeManager.IsEnumType (pi.PropertyType) ?
                                                        System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
                                                        prop_values_arr [i];
 
@@ -995,15 +982,15 @@ namespace Mono.CSharp {
                        // IS is correct because for corlib we are using an instance from old corlib
                        if (!(perm is System.Security.CodeAccessPermission)) {
                                switch (action) {
-                                       case SecurityAction.Demand:
-                                               action = (SecurityAction)13;
-                                               break;
-                                       case SecurityAction.LinkDemand:
-                                               action = (SecurityAction)14;
-                                               break;
-                                       case SecurityAction.InheritanceDemand:
-                                               action = (SecurityAction)15;
-                                               break;
+                               case SecurityAction.Demand:
+                                       action = (SecurityAction)13;
+                                       break;
+                               case SecurityAction.LinkDemand:
+                                       action = (SecurityAction)14;
+                                       break;
+                               case SecurityAction.InheritanceDemand:
+                                       action = (SecurityAction)15;
+                                       break;
                                }
                        }
 
@@ -1022,14 +1009,6 @@ namespace Mono.CSharp {
                        ps.AddPermission (perm);
                }
 
-               static object GetValue (object value)
-               {
-                       if (value is EnumConstant)
-                               return ((EnumConstant) value).GetValue ();
-                       else
-                               return value;                           
-               }
-
                public object GetPropertyValue (string name)
                {
                        if (prop_info_arr == null)
@@ -1043,25 +1022,12 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               object GetFieldValue (string name)
-               {
-                       int i;
-                       if (field_info_arr == null)
-                               return null;
-                       i = 0;
-                       foreach (FieldInfo fi in field_info_arr) {
-                               if (fi.Name == name)
-                                       return GetValue (field_values_arr [i]);
-                               i++;
-                       }
-                       return null;
-               }
-
                //
                // Theoretically, we can get rid of this, since FieldBuilder.SetCustomAttribute()
                // and ParameterBuilder.SetCustomAttribute() are supposed to handle this attribute.
                // However, we can't, since it appears that the .NET 1.1 SRE hangs when given a MarshalAsAttribute.
                //
+#if !NET_2_0
                public UnmanagedMarshal GetMarshal (Attributable attr)
                {
                        UnmanagedType UnmanagedType;
@@ -1135,11 +1101,48 @@ namespace Mono.CSharp {
                        }
                }
 
+               object GetFieldValue (string name)
+               {
+                       int i;
+                       if (field_info_arr == null)
+                               return null;
+                       i = 0;
+                       foreach (FieldInfo fi in field_info_arr) {
+                               if (fi.Name == name)
+                                       return GetValue (field_values_arr [i]);
+                               i++;
+                       }
+                       return null;
+               }
+
+               static object GetValue (object value)
+               {
+                       if (value is EnumConstant)
+                               return ((EnumConstant) value).GetValue ();
+                       else
+                               return value;                           
+               }
+               
+#endif
+
                public CharSet GetCharSetValue ()
                {
                        return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
                }
 
+               public bool HasField (string fieldName)
+               {
+                       if (field_info_arr == null)
+                               return false;
+
+                       foreach (FieldInfo fi in field_info_arr) {
+                               if (fi.Name == fieldName)
+                                       return true;
+                       }
+
+                       return false;
+               }
+
                public bool IsInternalMethodImplAttribute {
                        get {
                                if (Type != TypeManager.methodimpl_attr_type)
@@ -1233,7 +1236,7 @@ namespace Mono.CSharp {
                                                        return;
 
                                                if (arg.Type.IsArray) {
-                                                       Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
+                                                       Report.Warning (3016, 1, Location, "Arrays as attribute arguments are not CLS-compliant");
                                                        return;
                                                }
                                        }
@@ -1250,7 +1253,7 @@ namespace Mono.CSharp {
                                                return;
 
                                        if (arg.Type.IsArray) {
-                                               Report.Error (3016, Location, "Arrays as attribute arguments are not CLS-compliant");
+                                               Report.Warning (3016, 1, Location, "Arrays as attribute arguments are not CLS-compliant");
                                                return;
                                        }
                                }
@@ -1288,6 +1291,21 @@ namespace Mono.CSharp {
                                return null;
                        return e.TypeArgument;
                }
+
+               public override Expression CreateExpressionTree (EmitContext ec)
+               {
+                       throw new NotSupportedException ("ET");
+               }
+
+               public override Expression DoResolve (EmitContext ec)
+               {
+                       throw new NotImplementedException ();
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       throw new NotImplementedException ();
+               }
        }
        
 
@@ -1335,6 +1353,11 @@ namespace Mono.CSharp {
                        RootContext.ToplevelTypes.NamespaceEntry = ns;
                }
 
+               protected override bool IsSecurityActionValid (bool for_assembly)
+               {
+                       return base.IsSecurityActionValid (true);
+               }
+
                void Leave ()
                {
                        RootContext.ToplevelTypes.NamespaceEntry = null;
@@ -1399,6 +1422,15 @@ namespace Mono.CSharp {
                                a.AttachTo (attributable);
                }
 
+               public Attributes Clone ()
+               {
+                       ArrayList al = new ArrayList (Attrs.Count);
+                       foreach (Attribute a in Attrs)
+                               al.Add (a.Clone ());
+
+                       return new Attributes (al);
+               }
+
                /// <summary>
                /// Checks whether attribute target is valid for the current element
                /// </summary>
@@ -1478,10 +1510,7 @@ namespace Mono.CSharp {
                static PtrHashtable analyzed_types_obsolete;
                static PtrHashtable analyzed_member_obsolete;
                static PtrHashtable analyzed_method_excluded;
-
-#if NET_2_0
                static PtrHashtable fixed_buffer_cache;
-#endif
 
                static object TRUE = new object ();
                static object FALSE = new object ();
@@ -1501,9 +1530,7 @@ namespace Mono.CSharp {
                        analyzed_types_obsolete = new PtrHashtable ();
                        analyzed_member_obsolete = new PtrHashtable ();
                        analyzed_method_excluded = new PtrHashtable ();
-#if NET_2_0
                        fixed_buffer_cache = new PtrHashtable ();
-#endif
                }
 
                public enum Result {
@@ -1516,8 +1543,10 @@ namespace Mono.CSharp {
                /// 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 (Type[] types_a, Type[] types_b) 
+               public static Result AreOverloadedMethodParamsClsCompliant (AParametersCollection pa, AParametersCollection pb) 
                {
+                       Type [] types_a = pa.Types;
+                       Type [] types_b = pb.Types;
                        if (types_a == null || types_b == null)
                                return Result.Ok;
 
@@ -1530,8 +1559,8 @@ namespace Mono.CSharp {
                                Type bType = types_b [i];
 
                                if (aType.IsArray && bType.IsArray) {
-                                       Type a_el_type = aType.GetElementType ();
-                                       Type b_el_type = bType.GetElementType ();
+                                       Type a_el_type = TypeManager.GetElementType (aType);
+                                       Type b_el_type = TypeManager.GetElementType (bType);
                                        if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
                                                result = Result.RefOutArrayError;
                                                continue;
@@ -1543,25 +1572,10 @@ namespace Mono.CSharp {
                                        }
                                }
 
-                               Type aBaseType = aType;
-                               bool is_either_ref_or_out = false;
-
-                               if (aType.IsByRef || aType.IsPointer) {
-                                       aBaseType = aType.GetElementType ();
-                                       is_either_ref_or_out = true;
-                               }
-
-                               Type bBaseType = bType;
-                               if (bType.IsByRef || bType.IsPointer) 
-                               {
-                                       bBaseType = bType.GetElementType ();
-                                       is_either_ref_or_out = !is_either_ref_or_out;
-                               }
-
-                               if (aBaseType != bBaseType)
+                               if (aType != bType)
                                        return Result.Ok;
 
-                               if (is_either_ref_or_out)
+                               if (pa.FixedParameters [i].ModFlags != pb.FixedParameters [i].ModFlags)
                                        result = Result.RefOutArrayError;
                        }
                        return result;
@@ -1580,12 +1594,12 @@ namespace Mono.CSharp {
                                return type_compliance == TRUE;
 
                        if (type.IsPointer) {
-                               analyzed_types.Add (type, null);
+                               analyzed_types.Add (type, FALSE);
                                return false;
                        }
 
                        bool result;
-                       if (type.IsArray || type.IsByRef) {
+                       if (type.IsArray) {
                                result = IsClsCompliant (TypeManager.GetElementType (type));
                        } else if (TypeManager.IsNullableType (type)) {
                                result = IsClsCompliant (TypeManager.GetTypeArguments (type) [0]);
@@ -1602,7 +1616,7 @@ namespace Mono.CSharp {
                public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
                {
                        // Fixed buffer helper type is generated as value type
-                       if (!fi.FieldType.IsValueType)
+                       if (TypeManager.IsReferenceType (fi.FieldType))
                                return null;
 
                        FieldBase fb = TypeManager.GetField (fi);
@@ -1613,9 +1627,11 @@ namespace Mono.CSharp {
                        if (TypeManager.GetConstant (fi) != null)
                                return null;
 
-#if NET_2_0
                        object o = fixed_buffer_cache [fi];
                        if (o == null) {
+                               if (TypeManager.fixed_buffer_attr_type == null)
+                                       return null;
+
                                if (!fi.IsDefined (TypeManager.fixed_buffer_attr_type, false)) {
                                        fixed_buffer_cache.Add (fi, FALSE);
                                        return null;
@@ -1630,9 +1646,6 @@ namespace Mono.CSharp {
                                return null;
 
                        return (IFixedBuffer)o;
-#else
-                       return null;
-#endif         
                }
 
                public static void VerifyModulesClsCompliance ()
@@ -1644,7 +1657,7 @@ namespace Mono.CSharp {
                        // The first module is generated assembly
                        for (int i = 1; i < modules.Length; ++i) {
                                Module module = modules [i];
-                               if (!IsClsCompliant (module)) {
+                               if (!GetClsCompliantAttributeValue (module, null)) {
                                        Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
                                                      "to match the assembly", module.Name);
                                        return;
@@ -1665,13 +1678,20 @@ namespace Mono.CSharp {
                        return null;
                }
 
-               static bool IsClsCompliant (ICustomAttributeProvider attribute_provider
+               static bool GetClsCompliantAttributeValue (ICustomAttributeProvider attribute_provider, Assembly a
                {
-                       object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
-                       if (CompliantAttribute.Length == 0)
+                       if (TypeManager.cls_compliant_attribute_type == null)
                                return false;
 
-                       return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
+                       object[] cls_attr = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
+                       if (cls_attr.Length == 0) {
+                               if (a == null)
+                                       return false;
+
+                               return GetClsCompliantAttributeValue (a, null);
+                       }
+                       
+                       return ((CLSCompliantAttribute)cls_attr [0]).IsCompliant;
                }
 
                static bool AnalyzeTypeCompliance (Type type)
@@ -1685,18 +1705,7 @@ namespace Mono.CSharp {
                        if (TypeManager.IsGenericParameter (type))
                                return true;
 
-                       object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
-                       if (CompliantAttribute.Length == 0) 
-                               return IsClsCompliant (type.Assembly);
-
-                       return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
-               }
-
-               // Registers the core type as we assume that they will never be obsolete which
-               // makes things easier for bootstrap and faster (we don't need to query Obsolete attribute).
-               public static void RegisterNonObsoleteType (Type type)
-               {
-                       analyzed_types_obsolete [type] = FALSE;
+                       return GetClsCompliantAttributeValue (type, type.Assembly);
                }
 
                /// <summary>
@@ -1714,16 +1723,20 @@ namespace Mono.CSharp {
                        ObsoleteAttribute result = null;
                        if (TypeManager.HasElementType (type)) {
                                result = GetObsoleteAttribute (TypeManager.GetElementType (type));
-                       } else if (TypeManager.IsGenericParameter (type) || TypeManager.IsGenericType (type))
-                               return null;
-                       else {
+                       } else if (TypeManager.IsGenericParameter (type))
+                               result = null;  // TODO: throw new NotSupportedException ()
+                       else if (TypeManager.IsGenericType (type) && !TypeManager.IsGenericTypeDefinition (type)) {
+                               return GetObsoleteAttribute (TypeManager.DropGenericTypeArguments (type));
+                       } else {
                                DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
 
                                // Type is external, we can get attribute directly
                                if (type_ds == null) {
-                                       object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
-                                       if (attribute.Length == 1)
-                                               result = (ObsoleteAttribute)attribute [0];
+                                       if (TypeManager.obsolete_attribute_type != null) {
+                                               object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
+                                               if (attribute.Length == 1)
+                                                       result = (ObsoleteAttribute) attribute[0];
+                                       }
                                } else {
                                        result = type_ds.GetObsoleteAttribute ();
                                }
@@ -1773,6 +1786,9 @@ namespace Mono.CSharp {
                        if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
                                return null;
 
+                       if (TypeManager.obsolete_attribute_type == null)
+                               return null;
+
                        ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false)
                                as ObsoleteAttribute;
                        analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
@@ -1789,24 +1805,22 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       if (oa.Message == null) {
+                       if (oa.Message == null || oa.Message.Length == 0) {
                                Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
                                return;
                        }
                        Report.Warning (618, 2, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
                }
 
-               public static bool IsConditionalMethodExcluded (MethodBase mb)
+               public static bool IsConditionalMethodExcluded (MethodBase mb, Location loc)
                {
-                       mb = TypeManager.DropGenericMethodArguments (mb);
-                       // TODO: Has to be fixed for partial methods
-                       if ((mb is MethodBuilder) || (mb is ConstructorBuilder))
-                               return false;
-
                        object excluded = analyzed_method_excluded [mb];
                        if (excluded != null)
                                return excluded == TRUE ? true : false;
 
+                       if (TypeManager.conditional_attribute_type == null)
+                               return false;
+
                        ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true)
                                as ConditionalAttribute[];
                        if (attrs.Length == 0) {
@@ -1815,11 +1829,12 @@ namespace Mono.CSharp {
                        }
 
                        foreach (ConditionalAttribute a in attrs) {
-                               if (RootContext.AllDefines.Contains (a.ConditionString)) {
+                               if (loc.CompilationUnit.IsConditionalDefined (a.ConditionString)) {
                                        analyzed_method_excluded.Add (mb, FALSE);
                                        return false;
                                }
                        }
+
                        analyzed_method_excluded.Add (mb, TRUE);
                        return true;
                }
@@ -1828,7 +1843,7 @@ namespace Mono.CSharp {
                /// Analyzes class whether it has attribute which has ConditionalAttribute
                /// and its condition is not defined.
                /// </summary>
-               public static bool IsAttributeExcluded (Type type)
+               public static bool IsAttributeExcluded (Type type, Location loc)
                {
                        if (!type.IsClass)
                                return false;
@@ -1837,10 +1852,10 @@ namespace Mono.CSharp {
 
                        // TODO: add caching
                        // TODO: merge all Type bases attribute caching to one cache to save memory
-                       if (class_decl == null) {
+                       if (class_decl == null && TypeManager.conditional_attribute_type != null) {
                                object[] attributes = type.GetCustomAttributes (TypeManager.conditional_attribute_type, false);
                                foreach (ConditionalAttribute ca in attributes) {
-                                       if (RootContext.AllDefines.Contains (ca.ConditionString))
+                                       if (loc.CompilationUnit.IsConditionalDefined (ca.ConditionString))
                                                return false;
                                }
                                return attributes.Length > 0;
@@ -1853,13 +1868,16 @@ namespace Mono.CSharp {
                {
                        TypeContainer tc = TypeManager.LookupInterface (type);
                        if (tc == null) {
+                               if (TypeManager.coclass_attr_type == null)
+                                       return null;
+
                                object[] o = type.GetCustomAttributes (TypeManager.coclass_attr_type, false);
                                if (o.Length < 1)
                                        return null;
                                return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
                        }
 
-                       if (tc.OptAttributes == null)
+                       if (tc.OptAttributes == null || TypeManager.coclass_attr_type == null)
                                return null;
 
                        Attribute a = tc.OptAttributes.Search (TypeManager.coclass_attr_type);