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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Parser/CSharpParser.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
// CSharpParser.cs
 
3
//
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using System.Collections.Generic;
 
29
using System.IO;
 
30
using System.Text;
 
31
using ICSharpCode.NRefactory.Editor;
 
32
using Mono.CSharp;
 
33
using ICSharpCode.NRefactory.TypeSystem;
 
34
 
 
35
namespace ICSharpCode.NRefactory.CSharp
 
36
{
 
37
        public class CSharpParser
 
38
        {
 
39
                CompilerSettings compilerSettings;
 
40
                
 
41
                class ConversionVisitor : StructuralVisitor
 
42
                {
 
43
                        SyntaxTree unit = new SyntaxTree ();
 
44
                        internal bool convertTypeSystemMode;
 
45
                        
 
46
                        public SyntaxTree Unit {
 
47
                                get {
 
48
                                        return unit;
 
49
                                }
 
50
                                set {
 
51
                                        unit = value;
 
52
                                }
 
53
                        }
 
54
                        
 
55
                        public LocationsBag LocationsBag {
 
56
                                get;
 
57
                                private set;
 
58
                        }
 
59
                        
 
60
                        public ConversionVisitor (bool convertTypeSystemMode, LocationsBag locationsBag)
 
61
                        {
 
62
                                this.convertTypeSystemMode = convertTypeSystemMode;
 
63
                                this.LocationsBag = locationsBag;
 
64
                        }
 
65
                        
 
66
                        public static TextLocation Convert (Mono.CSharp.Location loc)
 
67
                        {
 
68
                                return new TextLocation (loc.Row, loc.Column);
 
69
                        }
 
70
                        
 
71
                        public override void Visit(ModuleContainer mc)
 
72
                        {
 
73
                                bool first = true;
 
74
                                foreach (var container in mc.Containers) {
 
75
                                        var nspace = container as NamespaceContainer;
 
76
                                        if (nspace == null) {
 
77
                                                container.Accept(this);
 
78
                                                continue;
 
79
                                        }
 
80
                                        NamespaceDeclaration nDecl = null;
 
81
                                        var loc = LocationsBag.GetLocations(nspace);
 
82
                                        
 
83
                                        if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
 
84
                                                nDecl = new NamespaceDeclaration ();
 
85
                                                if (loc != null) {
 
86
                                                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
 
87
                                                }
 
88
                                                ConvertNamespaceName(nspace.RealMemberName, nDecl);
 
89
                                                if (loc != null && loc.Count > 1) {
 
90
                                                        nDecl.AddChild(new CSharpTokenNode (Convert(loc [1]), Roles.LBrace), Roles.LBrace);
 
91
                                                }
 
92
                                                AddToNamespace(nDecl);
 
93
                                                namespaceStack.Push(nDecl);
 
94
                                        }
 
95
                                        
 
96
                                        if (nspace.Usings != null) {
 
97
                                                foreach (var us in nspace.Usings) {
 
98
                                                        us.Accept(this);
 
99
                                                }
 
100
                                        }
 
101
                                        
 
102
                                        if (first) {
 
103
                                                first = false;
 
104
                                                if (mc.OptAttributes != null) {
 
105
                                                        foreach (var attr in mc.OptAttributes.Sections) {
 
106
                                                                unit.AddChild (ConvertAttributeSection (attr), SyntaxTree.MemberRole);
 
107
                                                        }
 
108
                                                }
 
109
                                        }
 
110
                                        
 
111
                                        if (nspace.Containers != null) {
 
112
                                                foreach (var subContainer in nspace.Containers) {
 
113
                                                        subContainer.Accept(this);
 
114
                                                }
 
115
                                        }
 
116
                                        if (nDecl != null) {
 
117
                                                AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
118
                                                if (loc != null && loc.Count > 2)
 
119
                                                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [2]), Roles.RBrace), Roles.RBrace);
 
120
                                                if (loc != null && loc.Count > 3)
 
121
                                                        nDecl.AddChild (new CSharpTokenNode (Convert (loc [3]), Roles.Semicolon), Roles.Semicolon);
 
122
                                                
 
123
                                                namespaceStack.Pop ();
 
124
                                        } else {
 
125
                                                AddAttributeSection (unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
126
                                        }
 
127
                                }
 
128
                                AddAttributeSection (unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
129
                        }
 
130
                        
 
131
                        #region Global
 
132
                        Stack<NamespaceDeclaration> namespaceStack = new Stack<NamespaceDeclaration> ();
 
133
                        
 
134
                        void AddTypeArguments (ATypeNameExpression texpr, AstType result)
 
135
                        {
 
136
                                if (texpr.TypeArguments == null || texpr.TypeArguments.Args == null)
 
137
                                        return;
 
138
                                var loc = LocationsBag.GetLocations (texpr.TypeArguments);
 
139
                                if (loc != null && loc.Count >= 2)
 
140
                                        result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 2]), Roles.LChevron), Roles.LChevron);
 
141
                                int i = 0;
 
142
                                foreach (var arg in texpr.TypeArguments.Args) {
 
143
                                        result.AddChild (ConvertToType (arg), Roles.TypeArgument);
 
144
                                        if (loc != null && i < loc.Count - 2)
 
145
                                                result.AddChild (new CSharpTokenNode (Convert (loc [i++]), Roles.Comma), Roles.Comma);
 
146
                                }
 
147
                                if (loc != null && loc.Count >= 2)
 
148
                                        result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 1]), Roles.RChevron), Roles.RChevron);
 
149
                        }
 
150
                        
 
151
                        AstType ConvertToType (TypeParameter spec)
 
152
                        {
 
153
                                AstType result;
 
154
                                result = new SimpleType () { IdentifierToken = Identifier.Create (spec.Name, Convert (spec.Location)) };
 
155
                                return result;
 
156
                        }
 
157
                        
 
158
                        AstType ConvertToType (MemberName memberName)
 
159
                        {
 
160
                                AstType result;
 
161
                                if (memberName.Left != null) {
 
162
                                        result = new MemberType ();
 
163
                                        result.AddChild (ConvertToType (memberName.Left), MemberType.TargetRole);
 
164
                                        var loc = LocationsBag.GetLocations (memberName.Left);
 
165
                                        if (loc != null)
 
166
                                                result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Dot), Roles.Dot);
 
167
                                        result.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
 
168
                                } else {
 
169
                                        result = new SimpleType () { IdentifierToken = Identifier.Create (memberName.Name, Convert (memberName.Location)) };
 
170
                                }
 
171
                                if (memberName.TypeParameters != null) {
 
172
                                        var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
 
173
                                        if (chevronLocs != null)
 
174
                                                result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
 
175
                                        for (int i = 0; i < memberName.TypeParameters.Count; i++) {
 
176
                                                var param = memberName.TypeParameters [i];
 
177
                                                result.AddChild (new SimpleType (Identifier.Create (param.Name, Convert (param.Location))), Roles.TypeArgument);
 
178
                                                if (chevronLocs != null && i < chevronLocs.Count - 2)
 
179
                                                        result.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
 
180
                                        }
 
181
                                        if (chevronLocs != null)
 
182
                                                result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
 
183
                                }
 
184
                                return result;
 
185
                        }
 
186
                        
 
187
                        AstType ConvertToType (Mono.CSharp.Expression typeName)
 
188
                        {
 
189
                                if (typeName == null) // may happen in typeof(Generic<,,,,>)
 
190
                                        return new SimpleType ();
 
191
                                
 
192
                                if (typeName is TypeExpression) {
 
193
                                        var typeExpr = (Mono.CSharp.TypeExpression)typeName;
 
194
                                        return new PrimitiveType (typeExpr.GetSignatureForError (), Convert (typeExpr.Location));
 
195
                                }
 
196
                                
 
197
                                if (typeName is Mono.CSharp.QualifiedAliasMember) {
 
198
                                        var qam = (Mono.CSharp.QualifiedAliasMember)typeName;
 
199
                                        var loc = LocationsBag.GetLocations (typeName);
 
200
                                        var memberType = new MemberType ();
 
201
                                        memberType.Target = new SimpleType (qam.alias, Convert (qam.Location));
 
202
                                        memberType.IsDoubleColon = true;
 
203
 
 
204
                                        if (loc != null && loc.Count > 0)
 
205
                                                memberType.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.DoubleColon), Roles.DoubleColon);
 
206
 
 
207
                                        memberType.MemberNameToken = Identifier.Create (qam.Name, loc != null ? Convert (loc[1]) : TextLocation.Empty);
 
208
                                        return memberType;
 
209
                                }
 
210
                                
 
211
                                if (typeName is MemberAccess) {
 
212
                                        MemberAccess ma = (MemberAccess)typeName;
 
213
                                        
 
214
                                        var memberType = new MemberType ();
 
215
                                        memberType.AddChild (ConvertToType (ma.LeftExpression), MemberType.TargetRole);
 
216
                                        if (!ma.DotLocation.IsNull)
 
217
                                                memberType.AddChild (new CSharpTokenNode (Convert (ma.DotLocation), Roles.Dot), Roles.Dot);
 
218
                                        
 
219
                                        memberType.MemberNameToken = Identifier.Create (ma.Name, Convert (ma.Location));
 
220
                                        
 
221
                                        AddTypeArguments (ma, memberType);
 
222
                                        return memberType;
 
223
                                }
 
224
                                
 
225
                                if (typeName is SimpleName) {
 
226
                                        var sn = (SimpleName)typeName;
 
227
                                        var result = new SimpleType (sn.Name, Convert (sn.Location));
 
228
                                        AddTypeArguments (sn, result);
 
229
                                        return result;
 
230
                                }
 
231
                                
 
232
                                if (typeName is ComposedCast) {
 
233
                                        var cc = (ComposedCast)typeName;
 
234
                                        var baseType = ConvertToType (cc.Left);
 
235
                                        var result = new ComposedType () { BaseType = baseType };
 
236
                                        var ccSpec = cc.Spec;
 
237
                                        while (ccSpec != null) {
 
238
                                                if (ccSpec.IsNullable) {
 
239
                                                        result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), ComposedType.NullableRole), ComposedType.NullableRole);
 
240
                                                } else if (ccSpec.IsPointer) {
 
241
                                                        result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), ComposedType.PointerRole), ComposedType.PointerRole);
 
242
                                                } else {
 
243
                                                        var location = LocationsBag.GetLocations (ccSpec);
 
244
                                                        var spec = new ArraySpecifier () { Dimensions = ccSpec.Dimension };
 
245
                                                        spec.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), Roles.LBracket), Roles.LBracket);
 
246
                                                        if (location != null)
 
247
                                                                spec.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RBracket), Roles.RBracket);
 
248
                                                        
 
249
                                                        result.ArraySpecifiers.Add (spec);
 
250
                                                }
 
251
                                                ccSpec = ccSpec.Next;
 
252
                                        }
 
253
                                        return result;
 
254
                                }
 
255
                                
 
256
                                if (typeName is SpecialContraintExpr) {
 
257
                                        var sce = (SpecialContraintExpr)typeName;
 
258
                                        switch (sce.Constraint) {
 
259
                                                case SpecialConstraint.Class:
 
260
                                                        return new PrimitiveType ("class", Convert (sce.Location));
 
261
                                                case SpecialConstraint.Struct:
 
262
                                                        return new PrimitiveType ("struct", Convert (sce.Location));
 
263
                                                case SpecialConstraint.Constructor:
 
264
                                                        return new PrimitiveType ("new", Convert (sce.Location));
 
265
                                        }
 
266
                                }
 
267
                                return new SimpleType ("unknown");
 
268
                        }
 
269
                        
 
270
                        IEnumerable<Attribute> GetAttributes (IEnumerable<Mono.CSharp.Attribute> optAttributes)
 
271
                        {
 
272
                                if (optAttributes == null)
 
273
                                        yield break;
 
274
                                foreach (var attr in optAttributes) {
 
275
                                        Attribute result = new Attribute ();
 
276
                                        result.Type = ConvertToType (attr.TypeNameExpression);
 
277
                                        var loc = LocationsBag.GetLocations (attr);
 
278
                                        result.HasArgumentList = loc != null;
 
279
                                        int pos = 0;
 
280
                                        if (loc != null)
 
281
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.LPar), Roles.LPar);
 
282
                                        
 
283
                                        if (attr.PositionalArguments != null) {
 
284
                                                foreach (var arg in attr.PositionalArguments) {
 
285
                                                        if (arg == null)
 
286
                                                                continue;
 
287
                                                        var na = arg as NamedArgument;
 
288
                                                        if (na != null) {
 
289
                                                                var newArg = new NamedArgumentExpression ();
 
290
                                                                newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
 
291
                                                                
 
292
                                                                var argLoc = LocationsBag.GetLocations (na);
 
293
                                                                if (argLoc != null)
 
294
                                                                        newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0]), Roles.Colon), Roles.Colon);
 
295
                                                                if (na.Expr != null)
 
296
                                                                        newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
 
297
                                                                result.AddChild (newArg, Roles.Argument);
 
298
                                                        } else {
 
299
                                                                if (arg.Expr != null)
 
300
                                                                        result.AddChild ((Expression)arg.Expr.Accept (this), Roles.Argument);
 
301
                                                        }
 
302
                                                        if (loc != null && pos + 1 < loc.Count)
 
303
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
 
304
                                                }
 
305
                                        }
 
306
                                        if (attr.NamedArguments != null) {
 
307
                                                foreach (NamedArgument na in attr.NamedArguments) {
 
308
                                                        var newArg = new NamedExpression ();
 
309
                                                        newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
 
310
                                                        
 
311
                                                        var argLoc = LocationsBag.GetLocations (na);
 
312
                                                        if (argLoc != null)
 
313
                                                                newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0]), Roles.Assign), Roles.Assign);
 
314
                                                        if (na.Expr != null)
 
315
                                                                newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
 
316
                                                        result.AddChild (newArg, Roles.Argument);
 
317
                                                        if (loc != null && pos + 1 < loc.Count)
 
318
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
 
319
                                                }
 
320
                                        }
 
321
                                        if (loc != null && pos < loc.Count)
 
322
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.RPar), Roles.RPar);
 
323
                                        
 
324
                                        yield return result;
 
325
                                }
 
326
                        }
 
327
                        
 
328
                        AttributeSection ConvertAttributeSection (IEnumerable<Mono.CSharp.Attribute> optAttributes)
 
329
                        {
 
330
 
 
331
                                if (optAttributes == null)
 
332
                                        return null;
 
333
                                AttributeSection result = new AttributeSection ();
 
334
                                var loc = LocationsBag.GetLocations (optAttributes);
 
335
                                int pos = 0;
 
336
                                if (loc != null)
 
337
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.LBracket), Roles.LBracket);
 
338
                                var first = optAttributes.FirstOrDefault ();
 
339
                                string target = first != null ? first.ExplicitTarget : null;
 
340
                                
 
341
                                if (!string.IsNullOrEmpty (target)) {
 
342
                                        if (loc != null && pos < loc.Count - 1) {
 
343
                                                result.AddChild (Identifier.Create (target, Convert (loc [pos++])), Roles.Identifier);
 
344
                                        } else {
 
345
                                                result.AddChild (Identifier.Create (target), Roles.Identifier);
 
346
                                        }
 
347
                                        if (loc != null && pos < loc.Count)
 
348
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Colon), Roles.Colon);
 
349
                                }
 
350
 
 
351
                                int attributeCount = 0;
 
352
                                foreach (var attr in GetAttributes (optAttributes)) {
 
353
                                        result.AddChild (attr, Roles.Attribute);
 
354
                                        if (loc != null && pos + 1 < loc.Count)
 
355
                                                result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
 
356
 
 
357
                                        attributeCount++;
 
358
                                }
 
359
                                // Left and right bracket + commas between the attributes
 
360
                                int locCount = 2 + attributeCount - 1;
 
361
                                // optional comma
 
362
                                if (loc != null && pos < loc.Count - 1 && loc.Count == locCount + 1)
 
363
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
 
364
                                if (loc != null && pos < loc.Count)
 
365
                                        result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.RBracket), Roles.RBracket);
 
366
                                return result;
 
367
                        }
 
368
                        
 
369
                        public override void Visit(NamespaceContainer nspace)
 
370
                        {
 
371
                                NamespaceDeclaration nDecl = null;
 
372
                                var loc = LocationsBag.GetLocations(nspace);
 
373
                                
 
374
                                if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
 
375
                                        nDecl = new NamespaceDeclaration ();
 
376
                                        if (loc != null) {
 
377
                                                nDecl.AddChild(new CSharpTokenNode (Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
 
378
                                        }
 
379
                                        ConvertNamespaceName(nspace.RealMemberName, nDecl);
 
380
                                        if (loc != null && loc.Count > 1) {
 
381
                                                nDecl.AddChild(new CSharpTokenNode (Convert(loc [1]), Roles.LBrace), Roles.LBrace);
 
382
                                        }
 
383
                                        AddToNamespace(nDecl);
 
384
                                        namespaceStack.Push(nDecl);
 
385
                                }
 
386
                                
 
387
                                if (nspace.Usings != null) {
 
388
                                        foreach (var us in nspace.Usings) {
 
389
                                                us.Accept(this);
 
390
                                        }
 
391
                                }
 
392
                                
 
393
                                if (nspace.Containers != null) {
 
394
                                        foreach (var container in nspace.Containers) {
 
395
                                                container.Accept(this);
 
396
                                        }
 
397
                                }
 
398
                                
 
399
                                if (nDecl != null) {
 
400
                                        AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
401
                                        if (loc != null && loc.Count > 2)
 
402
                                                nDecl.AddChild (new CSharpTokenNode (Convert (loc [2]), Roles.RBrace), Roles.RBrace);
 
403
                                        if (loc != null && loc.Count > 3)
 
404
                                                nDecl.AddChild (new CSharpTokenNode (Convert (loc [3]), Roles.Semicolon), Roles.Semicolon);
 
405
                                        
 
406
                                        namespaceStack.Pop ();
 
407
                                }
 
408
                        }
 
409
//                      public override void Visit (UsingsBag.Namespace nspace)
 
410
//                      {
 
411
//
 
412
//
 
413
//                              VisitNamespaceUsings (nspace);
 
414
//                              VisitNamespaceBody (nspace);
 
415
//
 
416
//                      }
 
417
//
 
418
                        void ConvertNamespaceName (MemberName memberName, NamespaceDeclaration namespaceDecl)
 
419
                        {
 
420
                                AstNode insertPos = null;
 
421
                                while (memberName != null) {
 
422
                                        Identifier newIdent = Identifier.Create (memberName.Name, Convert (memberName.Location));
 
423
                                        // HACK for a parser 'bug' - sometimes it generates "<invalid>" identifiers in namespace names (on certain bugs in the input file)
 
424
                                        if (newIdent.Name != "<invalid>") {
 
425
                                                namespaceDecl.InsertChildBefore (insertPos, newIdent, Roles.Identifier);
 
426
                                                insertPos = newIdent;
 
427
                                                
 
428
                                                if (!memberName.DotLocation.IsNull) {
 
429
                                                        var dotToken = new CSharpTokenNode (Convert (memberName.DotLocation), Roles.Dot);
 
430
                                                        namespaceDecl.InsertChildBefore (insertPos, dotToken, Roles.Dot);
 
431
                                                        insertPos = dotToken;
 
432
                                                }
 
433
                                        }
 
434
                                        memberName = memberName.Left;
 
435
                                }
 
436
                        }
 
437
                        
 
438
                        public override void Visit (UsingNamespace un)
 
439
                        {
 
440
                                var ud = new UsingDeclaration ();
 
441
                                var loc = LocationsBag.GetLocations (un);
 
442
                                ud.AddChild (new CSharpTokenNode (Convert (un.Location), UsingDeclaration.UsingKeywordRole), UsingDeclaration.UsingKeywordRole);
 
443
                                if (un.NamespaceExpression != null)
 
444
                                        ud.AddChild (ConvertToType (un.NamespaceExpression), UsingDeclaration.ImportRole);
 
445
                                if (loc != null)
 
446
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Semicolon), Roles.Semicolon);
 
447
                                AddToNamespace (ud);
 
448
                        }
 
449
 
 
450
                        public override void Visit (UsingAliasNamespace uan)
 
451
                        {
 
452
                                var ud = new UsingAliasDeclaration ();
 
453
                                var loc = LocationsBag.GetLocations (uan);
 
454
                                
 
455
                                ud.AddChild (new CSharpTokenNode (Convert (uan.Location), UsingAliasDeclaration.UsingKeywordRole), UsingAliasDeclaration.UsingKeywordRole);
 
456
                                ud.AddChild (Identifier.Create (uan.Alias.Value, Convert (uan.Alias.Location)), UsingAliasDeclaration.AliasRole);
 
457
                                if (loc != null)
 
458
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
 
459
                                if (uan.NamespaceExpression != null)
 
460
                                        ud.AddChild (ConvertToType (uan.NamespaceExpression), UsingAliasDeclaration.ImportRole);
 
461
                                if (loc != null && loc.Count > 1)
 
462
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Semicolon), Roles.Semicolon);
 
463
                                AddToNamespace (ud);
 
464
                        }
 
465
                        
 
466
                        public override void Visit (UsingExternAlias uea)
 
467
                        {
 
468
                                var ud = new ExternAliasDeclaration ();
 
469
                                var loc = LocationsBag.GetLocations (uea);
 
470
                                ud.AddChild (new CSharpTokenNode (Convert (uea.Location), Roles.ExternKeyword), Roles.ExternKeyword);
 
471
                                if (loc != null)
 
472
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.AliasKeyword), Roles.AliasKeyword);
 
473
                                ud.AddChild (Identifier.Create (uea.Alias.Value, Convert (uea.Alias.Location)), Roles.Identifier);
 
474
                                if (loc != null && loc.Count > 1)
 
475
                                        ud.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Semicolon), Roles.Semicolon);
 
476
                                AddToNamespace (ud);
 
477
                        }
 
478
 
 
479
                        AstType ConvertImport (MemberName memberName)
 
480
                        {
 
481
                                if (memberName.Left != null) {
 
482
                                        // left.name
 
483
                                        var t = new MemberType ();
 
484
//                                      t.IsDoubleColon = memberName.IsDoubleColon;
 
485
                                        t.AddChild (ConvertImport (memberName.Left), MemberType.TargetRole);
 
486
                                        
 
487
                                        if (!memberName.DotLocation.IsNull)
 
488
                                                t.AddChild (new CSharpTokenNode (Convert (memberName.DotLocation), Roles.Dot), Roles.Dot);
 
489
                                        
 
490
                                        t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
 
491
                                        AddTypeArguments (t, memberName);
 
492
                                        return t;
 
493
                                } else {
 
494
                                        SimpleType t = new SimpleType ();
 
495
                                        t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
 
496
                                        AddTypeArguments (t, memberName);
 
497
                                        return t;
 
498
                                }
 
499
                        }
 
500
                        
 
501
                        public override void Visit (MemberCore member)
 
502
                        {
 
503
                                Console.WriteLine ("Unknown member:");
 
504
                                Console.WriteLine (member.GetType () + "-> Member {0}", member.GetSignatureForError ());
 
505
                        }
 
506
                        
 
507
                        Stack<TypeDeclaration> typeStack = new Stack<TypeDeclaration> ();
 
508
                        
 
509
                        public override void Visit(Class c)
 
510
                        {
 
511
                                var newType = new TypeDeclaration ();
 
512
                                newType.ClassType = ClassType.Class;
 
513
                                AddAttributeSection(newType, c);
 
514
                                var location = LocationsBag.GetMemberLocation(c);
 
515
                                AddModifiers(newType, location);
 
516
                                int curLoc = 0;
 
517
                                if (location != null && location.Count > 0)
 
518
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.ClassKeyword), Roles.ClassKeyword);
 
519
                                
 
520
                                newType.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
 
521
                                AddTypeParameters (newType, c.MemberName);
 
522
                                
 
523
                                if (c.TypeBaseExpressions != null) {
 
524
                                        if (location != null && curLoc < location.Count)
 
525
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
 
526
                                        
 
527
                                        var commaLocations = LocationsBag.GetLocations (c.TypeBaseExpressions);
 
528
                                        int i = 0;
 
529
                                        foreach (var baseTypes in c.TypeBaseExpressions) {
 
530
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
 
531
                                                if (commaLocations != null && i < commaLocations.Count) {
 
532
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
533
                                                        i++;
 
534
                                                }
 
535
                                        }
 
536
                                }
 
537
                                
 
538
                                AddConstraints (newType, c.CurrentTypeParameters);
 
539
                                if (location != null && curLoc < location.Count)
 
540
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
 
541
                                typeStack.Push (newType);
 
542
                                base.Visit (c);
 
543
                                AddAttributeSection (newType, c.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
544
 
 
545
                                if (location != null && curLoc < location.Count) {
 
546
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
 
547
                                        
 
548
                                        if (location != null && curLoc < location.Count)
 
549
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
 
550
                                        
 
551
                                } else {
 
552
                                        // parser error, set end node to max value.
 
553
                                        newType.AddChild (new ErrorNode (), Roles.Error);
 
554
                                }
 
555
                                typeStack.Pop ();
 
556
                                AddType (newType);
 
557
                        }
 
558
                        
 
559
                        public override void Visit(Struct s)
 
560
                        {
 
561
                                var newType = new TypeDeclaration();
 
562
                                newType.ClassType = ClassType.Struct;
 
563
                                AddAttributeSection(newType, s);
 
564
                                var location = LocationsBag.GetMemberLocation(s);
 
565
                                AddModifiers(newType, location);
 
566
                                int curLoc = 0;
 
567
                                if (location != null && location.Count > 0)
 
568
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.StructKeyword), Roles.StructKeyword);
 
569
                                newType.AddChild (Identifier.Create (s.MemberName.Name, Convert (s.MemberName.Location)), Roles.Identifier);
 
