2003-01-19 Gaurav Vaish <gvaish_mono AT lycos.com>
[mono.git] / mcs / mcs / decl.cs
index 3fde037b675c99dffd7e41eeafb8d0bf4abe67e6..40fd4dcdca59ab015aa0e15f550503edb2d24183 100755 (executable)
@@ -94,37 +94,87 @@ namespace Mono.CSharp {
                                                      "' because it is sealed.");
                                        ok = false;
                                }
-
                                //
                                // Check that the permissions are not being changed
                                //
                                MethodAttributes thisp = my_attrs & MethodAttributes.MemberAccessMask;
                                MethodAttributes parentp = mb.Attributes & MethodAttributes.MemberAccessMask;
 
-                               if (thisp != parentp){
-                                       Error_CannotChangeAccessModifiers (parent, mb, name);
-                                       ok = false;
+                               //
+                               // special case for "protected internal"
+                               //
+
+                               if ((parentp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
+                                       //
+                                       // when overriding protected internal, the method can be declared
+                                       // protected internal only within the same assembly
+                                       //
+
+                                       if ((thisp & MethodAttributes.FamORAssem) == MethodAttributes.FamORAssem){
+                                               if (parent.TypeBuilder.Assembly != mb.DeclaringType.Assembly){
+                                                       //
+                                                       // assemblies differ - report an error
+                                                       //
+                                                       
+                                                       Error_CannotChangeAccessModifiers (parent, mb, name);
+                                                   ok = false;
+                                               } else if (thisp != parentp) {
+                                                       //
+                                                       // same assembly, but other attributes differ - report an error
+                                                       //
+                                                       
+                                                       Error_CannotChangeAccessModifiers (parent, mb, name);
+                                                       ok = false;
+                                               };
+                                       } else if ((thisp & MethodAttributes.Family) != MethodAttributes.Family) {
+                                               //
+                                               // if it's not "protected internal", it must be "protected"
+                                               //
+
+                                               Error_CannotChangeAccessModifiers (parent, mb, name);
+                                               ok = false;
+                                       } else if (parent.TypeBuilder.Assembly == mb.DeclaringType.Assembly) {
+                                               //
+                                               // protected within the same assembly - an error
+                                               //
+                                               Error_CannotChangeAccessModifiers (parent, mb, name);
+                                               ok = false;
+                                       } else if ((thisp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem)) != 
+                                                  (parentp & ~(MethodAttributes.Family | MethodAttributes.FamORAssem))) {
+                                               //
+                                               // protected ok, but other attributes differ - report an error
+                                               //
+                                               Error_CannotChangeAccessModifiers (parent, mb, name);
+                                               ok = false;
+                                       }
+                               } else {
+                                       if (thisp != parentp){
+                                               Error_CannotChangeAccessModifiers (parent, mb, name);
+                                               ok = false;
+                                       }
                                }
                        }
 
                        if (mb.IsVirtual || mb.IsAbstract){
                                if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
-                                       if (Name != "Finalize" && (RootContext.WarningLevel >= 2)){
+                                       if (Name != "Finalize"){
                                                Report.Warning (
-                                                       114, Location, parent.MakeName (Name) + 
+                                                       114, 2, Location, parent.MakeName (Name) + 
                                                        " hides inherited member `" + name +
                                                        "'.  To make the current member override that " +
                                                        "implementation, add the override keyword, " +
                                                        "otherwise use the new keyword");
+                                               ModFlags |= Modifiers.NEW;
                                        }
                                }
                        } else {
                                if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE)) == 0){
-                                       if (Name != "Finalize" && (RootContext.WarningLevel >= 1)){
+                                       if (Name != "Finalize"){
                                                Report.Warning (
-                                                       108, Location, "The keyword new is required on " +
+                                                       108, 1, Location, "The keyword new is required on " +
                                                        parent.MakeName (Name) + " because it hides " +
                                                        "inherited member `" + name + "'");
+                                               ModFlags |= Modifiers.NEW;
                                        }
                                }
                        }
@@ -258,6 +308,9 @@ namespace Mono.CSharp {
                        return AdditionResult.Success;
                }
 
+               public static int length;
+               public static int small;
+               
                /// <summary>
                ///   Introduce @name into this declaration space and
                ///   associates it with the object @o.  Note that for
