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.Type, 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 (Type t, Location loc)
393 Report.SymbolRelatedToPreviousError (t);
394 Error_TypeArgumentsCannotBeUsed (loc, "type", TypeManager.CSharpName (t));
397 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
399 Report.SymbolRelatedToPreviousError (mi);
400 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
403 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
405 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
409 public override string GetSignatureForError ()
414 public Namespace GetNamespace (string name, bool create)
416 int pos = name.IndexOf ('.');
421 first = name.Substring (0, pos);
425 ns = (Namespace) namespaces [first];
430 ns = new Namespace (this, first);
431 namespaces.Add (first, ns);
435 ns = ns.GetNamespace (name.Substring (pos + 1), create);
440 public bool HasDefinition (string name)
442 return declspaces != null && declspaces [name] != null;
445 TypeExpr LookupType (string name, Location loc)
447 if (cached_types.Contains (name))
448 return cached_types [name] as TypeExpr;
451 if (declspaces != null) {
452 DeclSpace tdecl = declspaces [name] as DeclSpace;
455 // Note that this is not:
457 // t = tdecl.DefineType ()
459 // This is to make it somewhat more useful when a DefineType
460 // fails due to problems in nested types (more useful in the sense
461 // of fewer misleading error messages)
464 t = tdecl.TypeBuilder;
466 if (RootContext.EvalMode){
467 // Replace the TypeBuilder with a System.Type, as
468 // Reflection.Emit fails otherwise (we end up pretty
469 // much with Random type definitions later on).
470 Type tt = t.Assembly.GetType (t.Name);
476 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
477 Type rt = root.LookupTypeReflection (lookup, loc);
479 // HACK: loc.IsNull when the type is core type
480 if (t == null || (rt != null && loc.IsNull))
483 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
484 cached_types [name] = te;
489 /// Used for better error reporting only
491 public Type LookForAnyGenericType (string typeName)
493 if (declspaces == null)
496 typeName = SimpleName.RemoveGenericArity (typeName);
498 foreach (DictionaryEntry de in declspaces) {
499 string type_item = (string) de.Key;
500 int pos = type_item.LastIndexOf ('`');
501 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
502 return ((DeclSpace) de.Value).TypeBuilder;
507 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
509 if (namespaces.Contains (name))
510 return (Namespace) namespaces [name];
512 return LookupType (name, loc);
515 public void RegisterExternalExtensionMethodClass (Type type)
517 if (external_exmethod_classes == null)
518 external_exmethod_classes = new ArrayList ();
520 external_exmethod_classes.Add (type);
524 /// Looks for extension method in this namespace
526 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
528 ArrayList found = null;
530 if (declspaces != null) {
531 IEnumerator e = declspaces.Values.GetEnumerator ();
533 while (e.MoveNext ()) {
534 Class c = e.Current as Class;
538 if (!c.IsStaticClass)
541 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
548 found.AddRange (res);
552 if (external_exmethod_classes == null)
555 foreach (Type t in external_exmethod_classes) {
556 MemberCache m = TypeHandle.GetMemberCache (t);
557 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
564 found.AddRange (res);
570 public void AddDeclSpace (string name, DeclSpace ds)
572 if (declspaces == null)
573 declspaces = new HybridDictionary ();
574 declspaces.Add (name, ds);
577 public void RemoveDeclSpace (string name)
579 declspaces.Remove (name);
583 /// The qualified name of the current namespace
586 get { return fullname; }
590 /// The parent of this namespace, used by the parser to "Pop"
591 /// the current namespace declaration
593 public Namespace Parent {
594 get { return parent; }
599 // Namespace container as created by the parser
601 public class NamespaceEntry : IResolveContext {
604 readonly MemberName name;
607 public UsingEntry (MemberName name)
612 public string GetSignatureForError ()
614 return name.GetSignatureForError ();
617 public Location Location {
618 get { return name.Location; }
621 public MemberName MemberName {
626 get { return GetSignatureForError (); }
629 public Namespace Resolve (IResolveContext rc)
631 if (resolved != null)
634 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
638 resolved = fne as Namespace;
639 if (resolved == null) {
640 Report.SymbolRelatedToPreviousError (fne.Type);
641 Report.Error (138, Location,
642 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
643 GetSignatureForError ());
648 public override string ToString ()
654 class UsingAliasEntry {
655 public readonly string Alias;
656 public Location Location;
658 public UsingAliasEntry (string alias, Location loc)
664 public virtual FullNamedExpression Resolve (IResolveContext rc)
666 FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
668 Report.Error (430, Location,
669 "The extern alias `{0}' was not specified in -reference option",
676 public override string ToString ()
683 class LocalUsingAliasEntry : UsingAliasEntry {
687 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
693 public override FullNamedExpression Resolve (IResolveContext rc)
695 if (resolved != null || value == null)
696 return (FullNamedExpression)resolved;
698 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
699 if (resolved == null) {
704 // FIXME: This is quite wrong, the accessibility is not global
705 if (resolved.Type != null) {
706 TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
707 if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
708 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) &&
709 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
710 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
715 return (FullNamedExpression)resolved;
718 public override string ToString ()
720 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
725 NamespaceEntry parent, implicit_parent;
726 CompilationUnit file;
729 // Namespace using import block
730 ArrayList using_aliases;
731 ArrayList using_clauses;
732 public bool DeclarationFound = false;
735 public readonly bool IsImplicit;
736 public readonly DeclSpace SlaveDeclSpace;
737 static readonly Namespace [] empty_namespaces = new Namespace [0];
738 Namespace [] namespace_using_table;
740 static ArrayList entries = new ArrayList ();
742 public static void Reset ()
744 entries = new ArrayList ();
747 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
749 this.parent = parent;
754 ns = parent.NS.GetNamespace (name, true);
755 else if (name != null)
756 ns = RootNamespace.Global.GetNamespace (name, true);
758 ns = RootNamespace.Global;
759 SlaveDeclSpace = new RootDeclSpace (this);
762 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
764 this.parent = parent;
766 this.IsImplicit = true;
768 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
772 // Populates the Namespace with some using declarations, used by the
775 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
777 foreach (UsingAliasEntry uae in source_using_aliases){
778 if (using_aliases == null)
779 using_aliases = new ArrayList ();
781 using_aliases.Add (uae);
784 foreach (UsingEntry ue in source_using_clauses){
785 if (using_clauses == null)
786 using_clauses = new ArrayList ();
788 using_clauses.Add (ue);
793 // Extracts the using alises and using clauses into a couple of
794 // arrays that might already have the same information; Used by the
797 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
799 if (using_aliases != null){
800 foreach (UsingAliasEntry uae in using_aliases){
801 bool replaced = false;
803 for (int i = 0; i < out_using_aliases.Count; i++){
804 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
806 if (out_uea.Alias == uae.Alias){
807 out_using_aliases [i] = uae;
813 out_using_aliases.Add (uae);
817 if (using_clauses != null){
818 foreach (UsingEntry ue in using_clauses){
821 foreach (UsingEntry out_ue in out_using_clauses)
822 if (out_ue.Name == ue.Name){
827 out_using_clauses.Add (ue);
833 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
834 // resolved as if the immediately containing namespace body has no using-directives.
836 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
837 // in the using-namespace-directive.
839 // To implement these rules, the expressions in the using directives are resolved using
840 // the "doppelganger" (ghostly bodiless duplicate).
842 NamespaceEntry doppelganger;
843 NamespaceEntry Doppelganger {
845 if (!IsImplicit && doppelganger == null) {
846 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
847 doppelganger.using_aliases = using_aliases;
853 public Namespace NS {
857 public NamespaceEntry Parent {
858 get { return parent; }
861 public NamespaceEntry ImplicitParent {
865 if (implicit_parent == null) {
866 implicit_parent = (parent.NS == ns.Parent)
868 : new NamespaceEntry (parent, file, ns.Parent, false);
870 return implicit_parent;
875 /// Records a new namespace for resolving name references
877 public void AddUsing (MemberName name, Location loc)
879 if (DeclarationFound){
880 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
883 if (using_clauses == null) {
884 using_clauses = new ArrayList ();
886 foreach (UsingEntry old_entry in using_clauses) {
887 if (name.Equals (old_entry.MemberName)) {
888 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
889 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
895 using_clauses.Add (new UsingEntry (name));
898 public void AddUsingAlias (string alias, MemberName name, Location loc)
900 // TODO: This is parser bussines
901 if (DeclarationFound){
902 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
905 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
906 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
907 " the global namespace will be used instead");
909 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
912 public void AddUsingExternalAlias (string alias, Location loc)
914 // TODO: Do this in parser
915 bool not_first = using_clauses != null || DeclarationFound;
916 if (using_aliases != null && !not_first) {
917 foreach (UsingAliasEntry uae in using_aliases) {
918 if (uae is LocalUsingAliasEntry) {
926 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
928 if (alias == "global") {
929 Error_GlobalNamespaceRedefined (loc);
933 AddUsingAlias (new UsingAliasEntry (alias, loc));
936 void AddUsingAlias (UsingAliasEntry uae)
938 if (using_aliases == null) {
939 using_aliases = new ArrayList ();
941 foreach (UsingAliasEntry entry in using_aliases) {
942 if (uae.Alias == entry.Alias) {
943 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
944 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
951 using_aliases.Add (uae);
955 /// Does extension methods look up to find a method which matches name and extensionType.
956 /// Search starts from this namespace and continues hierarchically up to top level.
958 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
960 ArrayList candidates = null;
961 if (currentClass != null) {
962 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
963 if (candidates != null)
964 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
967 foreach (Namespace n in GetUsingTable ()) {
968 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
972 if (candidates == null)
975 candidates.AddRange (a);
978 if (candidates != null)
979 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
985 // Inspect parent namespaces in namespace expression
987 Namespace parent_ns = ns.Parent;
989 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
990 if (candidates != null)
991 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
993 parent_ns = parent_ns.Parent;
994 } while (parent_ns != null);
997 // Continue in parent scope
999 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1002 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1004 // Precondition: Only simple names (no dots) will be looked up with this function.
1005 FullNamedExpression resolved = null;
1006 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1007 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1013 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1015 Report.SymbolRelatedToPreviousError (t1.Type);
1016 Report.SymbolRelatedToPreviousError (t2.Type);
1017 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1018 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1021 // Looks-up a alias named @name in this and surrounding namespace declarations
1022 public FullNamedExpression LookupAlias (string name)
1024 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1025 if (n.using_aliases == null)
1028 foreach (UsingAliasEntry ue in n.using_aliases) {
1029 if (ue.Alias == name)
1030 return ue.Resolve (Doppelganger);
1037 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1040 // Check whether it's in the namespace.
1042 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1047 if (using_aliases != null) {
1048 foreach (UsingAliasEntry ue in using_aliases) {
1049 if (ue.Alias == name) {
1051 if (Doppelganger != null) {
1052 // TODO: Namespace has broken location
1053 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1054 Report.SymbolRelatedToPreviousError (ue.Location, null);
1055 Report.Error (576, loc,
1056 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1057 GetSignatureForError (), name);
1063 return ue.Resolve (Doppelganger);
1075 // Check using entries.
1077 FullNamedExpression match = null;
1078 foreach (Namespace using_ns in GetUsingTable ()) {
1079 match = using_ns.Lookup (ds, name, loc);
1080 if (match == null || !(match is TypeExpr))
1084 Error_AmbiguousTypeReference (loc, name, fne, match);
1093 Namespace [] GetUsingTable ()
1095 if (namespace_using_table != null)
1096 return namespace_using_table;
1098 if (using_clauses == null) {
1099 namespace_using_table = empty_namespaces;
1100 return namespace_using_table;
1103 ArrayList list = new ArrayList (using_clauses.Count);
1105 foreach (UsingEntry ue in using_clauses) {
1106 Namespace using_ns = ue.Resolve (Doppelganger);
1107 if (using_ns == null)
1110 list.Add (using_ns);
1113 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1114 return namespace_using_table;
1117 static readonly string [] empty_using_list = new string [0];
1119 public int SymbolFileID {
1121 if (symfile_id == 0 && file.SourceFileEntry != null) {
1122 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1124 string [] using_list = empty_using_list;
1125 if (using_clauses != null) {
1126 using_list = new string [using_clauses.Count];
1127 for (int i = 0; i < using_clauses.Count; i++)
1128 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1131 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1137 static void MsgtryRef (string s)
1139 Console.WriteLine (" Try using -r:" + s);
1142 static void MsgtryPkg (string s)
1144 Console.WriteLine (" Try using -pkg:" + s);
1147 public static void Error_GlobalNamespaceRedefined (Location loc)
1149 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1152 public static void Error_NamespaceNotFound (Location loc, string name)
1154 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1158 case "Gtk": case "GtkSharp":
1159 MsgtryPkg ("gtk-sharp");
1162 case "Gdk": case "GdkSharp":
1163 MsgtryPkg ("gdk-sharp");
1166 case "Glade": case "GladeSharp":
1167 MsgtryPkg ("glade-sharp");
1170 case "System.Drawing":
1171 case "System.Web.Services":
1174 case "System.Windows.Forms":
1181 /// Used to validate that all the using clauses are correct
1182 /// after we are finished parsing all the files.
1186 if (using_aliases != null) {
1187 foreach (UsingAliasEntry ue in using_aliases)
1188 ue.Resolve (Doppelganger);
1191 if (using_clauses != null) {
1192 foreach (UsingEntry ue in using_clauses)
1193 ue.Resolve (Doppelganger);
1198 /// Used to validate that all the using clauses are correct
1199 /// after we are finished parsing all the files.
1201 static public void VerifyAllUsing ()
1203 foreach (NamespaceEntry entry in entries)
1204 entry.VerifyUsing ();
1207 public string GetSignatureForError ()
1209 return ns.GetSignatureForError ();
1212 public override string ToString ()
1214 return ns.ToString ();
1217 #region IResolveContext Members
1219 public DeclSpace DeclContainer {
1220 get { return SlaveDeclSpace; }
1223 public bool IsInObsoleteScope {
1224 get { return SlaveDeclSpace.IsInObsoleteScope; }
1227 public bool IsInUnsafeScope {
1228 get { return SlaveDeclSpace.IsInUnsafeScope; }
1231 public DeclSpace GenericDeclContainer {
1232 get { return SlaveDeclSpace; }