570
                                AddTypeParameters (newType, s.MemberName);
 
571
                                
 
572
                                if (s.TypeBaseExpressions != null) {
 
573
                                        if (location != null && curLoc < location.Count)
 
574
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
 
575
                                        var commaLocations = LocationsBag.GetLocations (s.TypeBaseExpressions);
 
576
                                        int i = 0;
 
577
                                        foreach (var baseTypes in s.TypeBaseExpressions) {
 
578
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
 
579
                                                if (commaLocations != null && i < commaLocations.Count) {
 
580
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
581
                                                        i++;
 
582
                                                }
 
583
                                        }
 
584
                                }
 
585
                                
 
586
                                AddConstraints (newType, s.CurrentTypeParameters);
 
587
                                if (location != null && curLoc < location.Count)
 
588
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
 
589
                                typeStack.Push (newType);
 
590
                                base.Visit (s);
 
591
                                if (location != null && location.Count > 2) {
 
592
                                        if (location != null && curLoc < location.Count)
 
593
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
 
594
                                        if (location != null && curLoc < location.Count)
 
595
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
 
596
                                } else {
 
597
                                        // parser error, set end node to max value.
 
598
                                        newType.AddChild (new ErrorNode (), Roles.Error);
 
599
                                }
 
600
                                typeStack.Pop ();
 
601
                                AddType (newType);
 
602
                        }
 
603
                        
 
604
                        public override void Visit(Interface i)
 
605
                        {
 
606
                                var newType = new TypeDeclaration();
 
607
                                newType.ClassType = ClassType.Interface;
 
608
                                AddAttributeSection(newType, i);
 
609
                                var location = LocationsBag.GetMemberLocation(i);
 
610
                                AddModifiers(newType, location);
 
611
                                int curLoc = 0;
 
612
                                if (location != null && location.Count > 0)
 
613
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.InterfaceKeyword), Roles.InterfaceKeyword);
 
614
                                newType.AddChild (Identifier.Create (i.MemberName.Name, Convert (i.MemberName.Location)), Roles.Identifier);
 
615
                                AddTypeParameters (newType, i.MemberName);
 
616
                                
 
617
                                if (i.TypeBaseExpressions != null) {
 
618
                                        if (location != null && curLoc < location.Count)
 
619
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
 
620
                                        var commaLocations = LocationsBag.GetLocations (i.TypeBaseExpressions);
 
621
                                        int j = 0;
 
622
                                        foreach (var baseTypes in i.TypeBaseExpressions) {
 
623
                                                newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
 
624
                                                if (commaLocations != null && j < commaLocations.Count) {
 
625
                                                        newType.AddChild (new CSharpTokenNode (Convert (commaLocations [j]), Roles.Comma), Roles.Comma);
 
626
                                                        j++;
 
627
                                                }
 
628
                                        }
 
629
                                }
 
630
                                
 
631
                                AddConstraints (newType, i.CurrentTypeParameters);
 
632
                                if (location != null && curLoc < location.Count)
 
633
                                        newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
 
634
                                typeStack.Push (newType);
 
635
                                base.Visit (i);
 
636
                                if (location != null && location.Count > 2) {
 
637
                                        if (location != null && curLoc < location.Count)
 
638
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
 
639
                                        if (location != null && curLoc < location.Count)
 
640
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
 
641
                                } else {
 
642
                                        // parser error, set end node to max value.
 
643
                                        newType.AddChild (new ErrorNode (), Roles.Error);
 
644
                                }
 
645
                                typeStack.Pop ();
 
646
                                AddType (newType);
 
647
                        }
 
648
                        
 
649
                        public override void Visit(Mono.CSharp.Delegate d)
 
650
                        {
 
651
                                DelegateDeclaration newDelegate = new DelegateDeclaration ();
 
652
                                var location = LocationsBag.GetMemberLocation(d);
 
653
                                AddAttributeSection(newDelegate, d);
 
654
                                AddModifiers(newDelegate, location);
 
655
                                if (location != null && location.Count > 0) {
 
656
                                        newDelegate.AddChild(new CSharpTokenNode (Convert(location [0]), Roles.DelegateKeyword), Roles.DelegateKeyword);
 
657
                                }
 
658
                                if (d.ReturnType != null)
 
659
                                        newDelegate.AddChild (ConvertToType (d.ReturnType), Roles.Type);
 
660
                                newDelegate.AddChild (Identifier.Create (d.MemberName.Name, Convert (d.MemberName.Location)), Roles.Identifier);
 
661
                                AddTypeParameters (newDelegate, d.MemberName);
 
662
                                
 
663
                                if (location != null && location.Count > 1)
 
664
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
 
665
                                AddParameter (newDelegate, d.Parameters);
 
666
                                
 
667
                                if (location != null && location.Count > 2) {
 
668
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
 
669
                                }
 
670
                                AddConstraints (newDelegate, d.CurrentTypeParameters);
 
671
                                if (location != null && location.Count > 3) {
 
672
                                        newDelegate.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.Semicolon), Roles.Semicolon);
 
673
                                }
 
674
                                AddType (newDelegate);
 
675
                        }
 
676
                        
 
677
                        void AddType (EntityDeclaration child)
 
678
                        {
 
679
                                if (typeStack.Count > 0) {
 
680
                                        typeStack.Peek ().AddChild (child, Roles.TypeMemberRole);
 
681
                                } else {
 
682
                                        AddToNamespace (child);
 
683
                                }
 
684
                        }
 
685
                        
 
686
                        void AddToNamespace (AstNode child)
 
687
                        {
 
688
                                if (namespaceStack.Count > 0) {
 
689
                                        namespaceStack.Peek ().AddChild (child, NamespaceDeclaration.MemberRole);
 
690
                                } else {
 
691
                                        unit.AddChild (child, SyntaxTree.MemberRole);
 
692
                                }
 
693
                        }
 
694
                        
 
695
                        public override void Visit(Mono.CSharp.Enum e)
 
696
                        {
 
697
                                var newType = new TypeDeclaration();
 
698
                                newType.ClassType = ClassType.Enum;
 
699
                                AddAttributeSection(newType, e);
 
700
                                var location = LocationsBag.GetMemberLocation(e);
 
701
                                
 
702
                                AddModifiers(newType, location);
 
703
                                int curLoc = 0;
 
704
                                if (location != null && location.Count > 0)
 
705
                                        newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.EnumKeyword), Roles.EnumKeyword);
 
706
                                newType.AddChild(Identifier.Create(e.MemberName.Name, Convert(e.MemberName.Location)), Roles.Identifier);
 
707
                                
 
708
                                if (e.BaseTypeExpression != null) {
 
709
                                        if (location != null && curLoc < location.Count)
 
710
                                                newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
 
711
                                        newType.AddChild(ConvertToType(e.BaseTypeExpression), Roles.BaseType);
 
712
                                }
 
713
 
 
714
                                if (location != null && curLoc < location.Count)
 
715
                                        newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
 
716
                                typeStack.Push(newType);
 
717
                                
 
718
                                foreach (var m in e.Members) {
 
719
                                        EnumMember member = m as EnumMember;
 
720
                                        if (member == null) {
 
721
                                                Console.WriteLine("WARNING - ENUM MEMBER: " + m);
 
722
                                                continue;
 
723
                                        }
 
724
                                        Visit(member);
 
725
                                        if (location != null && curLoc < location.Count - 1) //last one is closing brace
 
726
                                                newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Comma), Roles.Comma);
 
727
                                }
 
728
                                
 
729
                                if (location != null && location.Count > 2) {
 
730
                                        if (location != null && curLoc < location.Count)
 
731
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
 
732
                                        if (location != null && curLoc < location.Count)
 
733
                                                newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
 
734
                                } else {
 
735
                                        // parser error, set end node to max value.
 
736
                                        newType.AddChild (new ErrorNode (), Roles.Error);
 
737
                                }
 
738
 
 
739
                                AddAttributeSection (newType, e.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
 
740
                                typeStack.Pop ();
 
741
                                AddType (newType);
 
742
                        }
 
743
                        
 
744
                        public override void Visit (EnumMember em)
 
745
                        {
 
746
                                EnumMemberDeclaration newField = new EnumMemberDeclaration ();
 
747
                                AddAttributeSection (newField, em);
 
748
                                newField.AddChild (Identifier.Create (em.Name, Convert (em.Location)), Roles.Identifier);
 
749
                                if (em.Initializer != null) {
 
750
                                        newField.AddChild (new CSharpTokenNode (Convert (em.Initializer.Location), Roles.Assign), Roles.Assign);
 
751
                                        newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole);
 
752
                                }
 
753
                                //Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation);
 
754
                                
 
755
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
 
756
                        }
 
757
                        #endregion
 
758
                        
 
759
                        #region Type members
 
760
                        
 
761
                        
 
762
                        public override void Visit (FixedField f)
 
763
                        {
 
764
                                var location = LocationsBag.GetMemberLocation (f);
 
765
                                int locationIdx = 0;
 
766
                                
 
767
                                var newField = new FixedFieldDeclaration ();
 
768
                                AddAttributeSection (newField, f);
 
769
                                AddModifiers (newField, location);
 
770
                                if (location != null && location.Count > 0)
 
771
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), FixedFieldDeclaration.FixedKeywordRole), FixedFieldDeclaration.FixedKeywordRole);
 
772
 
 
773
                                if (f.TypeExpression != null)
 
774
                                        newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
 
775
                                
 
776
                                var variable = new FixedVariableInitializer ();
 
777
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
 
778
                                if (f.Initializer != null && !f.Initializer.IsNull) {
 
779
                                        var bracketLocations = LocationsBag.GetLocations (f.Initializer);
 
780
                                        if (bracketLocations != null && bracketLocations.Count > 1)
 
781
                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.LBracket), Roles.LBracket);
 
782
                                        
 
783
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
 
784
                                        if (bracketLocations != null && bracketLocations.Count > 1)
 
785
                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.RBracket), Roles.RBracket);
 
786
                                }
 
787
                                newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
 
788
                                
 
789
                                if (f.Declarators != null) {
 
790
                                        foreach (var decl in f.Declarators) {
 
791
                                                var declLoc = LocationsBag.GetLocations (decl);
 
792
                                                if (declLoc != null)
 
793
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
 
794
                                                
 
795
                                                variable = new FixedVariableInitializer ();
 
796
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
 
797
                                                if (!decl.Initializer.IsNull) {
 
798
                                                        var bracketLocations = LocationsBag.GetLocations (f.Initializer);
 
799
                                                        if (bracketLocations != null && bracketLocations.Count > 1)
 
800
                                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.LBracket), Roles.LBracket);
 
801
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
802
                                                        if (bracketLocations != null && bracketLocations.Count > 1)
 
803
                                                                variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.RBracket), Roles.RBracket);
 
804
                                                }
 
805
                                                newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
 
806
                                        }
 
807
                                }
 
808
                                if (location != null && location.Count > locationIdx)
 
809
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx]), Roles.Semicolon), Roles.Semicolon);
 
810
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
 
811
                                
 
812
                        }
 
813
 
 
814
                        public override void Visit(Field f)
 
815
                        {
 
816
                                var location = LocationsBag.GetMemberLocation(f);
 
817
                                
 
818
                                FieldDeclaration newField = new FieldDeclaration ();
 
819
                                AddAttributeSection (newField, f);
 
820
                                AddModifiers (newField, location);
 
821
                                newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
 
822
                                
 
823
                                VariableInitializer variable = new VariableInitializer ();
 
824
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
 
825
                                int locationIdx = 0;
 
826
                                if (f.Initializer != null) {
 
827
                                        if (location != null)
 
828
                                                variable.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), Roles.Assign), Roles.Assign);
 
829
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
 
830
                                }
 
831
                                newField.AddChild (variable, Roles.Variable);
 
832
                                if (f.Declarators != null) {
 
833
                                        foreach (var decl in f.Declarators) {
 
834
                                                var declLoc = LocationsBag.GetLocations (decl);
 
835
                                                if (declLoc != null)
 
836
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
 
837
                                                
 
838
                                                variable = new VariableInitializer ();
 
839
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
 
840
                                                if (decl.Initializer != null) {
 
841
                                                        if (declLoc != null)
 
842
                                                                variable.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
 
843
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
844
                                                }
 
845
                                                newField.AddChild (variable, Roles.Variable);
 
846
                                        }
 
847
                                }
 
848
                                if (location != null &&location.Count > locationIdx)
 
849
                                        newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), Roles.Semicolon), Roles.Semicolon);
 
850
 
 
851
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
 
852
                        }
 
853
                        
 
854
                        public override void Visit (Const f)
 
855
                        {
 
856
                                var location = LocationsBag.GetMemberLocation (f);
 
857
                                
 
858
                                FieldDeclaration newField = new FieldDeclaration ();
 
859
                                AddAttributeSection (newField, f);
 
860
                                AddModifiers (newField, location);
 
861
                                if (location != null)
 
862
                                        newField.AddChild (new CSharpModifierToken (Convert (location [0]), Modifiers.Const), EntityDeclaration.ModifierRole);
 
863
                                newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
 
864
                                
 
865
                                VariableInitializer variable = new VariableInitializer ();
 
866
                                variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
 
867
                                
 
868
                                if (f.Initializer != null) {
 
869
                                        variable.AddChild (new CSharpTokenNode (Convert (f.Initializer.Location), Roles.Assign), Roles.Assign);
 
870
                                        variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
 
871
                                }
 
872
                                newField.AddChild (variable, Roles.Variable);
 
873
                                if (f.Declarators != null) {
 
874
                                        foreach (var decl in f.Declarators) {
 
875
                                                var declLoc = LocationsBag.GetLocations (decl);
 
876
                                                if (declLoc != null)
 
877
                                                        newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
 
878
                                                
 
879
                                                variable = new VariableInitializer ();
 
880
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
 
881
                                                if (decl.Initializer != null) {
 
882
                                                        variable.AddChild (new CSharpTokenNode (Convert (decl.Initializer.Location), Roles.Assign), Roles.Assign);
 
883
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
884
                                                }
 
885
                                                newField.AddChild (variable, Roles.Variable);
 
886
                                        }
 
887
                                }
 
888
                                if (location != null)
 
889
                                        newField.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
890
                                
 
891
                                typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
 
892
 
 
893
                                
 
894
                        }
 
895
                        
 
896
                        public override void Visit (Operator o)
 
897
                        {
 
898
                                OperatorDeclaration newOperator = new OperatorDeclaration ();
 
899
                                newOperator.OperatorType = (OperatorType)o.OperatorType;
 
900
                                
 
901
                                var location = LocationsBag.GetMemberLocation (o);
 
902
                                AddAttributeSection (newOperator, o);
 
903
                                AddModifiers (newOperator, location);
 
904
                                
 
905
                                
 
906
                                if (o.OperatorType == Operator.OpType.Implicit) {
 
907
                                        if (location != null && location.Count > 0) {
 
908
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.ImplicitRole), OperatorDeclaration.ImplicitRole);
 
909
                                                if (location.Count > 1)
 
910
                                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
 
911
                                        }
 
912
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
 
913
                                } else if (o.OperatorType == Operator.OpType.Explicit) {
 
914
                                        if (location != null && location.Count > 0) {
 
915
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.ExplicitRole), OperatorDeclaration.ExplicitRole);
 
916
                                                if (location.Count > 1)
 
917
                                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
 
918
                                        }
 
919
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
 
920
                                } else {
 
921
                                        newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
 
922
 
 
923
                                        if (location != null && location.Count > 0)
 
924
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
 
925
                                        
 
926
                                        if (location != null && location.Count > 1) {
 
927
                                                var r = OperatorDeclaration.GetRole(newOperator.OperatorType);
 
928
                                                newOperator.AddChild(new CSharpTokenNode(Convert(location [1]), r), r);
 
929
                                        }
 
930
                                }
 
931
                                if (location != null && location.Count > 2)
 
932
                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LPar), Roles.LPar);
 
933
                                AddParameter (newOperator, o.ParameterInfo);
 
934
                                if (location != null && location.Count > 3)
 
935
                                        newOperator.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RPar), Roles.RPar);
 
936
                                
 
937
                                if (o.Block != null) {
 
938
                                        newOperator.AddChild ((BlockStatement)o.Block.Accept (this), Roles.Body);
 
939
                                } else {
 
940
                                        if (location != null && location.Count >= 5)
 
941
                                                newOperator.AddChild (new CSharpTokenNode (Convert (location [4]), Roles.Semicolon), Roles.Semicolon);
 
942
                                }
 
943
                                typeStack.Peek ().AddChild (newOperator, Roles.TypeMemberRole);
 
944
                        }
 
945
                        
 
946
                        public void AddAttributeSection (AstNode parent, Attributable a)
 
947
                        {
 
948
                                if (a == null || a.OptAttributes == null)
 
949
                                        return;
 
950
                                AddAttributeSection (parent, a.OptAttributes);
 
951
                        }
 
952
                        
 
953
                        public void AddAttributeSection (AstNode parent, Attributes attrs, Role<AttributeSection> role)
 
954
                        {
 
955
                                if (attrs == null)
 
956
                                        return;
 
957
                                foreach (var attr in attrs.Sections) {
 
958
                                        parent.AddChild (ConvertAttributeSection (attr), role);
 
959
                                }
 
960
                        }
 
961
 
 
962
                        public void AddAttributeSection (AstNode parent, Attributes attrs)
 
963
                        {
 
964
                                AddAttributeSection (parent, attrs, EntityDeclaration.AttributeRole);
 
965
                        }
 
966
                        
 
967
                        public override void Visit (Indexer indexer)
 
968
                        {
 
969
                                IndexerDeclaration newIndexer = new IndexerDeclaration ();
 
970
                                AddAttributeSection (newIndexer, indexer);
 
971
                                var location = LocationsBag.GetMemberLocation (indexer);
 
972
                                AddModifiers (newIndexer, location);
 
973
                                newIndexer.AddChild (ConvertToType (indexer.TypeExpression), Roles.Type);
 
974
                                AddExplicitInterface (newIndexer, indexer.MemberName);
 
975
                                var name = indexer.MemberName;
 
976
                                newIndexer.AddChild (new CSharpTokenNode(Convert (name.Location), IndexerDeclaration.ThisKeywordRole), IndexerDeclaration.ThisKeywordRole);
 
977
                                
 
978
                                if (location != null && location.Count > 0)
 
979
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBracket), Roles.LBracket);
 
980
                                AddParameter (newIndexer, indexer.ParameterInfo);
 
981
                                if (location != null && location.Count > 1)
 
982
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBracket), Roles.RBracket);
 
983
                                
 
984
                                if (location != null && location.Count > 2)
 
985
                                        newIndexer.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
 
986
                                if (indexer.Get != null) {
 
987
                                        Accessor getAccessor = new Accessor ();
 
988
                                        var getLocation = LocationsBag.GetMemberLocation (indexer.Get);
 
989
                                        AddAttributeSection (getAccessor, indexer.Get);
 
990
                                        AddModifiers (getAccessor, getLocation);
 
991
                                        if (getLocation != null)
 
992
                                                getAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Get.Location), PropertyDeclaration.GetKeywordRole), PropertyDeclaration.GetKeywordRole);
 
993
                                        if (indexer.Get.Block != null) {
 
994
                                                getAccessor.AddChild ((BlockStatement)indexer.Get.Block.Accept (this), Roles.Body);
 
995
                                        } else {
 
996
                                                if (getLocation != null && getLocation.Count > 0)
 
997
                                                        newIndexer.AddChild (new CSharpTokenNode (Convert (getLocation [0]), Roles.Semicolon), Roles.Semicolon);
 
998
                                        }
 
999
                                        newIndexer.AddChild (getAccessor, PropertyDeclaration.GetterRole);
 
1000
                                }
 
1001
                                
 
1002
                                if (indexer.Set != null) {
 
1003
                                        Accessor setAccessor = new Accessor ();
 
1004
                                        var setLocation = LocationsBag.GetMemberLocation (indexer.Set);
 
1005
                                        AddAttributeSection (setAccessor, indexer.Set);
 
1006
                                        AddModifiers (setAccessor, setLocation);
 
1007
                                        if (setLocation != null)
 
1008
                                                setAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Set.Location), PropertyDeclaration.SetKeywordRole), PropertyDeclaration.SetKeywordRole);
 
1009
                                        
 
1010
                                        if (indexer.Set.Block != null) {
 
1011
                                                setAccessor.AddChild ((BlockStatement)indexer.Set.Block.Accept (this), Roles.Body);
 
1012
                                        } else {
 
1013
                                                if (setLocation != null && setLocation.Count > 0)
 
1014
                                                        newIndexer.AddChild (new CSharpTokenNode (Convert (setLocation [0]), Roles.Semicolon), Roles.Semicolon);
 
1015
                                        }
 
1016
                                        newIndexer.AddChild (setAccessor, PropertyDeclaration.SetterRole);
 
1017
                                }
 
1018
                                
 
1019
                                if (location != null) {
 
1020
                                        if (location.Count > 3)
 
1021
                                                newIndexer.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
 
1022
                                } else {
 
1023
                                        // parser error, set end node to max value.
 
1024
                                        newIndexer.AddChild (new ErrorNode (), Roles.Error);
 
1025
                                }
 
1026
                                typeStack.Peek ().AddChild (newIndexer, Roles.TypeMemberRole);
 
1027
                        }
 
1028
                        
 
1029
                        public override void Visit (Method m)
 
1030
                        {
 
1031
                                MethodDeclaration newMethod = new MethodDeclaration ();
 
1032
                                AddAttributeSection (newMethod, m);
 
1033
                                var location = LocationsBag.GetMemberLocation (m);
 
1034
                                AddModifiers (newMethod, location);
 
1035
                                newMethod.AddChild (ConvertToType (m.TypeExpression), Roles.Type);
 
1036
                                AddExplicitInterface (newMethod, m.MethodName);
 
1037
                                newMethod.AddChild (Identifier.Create (m.MethodName.Name, Convert (m.Location)), Roles.Identifier);
 
1038
                                
 
1039
                                AddTypeParameters (newMethod, m.MemberName);
 
1040
                                
 
1041
                                if (location != null && location.Count > 0)
 
1042
                                        newMethod.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1043
                                AddParameter (newMethod, m.ParameterInfo);
 
1044
                                
 
1045
                                if (location != null && location.Count > 1)
 
1046
                                        newMethod.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1047
                                
 
1048
                                AddConstraints (newMethod, m.CurrentTypeParameters);
 
1049
                                
 
1050
                                if (m.Block != null) {
 
1051
                                        var bodyBlock = (BlockStatement)m.Block.Accept (this);
 
1052
//                                      if (m.Block is ToplevelBlock) {
 
1053
//                                              newMethod.AddChild (bodyBlock.FirstChild.NextSibling, Roles.Body);
 
1054
//                                      } else {
 
1055
                                        newMethod.AddChild (bodyBlock, Roles.Body);
 
1056
//                                      }
 
1057
                                } else if (location != null) {
 
1058
                                        if (location.Count < 3) {
 
1059
                                                // parser error, set end node to max value.
 
1060
                                                newMethod.AddChild (new ErrorNode (), Roles.Error);
 
1061
                                        } else {
 
1062
                                                newMethod.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.Semicolon), Roles.Semicolon);
 
1063
                                        }
 
1064
                                }
 
1065
                                typeStack.Peek ().AddChild (newMethod, Roles.TypeMemberRole);
 
1066
                        }
 
1067
                        
 
1068
                        static Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> modifierTable = new Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> ();
 
1069
                        static string[] keywordTable;
 
1070
                        
 
1071
                        static ConversionVisitor ()
 