@@ -266,6 +319,13 @@ namespace Mono.CSharp {
                protected void DefineName (string name, object o)
                {
                        defined_names.Add (name, o);
+
+#if DEBUGME
+                       int p = name.LastIndexOf (".");
+                       int l = name.Length;
+                       length += l;
+                       small += l -p;
+#endif
                }
 
                /// <summary>
@@ -372,9 +432,9 @@ namespace Mono.CSharp {
 
                public static string MakeFQN (string nsn, string name)
                {
-                       string prefix = (nsn == "" ? "" : nsn + ".");
-
-                       return prefix + name;
+                       if (nsn == "")
+                               return name;
+                       return String.Concat (nsn, ".", name);
                }
 
                EmitContext type_resolve_ec;
@@ -394,9 +454,12 @@ namespace Mono.CSharp {
                        if (type_resolve_ec == null)
                                type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
                        type_resolve_ec.loc = loc;
+
+                       int errors = Report.Errors;
                        Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
+                       
                        if (d == null || d.eclass != ExprClass.Type){
-                               if (!silent){
+                               if (!silent && errors == Report.Errors){
                                        Report.Error (246, loc, "Cannot find type `"+ e.ToString () +"'");
                                }
                                return null;
@@ -415,6 +478,7 @@ namespace Mono.CSharp {
                                type_resolve_ec = GetTypeResolveEmitContext (parent, loc);
 
                        Expression d = e.Resolve (type_resolve_ec, ResolveFlags.Type);
+                        
                        if (d == null || d.eclass != ExprClass.Type){
                                if (!silent){
                                        Report.Error (246, loc, "Cannot find type `"+ e +"'");
@@ -443,13 +507,20 @@ namespace Mono.CSharp {
                        
                        t = parent.DefineType ();
                        if (t == null){
-                               Report.Error (146, "Class definition is circular: `"+name+"'");
+                               Report.Error (146, Location, "Class definition is circular: `"+name+"'");
                                error = true;
                                return null;
                        }
                        return t;
                }
-               
+
+               public static void Error_AmbiguousTypeReference (Location loc, string name, Type t1, Type t2)
+               {
+                       Report.Error (104, loc,
+                                     String.Format ("`{0}' is an ambiguous reference ({1} or {2}) ", name,
+                                                    t1.FullName, t2.FullName));
+               }
+
                /// <summary>
                ///   GetType is used to resolve type names at the DeclSpace level.
                ///   Use this to lookup class/struct bases, interface bases or 
@@ -462,7 +533,7 @@ namespace Mono.CSharp {
                ///   during the tree resolution process and potentially define
                ///   recursively the type
                /// </remarks>
-               public Type FindType (string name)
+               public Type FindType (Location loc, string name)
                {
                        Type t;
                        bool error;
@@ -478,7 +549,7 @@ namespace Mono.CSharp {
 
                                while (current_type != null) {
                                        string pre = current_type.FullName;
-                                       
+
                                        t = LookupInterfaceOrClass (pre, name, out error);
                                        if (error)
                                                return null;
@@ -536,15 +607,24 @@ namespace Mono.CSharp {
                                if (using_list == null)
                                        continue;
 
-                               foreach (string n in using_list){
-                                       t = LookupInterfaceOrClass (n, name, out error);
+                               Type match = null;
+                               foreach (Namespace.UsingEntry ue in using_list){
+                                       match = LookupInterfaceOrClass (ue.Name, name, out error);
                                        if (error)
                                                return null;
 
-                                       if (t != null)
-                                               return t;
+                                       if (match != null){
+                                               if (t != null){
+                                                       Error_AmbiguousTypeReference (loc, name, t, match);
+                                                       return null;
+                                               }
+                                               
+                                               t = match;
+                                               ue.Used = true;
+                                       }
                                }
-                               
+                               if (t != null)
+                                       return t;
                        }
 
                        //Report.Error (246, Location, "Can not find type `"+name+"'");
@@ -793,6 +873,8 @@ namespace Mono.CSharp {
        public class MemberCache {
                public readonly IMemberContainer Container;
                protected Hashtable member_hash;
+               protected Hashtable method_hash;
+               protected Hashtable interface_hash;
 
                /// <summary>
                ///   Create a new MemberCache for the given IMemberContainer `container'.
@@ -804,6 +886,8 @@ namespace Mono.CSharp {
                        Timer.IncrementCounter (CounterType.MemberCache);
                        Timer.StartTimer (TimerType.CacheInit);
 
+                       interface_hash = new Hashtable ();
+
                        // If we have a parent class (we have a parent class unless we're
                        // TypeManager.object_type), we deep-copy its MemberCache here.
                        if (Container.Parent != null)
@@ -813,6 +897,14 @@ namespace Mono.CSharp {
                        else
                                member_hash = new Hashtable ();
 
+                       // If this is neither a dynamic type nor an interface, create a special
+                       // method cache with all declared and inherited methods.
+                       Type type = container.Type;
+                       if (!(type is TypeBuilder) && !type.IsInterface) {
+                               method_hash = new Hashtable ();
+                               AddMethods (type);
+                       }
+
                        // Add all members from the current class.
                        AddMembers (Container);
 
@@ -826,8 +918,6 @@ namespace Mono.CSharp {
                {
                        Hashtable hash = new Hashtable ();
 
-                       Report.Debug (8, "SETUP CACHE", Container, Container.Parent, parent);
-
                        IDictionaryEnumerator it = parent.member_hash.GetEnumerator ();
                        while (it.MoveNext ()) {
                                hash [it.Key] = ((ArrayList) it.Value).Clone ();
@@ -836,6 +926,14 @@ namespace Mono.CSharp {
                        return hash;
                }
 
+               void AddInterfaces (MemberCache parent)
+               {
+                       foreach (Type iface in parent.interface_hash.Keys) {
+                               if (!interface_hash.Contains (iface))
+                                       interface_hash.Add (iface, true);
+                       }
+               }
+
                /// <summary>
                ///   Add the contents of `new_hash' to `hash'.
                /// </summary>
@@ -862,11 +960,16 @@ namespace Mono.CSharp {
                        Type [] ifaces = TypeManager.GetInterfaces (Container.Type);
 
                        foreach (Type iface in ifaces) {
+                               if (interface_hash.Contains (iface))
+                                       continue;
+                               interface_hash.Add (iface, true);
+
                                IMemberContainer iface_container =
                                        TypeManager.LookupMemberContainer (iface);
 
                                MemberCache iface_cache = iface_container.MemberCache;
                                AddHashtable (hash, iface_cache.member_hash);
+                               AddInterfaces (iface_cache);
                        }
 
                        return hash;
@@ -877,12 +980,18 @@ namespace Mono.CSharp {
                /// </summary>
                void AddMembers (IMemberContainer container)
                {
-                       AddMembers (MemberTypes.Constructor | MemberTypes.Field | MemberTypes.Method |
-                                   MemberTypes.Property, container);
-                       AddMembers (MemberTypes.NestedType | MemberTypes.Event,
-                                   BindingFlags.Public, container);
-                       AddMembers (MemberTypes.NestedType | MemberTypes.Event,
-                                   BindingFlags.NonPublic, container);
+                       // We need to call AddMembers() with a single member type at a time
+                       // to get the member type part of CacheEntry.EntryType right.
+                       AddMembers (MemberTypes.Constructor, container);
+                       AddMembers (MemberTypes.Field, container);
+                       AddMembers (MemberTypes.Method, container);
+                       AddMembers (MemberTypes.Property, container);
+                       AddMembers (MemberTypes.Event, container);
+                       // Nested types are returned by both Static and Instance searches.
+                       AddMembers (MemberTypes.NestedType,
+                                   BindingFlags.Static | BindingFlags.Public, container);
+                       AddMembers (MemberTypes.NestedType,
+                                   BindingFlags.Static | BindingFlags.NonPublic, container);
                }
 
                void AddMembers (MemberTypes mt, IMemberContainer container)
@@ -893,7 +1002,6 @@ namespace Mono.CSharp {
                        AddMembers (mt, BindingFlags.Instance | BindingFlags.NonPublic, container);
                }
 
-
                /// <summary>
                ///   Add all members from class `container' with the requested MemberTypes and
                ///   BindingFlags to the cache.  This method is called multiple times with different
@@ -920,7 +1028,48 @@ namespace Mono.CSharp {
                                // We cannot add new members in front of the list since this'd be an
                                // expensive operation, that's why the list is sorted in reverse order
                                // (ie. members from the current class are coming last).
-                               list.Add (new CacheEntry (container, member, mt, new_bf));
+                               list.Add (new CacheEntry (container, member, mt, bf));
+                       }
+               }
+
+               /// <summary>
+               ///   Add all declared and inherited methods from class `type' to the method cache.
+               /// </summary>
+               void AddMethods (Type type)
+               {
+                       AddMethods (BindingFlags.Static | BindingFlags.Public |
+                                   BindingFlags.FlattenHierarchy, type);
+                       AddMethods (BindingFlags.Static | BindingFlags.NonPublic |
+                                   BindingFlags.FlattenHierarchy, type);
+                       AddMethods (BindingFlags.Instance | BindingFlags.Public, type);
+                       AddMethods (BindingFlags.Instance | BindingFlags.NonPublic, type);
+               }
+
+               void AddMethods (BindingFlags bf, Type type)
+               {
+                       MemberInfo [] members = type.GetMethods (bf);
+
+                       foreach (MethodBase member in members) {
+                               string name = member.Name;
+
+                               // Varargs methods aren't allowed in C# code.
+                               if ((member.CallingConvention & CallingConventions.VarArgs) != 0)
+                                       continue;
+
+                               // We use a name-based hash table of ArrayList's.
+                               ArrayList list = (ArrayList) method_hash [name];
+                               if (list == null) {
+                                       list = new ArrayList ();
+                                       method_hash.Add (name, list);
+                               }
+
+                               // Unfortunately, the elements returned by Type.GetMethods() aren't
+                               // sorted so we need to do this check for every member.
+                               BindingFlags new_bf = bf;
+                               if (member.DeclaringType == type)
+                                       new_bf |= BindingFlags.DeclaredOnly;
+
+                               list.Add (new CacheEntry (Container, member, MemberTypes.Method, new_bf));
                        }
                }
 
@@ -942,15 +1091,14 @@ namespace Mono.CSharp {
                                type |= EntryType.Method;
                        if ((mt & MemberTypes.Property) != 0)
                                type |= EntryType.Property;
+                       // Nested types are returned by static and instance searches.
                        if ((mt & MemberTypes.NestedType) != 0)
-                               type |= EntryType.NestedType;
+                               type |= EntryType.NestedType | EntryType.Static | EntryType.Instance;
 
                        if ((bf & BindingFlags.Instance) != 0)
                                type |= EntryType.Instance;
                        if ((bf & BindingFlags.Static) != 0)
                                type |= EntryType.Static;
-                       if ((bf & (BindingFlags.Instance | BindingFlags.Static)) == 0)
-                               type |= EntryType.Instance | EntryType.Static;
                        if ((bf & BindingFlags.Public) != 0)
                                type |= EntryType.Public;
                        if ((bf & BindingFlags.NonPublic) != 0)
@@ -1057,13 +1205,34 @@ namespace Mono.CSharp {
                ///
                ///   Unlike other FindMembers implementations, this method will always
                ///   check all inherited members - even when called on an interface type.
+               ///
+               ///   If you know that you're only looking for methods, you should use
+               ///   MemberTypes.Method alone since this speeds up the lookup a bit.
+               ///   When doing a method-only search, it'll try to use a special method
+               ///   cache (unless it's a dynamic type or an interface) and the returned
+               ///   MemberInfo's will have the correct ReflectedType for inherited methods.
+               ///   The lookup process will automatically restart itself in method-only
+               ///   search mode if it discovers that it's about to return methods.
                /// </summary>
                public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
                                               MemberFilter filter, object criteria)
                {
                        bool declared_only = (bf & BindingFlags.DeclaredOnly) != 0;
+                       bool method_search = mt == MemberTypes.Method;
+                       // If we have a method cache and we aren't already doing a method-only search,
+                       // then we restart a method search if the first match is a method.
+                       bool do_method_search = !method_search && (method_hash != null);
+
+                       ArrayList applicable;
+
+                       // If this is a method-only search, we try to use the method cache if
+                       // possible; a lookup in the method cache will return a MemberInfo with
+                       // the correct ReflectedType for inherited methods.
+                       if (method_search && (method_hash != null))
+                               applicable = (ArrayList) method_hash [name];
+                       else
+                               applicable = (ArrayList) member_hash [name];
 
-                       ArrayList applicable = (ArrayList) member_hash [name];
                        if (applicable == null)
                                return MemberList.Empty;
 
@@ -1071,6 +1240,8 @@ namespace Mono.CSharp {
 
                        Timer.StartTimer (TimerType.CachedLookup);
 
+                       EntryType type = GetEntryType (mt, bf);
+
                        IMemberContainer current = Container;
 
                        // `applicable' is a list of all members with the given member name `name'
@@ -1093,8 +1264,6 @@ namespace Mono.CSharp {
                                        current = entry.Container;
                                }
 
-                               EntryType type = GetEntryType (mt, bf);
-
                                // Is the member of the correct type ?
                                if ((entry.EntryType & type & EntryType.MaskType) == 0)
                                        continue;
@@ -1104,12 +1273,22 @@ namespace Mono.CSharp {
                                        continue;
 
                                // Apply the filter to it.
-                               if (filter (entry.Member, criteria))
+                               if (filter (entry.Member, criteria)) {
+                                       if ((entry.EntryType & EntryType.MaskType) != EntryType.Method)
+                                               do_method_search = false;
                                        list.Add (entry.Member);
+                               }
                        }
 
                        Timer.StopTimer (TimerType.CachedLookup);
 
+                       // If we have a method cache and we aren't already doing a method-only
+                       // search, we restart in method-only search mode if the first match is
+                       // a method.  This ensures that we return a MemberInfo with the correct
+                       // ReflectedType for inherited methods.
+                       if (do_method_search && (list.Count > 0))
+                               return FindMembers (MemberTypes.Method, bf, name, filter, criteria);
+
                        return new MemberList (list);
                }
        }