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);
91 Error_AmbiguousPredefinedType (loc, name, found_type);
93 Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
103 // Returns the types starting with the given prefix
105 public ICollection CompletionGetTypesStartingWith (string prefix)
107 Hashtable result = null;
109 foreach (Assembly a in referenced_assemblies){
110 Type [] mtypes = a.GetTypes ();
112 foreach (Type t in mtypes){
113 string f = t.FullName;
115 if (f.StartsWith (prefix) && (result == null || !result.Contains (f))){
117 result = new Hashtable ();
123 return result == null ? result : result.Keys;
126 protected void Error_AmbiguousPredefinedType (Location loc, string name, Type type)
128 Report.Warning (1685, 1, loc,
129 "The predefined type `{0}' is ambiguous. Using definition from `{1}'",
130 name, type.Assembly.FullName);
133 public void RegisterNamespace (Namespace child)
136 all_namespaces.Add (child.Name, child);
139 public bool IsNamespace (string name)
141 return all_namespaces.Contains (name);
144 protected void RegisterNamespace (string dotted_name)
146 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
147 GetNamespace (dotted_name, true);
150 void RegisterExtensionMethodClass (Type t)
152 string n = t.Namespace;
153 Namespace ns = n == null ? GlobalRootNamespace.Instance : (Namespace) all_namespaces[n];
155 ns = GetNamespace (n, true);
157 ns.RegisterExternalExtensionMethodClass (t);
160 void ComputeNamespaces (Assembly assembly, Type extensionType)
162 bool contains_extension_methods = extensionType != null && assembly.IsDefined (extensionType, false);
164 if (get_namespaces_method != null) {
165 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
166 foreach (string ns in namespaces)
167 RegisterNamespace (ns);
169 if (!contains_extension_methods)
173 foreach (Type t in assembly.GetTypes ()) {
174 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
175 contains_extension_methods && t.IsDefined (extensionType, false))
176 RegisterExtensionMethodClass (t);
178 if (get_namespaces_method == null)
179 RegisterNamespace (t.Namespace);
183 protected static Type GetTypeInAssembly (Assembly assembly, string name)
185 if (assembly == null)
186 throw new ArgumentNullException ("assembly");
188 throw new ArgumentNullException ("name");
189 Type t = assembly.GetType (name);
194 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
196 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
197 if (ta == TypeAttributes.NestedPrivate)
200 if ((ta == TypeAttributes.NotPublic ||
201 ta == TypeAttributes.NestedAssembly ||
202 ta == TypeAttributes.NestedFamANDAssem) &&
203 !TypeManager.IsThisOrFriendAssembly (t.Assembly))
209 public override string ToString ()
211 return String.Format ("RootNamespace ({0}::)", alias_name);
214 public override string GetSignatureForError ()
216 return alias_name + "::";
220 class GlobalRootNamespace : RootNamespace {
222 ListDictionary root_namespaces;
224 public static GlobalRootNamespace Instance = new GlobalRootNamespace ();
226 GlobalRootNamespace ()
229 root_namespaces = new ListDictionary ();
230 root_namespaces.Add (alias_name, this);
233 public static void Reset ()
235 Instance = new GlobalRootNamespace ();
238 public Assembly [] Assemblies {
239 get { return referenced_assemblies; }
242 public Module [] Modules {
243 get { return modules; }
246 public void AddModuleReference (Module m)
248 int top = modules != null ? modules.Length : 0;
249 Module [] n = new Module [top + 1];
251 modules.CopyTo (n, 0);
255 if (m == RootContext.ToplevelTypes.Builder)
258 foreach (Type t in m.GetTypes ())
259 RegisterNamespace (t.Namespace);
262 public void ComputeNamespaces ()
265 // Do very early lookup because type is required when we cache
266 // imported extension types in ComputeNamespaces
268 Type extension_attribute_type = TypeManager.CoreLookupType ("System.Runtime.CompilerServices", "ExtensionAttribute", Kind.Class, false);
270 foreach (RootNamespace rn in root_namespaces.Values) {
271 rn.ComputeNamespace (extension_attribute_type);
275 public void DefineRootNamespace (string alias, Assembly assembly)
277 if (alias == alias_name) {
278 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null);
282 RootNamespace retval = GetRootNamespace (alias);
283 if (retval == null) {
284 retval = new RootNamespace (alias);
285 root_namespaces.Add (alias, retval);
288 retval.AddAssemblyReference (assembly);
291 public override void Error_NamespaceDoesNotExist(DeclSpace ds, Location loc, string name)
293 Report.Error (400, loc, "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
297 public RootNamespace GetRootNamespace (string name)
299 return (RootNamespace) root_namespaces[name];
302 public override Type LookupTypeReflection (string name, Location loc)
304 Type found_type = base.LookupTypeReflection (name, loc);
306 if (modules != null) {
307 foreach (Module module in modules) {
308 Type t = module.GetType (name);
312 if (found_type == null) {
317 Report.SymbolRelatedToPreviousError (found_type);
319 DeclSpace ds = TypeManager.LookupDeclSpace (t);
320 Error_AmbiguousPredefinedType (ds.Location, name, found_type);
323 Report.SymbolRelatedToPreviousError (t);
324 Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
325 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
335 /// Keeps track of the namespaces defined in the C# code.
337 /// This is an Expression to allow it to be referenced in the
338 /// compiler parse/intermediate tree during name resolution.
340 public class Namespace : FullNamedExpression {
344 IDictionary namespaces;
345 IDictionary declspaces;
346 Hashtable cached_types;
348 ArrayList external_exmethod_classes;
350 public readonly MemberName MemberName;
353 /// Constructor Takes the current namespace and the
354 /// name. This is bootstrapped with parent == null
357 public Namespace (Namespace parent, string name)
359 // Expression members.
360 this.eclass = ExprClass.Namespace;
361 this.Type = typeof (Namespace);
362 this.loc = Location.Null;
364 this.parent = parent;
367 this.root = parent.root;
369 this.root = this as RootNamespace;
371 if (this.root == null)
372 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
374 string pname = parent != null ? parent.fullname : "";
379 fullname = parent.fullname + "." + name;
381 if (fullname == null)
382 throw new InternalErrorException ("Namespace has a null fullname");
384 if (parent != null && parent.MemberName != MemberName.Null)
385 MemberName = new MemberName (parent.MemberName, name);
386 else if (name.Length == 0)
387 MemberName = MemberName.Null;
389 MemberName = new MemberName (name);
391 namespaces = new HybridDictionary ();
392 cached_types = new Hashtable ();
394 root.RegisterNamespace (this);
397 public override Expression DoResolve (EmitContext ec)
402 public virtual void Error_NamespaceDoesNotExist (DeclSpace ds, Location loc, string name)
404 if (name.IndexOf ('`') > 0) {
405 FullNamedExpression retval = Lookup (ds, SimpleName.RemoveGenericArity (name), loc);
406 if (retval != null) {
407 Error_TypeArgumentsCannotBeUsed (retval, loc);
411 Type t = LookForAnyGenericType (name);
413 Error_InvalidNumberOfTypeArguments (t, loc);
418 Report.Error (234, loc, "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
419 name, GetSignatureForError ());
422 public static void Error_InvalidNumberOfTypeArguments (Type t, Location loc)
424 Report.SymbolRelatedToPreviousError (t);
425 Report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
426 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
429 public static void Error_TypeArgumentsCannotBeUsed (FullNamedExpression expr, Location loc)
431 if (expr is TypeExpr) {
432 Report.SymbolRelatedToPreviousError (expr.Type);
433 Error_TypeArgumentsCannotBeUsed (loc, "type", expr.GetSignatureForError ());
435 expr.Error_ExpressionCannotBeGeneric (loc);
439 public static void Error_TypeArgumentsCannotBeUsed (MethodBase mi, Location loc)
441 Report.SymbolRelatedToPreviousError (mi);
442 Error_TypeArgumentsCannotBeUsed (loc, "method", TypeManager.CSharpSignature (mi));
445 static void Error_TypeArgumentsCannotBeUsed (Location loc, string type, string name)
447 Report.Error(308, loc, "The non-generic {0} `{1}' cannot be used with the type arguments",
451 public override string GetSignatureForError ()
456 public Namespace GetNamespace (string name, bool create)
458 int pos = name.IndexOf ('.');
463 first = name.Substring (0, pos);
467 ns = (Namespace) namespaces [first];
472 ns = new Namespace (this, first);
473 namespaces.Add (first, ns);
477 ns = ns.GetNamespace (name.Substring (pos + 1), create);
482 public bool HasDefinition (string name)
484 return declspaces != null && declspaces [name] != null;
487 TypeExpr LookupType (string name, Location loc)
489 if (cached_types.Contains (name))
490 return cached_types [name] as TypeExpr;
493 if (declspaces != null) {
494 DeclSpace tdecl = declspaces [name] as DeclSpace;
497 // Note that this is not:
499 // t = tdecl.DefineType ()
501 // This is to make it somewhat more useful when a DefineType
502 // fails due to problems in nested types (more useful in the sense
503 // of fewer misleading error messages)
506 t = tdecl.TypeBuilder;
508 if (RootContext.EvalMode){
509 // Replace the TypeBuilder with a System.Type, as
510 // Reflection.Emit fails otherwise (we end up pretty
511 // much with Random type definitions later on).
512 Type tt = t.Assembly.GetType (t.Name);
518 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
519 Type rt = root.LookupTypeReflection (lookup, loc);
521 // HACK: loc.IsNull when the type is core type
522 if (t == null || (rt != null && loc.IsNull))
525 TypeExpr te = t == null ? null : new TypeExpression (t, Location.Null);
526 cached_types [name] = te;
531 /// Used for better error reporting only
533 public Type LookForAnyGenericType (string typeName)
535 if (declspaces == null)
538 typeName = SimpleName.RemoveGenericArity (typeName);
540 foreach (DictionaryEntry de in declspaces) {
541 string type_item = (string) de.Key;
542 int pos = type_item.LastIndexOf ('`');
543 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
544 return ((DeclSpace) de.Value).TypeBuilder;
549 public FullNamedExpression Lookup (DeclSpace ds, string name, Location loc)
551 if (namespaces.Contains (name))
552 return (Namespace) namespaces [name];
554 return LookupType (name, loc);
558 // Completes types with the given `prefix' and stores the results in `result'
560 public void CompletionGetTypesStartingWith (DeclSpace ds, string prefix, Hashtable result)
562 int l = fullname.Length + 1;
563 ICollection res = root.CompletionGetTypesStartingWith (fullname + "." + prefix);
568 foreach (string match in res){
569 string x = match.Substring (l);
571 // Turn reflection nested classes foo+bar into foo.bar
572 x = x.Replace ('+', '.');
574 // Only get the first name element, no point in adding anything beyond the first dot.
575 int p = x.IndexOf ('.');
577 x = x.Substring (0, p);
579 // Turn Foo`N into Foo<
582 x = x.Substring (0, p) + "<";
584 if (!result.Contains (x))
589 public void RegisterExternalExtensionMethodClass (Type type)
591 // Ignore, extension methods cannot be nested
592 if (type.DeclaringType != null)
595 if (type.IsNotPublic && !TypeManager.IsThisOrFriendAssembly (type.Assembly))
598 if (external_exmethod_classes == null)
599 external_exmethod_classes = new ArrayList ();
601 external_exmethod_classes.Add (type);
605 /// Looks for extension method in this namespace
607 public ArrayList LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
609 ArrayList found = null;
611 if (declspaces != null) {
612 IEnumerator e = declspaces.Values.GetEnumerator ();
614 while (e.MoveNext ()) {
615 Class c = e.Current as Class;
619 if ((c.ModFlags & Modifiers.METHOD_EXTENSION) == 0)
622 ArrayList res = c.MemberCache.FindExtensionMethods (extensionType, name, c != currentClass);
629 found.AddRange (res);
633 if (external_exmethod_classes == null)
636 foreach (Type t in external_exmethod_classes) {
637 MemberCache m = TypeHandle.GetMemberCache (t);
638 ArrayList res = m.FindExtensionMethods (extensionType, name, true);
645 found.AddRange (res);
651 public void AddDeclSpace (string name, DeclSpace ds)
653 if (declspaces == null)
654 declspaces = new HybridDictionary ();
655 declspaces.Add (name, ds);
658 public void RemoveDeclSpace (string name)
660 declspaces.Remove (name);
664 /// The qualified name of the current namespace
667 get { return fullname; }
671 /// The parent of this namespace, used by the parser to "Pop"
672 /// the current namespace declaration
674 public Namespace Parent {
675 get { return parent; }
680 // Namespace container as created by the parser
682 public class NamespaceEntry : IResolveContext {
685 readonly MemberName name;
688 public UsingEntry (MemberName name)
693 public string GetSignatureForError ()
695 return name.GetSignatureForError ();
698 public Location Location {
699 get { return name.Location; }
702 public MemberName MemberName {
707 get { return GetSignatureForError (); }
710 public Namespace Resolve (IResolveContext rc)
712 if (resolved != null)
715 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
719 resolved = fne as Namespace;
720 if (resolved == null) {
721 Report.SymbolRelatedToPreviousError (fne.Type);
722 Report.Error (138, Location,
723 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
724 GetSignatureForError ());
729 public override string ToString ()
735 class UsingAliasEntry {
736 public readonly string Alias;
737 public Location Location;
739 public UsingAliasEntry (string alias, Location loc)
745 public virtual FullNamedExpression Resolve (IResolveContext rc)
747 FullNamedExpression fne = GlobalRootNamespace.Instance.GetRootNamespace (Alias);
749 Report.Error (430, Location,
750 "The extern alias `{0}' was not specified in -reference option",
757 public override string ToString ()
764 class LocalUsingAliasEntry : UsingAliasEntry {
765 FullNamedExpression resolved;
768 public LocalUsingAliasEntry (string alias, MemberName name, Location loc)
774 public override FullNamedExpression Resolve (IResolveContext rc)
776 if (resolved != null || value == null)
779 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
780 if (resolved == null) {
785 TypeExpr te = resolved as TypeExpr;
787 if (!te.CheckAccessLevel (rc.DeclContainer)) {
788 Report.SymbolRelatedToPreviousError (te.Type);
789 Expression.ErrorIsInaccesible (resolved.Location, resolved.GetSignatureForError ());
796 public override string ToString ()
798 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
803 NamespaceEntry parent, implicit_parent;
804 CompilationUnit file;
807 // Namespace using import block
808 ArrayList using_aliases;
809 ArrayList using_clauses;
810 public bool DeclarationFound = false;
813 public readonly bool IsImplicit;
814 public readonly DeclSpace SlaveDeclSpace;
815 static readonly Namespace [] empty_namespaces = new Namespace [0];
816 Namespace [] namespace_using_table;
818 static ArrayList entries = new ArrayList ();
820 public static void Reset ()
822 entries = new ArrayList ();
825 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
827 this.parent = parent;
832 ns = parent.NS.GetNamespace (name, true);
833 else if (name != null)
834 ns = GlobalRootNamespace.Instance.GetNamespace (name, true);
836 ns = GlobalRootNamespace.Instance;
837 SlaveDeclSpace = new RootDeclSpace (this);
840 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
842 this.parent = parent;
844 this.IsImplicit = true;
846 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
850 // Populates the Namespace with some using declarations, used by the
853 public void Populate (ArrayList source_using_aliases, ArrayList source_using_clauses)
855 foreach (UsingAliasEntry uae in source_using_aliases){
856 if (using_aliases == null)
857 using_aliases = new ArrayList ();
859 using_aliases.Add (uae);
862 foreach (UsingEntry ue in source_using_clauses){
863 if (using_clauses == null)
864 using_clauses = new ArrayList ();
866 using_clauses.Add (ue);
871 // Extracts the using alises and using clauses into a couple of
872 // arrays that might already have the same information; Used by the
875 public void Extract (ArrayList out_using_aliases, ArrayList out_using_clauses)
877 if (using_aliases != null){
878 foreach (UsingAliasEntry uae in using_aliases){
879 bool replaced = false;
881 for (int i = 0; i < out_using_aliases.Count; i++){
882 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
884 if (out_uea.Alias == uae.Alias){
885 out_using_aliases [i] = uae;
891 out_using_aliases.Add (uae);
895 if (using_clauses != null){
896 foreach (UsingEntry ue in using_clauses){
899 foreach (UsingEntry out_ue in out_using_clauses)
900 if (out_ue.Name == ue.Name){
905 out_using_clauses.Add (ue);
911 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
912 // resolved as if the immediately containing namespace body has no using-directives.
914 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
915 // in the using-namespace-directive.
917 // To implement these rules, the expressions in the using directives are resolved using
918 // the "doppelganger" (ghostly bodiless duplicate).
920 NamespaceEntry doppelganger;
921 NamespaceEntry Doppelganger {
923 if (!IsImplicit && doppelganger == null) {
924 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
925 doppelganger.using_aliases = using_aliases;
931 public Namespace NS {
935 public NamespaceEntry Parent {
936 get { return parent; }
939 public NamespaceEntry ImplicitParent {
943 if (implicit_parent == null) {
944 implicit_parent = (parent.NS == ns.Parent)
946 : new NamespaceEntry (parent, file, ns.Parent, false);
948 return implicit_parent;
953 /// Records a new namespace for resolving name references
955 public void AddUsing (MemberName name, Location loc)
957 if (DeclarationFound){
958 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
961 if (using_clauses == null) {
962 using_clauses = new ArrayList ();
964 foreach (UsingEntry old_entry in using_clauses) {
965 if (name.Equals (old_entry.MemberName)) {
966 Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
967 Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
973 using_clauses.Add (new UsingEntry (name));
976 public void AddUsingAlias (string alias, MemberName name, Location loc)
978 // TODO: This is parser bussines
979 if (DeclarationFound){
980 Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
983 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
984 Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
985 " the global namespace will be used instead");
987 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
990 public void AddUsingExternalAlias (string alias, Location loc)
992 // TODO: Do this in parser
993 bool not_first = using_clauses != null || DeclarationFound;
994 if (using_aliases != null && !not_first) {
995 foreach (UsingAliasEntry uae in using_aliases) {
996 if (uae is LocalUsingAliasEntry) {
1004 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
1006 if (alias == "global") {
1007 Error_GlobalNamespaceRedefined (loc);
1011 AddUsingAlias (new UsingAliasEntry (alias, loc));
1014 void AddUsingAlias (UsingAliasEntry uae)
1016 if (using_aliases == null) {
1017 using_aliases = new ArrayList ();
1019 foreach (UsingAliasEntry entry in using_aliases) {
1020 if (uae.Alias == entry.Alias) {
1021 Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
1022 Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
1029 using_aliases.Add (uae);
1033 /// Does extension methods look up to find a method which matches name and extensionType.
1034 /// Search starts from this namespace and continues hierarchically up to top level.
1036 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name, Location loc)
1038 ArrayList candidates = null;
1039 if (currentClass != null) {
1040 candidates = ns.LookupExtensionMethod (extensionType, currentClass, name);
1041 if (candidates != null)
1042 return new ExtensionMethodGroupExpr (candidates, this, extensionType, loc);
1045 foreach (Namespace n in GetUsingTable ()) {
1046 ArrayList a = n.LookupExtensionMethod (extensionType, null, name);
1050 if (candidates == null)
1053 candidates.AddRange (a);
1056 if (candidates != null)
1057 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1063 // Inspect parent namespaces in namespace expression
1065 Namespace parent_ns = ns.Parent;
1067 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
1068 if (candidates != null)
1069 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1071 parent_ns = parent_ns.Parent;
1072 } while (parent_ns != null);
1075 // Continue in parent scope
1077 return parent.LookupExtensionMethod (extensionType, currentClass, name, loc);
1080 public FullNamedExpression LookupNamespaceOrType (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1082 // Precondition: Only simple names (no dots) will be looked up with this function.
1083 FullNamedExpression resolved = null;
1084 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1085 if ((resolved = curr_ns.Lookup (ds, name, loc, ignore_cs0104)) != null)
1091 public ICollection CompletionGetTypesStartingWith (DeclSpace ds, string prefix)
1093 Hashtable result = new Hashtable ();
1095 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent){
1096 foreach (Namespace using_ns in GetUsingTable ()){
1097 using_ns.CompletionGetTypesStartingWith (ds, prefix, result);
1104 static void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1106 Report.SymbolRelatedToPreviousError (t1.Type);
1107 Report.SymbolRelatedToPreviousError (t2.Type);
1108 Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1109 name, t1.GetSignatureForError (), t2.GetSignatureForError ());
1112 // Looks-up a alias named @name in this and surrounding namespace declarations
1113 public FullNamedExpression LookupAlias (string name)
1115 for (NamespaceEntry n = this; n != null; n = n.ImplicitParent) {
1116 if (n.using_aliases == null)
1119 foreach (UsingAliasEntry ue in n.using_aliases) {
1120 if (ue.Alias == name)
1121 return ue.Resolve (Doppelganger);
1128 private FullNamedExpression Lookup (DeclSpace ds, string name, Location loc, bool ignore_cs0104)
1131 // Check whether it's in the namespace.
1133 FullNamedExpression fne = ns.Lookup (ds, name, loc);
1138 if (using_aliases != null) {
1139 foreach (UsingAliasEntry ue in using_aliases) {
1140 if (ue.Alias == name) {
1142 if (Doppelganger != null) {
1143 // TODO: Namespace has broken location
1144 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1145 Report.SymbolRelatedToPreviousError (ue.Location, null);
1146 Report.Error (576, loc,
1147 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1148 GetSignatureForError (), name);
1154 return ue.Resolve (Doppelganger);
1166 // Check using entries.
1168 FullNamedExpression match = null;
1169 foreach (Namespace using_ns in GetUsingTable ()) {
1170 match = using_ns.Lookup (ds, name, loc);
1171 if (match == null || !(match is TypeExpr))
1175 Error_AmbiguousTypeReference (loc, name, fne, match);
1184 Namespace [] GetUsingTable ()
1186 if (namespace_using_table != null)
1187 return namespace_using_table;
1189 if (using_clauses == null) {
1190 namespace_using_table = empty_namespaces;
1191 return namespace_using_table;
1194 ArrayList list = new ArrayList (using_clauses.Count);
1196 foreach (UsingEntry ue in using_clauses) {
1197 Namespace using_ns = ue.Resolve (Doppelganger);
1198 if (using_ns == null)
1201 list.Add (using_ns);
1204 namespace_using_table = (Namespace[])list.ToArray (typeof (Namespace));
1205 return namespace_using_table;
1208 static readonly string [] empty_using_list = new string [0];
1210 public int SymbolFileID {
1212 if (symfile_id == 0 && file.SourceFileEntry != null) {
1213 int parent_id = parent == null ? 0 : parent.SymbolFileID;
1215 string [] using_list = empty_using_list;
1216 if (using_clauses != null) {
1217 using_list = new string [using_clauses.Count];
1218 for (int i = 0; i < using_clauses.Count; i++)
1219 using_list [i] = ((UsingEntry) using_clauses [i]).MemberName.GetName ();
1222 symfile_id = SymbolWriter.DefineNamespace (ns.Name, file.CompileUnitEntry, using_list, parent_id);
1228 static void MsgtryRef (string s)
1230 Console.WriteLine (" Try using -r:" + s);
1233 static void MsgtryPkg (string s)
1235 Console.WriteLine (" Try using -pkg:" + s);
1238 public static void Error_GlobalNamespaceRedefined (Location loc)
1240 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1243 public static void Error_NamespaceNotFound (Location loc, string name)
1245 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1249 case "Gtk": case "GtkSharp":
1250 MsgtryPkg ("gtk-sharp");
1253 case "Gdk": case "GdkSharp":
1254 MsgtryPkg ("gdk-sharp");
1257 case "Glade": case "GladeSharp":
1258 MsgtryPkg ("glade-sharp");
1261 case "System.Drawing":
1262 case "System.Web.Services":
1265 case "System.Windows.Forms":
1272 /// Used to validate that all the using clauses are correct
1273 /// after we are finished parsing all the files.
1277 if (using_aliases != null) {
1278 foreach (UsingAliasEntry ue in using_aliases)
1279 ue.Resolve (Doppelganger);
1282 if (using_clauses != null) {
1283 foreach (UsingEntry ue in using_clauses)
1284 ue.Resolve (Doppelganger);
1289 /// Used to validate that all the using clauses are correct
1290 /// after we are finished parsing all the files.
1292 static public void VerifyAllUsing ()
1294 foreach (NamespaceEntry entry in entries)
1295 entry.VerifyUsing ();
1298 public string GetSignatureForError ()
1300 return ns.GetSignatureForError ();
1303 public override string ToString ()
1305 return ns.ToString ();
1308 #region IResolveContext Members
1310 public DeclSpace DeclContainer {
1311 get { return SlaveDeclSpace; }
1314 public bool IsInObsoleteScope {
1315 get { return SlaveDeclSpace.IsInObsoleteScope; }
1318 public bool IsInUnsafeScope {
1319 get { return SlaveDeclSpace.IsInUnsafeScope; }
1322 public DeclSpace GenericDeclContainer {
1323 get { return SlaveDeclSpace; }