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 // How to test whether attribute exists without loading the assembly :-(
67 const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
69 const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
71 if (TypeManager.extension_attribute_type == null &&
72 a.FullName == SystemCore) {
73 TypeManager.extension_attribute_type = a.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
76 int top = referenced_assemblies.Length;
77 Assembly [] n = new Assembly [top + 1];
78 referenced_assemblies.CopyTo (n, 0);
80 referenced_assemblies = n;
83 public static void DefineRootNamespace (string alias, Assembly assembly)
85 if (alias == "global") {
86 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
90 RootNamespace retval = GetRootNamespace (alias);
92 retval = new RootNamespace (alias);
93 root_namespaces.Add (alias, retval);
96 retval.AddAssemblyReference (assembly);
99 public static RootNamespace GetRootNamespace (string name)
101 return (RootNamespace) root_namespaces [name];
104 public static void ComputeNamespaces ()
106 foreach (RootNamespace rn in root_namespaces.Values) {
107 foreach (Assembly a in rn.referenced_assemblies) {
109 rn.ComputeNamespaces (a);
110 } catch (TypeLoadException e) {
111 Report.Error (11, Location.Null, e.Message);
112 } catch (System.IO.FileNotFoundException) {
113 Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
120 public virtual Type LookupTypeReflection (string name, Location loc)
122 Type found_type = null;
124 foreach (Assembly a in referenced_assemblies) {
125 Type t = GetTypeInAssembly (a, name);
129 if (found_type == null) {
134 Report.SymbolRelatedToPreviousError (found_type);
135 Report.SymbolRelatedToPreviousError (t);
136 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
144 public void RegisterNamespace (Namespace child)
147 all_namespaces.Add (child.Name, child);
150 public bool IsNamespace (string name)
152 return all_namespaces.Contains (name);
155 protected void RegisterNamespace (string dotted_name)
157 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
158 GetNamespace (dotted_name, true);
161 void RegisterExtensionMethodClass (Type t)
163 string n = t.Namespace;
164 Namespace ns = n == null ? Global : (Namespace)all_namespaces [n];
166 ns = GetNamespace (n, true);
168 ns.RegisterExternalExtensionMethodClass (t);
171 protected void ComputeNamespaces (Assembly assembly)
173 bool contains_extension_methods = TypeManager.extension_attribute_type != null &&
174 assembly.IsDefined(TypeManager.extension_attribute_type, false);
176 if (get_namespaces_method != null && !contains_extension_methods) {
177 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
178 foreach (string ns in namespaces)
179 RegisterNamespace (ns);
183 foreach (Type t in assembly.GetExportedTypes ()) {
184 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
185 contains_extension_methods && t.IsDefined (TypeManager.extension_attribute_type, false))
186 RegisterExtensionMethodClass (t);
188 RegisterNamespace (t.Namespace);
192 protected static Type GetTypeInAssembly (Assembly assembly, string name)
194 Type t = assembly.GetType (name);
199 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
201 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
202 if (ta == TypeAttributes.NestedPrivate)
205 if ((ta == TypeAttributes.NotPublic ||
206 ta == TypeAttributes.NestedAssembly ||
207 ta == TypeAttributes.NestedFamANDAssem) &&
208 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
214 public override string ToString ()
216 return String.Format ("RootNamespace ({0}::)", alias_name);
219 public override string GetSignatureForError ()
221 return alias_name + "::";
225 public class GlobalRootNamespace : RootNamespace {
228 public GlobalRootNamespace ()
233 public Assembly [] Assemblies {
234 get { return referenced_assemblies; }
237 public Module [] Modules {
238 get { return modules; }
241 public void AddModuleReference (Module m)
243 int top = modules != null ? modules.Length : 0;
244 Module [] n = new Module [top + 1];
246 modules.CopyTo (n, 0);
250 if (m == CodeGen.Module.Builder)
253 foreach (Type t in m.GetTypes ())
254 RegisterNamespace (t.Namespace);
257 public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
259 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
263 public override Type LookupTypeReflection (string name, Location loc)
265 Type found_type = base.LookupTypeReflection (name, loc);
267 if (modules != null) {
268 foreach (Module module in modules) {
269 Type t = module.GetType (name);
273 if (found_type == null) {
278 Report.SymbolRelatedToPreviousError (found_type);
280 DeclSpace ds = TypeManager.LookupDeclSpace (t);
281 Report.Warning (1685, 1, ds.Location, "The type `{0}' conflicts with the predefined type `{1}' and will be ignored",
282 ds.GetSignatureForError (), TypeManager.CSharpName (found_type));
285 Report.SymbolRelatedToPreviousError (t);
286 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
287 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
297 /// Keeps track of the namespaces defined in the C# code.
299 /// This is an Expression to allow it to be referenced in the
300 /// compiler parse/intermediate tree during name resolution.
302 public class Namespace : FullNamedExpression {
306 IDictionary namespaces;
307 IDictionary declspaces;
308 Hashtable cached_types;
310 ArrayList external_exmethod_classes;
312 public readonly MemberName MemberName;
315 /// Constructor Takes the current namespace and the
316 /// name. This is bootstrapped with parent == null
319 public Namespace (Namespace parent, string name)
321 // Expression members.
322 this.eclass = ExprClass.Namespace;
323 this.Type = typeof (Namespace);
324 this.loc = Location.Null;
326 this.parent = parent;
329 this.root = parent.root;
331 this.root = this as RootNamespace;
333 if (this.root == null)
334 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
336 string pname = parent != null ? parent.fullname : "";
341 fullname = parent.fullname + "." + name;
343 if (fullname == null)
344 throw new InternalErrorException ("Namespace has a null fullname");
346 if (parent != null && parent.MemberName != MemberName.Null)
347 MemberName = new MemberName (parent.MemberName, name);
348 else if (name.Length == 0)
349 MemberName = MemberName.Null;
351 MemberName = new MemberName (name);
353 namespaces = new HybridDictionary ();
354 cached_types = new Hashtable ();
356 root.RegisterNamespace (this);
359 public override Expression DoResolve (EmitContext ec)
364 public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
366 if (name.IndexOf ('`') > 0) {
367 FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
368 if (retval != null) {
369 Error_TypeArgumentsCannotBeUsed (retval, loc);
373 Type t = LookForAnyGenericType (name);
375 Error_InvalidNumberOfTypeArguments (t, loc);
380 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
381 name, GetSignatureForError ());
384 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
386 Report.SymbolRelatedToPreviousError (t);
387 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
388 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
391 public static void Error_TypeArgumentsCannotBeUsed (FullNamedExpression expr, Location loc)
393 if (expr is TypeExpr) {
394 Report.SymbolRelatedToPreviousError (expr.Type);
395 Error_TypeArgumentsCannotBeUsed (loc, "type", expr.GetSignatureForError ());
397 expr.Error_ExpressionCannotBeGeneric (loc);
401 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
403 Report.SymbolRelatedToPreviousError (mi);
404 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
407 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
409 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
413 public override string GetSignatureForError ()
418 public Namespace GetNamespace (string name, bool create)
420 int pos = name.IndexOf ('.');
425 first = name.Substring (0, pos);
429 ns = (Namespace) namespaces [first];
434 ns = new Namespace (this, first);
435 namespaces.Add (first, ns);
439 ns = ns.GetNamespace (name.Substring (pos + 1), create);
444 public bool HasDefinition (string name)
446 return declspaces != null && declspaces [name] != null;
449 TypeExpr LookupType (string name, Location loc)
451 if (cached_types.Contains (name))
452 return cached_types [name] as TypeExpr;
455 if (declspaces != null) {
456 DeclSpace tdecl = declspaces [name] as DeclSpace;
459 // Note that this is not:
461 // t = tdecl.DefineType ()
463 // This is to make it somewhat more useful when a DefineType
464 // fails due to problems in nested types (more useful in the sense
465 // of fewer misleading error messages)
468 t = tdecl.TypeBuilder;
470 if (RootContext.EvalMode){
471 // Replace the TypeBuilder with a System.Type, as
472 // Reflection.Emit fails otherwise (we end up pretty
473 // much with Random type definitions later on).
474 Type tt = t.Assembly.GetType (t.Name);
480 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
481 Type rt = root.LookupTypeReflection (lookup, loc);
483 // HACK: loc.IsNull when the type is core type
484 if (t == null || (rt != null && loc.IsNull))
487 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
488 cached_types [name] = te;
493 /// Used for better error reporting only
495 public Type LookForAnyGenericType (string typeName)
497 if (declspaces == null)
500 typeName = SimpleName.RemoveGenericArity (typeName);
502 foreach (DictionaryEntry de in declspaces) {
503 string type_item = (string) de.Key;
504 int pos = type_item.LastIndexOf ('`');
505 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
506 return ((DeclSpace) de.Value).TypeBuilder;
511 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
513 if (namespaces.Contains (name))
514 return (Namespace) namespaces [name];
516 return LookupType (name, loc);
519 public void RegisterExternalExtensionMethodClass (Type type)
521 if (external_exmethod_classes == null)
522 external_exmethod_classes = new ArrayList ();
524 external_exmethod_classes.Add (type);
528 /// Looks for extension method in this namespace
530 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
532 ArrayList found = null;
534 if (declspaces != null) {
535 IEnumerator e = declspaces.Values.GetEnumerator ();
537 while (e.MoveNext ()) {
538 Class c = e.Current as Class;
542 if (!c.IsStaticClass)
545 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
552 found.AddRange (res);
556 if (external_exmethod_classes == null)
559 foreach (Type t in external_exmethod_classes) {
560 MemberCache m = TypeHandle.GetMemberCache (t);
561 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
568 found.AddRange (res);
574 public void AddDeclSpace (string name, DeclSpace ds)
576 if (declspaces == null)
577 declspaces = new HybridDictionary ();
578 declspaces.Add (name, ds);
581 public void RemoveDeclSpace (string name)
583 declspaces.Remove (name);
587 /// The qualified name of the current namespace
590 get { return fullname; }
594 /// The parent of this namespace, used by the parser to "Pop"
595 /// the current namespace declaration
597 public Namespace Parent {
598 get { return parent; }
603 // Namespace container as created by the parser
605 public class NamespaceEntry : IResolveContext {
608 readonly MemberName name;
611 public UsingEntry (MemberName name)
616 public string GetSignatureForError ()
618 return name.GetSignatureForError ();
621 public Location Location {
622 get { return name.Location; }
625 public MemberName MemberName {
630 get { return GetSignatureForError (); }
633 public Namespace Resolve (IResolveContext rc)
635 if (resolved != null)
638 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
642 resolved = fne as Namespace;
643 if (resolved == null) {
644 Report.SymbolRelatedToPreviousError (fne.Type);
645 Report.Error (138, Location,
646 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
647 GetSignatureForError ());
652 public override string ToString ()
658 class UsingAliasEntry {
659 public readonly string Alias;
660 public Location Location;
662 public UsingAliasEntry (string alias, Location loc)
668 public virtual FullNamedExpression Resolve (IResolveContext rc)
670 FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
672 Report.Error (430, Location,
673 "The extern alias `{0}' was not specified in -reference option",
680 public override string ToString ()
687 class LocalUsingAliasEntry : UsingAliasEntry {
691 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
697 public override FullNamedExpression Resolve (IResolveContext rc)
699 if (resolved != null || value == null)
700 return (FullNamedExpression)resolved;
702 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
703 if (resolved == null) {
708 // FIXME: This is quite wrong, the accessibility is not global
709 if (resolved.Type != null) {
710 TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
711 if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
712 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) &&
713 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
714 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
719 return (FullNamedExpression)resolved;
722 public override string ToString ()
724 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
729 NamespaceEntry parent, implicit_parent;
730 CompilationUnit file;
733 // Namespace using import block
734 ArrayList using_aliases;
735 ArrayList using_clauses;
736 public bool DeclarationFound = false;
739 public readonly bool IsImplicit;
740 public readonly DeclSpace SlaveDeclSpace;
741 static readonly Namespace [] empty_namespaces = new Namespace [0];
742 Namespace [] namespace_using_table;
744 static ArrayList entries = new ArrayList ();
746 public static void Reset ()
748 entries = new ArrayList ();
751 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
753 this.parent = parent;
758 ns = parent.NS.GetNamespace (name, true);
759 else if (name != null)
760 ns = RootNamespace.Global.GetNamespace (name, true);
762 ns = RootNamespace.Global;
763 SlaveDeclSpace = new RootDeclSpace (this);
766 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
768 this.parent = parent;
770 this.IsImplicit = true;
772 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
776 // Populates the Namespace with some using declarations, used by the
779 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
781 foreach (UsingAliasEntry uae in source_using_aliases){
782 if (using_aliases == null)
783 using_aliases = new ArrayList ();
785 using_aliases.Add (uae);
788 foreach (UsingEntry ue in source_using_clauses){
789 if (using_clauses == null)
790 using_clauses = new ArrayList ();
792 using_clauses.Add (ue);
797 // Extracts the using alises and using clauses into a couple of
798 // arrays that might already have the same information; Used by the
801 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
803 if (using_aliases != null){
804 foreach (UsingAliasEntry uae in using_aliases){
805 bool replaced = false;
807 for (int i = 0; i < out_using_aliases.Count; i++){
808 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
810 if (out_uea.Alias == uae.Alias){
811 out_using_aliases [i] = uae;
817 out_using_aliases.Add (uae);
821 if (using_clauses != null){
822 foreach (UsingEntry ue in using_clauses){
825 foreach (UsingEntry out_ue in out_using_clauses)
826 if (out_ue.Name == ue.Name){
831 out_using_clauses.Add (ue);
837 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
838 // resolved as if the immediately containing namespace body has no using-directives.
840 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
841 // in the using-namespace-directive.
843 // To implement these rules, the expressions in the using directives are resolved using
844 // the "doppelganger" (ghostly bodiless duplicate).
846 NamespaceEntry doppelganger;
847 NamespaceEntry Doppelganger {
849 if (!IsImplicit && doppelganger == null) {
850 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
851 doppelganger.using_aliases = using_aliases;
857 public Namespace NS {
861 public NamespaceEntry Parent {
862 get { return parent; }
865 public NamespaceEntry ImplicitParent {
869 if (implicit_parent == null) {
870 implicit_parent = (parent.NS == ns.Parent)
872 : new NamespaceEntry (parent, file, ns.Parent, false);
874 return implicit_parent;
879 /// Records a new namespace for resolving name references
881 public void AddUsing (MemberName name, Location loc)
883 if (DeclarationFound){
884 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
887 if (using_clauses == null) {
888 using_clauses = new ArrayList ();
890 foreach (UsingEntry old_entry in using_clauses) {
891 if (name.Equals (old_entry.MemberName)) {
892 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
893 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
899 using_clauses.Add (new UsingEntry (name));
902 public void AddUsingAlias (string alias, MemberName name, Location loc)
904 // TODO: This is parser bussines
905 if (DeclarationFound){
906 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
909 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
910 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
911 " the global namespace will be used instead");
913 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
916 public void AddUsingExternalAlias (string alias, Location loc)
918 // TODO: Do this in parser
919 bool not_first = using_clauses != null || DeclarationFound;
920 if (using_aliases != null && !not_first) {
921 foreach (UsingAliasEntry uae in using_aliases) {
922 if (uae is LocalUsingAliasEntry) {
930 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
932 if (alias == "global") {
933 Error_GlobalNamespaceRedefined (loc);
937 AddUsingAlias (new UsingAliasEntry (alias, loc));
940 void AddUsingAlias (UsingAliasEntry uae)
942 if (using_aliases == null) {
943 using_aliases = new ArrayList ();
945 foreach (UsingAliasEntry entry in using_aliases) {
946 if (uae.Alias == entry.Alias) {
947 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
948 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
955 using_aliases.Add (uae);
959 /// Does extension methods look up to find a method which matches name and extensionType.
960 /// Search starts from this namespace and continues hierarchically up to top level.
962 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
964 ArrayList candidates = null;
965 if (currentClass != null) {
966 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
967 if (candidates != null)
968 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
971 foreach (Namespace n in GetUsingTable ()) {
972 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
976 if (candidates == null)
979 candidates.AddRange (a);
982 if (candidates != null)
983 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
989 // Inspect parent namespaces in namespace expression
991 Namespace parent_ns = ns.Parent;
993 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
994 if (candidates != null)
995 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
997 parent_ns = parent_ns.Parent;
998 } while (parent_ns != null);
1001 // Continue in parent scope
1003 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1006 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1008 // Precondition: Only simple names (no dots) will be looked up with this function.
1009 FullNamedExpression resolved = null;
1010 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1011 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1017 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1019 Report.SymbolRelatedToPreviousError (t1.Type);
1020 Report.SymbolRelatedToPreviousError (t2.Type);
1021 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1022 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1025 // Looks-up a alias named @name in this and surrounding namespace declarations
1026 public FullNamedExpression LookupAlias (string name)
1028 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1029 if (n.using_aliases == null)
1032 foreach (UsingAliasEntry ue in n.using_aliases) {
1033 if (ue.Alias == name)
1034 return ue.Resolve (Doppelganger);
1041 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1044 // Check whether it's in the namespace.
1046 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1051 if (using_aliases != null) {
1052 foreach (UsingAliasEntry ue in using_aliases) {
1053 if (ue.Alias == name) {
1055 if (Doppelganger != null) {
1056 // TODO: Namespace has broken location
1057 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1058 Report.SymbolRelatedToPreviousError (ue.Location, null);
1059 Report.Error (576, loc,
1060 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1061 GetSignatureForError (), name);
1067 return ue.Resolve (Doppelganger);
1079 // Check using entries.
1081 FullNamedExpression match = null;
1082 foreach (Namespace using_ns in GetUsingTable ()) {
1083 match = using_ns.Lookup (ds, name, loc);
1084 if (match == null || !(match is TypeExpr))
1088 Error_AmbiguousTypeReference (loc, name, fne, match);
1097 Namespace [] GetUsingTable ()
1099 if (namespace_using_table != null)
1100 return namespace_using_table;
1102 if (using_clauses == null) {
1103 namespace_using_table = empty_namespaces;
1104 return namespace_using_table;
1107 ArrayList list = new ArrayList (using_clauses.Count);
1109 foreach (UsingEntry ue in using_clauses) {
1110 Namespace using_ns = ue.Resolve (Doppelganger);
1111 if (using_ns == null)
1114 list.Add (using_ns);
1117 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1118 return namespace_using_table;
1121 static readonly string [] empty_using_list = new string [0];
1123 public int SymbolFileID {
1125 if (symfile_id == 0 && file.SourceFileEntry != null) {
1126 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1128 string [] using_list = empty_using_list;
1129 if (using_clauses != null) {
1130 using_list = new string [using_clauses.Count];
1131 for (int i = 0; i < using_clauses.Count; i++)
1132 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1135 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1141 static void MsgtryRef (string s)
1143 Console.WriteLine (" Try using -r:" + s);
1146 static void MsgtryPkg (string s)
1148 Console.WriteLine (" Try using -pkg:" + s);
1151 public static void Error_GlobalNamespaceRedefined (Location loc)
1153 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1156 public static void Error_NamespaceNotFound (Location loc, string name)
1158 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1162 case "Gtk": case "GtkSharp":
1163 MsgtryPkg ("gtk-sharp");
1166 case "Gdk": case "GdkSharp":
1167 MsgtryPkg ("gdk-sharp");
1170 case "Glade": case "GladeSharp":
1171 MsgtryPkg ("glade-sharp");
1174 case "System.Drawing":
1175 case "System.Web.Services":
1178 case "System.Windows.Forms":
1185 /// Used to validate that all the using clauses are correct
1186 /// after we are finished parsing all the files.
1190 if (using_aliases != null) {
1191 foreach (UsingAliasEntry ue in using_aliases)
1192 ue.Resolve (Doppelganger);
1195 if (using_clauses != null) {
1196 foreach (UsingEntry ue in using_clauses)
1197 ue.Resolve (Doppelganger);
1202 /// Used to validate that all the using clauses are correct
1203 /// after we are finished parsing all the files.
1205 static public void VerifyAllUsing ()
1207 foreach (NamespaceEntry entry in entries)
1208 entry.VerifyUsing ();
1211 public string GetSignatureForError ()
1213 return ns.GetSignatureForError ();
1216 public override string ToString ()
1218 return ns.ToString ();
1221 #region IResolveContext Members
1223 public DeclSpace DeclContainer {
1224 get { return SlaveDeclSpace; }
1227 public bool IsInObsoleteScope {
1228 get { return SlaveDeclSpace.IsInObsoleteScope; }
1231 public bool IsInUnsafeScope {
1232 get { return SlaveDeclSpace.IsInUnsafeScope; }
1235 public DeclSpace GenericDeclContainer {
1236 get { return SlaveDeclSpace; }