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.Generic;
13 using System.Reflection;
15 namespace Mono.CSharp {
17 public class RootNamespace : Namespace {
19 // Points to Mono's GetNamespaces method, an
20 // optimization when running on Mono to fetch all the
21 // namespaces in an assembly
23 static MethodInfo get_namespaces_method;
25 protected readonly string alias_name;
26 protected Assembly [] referenced_assemblies;
28 Dictionary<string, Namespace> all_namespaces;
30 static RootNamespace ()
32 get_namespaces_method = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance | BindingFlags.NonPublic);
35 public RootNamespace (string alias_name)
36 : base (null, String.Empty)
38 this.alias_name = alias_name;
39 referenced_assemblies = new Assembly [0];
41 all_namespaces = new Dictionary<string, Namespace> ();
42 all_namespaces.Add ("", this);
45 public void AddAssemblyReference (Assembly a)
47 foreach (Assembly assembly in referenced_assemblies) {
52 int top = referenced_assemblies.Length;
53 Assembly [] n = new Assembly [top + 1];
54 referenced_assemblies.CopyTo (n, 0);
56 referenced_assemblies = n;
59 public void ComputeNamespace (CompilerContext ctx, Type extensionType)
61 foreach (Assembly a in referenced_assemblies) {
63 ComputeNamespaces (a, extensionType);
64 } catch (TypeLoadException e) {
65 ctx.Report.Error (11, Location.Null, e.Message);
66 } catch (System.IO.FileNotFoundException) {
67 ctx.Report.Error (12, Location.Null, "An assembly `{0}' is used without being referenced",
73 public virtual Type LookupTypeReflection (CompilerContext ctx, string name, Location loc, bool must_be_unique)
75 // FIXME: Breaks dynamic
76 Assembly invocation_assembly = CodeGen.Assembly.Builder;
78 Type found_type = null;
79 foreach (Assembly a in referenced_assemblies) {
80 Type t = GetTypeInAssembly (invocation_assembly, a, name);
87 if (found_type == null) {
92 // When type is forwarded
93 if (t.Assembly == found_type.Assembly)
96 ctx.Report.SymbolRelatedToPreviousError (found_type);
97 ctx.Report.SymbolRelatedToPreviousError (t);
99 Error_AmbiguousPredefinedType (ctx, loc, name, found_type);
101 ctx.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", name);
111 // Returns the types starting with the given prefix
113 public ICollection<string> CompletionGetTypesStartingWith (string prefix)
115 Dictionary<string, string> result = null;
117 foreach (Assembly a in referenced_assemblies){
118 Type [] mtypes = a.GetTypes ();
120 foreach (Type t in mtypes){
121 string f = t.FullName;
123 if (f.StartsWith (prefix) && (result == null || !result.ContainsKey (f))){
125 result = new Dictionary<string, string> ();
131 return result == null ? null : result.Keys;
134 protected static void Error_AmbiguousPredefinedType (CompilerContext ctx, Location loc, string name, Type type)
136 ctx.Report.Warning (1685, 1, loc,
137 "The predefined type `{0}' is ambiguous. Using definition from `{1}'",
138 name, type.Assembly.FullName);
141 public void RegisterNamespace (Namespace child)
144 all_namespaces.Add (child.Name, child);
147 public bool IsNamespace (string name)
149 return all_namespaces.ContainsKey (name);
152 protected void RegisterNamespace (string dotted_name)
154 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
155 GetNamespace (dotted_name, true);
158 void RegisterExtensionMethodClass (Type t)
160 string n = t.Namespace;
163 ns = GlobalRootNamespace.Instance;
165 all_namespaces.TryGetValue (n, out ns);
168 ns = GetNamespace (n, true);
170 ns.RegisterExternalExtensionMethodClass (t);
173 void ComputeNamespaces (Assembly assembly, Type extensionType)
175 bool contains_extension_methods = extensionType != null && assembly.IsDefined (extensionType, false);
177 if (get_namespaces_method != null) {
178 string [] namespaces = (string []) get_namespaces_method.Invoke (assembly, null);
179 foreach (string ns in namespaces)
180 RegisterNamespace (ns);
182 if (!contains_extension_methods)
186 foreach (Type t in assembly.GetTypes ()) {
187 if ((t.Attributes & Class.StaticClassAttribute) == Class.StaticClassAttribute &&
188 contains_extension_methods && t.IsDefined (extensionType, false))
189 RegisterExtensionMethodClass (t);
191 if (get_namespaces_method == null)
192 RegisterNamespace (t.Namespace);
196 protected static Type GetTypeInAssembly (Assembly invocation, Assembly assembly, string name)
198 if (assembly == null)
199 throw new ArgumentNullException ("assembly");
201 throw new ArgumentNullException ("name");
202 Type t = assembly.GetType (name);
207 throw new InternalErrorException ("Use GetPointerType() to get a pointer");
209 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
210 if (ta == TypeAttributes.NestedPrivate)
213 if ((ta == TypeAttributes.NotPublic ||
214 ta == TypeAttributes.NestedAssembly ||
215 ta == TypeAttributes.NestedFamANDAssem) &&
216 !TypeManager.IsThisOrFriendAssembly (invocation, t.Assembly))
222 public override string ToString ()
224 return String.Format ("RootNamespace ({0}::)", alias_name);
227 public override string GetSignatureForError ()
229 return alias_name + "::";
233 public class GlobalRootNamespace : RootNamespace {
235 Dictionary<string, RootNamespace> root_namespaces;
237 public static GlobalRootNamespace Instance = new GlobalRootNamespace ();
239 GlobalRootNamespace ()
242 root_namespaces = new Dictionary<string, RootNamespace> ();
243 root_namespaces.Add (alias_name, this);
246 public static void Reset ()
248 Instance = new GlobalRootNamespace ();
251 public Assembly [] Assemblies {
252 get { return referenced_assemblies; }
255 public Module [] Modules {
256 get { return modules; }
259 public void AddModuleReference (Module m)
261 int top = modules != null ? modules.Length : 0;
262 Module [] n = new Module [top + 1];
264 modules.CopyTo (n, 0);
268 if (m == RootContext.ToplevelTypes.Builder)
271 foreach (Type t in m.GetTypes ())
272 RegisterNamespace (t.Namespace);
275 public void ComputeNamespaces (CompilerContext ctx)
278 // Do very early lookup because type is required when we cache
279 // imported extension types in ComputeNamespaces
281 Type extension_attribute_type = TypeManager.CoreLookupType (ctx, "System.Runtime.CompilerServices", "ExtensionAttribute", MemberKind.Class, false);
283 foreach (RootNamespace rn in root_namespaces.Values) {
284 rn.ComputeNamespace (ctx, extension_attribute_type);
288 public void DefineRootNamespace (string alias, Assembly assembly, CompilerContext ctx)
290 if (alias == alias_name) {
291 NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null, ctx.Report);
295 RootNamespace retval = GetRootNamespace (alias);
296 if (retval == null) {
297 retval = new RootNamespace (alias);
298 root_namespaces.Add (alias, retval);
301 retval.AddAssemblyReference (assembly);
304 public override void Error_NamespaceDoesNotExist (Location loc, string name, IMemberContext ctx)
306 ctx.Compiler.Report.Error (400, loc,
307 "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
311 public RootNamespace GetRootNamespace (string name)
314 if (!root_namespaces.TryGetValue (name, out rn))
320 public override Type LookupTypeReflection (CompilerContext ctx, string name, Location loc, bool must_be_unique)
322 Type found_type = base.LookupTypeReflection (ctx, name, loc, must_be_unique);
324 if (modules != null) {
325 foreach (Module module in modules) {
326 Type t = module.GetType (name);
330 if (found_type == null) {
335 ctx.Report.SymbolRelatedToPreviousError (found_type);
337 DeclSpace ds = TypeManager.LookupDeclSpace (t);
338 Error_AmbiguousPredefinedType (ctx, ds.Location, name, found_type);
341 ctx.Report.SymbolRelatedToPreviousError (t);
342 ctx.Report.Warning (436, 2, loc, "The type `{0}' conflicts with the imported type `{1}'. Ignoring the imported type definition",
343 TypeManager.CSharpName (t), TypeManager.CSharpName (found_type));
353 /// Keeps track of the namespaces defined in the C# code.
355 /// This is an Expression to allow it to be referenced in the
356 /// compiler parse/intermediate tree during name resolution.
358 public class Namespace : FullNamedExpression {
362 Dictionary<string, Namespace> namespaces;
363 Dictionary<string, DeclSpace> declspaces;
364 Dictionary<string, TypeExpr> cached_types;
366 List<Type> external_exmethod_classes;
368 public readonly MemberName MemberName;
371 /// Constructor Takes the current namespace and the
372 /// name. This is bootstrapped with parent == null
375 public Namespace (Namespace parent, string name)
377 // Expression members.
378 this.eclass = ExprClass.Namespace;
379 this.Type = typeof (Namespace);
380 this.loc = Location.Null;
382 this.parent = parent;
385 this.root = parent.root;
387 this.root = this as RootNamespace;
389 if (this.root == null)
390 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
392 string pname = parent != null ? parent.fullname : "";
397 fullname = parent.fullname + "." + name;
399 if (fullname == null)
400 throw new InternalErrorException ("Namespace has a null fullname");
402 if (parent != null && parent.MemberName != MemberName.Null)
403 MemberName = new MemberName (parent.MemberName, name);
404 else if (name.Length == 0)
405 MemberName = MemberName.Null;
407 MemberName = new MemberName (name);
409 namespaces = new Dictionary<string, Namespace> ();
410 cached_types = new Dictionary<string, TypeExpr> ();
412 root.RegisterNamespace (this);
415 protected override Expression DoResolve (ResolveContext ec)
420 public virtual void Error_NamespaceDoesNotExist (Location loc, string name, IMemberContext ctx)
422 if (name.IndexOf ('`') > 0) {
423 FullNamedExpression retval = Lookup (ctx.Compiler, SimpleName.RemoveGenericArity (name), loc);
424 if (retval != null) {
425 retval.Error_TypeArgumentsCannotBeUsed (ctx.Compiler.Report, loc);
429 Type t = LookForAnyGenericType (name);
431 Error_InvalidNumberOfTypeArguments (ctx.Compiler.Report, t, loc);
436 ctx.Compiler.Report.Error (234, loc,
437 "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
438 name, GetSignatureForError ());
441 public static void Error_InvalidNumberOfTypeArguments (Report report, Type t, Location loc)
443 report.SymbolRelatedToPreviousError (t);
444 report.Error (305, loc, "Using the generic type `{0}' requires `{1}' type argument(s)",
445 TypeManager.CSharpName(t), TypeManager.GetNumberOfTypeArguments(t).ToString());
448 public override string GetSignatureForError ()
453 public Namespace GetNamespace (string name, bool create)
455 int pos = name.IndexOf ('.');
460 first = name.Substring (0, pos);
464 if (!namespaces.TryGetValue (first, out ns)) {
468 ns = new Namespace (this, first);
469 namespaces.Add (first, ns);
473 ns = ns.GetNamespace (name.Substring (pos + 1), create);
478 public bool HasDefinition (string name)
480 return declspaces != null && declspaces.ContainsKey (name);
483 TypeExpr LookupType (CompilerContext ctx, string name, Location loc)
486 if (cached_types.TryGetValue (name, out te))
490 if (declspaces != null) {
492 if (declspaces.TryGetValue (name, out tdecl)) {
494 // Note that this is not:
496 // t = tdecl.DefineType ()
498 // This is to make it somewhat more useful when a DefineType
499 // fails due to problems in nested types (more useful in the sense
500 // of fewer misleading error messages)
503 t = tdecl.TypeBuilder;
505 if (RootContext.EvalMode){
506 // Replace the TypeBuilder with a System.Type, as
507 // Reflection.Emit fails otherwise (we end up pretty
508 // much with Random type definitions later on).
509 Type tt = t.Assembly.GetType (t.Name);
515 string lookup = t != null ? t.FullName : (fullname.Length == 0 ? name : fullname + "." + name);
516 Type rt = root.LookupTypeReflection (ctx, lookup, loc, t == null);
518 // HACK: loc.IsNull when the type is core type
519 if (t == null || (rt != null && loc.IsNull))
522 te = t == null ? null : new TypeExpression (t, Location.Null);
523 cached_types [name] = te;
528 /// Used for better error reporting only
530 public Type LookForAnyGenericType (string typeName)
532 if (declspaces == null)
535 typeName = SimpleName.RemoveGenericArity (typeName);
537 foreach (var de in declspaces) {
538 string type_item = de.Key;
539 int pos = type_item.LastIndexOf ('`');
540 if (pos == typeName.Length && String.Compare (typeName, 0, type_item, 0, pos) == 0)
541 return de.Value.TypeBuilder;
546 public FullNamedExpression Lookup (CompilerContext ctx, string name, Location loc)
548 if (namespaces.ContainsKey (name))
549 return namespaces [name];
551 return LookupType (ctx, name, loc);
555 // Completes types with the given `prefix' and stores the results in `result'
557 public void CompletionGetTypesStartingWith (string prefix, Dictionary<string, string> result)
559 int l = fullname.Length + 1;
560 var res = root.CompletionGetTypesStartingWith (fullname + "." + prefix);
565 foreach (string match in res){
566 string x = match.Substring (l);
568 // Turn reflection nested classes foo+bar into foo.bar
569 x = x.Replace ('+', '.');
571 // Only get the first name element, no point in adding anything beyond the first dot.
572 int p = x.IndexOf ('.');
574 x = x.Substring (0, p);
576 // Turn Foo`N into Foo<
579 x = x.Substring (0, p) + "<";
581 if (!result.ContainsKey (x))
586 public void RegisterExternalExtensionMethodClass (Type type)
588 // Ignore, extension methods cannot be nested
589 if (type.DeclaringType != null)
592 // TODO: CodeGen.Assembly.Builder is global
593 if (type.IsNotPublic && !TypeManager.IsThisOrFriendAssembly (CodeGen.Assembly.Builder, type.Assembly))
596 if (external_exmethod_classes == null)
597 external_exmethod_classes = new List<Type> ();
599 external_exmethod_classes.Add (type);
603 /// Looks for extension method in this namespace
605 public List<MethodSpec> LookupExtensionMethod (Type extensionType, ClassOrStruct currentClass, string name)
607 List<MethodSpec> found = null;
610 var invocation_assembly = CodeGen.Assembly.Builder;
612 if (declspaces != null) {
613 var e = declspaces.Values.GetEnumerator ();
614 while (e.MoveNext ()) {
615 Class c = e.Current as Class;
619 if ((c.ModFlags & Modifiers.METHOD_EXTENSION) == 0)
622 var res = c.MemberCache.FindExtensionMethods (invocation_assembly, 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 var res = m.FindExtensionMethods (invocation_assembly, extensionType, name, true);
645 found.AddRange (res);
651 public void AddDeclSpace (string name, DeclSpace ds)
653 if (declspaces == null)
654 declspaces = new Dictionary<string, DeclSpace> ();
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 : IMemberContext {
684 public class UsingEntry {
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 (IMemberContext rc)
712 if (resolved != null)
715 FullNamedExpression fne = name.GetTypeExpression ().ResolveAsTypeStep (rc, false);
719 resolved = fne as Namespace;
720 if (resolved == null) {
721 rc.Compiler.Report.SymbolRelatedToPreviousError (fne.Type);
722 rc.Compiler.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 public class UsingAliasEntry {
736 public readonly string Alias;
737 public Location Location;
739 public UsingAliasEntry (string alias, Location loc)
745 public virtual FullNamedExpression Resolve (IMemberContext rc)
747 FullNamedExpression fne = GlobalRootNamespace.Instance.GetRootNamespace (Alias);
749 rc.Compiler.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 (IMemberContext rc)
776 if (resolved != null || value == null)
782 resolved = value.GetTypeExpression ().ResolveAsTypeStep (rc, false);
783 if (resolved == null) {
788 if (resolved is TypeExpr)
789 resolved = resolved.ResolveAsBaseTerminal (rc, false);
794 public override string ToString ()
796 return String.Format ("{0} = {1}", Alias, value.GetSignatureForError ());
801 NamespaceEntry parent, implicit_parent;
802 CompilationUnit file;
805 // Namespace using import block
806 List<UsingAliasEntry> using_aliases;
807 List<UsingEntry> using_clauses;
808 public bool DeclarationFound;
811 public readonly bool IsImplicit;
812 public readonly DeclSpace SlaveDeclSpace;
813 static readonly Namespace [] empty_namespaces = new Namespace [0];
814 Namespace [] namespace_using_table;
816 static List<NamespaceEntry> entries = new List<NamespaceEntry> ();
818 public static void Reset ()
820 entries = new List<NamespaceEntry> ();
823 public NamespaceEntry (NamespaceEntry parent, CompilationUnit file, string name)
825 this.parent = parent;
830 ns = parent.NS.GetNamespace (name, true);
831 else if (name != null)
832 ns = GlobalRootNamespace.Instance.GetNamespace (name, true);
834 ns = GlobalRootNamespace.Instance;
835 SlaveDeclSpace = new RootDeclSpace (this);
838 private NamespaceEntry (NamespaceEntry parent, CompilationUnit file, Namespace ns, bool slave)
840 this.parent = parent;
842 this.IsImplicit = true;
844 this.SlaveDeclSpace = slave ? new RootDeclSpace (this) : null;
848 // Populates the Namespace with some using declarations, used by the
851 public void Populate (List<UsingAliasEntry> source_using_aliases, List<UsingEntry> source_using_clauses)
853 foreach (UsingAliasEntry uae in source_using_aliases){
854 if (using_aliases == null)
855 using_aliases = new List<UsingAliasEntry> ();
857 using_aliases.Add (uae);
860 foreach (UsingEntry ue in source_using_clauses){
861 if (using_clauses == null)
862 using_clauses = new List<UsingEntry> ();
864 using_clauses.Add (ue);
869 // Extracts the using alises and using clauses into a couple of
870 // arrays that might already have the same information; Used by the
873 public void Extract (List<UsingAliasEntry> out_using_aliases, List<UsingEntry> out_using_clauses)
875 if (using_aliases != null){
876 foreach (UsingAliasEntry uae in using_aliases){
877 bool replaced = false;
879 for (int i = 0; i < out_using_aliases.Count; i++){
880 UsingAliasEntry out_uea = (UsingAliasEntry) out_using_aliases [i];
882 if (out_uea.Alias == uae.Alias){
883 out_using_aliases [i] = uae;
889 out_using_aliases.Add (uae);
893 if (using_clauses != null){
894 foreach (UsingEntry ue in using_clauses){
897 foreach (UsingEntry out_ue in out_using_clauses)
898 if (out_ue.Name == ue.Name){
903 out_using_clauses.Add (ue);
909 // According to section 16.3.1 (using-alias-directive), the namespace-or-type-name is
910 // resolved as if the immediately containing namespace body has no using-directives.
912 // Section 16.3.2 says that the same rule is applied when resolving the namespace-name
913 // in the using-namespace-directive.
915 // To implement these rules, the expressions in the using directives are resolved using
916 // the "doppelganger" (ghostly bodiless duplicate).
918 NamespaceEntry doppelganger;
919 NamespaceEntry Doppelganger {
921 if (!IsImplicit && doppelganger == null) {
922 doppelganger = new NamespaceEntry (ImplicitParent, file, ns, true);
923 doppelganger.using_aliases = using_aliases;
929 public Namespace NS {
933 public NamespaceEntry Parent {
934 get { return parent; }
937 public NamespaceEntry ImplicitParent {
941 if (implicit_parent == null) {
942 implicit_parent = (parent.NS == ns.Parent)
944 : new NamespaceEntry (parent, file, ns.Parent, false);
946 return implicit_parent;
951 /// Records a new namespace for resolving name references
953 public void AddUsing (MemberName name, Location loc)
955 if (DeclarationFound){
956 Compiler.Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
959 if (using_clauses == null) {
960 using_clauses = new List<UsingEntry> ();
962 foreach (UsingEntry old_entry in using_clauses) {
963 if (name.Equals (old_entry.MemberName)) {
964 Compiler.Report.SymbolRelatedToPreviousError (old_entry.Location, old_entry.GetSignatureForError ());
965 Compiler.Report.Warning (105, 3, loc, "The using directive for `{0}' appeared previously in this namespace", name.GetSignatureForError ());
971 using_clauses.Add (new UsingEntry (name));
974 public void AddUsingAlias (string alias, MemberName name, Location loc)
976 // TODO: This is parser bussines
977 if (DeclarationFound){
978 Compiler.Report.Error (1529, loc, "A using clause must precede all other namespace elements except extern alias declarations");
981 if (RootContext.Version != LanguageVersion.ISO_1 && alias == "global")
982 Compiler.Report.Warning (440, 2, loc, "An alias named `global' will not be used when resolving 'global::';" +
983 " the global namespace will be used instead");
985 AddUsingAlias (new LocalUsingAliasEntry (alias, name, loc));
988 public void AddUsingExternalAlias (string alias, Location loc, Report Report)
990 // TODO: Do this in parser
991 bool not_first = using_clauses != null || DeclarationFound;
992 if (using_aliases != null && !not_first) {
993 foreach (UsingAliasEntry uae in using_aliases) {
994 if (uae is LocalUsingAliasEntry) {
1002 Report.Error (439, loc, "An extern alias declaration must precede all other elements");
1004 if (alias == "global") {
1005 Error_GlobalNamespaceRedefined (loc, Report);
1009 AddUsingAlias (new UsingAliasEntry (alias, loc));
1012 void AddUsingAlias (UsingAliasEntry uae)
1014 if (using_aliases == null) {
1015 using_aliases = new List<UsingAliasEntry> ();
1017 foreach (UsingAliasEntry entry in using_aliases) {
1018 if (uae.Alias == entry.Alias) {
1019 Compiler.Report.SymbolRelatedToPreviousError (uae.Location, uae.Alias);
1020 Compiler.Report.Error (1537, entry.Location, "The using alias `{0}' appeared previously in this namespace",
1027 using_aliases.Add (uae);
1031 /// Does extension methods look up to find a method which matches name and extensionType.
1032 /// Search starts from this namespace and continues hierarchically up to top level.
1034 public ExtensionMethodGroupExpr LookupExtensionMethod (Type extensionType, string name, Location loc)
1036 List<MethodSpec> candidates = null;
1037 foreach (Namespace n in GetUsingTable ()) {
1038 var a = n.LookupExtensionMethod (extensionType, null, name);
1042 if (candidates == null)
1045 candidates.AddRange (a);
1048 if (candidates != null)
1049 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1055 // Inspect parent namespaces in namespace expression
1057 Namespace parent_ns = ns.Parent;
1059 candidates = parent_ns.LookupExtensionMethod (extensionType, null, name);
1060 if (candidates != null)
1061 return new ExtensionMethodGroupExpr (candidates, parent, extensionType, loc);
1063 parent_ns = parent_ns.Parent;
1064 } while (parent_ns != null);
1067 // Continue in parent scope
1069 return parent.LookupExtensionMethod (extensionType, name, loc);
1072 public FullNamedExpression LookupNamespaceOrType (string name, Location loc, bool ignore_cs0104)
1074 // Precondition: Only simple names (no dots) will be looked up with this function.
1075 FullNamedExpression resolved = null;
1076 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent) {
1077 if ((resolved = curr_ns.Lookup (name, loc, ignore_cs0104)) != null)
1083 public ICollection<string> CompletionGetTypesStartingWith (string prefix)
1085 var result = new Dictionary<string, string> ();
1087 for (NamespaceEntry curr_ns = this; curr_ns != null; curr_ns = curr_ns.ImplicitParent){
1088 foreach (Namespace using_ns in GetUsingTable ()){
1089 if (prefix.StartsWith (using_ns.Name)){
1090 int ld = prefix.LastIndexOf ('.');
1092 string rest = prefix.Substring (ld+1);
1094 using_ns.CompletionGetTypesStartingWith (rest, result);
1097 using_ns.CompletionGetTypesStartingWith (prefix, result);
1104 void Error_AmbiguousTypeReference (Location loc, string name, FullNamedExpression t1, FullNamedExpression t2)
1106 Compiler.Report.SymbolRelatedToPreviousError (t1.Type);
1107 Compiler.Report.SymbolRelatedToPreviousError (t2.Type);
1108 Compiler.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 LookupNamespaceAlias (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 (string name, Location loc, bool ignore_cs0104)
1131 // Check whether it's in the namespace.
1133 FullNamedExpression fne = ns.Lookup (Compiler, 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 Compiler.Report.SymbolRelatedToPreviousError (ue.Location, null);
1146 Compiler.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 (Compiler, 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 var list = new List<Namespace> (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 = list.ToArray ();
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, Report Report)
1240 Report.Error (1681, loc, "You cannot redefine the global extern alias");
1243 public static void Error_NamespaceNotFound (Location loc, string name, Report Report)
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 IMemberContext Members
1310 public CompilerContext Compiler {
1311 get { return RootContext.ToplevelTypes.Compiler; }
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; }