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");
45 // For better error reporting where we try to guess missing using directive
47 public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
49 List<string> res = null;
51 foreach (var ns in all_namespaces) {
52 var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
55 res = new List<string> ();
65 // For better error reporting where compiler tries to guess missing using directive
67 public List<string> FindExtensionMethodNamespaces (IMemberContext ctx, TypeSpec extensionType, string name, int arity)
69 List<string> res = null;
71 foreach (var ns in all_namespaces) {
72 var methods = ns.Value.LookupExtensionMethod (ctx, extensionType, name, arity);
73 if (methods != null) {
75 res = new List<string> ();
84 public void RegisterNamespace (Namespace child)
87 all_namespaces.Add (child.Name, child);
90 public bool IsNamespace (string name)
92 return all_namespaces.ContainsKey (name);
95 protected void RegisterNamespace (string dotted_name)
97 if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
98 GetNamespace (dotted_name, true);
101 public override string GetSignatureForError ()
103 return alias_name + "::";
107 public class GlobalRootNamespace : RootNamespace
109 public GlobalRootNamespace ()
116 // Namespace cache for imported and compiled namespaces
118 // This is an Expression to allow it to be referenced in the
119 // compiler parse/intermediate tree during name resolution.
121 public class Namespace : FullNamedExpression
125 protected Dictionary<string, Namespace> namespaces;
126 protected Dictionary<string, IList<TypeSpec>> types;
127 List<TypeSpec> extension_method_types;
128 Dictionary<string, TypeExpr> cached_types;
132 public readonly MemberName MemberName;
135 /// Constructor Takes the current namespace and the
136 /// name. This is bootstrapped with parent == null
139 public Namespace (Namespace parent, string name)
141 // Expression members.
142 this.eclass = ExprClass.Namespace;
143 this.Type = InternalType.Namespace;
144 this.loc = Location.Null;
146 this.parent = parent;
149 this.root = parent.root;
151 this.root = this as RootNamespace;
153 if (this.root == null)
154 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
156 string pname = parent != null ? parent.fullname : "";
161 fullname = parent.fullname + "." + name;
163 if (fullname == null)
164 throw new InternalErrorException ("Namespace has a null fullname");
166 if (parent != null && parent.MemberName != MemberName.Null)
167 MemberName = new MemberName (parent.MemberName, name, Location.Null);
168 else if (name.Length == 0)
169 MemberName = MemberName.Null;
171 MemberName = new MemberName (name, Location.Null);
173 namespaces = new Dictionary<string, Namespace> ();
174 cached_types = new Dictionary<string, TypeExpr> ();
176 root.RegisterNamespace (this);
182 /// The qualified name of the current namespace
185 get { return fullname; }
189 /// The parent of this namespace, used by the parser to "Pop"
190 /// the current namespace declaration
192 public Namespace Parent {
193 get { return parent; }
198 protected override Expression DoResolve (ResolveContext ec)
203 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
205 var retval = LookupType (ctx, name, arity, LookupMode.IgnoreAccessibility, loc);
206 if (retval != null) {
207 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.Type);
208 ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
212 retval = LookupType (ctx, name, -System.Math.Max (1, arity), LookupMode.Probing, loc);
213 if (retval != null) {
214 Error_TypeArgumentsCannotBeUsed (ctx, retval.Type, arity, loc);
219 if (arity > 0 && namespaces.TryGetValue (name, out ns)) {
220 ns.Error_TypeArgumentsCannotBeUsed (ctx, null, arity, loc);
224 string assembly = null;
225 string possible_name = fullname + "." + name;
227 // Only assembly unique name should be added
228 switch (possible_name) {
229 case "System.Drawing":
230 case "System.Web.Services":
233 case "System.Configuration":
234 case "System.Data.Services":
235 case "System.DirectoryServices":
237 case "System.Net.Http":
238 case "System.Numerics":
239 case "System.Runtime.Caching":
240 case "System.ServiceModel":
241 case "System.Transactions":
242 case "System.Web.Routing":
243 case "System.Xml.Linq":
245 assembly = possible_name;
249 case "System.Linq.Expressions":
250 assembly = "System.Core";
253 case "System.Windows.Forms":
254 case "System.Windows.Forms.Layout":
255 assembly = "System.Windows.Name";
259 assembly = assembly == null ? "an" : "`" + assembly + "'";
261 if (this is GlobalRootNamespace) {
262 ctx.Module.Compiler.Report.Error (400, loc,
263 "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
266 ctx.Module.Compiler.Report.Error (234, loc,
267 "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
268 name, GetSignatureForError (), assembly);
272 public override string GetSignatureForError ()
277 public Namespace AddNamespace (MemberName name)
280 if (name.Left != null) {
282 ns_parent = parent.AddNamespace (name.Left);
284 ns_parent = AddNamespace (name.Left);
289 return ns_parent.TryAddNamespace (name.Basename);
292 Namespace TryAddNamespace (string name)
296 if (!namespaces.TryGetValue (name, out ns)) {
297 ns = new Namespace (this, name);
298 namespaces.Add (name, ns);
304 // TODO: Replace with CreateNamespace where MemberName is created for the method call
305 public Namespace GetNamespace (string name, bool create)
307 int pos = name.IndexOf ('.');
312 first = name.Substring (0, pos);
316 if (!namespaces.TryGetValue (first, out ns)) {
320 ns = new Namespace (this, first);
321 namespaces.Add (first, ns);
325 ns = ns.GetNamespace (name.Substring (pos + 1), create);
330 public IList<TypeSpec> GetAllTypes (string name)
332 IList<TypeSpec> found;
333 if (types == null || !types.TryGetValue (name, out found))
339 public TypeExpr LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
345 if (arity == 0 && cached_types.TryGetValue (name, out te))
348 IList<TypeSpec> found;
349 if (!types.TryGetValue (name, out found))
352 TypeSpec best = null;
353 foreach (var ts in found) {
354 if (ts.Arity == arity) {
356 if ((ts.Modifiers & Modifiers.INTERNAL) != 0 && !ts.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly) && mode != LookupMode.IgnoreAccessibility)
363 if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) {
364 if (mode == LookupMode.Normal) {
365 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
366 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
367 ctx.Module.Compiler.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError ());
372 if (best.MemberDefinition.IsImported)
375 if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly))
378 if (mode != LookupMode.Normal)
381 if (ts.MemberDefinition.IsImported) {
382 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
383 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
386 ctx.Module.Compiler.Report.Warning (436, 2, loc,
387 "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
388 best.GetSignatureForError ());
392 // Lookup for the best candidate with the closest arity match
397 } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (best.Arity + arity)) {
406 te = new TypeExpression (best, Location.Null);
408 // TODO MemberCache: Cache more
409 if (arity == 0 && mode == LookupMode.Normal)
410 cached_types.Add (name, te);
415 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
417 var texpr = LookupType (ctx, name, arity, mode, loc);
420 if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
424 if (mode != LookupMode.Probing) {
425 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
426 // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ns.loc, "");
427 ctx.Module.Compiler.Report.Warning (437, 2, loc,
428 "The type `{0}' conflicts with the imported namespace `{1}'. Using the definition found in the source file",
429 texpr.GetSignatureForError (), ns.GetSignatureForError ());
432 if (texpr.Type.MemberDefinition.IsImported)
440 // Completes types with the given `prefix'
442 public IEnumerable<string> CompletionGetTypesStartingWith (string prefix)
445 return Enumerable.Empty<string> ();
447 var res = from item in types
448 where item.Key.StartsWith (prefix) && item.Value.Any (l => (l.Modifiers & Modifiers.PUBLIC) != 0)
451 if (namespaces != null)
452 res = res.Concat (from item in namespaces where item.Key.StartsWith (prefix) select item.Key);
458 // Looks for extension method in this namespace
460 public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
462 if (extension_method_types == null)
465 List<MethodSpec> found = null;
466 for (int i = 0; i < extension_method_types.Count; ++i) {
467 var ts = extension_method_types[i];
470 // When the list was built we didn't know what members the type
473 if ((ts.Modifiers & Modifiers.METHOD_EXTENSION) == 0) {
474 if (extension_method_types.Count == 1) {
475 extension_method_types = null;
479 extension_method_types.RemoveAt (i--);
483 var res = ts.MemberCache.FindExtensionMethods (invocationContext, extensionType, name, arity);
490 found.AddRange (res);
497 public void AddType (ModuleContainer module, TypeSpec ts)
500 types = new Dictionary<string, IList<TypeSpec>> (64);
503 if (ts.IsClass && ts.Arity == 0) {
504 var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial);
505 if (extension_method_allowed) {
506 if (extension_method_types == null)
507 extension_method_types = new List<TypeSpec> ();
509 extension_method_types.Add (ts);
514 IList<TypeSpec> existing;
515 if (types.TryGetValue (name, out existing)) {
516 TypeSpec better_type;
518 if (existing.Count == 1) {
520 if (ts.Arity == found.Arity) {
521 better_type = IsImportedTypeOverride (module, ts, found);
522 if (better_type == found)
525 if (better_type != null) {
526 existing [0] = better_type;
531 existing = new List<TypeSpec> ();
532 existing.Add (found);
533 types[name] = existing;
535 for (int i = 0; i < existing.Count; ++i) {
537 if (ts.Arity != found.Arity)
540 better_type = IsImportedTypeOverride (module, ts, found);
541 if (better_type == found)
544 if (better_type != null) {
545 existing.RemoveAt (i);
554 types.Add (name, new TypeSpec[] { ts });
559 // We import any types but in the situation there are same types
560 // but one has better visibility (either public or internal with friend)
561 // the less visible type is removed from the namespace cache
563 public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found)
565 var ts_accessible = (ts.Modifiers & Modifiers.PUBLIC) != 0 || ts.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
566 var found_accessible = (found.Modifiers & Modifiers.PUBLIC) != 0 || found.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
568 if (ts_accessible && !found_accessible)
571 // found is better always better for accessible or inaccessible ts
578 public void RemoveContainer (TypeContainer tc)
580 types.Remove (tc.Basename);
581 cached_types.Remove (tc.Basename);
584 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
589 public void SetBuiltinType (BuiltinTypeSpec pts)
591 var found = types[pts.Name];
592 cached_types.Remove (pts.Name);
593 if (found.Count == 1) {
594 types[pts.Name][0] = pts;
596 throw new NotImplementedException ();
600 public void VerifyClsCompliance ()
602 if (types == null || cls_checked)
607 // TODO: This is quite ugly way to check for CLS compliance at namespace level
609 var locase_types = new Dictionary<string, List<TypeSpec>> (StringComparer.OrdinalIgnoreCase);
610 foreach (var tgroup in types.Values) {
611 foreach (var tm in tgroup) {
612 if ((tm.Modifiers & Modifiers.PUBLIC) == 0 || !tm.IsCLSCompliant ())
615 List<TypeSpec> found;
616 if (!locase_types.TryGetValue (tm.Name, out found)) {
617 found = new List<TypeSpec> ();
618 locase_types.Add (tm.Name, found);
625 foreach (var locase in locase_types.Values) {
626 if (locase.Count < 2)
629 bool all_same = true;
630 foreach (var notcompliant in locase) {
631 all_same = notcompliant.Name == locase[0].Name;
639 TypeContainer compiled = null;
640 foreach (var notcompliant in locase) {
641 if (!notcompliant.MemberDefinition.IsImported) {
642 if (compiled != null)
643 compiled.Compiler.Report.SymbolRelatedToPreviousError (compiled);
645 compiled = notcompliant.MemberDefinition as TypeContainer;
647 compiled.Compiler.Report.SymbolRelatedToPreviousError (notcompliant);
651 compiled.Compiler.Report.Warning (3005, 1, compiled.Location,
652 "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
656 public override string ToString ()
662 public class CompilationSourceFile : NamespaceContainer
664 readonly SourceFile file;
665 CompileUnitEntry comp_unit;
666 Dictionary<string, SourceFile> include_files;
667 Dictionary<string, bool> conditionals;
669 public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile)
672 this.file = sourceFile;
675 public CompilationSourceFile (ModuleContainer parent)
680 public CompileUnitEntry SymbolUnitEntry {
686 public string FileName {
692 public SourceFile SourceFile {
698 public void AddIncludeFile (SourceFile file)
700 if (file == this.file)
703 if (include_files == null)
704 include_files = new Dictionary<string, SourceFile> ();
706 if (!include_files.ContainsKey (file.FullPathName))
707 include_files.Add (file.FullPathName, file);
710 public void AddDefine (string value)
712 if (conditionals == null)
713 conditionals = new Dictionary<string, bool> (2);
715 conditionals[value] = true;
718 public void AddUndefine (string value)
720 if (conditionals == null)
721 conditionals = new Dictionary<string, bool> (2);
723 conditionals[value] = false;
726 public override void PrepareEmit ()
728 var sw = Module.DeclaringAssembly.SymbolWriter;
730 CreateUnitSymbolInfo (sw);
737 // Creates symbol file index in debug symbol file
739 void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
741 var si = file.CreateSymbolInfo (symwriter);
742 comp_unit = new CompileUnitEntry (symwriter, si);;
744 if (include_files != null) {
745 foreach (SourceFile include in include_files.Values) {
746 si = include.CreateSymbolInfo (symwriter);
747 comp_unit.AddFile (si);
752 public bool IsConditionalDefined (string value)
754 if (conditionals != null) {
756 if (conditionals.TryGetValue (value, out res))
759 // When conditional was undefined
760 if (conditionals.ContainsKey (value))
764 return Compiler.Settings.IsConditionalSymbolDefined (value);
770 // Namespace block as created by the parser
772 public class NamespaceContainer : TypeContainer, IMemberContext
774 static readonly Namespace[] empty_namespaces = new Namespace[0];
776 readonly Namespace ns;
778 public new readonly NamespaceContainer Parent;
780 List<UsingNamespace> clauses;
782 // Used by parsed to check for parser errors
783 public bool DeclarationFound;
785 Namespace[] namespace_using_table;
786 Dictionary<string, UsingAliasNamespace> aliases;
788 public NamespaceContainer (MemberName name, NamespaceContainer parent)
789 : base (parent, name, null, MemberKind.Namespace)
791 this.Parent = parent;
792 this.ns = parent.NS.AddNamespace (name);
794 containers = new List<TypeContainer> ();
797 protected NamespaceContainer (ModuleContainer parent)
798 : base (parent, null, null, MemberKind.Namespace)
800 ns = parent.GlobalRootNamespace;
801 containers = new List<TypeContainer> (2);
806 public override AttributeTargets AttributeTargets {
808 throw new NotSupportedException ();
812 public override string DocCommentHeader {
814 throw new NotSupportedException ();
818 public Namespace NS {
824 public List<UsingNamespace> Usings {
830 public override string[] ValidAttributeTargets {
832 throw new NotSupportedException ();
838 public void AddUsing (UsingNamespace un)
840 if (DeclarationFound){
841 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
845 clauses = new List<UsingNamespace> ();
850 public void AddUsing (UsingAliasNamespace un)
852 if (DeclarationFound){
853 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
859 void AddAlias (UsingAliasNamespace un)
861 if (clauses == null) {
862 clauses = new List<UsingNamespace> ();
864 foreach (var entry in clauses) {
865 var a = entry as UsingAliasNamespace;
866 if (a != null && a.Alias.Value == un.Alias.Value) {
867 Compiler.Report.SymbolRelatedToPreviousError (a.Location, "");
868 Compiler.Report.Error (1537, un.Location,
869 "The using alias `{0}' appeared previously in this namespace", un.Alias.Value);
877 public override void AddPartial (TypeDefinition next_part)
879 var existing = ns.LookupType (this, next_part.MemberName.Name, next_part.MemberName.Arity, LookupMode.Probing, Location.Null);
880 var td = existing != null ? existing.Type.MemberDefinition as TypeDefinition : null;
881 AddPartial (next_part, td);
884 public override void AddTypeContainer (TypeContainer tc)
886 string name = tc.Basename;
888 var mn = tc.MemberName;
889 while (mn.Left != null) {
894 var names_container = Parent == null ? Module : (TypeContainer) this;
897 if (names_container.DefinedNames.TryGetValue (name, out mc)) {
898 if (tc is NamespaceContainer && mc is NamespaceContainer) {
903 Report.SymbolRelatedToPreviousError (mc);
904 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (tc is ClassOrStruct || tc is Interface)) {
905 Error_MissingPartialModifier (tc);
907 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
908 GetSignatureForError (), mn.GetSignatureForError ());
911 names_container.DefinedNames.Add (name, tc);
913 var tdef = tc.PartialContainer;
916 // Same name conflict in different namespace containers
918 var conflict = ns.GetAllTypes (name);
919 if (conflict != null) {
920 foreach (var e in conflict) {
921 if (e.Arity == mn.Arity) {
922 mc = (MemberCore) e.MemberDefinition;
929 Report.SymbolRelatedToPreviousError (mc);
930 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
931 GetSignatureForError (), mn.GetSignatureForError ());
933 ns.AddType (Module, tdef.Definition);
938 base.AddTypeContainer (tc);
941 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
943 throw new NotSupportedException ();
946 public override void EmitContainer ()
948 VerifyClsCompliance ();
950 base.EmitContainer ();
953 public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
956 // Here we try to resume the search for extension method at the point
957 // where the last bunch of candidates was found. It's more tricky than
958 // it seems as we have to check both namespace containers and namespace
966 // <our first search found candidates in A.B.C.D
970 // In the example above namespace A.B.C.D, A.B.C and A.B have to be
971 // checked before we hit A.N1 using
973 ExtensionMethodCandidates candidates;
974 var container = this;
976 candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
977 if (candidates != null || container.MemberName == null)
980 var container_ns = container.ns.Parent;
981 var mn = container.MemberName.Left;
982 int already_checked = position - 2;
983 while (already_checked-- > 0) {
985 container_ns = container_ns.Parent;
991 var methods = container_ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
992 if (methods != null) {
993 return new ExtensionMethodCandidates (invocationContext, methods, container, position);
997 container_ns = container_ns.Parent;
1001 container = container.Parent;
1002 } while (container != null);
1007 ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, ref int position)
1009 List<MethodSpec> candidates = null;
1011 if (position == 0) {
1014 candidates = ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1015 if (candidates != null) {
1016 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1020 if (position == 1) {
1023 foreach (Namespace n in namespace_using_table) {
1024 var a = n.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1028 if (candidates == null)
1031 candidates.AddRange (a);
1034 if (candidates != null)
1035 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1041 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1044 // Only simple names (no dots) will be looked up with this function
1046 FullNamedExpression resolved;
1047 for (NamespaceContainer container = this; container != null; container = container.Parent) {
1048 resolved = container.Lookup (name, arity, mode, loc);
1049 if (resolved != null || container.MemberName == null)
1052 var container_ns = container.ns.Parent;
1053 var mn = container.MemberName.Left;
1054 while (mn != null) {
1055 resolved = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1056 if (resolved != null)
1060 container_ns = container_ns.Parent;
1067 public override void GetCompletionStartingWith (string prefix, List<string> results)
1069 foreach (var un in Usings) {
1070 if (un.Alias != null)
1073 var name = un.NamespaceExpression.Name;
1074 if (name.StartsWith (prefix))
1079 IEnumerable<string> all = Enumerable.Empty<string> ();
1081 foreach (Namespace using_ns in namespace_using_table) {
1082 if (prefix.StartsWith (using_ns.Name)) {
1083 int ld = prefix.LastIndexOf ('.');
1085 string rest = prefix.Substring (ld + 1);
1087 all = all.Concat (using_ns.CompletionGetTypesStartingWith (rest));
1090 all = all.Concat (using_ns.CompletionGetTypesStartingWith (prefix));
1093 results.AddRange (all);
1095 base.GetCompletionStartingWith (prefix, results);
1100 // Looks-up a alias named @name in this and surrounding namespace declarations
1102 public FullNamedExpression LookupExternAlias (string name)
1104 if (aliases == null)
1107 UsingAliasNamespace uan;
1108 if (aliases.TryGetValue (name, out uan) && uan is UsingExternAlias)
1109 return uan.ResolvedExpression;
1115 // Looks-up a alias named @name in this and surrounding namespace declarations
1117 public override FullNamedExpression LookupNamespaceAlias (string name)
1119 for (NamespaceContainer n = this; n != null; n = n.Parent) {
1120 if (n.aliases == null)
1123 UsingAliasNamespace uan;
1124 if (n.aliases.TryGetValue (name, out uan))
1125 return uan.ResolvedExpression;
1131 FullNamedExpression Lookup (string name, int arity, LookupMode mode, Location loc)
1134 // Check whether it's in the namespace.
1136 FullNamedExpression fne = ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1141 if (aliases != null && arity == 0) {
1142 UsingAliasNamespace uan;
1143 if (aliases.TryGetValue (name, out uan)) {
1145 // TODO: Namespace has broken location
1146 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1147 Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
1148 Compiler.Report.Error (576, loc,
1149 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1150 GetSignatureForError (), name);
1153 return uan.ResolvedExpression;
1161 // Lookup can be called before the namespace is defined from different namespace using alias clause
1163 if (namespace_using_table == null) {
1164 DoDefineNamespace ();
1168 // Check using entries.
1170 FullNamedExpression match = null;
1171 foreach (Namespace using_ns in namespace_using_table) {
1173 // A using directive imports only types contained in the namespace, it
1174 // does not import any nested namespaces
1176 fne = using_ns.LookupType (this, name, arity, mode, loc);
1180 if (match == null) {
1185 // Prefer types over namespaces
1186 var texpr_fne = fne as TypeExpr;
1187 var texpr_match = match as TypeExpr;
1188 if (texpr_fne != null && texpr_match == null) {
1191 } else if (texpr_fne == null) {
1195 // It can be top level accessibility only
1196 var better = Namespace.IsImportedTypeOverride (Module, texpr_match.Type, texpr_fne.Type);
1197 if (better == null) {
1198 if (mode == LookupMode.Normal) {
1199 Compiler.Report.SymbolRelatedToPreviousError (texpr_match.Type);
1200 Compiler.Report.SymbolRelatedToPreviousError (texpr_fne.Type);
1201 Compiler.Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1202 name, texpr_match.GetSignatureForError (), texpr_fne.GetSignatureForError ());
1208 if (better == texpr_fne.Type)
1215 protected override void DefineNamespace ()
1217 if (namespace_using_table == null)
1218 DoDefineNamespace ();
1220 base.DefineNamespace ();
1223 void DoDefineNamespace ()
1225 namespace_using_table = empty_namespaces;
1227 if (clauses != null) {
1228 var list = new List<Namespace> (clauses.Count);
1229 bool post_process_using_aliases = false;
1231 for (int i = 0; i < clauses.Count; ++i) {
1232 var entry = clauses[i];
1234 if (entry.Alias != null) {
1235 if (aliases == null)
1236 aliases = new Dictionary<string, UsingAliasNamespace> ();
1239 // Aliases are not available when resolving using section
1240 // except extern aliases
1242 if (entry is UsingExternAlias) {
1243 entry.Define (this);
1244 if (entry.ResolvedExpression != null)
1245 aliases.Add (entry.Alias.Value, (UsingExternAlias) entry);
1247 clauses.RemoveAt (i--);
1249 post_process_using_aliases = true;
1255 entry.Define (this);
1258 // It's needed for repl only, when using clause cannot be resolved don't hold it in
1259 // global list which is resolved for each evaluation
1261 if (entry.ResolvedExpression == null) {
1262 clauses.RemoveAt (i--);
1266 Namespace using_ns = entry.ResolvedExpression as Namespace;
1267 if (using_ns == null)
1270 if (list.Contains (using_ns)) {
1271 // Ensure we don't report the warning multiple times in repl
1272 clauses.RemoveAt (i--);
1274 Compiler.Report.Warning (105, 3, entry.Location,
1275 "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
1277 list.Add (using_ns);
1281 namespace_using_table = list.ToArray ();
1283 if (post_process_using_aliases) {
1284 for (int i = 0; i < clauses.Count; ++i) {
1285 var entry = clauses[i];
1286 if (entry.Alias != null) {
1287 entry.Define (this);
1288 if (entry.ResolvedExpression != null) {
1289 aliases.Add (entry.Alias.Value, (UsingAliasNamespace) entry);
1292 clauses.RemoveAt (i--);
1299 public void EnableRedefinition ()
1302 namespace_using_table = null;
1305 internal override void GenerateDocComment (DocumentationBuilder builder)
1307 if (containers != null) {
1308 foreach (var tc in containers)
1309 tc.GenerateDocComment (builder);
1313 public override string GetSignatureForError ()
1315 return MemberName == null ? "global::" : base.GetSignatureForError ();
1318 public override void RemoveContainer (TypeContainer cont)
1320 base.RemoveContainer (cont);
1321 NS.RemoveContainer (cont);
1324 protected override bool VerifyClsCompliance ()
1326 if (Module.IsClsComplianceRequired ()) {
1327 if (MemberName != null && MemberName.Name[0] == '_') {
1328 Warning_IdentifierNotCompliant ();
1331 ns.VerifyClsCompliance ();
1339 public class UsingNamespace
1341 readonly ATypeNameExpression expr;
1342 readonly Location loc;
1343 protected FullNamedExpression resolved;
1345 public UsingNamespace (ATypeNameExpression expr, Location loc)
1353 public virtual SimpleMemberName Alias {
1359 public Location Location {
1365 public ATypeNameExpression NamespaceExpression {
1371 public FullNamedExpression ResolvedExpression {
1379 public string GetSignatureForError ()
1381 return expr.GetSignatureForError ();
1384 public virtual void Define (NamespaceContainer ctx)
1386 resolved = expr.ResolveAsTypeOrNamespace (ctx);
1387 var ns = resolved as Namespace;
1389 if (resolved != null) {
1390 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (resolved.Type);
1391 ctx.Module.Compiler.Report.Error (138, Location,
1392 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
1393 GetSignatureForError ());
1398 public override string ToString()
1400 return resolved.ToString();
1404 public class UsingExternAlias : UsingAliasNamespace
1406 public UsingExternAlias (SimpleMemberName alias, Location loc)
1407 : base (alias, null, loc)
1411 public override void Define (NamespaceContainer ctx)
1413 resolved = ctx.Module.GetRootNamespace (Alias.Value);
1414 if (resolved == null) {
1415 ctx.Module.Compiler.Report.Error (430, Location,
1416 "The extern alias `{0}' was not specified in -reference option",
1422 public class UsingAliasNamespace : UsingNamespace
1424 readonly SimpleMemberName alias;
1426 public struct AliasContext : IMemberContext
1428 readonly NamespaceContainer ns;
1430 public AliasContext (NamespaceContainer ns)
1435 public TypeSpec CurrentType {
1441 public TypeParameters CurrentTypeParameters {
1447 public MemberCore CurrentMemberDefinition {
1453 public bool IsObsolete {
1459 public bool IsUnsafe {
1461 throw new NotImplementedException ();
1465 public bool IsStatic {
1467 throw new NotImplementedException ();
1471 public ModuleContainer Module {
1477 public string GetSignatureForError ()
1479 throw new NotImplementedException ();
1482 public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
1487 public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1489 var fne = ns.NS.LookupTypeOrNamespace (ns, name, arity, mode, loc);
1494 // Only extern aliases are allowed in this context
1496 fne = ns.LookupExternAlias (name);
1497 if (fne != null || ns.MemberName == null)
1500 var container_ns = ns.NS.Parent;
1501 var mn = ns.MemberName.Left;
1502 while (mn != null) {
1503 fne = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1508 container_ns = container_ns.Parent;
1511 if (ns.Parent != null)
1512 return ns.Parent.LookupNamespaceOrType (name, arity, mode, loc);
1517 public FullNamedExpression LookupNamespaceAlias (string name)
1519 return ns.LookupNamespaceAlias (name);
1523 public UsingAliasNamespace (SimpleMemberName alias, ATypeNameExpression expr, Location loc)
1529 public override SimpleMemberName Alias {
1535 public override void Define (NamespaceContainer ctx)
1538 // The namespace-or-type-name of a using-alias-directive is resolved as if
1539 // the immediately containing compilation unit or namespace body had no
1540 // using-directives. A using-alias-directive may however be affected
1541 // by extern-alias-directives in the immediately containing compilation
1542 // unit or namespace body
1544 // We achieve that by introducing alias-context which redirect any local
1545 // namespace or type resolve calls to parent namespace
1547 resolved = NamespaceExpression.ResolveAsTypeOrNamespace (new AliasContext (ctx));