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 Assembly referenced_assembly;
29 Hashtable all_namespaces;
31 static Hashtable 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 Hashtable ();
44 Global = new GlobalRootNamespace ();
45 root_namespaces ["global"] = Global;
48 protected RootNamespace (string alias_name, Assembly assembly)
49 : base (null, String.Empty)
51 this.alias_name = alias_name;
52 referenced_assembly = assembly;
54 all_namespaces = new Hashtable ();
55 all_namespaces.Add ("", this);
57 if (referenced_assembly != null)
58 ComputeNamespaces (this.referenced_assembly);
61 public static void DefineRootNamespace (string name, Assembly assembly)
63 if (name == "global") {
64 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
67 RootNamespace retval = GetRootNamespace (name);
68 if (retval == null || retval.referenced_assembly != assembly)
69 root_namespaces [name] = new RootNamespace (name, assembly);
72 public static RootNamespace GetRootNamespace (string name)
74 return (RootNamespace) root_namespaces [name];
77 public virtual Type LookupTypeReflection (string name, Location loc)
79 return GetTypeInAssembly (referenced_assembly, name);
82 public void RegisterNamespace (Namespace child)
85 all_namespaces.Add (child.Name, child);
88 public bool IsNamespace (string name)
90 return all_namespaces.Contains (name);
93 protected void RegisterNamespace (string dotted_name)
95 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
96 GetNamespace (dotted_name, true);
99 void RegisterExtensionMethodClass (Type t)
101 string n = t.Namespace;
102 Namespace ns = n == null ? Global : (Namespace)all_namespaces [n];
104 ns = GetNamespace (n, true);
106 ns.RegisterExternalExtensionMethodClass (t);
109 protected void ComputeNamespaces (Assembly assembly)
111 // How to test whether attribute exists without loading the assembly :-(
113 const string SystemCore = "System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
115 const string SystemCore = "System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
117 if (TypeManager.extension_attribute_type == null &&
118 assembly.FullName == SystemCore) {
119 TypeManager.extension_attribute_type = assembly.GetType("System.Runtime.CompilerServices.ExtensionAttribute");
122 bool contains_extension_methods = TypeManager.extension_attribute_type != null &&
123 assembly.IsDefined(TypeManager.extension_attribute_type, false);
125 if (get_namespaces_method != null && !contains_extension_methods) {
126 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
127 foreach (string ns in namespaces)
128 RegisterNamespace (ns);
132 foreach (Type t in assembly.GetExportedTypes ()) {
133 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
134 contains_extension_methods && t.IsDefined (TypeManager.extension_attribute_type, false))
135 RegisterExtensionMethodClass (t);
137 RegisterNamespace (t.Namespace);
141 protected static Type GetTypeInAssembly (Assembly assembly, string name)
143 Type t = assembly.GetType (name);
148 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
150 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
151 if (ta == TypeAttributes.NestedPrivate)
154 if ((ta == TypeAttributes.NotPublic ||
155 ta == TypeAttributes.NestedAssembly ||
156 ta == TypeAttributes.NestedFamANDAssem) &&
157 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
163 public override string ToString ()
165 return String.Format ("RootNamespace ({0}::)", alias_name);
168 public override string GetSignatureForError ()
170 return alias_name + "::";
174 public class GlobalRootNamespace : RootNamespace {
175 Assembly [] assemblies;
178 public GlobalRootNamespace ()
179 : base ("global", null)
181 assemblies = new Assembly [0];
184 public Assembly [] Assemblies {
185 get { return assemblies; }
188 public Module [] Modules {
189 get { return modules; }
192 public void AddAssemblyReference (Assembly a)
194 foreach (Assembly assembly in assemblies) {
199 int top = assemblies.Length;
200 Assembly [] n = new Assembly [top + 1];
201 assemblies.CopyTo (n, 0);
205 ComputeNamespaces (a);
208 public void AddModuleReference (Module m)
210 int top = modules != null ? modules.Length : 0;
211 Module [] n = new Module [top + 1];
213 modules.CopyTo (n, 0);
217 if (m == CodeGen.Module.Builder)
220 foreach (Type t in m.GetTypes ())
221 RegisterNamespace (t.Namespace);
224 public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
226 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
230 public override Type LookupTypeReflection (string name, Location loc)
232 Type found_type = null;
234 foreach (Assembly a in assemblies) {
235 Type t = GetTypeInAssembly (a, name);
239 if (found_type == null) {
244 Report.SymbolRelatedToPreviousError (found_type);
245 Report.SymbolRelatedToPreviousError (t);
246 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
251 if (modules != null) {
252 foreach (Module module in modules) {
253 Type t = module.GetType (name);
257 if (found_type == null) {
262 Report.SymbolRelatedToPreviousError (found_type);
264 DeclSpace ds = TypeManager.LookupDeclSpace (t);
265 Report.Warning (1685, 1, ds.Location, "The type `{0}' conflicts with the predefined type `{1}' and will be ignored",
266 ds.GetSignatureForError (), TypeManager.CSharpName (found_type));
269 Report.SymbolRelatedToPreviousError (t);
270 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
271 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
281 /// Keeps track of the namespaces defined in the C# code.
283 /// This is an Expression to allow it to be referenced in the
284 /// compiler parse/intermediate tree during name resolution.
286 public class Namespace : FullNamedExpression {
290 IDictionary namespaces;
291 IDictionary declspaces;
292 Hashtable cached_types;
294 ArrayList external_exmethod_classes;
296 public readonly MemberName MemberName;
299 /// Constructor Takes the current namespace and the
300 /// name. This is bootstrapped with parent == null
303 public Namespace (Namespace parent, string name)
305 // Expression members.
306 this.eclass = ExprClass.Namespace;
307 this.Type = typeof (Namespace);
308 this.loc = Location.Null;
310 this.parent = parent;
313 this.root = parent.root;
315 this.root = this as RootNamespace;
317 if (this.root == null)
318 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
320 string pname = parent != null ? parent.fullname : "";
325 fullname = parent.fullname + "." + name;
327 if (fullname == null)
328 throw new InternalErrorException ("Namespace has a null fullname");
330 if (parent != null && parent.MemberName != MemberName.Null)
331 MemberName = new MemberName (parent.MemberName, name);
332 else if (name.Length == 0)
333 MemberName = MemberName.Null;
335 MemberName = new MemberName (name);
337 namespaces = new HybridDictionary ();
338 cached_types = new Hashtable ();
340 root.RegisterNamespace (this);
343 public override Expression DoResolve (EmitContext ec)
348 public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
350 if (name.IndexOf ('`') > 0) {
351 FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
352 if (retval != null) {
353 Error_TypeArgumentsCannotBeUsed (retval.Type, loc);
357 Type t = LookForAnyGenericType (name);
359 Error_InvalidNumberOfTypeArguments (t, loc);
364 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
365 name, GetSignatureForError ());
368 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
370 Report.SymbolRelatedToPreviousError (t);
371 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
372 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
375 public static void Error_TypeArgumentsCannotBeUsed (Type t, Location loc)
377 Report.SymbolRelatedToPreviousError (t);
378 Error_TypeArgumentsCannotBeUsed (loc, "type", TypeManager.CSharpName (t));
381 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
383 Report.SymbolRelatedToPreviousError (mi);
384 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
387 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
389 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
393 public override string GetSignatureForError ()
398 public Namespace GetNamespace (string name, bool create)
400 int pos = name.IndexOf ('.');
405 first = name.Substring (0, pos);
409 ns = (Namespace) namespaces [first];
414 ns = new Namespace (this, first);
415 namespaces.Add (first, ns);
419 ns = ns.GetNamespace (name.Substring (pos + 1), create);
424 TypeExpr LookupType (string name, Location loc)
426 if (cached_types.Contains (name))
427 return cached_types [name] as TypeExpr;
430 if (declspaces != null) {
431 DeclSpace tdecl = declspaces [name] as DeclSpace;
434 // Note that this is not:
436 // t = tdecl.DefineType ()
438 // This is to make it somewhat more useful when a DefineType
439 // fails due to problems in nested types (more useful in the sense
440 // of fewer misleading error messages)
443 t = tdecl.TypeBuilder;
445 if (RootContext.EvalMode){
446 // Replace the TypeBuilder with a System.Type, as
447 // Reflection.Emit fails otherwise (we end up pretty
448 // much with Random type definitions later on).
449 Type tt = t.Assembly.GetType (t.Name);
455 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
456 Type rt = root.LookupTypeReflection (lookup, loc);
458 // HACK: loc.IsNull when the type is core type
459 if (t == null || (rt != null && loc.IsNull))
462 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
463 cached_types [name] = te;
468 /// Used for better error reporting only
470 public Type LookForAnyGenericType (string typeName)
472 if (declspaces == null)
475 typeName = SimpleName.RemoveGenericArity (typeName);
477 foreach (DictionaryEntry de in declspaces) {
478 string type_item = (string) de.Key;
479 int pos = type_item.LastIndexOf ('`');
480 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
481 return ((DeclSpace) de.Value).TypeBuilder;
486 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
488 if (namespaces.Contains (name))
489 return (Namespace) namespaces [name];
491 return LookupType (name, loc);
494 public void RegisterExternalExtensionMethodClass (Type type)
496 if (external_exmethod_classes == null)
497 external_exmethod_classes = new ArrayList ();
499 external_exmethod_classes.Add (type);
503 /// Looks for extension method in this namespace
505 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
507 ArrayList found = null;
509 if (declspaces != null) {
510 IEnumerator e = declspaces.Values.GetEnumerator ();
512 while (e.MoveNext ()) {
513 Class c = e.Current as Class;
517 if (!c.IsStaticClass)
520 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
527 found.AddRange (res);
531 if (external_exmethod_classes == null)
534 foreach (Type t in external_exmethod_classes) {
535 MemberCache m = TypeHandle.GetMemberCache (t);
536 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
543 found.AddRange (res);
549 public void AddDeclSpace (string name, DeclSpace ds)
551 if (declspaces == null)
552 declspaces = new HybridDictionary ();
553 declspaces.Add (name, ds);
556 public void RemoveDeclSpace (string name)
558 declspaces.Remove (name);
562 /// The qualified name of the current namespace
565 get { return fullname; }
569 /// The parent of this namespace, used by the parser to "Pop"
570 /// the current namespace declaration
572 public Namespace Parent {
573 get { return parent; }
578 // Namespace container as created by the parser
580 public class NamespaceEntry : IResolveContext {
583 readonly MemberName name;
586 public UsingEntry (MemberName name)
591 public string GetSignatureForError ()
593 return name.GetSignatureForError ();
596 public Location Location {
597 get { return name.Location; }
600 public MemberName MemberName {
605 get { return GetSignatureForError (); }
608 public Namespace Resolve (IResolveContext rc)
610 if (resolved != null)
613 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
617 resolved = fne as Namespace;
618 if (resolved == null) {
619 Report.SymbolRelatedToPreviousError (fne.Type);
620 Report.Error (138, Location,
621 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
622 GetSignatureForError ());
627 public override string ToString ()
633 class UsingAliasEntry {
634 public readonly string Alias;
635 public Location Location;
637 public UsingAliasEntry (string alias, Location loc)
643 public virtual FullNamedExpression Resolve (IResolveContext rc)
645 FullNamedExpression fne = RootNamespace.GetRootNamespace (Alias);
647 Report.Error (430, Location,
648 "The extern alias `{0}' was not specified in -reference option",
655 public override string ToString ()
662 class LocalUsingAliasEntry : UsingAliasEntry {
666 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
672 public override FullNamedExpression Resolve (IResolveContext rc)
674 if (resolved != null)
675 return (FullNamedExpression)resolved;
677 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
678 if (resolved == null)
681 // FIXME: This is quite wrong, the accessibility is not global
682 if (resolved.Type != null) {
683 TypeAttributes attr = resolved.Type.Attributes & TypeAttributes.VisibilityMask;
684 if (attr == TypeAttributes.NestedPrivate || attr == TypeAttributes.NestedFamily ||
685 ((attr == TypeAttributes.NestedFamORAssem || attr == TypeAttributes.NestedAssembly) &&
686 TypeManager.LookupDeclSpace (resolved.Type) == null)) {
687 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
692 return (FullNamedExpression)resolved;
695 public override string ToString ()
697 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
702 NamespaceEntry parent, implicit_parent;
703 CompilationUnit file;
706 // Namespace using import block
707 ArrayList using_aliases;
708 ArrayList using_clauses;
709 public bool DeclarationFound = false;
712 public readonly bool IsImplicit;
713 public readonly DeclSpace SlaveDeclSpace;
714 static readonly Namespace [] empty_namespaces = new Namespace [0];
715 Namespace [] namespace_using_table;
717 static ArrayList entries = new ArrayList ();
719 public static void Reset ()
721 entries = new ArrayList ();
724 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
726 this.parent = parent;
731 ns = parent.NS.GetNamespace (name, true);
732 else if (name != null)
733 ns = RootNamespace.Global.GetNamespace (name, true);
735 ns = RootNamespace.Global;
736 SlaveDeclSpace = new RootDeclSpace (this);
739 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
741 this.parent = parent;
743 this.IsImplicit = true;
745 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
749 // Populates the Namespace with some using declarations, used by the
752 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
754 foreach (UsingAliasEntry uae in source_using_aliases){
755 if (using_aliases == null)
756 using_aliases = new ArrayList ();
758 using_aliases.Add (uae);
761 foreach (UsingEntry ue in source_using_clauses){
762 if (using_clauses == null)
763 using_clauses = new ArrayList ();
765 using_clauses.Add (ue);
770 // Extracts the using alises and using clauses into a couple of
771 // arrays that might already have the same information; Used by the
774 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
776 if (using_aliases != null){
777 foreach (UsingAliasEntry uae in using_aliases){
778 bool replaced = false;
780 for (int i = 0; i < out_using_aliases.Count; i++){
781 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
783 if (out_uea.Alias == uae.Alias){
784 out_using_aliases [i] = uae;
790 out_using_aliases.Add (uae);
794 if (using_clauses != null){
795 foreach (UsingEntry ue in using_clauses){
798 foreach (UsingEntry out_ue in out_using_clauses)
799 if (out_ue.Name == ue.Name){
804 out_using_clauses.Add (ue);
810 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
811 // resolved as if the immediately containing namespace body has no using-directives.
813 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
814 // in the using-namespace-directive.
816 // To implement these rules, the expressions in the using directives are resolved using
817 // the "doppelganger" (ghostly bodiless duplicate).
819 NamespaceEntry doppelganger;
820 NamespaceEntry Doppelganger {
822 if (!IsImplicit && doppelganger == null) {
823 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
824 doppelganger.using_aliases = using_aliases;
830 public Namespace NS {
834 public NamespaceEntry Parent {
835 get { return parent; }
838 public NamespaceEntry ImplicitParent {
842 if (implicit_parent == null) {
843 implicit_parent = (parent.NS == ns.Parent)
845 : new NamespaceEntry (parent, file, ns.Parent, false);
847 return implicit_parent;
852 /// Records a new namespace for resolving name references
854 public void AddUsing (MemberName name, Location loc)
856 if (DeclarationFound){
857 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
860 if (using_clauses == null) {
861 using_clauses = new ArrayList ();
863 foreach (UsingEntry old_entry in using_clauses) {
864 if (name.Equals (old_entry.MemberName)) {
865 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
866 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
872 using_clauses.Add (new UsingEntry (name));
875 public void AddUsingAlias (string alias, MemberName name, Location loc)
877 // TODO: This is parser bussines
878 if (DeclarationFound){
879 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
882 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
883 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
884 " the global namespace will be used instead");
886 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
889 public void AddUsingExternalAlias (string alias, Location loc)
891 // TODO: Do this in parser
892 bool not_first = using_clauses != null || DeclarationFound;
893 if (using_aliases != null && !not_first) {
894 foreach (UsingAliasEntry uae in using_aliases) {
895 if (uae is LocalUsingAliasEntry) {
903 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
905 if (alias == "global") {
906 Error_GlobalNamespaceRedefined (loc);
910 AddUsingAlias (new UsingAliasEntry (alias, loc));
913 void AddUsingAlias (UsingAliasEntry uae)
915 if (using_aliases == null) {
916 using_aliases = new ArrayList ();
918 foreach (UsingAliasEntry entry in using_aliases) {
919 if (uae.Alias == entry.Alias) {
920 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
921 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
928 using_aliases.Add (uae);
932 /// Does extension methods look up to find a method which matches name and extensionType.
933 /// Search starts from this namespace and continues hierarchically up to top level.
935 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
937 ArrayList candidates = null;
938 if (currentClass != null) {
939 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
940 if (candidates != null)
941 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
944 foreach (Namespace n in GetUsingTable ()) {
945 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
949 if (candidates == null)
952 candidates.AddRange (a);
955 if (candidates != null)
956 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
962 // Inspect parent namespaces in namespace expression
964 Namespace parent_ns = ns.Parent;
966 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
967 if (candidates != null)
968 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
970 parent_ns = parent_ns.Parent;
971 } while (parent_ns != null);
974 // Continue in parent scope
976 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
979 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
981 // Precondition: Only simple names (no dots) will be looked up with this function.
982 FullNamedExpression resolved = null;
983 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
984 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
990 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
992 Report.SymbolRelatedToPreviousError (t1.Type);
993 Report.SymbolRelatedToPreviousError (t2.Type);
994 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
995 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
998 // Looks-up a alias named @name in this and surrounding namespace declarations
999 public FullNamedExpression LookupAlias (string name)
1001 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1002 if (n.using_aliases == null)
1005 foreach (UsingAliasEntry ue in n.using_aliases) {
1006 if (ue.Alias == name)
1007 return ue.Resolve (Doppelganger);
1014 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1017 // Check whether it's in the namespace.
1019 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1024 if (using_aliases != null) {
1025 foreach (UsingAliasEntry ue in using_aliases) {
1026 if (ue.Alias == name) {
1028 if (Doppelganger != null) {
1029 // TODO: Namespace has broken location
1030 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1031 Report.SymbolRelatedToPreviousError (ue.Location, null);
1032 Report.Error (576, loc,
1033 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1034 GetSignatureForError (), name);
1040 return ue.Resolve (Doppelganger);
1052 // Check using entries.
1054 FullNamedExpression match = null;
1055 foreach (Namespace using_ns in GetUsingTable ()) {
1056 match = using_ns.Lookup (ds, name, loc);
1057 if (match == null || !(match is TypeExpr))
1061 Error_AmbiguousTypeReference (loc, name, fne, match);
1070 Namespace [] GetUsingTable ()
1072 if (namespace_using_table != null)
1073 return namespace_using_table;
1075 if (using_clauses == null) {
1076 namespace_using_table = empty_namespaces;
1077 return namespace_using_table;
1080 ArrayList list = new ArrayList (using_clauses.Count);
1082 foreach (UsingEntry ue in using_clauses) {
1083 Namespace using_ns = ue.Resolve (Doppelganger);
1084 if (using_ns == null)
1087 list.Add (using_ns);
1090 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1091 return namespace_using_table;
1094 static readonly string [] empty_using_list = new string [0];
1096 public int SymbolFileID {
1098 if (symfile_id == 0 && file.SourceFileEntry != null) {
1099 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1101 string [] using_list = empty_using_list;
1102 if (using_clauses != null) {
1103 using_list = new string [using_clauses.Count];
1104 for (int i = 0; i < using_clauses.Count; i++)
1105 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetTypeName ();
1108 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1114 static void MsgtryRef (string s)
1116 Console.WriteLine (" Try using -r:" + s);
1119 static void MsgtryPkg (string s)
1121 Console.WriteLine (" Try using -pkg:" + s);
1124 public static void Error_GlobalNamespaceRedefined (Location loc)
1126 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1129 public static void Error_NamespaceNotFound (Location loc, string name)
1131 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1135 case "Gtk": case "GtkSharp":
1136 MsgtryPkg ("gtk-sharp");
1139 case "Gdk": case "GdkSharp":
1140 MsgtryPkg ("gdk-sharp");
1143 case "Glade": case "GladeSharp":
1144 MsgtryPkg ("glade-sharp");
1147 case "System.Drawing":
1148 case "System.Web.Services":
1151 case "System.Windows.Forms":
1158 /// Used to validate that all the using clauses are correct
1159 /// after we are finished parsing all the files.
1163 if (using_aliases != null) {
1164 foreach (UsingAliasEntry ue in using_aliases)
1165 ue.Resolve (Doppelganger);
1168 if (using_clauses != null) {
1169 foreach (UsingEntry ue in using_clauses)
1170 ue.Resolve (Doppelganger);
1175 /// Used to validate that all the using clauses are correct
1176 /// after we are finished parsing all the files.
1178 static public void VerifyAllUsing ()
1180 foreach (NamespaceEntry entry in entries)
1181 entry.VerifyUsing ();
1184 public string GetSignatureForError ()
1186 return ns.GetSignatureForError ();
1189 public override string ToString ()
1191 return ns.ToString ();
1194 #region IResolveContext Members
1196 public DeclSpace DeclContainer {
1197 get { return SlaveDeclSpace; }
1200 public bool IsInObsoleteScope {
1201 get { return SlaveDeclSpace.IsInObsoleteScope; }
1204 public bool IsInUnsafeScope {
1205 get { return SlaveDeclSpace.IsInUnsafeScope; }
1208 public DeclSpace GenericDeclContainer {
1209 get { return SlaveDeclSpace; }