Merge pull request #665 from andreas-auerswald/master
[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.Forms";
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                         var ns_parent = name.Left == null ? this : AddNamespace (name.Left);
280                         return ns_parent.TryAddNamespace (name.Basename);
281                 }
282
283                 Namespace TryAddNamespace (string name)
284                 {
285                         Namespace ns;
286
287                         if (!namespaces.TryGetValue (name, out ns)) {
288                                 ns = new Namespace (this, name);
289                                 namespaces.Add (name, ns);
290                         }
291
292                         return ns;
293                 }
294
295                 // TODO: Replace with CreateNamespace where MemberName is created for the method call
296                 public Namespace GetNamespace (string name, bool create)
297                 {
298                         int pos = name.IndexOf ('.');
299
300                         Namespace ns;
301                         string first;
302                         if (pos >= 0)
303                                 first = name.Substring (0, pos);
304                         else
305                                 first = name;
306
307                         if (!namespaces.TryGetValue (first, out ns)) {
308                                 if (!create)
309                                         return null;
310
311                                 ns = new Namespace (this, first);
312                                 namespaces.Add (first, ns);
313                         }
314
315                         if (pos >= 0)
316                                 ns = ns.GetNamespace (name.Substring (pos + 1), create);
317
318                         return ns;
319                 }
320
321                 public IList<TypeSpec> GetAllTypes (string name)
322                 {
323                         IList<TypeSpec> found;
324                         if (types == null || !types.TryGetValue (name, out found))
325                                 return null;
326
327                         return found;
328                 }
329
330                 public TypeExpr LookupType (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
331                 {
332                         if (types == null)
333                                 return null;
334
335                         TypeExpr te;
336                         if (arity == 0 && cached_types.TryGetValue (name, out te))
337                                 return te;
338
339                         IList<TypeSpec> found;
340                         if (!types.TryGetValue (name, out found))
341                                 return null;
342
343                         TypeSpec best = null;
344                         foreach (var ts in found) {
345                                 if (ts.Arity == arity) {
346                                         if (best == null) {
347                                                 if ((ts.Modifiers & Modifiers.INTERNAL) != 0 && !ts.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly) && mode != LookupMode.IgnoreAccessibility)
348                                                         continue;
349
350                                                 best = ts;
351                                                 continue;
352                                         }
353
354                                         if (best.MemberDefinition.IsImported && ts.MemberDefinition.IsImported) {
355                                                 if (mode == LookupMode.Normal) {
356                                                         ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
357                                                         ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
358                                                         ctx.Module.Compiler.Report.Error (433, loc, "The imported type `{0}' is defined multiple times", ts.GetSignatureForError ());
359                                                 }
360                                                 break;
361                                         }
362
363                                         if (best.MemberDefinition.IsImported)
364                                                 best = ts;
365
366                                         if ((best.Modifiers & Modifiers.INTERNAL) != 0 && !best.MemberDefinition.IsInternalAsPublic (ctx.Module.DeclaringAssembly))
367                                                 continue;
368
369                                         if (mode != LookupMode.Normal)
370                                                 continue;
371
372                                         if (ts.MemberDefinition.IsImported) {
373                                                 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (best);
374                                                 ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ts);
375                                         }
376
377                                         ctx.Module.Compiler.Report.Warning (436, 2, loc,
378                                                 "The type `{0}' conflicts with the imported type of same name'. Ignoring the imported type definition",
379                                                 best.GetSignatureForError ());
380                                 }
381
382                                 //
383                                 // Lookup for the best candidate with the closest arity match
384                                 //
385                                 if (arity < 0) {
386                                         if (best == null) {
387                                                 best = ts;
388                                         } else if (System.Math.Abs (ts.Arity + arity) < System.Math.Abs (best.Arity + arity)) {
389                                                 best = ts;
390                                         }
391                                 }
392                         }
393
394                         if (best == null)
395                                 return null;
396
397                         te = new TypeExpression (best, Location.Null);
398
399                         // TODO MemberCache: Cache more
400                         if (arity == 0 && mode == LookupMode.Normal)
401                                 cached_types.Add (name, te);
402
403                         return te;
404                 }
405
406                 public FullNamedExpression LookupTypeOrNamespace (IMemberContext ctx, string name, int arity, LookupMode mode, Location loc)
407                 {
408                         var texpr = LookupType (ctx, name, arity, mode, loc);
409
410                         Namespace ns;
411                         if (arity == 0 && namespaces.TryGetValue (name, out ns)) {
412                                 if (texpr == null)
413                                         return ns;
414
415                                 if (mode != LookupMode.Probing) {
416                                         ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (texpr.Type);
417                                         // ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (ns.loc, "");
418                                         ctx.Module.Compiler.Report.Warning (437, 2, loc,
419                                                 "The type `{0}' conflicts with the imported namespace `{1}'. Using the definition found in the source file",
420                                                 texpr.GetSignatureForError (), ns.GetSignatureForError ());
421                                 }
422
423                                 if (texpr.Type.MemberDefinition.IsImported)
424                                         return ns;
425                         }
426
427                         return texpr;
428                 }
429
430                 //
431                 // Completes types with the given `prefix'
432                 //
433                 public IEnumerable<string> CompletionGetTypesStartingWith (string prefix)
434                 {
435                         if (types == null)
436                                 return Enumerable.Empty<string> ();
437
438                         var res = from item in types
439                                           where item.Key.StartsWith (prefix) && item.Value.Any (l => (l.Modifiers & Modifiers.PUBLIC) != 0)
440                                           select item.Key;
441
442                         if (namespaces != null)
443                                 res = res.Concat (from item in namespaces where item.Key.StartsWith (prefix) select item.Key);
444
445                         return res;
446                 }
447
448                 // 
449                 // Looks for extension method in this namespace
450                 //
451                 public List<MethodSpec> LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity)
452                 {
453                         if (extension_method_types == null)
454                                 return null;
455
456                         List<MethodSpec> found = null;
457                         for (int i = 0; i < extension_method_types.Count; ++i) {
458                                 var ts = extension_method_types[i];
459
460                                 //
461                                 // When the list was built we didn't know what members the type
462                                 // contains
463                                 //
464                                 if ((ts.Modifiers & Modifiers.METHOD_EXTENSION) == 0) {
465                                         if (extension_method_types.Count == 1) {
466                                                 extension_method_types = null;
467                                                 return found;
468                                         }
469
470                                         extension_method_types.RemoveAt (i--);
471                                         continue;
472                                 }
473
474                                 var res = ts.MemberCache.FindExtensionMethods (invocationContext, extensionType, name, arity);
475                                 if (res == null)
476                                         continue;
477
478                                 if (found == null) {
479                                         found = res;
480                                 } else {
481                                         found.AddRange (res);
482                                 }
483                         }
484
485                         return found;
486                 }
487
488                 public void AddType (ModuleContainer module, TypeSpec ts)
489                 {
490                         if (types == null) {
491                                 types = new Dictionary<string, IList<TypeSpec>> (64);
492                         }
493
494                         if (ts.IsClass && ts.Arity == 0) {
495                                 var extension_method_allowed = ts.MemberDefinition.IsImported ? (ts.Modifiers & Modifiers.METHOD_EXTENSION) != 0 : (ts.IsStatic || ts.MemberDefinition.IsPartial);
496                                 if (extension_method_allowed) {
497                                         if (extension_method_types == null)
498                                                 extension_method_types = new List<TypeSpec> ();
499
500                                         extension_method_types.Add (ts);
501                                 }
502                         }
503
504                         var name = ts.Name;
505                         IList<TypeSpec> existing;
506                         if (types.TryGetValue (name, out existing)) {
507                                 TypeSpec better_type;
508                                 TypeSpec found;
509                                 if (existing.Count == 1) {
510                                         found = existing[0];
511                                         if (ts.Arity == found.Arity) {
512                                                 better_type = IsImportedTypeOverride (module, ts, found);
513                                                 if (better_type == found)
514                                                         return;
515
516                                                 if (better_type != null) {
517                                                         existing [0] = better_type;
518                                                         return;
519                                                 }
520                                         }
521
522                                         existing = new List<TypeSpec> ();
523                                         existing.Add (found);
524                                         types[name] = existing;
525                                 } else {
526                                         for (int i = 0; i < existing.Count; ++i) {
527                                                 found = existing[i];
528                                                 if (ts.Arity != found.Arity)
529                                                         continue;
530
531                                                 better_type = IsImportedTypeOverride (module, ts, found);
532                                                 if (better_type == found)
533                                                         return;
534
535                                                 if (better_type != null) {
536                                                         existing.RemoveAt (i);
537                                                         --i;
538                                                         continue;
539                                                 }
540                                         }
541                                 }
542
543                                 existing.Add (ts);
544                         } else {
545                                 types.Add (name, new TypeSpec[] { ts });
546                         }
547                 }
548
549                 //
550                 // We import any types but in the situation there are same types
551                 // but one has better visibility (either public or internal with friend)
552                 // the less visible type is removed from the namespace cache
553                 //
554                 public static TypeSpec IsImportedTypeOverride (ModuleContainer module, TypeSpec ts, TypeSpec found)
555                 {
556                         var ts_accessible = (ts.Modifiers & Modifiers.PUBLIC) != 0 || ts.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
557                         var found_accessible = (found.Modifiers & Modifiers.PUBLIC) != 0 || found.MemberDefinition.IsInternalAsPublic (module.DeclaringAssembly);
558
559                         if (ts_accessible && !found_accessible)
560                                 return ts;
561
562                         // found is better always better for accessible or inaccessible ts
563                         if (!ts_accessible)
564                                 return found;
565
566                         return null;
567                 }
568
569                 public void RemoveContainer (TypeContainer tc)
570                 {
571                         types.Remove (tc.Basename);
572                         cached_types.Remove (tc.Basename);
573                 }
574
575                 public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext mc)
576                 {
577                         return this;
578                 }
579
580                 public void SetBuiltinType (BuiltinTypeSpec pts)
581                 {
582                         var found = types[pts.Name];
583                         cached_types.Remove (pts.Name);
584                         if (found.Count == 1) {
585                                 types[pts.Name][0] = pts;
586                         } else {
587                                 throw new NotImplementedException ();
588                         }
589                 }
590
591                 public void VerifyClsCompliance ()
592                 {
593                         if (types == null || cls_checked)
594                                 return;
595
596                         cls_checked = true;
597
598                         // TODO: This is quite ugly way to check for CLS compliance at namespace level
599
600                         var locase_types = new Dictionary<string, List<TypeSpec>> (StringComparer.OrdinalIgnoreCase);
601                         foreach (var tgroup in types.Values) {
602                                 foreach (var tm in tgroup) {
603                                         if ((tm.Modifiers & Modifiers.PUBLIC) == 0 || !tm.IsCLSCompliant ())
604                                                 continue;
605
606                                         List<TypeSpec> found;
607                                         if (!locase_types.TryGetValue (tm.Name, out found)) {
608                                                 found = new List<TypeSpec> ();
609                                                 locase_types.Add (tm.Name, found);
610                                         }
611
612                                         found.Add (tm);
613                                 }
614                         }
615
616                         foreach (var locase in locase_types.Values) {
617                                 if (locase.Count < 2)
618                                         continue;
619
620                                 bool all_same = true;
621                                 foreach (var notcompliant in locase) {
622                                         all_same = notcompliant.Name == locase[0].Name;
623                                         if (!all_same)
624                                                 break;
625                                 }
626
627                                 if (all_same)
628                                         continue;
629
630                                 TypeContainer compiled = null;
631                                 foreach (var notcompliant in locase) {
632                                         if (!notcompliant.MemberDefinition.IsImported) {
633                                                 if (compiled != null)
634                                                         compiled.Compiler.Report.SymbolRelatedToPreviousError (compiled);
635
636                                                 compiled = notcompliant.MemberDefinition as TypeContainer;
637                                         } else {
638                                                 compiled.Compiler.Report.SymbolRelatedToPreviousError (notcompliant);
639                                         }
640                                 }
641
642                                 compiled.Compiler.Report.Warning (3005, 1, compiled.Location,
643                                         "Identifier `{0}' differing only in case is not CLS-compliant", compiled.GetSignatureForError ());
644                         }
645                 }
646
647                 public override string ToString ()
648                 {
649                         return Name;
650                 }
651         }
652
653         public class CompilationSourceFile : NamespaceContainer
654         {
655                 readonly SourceFile file;
656                 CompileUnitEntry comp_unit;
657                 Dictionary<string, SourceFile> include_files;
658                 Dictionary<string, bool> conditionals;
659
660                 public CompilationSourceFile (ModuleContainer parent, SourceFile sourceFile)
661                         : this (parent)
662                 {
663                         this.file = sourceFile;
664                 }
665
666                 public CompilationSourceFile (ModuleContainer parent)
667                         : base (parent)
668                 {
669                 }
670
671                 public CompileUnitEntry SymbolUnitEntry {
672                         get {
673                                 return comp_unit;
674                         }
675                 }
676
677                 public string FileName {
678                         get {
679                                 return file.Name;
680                         }
681                 }
682
683                 public SourceFile SourceFile {
684                         get {
685                                 return file;
686                         }
687                 }
688
689                 public void AddIncludeFile (SourceFile file)
690                 {
691                         if (file == this.file)
692                                 return;
693
694                         if (include_files == null)
695                                 include_files = new Dictionary<string, SourceFile> ();
696
697                         if (!include_files.ContainsKey (file.FullPathName))
698                                 include_files.Add (file.FullPathName, file);
699                 }
700
701                 public void AddDefine (string value)
702                 {
703                         if (conditionals == null)
704                                 conditionals = new Dictionary<string, bool> (2);
705
706                         conditionals[value] = true;
707                 }
708
709                 public void AddUndefine (string value)
710                 {
711                         if (conditionals == null)
712                                 conditionals = new Dictionary<string, bool> (2);
713
714                         conditionals[value] = false;
715                 }
716
717                 public override void PrepareEmit ()
718                 {
719                         var sw = Module.DeclaringAssembly.SymbolWriter;
720                         if (sw != null) {
721                                 CreateUnitSymbolInfo (sw);
722                         }
723
724                         base.PrepareEmit ();
725                 }
726
727                 //
728                 // Creates symbol file index in debug symbol file
729                 //
730                 void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
731                 {
732                         var si = file.CreateSymbolInfo (symwriter);
733                         comp_unit = new CompileUnitEntry (symwriter, si);;
734
735                         if (include_files != null) {
736                                 foreach (SourceFile include in include_files.Values) {
737                                         si = include.CreateSymbolInfo (symwriter);
738                                         comp_unit.AddFile (si);
739                                 }
740                         }
741                 }
742
743                 public bool IsConditionalDefined (string value)
744                 {
745                         if (conditionals != null) {
746                                 bool res;
747                                 if (conditionals.TryGetValue (value, out res))
748                                         return res;
749
750                                 // When conditional was undefined
751                                 if (conditionals.ContainsKey (value))
752                                         return false;
753                         }
754
755                         return Compiler.Settings.IsConditionalSymbolDefined (value);
756                 }
757
758                 public override void Accept (StructuralVisitor visitor)
759                 {
760                         visitor.Visit (this);
761                 }
762         }
763
764
765         //
766         // Namespace block as created by the parser
767         //
768         public class NamespaceContainer : TypeContainer, IMemberContext
769         {
770                 static readonly Namespace[] empty_namespaces = new Namespace[0];
771
772                 readonly Namespace ns;
773
774                 public new readonly NamespaceContainer Parent;
775
776                 List<UsingNamespace> clauses;
777
778                 // Used by parsed to check for parser errors
779                 public bool DeclarationFound;
780
781                 Namespace[] namespace_using_table;
782                 Dictionary<string, UsingAliasNamespace> aliases;
783
784                 public NamespaceContainer (MemberName name, NamespaceContainer parent)
785                         : base (parent, name, null, MemberKind.Namespace)
786                 {
787                         this.Parent = parent;
788                         this.ns = parent.NS.AddNamespace (name);
789
790                         containers = new List<TypeContainer> ();
791                 }
792
793                 protected NamespaceContainer (ModuleContainer parent)
794                         : base (parent, null, null, MemberKind.Namespace)
795                 {
796                         ns = parent.GlobalRootNamespace;
797                         containers = new List<TypeContainer> (2);
798                 }
799
800                 #region Properties
801
802                 public override AttributeTargets AttributeTargets {
803                         get {
804                                 throw new NotSupportedException ();
805                         }
806                 }
807
808                 public override string DocCommentHeader {
809                         get {
810                                 throw new NotSupportedException ();
811                         }
812                 }
813
814                 public Namespace NS {
815                         get {
816                                 return ns;
817                         }
818                 }
819
820                 public List<UsingNamespace> Usings {
821                         get {
822                                 return clauses;
823                         }
824                 }
825
826                 public override string[] ValidAttributeTargets {
827                         get {
828                                 throw new NotSupportedException ();
829                         }
830                 }
831
832                 #endregion
833
834                 public void AddUsing (UsingNamespace un)
835                 {
836                         if (DeclarationFound){
837                                 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
838                         }
839
840                         if (clauses == null)
841                                 clauses = new List<UsingNamespace> ();
842
843                         clauses.Add (un);
844                 }
845
846                 public void AddUsing (UsingAliasNamespace un)
847                 {
848                         if (DeclarationFound){
849                                 Compiler.Report.Error (1529, un.Location, "A using clause must precede all other namespace elements except extern alias declarations");
850                         }
851
852                         AddAlias (un);
853                 }
854
855                 void AddAlias (UsingAliasNamespace un)
856                 {
857                         if (clauses == null) {
858                                 clauses = new List<UsingNamespace> ();
859                         } else {
860                                 foreach (var entry in clauses) {
861                                         var a = entry as UsingAliasNamespace;
862                                         if (a != null && a.Alias.Value == un.Alias.Value) {
863                                                 Compiler.Report.SymbolRelatedToPreviousError (a.Location, "");
864                                                 Compiler.Report.Error (1537, un.Location,
865                                                         "The using alias `{0}' appeared previously in this namespace", un.Alias.Value);
866                                         }
867                                 }
868                         }
869
870                         clauses.Add (un);
871                 }
872
873                 public override void AddPartial (TypeDefinition next_part)
874                 {
875                         var existing = ns.LookupType (this, next_part.MemberName.Name, next_part.MemberName.Arity, LookupMode.Probing, Location.Null);
876                         var td = existing != null ? existing.Type.MemberDefinition as TypeDefinition : null;
877                         AddPartial (next_part, td);
878                 }
879
880                 public override void AddTypeContainer (TypeContainer tc)
881                 {
882                         string name = tc.Basename;
883
884                         var mn = tc.MemberName;
885                         while (mn.Left != null) {
886                                 mn = mn.Left;
887                                 name = mn.Name;
888                         }
889
890                         var names_container = Parent == null ? Module : (TypeContainer) this;
891
892                         MemberCore mc;
893                         if (names_container.DefinedNames.TryGetValue (name, out mc)) {
894                                 if (tc is NamespaceContainer && mc is NamespaceContainer) {
895                                         AddTypeContainerMember (tc);
896                                         return;
897                                 }
898
899                                 Report.SymbolRelatedToPreviousError (mc);
900                                 if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (tc is ClassOrStruct || tc is Interface)) {
901                                         Error_MissingPartialModifier (tc);
902                                 } else {
903                                         Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
904                                                 GetSignatureForError (), mn.GetSignatureForError ());
905                                 }
906                         } else {
907                                 names_container.DefinedNames.Add (name, tc);
908
909                                 var tdef = tc.PartialContainer;
910                                 if (tdef != null) {
911                                         //
912                                         // Same name conflict in different namespace containers
913                                         //
914                                         var conflict = ns.GetAllTypes (name);
915                                         if (conflict != null) {
916                                                 foreach (var e in conflict) {
917                                                         if (e.Arity == mn.Arity) {
918                                                                 mc = (MemberCore) e.MemberDefinition;
919                                                                 break;
920                                                         }
921                                                 }
922                                         }
923
924                                         if (mc != null) {
925                                                 Report.SymbolRelatedToPreviousError (mc);
926                                                 Report.Error (101, tc.Location, "The namespace `{0}' already contains a definition for `{1}'",
927                                                         GetSignatureForError (), mn.GetSignatureForError ());
928                                         } else {
929                                                 ns.AddType (Module, tdef.Definition);
930                                         }
931                                 }
932                         }
933
934                         base.AddTypeContainer (tc);
935                 }
936
937                 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
938                 {
939                         throw new NotSupportedException ();
940                 }
941
942                 public override void EmitContainer ()
943                 {
944                         VerifyClsCompliance ();
945
946                         base.EmitContainer ();
947                 }
948
949                 public ExtensionMethodCandidates LookupExtensionMethod (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, int position)
950                 {
951                         //
952                         // Here we try to resume the search for extension method at the point
953                         // where the last bunch of candidates was found. It's more tricky than
954                         // it seems as we have to check both namespace containers and namespace
955                         // in correct order.
956                         //
957                         // Consider:
958                         // 
959                         // namespace A {
960                         //      using N1;
961                         //  namespace B.C.D {
962                         //              <our first search found candidates in A.B.C.D
963                         //  }
964                         // }
965                         //
966                         // In the example above namespace A.B.C.D, A.B.C and A.B have to be
967                         // checked before we hit A.N1 using
968                         //
969                         ExtensionMethodCandidates candidates;
970                         var container = this;
971                         do {
972                                 candidates = container.LookupExtensionMethodCandidates (invocationContext, extensionType, name, arity, ref position);
973                                 if (candidates != null || container.MemberName == null)
974                                         return candidates;
975
976                                 var container_ns = container.ns.Parent;
977                                 var mn = container.MemberName.Left;
978                                 int already_checked = position - 2;
979                                 while (already_checked-- > 0) {
980                                         mn = mn.Left;
981                                         container_ns = container_ns.Parent;
982                                 }
983
984                                 while (mn != null) {
985                                         ++position;
986
987                                         var methods = container_ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
988                                         if (methods != null) {
989                                                 return new ExtensionMethodCandidates (invocationContext, methods, container, position);
990                                         }
991
992                                         mn = mn.Left;
993                                         container_ns = container_ns.Parent;
994                                 }
995
996                                 position = 0;
997                                 container = container.Parent;
998                         } while (container != null);
999
1000                         return null;
1001                 }
1002
1003                 ExtensionMethodCandidates LookupExtensionMethodCandidates (IMemberContext invocationContext, TypeSpec extensionType, string name, int arity, ref int position)
1004                 {
1005                         List<MethodSpec> candidates = null;
1006
1007                         if (position == 0) {
1008                                 ++position;
1009
1010                                 candidates = ns.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1011                                 if (candidates != null) {
1012                                         return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1013                                 }
1014                         }
1015
1016                         if (position == 1) {
1017                                 ++position;
1018
1019                                 foreach (Namespace n in namespace_using_table) {
1020                                         var a = n.LookupExtensionMethod (invocationContext, extensionType, name, arity);
1021                                         if (a == null)
1022                                                 continue;
1023
1024                                         if (candidates == null)
1025                                                 candidates = a;
1026                                         else
1027                                                 candidates.AddRange (a);
1028                                 }
1029
1030                                 if (candidates != null)
1031                                         return new ExtensionMethodCandidates (invocationContext, candidates, this, position);
1032                         }
1033
1034                         return null;
1035                 }
1036
1037                 public override FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1038                 {
1039                         //
1040                         // Only simple names (no dots) will be looked up with this function
1041                         //
1042                         FullNamedExpression resolved;
1043                         for (NamespaceContainer container = this; container != null; container = container.Parent) {
1044                                 resolved = container.Lookup (name, arity, mode, loc);
1045                                 if (resolved != null || container.MemberName == null)
1046                                         return resolved;
1047
1048                                 var container_ns = container.ns.Parent;
1049                                 var mn = container.MemberName.Left;
1050                                 while (mn != null) {
1051                                         resolved = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1052                                         if (resolved != null)
1053                                                 return resolved;
1054
1055                                         mn = mn.Left;
1056                                         container_ns = container_ns.Parent;
1057                                 }
1058                         }
1059
1060                         return null;
1061                 }
1062
1063                 public override void GetCompletionStartingWith (string prefix, List<string> results)
1064                 {
1065                         if (Usings == null)
1066                                 return;
1067
1068                         foreach (var un in Usings) {
1069                                 if (un.Alias != null)
1070                                         continue;
1071
1072                                 var name = un.NamespaceExpression.Name;
1073                                 if (name.StartsWith (prefix))
1074                                         results.Add (name);
1075                         }
1076
1077
1078                         IEnumerable<string> all = Enumerable.Empty<string> ();
1079
1080                         foreach (Namespace using_ns in namespace_using_table) {
1081                                 if (prefix.StartsWith (using_ns.Name)) {
1082                                         int ld = prefix.LastIndexOf ('.');
1083                                         if (ld != -1) {
1084                                                 string rest = prefix.Substring (ld + 1);
1085
1086                                                 all = all.Concat (using_ns.CompletionGetTypesStartingWith (rest));
1087                                         }
1088                                 }
1089                                 all = all.Concat (using_ns.CompletionGetTypesStartingWith (prefix));
1090                         }
1091
1092                         results.AddRange (all);
1093
1094                         base.GetCompletionStartingWith (prefix, results);
1095                 }
1096
1097                 
1098                 //
1099                 // Looks-up a alias named @name in this and surrounding namespace declarations
1100                 //
1101                 public FullNamedExpression LookupExternAlias (string name)
1102                 {
1103                         if (aliases == null)
1104                                 return null;
1105
1106                         UsingAliasNamespace uan;
1107                         if (aliases.TryGetValue (name, out uan) && uan is UsingExternAlias)
1108                                 return uan.ResolvedExpression;
1109
1110                         return null;
1111                 }
1112                 
1113                 //
1114                 // Looks-up a alias named @name in this and surrounding namespace declarations
1115                 //
1116                 public override FullNamedExpression LookupNamespaceAlias (string name)
1117                 {
1118                         for (NamespaceContainer n = this; n != null; n = n.Parent) {
1119                                 if (n.aliases == null)
1120                                         continue;
1121
1122                                 UsingAliasNamespace uan;
1123                                 if (n.aliases.TryGetValue (name, out uan))
1124                                         return uan.ResolvedExpression;
1125                         }
1126
1127                         return null;
1128                 }
1129
1130                 FullNamedExpression Lookup (string name, int arity, LookupMode mode, Location loc)
1131                 {
1132                         //
1133                         // Check whether it's in the namespace.
1134                         //
1135                         FullNamedExpression fne = ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1136
1137                         //
1138                         // Check aliases. 
1139                         //
1140                         if (aliases != null && arity == 0) {
1141                                 UsingAliasNamespace uan;
1142                                 if (aliases.TryGetValue (name, out uan)) {
1143                                         if (fne != null && mode != LookupMode.Probing) {
1144                                                 // TODO: Namespace has broken location
1145                                                 //Report.SymbolRelatedToPreviousError (fne.Location, null);
1146                                                 Compiler.Report.SymbolRelatedToPreviousError (uan.Location, null);
1147                                                 Compiler.Report.Error (576, loc,
1148                                                         "Namespace `{0}' contains a definition with same name as alias `{1}'",
1149                                                         GetSignatureForError (), name);
1150                                         }
1151
1152                                         return uan.ResolvedExpression;
1153                                 }
1154                         }
1155
1156                         if (fne != null)
1157                                 return fne;
1158
1159                         //
1160                         // Lookup can be called before the namespace is defined from different namespace using alias clause
1161                         //
1162                         if (namespace_using_table == null) {
1163                                 DoDefineNamespace ();
1164                         }
1165
1166                         //
1167                         // Check using entries.
1168                         //
1169                         FullNamedExpression match = null;
1170                         foreach (Namespace using_ns in namespace_using_table) {
1171                                 //
1172                                 // A using directive imports only types contained in the namespace, it
1173                                 // does not import any nested namespaces
1174                                 //
1175                                 fne = using_ns.LookupType (this, name, arity, mode, loc);
1176                                 if (fne == null)
1177                                         continue;
1178
1179                                 if (match == null) {
1180                                         match = fne;
1181                                         continue;
1182                                 }
1183
1184                                 // Prefer types over namespaces
1185                                 var texpr_fne = fne as TypeExpr;
1186                                 var texpr_match = match as TypeExpr;
1187                                 if (texpr_fne != null && texpr_match == null) {
1188                                         match = fne;
1189                                         continue;
1190                                 } else if (texpr_fne == null) {
1191                                         continue;
1192                                 }
1193
1194                                 // It can be top level accessibility only
1195                                 var better = Namespace.IsImportedTypeOverride (Module, texpr_match.Type, texpr_fne.Type);
1196                                 if (better == null) {
1197                                         if (mode == LookupMode.Normal) {
1198                                                 Compiler.Report.SymbolRelatedToPreviousError (texpr_match.Type);
1199                                                 Compiler.Report.SymbolRelatedToPreviousError (texpr_fne.Type);
1200                                                 Compiler.Report.Error (104, loc, "`{0}' is an ambiguous reference between `{1}' and `{2}'",
1201                                                         name, texpr_match.GetSignatureForError (), texpr_fne.GetSignatureForError ());
1202                                         }
1203
1204                                         return match;
1205                                 }
1206
1207                                 if (better == texpr_fne.Type)
1208                                         match = texpr_fne;
1209                         }
1210
1211                         return match;
1212                 }
1213
1214                 protected override void DefineNamespace ()
1215                 {
1216                         if (namespace_using_table == null)
1217                                 DoDefineNamespace ();
1218
1219                         base.DefineNamespace ();
1220                 }
1221
1222                 void DoDefineNamespace ()
1223                 {
1224                         namespace_using_table = empty_namespaces;
1225
1226                         if (clauses != null) {
1227                                 var list = new List<Namespace> (clauses.Count);
1228                                 bool post_process_using_aliases = false;
1229
1230                                 for (int i = 0; i < clauses.Count; ++i) {
1231                                         var entry = clauses[i];
1232
1233                                         if (entry.Alias != null) {
1234                                                 if (aliases == null)
1235                                                         aliases = new Dictionary<string, UsingAliasNamespace> ();
1236
1237                                                 //
1238                                                 // Aliases are not available when resolving using section
1239                                                 // except extern aliases
1240                                                 //
1241                                                 if (entry is UsingExternAlias) {
1242                                                         entry.Define (this);
1243                                                         if (entry.ResolvedExpression != null)
1244                                                                 aliases.Add (entry.Alias.Value, (UsingExternAlias) entry);
1245
1246                                                         clauses.RemoveAt (i--);
1247                                                 } else {
1248                                                         post_process_using_aliases = true;
1249                                                 }
1250
1251                                                 continue;
1252                                         }
1253
1254                                         entry.Define (this);
1255
1256                                         //
1257                                         // It's needed for repl only, when using clause cannot be resolved don't hold it in
1258                                         // global list which is resolved for each evaluation
1259                                         //
1260                                         if (entry.ResolvedExpression == null) {
1261                                                 clauses.RemoveAt (i--);
1262                                                 continue;
1263                                         }
1264
1265                                         Namespace using_ns = entry.ResolvedExpression as Namespace;
1266                                         if (using_ns == null)
1267                                                 continue;
1268
1269                                         if (list.Contains (using_ns)) {
1270                                                 // Ensure we don't report the warning multiple times in repl
1271                                                 clauses.RemoveAt (i--);
1272
1273                                                 Compiler.Report.Warning (105, 3, entry.Location,
1274                                                         "The using directive for `{0}' appeared previously in this namespace", using_ns.GetSignatureForError ());
1275                                         } else {
1276                                                 list.Add (using_ns);
1277                                         }
1278                                 }
1279
1280                                 namespace_using_table = list.ToArray ();
1281
1282                                 if (post_process_using_aliases) {
1283                                         for (int i = 0; i < clauses.Count; ++i) {
1284                                                 var entry = clauses[i];
1285                                                 if (entry.Alias != null) {
1286                                                         entry.Define (this);
1287                                                         if (entry.ResolvedExpression != null) {
1288                                                                 aliases.Add (entry.Alias.Value, (UsingAliasNamespace) entry);
1289                                                         }
1290
1291                                                         clauses.RemoveAt (i--);
1292                                                 }
1293                                         }
1294                                 }
1295                         }
1296                 }
1297
1298                 public void EnableRedefinition ()
1299                 {
1300                         is_defined = false;
1301                         namespace_using_table = null;
1302                 }
1303
1304                 internal override void GenerateDocComment (DocumentationBuilder builder)
1305                 {
1306                         if (containers != null) {
1307                                 foreach (var tc in containers)
1308                                         tc.GenerateDocComment (builder);
1309                         }
1310                 }
1311
1312                 public override string GetSignatureForError ()
1313                 {
1314                         return MemberName == null ? "global::" : base.GetSignatureForError ();
1315                 }
1316
1317                 public override void RemoveContainer (TypeContainer cont)
1318                 {
1319                         base.RemoveContainer (cont);
1320                         NS.RemoveContainer (cont);
1321                 }
1322
1323                 protected override bool VerifyClsCompliance ()
1324                 {
1325                         if (Module.IsClsComplianceRequired ()) {
1326                                 if (MemberName != null && MemberName.Name[0] == '_') {
1327                                         Warning_IdentifierNotCompliant ();
1328                                 }
1329
1330                                 ns.VerifyClsCompliance ();
1331                                 return true;
1332                         }
1333
1334                         return false;
1335                 }
1336
1337                 public override void Accept (StructuralVisitor visitor)
1338                 {
1339                         visitor.Visit (this);
1340                 }
1341         }
1342
1343         public class UsingNamespace
1344         {
1345                 readonly ATypeNameExpression expr;
1346                 readonly Location loc;
1347                 protected FullNamedExpression resolved;
1348
1349                 public UsingNamespace (ATypeNameExpression expr, Location loc)
1350                 {
1351                         this.expr = expr;
1352                         this.loc = loc;
1353                 }
1354
1355                 #region Properties
1356
1357                 public virtual SimpleMemberName Alias {
1358                         get {
1359                                 return null;
1360                         }
1361                 }
1362
1363                 public Location Location {
1364                         get {
1365                                 return loc;
1366                         }
1367                 }
1368
1369                 public ATypeNameExpression NamespaceExpression  {
1370                         get {
1371                                 return expr;
1372                         }
1373                 }
1374
1375                 public FullNamedExpression ResolvedExpression {
1376                         get {
1377                                 return resolved;
1378                         }
1379                 }
1380
1381                 #endregion
1382
1383                 public string GetSignatureForError ()
1384                 {
1385                         return expr.GetSignatureForError ();
1386                 }
1387
1388                 public virtual void Define (NamespaceContainer ctx)
1389                 {
1390                         resolved = expr.ResolveAsTypeOrNamespace (ctx);
1391                         var ns = resolved as Namespace;
1392                         if (ns == null) {
1393                                 if (resolved != null) {
1394                                         ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (resolved.Type);
1395                                         ctx.Module.Compiler.Report.Error (138, Location,
1396                                                 "`{0}' is a type not a namespace. A using namespace directive can only be applied to namespaces",
1397                                                 GetSignatureForError ());
1398                                 }
1399                         }
1400                 }
1401
1402                 public override string ToString()
1403                 {
1404                         return resolved.ToString();
1405                 }
1406         }
1407
1408         public class UsingExternAlias : UsingAliasNamespace
1409         {
1410                 public UsingExternAlias (SimpleMemberName alias, Location loc)
1411                         : base (alias, null, loc)
1412                 {
1413                 }
1414
1415                 public override void Define (NamespaceContainer ctx)
1416                 {
1417                         resolved = ctx.Module.GetRootNamespace (Alias.Value);
1418                         if (resolved == null) {
1419                                 ctx.Module.Compiler.Report.Error (430, Location,
1420                                         "The extern alias `{0}' was not specified in -reference option",
1421                                         Alias.Value);
1422                         }
1423                 }
1424         }
1425
1426         public class UsingAliasNamespace : UsingNamespace
1427         {
1428                 readonly SimpleMemberName alias;
1429
1430                 public struct AliasContext : IMemberContext
1431                 {
1432                         readonly NamespaceContainer ns;
1433
1434                         public AliasContext (NamespaceContainer ns)
1435                         {
1436                                 this.ns = ns;
1437                         }
1438
1439                         public TypeSpec CurrentType {
1440                                 get {
1441                                         return null;
1442                                 }
1443                         }
1444
1445                         public TypeParameters CurrentTypeParameters {
1446                                 get {
1447                                         return null;
1448                                 }
1449                         }
1450
1451                         public MemberCore CurrentMemberDefinition {
1452                                 get {
1453                                         return null;
1454                                 }
1455                         }
1456
1457                         public bool IsObsolete {
1458                                 get {
1459                                         return false;
1460                                 }
1461                         }
1462
1463                         public bool IsUnsafe {
1464                                 get {
1465                                         throw new NotImplementedException ();
1466                                 }
1467                         }
1468
1469                         public bool IsStatic {
1470                                 get {
1471                                         throw new NotImplementedException ();
1472                                 }
1473                         }
1474
1475                         public ModuleContainer Module {
1476                                 get {
1477                                         return ns.Module;
1478                                 }
1479                         }
1480
1481                         public string GetSignatureForError ()
1482                         {
1483                                 throw new NotImplementedException ();
1484                         }
1485
1486                         public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
1487                         {
1488                                 return null;
1489                         }
1490
1491                         public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
1492                         {
1493                                 var fne = ns.NS.LookupTypeOrNamespace (ns, name, arity, mode, loc);
1494                                 if (fne != null)
1495                                         return fne;
1496
1497                                 //
1498                                 // Only extern aliases are allowed in this context
1499                                 //
1500                                 fne = ns.LookupExternAlias (name);
1501                                 if (fne != null || ns.MemberName == null)
1502                                         return fne;
1503
1504                                 var container_ns = ns.NS.Parent;
1505                                 var mn = ns.MemberName.Left;
1506                                 while (mn != null) {
1507                                         fne = container_ns.LookupTypeOrNamespace (this, name, arity, mode, loc);
1508                                         if (fne != null)
1509                                                 return fne;
1510
1511                                         mn = mn.Left;
1512                                         container_ns = container_ns.Parent;
1513                                 }
1514
1515                                 if (ns.Parent != null)
1516                                         return ns.Parent.LookupNamespaceOrType (name, arity, mode, loc);
1517
1518                                 return null;
1519                         }
1520
1521                         public FullNamedExpression LookupNamespaceAlias (string name)
1522                         {
1523                                 return ns.LookupNamespaceAlias (name);
1524                         }
1525                 }
1526
1527                 public UsingAliasNamespace (SimpleMemberName alias, ATypeNameExpression expr, Location loc)
1528                         : base (expr, loc)
1529                 {
1530                         this.alias = alias;
1531                 }
1532
1533                 public override SimpleMemberName Alias {
1534                         get {
1535                                 return alias;
1536                         }
1537                 }
1538
1539                 public override void Define (NamespaceContainer ctx)
1540                 {
1541                         //
1542                         // The namespace-or-type-name of a using-alias-directive is resolved as if
1543                         // the immediately containing compilation unit or namespace body had no
1544                         // using-directives. A using-alias-directive may however be affected
1545                         // by extern-alias-directives in the immediately containing compilation
1546                         // unit or namespace body
1547                         //
1548                         // We achieve that by introducing alias-context which redirect any local
1549                         // namespace or type resolve calls to parent namespace
1550                         //
1551                         resolved = NamespaceExpression.ResolveAsTypeOrNamespace (new AliasContext (ctx));
1552                 }
1553         }
1554 }