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 {
686 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
692 public override FullNamedExpression Resolve (IResolveContext rc)
694 if (resolved != null || value == null)
695 return (FullNamedExpression)resolved;
697 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
698 if (resolved == null) {
703 // FIXME: This is quite wrong, the accessibility is not global
704 if (resolved.Type != null) {
705 TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
706 if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
707 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) &&
708 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
709 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
714 return (FullNamedExpression)resolved;
717 public override string ToString ()
719 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
724 NamespaceEntry parent, implicit_parent;
725 CompilationUnit file;
728 // Namespace using import block
729 ArrayList using_aliases;
730 ArrayList using_clauses;
731 public bool DeclarationFound = false;
734 public readonly bool IsImplicit;
735 public readonly DeclSpace SlaveDeclSpace;
736 static readonly Namespace [] empty_namespaces = new Namespace [0];
737 Namespace [] namespace_using_table;
739 static ArrayList entries = new ArrayList ();
741 public static void Reset ()
743 entries = new ArrayList ();
746 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
748 this.parent = parent;
753 ns = parent.NS.GetNamespace (name, true);
754 else if (name != null)
755 ns = RootNamespace.Global.GetNamespace (name, true);
757 ns = RootNamespace.Global;
758 SlaveDeclSpace = new RootDeclSpace (this);
761 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
763 this.parent = parent;
765 this.IsImplicit = true;
767 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
771 // Populates the Namespace with some using declarations, used by the
774 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
776 foreach (UsingAliasEntry uae in source_using_aliases){
777 if (using_aliases == null)
778 using_aliases = new ArrayList ();
780 using_aliases.Add (uae);
783 foreach (UsingEntry ue in source_using_clauses){
784 if (using_clauses == null)
785 using_clauses = new ArrayList ();
787 using_clauses.Add (ue);
792 // Extracts the using alises and using clauses into a couple of
793 // arrays that might already have the same information; Used by the
796 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
798 if (using_aliases != null){
799 foreach (UsingAliasEntry uae in using_aliases){
800 bool replaced = false;
802 for (int i = 0; i < out_using_aliases.Count; i++){
803 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
805 if (out_uea.Alias == uae.Alias){
806 out_using_aliases [i] = uae;
812 out_using_aliases.Add (uae);
816 if (using_clauses != null){
817 foreach (UsingEntry ue in using_clauses){
820 foreach (UsingEntry out_ue in out_using_clauses)
821 if (out_ue.Name == ue.Name){
826 out_using_clauses.Add (ue);
832 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
833 // resolved as if the immediately containing namespace body has no using-directives.
835 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
836 // in the using-namespace-directive.
838 // To implement these rules, the expressions in the using directives are resolved using
839 // the "doppelganger" (ghostly bodiless duplicate).
841 NamespaceEntry doppelganger;
842 NamespaceEntry Doppelganger {
844 if (!IsImplicit && doppelganger == null) {
845 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
846 doppelganger.using_aliases = using_aliases;
852 public Namespace NS {
856 public NamespaceEntry Parent {
857 get { return parent; }
860 public NamespaceEntry ImplicitParent {
864 if (implicit_parent == null) {
865 implicit_parent = (parent.NS == ns.Parent)
867 : new NamespaceEntry (parent, file, ns.Parent, false);
869 return implicit_parent;
874 /// Records a new namespace for resolving name references
876 public void AddUsing (MemberName name, Location loc)
878 if (DeclarationFound){
879 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
882 if (using_clauses == null) {
883 using_clauses = new ArrayList ();
885 foreach (UsingEntry old_entry in using_clauses) {
886 if (name.Equals (old_entry.MemberName)) {
887 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
888 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
894 using_clauses.Add (new UsingEntry (name));
897 public void AddUsingAlias (string alias, MemberName name, Location loc)
899 // TODO: This is parser bussines
900 if (DeclarationFound){
901 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
904 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
905 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
906 " the global namespace will be used instead");
908 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
911 public void AddUsingExternalAlias (string alias, Location loc)
913 // TODO: Do this in parser
914 bool not_first = using_clauses != null || DeclarationFound;
915 if (using_aliases != null && !not_first) {
916 foreach (UsingAliasEntry uae in using_aliases) {
917 if (uae is LocalUsingAliasEntry) {
925 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
927 if (alias == "global") {
928 Error_GlobalNamespaceRedefined (loc);
932 AddUsingAlias (new UsingAliasEntry (alias, loc));
935 void AddUsingAlias (UsingAliasEntry uae)
937 if (using_aliases == null) {
938 using_aliases = new ArrayList ();
940 foreach (UsingAliasEntry entry in using_aliases) {
941 if (uae.Alias == entry.Alias) {
942 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
943 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
950 using_aliases.Add (uae);
954 /// Does extension methods look up to find a method which matches name and extensionType.
955 /// Search starts from this namespace and continues hierarchically up to top level.
957 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
959 ArrayList candidates = null;
960 if (currentClass != null) {
961 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
962 if (candidates != null)
963 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
966 foreach (Namespace n in GetUsingTable ()) {
967 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
971 if (candidates == null)
974 candidates.AddRange (a);
977 if (candidates != null)
978 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
984 // Inspect parent namespaces in namespace expression
986 Namespace parent_ns = ns.Parent;
988 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
989 if (candidates != null)
990 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
992 parent_ns = parent_ns.Parent;
993 } while (parent_ns != null);
996 // Continue in parent scope
998 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1001 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1003 // Precondition: Only simple names (no dots) will be looked up with this function.
1004 FullNamedExpression resolved = null;
1005 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1006 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1012 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1014 Report.SymbolRelatedToPreviousError (t1.Type);
1015 Report.SymbolRelatedToPreviousError (t2.Type);
1016 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1017 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1020 // Looks-up a alias named @name in this and surrounding namespace declarations
1021 public FullNamedExpression LookupAlias (string name)
1023 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1024 if (n.using_aliases == null)
1027 foreach (UsingAliasEntry ue in n.using_aliases) {
1028 if (ue.Alias == name)
1029 return ue.Resolve (Doppelganger);
1036 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1039 // Check whether it's in the namespace.
1041 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1046 if (using_aliases != null) {
1047 foreach (UsingAliasEntry ue in using_aliases) {
1048 if (ue.Alias == name) {
1050 if (Doppelganger != null) {
1051 // TODO: Namespace has broken location
1052 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1053 Report.SymbolRelatedToPreviousError (ue.Location, null);
1054 Report.Error (576, loc,
1055 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1056 GetSignatureForError (), name);
1062 return ue.Resolve (Doppelganger);
1074 // Check using entries.
1076 FullNamedExpression match = null;
1077 foreach (Namespace using_ns in GetUsingTable ()) {
1078 match = using_ns.Lookup (ds, name, loc);
1079 if (match == null || !(match is TypeExpr))
1083 Error_AmbiguousTypeReference (loc, name, fne, match);
1092 Namespace [] GetUsingTable ()
1094 if (namespace_using_table != null)
1095 return namespace_using_table;
1097 if (using_clauses == null) {
1098 namespace_using_table = empty_namespaces;
1099 return namespace_using_table;
1102 ArrayList list = new ArrayList (using_clauses.Count);
1104 foreach (UsingEntry ue in using_clauses) {
1105 Namespace using_ns = ue.Resolve (Doppelganger);
1106 if (using_ns == null)
1109 list.Add (using_ns);
1112 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1113 return namespace_using_table;
1116 static readonly string [] empty_using_list = new string [0];
1118 public int SymbolFileID {
1120 if (symfile_id == 0 && file.SourceFileEntry != null) {
1121 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1123 string [] using_list = empty_using_list;
1124 if (using_clauses != null) {
1125 using_list = new string [using_clauses.Count];
1126 for (int i = 0; i < using_clauses.Count; i++)
1127 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1130 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1136 static void MsgtryRef (string s)
1138 Console.WriteLine (" Try using -r:" + s);
1141 static void MsgtryPkg (string s)
1143 Console.WriteLine (" Try using -pkg:" + s);
1146 public static void Error_GlobalNamespaceRedefined (Location loc)
1148 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1151 public static void Error_NamespaceNotFound (Location loc, string name)
1153 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1157 case "Gtk": case "GtkSharp":
1158 MsgtryPkg ("gtk-sharp");
1161 case "Gdk": case "GdkSharp":
1162 MsgtryPkg ("gdk-sharp");
1165 case "Glade": case "GladeSharp":
1166 MsgtryPkg ("glade-sharp");
1169 case "System.Drawing":
1170 case "System.Web.Services":
1173 case "System.Windows.Forms":
1180 /// Used to validate that all the using clauses are correct
1181 /// after we are finished parsing all the files.
1185 if (using_aliases != null) {
1186 foreach (UsingAliasEntry ue in using_aliases)
1187 ue.Resolve (Doppelganger);
1190 if (using_clauses != null) {
1191 foreach (UsingEntry ue in using_clauses)
1192 ue.Resolve (Doppelganger);
1197 /// Used to validate that all the using clauses are correct
1198 /// after we are finished parsing all the files.
1200 static public void VerifyAllUsing ()
1202 foreach (NamespaceEntry entry in entries)
1203 entry.VerifyUsing ();
1206 public string GetSignatureForError ()
1208 return ns.GetSignatureForError ();
1211 public override string ToString ()
1213 return ns.ToString ();
1216 #region IResolveContext Members
1218 public DeclSpace DeclContainer {
1219 get { return SlaveDeclSpace; }
1222 public bool IsInObsoleteScope {
1223 get { return SlaveDeclSpace.IsInObsoleteScope; }
1226 public bool IsInUnsafeScope {
1227 get { return SlaveDeclSpace.IsInUnsafeScope; }
1230 public DeclSpace GenericDeclContainer {
1231 get { return SlaveDeclSpace; }