1072
                        {
 
1073
                                modifierTable [Mono.CSharp.Modifiers.NEW] = ICSharpCode.NRefactory.CSharp.Modifiers.New;
 
1074
                                modifierTable [Mono.CSharp.Modifiers.PUBLIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
 
1075
                                modifierTable [Mono.CSharp.Modifiers.PROTECTED] = ICSharpCode.NRefactory.CSharp.Modifiers.Protected;
 
1076
                                modifierTable [Mono.CSharp.Modifiers.PRIVATE] = ICSharpCode.NRefactory.CSharp.Modifiers.Private;
 
1077
                                modifierTable [Mono.CSharp.Modifiers.INTERNAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Internal;
 
1078
                                modifierTable [Mono.CSharp.Modifiers.ABSTRACT] = ICSharpCode.NRefactory.CSharp.Modifiers.Abstract;
 
1079
                                modifierTable [Mono.CSharp.Modifiers.VIRTUAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Virtual;
 
1080
                                modifierTable [Mono.CSharp.Modifiers.SEALED] = ICSharpCode.NRefactory.CSharp.Modifiers.Sealed;
 
1081
                                modifierTable [Mono.CSharp.Modifiers.STATIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Static;
 
1082
                                modifierTable [Mono.CSharp.Modifiers.OVERRIDE] = ICSharpCode.NRefactory.CSharp.Modifiers.Override;
 
1083
                                modifierTable [Mono.CSharp.Modifiers.READONLY] = ICSharpCode.NRefactory.CSharp.Modifiers.Readonly;
 
1084
                                modifierTable [Mono.CSharp.Modifiers.PARTIAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Partial;
 
1085
                                modifierTable [Mono.CSharp.Modifiers.EXTERN] = ICSharpCode.NRefactory.CSharp.Modifiers.Extern;
 
1086
                                modifierTable [Mono.CSharp.Modifiers.VOLATILE] = ICSharpCode.NRefactory.CSharp.Modifiers.Volatile;
 
1087
                                modifierTable [Mono.CSharp.Modifiers.UNSAFE] = ICSharpCode.NRefactory.CSharp.Modifiers.Unsafe;
 
1088
                                modifierTable [Mono.CSharp.Modifiers.ASYNC] = ICSharpCode.NRefactory.CSharp.Modifiers.Async;
 
1089
                                
 
1090
                                keywordTable = new string[255];
 
1091
                                for (int i = 0; i< keywordTable.Length; i++)
 
1092
                                        keywordTable [i] = "unknown";
 
1093
                                
 
1094
                                keywordTable [(int)BuiltinTypeSpec.Type.Other] = "void";
 
1095
                                keywordTable [(int)BuiltinTypeSpec.Type.String] = "string";
 
1096
                                keywordTable [(int)BuiltinTypeSpec.Type.Int] = "int";
 
1097
                                keywordTable [(int)BuiltinTypeSpec.Type.Object] = "object";
 
1098
                                keywordTable [(int)BuiltinTypeSpec.Type.Float] = "float";
 
1099
                                keywordTable [(int)BuiltinTypeSpec.Type.Double] = "double";
 
1100
                                keywordTable [(int)BuiltinTypeSpec.Type.Long] = "long";
 
1101
                                keywordTable [(int)BuiltinTypeSpec.Type.Byte] = "byte";
 
1102
                                keywordTable [(int)BuiltinTypeSpec.Type.UInt] = "uint";
 
1103
                                keywordTable [(int)BuiltinTypeSpec.Type.ULong] = "ulong";
 
1104
                                keywordTable [(int)BuiltinTypeSpec.Type.Short] = "short";
 
1105
                                keywordTable [(int)BuiltinTypeSpec.Type.UShort] = "ushort";
 
1106
                                keywordTable [(int)BuiltinTypeSpec.Type.SByte] = "sbyte";
 
1107
                                keywordTable [(int)BuiltinTypeSpec.Type.Decimal] = "decimal";
 
1108
                                keywordTable [(int)BuiltinTypeSpec.Type.Char] = "char";
 
1109
                                keywordTable [(int)BuiltinTypeSpec.Type.Bool] = "bool";
 
1110
                        }
 
1111
                        
 
1112
                        void AddModifiers (EntityDeclaration parent, LocationsBag.MemberLocations location)
 
1113
                        {
 
1114
                                if (location == null || location.Modifiers == null)
 
1115
                                        return;
 
1116
                                foreach (var modifier in location.Modifiers) {
 
1117
                                        ICSharpCode.NRefactory.CSharp.Modifiers mod;
 
1118
                                        if (!modifierTable.TryGetValue (modifier.Item1, out mod)) {
 
1119
                                                Console.WriteLine ("modifier " + modifier.Item1 + " can't be converted,");
 
1120
                                        }
 
1121
                                        
 
1122
                                        parent.AddChild (new CSharpModifierToken (Convert (modifier.Item2), mod), EntityDeclaration.ModifierRole);
 
1123
                                }
 
1124
                        }
 
1125
                        
 
1126
                        public override void Visit (Property p)
 
1127
                        {
 
1128
                                PropertyDeclaration newProperty = new PropertyDeclaration ();
 
1129
                                AddAttributeSection (newProperty, p);
 
1130
                                var location = LocationsBag.GetMemberLocation (p);
 
1131
                                AddModifiers (newProperty, location);
 
1132
                                newProperty.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
 
1133
                                AddExplicitInterface (newProperty, p.MemberName);
 
1134
                                newProperty.AddChild (Identifier.Create (p.MemberName.Name, Convert (p.Location)), Roles.Identifier);
 
1135
                                
 
1136
                                if (location != null && location.Count > 0)
 
1137
                                        newProperty.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBrace), Roles.LBrace);
 
1138
                                
 
1139
                                Accessor getAccessor = null;
 
1140
                                if (p.Get != null) {
 
1141
                                        getAccessor = new Accessor ();
 
1142
                                        AddAttributeSection (getAccessor, p.Get);
 
1143
                                        var getLocation = LocationsBag.GetMemberLocation (p.Get);
 
1144
                                        AddModifiers (getAccessor, getLocation);
 
1145
                                        getAccessor.AddChild (new CSharpTokenNode (Convert (p.Get.Location), PropertyDeclaration.GetKeywordRole), PropertyDeclaration.GetKeywordRole);
 
1146
                                        
 
1147
                                        if (p.Get.Block != null) {
 
1148
                                                getAccessor.AddChild ((BlockStatement)p.Get.Block.Accept (this), Roles.Body);
 
1149
                                        } else {
 
1150
                                                if (getLocation != null && getLocation.Count > 0)
 
1151
                                                        getAccessor.AddChild (new CSharpTokenNode (Convert (getLocation [0]), Roles.Semicolon), Roles.Semicolon);
 
1152
                                        }
 
1153
                                }
 
1154
                                
 
1155
                                Accessor setAccessor = null;
 
1156
                                if (p.Set != null) {
 
1157
                                        setAccessor = new Accessor ();
 
1158
                                        AddAttributeSection (setAccessor, p.Set);
 
1159
                                        var setLocation = LocationsBag.GetMemberLocation (p.Set);
 
1160
                                        AddModifiers (setAccessor, setLocation);
 
1161
                                        setAccessor.AddChild (new CSharpTokenNode (Convert (p.Set.Location), PropertyDeclaration.SetKeywordRole), PropertyDeclaration.SetKeywordRole);
 
1162
                                        
 
1163
                                        if (p.Set.Block != null) {
 
1164
                                                setAccessor.AddChild ((BlockStatement)p.Set.Block.Accept (this), Roles.Body);
 
1165
                                        } else {
 
1166
                                                if (setLocation != null && setLocation.Count > 0)
 
1167
                                                        setAccessor.AddChild (new CSharpTokenNode (Convert (setLocation [0]), Roles.Semicolon), Roles.Semicolon);
 
1168
                                        }
 
1169
                                }
 
1170
                                if (getAccessor != null && setAccessor != null) {
 
1171
                                        if (getAccessor.StartLocation < setAccessor.StartLocation) {
 
1172
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
 
1173
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
 
1174
                                        } else {
 
1175
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
 
1176
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
 
1177
                                        }
 
1178
                                } else {
 
1179
                                        if (getAccessor != null)
 
1180
                                                newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
 
1181
                                        if (setAccessor != null)
 
1182
                                                newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
 
1183
                                }
 
1184
                                
 
1185
                                if (location != null && location.Count > 1) {
 
1186
                                        newProperty.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBrace), Roles.RBrace);
 
1187
                                } else {
 
1188
                                        // parser error, set end node to max value.
 
1189
                                        newProperty.AddChild (new ErrorNode (), Roles.Error);
 
1190
                                }
 
1191
                                
 
1192
                                typeStack.Peek ().AddChild (newProperty, Roles.TypeMemberRole);
 
1193
                        }
 
1194
                        
 
1195
                        public override void Visit (Constructor c)
 
1196
                        {
 
1197
                                ConstructorDeclaration newConstructor = new ConstructorDeclaration ();
 
1198
                                AddAttributeSection (newConstructor, c);
 
1199
                                var location = LocationsBag.GetMemberLocation (c);
 
1200
                                AddModifiers (newConstructor, location);
 
1201
                                newConstructor.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
 
1202
                                if (location != null && location.Count > 0)
 
1203
                                        newConstructor.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1204
                                
 
1205
                                AddParameter (newConstructor, c.ParameterInfo);
 
1206
                                if (location != null && location.Count > 1)
 
1207
                                        newConstructor.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1208
                                
 
1209
                                if (c.Initializer != null) {
 
1210
                                        var initializer = new ConstructorInitializer ();
 
1211
                                        initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This;
 
1212
                                        var initializerLocation = LocationsBag.GetLocations (c.Initializer);
 
1213
                                        
 
1214
                                        if (initializerLocation != null)
 
1215
                                                newConstructor.AddChild (new CSharpTokenNode (Convert (initializerLocation [0]), Roles.Colon), Roles.Colon);
 
1216
                                        
 
1217
                                        if (initializerLocation != null && initializerLocation.Count > 1) {
 
1218
                                                // this and base has the same length
 
1219
                                                var r = initializer.ConstructorInitializerType == ConstructorInitializerType.This ? ConstructorInitializer.ThisKeywordRole : ConstructorInitializer.BaseKeywordRole;
 
1220
                                                initializer.AddChild (new CSharpTokenNode (Convert (c.Initializer.Location), r), r);
 
1221
                                                initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [1]), Roles.LPar), Roles.LPar);
 
1222
                                                AddArguments (initializer, LocationsBag.GetLocations (c.Initializer.Arguments), c.Initializer.Arguments);
 
1223
                                                initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [2]), Roles.RPar), Roles.RPar);
 
1224
                                                newConstructor.AddChild (initializer, ConstructorDeclaration.InitializerRole);
 
1225
                                        }
 
1226
                                }
 
1227
                                
 
1228
                                if (c.Block != null)
 
1229
                                        newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), Roles.Body);
 
1230
                                typeStack.Peek ().AddChild (newConstructor, Roles.TypeMemberRole);
 
1231
                        }
 
1232
                        
 
1233
                        public override void Visit (Destructor d)
 
1234
                        {
 
1235
                                DestructorDeclaration newDestructor = new DestructorDeclaration ();
 
1236
                                AddAttributeSection (newDestructor, d);
 
1237
                                var location = LocationsBag.GetMemberLocation (d);
 
1238
                                AddModifiers (newDestructor, location);
 
1239
                                if (location != null && location.Count > 0)
 
1240
                                        newDestructor.AddChild (new CSharpTokenNode (Convert (location [0]), DestructorDeclaration.TildeRole), DestructorDeclaration.TildeRole);
 
1241
                                newDestructor.AddChild (Identifier.Create (d.Identifier, Convert (d.MemberName.Location)), Roles.Identifier);
 
1242
                                
 
1243
                                if (location != null && location.Count > 1) {
 
1244
                                        newDestructor.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
 
1245
                                        
 
1246
                                        if (location.Count > 2)
 
1247
                                                newDestructor.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
 
1248
                                }
 
1249
                                
 
1250
                                if (d.Block != null)
 
1251
                                        newDestructor.AddChild ((BlockStatement)d.Block.Accept (this), Roles.Body);
 
1252
                                
 
1253
                                typeStack.Peek ().AddChild (newDestructor, Roles.TypeMemberRole);
 
1254
                        }
 
1255
                        
 
1256
                        public override void Visit (EventField e)
 
1257
                        {
 
1258
                                EventDeclaration newEvent = new EventDeclaration ();
 
1259
                                AddAttributeSection (newEvent, e);
 
1260
                                var location = LocationsBag.GetMemberLocation (e);
 
1261
                                int l = 0;
 
1262
                                AddModifiers (newEvent, location);
 
1263
                                
 
1264
                                if (location != null && location.Count > 0)
 
1265
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [l++]), EventDeclaration.EventKeywordRole), EventDeclaration.EventKeywordRole);
 
1266
                                newEvent.AddChild (ConvertToType (e.TypeExpression), Roles.Type);
 
1267
                                
 
1268
                                VariableInitializer variable = new VariableInitializer ();
 
1269
                                variable.AddChild (Identifier.Create (e.MemberName.Name, Convert (e.MemberName.Location)), Roles.Identifier);
 
1270
                                
 
1271
                                if (e.Initializer != null) {
 
1272
                                        if (location != null && location.Count > l)
 
1273
                                                variable.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.Assign), Roles.Assign);
 
1274
                                        variable.AddChild ((Expression)e.Initializer.Accept (this), Roles.Expression);
 
1275
                                }
 
1276
                                newEvent.AddChild (variable, Roles.Variable);
 
1277
                                if (e.Declarators != null) {
 
1278
                                        foreach (var decl in e.Declarators) {
 
1279
                                                var declLoc = LocationsBag.GetLocations (decl);
 
1280
                                                if (declLoc != null)
 
1281
                                                        newEvent.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
 
1282
                                                
 
1283
                                                variable = new VariableInitializer ();
 
1284
                                                variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
 
1285
 
 
1286
                                                if (decl.Initializer != null) {
 
1287
                                                        if (declLoc != null)
 
1288
                                                                variable.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
 
1289
                                                        variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
1290
                                                }
 
1291
                                                newEvent.AddChild (variable, Roles.Variable);
 
1292
                                        }
 
1293
                                }
 
1294
                                
 
1295
                                if (location != null && location.Count > l)
 
1296
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.Semicolon), Roles.Semicolon);
 
1297
                                
 
1298
                                typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
 
1299
                        }
 
1300
 
 
1301
                        void AddExplicitInterface (AstNode parent, MemberName memberName)
 
1302
                        {
 
1303
                                if (memberName == null || memberName.ExplicitInterface == null)
 
1304
                                        return;
 
1305
                                
 
1306
                                parent.AddChild (ConvertToType (memberName.ExplicitInterface), EntityDeclaration.PrivateImplementationTypeRole);
 
1307
                                var privateImplTypeLoc = LocationsBag.GetLocations (memberName.ExplicitInterface);
 
1308
                                if (privateImplTypeLoc != null)
 
1309
                                        parent.AddChild (new CSharpTokenNode (Convert (privateImplTypeLoc [0]), Roles.Dot), Roles.Dot);
 
1310
                        }
 
1311
                        
 
1312
                        public override void Visit (EventProperty ep)
 
1313
                        {
 
1314
                                CustomEventDeclaration newEvent = new CustomEventDeclaration ();
 
1315
                                AddAttributeSection (newEvent, ep);
 
1316
                                var location = LocationsBag.GetMemberLocation (ep);
 
1317
                                AddModifiers (newEvent, location);
 
1318
                                
 
1319
                                if (location != null && location.Count > 0)
 
1320
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [0]), CustomEventDeclaration.EventKeywordRole), CustomEventDeclaration.EventKeywordRole);
 
1321
                                newEvent.AddChild (ConvertToType (ep.TypeExpression), Roles.Type);
 
1322
                                
 
1323
                                AddExplicitInterface (newEvent, ep.MemberName);
 
1324
                                
 
1325
                                newEvent.AddChild (Identifier.Create (ep.MemberName.Name, Convert (ep.Location)), Roles.Identifier);
 
1326
 
 
1327
                                if (location != null && location.Count >= 2)
 
1328
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LBrace), Roles.LBrace);
 
1329
                                
 
1330
                                if (ep.Add != null) {
 
1331
                                        Accessor addAccessor = new Accessor ();
 
1332
                                        AddAttributeSection (addAccessor, ep.Add);
 
1333
                                        var addLocation = LocationsBag.GetMemberLocation (ep.Add);
 
1334
                                        AddModifiers (addAccessor, addLocation);
 
1335
                                        addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location), CustomEventDeclaration.AddKeywordRole), CustomEventDeclaration.AddKeywordRole);
 
1336
                                        if (ep.Add.Block != null)
 
1337
                                                addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), Roles.Body);
 
1338
                                        newEvent.AddChild (addAccessor, CustomEventDeclaration.AddAccessorRole);
 
1339
                                }
 
1340
                                
 
1341
                                if (ep.Remove != null) {
 
1342
                                        Accessor removeAccessor = new Accessor ();
 
1343
                                        AddAttributeSection (removeAccessor, ep.Remove);
 
1344
                                        var removeLocation = LocationsBag.GetMemberLocation (ep.Remove);
 
1345
                                        AddModifiers (removeAccessor, removeLocation);
 
1346
                                        removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location), CustomEventDeclaration.RemoveKeywordRole), CustomEventDeclaration.RemoveKeywordRole);
 
1347
                                        
 
1348
                                        if (ep.Remove.Block != null)
 
1349
                                                removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), Roles.Body);
 
1350
                                        newEvent.AddChild (removeAccessor, CustomEventDeclaration.RemoveAccessorRole);
 
1351
                                }
 
1352
                                if (location != null && location.Count >= 3) {
 
1353
                                        newEvent.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RBrace), Roles.RBrace);
 
1354
                                } else {
 
1355
                                        // parser error, set end node to max value.
 
1356
                                        newEvent.AddChild (new ErrorNode (), Roles.Error);
 
1357
                                }
 
1358
                                
 
1359
                                typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
 
1360
                        }
 
1361
                        
 
1362
                        #endregion
 
1363
                        
 
1364
                        #region Statements
 
1365
                        public override object Visit (Mono.CSharp.Statement stmt)
 
1366
                        {
 
1367
                                Console.WriteLine ("unknown statement:" + stmt);
 
1368
                                return null;
 
1369
                        }
 
1370
                        
 
1371
                        public override object Visit (BlockVariableDeclaration blockVariableDeclaration)
 
1372
                        {
 
1373
                                var result = new VariableDeclarationStatement ();
 
1374
                                result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
 
1375
                                
 
1376
                                var varInit = new VariableInitializer ();
 
1377
                                var location = LocationsBag.GetLocations (blockVariableDeclaration);
 
1378
                                varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
 
1379
                                if (blockVariableDeclaration.Initializer != null) {
 
1380
                                        if (location != null && location.Count > 0)
 
1381
                                                varInit.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Assign), Roles.Assign);
 
1382
                                        varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
 
1383
                                }
 
1384
                                
 
1385
                                result.AddChild (varInit, Roles.Variable);
 
1386
                                
 
1387
                                if (blockVariableDeclaration.Declarators != null) {
 
1388
                                        foreach (var decl in blockVariableDeclaration.Declarators) {
 
1389
                                                var loc = LocationsBag.GetLocations (decl);
 
1390
                                                var init = new VariableInitializer ();
 
1391
                                                if (loc != null && loc.Count > 0)
 
1392
                                                        result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
 
1393
                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
 
1394
                                                if (decl.Initializer != null) {
 
1395
                                                        if (loc != null && loc.Count > 1)
 
1396
                                                                init.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Assign), Roles.Assign);
 
1397
                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
1398
                                                } else {
 
1399
                                                }
 
1400
                                                result.AddChild (init, Roles.Variable);
 
1401
                                        }
 
1402
                                }
 
1403
                                if (location != null && (blockVariableDeclaration.Initializer == null || location.Count > 1))
 
1404
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.Semicolon), Roles.Semicolon);
 
1405
                                return result;
 
1406
                        }
 
1407
                        
 
1408
                        public override object Visit (BlockConstantDeclaration blockVariableDeclaration)
 
1409
                        {
 
1410
                                var result = new VariableDeclarationStatement ();
 
1411
                                
 
1412
                                var location = LocationsBag.GetLocations (blockVariableDeclaration);
 
1413
                                if (location != null && location.Count > 0)
 
1414
                                        result.AddChild (new CSharpModifierToken (Convert (location [0]), ICSharpCode.NRefactory.CSharp.Modifiers.Const), VariableDeclarationStatement.ModifierRole);
 
1415
                                
 
1416
                                result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
 
1417
                                
 
1418
                                var varInit = new VariableInitializer ();
 
1419
                                varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
 
1420
                                if (blockVariableDeclaration.Initializer != null) {
 
1421
                                        if (location != null && location.Count > 1)
 
1422
                                                varInit.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Assign), Roles.Assign);
 
1423
                                        varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
 
1424
                                }
 
1425
                                
 
1426
                                result.AddChild (varInit, Roles.Variable);
 
1427
                                
 
1428
                                if (blockVariableDeclaration.Declarators != null) {
 
1429
                                        foreach (var decl in blockVariableDeclaration.Declarators) {
 
1430
                                                var loc = LocationsBag.GetLocations (decl);
 
1431
                                                var init = new VariableInitializer ();
 
1432
                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
 
1433
                                                if (decl.Initializer != null) {
 
1434
                                                        if (loc != null)
 
1435
                                                                init.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
 
1436
                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
1437
                                                        if (loc != null && loc.Count > 1)
 
1438
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Comma), Roles.Comma);
 
1439
                                                } else {
 
1440
                                                        if (loc != null && loc.Count > 0)
 
1441
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
 
1442
                                                }
 
1443
                                                result.AddChild (init, Roles.Variable);
 
1444
                                        }
 
1445
                                }
 
1446
                                if (location != null) {
 
1447
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.Semicolon), Roles.Semicolon);
 
1448
                                } else {
 
1449
                                        // parser error, set end node to max value.
 
1450
                                        result.AddChild (new ErrorNode (), Roles.Error);
 
1451
                                }
 
1452
                                return result;
 
1453
                        }
 
1454
                        
 
1455
                        public override object Visit (Mono.CSharp.EmptyStatement emptyStatement)
 
1456
                        {
 
1457
                                var result = new EmptyStatement ();
 
1458
                                result.Location = Convert (emptyStatement.loc);
 
1459
                                return result;
 
1460
                        }
 
1461
                        
 
1462
                        public override object Visit (Mono.CSharp.EmptyExpression emptyExpression)
 
1463
                        {
 
1464
                                return new ICSharpCode.NRefactory.CSharp.EmptyExpression (Convert (emptyExpression.Location));
 
1465
                        }
 
1466
                        
 
1467
                        public override object Visit (Mono.CSharp.ErrorExpression emptyExpression)
 
1468
                        {
 
1469
                                return new ICSharpCode.NRefactory.CSharp.ErrorExpression (Convert (emptyExpression.Location));
 
1470
                        }
 
1471
                        
 
1472
                        public override object Visit (EmptyExpressionStatement emptyExpressionStatement)
 
1473
                        {
 
1474
                                return new EmptyExpression (Convert (emptyExpressionStatement.Location));
 
1475
                        }
 
1476
                        
 
1477
                        public override object Visit (If ifStatement)
 
1478
                        {
 
1479
                                var result = new IfElseStatement ();
 
1480
                                
 
1481
                                var location = LocationsBag.GetLocations (ifStatement);
 
1482
                                
 
1483
                                result.AddChild (new CSharpTokenNode (Convert (ifStatement.loc), IfElseStatement.IfKeywordRole), IfElseStatement.IfKeywordRole);
 
1484
                                if (location != null)
 
1485
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1486
                                if (ifStatement.Expr != null)
 
1487
                                        result.AddChild ((Expression)ifStatement.Expr.Accept (this), Roles.Condition);
 
1488
                                if (location != null && location.Count > 1)
 
1489
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1490
                                
 
1491
                                if (ifStatement.TrueStatement != null)
 
1492
                                        result.AddChild ((Statement)ifStatement.TrueStatement.Accept (this), IfElseStatement.TrueRole);
 
1493
                                
 
1494
                                if (ifStatement.FalseStatement != null) {
 
1495
                                        if (location != null && location.Count > 2)
 
1496
                                                result.AddChild (new CSharpTokenNode (Convert (location [2]), IfElseStatement.ElseKeywordRole), IfElseStatement.ElseKeywordRole);
 
1497
                                        result.AddChild ((Statement)ifStatement.FalseStatement.Accept (this), IfElseStatement.FalseRole);
 
1498
                                }
 
1499
                                
 
1500
                                return result;
 
1501
                        }
 
1502
                        
 
1503
                        public override object Visit (Do doStatement)
 
1504
                        {
 
1505
                                var result = new DoWhileStatement ();
 
1506
                                var location = LocationsBag.GetLocations (doStatement);
 
1507
                                result.AddChild (new CSharpTokenNode (Convert (doStatement.loc), DoWhileStatement.DoKeywordRole), DoWhileStatement.DoKeywordRole);
 
1508
                                if (doStatement.EmbeddedStatement != null)
 
1509
                                        result.AddChild ((Statement)doStatement.EmbeddedStatement.Accept (this), Roles.EmbeddedStatement);
 
1510
                                if (location != null)
 
1511
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), DoWhileStatement.WhileKeywordRole), DoWhileStatement.WhileKeywordRole);
 
1512
                                if (location != null && location.Count > 1)
 
1513
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
 
1514
                                if (doStatement.expr != null)
 
1515
                                        result.AddChild ((Expression)doStatement.expr.Accept (this), Roles.Condition);
 
1516
                                if (location != null && location.Count > 2) {
 
1517
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
 
1518
                                        if (location.Count > 3)
 
1519
                                                result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.Semicolon), Roles.Semicolon);
 
1520
                                }
 
1521
                                
 
1522
                                return result;
 
1523
                        }
 
1524
                        
 
1525
                        public override object Visit (While whileStatement)
 
1526
                        {
 
1527
                                var result = new WhileStatement ();
 
1528
                                var location = LocationsBag.GetLocations (whileStatement);
 
1529
                                result.AddChild (new CSharpTokenNode (Convert (whileStatement.loc), WhileStatement.WhileKeywordRole), WhileStatement.WhileKeywordRole);
 
1530
                                
 
1531
                                if (location != null)
 
1532
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1533
                                if (whileStatement.expr != null)
 
1534
                                        result.AddChild ((Expression)whileStatement.expr.Accept (this), Roles.Condition);
 
1535
                                if (location != null && location.Count > 1)
 
1536
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1537
                                if (whileStatement.Statement != null)
 
1538
                                        result.AddChild ((Statement)whileStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
1539
                                return result;
 
1540
                        }
 
1541
                        
 
1542
                        void AddStatementOrList (ForStatement forStatement, Mono.CSharp.Statement init, Role<Statement> role)
 
1543
                        {
 
1544
                                if (init == null)
 
1545
                                        return;
 
1546
                                if (init is StatementList) {
 
1547
                                        foreach (var stmt in ((StatementList)init).Statements) {
 
1548
                                                forStatement.AddChild ((Statement)stmt.Accept (this), role);
 
1549
                                        }
 
1550
                                } else if (init is Mono.CSharp.EmptyStatement) {
 
1551
                                        
 
1552
                                } else {
 
1553
                                        forStatement.AddChild ((Statement)init.Accept (this), role);
 
1554
                                }
 
1555
                        }
 
1556
 
 
1557
                        public override object Visit (For forStatement)
 
1558
                        {
 
1559
                                var result = new ForStatement ();
 
1560
                                
 
1561
                                var location = LocationsBag.GetLocations (forStatement);
 
1562
                                
 
1563
                                result.AddChild (new CSharpTokenNode (Convert (forStatement.loc), ForStatement.ForKeywordRole), ForStatement.ForKeywordRole);
 
1564
                                if (location != null)
 
1565
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1566
                                
 
1567
                                AddStatementOrList (result, forStatement.Initializer, ForStatement.InitializerRole);
 
1568
                                
 
1569
                                if (location != null && location.Count > 1)
 
1570
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
1571
                                if (forStatement.Condition != null)
 
1572
                                        result.AddChild ((Expression)forStatement.Condition.Accept (this), Roles.Condition);
 
1573
                                if (location != null && location.Count >= 3)
 
1574
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.Semicolon), Roles.Semicolon);
 
