A type parameter can never be optional.
[mono.git] / mcs / gmcs / decl.cs
index 3baeb72074e862d9136d042c154f275f524d1051..800a622ad3e08bd69ffd748644d2270ce278f077 100755 (executable)
@@ -2,6 +2,7 @@
 // decl.cs: Declaration base class for structs, classes, enums and interfaces.
 //
 // Author: Miguel de Icaza (miguel@gnu.org)
+//         Marek Safar (marek.safar@seznam.cz)
 //
 // Licensed under the terms of the GNU GPL
 //
@@ -13,6 +14,7 @@
 using System;
 using System.Text;
 using System.Collections;
+using System.Globalization;
 using System.Reflection.Emit;
 using System.Reflection;
 
@@ -260,11 +262,17 @@ namespace Mono.CSharp {
 
                [Flags]
                public enum Flags {
-                       Obsolete_Undetected = 1,                // Obsolete attribute has not beed detected yet
+                       Obsolete_Undetected = 1,                // Obsolete attribute has not been detected yet
                        Obsolete = 1 << 1,                      // Type has obsolete attribute
                        ClsCompliance_Undetected = 1 << 2,      // CLS Compliance has not been detected yet
                        ClsCompliant = 1 << 3,                  // Type is CLS Compliant
-                       CloseTypeCreated = 1 << 4               // Tracks whether we have Closed the type
+                       CloseTypeCreated = 1 << 4,              // Tracks whether we have Closed the type
+                       HasCompliantAttribute_Undetected = 1 << 5,      // Presence of CLSCompliantAttribute has not been detected
+                       HasClsCompliantAttribute = 1 << 6,                      // Type has CLSCompliantAttribute
+                       ClsCompliantAttributeTrue = 1 << 7,                     // Type has CLSCompliant (true)
+                       Excluded_Undetected = 1 << 8,           // Conditional attribute has not been detected yet
+                       Excluded = 1 << 9                                       // Method is conditional
+
                }
   
                /// <summary>
@@ -278,7 +286,22 @@ namespace Mono.CSharp {
                        Name = name.GetName (!(this is GenericMethod) && !(this is Method));
                        MemberName = name;
                        Location = loc;
-                       caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected;
+                       caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
+               }
+
+               /// <summary>
+               /// Tests presence of ObsoleteAttribute and report proper error
+               /// </summary>
+               protected void CheckUsageOfObsoleteAttribute (Type type)
+               {
+                       if (type == null)
+                               return;
+
+                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
+                       if (obsolete_attr == null)
+                               return;
+
+                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, Location);
                }
 
                public abstract bool Define (TypeContainer parent);
