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, bool must_be_unique)
76 Type found_type = null;
78 foreach (Assembly a in referenced_assemblies) {
79 Type t = GetTypeInAssembly (a, name);
86 if (found_type == null) {
91 // When type is forwarded
92 if (t.Assembly == found_type.Assembly)
95 Report.SymbolRelatedToPreviousError (found_type);
96 Report.SymbolRelatedToPreviousError (t);
98 Error_AmbiguousPredefinedType (loc, name, found_type);
100 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
110 // Returns the types starting with the given prefix
112 public ICollection CompletionGetTypesStartingWith (string prefix)
114 Hashtable result = null;
116 foreach (Assembly a in referenced_assemblies){
117 Type [] mtypes = a.GetTypes ();
119 foreach (Type t in mtypes){
120 string f = t.FullName;
122 if (f.StartsWith (prefix) && (result == null || !result.Contains (f))){
124 result = new Hashtable ();
130 return result == null ? result : result.Keys;
133 protected void Error_AmbiguousPredefinedType (Location loc, string name, Type type)
135 Report.Warning (1685, 1, loc,
136 "The predefined type `{0}' is ambiguous. Using definition from `{1}'",
137 name, type.Assembly.FullName);
140 public void RegisterNamespace (Namespace child)
143 all_namespaces.Add (child.Name, child);
146 public bool IsNamespace (string name)
148 return all_namespaces.Contains (name);
151 protected void RegisterNamespace (string dotted_name)
153 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
154 GetNamespace (dotted_name, true);
157 void RegisterExtensionMethodClass (Type t)
159 string n = t.Namespace;
160 Namespace ns = n == null ? GlobalRootNamespace.Instance : (Namespace) all_namespaces[n];
162 ns = GetNamespace (n, true);
164 ns.RegisterExternalExtensionMethodClass (t);
167 void ComputeNamespaces (Assembly assembly, Type extensionType)
169 bool contains_extension_methods = extensionType != null && assembly.IsDefined (extensionType, 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 (extensionType, false))
183 RegisterExtensionMethodClass (t);
185 if (get_namespaces_method == null)
186 RegisterNamespace (t.Namespace);
190 protected static Type GetTypeInAssembly (Assembly assembly, string name)
192 if (assembly == null)
193 throw new ArgumentNullException ("assembly");
195 throw new ArgumentNullException ("name");
196 Type t = assembly.GetType (name);
201 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
203 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
204 if (ta == TypeAttributes.NestedPrivate)
207 if ((ta == TypeAttributes.NotPublic ||
208 ta == TypeAttributes.NestedAssembly ||
209 ta == TypeAttributes.NestedFamANDAssem) &&
210 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
216 public override string ToString ()
218 return String.Format ("RootNamespace ({0}::)", alias_name);
221 public override string GetSignatureForError ()
223 return alias_name + "::";
227 class GlobalRootNamespace : RootNamespace {
229 ListDictionary root_namespaces;
231 public static GlobalRootNamespace Instance = new GlobalRootNamespace ();
233 GlobalRootNamespace ()
236 root_namespaces = new ListDictionary ();
237 root_namespaces.Add (alias_name, this);
240 public static void Reset ()
242 Instance = new GlobalRootNamespace ();
245 public Assembly [] Assemblies {
246 get { return referenced_assemblies; }
249 public Module [] Modules {
250 get { return modules; }
253 public void AddModuleReference (Module m)
255 int top = modules != null ? modules.Length : 0;
256 Module [] n = new Module [top + 1];
258 modules.CopyTo (n, 0);
262 if (m == RootContext.ToplevelTypes.Builder)
265 foreach (Type t in m.GetTypes ())
266 RegisterNamespace (t.Namespace);
269 public void ComputeNamespaces ()
272 // Do very early lookup because type is required when we cache
273 // imported extension types in ComputeNamespaces
275 Type extension_attribute_type = TypeManager.CoreLookupType ("System.Runtime.CompilerServices", "ExtensionAttribute", Kind.Class, false);
277 foreach (RootNamespace rn in root_namespaces.Values) {
278 rn.ComputeNamespace (extension_attribute_type);
282 public void DefineRootNamespace (string alias, Assembly assembly)
284 if (alias == alias_name) {
285 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
289 RootNamespace retval = GetRootNamespace (alias);
290 if (retval == null) {
291 retval = new RootNamespace (alias);
292 root_namespaces.Add (alias, retval);
295 retval.AddAssemblyReference (assembly);
298 public override void Error_NamespaceDoesNotExist (Location loc, string name)
300 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
304 public RootNamespace GetRootNamespace (string name)
306 return (RootNamespace) root_namespaces[name];
309 public override Type LookupTypeReflection (string name, Location loc, bool must_be_unique)
311 Type found_type = base.LookupTypeReflection (name, loc, must_be_unique);
313 if (modules != null) {
314 foreach (Module module in modules) {
315 Type t = module.GetType (name);
319 if (found_type == null) {
324 Report.SymbolRelatedToPreviousError (found_type);
326 DeclSpace ds = TypeManager.LookupDeclSpace (t);
327 Error_AmbiguousPredefinedType (ds.Location, name, found_type);
330 Report.SymbolRelatedToPreviousError (t);
331 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
332 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
342 /// Keeps track of the namespaces defined in the C# code.
344 /// This is an Expression to allow it to be referenced in the
345 /// compiler parse/intermediate tree during name resolution.
347 public class Namespace : FullNamedExpression {
351 IDictionary namespaces;
352 IDictionary declspaces;
353 Hashtable cached_types;
355 ArrayList external_exmethod_classes;
357 public readonly MemberName MemberName;
360 /// Constructor Takes the current namespace and the
361 /// name. This is bootstrapped with parent == null
364 public Namespace (Namespace parent, string name)
366 // Expression members.
367 this.eclass = ExprClass.Namespace;
368 this.Type = typeof (Namespace);
369 this.loc = Location.Null;
371 this.parent = parent;
374 this.root = parent.root;
376 this.root = this as RootNamespace;
378 if (this.root == null)
379 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
381 string pname = parent != null ? parent.fullname : "";
386 fullname = parent.fullname + "." + name;
388 if (fullname == null)
389 throw new InternalErrorException ("Namespace has a null fullname");
391 if (parent != null && parent.MemberName != MemberName.Null)
392 MemberName = new MemberName (parent.MemberName, name);
393 else if (name.Length == 0)
394 MemberName = MemberName.Null;
396 MemberName = new MemberName (name);
398 namespaces = new HybridDictionary ();
399 cached_types = new Hashtable ();
401 root.RegisterNamespace (this);
404 public override Expression DoResolve (ResolveContext ec)
409 public virtual void Error_NamespaceDoesNotExist (Location loc, string name)
411 if (name.IndexOf ('`') > 0) {
412 FullNamedExpression retval = Lookup (SimpleName.RemoveGenericArity (name), loc);
413 if (retval != null) {
414 Error_TypeArgumentsCannotBeUsed (retval, loc);
418 Type t = LookForAnyGenericType (name);
420 Error_InvalidNumberOfTypeArguments (t, loc);
425 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
426 name, GetSignatureForError ());
429 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
431 Report.SymbolRelatedToPreviousError (t);
432 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
433 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
436 public static void Error_TypeArgumentsCannotBeUsed (FullNamedExpression expr, Location loc)
438 if (expr is TypeExpr) {
439 Report.SymbolRelatedToPreviousError (expr.Type);
440 Error_TypeArgumentsCannotBeUsed (loc, "type", expr.GetSignatureForError ());
442 expr.Error_ExpressionCannotBeGeneric (loc);
446 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
448 Report.SymbolRelatedToPreviousError (mi);
449 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
452 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
454 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
458 public override string GetSignatureForError ()
463 public Namespace GetNamespace (string name, bool create)
465 int pos = name.IndexOf ('.');
470 first = name.Substring (0, pos);
474 ns = (Namespace) namespaces [first];
479 ns = new Namespace (this, first);
480 namespaces.Add (first, ns);
484 ns = ns.GetNamespace (name.Substring (pos + 1), create);
489 public bool HasDefinition (string name)
491 return declspaces != null && declspaces [name] != null;
494 TypeExpr LookupType (string name, Location loc)
496 if (cached_types.Contains (name))
497 return cached_types [name] as TypeExpr;
500 if (declspaces != null) {
501 DeclSpace tdecl = declspaces [name] as DeclSpace;
504 // Note that this is not:
506 // t = tdecl.DefineType ()
508 // This is to make it somewhat more useful when a DefineType
509 // fails due to problems in nested types (more useful in the sense
510 // of fewer misleading error messages)
513 t = tdecl.TypeBuilder;
515 if (RootContext.EvalMode){
516 // Replace the TypeBuilder with a System.Type, as
517 // Reflection.Emit fails otherwise (we end up pretty
518 // much with Random type definitions later on).
519 Type tt = t.Assembly.GetType (t.Name);
525 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
526 Type rt = root.LookupTypeReflection (lookup, loc, t == null);
528 // HACK: loc.IsNull when the type is core type
529 if (t == null || (rt != null && loc.IsNull))
532 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
533 cached_types [name] = te;
538 /// Used for better error reporting only
540 public Type LookForAnyGenericType (string typeName)
542 if (declspaces == null)
545 typeName = SimpleName.RemoveGenericArity (typeName);
547 foreach (DictionaryEntry de in declspaces) {
548 string type_item = (string) de.Key;
549 int pos = type_item.LastIndexOf ('`');
550 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
551 return ((DeclSpace) de.Value).TypeBuilder;
556 public FullNamedExpression Lookup (string name, Location loc)
558 if (namespaces.Contains (name))
559 return (Namespace) namespaces [name];
561 return LookupType (name, loc);
565 // Completes types with the given `prefix' and stores the results in `result'
567 public void CompletionGetTypesStartingWith (string prefix, Hashtable result)
569 int l = fullname.Length + 1;
570 ICollection res = root.CompletionGetTypesStartingWith (fullname + "." + prefix);
575 foreach (string match in res){
576 string x = match.Substring (l);
578 // Turn reflection nested classes foo+bar into foo.bar
579 x = x.Replace ('+', '.');
581 // Only get the first name element, no point in adding anything beyond the first dot.
582 int p = x.IndexOf ('.');
584 x = x.Substring (0, p);
586 // Turn Foo`N into Foo<
589 x = x.Substring (0, p) + "<";
591 if (!result.Contains (x))
596 public void RegisterExternalExtensionMethodClass (Type type)
598 // Ignore, extension methods cannot be nested
599 if (type.DeclaringType != null)
602 if (type.IsNotPublic && !TypeManager.IsThisOrFriendAssembly (type.Assembly))
605 if (external_exmethod_classes == null)
606 external_exmethod_classes = new ArrayList ();
608 external_exmethod_classes.Add (type);
612 /// Looks for extension method in this namespace
614 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
616 ArrayList found = null;
618 if (declspaces != null) {
619 IEnumerator e = declspaces.Values.GetEnumerator ();
621 while (e.MoveNext ()) {
622 Class c = e.Current as Class;
626 if ((c.ModFlags & Modifiers.METHOD_EXTENSION) == 0)
629 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
636 found.AddRange (res);
640 if (external_exmethod_classes == null)
643 foreach (Type t in external_exmethod_classes) {
644 MemberCache m = TypeHandle.GetMemberCache (t);
645 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
652 found.AddRange (res);
658 public void AddDeclSpace (string name, DeclSpace ds)
660 if (declspaces == null)
661 declspaces = new HybridDictionary ();
662 declspaces.Add (name, ds);
665 public void RemoveDeclSpace (string name)
667 declspaces.Remove (name);
671 /// The qualified name of the current namespace
674 get { return fullname; }
678 /// The parent of this namespace, used by the parser to "Pop"
679 /// the current namespace declaration
681 public Namespace Parent {
682 get { return parent; }
687 // Namespace container as created by the parser
689 public class NamespaceEntry : IMemberContext {
692 readonly MemberName name;
695 public UsingEntry (MemberName name)
700 public string GetSignatureForError ()
702 return name.GetSignatureForError ();
705 public Location Location {
706 get { return name.Location; }
709 public MemberName MemberName {
714 get { return GetSignatureForError (); }
717 public Namespace Resolve (IMemberContext rc)
719 if (resolved != null)
722 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
726 resolved = fne as Namespace;
727 if (resolved == null) {
728 Report.SymbolRelatedToPreviousError (fne.Type);
729 Report.Error (138, Location,
730 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
731 GetSignatureForError ());
736 public override string ToString ()
742 class UsingAliasEntry {
743 public readonly string Alias;
744 public Location Location;
746 public UsingAliasEntry (string alias, Location loc)
752 public virtual FullNamedExpression Resolve (IMemberContext rc)
754 FullNamedExpression fne = GlobalRootNamespace.Instance.GetRootNamespace (Alias);
756 Report.Error (430, Location,
757 "The extern alias `{0}' was not specified in -reference option",
764 public override string ToString ()
771 class LocalUsingAliasEntry : UsingAliasEntry {
772 FullNamedExpression resolved;
775 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
781 public override FullNamedExpression Resolve (IMemberContext rc)
783 if (resolved != null || value == null)
786 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
787 if (resolved == null) {
792 if (resolved is TypeExpr)
793 resolved = resolved.ResolveAsBaseTerminal (rc, false);
798 public override string ToString ()
800 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
805 NamespaceEntry parent, implicit_parent;
806 CompilationUnit file;
809 // Namespace using import block
810 ArrayList using_aliases;
811 ArrayList using_clauses;
812 public bool DeclarationFound = false;
815 public readonly bool IsImplicit;
816 public readonly DeclSpace SlaveDeclSpace;
817 static readonly Namespace [] empty_namespaces = new Namespace [0];
818 Namespace [] namespace_using_table;
820 static ArrayList entries = new ArrayList ();
822 public static void Reset ()
824 entries = new ArrayList ();
827 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
829 this.parent = parent;
834 ns = parent.NS.GetNamespace (name, true);
835 else if (name != null)
836 ns = GlobalRootNamespace.Instance.GetNamespace (name, true);
838 ns = GlobalRootNamespace.Instance;
839 SlaveDeclSpace = new RootDeclSpace (this);
842 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
844 this.parent = parent;
846 this.IsImplicit = true;
848 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
852 // Populates the Namespace with some using declarations, used by the
855 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
857 foreach (UsingAliasEntry uae in source_using_aliases){
858 if (using_aliases == null)
859 using_aliases = new ArrayList ();
861 using_aliases.Add (uae);
864 foreach (UsingEntry ue in source_using_clauses){
865 if (using_clauses == null)
866 using_clauses = new ArrayList ();
868 using_clauses.Add (ue);
873 // Extracts the using alises and using clauses into a couple of
874 // arrays that might already have the same information; Used by the
877 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
879 if (using_aliases != null){
880 foreach (UsingAliasEntry uae in using_aliases){
881 bool replaced = false;
883 for (int i = 0; i < out_using_aliases.Count; i++){
884 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
886 if (out_uea.Alias == uae.Alias){
887 out_using_aliases [i] = uae;
893 out_using_aliases.Add (uae);
897 if (using_clauses != null){
898 foreach (UsingEntry ue in using_clauses){
901 foreach (UsingEntry out_ue in out_using_clauses)
902 if (out_ue.Name == ue.Name){
907 out_using_clauses.Add (ue);
913 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
914 // resolved as if the immediately containing namespace body has no using-directives.
916 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
917 // in the using-namespace-directive.
919 // To implement these rules, the expressions in the using directives are resolved using
920 // the "doppelganger" (ghostly bodiless duplicate).
922 NamespaceEntry doppelganger;
923 NamespaceEntry Doppelganger {
925 if (!IsImplicit && doppelganger == null) {
926 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
927 doppelganger.using_aliases = using_aliases;
933 public Namespace NS {
937 public NamespaceEntry Parent {
938 get { return parent; }
941 public NamespaceEntry ImplicitParent {
945 if (implicit_parent == null) {
946 implicit_parent = (parent.NS == ns.Parent)
948 : new NamespaceEntry (parent, file, ns.Parent, false);
950 return implicit_parent;
955 /// Records a new namespace for resolving name references
957 public void AddUsing (MemberName name, Location loc)
959 if (DeclarationFound){
960 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
963 if (using_clauses == null) {
964 using_clauses = new ArrayList ();
966 foreach (UsingEntry old_entry in using_clauses) {
967 if (name.Equals (old_entry.MemberName)) {
968 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
969 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
975 using_clauses.Add (new UsingEntry (name));
978 public void AddUsingAlias (string alias, MemberName name, Location loc)
980 // TODO: This is parser bussines
981 if (DeclarationFound){
982 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
985 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
986 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
987 " the global namespace will be used instead");
989 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
992 public void AddUsingExternalAlias (string alias, Location loc)
994 // TODO: Do this in parser
995 bool not_first = using_clauses != null || DeclarationFound;
996 if (using_aliases != null && !not_first) {
997 foreach (UsingAliasEntry uae in using_aliases) {
998 if (uae is LocalUsingAliasEntry) {
1006 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
1008 if (alias == "global") {
1009 Error_GlobalNamespaceRedefined (loc);
1013 AddUsingAlias (new UsingAliasEntry (alias, loc));
1016 void AddUsingAlias (UsingAliasEntry uae)
1018 if (using_aliases == null) {
1019 using_aliases = new ArrayList ();
1021 foreach (UsingAliasEntry entry in using_aliases) {
1022 if (uae.Alias == entry.Alias) {
1023 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
1024 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
1031 using_aliases.Add (uae);
1035 /// Does extension methods look up to find a method which matches name and extensionType.
1036 /// Search starts from this namespace and continues hierarchically up to top level.
1038 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
1040 ArrayList candidates = null;
1041 foreach (Namespace n in GetUsingTable ()) {
1042 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
1046 if (candidates == null)
1049 candidates.AddRange (a);
1052 if (candidates != null)
1053 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1059 // Inspect parent namespaces in namespace expression
1061 Namespace parent_ns = ns.Parent;
1063 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
1064 if (candidates != null)
1065 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1067 parent_ns = parent_ns.Parent;
1068 } while (parent_ns != null);
1071 // Continue in parent scope
1073 return parent.LookupExtensionMethod (extensionType, name, loc);
1076 public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
1078 // Precondition: Only simple names (no dots) will be looked up with this function.
1079 FullNamedExpression resolved = null;
1080 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1081 if ((resolved = curr_ns.Lookup (name, loc, ignore_cs0104)) != null)
1087 public ICollection CompletionGetTypesStartingWith (string prefix)
1089 Hashtable result = new Hashtable ();
1091 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent){
1092 foreach (Namespace using_ns in GetUsingTable ()){
1093 if (prefix.StartsWith (using_ns.Name)){
1094 int ld = prefix.LastIndexOf ('.');
1096 string rest = prefix.Substring (ld+1);
1098 using_ns.CompletionGetTypesStartingWith (rest, result);
1101 using_ns.CompletionGetTypesStartingWith (prefix, result);
1108 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1110 Report.SymbolRelatedToPreviousError (t1.Type);
1111 Report.SymbolRelatedToPreviousError (t2.Type);
1112 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1113 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1116 // Looks-up a alias named @name in this and surrounding namespace declarations
1117 public FullNamedExpression LookupNamespaceAlias (string name)
1119 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1120 if (n.using_aliases == null)
1123 foreach (UsingAliasEntry ue in n.using_aliases) {
1124 if (ue.Alias == name)
1125 return ue.Resolve (Doppelganger);
1132 private FullNamedExpression Lookup (string name, Location loc, bool ignore_cs0104)
1135 // Check whether it's in the namespace.
1137 FullNamedExpression fne = ns.Lookup (name, loc);
1142 if (using_aliases != null) {
1143 foreach (UsingAliasEntry ue in using_aliases) {
1144 if (ue.Alias == name) {
1146 if (Doppelganger != null) {
1147 // TODO: Namespace has broken location
1148 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1149 Report.SymbolRelatedToPreviousError (ue.Location, null);
1150 Report.Error (576, loc,
1151 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1152 GetSignatureForError (), name);
1158 return ue.Resolve (Doppelganger);
1170 // Check using entries.
1172 FullNamedExpression match = null;
1173 foreach (Namespace using_ns in GetUsingTable ()) {
1174 match = using_ns.Lookup (name, loc);
1175 if (match == null || !(match is TypeExpr))
1179 Error_AmbiguousTypeReference (loc, name, fne, match);
1188 Namespace [] GetUsingTable ()
1190 if (namespace_using_table != null)
1191 return namespace_using_table;
1193 if (using_clauses == null) {
1194 namespace_using_table = empty_namespaces;
1195 return namespace_using_table;
1198 ArrayList list = new ArrayList (using_clauses.Count);
1200 foreach (UsingEntry ue in using_clauses) {
1201 Namespace using_ns = ue.Resolve (Doppelganger);
1202 if (using_ns == null)
1205 list.Add (using_ns);
1208 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1209 return namespace_using_table;
1212 static readonly string [] empty_using_list = new string [0];
1214 public int SymbolFileID {
1216 if (symfile_id == 0 && file.SourceFileEntry != null) {
1217 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1219 string [] using_list = empty_using_list;
1220 if (using_clauses != null) {
1221 using_list = new string [using_clauses.Count];
1222 for (int i = 0; i < using_clauses.Count; i++)
1223 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1226 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1232 static void MsgtryRef (string s)
1234 Console.WriteLine (" Try using -r:" + s);
1237 static void MsgtryPkg (string s)
1239 Console.WriteLine (" Try using -pkg:" + s);
1242 public static void Error_GlobalNamespaceRedefined (Location loc)
1244 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1247 public static void Error_NamespaceNotFound (Location loc, string name)
1249 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1253 case "Gtk": case "GtkSharp":
1254 MsgtryPkg ("gtk-sharp");
1257 case "Gdk": case "GdkSharp":
1258 MsgtryPkg ("gdk-sharp");
1261 case "Glade": case "GladeSharp":
1262 MsgtryPkg ("glade-sharp");
1265 case "System.Drawing":
1266 case "System.Web.Services":
1269 case "System.Windows.Forms":
1276 /// Used to validate that all the using clauses are correct
1277 /// after we are finished parsing all the files.
1281 if (using_aliases != null) {
1282 foreach (UsingAliasEntry ue in using_aliases)
1283 ue.Resolve (Doppelganger);
1286 if (using_clauses != null) {
1287 foreach (UsingEntry ue in using_clauses)
1288 ue.Resolve (Doppelganger);
1293 /// Used to validate that all the using clauses are correct
1294 /// after we are finished parsing all the files.
1296 static public void VerifyAllUsing ()
1298 foreach (NamespaceEntry entry in entries)
1299 entry.VerifyUsing ();
1302 public string GetSignatureForError ()
1304 return ns.GetSignatureForError ();
1307 public override string ToString ()
1309 return ns.ToString ();
1312 #region IMemberContext Members
1314 public Type CurrentType {
1315 get { return SlaveDeclSpace.CurrentType; }
1318 public TypeContainer CurrentTypeDefinition {
1319 get { return SlaveDeclSpace.CurrentTypeDefinition; }
1322 public TypeParameter[] CurrentTypeParameters {
1323 get { return SlaveDeclSpace.CurrentTypeParameters; }
1326 public bool IsObsolete {
1327 get { return SlaveDeclSpace.IsObsolete; }
1330 public bool IsUnsafe {
1331 get { return SlaveDeclSpace.IsUnsafe; }
1334 public bool IsStatic {
1335 get { return SlaveDeclSpace.IsStatic; }