[io-layer] add URLs for some ximian bug numbers in sockets.cs
[mono.git] / mcs / mcs / namespace.cs
1 //
2 // namespace.cs: Tracks namespaces
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // Copyright 2001 Ximian, Inc.
9 // Copyright 2003-2008 Novell, Inc.
10 // Copyright 2011 Xamarin Inc
11 //
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using Mono.CompilerServices.SymbolWriter;
16
17 namespace Mono.CSharp {
18
19         public class RootNamespace : Namespace {
20
21                 readonly string alias_name;
22                 readonly Dictionary<string, Namespace> all_namespaces;
23
24                 public RootNamespace (string alias_name)
25                         : base (null, String.Empty)
26                 {
27                         this.alias_name = alias_name;
28
29                         all_namespaces = new Dictionary<string, Namespace> ();
30                         all_namespaces.Add ("", this);
31                 }
32
33                 public string Alias {
34                         get {
35                                 return alias_name;
36                         }
37                 }
38
39                 public static void Error_GlobalNamespaceRedefined (Report report, Location loc)
40                 {
41                         report.Error (1681, loc, "The global extern alias cannot be redefined");
42                 }
43
44                 //
45                 // For better error reporting where we try to guess missing using directive
46                 //
47                 public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
48                 {
49                         List<string> res = null;
50
51                         foreach (var ns in all_namespaces) {
52                                 var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
53                                 if (type != null) {
54                                         if (res == null)
55                                                 res = new List<string> ();
56
57                                         res.Add (ns.Key);
58                                 }
59                         }
60
61                         return res;
62                 }
63
64                 //
65                 // For better error reporting where compiler tries to guess missing using directive
66                 //
67                 public List<string> FindExtensionMethodNamespaces (IMemberContext ctx, TypeSpec extensionType, string name, int arity)
68                 {
69                         List<string> res = null;
70
71                         foreach (var ns in all_namespaces) {
72                                 var methods = ns.Value.LookupExtensionMethod (ctx, extensionType, name, arity);
73                                 if (methods != null) {
74                                         if (res == null)
75                                                 res = new List<string> ();
76
77                                         res.Add (ns.Key);
78                                 }
79                         }
80
81                         return res;
82                 }
83
84                 public void RegisterNamespace (Namespace child)
85                 {
86                         if (child != this)
87                                 all_namespaces.Add (child.Name, child);
88                 }
89
90                 public bool IsNamespace (string name)
91                 {
92                         return all_namespaces.ContainsKey (name);
93                 }
94
95                 protected void RegisterNamespace (string dotted_name)
96                 {
97                         if (dotted_name != null && dotted_name.Length != 0 && ! IsNamespace (dotted_name))
98                                 GetNamespace (dotted_name, true);
99                 }
100
101                 public override string GetSignatureForError ()
102                 {
103                         return alias_name + "::";
104                 }
105         }
106
107         public class GlobalRootNamespace : RootNamespace
108         {
109                 public GlobalRootNamespace ()
110                         : base ("global")
111                 {
112                 }
113         }
114
115         //
116         // Namespace cache for imported and compiled namespaces
117         //
118         // This is an Expression to allow it to be referenced in the
119         // compiler parse/intermediate tree during name resolution.
120         //
121         public class Namespace : FullNamedExpression
122         {
123                 Namespace parent;
124                 string fullname;
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;
129                 RootNamespace root;
130                 bool cls_checked;
131
132                 public readonly MemberName MemberName;
133
134                 /// <summary>
135                 ///   Constructor Takes the current namespace and the
136                 ///   name.  This is bootstrapped with parent == null
137                 ///   and name = ""
138                 /// </summary>
139                 public Namespace (Namespace parent, string name)
140                 {
141                         // Expression members.
142                         this.eclass = ExprClass.Namespace;
143                         this.Type = InternalType.Namespace;
144                         this.loc = Location.Null;
145
146                         this.parent = parent;
147
148                         if (parent != null)
149                                 this.root = parent.root;
150                         else
151                                 this.root = this as RootNamespace;
152
153                         if (this.root == null)
154                                 throw new InternalErrorException ("Root namespaces must be created using RootNamespace");
155                         
156                         string pname = parent != null ? parent.fullname : "";
157                                 
158                         if (pname == "")
159                                 fullname = name;
160                         else
161                                 fullname = parent.fullname + "." + name;
162
163                         if (fullname == null)
164                                 throw new InternalErrorException ("Namespace has a null fullname");
165
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;
170                         else
171                                 MemberName = new MemberName (name, Location.Null);
172
173                         namespaces = new Dictionary<string, Namespace> ();
174                         cached_types = new Dictionary<string, TypeExpr> ();
175
176                         root.RegisterNamespace (this);
177                 }
178
179                 #region Properties
180
181                 /// <summary>
182                 ///   The qualified name of the current namespace
183                 /// </summary>
184                 public string Name {
185                         get { return fullname; }
186                 }
187
188                 /// <summary>
189                 ///   The parent of this namespace, used by the parser to "Pop"
190                 ///   the current namespace declaration
191                 /// </summary>
192                 public Namespace Parent {
193                         get { return parent; }
194                 }
195
196                 #endregion
197
198                 protected override Expression DoResolve (ResolveContext ec)
199                 {
200                         return this;
201                 }
202
203                 public void Error_NamespaceDoesNotExist (IMemberContext ctx, string name, int arity, Location loc)
204                 {
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);
209                                 return;
210                         }
211
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);
215                                 return;
216                         }
217
218                         Namespace ns;
219                         if (arity > 0 && namespaces.TryGetValue (name, out ns)) {
220                                 ns.Error_TypeArgumentsCannotBeUsed (ctx, null, arity, loc);
221                                 return;
222                         }
223
224                         string assembly = null;
225                         string possible_name = fullname + "." + name;
226
227                         // Only assembly unique name should be added
228                         switch (possible_name) {
229                         case "System.Drawing":
230                         case "System.Web.Services":
231                         case "System.Web":
232                         case "System.Data":
233                         case "System.Configuration":
234                         case "System.Data.Services":
235                         case "System.DirectoryServices":
236                         case "System.Json":
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":
244                         case "System.Xml":
245                                 assembly = possible_name;
246                                 break;
247
248                         case "System.Linq":
249                         case "System.Linq.Expressions":
250                                 assembly = "System.Core";
251                                 break;
252
253                         case "System.Windows.Forms":
254                         case "System.Windows.Forms.Layout":
255                                 assembly = "System.Windows.Name";
256                                 break;
257                         }
258
259                         assembly = assembly == null ? "an" : "`" + assembly + "'";
260
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?",
264                                         name, assembly);
265                         } else {
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);
269                         }
270                 }
271
272                 public override string GetSignatureForError ()
273                 {
274                         return fullname;
275                 }
276
277                 public Namespace AddNamespace (MemberName name)
278                 {
279                         Namespace ns_parent;
280                         if (name.Left != null) {
281                                 if (parent != null)
282                                         ns_parent = parent.AddNamespace (name.Left);
283                                 else
284                                         ns_parent = AddNamespace (name.Left);
285                         } else {
286                                 ns_parent = this;
287                         }
288
289                         return ns_parent.TryAddNamespace (name.Basename);
290                 }
291
292                 Namespace TryAddNamespace (string name)
293                 {
294                         Namespace ns;
295
296                         if (!namespaces.TryGetValue (name, out ns)) {
297                                 ns = new Namespace (this, name);
298                                 namespaces.Add (name, ns);
299                         }
300
301                         return ns;
302                 }
303
304                 // TODO: Replace with CreateNamespace where MemberName is created for the method call
305                 public Namespace GetNamespace (string name, bool create)
306                 {
307                         int pos = name.IndexOf ('.');
308
309                         Namespace ns;
310                         string first;
311                         if (pos >= 0)
312                                 first = name.Substring (0, pos);
313                         else
314                                 first = name;
315
316                         if (!namespaces.TryGetValue (first, out ns)) {
317                                 if (!create)
318                                         return null;
319
320                                 ns = new Namespace (this, first);
321                                 namespaces.Add (first, ns);
322                         }
323
324                         if (pos >= 0)
325                                 ns = ns.GetNamespace (name.Substring (pos + 1), create);
326
327                         return ns;
328                 }
329
330                 public IList<TypeSpec> GetAllTypes (string name)
331                 {
332                         IList<TypeSpec> found;
333                         if (types == null || !types.TryGetValue (name, out found))
334                                 return null;
335
336                         return found;
337                 }
338
339                 public TypeExpr LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
340                 {
341                         if (types == null)
342                                 return null;
343
344                         TypeExpr te;
345                         if (arity == 0 && cached_types.TryGetValue (name, out te))
346                                 return te;
347
348                         IList<TypeSpec> found;
349                         if (!types.TryGetValue (name, out found))
350                                 return null;
351
352                         TypeSpec best = null;
353                         foreach (var ts in found) {
354                                 if (ts.Arity == arity) {
355                                         if (best == null) {
356                                                 if ((ts.Modifiers & Modifiers.INTERNAL) != 0 && !ts.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly) && mode != LookupMode.IgnoreAccessibility)
357                                                         continue;
358
359                                                 best = ts;
360                                                 continue;
361                                         }
362
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 ());
368                                                 }
369                                                 break;
370                                         }
371
372                                         if (best.MemberDefinition.IsImported)
373                                                 best = ts;
374
375                                         if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly))
376                                                 continue;
377
378                                         if (mode != LookupMode.Normal)
379                                                 continue;
380
381                                         if (ts.MemberDefinition.IsImported) {
382                                                 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
383                                                 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
384                                         }
385
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 ());
389                                 }
390
391                                 //
392                                 // Lookup for the best candidate with the closest arity match
393                                 //
394                                 if (arity < 0) {
395                                         if (best == null) {
396                                                 best = ts;
397                                         } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (best.Arity + arity)) {
398                                                 best = ts;
399                                         }
400                                 }
401                         }
402
403                         if (best == null)
404                                 return null;
405
406                         te = new TypeExpression (best, Location.Null);
407
408                         // TODO MemberCache: Cache more
409                         if (arity == 0 && mode == LookupMode.Normal)
410                                 cached_types.Add (name, te);
411
412                         return te;
413                 }
414
415                 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
416                 {
417                         var texpr = LookupType (ctx, name, arity, mode, loc);
418
419                         Namespace ns;
420                         if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
421                                 if (texpr == null)
422                                         return ns;
423
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 ());
430                                 }
431
432                                 if (texpr.Type.MemberDefinition.IsImported)
433                                         return ns;
434                         }
435
436                         return texpr;
437                 }
438
439                 //
440                 // Completes types with the given `prefix'
441                 //
442                 public IEnumerable<string> CompletionGetTypesStartingWith (string prefix)
443                 {
444                         if (types == null)
445                                 return Enumerable.Empty<string> ();
446
447                         var res = from item in types
448                                           where item.Key.StartsWith (prefix) && item.Value.Any (l => (l.Modifiers & Modifiers.PUBLIC) != 0)
449                                           select item.Key;
450
451                         if (namespaces != null)
452                                 res = res.Concat (from item in namespaces where item.Key.StartsWith (prefix) select item.Key);
453
454                         return res;
455                 }
456
457                 // 
458                 // Looks for extension method in this namespace
459                 //
460                 public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
461                 {
462                         if (extension_method_types == null)
463                                 return null;
464
465                         List<MethodSpec> found = null;
466                         for (int i = 0; i < extension_method_types.Count; ++i) {
467                                 var ts = extension_method_types[i];
468
469                                 //
470                                 // When the list was built we didn't know what members the type
471                                 // contains
472                                 //
473                                 if ((ts.Modifiers & Modifiers.METHOD_EXTENSION) == 0) {
474                                         if (extension_method_types.Count == 1) {
475                                                 extension_method_types = null;
476                                                 return found;
477                                         }
478
479                                         extension_method_types.RemoveAt (i--);
480                                         continue;
481                                 }
482
483                                 var res = ts.MemberCache.FindExtensionMethods (invocationContext, extensionType, name, arity);
484                                 if (res == null)
485                                         continue;
486
487                                 if (found == null) {
488                                         found = res;
489                                 } else {
490                                         found.AddRange (res);
491                                 }
492                         }
493
494                         return found;
495                 }
496
497                 public void AddType (ModuleContainer module, TypeSpec ts)
498                 {
499                         if (types == null) {
500                                 types = new Dictionary<string, IList<TypeSpec>> (64);
501                         }
502
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> ();
508
509                                         extension_method_types.Add (ts);
510                                 }
511                         }
512
513                         var name = ts.Name;
514                         IList<TypeSpec> existing;
515                         if (types.TryGetValue (name, out existing)) {
516                                 TypeSpec better_type;
517                                 TypeSpec found;
518                                 if (existing.Count == 1) {
519                                         found = existing[0];
520                                         if (ts.Arity == found.Arity) {
521                                                 better_type = IsImportedTypeOverride (module, ts, found);
522                                                 if (better_type == found)
523                                                         return;
524
525                                                 if (better_type != null) {
526                                                         existing [0] = better_type;
527                                                         return;
528                                                 }
529                                         }
530
531                                         existing = new List<TypeSpec> ();
532                                         existing.Add (found);
533                                         types[name] = existing;
534                                 } else {
535                                         for (int i = 0; i < existing.Count; ++i) {
536                                                 found = existing[i];
537                                                 if (ts.Arity != found.Arity)
538                                                         continue;
539
540                                                 better_type = IsImportedTypeOverride (module, ts, found);
541                                                 if (better_type == found)
542                                                         return;
543
544                                                 if (better_type != null) {
545                                                         existing.RemoveAt (i);
546                                                         --i;
547                                                         continue;
548                                                 }
549                                         }
550                                 }
551
552                                 existing.Add (ts);
553                         } else {
554                                 types.Add (name, new TypeSpec[] { ts });
555                         }
556                 }
557
558                 //
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
562                 //
563                 public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found)
564                 {
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);
567
568                         if (ts_accessible && !found_accessible)
569                                 return ts;
570
571                         // found is better always better for accessible or inaccessible ts
572                         if (!ts_accessible)
573                                 return found;
574
575                         return null;
576                 }
577
578                 public void RemoveContainer (TypeContainer tc)
579                 {
580                         types.Remove (tc.Basename);
581                         cached_types.Remove (tc.Basename);
582                 }
583
584                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
585                 {
586                         return this;
587                 }
588
589                 public void SetBuiltinType (BuiltinTypeSpec pts)
590                 {
591                         var found = types[pts.Name];
592                         cached_types.Remove (pts.Name);
593                         if (found.Count == 1) {
594                                 types[pts.Name][0] = pts;
595                         } else {
596                                 throw new NotImplementedException ();
597                         }
598                 }
599
600                 public void VerifyClsCompliance ()
601                 {
602                         if (types == null || cls_checked)
603                                 return;
604
605                         cls_checked = true;
606
607                         // TODO: This is quite ugly way to check for CLS compliance at namespace level
608
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 ())
613                                                 continue;
614
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);
619                                         }
620
621                                         found.Add (tm);
622                                 }
623                         }
624
625                         foreach (var locase in locase_types.Values) {
626                                 if (locase.Count < 2)
627                                         continue;
628
629                                 bool all_same = true;
630                                 foreach (var notcompliant in locase) {
631                                         all_same = notcompliant.Name == locase[0].Name;
632                                         if (!all_same)
633                                                 break;
634                                 }
635
636                                 if (all_same)
637                                         continue;
638
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);
644
645                                                 compiled = notcompliant.MemberDefinition as TypeContainer;
646                                         } else {
647                                                 compiled.Compiler.Report.SymbolRelatedToPreviousError (notcompliant);
648                                         }
649                                 }
650
651                                 compiled.Compiler.Report.Warning (3005, 1, compiled.Location,
652                                         "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
653                         }
654                 }
655
656                 public override string ToString ()
657                 {
658                         return Name;
659                 }
660         }
661
662         public class CompilationSourceFile : NamespaceContainer
663         {
664                 readonly SourceFile file;
665                 CompileUnitEntry comp_unit;
666                 Dictionary<string, SourceFile> include_files;
667                 Dictionary<string, bool> conditionals;
668
669                 public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile)
670                         : this (parent)
671                 {
672                         this.file = sourceFile;
673                 }
674
675                 public CompilationSourceFile (ModuleContainer parent)
676                         : base (parent)
677                 {
678                 }
679
680                 public CompileUnitEntry SymbolUnitEntry {
681                         get {
682                                 return comp_unit;
683                         }
684                 }
685
686                 public string FileName {
687                         get {
688                                 return file.Name;
689                         }
690                 }
691
692                 public SourceFile SourceFile {
693                         get {
694                                 return file;
695                         }
696                 }
697
698                 public void AddIncludeFile (SourceFile file)
699                 {
700                         if (file == this.file)
701                                 return;
702
703                         if (include_files == null)
704                                 include_files = new Dictionary<string, SourceFile> ();
705
706                         if (!include_files.ContainsKey (file.FullPathName))
707                                 include_files.Add (file.FullPathName, file);
708                 }
709
710                 public void AddDefine (string value)
711                 {
712                         if (conditionals == null)
713                                 conditionals = new Dictionary<string, bool> (2);
714
715                         conditionals[value] = true;
716                 }
717
718                 public void AddUndefine (string value)
719                 {
720                         if (conditionals == null)
721                                 conditionals = new Dictionary<string, bool> (2);
722
723                         conditionals[value] = false;
724                 }
725
726                 public override void PrepareEmit ()
727                 {
728                         var sw = Module.DeclaringAssembly.SymbolWriter;
729                         if (sw != null) {
730                                 CreateUnitSymbolInfo (sw);
731                         }
732
733                         base.PrepareEmit ();
734                 }
735
736                 //
737                 // Creates symbol file index in debug symbol file
738                 //
739                 void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
740                 {
741                         var si = file.CreateSymbolInfo (symwriter);
742                         comp_unit = new CompileUnitEntry (symwriter, si);;
743
744                         if (include_files != null) {
745                                 foreach (SourceFile include in include_files.Values) {
746                                         si = include.CreateSymbolInfo (symwriter);
747                                         comp_unit.AddFile (si);
748                                 }
749                         }
750                 }
751
752                 public bool IsConditionalDefined (string value)
753                 {
754                         if (conditionals != null) {
755                                 bool res;
756                                 if (conditionals.TryGetValue (value, out res))
757                                         return res;
758
759                                 // When conditional was undefined
760                                 if (conditionals.ContainsKey (value))
761                                         return false;
762                         }
763
764                         return Compiler.Settings.IsConditionalSymbolDefined (value);
765                 }
766         }
767
768
769         //
770         // Namespace block as created by the parser
771         //
772         public class NamespaceContainer : TypeContainer, IMemberContext
773         {
774                 static readonly Namespace[] empty_namespaces = new Namespace[0];
775
776                 readonly Namespace ns;
777
778                 public new readonly NamespaceContainer Parent;
779
780                 List<UsingNamespace> clauses;
781
782                 // Used by parsed to check for parser errors
783                 public bool DeclarationFound;
784
785                 Namespace[] namespace_using_table;
786                 Dictionary<string, UsingAliasNamespace> aliases;
787
788                 public NamespaceContainer (MemberName name, NamespaceContainer parent)
789                         : base (parent, name, null, MemberKind.Namespace)
790                 {
791                         this.Parent = parent;
792                         this.ns = parent.NS.AddNamespace (name);
793
794                         containers = new List<TypeContainer> ();
795                 }
796
797                 protected NamespaceContainer (ModuleContainer parent)
798                         : base (parent, null, null, MemberKind.Namespace)
799                 {
800                         ns = parent.GlobalRootNamespace;
801                         containers = new List<TypeContainer> (2);
802                 }
803
804                 #region Properties
805
806                 public override AttributeTargets AttributeTargets {
807                         get {
808                                 throw new NotSupportedException ();
809                         }
810                 }
811
812                 public override string DocCommentHeader {
813                         get {
814                                 throw new NotSupportedException ();
815                         }
816                 }
817
818                 public Namespace NS {
819                         get {
820                                 return ns;
821                         }
822                 }
823
824                 public List<UsingNamespace> Usings {
825                         get {
826                                 return clauses;
827                         }
828                 }
829
830                 public override string[] ValidAttributeTargets {
831                         get {
832                                 throw new NotSupportedException ();
833                         }
834                 }
835
836                 #endregion
837
838                 public void AddUsing (UsingNamespace un)
839                 {
840                         if (DeclarationFound){
841                                 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
842                         }
843
844                         if (clauses == null)
845                                 clauses = new List<UsingNamespace> ();
846
847                         clauses.Add (un);
848                 }
849
850                 public void AddUsing (UsingAliasNamespace un)
851                 {
852                         if (DeclarationFound){
853                                 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
854                         }
855
856                         AddAlias (un);
857                 }
858
859                 void AddAlias (UsingAliasNamespace un)
860                 {
861                         if (clauses == null) {
862                                 clauses = new List<UsingNamespace> ();
863                         } else {
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);
870                                         }
871                                 }
872                         }
873
874                         clauses.Add (un);
875                 }
876
877                 public override void AddPartial (TypeDefinition next_part)
878                 {
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);
882                 }
883
884                 public override void AddTypeContainer (TypeContainer tc)
885                 {
886                         string name = tc.Basename;
887
888                         var mn = tc.MemberName;
889                         while (mn.Left != null) {
890                                 mn = mn.Left;
891                                 name = mn.Name;
892                         }
893
894                         var names_container = Parent == null ? Module : (TypeContainer) this;
895
896                         MemberCore mc;
897                         if (names_container.DefinedNames.TryGetValue (name, out mc)) {
898                                 if (tc is NamespaceContainer && mc is NamespaceContainer) {
899                                         containers.Add (tc);
900                                         return;
901                                 }
902
903                                 Report.SymbolRelatedToPreviousError (mc);
904                                 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (tc is ClassOrStruct || tc is Interface)) {
905                                         Error_MissingPartialModifier (tc);
906                                 } else {
907                                         Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
908                                                 GetSignatureForError (), mn.GetSignatureForError ());
909                                 }
910                         } else {
911                                 names_container.DefinedNames.Add (name, tc);
912
913                                 var tdef = tc.PartialContainer;
914                                 if (tdef != null) {
915                                         //
916                                         // Same name conflict in different namespace containers
917                                         //
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;
923                                                                 break;
924                                                         }
925                                                 }
926                                         }
927
928                                         if (mc != null) {
929                                                 Report.SymbolRelatedToPreviousError (mc);
930                                                 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
931                                                         GetSignatureForError (), mn.GetSignatureForError ());
932                                         } else {
933                                                 ns.AddType (Module, tdef.Definition);
934                                         }
935                                 }
936                         }
937
938                         base.AddTypeContainer (tc);
939                 }
940
941                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
942                 {
943                         throw new NotSupportedException ();
944                 }
945
946                 public override void EmitContainer ()
947                 {
948                         VerifyClsCompliance ();
949
950                         base.EmitContainer ();
951                 }
952
953                 public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
954                 {
955                         //
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
959                         // in correct order.
960                         //
961                         // Consider:
962                         // 
963                         // namespace A {
964                         //      using N1;
965                         //  namespace B.C.D {
966                         //              <our first search found candidates in A.B.C.D
967                         //  }
968                         // }
969                         //
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
972                         //
973                         ExtensionMethodCandidates candidates;
974                         var container = this;
975                         do {
976                                 candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
977                                 if (candidates != null || container.MemberName == null)
978                                         return candidates;
979
980                                 var container_ns = container.ns.Parent;
981                                 var mn = container.MemberName.Left;
982                                 int already_checked = position - 2;
983                                 while (already_checked-- > 0) {
984                                         mn = mn.Left;
985                                         container_ns = container_ns.Parent;
986                                 }
987
988                                 while (mn != null) {
989                                         ++position;
990
991                                         var methods = container_ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
992                                         if (methods != null) {
993                                                 return new ExtensionMethodCandidates (invocationContext, methods, container, position);
994                                         }
995
996                                         mn = mn.Left;
997                                         container_ns = container_ns.Parent;
998                                 }
999
1000                                 position = 0;
1001                                 container = container.Parent;
1002                         } while (container != null);
1003
1004                         return null;
1005                 }
1006
1007                 ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, ref int position)
1008                 {
1009                         List<MethodSpec> candidates = null;
1010
1011                         if (position == 0) {
1012                                 ++position;
1013
1014                                 candidates = ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1015                                 if (candidates != null) {
1016                                         return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1017                                 }
1018                         }
1019
1020                         if (position == 1) {
1021                                 ++position;
1022
1023                                 foreach (Namespace n in namespace_using_table) {
1024                                         var a = n.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1025                                         if (a == null)
1026                                                 continue;
1027
1028                                         if (candidates == null)
1029                                                 candidates = a;
1030                                         else
1031                                                 candidates.AddRange (a);
1032                                 }
1033
1034                                 if (candidates != null)
1035                                         return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1036                         }
1037
1038                         return null;
1039                 }
1040
1041                 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1042                 {
1043                         //
1044                         // Only simple names (no dots) will be looked up with this function
1045                         //
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)
1050                                         return resolved;
1051
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)
1057                                                 return resolved;
1058
1059                                         mn = mn.Left;
1060                                         container_ns = container_ns.Parent;
1061                                 }
1062                         }
1063
1064                         return null;
1065                 }
1066
1067                 public override void GetCompletionStartingWith (string prefix, List<string> results)
1068                 {
1069                         foreach (var un in Usings) {
1070                                 if (un.Alias != null)
1071                                         continue;
1072
1073                                 var name = un.NamespaceExpression.Name;
1074                                 if (name.StartsWith (prefix))
1075                                         results.Add (name);
1076                         }
1077
1078
1079                         IEnumerable<string> all = Enumerable.Empty<string> ();
1080
1081                         foreach (Namespace using_ns in namespace_using_table) {
1082                                 if (prefix.StartsWith (using_ns.Name)) {
1083                                         int ld = prefix.LastIndexOf ('.');
1084                                         if (ld != -1) {
1085                                                 string rest = prefix.Substring (ld + 1);
1086
1087                                                 all = all.Concat (using_ns.CompletionGetTypesStartingWith (rest));
1088                                         }
1089                                 }
1090                                 all = all.Concat (using_ns.CompletionGetTypesStartingWith (prefix));
1091                         }
1092
1093                         results.AddRange (all);
1094
1095                         base.GetCompletionStartingWith (prefix, results);
1096                 }
1097
1098                 
1099                 //
1100                 // Looks-up a alias named @name in this and surrounding namespace declarations
1101                 //
1102                 public FullNamedExpression LookupExternAlias (string name)
1103                 {
1104                         if (aliases == null)
1105                                 return null;
1106
1107                         UsingAliasNamespace uan;
1108                         if (aliases.TryGetValue (name, out uan) && uan is UsingExternAlias)
1109                                 return uan.ResolvedExpression;
1110
1111                         return null;
1112                 }
1113                 
1114                 //
1115                 // Looks-up a alias named @name in this and surrounding namespace declarations
1116                 //
1117                 public override FullNamedExpression LookupNamespaceAlias (string name)
1118                 {
1119                         for (NamespaceContainer n = this; n != null; n = n.Parent) {
1120                                 if (n.aliases == null)
1121                                         continue;
1122
1123                                 UsingAliasNamespace uan;
1124                                 if (n.aliases.TryGetValue (name, out uan))
1125                                         return uan.ResolvedExpression;
1126                         }
1127
1128                         return null;
1129                 }
1130
1131                 FullNamedExpression Lookup (string name, int arity, LookupMode mode, Location loc)
1132                 {
1133                         //
1134                         // Check whether it's in the namespace.
1135                         //
1136                         FullNamedExpression fne = ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1137
1138                         //
1139                         // Check aliases. 
1140                         //
1141                         if (aliases != null && arity == 0) {
1142                                 UsingAliasNamespace uan;
1143                                 if (aliases.TryGetValue (name, out uan)) {
1144                                         if (fne != null) {
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);
1151                                         }
1152
1153                                         return uan.ResolvedExpression;
1154                                 }
1155                         }
1156
1157                         if (fne != null)
1158                                 return fne;
1159
1160                         //
1161                         // Lookup can be called before the namespace is defined from different namespace using alias clause
1162                         //
1163                         if (namespace_using_table == null) {
1164                                 DoDefineNamespace ();
1165                         }
1166
1167                         //
1168                         // Check using entries.
1169                         //
1170                         FullNamedExpression match = null;
1171                         foreach (Namespace using_ns in namespace_using_table) {
1172                                 //
1173                                 // A using directive imports only types contained in the namespace, it
1174                                 // does not import any nested namespaces
1175                                 //
1176                                 fne = using_ns.LookupType (this, name, arity, mode, loc);
1177                                 if (fne == null)
1178                                         continue;
1179
1180                                 if (match == null) {
1181                                         match = fne;
1182                                         continue;
1183                                 }
1184
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) {
1189                                         match = fne;
1190                                         continue;
1191                                 } else if (texpr_fne == null) {
1192                                         continue;
1193                                 }
1194
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 ());
1203                                         }
1204
1205                                         return match;
1206                                 }
1207
1208                                 if (better == texpr_fne.Type)
1209                                         match = texpr_fne;
1210                         }
1211
1212                         return match;
1213                 }
1214
1215                 protected override void DefineNamespace ()
1216                 {
1217                         if (namespace_using_table == null)
1218                                 DoDefineNamespace ();
1219
1220                         base.DefineNamespace ();
1221                 }
1222
1223                 void DoDefineNamespace ()
1224                 {
1225                         namespace_using_table = empty_namespaces;
1226
1227                         if (clauses != null) {
1228                                 var list = new List<Namespace> (clauses.Count);
1229                                 bool post_process_using_aliases = false;
1230
1231                                 for (int i = 0; i < clauses.Count; ++i) {
1232                                         var entry = clauses[i];
1233
1234                                         if (entry.Alias != null) {
1235                                                 if (aliases == null)
1236                                                         aliases = new Dictionary<string, UsingAliasNamespace> ();
1237
1238                                                 //
1239                                                 // Aliases are not available when resolving using section
1240                                                 // except extern aliases
1241                                                 //
1242                                                 if (entry is UsingExternAlias) {
1243                                                         entry.Define (this);
1244                                                         if (entry.ResolvedExpression != null)
1245                                                                 aliases.Add (entry.Alias.Value, (UsingExternAlias) entry);
1246
1247                                                         clauses.RemoveAt (i--);
1248                                                 } else {
1249                                                         post_process_using_aliases = true;
1250                                                 }
1251
1252                                                 continue;
1253                                         }
1254
1255                                         entry.Define (this);
1256
1257                                         //
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
1260                                         //
1261                                         if (entry.ResolvedExpression == null) {
1262                                                 clauses.RemoveAt (i--);
1263                                                 continue;
1264                                         }
1265
1266                                         Namespace using_ns = entry.ResolvedExpression as Namespace;
1267                                         if (using_ns == null)
1268                                                 continue;
1269
1270                                         if (list.Contains (using_ns)) {
1271                                                 // Ensure we don't report the warning multiple times in repl
1272                                                 clauses.RemoveAt (i--);
1273
1274                                                 Compiler.Report.Warning (105, 3, entry.Location,
1275                                                         "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
1276                                         } else {
1277                                                 list.Add (using_ns);
1278                                         }
1279                                 }
1280
1281                                 namespace_using_table = list.ToArray ();
1282
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);
1290                                                         }
1291
1292                                                         clauses.RemoveAt (i--);
1293                                                 }
1294                                         }
1295                                 }
1296                         }
1297                 }
1298
1299                 public void EnableRedefinition ()
1300                 {
1301                         is_defined = false;
1302                         namespace_using_table = null;
1303                 }
1304
1305                 internal override void GenerateDocComment (DocumentationBuilder builder)
1306                 {
1307                         if (containers != null) {
1308                                 foreach (var tc in containers)
1309                                         tc.GenerateDocComment (builder);
1310                         }
1311                 }
1312
1313                 public override string GetSignatureForError ()
1314                 {
1315                         return MemberName == null ? "global::" : base.GetSignatureForError ();
1316                 }
1317
1318                 public override void RemoveContainer (TypeContainer cont)
1319                 {
1320                         base.RemoveContainer (cont);
1321                         NS.RemoveContainer (cont);
1322                 }
1323
1324                 protected override bool VerifyClsCompliance ()
1325                 {
1326                         if (Module.IsClsComplianceRequired ()) {
1327                                 if (MemberName != null && MemberName.Name[0] == '_') {
1328                                         Warning_IdentifierNotCompliant ();
1329                                 }
1330
1331                                 ns.VerifyClsCompliance ();
1332                                 return true;
1333                         }
1334
1335                         return false;
1336                 }
1337         }
1338
1339         public class UsingNamespace
1340         {
1341                 readonly ATypeNameExpression expr;
1342                 readonly Location loc;
1343                 protected FullNamedExpression resolved;
1344
1345                 public UsingNamespace (ATypeNameExpression expr, Location loc)
1346                 {
1347                         this.expr = expr;
1348                         this.loc = loc;
1349                 }
1350
1351                 #region Properties
1352
1353                 public virtual SimpleMemberName Alias {
1354                         get {
1355                                 return null;
1356                         }
1357                 }
1358
1359                 public Location Location {
1360                         get {
1361                                 return loc;
1362                         }
1363                 }
1364
1365                 public ATypeNameExpression NamespaceExpression  {
1366                         get {
1367                                 return expr;
1368                         }
1369                 }
1370
1371                 public FullNamedExpression ResolvedExpression {
1372                         get {
1373                                 return resolved;
1374                         }
1375                 }
1376
1377                 #endregion
1378
1379                 public string GetSignatureForError ()
1380                 {
1381                         return expr.GetSignatureForError ();
1382                 }
1383
1384                 public virtual void Define (NamespaceContainer ctx)
1385                 {
1386                         resolved = expr.ResolveAsTypeOrNamespace (ctx);
1387                         var ns = resolved as Namespace;
1388                         if (ns == null) {
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 ());
1394                                 }
1395                         }
1396                 }
1397
1398                 public override string ToString()
1399                 {
1400                         return resolved.ToString();
1401                 }
1402         }
1403
1404         public class UsingExternAlias : UsingAliasNamespace
1405         {
1406                 public UsingExternAlias (SimpleMemberName alias, Location loc)
1407                         : base (alias, null, loc)
1408                 {
1409                 }
1410
1411                 public override void Define (NamespaceContainer ctx)
1412                 {
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",
1417                                         Alias.Value);
1418                         }
1419                 }
1420         }
1421
1422         public class UsingAliasNamespace : UsingNamespace
1423         {
1424                 readonly SimpleMemberName alias;
1425
1426                 public struct AliasContext : IMemberContext
1427                 {
1428                         readonly NamespaceContainer ns;
1429
1430                         public AliasContext (NamespaceContainer ns)
1431                         {
1432                                 this.ns = ns;
1433                         }
1434
1435                         public TypeSpec CurrentType {
1436                                 get {
1437                                         return null;
1438                                 }
1439                         }
1440
1441                         public TypeParameters CurrentTypeParameters {
1442                                 get {
1443                                         return null;
1444                                 }
1445                         }
1446
1447                         public MemberCore CurrentMemberDefinition {
1448                                 get {
1449                                         return null;
1450                                 }
1451                         }
1452
1453                         public bool IsObsolete {
1454                                 get {
1455                                         return false;
1456                                 }
1457                         }
1458
1459                         public bool IsUnsafe {
1460                                 get {
1461                                         throw new NotImplementedException ();
1462                                 }
1463                         }
1464
1465                         public bool IsStatic {
1466                                 get {
1467                                         throw new NotImplementedException ();
1468                                 }
1469                         }
1470
1471                         public ModuleContainer Module {
1472                                 get {
1473                                         return ns.Module;
1474                                 }
1475                         }
1476
1477                         public string GetSignatureForError ()
1478                         {
1479                                 throw new NotImplementedException ();
1480                         }
1481
1482                         public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
1483                         {
1484                                 return null;
1485                         }
1486
1487                         public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1488                         {
1489                                 var fne = ns.NS.LookupTypeOrNamespace (ns, name, arity, mode, loc);
1490                                 if (fne != null)
1491                                         return fne;
1492
1493                                 //
1494                                 // Only extern aliases are allowed in this context
1495                                 //
1496                                 fne = ns.LookupExternAlias (name);
1497                                 if (fne != null || ns.MemberName == null)
1498                                         return fne;
1499
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);
1504                                         if (fne != null)
1505                                                 return fne;
1506
1507                                         mn = mn.Left;
1508                                         container_ns = container_ns.Parent;
1509                                 }
1510
1511                                 if (ns.Parent != null)
1512                                         return ns.Parent.LookupNamespaceOrType (name, arity, mode, loc);
1513
1514                                 return null;
1515                         }
1516
1517                         public FullNamedExpression LookupNamespaceAlias (string name)
1518                         {
1519                                 return ns.LookupNamespaceAlias (name);
1520                         }
1521                 }
1522
1523                 public UsingAliasNamespace (SimpleMemberName alias, ATypeNameExpression expr, Location loc)
1524                         : base (expr, loc)
1525                 {
1526                         this.alias = alias;
1527                 }
1528
1529                 public override SimpleMemberName Alias {
1530                         get {
1531                                 return alias;
1532                         }
1533                 }
1534
1535                 public override void Define (NamespaceContainer ctx)
1536                 {
1537                         //
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
1543                         //
1544                         // We achieve that by introducing alias-context which redirect any local
1545                         // namespace or type resolve calls to parent namespace
1546                         //
1547                         resolved = NamespaceExpression.ResolveAsTypeOrNamespace (new AliasContext (ctx));
1548                 }
1549         }
1550 }