1575
                                
 
1576
                                AddStatementOrList (result, forStatement.Iterator, ForStatement.IteratorRole);
 
1577
                                
 
1578
                                if (location != null && location.Count >= 4)
 
1579
                                        result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RPar), Roles.RPar);
 
1580
                                
 
1581
                                if (forStatement.Statement != null)
 
1582
                                        result.AddChild ((Statement)forStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
1583
                                
 
1584
                                return result;
 
1585
                        }
 
1586
                        
 
1587
                        public override object Visit (StatementExpression statementExpression)
 
1588
                        {
 
1589
                                var result = new ExpressionStatement ();
 
1590
                                var expr = statementExpression.Expr.Accept (this) as Expression;
 
1591
                                if (expr != null)
 
1592
                                        result.AddChild ((Expression)expr, Roles.Expression);
 
1593
                                var location = LocationsBag.GetLocations (statementExpression);
 
1594
                                if (location != null)
 
1595
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1596
                                return result;
 
1597
                        }
 
1598
                        
 
1599
                        public override object Visit (StatementErrorExpression statementErrorExpression)
 
1600
                        {
 
1601
                                var result = new ExpressionStatement ();
 
1602
                                var expr = statementErrorExpression.Expr.Accept (this) as Expression;
 
1603
                                if (expr != null)
 
1604
                                        result.AddChild ((Expression)expr, Roles.Expression);
 
1605
                                var location = LocationsBag.GetLocations (statementErrorExpression);
 
1606
                                if (location != null)
 
1607
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1608
                                return result;
 
1609
                        }
 
1610
                        
 
1611
                        public override object Visit (InvalidStatementExpression statementExpression)
 
1612
                        {
 
1613
                                var result = new ExpressionStatement ();
 
1614
                                if (statementExpression.Expression == null)
 
1615
                                        return result;
 
1616
                                var expr = statementExpression.Expression.Accept (this) as Expression;
 
1617
                                if (expr != null)
 
1618
                                        result.AddChild (expr, Roles.Expression);
 
1619
                                var location = LocationsBag.GetLocations (statementExpression);
 
1620
                                if (location != null)
 
1621
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1622
                                return result;
 
1623
                        }
 
1624
                        
 
1625
                        public override object Visit (Return returnStatement)
 
1626
                        {
 
1627
                                var result = new ReturnStatement ();
 
1628
                                
 
1629
                                result.AddChild (new CSharpTokenNode (Convert (returnStatement.loc), ReturnStatement.ReturnKeywordRole), ReturnStatement.ReturnKeywordRole);
 
1630
                                if (returnStatement.Expr != null)
 
1631
                                        result.AddChild ((Expression)returnStatement.Expr.Accept (this), Roles.Expression);
 
1632
                                
 
1633
                                var location = LocationsBag.GetLocations (returnStatement);
 
1634
                                if (location != null)
 
1635
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1636
                                
 
1637
                                return result;
 
1638
                        }
 
1639
                        
 
1640
                        public override object Visit (Goto gotoStatement)
 
1641
                        {
 
1642
                                var result = new GotoStatement ();
 
1643
                                var location = LocationsBag.GetLocations (gotoStatement);
 
1644
                                result.AddChild (new CSharpTokenNode (Convert (gotoStatement.loc), GotoStatement.GotoKeywordRole), GotoStatement.GotoKeywordRole);
 
1645
                                var loc = location != null ? Convert (location [0]) : TextLocation.Empty;
 
1646
                                result.AddChild (Identifier.Create (gotoStatement.Target, loc), Roles.Identifier);
 
1647
                                if (location != null && location.Count > 1)
 
1648
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
1649
                                
 
1650
                                return result;
 
1651
                        }
 
1652
                        
 
1653
                        public override object Visit (LabeledStatement labeledStatement)
 
1654
                        {
 
1655
                                var result = new LabelStatement ();
 
1656
                                result.AddChild (Identifier.Create (labeledStatement.Name, Convert (labeledStatement.loc)), Roles.Identifier);
 
1657
                                var location = LocationsBag.GetLocations (labeledStatement);
 
1658
                                if (location != null)
 
1659
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Colon), Roles.Colon);
 
1660
                                return result;
 
1661
                        }
 
1662
                        
 
1663
                        public override object Visit (GotoDefault gotoDefault)
 
1664
                        {
 
1665
                                var result = new GotoDefaultStatement ();
 
1666
                                result.AddChild (new CSharpTokenNode (Convert (gotoDefault.loc), GotoDefaultStatement.GotoKeywordRole), GotoDefaultStatement.GotoKeywordRole);
 
1667
                                var location = LocationsBag.GetLocations (gotoDefault);
 
1668
                                if (location != null) {
 
1669
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), GotoDefaultStatement.DefaultKeywordRole), GotoDefaultStatement.DefaultKeywordRole);
 
1670
                                        if (location.Count > 1)
 
1671
                                                result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
1672
                                }
 
1673
                                
 
1674
                                return result;
 
1675
                        }
 
1676
                        
 
1677
                        public override object Visit (GotoCase gotoCase)
 
1678
                        {
 
1679
                                var result = new GotoCaseStatement ();
 
1680
                                result.AddChild (new CSharpTokenNode (Convert (gotoCase.loc), GotoCaseStatement.GotoKeywordRole), GotoCaseStatement.GotoKeywordRole);
 
1681
                                
 
1682
                                var location = LocationsBag.GetLocations (gotoCase);
 
1683
                                if (location != null)
 
1684
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), GotoCaseStatement.CaseKeywordRole), GotoCaseStatement.CaseKeywordRole);
 
1685
                                if (gotoCase.Expr != null)
 
1686
                                        result.AddChild ((Expression)gotoCase.Expr.Accept (this), Roles.Expression);
 
1687
                                if (location != null && location.Count > 1)
 
1688
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
1689
                                return result;
 
1690
                        }
 
1691
                        
 
1692
                        public override object Visit (Throw throwStatement)
 
1693
                        {
 
1694
                                var result = new ThrowStatement ();
 
1695
                                var location = LocationsBag.GetLocations (throwStatement);
 
1696
                                
 
1697
                                result.AddChild (new CSharpTokenNode (Convert (throwStatement.loc), ThrowStatement.ThrowKeywordRole), ThrowStatement.ThrowKeywordRole);
 
1698
                                if (throwStatement.Expr != null)
 
1699
                                        result.AddChild ((Expression)throwStatement.Expr.Accept (this), Roles.Expression);
 
1700
                                if (location != null)
 
1701
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1702
                                return result;
 
1703
                        }
 
1704
                        
 
1705
                        public override object Visit (Break breakStatement)
 
1706
                        {
 
1707
                                var result = new BreakStatement ();
 
1708
                                var location = LocationsBag.GetLocations (breakStatement);
 
1709
                                
 
1710
                                result.AddChild (new CSharpTokenNode (Convert (breakStatement.loc), BreakStatement.BreakKeywordRole), BreakStatement.BreakKeywordRole);
 
1711
                                if (location != null)
 
1712
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1713
                                return result;
 
1714
                        }
 
1715
                        
 
1716
                        public override object Visit (Continue continueStatement)
 
1717
                        {
 
1718
                                var result = new ContinueStatement ();
 
1719
                                var location = LocationsBag.GetLocations (continueStatement);
 
1720
                                result.AddChild (new CSharpTokenNode (Convert (continueStatement.loc), ContinueStatement.ContinueKeywordRole), ContinueStatement.ContinueKeywordRole);
 
1721
                                if (location != null)
 
1722
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
 
1723
                                return result;
 
1724
                        }
 
1725
                        
 
1726
                        public static bool IsLower (Location left, Location right)
 
1727
                        {
 
1728
                                return left.Row < right.Row || left.Row == right.Row && left.Column < right.Column;
 
1729
                        }
 
1730
                        
 
1731
                        public UsingStatement CreateUsingStatement (Block blockStatement)
 
1732
                        {
 
1733
                                var usingResult = new UsingStatement ();
 
1734
                                Mono.CSharp.Statement cur = blockStatement.Statements [0];
 
1735
                                if (cur is Using) {
 
1736
                                        Using u = (Using)cur;
 
1737
                                        usingResult.AddChild (new CSharpTokenNode (Convert (u.loc), UsingStatement.UsingKeywordRole), UsingStatement.UsingKeywordRole);
 
1738
                                        usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation), Roles.LPar), Roles.LPar);
 
1739
                                        if (u.Variables != null) {
 
1740
                                                var initializer = new VariableInitializer () {
 
1741
                                                        NameToken = Identifier.Create (u.Variables.Variable.Name, Convert (u.Variables.Variable.Location)),
 
1742
                                                };
 
1743
                                                
 
1744
                                                var loc = LocationsBag.GetLocations (u.Variables);
 
1745
                                                if (loc != null)
 
1746
                                                        initializer.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
 
1747
                                                if (u.Variables.Initializer != null)
 
1748
                                                        initializer.Initializer = u.Variables.Initializer.Accept (this) as Expression;
 
1749
                                                
 
1750
                                                
 
1751
                                                var varDec = new VariableDeclarationStatement () {
 
1752
                                                        Type = ConvertToType (u.Variables.TypeExpression),
 
1753
                                                        Variables = { initializer }
 
1754
                                                };
 
1755
                                                
 
1756
                                                if (u.Variables.Declarators != null) {
 
1757
                                                        foreach (var decl in u.Variables.Declarators) {
 
1758
                                                                var declLoc = LocationsBag.GetLocations (decl);
 
1759
                                                                var init = new VariableInitializer ();
 
1760
                                                                if (declLoc != null && declLoc.Count > 0)
 
1761
                                                                        varDec.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
 
1762
                                                                init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
 
1763
                                                                if (decl.Initializer != null) {
 
1764
                                                                        if (declLoc != null && declLoc.Count > 1)
 
1765
                                                                                init.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
 
1766
                                                                        init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
1767
                                                                }
 
1768
                                                                varDec.AddChild (init, Roles.Variable);
 
1769
                                                        }
 
1770
                                                }
 
1771
                                                usingResult.AddChild (varDec, UsingStatement.ResourceAcquisitionRole);
 
1772
                                        }
 
1773
                                        cur = u.Statement;
 
1774
                                        usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation), Roles.RPar), Roles.RPar);
 
1775
                                        if (cur != null)
 
1776
                                                usingResult.AddChild ((Statement)cur.Accept (this), Roles.EmbeddedStatement);
 
1777
                                }
 
1778
                                return usingResult;
 
1779
                        }
 
1780
                        
 
1781
                        void AddBlockChildren (BlockStatement result, Block blockStatement, ref int curLocal)
 
1782
                        {
 
1783
                                if (convertTypeSystemMode) {
 
1784
                                        return;
 
1785
                                }
 
1786
                                foreach (Mono.CSharp.Statement stmt in blockStatement.Statements) {
 
1787
                                        if (stmt == null)
 
1788
                                                continue;
 
1789
                                        /*                                      if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) {
 
1790
                                                result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), Roles.Statement);
 
1791
                                                curLocal++;
 
1792
                                        }*/
 
1793
                                        if (stmt is Block && !(stmt is ToplevelBlock || stmt is ExplicitBlock)) {
 
1794
                                                AddBlockChildren (result, (Block)stmt, ref curLocal);
 
1795
                                        } else {
 
1796
                                                result.AddChild ((Statement)stmt.Accept (this), BlockStatement.StatementRole);
 
1797
                                        }
 
1798
                                }
 
1799
                        }
 
1800
                        
 
1801
                        public override object Visit (Block blockStatement)
 
1802
                        {
 
1803
                                if (blockStatement.IsCompilerGenerated && blockStatement.Statements.Any ()) {
 
1804
                                        if (blockStatement.Statements.First () is Using)
 
1805
                                                return CreateUsingStatement (blockStatement);
 
1806
                                        return blockStatement.Statements.Last ().Accept (this);
 
1807
                                }
 
1808
                                var result = new BlockStatement ();
 
1809
                                result.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation), Roles.LBrace), Roles.LBrace);
 
1810
                                int curLocal = 0;
 
1811
                                AddBlockChildren (result, blockStatement, ref curLocal);
 
1812
                                
 
1813
                                result.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation), Roles.RBrace), Roles.RBrace);
 
1814
                                return result;
 
1815
                        }
 
1816
 
 
1817
                        public override object Visit (Switch switchStatement)
 
1818
                        {
 
1819
                                var result = new SwitchStatement ();
 
1820
                                
 
1821
                                var location = LocationsBag.GetLocations (switchStatement);
 
1822
                                result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole);
 
1823
                                if (location != null)
 
1824
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1825
                                if (switchStatement.Expr != null)
 
1826
                                        result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression);
 
1827
                                if (location != null && location.Count > 1)
 
1828
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1829
                                if (location != null && location.Count > 2)
 
1830
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
 
1831
                                SwitchSection newSection = null;
 
1832
                                bool lastWasCase = false, added = true;
 
1833
                                if (switchStatement.Block != null) {
 
1834
                                        foreach (var child in switchStatement.Block.Statements) {
 
1835
                                                var statement = child.Accept(this);
 
1836
                                                var caseLabel = statement as CaseLabel;
 
1837
                                                if (caseLabel != null) {
 
1838
                                                        if (!lastWasCase) {
 
1839
                                                                newSection = new SwitchSection();
 
1840
                                                                added = false;
 
1841
                                                        }
 
1842
                                                        newSection.AddChild (caseLabel, SwitchSection.CaseLabelRole);
 
1843
                                                        lastWasCase = true;
 
1844
                                                } else {
 
1845
                                                        if (lastWasCase) {
 
1846
                                                                result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
 
1847
                                                                lastWasCase = false;
 
1848
                                                                added = true;
 
1849
                                                        }
 
1850
                                                        newSection.AddChild((Statement)statement, Roles.EmbeddedStatement);
 
1851
                                                }
 
1852
                                        }
 
1853
                                }
 
1854
                                if (!added)
 
1855
                                        result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
 
1856
 
 
1857
                                if (location != null && location.Count > 3) {
 
1858
                                        result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
 
1859
                                } else {
 
1860
                                        // parser error, set end node to max value.
 
1861
                                        result.AddChild (new ErrorNode (), Roles.Error);
 
1862
                                }
 
1863
                                
 
1864
                                return result;
 
1865
                        }
 
1866
 
 
1867
                        public override object Visit(SwitchLabel switchLabel)
 
1868
                        {
 
1869
                                var newLabel = new CaseLabel ();
 
1870
                                if (!switchLabel.IsDefault) {
 
1871
                                        newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole);
 
1872
                                        if (switchLabel.Label != null)
 
1873
                                                newLabel.AddChild ((Expression)switchLabel.Label.Accept (this), Roles.Expression);
 
1874
                                        var colonLocation = LocationsBag.GetLocations (switchLabel);
 
1875
                                        if (colonLocation != null)
 
1876
                                                newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon);
 
1877
                                } else {
 
1878
                                        newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole);
 
1879
                                        newLabel.AddChild (new CSharpTokenNode (new TextLocation (switchLabel.Location.Row, switchLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon);
 
1880
                                }
 
1881
                                return newLabel;
 
1882
                        }
 
1883
 
 
1884
 
 
1885
                        public override object Visit (Lock lockStatement)
 
1886
                        {
 
1887
                                var result = new LockStatement ();
 
1888
                                var location = LocationsBag.GetLocations (lockStatement);
 
1889
                                result.AddChild (new CSharpTokenNode (Convert (lockStatement.loc), LockStatement.LockKeywordRole), LockStatement.LockKeywordRole);
 
1890
                                
 
1891
                                if (location != null)
 
1892
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1893
                                if (lockStatement.Expr != null)
 
1894
                                        result.AddChild ((Expression)lockStatement.Expr.Accept (this), Roles.Expression);
 
1895
                                
 
1896
                                if (location != null && location.Count > 1)
 
1897
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1898
                                if (lockStatement.Statement != null)
 
1899
                                        result.AddChild ((Statement)lockStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
1900
                                
 
1901
                                return result;
 
1902
                        }
 
1903
                        
 
1904
                        public override object Visit (Unchecked uncheckedStatement)
 
1905
                        {
 
1906
                                var result = new UncheckedStatement ();
 
1907
                                result.AddChild (new CSharpTokenNode (Convert (uncheckedStatement.loc), UncheckedStatement.UncheckedKeywordRole), UncheckedStatement.UncheckedKeywordRole);
 
1908
                                if (uncheckedStatement.Block != null)
 
1909
                                        result.AddChild ((BlockStatement)uncheckedStatement.Block.Accept (this), Roles.Body);
 
1910
                                return result;
 
1911
                        }
 
1912
                        
 
1913
                        public override object Visit (Checked checkedStatement)
 
1914
                        {
 
1915
                                var result = new CheckedStatement ();
 
1916
                                result.AddChild (new CSharpTokenNode (Convert (checkedStatement.loc), CheckedStatement.CheckedKeywordRole), CheckedStatement.CheckedKeywordRole);
 
1917
                                if (checkedStatement.Block != null)
 
1918
                                        result.AddChild ((BlockStatement)checkedStatement.Block.Accept (this), Roles.Body);
 
1919
                                return result;
 
1920
                        }
 
1921
                        
 
1922
                        public override object Visit (Unsafe unsafeStatement)
 
1923
                        {
 
1924
                                var result = new UnsafeStatement ();
 
1925
                                result.AddChild (new CSharpTokenNode (Convert (unsafeStatement.loc), UnsafeStatement.UnsafeKeywordRole), UnsafeStatement.UnsafeKeywordRole);
 
1926
                                if (unsafeStatement.Block != null)
 
1927
                                        result.AddChild ((BlockStatement)unsafeStatement.Block.Accept (this), Roles.Body);
 
1928
                                return result;
 
1929
                        }
 
1930
                        
 
1931
                        public override object Visit (Fixed fixedStatement)
 
1932
                        {
 
1933
                                var result = new FixedStatement ();
 
1934
                                var location = LocationsBag.GetLocations (fixedStatement);
 
1935
                                
 
1936
                                result.AddChild (new CSharpTokenNode (Convert (fixedStatement.loc), FixedStatement.FixedKeywordRole), FixedStatement.FixedKeywordRole);
 
1937
                                if (location != null)
 
1938
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
1939
                                
 
1940
                                if (fixedStatement.Variables != null) {
 
1941
                                        var blockVariableDeclaration = fixedStatement.Variables;
 
1942
                                        result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
 
1943
                                        var varInit = new VariableInitializer ();
 
1944
                                        var initLocation = LocationsBag.GetLocations (blockVariableDeclaration);
 
1945
                                        varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
 
1946
                                        if (blockVariableDeclaration.Initializer != null) {
 
1947
                                                if (initLocation != null)
 
1948
                                                        varInit.AddChild (new CSharpTokenNode (Convert (initLocation [0]), Roles.Assign), Roles.Assign);
 
1949
                                                varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
 
1950
                                        }
 
1951
                                        
 
1952
                                        result.AddChild (varInit, Roles.Variable);
 
1953
                                        
 
1954
                                        if (blockVariableDeclaration.Declarators != null) {
 
1955
                                                foreach (var decl in blockVariableDeclaration.Declarators) {
 
1956
                                                        var loc = LocationsBag.GetLocations (decl);
 
1957
                                                        var init = new VariableInitializer ();
 
1958
                                                        if (loc != null && loc.Count > 0)
 
1959
                                                                result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
 
1960
                                                        init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
 
1961
                                                        if (decl.Initializer != null) {
 
1962
                                                                if (loc != null && loc.Count > 1)
 
1963
                                                                        init.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Assign), Roles.Assign);
 
1964
                                                                init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
 
1965
                                                        } else {
 
1966
                                                        }
 
1967
                                                        result.AddChild (init, Roles.Variable);
 
1968
                                                }
 
1969
                                        }
 
1970
                                }
 
1971
                                
 
1972
                                if (location != null && location.Count > 1)
 
1973
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
1974
                                if (fixedStatement.Statement != null)
 
1975
                                        result.AddChild ((Statement)fixedStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
1976
                                return result;
 
1977
                        }
 
1978
                        
 
1979
                        public override object Visit (TryFinally tryFinallyStatement)
 
1980
                        {
 
1981
                                TryCatchStatement result;
 
1982
                                var location = LocationsBag.GetLocations (tryFinallyStatement);
 
1983
                                
 
1984
                                if (tryFinallyStatement.Stmt is TryCatch) {
 
1985
                                        result = (TryCatchStatement)tryFinallyStatement.Stmt.Accept (this);
 
1986
                                } else {
 
1987
                                        result = new TryCatchStatement ();
 
1988
                                        result.AddChild (new CSharpTokenNode (Convert (tryFinallyStatement.loc), TryCatchStatement.TryKeywordRole), TryCatchStatement.TryKeywordRole);
 
1989
                                        if (tryFinallyStatement.Stmt != null)
 
1990
                                                result.AddChild ((BlockStatement)tryFinallyStatement.Stmt.Accept (this), TryCatchStatement.TryBlockRole);
 
1991
                                }
 
1992
                                if (location != null)
 
1993
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), TryCatchStatement.FinallyKeywordRole), TryCatchStatement.FinallyKeywordRole);
 
1994
                                if (tryFinallyStatement.Fini != null)
 
1995
                                        result.AddChild ((BlockStatement)tryFinallyStatement.Fini.Accept (this), TryCatchStatement.FinallyBlockRole);
 
1996
                                
 
1997
                                return result;
 
1998
                        }
 
1999
                        
 
2000
                        CatchClause ConvertCatch (Catch ctch)
 
2001
                        {
 
2002
                                CatchClause result = new CatchClause ();
 
2003
                                var location = LocationsBag.GetLocations (ctch);
 
2004
                                result.AddChild (new CSharpTokenNode (Convert (ctch.loc), CatchClause.CatchKeywordRole), CatchClause.CatchKeywordRole);
 
2005
                                if (ctch.TypeExpression != null) {
 
2006
                                        if (location != null)
 
2007
                                                result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2008
                                        
 
2009
                                        if (ctch.TypeExpression != null)
 
2010
                                                result.AddChild (ConvertToType (ctch.TypeExpression), Roles.Type);
 
2011
                                        if (ctch.Variable != null && !string.IsNullOrEmpty (ctch.Variable.Name))
 
2012
                                                result.AddChild (Identifier.Create (ctch.Variable.Name, Convert (ctch.Variable.Location)), Roles.Identifier);
 
2013
                                        
 
2014
                                        if (location != null && location.Count > 1)
 
2015
                                                result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2016
                                }
 
2017
 
 
2018
                                if (ctch.Block != null)
 
2019
                                        result.AddChild ((BlockStatement)ctch.Block.Accept (this), Roles.Body);
 
2020
                                
 
2021
                                return result;
 
2022
                        }
 
2023
                        
 
2024
                        public override object Visit (TryCatch tryCatchStatement)
 
2025
                        {
 
2026
                                var result = new TryCatchStatement ();
 
2027
                                result.AddChild (new CSharpTokenNode (Convert (tryCatchStatement.loc), TryCatchStatement.TryKeywordRole), TryCatchStatement.TryKeywordRole);
 
2028
                                if (tryCatchStatement.Block != null)
 
2029
                                        result.AddChild ((BlockStatement)tryCatchStatement.Block.Accept (this), TryCatchStatement.TryBlockRole);
 
2030
                                if (tryCatchStatement.Clauses != null) {
 
2031
                                        foreach (var ctch in tryCatchStatement.Clauses) {
 
2032
                                                result.AddChild (ConvertCatch (ctch), TryCatchStatement.CatchClauseRole);
 
2033
                                        }
 
2034
                                }
 
2035
//                              if (tryCatchStatement.General != null)
 
2036
//                                      result.AddChild (ConvertCatch (tryCatchStatement.General), TryCatchStatement.CatchClauseRole);
 
2037
                                
 
2038
                                return result;
 
2039
                        }
 
2040
                        
 
2041
                        public override object Visit (Using usingStatement)
 
2042
                        {
 
2043
                                var result = new UsingStatement ();
 
2044
                                var location = LocationsBag.GetLocations (usingStatement);
 
2045
                                
 
2046
                                result.AddChild (new CSharpTokenNode (Convert (usingStatement.loc), UsingStatement.UsingKeywordRole), UsingStatement.UsingKeywordRole);
 
2047
                                if (location != null)
 
2048
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2049
                                if (usingStatement.Expr != null)
 
2050
                                        result.AddChild ((AstNode)usingStatement.Expr.Accept (this), UsingStatement.ResourceAcquisitionRole);
 
2051
                                
 
2052
                                if (location != null && location.Count > 1)
 
2053
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2054
                                
 
2055
                                if (usingStatement.Statement != null)
 
2056
                                        result.AddChild ((Statement)usingStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
2057
                                return result;
 
2058
                        }
 
2059
                        
 
2060
                        public override object Visit (Foreach foreachStatement)
 
2061
                        {
 
2062
                                var result = new ForeachStatement ();
 
2063
                                
 
2064
                                var location = LocationsBag.GetLocations (foreachStatement);
 
2065
                                
 
2066
                                result.AddChild (new CSharpTokenNode (Convert (foreachStatement.loc), ForeachStatement.ForeachKeywordRole), ForeachStatement.ForeachKeywordRole);
 
2067
                                if (location != null)
 
2068
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2069
                                
 
2070
                                if (foreachStatement.TypeExpression != null)
 
2071
                                        result.AddChild (ConvertToType (foreachStatement.TypeExpression), Roles.Type);
 
2072
                                
 
2073
                                if (foreachStatement.Variable != null)
 
2074
                                        result.AddChild (Identifier.Create (foreachStatement.Variable.Name, Convert (foreachStatement.Variable.Location)), Roles.Identifier);
 
2075
                                
 
2076
                                if (location != null && location.Count > 1)
 
2077
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), ForeachStatement.InKeywordRole), ForeachStatement.InKeywordRole);
 
