2 // namespace.cs: Tracks namespaces
5 // Miguel de Icaza (miguel@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Copyright 2001 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
12 using System.Collections;
13 using System.Collections.Specialized;
14 using System.Reflection;
16 namespace Mono.CSharp {
18 public class RootNamespace : Namespace {
20 // Points to Mono's GetNamespaces method, an
21 // optimization when running on Mono to fetch all the
22 // namespaces in an assembly
24 static MethodInfo get_namespaces_method;
27 protected Assembly [] referenced_assemblies;
29 Hashtable all_namespaces;
31 static ListDictionary root_namespaces;
32 public static GlobalRootNamespace Global;
34 static RootNamespace ()
36 get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
41 public static void Reset ()
43 root_namespaces = new ListDictionary ();
44 Global = new GlobalRootNamespace ();
45 root_namespaces ["global"] = Global;
48 protected RootNamespace (string alias_name)
49 : base (null, String.Empty)
51 this.alias_name = alias_name;
52 referenced_assemblies = new Assembly [0];
54 all_namespaces = new Hashtable ();
55 all_namespaces.Add ("", this);
58 public void AddAssemblyReference (Assembly a)
60 foreach (Assembly assembly in referenced_assemblies) {
65 int top = referenced_assemblies.Length;
66 Assembly [] n = new Assembly [top + 1];
67 referenced_assemblies.CopyTo (n, 0);
69 referenced_assemblies = n;
72 public static void DefineRootNamespace (string alias, Assembly assembly)
74 if (alias == "global") {
75 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
79 RootNamespace retval = GetRootNamespace (alias);
81 retval = new RootNamespace (alias);
82 root_namespaces.Add (alias, retval);
85 retval.AddAssemblyReference (assembly);
88 public static RootNamespace GetRootNamespace (string name)
90 return (RootNamespace) root_namespaces [name];
93 public static void ComputeNamespaces ()
96 // Do very early lookup because type is required when we cache
97 // imported extension types in ComputeNamespaces
99 TypeManager.extension_attribute_type = TypeManager.CoreLookupType ("System.Runtime.CompilerServices", "ExtensionAttribute", Kind.Class, false);
101 foreach (RootNamespace rn in root_namespaces.Values) {
102 foreach (Assembly a in rn.referenced_assemblies) {
104 rn.ComputeNamespaces (a);
105 } catch (TypeLoadException e) {
106 Report.Error (11, Location.Null, e.Message);
107 } catch (System.IO.FileNotFoundException) {
108 Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
115 public virtual Type LookupTypeReflection (string name, Location loc)
117 Type found_type = null;
119 foreach (Assembly a in referenced_assemblies) {
120 Type t = GetTypeInAssembly (a, name);
124 if (found_type == null) {
129 Report.SymbolRelatedToPreviousError (found_type);
130 Report.SymbolRelatedToPreviousError (t);
131 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
139 public void RegisterNamespace (Namespace child)
142 all_namespaces.Add (child.Name, child);
145 public bool IsNamespace (string name)
147 return all_namespaces.Contains (name);
150 protected void RegisterNamespace (string dotted_name)
152 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
153 GetNamespace (dotted_name, true);
156 void RegisterExtensionMethodClass (Type t)
158 string n = t.Namespace;
159 Namespace ns = n == null ? Global : (Namespace)all_namespaces [n];
161 ns = GetNamespace (n, true);
163 ns.RegisterExternalExtensionMethodClass (t);
166 protected void ComputeNamespaces (Assembly assembly)
168 bool contains_extension_methods = TypeManager.extension_attribute_type != null &&
169 assembly.IsDefined(TypeManager.extension_attribute_type, false);
171 if (get_namespaces_method != null && !contains_extension_methods) {
172 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
173 foreach (string ns in namespaces)
174 RegisterNamespace (ns);
178 foreach (Type t in assembly.GetExportedTypes ()) {
179 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
180 contains_extension_methods && t.IsDefined (TypeManager.extension_attribute_type, false))
181 RegisterExtensionMethodClass (t);
183 RegisterNamespace (t.Namespace);
187 protected static Type GetTypeInAssembly (Assembly assembly, string name)
189 Type t = assembly.GetType (name);
194 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
196 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
197 if (ta == TypeAttributes.NestedPrivate)
200 if ((ta == TypeAttributes.NotPublic ||
201 ta == TypeAttributes.NestedAssembly ||
202 ta == TypeAttributes.NestedFamANDAssem) &&
203 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
209 public override string ToString ()
211 return String.Format ("RootNamespace ({0}::)", alias_name);
214 public override string GetSignatureForError ()
216 return alias_name + "::";
220 public class GlobalRootNamespace : RootNamespace {
223 public GlobalRootNamespace ()
228 public Assembly [] Assemblies {
229 get { return referenced_assemblies; }
232 public Module [] Modules {
233 get { return modules; }
236 public void AddModuleReference (Module m)
238 int top = modules != null ? modules.Length : 0;
239 Module [] n = new Module [top + 1];
241 modules.CopyTo (n, 0);
245 if (m == CodeGen.Module.Builder)
248 foreach (Type t in m.GetTypes ())
249 RegisterNamespace (t.Namespace);
252 public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
254 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
258 public override Type LookupTypeReflection (string name, Location loc)
260 Type found_type = base.LookupTypeReflection (name, loc);
262 if (modules != null) {
263 foreach (Module module in modules) {
264 Type t = module.GetType (name);
268 if (found_type == null) {
273 Report.SymbolRelatedToPreviousError (found_type);
275 DeclSpace ds = TypeManager.LookupDeclSpace (t);
276 Report.Warning (1685, 1, ds.Location, "The type `{0}' conflicts with the predefined type `{1}' and will be ignored",
277 ds.GetSignatureForError (), TypeManager.CSharpName (found_type));
280 Report.SymbolRelatedToPreviousError (t);
281 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
282 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
292 /// Keeps track of the namespaces defined in the C# code.
294 /// This is an Expression to allow it to be referenced in the
295 /// compiler parse/intermediate tree during name resolution.
297 public class Namespace : FullNamedExpression {
301 IDictionary namespaces;
302 IDictionary declspaces;
303 Hashtable cached_types;
305 ArrayList external_exmethod_classes;
307 public readonly MemberName MemberName;
310 /// Constructor Takes the current namespace and the
311 /// name. This is bootstrapped with parent == null
314 public Namespace (Namespace parent, string name)
316 // Expression members.
317 this.eclass = ExprClass.Namespace;
318 this.Type = typeof (Namespace);
319 this.loc = Location.Null;
321 this.parent = parent;
324 this.root = parent.root;
326 this.root = this as RootNamespace;
328 if (this.root == null)
329 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
331 string pname = parent != null ? parent.fullname : "";
336 fullname = parent.fullname + "." + name;
338 if (fullname == null)
339 throw new InternalErrorException ("Namespace has a null fullname");
341 if (parent != null && parent.MemberName != MemberName.Null)
342 MemberName = new MemberName (parent.MemberName, name);
343 else if (name.Length == 0)
344 MemberName = MemberName.Null;
346 MemberName = new MemberName (name);
348 namespaces = new HybridDictionary ();
349 cached_types = new Hashtable ();
351 root.RegisterNamespace (this);
354 public override Expression DoResolve (EmitContext ec)
359 public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
361 if (name.IndexOf ('`') > 0) {
362 FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
363 if (retval != null) {
364 Error_TypeArgumentsCannotBeUsed (retval, loc);
368 Type t = LookForAnyGenericType (name);
370 Error_InvalidNumberOfTypeArguments (t, loc);
375 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
376 name, GetSignatureForError ());
379 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
381 Report.SymbolRelatedToPreviousError (t);
382 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
383 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
386 public static void Error_TypeArgumentsCannotBeUsed (FullNamedExpression expr, Location loc)
388 if (expr is TypeExpr) {
389 Report.SymbolRelatedToPreviousError (expr.Type);
390 Error_TypeArgumentsCannotBeUsed (loc, "type", expr.GetSignatureForError ());
392 expr.Error_ExpressionCannotBeGeneric (loc);
396 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
398 Report.SymbolRelatedToPreviousError (mi);
399 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
402 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
404 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
408 public override string GetSignatureForError ()
413 public Namespace GetNamespace (string name, bool create)
415 int pos = name.IndexOf ('.');
420 first = name.Substring (0, pos);
424 ns = (Namespace) namespaces [first];
429 ns = new Namespace (this, first);
430 namespaces.Add (first, ns);
434 ns = ns.GetNamespace (name.Substring (pos + 1), create);
439 public bool HasDefinition (string name)
441 return declspaces != null && declspaces [name] != null;
444 TypeExpr LookupType (string name, Location loc)
446 if (cached_types.Contains (name))
447 return cached_types [name] as TypeExpr;
450 if (declspaces != null) {
451 DeclSpace tdecl = declspaces [name] as DeclSpace;
454 // Note that this is not:
456 // t = tdecl.DefineType ()
458 // This is to make it somewhat more useful when a DefineType
459 // fails due to problems in nested types (more useful in the sense
460 // of fewer misleading error messages)
463 t = tdecl.TypeBuilder;
465 if (RootContext.EvalMode){
466 // Replace the TypeBuilder with a System.Type, as
467 // Reflection.Emit fails otherwise (we end up pretty
468 // much with Random type definitions later on).
469 Type tt = t.Assembly.GetType (t.Name);
475 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
476 Type rt = root.LookupTypeReflection (lookup, loc);
478 // HACK: loc.IsNull when the type is core type
479 if (t == null || (rt != null && loc.IsNull))
482 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
483 cached_types [name] = te;
488 /// Used for better error reporting only
490 public Type LookForAnyGenericType (string typeName)
492 if (declspaces == null)
495 typeName = SimpleName.RemoveGenericArity (typeName);
497 foreach (DictionaryEntry de in declspaces) {
498 string type_item = (string) de.Key;
499 int pos = type_item.LastIndexOf ('`');
500 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
501 return ((DeclSpace) de.Value).TypeBuilder;
506 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
508 if (namespaces.Contains (name))
509 return (Namespace) namespaces [name];
511 return LookupType (name, loc);
514 public void RegisterExternalExtensionMethodClass (Type type)
516 if (external_exmethod_classes == null)
517 external_exmethod_classes = new ArrayList ();
519 external_exmethod_classes.Add (type);
523 /// Looks for extension method in this namespace
525 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
527 ArrayList found = null;
529 if (declspaces != null) {
530 IEnumerator e = declspaces.Values.GetEnumerator ();
532 while (e.MoveNext ()) {
533 Class c = e.Current as Class;
537 if (!c.IsStaticClass)
540 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
547 found.AddRange (res);
551 if (external_exmethod_classes == null)
554 foreach (Type t in external_exmethod_classes) {
555 MemberCache m = TypeHandle.GetMemberCache (t);
556 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
563 found.AddRange (res);
569 public void AddDeclSpace (string name, DeclSpace ds)
571 if (declspaces == null)
572 declspaces = new HybridDictionary ();
573 declspaces.Add (name, ds);
576 public void RemoveDeclSpace (string name)
578 declspaces.Remove (name);
582 /// The qualified name of the current namespace
585 get { return fullname; }
589 /// The parent of this namespace, used by the parser to "Pop"
590 /// the current namespace declaration
592 public Namespace Parent {
593 get { return parent; }
598 // Namespace container as created by the parser
600 public class NamespaceEntry : IResolveContext {
603 readonly MemberName name;
606 public UsingEntry (MemberName name)
611 public string GetSignatureForError ()
613 return name.GetSignatureForError ();
616 public Location Location {
617 get { return name.Location; }
620 public MemberName MemberName {
625 get { return GetSignatureForError (); }
628 public Namespace Resolve (IResolveContext rc)
630 if (resolved != null)
633 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
637 resolved = fne as Namespace;
638 if (resolved == null) {
639 Report.SymbolRelatedToPreviousError (fne.Type);
640 Report.Error (138, Location,
641 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
642 GetSignatureForError ());
647 public override string ToString ()
653 class UsingAliasEntry {
654 public readonly string Alias;
655 public Location Location;
657 public UsingAliasEntry (string alias, Location loc)
663 public virtual FullNamedExpression Resolve (IResolveContext rc)
665 FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
667 Report.Error (430, Location,
668 "The extern alias `{0}' was not specified in -reference option",
675 public override string ToString ()
682 class LocalUsingAliasEntry : UsingAliasEntry {
683 FullNamedExpression resolved;
686 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
692 public override FullNamedExpression Resolve (IResolveContext rc)
694 if (resolved != null || value == null)
697 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
698 if (resolved == null) {
703 TypeExpr te = resolved as TypeExpr;
705 if (!te.CheckAccessLevel (rc.DeclContainer)) {
706 Report.SymbolRelatedToPreviousError (te.Type);
707 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
714 public override string ToString ()
716 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
721 NamespaceEntry parent, implicit_parent;
722 CompilationUnit file;
725 // Namespace using import block
726 ArrayList using_aliases;
727 ArrayList using_clauses;
728 public bool DeclarationFound = false;
731 public readonly bool IsImplicit;
732 public readonly DeclSpace SlaveDeclSpace;
733 static readonly Namespace [] empty_namespaces = new Namespace [0];
734 Namespace [] namespace_using_table;
736 static ArrayList entries = new ArrayList ();
738 public static void Reset ()
740 entries = new ArrayList ();
743 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
745 this.parent = parent;
750 ns = parent.NS.GetNamespace (name, true);
751 else if (name != null)
752 ns = RootNamespace.Global.GetNamespace (name, true);
754 ns = RootNamespace.Global;
755 SlaveDeclSpace = new RootDeclSpace (this);
758 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
760 this.parent = parent;
762 this.IsImplicit = true;
764 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
768 // Populates the Namespace with some using declarations, used by the
771 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
773 foreach (UsingAliasEntry uae in source_using_aliases){
774 if (using_aliases == null)
775 using_aliases = new ArrayList ();
777 using_aliases.Add (uae);
780 foreach (UsingEntry ue in source_using_clauses){
781 if (using_clauses == null)
782 using_clauses = new ArrayList ();
784 using_clauses.Add (ue);
789 // Extracts the using alises and using clauses into a couple of
790 // arrays that might already have the same information; Used by the
793 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
795 if (using_aliases != null){
796 foreach (UsingAliasEntry uae in using_aliases){
797 bool replaced = false;
799 for (int i = 0; i < out_using_aliases.Count; i++){
800 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
802 if (out_uea.Alias == uae.Alias){
803 out_using_aliases [i] = uae;
809 out_using_aliases.Add (uae);
813 if (using_clauses != null){
814 foreach (UsingEntry ue in using_clauses){
817 foreach (UsingEntry out_ue in out_using_clauses)
818 if (out_ue.Name == ue.Name){
823 out_using_clauses.Add (ue);
829 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
830 // resolved as if the immediately containing namespace body has no using-directives.
832 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
833 // in the using-namespace-directive.
835 // To implement these rules, the expressions in the using directives are resolved using
836 // the "doppelganger" (ghostly bodiless duplicate).
838 NamespaceEntry doppelganger;
839 NamespaceEntry Doppelganger {
841 if (!IsImplicit && doppelganger == null) {
842 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
843 doppelganger.using_aliases = using_aliases;
849 public Namespace NS {
853 public NamespaceEntry Parent {
854 get { return parent; }
857 public NamespaceEntry ImplicitParent {
861 if (implicit_parent == null) {
862 implicit_parent = (parent.NS == ns.Parent)
864 : new NamespaceEntry (parent, file, ns.Parent, false);
866 return implicit_parent;
871 /// Records a new namespace for resolving name references
873 public void AddUsing (MemberName name, Location loc)
875 if (DeclarationFound){
876 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
879 if (using_clauses == null) {
880 using_clauses = new ArrayList ();
882 foreach (UsingEntry old_entry in using_clauses) {
883 if (name.Equals (old_entry.MemberName)) {
884 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
885 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
891 using_clauses.Add (new UsingEntry (name));
894 public void AddUsingAlias (string alias, MemberName name, Location loc)
896 // TODO: This is parser bussines
897 if (DeclarationFound){
898 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
901 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
902 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
903 " the global namespace will be used instead");
905 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
908 public void AddUsingExternalAlias (string alias, Location loc)
910 // TODO: Do this in parser
911 bool not_first = using_clauses != null || DeclarationFound;
912 if (using_aliases != null && !not_first) {
913 foreach (UsingAliasEntry uae in using_aliases) {
914 if (uae is LocalUsingAliasEntry) {
922 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
924 if (alias == "global") {
925 Error_GlobalNamespaceRedefined (loc);
929 AddUsingAlias (new UsingAliasEntry (alias, loc));
932 void AddUsingAlias (UsingAliasEntry uae)
934 if (using_aliases == null) {
935 using_aliases = new ArrayList ();
937 foreach (UsingAliasEntry entry in using_aliases) {
938 if (uae.Alias == entry.Alias) {
939 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
940 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
947 using_aliases.Add (uae);
951 /// Does extension methods look up to find a method which matches name and extensionType.
952 /// Search starts from this namespace and continues hierarchically up to top level.
954 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
956 ArrayList candidates = null;
957 if (currentClass != null) {
958 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
959 if (candidates != null)
960 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
963 foreach (Namespace n in GetUsingTable ()) {
964 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
968 if (candidates == null)
971 candidates.AddRange (a);
974 if (candidates != null)
975 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
981 // Inspect parent namespaces in namespace expression
983 Namespace parent_ns = ns.Parent;
985 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
986 if (candidates != null)
987 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
989 parent_ns = parent_ns.Parent;
990 } while (parent_ns != null);
993 // Continue in parent scope
995 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
998 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1000 // Precondition: Only simple names (no dots) will be looked up with this function.
1001 FullNamedExpression resolved = null;
1002 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1003 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1009 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1011 Report.SymbolRelatedToPreviousError (t1.Type);
1012 Report.SymbolRelatedToPreviousError (t2.Type);
1013 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1014 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1017 // Looks-up a alias named @name in this and surrounding namespace declarations
1018 public FullNamedExpression LookupAlias (string name)
1020 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1021 if (n.using_aliases == null)
1024 foreach (UsingAliasEntry ue in n.using_aliases) {
1025 if (ue.Alias == name)
1026 return ue.Resolve (Doppelganger);
1033 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1036 // Check whether it's in the namespace.
1038 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1043 if (using_aliases != null) {
1044 foreach (UsingAliasEntry ue in using_aliases) {
1045 if (ue.Alias == name) {
1047 if (Doppelganger != null) {
1048 // TODO: Namespace has broken location
1049 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1050 Report.SymbolRelatedToPreviousError (ue.Location, null);
1051 Report.Error (576, loc,
1052 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1053 GetSignatureForError (), name);
1059 return ue.Resolve (Doppelganger);
1071 // Check using entries.
1073 FullNamedExpression match = null;
1074 foreach (Namespace using_ns in GetUsingTable ()) {
1075 match = using_ns.Lookup (ds, name, loc);
1076 if (match == null || !(match is TypeExpr))
1080 Error_AmbiguousTypeReference (loc, name, fne, match);
1089 Namespace [] GetUsingTable ()
1091 if (namespace_using_table != null)
1092 return namespace_using_table;
1094 if (using_clauses == null) {
1095 namespace_using_table = empty_namespaces;
1096 return namespace_using_table;
1099 ArrayList list = new ArrayList (using_clauses.Count);
1101 foreach (UsingEntry ue in using_clauses) {
1102 Namespace using_ns = ue.Resolve (Doppelganger);
1103 if (using_ns == null)
1106 list.Add (using_ns);
1109 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1110 return namespace_using_table;
1113 static readonly string [] empty_using_list = new string [0];
1115 public int SymbolFileID {
1117 if (symfile_id == 0 && file.SourceFileEntry != null) {
1118 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1120 string [] using_list = empty_using_list;
1121 if (using_clauses != null) {
1122 using_list = new string [using_clauses.Count];
1123 for (int i = 0; i < using_clauses.Count; i++)
1124 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1127 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1133 static void MsgtryRef (string s)
1135 Console.WriteLine (" Try using -r:" + s);
1138 static void MsgtryPkg (string s)
1140 Console.WriteLine (" Try using -pkg:" + s);
1143 public static void Error_GlobalNamespaceRedefined (Location loc)
1145 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1148 public static void Error_NamespaceNotFound (Location loc, string name)
1150 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1154 case "Gtk": case "GtkSharp":
1155 MsgtryPkg ("gtk-sharp");
1158 case "Gdk": case "GdkSharp":
1159 MsgtryPkg ("gdk-sharp");
1162 case "Glade": case "GladeSharp":
1163 MsgtryPkg ("glade-sharp");
1166 case "System.Drawing":
1167 case "System.Web.Services":
1170 case "System.Windows.Forms":
1177 /// Used to validate that all the using clauses are correct
1178 /// after we are finished parsing all the files.
1182 if (using_aliases != null) {
1183 foreach (UsingAliasEntry ue in using_aliases)
1184 ue.Resolve (Doppelganger);
1187 if (using_clauses != null) {
1188 foreach (UsingEntry ue in using_clauses)
1189 ue.Resolve (Doppelganger);
1194 /// Used to validate that all the using clauses are correct
1195 /// after we are finished parsing all the files.
1197 static public void VerifyAllUsing ()
1199 foreach (NamespaceEntry entry in entries)
1200 entry.VerifyUsing ();
1203 public string GetSignatureForError ()
1205 return ns.GetSignatureForError ();
1208 public override string ToString ()
1210 return ns.ToString ();
1213 #region IResolveContext Members
1215 public DeclSpace DeclContainer {
1216 get { return SlaveDeclSpace; }
1219 public bool IsInObsoleteScope {
1220 get { return SlaveDeclSpace.IsInObsoleteScope; }
1223 public bool IsInUnsafeScope {
1224 get { return SlaveDeclSpace.IsInUnsafeScope; }
1227 public DeclSpace GenericDeclContainer {
1228 get { return SlaveDeclSpace; }