~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Parser/mcs/namespace.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

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