2078
                                
 
2079
                                if (foreachStatement.Expr != null)
 
2080
                                        result.AddChild ((Expression)foreachStatement.Expr.Accept (this), Roles.Expression);
 
2081
                                
 
2082
                                if (location != null && location.Count > 2)
 
2083
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
 
2084
                                
 
2085
                                if (foreachStatement.Statement != null)
 
2086
                                        result.AddChild ((Statement)foreachStatement.Statement.Accept (this), Roles.EmbeddedStatement);
 
2087
                                
 
2088
                                return result;
 
2089
                        }
 
2090
                        
 
2091
                        public override object Visit (Yield yieldStatement)
 
2092
                        {
 
2093
                                var result = new YieldReturnStatement ();
 
2094
                                var location = LocationsBag.GetLocations (yieldStatement);
 
2095
                                
 
2096
                                result.AddChild (new CSharpTokenNode (Convert (yieldStatement.loc), YieldReturnStatement.YieldKeywordRole), YieldReturnStatement.YieldKeywordRole);
 
2097
                                if (location != null)
 
2098
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), YieldReturnStatement.ReturnKeywordRole), YieldReturnStatement.ReturnKeywordRole);
 
2099
                                if (yieldStatement.Expr != null)
 
2100
                                        result.AddChild ((Expression)yieldStatement.Expr.Accept (this), Roles.Expression);
 
2101
                                if (location != null && location.Count > 1)
 
2102
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
2103
                                
 
2104
                                return result;
 
2105
                        }
 
2106
                        
 
2107
                        public override object Visit (YieldBreak yieldBreakStatement)
 
2108
                        {
 
2109
                                var result = new YieldBreakStatement ();
 
2110
                                var location = LocationsBag.GetLocations (yieldBreakStatement);
 
2111
                                result.AddChild (new CSharpTokenNode (Convert (yieldBreakStatement.loc), YieldBreakStatement.YieldKeywordRole), YieldBreakStatement.YieldKeywordRole);
 
2112
                                if (location != null) {
 
2113
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), YieldBreakStatement.BreakKeywordRole), YieldBreakStatement.BreakKeywordRole);
 
2114
                                        if (location.Count > 1)
 
2115
                                                result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
 
2116
                                }
 
2117
                                return result;
 
2118
                        }
 
2119
                        #endregion
 
2120
                        
 
2121
                        #region Expression
 
2122
                        public override object Visit (Mono.CSharp.Expression expression)
 
2123
                        {
 
2124
                                Console.WriteLine ("Visit unknown expression:" + expression);
 
2125
                                System.Console.WriteLine (Environment.StackTrace);
 
2126
                                return null;
 
2127
                        }
 
2128
                        
 
2129
                        public override object Visit (Mono.CSharp.DefaultParameterValueExpression defaultParameterValueExpression)
 
2130
                        {
 
2131
                                return defaultParameterValueExpression.Child.Accept (this);
 
2132
                        }
 
2133
 
 
2134
                        public override object Visit (TypeExpression typeExpression)
 
2135
                        {
 
2136
                                return new TypeReferenceExpression (new PrimitiveType (keywordTable [(int)typeExpression.Type.BuiltinType], Convert (typeExpression.Location)));
 
2137
                        }
 
2138
 
 
2139
                        public override object Visit (LocalVariableReference localVariableReference)
 
2140
                        {
 
2141
                                return Identifier.Create (localVariableReference.Name, Convert (localVariableReference.Location));
 
2142
                        }
 
2143
 
 
2144
                        public override object Visit (MemberAccess memberAccess)
 
2145
                        {
 
2146
                                Expression result;
 
2147
                                if (memberAccess.LeftExpression is Indirection) {
 
2148
                                        var ind = memberAccess.LeftExpression as Indirection;
 
2149
                                        result = new PointerReferenceExpression ();
 
2150
                                        result.AddChild ((Expression)ind.Expr.Accept (this), Roles.TargetExpression);
 
2151
                                        result.AddChild (new CSharpTokenNode (Convert (ind.Location), PointerReferenceExpression.ArrowRole), PointerReferenceExpression.ArrowRole);
 
2152
                                } else {
 
2153
                                        result = new MemberReferenceExpression ();
 
2154
                                        if (memberAccess.LeftExpression != null) {
 
2155
                                                var leftExpr = memberAccess.LeftExpression.Accept (this);
 
2156
                                                result.AddChild ((Expression)leftExpr, Roles.TargetExpression);
 
2157
                                        }
 
2158
                                        if (!memberAccess.DotLocation.IsNull) {
 
2159
                                                result.AddChild (new CSharpTokenNode (Convert (memberAccess.DotLocation), Roles.Dot), Roles.Dot);
 
2160
                                        }
 
2161
                                }
 
2162
                                
 
2163
                                result.AddChild (Identifier.Create (memberAccess.Name, Convert (memberAccess.Location)), Roles.Identifier);
 
2164
                                
 
2165
                                AddTypeArguments (result, memberAccess);
 
2166
                                return result;
 
2167
                        }
 
2168
                        
 
2169
                        public override object Visit (QualifiedAliasMember qualifiedAliasMember)
 
2170
                        {
 
2171
                                var result = new MemberType ();
 
2172
                                result.Target = new SimpleType (qualifiedAliasMember.alias, Convert (qualifiedAliasMember.Location));
 
2173
                                result.IsDoubleColon = true;
 
2174
                                var location = LocationsBag.GetLocations (qualifiedAliasMember);
 
2175
                                if (location != null && location.Count > 0)
 
2176
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.DoubleColon), Roles.DoubleColon);
 
2177
 
 
2178
                                result.AddChild (Identifier.Create (qualifiedAliasMember.Name, location != null && location.Count > 1 ? Convert (location [1]) : TextLocation.Empty), Roles.Identifier);
 
2179
                                return  new TypeReferenceExpression () { Type = result };
 
2180
                        }
 
2181
                        
 
2182
                        public override object Visit (Constant constant)
 
2183
                        {
 
2184
                                if (constant.GetValue () == null)
 
2185
                                        return new NullReferenceExpression (Convert (constant.Location));
 
2186
                                string literalValue;
 
2187
                                if (constant is ILiteralConstant) {
 
2188
                                        literalValue = new string (((ILiteralConstant)constant).ParsedValue);
 
2189
                                } else {
 
2190
                                        literalValue = constant.GetValueAsLiteral ();
 
2191
                                }
 
2192
                                object val = constant.GetValue ();
 
2193
                                if (val is bool)
 
2194
                                        literalValue = (bool)val ? "true" : "false";
 
2195
                                var result = new PrimitiveExpression (val, Convert (constant.Location), literalValue);
 
2196
                                return result;
 
2197
                        }
 
2198
 
 
2199
                        public override object Visit (SimpleName simpleName)
 
2200
                        {
 
2201
                                var result = new IdentifierExpression ();
 
2202
                                result.AddChild (Identifier.Create (simpleName.Name, Convert (simpleName.Location)), Roles.Identifier);
 
2203
                                AddTypeArguments (result, simpleName);
 
2204
                                return result;
 
2205
                        }
 
2206
                        
 
2207
                        public override object Visit (BooleanExpression booleanExpression)
 
2208
                        {
 
2209
                                return booleanExpression.Expr.Accept (this);
 
2210
                        }
 
2211
                        
 
2212
                        public override object Visit (Mono.CSharp.ParenthesizedExpression parenthesizedExpression)
 
2213
                        {
 
2214
                                var result = new ParenthesizedExpression ();
 
2215
                                var location = LocationsBag.GetLocations (parenthesizedExpression);
 
2216
                                if (location != null)
 
2217
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2218
                                if (parenthesizedExpression.Expr != null)
 
2219
                                        result.AddChild ((Expression)parenthesizedExpression.Expr.Accept (this), Roles.Expression);
 
2220
                                if (location != null && location.Count > 1)
 
2221
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2222
                                return result;
 
2223
                        }
 
2224
                        
 
2225
                        public override object Visit (Unary unaryExpression)
 
2226
                        {
 
2227
                                var result = new UnaryOperatorExpression ();
 
2228
                                switch (unaryExpression.Oper) {
 
2229
                                        case Unary.Operator.UnaryPlus:
 
2230
                                                result.Operator = UnaryOperatorType.Plus;
 
2231
                                                break;
 
2232
                                        case Unary.Operator.UnaryNegation:
 
2233
                                                result.Operator = UnaryOperatorType.Minus;
 
2234
                                                break;
 
2235
                                        case Unary.Operator.LogicalNot:
 
2236
                                                result.Operator = UnaryOperatorType.Not;
 
2237
                                                break;
 
2238
                                        case Unary.Operator.OnesComplement:
 
2239
                                                result.Operator = UnaryOperatorType.BitNot;
 
2240
                                                break;
 
2241
                                        case Unary.Operator.AddressOf:
 
2242
                                                result.Operator = UnaryOperatorType.AddressOf;
 
2243
                                                break;
 
2244
                                }
 
2245
                                var r = UnaryOperatorExpression.GetOperatorRole (result.Operator);
 
2246
                                result.AddChild (new CSharpTokenNode (Convert (unaryExpression.Location), r), r);
 
2247
                                if (unaryExpression.Expr != null)
 
2248
                                        result.AddChild ((Expression)unaryExpression.Expr.Accept (this), Roles.Expression);
 
2249
                                return result;
 
2250
                        }
 
2251
                        
 
2252
                        public override object Visit (UnaryMutator unaryMutatorExpression)
 
2253
                        {
 
2254
                                var result = new UnaryOperatorExpression ();
 
2255
                                if (unaryMutatorExpression.Expr == null)
 
2256
                                        return result;
 
2257
                                var expression = (Expression)unaryMutatorExpression.Expr.Accept (this);
 
2258
                                switch (unaryMutatorExpression.UnaryMutatorMode) {
 
2259
                                        case UnaryMutator.Mode.PostDecrement:
 
2260
                                                result.Operator = UnaryOperatorType.PostDecrement;
 
2261
                                                result.AddChild (expression, Roles.Expression);
 
2262
                                                result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.DecrementRole), UnaryOperatorExpression.DecrementRole);
 
2263
                                                break;
 
2264
                                        case UnaryMutator.Mode.PostIncrement:
 
2265
                                                result.Operator = UnaryOperatorType.PostIncrement;
 
2266
                                                result.AddChild (expression, Roles.Expression);
 
2267
                                                result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.IncrementRole), UnaryOperatorExpression.IncrementRole);
 
2268
                                                break;
 
2269
                                                
 
2270
                                        case UnaryMutator.Mode.PreIncrement:
 
2271
                                                result.Operator = UnaryOperatorType.Increment;
 
2272
                                                result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.IncrementRole), UnaryOperatorExpression.IncrementRole);
 
2273
                                                result.AddChild (expression, Roles.Expression);
 
2274
                                                break;
 
2275
                                        case UnaryMutator.Mode.PreDecrement:
 
2276
                                                result.Operator = UnaryOperatorType.Decrement;
 
2277
                                                result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.DecrementRole), UnaryOperatorExpression.DecrementRole);
 
2278
                                                result.AddChild (expression, Roles.Expression);
 
2279
                                                break;
 
2280
                                }
 
2281
                                
 
2282
                                return result;
 
2283
                        }
 
2284
                        
 
2285
                        public override object Visit (Indirection indirectionExpression)
 
2286
                        {
 
2287
                                var result = new UnaryOperatorExpression ();
 
2288
                                result.Operator = UnaryOperatorType.Dereference;
 
2289
                                result.AddChild (new CSharpTokenNode (Convert (indirectionExpression.Location), UnaryOperatorExpression.DereferenceRole), UnaryOperatorExpression.DereferenceRole);
 
2290
                                if (indirectionExpression.Expr != null)
 
2291
                                        result.AddChild ((Expression)indirectionExpression.Expr.Accept (this), Roles.Expression);
 
2292
                                return result;
 
2293
                        }
 
2294
                        
 
2295
                        public override object Visit (Is isExpression)
 
2296
                        {
 
2297
                                var result = new IsExpression ();
 
2298
                                if (isExpression.Expr != null)
 
2299
                                        result.AddChild ((Expression)isExpression.Expr.Accept (this), Roles.Expression);
 
2300
                                result.AddChild (new CSharpTokenNode (Convert (isExpression.Location), IsExpression.IsKeywordRole), IsExpression.IsKeywordRole);
 
2301
                                
 
2302
                                if (isExpression.ProbeType != null)
 
2303
                                        result.AddChild (ConvertToType (isExpression.ProbeType), Roles.Type);
 
2304
                                return result;
 
2305
                        }
 
2306
                        
 
2307
                        public override object Visit (As asExpression)
 
2308
                        {
 
2309
                                var result = new AsExpression ();
 
2310
                                if (asExpression.Expr != null)
 
2311
                                        result.AddChild ((Expression)asExpression.Expr.Accept (this), Roles.Expression);
 
2312
                                result.AddChild (new CSharpTokenNode (Convert (asExpression.Location), AsExpression.AsKeywordRole), AsExpression.AsKeywordRole);
 
2313
                                if (asExpression.ProbeType != null)
 
2314
                                        result.AddChild (ConvertToType (asExpression.ProbeType), Roles.Type);
 
2315
                                return result;
 
2316
                        }
 
2317
                        
 
2318
                        public override object Visit (Cast castExpression)
 
2319
                        {
 
2320
                                var result = new CastExpression ();
 
2321
                                var location = LocationsBag.GetLocations (castExpression);
 
2322
                                
 
2323
                                result.AddChild (new CSharpTokenNode (Convert (castExpression.Location), Roles.LPar), Roles.LPar);
 
2324
                                if (castExpression.TargetType != null)
 
2325
                                        result.AddChild (ConvertToType (castExpression.TargetType), Roles.Type);
 
2326
                                if (location != null)
 
2327
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RPar), Roles.RPar);
 
2328
                                if (castExpression.Expr != null)
 
2329
                                        result.AddChild ((Expression)castExpression.Expr.Accept (this), Roles.Expression);
 
2330
                                return result;
 
2331
                        }
 
2332
                        
 
2333
                        public override object Visit (ComposedCast composedCast)
 
2334
                        {
 
2335
                                var result = new ComposedType ();
 
2336
                                result.AddChild (ConvertToType (composedCast.Left), Roles.Type);
 
2337
                                
 
2338
                                var spec = composedCast.Spec;
 
2339
                                while (spec != null) {
 
2340
                                        if (spec.IsNullable) {
 
2341
                                                result.AddChild (new CSharpTokenNode (Convert (spec.Location), ComposedType.NullableRole), ComposedType.NullableRole);
 
2342
                                        } else if (spec.IsPointer) {
 
2343
                                                result.AddChild (new CSharpTokenNode (Convert (spec.Location), ComposedType.PointerRole), ComposedType.PointerRole);
 
2344
                                        } else {
 
2345
                                                var aSpec = new ArraySpecifier ();
 
2346
                                                aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location), Roles.LBracket), Roles.LBracket);
 
2347
                                                var location = LocationsBag.GetLocations (spec);
 
2348
                                                if (location != null)
 
2349
                                                        aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location), Roles.RBracket), Roles.RBracket);
 
2350
                                                result.AddChild (aSpec, ComposedType.ArraySpecifierRole);
 
2351
                                        }
 
2352
                                        spec = spec.Next;
 
2353
                                }
 
2354
                                
 
2355
                                return result;
 
2356
                        }
 
2357
                        
 
2358
                        public override object Visit (Mono.CSharp.DefaultValueExpression defaultValueExpression)
 
2359
                        {
 
2360
                                var result = new DefaultValueExpression ();
 
2361
                                result.AddChild (new CSharpTokenNode (Convert (defaultValueExpression.Location), DefaultValueExpression.DefaultKeywordRole), DefaultValueExpression.DefaultKeywordRole);
 
2362
                                var location = LocationsBag.GetLocations (defaultValueExpression);
 
2363
                                if (location != null)
 
2364
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2365
                                result.AddChild (ConvertToType (defaultValueExpression.Expr), Roles.Type);
 
2366
                                if (location != null && location.Count > 1)
 
2367
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2368
                                return result;
 
2369
                        }
 
2370
                        
 
2371
                        public override object Visit(Binary binaryExpression)
 
2372
                        {
 
2373
                                var result = new BinaryOperatorExpression();
 
2374
                                switch (binaryExpression.Oper) {
 
2375
                                        case Binary.Operator.Multiply:
 
2376
                                                result.Operator = BinaryOperatorType.Multiply;
 
2377
                                                break;
 
2378
                                        case Binary.Operator.Division:
 
2379
                                                result.Operator = BinaryOperatorType.Divide;
 
2380
                                                break;
 
2381
                                        case Binary.Operator.Modulus:
 
2382
                                                result.Operator = BinaryOperatorType.Modulus;
 
2383
                                                break;
 
2384
                                        case Binary.Operator.Addition:
 
2385
                                                result.Operator = BinaryOperatorType.Add;
 
2386
                                                break;
 
2387
                                        case Binary.Operator.Subtraction:
 
2388
                                                result.Operator = BinaryOperatorType.Subtract;
 
2389
                                                break;
 
2390
                                        case Binary.Operator.LeftShift:
 
2391
                                                result.Operator = BinaryOperatorType.ShiftLeft;
 
2392
                                                break;
 
2393
                                        case Binary.Operator.RightShift:
 
2394
                                                result.Operator = BinaryOperatorType.ShiftRight;
 
2395
                                                break;
 
2396
                                        case Binary.Operator.LessThan:
 
2397
                                                result.Operator = BinaryOperatorType.LessThan;
 
2398
                                                break;
 
2399
                                        case Binary.Operator.GreaterThan:
 
2400
                                                result.Operator = BinaryOperatorType.GreaterThan;
 
2401
                                                break;
 
2402
                                        case Binary.Operator.LessThanOrEqual:
 
2403
                                                result.Operator = BinaryOperatorType.LessThanOrEqual;
 
2404
                                                break;
 
2405
                                        case Binary.Operator.GreaterThanOrEqual:
 
2406
                                                result.Operator = BinaryOperatorType.GreaterThanOrEqual;
 
2407
                                                break;
 
2408
                                        case Binary.Operator.Equality:
 
2409
                                                result.Operator = BinaryOperatorType.Equality;
 
2410
                                                break;
 
2411
                                        case Binary.Operator.Inequality:
 
2412
                                                result.Operator = BinaryOperatorType.InEquality;
 
2413
                                                break;
 
2414
                                        case Binary.Operator.BitwiseAnd:
 
2415
                                                result.Operator = BinaryOperatorType.BitwiseAnd;
 
2416
                                                break;
 
2417
                                        case Binary.Operator.ExclusiveOr:
 
2418
                                                result.Operator = BinaryOperatorType.ExclusiveOr;
 
2419
                                                break;
 
2420
                                        case Binary.Operator.BitwiseOr:
 
2421
                                                result.Operator = BinaryOperatorType.BitwiseOr;
 
2422
                                                break;
 
2423
                                        case Binary.Operator.LogicalAnd:
 
2424
                                                result.Operator = BinaryOperatorType.ConditionalAnd;
 
2425
                                                break;
 
2426
                                        case Binary.Operator.LogicalOr:
 
2427
                                                result.Operator = BinaryOperatorType.ConditionalOr;
 
2428
                                                break;
 
2429
                                }
 
2430
                                
 
2431
                                if (binaryExpression.Left != null)
 
2432
                                        result.AddChild((Expression)binaryExpression.Left.Accept(this), BinaryOperatorExpression.LeftRole);
 
2433
                                var location = LocationsBag.GetLocations(binaryExpression);
 
2434
                                if (location != null) {
 
2435
                                        var r = BinaryOperatorExpression.GetOperatorRole (result.Operator);
 
2436
                                        result.AddChild(new CSharpTokenNode(Convert(location [0]), r), r);
 
2437
                                }
 
2438
                                if (binaryExpression.Right != null)
 
2439
                                        result.AddChild ((Expression)binaryExpression.Right.Accept (this), BinaryOperatorExpression.RightRole);
 
2440
                                return result;
 
2441
                        }
 
2442
                        
 
2443
                        public override object Visit (Mono.CSharp.Nullable.NullCoalescingOperator nullCoalescingOperator)
 
2444
                        {
 
2445
                                var result = new BinaryOperatorExpression ();
 
2446
                                result.Operator = BinaryOperatorType.NullCoalescing;
 
2447
                                if (nullCoalescingOperator.LeftExpression != null)
 
2448
                                        result.AddChild ((Expression)nullCoalescingOperator.LeftExpression.Accept (this), BinaryOperatorExpression.LeftRole);
 
2449
                                var location = LocationsBag.GetLocations (nullCoalescingOperator);
 
2450
                                if (location != null)
 
2451
                                        result.AddChild (new CSharpTokenNode (Convert (location[0]), BinaryOperatorExpression.NullCoalescingRole), BinaryOperatorExpression.NullCoalescingRole);
 
2452
                                if (nullCoalescingOperator.RightExpression != null)
 
2453
                                        result.AddChild ((Expression)nullCoalescingOperator.RightExpression.Accept (this), BinaryOperatorExpression.RightRole);
 
2454
                                return result;
 
2455
                        }
 
2456
                        
 
2457
                        public override object Visit (Conditional conditionalExpression)
 
2458
                        {
 
2459
                                var result = new ConditionalExpression ();
 
2460
                                
 
2461
                                if (conditionalExpression.Expr != null)
 
2462
                                        result.AddChild ((Expression)conditionalExpression.Expr.Accept (this), Roles.Condition);
 
2463
                                var location = LocationsBag.GetLocations (conditionalExpression);
 
2464
                                
 
2465
                                result.AddChild (new CSharpTokenNode (Convert (conditionalExpression.Location), ConditionalExpression.QuestionMarkRole), ConditionalExpression.QuestionMarkRole);
 
2466
                                if (conditionalExpression.TrueExpr != null)
 
2467
                                        result.AddChild ((Expression)conditionalExpression.TrueExpr.Accept (this), ConditionalExpression.TrueRole);
 
2468
                                if (location != null)
 
2469
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), ConditionalExpression.ColonRole), ConditionalExpression.ColonRole);
 
2470
                                if (conditionalExpression.FalseExpr != null)
 
2471
                                        result.AddChild ((Expression)conditionalExpression.FalseExpr.Accept (this), ConditionalExpression.FalseRole);
 
2472
                                return result;
 
2473
                        }
 
2474
                        
 
2475
                        void AddParameter (AstNode parent, Mono.CSharp.AParametersCollection parameters)
 
2476
                        {
 
2477
                                if (parameters == null)
 
2478
                                        return;
 
2479
                                var paramLocation = LocationsBag.GetLocations (parameters);
 
2480
                                
 
2481
                                for (int i = 0; i < parameters.Count; i++) {
 
2482
                                        var p = (Parameter)parameters.FixedParameters [i];
 
2483
                                        if (p == null)
 
2484
                                                continue;
 
2485
                                        var location = LocationsBag.GetLocations (p);
 
2486
                                        ParameterDeclaration parameterDeclarationExpression = new ParameterDeclaration ();
 
2487
                                        AddAttributeSection (parameterDeclarationExpression, p);
 
2488
                                        switch (p.ModFlags) {
 
2489
                                                case Parameter.Modifier.OUT:
 
2490
                                                        parameterDeclarationExpression.ParameterModifier = ParameterModifier.Out;
 
2491
                                                        if (location != null)
 
2492
                                                                parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.OutModifierRole), ParameterDeclaration.OutModifierRole);
 
2493
                                                        break;
 
2494
                                                case Parameter.Modifier.REF:
 
2495
                                                        parameterDeclarationExpression.ParameterModifier = ParameterModifier.Ref;
 
2496
                                                        if (location != null)
 
2497
                                                                parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.RefModifierRole), ParameterDeclaration.RefModifierRole);
 
2498
                                                        break;
 
2499
                                                case Parameter.Modifier.PARAMS:
 
2500
                                                        parameterDeclarationExpression.ParameterModifier = ParameterModifier.Params;
 
2501
                                                        if (location != null)
 
2502
                                                                parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.ParamsModifierRole), ParameterDeclaration.ParamsModifierRole);
 
2503
                                                        break;
 
2504
                                                default:
 
2505
                                                        if (p.HasExtensionMethodModifier) {
 
2506
                                                                parameterDeclarationExpression.ParameterModifier = ParameterModifier.This;
 
2507
                                                                if (location != null) {
 
2508
                                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.ThisModifierRole), ParameterDeclaration.ThisModifierRole);
 
2509
                                                                }
 
2510
                                                        }
 
2511
                                                        break;
 
2512
                                        }
 
2513
                                        if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
 
2514
                                                parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
 
2515
                                        if (p.Name != null)
 
2516
                                                parameterDeclarationExpression.AddChild (Identifier.Create (p.Name, Convert (p.Location)), Roles.Identifier);
 
2517
                                        if (p.HasDefaultValue) {
 
2518
                                                if (location != null && location.Count > 1)
 
2519
                                                        parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Assign), Roles.Assign);
 
2520
                                                parameterDeclarationExpression.AddChild ((Expression)p.DefaultValue.Accept (this), Roles.Expression);
 
2521
                                        }
 
2522
                                        parent.AddChild (parameterDeclarationExpression, Roles.Parameter);
 
2523
                                        if (paramLocation != null && i < paramLocation.Count) {
 
2524
                                                parent.AddChild (new CSharpTokenNode (Convert (paramLocation [i]), Roles.Comma), Roles.Comma);
 
2525
                                        }
 
