5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
28
using System.Collections.Generic;
31
using ICSharpCode.NRefactory.Editor;
33
using ICSharpCode.NRefactory.TypeSystem;
35
namespace ICSharpCode.NRefactory.CSharp
37
public class CSharpParser
39
class ConversionVisitor : StructuralVisitor
41
CompilationUnit unit = new CompilationUnit ();
42
internal bool convertTypeSystemMode;
44
public CompilationUnit Unit {
53
public LocationsBag LocationsBag {
58
public ConversionVisitor (bool convertTypeSystemMode, LocationsBag locationsBag)
60
this.convertTypeSystemMode = convertTypeSystemMode;
61
this.LocationsBag = locationsBag;
64
public static TextLocation Convert (Mono.CSharp.Location loc)
66
return new TextLocation (loc.Row, loc.Column);
69
public override void Visit(ModuleContainer mc)
72
foreach (var container in mc.Containers) {
73
var nspace = container as NamespaceContainer;
75
container.Accept(this);
78
NamespaceDeclaration nDecl = null;
79
var loc = LocationsBag.GetLocations(nspace);
81
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
82
nDecl = new NamespaceDeclaration ();
84
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
86
ConvertNamespaceName(nspace.RealMemberName, nDecl);
87
if (loc != null && loc.Count > 1) {
88
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
90
AddToNamespace(nDecl);
91
namespaceStack.Push(nDecl);
94
if (nspace.Usings != null) {
95
foreach (var us in nspace.Usings) {
102
AddAttributeSection(Unit, mc);
105
if (nspace.Containers != null) {
106
foreach (var subContainer in nspace.Containers) {
107
subContainer.Accept(this);
111
AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
112
if (loc != null && loc.Count > 2)
113
nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
114
if (loc != null && loc.Count > 3)
115
nDecl.AddChild (new CSharpTokenNode (Convert (loc [3])), Roles.Semicolon);
117
namespaceStack.Pop ();
119
AddAttributeSection (unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
122
AddAttributeSection (unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
126
Stack<NamespaceDeclaration> namespaceStack = new Stack<NamespaceDeclaration> ();
128
void AddTypeArguments (ATypeNameExpression texpr, AstType result)
130
if (texpr.TypeArguments == null || texpr.TypeArguments.Args == null)
132
var loc = LocationsBag.GetLocations (texpr.TypeArguments);
133
if (loc != null && loc.Count >= 2)
134
result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 2])), Roles.LChevron);
136
foreach (var arg in texpr.TypeArguments.Args) {
137
result.AddChild (ConvertToType (arg), Roles.TypeArgument);
138
if (loc != null && i < loc.Count - 2)
139
result.AddChild (new CSharpTokenNode (Convert (loc [i++])), Roles.Comma);
141
if (loc != null && loc.Count >= 2)
142
result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 1])), Roles.RChevron);
145
AstType ConvertToType (TypeParameter spec)
148
result = new SimpleType () { IdentifierToken = Identifier.Create (spec.Name, Convert (spec.Location)) };
152
AstType ConvertToType (MemberName memberName)
155
if (memberName.Left != null) {
156
result = new MemberType ();
157
result.AddChild (ConvertToType (memberName.Left), MemberType.TargetRole);
158
var loc = LocationsBag.GetLocations (memberName.Left);
160
result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Dot);
161
result.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
163
result = new SimpleType () { IdentifierToken = Identifier.Create (memberName.Name, Convert (memberName.Location)) };
165
if (memberName.TypeParameters != null) {
166
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
167
if (chevronLocs != null)
168
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
169
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
170
var param = memberName.TypeParameters [i];
171
result.AddChild (new SimpleType (Identifier.Create (param.Name, Convert (param.Location))), Roles.TypeArgument);
172
if (chevronLocs != null && i < chevronLocs.Count - 2)
173
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
175
if (chevronLocs != null)
176
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
181
AstType ConvertToType (Mono.CSharp.Expression typeName)
183
if (typeName == null) // may happen in typeof(Generic<,,,,>)
184
return new SimpleType ();
186
if (typeName is TypeExpression) {
187
var typeExpr = (Mono.CSharp.TypeExpression)typeName;
188
return new PrimitiveType (typeExpr.GetSignatureForError (), Convert (typeExpr.Location));
191
if (typeName is Mono.CSharp.QualifiedAliasMember) {
192
var qam = (Mono.CSharp.QualifiedAliasMember)typeName;
193
var memberType = new MemberType ();
194
memberType.Target = new SimpleType (qam.alias, Convert (qam.Location));
195
memberType.IsDoubleColon = true;
196
memberType.MemberName = qam.Name;
200
if (typeName is MemberAccess) {
201
MemberAccess ma = (MemberAccess)typeName;
203
var memberType = new MemberType ();
204
memberType.AddChild (ConvertToType (ma.LeftExpression), MemberType.TargetRole);
205
memberType.AddChild (new CSharpTokenNode (Convert (ma.DotLocation)), Roles.Dot);
207
memberType.MemberNameToken = Identifier.Create (ma.Name, Convert (ma.Location));
209
AddTypeArguments (ma, memberType);
213
if (typeName is SimpleName) {
214
var sn = (SimpleName)typeName;
215
var result = new SimpleType (sn.Name, Convert (sn.Location));
216
AddTypeArguments (sn, result);
220
if (typeName is ComposedCast) {
221
var cc = (ComposedCast)typeName;
222
var baseType = ConvertToType (cc.Left);
223
var result = new ComposedType () { BaseType = baseType };
224
var ccSpec = cc.Spec;
225
while (ccSpec != null) {
226
if (ccSpec.IsNullable) {
227
result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), ComposedType.NullableRole);
228
} else if (ccSpec.IsPointer) {
229
result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), ComposedType.PointerRole);
231
var location = LocationsBag.GetLocations (ccSpec);
232
var spec = new ArraySpecifier () { Dimensions = ccSpec.Dimension };
233
spec.AddChild (new CSharpTokenNode (Convert (ccSpec.Location)), Roles.LBracket);
234
if (location != null)
235
spec.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RBracket);
237
result.ArraySpecifiers.Add (spec);
239
ccSpec = ccSpec.Next;
244
if (typeName is SpecialContraintExpr) {
245
var sce = (SpecialContraintExpr)typeName;
246
switch (sce.Constraint) {
247
case SpecialConstraint.Class:
248
return new PrimitiveType ("class", Convert (sce.Location));
249
case SpecialConstraint.Struct:
250
return new PrimitiveType ("struct", Convert (sce.Location));
251
case SpecialConstraint.Constructor:
252
return new PrimitiveType ("new", Convert (sce.Location));
255
return new SimpleType ("unknown");
258
IEnumerable<Attribute> GetAttributes (IEnumerable<Mono.CSharp.Attribute> optAttributes)
260
if (optAttributes == null)
262
foreach (var attr in optAttributes) {
263
Attribute result = new Attribute ();
264
result.Type = ConvertToType (attr.TypeNameExpression);
265
var loc = LocationsBag.GetLocations (attr);
266
result.HasArgumentList = loc != null;
269
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.LPar);
271
if (attr.PositionalArguments != null) {
272
foreach (var arg in attr.PositionalArguments) {
273
var na = arg as NamedArgument;
275
var newArg = new NamedArgumentExpression ();
276
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
278
var argLoc = LocationsBag.GetLocations (na);
280
newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0])), Roles.Colon);
282
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
283
result.AddChild (newArg, Roles.Argument);
285
if (arg.Expr != null)
286
result.AddChild ((Expression)arg.Expr.Accept (this), Roles.Argument);
288
if (loc != null && pos + 1 < loc.Count)
289
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
292
if (attr.NamedArguments != null) {
293
foreach (NamedArgument na in attr.NamedArguments) {
294
var newArg = new NamedExpression ();
295
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
297
var argLoc = LocationsBag.GetLocations (na);
299
newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0])), Roles.Assign);
301
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
302
result.AddChild (newArg, Roles.Argument);
303
if (loc != null && pos + 1 < loc.Count)
304
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
307
if (loc != null && pos < loc.Count)
308
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.RPar);
314
AttributeSection ConvertAttributeSection (IEnumerable<Mono.CSharp.Attribute> optAttributes)
316
if (optAttributes == null)
318
AttributeSection result = new AttributeSection ();
319
var loc = LocationsBag.GetLocations (optAttributes);
322
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.LBracket);
324
var first = optAttributes.FirstOrDefault ();
325
string target = first != null ? first.ExplicitTarget : null;
327
if (!string.IsNullOrEmpty (target)) {
328
if (loc != null && pos < loc.Count - 1) {
329
result.AddChild (Identifier.Create (target, Convert (loc [pos++])), Roles.Identifier);
331
result.AddChild (Identifier.Create (target), Roles.Identifier);
333
if (loc != null && pos < loc.Count)
334
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Colon);
337
foreach (var attr in GetAttributes (optAttributes)) {
338
result.AddChild (attr, Roles.Attribute);
341
if (loc != null && pos < loc.Count - 1 && !loc [pos].Equals (loc [pos + 1]))
342
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.Comma);
343
if (loc != null && pos < loc.Count)
344
result.AddChild (new CSharpTokenNode (Convert (loc [pos++])), Roles.RBracket);
348
public override void Visit(NamespaceContainer nspace)
350
NamespaceDeclaration nDecl = null;
351
var loc = LocationsBag.GetLocations(nspace);
353
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
354
nDecl = new NamespaceDeclaration ();
356
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0])), Roles.NamespaceKeyword);
358
ConvertNamespaceName(nspace.RealMemberName, nDecl);
359
if (loc != null && loc.Count > 1) {
360
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1])), Roles.LBrace);
362
AddToNamespace(nDecl);
363
namespaceStack.Push(nDecl);
366
if (nspace.Usings != null) {
367
foreach (var us in nspace.Usings) {
372
if (nspace.Containers != null) {
373
foreach (var container in nspace.Containers) {
374
container.Accept(this);
379
AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
380
if (loc != null && loc.Count > 2)
381
nDecl.AddChild (new CSharpTokenNode (Convert (loc [2])), Roles.RBrace);
382
if (loc != null && loc.Count > 3)
383
nDecl.AddChild (new CSharpTokenNode (Convert (loc [3])), Roles.Semicolon);
385
namespaceStack.Pop ();
388
// public override void Visit (UsingsBag.Namespace nspace)
392
// VisitNamespaceUsings (nspace);
393
// VisitNamespaceBody (nspace);
397
void ConvertNamespaceName (MemberName memberName, NamespaceDeclaration namespaceDecl)
399
AstNode insertPos = null;
400
while (memberName != null) {
401
Identifier newIdent = Identifier.Create (memberName.Name, Convert (memberName.Location));
402
namespaceDecl.InsertChildBefore (insertPos, newIdent, Roles.Identifier);
403
insertPos = newIdent;
405
if (!memberName.DotLocation.IsNull) {
406
var dotToken = new CSharpTokenNode (Convert (memberName.DotLocation));
407
namespaceDecl.InsertChildBefore (insertPos, dotToken, Roles.Dot);
408
insertPos = dotToken;
411
memberName = memberName.Left;
415
public override void Visit (UsingNamespace un)
417
var ud = new UsingDeclaration ();
418
var loc = LocationsBag.GetLocations (un);
419
ud.AddChild (new CSharpTokenNode (Convert (un.Location)), UsingDeclaration.UsingKeywordRole);
420
if (un.NamespaceExpression != null)
421
ud.AddChild (ConvertToType (un.NamespaceExpression), UsingDeclaration.ImportRole);
423
ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Semicolon);
427
public override void Visit (UsingAliasNamespace uan)
429
var ud = new UsingAliasDeclaration ();
430
var loc = LocationsBag.GetLocations (uan);
432
ud.AddChild (new CSharpTokenNode (Convert (uan.Location)), UsingAliasDeclaration.UsingKeywordRole);
433
ud.AddChild (Identifier.Create (uan.Alias.Value, Convert (uan.Alias.Location)), UsingAliasDeclaration.AliasRole);
435
ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
436
if (uan.NamespaceExpression != null)
437
ud.AddChild (ConvertToType (uan.NamespaceExpression), UsingAliasDeclaration.ImportRole);
438
if (loc != null && loc.Count > 1)
439
ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon);
443
public override void Visit (UsingExternAlias uea)
445
var ud = new ExternAliasDeclaration ();
446
var loc = LocationsBag.GetLocations (uea);
447
ud.AddChild (new CSharpTokenNode (Convert (uea.Location)), Roles.ExternKeyword);
449
ud.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.AliasKeyword);
450
ud.AddChild (Identifier.Create (uea.Alias.Value, Convert (uea.Alias.Location)), Roles.Identifier);
451
if (loc != null && loc.Count > 1)
452
ud.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Semicolon);
456
AstType ConvertImport (MemberName memberName)
458
if (memberName.Left != null) {
460
var t = new MemberType ();
461
// t.IsDoubleColon = memberName.IsDoubleColon;
462
t.AddChild (ConvertImport (memberName.Left), MemberType.TargetRole);
464
if (!memberName.DotLocation.IsNull)
465
t.AddChild (new CSharpTokenNode (Convert (memberName.DotLocation)), Roles.Dot);
467
t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
468
AddTypeArguments (t, memberName);
471
SimpleType t = new SimpleType ();
472
t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
473
AddTypeArguments (t, memberName);
478
public override void Visit (MemberCore member)
480
Console.WriteLine ("Unknown member:");
481
Console.WriteLine (member.GetType () + "-> Member {0}", member.GetSignatureForError ());
484
Stack<TypeDeclaration> typeStack = new Stack<TypeDeclaration> ();
486
public override void Visit(Class c)
488
var newType = new TypeDeclaration ();
489
newType.ClassType = ClassType.Class;
490
AddAttributeSection(newType, c);
492
var location = LocationsBag.GetMemberLocation(c);
493
AddModifiers(newType, location);
495
if (location != null && location.Count > 0)
496
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.ClassKeyword);
498
newType.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
499
AddTypeParameters (newType, c.MemberName);
501
if (c.TypeBaseExpressions != null) {
502
if (location != null && curLoc < location.Count)
503
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
505
var commaLocations = LocationsBag.GetLocations (c.TypeBaseExpressions);
507
foreach (var baseTypes in c.TypeBaseExpressions) {
508
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
509
if (commaLocations != null && i < commaLocations.Count) {
510
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
516
AddConstraints (newType, c.CurrentTypeParameters);
517
if (location != null && curLoc < location.Count)
518
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
519
typeStack.Push (newType);
521
AddAttributeSection (newType, c.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
523
if (location != null && curLoc < location.Count) {
524
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
526
if (location != null && curLoc < location.Count)
527
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
530
// parser error, set end node to max value.
531
newType.AddChild (new ErrorNode (), Roles.Error);
537
public override void Visit(Struct s)
539
var newType = new TypeDeclaration();
540
newType.ClassType = ClassType.Struct;
541
AddAttributeSection(newType, s);
542
var location = LocationsBag.GetMemberLocation(s);
543
AddModifiers(newType, location);
545
if (location != null && location.Count > 0)
546
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.StructKeyword);
547
newType.AddChild (Identifier.Create (s.MemberName.Name, Convert (s.MemberName.Location)), Roles.Identifier);
548
AddTypeParameters (newType, s.MemberName);
550
if (s.TypeBaseExpressions != null) {
551
if (location != null && curLoc < location.Count)
552
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
553
var commaLocations = LocationsBag.GetLocations (s.TypeBaseExpressions);
555
foreach (var baseTypes in s.TypeBaseExpressions) {
556
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
557
if (commaLocations != null && i < commaLocations.Count) {
558
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
564
AddConstraints (newType, s.CurrentTypeParameters);
565
if (location != null && curLoc < location.Count)
566
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
567
typeStack.Push (newType);
569
if (location != null && location.Count > 2) {
570
if (location != null && curLoc < location.Count)
571
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
572
if (location != null && curLoc < location.Count)
573
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
575
// parser error, set end node to max value.
576
newType.AddChild (new ErrorNode (), Roles.Error);
582
public override void Visit(Interface i)
584
var newType = new TypeDeclaration();
585
newType.ClassType = ClassType.Interface;
586
AddAttributeSection(newType, i);
587
var location = LocationsBag.GetMemberLocation(i);
588
AddModifiers(newType, location);
590
if (location != null && location.Count > 0)
591
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.InterfaceKeyword);
592
newType.AddChild (Identifier.Create (i.MemberName.Name, Convert (i.MemberName.Location)), Roles.Identifier);
593
AddTypeParameters (newType, i.MemberName);
595
if (i.TypeBaseExpressions != null) {
596
if (location != null && curLoc < location.Count)
597
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Colon);
598
var commaLocations = LocationsBag.GetLocations (i.TypeBaseExpressions);
600
foreach (var baseTypes in i.TypeBaseExpressions) {
601
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
602
if (commaLocations != null && j < commaLocations.Count) {
603
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [j])), Roles.Comma);
609
AddConstraints (newType, i.CurrentTypeParameters);
610
if (location != null && curLoc < location.Count)
611
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.LBrace);
612
typeStack.Push (newType);
614
if (location != null && location.Count > 2) {
615
if (location != null && curLoc < location.Count)
616
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
617
if (location != null && curLoc < location.Count)
618
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
620
// parser error, set end node to max value.
621
newType.AddChild (new ErrorNode (), Roles.Error);
627
public override void Visit(Mono.CSharp.Delegate d)
629
DelegateDeclaration newDelegate = new DelegateDeclaration ();
630
var location = LocationsBag.GetMemberLocation(d);
631
AddAttributeSection(newDelegate, d);
632
AddModifiers(newDelegate, location);
633
if (location != null && location.Count > 0) {
634
newDelegate.AddChild(new CSharpTokenNode (Convert(location [0])), Roles.DelegateKeyword);
636
if (d.ReturnType != null)
637
newDelegate.AddChild (ConvertToType (d.ReturnType), Roles.Type);
638
newDelegate.AddChild (Identifier.Create (d.MemberName.Name, Convert (d.MemberName.Location)), Roles.Identifier);
639
AddTypeParameters (newDelegate, d.MemberName);
641
if (location != null && location.Count > 1)
642
newDelegate.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
643
AddParameter (newDelegate, d.Parameters);
645
if (location != null && location.Count > 2) {
646
newDelegate.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
648
AddConstraints (newDelegate, d.CurrentTypeParameters);
649
if (location != null && location.Count > 3) {
650
newDelegate.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.Semicolon);
652
AddType (newDelegate);
655
void AddType (EntityDeclaration child)
657
if (typeStack.Count > 0) {
658
typeStack.Peek ().AddChild (child, Roles.TypeMemberRole);
660
AddToNamespace (child);
664
void AddToNamespace (AstNode child)
666
if (namespaceStack.Count > 0) {
667
namespaceStack.Peek ().AddChild (child, NamespaceDeclaration.MemberRole);
669
unit.AddChild (child, CompilationUnit.MemberRole);
673
public override void Visit(Mono.CSharp.Enum e)
675
var newType = new TypeDeclaration();
676
newType.ClassType = ClassType.Enum;
677
AddAttributeSection(newType, e);
678
var location = LocationsBag.GetMemberLocation(e);
680
AddModifiers(newType, location);
682
if (location != null && location.Count > 0)
683
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.EnumKeyword);
684
newType.AddChild(Identifier.Create(e.MemberName.Name, Convert(e.MemberName.Location)), Roles.Identifier);
686
if (e.BaseTypeExpression != null) {
687
if (location != null && curLoc < location.Count)
688
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.Colon);
689
newType.AddChild(ConvertToType(e.BaseTypeExpression), Roles.BaseType);
692
if (location != null && curLoc < location.Count)
693
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.LBrace);
694
typeStack.Push(newType);
696
foreach (var m in e.Members) {
697
EnumMember member = m as EnumMember;
698
if (member == null) {
699
Console.WriteLine("WARNING - ENUM MEMBER: " + m);
703
if (location != null && curLoc < location.Count - 1) //last one is closing brace
704
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++])), Roles.Comma);
707
if (location != null && location.Count > 2) {
708
if (location != null && curLoc < location.Count)
709
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.RBrace);
710
if (location != null && curLoc < location.Count)
711
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++])), Roles.Semicolon);
713
// parser error, set end node to max value.
714
newType.AddChild (new ErrorNode (), Roles.Error);
720
public override void Visit (EnumMember em)
722
EnumMemberDeclaration newField = new EnumMemberDeclaration ();
723
AddAttributeSection (newField, em);
724
newField.AddChild (Identifier.Create (em.Name, Convert (em.Location)), Roles.Identifier);
725
if (em.Initializer != null) {
726
newField.AddChild (new CSharpTokenNode (Convert (em.Initializer.Location)), Roles.Assign);
727
newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole);
729
//Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation);
731
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
738
public override void Visit (FixedField f)
740
var location = LocationsBag.GetMemberLocation (f);
743
var newField = new FixedFieldDeclaration ();
744
AddAttributeSection (newField, f);
745
AddModifiers (newField, location);
746
if (location != null && location.Count > 0)
747
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), FixedFieldDeclaration.FixedKeywordRole);
749
if (f.TypeExpression != null)
750
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
752
var variable = new FixedVariableInitializer ();
753
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
754
if (f.Initializer != null && !f.Initializer.IsNull) {
755
var bracketLocations = LocationsBag.GetLocations (f.Initializer);
756
if (bracketLocations != null && bracketLocations.Count > 1)
757
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.LBracket);
759
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
760
if (bracketLocations != null && bracketLocations.Count > 1)
761
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.RBracket);
763
newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
765
if (f.Declarators != null) {
766
foreach (var decl in f.Declarators) {
767
var declLoc = LocationsBag.GetLocations (decl);
769
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
771
variable = new FixedVariableInitializer ();
772
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
773
if (!decl.Initializer.IsNull) {
774
var bracketLocations = LocationsBag.GetLocations (f.Initializer);
775
if (bracketLocations != null && bracketLocations.Count > 1)
776
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.LBracket);
777
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
778
if (bracketLocations != null && bracketLocations.Count > 1)
779
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0])), Roles.RBracket);
781
newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
784
if (location != null && location.Count > locationIdx)
785
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx])), Roles.Semicolon);
786
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
790
public override void Visit(Field f)
792
var location = LocationsBag.GetMemberLocation(f);
794
FieldDeclaration newField = new FieldDeclaration ();
795
AddAttributeSection (newField, f);
796
AddModifiers (newField, location);
797
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
799
VariableInitializer variable = new VariableInitializer ();
800
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
802
if (f.Initializer != null) {
803
if (location != null)
804
variable.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), Roles.Assign);
805
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
807
newField.AddChild (variable, Roles.Variable);
808
if (f.Declarators != null) {
809
foreach (var decl in f.Declarators) {
810
var declLoc = LocationsBag.GetLocations (decl);
812
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
814
variable = new VariableInitializer ();
815
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
816
if (decl.Initializer != null) {
818
variable.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
819
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
821
newField.AddChild (variable, Roles.Variable);
824
if (location != null &&location.Count > locationIdx)
825
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++])), Roles.Semicolon);
827
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
830
public override void Visit (Const f)
832
var location = LocationsBag.GetMemberLocation (f);
834
FieldDeclaration newField = new FieldDeclaration ();
835
AddAttributeSection (newField, f);
836
AddModifiers (newField, location);
837
if (location != null)
838
newField.AddChild (new CSharpModifierToken (Convert (location [0]), Modifiers.Const), EntityDeclaration.ModifierRole);
839
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
841
VariableInitializer variable = new VariableInitializer ();
842
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
844
if (f.Initializer != null) {
845
variable.AddChild (new CSharpTokenNode (Convert (f.Initializer.Location)), Roles.Assign);
846
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
848
newField.AddChild (variable, Roles.Variable);
849
if (f.Declarators != null) {
850
foreach (var decl in f.Declarators) {
851
var declLoc = LocationsBag.GetLocations (decl);
853
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
855
variable = new VariableInitializer ();
856
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
857
if (decl.Initializer != null) {
858
variable.AddChild (new CSharpTokenNode (Convert (decl.Initializer.Location)), Roles.Assign);
859
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
861
newField.AddChild (variable, Roles.Variable);
864
if (location != null)
865
newField.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
867
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
872
public override void Visit (Operator o)
874
OperatorDeclaration newOperator = new OperatorDeclaration ();
875
newOperator.OperatorType = (OperatorType)o.OperatorType;
877
var location = LocationsBag.GetMemberLocation (o);
878
AddAttributeSection (newOperator, o);
879
AddModifiers (newOperator, location);
882
if (o.OperatorType == Operator.OpType.Implicit) {
883
if (location != null && location.Count > 0) {
884
newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.ImplicitRole);
885
if (location.Count > 1)
886
newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.OperatorKeywordRole);
888
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
889
} else if (o.OperatorType == Operator.OpType.Explicit) {
890
if (location != null && location.Count > 0) {
891
newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.ExplicitRole);
892
if (location.Count > 1)
893
newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.OperatorKeywordRole);
895
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
897
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
899
if (location != null && location.Count > 0)
900
newOperator.AddChild (new CSharpTokenNode (Convert (location [0])), OperatorDeclaration.OperatorKeywordRole);
902
if (location != null && location.Count > 1)
903
newOperator.AddChild (new CSharpTokenNode (Convert (location [1])), OperatorDeclaration.GetRole (newOperator.OperatorType));
905
if (location != null && location.Count > 2)
906
newOperator.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LPar);
907
AddParameter (newOperator, o.ParameterInfo);
908
if (location != null && location.Count > 3)
909
newOperator.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RPar);
911
if (o.Block != null) {
912
newOperator.AddChild ((BlockStatement)o.Block.Accept (this), Roles.Body);
914
if (location != null && location.Count >= 5)
915
newOperator.AddChild (new CSharpTokenNode (Convert (location [4])), Roles.Semicolon);
917
typeStack.Peek ().AddChild (newOperator, Roles.TypeMemberRole);
920
public void AddAttributeSection (AstNode parent, Attributable a)
922
if (a == null || a.OptAttributes == null)
924
AddAttributeSection (parent, a.OptAttributes);
927
public void AddAttributeSection (AstNode parent, Attributes attrs, Role role)
931
foreach (var attr in attrs.Sections) {
932
parent.AddChild (ConvertAttributeSection (attr), EntityDeclaration.AttributeRole);
936
public void AddAttributeSection (AstNode parent, Attributes attrs)
938
AddAttributeSection (parent, attrs, EntityDeclaration.AttributeRole);
941
public override void Visit (Indexer indexer)
943
IndexerDeclaration newIndexer = new IndexerDeclaration ();
944
AddAttributeSection (newIndexer, indexer);
945
var location = LocationsBag.GetMemberLocation (indexer);
946
AddModifiers (newIndexer, location);
947
newIndexer.AddChild (ConvertToType (indexer.TypeExpression), Roles.Type);
948
AddExplicitInterface (newIndexer, indexer.MemberName);
949
var name = indexer.MemberName;
950
newIndexer.AddChild (Identifier.Create ("this", Convert (name.Location)), Roles.Identifier);
952
if (location != null && location.Count > 0)
953
newIndexer.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBracket);
954
AddParameter (newIndexer, indexer.ParameterInfo);
955
if (location != null && location.Count > 1)
956
newIndexer.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBracket);
958
if (location != null && location.Count > 2)
959
newIndexer.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LBrace);
960
if (indexer.Get != null) {
961
Accessor getAccessor = new Accessor ();
962
var getLocation = LocationsBag.GetMemberLocation (indexer.Get);
963
AddAttributeSection (getAccessor, indexer.Get);
964
AddModifiers (getAccessor, getLocation);
965
if (getLocation != null)
966
getAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Get.Location)), PropertyDeclaration.GetKeywordRole);
967
if (indexer.Get.Block != null) {
968
getAccessor.AddChild ((BlockStatement)indexer.Get.Block.Accept (this), Roles.Body);
970
if (getLocation != null && getLocation.Count > 0)
971
newIndexer.AddChild (new CSharpTokenNode (Convert (getLocation [0])), Roles.Semicolon);
973
newIndexer.AddChild (getAccessor, PropertyDeclaration.GetterRole);
976
if (indexer.Set != null) {
977
Accessor setAccessor = new Accessor ();
978
var setLocation = LocationsBag.GetMemberLocation (indexer.Set);
979
AddAttributeSection (setAccessor, indexer.Set);
980
AddModifiers (setAccessor, setLocation);
981
if (setLocation != null)
982
setAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Set.Location)), PropertyDeclaration.SetKeywordRole);
984
if (indexer.Set.Block != null) {
985
setAccessor.AddChild ((BlockStatement)indexer.Set.Block.Accept (this), Roles.Body);
987
if (setLocation != null && setLocation.Count > 0)
988
newIndexer.AddChild (new CSharpTokenNode (Convert (setLocation [0])), Roles.Semicolon);
990
newIndexer.AddChild (setAccessor, PropertyDeclaration.SetterRole);
993
if (location != null) {
994
if (location.Count > 3)
995
newIndexer.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RBrace);
997
// parser error, set end node to max value.
998
newIndexer.AddChild (new ErrorNode (), Roles.Error);
1000
typeStack.Peek ().AddChild (newIndexer, Roles.TypeMemberRole);
1003
public override void Visit (Method m)
1005
MethodDeclaration newMethod = new MethodDeclaration ();
1006
AddAttributeSection (newMethod, m);
1007
var location = LocationsBag.GetMemberLocation (m);
1008
AddModifiers (newMethod, location);
1009
newMethod.AddChild (ConvertToType (m.TypeExpression), Roles.Type);
1010
AddExplicitInterface (newMethod, m.MethodName);
1011
newMethod.AddChild (Identifier.Create (m.MethodName.Name, Convert (m.Location)), Roles.Identifier);
1013
AddTypeParameters (newMethod, m.MemberName);
1015
if (location != null && location.Count > 0)
1016
newMethod.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1017
AddParameter (newMethod, m.ParameterInfo);
1019
if (location != null && location.Count > 1)
1020
newMethod.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1022
AddConstraints (newMethod, m.CurrentTypeParameters);
1024
if (m.Block != null) {
1025
var bodyBlock = (BlockStatement)m.Block.Accept (this);
1026
// if (m.Block is ToplevelBlock) {
1027
// newMethod.AddChild (bodyBlock.FirstChild.NextSibling, Roles.Body);
1029
newMethod.AddChild (bodyBlock, Roles.Body);
1031
} else if (location != null) {
1032
if (location.Count < 3) {
1033
// parser error, set end node to max value.
1034
newMethod.AddChild (new ErrorNode (), Roles.Error);
1036
newMethod.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.Semicolon);
1039
typeStack.Peek ().AddChild (newMethod, Roles.TypeMemberRole);
1042
static Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> modifierTable = new Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> ();
1043
static string[] keywordTable;
1045
static ConversionVisitor ()
1047
modifierTable [Mono.CSharp.Modifiers.NEW] = ICSharpCode.NRefactory.CSharp.Modifiers.New;
1048
modifierTable [Mono.CSharp.Modifiers.PUBLIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
1049
modifierTable [Mono.CSharp.Modifiers.PROTECTED] = ICSharpCode.NRefactory.CSharp.Modifiers.Protected;
1050
modifierTable [Mono.CSharp.Modifiers.PRIVATE] = ICSharpCode.NRefactory.CSharp.Modifiers.Private;
1051
modifierTable [Mono.CSharp.Modifiers.INTERNAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Internal;
1052
modifierTable [Mono.CSharp.Modifiers.ABSTRACT] = ICSharpCode.NRefactory.CSharp.Modifiers.Abstract;
1053
modifierTable [Mono.CSharp.Modifiers.VIRTUAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Virtual;
1054
modifierTable [Mono.CSharp.Modifiers.SEALED] = ICSharpCode.NRefactory.CSharp.Modifiers.Sealed;
1055
modifierTable [Mono.CSharp.Modifiers.STATIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Static;
1056
modifierTable [Mono.CSharp.Modifiers.OVERRIDE] = ICSharpCode.NRefactory.CSharp.Modifiers.Override;
1057
modifierTable [Mono.CSharp.Modifiers.READONLY] = ICSharpCode.NRefactory.CSharp.Modifiers.Readonly;
1058
modifierTable [Mono.CSharp.Modifiers.PARTIAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Partial;
1059
modifierTable [Mono.CSharp.Modifiers.EXTERN] = ICSharpCode.NRefactory.CSharp.Modifiers.Extern;
1060
modifierTable [Mono.CSharp.Modifiers.VOLATILE] = ICSharpCode.NRefactory.CSharp.Modifiers.Volatile;
1061
modifierTable [Mono.CSharp.Modifiers.UNSAFE] = ICSharpCode.NRefactory.CSharp.Modifiers.Unsafe;
1062
modifierTable [Mono.CSharp.Modifiers.ASYNC] = ICSharpCode.NRefactory.CSharp.Modifiers.Async;
1064
keywordTable = new string[255];
1065
for (int i = 0; i< keywordTable.Length; i++)
1066
keywordTable [i] = "unknown";
1068
keywordTable [(int)BuiltinTypeSpec.Type.Other] = "void";
1069
keywordTable [(int)BuiltinTypeSpec.Type.String] = "string";
1070
keywordTable [(int)BuiltinTypeSpec.Type.Int] = "int";
1071
keywordTable [(int)BuiltinTypeSpec.Type.Object] = "object";
1072
keywordTable [(int)BuiltinTypeSpec.Type.Float] = "float";
1073
keywordTable [(int)BuiltinTypeSpec.Type.Double] = "double";
1074
keywordTable [(int)BuiltinTypeSpec.Type.Long] = "long";
1075
keywordTable [(int)BuiltinTypeSpec.Type.Byte] = "byte";
1076
keywordTable [(int)BuiltinTypeSpec.Type.UInt] = "uint";
1077
keywordTable [(int)BuiltinTypeSpec.Type.ULong] = "ulong";
1078
keywordTable [(int)BuiltinTypeSpec.Type.Short] = "short";
1079
keywordTable [(int)BuiltinTypeSpec.Type.UShort] = "ushort";
1080
keywordTable [(int)BuiltinTypeSpec.Type.SByte] = "sbyte";
1081
keywordTable [(int)BuiltinTypeSpec.Type.Decimal] = "decimal";
1082
keywordTable [(int)BuiltinTypeSpec.Type.Char] = "char";
1083
keywordTable [(int)BuiltinTypeSpec.Type.Bool] = "bool";
1086
void AddModifiers (EntityDeclaration parent, LocationsBag.MemberLocations location)
1088
if (location == null || location.Modifiers == null)
1090
foreach (var modifier in location.Modifiers) {
1091
ICSharpCode.NRefactory.CSharp.Modifiers mod;
1092
if (!modifierTable.TryGetValue (modifier.Item1, out mod)) {
1093
Console.WriteLine ("modifier " + modifier.Item1 + " can't be converted,");
1096
parent.AddChild (new CSharpModifierToken (Convert (modifier.Item2), mod), EntityDeclaration.ModifierRole);
1100
public override void Visit (Property p)
1102
PropertyDeclaration newProperty = new PropertyDeclaration ();
1103
AddAttributeSection (newProperty, p);
1104
var location = LocationsBag.GetMemberLocation (p);
1105
AddModifiers (newProperty, location);
1106
newProperty.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
1107
AddExplicitInterface (newProperty, p.MemberName);
1108
newProperty.AddChild (Identifier.Create (p.MemberName.Name, Convert (p.Location)), Roles.Identifier);
1110
if (location != null && location.Count > 0)
1111
newProperty.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBrace);
1113
Accessor getAccessor = null;
1114
if (p.Get != null) {
1115
getAccessor = new Accessor ();
1116
AddAttributeSection (getAccessor, p.Get);
1117
var getLocation = LocationsBag.GetMemberLocation (p.Get);
1118
AddModifiers (getAccessor, getLocation);
1119
getAccessor.AddChild (new CSharpTokenNode (Convert (p.Get.Location)), PropertyDeclaration.GetKeywordRole);
1121
if (p.Get.Block != null) {
1122
getAccessor.AddChild ((BlockStatement)p.Get.Block.Accept (this), Roles.Body);
1124
if (getLocation != null && getLocation.Count > 0)
1125
getAccessor.AddChild (new CSharpTokenNode (Convert (getLocation [0])), Roles.Semicolon);
1129
Accessor setAccessor = null;
1130
if (p.Set != null) {
1131
setAccessor = new Accessor ();
1132
AddAttributeSection (setAccessor, p.Set);
1133
var setLocation = LocationsBag.GetMemberLocation (p.Set);
1134
AddModifiers (setAccessor, setLocation);
1135
setAccessor.AddChild (new CSharpTokenNode (Convert (p.Set.Location)), PropertyDeclaration.SetKeywordRole);
1137
if (p.Set.Block != null) {
1138
setAccessor.AddChild ((BlockStatement)p.Set.Block.Accept (this), Roles.Body);
1140
if (setLocation != null && setLocation.Count > 0)
1141
setAccessor.AddChild (new CSharpTokenNode (Convert (setLocation [0])), Roles.Semicolon);
1144
if (getAccessor != null && setAccessor != null) {
1145
if (getAccessor.StartLocation < setAccessor.StartLocation) {
1146
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1147
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1149
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1150
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1153
if (getAccessor != null)
1154
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1155
if (setAccessor != null)
1156
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1159
if (location != null && location.Count > 1) {
1160
newProperty.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBrace);
1162
// parser error, set end node to max value.
1163
newProperty.AddChild (new ErrorNode (), Roles.Error);
1166
typeStack.Peek ().AddChild (newProperty, Roles.TypeMemberRole);
1169
public override void Visit (Constructor c)
1171
ConstructorDeclaration newConstructor = new ConstructorDeclaration ();
1172
AddAttributeSection (newConstructor, c);
1173
var location = LocationsBag.GetMemberLocation (c);
1174
AddModifiers (newConstructor, location);
1175
newConstructor.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
1176
if (location != null && location.Count > 0)
1177
newConstructor.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1179
AddParameter (newConstructor, c.ParameterInfo);
1180
if (location != null && location.Count > 1)
1181
newConstructor.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1183
if (c.Initializer != null) {
1184
var initializer = new ConstructorInitializer ();
1185
initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This;
1186
var initializerLocation = LocationsBag.GetLocations (c.Initializer);
1188
if (initializerLocation != null)
1189
newConstructor.AddChild (new CSharpTokenNode (Convert (initializerLocation [0])), Roles.Colon);
1191
if (initializerLocation != null && initializerLocation.Count > 1) {
1192
// this and base has the same length
1193
initializer.AddChild (new CSharpTokenNode (Convert (c.Initializer.Location)), initializer.ConstructorInitializerType == ConstructorInitializerType.This ? ConstructorInitializer.ThisKeywordRole : ConstructorInitializer.BaseKeywordRole);
1194
initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [1])), Roles.LPar);
1195
AddArguments (initializer, LocationsBag.GetLocations (c.Initializer.Arguments), c.Initializer.Arguments);
1196
initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [2])), Roles.RPar);
1197
newConstructor.AddChild (initializer, ConstructorDeclaration.InitializerRole);
1201
if (c.Block != null)
1202
newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), Roles.Body);
1203
typeStack.Peek ().AddChild (newConstructor, Roles.TypeMemberRole);
1206
public override void Visit (Destructor d)
1208
DestructorDeclaration newDestructor = new DestructorDeclaration ();
1209
AddAttributeSection (newDestructor, d);
1210
var location = LocationsBag.GetMemberLocation (d);
1211
AddModifiers (newDestructor, location);
1212
if (location != null && location.Count > 0)
1213
newDestructor.AddChild (new CSharpTokenNode (Convert (location [0])), DestructorDeclaration.TildeRole);
1214
newDestructor.AddChild (Identifier.Create (d.Identifier, Convert (d.MemberName.Location)), Roles.Identifier);
1216
if (location != null && location.Count > 1) {
1217
newDestructor.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
1219
if (location.Count > 2)
1220
newDestructor.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
1223
if (d.Block != null)
1224
newDestructor.AddChild ((BlockStatement)d.Block.Accept (this), Roles.Body);
1226
typeStack.Peek ().AddChild (newDestructor, Roles.TypeMemberRole);
1229
public override void Visit (EventField e)
1231
EventDeclaration newEvent = new EventDeclaration ();
1232
AddAttributeSection (newEvent, e);
1233
var location = LocationsBag.GetMemberLocation (e);
1234
AddModifiers (newEvent, location);
1236
if (location != null && location.Count > 0)
1237
newEvent.AddChild (new CSharpTokenNode (Convert (location [0])), EventDeclaration.EventKeywordRole);
1238
newEvent.AddChild (ConvertToType (e.TypeExpression), Roles.Type);
1240
VariableInitializer variable = new VariableInitializer ();
1241
variable.AddChild (Identifier.Create (e.MemberName.Name, Convert (e.MemberName.Location)), Roles.Identifier);
1243
if (e.Initializer != null) {
1244
if (location != null && location.Count > 0)
1245
variable.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
1246
variable.AddChild ((Expression)e.Initializer.Accept (this), Roles.Expression);
1248
newEvent.AddChild (variable, Roles.Variable);
1249
if (e.Declarators != null) {
1250
foreach (var decl in e.Declarators) {
1251
var declLoc = LocationsBag.GetLocations (decl);
1252
if (declLoc != null)
1253
newEvent.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
1255
variable = new VariableInitializer ();
1256
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
1258
if (decl.Initializer != null) {
1259
if (declLoc != null)
1260
variable.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
1261
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1263
newEvent.AddChild (variable, Roles.Variable);
1267
if (location != null && location.Count > 1)
1268
newEvent.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1270
typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1273
void AddExplicitInterface (AstNode parent, MemberName memberName)
1275
if (memberName == null || memberName.ExplicitInterface == null)
1278
parent.AddChild (ConvertToType (memberName.ExplicitInterface), EntityDeclaration.PrivateImplementationTypeRole);
1279
var privateImplTypeLoc = LocationsBag.GetLocations (memberName.ExplicitInterface);
1280
if (privateImplTypeLoc != null)
1281
parent.AddChild (new CSharpTokenNode (Convert (privateImplTypeLoc [0])), Roles.Dot);
1284
public override void Visit (EventProperty ep)
1286
CustomEventDeclaration newEvent = new CustomEventDeclaration ();
1287
AddAttributeSection (newEvent, ep);
1288
var location = LocationsBag.GetMemberLocation (ep);
1289
AddModifiers (newEvent, location);
1291
if (location != null && location.Count > 0)
1292
newEvent.AddChild (new CSharpTokenNode (Convert (location [0])), CustomEventDeclaration.EventKeywordRole);
1293
newEvent.AddChild (ConvertToType (ep.TypeExpression), Roles.Type);
1295
AddExplicitInterface (newEvent, ep.MemberName);
1297
newEvent.AddChild (Identifier.Create (ep.MemberName.Name, Convert (ep.Location)), Roles.Identifier);
1299
if (location != null && location.Count >= 2)
1300
newEvent.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LBrace);
1302
if (ep.Add != null) {
1303
Accessor addAccessor = new Accessor ();
1304
AddAttributeSection (addAccessor, ep.Add);
1305
var addLocation = LocationsBag.GetMemberLocation (ep.Add);
1306
AddModifiers (addAccessor, addLocation);
1307
addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location)), CustomEventDeclaration.AddKeywordRole);
1308
if (ep.Add.Block != null)
1309
addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), Roles.Body);
1310
newEvent.AddChild (addAccessor, CustomEventDeclaration.AddAccessorRole);
1313
if (ep.Remove != null) {
1314
Accessor removeAccessor = new Accessor ();
1315
AddAttributeSection (removeAccessor, ep.Remove);
1316
var removeLocation = LocationsBag.GetMemberLocation (ep.Remove);
1317
AddModifiers (removeAccessor, removeLocation);
1318
removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location)), CustomEventDeclaration.RemoveKeywordRole);
1320
if (ep.Remove.Block != null)
1321
removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), Roles.Body);
1322
newEvent.AddChild (removeAccessor, CustomEventDeclaration.RemoveAccessorRole);
1324
if (location != null && location.Count >= 3) {
1325
newEvent.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RBrace);
1327
// parser error, set end node to max value.
1328
newEvent.AddChild (new ErrorNode (), Roles.Error);
1331
typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1337
public override object Visit (Mono.CSharp.Statement stmt)
1339
Console.WriteLine ("unknown statement:" + stmt);
1343
public override object Visit (BlockVariableDeclaration blockVariableDeclaration)
1345
var result = new VariableDeclarationStatement ();
1346
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1348
var varInit = new VariableInitializer ();
1349
var location = LocationsBag.GetLocations (blockVariableDeclaration);
1350
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1351
if (blockVariableDeclaration.Initializer != null) {
1352
if (location != null && location.Count > 0)
1353
varInit.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
1354
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1357
result.AddChild (varInit, Roles.Variable);
1359
if (blockVariableDeclaration.Declarators != null) {
1360
foreach (var decl in blockVariableDeclaration.Declarators) {
1361
var loc = LocationsBag.GetLocations (decl);
1362
var init = new VariableInitializer ();
1363
if (loc != null && loc.Count > 0)
1364
result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1365
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1366
if (decl.Initializer != null) {
1367
if (loc != null && loc.Count > 1)
1368
init.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Assign);
1369
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1372
result.AddChild (init, Roles.Variable);
1375
if (location != null && (blockVariableDeclaration.Initializer == null || location.Count > 1))
1376
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.Semicolon);
1380
public override object Visit (BlockConstantDeclaration blockVariableDeclaration)
1382
var result = new VariableDeclarationStatement ();
1384
var location = LocationsBag.GetLocations (blockVariableDeclaration);
1385
if (location != null && location.Count > 0)
1386
result.AddChild (new CSharpModifierToken (Convert (location [0]), ICSharpCode.NRefactory.CSharp.Modifiers.Const), VariableDeclarationStatement.ModifierRole);
1388
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1390
var varInit = new VariableInitializer ();
1391
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1392
if (blockVariableDeclaration.Initializer != null) {
1393
if (location != null && location.Count > 1)
1394
varInit.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Assign);
1395
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1398
result.AddChild (varInit, Roles.Variable);
1400
if (blockVariableDeclaration.Declarators != null) {
1401
foreach (var decl in blockVariableDeclaration.Declarators) {
1402
var loc = LocationsBag.GetLocations (decl);
1403
var init = new VariableInitializer ();
1404
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1405
if (decl.Initializer != null) {
1407
init.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
1408
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1409
if (loc != null && loc.Count > 1)
1410
result.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Comma);
1412
if (loc != null && loc.Count > 0)
1413
result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1415
result.AddChild (init, Roles.Variable);
1418
if (location != null) {
1419
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.Semicolon);
1421
// parser error, set end node to max value.
1422
result.AddChild (new ErrorNode (), Roles.Error);
1427
public override object Visit (Mono.CSharp.EmptyStatement emptyStatement)
1429
var result = new EmptyStatement ();
1430
result.Location = Convert (emptyStatement.loc);
1434
public override object Visit (Mono.CSharp.EmptyExpression emptyExpression)
1436
return new ICSharpCode.NRefactory.CSharp.EmptyExpression (Convert (emptyExpression.Location));
1439
public override object Visit (Mono.CSharp.ErrorExpression emptyExpression)
1441
return new ICSharpCode.NRefactory.CSharp.ErrorExpression (Convert (emptyExpression.Location));
1444
public override object Visit (EmptyExpressionStatement emptyExpressionStatement)
1446
return new EmptyExpression (Convert (emptyExpressionStatement.Location));
1449
public override object Visit (If ifStatement)
1451
var result = new IfElseStatement ();
1453
var location = LocationsBag.GetLocations (ifStatement);
1455
result.AddChild (new CSharpTokenNode (Convert (ifStatement.loc)), IfElseStatement.IfKeywordRole);
1456
if (location != null)
1457
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1458
if (ifStatement.Expr != null)
1459
result.AddChild ((Expression)ifStatement.Expr.Accept (this), Roles.Condition);
1460
if (location != null && location.Count > 1)
1461
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1463
if (ifStatement.TrueStatement != null)
1464
result.AddChild ((Statement)ifStatement.TrueStatement.Accept (this), IfElseStatement.TrueRole);
1466
if (ifStatement.FalseStatement != null) {
1467
if (location != null && location.Count > 2)
1468
result.AddChild (new CSharpTokenNode (Convert (location [2])), IfElseStatement.ElseKeywordRole);
1469
result.AddChild ((Statement)ifStatement.FalseStatement.Accept (this), IfElseStatement.FalseRole);
1475
public override object Visit (Do doStatement)
1477
var result = new DoWhileStatement ();
1478
var location = LocationsBag.GetLocations (doStatement);
1479
result.AddChild (new CSharpTokenNode (Convert (doStatement.loc)), DoWhileStatement.DoKeywordRole);
1480
if (doStatement.EmbeddedStatement != null)
1481
result.AddChild ((Statement)doStatement.EmbeddedStatement.Accept (this), Roles.EmbeddedStatement);
1482
if (location != null)
1483
result.AddChild (new CSharpTokenNode (Convert (location [0])), DoWhileStatement.WhileKeywordRole);
1484
if (location != null && location.Count > 1)
1485
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LPar);
1486
if (doStatement.expr != null)
1487
result.AddChild ((Expression)doStatement.expr.Accept (this), Roles.Condition);
1488
if (location != null && location.Count > 2) {
1489
result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
1490
if (location.Count > 3)
1491
result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.Semicolon);
1497
public override object Visit (While whileStatement)
1499
var result = new WhileStatement ();
1500
var location = LocationsBag.GetLocations (whileStatement);
1501
result.AddChild (new CSharpTokenNode (Convert (whileStatement.loc)), WhileStatement.WhileKeywordRole);
1503
if (location != null)
1504
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1505
if (whileStatement.expr != null)
1506
result.AddChild ((Expression)whileStatement.expr.Accept (this), Roles.Condition);
1507
if (location != null && location.Count > 1)
1508
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1509
if (whileStatement.Statement != null)
1510
result.AddChild ((Statement)whileStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1514
void AddStatementOrList (ForStatement forStatement, Mono.CSharp.Statement init, Role<Statement> role)
1518
if (init is StatementList) {
1519
foreach (var stmt in ((StatementList)init).Statements) {
1520
forStatement.AddChild ((Statement)stmt.Accept (this), role);
1522
} else if (init is Mono.CSharp.EmptyStatement) {
1525
forStatement.AddChild ((Statement)init.Accept (this), role);
1529
public override object Visit (For forStatement)
1531
var result = new ForStatement ();
1533
var location = LocationsBag.GetLocations (forStatement);
1535
result.AddChild (new CSharpTokenNode (Convert (forStatement.loc)), ForStatement.ForKeywordRole);
1536
if (location != null)
1537
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1539
AddStatementOrList (result, forStatement.Initializer, ForStatement.InitializerRole);
1541
if (location != null && location.Count > 1)
1542
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1543
if (forStatement.Condition != null)
1544
result.AddChild ((Expression)forStatement.Condition.Accept (this), Roles.Condition);
1545
if (location != null && location.Count >= 3)
1546
result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.Semicolon);
1548
AddStatementOrList (result, forStatement.Iterator, ForStatement.IteratorRole);
1550
if (location != null && location.Count >= 4)
1551
result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RPar);
1553
if (forStatement.Statement != null)
1554
result.AddChild ((Statement)forStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1559
public override object Visit (StatementExpression statementExpression)
1561
var result = new ExpressionStatement ();
1562
var expr = statementExpression.Expr.Accept (this) as Expression;
1564
result.AddChild ((Expression)expr, Roles.Expression);
1565
var location = LocationsBag.GetLocations (statementExpression);
1566
if (location != null)
1567
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1571
public override object Visit (StatementErrorExpression statementErrorExpression)
1573
var result = new ExpressionStatement ();
1574
var expr = statementErrorExpression.Expr.Accept (this) as Expression;
1576
result.AddChild ((Expression)expr, Roles.Expression);
1580
public override object Visit (InvalidStatementExpression statementExpression)
1582
var result = new ExpressionStatement ();
1583
if (statementExpression.Expression == null)
1585
var expr = statementExpression.Expression.Accept (this) as Expression;
1587
result.AddChild (expr, Roles.Expression);
1588
var location = LocationsBag.GetLocations (statementExpression);
1589
if (location != null)
1590
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1594
public override object Visit (Return returnStatement)
1596
var result = new ReturnStatement ();
1598
result.AddChild (new CSharpTokenNode (Convert (returnStatement.loc)), ReturnStatement.ReturnKeywordRole);
1599
if (returnStatement.Expr != null)
1600
result.AddChild ((Expression)returnStatement.Expr.Accept (this), Roles.Expression);
1602
var location = LocationsBag.GetLocations (returnStatement);
1603
if (location != null)
1604
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1609
public override object Visit (Goto gotoStatement)
1611
var result = new GotoStatement ();
1612
var location = LocationsBag.GetLocations (gotoStatement);
1613
result.AddChild (new CSharpTokenNode (Convert (gotoStatement.loc)), GotoStatement.GotoKeywordRole);
1614
var loc = location != null ? Convert (location [0]) : TextLocation.Empty;
1615
result.AddChild (Identifier.Create (gotoStatement.Target, loc), Roles.Identifier);
1616
if (location != null && location.Count > 1)
1617
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1622
public override object Visit (LabeledStatement labeledStatement)
1624
var result = new LabelStatement ();
1625
result.AddChild (Identifier.Create (labeledStatement.Name, Convert (labeledStatement.loc)), Roles.Identifier);
1626
var location = LocationsBag.GetLocations (labeledStatement);
1627
if (location != null)
1628
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Colon);
1632
public override object Visit (GotoDefault gotoDefault)
1634
var result = new GotoDefaultStatement ();
1635
result.AddChild (new CSharpTokenNode (Convert (gotoDefault.loc)), GotoDefaultStatement.GotoKeywordRole);
1636
var location = LocationsBag.GetLocations (gotoDefault);
1637
if (location != null) {
1638
result.AddChild (new CSharpTokenNode (Convert (location [0])), GotoDefaultStatement.DefaultKeywordRole);
1639
if (location.Count > 1)
1640
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1646
public override object Visit (GotoCase gotoCase)
1648
var result = new GotoCaseStatement ();
1649
result.AddChild (new CSharpTokenNode (Convert (gotoCase.loc)), GotoCaseStatement.GotoKeywordRole);
1651
var location = LocationsBag.GetLocations (gotoCase);
1652
if (location != null)
1653
result.AddChild (new CSharpTokenNode (Convert (location [0])), GotoCaseStatement.CaseKeywordRole);
1654
if (gotoCase.Expr != null)
1655
result.AddChild ((Expression)gotoCase.Expr.Accept (this), Roles.Expression);
1656
if (location != null && location.Count > 1)
1657
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
1661
public override object Visit (Throw throwStatement)
1663
var result = new ThrowStatement ();
1664
var location = LocationsBag.GetLocations (throwStatement);
1666
result.AddChild (new CSharpTokenNode (Convert (throwStatement.loc)), ThrowStatement.ThrowKeywordRole);
1667
if (throwStatement.Expr != null)
1668
result.AddChild ((Expression)throwStatement.Expr.Accept (this), Roles.Expression);
1669
if (location != null)
1670
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1674
public override object Visit (Break breakStatement)
1676
var result = new BreakStatement ();
1677
var location = LocationsBag.GetLocations (breakStatement);
1679
result.AddChild (new CSharpTokenNode (Convert (breakStatement.loc)), BreakStatement.BreakKeywordRole);
1680
if (location != null)
1681
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1685
public override object Visit (Continue continueStatement)
1687
var result = new ContinueStatement ();
1688
var location = LocationsBag.GetLocations (continueStatement);
1689
result.AddChild (new CSharpTokenNode (Convert (continueStatement.loc)), ContinueStatement.ContinueKeywordRole);
1690
if (location != null)
1691
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Semicolon);
1695
public static bool IsLower (Location left, Location right)
1697
return left.Row < right.Row || left.Row == right.Row && left.Column < right.Column;
1700
public UsingStatement CreateUsingStatement (Block blockStatement)
1702
var usingResult = new UsingStatement ();
1703
Mono.CSharp.Statement cur = blockStatement.Statements [0];
1705
Using u = (Using)cur;
1706
usingResult.AddChild (new CSharpTokenNode (Convert (u.loc)), UsingStatement.UsingKeywordRole);
1707
usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation)), Roles.LPar);
1708
if (u.Variables != null) {
1709
var initializer = new VariableInitializer () {
1710
NameToken = Identifier.Create (u.Variables.Variable.Name, Convert (u.Variables.Variable.Location)),
1713
var loc = LocationsBag.GetLocations (u.Variables);
1715
initializer.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Assign);
1716
if (u.Variables.Initializer != null)
1717
initializer.Initializer = u.Variables.Initializer.Accept (this) as Expression;
1720
var varDec = new VariableDeclarationStatement () {
1721
Type = ConvertToType (u.Variables.TypeExpression),
1722
Variables = { initializer }
1725
if (u.Variables.Declarators != null) {
1726
foreach (var decl in u.Variables.Declarators) {
1727
var declLoc = LocationsBag.GetLocations (decl);
1728
var init = new VariableInitializer ();
1729
if (declLoc != null && declLoc.Count > 0)
1730
varDec.AddChild (new CSharpTokenNode (Convert (declLoc [0])), Roles.Comma);
1731
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1732
if (decl.Initializer != null) {
1733
if (declLoc != null && declLoc.Count > 1)
1734
init.AddChild (new CSharpTokenNode (Convert (declLoc [1])), Roles.Assign);
1735
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1737
varDec.AddChild (init, Roles.Variable);
1740
usingResult.AddChild (varDec, UsingStatement.ResourceAcquisitionRole);
1743
usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation)), Roles.RPar);
1745
usingResult.AddChild ((Statement)cur.Accept (this), Roles.EmbeddedStatement);
1750
void AddBlockChildren (BlockStatement result, Block blockStatement, ref int curLocal)
1752
if (convertTypeSystemMode) {
1755
foreach (Mono.CSharp.Statement stmt in blockStatement.Statements) {
1758
/* if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) {
1759
result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), Roles.Statement);
1762
if (stmt is Block && !(stmt is ToplevelBlock || stmt is ExplicitBlock)) {
1763
AddBlockChildren (result, (Block)stmt, ref curLocal);
1765
result.AddChild ((Statement)stmt.Accept (this), BlockStatement.StatementRole);
1770
public override object Visit (Block blockStatement)
1772
if (blockStatement.IsCompilerGenerated && blockStatement.Statements.Any ()) {
1773
if (blockStatement.Statements.First () is Using)
1774
return CreateUsingStatement (blockStatement);
1775
return blockStatement.Statements.Last ().Accept (this);
1777
var result = new BlockStatement ();
1778
result.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation)), Roles.LBrace);
1780
AddBlockChildren (result, blockStatement, ref curLocal);
1782
result.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation)), Roles.RBrace);
1786
public override object Visit (Switch switchStatement)
1788
var result = new SwitchStatement ();
1790
var location = LocationsBag.GetLocations (switchStatement);
1791
result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc)), SwitchStatement.SwitchKeywordRole);
1792
if (location != null)
1793
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1794
if (switchStatement.Expr != null)
1795
result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression);
1796
if (location != null && location.Count > 1)
1797
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1798
if (location != null && location.Count > 2)
1799
result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.LBrace);
1800
if (switchStatement.Sections != null) {
1801
foreach (var section in switchStatement.Sections) {
1802
var newSection = new SwitchSection ();
1803
if (section.Labels != null) {
1804
foreach (var caseLabel in section.Labels) {
1805
var newLabel = new CaseLabel ();
1806
if (caseLabel.Label != null) {
1807
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location)), CaseLabel.CaseKeywordRole);
1808
if (caseLabel.Label != null)
1809
newLabel.AddChild ((Expression)caseLabel.Label.Accept (this), Roles.Expression);
1810
var colonLocation = LocationsBag.GetLocations (caseLabel);
1811
if (colonLocation != null)
1812
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0])), Roles.Colon);
1814
newLabel.AddChild (new CSharpTokenNode (Convert (caseLabel.Location)), CaseLabel.DefaultKeywordRole);
1815
newLabel.AddChild (new CSharpTokenNode (new TextLocation (caseLabel.Location.Row, caseLabel.Location.Column + "default".Length)), Roles.Colon);
1817
newSection.AddChild (newLabel, SwitchSection.CaseLabelRole);
1821
var blockStatement = section.Block;
1822
var bodyBlock = new BlockStatement ();
1824
AddBlockChildren (bodyBlock, blockStatement, ref curLocal);
1825
foreach (var statement in bodyBlock.Statements) {
1826
statement.Remove ();
1827
newSection.AddChild (statement, Roles.EmbeddedStatement);
1830
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
1834
if (location != null && location.Count > 3) {
1835
result.AddChild (new CSharpTokenNode (Convert (location [3])), Roles.RBrace);
1837
// parser error, set end node to max value.
1838
result.AddChild (new ErrorNode (), Roles.Error);
1844
public override object Visit (Lock lockStatement)
1846
var result = new LockStatement ();
1847
var location = LocationsBag.GetLocations (lockStatement);
1848
result.AddChild (new CSharpTokenNode (Convert (lockStatement.loc)), LockStatement.LockKeywordRole);
1850
if (location != null)
1851
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1852
if (lockStatement.Expr != null)
1853
result.AddChild ((Expression)lockStatement.Expr.Accept (this), Roles.Expression);
1855
if (location != null && location.Count > 1)
1856
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1857
if (lockStatement.Statement != null)
1858
result.AddChild ((Statement)lockStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1863
public override object Visit (Unchecked uncheckedStatement)
1865
var result = new UncheckedStatement ();
1866
result.AddChild (new CSharpTokenNode (Convert (uncheckedStatement.loc)), UncheckedStatement.UncheckedKeywordRole);
1867
if (uncheckedStatement.Block != null)
1868
result.AddChild ((BlockStatement)uncheckedStatement.Block.Accept (this), Roles.Body);
1872
public override object Visit (Checked checkedStatement)
1874
var result = new CheckedStatement ();
1875
result.AddChild (new CSharpTokenNode (Convert (checkedStatement.loc)), CheckedStatement.CheckedKeywordRole);
1876
if (checkedStatement.Block != null)
1877
result.AddChild ((BlockStatement)checkedStatement.Block.Accept (this), Roles.Body);
1881
public override object Visit (Unsafe unsafeStatement)
1883
var result = new UnsafeStatement ();
1884
result.AddChild (new CSharpTokenNode (Convert (unsafeStatement.loc)), UnsafeStatement.UnsafeKeywordRole);
1885
if (unsafeStatement.Block != null)
1886
result.AddChild ((BlockStatement)unsafeStatement.Block.Accept (this), Roles.Body);
1890
public override object Visit (Fixed fixedStatement)
1892
var result = new FixedStatement ();
1893
var location = LocationsBag.GetLocations (fixedStatement);
1895
result.AddChild (new CSharpTokenNode (Convert (fixedStatement.loc)), FixedStatement.FixedKeywordRole);
1896
if (location != null)
1897
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1899
if (fixedStatement.Variables != null) {
1900
var blockVariableDeclaration = fixedStatement.Variables;
1901
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1902
var varInit = new VariableInitializer ();
1903
var initLocation = LocationsBag.GetLocations (blockVariableDeclaration);
1904
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1905
if (blockVariableDeclaration.Initializer != null) {
1906
if (initLocation != null)
1907
varInit.AddChild (new CSharpTokenNode (Convert (initLocation [0])), Roles.Assign);
1908
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1911
result.AddChild (varInit, Roles.Variable);
1913
if (blockVariableDeclaration.Declarators != null) {
1914
foreach (var decl in blockVariableDeclaration.Declarators) {
1915
var loc = LocationsBag.GetLocations (decl);
1916
var init = new VariableInitializer ();
1917
if (loc != null && loc.Count > 0)
1918
result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Comma);
1919
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1920
if (decl.Initializer != null) {
1921
if (loc != null && loc.Count > 1)
1922
init.AddChild (new CSharpTokenNode (Convert (loc [1])), Roles.Assign);
1923
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1926
result.AddChild (init, Roles.Variable);
1931
if (location != null && location.Count > 1)
1932
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1933
if (fixedStatement.Statement != null)
1934
result.AddChild ((Statement)fixedStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1938
public override object Visit (TryFinally tryFinallyStatement)
1940
TryCatchStatement result;
1941
var location = LocationsBag.GetLocations (tryFinallyStatement);
1943
if (tryFinallyStatement.Stmt is TryCatch) {
1944
result = (TryCatchStatement)tryFinallyStatement.Stmt.Accept (this);
1946
result = new TryCatchStatement ();
1947
result.AddChild (new CSharpTokenNode (Convert (tryFinallyStatement.loc)), TryCatchStatement.TryKeywordRole);
1948
if (tryFinallyStatement.Stmt != null)
1949
result.AddChild ((BlockStatement)tryFinallyStatement.Stmt.Accept (this), TryCatchStatement.TryBlockRole);
1951
if (location != null)
1952
result.AddChild (new CSharpTokenNode (Convert (location [0])), TryCatchStatement.FinallyKeywordRole);
1953
if (tryFinallyStatement.Fini != null)
1954
result.AddChild ((BlockStatement)tryFinallyStatement.Fini.Accept (this), TryCatchStatement.FinallyBlockRole);
1959
CatchClause ConvertCatch (Catch ctch)
1961
CatchClause result = new CatchClause ();
1962
var location = LocationsBag.GetLocations (ctch);
1963
result.AddChild (new CSharpTokenNode (Convert (ctch.loc)), CatchClause.CatchKeywordRole);
1964
if (ctch.TypeExpression != null) {
1965
if (location != null)
1966
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
1968
if (ctch.TypeExpression != null)
1969
result.AddChild (ConvertToType (ctch.TypeExpression), Roles.Type);
1970
if (ctch.Variable != null && !string.IsNullOrEmpty (ctch.Variable.Name))
1971
result.AddChild (Identifier.Create (ctch.Variable.Name, Convert (ctch.Variable.Location)), Roles.Identifier);
1973
if (location != null && location.Count > 1)
1974
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
1977
if (ctch.Block != null)
1978
result.AddChild ((BlockStatement)ctch.Block.Accept (this), Roles.Body);
1983
public override object Visit (TryCatch tryCatchStatement)
1985
var result = new TryCatchStatement ();
1986
result.AddChild (new CSharpTokenNode (Convert (tryCatchStatement.loc)), TryCatchStatement.TryKeywordRole);
1987
if (tryCatchStatement.Block != null)
1988
result.AddChild ((BlockStatement)tryCatchStatement.Block.Accept (this), TryCatchStatement.TryBlockRole);
1989
if (tryCatchStatement.Clauses != null) {
1990
foreach (var ctch in tryCatchStatement.Clauses) {
1991
result.AddChild (ConvertCatch (ctch), TryCatchStatement.CatchClauseRole);
1994
// if (tryCatchStatement.General != null)
1995
// result.AddChild (ConvertCatch (tryCatchStatement.General), TryCatchStatement.CatchClauseRole);
2000
public override object Visit (Using usingStatement)
2002
var result = new UsingStatement ();
2003
var location = LocationsBag.GetLocations (usingStatement);
2005
result.AddChild (new CSharpTokenNode (Convert (usingStatement.loc)), UsingStatement.UsingKeywordRole);
2006
if (location != null)
2007
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2008
if (usingStatement.Expr != null)
2009
result.AddChild ((AstNode)usingStatement.Expr.Accept (this), UsingStatement.ResourceAcquisitionRole);
2011
if (location != null && location.Count > 1)
2012
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2014
if (usingStatement.Statement != null)
2015
result.AddChild ((Statement)usingStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2019
public override object Visit (Foreach foreachStatement)
2021
var result = new ForeachStatement ();
2023
var location = LocationsBag.GetLocations (foreachStatement);
2025
result.AddChild (new CSharpTokenNode (Convert (foreachStatement.loc)), ForeachStatement.ForeachKeywordRole);
2026
if (location != null)
2027
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2029
if (foreachStatement.TypeExpression != null)
2030
result.AddChild (ConvertToType (foreachStatement.TypeExpression), Roles.Type);
2032
if (foreachStatement.Variable != null)
2033
result.AddChild (Identifier.Create (foreachStatement.Variable.Name, Convert (foreachStatement.Variable.Location)), Roles.Identifier);
2035
if (location != null && location.Count > 1)
2036
result.AddChild (new CSharpTokenNode (Convert (location [1])), ForeachStatement.InKeywordRole);
2038
if (foreachStatement.Expr != null)
2039
result.AddChild ((Expression)foreachStatement.Expr.Accept (this), Roles.Expression);
2041
if (location != null && location.Count > 2)
2042
result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RPar);
2044
if (foreachStatement.Statement != null)
2045
result.AddChild ((Statement)foreachStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2050
public override object Visit (Yield yieldStatement)
2052
var result = new YieldReturnStatement ();
2053
var location = LocationsBag.GetLocations (yieldStatement);
2055
result.AddChild (new CSharpTokenNode (Convert (yieldStatement.loc)), YieldReturnStatement.YieldKeywordRole);
2056
if (location != null)
2057
result.AddChild (new CSharpTokenNode (Convert (location [0])), YieldReturnStatement.ReturnKeywordRole);
2058
if (yieldStatement.Expr != null)
2059
result.AddChild ((Expression)yieldStatement.Expr.Accept (this), Roles.Expression);
2060
if (location != null && location.Count > 1)
2061
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
2066
public override object Visit (YieldBreak yieldBreakStatement)
2068
var result = new YieldBreakStatement ();
2069
var location = LocationsBag.GetLocations (yieldBreakStatement);
2070
result.AddChild (new CSharpTokenNode (Convert (yieldBreakStatement.loc)), YieldBreakStatement.YieldKeywordRole);
2071
if (location != null) {
2072
result.AddChild (new CSharpTokenNode (Convert (location [0])), YieldBreakStatement.BreakKeywordRole);
2073
if (location.Count > 1)
2074
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Semicolon);
2081
public override object Visit (Mono.CSharp.Expression expression)
2083
Console.WriteLine ("Visit unknown expression:" + expression);
2084
System.Console.WriteLine (Environment.StackTrace);
2088
public override object Visit (Mono.CSharp.DefaultParameterValueExpression defaultParameterValueExpression)
2090
return defaultParameterValueExpression.Child.Accept (this);
2093
public override object Visit (TypeExpression typeExpression)
2095
return new TypeReferenceExpression (new PrimitiveType (keywordTable [(int)typeExpression.Type.BuiltinType], Convert (typeExpression.Location)));
2098
public override object Visit (LocalVariableReference localVariableReference)
2100
return Identifier.Create (localVariableReference.Name, Convert (localVariableReference.Location));
2103
public override object Visit (MemberAccess memberAccess)
2106
if (memberAccess.LeftExpression is Indirection) {
2107
var ind = memberAccess.LeftExpression as Indirection;
2108
result = new PointerReferenceExpression ();
2109
result.AddChild ((Expression)ind.Expr.Accept (this), Roles.TargetExpression);
2110
result.AddChild (new CSharpTokenNode (Convert (ind.Location)), PointerReferenceExpression.ArrowRole);
2112
result = new MemberReferenceExpression ();
2113
if (memberAccess.LeftExpression != null) {
2114
var leftExpr = memberAccess.LeftExpression.Accept (this);
2115
result.AddChild ((Expression)leftExpr, Roles.TargetExpression);
2117
if (!memberAccess.DotLocation.IsNull) {
2118
result.AddChild (new CSharpTokenNode (Convert (memberAccess.DotLocation)), Roles.Dot);
2122
result.AddChild (Identifier.Create (memberAccess.Name, Convert (memberAccess.Location)), Roles.Identifier);
2124
AddTypeArguments (result, memberAccess);
2128
public override object Visit (QualifiedAliasMember qualifiedAliasMember)
2130
var result = new MemberType ();
2131
result.Target = new SimpleType (qualifiedAliasMember.alias, Convert (qualifiedAliasMember.Location));
2132
result.IsDoubleColon = true;
2133
var location = LocationsBag.GetLocations (qualifiedAliasMember);
2134
result.AddChild (Identifier.Create (qualifiedAliasMember.Name, location != null ? Convert (location [0]) : TextLocation.Empty), Roles.Identifier);
2135
return new TypeReferenceExpression () { Type = result };
2138
public override object Visit (Constant constant)
2140
if (constant.GetValue () == null)
2141
return new NullReferenceExpression (Convert (constant.Location));
2142
string literalValue;
2143
if (constant is ILiteralConstant) {
2144
literalValue = new string (((ILiteralConstant)constant).ParsedValue);
2146
literalValue = constant.GetValueAsLiteral ();
2148
object val = constant.GetValue ();
2150
literalValue = (bool)val ? "true" : "false";
2151
var result = new PrimitiveExpression (val, Convert (constant.Location), literalValue);
2155
public override object Visit (SimpleName simpleName)
2157
var result = new IdentifierExpression ();
2158
result.AddChild (Identifier.Create (simpleName.Name, Convert (simpleName.Location)), Roles.Identifier);
2159
AddTypeArguments (result, simpleName);
2163
public override object Visit (BooleanExpression booleanExpression)
2165
return booleanExpression.Expr.Accept (this);
2168
public override object Visit (Mono.CSharp.ParenthesizedExpression parenthesizedExpression)
2170
var result = new ParenthesizedExpression ();
2171
var location = LocationsBag.GetLocations (parenthesizedExpression);
2172
if (location != null)
2173
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2174
if (parenthesizedExpression.Expr != null)
2175
result.AddChild ((Expression)parenthesizedExpression.Expr.Accept (this), Roles.Expression);
2176
if (location != null && location.Count > 1)
2177
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2181
public override object Visit (Unary unaryExpression)
2183
var result = new UnaryOperatorExpression ();
2184
switch (unaryExpression.Oper) {
2185
case Unary.Operator.UnaryPlus:
2186
result.Operator = UnaryOperatorType.Plus;
2188
case Unary.Operator.UnaryNegation:
2189
result.Operator = UnaryOperatorType.Minus;
2191
case Unary.Operator.LogicalNot:
2192
result.Operator = UnaryOperatorType.Not;
2194
case Unary.Operator.OnesComplement:
2195
result.Operator = UnaryOperatorType.BitNot;
2197
case Unary.Operator.AddressOf:
2198
result.Operator = UnaryOperatorType.AddressOf;
2201
result.AddChild (new CSharpTokenNode (Convert (unaryExpression.Location)), UnaryOperatorExpression.GetOperatorRole (result.Operator));
2202
if (unaryExpression.Expr != null)
2203
result.AddChild ((Expression)unaryExpression.Expr.Accept (this), Roles.Expression);
2207
public override object Visit (UnaryMutator unaryMutatorExpression)
2209
var result = new UnaryOperatorExpression ();
2210
if (unaryMutatorExpression.Expr == null)
2212
var expression = (Expression)unaryMutatorExpression.Expr.Accept (this);
2213
switch (unaryMutatorExpression.UnaryMutatorMode) {
2214
case UnaryMutator.Mode.PostDecrement:
2215
result.Operator = UnaryOperatorType.PostDecrement;
2216
result.AddChild (expression, Roles.Expression);
2217
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.DecrementRole);
2219
case UnaryMutator.Mode.PostIncrement:
2220
result.Operator = UnaryOperatorType.PostIncrement;
2221
result.AddChild (expression, Roles.Expression);
2222
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.IncrementRole);
2225
case UnaryMutator.Mode.PreIncrement:
2226
result.Operator = UnaryOperatorType.Increment;
2227
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.IncrementRole);
2228
result.AddChild (expression, Roles.Expression);
2230
case UnaryMutator.Mode.PreDecrement:
2231
result.Operator = UnaryOperatorType.Decrement;
2232
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location)), UnaryOperatorExpression.DecrementRole);
2233
result.AddChild (expression, Roles.Expression);
2240
public override object Visit (Indirection indirectionExpression)
2242
var result = new UnaryOperatorExpression ();
2243
result.Operator = UnaryOperatorType.Dereference;
2244
result.AddChild (new CSharpTokenNode (Convert (indirectionExpression.Location)), UnaryOperatorExpression.DereferenceRole);
2245
if (indirectionExpression.Expr != null)
2246
result.AddChild ((Expression)indirectionExpression.Expr.Accept (this), Roles.Expression);
2250
public override object Visit (Is isExpression)
2252
var result = new IsExpression ();
2253
if (isExpression.Expr != null)
2254
result.AddChild ((Expression)isExpression.Expr.Accept (this), Roles.Expression);
2255
result.AddChild (new CSharpTokenNode (Convert (isExpression.Location)), IsExpression.IsKeywordRole);
2257
if (isExpression.ProbeType != null)
2258
result.AddChild (ConvertToType (isExpression.ProbeType), Roles.Type);
2262
public override object Visit (As asExpression)
2264
var result = new AsExpression ();
2265
if (asExpression.Expr != null)
2266
result.AddChild ((Expression)asExpression.Expr.Accept (this), Roles.Expression);
2267
result.AddChild (new CSharpTokenNode (Convert (asExpression.Location)), AsExpression.AsKeywordRole);
2268
if (asExpression.ProbeType != null)
2269
result.AddChild (ConvertToType (asExpression.ProbeType), Roles.Type);
2273
public override object Visit (Cast castExpression)
2275
var result = new CastExpression ();
2276
var location = LocationsBag.GetLocations (castExpression);
2278
result.AddChild (new CSharpTokenNode (Convert (castExpression.Location)), Roles.LPar);
2279
if (castExpression.TargetType != null)
2280
result.AddChild (ConvertToType (castExpression.TargetType), Roles.Type);
2281
if (location != null)
2282
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RPar);
2283
if (castExpression.Expr != null)
2284
result.AddChild ((Expression)castExpression.Expr.Accept (this), Roles.Expression);
2288
public override object Visit (ComposedCast composedCast)
2290
var result = new ComposedType ();
2291
result.AddChild (ConvertToType (composedCast.Left), Roles.Type);
2293
var spec = composedCast.Spec;
2294
while (spec != null) {
2295
if (spec.IsNullable) {
2296
result.AddChild (new CSharpTokenNode (Convert (spec.Location)), ComposedType.NullableRole);
2297
} else if (spec.IsPointer) {
2298
result.AddChild (new CSharpTokenNode (Convert (spec.Location)), ComposedType.PointerRole);
2300
var aSpec = new ArraySpecifier ();
2301
aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location)), Roles.LBracket);
2302
var location = LocationsBag.GetLocations (spec);
2303
if (location != null)
2304
aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location)), Roles.RBracket);
2305
result.AddChild (aSpec, ComposedType.ArraySpecifierRole);
2313
public override object Visit (Mono.CSharp.DefaultValueExpression defaultValueExpression)
2315
var result = new DefaultValueExpression ();
2316
result.AddChild (new CSharpTokenNode (Convert (defaultValueExpression.Location)), DefaultValueExpression.DefaultKeywordRole);
2317
var location = LocationsBag.GetLocations (defaultValueExpression);
2318
if (location != null)
2319
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2320
result.AddChild (ConvertToType (defaultValueExpression.Expr), Roles.Type);
2321
if (location != null && location.Count > 1)
2322
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2326
public override object Visit (Binary binaryExpression)
2328
var result = new BinaryOperatorExpression ();
2329
switch (binaryExpression.Oper) {
2330
case Binary.Operator.Multiply:
2331
result.Operator = BinaryOperatorType.Multiply;
2333
case Binary.Operator.Division:
2334
result.Operator = BinaryOperatorType.Divide;
2336
case Binary.Operator.Modulus:
2337
result.Operator = BinaryOperatorType.Modulus;
2339
case Binary.Operator.Addition:
2340
result.Operator = BinaryOperatorType.Add;
2342
case Binary.Operator.Subtraction:
2343
result.Operator = BinaryOperatorType.Subtract;
2345
case Binary.Operator.LeftShift:
2346
result.Operator = BinaryOperatorType.ShiftLeft;
2348
case Binary.Operator.RightShift:
2349
result.Operator = BinaryOperatorType.ShiftRight;
2351
case Binary.Operator.LessThan:
2352
result.Operator = BinaryOperatorType.LessThan;
2354
case Binary.Operator.GreaterThan:
2355
result.Operator = BinaryOperatorType.GreaterThan;
2357
case Binary.Operator.LessThanOrEqual:
2358
result.Operator = BinaryOperatorType.LessThanOrEqual;
2360
case Binary.Operator.GreaterThanOrEqual:
2361
result.Operator = BinaryOperatorType.GreaterThanOrEqual;
2363
case Binary.Operator.Equality:
2364
result.Operator = BinaryOperatorType.Equality;
2366
case Binary.Operator.Inequality:
2367
result.Operator = BinaryOperatorType.InEquality;
2369
case Binary.Operator.BitwiseAnd:
2370
result.Operator = BinaryOperatorType.BitwiseAnd;
2372
case Binary.Operator.ExclusiveOr:
2373
result.Operator = BinaryOperatorType.ExclusiveOr;
2375
case Binary.Operator.BitwiseOr:
2376
result.Operator = BinaryOperatorType.BitwiseOr;
2378
case Binary.Operator.LogicalAnd:
2379
result.Operator = BinaryOperatorType.ConditionalAnd;
2381
case Binary.Operator.LogicalOr:
2382
result.Operator = BinaryOperatorType.ConditionalOr;
2386
if (binaryExpression.Left != null)
2387
result.AddChild ((Expression)binaryExpression.Left.Accept (this), BinaryOperatorExpression.LeftRole);
2388
result.AddChild (new CSharpTokenNode (Convert (binaryExpression.Location)), BinaryOperatorExpression.GetOperatorRole (result.Operator));
2389
if (binaryExpression.Right != null)
2390
result.AddChild ((Expression)binaryExpression.Right.Accept (this), BinaryOperatorExpression.RightRole);
2394
public override object Visit (Mono.CSharp.Nullable.NullCoalescingOperator nullCoalescingOperator)
2396
var result = new BinaryOperatorExpression ();
2397
result.Operator = BinaryOperatorType.NullCoalescing;
2398
if (nullCoalescingOperator.LeftExpression != null)
2399
result.AddChild ((Expression)nullCoalescingOperator.LeftExpression.Accept (this), BinaryOperatorExpression.LeftRole);
2400
result.AddChild (new CSharpTokenNode (Convert (nullCoalescingOperator.Location)), BinaryOperatorExpression.NullCoalescingRole);
2401
if (nullCoalescingOperator.RightExpression != null)
2402
result.AddChild ((Expression)nullCoalescingOperator.RightExpression.Accept (this), BinaryOperatorExpression.RightRole);
2406
public override object Visit (Conditional conditionalExpression)
2408
var result = new ConditionalExpression ();
2410
if (conditionalExpression.Expr != null)
2411
result.AddChild ((Expression)conditionalExpression.Expr.Accept (this), Roles.Condition);
2412
var location = LocationsBag.GetLocations (conditionalExpression);
2414
result.AddChild (new CSharpTokenNode (Convert (conditionalExpression.Location)), ConditionalExpression.QuestionMarkRole);
2415
if (conditionalExpression.TrueExpr != null)
2416
result.AddChild ((Expression)conditionalExpression.TrueExpr.Accept (this), ConditionalExpression.TrueRole);
2417
if (location != null)
2418
result.AddChild (new CSharpTokenNode (Convert (location [0])), ConditionalExpression.ColonRole);
2419
if (conditionalExpression.FalseExpr != null)
2420
result.AddChild ((Expression)conditionalExpression.FalseExpr.Accept (this), ConditionalExpression.FalseRole);
2424
void AddParameter (AstNode parent, Mono.CSharp.AParametersCollection parameters)
2426
if (parameters == null)
2428
var paramLocation = LocationsBag.GetLocations (parameters);
2430
for (int i = 0; i < parameters.Count; i++) {
2431
var p = (Parameter)parameters.FixedParameters [i];
2434
var location = LocationsBag.GetLocations (p);
2435
ParameterDeclaration parameterDeclarationExpression = new ParameterDeclaration ();
2436
AddAttributeSection (parameterDeclarationExpression, p);
2437
switch (p.ModFlags) {
2438
case Parameter.Modifier.OUT:
2439
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Out;
2440
if (location != null)
2441
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.OutModifierRole);
2443
case Parameter.Modifier.REF:
2444
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Ref;
2445
if (location != null)
2446
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.RefModifierRole);
2448
case Parameter.Modifier.PARAMS:
2449
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Params;
2450
if (location != null)
2451
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.ParamsModifierRole);
2454
if (p.HasExtensionMethodModifier) {
2455
parameterDeclarationExpression.ParameterModifier = ParameterModifier.This;
2456
if (location != null) {
2457
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0])), ParameterDeclaration.ThisModifierRole);
2462
if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
2463
parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
2465
parameterDeclarationExpression.AddChild (Identifier.Create (p.Name, Convert (p.Location)), Roles.Identifier);
2466
if (p.HasDefaultValue) {
2467
if (location != null && location.Count > 1)
2468
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Assign);
2469
parameterDeclarationExpression.AddChild ((Expression)p.DefaultValue.Accept (this), Roles.Expression);
2471
parent.AddChild (parameterDeclarationExpression, Roles.Parameter);
2472
if (paramLocation != null && i < paramLocation.Count) {
2473
parent.AddChild (new CSharpTokenNode (Convert (paramLocation [i])), Roles.Comma);
2478
void AddTypeParameters (AstNode parent, MemberName memberName)
2480
if (memberName == null || memberName.TypeParameters == null)
2482
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2483
if (chevronLocs != null)
2484
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2485
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2486
if (chevronLocs != null && i > 0 && i - 1 < chevronLocs.Count)
2487
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i - 1])), Roles.Comma);
2488
var arg = memberName.TypeParameters [i];
2491
TypeParameterDeclaration tp = new TypeParameterDeclaration ();
2493
List<Location> varianceLocation;
2494
switch (arg.Variance) {
2495
case Variance.Contravariant:
2496
tp.Variance = VarianceModifier.Contravariant;
2497
varianceLocation = LocationsBag.GetLocations (arg);
2498
if (varianceLocation != null)
2499
tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0])), TypeParameterDeclaration.InVarianceKeywordRole);
2501
case Variance.Covariant:
2502
tp.Variance = VarianceModifier.Covariant;
2503
varianceLocation = LocationsBag.GetLocations (arg);
2504
if (varianceLocation != null)
2505
tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0])), TypeParameterDeclaration.OutVarianceKeywordRole);
2508
tp.Variance = VarianceModifier.Invariant;
2513
AddAttributeSection (tp, arg.OptAttributes);
2515
switch (arg.Variance) {
2516
case Variance.Covariant:
2517
tp.Variance = VarianceModifier.Covariant;
2519
case Variance.Contravariant:
2520
tp.Variance = VarianceModifier.Contravariant;
2523
tp.AddChild (Identifier.Create (arg.Name, Convert (arg.Location)), Roles.Identifier);
2524
parent.AddChild (tp, Roles.TypeParameter);
2526
if (chevronLocs != null)
2527
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2530
void AddTypeArguments (AstNode parent, MemberName memberName)
2532
if (memberName == null || memberName.TypeParameters == null)
2534
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2535
if (chevronLocs != null)
2536
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2538
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2539
var arg = memberName.TypeParameters [i];
2542
parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2543
if (chevronLocs != null && i < chevronLocs.Count - 2)
2544
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
2547
if (chevronLocs != null)
2548
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2551
void AddTypeArguments (AstNode parent, ATypeNameExpression memberName)
2553
if (memberName == null || !memberName.HasTypeArguments)
2555
var chevronLocs = LocationsBag.GetLocations (memberName.TypeArguments);
2556
if (chevronLocs != null)
2557
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2])), Roles.LChevron);
2559
for (int i = 0; i < memberName.TypeArguments.Count; i++) {
2560
var arg = memberName.TypeArguments.Args [i];
2563
parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2564
if (chevronLocs != null && i < chevronLocs.Count - 2)
2565
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i])), Roles.Comma);
2568
if (chevronLocs != null)
2569
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1])), Roles.RChevron);
2572
void AddConstraints(AstNode parent, TypeParameters d)
2576
for (int i = d.Count - 1; i >= 0; i--) {
2577
var typeParameter = d [i];
2578
if (typeParameter == null)
2580
var c = typeParameter.Constraints;
2583
var location = LocationsBag.GetLocations (c);
2584
var constraint = new Constraint ();
2585
constraint.AddChild (new CSharpTokenNode (Convert (c.Location)), Roles.WhereKeyword);
2586
constraint.AddChild (new SimpleType (Identifier.Create (c.TypeParameter.Value, Convert (c.TypeParameter.Location))), Roles.ConstraintTypeParameter);
2587
if (location != null)
2588
constraint.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Colon);
2589
var commaLocs = LocationsBag.GetLocations (c.ConstraintExpressions);
2591
if (c.ConstraintExpressions != null) {
2592
foreach (var expr in c.ConstraintExpressions) {
2593
constraint.AddChild (ConvertToType (expr), Roles.BaseType);
2594
if (commaLocs != null && curComma < commaLocs.Count)
2595
constraint.AddChild (new CSharpTokenNode (Convert (commaLocs [curComma++])), Roles.Comma);
2599
parent.AddChild (constraint, Roles.Constraint);
2603
Expression ConvertArgument (Argument arg)
2605
if (arg is NamedArgument) {
2606
var na = (NamedArgument)arg;
2607
NamedArgumentExpression newArg = new NamedArgumentExpression ();
2608
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
2610
var loc = LocationsBag.GetLocations (na);
2612
newArg.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.Colon);
2614
if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2615
DirectionExpression direction = new DirectionExpression ();
2616
direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2617
var argLocation = LocationsBag.GetLocations (arg);
2618
if (argLocation != null)
2619
direction.AddChild (new CSharpTokenNode (Convert (argLocation [0])), arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole);
2620
direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2621
newArg.AddChild (direction, Roles.Expression);
2623
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
2628
if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2629
DirectionExpression direction = new DirectionExpression ();
2630
direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2631
var argLocation = LocationsBag.GetLocations (arg);
2632
if (argLocation != null)
2633
direction.AddChild (new CSharpTokenNode (Convert (argLocation [0])), arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole);
2634
direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2638
return (Expression)arg.Expr.Accept (this);
2641
void AddArguments (AstNode parent, object location, Mono.CSharp.Arguments args)
2646
var commaLocations = LocationsBag.GetLocations (args);
2647
for (int i = 0; i < args.Count; i++) {
2648
parent.AddChild (ConvertArgument (args [i]), Roles.Argument);
2649
if (commaLocations != null && i < commaLocations.Count) {
2650
parent.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2653
if (commaLocations != null && commaLocations.Count > args.Count)
2654
parent.AddChild (new CSharpTokenNode (Convert (commaLocations [args.Count])), Roles.Comma);
2657
public override object Visit (Invocation invocationExpression)
2659
var result = new InvocationExpression ();
2660
var location = LocationsBag.GetLocations (invocationExpression);
2661
if (invocationExpression.Exp != null)
2662
result.AddChild ((Expression)invocationExpression.Exp.Accept (this), Roles.TargetExpression);
2663
if (location != null)
2664
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2665
AddArguments (result, location, invocationExpression.Arguments);
2667
if (location != null && location.Count > 1)
2668
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2672
public override object Visit (New newExpression)
2674
var result = new ObjectCreateExpression ();
2675
var location = LocationsBag.GetLocations (newExpression);
2676
result.AddChild (new CSharpTokenNode (Convert (newExpression.Location)), ObjectCreateExpression.NewKeywordRole);
2678
if (newExpression.TypeRequested != null)
2679
result.AddChild (ConvertToType (newExpression.TypeRequested), Roles.Type);
2680
if (location != null)
2681
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2682
AddArguments (result, location, newExpression.Arguments);
2684
if (location != null && location.Count > 1)
2685
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2690
public override object Visit (NewAnonymousType newAnonymousType)
2692
var result = new AnonymousTypeCreateExpression ();
2693
var location = LocationsBag.GetLocations (newAnonymousType);
2694
result.AddChild (new CSharpTokenNode (Convert (newAnonymousType.Location)), ObjectCreateExpression.NewKeywordRole);
2695
if (location != null)
2696
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBrace);
2697
if (newAnonymousType.Parameters != null) {
2698
foreach (var par in newAnonymousType.Parameters) {
2701
var parLocation = LocationsBag.GetLocations (par);
2703
if (parLocation == null) {
2704
if (par.Expr != null)
2705
result.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2707
var namedExpression = new NamedExpression ();
2708
namedExpression.AddChild (Identifier.Create (par.Name, Convert (par.Location)), Roles.Identifier);
2709
namedExpression.AddChild (new CSharpTokenNode (Convert (parLocation [0])), Roles.Assign);
2710
if (par.Expr != null)
2711
namedExpression.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2712
result.AddChild (namedExpression, Roles.Expression);
2716
if (location != null && location.Count > 1)
2717
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBrace);
2721
ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
2725
var init = new ArrayInitializerExpression();
2726
AddConvertCollectionOrObjectInitializers(init, minit);
2730
void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
2732
var initLoc = LocationsBag.GetLocations(minit);
2733
var commaLoc = LocationsBag.GetLocations(minit.Initializers);
2735
if (initLoc != null)
2736
init.AddChild(new CSharpTokenNode(Convert(initLoc [0])), Roles.LBrace);
2737
foreach (var expr in minit.Initializers) {
2738
var collectionInit = expr as CollectionElementInitializer;
2739
if (collectionInit != null) {
2741
// For ease of use purposes in the resolver the ast representation
2742
// of { a, b, c } is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
2743
// can be identified by expr.IsSingleElement.
2744
if (!collectionInit.IsSingle) {
2745
parent = new ArrayInitializerExpression();
2746
parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location)), Roles.LBrace);
2748
parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
2750
for (int i = 0; i < collectionInit.Arguments.Count; i++) {
2751
var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
2755
(ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept(this),
2760
if (!collectionInit.IsSingle) {
2761
var braceLocs = LocationsBag.GetLocations(expr);
2762
if (braceLocs != null)
2763
parent.AddChild(new CSharpTokenNode(Convert(braceLocs [0])), Roles.RBrace);
2765
init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
2767
var eleInit = expr as ElementInitializer;
2768
if (eleInit != null) {
2769
var nexpr = new NamedExpression();
2771
Identifier.Create(eleInit.Name, Convert(eleInit.Location)),
2774
var assignLoc = LocationsBag.GetLocations(eleInit);
2775
if (assignLoc != null)
2776
nexpr.AddChild(new CSharpTokenNode(Convert(assignLoc [0])), Roles.Assign);
2777
if (eleInit.Source != null) {
2778
if (eleInit.Source is CollectionOrObjectInitializers) {
2779
var arrInit = new ArrayInitializerExpression();
2780
AddConvertCollectionOrObjectInitializers(
2782
eleInit.Source as CollectionOrObjectInitializers
2784
nexpr.AddChild(arrInit, Roles.Expression);
2786
nexpr.AddChild((Expression)eleInit.Source.Accept(this), Roles.Expression);
2790
init.AddChild(nexpr, Roles.Expression);
2793
if (commaLoc != null && curComma < commaLoc.Count)
2794
init.AddChild(new CSharpTokenNode(Convert(commaLoc [curComma++])), Roles.Comma);
2797
if (initLoc != null) {
2798
if (initLoc.Count == 3) // optional comma
2799
init.AddChild(new CSharpTokenNode(Convert(initLoc [1])), Roles.Comma);
2800
init.AddChild(new CSharpTokenNode(Convert(initLoc [initLoc.Count - 1])), Roles.RBrace);
2805
public override object Visit (NewInitialize newInitializeExpression)
2807
var result = new ObjectCreateExpression ();
2808
result.AddChild (new CSharpTokenNode (Convert (newInitializeExpression.Location)), ObjectCreateExpression.NewKeywordRole);
2810
if (newInitializeExpression.TypeRequested != null)
2811
result.AddChild (ConvertToType (newInitializeExpression.TypeRequested), Roles.Type);
2813
var location = LocationsBag.GetLocations (newInitializeExpression);
2814
if (location != null)
2815
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2816
AddArguments (result, location, newInitializeExpression.Arguments);
2817
if (location != null && location.Count > 1)
2818
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2820
var init = ConvertCollectionOrObjectInitializers (newInitializeExpression.Initializers);
2822
result.AddChild (init, ObjectCreateExpression.InitializerRole);
2827
public override object Visit (ArrayCreation arrayCreationExpression)
2829
var result = new ArrayCreateExpression ();
2831
var location = LocationsBag.GetLocations (arrayCreationExpression);
2832
result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location)), ArrayCreateExpression.NewKeywordRole);
2833
if (arrayCreationExpression.TypeExpression != null)
2834
result.AddChild (ConvertToType (arrayCreationExpression.TypeExpression), Roles.Type);
2836
var next = arrayCreationExpression.Rank;
2837
if (arrayCreationExpression.Arguments != null) {
2838
// skip first array rank.
2841
if (location != null)
2842
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LBracket);
2844
var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments);
2845
for (int i = 0; i < arrayCreationExpression.Arguments.Count; i++) {
2846
result.AddChild ((Expression)arrayCreationExpression.Arguments [i].Accept (this), Roles.Argument);
2847
if (commaLocations != null && i < commaLocations.Count)
2848
result.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2850
if (location != null && location.Count > 1)
2851
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RBracket);
2855
while (next != null) {
2856
ArraySpecifier spec = new ArraySpecifier (next.Dimension);
2857
var loc = LocationsBag.GetLocations (next);
2858
spec.AddChild (new CSharpTokenNode (Convert (next.Location)), Roles.LBracket);
2859
result.AddChild (spec, ArrayCreateExpression.AdditionalArraySpecifierRole);
2861
result.AddChild (new CSharpTokenNode (Convert (loc [0])), Roles.RBracket);
2865
if (arrayCreationExpression.Initializers != null) {
2866
var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers);
2867
ArrayInitializerExpression initializer = new ArrayInitializerExpression ();
2869
initializer.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location)), Roles.LBrace);
2870
var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements);
2871
for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) {
2872
var init = arrayCreationExpression.Initializers [i];
2875
initializer.AddChild ((Expression)init.Accept (this), Roles.Expression);
2876
if (commaLocations != null && i < commaLocations.Count) {
2877
initializer.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
2881
if (initLocation != null)
2882
initializer.AddChild (new CSharpTokenNode (Convert (initLocation [initLocation.Count - 1])), Roles.RBrace);
2883
result.AddChild (initializer, ArrayCreateExpression.InitializerRole);
2889
public override object Visit (This thisExpression)
2891
var result = new ThisReferenceExpression ();
2892
result.Location = Convert (thisExpression.Location);
2896
public override object Visit (ArglistAccess argListAccessExpression)
2898
var result = new UndocumentedExpression () {
2899
UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess
2901
result.AddChild (new CSharpTokenNode (Convert (argListAccessExpression.Location)), UndocumentedExpression.ArglistKeywordRole);
2905
#region Undocumented expressions
2906
public override object Visit (Arglist argListExpression)
2908
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.ArgList };
2909
result.AddChild (new CSharpTokenNode (Convert (argListExpression.Location)), UndocumentedExpression.ArglistKeywordRole);
2910
var location = LocationsBag.GetLocations (argListExpression);
2911
if (location != null)
2912
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2914
AddArguments (result, location, argListExpression.Arguments);
2916
if (location != null && location.Count > 1)
2917
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2921
public override object Visit (MakeRefExpr makeRefExpr)
2923
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.MakeRef };
2924
result.AddChild (new CSharpTokenNode (Convert (makeRefExpr.Location)), UndocumentedExpression.MakerefKeywordRole);
2925
var location = LocationsBag.GetLocations (makeRefExpr);
2926
if (location != null)
2927
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2928
if (makeRefExpr.Expr != null)
2929
result.AddChild ((Expression)makeRefExpr.Expr.Accept (this), Roles.Argument);
2930
if (location != null && location.Count > 1)
2931
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2935
public override object Visit (RefTypeExpr refTypeExpr)
2937
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefType };
2938
result.AddChild (new CSharpTokenNode (Convert (refTypeExpr.Location)), UndocumentedExpression.ReftypeKeywordRole);
2939
var location = LocationsBag.GetLocations (refTypeExpr);
2940
if (location != null)
2941
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2943
if (refTypeExpr.Expr != null)
2944
result.AddChild ((Expression)refTypeExpr.Expr.Accept (this), Roles.Argument);
2946
if (location != null && location.Count > 1)
2947
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2951
public override object Visit (RefValueExpr refValueExpr)
2953
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefValue };
2954
result.AddChild (new CSharpTokenNode (Convert (refValueExpr.Location)), UndocumentedExpression.RefvalueKeywordRole);
2955
var location = LocationsBag.GetLocations (refValueExpr);
2956
if (location != null)
2957
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2960
if (refValueExpr.Expr != null)
2961
result.AddChild ((Expression)refValueExpr.Expr.Accept (this), Roles.Argument);
2963
if (refValueExpr.FullNamedExpression != null)
2964
result.AddChild ((Expression)refValueExpr.FullNamedExpression.Accept (this), Roles.Argument);
2966
if (location != null && location.Count > 1)
2967
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2972
public override object Visit (TypeOf typeOfExpression)
2974
var result = new TypeOfExpression ();
2975
var location = LocationsBag.GetLocations (typeOfExpression);
2976
result.AddChild (new CSharpTokenNode (Convert (typeOfExpression.Location)), TypeOfExpression.TypeofKeywordRole);
2977
if (location != null)
2978
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2979
if (typeOfExpression.TypeExpression != null)
2980
result.AddChild (ConvertToType (typeOfExpression.TypeExpression), Roles.Type);
2981
if (location != null && location.Count > 1)
2982
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
2986
public override object Visit (SizeOf sizeOfExpression)
2988
var result = new SizeOfExpression ();
2989
var location = LocationsBag.GetLocations (sizeOfExpression);
2990
result.AddChild (new CSharpTokenNode (Convert (sizeOfExpression.Location)), SizeOfExpression.SizeofKeywordRole);
2991
if (location != null)
2992
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
2993
if (sizeOfExpression.TypeExpression != null)
2994
result.AddChild (ConvertToType (sizeOfExpression.TypeExpression), Roles.Type);
2995
if (location != null && location.Count > 1)
2996
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
3000
public override object Visit (CheckedExpr checkedExpression)
3002
var result = new CheckedExpression ();
3003
var location = LocationsBag.GetLocations (checkedExpression);
3004
result.AddChild (new CSharpTokenNode (Convert (checkedExpression.Location)), CheckedExpression.CheckedKeywordRole);
3005
if (location != null)
3006
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
3007
if (checkedExpression.Expr != null)
3008
result.AddChild ((Expression)checkedExpression.Expr.Accept (this), Roles.Expression);
3009
if (location != null && location.Count > 1)
3010
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
3014
public override object Visit (UnCheckedExpr uncheckedExpression)
3016
var result = new UncheckedExpression ();
3017
var location = LocationsBag.GetLocations (uncheckedExpression);
3018
result.AddChild (new CSharpTokenNode (Convert (uncheckedExpression.Location)), UncheckedExpression.UncheckedKeywordRole);
3019
if (location != null)
3020
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.LPar);
3021
if (uncheckedExpression.Expr != null)
3022
result.AddChild ((Expression)uncheckedExpression.Expr.Accept (this), Roles.Expression);
3023
if (location != null && location.Count > 1)
3024
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.RPar);
3028
public override object Visit (ElementAccess elementAccessExpression)
3030
IndexerExpression result = new IndexerExpression ();
3031
var location = LocationsBag.GetLocations (elementAccessExpression);
3033
if (elementAccessExpression.Expr != null)
3034
result.AddChild ((Expression)elementAccessExpression.Expr.Accept (this), Roles.TargetExpression);
3035
result.AddChild (new CSharpTokenNode (Convert (elementAccessExpression.Location)), Roles.LBracket);
3036
AddArguments (result, location, elementAccessExpression.Arguments);
3037
if (location != null)
3038
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.RBracket);
3042
public override object Visit (BaseThis baseAccessExpression)
3044
var result = new BaseReferenceExpression ();
3045
result.Location = Convert (baseAccessExpression.Location);
3049
public override object Visit (StackAlloc stackAllocExpression)
3051
var result = new StackAllocExpression ();
3053
var location = LocationsBag.GetLocations (stackAllocExpression);
3054
if (location != null)
3055
result.AddChild (new CSharpTokenNode (Convert (location [0])), StackAllocExpression.StackallocKeywordRole);
3056
if (stackAllocExpression.TypeExpression != null)
3057
result.AddChild (ConvertToType (stackAllocExpression.TypeExpression), Roles.Type);
3058
if (location != null && location.Count > 1)
3059
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.LBracket);
3060
if (stackAllocExpression.CountExpression != null)
3061
result.AddChild ((Expression)stackAllocExpression.CountExpression.Accept (this), Roles.Expression);
3062
if (location != null && location.Count > 2)
3063
result.AddChild (new CSharpTokenNode (Convert (location [2])), Roles.RBracket);
3067
public override object Visit (SimpleAssign simpleAssign)
3069
var result = new AssignmentExpression ();
3071
result.Operator = AssignmentOperatorType.Assign;
3072
if (simpleAssign.Target != null)
3073
result.AddChild ((Expression)simpleAssign.Target.Accept (this), AssignmentExpression.LeftRole);
3074
result.AddChild (new CSharpTokenNode (Convert (simpleAssign.Location)), AssignmentExpression.AssignRole);
3075
if (simpleAssign.Source != null) {
3076
result.AddChild ((Expression)simpleAssign.Source.Accept (this), AssignmentExpression.RightRole);
3081
public override object Visit (CompoundAssign compoundAssign)
3083
var result = new AssignmentExpression ();
3084
switch (compoundAssign.Op) {
3085
case Binary.Operator.Multiply:
3086
result.Operator = AssignmentOperatorType.Multiply;
3088
case Binary.Operator.Division:
3089
result.Operator = AssignmentOperatorType.Divide;
3091
case Binary.Operator.Modulus:
3092
result.Operator = AssignmentOperatorType.Modulus;
3094
case Binary.Operator.Addition:
3095
result.Operator = AssignmentOperatorType.Add;
3097
case Binary.Operator.Subtraction:
3098
result.Operator = AssignmentOperatorType.Subtract;
3100
case Binary.Operator.LeftShift:
3101
result.Operator = AssignmentOperatorType.ShiftLeft;
3103
case Binary.Operator.RightShift:
3104
result.Operator = AssignmentOperatorType.ShiftRight;
3106
case Binary.Operator.BitwiseAnd:
3107
result.Operator = AssignmentOperatorType.BitwiseAnd;
3109
case Binary.Operator.BitwiseOr:
3110
result.Operator = AssignmentOperatorType.BitwiseOr;
3112
case Binary.Operator.ExclusiveOr:
3113
result.Operator = AssignmentOperatorType.ExclusiveOr;
3117
if (compoundAssign.Target != null)
3118
result.AddChild ((Expression)compoundAssign.Target.Accept (this), AssignmentExpression.LeftRole);
3119
result.AddChild (new CSharpTokenNode (Convert (compoundAssign.Location)), AssignmentExpression.GetOperatorRole (result.Operator));
3120
if (compoundAssign.Source != null)
3121
result.AddChild ((Expression)compoundAssign.Source.Accept (this), AssignmentExpression.RightRole);
3125
public override object Visit (Mono.CSharp.AnonymousMethodExpression anonymousMethodExpression)
3127
var result = new AnonymousMethodExpression ();
3128
var location = LocationsBag.GetLocations (anonymousMethodExpression);
3130
if (anonymousMethodExpression.IsAsync) {
3131
result.IsAsync = true;
3132
result.AddChild (new CSharpTokenNode (Convert (location [l++])), AnonymousMethodExpression.AsyncModifierRole);
3134
if (location != null) {
3135
result.AddChild (new CSharpTokenNode (Convert (location [l++])), AnonymousMethodExpression.DelegateKeywordRole);
3137
if (location.Count > l) {
3138
result.HasParameterList = true;
3139
result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.LPar);
3140
AddParameter (result, anonymousMethodExpression.Parameters);
3141
result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.RPar);
3144
if (anonymousMethodExpression.Block != null)
3145
result.AddChild ((BlockStatement)anonymousMethodExpression.Block.Accept (this), Roles.Body);
3149
public override object Visit (Mono.CSharp.LambdaExpression lambdaExpression)
3151
var result = new LambdaExpression ();
3152
var location = LocationsBag.GetLocations (lambdaExpression);
3154
if (lambdaExpression.IsAsync) {
3155
result.IsAsync = true;
3156
result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.AsyncModifierRole);
3159
if (location == null || location.Count == l + 1) {
3160
AddParameter (result, lambdaExpression.Parameters);
3161
if (location != null)
3162
result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.ArrowRole);
3164
result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.LPar);
3165
AddParameter (result, lambdaExpression.Parameters);
3166
if (location != null) {
3167
result.AddChild (new CSharpTokenNode (Convert (location [l++])), Roles.RPar);
3168
result.AddChild (new CSharpTokenNode (Convert (location [l++])), LambdaExpression.ArrowRole);
3171
if (lambdaExpression.Block != null) {
3172
if (lambdaExpression.Block.IsCompilerGenerated) {
3173
ContextualReturn generatedReturn = (ContextualReturn)lambdaExpression.Block.Statements [0];
3174
result.AddChild ((AstNode)generatedReturn.Expr.Accept (this), LambdaExpression.BodyRole);
3176
result.AddChild ((AstNode)lambdaExpression.Block.Accept (this), LambdaExpression.BodyRole);
3183
public override object Visit (ConstInitializer constInitializer)
3185
return constInitializer.Expr.Accept (this);
3188
public override object Visit (ArrayInitializer arrayInitializer)
3190
var result = new ArrayInitializerExpression ();
3191
var location = LocationsBag.GetLocations (arrayInitializer);
3192
result.AddChild (new CSharpTokenNode (Convert (arrayInitializer.Location)), Roles.LBrace);
3193
var commaLocations = LocationsBag.GetLocations (arrayInitializer.Elements);
3194
for (int i = 0; i < arrayInitializer.Count; i++) {
3195
var init = arrayInitializer [i];
3198
result.AddChild ((Expression)init.Accept (this), Roles.Expression);
3199
if (commaLocations != null && i < commaLocations.Count)
3200
result.AddChild (new CSharpTokenNode (Convert (commaLocations [i])), Roles.Comma);
3203
if (location != null) {
3204
if (location.Count == 2) // optional comma
3205
result.AddChild (new CSharpTokenNode (Convert (location [1])), Roles.Comma);
3206
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1])), Roles.RBrace);
3213
#region LINQ expressions
3214
public override object Visit (Mono.CSharp.Linq.QueryExpression queryExpression)
3216
var result = new QueryExpression ();
3218
var currentClause = queryExpression.next;
3220
while (currentClause != null) {
3221
QueryClause clause = (QueryClause)currentClause.Accept (this);
3222
if (clause is QueryContinuationClause) {
3223
// insert preceding query at beginning of QueryContinuationClause
3224
clause.InsertChildAfter (null, result, QueryContinuationClause.PrecedingQueryRole);
3225
// create a new QueryExpression for the remaining query
3226
result = new QueryExpression ();
3228
result.AddChild (clause, QueryExpression.ClauseRole);
3229
currentClause = currentClause.next;
3235
public override object Visit (Mono.CSharp.Linq.QueryStartClause queryStart)
3237
if (queryStart.Expr == null) {
3238
var intoClause = new QueryContinuationClause ();
3239
intoClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryContinuationClause.IntoKeywordRole);
3240
intoClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3244
var fromClause = new QueryFromClause ();
3246
fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryFromClause.FromKeywordRole);
3248
if (queryStart.IdentifierType != null)
3249
fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3251
fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3253
var location = LocationsBag.GetLocations (queryStart);
3254
if (location != null)
3255
fromClause.AddChild (new CSharpTokenNode (Convert (location [0])), QueryFromClause.InKeywordRole);
3257
if (queryStart.Expr != null)
3258
fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3262
public override object Visit (Mono.CSharp.Linq.SelectMany queryStart)
3264
var fromClause = new QueryFromClause ();
3266
fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location)), QueryFromClause.FromKeywordRole);
3268
if (queryStart.IdentifierType != null)
3269
fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3271
fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3273
var location = LocationsBag.GetLocations (queryStart);
3274
if (location != null)
3275
fromClause.AddChild (new CSharpTokenNode (Convert (location [0])), QueryFromClause.InKeywordRole);
3277
if (queryStart.Expr != null)
3278
fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3282
public override object Visit (Mono.CSharp.Linq.Select sel)
3284
var result = new QuerySelectClause ();
3285
result.AddChild (new CSharpTokenNode (Convert (sel.Location)), QuerySelectClause.SelectKeywordRole);
3286
if (sel.Expr != null)
3287
result.AddChild ((Expression)sel.Expr.Accept (this), Roles.Expression);
3291
public override object Visit (Mono.CSharp.Linq.GroupBy groupBy)
3293
var result = new QueryGroupClause ();
3294
var location = LocationsBag.GetLocations (groupBy);
3295
result.AddChild (new CSharpTokenNode (Convert (groupBy.Location)), QueryGroupClause.GroupKeywordRole);
3296
if (groupBy.ElementSelector != null)
3297
result.AddChild ((Expression)groupBy.ElementSelector.Accept (this), QueryGroupClause.ProjectionRole);
3298
if (location != null)
3299
result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryGroupClause.ByKeywordRole);
3300
if (groupBy.Expr != null)
3301
result.AddChild ((Expression)groupBy.Expr.Accept (this), QueryGroupClause.KeyRole);
3305
public override object Visit (Mono.CSharp.Linq.Let l)
3307
var result = new QueryLetClause ();
3308
var location = LocationsBag.GetLocations (l);
3310
result.AddChild (new CSharpTokenNode (Convert (l.Location)), QueryLetClause.LetKeywordRole);
3311
result.AddChild (Identifier.Create (l.IntoVariable.Name, Convert (l.IntoVariable.Location)), Roles.Identifier);
3312
if (location != null)
3313
result.AddChild (new CSharpTokenNode (Convert (location [0])), Roles.Assign);
3315
result.AddChild ((Expression)l.Expr.Accept (this), Roles.Expression);
3319
public override object Visit (Mono.CSharp.Linq.Where w)
3321
var result = new QueryWhereClause ();
3322
var location = LocationsBag.GetLocations (w);
3323
if (location != null)
3324
result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryWhereClause.WhereKeywordRole);
3326
result.AddChild ((Expression)w.Expr.Accept (this), Roles.Condition);
3330
public override object Visit (Mono.CSharp.Linq.Join join)
3332
var result = new QueryJoinClause ();
3333
var location = LocationsBag.GetLocations (join);
3334
result.AddChild (new CSharpTokenNode (Convert (join.Location)), QueryJoinClause.JoinKeywordRole);
3335
result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3337
if (location != null)
3338
result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryJoinClause.InKeywordRole);
3340
if (join.Expr != null)
3341
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3343
if (location != null && location.Count > 1)
3344
result.AddChild (new CSharpTokenNode (Convert (location [1])), QueryJoinClause.OnKeywordRole);
3346
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3348
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3350
if (location != null && location.Count > 2)
3351
result.AddChild (new CSharpTokenNode (Convert (location [2])), QueryJoinClause.EqualsKeywordRole);
3353
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3355
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3360
public override object Visit (Mono.CSharp.Linq.GroupJoin join)
3362
var result = new QueryJoinClause ();
3363
var location = LocationsBag.GetLocations (join);
3364
result.AddChild (new CSharpTokenNode (Convert (join.Location)), QueryJoinClause.JoinKeywordRole);
3366
// mcs seems to have swapped IntoVariable with JoinVariable, so we'll swap it back here
3367
result.AddChild (Identifier.Create (join.IntoVariable.Name, Convert (join.IntoVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3369
if (location != null)
3370
result.AddChild (new CSharpTokenNode (Convert (location [0])), QueryJoinClause.InKeywordRole);
3372
if (join.Expr != null)
3373
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3375
if (location != null && location.Count > 1)
3376
result.AddChild (new CSharpTokenNode (Convert (location [1])), QueryJoinClause.OnKeywordRole);
3378
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3380
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3383
if (location != null && location.Count > 2)
3384
result.AddChild (new CSharpTokenNode (Convert (location [2])), QueryJoinClause.EqualsKeywordRole);
3385
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3387
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3389
if (location != null && location.Count > 3)
3390
result.AddChild (new CSharpTokenNode (Convert (location [3])), QueryJoinClause.IntoKeywordRole);
3392
result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.IntoIdentifierRole);
3396
public override object Visit (Mono.CSharp.Linq.OrderByAscending orderByAscending)
3398
var result = new QueryOrderClause ();
3400
var ordering = new QueryOrdering ();
3401
if (orderByAscending.Expr != null)
3402
ordering.AddChild ((Expression)orderByAscending.Expr.Accept (this), Roles.Expression);
3403
var location = LocationsBag.GetLocations (orderByAscending);
3404
if (location != null) {
3405
ordering.Direction = QueryOrderingDirection.Ascending;
3406
ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.AscendingKeywordRole);
3408
result.AddChild (ordering, QueryOrderClause.OrderingRole);
3412
public override object Visit (Mono.CSharp.Linq.OrderByDescending orderByDescending)
3414
var result = new QueryOrderClause ();
3416
var ordering = new QueryOrdering ();
3417
if (orderByDescending.Expr != null)
3418
ordering.AddChild ((Expression)orderByDescending.Expr.Accept (this), Roles.Expression);
3419
var location = LocationsBag.GetLocations (orderByDescending);
3420
if (location != null) {
3421
ordering.Direction = QueryOrderingDirection.Descending;
3422
ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.DescendingKeywordRole);
3424
result.AddChild (ordering, QueryOrderClause.OrderingRole);
3428
public override object Visit (Mono.CSharp.Linq.ThenByAscending thenByAscending)
3430
var result = new QueryOrderClause ();
3432
var ordering = new QueryOrdering ();
3433
if (thenByAscending.Expr != null)
3434
ordering.AddChild ((Expression)thenByAscending.Expr.Accept (this), Roles.Expression);
3435
var location = LocationsBag.GetLocations (thenByAscending);
3436
if (location != null) {
3437
ordering.Direction = QueryOrderingDirection.Ascending;
3438
ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.AscendingKeywordRole);
3440
result.AddChild (ordering, QueryOrderClause.OrderingRole);
3444
public override object Visit (Mono.CSharp.Linq.ThenByDescending thenByDescending)
3446
var result = new QueryOrderClause ();
3448
var ordering = new QueryOrdering ();
3449
if (thenByDescending.Expr != null)
3450
ordering.AddChild ((Expression)thenByDescending.Expr.Accept (this), Roles.Expression);
3451
var location = LocationsBag.GetLocations (thenByDescending);
3452
if (location != null) {
3453
ordering.Direction = QueryOrderingDirection.Descending;
3454
ordering.AddChild (new CSharpTokenNode (Convert (location [0])), QueryOrdering.DescendingKeywordRole);
3456
result.AddChild (ordering, QueryOrderClause.OrderingRole);
3460
public override object Visit (Await awaitExpr)
3462
var result = new UnaryOperatorExpression ();
3463
result.Operator = UnaryOperatorType.Await;
3464
result.AddChild (new CSharpTokenNode (Convert (awaitExpr.Location)), UnaryOperatorExpression.AwaitRole);
3465
if (awaitExpr.Expression != null)
3466
result.AddChild ((Expression)awaitExpr.Expression.Accept (this), Roles.Expression);
3472
public CSharpParser ()
3474
CompilerSettings = new CompilerSettings ();
3475
CompilerSettings.Unsafe = true;
3478
public CSharpParser (CompilerSettings args)
3480
CompilerSettings = args;
3483
static AstNode GetOuterLeft (AstNode node)
3485
var outerLeft = node;
3486
while (outerLeft.FirstChild != null)
3487
outerLeft = outerLeft.FirstChild;
3491
static AstNode NextLeaf (AstNode node)
3495
var next = node.NextSibling;
3498
return GetOuterLeft (next);
3501
static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
3503
var leaf = GetOuterLeft (conversionVisitor.Unit);
3504
for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) {
3505
var special = top.SpecialsBag.Specials [i];
3506
AstNode newLeaf = null;
3507
var comment = special as SpecialsBag.Comment;
3508
if (comment != null) {
3509
// HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer
3510
bool isMultilineDocumentationComment = (
3511
comment.CommentType == SpecialsBag.CommentType.Multi
3512
&& comment.Content.StartsWith("*", StringComparison.Ordinal)
3513
&& !comment.Content.StartsWith("**", StringComparison.Ordinal)
3515
if (conversionVisitor.convertTypeSystemMode && !(comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment))
3517
var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType;
3518
var start = new TextLocation (comment.Line, comment.Col);
3519
var end = new TextLocation (comment.EndLine, comment.EndCol);
3520
newLeaf = new Comment (type, start, end) {
3521
StartsLine = comment.StartsLine,
3522
Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
3525
var directive = special as SpecialsBag.PreProcessorDirective;
3526
if (directive != null) {
3527
newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
3528
Argument = directive.Arg,
3529
Take = directive.Take
3533
if (newLeaf == null)
3537
var nextLeaf = NextLeaf (leaf);
3538
// insert comment at begin
3539
if (newLeaf.StartLocation < leaf.StartLocation) {
3540
var node = leaf.Parent ?? conversionVisitor.Unit;
3541
while (node.Parent != null && node.FirstChild == leaf) {
3545
if (newLeaf is Comment) {
3546
node.InsertChildBefore (leaf, (Comment)newLeaf, Roles.Comment);
3548
node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3554
// insert comment at the end
3555
if (nextLeaf == null) {
3556
var node = leaf.Parent ?? conversionVisitor.Unit;
3557
if (newLeaf is Comment) {
3558
node.AddChild ((Comment)newLeaf, Roles.Comment);
3560
node.AddChild ((PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3566
// comment is between 2 nodes
3567
if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) {
3568
var node = leaf.Parent ?? conversionVisitor.Unit;
3569
if (newLeaf is Comment) {
3570
node.InsertChildAfter (leaf, (Comment)newLeaf, Roles.Comment);
3572
node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3582
public class ErrorReportPrinter : ReportPrinter
3584
readonly string fileName;
3585
public readonly List<Error> Errors = new List<Error> ();
3587
public ErrorReportPrinter (string fileName)
3589
this.fileName = fileName;
3592
public override void Print (AbstractMessage msg, bool showFullPath)
3594
base.Print (msg, showFullPath);
3595
var newError = new Error (msg.IsWarning ? ErrorType.Warning : ErrorType.Error, msg.Text, new DomRegion (fileName, msg.Location.Row, msg.Location.Column));
3596
Errors.Add (newError);
3599
ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (null);
3601
public ErrorReportPrinter ErrorPrinter {
3603
return errorReportPrinter;
3607
public bool HasErrors {
3609
return errorReportPrinter.ErrorsCount > 0;
3613
public bool HasWarnings {
3615
return errorReportPrinter.WarningsCount > 0;
3619
public CompilationUnit Parse (ITextSource textSource, string fileName, int lineModifier = 0)
3621
return Parse (textSource.CreateReader (), fileName, lineModifier);
3624
public CompilationUnit Parse (TextReader reader, string fileName, int lineModifier = 0)
3626
// TODO: can we optimize this to avoid the text->stream->text roundtrip?
3627
using (MemoryStream stream = new MemoryStream ()) {
3628
StreamWriter w = new StreamWriter (stream, Encoding.UTF8);
3629
char[] buffer = new char[2048];
3631
while ((read = reader.ReadBlock(buffer, 0, buffer.Length)) > 0)
3632
w.Write (buffer, 0, read);
3633
w.Flush (); // we can't close the StreamWriter because that would also close the MemoryStream
3634
stream.Position = 0;
3636
return Parse (stream, fileName, lineModifier);
3640
public CompilationUnit Parse(CompilerCompilationUnit top, string fileName, int lineModifier = 0)
3645
CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag);
3646
top.ModuleCompiled.Accept(conversionVisitor);
3647
InsertComments(top, conversionVisitor);
3648
if (CompilationUnitCallback != null) {
3649
CompilationUnitCallback(top);
3651
if (top.LastYYValue is Mono.CSharp.Expression) {
3652
conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode;
3654
conversionVisitor.Unit.FileName = fileName;
3655
return conversionVisitor.Unit;
3658
public CompilerSettings CompilerSettings {
3663
public Action<CompilerCompilationUnit> CompilationUnitCallback {
3668
public bool GenerateTypeSystemMode {
3673
public CompilationUnit Parse (string program, string fileName)
3675
return Parse (new StringReader (program), fileName);
3678
internal static object parseLock = new object ();
3680
public CompilationUnit Parse(Stream stream, string fileName, int lineModifier = 0)
3683
errorReportPrinter = new ErrorReportPrinter ("");
3684
var ctx = new CompilerContext (CompilerSettings, errorReportPrinter);
3685
ctx.Settings.TabSize = 1;
3686
var reader = new SeekableStreamReader (stream, Encoding.UTF8);
3687
var file = new SourceFile (fileName, fileName, 0);
3688
Location.Initialize (new List<SourceFile> (new [] { file }));
3689
var module = new ModuleContainer (ctx);
3690
var parser = Driver.Parse (reader, file, module, lineModifier);
3692
var top = new CompilerCompilationUnit () {
3693
ModuleCompiled = module,
3694
LocationsBag = parser.LocationsBag,
3695
SpecialsBag = parser.Lexer.sbag
3697
var unit = Parse (top, fileName, lineModifier);
3698
unit.Errors.AddRange (errorReportPrinter.Errors);
3699
CompilerCallableEntryPoint.Reset ();
3704
public IEnumerable<EntityDeclaration> ParseTypeMembers (TextReader reader, int lineModifier = 0)
3706
string code = "unsafe partial class MyClass { " + Environment.NewLine + reader.ReadToEnd () + "}";
3707
var cu = Parse (new StringReader (code), "parsed.cs", lineModifier - 1);
3709
return Enumerable.Empty<EntityDeclaration> ();
3710
var td = cu.Children.FirstOrDefault () as TypeDeclaration;
3713
return Enumerable.Empty<EntityDeclaration> ();
3716
public IEnumerable<Statement> ParseStatements (TextReader reader, int lineModifier = 0)
3718
string code = "void M() { " + Environment.NewLine + reader.ReadToEnd () + "}";
3719
var members = ParseTypeMembers (new StringReader (code), lineModifier - 1);
3720
var method = members.FirstOrDefault () as MethodDeclaration;
3721
if (method != null && method.Body != null)
3722
return method.Body.Statements;
3723
return Enumerable.Empty<Statement> ();
3726
public AstType ParseTypeReference (TextReader reader)
3728
string code = reader.ReadToEnd () + " a;";
3729
var members = ParseTypeMembers (new StringReader (code));
3730
var field = members.FirstOrDefault () as FieldDeclaration;
3732
return field.ReturnType;
3733
return AstType.Null;
3736
public AstNode ParseExpression (TextReader reader)
3738
var es = ParseStatements (new StringReader ("tmp = " + Environment.NewLine + reader.ReadToEnd () + ";"), -1).FirstOrDefault () as ExpressionStatement;
3740
AssignmentExpression ae = es.Expression as AssignmentExpression;
3748
/// Parses a file snippet; guessing what the code snippet represents (compilation unit, type members, block, type reference, expression).
3750
public AstNode ParseSnippet (TextReader reader)
3752
// TODO: add support for parsing a part of a file
3753
throw new NotImplementedException ();
3756
public DocumentationReference ParseDocumentationReference (string cref)
3759
throw new ArgumentNullException ("cref");
3760
cref = cref.Replace ('{', '<').Replace ('}', '>');
3761
// TODO: add support for parsing cref attributes
3762
// (documentation_parsing production, see DocumentationBuilder.HandleXrefCommon)
3763
throw new NotImplementedException ();