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 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;
26 protected readonly string alias_name;
27 protected Assembly [] referenced_assemblies;
29 Hashtable all_namespaces;
31 static RootNamespace ()
33 get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
36 public RootNamespace (string alias_name)
37 : base (null, String.Empty)
39 this.alias_name = alias_name;
40 referenced_assemblies = new Assembly [0];
42 all_namespaces = new Hashtable ();
43 all_namespaces.Add ("", this);
46 public void AddAssemblyReference (Assembly a)
48 foreach (Assembly assembly in referenced_assemblies) {
53 int top = referenced_assemblies.Length;
54 Assembly [] n = new Assembly [top + 1];
55 referenced_assemblies.CopyTo (n, 0);
57 referenced_assemblies = n;
60 public void ComputeNamespace (Type extensionType)
62 foreach (Assembly a in referenced_assemblies) {
64 ComputeNamespaces (a, extensionType);
65 } catch (TypeLoadException e) {
66 Report.Error (11, Location.Null, e.Message);
67 } catch (System.IO.FileNotFoundException) {
68 Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
74 public virtual Type LookupTypeReflection (string name, Location loc)
76 Type found_type = null;
78 foreach (Assembly a in referenced_assemblies) {
79 Type t = GetTypeInAssembly (a, name);
83 if (found_type == null) {
88 Report.SymbolRelatedToPreviousError (found_type);
89 Report.SymbolRelatedToPreviousError (t);
90 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
98 public void RegisterNamespace (Namespace child)
101 all_namespaces.Add (child.Name, child);
104 public bool IsNamespace (string name)
106 return all_namespaces.Contains (name);
109 protected void RegisterNamespace (string dotted_name)
111 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
112 GetNamespace (dotted_name, true);
115 void RegisterExtensionMethodClass (Type t)
117 string n = t.Namespace;
118 Namespace ns = n == null ? GlobalRootNamespace.Instance : (Namespace) all_namespaces[n];
120 ns = GetNamespace (n, true);
122 ns.RegisterExternalExtensionMethodClass (t);
125 void ComputeNamespaces (Assembly assembly, Type extensionType)
127 bool contains_extension_methods = extensionType != null && assembly.IsDefined (extensionType, false);
129 if (get_namespaces_method != null) {
130 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
131 foreach (string ns in namespaces)
132 RegisterNamespace (ns);
134 if (!contains_extension_methods)
138 foreach (Type t in assembly.GetTypes ()) {
139 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
140 contains_extension_methods && t.IsDefined (extensionType, false))
141 RegisterExtensionMethodClass (t);
143 if (get_namespaces_method == null)
144 RegisterNamespace (t.Namespace);
148 protected static Type GetTypeInAssembly (Assembly assembly, string name)
150 Type t = assembly.GetType (name);
155 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
157 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
158 if (ta == TypeAttributes.NestedPrivate)
161 if ((ta == TypeAttributes.NotPublic ||
162 ta == TypeAttributes.NestedAssembly ||
163 ta == TypeAttributes.NestedFamANDAssem) &&
164 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
170 public override string ToString ()
172 return String.Format ("RootNamespace ({0}::)", alias_name);
175 public override string GetSignatureForError ()
177 return alias_name + "::";
181 class GlobalRootNamespace : RootNamespace {
183 ListDictionary root_namespaces;
185 public static GlobalRootNamespace Instance = new GlobalRootNamespace ();
187 GlobalRootNamespace ()
190 root_namespaces = new ListDictionary ();
191 root_namespaces.Add (alias_name, this);
194 public static void Reset ()
196 Instance = new GlobalRootNamespace ();
199 public Assembly [] Assemblies {
200 get { return referenced_assemblies; }
203 public Module [] Modules {
204 get { return modules; }
207 public void AddModuleReference (Module m)
209 int top = modules != null ? modules.Length : 0;
210 Module [] n = new Module [top + 1];
212 modules.CopyTo (n, 0);
216 if (m == RootContext.ToplevelTypes.Builder)
219 foreach (Type t in m.GetTypes ())
220 RegisterNamespace (t.Namespace);
223 public void ComputeNamespaces ()
226 // Do very early lookup because type is required when we cache
227 // imported extension types in ComputeNamespaces
229 Type extension_attribute_type = TypeManager.CoreLookupType ("System.Runtime.CompilerServices", "ExtensionAttribute", Kind.Class, false);
231 foreach (RootNamespace rn in root_namespaces.Values) {
232 rn.ComputeNamespace (extension_attribute_type);
236 public void DefineRootNamespace (string alias, Assembly assembly)
238 if (alias == alias_name) {
239 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
243 RootNamespace retval = GetRootNamespace (alias);
244 if (retval == null) {
245 retval = new RootNamespace (alias);
246 root_namespaces.Add (alias, retval);
249 retval.AddAssemblyReference (assembly);
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 RootNamespace GetRootNamespace (string name)
260 return (RootNamespace) root_namespaces[name];
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 // Ignore, extension methods cannot be nested
522 if (type.DeclaringType != null)
525 if (type.IsNotPublic && !TypeManager.IsThisOrFriendAssembly (type.Assembly))
528 if (external_exmethod_classes == null)
529 external_exmethod_classes = new ArrayList ();
531 external_exmethod_classes.Add (type);
535 /// Looks for extension method in this namespace
537 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
539 ArrayList found = null;
541 if (declspaces != null) {
542 IEnumerator e = declspaces.Values.GetEnumerator ();
544 while (e.MoveNext ()) {
545 Class c = e.Current as Class;
549 if ((c.ModFlags & Modifiers.METHOD_EXTENSION) == 0)
552 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
559 found.AddRange (res);
563 if (external_exmethod_classes == null)
566 foreach (Type t in external_exmethod_classes) {
567 MemberCache m = TypeHandle.GetMemberCache (t);
568 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
575 found.AddRange (res);
581 public void AddDeclSpace (string name, DeclSpace ds)
583 if (declspaces == null)
584 declspaces = new HybridDictionary ();
585 declspaces.Add (name, ds);
588 public void RemoveDeclSpace (string name)
590 declspaces.Remove (name);
594 /// The qualified name of the current namespace
597 get { return fullname; }
601 /// The parent of this namespace, used by the parser to "Pop"
602 /// the current namespace declaration
604 public Namespace Parent {
605 get { return parent; }
610 // Namespace container as created by the parser
612 public class NamespaceEntry : IResolveContext {
615 readonly MemberName name;
618 public UsingEntry (MemberName name)
623 public string GetSignatureForError ()
625 return name.GetSignatureForError ();
628 public Location Location {
629 get { return name.Location; }
632 public MemberName MemberName {
637 get { return GetSignatureForError (); }
640 public Namespace Resolve (IResolveContext rc)
642 if (resolved != null)
645 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
649 resolved = fne as Namespace;
650 if (resolved == null) {
651 Report.SymbolRelatedToPreviousError (fne.Type);
652 Report.Error (138, Location,
653 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
654 GetSignatureForError ());
659 public override string ToString ()
665 class UsingAliasEntry {
666 public readonly string Alias;
667 public Location Location;
669 public UsingAliasEntry (string alias, Location loc)
675 public virtual FullNamedExpression Resolve (IResolveContext rc)
677 FullNamedExpression fne = GlobalRootNamespace.Instance.GetRootNamespace (Alias);
679 Report.Error (430, Location,
680 "The extern alias `{0}' was not specified in -reference option",
687 public override string ToString ()
694 class LocalUsingAliasEntry : UsingAliasEntry {
695 FullNamedExpression resolved;
698 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
704 public override FullNamedExpression Resolve (IResolveContext rc)
706 if (resolved != null || value == null)
709 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
710 if (resolved == null) {
715 TypeExpr te = resolved as TypeExpr;
717 if (!te.CheckAccessLevel (rc.DeclContainer)) {
718 Report.SymbolRelatedToPreviousError (te.Type);
719 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
726 public override string ToString ()
728 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
733 NamespaceEntry parent, implicit_parent;
734 CompilationUnit file;
737 // Namespace using import block
738 ArrayList using_aliases;
739 ArrayList using_clauses;
740 public bool DeclarationFound = false;
743 public readonly bool IsImplicit;
744 public readonly DeclSpace SlaveDeclSpace;
745 static readonly Namespace [] empty_namespaces = new Namespace [0];
746 Namespace [] namespace_using_table;
748 static ArrayList entries = new ArrayList ();
750 public static void Reset ()
752 entries = new ArrayList ();
755 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
757 this.parent = parent;
762 ns = parent.NS.GetNamespace (name, true);
763 else if (name != null)
764 ns = GlobalRootNamespace.Instance.GetNamespace (name, true);
766 ns = GlobalRootNamespace.Instance;
767 SlaveDeclSpace = new RootDeclSpace (this);
770 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
772 this.parent = parent;
774 this.IsImplicit = true;
776 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
780 // Populates the Namespace with some using declarations, used by the
783 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
785 foreach (UsingAliasEntry uae in source_using_aliases){
786 if (using_aliases == null)
787 using_aliases = new ArrayList ();
789 using_aliases.Add (uae);
792 foreach (UsingEntry ue in source_using_clauses){
793 if (using_clauses == null)
794 using_clauses = new ArrayList ();
796 using_clauses.Add (ue);
801 // Extracts the using alises and using clauses into a couple of
802 // arrays that might already have the same information; Used by the
805 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
807 if (using_aliases != null){
808 foreach (UsingAliasEntry uae in using_aliases){
809 bool replaced = false;
811 for (int i = 0; i < out_using_aliases.Count; i++){
812 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
814 if (out_uea.Alias == uae.Alias){
815 out_using_aliases [i] = uae;
821 out_using_aliases.Add (uae);
825 if (using_clauses != null){
826 foreach (UsingEntry ue in using_clauses){
829 foreach (UsingEntry out_ue in out_using_clauses)
830 if (out_ue.Name == ue.Name){
835 out_using_clauses.Add (ue);
841 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
842 // resolved as if the immediately containing namespace body has no using-directives.
844 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
845 // in the using-namespace-directive.
847 // To implement these rules, the expressions in the using directives are resolved using
848 // the "doppelganger" (ghostly bodiless duplicate).
850 NamespaceEntry doppelganger;
851 NamespaceEntry Doppelganger {
853 if (!IsImplicit && doppelganger == null) {
854 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
855 doppelganger.using_aliases = using_aliases;
861 public Namespace NS {
865 public NamespaceEntry Parent {
866 get { return parent; }
869 public NamespaceEntry ImplicitParent {
873 if (implicit_parent == null) {
874 implicit_parent = (parent.NS == ns.Parent)
876 : new NamespaceEntry (parent, file, ns.Parent, false);
878 return implicit_parent;
883 /// Records a new namespace for resolving name references
885 public void AddUsing (MemberName name, Location loc)
887 if (DeclarationFound){
888 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
891 if (using_clauses == null) {
892 using_clauses = new ArrayList ();
894 foreach (UsingEntry old_entry in using_clauses) {
895 if (name.Equals (old_entry.MemberName)) {
896 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
897 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
903 using_clauses.Add (new UsingEntry (name));
906 public void AddUsingAlias (string alias, MemberName name, Location loc)
908 // TODO: This is parser bussines
909 if (DeclarationFound){
910 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
913 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
914 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
915 " the global namespace will be used instead");
917 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
920 public void AddUsingExternalAlias (string alias, Location loc)
922 // TODO: Do this in parser
923 bool not_first = using_clauses != null || DeclarationFound;
924 if (using_aliases != null && !not_first) {
925 foreach (UsingAliasEntry uae in using_aliases) {
926 if (uae is LocalUsingAliasEntry) {
934 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
936 if (alias == "global") {
937 Error_GlobalNamespaceRedefined (loc);
941 AddUsingAlias (new UsingAliasEntry (alias, loc));
944 void AddUsingAlias (UsingAliasEntry uae)
946 if (using_aliases == null) {
947 using_aliases = new ArrayList ();
949 foreach (UsingAliasEntry entry in using_aliases) {
950 if (uae.Alias == entry.Alias) {
951 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
952 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
959 using_aliases.Add (uae);
963 /// Does extension methods look up to find a method which matches name and extensionType.
964 /// Search starts from this namespace and continues hierarchically up to top level.
966 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
968 ArrayList candidates = null;
969 if (currentClass != null) {
970 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
971 if (candidates != null)
972 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
975 foreach (Namespace n in GetUsingTable ()) {
976 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
980 if (candidates == null)
983 candidates.AddRange (a);
986 if (candidates != null)
987 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
993 // Inspect parent namespaces in namespace expression
995 Namespace parent_ns = ns.Parent;
997 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
998 if (candidates != null)
999 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1001 parent_ns = parent_ns.Parent;
1002 } while (parent_ns != null);
1005 // Continue in parent scope
1007 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1010 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1012 // Precondition: Only simple names (no dots) will be looked up with this function.
1013 FullNamedExpression resolved = null;
1014 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1015 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1021 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1023 Report.SymbolRelatedToPreviousError (t1.Type);
1024 Report.SymbolRelatedToPreviousError (t2.Type);
1025 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1026 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1029 // Looks-up a alias named @name in this and surrounding namespace declarations
1030 public FullNamedExpression LookupAlias (string name)
1032 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1033 if (n.using_aliases == null)
1036 foreach (UsingAliasEntry ue in n.using_aliases) {
1037 if (ue.Alias == name)
1038 return ue.Resolve (Doppelganger);
1045 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1048 // Check whether it's in the namespace.
1050 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1055 if (using_aliases != null) {
1056 foreach (UsingAliasEntry ue in using_aliases) {
1057 if (ue.Alias == name) {
1059 if (Doppelganger != null) {
1060 // TODO: Namespace has broken location
1061 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1062 Report.SymbolRelatedToPreviousError (ue.Location, null);
1063 Report.Error (576, loc,
1064 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1065 GetSignatureForError (), name);
1071 return ue.Resolve (Doppelganger);
1083 // Check using entries.
1085 FullNamedExpression match = null;
1086 foreach (Namespace using_ns in GetUsingTable ()) {
1087 match = using_ns.Lookup (ds, name, loc);
1088 if (match == null || !(match is TypeExpr))
1092 Error_AmbiguousTypeReference (loc, name, fne, match);
1101 Namespace [] GetUsingTable ()
1103 if (namespace_using_table != null)
1104 return namespace_using_table;
1106 if (using_clauses == null) {
1107 namespace_using_table = empty_namespaces;
1108 return namespace_using_table;
1111 ArrayList list = new ArrayList (using_clauses.Count);
1113 foreach (UsingEntry ue in using_clauses) {
1114 Namespace using_ns = ue.Resolve (Doppelganger);
1115 if (using_ns == null)
1118 list.Add (using_ns);
1121 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1122 return namespace_using_table;
1125 static readonly string [] empty_using_list = new string [0];
1127 public int SymbolFileID {
1129 if (symfile_id == 0 && file.SourceFileEntry != null) {
1130 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1132 string [] using_list = empty_using_list;
1133 if (using_clauses != null) {
1134 using_list = new string [using_clauses.Count];
1135 for (int i = 0; i < using_clauses.Count; i++)
1136 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1139 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1145 static void MsgtryRef (string s)
1147 Console.WriteLine (" Try using -r:" + s);
1150 static void MsgtryPkg (string s)
1152 Console.WriteLine (" Try using -pkg:" + s);
1155 public static void Error_GlobalNamespaceRedefined (Location loc)
1157 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1160 public static void Error_NamespaceNotFound (Location loc, string name)
1162 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1166 case "Gtk": case "GtkSharp":
1167 MsgtryPkg ("gtk-sharp");
1170 case "Gdk": case "GdkSharp":
1171 MsgtryPkg ("gdk-sharp");
1174 case "Glade": case "GladeSharp":
1175 MsgtryPkg ("glade-sharp");
1178 case "System.Drawing":
1179 case "System.Web.Services":
1182 case "System.Windows.Forms":
1189 /// Used to validate that all the using clauses are correct
1190 /// after we are finished parsing all the files.
1194 if (using_aliases != null) {
1195 foreach (UsingAliasEntry ue in using_aliases)
1196 ue.Resolve (Doppelganger);
1199 if (using_clauses != null) {
1200 foreach (UsingEntry ue in using_clauses)
1201 ue.Resolve (Doppelganger);
1206 /// Used to validate that all the using clauses are correct
1207 /// after we are finished parsing all the files.
1209 static public void VerifyAllUsing ()
1211 foreach (NamespaceEntry entry in entries)
1212 entry.VerifyUsing ();
1215 public string GetSignatureForError ()
1217 return ns.GetSignatureForError ();
1220 public override string ToString ()
1222 return ns.ToString ();
1225 #region IResolveContext Members
1227 public DeclSpace DeclContainer {
1228 get { return SlaveDeclSpace; }
1231 public bool IsInObsoleteScope {
1232 get { return SlaveDeclSpace.IsInObsoleteScope; }
1235 public bool IsInUnsafeScope {
1236 get { return SlaveDeclSpace.IsInUnsafeScope; }
1239 public DeclSpace GenericDeclContainer {
1240 get { return SlaveDeclSpace; }