2526
                                }
 
2527
                        }
 
2528
                        
 
2529
                        void AddTypeParameters (AstNode parent, MemberName memberName)
 
2530
                        {
 
2531
                                if (memberName == null || memberName.TypeParameters == null)
 
2532
                                        return;
 
2533
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
 
2534
                                if (chevronLocs != null)
 
2535
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
 
2536
                                for (int i = 0; i < memberName.TypeParameters.Count; i++) {
 
2537
                                        if (chevronLocs != null && i > 0 && i - 1 < chevronLocs.Count)
 
2538
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i - 1]), Roles.Comma), Roles.Comma);
 
2539
                                        var arg = memberName.TypeParameters [i];
 
2540
                                        if (arg == null)
 
2541
                                                continue;
 
2542
                                        TypeParameterDeclaration tp = new TypeParameterDeclaration ();
 
2543
                                        
 
2544
                                        List<Location> varianceLocation;
 
2545
                                        switch (arg.Variance) {
 
2546
                                                case Variance.Contravariant:
 
2547
                                                        tp.Variance = VarianceModifier.Contravariant;
 
2548
                                                        varianceLocation = LocationsBag.GetLocations (arg);
 
2549
                                                        if (varianceLocation != null)
 
2550
                                                                tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0]), TypeParameterDeclaration.InVarianceKeywordRole), TypeParameterDeclaration.InVarianceKeywordRole);
 
2551
                                                        break;
 
2552
                                                case Variance.Covariant:
 
2553
                                                        tp.Variance = VarianceModifier.Covariant;
 
2554
                                                        varianceLocation = LocationsBag.GetLocations (arg);
 
2555
                                                        if (varianceLocation != null)
 
2556
                                                                tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0]), TypeParameterDeclaration.OutVarianceKeywordRole), TypeParameterDeclaration.OutVarianceKeywordRole);
 
2557
                                                        break;
 
2558
                                                default:
 
2559
                                                        tp.Variance = VarianceModifier.Invariant;
 
2560
                                                        break;
 
2561
                                                        
 
2562
                                        }
 
2563
                                        
 
2564
                                        AddAttributeSection (tp, arg.OptAttributes);
 
2565
 
 
2566
                                        switch (arg.Variance) {
 
2567
                                                case Variance.Covariant:
 
2568
                                                        tp.Variance = VarianceModifier.Covariant;
 
2569
                                                        break;
 
2570
                                                case Variance.Contravariant:
 
2571
                                                        tp.Variance = VarianceModifier.Contravariant;
 
2572
                                                        break;
 
2573
                                        }
 
2574
                                        tp.AddChild (Identifier.Create (arg.Name, Convert (arg.Location)), Roles.Identifier);
 
2575
                                        parent.AddChild (tp, Roles.TypeParameter);
 
2576
                                }
 
2577
                                if (chevronLocs != null)
 
2578
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
 
2579
                        }
 
2580
                        
 
2581
                        void AddTypeArguments (AstNode parent, MemberName memberName)
 
2582
                        {
 
2583
                                if (memberName == null || memberName.TypeParameters == null)
 
2584
                                        return;
 
2585
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
 
2586
                                if (chevronLocs != null)
 
2587
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
 
2588
                                
 
2589
                                for (int i = 0; i < memberName.TypeParameters.Count; i++) {
 
2590
                                        var arg = memberName.TypeParameters [i];
 
2591
                                        if (arg == null)
 
2592
                                                continue;
 
2593
                                        parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
 
2594
                                        if (chevronLocs != null && i < chevronLocs.Count - 2)
 
2595
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
 
2596
                                }
 
2597
                                
 
2598
                                if (chevronLocs != null)
 
2599
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
 
2600
                        }
 
2601
                        
 
2602
                        void AddTypeArguments (AstNode parent, ATypeNameExpression memberName)
 
2603
                        {
 
2604
                                if (memberName == null || !memberName.HasTypeArguments)
 
2605
                                        return;
 
2606
                                var chevronLocs = LocationsBag.GetLocations (memberName.TypeArguments);
 
2607
                                if (chevronLocs != null)
 
2608
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
 
2609
                                
 
2610
                                for (int i = 0; i < memberName.TypeArguments.Count; i++) {
 
2611
                                        var arg = memberName.TypeArguments.Args [i];
 
2612
                                        if (arg == null)
 
2613
                                                continue;
 
2614
                                        parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
 
2615
                                        if (chevronLocs != null && i < chevronLocs.Count - 2)
 
2616
                                                parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
 
2617
                                }
 
2618
                                
 
2619
                                if (chevronLocs != null)
 
2620
                                        parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
 
2621
                        }
 
2622
                        
 
2623
                        void AddConstraints(AstNode parent, TypeParameters d)
 
2624
                        {
 
2625
                                if (d == null)
 
2626
                                        return;
 
2627
                                for (int i = 0; i < d.Count; i++) {
 
2628
                                        var typeParameter = d [i];
 
2629
                                        if (typeParameter == null)
 
2630
                                                continue;
 
2631
                                        var c = typeParameter.Constraints;
 
2632
                                        if (c == null)
 
2633
                                                continue;
 
2634
                                        var location = LocationsBag.GetLocations (c);
 
2635
                                        var constraint = new Constraint ();
 
2636
                                        constraint.AddChild (new CSharpTokenNode (Convert (c.Location), Roles.WhereKeyword), Roles.WhereKeyword);
 
2637
                                        constraint.AddChild (new SimpleType (Identifier.Create (c.TypeParameter.Value, Convert (c.TypeParameter.Location))), Roles.ConstraintTypeParameter);
 
2638
                                        if (location != null)
 
2639
                                                constraint.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Colon), Roles.Colon);
 
2640
                                        var commaLocs = LocationsBag.GetLocations (c.ConstraintExpressions);
 
2641
                                        int curComma = 0;
 
2642
                                        if (c.ConstraintExpressions != null) {
 
2643
                                                foreach (var expr in c.ConstraintExpressions) {
 
2644
                                                        constraint.AddChild (ConvertToType (expr), Roles.BaseType);
 
2645
                                                        if (commaLocs != null && curComma < commaLocs.Count)
 
2646
                                                                constraint.AddChild (new CSharpTokenNode (Convert (commaLocs [curComma++]), Roles.Comma), Roles.Comma);
 
2647
                                                }
 
2648
                                        }
 
2649
                                        
 
2650
                                        // We need to sort the constraints by position; as they might be in a different order than the type parameters
 
2651
                                        AstNode prevSibling = parent.LastChild;
 
2652
                                        while (prevSibling.StartLocation > constraint.StartLocation && prevSibling.PrevSibling != null)
 
2653
                                                prevSibling = prevSibling.PrevSibling;
 
2654
                                        parent.InsertChildAfter (prevSibling, constraint, Roles.Constraint);
 
2655
                                }
 
2656
                        }
 
2657
                        
 
2658
                        Expression ConvertArgument (Argument arg)
 
2659
                        {
 
2660
                                if (arg is NamedArgument) {
 
2661
                                        var na = (NamedArgument)arg;
 
2662
                                        NamedArgumentExpression newArg = new NamedArgumentExpression ();
 
2663
                                        newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
 
2664
                                        
 
2665
                                        var loc = LocationsBag.GetLocations (na);
 
2666
                                        if (loc != null)
 
2667
                                                newArg.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Colon), Roles.Colon);
 
2668
                                        
 
2669
                                        if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
 
2670
                                                DirectionExpression direction = new DirectionExpression ();
 
2671
                                                direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
 
2672
                                                var argLocation = LocationsBag.GetLocations (arg);
 
2673
                                                if (argLocation != null) {
 
2674
                                                        var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole;
 
2675
                                                        direction.AddChild (new CSharpTokenNode (Convert (argLocation [0]), r), r);
 
2676
                                                }
 
2677
                                                direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
 
2678
                                                newArg.AddChild (direction, Roles.Expression);
 
2679
                                        } else {
 
2680
                                                newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
 
2681
                                        }
 
2682
                                        return newArg;
 
2683
                                }
 
2684
                                
 
2685
                                if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
 
2686
                                        DirectionExpression direction = new DirectionExpression ();
 
2687
                                        direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
 
2688
                                        var argLocation = LocationsBag.GetLocations (arg);
 
2689
                                        if (argLocation != null) {
 
2690
                                                var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole;
 
2691
                                                direction.AddChild (new CSharpTokenNode (Convert (argLocation [0]), r), r);
 
2692
                                        }
 
2693
                                        direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
 
2694
                                        return direction;
 
2695
                                }
 
2696
                                
 
2697
                                return (Expression)arg.Expr.Accept (this);
 
2698
                        }
 
2699
                        
 
2700
                        void AddArguments (AstNode parent, object location, Mono.CSharp.Arguments args)
 
2701
                        {
 
2702
                                if (args == null)
 
2703
                                        return;
 
2704
                                
 
2705
                                var commaLocations = LocationsBag.GetLocations (args);
 
2706
                                for (int i = 0; i < args.Count; i++) {
 
2707
                                        parent.AddChild (ConvertArgument (args [i]), Roles.Argument);
 
2708
                                        if (commaLocations != null && i < commaLocations.Count) {
 
2709
                                                parent.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
2710
                                        }
 
2711
                                }
 
2712
                                if (commaLocations != null && commaLocations.Count > args.Count)
 
2713
                                        parent.AddChild (new CSharpTokenNode (Convert (commaLocations [args.Count]), Roles.Comma), Roles.Comma);
 
2714
                        }
 
2715
                        
 
2716
                        public override object Visit (Invocation invocationExpression)
 
2717
                        {
 
2718
                                var result = new InvocationExpression ();
 
2719
                                var location = LocationsBag.GetLocations (invocationExpression);
 
2720
                                if (invocationExpression.Exp != null)
 
2721
                                        result.AddChild ((Expression)invocationExpression.Exp.Accept (this), Roles.TargetExpression);
 
2722
                                if (location != null)
 
2723
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2724
                                AddArguments (result, location, invocationExpression.Arguments);
 
2725
                                
 
2726
                                if (location != null && location.Count > 1)
 
2727
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2728
                                return result;
 
2729
                        }
 
2730
                        
 
2731
                        public override object Visit (New newExpression)
 
2732
                        {
 
2733
                                var result = new ObjectCreateExpression ();
 
2734
                                var location = LocationsBag.GetLocations (newExpression);
 
2735
                                result.AddChild (new CSharpTokenNode (Convert (newExpression.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
 
2736
                                
 
2737
                                if (newExpression.TypeRequested != null)
 
2738
                                        result.AddChild (ConvertToType (newExpression.TypeRequested), Roles.Type);
 
2739
                                if (location != null)
 
2740
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2741
                                AddArguments (result, location, newExpression.Arguments);
 
2742
                                
 
2743
                                if (location != null && location.Count > 1)
 
2744
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2745
                                
 
2746
                                return result;
 
2747
                        }
 
2748
                        
 
2749
                        public override object Visit (NewAnonymousType newAnonymousType)
 
2750
                        {
 
2751
                                var result = new AnonymousTypeCreateExpression ();
 
2752
                                var location = LocationsBag.GetLocations (newAnonymousType);
 
2753
                                result.AddChild (new CSharpTokenNode (Convert (newAnonymousType.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
 
2754
                                if (location != null)
 
2755
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBrace), Roles.LBrace);
 
2756
                                if (newAnonymousType.Parameters != null) {
 
2757
                                        foreach (var par in newAnonymousType.Parameters) {
 
2758
                                                if (par == null)
 
2759
                                                        continue;
 
2760
                                                var parLocation = LocationsBag.GetLocations (par);
 
2761
                                                
 
2762
                                                if (parLocation == null) {
 
2763
                                                        if (par.Expr != null)
 
2764
                                                                result.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
 
2765
                                                } else {
 
2766
                                                        var namedExpression = new NamedExpression ();
 
2767
                                                        namedExpression.AddChild (Identifier.Create (par.Name, Convert (par.Location)), Roles.Identifier);
 
2768
                                                        namedExpression.AddChild (new CSharpTokenNode (Convert (parLocation [0]), Roles.Assign), Roles.Assign);
 
2769
                                                        if (par.Expr != null)
 
2770
                                                                namedExpression.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
 
2771
                                                        result.AddChild (namedExpression, Roles.Expression);
 
2772
                                                }
 
2773
                                        }
 
2774
                                }
 
2775
                                if (location != null && location.Count > 1)
 
2776
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBrace), Roles.RBrace);
 
2777
                                return result;
 
2778
                        }
 
2779
 
 
2780
                        ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
 
2781
                        {
 
2782
                                if (minit == null)
 
2783
                                        return null;
 
2784
                                var init = new ArrayInitializerExpression();
 
2785
                                AddConvertCollectionOrObjectInitializers(init, minit);
 
2786
                                return init;
 
2787
                        }
 
2788
 
 
2789
                        void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
 
2790
                        {
 
2791
                                var initLoc = LocationsBag.GetLocations(minit);
 
2792
                                var commaLoc = LocationsBag.GetLocations(minit.Initializers);
 
2793
                                int curComma = 0;
 
2794
                                init.AddChild(new CSharpTokenNode(Convert(minit.Location), Roles.LBrace), Roles.LBrace);
 
2795
                                foreach (var expr in minit.Initializers) {
 
2796
                                        var collectionInit = expr as CollectionElementInitializer;
 
2797
                                        if (collectionInit != null) {
 
2798
                                                AstNode parent;
 
2799
                                                // For ease of use purposes in the resolver the ast representation
 
2800
                                                // of { a, b, c }  is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
 
2801
                                                // can be identified by expr.IsSingleElement.
 
2802
                                                if (!collectionInit.IsSingle) {
 
2803
                                                        parent = new ArrayInitializerExpression();
 
2804
                                                        parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location), Roles.LBrace), Roles.LBrace);
 
2805
                                                } else {
 
2806
                                                        parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
 
2807
                                                }
 
2808
                                                for (int i = 0; i < collectionInit.Arguments.Count; i++) {
 
2809
                                                        var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
 
2810
                                                        if (arg == null)
 
2811
                                                                continue;
 
2812
                                                        parent.AddChild(
 
2813
                                                                (ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept(this),
 
2814
                                                                Roles.Expression
 
2815
                                                        );
 
2816
                                                }
 
2817
 
 
2818
                                                if (!collectionInit.IsSingle) {
 
2819
                                                        var braceLocs = LocationsBag.GetLocations(expr);
 
2820
                                                        if (braceLocs != null)
 
2821
                                                                parent.AddChild(new CSharpTokenNode(Convert(braceLocs [0]), Roles.RBrace), Roles.RBrace);
 
2822
                                                }
 
2823
                                                init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
 
2824
                                        } else {
 
2825
                                                var eleInit = expr as ElementInitializer;
 
2826
                                                if (eleInit != null) {
 
2827
                                                        var nexpr = new NamedExpression();
 
2828
                                                        nexpr.AddChild(
 
2829
                                                                Identifier.Create(eleInit.Name, Convert(eleInit.Location)),
 
2830
                                                                Roles.Identifier
 
2831
                                                        );
 
2832
                                                        var assignLoc = LocationsBag.GetLocations(eleInit);
 
2833
                                                        if (assignLoc != null)
 
2834
                                                                nexpr.AddChild(new CSharpTokenNode(Convert(assignLoc [0]), Roles.Assign), Roles.Assign);
 
2835
                                                        if (eleInit.Source != null) {
 
2836
                                                                if (eleInit.Source is CollectionOrObjectInitializers) {
 
2837
                                                                        var arrInit = new ArrayInitializerExpression();
 
2838
                                                                        AddConvertCollectionOrObjectInitializers(
 
2839
                                                                                arrInit,
 
2840
                                                                                eleInit.Source as CollectionOrObjectInitializers
 
2841
                                                                        );
 
2842
                                                                        nexpr.AddChild(arrInit, Roles.Expression);
 
2843
                                                                } else {
 
2844
                                                                        nexpr.AddChild((Expression)eleInit.Source.Accept(this), Roles.Expression);
 
2845
                                                                }
 
2846
                                                        }
 
2847
 
 
2848
                                                        init.AddChild(nexpr, Roles.Expression);
 
2849
                                                }
 
2850
                                        }
 
2851
                                        if (commaLoc != null && curComma < commaLoc.Count)
 
2852
                                                init.AddChild(new CSharpTokenNode(Convert(commaLoc [curComma++]), Roles.Comma), Roles.Comma);
 
2853
                                }
 
2854
 
 
2855
                                if (initLoc != null) {
 
2856
                                        if (initLoc.Count == 2) // optional comma
 
2857
                                                init.AddChild(new CSharpTokenNode(Convert(initLoc [0]), Roles.Comma), Roles.Comma);
 
2858
                                        init.AddChild(new CSharpTokenNode(Convert(initLoc [initLoc.Count - 1]), Roles.RBrace), Roles.RBrace);
 
2859
                                }
 
2860
                        }
 
2861
 
 
2862
                        
 
2863
                        public override object Visit (NewInitialize newInitializeExpression)
 
2864
                        {
 
2865
                                var result = new ObjectCreateExpression ();
 
2866
                                result.AddChild (new CSharpTokenNode (Convert (newInitializeExpression.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
 
2867
                                
 
2868
                                if (newInitializeExpression.TypeRequested != null)
 
2869
                                        result.AddChild (ConvertToType (newInitializeExpression.TypeRequested), Roles.Type);
 
2870
                                
 
2871
                                var location = LocationsBag.GetLocations (newInitializeExpression);
 
2872
                                if (location != null)
 
2873
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2874
                                AddArguments (result, location, newInitializeExpression.Arguments);
 
2875
                                if (location != null && location.Count > 1)
 
2876
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2877
                                
 
2878
                                var init = ConvertCollectionOrObjectInitializers (newInitializeExpression.Initializers);
 
2879
                                if (init != null)
 
2880
                                        result.AddChild (init, ObjectCreateExpression.InitializerRole);
 
2881
                                
 
2882
                                return result;
 
2883
                        }
 
2884
                        
 
2885
                        public override object Visit (ArrayCreation arrayCreationExpression)
 
2886
                        {
 
2887
                                var result = new ArrayCreateExpression ();
 
2888
                                
 
2889
                                var location = LocationsBag.GetLocations (arrayCreationExpression);
 
2890
                                result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location), ArrayCreateExpression.NewKeywordRole), ArrayCreateExpression.NewKeywordRole);
 
2891
                                if (arrayCreationExpression.TypeExpression != null)
 
2892
                                        result.AddChild (ConvertToType (arrayCreationExpression.TypeExpression), Roles.Type);
 
2893
                                
 
2894
                                var next = arrayCreationExpression.Rank;
 
2895
                                if (arrayCreationExpression.Arguments != null) {
 
2896
                                        // skip first array rank.
 
2897
                                        next = next.Next;
 
2898
                                        
 
2899
                                        if (location != null)
 
2900
                                                result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBracket), Roles.LBracket);
 
2901
                                        
 
2902
                                        var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments);
 
2903
                                        for (int i = 0; i < arrayCreationExpression.Arguments.Count; i++) {
 
2904
                                                result.AddChild ((Expression)arrayCreationExpression.Arguments [i].Accept (this), Roles.Argument);
 
2905
                                                if (commaLocations != null && i < commaLocations.Count)
 
2906
                                                        result.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
2907
                                        }
 
2908
                                        if (location != null && location.Count > 1)
 
2909
                                                result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBracket), Roles.RBracket);
 
2910
                                        
 
2911
                                }
 
2912
                                
 
2913
                                while (next != null) {
 
2914
                                        ArraySpecifier spec = new ArraySpecifier (next.Dimension);
 
2915
                                        var loc = LocationsBag.GetLocations (next);
 
2916
                                        spec.AddChild (new CSharpTokenNode (Convert (next.Location), Roles.LBracket), Roles.LBracket);
 
2917
                                        result.AddChild (spec, ArrayCreateExpression.AdditionalArraySpecifierRole);
 
2918
                                        if (loc != null)
 
2919
                                                result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.RBracket), Roles.RBracket);
 
2920
                                        next = next.Next;
 
2921
                                }
 
2922
                                
 
2923
                                if (arrayCreationExpression.Initializers != null) {
 
2924
                                        var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers);
 
2925
                                        ArrayInitializerExpression initializer = new ArrayInitializerExpression ();
 
2926
                                        
 
2927
                                        initializer.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location), Roles.LBrace), Roles.LBrace);
 
2928
                                        var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements);
 
2929
                                        for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) {
 
2930
                                                var init = arrayCreationExpression.Initializers [i];
 
2931
                                                if (init == null)
 
2932
                                                        continue;
 
2933
                                                initializer.AddChild ((Expression)init.Accept (this), Roles.Expression);
 
2934
                                                if (commaLocations != null && i < commaLocations.Count) {
 
2935
                                                        initializer.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
2936
                                                }
 
2937
                                        }
 
2938
                                        if (initLocation != null) {
 
2939
                                                if (initLocation.Count == 2) // optional comma
 
2940
                                                        initializer.AddChild (new CSharpTokenNode(Convert(initLocation [0]), Roles.Comma), Roles.Comma);
 
2941
                                                initializer.AddChild (new CSharpTokenNode (Convert (initLocation [initLocation.Count - 1]), Roles.RBrace), Roles.RBrace);
 
2942
                                        }
 
2943
                                        result.AddChild (initializer, ArrayCreateExpression.InitializerRole);
 
2944
                                }
 
2945
                                
 
2946
                                return result;
 
2947
                        }
 
2948
                        
 
2949
                        public override object Visit (This thisExpression)
 
2950
                        {
 
2951
                                var result = new ThisReferenceExpression ();
 
2952
                                result.Location = Convert (thisExpression.Location);
 
2953
                                return result;
 
2954
                        }
 
2955
                        
 
2956
                        public override object Visit (ArglistAccess argListAccessExpression)
 
2957
                        {
 
2958
                                var result = new UndocumentedExpression () {
 
2959
                                        UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess
 
2960
                                };
 
2961
                                result.AddChild (new CSharpTokenNode (Convert (argListAccessExpression.Location), UndocumentedExpression.ArglistKeywordRole), UndocumentedExpression.ArglistKeywordRole);
 
2962
                                return result;
 
2963
                        }
 
2964
                        
 
2965
                        #region Undocumented expressions
 
2966
                        public override object Visit (Arglist argListExpression)
 
2967
                        {
 
2968
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.ArgList };
 
2969
                                result.AddChild (new CSharpTokenNode (Convert (argListExpression.Location), UndocumentedExpression.ArglistKeywordRole), UndocumentedExpression.ArglistKeywordRole);
 
2970
                                var location = LocationsBag.GetLocations (argListExpression);
 
2971
                                if (location != null)
 
2972
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2973
                                
 
2974
                                AddArguments (result, location, argListExpression.Arguments);
 
2975
                                
 
2976
                                if (location != null && location.Count > 1)
 
2977
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2978
                                return result;
 
2979
                        }
 
2980
                        
 
2981
                        public override object Visit (MakeRefExpr makeRefExpr)
 
2982
                        {
 
2983
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.MakeRef };
 
2984
                                result.AddChild (new CSharpTokenNode (Convert (makeRefExpr.Location), UndocumentedExpression.MakerefKeywordRole), UndocumentedExpression.MakerefKeywordRole);
 
2985
                                var location = LocationsBag.GetLocations (makeRefExpr);
 
2986
                                if (location != null)
 
2987
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
2988
                                if (makeRefExpr.Expr != null)
 
2989
                                        result.AddChild ((Expression)makeRefExpr.Expr.Accept (this), Roles.Argument);
 
2990
                                if (location != null && location.Count > 1)
 
2991
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
2992
                                return result;
 
2993
                        }
 
2994
                        
 
2995
                        public override object Visit (RefTypeExpr refTypeExpr)
 
2996
                        {
 
2997
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefType };
 
2998
                                result.AddChild (new CSharpTokenNode (Convert (refTypeExpr.Location), UndocumentedExpression.ReftypeKeywordRole), UndocumentedExpression.ReftypeKeywordRole);
 
2999
                                var location = LocationsBag.GetLocations (refTypeExpr);
 
3000
                                if (location != null)
 
3001
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3002
                                
 
3003
                                if (refTypeExpr.Expr != null)
 
3004
                                        result.AddChild ((Expression)refTypeExpr.Expr.Accept (this), Roles.Argument);
 
3005
                                
 
3006
                                if (location != null && location.Count > 1)
 
3007
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3008
                                return result;
 
3009
                        }
 
3010
                        
 
3011
                        public override object Visit (RefValueExpr refValueExpr)
 
3012
                        {
 
3013
                                var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefValue };
 
3014
                                result.AddChild (new CSharpTokenNode (Convert (refValueExpr.Location), UndocumentedExpression.RefvalueKeywordRole), UndocumentedExpression.RefvalueKeywordRole);
 
3015
                                var location = LocationsBag.GetLocations (refValueExpr);
 
3016
                                if (location != null)
 
3017
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3018
                                
 
3019
                                
 
3020
                                if (refValueExpr.Expr != null)
 
3021
                                        result.AddChild ((Expression)refValueExpr.Expr.Accept (this), Roles.Argument);
 
3022
 
 
3023
                                if (refValueExpr.FullNamedExpression != null)
 
3024
                                        result.AddChild ((Expression)refValueExpr.FullNamedExpression.Accept (this), Roles.Argument);
 
3025
                                
 
3026
                                if (location != null && location.Count > 1)
 
3027
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3028
                                return result;
 
3029
                        }
 
3030
                        #endregion
 
3031
                        
 
3032
                        public override object Visit (TypeOf typeOfExpression)
 
