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.
10 // Copyright 2011 Xamarin Inc
13 using System.Collections.Generic;
15 using Mono.CompilerServices.SymbolWriter;
17 namespace Mono.CSharp {
19 public class RootNamespace : Namespace {
21 readonly string alias_name;
22 readonly Dictionary<string, Namespace> all_namespaces;
24 public RootNamespace (string alias_name)
25 : base (null, String.Empty)
27 this.alias_name = alias_name;
29 all_namespaces = new Dictionary<string, Namespace> ();
30 all_namespaces.Add ("", this);
39 public static void Error_GlobalNamespaceRedefined (Report report, Location loc)
41 report.Error (1681, loc, "The global extern alias cannot be redefined");
44 public void RegisterNamespace (Namespace child)
47 all_namespaces.Add (child.Name, child);
50 public bool IsNamespace (string name)
52 return all_namespaces.ContainsKey (name);
55 protected void RegisterNamespace (string dotted_name)
57 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
58 GetNamespace (dotted_name, true);
61 public override string GetSignatureForError ()
63 return alias_name + "::";
67 public class GlobalRootNamespace : RootNamespace
69 public GlobalRootNamespace ()
76 // Namespace cache for imported and compiled namespaces
78 // This is an Expression to allow it to be referenced in the
79 // compiler parse/intermediate tree during name resolution.
81 public class Namespace : FullNamedExpression
85 protected Dictionary<string, Namespace> namespaces;
86 protected Dictionary<string, IList<TypeSpec>> types;
87 List<TypeSpec> extension_method_types;
88 Dictionary<string, TypeExpr> cached_types;
92 public readonly MemberName MemberName;
95 /// Constructor Takes the current namespace and the
96 /// name. This is bootstrapped with parent == null
99 public Namespace (Namespace parent, string name)
101 // Expression members.
102 this.eclass = ExprClass.Namespace;
103 this.Type = InternalType.Namespace;
104 this.loc = Location.Null;
106 this.parent = parent;
109 this.root = parent.root;
111 this.root = this as RootNamespace;
113 if (this.root == null)
114 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
116 string pname = parent != null ? parent.fullname : "";
121 fullname = parent.fullname + "." + name;
123 if (fullname == null)
124 throw new InternalErrorException ("Namespace has a null fullname");
126 if (parent != null && parent.MemberName != MemberName.Null)
127 MemberName = new MemberName (parent.MemberName, name, Location.Null);
128 else if (name.Length == 0)
129 MemberName = MemberName.Null;
131 MemberName = new MemberName (name, Location.Null);
133 namespaces = new Dictionary<string, Namespace> ();
134 cached_types = new Dictionary<string, TypeExpr> ();
136 root.RegisterNamespace (this);
142 /// The qualified name of the current namespace
145 get { return fullname; }
149 /// The parent of this namespace, used by the parser to "Pop"
150 /// the current namespace declaration
152 public Namespace Parent {
153 get { return parent; }
158 protected override Expression DoResolve (ResolveContext ec)
163 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
165 var retval = LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
166 if (retval != null) {
167 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.Type);
168 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
172 retval = LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
173 if (retval != null) {
174 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, arity, loc);
179 if (arity > 0 && namespaces.TryGetValue (name, out ns)) {
180 ns.Error_TypeArgumentsCannotBeUsed (ctx, null, arity, loc);
184 if (this is GlobalRootNamespace) {
185 ctx.Module.Compiler.Report.Error (400, loc,
186 "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
189 ctx.Module.Compiler.Report.Error (234, loc,
190 "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
191 name, GetSignatureForError ());
195 public override string GetSignatureForError ()
200 public Namespace AddNamespace (MemberName name)
203 if (name.Left != null) {
205 ns_parent = parent.AddNamespace (name.Left);
207 ns_parent = AddNamespace (name.Left);
212 return ns_parent.TryAddNamespace (name.Basename);
215 Namespace TryAddNamespace (string name)
219 if (!namespaces.TryGetValue (name, out ns)) {
220 ns = new Namespace (this, name);
221 namespaces.Add (name, ns);
227 // TODO: Replace with CreateNamespace where MemberName is created for the method call
228 public Namespace GetNamespace (string name, bool create)
230 int pos = name.IndexOf ('.');
235 first = name.Substring (0, pos);
239 if (!namespaces.TryGetValue (first, out ns)) {
243 ns = new Namespace (this, first);
244 namespaces.Add (first, ns);
248 ns = ns.GetNamespace (name.Substring (pos + 1), create);
253 public IList<TypeSpec> GetAllTypes (string name)
255 IList<TypeSpec> found;
256 if (types == null || !types.TryGetValue (name, out found))
262 public TypeExpr LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
268 if (arity == 0 && cached_types.TryGetValue (name, out te))
271 IList<TypeSpec> found;
272 if (!types.TryGetValue (name, out found))
275 TypeSpec best = null;
276 foreach (var ts in found) {
277 if (ts.Arity == arity) {
279 if ((ts.Modifiers & Modifiers.INTERNAL) != 0 && !ts.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly) && mode != LookupMode.IgnoreAccessibility)
286 if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) {
287 if (mode == LookupMode.Normal) {
288 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
289 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
290 ctx.Module.Compiler.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError ());
295 if (best.MemberDefinition.IsImported)
298 if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly))
301 if (mode != LookupMode.Normal)
304 if (ts.MemberDefinition.IsImported)
305 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
307 ctx.Module.Compiler.Report.Warning (436, 2, loc,
308 "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
309 best.GetSignatureForError ());
313 // Lookup for the best candidate with the closest arity match
318 } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (best.Arity + arity)) {
327 te = new TypeExpression (best, Location.Null);
329 // TODO MemberCache: Cache more
330 if (arity == 0 && mode == LookupMode.Normal)
331 cached_types.Add (name, te);
336 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
338 var texpr = LookupType (ctx, name, arity, mode, loc);
341 if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
345 if (mode != LookupMode.Probing) {
346 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
347 // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ns.loc, "");
348 ctx.Module.Compiler.Report.Warning (437, 2, loc,
349 "The type `{0}' conflicts with the imported namespace `{1}'. Using the definition found in the source file",
350 texpr.GetSignatureForError (), ns.GetSignatureForError ());
353 if (texpr.Type.MemberDefinition.IsImported)
361 // Completes types with the given `prefix'
363 public IEnumerable<string> CompletionGetTypesStartingWith (string prefix)
366 return Enumerable.Empty<string> ();
368 var res = from item in types
369 where item.Key.StartsWith (prefix) && item.Value.Any (l => (l.Modifiers & Modifiers.PUBLIC) != 0)
372 if (namespaces != null)
373 res = res.Concat (from item in namespaces where item.Key.StartsWith (prefix) select item.Key);
379 // Looks for extension method in this namespace
381 public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
383 if (extension_method_types == null)
386 List<MethodSpec> found = null;
387 for (int i = 0; i < extension_method_types.Count; ++i) {
388 var ts = extension_method_types[i];
391 // When the list was built we didn't know what members the type
394 if ((ts.Modifiers & Modifiers.METHOD_EXTENSION) == 0) {
395 if (extension_method_types.Count == 1) {
396 extension_method_types = null;
400 extension_method_types.RemoveAt (i--);
404 var res = ts.MemberCache.FindExtensionMethods (invocationContext, extensionType, name, arity);
411 found.AddRange (res);
418 public void AddType (ModuleContainer module, TypeSpec ts)
421 types = new Dictionary<string, IList<TypeSpec>> (64);
424 if ((ts.IsStatic || ts.MemberDefinition.IsPartial) && ts.Arity == 0 &&
425 (ts.MemberDefinition.DeclaringAssembly == null || ts.MemberDefinition.DeclaringAssembly.HasExtensionMethod)) {
426 if (extension_method_types == null)
427 extension_method_types = new List<TypeSpec> ();
429 extension_method_types.Add (ts);
433 IList<TypeSpec> existing;
434 if (types.TryGetValue (name, out existing)) {
435 TypeSpec better_type;
437 if (existing.Count == 1) {
439 if (ts.Arity == found.Arity) {
440 better_type = IsImportedTypeOverride (module, ts, found);
441 if (better_type == found)
444 if (better_type != null) {
445 existing [0] = better_type;
450 existing = new List<TypeSpec> ();
451 existing.Add (found);
452 types[name] = existing;
454 for (int i = 0; i < existing.Count; ++i) {
456 if (ts.Arity != found.Arity)
459 better_type = IsImportedTypeOverride (module, ts, found);
460 if (better_type == found)
463 if (better_type != null) {
464 existing.RemoveAt (i);
473 types.Add (name, new TypeSpec[] { ts });
478 // We import any types but in the situation there are same types
479 // but one has better visibility (either public or internal with friend)
480 // the less visible type is removed from the namespace cache
482 public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found)
484 var ts_accessible = (ts.Modifiers & Modifiers.PUBLIC) != 0 || ts.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
485 var found_accessible = (found.Modifiers & Modifiers.PUBLIC) != 0 || found.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
487 if (ts_accessible && !found_accessible)
490 // found is better always better for accessible or inaccessible ts
497 public void RemoveContainer (TypeContainer tc)
499 types.Remove (tc.Basename);
500 cached_types.Remove (tc.Basename);
503 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
508 public void SetBuiltinType (BuiltinTypeSpec pts)
510 var found = types[pts.Name];
511 cached_types.Remove (pts.Name);
512 if (found.Count == 1) {
513 types[pts.Name][0] = pts;
515 throw new NotImplementedException ();
519 public void VerifyClsCompliance ()
521 if (types == null || cls_checked)
526 // TODO: This is quite ugly way to check for CLS compliance at namespace level
528 var locase_types = new Dictionary<string, List<TypeSpec>> (StringComparer.OrdinalIgnoreCase);
529 foreach (var tgroup in types.Values) {
530 foreach (var tm in tgroup) {
531 if ((tm.Modifiers & Modifiers.PUBLIC) == 0 || !tm.IsCLSCompliant ())
534 List<TypeSpec> found;
535 if (!locase_types.TryGetValue (tm.Name, out found)) {
536 found = new List<TypeSpec> ();
537 locase_types.Add (tm.Name, found);
544 foreach (var locase in locase_types.Values) {
545 if (locase.Count < 2)
548 bool all_same = true;
549 foreach (var notcompliant in locase) {
550 all_same = notcompliant.Name == locase[0].Name;
558 TypeContainer compiled = null;
559 foreach (var notcompliant in locase) {
560 if (!notcompliant.MemberDefinition.IsImported) {
561 if (compiled != null)
562 compiled.Compiler.Report.SymbolRelatedToPreviousError (compiled);
564 compiled = notcompliant.MemberDefinition as TypeContainer;
566 compiled.Compiler.Report.SymbolRelatedToPreviousError (notcompliant);
570 compiled.Compiler.Report.Warning (3005, 1, compiled.Location,
571 "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
576 public class CompilationSourceFile : NamespaceContainer
578 readonly SourceFile file;
579 CompileUnitEntry comp_unit;
580 Dictionary<string, SourceFile> include_files;
581 Dictionary<string, bool> conditionals;
583 public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile)
586 this.file = sourceFile;
589 public CompilationSourceFile (ModuleContainer parent)
594 public CompileUnitEntry SymbolUnitEntry {
600 public string FileName {
606 public SourceFile SourceFile {
612 public void AddIncludeFile (SourceFile file)
614 if (file == this.file)
617 if (include_files == null)
618 include_files = new Dictionary<string, SourceFile> ();
620 if (!include_files.ContainsKey (file.FullPathName))
621 include_files.Add (file.FullPathName, file);
624 public void AddDefine (string value)
626 if (conditionals == null)
627 conditionals = new Dictionary<string, bool> (2);
629 conditionals[value] = true;
632 public void AddUndefine (string value)
634 if (conditionals == null)
635 conditionals = new Dictionary<string, bool> (2);
637 conditionals[value] = false;
640 public override void PrepareEmit ()
642 var sw = Module.DeclaringAssembly.SymbolWriter;
644 CreateUnitSymbolInfo (sw);
651 // Creates symbol file index in debug symbol file
653 void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
655 var si = file.CreateSymbolInfo (symwriter);
656 comp_unit = new CompileUnitEntry (symwriter, si);;
658 if (include_files != null) {
659 foreach (SourceFile include in include_files.Values) {
660 si = include.CreateSymbolInfo (symwriter);
661 comp_unit.AddFile (si);
666 public bool IsConditionalDefined (string value)
668 if (conditionals != null) {
670 if (conditionals.TryGetValue (value, out res))
673 // When conditional was undefined
674 if (conditionals.ContainsKey (value))
678 return Compiler.Settings.IsConditionalSymbolDefined (value);
684 // Namespace block as created by the parser
686 public class NamespaceContainer : TypeContainer, IMemberContext
688 static readonly Namespace[] empty_namespaces = new Namespace[0];
690 readonly Namespace ns;
692 public new readonly NamespaceContainer Parent;
694 List<UsingNamespace> clauses;
696 // Used by parsed to check for parser errors
697 public bool DeclarationFound;
699 Namespace[] namespace_using_table;
700 Dictionary<string, UsingAliasNamespace> aliases;
702 public NamespaceContainer (MemberName name, NamespaceContainer parent)
703 : base (parent, name, null, MemberKind.Namespace)
705 this.Parent = parent;
706 this.ns = parent.NS.AddNamespace (name);
708 containers = new List<TypeContainer> ();
711 protected NamespaceContainer (ModuleContainer parent)
712 : base (parent, null, null, MemberKind.Namespace)
714 ns = parent.GlobalRootNamespace;
715 containers = new List<TypeContainer> (2);
720 public override AttributeTargets AttributeTargets {
722 throw new NotSupportedException ();
726 public override string DocCommentHeader {
728 throw new NotSupportedException ();
732 public Namespace NS {
738 public List<UsingNamespace> Usings {
744 public override string[] ValidAttributeTargets {
746 throw new NotSupportedException ();
752 public void AddUsing (UsingNamespace un)
754 if (DeclarationFound){
755 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
759 clauses = new List<UsingNamespace> ();
764 public void AddUsing (UsingAliasNamespace un)
766 if (DeclarationFound){
767 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
773 void AddAlias (UsingAliasNamespace un)
775 if (clauses == null) {
776 clauses = new List<UsingNamespace> ();
778 foreach (var entry in clauses) {
779 var a = entry as UsingAliasNamespace;
780 if (a != null && a.Alias.Value == un.Alias.Value) {
781 Compiler.Report.SymbolRelatedToPreviousError (a.Location, "");
782 Compiler.Report.Error (1537, un.Location,
783 "The using alias `{0}' appeared previously in this namespace", un.Alias.Value);
791 public override void AddPartial (TypeDefinition next_part)
793 var existing = ns.LookupType (this, next_part.MemberName.Name, next_part.MemberName.Arity, LookupMode.Probing, Location.Null);
794 var td = existing != null ? existing.Type.MemberDefinition as TypeDefinition : null;
795 AddPartial (next_part, td);
798 public override void AddTypeContainer (TypeContainer tc)
800 string name = tc.Basename;
802 var mn = tc.MemberName;
803 while (mn.Left != null) {
808 var names_container = Parent == null ? Module : (TypeContainer) this;
811 if (names_container.DefinedNames.TryGetValue (name, out mc)) {
812 if (tc is NamespaceContainer && mc is NamespaceContainer) {
817 Report.SymbolRelatedToPreviousError (mc);
818 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (tc is ClassOrStruct || tc is Interface)) {
819 Error_MissingPartialModifier (tc);
821 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
822 GetSignatureForError (), mn.GetSignatureForError ());
825 names_container.DefinedNames.Add (name, tc);
828 base.AddTypeContainer (tc);
830 var tdef = tc.PartialContainer;
832 ns.AddType (Module, tdef.Definition);
835 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
837 throw new NotSupportedException ();
840 public override void EmitContainer ()
842 VerifyClsCompliance ();
844 base.EmitContainer ();
847 public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
850 // Here we try to resume the search for extension method at the point
851 // where the last bunch of candidates was found. It's more tricky than
852 // it seems as we have to check both namespace containers and namespace
860 // <our first search found candidates in A.B.C.D
864 // In the example above namespace A.B.C.D, A.B.C and A.B have to be
865 // checked before we hit A.N1 using
867 ExtensionMethodCandidates candidates;
868 var container = this;
870 candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
871 if (candidates != null || container.MemberName == null)
874 var container_ns = container.ns.Parent;
875 var mn = container.MemberName.Left;
876 int already_checked = position - 2;
877 while (already_checked-- > 0) {
879 container_ns = container_ns.Parent;
885 var methods = container_ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
886 if (methods != null) {
887 return new ExtensionMethodCandidates (invocationContext, methods, container, position);
891 container_ns = container_ns.Parent;
895 container = container.Parent;
896 } while (container != null);
901 ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, ref int position)
903 List<MethodSpec> candidates = null;
908 candidates = ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
909 if (candidates != null) {
910 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
917 foreach (Namespace n in namespace_using_table) {
918 var a = n.LookupExtensionMethod (invocationContext, extensionType, name, arity);
922 if (candidates == null)
925 candidates.AddRange (a);
928 if (candidates != null)
929 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
935 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
938 // Only simple names (no dots) will be looked up with this function
940 FullNamedExpression resolved;
941 for (NamespaceContainer container = this; container != null; container = container.Parent) {
942 resolved = container.Lookup (name, arity, mode, loc);
943 if (resolved != null || container.MemberName == null)
946 var container_ns = container.ns.Parent;
947 var mn = container.MemberName.Left;
949 resolved = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
950 if (resolved != null)
954 container_ns = container_ns.Parent;
961 public override void GetCompletionStartingWith (string prefix, List<string> results)
963 foreach (var un in Usings) {
964 if (un.Alias != null)
967 var name = un.NamespaceExpression.Name;
968 if (name.StartsWith (prefix))
973 IEnumerable<string> all = Enumerable.Empty<string> ();
975 foreach (Namespace using_ns in namespace_using_table) {
976 if (prefix.StartsWith (using_ns.Name)) {
977 int ld = prefix.LastIndexOf ('.');
979 string rest = prefix.Substring (ld + 1);
981 all = all.Concat (using_ns.CompletionGetTypesStartingWith (rest));
984 all = all.Concat (using_ns.CompletionGetTypesStartingWith (prefix));
987 results.AddRange (all);
989 base.GetCompletionStartingWith (prefix, results);
994 // Looks-up a alias named @name in this and surrounding namespace declarations
996 public FullNamedExpression LookupExternAlias (string name)
1001 UsingAliasNamespace uan;
1002 if (aliases.TryGetValue (name, out uan) && uan is UsingExternAlias)
1003 return uan.ResolvedExpression;
1009 // Looks-up a alias named @name in this and surrounding namespace declarations
1011 public override FullNamedExpression LookupNamespaceAlias (string name)
1013 for (NamespaceContainer n = this; n != null; n = n.Parent) {
1014 if (n.aliases == null)
1017 UsingAliasNamespace uan;
1018 if (n.aliases.TryGetValue (name, out uan))
1019 return uan.ResolvedExpression;
1025 FullNamedExpression Lookup (string name, int arity, LookupMode mode, Location loc)
1028 // Check whether it's in the namespace.
1030 FullNamedExpression fne = ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1035 if (aliases != null && arity == 0) {
1036 UsingAliasNamespace uan;
1037 if (aliases.TryGetValue (name, out uan)) {
1039 // TODO: Namespace has broken location
1040 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1041 Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
1042 Compiler.Report.Error (576, loc,
1043 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1044 GetSignatureForError (), name);
1047 return uan.ResolvedExpression;
1055 // Lookup can be called before the namespace is defined from different namespace using alias clause
1057 if (namespace_using_table == null) {
1058 DoDefineNamespace ();
1062 // Check using entries.
1064 FullNamedExpression match = null;
1065 foreach (Namespace using_ns in namespace_using_table) {
1067 // A using directive imports only types contained in the namespace, it
1068 // does not import any nested namespaces
1070 fne = using_ns.LookupType (this, name, arity, mode, loc);
1074 if (match == null) {
1079 // Prefer types over namespaces
1080 var texpr_fne = fne as TypeExpr;
1081 var texpr_match = match as TypeExpr;
1082 if (texpr_fne != null && texpr_match == null) {
1085 } else if (texpr_fne == null) {
1089 // It can be top level accessibility only
1090 var better = Namespace.IsImportedTypeOverride (Module, texpr_match.Type, texpr_fne.Type);
1091 if (better == null) {
1092 if (mode == LookupMode.Normal) {
1093 Compiler.Report.SymbolRelatedToPreviousError (texpr_match.Type);
1094 Compiler.Report.SymbolRelatedToPreviousError (texpr_fne.Type);
1095 Compiler.Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1096 name, texpr_match.GetSignatureForError (), texpr_fne.GetSignatureForError ());
1102 if (better == texpr_fne.Type)
1109 static void MsgtryRef (string s)
1111 Console.WriteLine (" Try using -r:" + s);
1114 static void MsgtryPkg (string s)
1116 Console.WriteLine (" Try using -pkg:" + s);
1119 public static void Error_NamespaceNotFound (Location loc, string name, Report Report)
1121 Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
1125 case "Gtk": case "GtkSharp":
1126 MsgtryPkg ("gtk-sharp-2.0");
1129 case "Gdk": case "GdkSharp":
1130 MsgtryPkg ("gdk-sharp-2.0");
1133 case "Glade": case "GladeSharp":
1134 MsgtryPkg ("glade-sharp-2.0");
1137 case "System.Drawing":
1138 case "System.Web.Services":
1141 case "System.Windows.Forms":
1147 protected override void DefineNamespace ()
1149 if (namespace_using_table == null)
1150 DoDefineNamespace ();
1152 base.DefineNamespace ();
1155 void DoDefineNamespace ()
1157 namespace_using_table = empty_namespaces;
1159 if (clauses != null) {
1160 var list = new List<Namespace> (clauses.Count);
1161 bool post_process_using_aliases = false;
1163 for (int i = 0; i < clauses.Count; ++i) {
1164 var entry = clauses[i];
1166 if (entry.Alias != null) {
1167 if (aliases == null)
1168 aliases = new Dictionary<string, UsingAliasNamespace> ();
1171 // Aliases are not available when resolving using section
1172 // except extern aliases
1174 if (entry is UsingExternAlias) {
1175 entry.Define (this);
1176 if (entry.ResolvedExpression != null)
1177 aliases.Add (entry.Alias.Value, (UsingExternAlias) entry);
1179 clauses.RemoveAt (i--);
1181 post_process_using_aliases = true;
1187 entry.Define (this);
1190 // It's needed for repl only, when using clause cannot be resolved don't hold it in
1191 // global list which is resolved for each evaluation
1193 if (entry.ResolvedExpression == null) {
1194 clauses.RemoveAt (i--);
1198 Namespace using_ns = entry.ResolvedExpression as Namespace;
1199 if (using_ns == null)
1202 if (list.Contains (using_ns)) {
1203 // Ensure we don't report the warning multiple times in repl
1204 clauses.RemoveAt (i--);
1206 Compiler.Report.Warning (105, 3, entry.Location,
1207 "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
1209 list.Add (using_ns);
1213 namespace_using_table = list.ToArray ();
1215 if (post_process_using_aliases) {
1216 for (int i = 0; i < clauses.Count; ++i) {
1217 var entry = clauses[i];
1218 if (entry.Alias != null) {
1219 entry.Define (this);
1220 if (entry.ResolvedExpression != null) {
1221 aliases.Add (entry.Alias.Value, (UsingAliasNamespace) entry);
1224 clauses.RemoveAt (i--);
1231 public void EnableUsingClausesRedefinition ()
1233 namespace_using_table = null;
1236 internal override void GenerateDocComment (DocumentationBuilder builder)
1238 if (containers != null) {
1239 foreach (var tc in containers)
1240 tc.GenerateDocComment (builder);
1244 public override string GetSignatureForError ()
1246 return MemberName == null ? "global::" : base.GetSignatureForError ();
1249 public override void RemoveContainer (TypeContainer cont)
1251 base.RemoveContainer (cont);
1252 NS.RemoveContainer (cont);
1255 protected override bool VerifyClsCompliance ()
1257 if (Module.IsClsComplianceRequired ()) {
1258 if (MemberName != null && MemberName.Name[0] == '_') {
1259 Warning_IdentifierNotCompliant ();
1262 ns.VerifyClsCompliance ();
1270 public class UsingNamespace
1272 readonly ATypeNameExpression expr;
1273 readonly Location loc;
1274 protected FullNamedExpression resolved;
1276 public UsingNamespace (ATypeNameExpression expr, Location loc)
1284 public virtual SimpleMemberName Alias {
1290 public Location Location {
1296 public ATypeNameExpression NamespaceExpression {
1302 public FullNamedExpression ResolvedExpression {
1310 public string GetSignatureForError ()
1312 return expr.GetSignatureForError ();
1315 public virtual void Define (NamespaceContainer ctx)
1317 resolved = expr.ResolveAsTypeOrNamespace (ctx);
1318 var ns = resolved as Namespace;
1320 if (resolved != null) {
1321 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (resolved.Type);
1322 ctx.Module.Compiler.Report.Error (138, Location,
1323 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
1324 GetSignatureForError ());
1330 public class UsingExternAlias : UsingAliasNamespace
1332 public UsingExternAlias (SimpleMemberName alias, Location loc)
1333 : base (alias, null, loc)
1337 public override void Define (NamespaceContainer ctx)
1339 resolved = ctx.Module.GetRootNamespace (Alias.Value);
1340 if (resolved == null) {
1341 ctx.Module.Compiler.Report.Error (430, Location,
1342 "The extern alias `{0}' was not specified in -reference option",
1348 public class UsingAliasNamespace : UsingNamespace
1350 readonly SimpleMemberName alias;
1352 public struct AliasContext : IMemberContext
1354 readonly NamespaceContainer ns;
1356 public AliasContext (NamespaceContainer ns)
1361 public TypeSpec CurrentType {
1367 public TypeParameters CurrentTypeParameters {
1373 public MemberCore CurrentMemberDefinition {
1379 public bool IsObsolete {
1385 public bool IsUnsafe {
1387 throw new NotImplementedException ();
1391 public bool IsStatic {
1393 throw new NotImplementedException ();
1397 public ModuleContainer Module {
1403 public string GetSignatureForError ()
1405 throw new NotImplementedException ();
1408 public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
1413 public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1415 var fne = ns.NS.LookupTypeOrNamespace (ns, name, arity, mode, loc);
1420 // Only extern aliases are allowed in this context
1422 fne = ns.LookupExternAlias (name);
1423 if (fne != null || ns.MemberName == null)
1426 var container_ns = ns.NS.Parent;
1427 var mn = ns.MemberName.Left;
1428 while (mn != null) {
1429 fne = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1434 container_ns = container_ns.Parent;
1437 if (ns.Parent != null)
1438 return ns.Parent.LookupNamespaceOrType (name, arity, mode, loc);
1443 public FullNamedExpression LookupNamespaceAlias (string name)
1445 return ns.LookupNamespaceAlias (name);
1449 public UsingAliasNamespace (SimpleMemberName alias, ATypeNameExpression expr, Location loc)
1455 public override SimpleMemberName Alias {
1461 public override void Define (NamespaceContainer ctx)
1464 // The namespace-or-type-name of a using-alias-directive is resolved as if
1465 // the immediately containing compilation unit or namespace body had no
1466 // using-directives. A using-alias-directive may however be affected
1467 // by extern-alias-directives in the immediately containing compilation
1468 // unit or namespace body
1470 // We achieve that by introducing alias-context which redirect any local
1471 // namespace or type resolve calls to parent namespace
1473 resolved = NamespaceExpression.ResolveAsTypeOrNamespace (new AliasContext (ctx));