2004-12-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / mcs / attribute.cs
index b4a7fcd454d46bf2895b92f9d8ac3006c2d4d028..51de7ad253295cced2fd2671c83a9d2deed1796a 100644 (file)
@@ -192,6 +192,7 @@ namespace Mono.CSharp {
                                      "Could not find attribute '" + Name 
                                      + "' (are you missing a using directive or an assembly reference ?)");
 
+                       resolve_error = true;
                        return null;
                }
 
@@ -265,7 +266,7 @@ namespace Mono.CSharp {
                        return false;
                }
                
-               public CustomAttributeBuilder Resolve (EmitContext ec)
+               public virtual CustomAttributeBuilder Resolve (EmitContext ec)
                {
                        if (resolve_error)
                                return null;
@@ -660,11 +661,10 @@ namespace Mono.CSharp {
                /// </summary>
                public string GetIndexerAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        return pos_values [0] as string;
                }
@@ -672,15 +672,12 @@ namespace Mono.CSharp {
                /// <summary>
                /// Returns condition of ConditionalAttribute
                /// </summary>
-               public string GetConditionalAttributeValue (DeclSpace ds)
+               public string GetConditionalAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values [0] == null)
@@ -692,15 +689,12 @@ namespace Mono.CSharp {
                /// <summary>
                /// Creates the instance of ObsoleteAttribute from this attribute instance
                /// </summary>
-               public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
+               public ObsoleteAttribute GetObsoleteAttribute (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values == null)
@@ -720,15 +714,12 @@ namespace Mono.CSharp {
                /// before ApplyAttribute. We need to resolve the arguments.
                /// This situation occurs when class deps is differs from Emit order.  
                /// </summary>
-               public bool GetClsCompliantAttributeValue (DeclSpace ds)
+               public bool GetClsCompliantAttributeValue (EmitContext ec)
                {
-                       if (pos_values == null) {
-                               EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
-
+                       if (pos_values == null)
                                // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
                                // But because a lot of attribute class code must be rewritten will be better to wait...
                                Resolve (ec);
-                       }
 
                        // Some error occurred
                        if (pos_values [0] == null)
@@ -1180,12 +1171,40 @@ namespace Mono.CSharp {
                        // in effect where the attribute was used.  Since code elsewhere cannot assume
                        // that the NamespaceEntry is right, just overwrite it.
                        //
-                       // FIXME: Check every place the NamespaceEntry of RootContext.Tree.Types is used
-                       //        to ensure the right one is used.
+                       // Precondition: RootContext.Tree.Types == null || RootContext.Tree.Types == ns.
+                       //               The second case happens when we are recursively invoked from inside Emit.
+
+                       NamespaceEntry old = null;
+                       if (ec.DeclSpace == RootContext.Tree.Types) {
+                               old = ec.DeclSpace.NamespaceEntry;
+                               ec.DeclSpace.NamespaceEntry = ns;
+                               if (old != null && old != ns)
+                                       throw new InternalErrorException (Location + " non-null NamespaceEntry " + old);
+                       }
+
+                       Type retval = base.CheckAttributeType (ec);
+
                        if (ec.DeclSpace == RootContext.Tree.Types)
+                               ec.DeclSpace.NamespaceEntry = old;
+
+                       return retval;
+               }
+
+               public override CustomAttributeBuilder Resolve (EmitContext ec)
+               {
+                       if (ec.DeclSpace == RootContext.Tree.Types) {
+                               NamespaceEntry old = ec.DeclSpace.NamespaceEntry;
                                ec.DeclSpace.NamespaceEntry = ns;
+                               if (old != null)
+                                       throw new InternalErrorException (Location + " non-null NamespaceEntry " + old);
+                       }
+
+                       CustomAttributeBuilder retval = base.Resolve (ec);
+
+                       if (ec.DeclSpace == RootContext.Tree.Types)
+                               ec.DeclSpace.NamespaceEntry = null;
 
-                       return base.CheckAttributeType (ec);
+                       return retval;
                }
        }