3033
                        {
 
3034
                                var result = new TypeOfExpression ();
 
3035
                                var location = LocationsBag.GetLocations (typeOfExpression);
 
3036
                                result.AddChild (new CSharpTokenNode (Convert (typeOfExpression.Location), TypeOfExpression.TypeofKeywordRole), TypeOfExpression.TypeofKeywordRole);
 
3037
                                if (location != null)
 
3038
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3039
                                if (typeOfExpression.TypeExpression != null)
 
3040
                                        result.AddChild (ConvertToType (typeOfExpression.TypeExpression), Roles.Type);
 
3041
                                if (location != null && location.Count > 1)
 
3042
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3043
                                return result;
 
3044
                        }
 
3045
                        
 
3046
                        public override object Visit (SizeOf sizeOfExpression)
 
3047
                        {
 
3048
                                var result = new SizeOfExpression ();
 
3049
                                var location = LocationsBag.GetLocations (sizeOfExpression);
 
3050
                                result.AddChild (new CSharpTokenNode (Convert (sizeOfExpression.Location), SizeOfExpression.SizeofKeywordRole), SizeOfExpression.SizeofKeywordRole);
 
3051
                                if (location != null)
 
3052
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3053
                                if (sizeOfExpression.TypeExpression != null)
 
3054
                                        result.AddChild (ConvertToType (sizeOfExpression.TypeExpression), Roles.Type);
 
3055
                                if (location != null && location.Count > 1)
 
3056
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3057
                                return result;
 
3058
                        }
 
3059
                        
 
3060
                        public override object Visit (CheckedExpr checkedExpression)
 
3061
                        {
 
3062
                                var result = new CheckedExpression ();
 
3063
                                var location = LocationsBag.GetLocations (checkedExpression);
 
3064
                                result.AddChild (new CSharpTokenNode (Convert (checkedExpression.Location), CheckedExpression.CheckedKeywordRole), CheckedExpression.CheckedKeywordRole);
 
3065
                                if (location != null)
 
3066
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3067
                                if (checkedExpression.Expr != null)
 
3068
                                        result.AddChild ((Expression)checkedExpression.Expr.Accept (this), Roles.Expression);
 
3069
                                if (location != null && location.Count > 1)
 
3070
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3071
                                return result;
 
3072
                        }
 
3073
                        
 
3074
                        public override object Visit (UnCheckedExpr uncheckedExpression)
 
3075
                        {
 
3076
                                var result = new UncheckedExpression ();
 
3077
                                var location = LocationsBag.GetLocations (uncheckedExpression);
 
3078
                                result.AddChild (new CSharpTokenNode (Convert (uncheckedExpression.Location), UncheckedExpression.UncheckedKeywordRole), UncheckedExpression.UncheckedKeywordRole);
 
3079
                                if (location != null)
 
3080
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
 
3081
                                if (uncheckedExpression.Expr != null)
 
3082
                                        result.AddChild ((Expression)uncheckedExpression.Expr.Accept (this), Roles.Expression);
 
3083
                                if (location != null && location.Count > 1)
 
3084
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
 
3085
                                return result;
 
3086
                        }
 
3087
                        
 
3088
                        public override object Visit (ElementAccess elementAccessExpression)
 
3089
                        {
 
3090
                                IndexerExpression result = new IndexerExpression ();
 
3091
                                var location = LocationsBag.GetLocations (elementAccessExpression);
 
3092
                                
 
3093
                                if (elementAccessExpression.Expr != null)
 
3094
                                        result.AddChild ((Expression)elementAccessExpression.Expr.Accept (this), Roles.TargetExpression);
 
3095
                                result.AddChild (new CSharpTokenNode (Convert (elementAccessExpression.Location), Roles.LBracket), Roles.LBracket);
 
3096
                                AddArguments (result, location, elementAccessExpression.Arguments);
 
3097
                                if (location != null)
 
3098
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RBracket), Roles.RBracket);
 
3099
                                return result;
 
3100
                        }
 
3101
                        
 
3102
                        public override object Visit (BaseThis baseAccessExpression)
 
3103
                        {
 
3104
                                var result = new BaseReferenceExpression ();
 
3105
                                result.Location = Convert (baseAccessExpression.Location);
 
3106
                                return result;
 
3107
                        }
 
3108
                        
 
3109
                        public override object Visit (StackAlloc stackAllocExpression)
 
3110
                        {
 
3111
                                var result = new StackAllocExpression ();
 
3112
                                
 
3113
                                var location = LocationsBag.GetLocations (stackAllocExpression);
 
3114
                                if (location != null)
 
3115
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), StackAllocExpression.StackallocKeywordRole), StackAllocExpression.StackallocKeywordRole);
 
3116
                                if (stackAllocExpression.TypeExpression != null)
 
3117
                                        result.AddChild (ConvertToType (stackAllocExpression.TypeExpression), Roles.Type);
 
3118
                                if (location != null && location.Count > 1)
 
3119
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LBracket), Roles.LBracket);
 
3120
                                if (stackAllocExpression.CountExpression != null)
 
3121
                                        result.AddChild ((Expression)stackAllocExpression.CountExpression.Accept (this), Roles.Expression);
 
3122
                                if (location != null && location.Count > 2)
 
3123
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RBracket), Roles.RBracket);
 
3124
                                return result;
 
3125
                        }
 
3126
                        
 
3127
                        public override object Visit (SimpleAssign simpleAssign)
 
3128
                        {
 
3129
                                var result = new AssignmentExpression ();
 
3130
                                
 
3131
                                result.Operator = AssignmentOperatorType.Assign;
 
3132
                                if (simpleAssign.Target != null)
 
3133
                                        result.AddChild ((Expression)simpleAssign.Target.Accept (this), AssignmentExpression.LeftRole);
 
3134
                                var location = LocationsBag.GetLocations (simpleAssign);
 
3135
                                if (location != null)
 
3136
                                        result.AddChild (new CSharpTokenNode (Convert (location[0]), AssignmentExpression.AssignRole), AssignmentExpression.AssignRole);
 
3137
                                if (simpleAssign.Source != null) {
 
3138
                                        result.AddChild ((Expression)simpleAssign.Source.Accept (this), AssignmentExpression.RightRole);
 
3139
                                }
 
3140
                                return result;
 
3141
                        }
 
3142
                        
 
3143
                        public override object Visit(CompoundAssign compoundAssign)
 
3144
                        {
 
3145
                                var result = new AssignmentExpression();
 
3146
                                switch (compoundAssign.Op) {
 
3147
                                        case Binary.Operator.Multiply:
 
3148
                                                result.Operator = AssignmentOperatorType.Multiply;
 
3149
                                                break;
 
3150
                                        case Binary.Operator.Division:
 
3151
                                                result.Operator = AssignmentOperatorType.Divide;
 
3152
                                                break;
 
3153
                                        case Binary.Operator.Modulus:
 
3154
                                                result.Operator = AssignmentOperatorType.Modulus;
 
3155
                                                break;
 
3156
                                        case Binary.Operator.Addition:
 
3157
                                                result.Operator = AssignmentOperatorType.Add;
 
3158
                                                break;
 
3159
                                        case Binary.Operator.Subtraction:
 
3160
                                                result.Operator = AssignmentOperatorType.Subtract;
 
3161
                                                break;
 
3162
                                        case Binary.Operator.LeftShift:
 
3163
                                                result.Operator = AssignmentOperatorType.ShiftLeft;
 
3164
                                                break;
 
3165
                                        case Binary.Operator.RightShift:
 
3166
                                                result.Operator = AssignmentOperatorType.ShiftRight;
 
3167
                                                break;
 
3168
                                        case Binary.Operator.BitwiseAnd:
 
3169
                                                result.Operator = AssignmentOperatorType.BitwiseAnd;
 
3170
                                                break;
 
3171
                                        case Binary.Operator.BitwiseOr:
 
3172
                                                result.Operator = AssignmentOperatorType.BitwiseOr;
 
3173
                                                break;
 
3174
                                        case Binary.Operator.ExclusiveOr:
 
3175
                                                result.Operator = AssignmentOperatorType.ExclusiveOr;
 
3176
                                                break;
 
3177
                                }
 
3178
                                
 
3179
                                if (compoundAssign.Target != null)
 
3180
                                        result.AddChild((Expression)compoundAssign.Target.Accept(this), AssignmentExpression.LeftRole);
 
3181
                                var location = LocationsBag.GetLocations(compoundAssign);
 
3182
                                if (location != null) {
 
3183
                                        var r = AssignmentExpression.GetOperatorRole (result.Operator);
 
3184
                                        result.AddChild(new CSharpTokenNode(Convert(location [0]), r), r);
 
3185
                                }       
 
3186
                                if (compoundAssign.Source != null)
 
3187
                                        result.AddChild ((Expression)compoundAssign.Source.Accept (this), AssignmentExpression.RightRole);
 
3188
                                return result;
 
3189
                        }
 
3190
                        
 
3191
                        public override object Visit (Mono.CSharp.AnonymousMethodExpression anonymousMethodExpression)
 
3192
                        {
 
3193
                                var result = new AnonymousMethodExpression ();
 
3194
                                var location = LocationsBag.GetLocations (anonymousMethodExpression);
 
3195
                                int l = 0;
 
3196
                                if (anonymousMethodExpression.IsAsync) {
 
3197
                                        result.IsAsync = true;
 
3198
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++]), AnonymousMethodExpression.AsyncModifierRole), AnonymousMethodExpression.AsyncModifierRole);
 
3199
                                }
 
3200
                                if (location != null) {
 
3201
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++]), AnonymousMethodExpression.DelegateKeywordRole), AnonymousMethodExpression.DelegateKeywordRole);
 
3202
                                        
 
3203
                                        if (location.Count > l) {
 
3204
                                                result.HasParameterList = true;
 
3205
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.LPar), Roles.LPar);
 
3206
                                                AddParameter (result, anonymousMethodExpression.Parameters);
 
3207
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.RPar), Roles.RPar);
 
3208
                                        }
 
3209
                                }
 
3210
                                if (anonymousMethodExpression.Block != null)
 
3211
                                        result.AddChild ((BlockStatement)anonymousMethodExpression.Block.Accept (this), Roles.Body);
 
3212
                                return result;
 
3213
                        }
 
3214
 
 
3215
                        public override object Visit (Mono.CSharp.LambdaExpression lambdaExpression)
 
3216
                        {
 
3217
                                var result = new LambdaExpression ();
 
3218
                                var location = LocationsBag.GetLocations (lambdaExpression);
 
3219
                                int l = 0;
 
3220
                                if (lambdaExpression.IsAsync) {
 
3221
                                        result.IsAsync = true;
 
3222
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.AsyncModifierRole), LambdaExpression.AsyncModifierRole);
 
3223
                                }
 
3224
                                if (location == null || location.Count == l + 1) {
 
3225
                                        if (lambdaExpression.Block != null)
 
3226
                                                AddParameter (result, lambdaExpression.Parameters);
 
3227
                                        if (location != null)
 
3228
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.ArrowRole), LambdaExpression.ArrowRole);
 
3229
                                } else {
 
3230
                                        result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.LPar), Roles.LPar);
 
3231
                                        if (lambdaExpression.Block != null)
 
3232
                                                AddParameter (result, lambdaExpression.Parameters);
 
3233
                                        if (location != null) {
 
3234
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.RPar), Roles.RPar);
 
3235
                                                result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.ArrowRole), LambdaExpression.ArrowRole);
 
3236
                                        }
 
3237
                                }
 
3238
                                if (lambdaExpression.Block != null) {
 
3239
                                        if (lambdaExpression.Block.IsCompilerGenerated) {
 
3240
                                                ContextualReturn generatedReturn = (ContextualReturn)lambdaExpression.Block.Statements [0];
 
3241
                                                result.AddChild ((AstNode)generatedReturn.Expr.Accept (this), LambdaExpression.BodyRole);
 
3242
                                        } else {
 
3243
                                                result.AddChild ((AstNode)lambdaExpression.Block.Accept (this), LambdaExpression.BodyRole);
 
3244
                                        }
 
3245
                                }
 
3246
                                return result;
 
3247
                        }
 
3248
                        
 
3249
                        public override object Visit (ConstInitializer constInitializer)
 
3250
                        {
 
3251
                                return constInitializer.Expr.Accept (this);
 
3252
                        }
 
3253
                        
 
3254
                        public override object Visit (ArrayInitializer arrayInitializer)
 
3255
                        {
 
3256
                                var result = new ArrayInitializerExpression ();
 
3257
                                var location = LocationsBag.GetLocations (arrayInitializer);
 
3258
                                result.AddChild (new CSharpTokenNode (Convert (arrayInitializer.Location), Roles.LBrace), Roles.LBrace);
 
3259
                                var commaLocations = LocationsBag.GetLocations (arrayInitializer.Elements);
 
3260
                                for (int i = 0; i < arrayInitializer.Count; i++) {
 
3261
                                        var init = arrayInitializer [i];
 
3262
                                        if (init == null)
 
3263
                                                continue;
 
3264
                                        result.AddChild ((Expression)init.Accept (this), Roles.Expression);
 
3265
                                        if (commaLocations != null && i < commaLocations.Count)
 
3266
                                                result.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
 
3267
                                }
 
3268
                                
 
3269
                                if (location != null) {
 
3270
                                        if (location.Count == 2) // optional comma
 
3271
                                                result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Comma), Roles.Comma);
 
3272
                                        result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.RBrace), Roles.RBrace);
 
3273
                                }
 
3274
                                return result;
 
3275
                        }
 
3276
                        
 
3277
                        #endregion
 
3278
                        
 
3279
                        #region LINQ expressions
 
3280
                        QueryOrderClause currentQueryOrderClause;
 
3281
 
 
3282
                        public override object Visit (Mono.CSharp.Linq.QueryExpression queryExpression)
 
3283
                        {
 
3284
                                var oldQueryOrderClause = currentQueryOrderClause;
 
3285
                                try {
 
3286
                                        currentQueryOrderClause = null;
 
3287
                                        var result = new QueryExpression ();
 
3288
                                        
 
3289
                                        var currentClause = queryExpression.next;
 
3290
                                        
 
3291
                                        while (currentClause != null) {
 
3292
                                                QueryClause clause = (QueryClause)currentClause.Accept (this);
 
3293
                                                if (clause is QueryContinuationClause) {
 
3294
                                                        // insert preceding query at beginning of QueryContinuationClause
 
3295
                                                        clause.InsertChildAfter (null, result, QueryContinuationClause.PrecedingQueryRole);
 
3296
                                                        // create a new QueryExpression for the remaining query
 
3297
                                                        result = new QueryExpression ();
 
3298
                                                }
 
3299
                                                if (clause != null) {
 
3300
                                                        result.AddChild (clause, QueryExpression.ClauseRole);
 
3301
                                                }
 
3302
                                                currentClause = currentClause.next;
 
3303
                                        }
 
3304
                                        
 
3305
                                        return result;
 
3306
                                }
 
3307
                                finally {
 
3308
                                        currentQueryOrderClause = oldQueryOrderClause;
 
3309
                                }
 
3310
                        }
 
3311
                        
 
3312
                        public override object Visit (Mono.CSharp.Linq.QueryStartClause queryStart)
 
3313
                        {
 
3314
                                if (queryStart.Expr == null) {
 
3315
                                        var intoClause = new QueryContinuationClause ();
 
3316
                                        intoClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryContinuationClause.IntoKeywordRole), QueryContinuationClause.IntoKeywordRole);
 
3317
                                        intoClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
 
3318
                                        return intoClause;
 
3319
                                }
 
3320
                                
 
3321
                                var fromClause = new QueryFromClause ();
 
3322
 
 
3323
                                fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryFromClause.FromKeywordRole), QueryFromClause.FromKeywordRole);
 
3324
                                
 
3325
                                if (queryStart.IdentifierType != null)
 
3326
                                        fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
 
3327
                                
 
3328
                                fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
 
3329
                                
 
3330
                                var location = LocationsBag.GetLocations (queryStart);
 
3331
                                if (location != null)
 
3332
                                        fromClause.AddChild (new CSharpTokenNode (Convert (location [0]), QueryFromClause.InKeywordRole), QueryFromClause.InKeywordRole);
 
3333
 
 
3334
                                if (queryStart.Expr != null)
 
3335
                                        fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
 
3336
                                return fromClause;
 
3337
                        }
 
3338
                        
 
3339
                        public override object Visit (Mono.CSharp.Linq.SelectMany queryStart)
 
3340
                        {
 
3341
                                var fromClause = new QueryFromClause ();
 
3342
 
 
3343
                                fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryFromClause.FromKeywordRole), QueryFromClause.FromKeywordRole);
 
3344
                                
 
3345
                                if (queryStart.IdentifierType != null)
 
3346
                                        fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
 
3347
                                
 
3348
                                fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
 
3349
                                
 
3350
                                var location = LocationsBag.GetLocations (queryStart);
 
3351
                                if (location != null)
 
3352
                                        fromClause.AddChild (new CSharpTokenNode (Convert (location [0]), QueryFromClause.InKeywordRole), QueryFromClause.InKeywordRole);
 
3353
 
 
3354
                                if (queryStart.Expr != null)
 
3355
                                        fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
 
3356
                                return fromClause;
 
3357
                        }
 
3358
                        
 
3359
                        public override object Visit (Mono.CSharp.Linq.Select sel)
 
3360
                        {
 
3361
                                var result = new QuerySelectClause ();
 
3362
                                result.AddChild (new CSharpTokenNode (Convert (sel.Location), QuerySelectClause.SelectKeywordRole), QuerySelectClause.SelectKeywordRole);
 
3363
                                if (sel.Expr != null)
 
3364
                                        result.AddChild ((Expression)sel.Expr.Accept (this), Roles.Expression);
 
3365
                                return result;
 
3366
                        }
 
3367
                        
 
3368
                        public override object Visit (Mono.CSharp.Linq.GroupBy groupBy)
 
3369
                        {
 
3370
                                var result = new QueryGroupClause ();
 
3371
                                var location = LocationsBag.GetLocations (groupBy);
 
3372
                                result.AddChild (new CSharpTokenNode (Convert (groupBy.Location), QueryGroupClause.GroupKeywordRole), QueryGroupClause.GroupKeywordRole);
 
3373
                                if (groupBy.ElementSelector != null)
 
3374
                                        result.AddChild ((Expression)groupBy.ElementSelector.Accept (this), QueryGroupClause.ProjectionRole);
 
3375
                                if (location != null)
 
3376
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryGroupClause.ByKeywordRole), QueryGroupClause.ByKeywordRole);
 
3377
                                if (groupBy.Expr != null)
 
3378
                                        result.AddChild ((Expression)groupBy.Expr.Accept (this), QueryGroupClause.KeyRole);
 
3379
                                return result;
 
3380
                        }
 
3381
                        
 
3382
                        public override object Visit (Mono.CSharp.Linq.Let l)
 
3383
                        {
 
3384
                                var result = new QueryLetClause ();
 
3385
                                var location = LocationsBag.GetLocations (l);
 
3386
                                
 
3387
                                result.AddChild (new CSharpTokenNode (Convert (l.Location), QueryLetClause.LetKeywordRole), QueryLetClause.LetKeywordRole);
 
3388
                                result.AddChild (Identifier.Create (l.IntoVariable.Name, Convert (l.IntoVariable.Location)), Roles.Identifier);
 
3389
                                if (location != null)
 
3390
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Assign), Roles.Assign);
 
3391
                                if (l.Expr != null)
 
3392
                                        result.AddChild ((Expression)l.Expr.Accept (this), Roles.Expression);
 
3393
                                return result;
 
3394
                        }
 
3395
                        
 
3396
                        public override object Visit (Mono.CSharp.Linq.Where w)
 
3397
                        {
 
3398
                                var result = new QueryWhereClause ();
 
3399
                                result.AddChild (new CSharpTokenNode (Convert (w.Location), QueryWhereClause.WhereKeywordRole), QueryWhereClause.WhereKeywordRole);
 
3400
                                if (w.Expr != null)
 
3401
                                        result.AddChild ((Expression)w.Expr.Accept (this), Roles.Condition);
 
3402
                                return result;
 
3403
                        }
 
3404
                        
 
3405
                        public override object Visit (Mono.CSharp.Linq.Join join)
 
3406
                        {
 
3407
                                var result = new QueryJoinClause ();
 
3408
                                var location = LocationsBag.GetLocations (join);
 
3409
                                result.AddChild (new CSharpTokenNode (Convert (join.Location), QueryJoinClause.JoinKeywordRole), QueryJoinClause.JoinKeywordRole);
 
3410
                                result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
 
3411
                                
 
3412
                                if (location != null)
 
3413
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryJoinClause.InKeywordRole), QueryJoinClause.InKeywordRole);
 
3414
 
 
3415
                                if (join.Expr != null)
 
3416
                                        result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
 
3417
                                
 
3418
                                if (location != null && location.Count > 1)
 
3419
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), QueryJoinClause.OnKeywordRole), QueryJoinClause.OnKeywordRole);
 
3420
                                
 
3421
                                var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
 
3422
                                if (outer != null)
 
3423
                                        result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
 
3424
                                
 
3425
                                if (location != null && location.Count > 2)
 
3426
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), QueryJoinClause.EqualsKeywordRole), QueryJoinClause.EqualsKeywordRole);
 
3427
                                
 
3428
                                var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
 
3429
                                if (inner != null)
 
3430
                                        result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
 
3431
                                
 
3432
                                return result;
 
3433
                        }
 
3434
                        
 
3435
                        public override object Visit (Mono.CSharp.Linq.GroupJoin join)
 
3436
                        {
 
3437
                                var result = new QueryJoinClause ();
 
3438
                                var location = LocationsBag.GetLocations (join);
 
3439
                                result.AddChild (new CSharpTokenNode (Convert (join.Location), QueryJoinClause.JoinKeywordRole), QueryJoinClause.JoinKeywordRole);
 
3440
                                
 
3441
                                // mcs seems to have swapped IntoVariable with JoinVariable, so we'll swap it back here
 
3442
                                result.AddChild (Identifier.Create (join.IntoVariable.Name, Convert (join.IntoVariable.Location)), QueryJoinClause.JoinIdentifierRole);
 
3443
                                
 
3444
                                if (location != null)
 
3445
                                        result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryJoinClause.InKeywordRole), QueryJoinClause.InKeywordRole);
 
3446
 
 
3447
                                if (join.Expr != null)
 
3448
                                        result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
 
3449
 
 
3450
                                if (location != null && location.Count > 1)
 
3451
                                        result.AddChild (new CSharpTokenNode (Convert (location [1]), QueryJoinClause.OnKeywordRole), QueryJoinClause.OnKeywordRole);
 
3452
 
 
3453
                                var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
 
3454
                                if (outer != null)
 
3455
                                        result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
 
3456
                                
 
3457
 
 
3458
                                if (location != null && location.Count > 2)
 
3459
                                        result.AddChild (new CSharpTokenNode (Convert (location [2]), QueryJoinClause.EqualsKeywordRole), QueryJoinClause.EqualsKeywordRole);
 
3460
                                var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
 
3461
                                if (inner != null)
 
3462
                                        result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
 
3463
                                
 
3464
                                if (location != null && location.Count > 3)
 
3465
                                        result.AddChild (new CSharpTokenNode (Convert (location [3]), QueryJoinClause.IntoKeywordRole), QueryJoinClause.IntoKeywordRole);
 
3466
                                
 
3467
                                result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.IntoIdentifierRole);
 
3468
                                return result;
 
3469
                        }
 
3470
                        
 
3471
                        public override object Visit (Mono.CSharp.Linq.OrderByAscending orderByAscending)
 
3472
                        {
 
3473
                                currentQueryOrderClause = new QueryOrderClause();
 
3474
                                var location2 = LocationsBag.GetLocations (orderByAscending.block);
 
3475
                                if (location2 != null)
 
3476
                                        currentQueryOrderClause.AddChild (new CSharpTokenNode (Convert (location2 [0]), QueryOrderClause.OrderbyKeywordRole), QueryOrderClause.OrderbyKeywordRole);
 
3477
                                var ordering = new QueryOrdering ();
 
3478
                                if (orderByAscending.Expr != null)
 
3479
                                        ordering.AddChild ((Expression)orderByAscending.Expr.Accept (this), Roles.Expression);
 
3480
                                var location = LocationsBag.GetLocations (orderByAscending);
 
3481
                                if (location != null) {
 
3482
                                        ordering.Direction = QueryOrderingDirection.Ascending;
 
3483
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.AscendingKeywordRole), QueryOrdering.AscendingKeywordRole);
 
3484
                                }
 
3485
                                currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
 
3486
                                return currentQueryOrderClause;
 
3487
                        }
 
3488
                        
 
3489
                        public override object Visit (Mono.CSharp.Linq.OrderByDescending orderByDescending)
 
3490
                        {
 
3491
                                currentQueryOrderClause = new QueryOrderClause ();
 
3492
                                
 
3493
                                var ordering = new QueryOrdering ();
 
3494
                                if (orderByDescending.Expr != null)
 
3495
                                        ordering.AddChild ((Expression)orderByDescending.Expr.Accept (this), Roles.Expression);
 
3496
                                var location = LocationsBag.GetLocations (orderByDescending);
 
3497
                                if (location != null) {
 
3498
                                        ordering.Direction = QueryOrderingDirection.Descending;
 
3499
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.DescendingKeywordRole), QueryOrdering.DescendingKeywordRole);
 
3500
                                }
 
3501
                                currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
 
3502
                                return currentQueryOrderClause;
 
3503
                        }
 
3504
                        
 
3505
                        public override object Visit (Mono.CSharp.Linq.ThenByAscending thenByAscending)
 
3506
                        {
 
3507
                                var ordering = new QueryOrdering ();
 
3508
                                if (thenByAscending.Expr != null)
 
3509
                                        ordering.AddChild ((Expression)thenByAscending.Expr.Accept (this), Roles.Expression);
 
3510
                                var location = LocationsBag.GetLocations (thenByAscending);
 
3511
                                if (location != null) {
 
3512
                                        ordering.Direction = QueryOrderingDirection.Ascending;
 
3513
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.AscendingKeywordRole), QueryOrdering.AscendingKeywordRole);
 