@@ -296,6 +319,8 @@ namespace Mono.CSharp {
                /// </summary>
                public virtual void Emit (TypeContainer container)
                {
+                       VerifyObsoleteAttribute ();
+
                        if (!RootContext.VerifyClsCompliance)
                                return;
 
@@ -320,28 +345,59 @@ namespace Mono.CSharp {
                        return false;
                }
 
+               /// <summary>
+               /// Returns instance of ObsoleteAttribute for this MemberCore
+               /// </summary>
+               public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
+               {
+                       // ((flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0) is slower, but why ?
+                       if ((caching_flags & Flags.Obsolete_Undetected) == 0 && (caching_flags & Flags.Obsolete) == 0) {
+                               return null;
+                       }
+
+                       caching_flags &= ~Flags.Obsolete_Undetected;
+
+                       if (OptAttributes == null)
+                               return null;
+
+                       // TODO: remove this allocation
+                       EmitContext ec = new EmitContext (ds.Parent, ds, ds.Location,
+                               null, null, ds.ModFlags, false);
+
+                       Attribute obsolete_attr = OptAttributes.Search (TypeManager.obsolete_attribute_type, ec);
+                       if (obsolete_attr == null)
+                               return null;
+
+                       ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute (ds);
+                       if (obsolete == null)
+                               return null;
+
+                       caching_flags |= Flags.Obsolete;
+                       return obsolete;
+               }
+
                /// <summary>
                /// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
                /// </summary>
-               public bool IsClsCompliaceRequired (DeclSpace container)
+               public override bool IsClsCompliaceRequired (DeclSpace container)
                {
                        if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
                                return (caching_flags & Flags.ClsCompliant) != 0;
 
-                       if (!IsExposedFromAssembly (container) || !GetClsCompliantAttributeValue (container)) {
+                       if (GetClsCompliantAttributeValue (container) && IsExposedFromAssembly (container)) {
                                caching_flags &= ~Flags.ClsCompliance_Undetected;
-                               return false;
+                               caching_flags |= Flags.ClsCompliant;
+                               return true;
                        }
 
                        caching_flags &= ~Flags.ClsCompliance_Undetected;
-                       caching_flags |= Flags.ClsCompliant;
-                       return true;
+                       return false;
                }
 
                /// <summary>
                /// Returns true when MemberCore is exposed from assembly.
                /// </summary>
-               protected virtual bool IsExposedFromAssembly (DeclSpace ds)
+               protected bool IsExposedFromAssembly (DeclSpace ds)
                {
                        if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
                                return false;
@@ -361,24 +417,24 @@ namespace Mono.CSharp {
                bool GetClsCompliantAttributeValue (DeclSpace ds)
                {
                        if (OptAttributes != null) {
-                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (ds);
+                               EmitContext ec = new EmitContext (ds.Parent, ds, ds.Location,
+                                                                 null, null, ds.ModFlags, false);
+                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (ec);
                                if (cls_attribute != null) {
+                                       caching_flags |= Flags.HasClsCompliantAttribute;
                                        return cls_attribute.GetClsCompliantAttributeValue (ds);
                                }
                        }
-
-                       return (ds.GetClsCompliantAttributeValue () & Flags.ClsCompliant) != 0;
+                       return ds.GetClsCompliantAttributeValue ();
                }
 
                /// <summary>
                /// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
                /// </summary>
-               protected bool HasClsCompliantAttribute (DeclSpace ds)
-               {
-                       if (OptAttributes == null)
-                               return false;
-
-                       return OptAttributes.GetClsCompliantAttribute (ds) != null;
+               protected bool HasClsCompliantAttribute {
+                       get {
+                               return (caching_flags & Flags.HasClsCompliantAttribute) != 0;
+                       }
                }
 
                /// <summary>
@@ -457,16 +513,20 @@ namespace Mono.CSharp {
                protected virtual bool VerifyClsCompliance (DeclSpace ds)
                {
                        if (!IsClsCompliaceRequired (ds)) {
+                               if (HasClsCompliantAttribute && !IsExposedFromAssembly (ds)) {
+                                       Report.Warning_T (3019, Location, GetSignatureForError ());
+                               }
                                return false;
                        }
 
                        if (!CodeGen.Assembly.IsClsCompliant) {
-                               if (HasClsCompliantAttribute (ds)) {
+                               if (HasClsCompliantAttribute) {
                                        Report.Error_T (3014, Location, GetSignatureForError ());
                                }
                        }
 
-                       if (Name[0] == '_') {
+                       int index = Name.LastIndexOf ('.');
+                       if (Name [index > 0 ? index + 1 : 0] == '_') {
                                Report.Error_T (3008, Location, GetSignatureForError () );
                        }
 
@@ -477,6 +537,8 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               protected abstract void VerifyObsoleteAttribute ();
+
        }
 
        /// <summary>
@@ -535,6 +597,8 @@ namespace Mono.CSharp {
 
                TypeContainer parent;
 
+               static string[] attribute_targets = new string [] { "type" };
+
                public DeclSpace (NamespaceEntry ns, TypeContainer parent, MemberName name,
                                  Attributes attrs, Location l)
                        : base (name, attrs, l)
@@ -770,8 +834,7 @@ namespace Mono.CSharp {
                public Type ResolveType (TypeExpr d, Location loc)
                {
                        if (!d.CheckAccessLevel (this)) {
-                               Report. Error (122, loc,  "`" + d.Name + "' " +
-                                      "is inaccessible because of its protection level");
+                               Report.Error_T (122, loc, d.Name);
                                return null;
                        }
 
@@ -1240,6 +1303,19 @@ namespace Mono.CSharp {
                                if (t != null)
                                        return t;
 
+                               if (name.IndexOf ('.') > 0)
+                                       continue;
+
+                               IAlias alias_value = ns.LookupAlias (name);
+                               if (alias_value != null) {
+                                       t = LookupInterfaceOrClass ("", alias_value.Name, out error);
+                                       if (error)
+                                               return null;
+
+                                       if (t != null)
+                                               return t;
+                               }
+
                                //
                                // Now check the using clause list
                                //
@@ -1285,35 +1361,59 @@ namespace Mono.CSharp {
                        get;
                }
 
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       try {
+                               TypeBuilder.SetCustomAttribute (cb);
+                       } catch (System.ArgumentException e) {
+                               Report.Warning (-21, a.Location,
+                                               "The CharSet named property on StructLayout\n"+
+                                               "\tdoes not work correctly on Microsoft.NET\n"+
+                                               "\tYou might want to remove the CharSet declaration\n"+
+                                               "\tor compile using the Mono runtime instead of the\n"+
+                                               "\tMicrosoft .NET runtime\n"+
+                                               "\tThe runtime gave the error: " + e);
+                       }
+               }
+
                /// <summary>
                /// Goes through class hierarchy and get value of first CLSCompliantAttribute that found.
                /// If no is attribute exists then return assembly CLSCompliantAttribute.
                /// </summary>
-               public Flags GetClsCompliantAttributeValue ()
+               public bool GetClsCompliantAttributeValue ()
                {
-                       if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
-                               return caching_flags;
+                       if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0)
+                               return (caching_flags & Flags.ClsCompliantAttributeTrue) != 0;
 
-                       caching_flags &= ~Flags.ClsCompliance_Undetected;
+                       caching_flags &= ~Flags.HasCompliantAttribute_Undetected;
 
                        if (OptAttributes != null) {
-                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (this);
+                               EmitContext ec = new EmitContext (parent, this, Location,
+                                                                 null, null, ModFlags, false);
+                               Attribute cls_attribute = OptAttributes.GetClsCompliantAttribute (ec);
                                if (cls_attribute != null) {
+                                       caching_flags |= Flags.HasClsCompliantAttribute;
                                        if (cls_attribute.GetClsCompliantAttributeValue (this)) {
-                                               caching_flags |= Flags.ClsCompliant;
+                                               caching_flags |= Flags.ClsCompliantAttributeTrue;
+                                               return true;
                                        }
-                                       return caching_flags;
+                                       return false;
                                }
                        }
 
                        if (parent == null) {
-                               if (CodeGen.Assembly.IsClsCompliant)
-                                       caching_flags |= Flags.ClsCompliant;
-                               return caching_flags;
+                               if (CodeGen.Assembly.IsClsCompliant) {
+                                       caching_flags |= Flags.ClsCompliantAttributeTrue;
+                                       return true;
+                               }
+                               return false;
                        }
 
-                       caching_flags |= (parent.GetClsCompliantAttributeValue () & Flags.ClsCompliant);
-                       return caching_flags;
+                       if (parent.GetClsCompliantAttributeValue ()) {
+                               caching_flags |= Flags.ClsCompliantAttributeTrue;
+                               return true;
+                       }
+                       return false;
                }
 
 
@@ -1330,7 +1430,7 @@ namespace Mono.CSharp {
                                        if (l != type_name.Length)
                                                continue;
 
-                                       if (String.Compare (Name, type_name, true) == 0 && 
+                                       if (String.Compare (Name, type_name, true, CultureInfo.InvariantCulture) == 0 && 
                                                AttributeTester.IsClsCompliant (TypeManager.all_imported_types [type_name] as Type)) {
                                                Report.SymbolRelatedToPreviousError ((Type)TypeManager.all_imported_types [type_name]);
                                                return false;
@@ -1343,7 +1443,7 @@ namespace Mono.CSharp {
                                if (l != name.Length)
                                        continue;
 
-                               if (String.Compare (Name, name, true) == 0) { 
+                               if (String.Compare (Name, name, true, CultureInfo.InvariantCulture) == 0) { 
 
                                        if (Name == name)
                                                continue;
@@ -1541,6 +1641,12 @@ namespace Mono.CSharp {
                                return new TypeExpression (TypeBuilder, Location);
                        }
                }
+
+               protected override string[] ValidAttributeTargets {
+                       get {
+                               return attribute_targets;
+                       }
+               }
        }
 
        /// <summary>