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)
27 this.alias_name = alias_name;
28 RegisterNamespace (this);
30 all_namespaces = new Dictionary<string, Namespace> ();
31 all_namespaces.Add ("", this);
40 public static void Error_GlobalNamespaceRedefined (Report report, Location loc)
42 report.Error (1681, loc, "The global extern alias cannot be redefined");
46 // For better error reporting where we try to guess missing using directive
48 public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
50 List<string> res = null;
52 foreach (var ns in all_namespaces) {
53 var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
56 res = new List<string> ();
66 // For better error reporting where compiler tries to guess missing using directive
68 public List<string> FindExtensionMethodNamespaces (IMemberContext ctx, string name, int arity)
70 List<string> res = null;
72 foreach (var ns in all_namespaces) {
73 var methods = ns.Value.LookupExtensionMethod (ctx, name, arity);
74 if (methods != null) {
76 res = new List<string> ();
85 public void RegisterNamespace (Namespace child)
88 all_namespaces.Add (child.Name, child);
91 public override string GetSignatureForError ()
93 return alias_name + "::";
97 public sealed class GlobalRootNamespace : RootNamespace
99 public GlobalRootNamespace ()
106 // Namespace cache for imported and compiled namespaces
108 public class Namespace
110 readonly Namespace parent;
112 protected Dictionary<string, Namespace> namespaces;
113 protected Dictionary<string, IList<TypeSpec>> types;
114 List<TypeSpec> extension_method_types;
115 Dictionary<string, TypeSpec> cached_types;
119 /// Constructor Takes the current namespace and the
120 /// name. This is bootstrapped with parent == null
123 public Namespace (Namespace parent, string name)
127 throw new ArgumentNullException ("name");
129 this.parent = parent;
131 string pname = parent != null ? parent.fullname : null;
136 fullname = pname + "." + name;
138 while (parent.parent != null)
139 parent = parent.parent;
141 var root = parent as RootNamespace;
143 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
145 root.RegisterNamespace (this);
148 protected Namespace ()
150 namespaces = new Dictionary<string, Namespace> ();
151 cached_types = new Dictionary<string, TypeSpec> ();
157 /// The qualified name of the current namespace
160 get { return fullname; }
164 /// The parent of this namespace, used by the parser to "Pop"
165 /// the current namespace declaration
167 public Namespace Parent {
168 get { return parent; }
173 public Namespace AddNamespace (MemberName name)
175 var ns_parent = name.Left == null ? this : AddNamespace (name.Left);
176 return ns_parent.TryAddNamespace (name.Basename);
179 Namespace TryAddNamespace (string name)
183 if (!namespaces.TryGetValue (name, out ns)) {
184 ns = new Namespace (this, name);
185 namespaces.Add (name, ns);
191 public bool TryGetNamespace (string name, out Namespace ns)
193 return namespaces.TryGetValue (name, out ns);
196 // TODO: Replace with CreateNamespace where MemberName is created for the method call
197 public Namespace GetNamespace (string name, bool create)
199 int pos = name.IndexOf ('.');
204 first = name.Substring (0, pos);
208 if (!namespaces.TryGetValue (first, out ns)) {
212 ns = new Namespace (this, first);
213 namespaces.Add (first, ns);
217 ns = ns.GetNamespace (name.Substring (pos + 1), create);
222 public IList<TypeSpec> GetAllTypes (string name)
224 IList<TypeSpec> found;
225 if (types == null || !types.TryGetValue (name, out found))
231 public virtual string GetSignatureForError ()
236 public TypeSpec LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
241 TypeSpec best = null;
242 if (arity == 0 && cached_types.TryGetValue (name, out best)) {
243 if (best != null || mode != LookupMode.IgnoreAccessibility)
247 IList<TypeSpec> found;
248 if (!types.TryGetValue (name, out found))
251 foreach (var ts in found) {
252 if (ts.Arity == arity) {
254 if ((ts.Modifiers & Modifiers.INTERNAL) != 0 && !ts.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly) && mode != LookupMode.IgnoreAccessibility)
261 if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) {
262 if (ts.Kind == MemberKind.MissingType)
265 if (best.Kind == MemberKind.MissingType) {
270 if (mode == LookupMode.Normal) {
271 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
272 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
273 ctx.Module.Compiler.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError ());
279 if (best.MemberDefinition.IsImported)
282 if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly))
285 if (mode != LookupMode.Normal)
288 if (ts.MemberDefinition.IsImported) {
289 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
290 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
293 ctx.Module.Compiler.Report.Warning (436, 2, loc,
294 "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
295 best.GetSignatureForError ());
299 // Lookup for the best candidate with the closest arity match
304 } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (best.Arity + arity)) {
310 // TODO MemberCache: Cache more
311 if (arity == 0 && mode == LookupMode.Normal)
312 cached_types.Add (name, best);
317 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
319 var texpr = LookupType (ctx, name, arity, mode, loc);
322 if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
324 return new NamespaceExpression (ns, loc);
326 if (mode != LookupMode.Probing) {
327 //ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
328 // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ns.loc, "");
329 ctx.Module.Compiler.Report.Warning (437, 2, loc,
330 "The type `{0}' conflicts with the imported namespace `{1}'. Using the definition found in the source file",
331 texpr.GetSignatureForError (), ns.GetSignatureForError ());
334 if (texpr.MemberDefinition.IsImported)
335 return new NamespaceExpression (ns, loc);
341 return new TypeExpression (texpr, loc);
345 // Completes types with the given `prefix'
347 public IEnumerable<string> CompletionGetTypesStartingWith (string prefix)
350 return Enumerable.Empty<string> ();
352 var res = from item in types
353 where item.Key.StartsWith (prefix) && item.Value.Any (l => (l.Modifiers & Modifiers.PUBLIC) != 0)
356 if (namespaces != null)
357 res = res.Concat (from item in namespaces where item.Key.StartsWith (prefix) select item.Key);
363 // Looks for extension method in this namespace
365 public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, string name, int arity)
367 if (extension_method_types == null)
370 List<MethodSpec> found = null;
371 for (int i = 0; i < extension_method_types.Count; ++i) {
372 var ts = extension_method_types[i];
375 // When the list was built we didn't know what members the type
378 if ((ts.Modifiers & Modifiers.METHOD_EXTENSION) == 0) {
379 if (extension_method_types.Count == 1) {
380 extension_method_types = null;
384 extension_method_types.RemoveAt (i--);
388 var res = ts.MemberCache.FindExtensionMethods (invocationContext, name, arity);
395 found.AddRange (res);
402 public void AddType (ModuleContainer module, TypeSpec ts)
405 types = new Dictionary<string, IList<TypeSpec>> (64);
408 if (ts.IsClass && ts.Arity == 0) {
409 var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial);
410 if (extension_method_allowed) {
411 if (extension_method_types == null)
412 extension_method_types = new List<TypeSpec> ();
414 extension_method_types.Add (ts);
419 IList<TypeSpec> existing;
420 if (types.TryGetValue (name, out existing)) {
421 TypeSpec better_type;
423 if (existing.Count == 1) {
425 if (ts.Arity == found.Arity) {
426 better_type = IsImportedTypeOverride (module, ts, found);
427 if (better_type == found)
430 if (better_type != null) {
431 existing [0] = better_type;
436 existing = new List<TypeSpec> ();
437 existing.Add (found);
438 types[name] = existing;
440 for (int i = 0; i < existing.Count; ++i) {
442 if (ts.Arity != found.Arity)
445 better_type = IsImportedTypeOverride (module, ts, found);
446 if (better_type == found)
449 if (better_type != null) {
450 existing.RemoveAt (i);
459 types.Add (name, new TypeSpec[] { ts });
464 // We import any types but in the situation there are same types
465 // but one has better visibility (either public or internal with friend)
466 // the less visible type is removed from the namespace cache
468 public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found)
470 var ts_accessible = (ts.Modifiers & Modifiers.PUBLIC) != 0 || ts.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
471 var found_accessible = (found.Modifiers & Modifiers.PUBLIC) != 0 || found.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
473 if (ts_accessible && !found_accessible)
476 // found is better always better for accessible or inaccessible ts
483 public void RemoveContainer (TypeContainer tc)
485 types.Remove (tc.Basename);
486 cached_types.Remove (tc.Basename);
489 public void SetBuiltinType (BuiltinTypeSpec pts)
491 var found = types[pts.Name];
492 cached_types.Remove (pts.Name);
493 if (found.Count == 1) {
494 types[pts.Name][0] = pts;
496 throw new NotImplementedException ();
500 public void VerifyClsCompliance ()
502 if (types == null || cls_checked)
507 // TODO: This is quite ugly way to check for CLS compliance at namespace level
509 var locase_types = new Dictionary<string, List<TypeSpec>> (StringComparer.OrdinalIgnoreCase);
510 foreach (var tgroup in types.Values) {
511 foreach (var tm in tgroup) {
512 if ((tm.Modifiers & Modifiers.PUBLIC) == 0 || !tm.IsCLSCompliant ())
515 List<TypeSpec> found;
516 if (!locase_types.TryGetValue (tm.Name, out found)) {
517 found = new List<TypeSpec> ();
518 locase_types.Add (tm.Name, found);
525 foreach (var locase in locase_types.Values) {
526 if (locase.Count < 2)
529 bool all_same = true;
530 foreach (var notcompliant in locase) {
531 all_same = notcompliant.Name == locase[0].Name;
539 TypeContainer compiled = null;
540 foreach (var notcompliant in locase) {
541 if (!notcompliant.MemberDefinition.IsImported) {
542 if (compiled != null)
543 compiled.Compiler.Report.SymbolRelatedToPreviousError (compiled);
545 compiled = notcompliant.MemberDefinition as TypeContainer;
547 compiled.Compiler.Report.SymbolRelatedToPreviousError (notcompliant);
551 compiled.Compiler.Report.Warning (3005, 1, compiled.Location,
552 "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
557 public class CompilationSourceFile : NamespaceContainer
559 readonly SourceFile file;
560 CompileUnitEntry comp_unit;
561 Dictionary<string, SourceFile> include_files;
562 Dictionary<string, bool> conditionals;
564 public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile)
567 this.file = sourceFile;
570 public CompilationSourceFile (ModuleContainer parent)
575 public CompileUnitEntry SymbolUnitEntry {
581 public string FileName {
587 public SourceFile SourceFile {
593 public void AddIncludeFile (SourceFile file)
595 if (file == this.file)
598 if (include_files == null)
599 include_files = new Dictionary<string, SourceFile> ();
601 if (!include_files.ContainsKey (file.FullPathName))
602 include_files.Add (file.FullPathName, file);
605 public void AddDefine (string value)
607 if (conditionals == null)
608 conditionals = new Dictionary<string, bool> (2);
610 conditionals[value] = true;
613 public void AddUndefine (string value)
615 if (conditionals == null)
616 conditionals = new Dictionary<string, bool> (2);
618 conditionals[value] = false;
621 public override void PrepareEmit ()
623 var sw = Module.DeclaringAssembly.SymbolWriter;
625 CreateUnitSymbolInfo (sw);
632 // Creates symbol file index in debug symbol file
634 void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
636 var si = file.CreateSymbolInfo (symwriter);
637 comp_unit = new CompileUnitEntry (symwriter, si);
639 if (include_files != null) {
640 foreach (SourceFile include in include_files.Values) {
641 si = include.CreateSymbolInfo (symwriter);
642 comp_unit.AddFile (si);
647 public bool IsConditionalDefined (string value)
649 if (conditionals != null) {
651 if (conditionals.TryGetValue (value, out res))
654 // When conditional was undefined
655 if (conditionals.ContainsKey (value))
659 return Compiler.Settings.IsConditionalSymbolDefined (value);
662 public override void Accept (StructuralVisitor visitor)
664 visitor.Visit (this);
670 // Namespace block as created by the parser
672 public class NamespaceContainer : TypeContainer, IMemberContext
674 static readonly Namespace[] empty_namespaces = new Namespace[0];
676 readonly Namespace ns;
678 public new readonly NamespaceContainer Parent;
680 List<UsingNamespace> clauses;
682 // Used by parsed to check for parser errors
683 public bool DeclarationFound;
685 Namespace[] namespace_using_table;
686 Dictionary<string, UsingAliasNamespace> aliases;
688 public NamespaceContainer (MemberName name, NamespaceContainer parent)
689 : base (parent, name, null, MemberKind.Namespace)
691 this.Parent = parent;
692 this.ns = parent.NS.AddNamespace (name);
694 containers = new List<TypeContainer> ();
697 protected NamespaceContainer (ModuleContainer parent)
698 : base (parent, null, null, MemberKind.Namespace)
700 ns = parent.GlobalRootNamespace;
701 containers = new List<TypeContainer> (2);
706 public override AttributeTargets AttributeTargets {
708 throw new NotSupportedException ();
712 public override string DocCommentHeader {
714 throw new NotSupportedException ();
718 public Namespace NS {
724 public List<UsingNamespace> Usings {
730 public override string[] ValidAttributeTargets {
732 throw new NotSupportedException ();
738 public void AddUsing (UsingNamespace un)
740 if (DeclarationFound){
741 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
745 clauses = new List<UsingNamespace> ();
750 public void AddUsing (UsingAliasNamespace un)
752 if (DeclarationFound){
753 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
759 void AddAlias (UsingAliasNamespace un)
761 if (clauses == null) {
762 clauses = new List<UsingNamespace> ();
764 foreach (var entry in clauses) {
765 var a = entry as UsingAliasNamespace;
766 if (a != null && a.Alias.Value == un.Alias.Value) {
767 Compiler.Report.SymbolRelatedToPreviousError (a.Location, "");
768 Compiler.Report.Error (1537, un.Location,
769 "The using alias `{0}' appeared previously in this namespace", un.Alias.Value);
777 public override void AddPartial (TypeDefinition next_part)
779 var existing = ns.LookupType (this, next_part.MemberName.Name, next_part.MemberName.Arity, LookupMode.Probing, Location.Null);
780 var td = existing != null ? existing.MemberDefinition as TypeDefinition : null;
781 AddPartial (next_part, td);
784 public override void AddTypeContainer (TypeContainer tc)
786 string name = tc.Basename;
788 var mn = tc.MemberName;
789 while (mn.Left != null) {
794 var names_container = Parent == null ? Module : (TypeContainer) this;
797 if (names_container.DefinedNames.TryGetValue (name, out mc)) {
798 if (tc is NamespaceContainer && mc is NamespaceContainer) {
799 AddTypeContainerMember (tc);
803 Report.SymbolRelatedToPreviousError (mc);
804 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (tc is ClassOrStruct || tc is Interface)) {
805 Error_MissingPartialModifier (tc);
807 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
808 GetSignatureForError (), mn.GetSignatureForError ());
811 names_container.DefinedNames.Add (name, tc);
813 var tdef = tc.PartialContainer;
816 // Same name conflict in different namespace containers
818 var conflict = ns.GetAllTypes (name);
819 if (conflict != null) {
820 foreach (var e in conflict) {
821 if (e.Arity == mn.Arity) {
822 mc = (MemberCore) e.MemberDefinition;
829 Report.SymbolRelatedToPreviousError (mc);
830 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
831 GetSignatureForError (), mn.GetSignatureForError ());
833 ns.AddType (Module, tdef.Definition);
838 base.AddTypeContainer (tc);
841 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
843 throw new NotSupportedException ();
846 public override void EmitContainer ()
848 VerifyClsCompliance ();
850 base.EmitContainer ();
853 public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
856 // Here we try to resume the search for extension method at the point
857 // where the last bunch of candidates was found. It's more tricky than
858 // it seems as we have to check both namespace containers and namespace
866 // <our first search found candidates in A.B.C.D
870 // In the example above namespace A.B.C.D, A.B.C and A.B have to be
871 // checked before we hit A.N1 using
873 ExtensionMethodCandidates candidates;
874 var container = this;
876 candidates = container.LookupExtensionMethodCandidates (invocationContext, name, arity, ref position);
877 if (candidates != null || container.MemberName == null)
880 var container_ns = container.ns.Parent;
881 var mn = container.MemberName.Left;
882 int already_checked = position - 2;
883 while (already_checked-- > 0) {
885 container_ns = container_ns.Parent;
891 var methods = container_ns.LookupExtensionMethod (invocationContext, name, arity);
892 if (methods != null) {
893 return new ExtensionMethodCandidates (invocationContext, methods, container, position);
897 container_ns = container_ns.Parent;
901 container = container.Parent;
902 } while (container != null);
907 ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, string name, int arity, ref int position)
909 List<MethodSpec> candidates = null;
914 candidates = ns.LookupExtensionMethod (invocationContext, name, arity);
915 if (candidates != null) {
916 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
923 foreach (Namespace n in namespace_using_table) {
924 var a = n.LookupExtensionMethod (invocationContext, name, arity);
928 if (candidates == null)
931 candidates.AddRange (a);
934 if (candidates != null)
935 return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
941 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
944 // Only simple names (no dots) will be looked up with this function
946 FullNamedExpression resolved;
947 for (NamespaceContainer container = this; container != null; container = container.Parent) {
948 resolved = container.Lookup (name, arity, mode, loc);
949 if (resolved != null || container.MemberName == null)
952 var container_ns = container.ns.Parent;
953 var mn = container.MemberName.Left;
955 resolved = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
956 if (resolved != null)
960 container_ns = container_ns.Parent;
967 public override void GetCompletionStartingWith (string prefix, List<string> results)
972 foreach (var un in Usings) {
973 if (un.Alias != null)
976 var name = un.NamespaceExpression.Name;
977 if (name.StartsWith (prefix))
982 IEnumerable<string> all = Enumerable.Empty<string> ();
984 foreach (Namespace using_ns in namespace_using_table) {
985 if (prefix.StartsWith (using_ns.Name)) {
986 int ld = prefix.LastIndexOf ('.');
988 string rest = prefix.Substring (ld + 1);
990 all = all.Concat (using_ns.CompletionGetTypesStartingWith (rest));
993 all = all.Concat (using_ns.CompletionGetTypesStartingWith (prefix));
996 results.AddRange (all);
998 base.GetCompletionStartingWith (prefix, results);
1003 // Looks-up a alias named @name in this and surrounding namespace declarations
1005 public FullNamedExpression LookupExternAlias (string name)
1007 if (aliases == null)
1010 UsingAliasNamespace uan;
1011 if (aliases.TryGetValue (name, out uan) && uan is UsingExternAlias)
1012 return uan.ResolvedExpression;
1018 // Looks-up a alias named @name in this and surrounding namespace declarations
1020 public override FullNamedExpression LookupNamespaceAlias (string name)
1022 for (NamespaceContainer n = this; n != null; n = n.Parent) {
1023 if (n.aliases == null)
1026 UsingAliasNamespace uan;
1027 if (n.aliases.TryGetValue (name, out uan))
1028 return uan.ResolvedExpression;
1034 FullNamedExpression Lookup (string name, int arity, LookupMode mode, Location loc)
1037 // Check whether it's in the namespace.
1039 FullNamedExpression fne = ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1044 if (aliases != null && arity == 0) {
1045 UsingAliasNamespace uan;
1046 if (aliases.TryGetValue (name, out uan)) {
1047 if (fne != null && mode != LookupMode.Probing) {
1048 // TODO: Namespace has broken location
1049 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1050 Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
1051 Compiler.Report.Error (576, loc,
1052 "Namespace `{0}' contains a definition with same name as alias `{1}'",
1053 GetSignatureForError (), name);
1056 return uan.ResolvedExpression;
1064 // Lookup can be called before the namespace is defined from different namespace using alias clause
1066 if (namespace_using_table == null) {
1067 DoDefineNamespace ();
1071 // Check using entries.
1073 FullNamedExpression match = null;
1074 foreach (Namespace using_ns in namespace_using_table) {
1076 // A using directive imports only types contained in the namespace, it
1077 // does not import any nested namespaces
1079 var t = using_ns.LookupType (this, name, arity, mode, loc);
1083 fne = new TypeExpression (t, loc);
1084 if (match == null) {
1089 // Prefer types over namespaces
1090 var texpr_fne = fne as TypeExpr;
1091 var texpr_match = match as TypeExpr;
1092 if (texpr_fne != null && texpr_match == null) {
1095 } else if (texpr_fne == null) {
1099 // It can be top level accessibility only
1100 var better = Namespace.IsImportedTypeOverride (Module, texpr_match.Type, texpr_fne.Type);
1101 if (better == null) {
1102 if (mode == LookupMode.Normal) {
1103 Compiler.Report.SymbolRelatedToPreviousError (texpr_match.Type);
1104 Compiler.Report.SymbolRelatedToPreviousError (texpr_fne.Type);
1105 Compiler.Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1106 name, texpr_match.GetSignatureForError (), texpr_fne.GetSignatureForError ());
1112 if (better == texpr_fne.Type)
1119 protected override void DefineNamespace ()
1121 if (namespace_using_table == null)
1122 DoDefineNamespace ();
1124 base.DefineNamespace ();
1127 void DoDefineNamespace ()
1129 namespace_using_table = empty_namespaces;
1131 if (clauses != null) {
1132 var list = new List<Namespace> (clauses.Count);
1133 bool post_process_using_aliases = false;
1135 for (int i = 0; i < clauses.Count; ++i) {
1136 var entry = clauses[i];
1138 if (entry.Alias != null) {
1139 if (aliases == null)
1140 aliases = new Dictionary<string, UsingAliasNamespace> ();
1143 // Aliases are not available when resolving using section
1144 // except extern aliases
1146 if (entry is UsingExternAlias) {
1147 entry.Define (this);
1148 if (entry.ResolvedExpression != null)
1149 aliases.Add (entry.Alias.Value, (UsingExternAlias) entry);
1151 clauses.RemoveAt (i--);
1153 post_process_using_aliases = true;
1159 entry.Define (this);
1162 // It's needed for repl only, when using clause cannot be resolved don't hold it in
1163 // global list which is resolved for each evaluation
1165 if (entry.ResolvedExpression == null) {
1166 clauses.RemoveAt (i--);
1170 var using_ns = entry.ResolvedExpression as NamespaceExpression;
1171 if (using_ns == null)
1174 if (list.Contains (using_ns.Namespace)) {
1175 // Ensure we don't report the warning multiple times in repl
1176 clauses.RemoveAt (i--);
1178 Compiler.Report.Warning (105, 3, entry.Location,
1179 "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
1181 list.Add (using_ns.Namespace);
1185 namespace_using_table = list.ToArray ();
1187 if (post_process_using_aliases) {
1188 for (int i = 0; i < clauses.Count; ++i) {
1189 var entry = clauses[i];
1190 if (entry.Alias != null) {
1191 entry.Define (this);
1192 if (entry.ResolvedExpression != null) {
1193 aliases.Add (entry.Alias.Value, (UsingAliasNamespace) entry);
1196 clauses.RemoveAt (i--);
1203 public void EnableRedefinition ()
1206 namespace_using_table = null;
1209 internal override void GenerateDocComment (DocumentationBuilder builder)
1211 if (containers != null) {
1212 foreach (var tc in containers)
1213 tc.GenerateDocComment (builder);
1217 public override string GetSignatureForError ()
1219 return MemberName == null ? "global::" : base.GetSignatureForError ();
1222 public override void RemoveContainer (TypeContainer cont)
1224 base.RemoveContainer (cont);
1225 NS.RemoveContainer (cont);
1228 protected override bool VerifyClsCompliance ()
1230 if (Module.IsClsComplianceRequired ()) {
1231 if (MemberName != null && MemberName.Name[0] == '_') {
1232 Warning_IdentifierNotCompliant ();
1235 ns.VerifyClsCompliance ();
1242 public override void Accept (StructuralVisitor visitor)
1244 visitor.Visit (this);
1248 public class UsingNamespace
1250 readonly ATypeNameExpression expr;
1251 readonly Location loc;
1252 protected FullNamedExpression resolved;
1254 public UsingNamespace (ATypeNameExpression expr, Location loc)
1262 public virtual SimpleMemberName Alias {
1268 public Location Location {
1274 public ATypeNameExpression NamespaceExpression {
1280 public FullNamedExpression ResolvedExpression {
1288 public string GetSignatureForError ()
1290 return expr.GetSignatureForError ();
1293 public virtual void Define (NamespaceContainer ctx)
1295 resolved = expr.ResolveAsTypeOrNamespace (ctx);
1296 var ns = resolved as NamespaceExpression;
1298 if (resolved != null) {
1299 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (resolved.Type);
1300 ctx.Module.Compiler.Report.Error (138, Location,
1301 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
1302 GetSignatureForError ());
1307 public override string ToString()
1309 return resolved.ToString();
1313 public class UsingExternAlias : UsingAliasNamespace
1315 public UsingExternAlias (SimpleMemberName alias, Location loc)
1316 : base (alias, null, loc)
1320 public override void Define (NamespaceContainer ctx)
1322 var ns = ctx.Module.GetRootNamespace (Alias.Value);
1324 ctx.Module.Compiler.Report.Error (430, Location,
1325 "The extern alias `{0}' was not specified in -reference option",
1330 resolved = new NamespaceExpression (ns, Location);
1334 public class UsingAliasNamespace : UsingNamespace
1336 readonly SimpleMemberName alias;
1338 public struct AliasContext : IMemberContext
1340 readonly NamespaceContainer ns;
1342 public AliasContext (NamespaceContainer ns)
1347 public TypeSpec CurrentType {
1353 public TypeParameters CurrentTypeParameters {
1359 public MemberCore CurrentMemberDefinition {
1365 public bool IsObsolete {
1371 public bool IsUnsafe {
1373 throw new NotImplementedException ();
1377 public bool IsStatic {
1379 throw new NotImplementedException ();
1383 public ModuleContainer Module {
1389 public string GetSignatureForError ()
1391 throw new NotImplementedException ();
1394 public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
1399 public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1401 var fne = ns.NS.LookupTypeOrNamespace (ns, name, arity, mode, loc);
1406 // Only extern aliases are allowed in this context
1408 fne = ns.LookupExternAlias (name);
1409 if (fne != null || ns.MemberName == null)
1412 var container_ns = ns.NS.Parent;
1413 var mn = ns.MemberName.Left;
1414 while (mn != null) {
1415 fne = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1420 container_ns = container_ns.Parent;
1423 if (ns.Parent != null)
1424 return ns.Parent.LookupNamespaceOrType (name, arity, mode, loc);
1429 public FullNamedExpression LookupNamespaceAlias (string name)
1431 return ns.LookupNamespaceAlias (name);
1435 public UsingAliasNamespace (SimpleMemberName alias, ATypeNameExpression expr, Location loc)
1441 public override SimpleMemberName Alias {
1447 public override void Define (NamespaceContainer ctx)
1450 // The namespace-or-type-name of a using-alias-directive is resolved as if
1451 // the immediately containing compilation unit or namespace body had no
1452 // using-directives. A using-alias-directive may however be affected
1453 // by extern-alias-directives in the immediately containing compilation
1454 // unit or namespace body
1456 // We achieve that by introducing alias-context which redirect any local
1457 // namespace or type resolve calls to parent namespace
1459 resolved = NamespaceExpression.ResolveAsTypeOrNamespace (new AliasContext (ctx));