3514
                                }
 
3515
                                currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
 
3516
                                return null;
 
3517
                        }
 
3518
                        
 
3519
                        public override object Visit (Mono.CSharp.Linq.ThenByDescending thenByDescending)
 
3520
                        {
 
3521
                                var ordering = new QueryOrdering ();
 
3522
                                if (thenByDescending.Expr != null)
 
3523
                                        ordering.AddChild ((Expression)thenByDescending.Expr.Accept (this), Roles.Expression);
 
3524
                                var location = LocationsBag.GetLocations (thenByDescending);
 
3525
                                if (location != null) {
 
3526
                                        ordering.Direction = QueryOrderingDirection.Descending;
 
3527
                                        ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.DescendingKeywordRole), QueryOrdering.DescendingKeywordRole);
 
3528
                                }
 
3529
                                currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
 
3530
                                return null;
 
3531
                        }
 
3532
                        
 
3533
                        public override object Visit (Await awaitExpr)
 
3534
                        {
 
3535
                                var result = new UnaryOperatorExpression ();
 
3536
                                result.Operator = UnaryOperatorType.Await;
 
3537
                                result.AddChild (new CSharpTokenNode (Convert (awaitExpr.Location), UnaryOperatorExpression.AwaitRole), UnaryOperatorExpression.AwaitRole);
 
3538
                                if (awaitExpr.Expression != null)
 
3539
                                        result.AddChild ((Expression)awaitExpr.Expression.Accept (this), Roles.Expression);
 
3540
                                return result;
 
3541
                        }
 
3542
                        #endregion
 
3543
                        
 
3544
                        #region XmlDoc
 
3545
                        public DocumentationReference ConvertXmlDoc(DocumentationBuilder doc)
 
3546
                        {
 
3547
                                DocumentationReference result = new DocumentationReference();
 
3548
                                if (doc.ParsedName != null) {
 
3549
                                        if (doc.ParsedName.Name == "<this>") {
 
3550
                                                result.EntityType = EntityType.Indexer;
 
3551
                                        } else {
 
3552
                                                result.MemberName = doc.ParsedName.Name;
 
3553
                                        }
 
3554
                                        if (doc.ParsedName.Left != null) {
 
3555
                                                result.DeclaringType = ConvertToType(doc.ParsedName.Left);
 
3556
                                        } else if (doc.ParsedBuiltinType != null) {
 
3557
                                                result.DeclaringType = ConvertToType(doc.ParsedBuiltinType);
 
3558
                                        }
 
3559
                                        if (doc.ParsedName.TypeParameters != null) {
 
3560
                                                for (int i = 0; i < doc.ParsedName.TypeParameters.Count; i++) {
 
3561
                                                        result.TypeArguments.Add(ConvertToType(doc.ParsedName.TypeParameters[i]));
 
3562
                                                }
 
3563
                                        }
 
3564
                                } else if (doc.ParsedBuiltinType != null) {
 
3565
                                        result.EntityType = EntityType.TypeDefinition;
 
3566
                                        result.DeclaringType = ConvertToType(doc.ParsedBuiltinType);
 
3567
                                }
 
3568
                                if (doc.ParsedParameters != null) {
 
3569
                                        result.HasParameterList = true;
 
3570
                                        result.Parameters.AddRange(doc.ParsedParameters.Select(ConvertXmlDocParameter));
 
3571
                                }
 
3572
                                if (doc.ParsedOperator != null) {
 
3573
                                        result.EntityType = EntityType.Operator;
 
3574
                                        result.OperatorType = (OperatorType)doc.ParsedOperator;
 
3575
                                        if (result.OperatorType == OperatorType.Implicit || result.OperatorType == OperatorType.Explicit) {
 
3576
                                                var returnTypeParam = result.Parameters.LastOrNullObject();
 
3577
                                                returnTypeParam.Remove(); // detach from parameter list
 
3578
                                                var returnType = returnTypeParam.Type;
 
3579
                                                returnType.Remove();
 
3580
                                                result.ConversionOperatorReturnType = returnType;
 
3581
                                        }
 
3582
                                        if (result.Parameters.Count == 0) {
 
3583
                                                // reset HasParameterList if necessary
 
3584
                                                result.HasParameterList = false;
 
3585
                                        }
 
3586
                                }
 
3587
                                return result;
 
3588
                        }
 
3589
                        
 
3590
                        ParameterDeclaration ConvertXmlDocParameter(DocumentationParameter p)
 
3591
                        {
 
3592
                                ParameterDeclaration result = new ParameterDeclaration();
 
3593
                                switch (p.Modifier) {
 
3594
                                        case Parameter.Modifier.OUT:
 
3595
                                                result.ParameterModifier = ParameterModifier.Out;
 
3596
                                                break;
 
3597
                                        case Parameter.Modifier.REF:
 
3598
                                                result.ParameterModifier = ParameterModifier.Ref;
 
3599
                                                break;
 
3600
                                        case Parameter.Modifier.PARAMS:
 
3601
                                                result.ParameterModifier = ParameterModifier.Params;
 
3602
                                                break;
 
3603
                                }
 
3604
                                if (p.Type != null) {
 
3605
                                        result.Type = ConvertToType(p.Type);
 
3606
                                }
 
3607
                                return result;
 
3608
                        }
 
3609
                        #endregion
 
3610
                }
 
3611
 
 
3612
                public CSharpParser ()
 
3613
                {
 
3614
                        compilerSettings = new CompilerSettings();
 
3615
                }
 
3616
                
 
3617
                public CSharpParser (CompilerSettings args)
 
3618
                {
 
3619
                        compilerSettings = args ?? new CompilerSettings();
 
3620
                }
 
3621
                
 
3622
                static AstNode GetOuterLeft (AstNode node)
 
3623
                {
 
3624
                        var outerLeft = node;
 
3625
                        while (outerLeft.FirstChild != null)
 
3626
                                outerLeft = outerLeft.FirstChild;
 
3627
                        return outerLeft;
 
3628
                }
 
3629
                
 
3630
                static AstNode NextLeaf (AstNode node)
 
3631
                {
 
3632
                        if (node == null)
 
3633
                                return null;
 
3634
                        var next = node.NextSibling;
 
3635
                        if (next == null)
 
3636
                                return node.Parent;
 
3637
                        return GetOuterLeft (next);
 
3638
                }
 
3639
                
 
3640
                void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
 
3641
                {
 
3642
                        var leaf = GetOuterLeft (conversionVisitor.Unit);
 
3643
                        for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) {
 
3644
                                var special = top.SpecialsBag.Specials [i];
 
3645
                                AstNode newLeaf = null;
 
3646
                                var comment = special as SpecialsBag.Comment;
 
3647
                                if (comment != null) {
 
3648
                                        // HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer
 
3649
                                        bool isMultilineDocumentationComment = (
 
3650
                                                comment.CommentType == SpecialsBag.CommentType.Multi
 
3651
                                                && comment.Content.StartsWith("*", StringComparison.Ordinal)
 
3652
                                                && !comment.Content.StartsWith("**", StringComparison.Ordinal)
 
3653
                                        );
 
3654
                                        if (conversionVisitor.convertTypeSystemMode && !(comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment))
 
3655
                                                continue;
 
3656
                                        var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType;
 
3657
                                        var start = new TextLocation (comment.Line, comment.Col);
 
3658
                                        var end = new TextLocation (comment.EndLine, comment.EndCol);
 
3659
                                        newLeaf = new Comment (type, start, end) {
 
3660
                                                StartsLine = comment.StartsLine,
 
3661
                                                Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
 
3662
                                        };
 
3663
                                } else if (!GenerateTypeSystemMode) {
 
3664
                                        var directive = special as SpecialsBag.PreProcessorDirective;
 
3665
                                        if (directive != null) {
 
3666
                                                newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
 
3667
                                                        Argument = directive.Arg,
 
3668
                                                        Take = directive.Take
 
3669
                                                };
 
3670
                                        } else {
 
3671
                                                var newLine = special as SpecialsBag.NewLineToken;
 
3672
                                                if (newLine != null) {
 
3673
                                                        if (newLine.NewLine == SpecialsBag.NewLine.Unix) {
 
3674
                                                                newLeaf = new UnixNewLine (new TextLocation (newLine.Line, newLine.Col + 1));
 
3675
                                                        } else {
 
3676
                                                                newLeaf = new WindowsNewLine (new TextLocation (newLine.Line, newLine.Col + 1));
 
3677
                                                        }
 
3678
                                                }
 
3679
                                        }
 
3680
                                }
 
3681
                                if (newLeaf == null)
 
3682
                                        continue;
 
3683
                                
 
3684
                                while (true) {
 
3685
                                        var nextLeaf = NextLeaf (leaf);
 
3686
                                        // insert comment at begin
 
3687
                                        if (newLeaf.StartLocation < leaf.StartLocation) {
 
3688
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
 
3689
                                                while (node.Parent != null && node.FirstChild == leaf) {
 
3690
                                                        leaf = node;
 
3691
                                                        node = node.Parent;
 
3692
                                                }
 
3693
                                                if (newLeaf is NewLineNode) {
 
3694
                                                        node.InsertChildBefore (leaf, (NewLineNode)newLeaf, Roles.NewLine);
 
3695
                                                } else if (newLeaf is Comment) {
 
3696
                                                        node.InsertChildBefore (leaf, (Comment)newLeaf, Roles.Comment);
 
3697
                                                } else {
 
3698
                                                        node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
 
3699
                                                }
 
3700
                                                leaf = newLeaf;
 
3701
                                                break;
 
3702
                                        }
 
3703
                                        
 
3704
                                        // insert comment at the end
 
3705
                                        if (nextLeaf == null) {
 
3706
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
 
3707
                                                if (newLeaf is NewLineNode) {
 
3708
                                                        node.AddChild ((NewLineNode)newLeaf, Roles.NewLine);
 
3709
                                                } else if (newLeaf is Comment) {
 
3710
                                                        node.AddChild ((Comment)newLeaf, Roles.Comment);
 
3711
                                                } else {
 
3712
                                                        node.AddChild ((PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
 
3713
                                                }
 
3714
                                                leaf = newLeaf;
 
3715
                                                break;
 
3716
                                        }
 
3717
                                        
 
3718
                                        // comment is between 2 nodes
 
3719
                                        if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) {
 
3720
                                                var node = leaf.Parent ?? conversionVisitor.Unit;
 
3721
                                                if (newLeaf is NewLineNode) {
 
3722
                                                        node.InsertChildAfter (leaf, (NewLineNode)newLeaf, Roles.NewLine);
 
3723
                                                } else if (newLeaf is Comment) {
 
3724
                                                        node.InsertChildAfter (leaf, (Comment)newLeaf, Roles.Comment);
 
3725
                                                } else {
 
3726
                                                        node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
 
3727
                                                }
 
3728
                                                leaf = newLeaf;
 
3729
                                                break;
 
3730
                                        }
 
3731
                                        leaf = nextLeaf;
 
3732
                                }
 
3733
                        }
 
3734
                }
 
3735
                
 
3736
                public class ErrorReportPrinter : ReportPrinter
 
3737
                {
 
3738
                        readonly string fileName;
 
3739
                        public readonly List<Error> Errors = new List<Error> ();
 
3740
                        
 
3741
                        public ErrorReportPrinter (string fileName)
 
3742
                        {
 
3743
                                this.fileName = fileName;
 
3744
                        }
 
3745
                        
 
3746
                        public override void Print (AbstractMessage msg, bool showFullPath)
 
3747
                        {
 
3748
                                base.Print (msg, showFullPath);
 
3749
                                var newError = new Error (msg.IsWarning ? ErrorType.Warning : ErrorType.Error, msg.Text, new DomRegion (fileName, msg.Location.Row, msg.Location.Column));
 
3750
                                Errors.Add (newError);
 
3751
                        }
 
3752
                }
 
3753
                ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (null);
 
3754
                
 
3755
                [Obsolete("Use the Errors/Warnings/ErrorsAndWarnings properties instead")]
 
3756
                public ErrorReportPrinter ErrorPrinter {
 
3757
                        get {
 
3758
                                return errorReportPrinter;
 
3759
                        }
 
3760
                }
 
3761
                public bool HasErrors {
 
3762
                        get {
 
3763
                                return errorReportPrinter.ErrorsCount > 0;
 
3764
                        }
 
3765
                }
 
3766
                
 
3767
                public bool HasWarnings {
 
3768
                        get {
 
3769
                                return errorReportPrinter.WarningsCount > 0;
 
3770
                        }
 
3771
                }
 
3772
                
 
3773
                public IEnumerable<Error> Errors {
 
3774
                        get {
 
3775
                                return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Error);
 
3776
                        }
 
3777
                }
 
3778
                
 
3779
                public IEnumerable<Error> Warnings {
 
3780
                        get {
 
3781
                                return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Warning);
 
3782
                        }
 
3783
                }
 
3784
                
 
3785
                public IEnumerable<Error> ErrorsAndWarnings {
 
3786
                        get { return errorReportPrinter.Errors; }
 
3787
                }
 
3788
                
 
3789
                /// <summary>
 
3790
                /// Parses a C# code file.
 
3791
                /// </summary>
 
3792
                /// <param name="program">The source code to parse.</param>
 
3793
                /// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
 
3794
                /// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
 
3795
                /// <returns>Returns the syntax tree.</returns>
 
3796
                public SyntaxTree Parse (string program, string fileName = "")
 
3797
                {
 
3798
                        return Parse (new StringTextSource (program), fileName);
 
3799
                }
 
3800
                
 
3801
                /// <summary>
 
3802
                /// Parses a C# code file.
 
3803
                /// </summary>
 
3804
                /// <param name="reader">The text reader containing the source code to parse.</param>
 
3805
                /// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
 
3806
                /// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
 
3807
                /// <returns>Returns the syntax tree.</returns>
 
3808
                public SyntaxTree Parse (TextReader reader, string fileName = "")
 
3809
                {
 
3810
                        return Parse(new StringTextSource (reader.ReadToEnd ()), fileName);
 
3811
                }
 
3812
 
 
3813
                /// <summary>
 
3814
                /// Converts a Mono.CSharp syntax tree into an NRefactory syntax tree.
 
3815
                /// </summary>
 
3816
                public SyntaxTree Parse(CompilerCompilationUnit top, string fileName)
 
3817
                {
 
3818
                        if (top == null) {
 
3819
                                return null;
 
3820
                        }
 
3821
                        CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag);
 
3822
                        top.ModuleCompiled.Accept(conversionVisitor);
 
3823
                        InsertComments(top, conversionVisitor);
 
3824
                        if (CompilationUnitCallback != null) {
 
3825
                                CompilationUnitCallback(top);
 
3826
                        }
 
3827
                        if (top.LastYYValue is Mono.CSharp.Expression) {
 
3828
                                conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode;
 
3829
                        }
 
3830
 
 
3831
                        conversionVisitor.Unit.FileName = fileName;
 
3832
                        conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.Concat (compilerSettings.ConditionalSymbols).ToArray ();
 
3833
                        return conversionVisitor.Unit;
 
3834
                }
 
3835
                
 
3836
                public CompilerSettings CompilerSettings {
 
3837
                        get { return compilerSettings; }
 
3838
                        set {
 
3839
                                if (value == null)
 
3840
                                        throw new ArgumentNullException();
 
3841
                                compilerSettings = value;
 
3842
                        }
 
3843
                }
 
3844
                
 
3845
                /// <summary>
 
3846
                /// Callback that gets called with the Mono.CSharp syntax tree whenever some code is parsed.
 
3847
                /// </summary>
 
3848
                public Action<CompilerCompilationUnit> CompilationUnitCallback {
 
3849
                        get;
 
3850
                        set;
 
3851
                }
 
3852
                
 
3853
                /// <summary>
 
3854
                /// Specifies whether to run the parser in a special mode for generating the type system.
 
3855
                /// If this property is true, the syntax tree will only contain nodes relevant for the
 
3856
                /// <see cref="SyntaxTree.ToTypeSystem()"/> call and might be missing other nodes (e.g. method bodies).
 
3857
                /// The default is false.
 
3858
                /// </summary>
 
3859
                public bool GenerateTypeSystemMode {
 
3860
                        get;
 
3861
                        set;
 
3862
                }
 
3863
                
 
3864
                TextLocation initialLocation = new TextLocation(1, 1);
 
3865
                
 
3866
                /// <summary>
 
3867
                /// Specifies the text location where parsing starts.
 
3868
                /// This property can be used when parsing a part of a file to make the locations of the AstNodes
 
3869
                /// refer to the position in the whole file.
 
3870
                /// The default is (1,1).
 
3871
                /// </summary>
 
3872
                public TextLocation InitialLocation {
 
3873
                        get { return initialLocation; }
 
3874
                        set { initialLocation = value; }
 
3875
                }
 
3876
                
 
3877
                internal static object parseLock = new object ();
 
3878
                
 
3879
                /// <summary>
 
3880
                /// Parses a C# code file.
 
3881
                /// </summary>
 
3882
                /// <param name="stream">The stream containing the source code to parse.</param>
 
3883
                /// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
 
3884
                /// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
 
3885
                /// <returns>Returns the syntax tree.</returns>
 
3886
                public SyntaxTree Parse (Stream stream, string fileName = "")
 
3887
                {
 
3888
                        return Parse (new StreamReader (stream), fileName);
 
3889
                }
 
3890
                
 
3891
                /// <summary>
 
3892
                /// Parses a C# code file.
 
3893
                /// </summary>
 
3894
                /// <param name="program">The source code to parse.</param>
 
3895
                /// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
 
3896
                /// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
 
3897
                ///  </param>
 
3898
                /// <returns>Returns the syntax tree.</returns>
 
3899
                public SyntaxTree Parse(ITextSource program, string fileName = "")
 
3900
                {
 
3901
                        return Parse(program, fileName, initialLocation.Line, initialLocation.Column);
 
3902
                }
 
3903
                
 
3904
                SyntaxTree Parse(ITextSource program, string fileName, int initialLine, int initialColumn)
 
3905
                {
 
3906
                        lock (parseLock) {
 
3907
                                errorReportPrinter = new ErrorReportPrinter ("");
 
3908
                                var ctx = new CompilerContext (compilerSettings.ToMono(), errorReportPrinter);
 
3909
                                ctx.Settings.TabSize = 1;
 
3910
                                var reader = new SeekableStreamReader (program);
 
3911
                                var file = new SourceFile (fileName, fileName, 0);
 
3912
                                Location.Initialize (new List<SourceFile> (new [] { file }));
 
3913
                                var module = new ModuleContainer (ctx);
 
3914
                                var session = new ParserSession ();
 
3915
                                session.LocationsBag = new LocationsBag ();
 
3916
                                var report = new Report (ctx, errorReportPrinter);
 
3917
                                var parser = Driver.Parse (reader, file, module, session, report, initialLine - 1, initialColumn - 1);
 
3918
                                var top = new CompilerCompilationUnit () {
 
3919
                                        ModuleCompiled = module,
 
3920
                                        LocationsBag = session.LocationsBag,
 
3921
                                        SpecialsBag = parser.Lexer.sbag,
 
3922
                                        Conditionals = parser.Lexer.SourceFile.Conditionals
 
3923
                                };
 
3924
                                var unit = Parse (top, fileName);
 
3925
                                unit.Errors.AddRange (errorReportPrinter.Errors);
 
3926
                                CompilerCallableEntryPoint.Reset ();
 
3927
                                return unit;
 
3928
                        }
 
3929
                }
 
3930
 
 
3931
                public IEnumerable<EntityDeclaration> ParseTypeMembers (string code)
 
3932
                {
 
3933
                        return ParseTypeMembers(code, initialLocation.Line, initialLocation.Column);
 
3934
                }
 
3935
                
 
3936
                IEnumerable<EntityDeclaration> ParseTypeMembers (string code, int initialLine, int initialColumn)
 
3937
                {
 
3938
                        const string prefix = "unsafe partial class MyClass { ";
 
3939
                        var syntaxTree = Parse (new StringTextSource (prefix + code + "}"), "parsed.cs", initialLine, initialColumn - prefix.Length);
 
3940
                        if (syntaxTree == null)
 
3941
                                return Enumerable.Empty<EntityDeclaration> ();
 
3942
                        var td = syntaxTree.FirstChild as TypeDeclaration;
 
3943
                        if (td != null) {
 
3944
                                var members = td.Members.ToArray();
 
3945
                                // detach members from parent
 
3946
                                foreach (var m in members)
 
3947
                                        m.Remove();
 
3948
                                return members;
 
3949
                        }
 
3950
                        return Enumerable.Empty<EntityDeclaration> ();
 
3951
                }
 
3952
                
 
3953
                public IEnumerable<Statement> ParseStatements (string code)
 
3954
                {
 
3955
                        return ParseStatements(code, initialLocation.Line, initialLocation.Column);
 
3956
                }
 
3957
                
 
3958
                IEnumerable<Statement> ParseStatements (string code, int initialLine, int initialColumn)
 
3959
                {
 
3960
                        // the dummy method is async so that 'await' expressions are parsed as expected
 
3961
                        const string prefix = "async void M() { ";
 
3962
                        var members = ParseTypeMembers (prefix + code + "}", initialLine, initialColumn - prefix.Length);
 
3963
                        var method = members.FirstOrDefault () as MethodDeclaration;
 
3964
                        if (method != null && method.Body != null) {
 
3965
                                var statements = method.Body.Statements.ToArray();
 
3966
                                // detach statements from parent
 
3967
                                foreach (var st in statements)
 
3968
                                        st.Remove();
 
3969
                                return statements;
 
3970
                        }
 
3971
                        return Enumerable.Empty<Statement> ();
 
3972
                }
 
3973
                
 
3974
                public AstType ParseTypeReference (string code)
 
3975
                {
 
3976
                        var members = ParseTypeMembers (code + " a;");
 
3977
                        var field = members.FirstOrDefault () as FieldDeclaration;
 
3978
                        if (field != null) {
 
3979
                                AstType type = field.ReturnType;
 
3980
                                type.Remove();
 
3981
                                return type;
 
3982
                        }
 
3983
                        return AstType.Null;
 
3984
                }
 
3985
                
 
3986
                public Expression ParseExpression (string code)
 
3987
                {
 
3988
                        const string prefix = "tmp = ";
 
3989
                        var statements = ParseStatements (prefix + code + ";", initialLocation.Line, initialLocation.Column - prefix.Length);
 
3990
                        var es = statements.FirstOrDefault () as ExpressionStatement;
 
3991
                        if (es != null) {
 
3992
                                AssignmentExpression ae = es.Expression as AssignmentExpression;
 
3993
                                if (ae != null) {
 
3994
                                        Expression expr = ae.Right;
 
3995
                                        expr.Remove();
 
3996
                                        return expr;
 
3997
                                }
 
3998
                        }
 
3999
                        return Expression.Null;
 
4000
                }
 
4001
                
 
4002
                /*
 
4003
                /// <summary>
 
4004
                /// Parses a file snippet; guessing what the code snippet represents (whole file, type members, block, type reference, expression).
 
4005
                /// </summary>
 
4006
                public AstNode ParseSnippet (string code)
 
4007
                {
 
4008
                        // TODO: add support for parsing a part of a file
 
4009
                        throw new NotImplementedException ();
 
4010
                }
 
4011
                */
 
4012
                
 
4013
                public DocumentationReference ParseDocumentationReference (string cref)
 
4014
                {
 
4015
                        // see Mono.CSharp.DocumentationBuilder.HandleXrefCommon
 
4016
                        if (cref == null)
 
4017
                                throw new ArgumentNullException ("cref");
 
4018
                        
 
4019
                        // Additional symbols for < and > are allowed for easier XML typing
 
4020
                        cref = cref.Replace ('{', '<').Replace ('}', '>');
 
4021
                        
 
4022
                        lock (parseLock) {
 
4023
                                errorReportPrinter = new ErrorReportPrinter("");
 
4024
                                var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter);
 
4025
                                ctx.Settings.TabSize = 1;
 
4026
                                var reader = new SeekableStreamReader(new StringTextSource (cref));
 
4027
                                var file = new SourceFile("", "", 0);
 
4028
                                Location.Initialize(new List<SourceFile> (new [] { file }));
 
4029
                                var module = new ModuleContainer(ctx);
 
4030
                                module.DocumentationBuilder = new DocumentationBuilder(module);
 
4031
                                var source_file = new CompilationSourceFile (module);
 
4032
                                var report = new Report (ctx, errorReportPrinter);
 
4033
                                ParserSession session = new ParserSession ();
 
4034
                                session.LocationsBag = new LocationsBag ();
 
4035
                                var parser = new Mono.CSharp.CSharpParser (reader, source_file, report, session);
 
4036
                                parser.Lexer.Line += initialLocation.Line - 1;
 
4037
                                parser.Lexer.Column += initialLocation.Column - 1;
 
4038
                                parser.Lexer.putback_char = Tokenizer.DocumentationXref;
 
4039
                                parser.Lexer.parsing_generic_declaration_doc = true;
 
4040
                                parser.parse ();
 
4041
                                if (report.Errors > 0) {
 
4042
//                                      Report.Warning (1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'",
 
4043
//                                                      mc.GetSignatureForError (), cref);
 
4044
                                }
 
4045
                                
 
4046
                                ConversionVisitor conversionVisitor = new ConversionVisitor (false, session.LocationsBag);
 
4047
                                DocumentationReference docRef = conversionVisitor.ConvertXmlDoc(module.DocumentationBuilder);
 
4048
                                CompilerCallableEntryPoint.Reset();
 
4049
                                return docRef;
 
4050
                        }
 
4051
                }
 
4052
        }
 
4053
}