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) {
172 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
173 foreach (string ns in namespaces)
174 RegisterNamespace (ns);
176 if (!contains_extension_methods)
180 foreach (Type t in assembly.GetTypes ()) {
181 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
182 contains_extension_methods && t.IsDefined (TypeManager.extension_attribute_type, false))
183 RegisterExtensionMethodClass (t);
185 if (get_namespaces_method == null)
186 RegisterNamespace (t.Namespace);
190 protected static Type GetTypeInAssembly (Assembly assembly, string name)
192 Type t = assembly.GetType (name);
197 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
199 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
200 if (ta == TypeAttributes.NestedPrivate)
203 if ((ta == TypeAttributes.NotPublic ||
204 ta == TypeAttributes.NestedAssembly ||
205 ta == TypeAttributes.NestedFamANDAssem) &&
206 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
212 public override string ToString ()
214 return String.Format ("RootNamespace ({0}::)", alias_name);
217 public override string GetSignatureForError ()
219 return alias_name + "::";
223 public class GlobalRootNamespace : RootNamespace {
226 public GlobalRootNamespace ()
231 public Assembly [] Assemblies {
232 get { return referenced_assemblies; }
235 public Module [] Modules {
236 get { return modules; }
239 public void AddModuleReference (Module m)
241 int top = modules != null ? modules.Length : 0;
242 Module [] n = new Module [top + 1];
244 modules.CopyTo (n, 0);
248 if (m == CodeGen.Module.Builder)
251 foreach (Type t in m.GetTypes ())
252 RegisterNamespace (t.Namespace);
255 public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
257 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
261 public override Type LookupTypeReflection (string name, Location loc)
263 Type found_type = base.LookupTypeReflection (name, loc);
265 if (modules != null) {
266 foreach (Module module in modules) {
267 Type t = module.GetType (name);
271 if (found_type == null) {
276 Report.SymbolRelatedToPreviousError (found_type);
278 DeclSpace ds = TypeManager.LookupDeclSpace (t);
279 Report.Warning (1685, 1, ds.Location, "The type `{0}' conflicts with the predefined type `{1}' and will be ignored",
280 ds.GetSignatureForError (), TypeManager.CSharpName (found_type));
283 Report.SymbolRelatedToPreviousError (t);
284 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
285 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
295 /// Keeps track of the namespaces defined in the C# code.
297 /// This is an Expression to allow it to be referenced in the
298 /// compiler parse/intermediate tree during name resolution.
300 public class Namespace : FullNamedExpression {
304 IDictionary namespaces;
305 IDictionary declspaces;
306 Hashtable cached_types;
308 ArrayList external_exmethod_classes;
310 public readonly MemberName MemberName;
313 /// Constructor Takes the current namespace and the
314 /// name. This is bootstrapped with parent == null
317 public Namespace (Namespace parent, string name)
319 // Expression members.
320 this.eclass = ExprClass.Namespace;
321 this.Type = typeof (Namespace);
322 this.loc = Location.Null;
324 this.parent = parent;
327 this.root = parent.root;
329 this.root = this as RootNamespace;
331 if (this.root == null)
332 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
334 string pname = parent != null ? parent.fullname : "";
339 fullname = parent.fullname + "." + name;
341 if (fullname == null)
342 throw new InternalErrorException ("Namespace has a null fullname");
344 if (parent != null && parent.MemberName != MemberName.Null)
345 MemberName = new MemberName (parent.MemberName, name);
346 else if (name.Length == 0)
347 MemberName = MemberName.Null;
349 MemberName = new MemberName (name);
351 namespaces = new HybridDictionary ();
352 cached_types = new Hashtable ();
354 root.RegisterNamespace (this);
357 public override Expression DoResolve (EmitContext ec)
362 public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
364 if (name.IndexOf ('`') > 0) {
365 FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
366 if (retval != null) {
367 Error_TypeArgumentsCannotBeUsed (retval, loc);
371 Type t = LookForAnyGenericType (name);
373 Error_InvalidNumberOfTypeArguments (t, loc);
378 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
379 name, GetSignatureForError ());
382 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
384 Report.SymbolRelatedToPreviousError (t);
385 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
386 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
389 public static void Error_TypeArgumentsCannotBeUsed (FullNamedExpression expr, Location loc)
391 if (expr is TypeExpr) {
392 Report.SymbolRelatedToPreviousError (expr.Type);
393 Error_TypeArgumentsCannotBeUsed (loc, "type", expr.GetSignatureForError ());
395 expr.Error_ExpressionCannotBeGeneric (loc);
399 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
401 Report.SymbolRelatedToPreviousError (mi);
402 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
405 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
407 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
411 public override string GetSignatureForError ()
416 public Namespace GetNamespace (string name, bool create)
418 int pos = name.IndexOf ('.');
423 first = name.Substring (0, pos);
427 ns = (Namespace) namespaces [first];
432 ns = new Namespace (this, first);
433 namespaces.Add (first, ns);
437 ns = ns.GetNamespace (name.Substring (pos + 1), create);
442 public bool HasDefinition (string name)
444 return declspaces != null && declspaces [name] != null;
447 TypeExpr LookupType (string name, Location loc)
449 if (cached_types.Contains (name))
450 return cached_types [name] as TypeExpr;
453 if (declspaces != null) {
454 DeclSpace tdecl = declspaces [name] as DeclSpace;
457 // Note that this is not:
459 // t = tdecl.DefineType ()
461 // This is to make it somewhat more useful when a DefineType
462 // fails due to problems in nested types (more useful in the sense
463 // of fewer misleading error messages)
466 t = tdecl.TypeBuilder;
468 if (RootContext.EvalMode){
469 // Replace the TypeBuilder with a System.Type, as
470 // Reflection.Emit fails otherwise (we end up pretty
471 // much with Random type definitions later on).
472 Type tt = t.Assembly.GetType (t.Name);
478 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
479 Type rt = root.LookupTypeReflection (lookup, loc);
481 // HACK: loc.IsNull when the type is core type
482 if (t == null || (rt != null && loc.IsNull))
485 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
486 cached_types [name] = te;
491 /// Used for better error reporting only
493 public Type LookForAnyGenericType (string typeName)
495 if (declspaces == null)
498 typeName = SimpleName.RemoveGenericArity (typeName);
500 foreach (DictionaryEntry de in declspaces) {
501 string type_item = (string) de.Key;
502 int pos = type_item.LastIndexOf ('`');
503 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
504 return ((DeclSpace) de.Value).TypeBuilder;
509 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
511 if (namespaces.Contains (name))
512 return (Namespace) namespaces [name];
514 return LookupType (name, loc);
517 public void RegisterExternalExtensionMethodClass (Type type)
519 // Ignore, extension methods cannot be nested
523 if (type.IsNotPublic && !TypeManager.IsThisOrFriendAssembly (type.Assembly))
526 if (external_exmethod_classes == null)
527 external_exmethod_classes = new ArrayList ();
529 external_exmethod_classes.Add (type);
533 /// Looks for extension method in this namespace
535 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
537 ArrayList found = null;
539 if (declspaces != null) {
540 IEnumerator e = declspaces.Values.GetEnumerator ();
542 while (e.MoveNext ()) {
543 Class c = e.Current as Class;
547 if ((c.ModFlags & Modifiers.METHOD_EXTENSION) == 0)
550 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
557 found.AddRange (res);
561 if (external_exmethod_classes == null)
564 foreach (Type t in external_exmethod_classes) {
565 MemberCache m = TypeHandle.GetMemberCache (t);
566 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
573 found.AddRange (res);
579 public void AddDeclSpace (string name, DeclSpace ds)
581 if (declspaces == null)
582 declspaces = new HybridDictionary ();
583 declspaces.Add (name, ds);
586 public void RemoveDeclSpace (string name)
588 declspaces.Remove (name);
592 /// The qualified name of the current namespace
595 get { return fullname; }
599 /// The parent of this namespace, used by the parser to "Pop"
600 /// the current namespace declaration
602 public Namespace Parent {
603 get { return parent; }
608 // Namespace container as created by the parser
610 public class NamespaceEntry : IResolveContext {
613 readonly MemberName name;
616 public UsingEntry (MemberName name)
621 public string GetSignatureForError ()
623 return name.GetSignatureForError ();
626 public Location Location {
627 get { return name.Location; }
630 public MemberName MemberName {
635 get { return GetSignatureForError (); }
638 public Namespace Resolve (IResolveContext rc)
640 if (resolved != null)
643 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
647 resolved = fne as Namespace;
648 if (resolved == null) {
649 Report.SymbolRelatedToPreviousError (fne.Type);
650 Report.Error (138, Location,
651 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
652 GetSignatureForError ());
657 public override string ToString ()
663 class UsingAliasEntry {
664 public readonly string Alias;
665 public Location Location;
667 public UsingAliasEntry (string alias, Location loc)
673 public virtual FullNamedExpression Resolve (IResolveContext rc)
675 FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
677 Report.Error (430, Location,
678 "The extern alias `{0}' was not specified in -reference option",
685 public override string ToString ()
692 class LocalUsingAliasEntry : UsingAliasEntry {
693 FullNamedExpression resolved;
696 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
702 public override FullNamedExpression Resolve (IResolveContext rc)
704 if (resolved != null || value == null)
707 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
708 if (resolved == null) {
713 TypeExpr te = resolved as TypeExpr;
715 if (!te.CheckAccessLevel (rc.DeclContainer)) {
716 Report.SymbolRelatedToPreviousError (te.Type);
717 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
724 public override string ToString ()
726 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
731 NamespaceEntry parent, implicit_parent;
732 CompilationUnit file;
735 // Namespace using import block
736 ArrayList using_aliases;
737 ArrayList using_clauses;
738 public bool DeclarationFound = false;
741 public readonly bool IsImplicit;
742 public readonly DeclSpace SlaveDeclSpace;
743 static readonly Namespace [] empty_namespaces = new Namespace [0];
744 Namespace [] namespace_using_table;
746 static ArrayList entries = new ArrayList ();
748 public static void Reset ()
750 entries = new ArrayList ();
753 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
755 this.parent = parent;
760 ns = parent.NS.GetNamespace (name, true);
761 else if (name != null)
762 ns = RootNamespace.Global.GetNamespace (name, true);
764 ns = RootNamespace.Global;
765 SlaveDeclSpace = new RootDeclSpace (this);
768 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
770 this.parent = parent;
772 this.IsImplicit = true;
774 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
778 // Populates the Namespace with some using declarations, used by the
781 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
783 foreach (UsingAliasEntry uae in source_using_aliases){
784 if (using_aliases == null)
785 using_aliases = new ArrayList ();
787 using_aliases.Add (uae);
790 foreach (UsingEntry ue in source_using_clauses){
791 if (using_clauses == null)
792 using_clauses = new ArrayList ();
794 using_clauses.Add (ue);
799 // Extracts the using alises and using clauses into a couple of
800 // arrays that might already have the same information; Used by the
803 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
805 if (using_aliases != null){
806 foreach (UsingAliasEntry uae in using_aliases){
807 bool replaced = false;
809 for (int i = 0; i < out_using_aliases.Count; i++){
810 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
812 if (out_uea.Alias == uae.Alias){
813 out_using_aliases [i] = uae;
819 out_using_aliases.Add (uae);
823 if (using_clauses != null){
824 foreach (UsingEntry ue in using_clauses){
827 foreach (UsingEntry out_ue in out_using_clauses)
828 if (out_ue.Name == ue.Name){
833 out_using_clauses.Add (ue);
839 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
840 // resolved as if the immediately containing namespace body has no using-directives.
842 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
843 // in the using-namespace-directive.
845 // To implement these rules, the expressions in the using directives are resolved using
846 // the "doppelganger" (ghostly bodiless duplicate).
848 NamespaceEntry doppelganger;
849 NamespaceEntry Doppelganger {
851 if (!IsImplicit && doppelganger == null) {
852 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
853 doppelganger.using_aliases = using_aliases;
859 public Namespace NS {
863 public NamespaceEntry Parent {
864 get { return parent; }
867 public NamespaceEntry ImplicitParent {
871 if (implicit_parent == null) {
872 implicit_parent = (parent.NS == ns.Parent)
874 : new NamespaceEntry (parent, file, ns.Parent, false);
876 return implicit_parent;
881 /// Records a new namespace for resolving name references
883 public void AddUsing (MemberName name, Location loc)
885 if (DeclarationFound){
886 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
889 if (using_clauses == null) {
890 using_clauses = new ArrayList ();
892 foreach (UsingEntry old_entry in using_clauses) {
893 if (name.Equals (old_entry.MemberName)) {
894 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
895 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
901 using_clauses.Add (new UsingEntry (name));
904 public void AddUsingAlias (string alias, MemberName name, Location loc)
906 // TODO: This is parser bussines
907 if (DeclarationFound){
908 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
911 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
912 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
913 " the global namespace will be used instead");
915 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
918 public void AddUsingExternalAlias (string alias, Location loc)
920 // TODO: Do this in parser
921 bool not_first = using_clauses != null || DeclarationFound;
922 if (using_aliases != null && !not_first) {
923 foreach (UsingAliasEntry uae in using_aliases) {
924 if (uae is LocalUsingAliasEntry) {
932 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
934 if (alias == "global") {
935 Error_GlobalNamespaceRedefined (loc);
939 AddUsingAlias (new UsingAliasEntry (alias, loc));
942 void AddUsingAlias (UsingAliasEntry uae)
944 if (using_aliases == null) {
945 using_aliases = new ArrayList ();
947 foreach (UsingAliasEntry entry in using_aliases) {
948 if (uae.Alias == entry.Alias) {
949 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
950 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
957 using_aliases.Add (uae);
961 /// Does extension methods look up to find a method which matches name and extensionType.
962 /// Search starts from this namespace and continues hierarchically up to top level.
964 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
966 ArrayList candidates = null;
967 if (currentClass != null) {
968 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
969 if (candidates != null)
970 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
973 foreach (Namespace n in GetUsingTable ()) {
974 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
978 if (candidates == null)
981 candidates.AddRange (a);
984 if (candidates != null)
985 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
991 // Inspect parent namespaces in namespace expression
993 Namespace parent_ns = ns.Parent;
995 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
996 if (candidates != null)
997 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
999 parent_ns = parent_ns.Parent;
1000 } while (parent_ns != null);
1003 // Continue in parent scope
1005 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1008 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1010 // Precondition: Only simple names (no dots) will be looked up with this function.
1011 FullNamedExpression resolved = null;
1012 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1013 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1019 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1021 Report.SymbolRelatedToPreviousError (t1.Type);
1022 Report.SymbolRelatedToPreviousError (t2.Type);
1023 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1024 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1027 // Looks-up a alias named @name in this and surrounding namespace declarations
1028 public FullNamedExpression LookupAlias (string name)
1030 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1031 if (n.using_aliases == null)
1034 foreach (UsingAliasEntry ue in n.using_aliases) {
1035 if (ue.Alias == name)
1036 return ue.Resolve (Doppelganger);
1043 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1046 // Check whether it's in the namespace.
1048 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1053 if (using_aliases != null) {
1054 foreach (UsingAliasEntry ue in using_aliases) {
1055 if (ue.Alias == name) {
1057 if (Doppelganger != null) {
1058 // TODO: Namespace has broken location
1059 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1060 Report.SymbolRelatedToPreviousError (ue.Location, null);
1061 Report.Error (576, loc,
1062 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1063 GetSignatureForError (), name);
1069 return ue.Resolve (Doppelganger);
1081 // Check using entries.
1083 FullNamedExpression match = null;
1084 foreach (Namespace using_ns in GetUsingTable ()) {
1085 match = using_ns.Lookup (ds, name, loc);
1086 if (match == null || !(match is TypeExpr))
1090 Error_AmbiguousTypeReference (loc, name, fne, match);
1099 Namespace [] GetUsingTable ()
1101 if (namespace_using_table != null)
1102 return namespace_using_table;
1104 if (using_clauses == null) {
1105 namespace_using_table = empty_namespaces;
1106 return namespace_using_table;
1109 ArrayList list = new ArrayList (using_clauses.Count);
1111 foreach (UsingEntry ue in using_clauses) {
1112 Namespace using_ns = ue.Resolve (Doppelganger);
1113 if (using_ns == null)
1116 list.Add (using_ns);
1119 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1120 return namespace_using_table;
1123 static readonly string [] empty_using_list = new string [0];
1125 public int SymbolFileID {
1127 if (symfile_id == 0 && file.SourceFileEntry != null) {
1128 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1130 string [] using_list = empty_using_list;
1131 if (using_clauses != null) {
1132 using_list = new string [using_clauses.Count];
1133 for (int i = 0; i < using_clauses.Count; i++)
1134 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1137 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1143 static void MsgtryRef (string s)
1145 Console.WriteLine (" Try using -r:" + s);
1148 static void MsgtryPkg (string s)
1150 Console.WriteLine (" Try using -pkg:" + s);
1153 public static void Error_GlobalNamespaceRedefined (Location loc)
1155 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1158 public static void Error_NamespaceNotFound (Location loc, string name)
1160 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1164 case "Gtk": case "GtkSharp":
1165 MsgtryPkg ("gtk-sharp");
1168 case "Gdk": case "GdkSharp":
1169 MsgtryPkg ("gdk-sharp");
1172 case "Glade": case "GladeSharp":
1173 MsgtryPkg ("glade-sharp");
1176 case "System.Drawing":
1177 case "System.Web.Services":
1180 case "System.Windows.Forms":
1187 /// Used to validate that all the using clauses are correct
1188 /// after we are finished parsing all the files.
1192 if (using_aliases != null) {
1193 foreach (UsingAliasEntry ue in using_aliases)
1194 ue.Resolve (Doppelganger);
1197 if (using_clauses != null) {
1198 foreach (UsingEntry ue in using_clauses)
1199 ue.Resolve (Doppelganger);
1204 /// Used to validate that all the using clauses are correct
1205 /// after we are finished parsing all the files.
1207 static public void VerifyAllUsing ()
1209 foreach (NamespaceEntry entry in entries)
1210 entry.VerifyUsing ();
1213 public string GetSignatureForError ()
1215 return ns.GetSignatureForError ();
1218 public override string ToString ()
1220 return ns.ToString ();
1223 #region IResolveContext Members
1225 public DeclSpace DeclContainer {
1226 get { return SlaveDeclSpace; }
1229 public bool IsInObsoleteScope {
1230 get { return SlaveDeclSpace.IsInObsoleteScope; }
1233 public bool IsInUnsafeScope {
1234 get { return SlaveDeclSpace.IsInUnsafeScope; }
1237 public DeclSpace GenericDeclContainer {
1238 get { return SlaveDeclSpace; }