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
CompilerSettings compilerSettings;
41
class ConversionVisitor : StructuralVisitor
43
SyntaxTree unit = new SyntaxTree ();
44
internal bool convertTypeSystemMode;
46
public SyntaxTree Unit {
55
public LocationsBag LocationsBag {
60
public ConversionVisitor (bool convertTypeSystemMode, LocationsBag locationsBag)
62
this.convertTypeSystemMode = convertTypeSystemMode;
63
this.LocationsBag = locationsBag;
66
public static TextLocation Convert (Mono.CSharp.Location loc)
68
return new TextLocation (loc.Row, loc.Column);
71
public override void Visit(ModuleContainer mc)
74
foreach (var container in mc.Containers) {
75
var nspace = container as NamespaceContainer;
77
container.Accept(this);
80
NamespaceDeclaration nDecl = null;
81
var loc = LocationsBag.GetLocations(nspace);
83
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
84
nDecl = new NamespaceDeclaration ();
86
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
88
ConvertNamespaceName(nspace.RealMemberName, nDecl);
89
if (loc != null && loc.Count > 1) {
90
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1]), Roles.LBrace), Roles.LBrace);
92
AddToNamespace(nDecl);
93
namespaceStack.Push(nDecl);
96
if (nspace.Usings != null) {
97
foreach (var us in nspace.Usings) {
104
if (mc.OptAttributes != null) {
105
foreach (var attr in mc.OptAttributes.Sections) {
106
unit.AddChild (ConvertAttributeSection (attr), SyntaxTree.MemberRole);
111
if (nspace.Containers != null) {
112
foreach (var subContainer in nspace.Containers) {
113
subContainer.Accept(this);
117
AddAttributeSection (nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
118
if (loc != null && loc.Count > 2)
119
nDecl.AddChild (new CSharpTokenNode (Convert (loc [2]), Roles.RBrace), Roles.RBrace);
120
if (loc != null && loc.Count > 3)
121
nDecl.AddChild (new CSharpTokenNode (Convert (loc [3]), Roles.Semicolon), Roles.Semicolon);
123
namespaceStack.Pop ();
125
AddAttributeSection (unit, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
128
AddAttributeSection (unit, mc.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
132
Stack<NamespaceDeclaration> namespaceStack = new Stack<NamespaceDeclaration> ();
134
void AddTypeArguments (ATypeNameExpression texpr, AstType result)
136
if (texpr.TypeArguments == null || texpr.TypeArguments.Args == null)
138
var loc = LocationsBag.GetLocations (texpr.TypeArguments);
139
if (loc != null && loc.Count >= 2)
140
result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 2]), Roles.LChevron), Roles.LChevron);
142
foreach (var arg in texpr.TypeArguments.Args) {
143
result.AddChild (ConvertToType (arg), Roles.TypeArgument);
144
if (loc != null && i < loc.Count - 2)
145
result.AddChild (new CSharpTokenNode (Convert (loc [i++]), Roles.Comma), Roles.Comma);
147
if (loc != null && loc.Count >= 2)
148
result.AddChild (new CSharpTokenNode (Convert (loc [loc.Count - 1]), Roles.RChevron), Roles.RChevron);
151
AstType ConvertToType (TypeParameter spec)
154
result = new SimpleType () { IdentifierToken = Identifier.Create (spec.Name, Convert (spec.Location)) };
158
AstType ConvertToType (MemberName memberName)
161
if (memberName.Left != null) {
162
result = new MemberType ();
163
result.AddChild (ConvertToType (memberName.Left), MemberType.TargetRole);
164
var loc = LocationsBag.GetLocations (memberName.Left);
166
result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Dot), Roles.Dot);
167
result.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
169
result = new SimpleType () { IdentifierToken = Identifier.Create (memberName.Name, Convert (memberName.Location)) };
171
if (memberName.TypeParameters != null) {
172
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
173
if (chevronLocs != null)
174
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
175
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
176
var param = memberName.TypeParameters [i];
177
result.AddChild (new SimpleType (Identifier.Create (param.Name, Convert (param.Location))), Roles.TypeArgument);
178
if (chevronLocs != null && i < chevronLocs.Count - 2)
179
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
181
if (chevronLocs != null)
182
result.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
187
AstType ConvertToType (Mono.CSharp.Expression typeName)
189
if (typeName == null) // may happen in typeof(Generic<,,,,>)
190
return new SimpleType ();
192
if (typeName is TypeExpression) {
193
var typeExpr = (Mono.CSharp.TypeExpression)typeName;
194
return new PrimitiveType (typeExpr.GetSignatureForError (), Convert (typeExpr.Location));
197
if (typeName is Mono.CSharp.QualifiedAliasMember) {
198
var qam = (Mono.CSharp.QualifiedAliasMember)typeName;
199
var loc = LocationsBag.GetLocations (typeName);
200
var memberType = new MemberType ();
201
memberType.Target = new SimpleType (qam.alias, Convert (qam.Location));
202
memberType.IsDoubleColon = true;
204
if (loc != null && loc.Count > 0)
205
memberType.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.DoubleColon), Roles.DoubleColon);
207
memberType.MemberNameToken = Identifier.Create (qam.Name, loc != null ? Convert (loc[1]) : TextLocation.Empty);
211
if (typeName is MemberAccess) {
212
MemberAccess ma = (MemberAccess)typeName;
214
var memberType = new MemberType ();
215
memberType.AddChild (ConvertToType (ma.LeftExpression), MemberType.TargetRole);
216
if (!ma.DotLocation.IsNull)
217
memberType.AddChild (new CSharpTokenNode (Convert (ma.DotLocation), Roles.Dot), Roles.Dot);
219
memberType.MemberNameToken = Identifier.Create (ma.Name, Convert (ma.Location));
221
AddTypeArguments (ma, memberType);
225
if (typeName is SimpleName) {
226
var sn = (SimpleName)typeName;
227
var result = new SimpleType (sn.Name, Convert (sn.Location));
228
AddTypeArguments (sn, result);
232
if (typeName is ComposedCast) {
233
var cc = (ComposedCast)typeName;
234
var baseType = ConvertToType (cc.Left);
235
var result = new ComposedType () { BaseType = baseType };
236
var ccSpec = cc.Spec;
237
while (ccSpec != null) {
238
if (ccSpec.IsNullable) {
239
result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), ComposedType.NullableRole), ComposedType.NullableRole);
240
} else if (ccSpec.IsPointer) {
241
result.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), ComposedType.PointerRole), ComposedType.PointerRole);
243
var location = LocationsBag.GetLocations (ccSpec);
244
var spec = new ArraySpecifier () { Dimensions = ccSpec.Dimension };
245
spec.AddChild (new CSharpTokenNode (Convert (ccSpec.Location), Roles.LBracket), Roles.LBracket);
246
if (location != null)
247
spec.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RBracket), Roles.RBracket);
249
result.ArraySpecifiers.Add (spec);
251
ccSpec = ccSpec.Next;
256
if (typeName is SpecialContraintExpr) {
257
var sce = (SpecialContraintExpr)typeName;
258
switch (sce.Constraint) {
259
case SpecialConstraint.Class:
260
return new PrimitiveType ("class", Convert (sce.Location));
261
case SpecialConstraint.Struct:
262
return new PrimitiveType ("struct", Convert (sce.Location));
263
case SpecialConstraint.Constructor:
264
return new PrimitiveType ("new", Convert (sce.Location));
267
return new SimpleType ("unknown");
270
IEnumerable<Attribute> GetAttributes (IEnumerable<Mono.CSharp.Attribute> optAttributes)
272
if (optAttributes == null)
274
foreach (var attr in optAttributes) {
275
Attribute result = new Attribute ();
276
result.Type = ConvertToType (attr.TypeNameExpression);
277
var loc = LocationsBag.GetLocations (attr);
278
result.HasArgumentList = loc != null;
281
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.LPar), Roles.LPar);
283
if (attr.PositionalArguments != null) {
284
foreach (var arg in attr.PositionalArguments) {
287
var na = arg as NamedArgument;
289
var newArg = new NamedArgumentExpression ();
290
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
292
var argLoc = LocationsBag.GetLocations (na);
294
newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0]), Roles.Colon), Roles.Colon);
296
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
297
result.AddChild (newArg, Roles.Argument);
299
if (arg.Expr != null)
300
result.AddChild ((Expression)arg.Expr.Accept (this), Roles.Argument);
302
if (loc != null && pos + 1 < loc.Count)
303
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
306
if (attr.NamedArguments != null) {
307
foreach (NamedArgument na in attr.NamedArguments) {
308
var newArg = new NamedExpression ();
309
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
311
var argLoc = LocationsBag.GetLocations (na);
313
newArg.AddChild (new CSharpTokenNode (Convert (argLoc [0]), Roles.Assign), Roles.Assign);
315
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
316
result.AddChild (newArg, Roles.Argument);
317
if (loc != null && pos + 1 < loc.Count)
318
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
321
if (loc != null && pos < loc.Count)
322
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.RPar), Roles.RPar);
328
AttributeSection ConvertAttributeSection (IEnumerable<Mono.CSharp.Attribute> optAttributes)
331
if (optAttributes == null)
333
AttributeSection result = new AttributeSection ();
334
var loc = LocationsBag.GetLocations (optAttributes);
337
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.LBracket), Roles.LBracket);
338
var first = optAttributes.FirstOrDefault ();
339
string target = first != null ? first.ExplicitTarget : null;
341
if (!string.IsNullOrEmpty (target)) {
342
if (loc != null && pos < loc.Count - 1) {
343
result.AddChild (Identifier.Create (target, Convert (loc [pos++])), Roles.Identifier);
345
result.AddChild (Identifier.Create (target), Roles.Identifier);
347
if (loc != null && pos < loc.Count)
348
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Colon), Roles.Colon);
351
int attributeCount = 0;
352
foreach (var attr in GetAttributes (optAttributes)) {
353
result.AddChild (attr, Roles.Attribute);
354
if (loc != null && pos + 1 < loc.Count)
355
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
359
// Left and right bracket + commas between the attributes
360
int locCount = 2 + attributeCount - 1;
362
if (loc != null && pos < loc.Count - 1 && loc.Count == locCount + 1)
363
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.Comma), Roles.Comma);
364
if (loc != null && pos < loc.Count)
365
result.AddChild (new CSharpTokenNode (Convert (loc [pos++]), Roles.RBracket), Roles.RBracket);
369
public override void Visit(NamespaceContainer nspace)
371
NamespaceDeclaration nDecl = null;
372
var loc = LocationsBag.GetLocations(nspace);
374
if (nspace.NS != null && !string.IsNullOrEmpty(nspace.NS.Name)) {
375
nDecl = new NamespaceDeclaration ();
377
nDecl.AddChild(new CSharpTokenNode (Convert(loc [0]), Roles.NamespaceKeyword), Roles.NamespaceKeyword);
379
ConvertNamespaceName(nspace.RealMemberName, nDecl);
380
if (loc != null && loc.Count > 1) {
381
nDecl.AddChild(new CSharpTokenNode (Convert(loc [1]), Roles.LBrace), Roles.LBrace);
383
AddToNamespace(nDecl);
384
namespaceStack.Push(nDecl);
387
if (nspace.Usings != null) {
388
foreach (var us in nspace.Usings) {
393
if (nspace.Containers != null) {
394
foreach (var container in nspace.Containers) {
395
container.Accept(this);
400
AddAttributeSection(nDecl, nspace.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
401
if (loc != null && loc.Count > 2)
402
nDecl.AddChild (new CSharpTokenNode (Convert (loc [2]), Roles.RBrace), Roles.RBrace);
403
if (loc != null && loc.Count > 3)
404
nDecl.AddChild (new CSharpTokenNode (Convert (loc [3]), Roles.Semicolon), Roles.Semicolon);
406
namespaceStack.Pop ();
409
// public override void Visit (UsingsBag.Namespace nspace)
413
// VisitNamespaceUsings (nspace);
414
// VisitNamespaceBody (nspace);
418
void ConvertNamespaceName (MemberName memberName, NamespaceDeclaration namespaceDecl)
420
AstNode insertPos = null;
421
while (memberName != null) {
422
Identifier newIdent = Identifier.Create (memberName.Name, Convert (memberName.Location));
423
// HACK for a parser 'bug' - sometimes it generates "<invalid>" identifiers in namespace names (on certain bugs in the input file)
424
if (newIdent.Name != "<invalid>") {
425
namespaceDecl.InsertChildBefore (insertPos, newIdent, Roles.Identifier);
426
insertPos = newIdent;
428
if (!memberName.DotLocation.IsNull) {
429
var dotToken = new CSharpTokenNode (Convert (memberName.DotLocation), Roles.Dot);
430
namespaceDecl.InsertChildBefore (insertPos, dotToken, Roles.Dot);
431
insertPos = dotToken;
434
memberName = memberName.Left;
438
public override void Visit (UsingNamespace un)
440
var ud = new UsingDeclaration ();
441
var loc = LocationsBag.GetLocations (un);
442
ud.AddChild (new CSharpTokenNode (Convert (un.Location), UsingDeclaration.UsingKeywordRole), UsingDeclaration.UsingKeywordRole);
443
if (un.NamespaceExpression != null)
444
ud.AddChild (ConvertToType (un.NamespaceExpression), UsingDeclaration.ImportRole);
446
ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Semicolon), Roles.Semicolon);
450
public override void Visit (UsingAliasNamespace uan)
452
var ud = new UsingAliasDeclaration ();
453
var loc = LocationsBag.GetLocations (uan);
455
ud.AddChild (new CSharpTokenNode (Convert (uan.Location), UsingAliasDeclaration.UsingKeywordRole), UsingAliasDeclaration.UsingKeywordRole);
456
ud.AddChild (Identifier.Create (uan.Alias.Value, Convert (uan.Alias.Location)), UsingAliasDeclaration.AliasRole);
458
ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
459
if (uan.NamespaceExpression != null)
460
ud.AddChild (ConvertToType (uan.NamespaceExpression), UsingAliasDeclaration.ImportRole);
461
if (loc != null && loc.Count > 1)
462
ud.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Semicolon), Roles.Semicolon);
466
public override void Visit (UsingExternAlias uea)
468
var ud = new ExternAliasDeclaration ();
469
var loc = LocationsBag.GetLocations (uea);
470
ud.AddChild (new CSharpTokenNode (Convert (uea.Location), Roles.ExternKeyword), Roles.ExternKeyword);
472
ud.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.AliasKeyword), Roles.AliasKeyword);
473
ud.AddChild (Identifier.Create (uea.Alias.Value, Convert (uea.Alias.Location)), Roles.Identifier);
474
if (loc != null && loc.Count > 1)
475
ud.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Semicolon), Roles.Semicolon);
479
AstType ConvertImport (MemberName memberName)
481
if (memberName.Left != null) {
483
var t = new MemberType ();
484
// t.IsDoubleColon = memberName.IsDoubleColon;
485
t.AddChild (ConvertImport (memberName.Left), MemberType.TargetRole);
487
if (!memberName.DotLocation.IsNull)
488
t.AddChild (new CSharpTokenNode (Convert (memberName.DotLocation), Roles.Dot), Roles.Dot);
490
t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
491
AddTypeArguments (t, memberName);
494
SimpleType t = new SimpleType ();
495
t.AddChild (Identifier.Create (memberName.Name, Convert (memberName.Location)), Roles.Identifier);
496
AddTypeArguments (t, memberName);
501
public override void Visit (MemberCore member)
503
Console.WriteLine ("Unknown member:");
504
Console.WriteLine (member.GetType () + "-> Member {0}", member.GetSignatureForError ());
507
Stack<TypeDeclaration> typeStack = new Stack<TypeDeclaration> ();
509
public override void Visit(Class c)
511
var newType = new TypeDeclaration ();
512
newType.ClassType = ClassType.Class;
513
AddAttributeSection(newType, c);
514
var location = LocationsBag.GetMemberLocation(c);
515
AddModifiers(newType, location);
517
if (location != null && location.Count > 0)
518
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.ClassKeyword), Roles.ClassKeyword);
520
newType.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
521
AddTypeParameters (newType, c.MemberName);
523
if (c.TypeBaseExpressions != null) {
524
if (location != null && curLoc < location.Count)
525
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
527
var commaLocations = LocationsBag.GetLocations (c.TypeBaseExpressions);
529
foreach (var baseTypes in c.TypeBaseExpressions) {
530
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
531
if (commaLocations != null && i < commaLocations.Count) {
532
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
538
AddConstraints (newType, c.CurrentTypeParameters);
539
if (location != null && curLoc < location.Count)
540
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
541
typeStack.Push (newType);
543
AddAttributeSection (newType, c.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
545
if (location != null && curLoc < location.Count) {
546
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
548
if (location != null && curLoc < location.Count)
549
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
552
// parser error, set end node to max value.
553
newType.AddChild (new ErrorNode (), Roles.Error);
559
public override void Visit(Struct s)
561
var newType = new TypeDeclaration();
562
newType.ClassType = ClassType.Struct;
563
AddAttributeSection(newType, s);
564
var location = LocationsBag.GetMemberLocation(s);
565
AddModifiers(newType, location);
567
if (location != null && location.Count > 0)
568
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.StructKeyword), Roles.StructKeyword);
569
newType.AddChild (Identifier.Create (s.MemberName.Name, Convert (s.MemberName.Location)), Roles.Identifier);
570
AddTypeParameters (newType, s.MemberName);
572
if (s.TypeBaseExpressions != null) {
573
if (location != null && curLoc < location.Count)
574
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
575
var commaLocations = LocationsBag.GetLocations (s.TypeBaseExpressions);
577
foreach (var baseTypes in s.TypeBaseExpressions) {
578
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
579
if (commaLocations != null && i < commaLocations.Count) {
580
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
586
AddConstraints (newType, s.CurrentTypeParameters);
587
if (location != null && curLoc < location.Count)
588
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
589
typeStack.Push (newType);
591
if (location != null && location.Count > 2) {
592
if (location != null && curLoc < location.Count)
593
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
594
if (location != null && curLoc < location.Count)
595
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
597
// parser error, set end node to max value.
598
newType.AddChild (new ErrorNode (), Roles.Error);
604
public override void Visit(Interface i)
606
var newType = new TypeDeclaration();
607
newType.ClassType = ClassType.Interface;
608
AddAttributeSection(newType, i);
609
var location = LocationsBag.GetMemberLocation(i);
610
AddModifiers(newType, location);
612
if (location != null && location.Count > 0)
613
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.InterfaceKeyword), Roles.InterfaceKeyword);
614
newType.AddChild (Identifier.Create (i.MemberName.Name, Convert (i.MemberName.Location)), Roles.Identifier);
615
AddTypeParameters (newType, i.MemberName);
617
if (i.TypeBaseExpressions != null) {
618
if (location != null && curLoc < location.Count)
619
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Colon), Roles.Colon);
620
var commaLocations = LocationsBag.GetLocations (i.TypeBaseExpressions);
622
foreach (var baseTypes in i.TypeBaseExpressions) {
623
newType.AddChild (ConvertToType (baseTypes), Roles.BaseType);
624
if (commaLocations != null && j < commaLocations.Count) {
625
newType.AddChild (new CSharpTokenNode (Convert (commaLocations [j]), Roles.Comma), Roles.Comma);
631
AddConstraints (newType, i.CurrentTypeParameters);
632
if (location != null && curLoc < location.Count)
633
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.LBrace), Roles.LBrace);
634
typeStack.Push (newType);
636
if (location != null && location.Count > 2) {
637
if (location != null && curLoc < location.Count)
638
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
639
if (location != null && curLoc < location.Count)
640
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
642
// parser error, set end node to max value.
643
newType.AddChild (new ErrorNode (), Roles.Error);
649
public override void Visit(Mono.CSharp.Delegate d)
651
DelegateDeclaration newDelegate = new DelegateDeclaration ();
652
var location = LocationsBag.GetMemberLocation(d);
653
AddAttributeSection(newDelegate, d);
654
AddModifiers(newDelegate, location);
655
if (location != null && location.Count > 0) {
656
newDelegate.AddChild(new CSharpTokenNode (Convert(location [0]), Roles.DelegateKeyword), Roles.DelegateKeyword);
658
if (d.ReturnType != null)
659
newDelegate.AddChild (ConvertToType (d.ReturnType), Roles.Type);
660
newDelegate.AddChild (Identifier.Create (d.MemberName.Name, Convert (d.MemberName.Location)), Roles.Identifier);
661
AddTypeParameters (newDelegate, d.MemberName);
663
if (location != null && location.Count > 1)
664
newDelegate.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
665
AddParameter (newDelegate, d.Parameters);
667
if (location != null && location.Count > 2) {
668
newDelegate.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
670
AddConstraints (newDelegate, d.CurrentTypeParameters);
671
if (location != null && location.Count > 3) {
672
newDelegate.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.Semicolon), Roles.Semicolon);
674
AddType (newDelegate);
677
void AddType (EntityDeclaration child)
679
if (typeStack.Count > 0) {
680
typeStack.Peek ().AddChild (child, Roles.TypeMemberRole);
682
AddToNamespace (child);
686
void AddToNamespace (AstNode child)
688
if (namespaceStack.Count > 0) {
689
namespaceStack.Peek ().AddChild (child, NamespaceDeclaration.MemberRole);
691
unit.AddChild (child, SyntaxTree.MemberRole);
695
public override void Visit(Mono.CSharp.Enum e)
697
var newType = new TypeDeclaration();
698
newType.ClassType = ClassType.Enum;
699
AddAttributeSection(newType, e);
700
var location = LocationsBag.GetMemberLocation(e);
702
AddModifiers(newType, location);
704
if (location != null && location.Count > 0)
705
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.EnumKeyword), Roles.EnumKeyword);
706
newType.AddChild(Identifier.Create(e.MemberName.Name, Convert(e.MemberName.Location)), Roles.Identifier);
708
if (e.BaseTypeExpression != null) {
709
if (location != null && curLoc < location.Count)
710
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Colon), Roles.Colon);
711
newType.AddChild(ConvertToType(e.BaseTypeExpression), Roles.BaseType);
714
if (location != null && curLoc < location.Count)
715
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.LBrace), Roles.LBrace);
716
typeStack.Push(newType);
718
foreach (var m in e.Members) {
719
EnumMember member = m as EnumMember;
720
if (member == null) {
721
Console.WriteLine("WARNING - ENUM MEMBER: " + m);
725
if (location != null && curLoc < location.Count - 1) //last one is closing brace
726
newType.AddChild(new CSharpTokenNode(Convert(location [curLoc++]), Roles.Comma), Roles.Comma);
729
if (location != null && location.Count > 2) {
730
if (location != null && curLoc < location.Count)
731
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.RBrace), Roles.RBrace);
732
if (location != null && curLoc < location.Count)
733
newType.AddChild (new CSharpTokenNode (Convert (location [curLoc++]), Roles.Semicolon), Roles.Semicolon);
735
// parser error, set end node to max value.
736
newType.AddChild (new ErrorNode (), Roles.Error);
739
AddAttributeSection (newType, e.UnattachedAttributes, EntityDeclaration.UnattachedAttributeRole);
744
public override void Visit (EnumMember em)
746
EnumMemberDeclaration newField = new EnumMemberDeclaration ();
747
AddAttributeSection (newField, em);
748
newField.AddChild (Identifier.Create (em.Name, Convert (em.Location)), Roles.Identifier);
749
if (em.Initializer != null) {
750
newField.AddChild (new CSharpTokenNode (Convert (em.Initializer.Location), Roles.Assign), Roles.Assign);
751
newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole);
753
//Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation);
755
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
762
public override void Visit (FixedField f)
764
var location = LocationsBag.GetMemberLocation (f);
767
var newField = new FixedFieldDeclaration ();
768
AddAttributeSection (newField, f);
769
AddModifiers (newField, location);
770
if (location != null && location.Count > 0)
771
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), FixedFieldDeclaration.FixedKeywordRole), FixedFieldDeclaration.FixedKeywordRole);
773
if (f.TypeExpression != null)
774
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
776
var variable = new FixedVariableInitializer ();
777
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
778
if (f.Initializer != null && !f.Initializer.IsNull) {
779
var bracketLocations = LocationsBag.GetLocations (f.Initializer);
780
if (bracketLocations != null && bracketLocations.Count > 1)
781
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.LBracket), Roles.LBracket);
783
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
784
if (bracketLocations != null && bracketLocations.Count > 1)
785
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.RBracket), Roles.RBracket);
787
newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
789
if (f.Declarators != null) {
790
foreach (var decl in f.Declarators) {
791
var declLoc = LocationsBag.GetLocations (decl);
793
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
795
variable = new FixedVariableInitializer ();
796
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
797
if (!decl.Initializer.IsNull) {
798
var bracketLocations = LocationsBag.GetLocations (f.Initializer);
799
if (bracketLocations != null && bracketLocations.Count > 1)
800
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.LBracket), Roles.LBracket);
801
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
802
if (bracketLocations != null && bracketLocations.Count > 1)
803
variable.AddChild (new CSharpTokenNode (Convert (bracketLocations [0]), Roles.RBracket), Roles.RBracket);
805
newField.AddChild (variable, FixedFieldDeclaration.VariableRole);
808
if (location != null && location.Count > locationIdx)
809
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx]), Roles.Semicolon), Roles.Semicolon);
810
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
814
public override void Visit(Field f)
816
var location = LocationsBag.GetMemberLocation(f);
818
FieldDeclaration newField = new FieldDeclaration ();
819
AddAttributeSection (newField, f);
820
AddModifiers (newField, location);
821
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
823
VariableInitializer variable = new VariableInitializer ();
824
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
826
if (f.Initializer != null) {
827
if (location != null)
828
variable.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), Roles.Assign), Roles.Assign);
829
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
831
newField.AddChild (variable, Roles.Variable);
832
if (f.Declarators != null) {
833
foreach (var decl in f.Declarators) {
834
var declLoc = LocationsBag.GetLocations (decl);
836
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
838
variable = new VariableInitializer ();
839
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
840
if (decl.Initializer != null) {
842
variable.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
843
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
845
newField.AddChild (variable, Roles.Variable);
848
if (location != null &&location.Count > locationIdx)
849
newField.AddChild (new CSharpTokenNode (Convert (location [locationIdx++]), Roles.Semicolon), Roles.Semicolon);
851
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
854
public override void Visit (Const f)
856
var location = LocationsBag.GetMemberLocation (f);
858
FieldDeclaration newField = new FieldDeclaration ();
859
AddAttributeSection (newField, f);
860
AddModifiers (newField, location);
861
if (location != null)
862
newField.AddChild (new CSharpModifierToken (Convert (location [0]), Modifiers.Const), EntityDeclaration.ModifierRole);
863
newField.AddChild (ConvertToType (f.TypeExpression), Roles.Type);
865
VariableInitializer variable = new VariableInitializer ();
866
variable.AddChild (Identifier.Create (f.MemberName.Name, Convert (f.MemberName.Location)), Roles.Identifier);
868
if (f.Initializer != null) {
869
variable.AddChild (new CSharpTokenNode (Convert (f.Initializer.Location), Roles.Assign), Roles.Assign);
870
variable.AddChild ((Expression)f.Initializer.Accept (this), Roles.Expression);
872
newField.AddChild (variable, Roles.Variable);
873
if (f.Declarators != null) {
874
foreach (var decl in f.Declarators) {
875
var declLoc = LocationsBag.GetLocations (decl);
877
newField.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
879
variable = new VariableInitializer ();
880
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
881
if (decl.Initializer != null) {
882
variable.AddChild (new CSharpTokenNode (Convert (decl.Initializer.Location), Roles.Assign), Roles.Assign);
883
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
885
newField.AddChild (variable, Roles.Variable);
888
if (location != null)
889
newField.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
891
typeStack.Peek ().AddChild (newField, Roles.TypeMemberRole);
896
public override void Visit (Operator o)
898
OperatorDeclaration newOperator = new OperatorDeclaration ();
899
newOperator.OperatorType = (OperatorType)o.OperatorType;
901
var location = LocationsBag.GetMemberLocation (o);
902
AddAttributeSection (newOperator, o);
903
AddModifiers (newOperator, location);
906
if (o.OperatorType == Operator.OpType.Implicit) {
907
if (location != null && location.Count > 0) {
908
newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.ImplicitRole), OperatorDeclaration.ImplicitRole);
909
if (location.Count > 1)
910
newOperator.AddChild (new CSharpTokenNode (Convert (location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
912
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
913
} else if (o.OperatorType == Operator.OpType.Explicit) {
914
if (location != null && location.Count > 0) {
915
newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.ExplicitRole), OperatorDeclaration.ExplicitRole);
916
if (location.Count > 1)
917
newOperator.AddChild (new CSharpTokenNode (Convert (location [1]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
919
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
921
newOperator.AddChild (ConvertToType (o.TypeExpression), Roles.Type);
923
if (location != null && location.Count > 0)
924
newOperator.AddChild (new CSharpTokenNode (Convert (location [0]), OperatorDeclaration.OperatorKeywordRole), OperatorDeclaration.OperatorKeywordRole);
926
if (location != null && location.Count > 1) {
927
var r = OperatorDeclaration.GetRole(newOperator.OperatorType);
928
newOperator.AddChild(new CSharpTokenNode(Convert(location [1]), r), r);
931
if (location != null && location.Count > 2)
932
newOperator.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LPar), Roles.LPar);
933
AddParameter (newOperator, o.ParameterInfo);
934
if (location != null && location.Count > 3)
935
newOperator.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RPar), Roles.RPar);
937
if (o.Block != null) {
938
newOperator.AddChild ((BlockStatement)o.Block.Accept (this), Roles.Body);
940
if (location != null && location.Count >= 5)
941
newOperator.AddChild (new CSharpTokenNode (Convert (location [4]), Roles.Semicolon), Roles.Semicolon);
943
typeStack.Peek ().AddChild (newOperator, Roles.TypeMemberRole);
946
public void AddAttributeSection (AstNode parent, Attributable a)
948
if (a == null || a.OptAttributes == null)
950
AddAttributeSection (parent, a.OptAttributes);
953
public void AddAttributeSection (AstNode parent, Attributes attrs, Role<AttributeSection> role)
957
foreach (var attr in attrs.Sections) {
958
parent.AddChild (ConvertAttributeSection (attr), role);
962
public void AddAttributeSection (AstNode parent, Attributes attrs)
964
AddAttributeSection (parent, attrs, EntityDeclaration.AttributeRole);
967
public override void Visit (Indexer indexer)
969
IndexerDeclaration newIndexer = new IndexerDeclaration ();
970
AddAttributeSection (newIndexer, indexer);
971
var location = LocationsBag.GetMemberLocation (indexer);
972
AddModifiers (newIndexer, location);
973
newIndexer.AddChild (ConvertToType (indexer.TypeExpression), Roles.Type);
974
AddExplicitInterface (newIndexer, indexer.MemberName);
975
var name = indexer.MemberName;
976
newIndexer.AddChild (new CSharpTokenNode(Convert (name.Location), IndexerDeclaration.ThisKeywordRole), IndexerDeclaration.ThisKeywordRole);
978
if (location != null && location.Count > 0)
979
newIndexer.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBracket), Roles.LBracket);
980
AddParameter (newIndexer, indexer.ParameterInfo);
981
if (location != null && location.Count > 1)
982
newIndexer.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBracket), Roles.RBracket);
984
if (location != null && location.Count > 2)
985
newIndexer.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
986
if (indexer.Get != null) {
987
Accessor getAccessor = new Accessor ();
988
var getLocation = LocationsBag.GetMemberLocation (indexer.Get);
989
AddAttributeSection (getAccessor, indexer.Get);
990
AddModifiers (getAccessor, getLocation);
991
if (getLocation != null)
992
getAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Get.Location), PropertyDeclaration.GetKeywordRole), PropertyDeclaration.GetKeywordRole);
993
if (indexer.Get.Block != null) {
994
getAccessor.AddChild ((BlockStatement)indexer.Get.Block.Accept (this), Roles.Body);
996
if (getLocation != null && getLocation.Count > 0)
997
newIndexer.AddChild (new CSharpTokenNode (Convert (getLocation [0]), Roles.Semicolon), Roles.Semicolon);
999
newIndexer.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1002
if (indexer.Set != null) {
1003
Accessor setAccessor = new Accessor ();
1004
var setLocation = LocationsBag.GetMemberLocation (indexer.Set);
1005
AddAttributeSection (setAccessor, indexer.Set);
1006
AddModifiers (setAccessor, setLocation);
1007
if (setLocation != null)
1008
setAccessor.AddChild (new CSharpTokenNode (Convert (indexer.Set.Location), PropertyDeclaration.SetKeywordRole), PropertyDeclaration.SetKeywordRole);
1010
if (indexer.Set.Block != null) {
1011
setAccessor.AddChild ((BlockStatement)indexer.Set.Block.Accept (this), Roles.Body);
1013
if (setLocation != null && setLocation.Count > 0)
1014
newIndexer.AddChild (new CSharpTokenNode (Convert (setLocation [0]), Roles.Semicolon), Roles.Semicolon);
1016
newIndexer.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1019
if (location != null) {
1020
if (location.Count > 3)
1021
newIndexer.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
1023
// parser error, set end node to max value.
1024
newIndexer.AddChild (new ErrorNode (), Roles.Error);
1026
typeStack.Peek ().AddChild (newIndexer, Roles.TypeMemberRole);
1029
public override void Visit (Method m)
1031
MethodDeclaration newMethod = new MethodDeclaration ();
1032
AddAttributeSection (newMethod, m);
1033
var location = LocationsBag.GetMemberLocation (m);
1034
AddModifiers (newMethod, location);
1035
newMethod.AddChild (ConvertToType (m.TypeExpression), Roles.Type);
1036
AddExplicitInterface (newMethod, m.MethodName);
1037
newMethod.AddChild (Identifier.Create (m.MethodName.Name, Convert (m.Location)), Roles.Identifier);
1039
AddTypeParameters (newMethod, m.MemberName);
1041
if (location != null && location.Count > 0)
1042
newMethod.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1043
AddParameter (newMethod, m.ParameterInfo);
1045
if (location != null && location.Count > 1)
1046
newMethod.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1048
AddConstraints (newMethod, m.CurrentTypeParameters);
1050
if (m.Block != null) {
1051
var bodyBlock = (BlockStatement)m.Block.Accept (this);
1052
// if (m.Block is ToplevelBlock) {
1053
// newMethod.AddChild (bodyBlock.FirstChild.NextSibling, Roles.Body);
1055
newMethod.AddChild (bodyBlock, Roles.Body);
1057
} else if (location != null) {
1058
if (location.Count < 3) {
1059
// parser error, set end node to max value.
1060
newMethod.AddChild (new ErrorNode (), Roles.Error);
1062
newMethod.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.Semicolon), Roles.Semicolon);
1065
typeStack.Peek ().AddChild (newMethod, Roles.TypeMemberRole);
1068
static Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> modifierTable = new Dictionary<Mono.CSharp.Modifiers, ICSharpCode.NRefactory.CSharp.Modifiers> ();
1069
static string[] keywordTable;
1071
static ConversionVisitor ()
1073
modifierTable [Mono.CSharp.Modifiers.NEW] = ICSharpCode.NRefactory.CSharp.Modifiers.New;
1074
modifierTable [Mono.CSharp.Modifiers.PUBLIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Public;
1075
modifierTable [Mono.CSharp.Modifiers.PROTECTED] = ICSharpCode.NRefactory.CSharp.Modifiers.Protected;
1076
modifierTable [Mono.CSharp.Modifiers.PRIVATE] = ICSharpCode.NRefactory.CSharp.Modifiers.Private;
1077
modifierTable [Mono.CSharp.Modifiers.INTERNAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Internal;
1078
modifierTable [Mono.CSharp.Modifiers.ABSTRACT] = ICSharpCode.NRefactory.CSharp.Modifiers.Abstract;
1079
modifierTable [Mono.CSharp.Modifiers.VIRTUAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Virtual;
1080
modifierTable [Mono.CSharp.Modifiers.SEALED] = ICSharpCode.NRefactory.CSharp.Modifiers.Sealed;
1081
modifierTable [Mono.CSharp.Modifiers.STATIC] = ICSharpCode.NRefactory.CSharp.Modifiers.Static;
1082
modifierTable [Mono.CSharp.Modifiers.OVERRIDE] = ICSharpCode.NRefactory.CSharp.Modifiers.Override;
1083
modifierTable [Mono.CSharp.Modifiers.READONLY] = ICSharpCode.NRefactory.CSharp.Modifiers.Readonly;
1084
modifierTable [Mono.CSharp.Modifiers.PARTIAL] = ICSharpCode.NRefactory.CSharp.Modifiers.Partial;
1085
modifierTable [Mono.CSharp.Modifiers.EXTERN] = ICSharpCode.NRefactory.CSharp.Modifiers.Extern;
1086
modifierTable [Mono.CSharp.Modifiers.VOLATILE] = ICSharpCode.NRefactory.CSharp.Modifiers.Volatile;
1087
modifierTable [Mono.CSharp.Modifiers.UNSAFE] = ICSharpCode.NRefactory.CSharp.Modifiers.Unsafe;
1088
modifierTable [Mono.CSharp.Modifiers.ASYNC] = ICSharpCode.NRefactory.CSharp.Modifiers.Async;
1090
keywordTable = new string[255];
1091
for (int i = 0; i< keywordTable.Length; i++)
1092
keywordTable [i] = "unknown";
1094
keywordTable [(int)BuiltinTypeSpec.Type.Other] = "void";
1095
keywordTable [(int)BuiltinTypeSpec.Type.String] = "string";
1096
keywordTable [(int)BuiltinTypeSpec.Type.Int] = "int";
1097
keywordTable [(int)BuiltinTypeSpec.Type.Object] = "object";
1098
keywordTable [(int)BuiltinTypeSpec.Type.Float] = "float";
1099
keywordTable [(int)BuiltinTypeSpec.Type.Double] = "double";
1100
keywordTable [(int)BuiltinTypeSpec.Type.Long] = "long";
1101
keywordTable [(int)BuiltinTypeSpec.Type.Byte] = "byte";
1102
keywordTable [(int)BuiltinTypeSpec.Type.UInt] = "uint";
1103
keywordTable [(int)BuiltinTypeSpec.Type.ULong] = "ulong";
1104
keywordTable [(int)BuiltinTypeSpec.Type.Short] = "short";
1105
keywordTable [(int)BuiltinTypeSpec.Type.UShort] = "ushort";
1106
keywordTable [(int)BuiltinTypeSpec.Type.SByte] = "sbyte";
1107
keywordTable [(int)BuiltinTypeSpec.Type.Decimal] = "decimal";
1108
keywordTable [(int)BuiltinTypeSpec.Type.Char] = "char";
1109
keywordTable [(int)BuiltinTypeSpec.Type.Bool] = "bool";
1112
void AddModifiers (EntityDeclaration parent, LocationsBag.MemberLocations location)
1114
if (location == null || location.Modifiers == null)
1116
foreach (var modifier in location.Modifiers) {
1117
ICSharpCode.NRefactory.CSharp.Modifiers mod;
1118
if (!modifierTable.TryGetValue (modifier.Item1, out mod)) {
1119
Console.WriteLine ("modifier " + modifier.Item1 + " can't be converted,");
1122
parent.AddChild (new CSharpModifierToken (Convert (modifier.Item2), mod), EntityDeclaration.ModifierRole);
1126
public override void Visit (Property p)
1128
PropertyDeclaration newProperty = new PropertyDeclaration ();
1129
AddAttributeSection (newProperty, p);
1130
var location = LocationsBag.GetMemberLocation (p);
1131
AddModifiers (newProperty, location);
1132
newProperty.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
1133
AddExplicitInterface (newProperty, p.MemberName);
1134
newProperty.AddChild (Identifier.Create (p.MemberName.Name, Convert (p.Location)), Roles.Identifier);
1136
if (location != null && location.Count > 0)
1137
newProperty.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBrace), Roles.LBrace);
1139
Accessor getAccessor = null;
1140
if (p.Get != null) {
1141
getAccessor = new Accessor ();
1142
AddAttributeSection (getAccessor, p.Get);
1143
var getLocation = LocationsBag.GetMemberLocation (p.Get);
1144
AddModifiers (getAccessor, getLocation);
1145
getAccessor.AddChild (new CSharpTokenNode (Convert (p.Get.Location), PropertyDeclaration.GetKeywordRole), PropertyDeclaration.GetKeywordRole);
1147
if (p.Get.Block != null) {
1148
getAccessor.AddChild ((BlockStatement)p.Get.Block.Accept (this), Roles.Body);
1150
if (getLocation != null && getLocation.Count > 0)
1151
getAccessor.AddChild (new CSharpTokenNode (Convert (getLocation [0]), Roles.Semicolon), Roles.Semicolon);
1155
Accessor setAccessor = null;
1156
if (p.Set != null) {
1157
setAccessor = new Accessor ();
1158
AddAttributeSection (setAccessor, p.Set);
1159
var setLocation = LocationsBag.GetMemberLocation (p.Set);
1160
AddModifiers (setAccessor, setLocation);
1161
setAccessor.AddChild (new CSharpTokenNode (Convert (p.Set.Location), PropertyDeclaration.SetKeywordRole), PropertyDeclaration.SetKeywordRole);
1163
if (p.Set.Block != null) {
1164
setAccessor.AddChild ((BlockStatement)p.Set.Block.Accept (this), Roles.Body);
1166
if (setLocation != null && setLocation.Count > 0)
1167
setAccessor.AddChild (new CSharpTokenNode (Convert (setLocation [0]), Roles.Semicolon), Roles.Semicolon);
1170
if (getAccessor != null && setAccessor != null) {
1171
if (getAccessor.StartLocation < setAccessor.StartLocation) {
1172
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1173
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1175
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1176
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1179
if (getAccessor != null)
1180
newProperty.AddChild (getAccessor, PropertyDeclaration.GetterRole);
1181
if (setAccessor != null)
1182
newProperty.AddChild (setAccessor, PropertyDeclaration.SetterRole);
1185
if (location != null && location.Count > 1) {
1186
newProperty.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBrace), Roles.RBrace);
1188
// parser error, set end node to max value.
1189
newProperty.AddChild (new ErrorNode (), Roles.Error);
1192
typeStack.Peek ().AddChild (newProperty, Roles.TypeMemberRole);
1195
public override void Visit (Constructor c)
1197
ConstructorDeclaration newConstructor = new ConstructorDeclaration ();
1198
AddAttributeSection (newConstructor, c);
1199
var location = LocationsBag.GetMemberLocation (c);
1200
AddModifiers (newConstructor, location);
1201
newConstructor.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), Roles.Identifier);
1202
if (location != null && location.Count > 0)
1203
newConstructor.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1205
AddParameter (newConstructor, c.ParameterInfo);
1206
if (location != null && location.Count > 1)
1207
newConstructor.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1209
if (c.Initializer != null) {
1210
var initializer = new ConstructorInitializer ();
1211
initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This;
1212
var initializerLocation = LocationsBag.GetLocations (c.Initializer);
1214
if (initializerLocation != null)
1215
newConstructor.AddChild (new CSharpTokenNode (Convert (initializerLocation [0]), Roles.Colon), Roles.Colon);
1217
if (initializerLocation != null && initializerLocation.Count > 1) {
1218
// this and base has the same length
1219
var r = initializer.ConstructorInitializerType == ConstructorInitializerType.This ? ConstructorInitializer.ThisKeywordRole : ConstructorInitializer.BaseKeywordRole;
1220
initializer.AddChild (new CSharpTokenNode (Convert (c.Initializer.Location), r), r);
1221
initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [1]), Roles.LPar), Roles.LPar);
1222
AddArguments (initializer, LocationsBag.GetLocations (c.Initializer.Arguments), c.Initializer.Arguments);
1223
initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation [2]), Roles.RPar), Roles.RPar);
1224
newConstructor.AddChild (initializer, ConstructorDeclaration.InitializerRole);
1228
if (c.Block != null)
1229
newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), Roles.Body);
1230
typeStack.Peek ().AddChild (newConstructor, Roles.TypeMemberRole);
1233
public override void Visit (Destructor d)
1235
DestructorDeclaration newDestructor = new DestructorDeclaration ();
1236
AddAttributeSection (newDestructor, d);
1237
var location = LocationsBag.GetMemberLocation (d);
1238
AddModifiers (newDestructor, location);
1239
if (location != null && location.Count > 0)
1240
newDestructor.AddChild (new CSharpTokenNode (Convert (location [0]), DestructorDeclaration.TildeRole), DestructorDeclaration.TildeRole);
1241
newDestructor.AddChild (Identifier.Create (d.Identifier, Convert (d.MemberName.Location)), Roles.Identifier);
1243
if (location != null && location.Count > 1) {
1244
newDestructor.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
1246
if (location.Count > 2)
1247
newDestructor.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
1250
if (d.Block != null)
1251
newDestructor.AddChild ((BlockStatement)d.Block.Accept (this), Roles.Body);
1253
typeStack.Peek ().AddChild (newDestructor, Roles.TypeMemberRole);
1256
public override void Visit (EventField e)
1258
EventDeclaration newEvent = new EventDeclaration ();
1259
AddAttributeSection (newEvent, e);
1260
var location = LocationsBag.GetMemberLocation (e);
1262
AddModifiers (newEvent, location);
1264
if (location != null && location.Count > 0)
1265
newEvent.AddChild (new CSharpTokenNode (Convert (location [l++]), EventDeclaration.EventKeywordRole), EventDeclaration.EventKeywordRole);
1266
newEvent.AddChild (ConvertToType (e.TypeExpression), Roles.Type);
1268
VariableInitializer variable = new VariableInitializer ();
1269
variable.AddChild (Identifier.Create (e.MemberName.Name, Convert (e.MemberName.Location)), Roles.Identifier);
1271
if (e.Initializer != null) {
1272
if (location != null && location.Count > l)
1273
variable.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.Assign), Roles.Assign);
1274
variable.AddChild ((Expression)e.Initializer.Accept (this), Roles.Expression);
1276
newEvent.AddChild (variable, Roles.Variable);
1277
if (e.Declarators != null) {
1278
foreach (var decl in e.Declarators) {
1279
var declLoc = LocationsBag.GetLocations (decl);
1280
if (declLoc != null)
1281
newEvent.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
1283
variable = new VariableInitializer ();
1284
variable.AddChild (Identifier.Create (decl.Name.Value, Convert (decl.Name.Location)), Roles.Identifier);
1286
if (decl.Initializer != null) {
1287
if (declLoc != null)
1288
variable.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
1289
variable.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1291
newEvent.AddChild (variable, Roles.Variable);
1295
if (location != null && location.Count > l)
1296
newEvent.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.Semicolon), Roles.Semicolon);
1298
typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1301
void AddExplicitInterface (AstNode parent, MemberName memberName)
1303
if (memberName == null || memberName.ExplicitInterface == null)
1306
parent.AddChild (ConvertToType (memberName.ExplicitInterface), EntityDeclaration.PrivateImplementationTypeRole);
1307
var privateImplTypeLoc = LocationsBag.GetLocations (memberName.ExplicitInterface);
1308
if (privateImplTypeLoc != null)
1309
parent.AddChild (new CSharpTokenNode (Convert (privateImplTypeLoc [0]), Roles.Dot), Roles.Dot);
1312
public override void Visit (EventProperty ep)
1314
CustomEventDeclaration newEvent = new CustomEventDeclaration ();
1315
AddAttributeSection (newEvent, ep);
1316
var location = LocationsBag.GetMemberLocation (ep);
1317
AddModifiers (newEvent, location);
1319
if (location != null && location.Count > 0)
1320
newEvent.AddChild (new CSharpTokenNode (Convert (location [0]), CustomEventDeclaration.EventKeywordRole), CustomEventDeclaration.EventKeywordRole);
1321
newEvent.AddChild (ConvertToType (ep.TypeExpression), Roles.Type);
1323
AddExplicitInterface (newEvent, ep.MemberName);
1325
newEvent.AddChild (Identifier.Create (ep.MemberName.Name, Convert (ep.Location)), Roles.Identifier);
1327
if (location != null && location.Count >= 2)
1328
newEvent.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LBrace), Roles.LBrace);
1330
if (ep.Add != null) {
1331
Accessor addAccessor = new Accessor ();
1332
AddAttributeSection (addAccessor, ep.Add);
1333
var addLocation = LocationsBag.GetMemberLocation (ep.Add);
1334
AddModifiers (addAccessor, addLocation);
1335
addAccessor.AddChild (new CSharpTokenNode (Convert (ep.Add.Location), CustomEventDeclaration.AddKeywordRole), CustomEventDeclaration.AddKeywordRole);
1336
if (ep.Add.Block != null)
1337
addAccessor.AddChild ((BlockStatement)ep.Add.Block.Accept (this), Roles.Body);
1338
newEvent.AddChild (addAccessor, CustomEventDeclaration.AddAccessorRole);
1341
if (ep.Remove != null) {
1342
Accessor removeAccessor = new Accessor ();
1343
AddAttributeSection (removeAccessor, ep.Remove);
1344
var removeLocation = LocationsBag.GetMemberLocation (ep.Remove);
1345
AddModifiers (removeAccessor, removeLocation);
1346
removeAccessor.AddChild (new CSharpTokenNode (Convert (ep.Remove.Location), CustomEventDeclaration.RemoveKeywordRole), CustomEventDeclaration.RemoveKeywordRole);
1348
if (ep.Remove.Block != null)
1349
removeAccessor.AddChild ((BlockStatement)ep.Remove.Block.Accept (this), Roles.Body);
1350
newEvent.AddChild (removeAccessor, CustomEventDeclaration.RemoveAccessorRole);
1352
if (location != null && location.Count >= 3) {
1353
newEvent.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RBrace), Roles.RBrace);
1355
// parser error, set end node to max value.
1356
newEvent.AddChild (new ErrorNode (), Roles.Error);
1359
typeStack.Peek ().AddChild (newEvent, Roles.TypeMemberRole);
1365
public override object Visit (Mono.CSharp.Statement stmt)
1367
Console.WriteLine ("unknown statement:" + stmt);
1371
public override object Visit (BlockVariableDeclaration blockVariableDeclaration)
1373
var result = new VariableDeclarationStatement ();
1374
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1376
var varInit = new VariableInitializer ();
1377
var location = LocationsBag.GetLocations (blockVariableDeclaration);
1378
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1379
if (blockVariableDeclaration.Initializer != null) {
1380
if (location != null && location.Count > 0)
1381
varInit.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Assign), Roles.Assign);
1382
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1385
result.AddChild (varInit, Roles.Variable);
1387
if (blockVariableDeclaration.Declarators != null) {
1388
foreach (var decl in blockVariableDeclaration.Declarators) {
1389
var loc = LocationsBag.GetLocations (decl);
1390
var init = new VariableInitializer ();
1391
if (loc != null && loc.Count > 0)
1392
result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
1393
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1394
if (decl.Initializer != null) {
1395
if (loc != null && loc.Count > 1)
1396
init.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Assign), Roles.Assign);
1397
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1400
result.AddChild (init, Roles.Variable);
1403
if (location != null && (blockVariableDeclaration.Initializer == null || location.Count > 1))
1404
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.Semicolon), Roles.Semicolon);
1408
public override object Visit (BlockConstantDeclaration blockVariableDeclaration)
1410
var result = new VariableDeclarationStatement ();
1412
var location = LocationsBag.GetLocations (blockVariableDeclaration);
1413
if (location != null && location.Count > 0)
1414
result.AddChild (new CSharpModifierToken (Convert (location [0]), ICSharpCode.NRefactory.CSharp.Modifiers.Const), VariableDeclarationStatement.ModifierRole);
1416
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1418
var varInit = new VariableInitializer ();
1419
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1420
if (blockVariableDeclaration.Initializer != null) {
1421
if (location != null && location.Count > 1)
1422
varInit.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Assign), Roles.Assign);
1423
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1426
result.AddChild (varInit, Roles.Variable);
1428
if (blockVariableDeclaration.Declarators != null) {
1429
foreach (var decl in blockVariableDeclaration.Declarators) {
1430
var loc = LocationsBag.GetLocations (decl);
1431
var init = new VariableInitializer ();
1432
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1433
if (decl.Initializer != null) {
1435
init.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
1436
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1437
if (loc != null && loc.Count > 1)
1438
result.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Comma), Roles.Comma);
1440
if (loc != null && loc.Count > 0)
1441
result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
1443
result.AddChild (init, Roles.Variable);
1446
if (location != null) {
1447
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.Semicolon), Roles.Semicolon);
1449
// parser error, set end node to max value.
1450
result.AddChild (new ErrorNode (), Roles.Error);
1455
public override object Visit (Mono.CSharp.EmptyStatement emptyStatement)
1457
var result = new EmptyStatement ();
1458
result.Location = Convert (emptyStatement.loc);
1462
public override object Visit (Mono.CSharp.EmptyExpression emptyExpression)
1464
return new ICSharpCode.NRefactory.CSharp.EmptyExpression (Convert (emptyExpression.Location));
1467
public override object Visit (Mono.CSharp.ErrorExpression emptyExpression)
1469
return new ICSharpCode.NRefactory.CSharp.ErrorExpression (Convert (emptyExpression.Location));
1472
public override object Visit (EmptyExpressionStatement emptyExpressionStatement)
1474
return new EmptyExpression (Convert (emptyExpressionStatement.Location));
1477
public override object Visit (If ifStatement)
1479
var result = new IfElseStatement ();
1481
var location = LocationsBag.GetLocations (ifStatement);
1483
result.AddChild (new CSharpTokenNode (Convert (ifStatement.loc), IfElseStatement.IfKeywordRole), IfElseStatement.IfKeywordRole);
1484
if (location != null)
1485
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1486
if (ifStatement.Expr != null)
1487
result.AddChild ((Expression)ifStatement.Expr.Accept (this), Roles.Condition);
1488
if (location != null && location.Count > 1)
1489
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1491
if (ifStatement.TrueStatement != null)
1492
result.AddChild ((Statement)ifStatement.TrueStatement.Accept (this), IfElseStatement.TrueRole);
1494
if (ifStatement.FalseStatement != null) {
1495
if (location != null && location.Count > 2)
1496
result.AddChild (new CSharpTokenNode (Convert (location [2]), IfElseStatement.ElseKeywordRole), IfElseStatement.ElseKeywordRole);
1497
result.AddChild ((Statement)ifStatement.FalseStatement.Accept (this), IfElseStatement.FalseRole);
1503
public override object Visit (Do doStatement)
1505
var result = new DoWhileStatement ();
1506
var location = LocationsBag.GetLocations (doStatement);
1507
result.AddChild (new CSharpTokenNode (Convert (doStatement.loc), DoWhileStatement.DoKeywordRole), DoWhileStatement.DoKeywordRole);
1508
if (doStatement.EmbeddedStatement != null)
1509
result.AddChild ((Statement)doStatement.EmbeddedStatement.Accept (this), Roles.EmbeddedStatement);
1510
if (location != null)
1511
result.AddChild (new CSharpTokenNode (Convert (location [0]), DoWhileStatement.WhileKeywordRole), DoWhileStatement.WhileKeywordRole);
1512
if (location != null && location.Count > 1)
1513
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LPar), Roles.LPar);
1514
if (doStatement.expr != null)
1515
result.AddChild ((Expression)doStatement.expr.Accept (this), Roles.Condition);
1516
if (location != null && location.Count > 2) {
1517
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
1518
if (location.Count > 3)
1519
result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.Semicolon), Roles.Semicolon);
1525
public override object Visit (While whileStatement)
1527
var result = new WhileStatement ();
1528
var location = LocationsBag.GetLocations (whileStatement);
1529
result.AddChild (new CSharpTokenNode (Convert (whileStatement.loc), WhileStatement.WhileKeywordRole), WhileStatement.WhileKeywordRole);
1531
if (location != null)
1532
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1533
if (whileStatement.expr != null)
1534
result.AddChild ((Expression)whileStatement.expr.Accept (this), Roles.Condition);
1535
if (location != null && location.Count > 1)
1536
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1537
if (whileStatement.Statement != null)
1538
result.AddChild ((Statement)whileStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1542
void AddStatementOrList (ForStatement forStatement, Mono.CSharp.Statement init, Role<Statement> role)
1546
if (init is StatementList) {
1547
foreach (var stmt in ((StatementList)init).Statements) {
1548
forStatement.AddChild ((Statement)stmt.Accept (this), role);
1550
} else if (init is Mono.CSharp.EmptyStatement) {
1553
forStatement.AddChild ((Statement)init.Accept (this), role);
1557
public override object Visit (For forStatement)
1559
var result = new ForStatement ();
1561
var location = LocationsBag.GetLocations (forStatement);
1563
result.AddChild (new CSharpTokenNode (Convert (forStatement.loc), ForStatement.ForKeywordRole), ForStatement.ForKeywordRole);
1564
if (location != null)
1565
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1567
AddStatementOrList (result, forStatement.Initializer, ForStatement.InitializerRole);
1569
if (location != null && location.Count > 1)
1570
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
1571
if (forStatement.Condition != null)
1572
result.AddChild ((Expression)forStatement.Condition.Accept (this), Roles.Condition);
1573
if (location != null && location.Count >= 3)
1574
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.Semicolon), Roles.Semicolon);
1576
AddStatementOrList (result, forStatement.Iterator, ForStatement.IteratorRole);
1578
if (location != null && location.Count >= 4)
1579
result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RPar), Roles.RPar);
1581
if (forStatement.Statement != null)
1582
result.AddChild ((Statement)forStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1587
public override object Visit (StatementExpression statementExpression)
1589
var result = new ExpressionStatement ();
1590
var expr = statementExpression.Expr.Accept (this) as Expression;
1592
result.AddChild ((Expression)expr, Roles.Expression);
1593
var location = LocationsBag.GetLocations (statementExpression);
1594
if (location != null)
1595
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1599
public override object Visit (StatementErrorExpression statementErrorExpression)
1601
var result = new ExpressionStatement ();
1602
var expr = statementErrorExpression.Expr.Accept (this) as Expression;
1604
result.AddChild ((Expression)expr, Roles.Expression);
1605
var location = LocationsBag.GetLocations (statementErrorExpression);
1606
if (location != null)
1607
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1611
public override object Visit (InvalidStatementExpression statementExpression)
1613
var result = new ExpressionStatement ();
1614
if (statementExpression.Expression == null)
1616
var expr = statementExpression.Expression.Accept (this) as Expression;
1618
result.AddChild (expr, Roles.Expression);
1619
var location = LocationsBag.GetLocations (statementExpression);
1620
if (location != null)
1621
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1625
public override object Visit (Return returnStatement)
1627
var result = new ReturnStatement ();
1629
result.AddChild (new CSharpTokenNode (Convert (returnStatement.loc), ReturnStatement.ReturnKeywordRole), ReturnStatement.ReturnKeywordRole);
1630
if (returnStatement.Expr != null)
1631
result.AddChild ((Expression)returnStatement.Expr.Accept (this), Roles.Expression);
1633
var location = LocationsBag.GetLocations (returnStatement);
1634
if (location != null)
1635
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1640
public override object Visit (Goto gotoStatement)
1642
var result = new GotoStatement ();
1643
var location = LocationsBag.GetLocations (gotoStatement);
1644
result.AddChild (new CSharpTokenNode (Convert (gotoStatement.loc), GotoStatement.GotoKeywordRole), GotoStatement.GotoKeywordRole);
1645
var loc = location != null ? Convert (location [0]) : TextLocation.Empty;
1646
result.AddChild (Identifier.Create (gotoStatement.Target, loc), Roles.Identifier);
1647
if (location != null && location.Count > 1)
1648
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
1653
public override object Visit (LabeledStatement labeledStatement)
1655
var result = new LabelStatement ();
1656
result.AddChild (Identifier.Create (labeledStatement.Name, Convert (labeledStatement.loc)), Roles.Identifier);
1657
var location = LocationsBag.GetLocations (labeledStatement);
1658
if (location != null)
1659
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Colon), Roles.Colon);
1663
public override object Visit (GotoDefault gotoDefault)
1665
var result = new GotoDefaultStatement ();
1666
result.AddChild (new CSharpTokenNode (Convert (gotoDefault.loc), GotoDefaultStatement.GotoKeywordRole), GotoDefaultStatement.GotoKeywordRole);
1667
var location = LocationsBag.GetLocations (gotoDefault);
1668
if (location != null) {
1669
result.AddChild (new CSharpTokenNode (Convert (location [0]), GotoDefaultStatement.DefaultKeywordRole), GotoDefaultStatement.DefaultKeywordRole);
1670
if (location.Count > 1)
1671
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
1677
public override object Visit (GotoCase gotoCase)
1679
var result = new GotoCaseStatement ();
1680
result.AddChild (new CSharpTokenNode (Convert (gotoCase.loc), GotoCaseStatement.GotoKeywordRole), GotoCaseStatement.GotoKeywordRole);
1682
var location = LocationsBag.GetLocations (gotoCase);
1683
if (location != null)
1684
result.AddChild (new CSharpTokenNode (Convert (location [0]), GotoCaseStatement.CaseKeywordRole), GotoCaseStatement.CaseKeywordRole);
1685
if (gotoCase.Expr != null)
1686
result.AddChild ((Expression)gotoCase.Expr.Accept (this), Roles.Expression);
1687
if (location != null && location.Count > 1)
1688
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
1692
public override object Visit (Throw throwStatement)
1694
var result = new ThrowStatement ();
1695
var location = LocationsBag.GetLocations (throwStatement);
1697
result.AddChild (new CSharpTokenNode (Convert (throwStatement.loc), ThrowStatement.ThrowKeywordRole), ThrowStatement.ThrowKeywordRole);
1698
if (throwStatement.Expr != null)
1699
result.AddChild ((Expression)throwStatement.Expr.Accept (this), Roles.Expression);
1700
if (location != null)
1701
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1705
public override object Visit (Break breakStatement)
1707
var result = new BreakStatement ();
1708
var location = LocationsBag.GetLocations (breakStatement);
1710
result.AddChild (new CSharpTokenNode (Convert (breakStatement.loc), BreakStatement.BreakKeywordRole), BreakStatement.BreakKeywordRole);
1711
if (location != null)
1712
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1716
public override object Visit (Continue continueStatement)
1718
var result = new ContinueStatement ();
1719
var location = LocationsBag.GetLocations (continueStatement);
1720
result.AddChild (new CSharpTokenNode (Convert (continueStatement.loc), ContinueStatement.ContinueKeywordRole), ContinueStatement.ContinueKeywordRole);
1721
if (location != null)
1722
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Semicolon), Roles.Semicolon);
1726
public static bool IsLower (Location left, Location right)
1728
return left.Row < right.Row || left.Row == right.Row && left.Column < right.Column;
1731
public UsingStatement CreateUsingStatement (Block blockStatement)
1733
var usingResult = new UsingStatement ();
1734
Mono.CSharp.Statement cur = blockStatement.Statements [0];
1736
Using u = (Using)cur;
1737
usingResult.AddChild (new CSharpTokenNode (Convert (u.loc), UsingStatement.UsingKeywordRole), UsingStatement.UsingKeywordRole);
1738
usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation), Roles.LPar), Roles.LPar);
1739
if (u.Variables != null) {
1740
var initializer = new VariableInitializer () {
1741
NameToken = Identifier.Create (u.Variables.Variable.Name, Convert (u.Variables.Variable.Location)),
1744
var loc = LocationsBag.GetLocations (u.Variables);
1746
initializer.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Assign), Roles.Assign);
1747
if (u.Variables.Initializer != null)
1748
initializer.Initializer = u.Variables.Initializer.Accept (this) as Expression;
1751
var varDec = new VariableDeclarationStatement () {
1752
Type = ConvertToType (u.Variables.TypeExpression),
1753
Variables = { initializer }
1756
if (u.Variables.Declarators != null) {
1757
foreach (var decl in u.Variables.Declarators) {
1758
var declLoc = LocationsBag.GetLocations (decl);
1759
var init = new VariableInitializer ();
1760
if (declLoc != null && declLoc.Count > 0)
1761
varDec.AddChild (new CSharpTokenNode (Convert (declLoc [0]), Roles.Comma), Roles.Comma);
1762
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1763
if (decl.Initializer != null) {
1764
if (declLoc != null && declLoc.Count > 1)
1765
init.AddChild (new CSharpTokenNode (Convert (declLoc [1]), Roles.Assign), Roles.Assign);
1766
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1768
varDec.AddChild (init, Roles.Variable);
1771
usingResult.AddChild (varDec, UsingStatement.ResourceAcquisitionRole);
1774
usingResult.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation), Roles.RPar), Roles.RPar);
1776
usingResult.AddChild ((Statement)cur.Accept (this), Roles.EmbeddedStatement);
1781
void AddBlockChildren (BlockStatement result, Block blockStatement, ref int curLocal)
1783
if (convertTypeSystemMode) {
1786
foreach (Mono.CSharp.Statement stmt in blockStatement.Statements) {
1789
/* if (curLocal < localVariables.Count && IsLower (localVariables[curLocal].Location, stmt.loc)) {
1790
result.AddChild (CreateVariableDeclaration (localVariables[curLocal]), Roles.Statement);
1793
if (stmt is Block && !(stmt is ToplevelBlock || stmt is ExplicitBlock)) {
1794
AddBlockChildren (result, (Block)stmt, ref curLocal);
1796
result.AddChild ((Statement)stmt.Accept (this), BlockStatement.StatementRole);
1801
public override object Visit (Block blockStatement)
1803
if (blockStatement.IsCompilerGenerated && blockStatement.Statements.Any ()) {
1804
if (blockStatement.Statements.First () is Using)
1805
return CreateUsingStatement (blockStatement);
1806
return blockStatement.Statements.Last ().Accept (this);
1808
var result = new BlockStatement ();
1809
result.AddChild (new CSharpTokenNode (Convert (blockStatement.StartLocation), Roles.LBrace), Roles.LBrace);
1811
AddBlockChildren (result, blockStatement, ref curLocal);
1813
result.AddChild (new CSharpTokenNode (Convert (blockStatement.EndLocation), Roles.RBrace), Roles.RBrace);
1817
public override object Visit (Switch switchStatement)
1819
var result = new SwitchStatement ();
1821
var location = LocationsBag.GetLocations (switchStatement);
1822
result.AddChild (new CSharpTokenNode (Convert (switchStatement.loc), SwitchStatement.SwitchKeywordRole), SwitchStatement.SwitchKeywordRole);
1823
if (location != null)
1824
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1825
if (switchStatement.Expr != null)
1826
result.AddChild ((Expression)switchStatement.Expr.Accept (this), Roles.Expression);
1827
if (location != null && location.Count > 1)
1828
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1829
if (location != null && location.Count > 2)
1830
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.LBrace), Roles.LBrace);
1831
SwitchSection newSection = null;
1832
bool lastWasCase = false, added = true;
1833
if (switchStatement.Block != null) {
1834
foreach (var child in switchStatement.Block.Statements) {
1835
var statement = child.Accept(this);
1836
var caseLabel = statement as CaseLabel;
1837
if (caseLabel != null) {
1839
newSection = new SwitchSection();
1842
newSection.AddChild (caseLabel, SwitchSection.CaseLabelRole);
1846
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
1847
lastWasCase = false;
1850
newSection.AddChild((Statement)statement, Roles.EmbeddedStatement);
1855
result.AddChild (newSection, SwitchStatement.SwitchSectionRole);
1857
if (location != null && location.Count > 3) {
1858
result.AddChild (new CSharpTokenNode (Convert (location [3]), Roles.RBrace), Roles.RBrace);
1860
// parser error, set end node to max value.
1861
result.AddChild (new ErrorNode (), Roles.Error);
1867
public override object Visit(SwitchLabel switchLabel)
1869
var newLabel = new CaseLabel ();
1870
if (!switchLabel.IsDefault) {
1871
newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.CaseKeywordRole), CaseLabel.CaseKeywordRole);
1872
if (switchLabel.Label != null)
1873
newLabel.AddChild ((Expression)switchLabel.Label.Accept (this), Roles.Expression);
1874
var colonLocation = LocationsBag.GetLocations (switchLabel);
1875
if (colonLocation != null)
1876
newLabel.AddChild (new CSharpTokenNode (Convert (colonLocation [0]), Roles.Colon), Roles.Colon);
1878
newLabel.AddChild (new CSharpTokenNode (Convert (switchLabel.Location), CaseLabel.DefaultKeywordRole), CaseLabel.DefaultKeywordRole);
1879
newLabel.AddChild (new CSharpTokenNode (new TextLocation (switchLabel.Location.Row, switchLabel.Location.Column + "default".Length), Roles.Colon), Roles.Colon);
1885
public override object Visit (Lock lockStatement)
1887
var result = new LockStatement ();
1888
var location = LocationsBag.GetLocations (lockStatement);
1889
result.AddChild (new CSharpTokenNode (Convert (lockStatement.loc), LockStatement.LockKeywordRole), LockStatement.LockKeywordRole);
1891
if (location != null)
1892
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1893
if (lockStatement.Expr != null)
1894
result.AddChild ((Expression)lockStatement.Expr.Accept (this), Roles.Expression);
1896
if (location != null && location.Count > 1)
1897
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1898
if (lockStatement.Statement != null)
1899
result.AddChild ((Statement)lockStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1904
public override object Visit (Unchecked uncheckedStatement)
1906
var result = new UncheckedStatement ();
1907
result.AddChild (new CSharpTokenNode (Convert (uncheckedStatement.loc), UncheckedStatement.UncheckedKeywordRole), UncheckedStatement.UncheckedKeywordRole);
1908
if (uncheckedStatement.Block != null)
1909
result.AddChild ((BlockStatement)uncheckedStatement.Block.Accept (this), Roles.Body);
1913
public override object Visit (Checked checkedStatement)
1915
var result = new CheckedStatement ();
1916
result.AddChild (new CSharpTokenNode (Convert (checkedStatement.loc), CheckedStatement.CheckedKeywordRole), CheckedStatement.CheckedKeywordRole);
1917
if (checkedStatement.Block != null)
1918
result.AddChild ((BlockStatement)checkedStatement.Block.Accept (this), Roles.Body);
1922
public override object Visit (Unsafe unsafeStatement)
1924
var result = new UnsafeStatement ();
1925
result.AddChild (new CSharpTokenNode (Convert (unsafeStatement.loc), UnsafeStatement.UnsafeKeywordRole), UnsafeStatement.UnsafeKeywordRole);
1926
if (unsafeStatement.Block != null)
1927
result.AddChild ((BlockStatement)unsafeStatement.Block.Accept (this), Roles.Body);
1931
public override object Visit (Fixed fixedStatement)
1933
var result = new FixedStatement ();
1934
var location = LocationsBag.GetLocations (fixedStatement);
1936
result.AddChild (new CSharpTokenNode (Convert (fixedStatement.loc), FixedStatement.FixedKeywordRole), FixedStatement.FixedKeywordRole);
1937
if (location != null)
1938
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
1940
if (fixedStatement.Variables != null) {
1941
var blockVariableDeclaration = fixedStatement.Variables;
1942
result.AddChild (ConvertToType (blockVariableDeclaration.TypeExpression), Roles.Type);
1943
var varInit = new VariableInitializer ();
1944
var initLocation = LocationsBag.GetLocations (blockVariableDeclaration);
1945
varInit.AddChild (Identifier.Create (blockVariableDeclaration.Variable.Name, Convert (blockVariableDeclaration.Variable.Location)), Roles.Identifier);
1946
if (blockVariableDeclaration.Initializer != null) {
1947
if (initLocation != null)
1948
varInit.AddChild (new CSharpTokenNode (Convert (initLocation [0]), Roles.Assign), Roles.Assign);
1949
varInit.AddChild ((Expression)blockVariableDeclaration.Initializer.Accept (this), Roles.Expression);
1952
result.AddChild (varInit, Roles.Variable);
1954
if (blockVariableDeclaration.Declarators != null) {
1955
foreach (var decl in blockVariableDeclaration.Declarators) {
1956
var loc = LocationsBag.GetLocations (decl);
1957
var init = new VariableInitializer ();
1958
if (loc != null && loc.Count > 0)
1959
result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Comma), Roles.Comma);
1960
init.AddChild (Identifier.Create (decl.Variable.Name, Convert (decl.Variable.Location)), Roles.Identifier);
1961
if (decl.Initializer != null) {
1962
if (loc != null && loc.Count > 1)
1963
init.AddChild (new CSharpTokenNode (Convert (loc [1]), Roles.Assign), Roles.Assign);
1964
init.AddChild ((Expression)decl.Initializer.Accept (this), Roles.Expression);
1967
result.AddChild (init, Roles.Variable);
1972
if (location != null && location.Count > 1)
1973
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
1974
if (fixedStatement.Statement != null)
1975
result.AddChild ((Statement)fixedStatement.Statement.Accept (this), Roles.EmbeddedStatement);
1979
public override object Visit (TryFinally tryFinallyStatement)
1981
TryCatchStatement result;
1982
var location = LocationsBag.GetLocations (tryFinallyStatement);
1984
if (tryFinallyStatement.Stmt is TryCatch) {
1985
result = (TryCatchStatement)tryFinallyStatement.Stmt.Accept (this);
1987
result = new TryCatchStatement ();
1988
result.AddChild (new CSharpTokenNode (Convert (tryFinallyStatement.loc), TryCatchStatement.TryKeywordRole), TryCatchStatement.TryKeywordRole);
1989
if (tryFinallyStatement.Stmt != null)
1990
result.AddChild ((BlockStatement)tryFinallyStatement.Stmt.Accept (this), TryCatchStatement.TryBlockRole);
1992
if (location != null)
1993
result.AddChild (new CSharpTokenNode (Convert (location [0]), TryCatchStatement.FinallyKeywordRole), TryCatchStatement.FinallyKeywordRole);
1994
if (tryFinallyStatement.Fini != null)
1995
result.AddChild ((BlockStatement)tryFinallyStatement.Fini.Accept (this), TryCatchStatement.FinallyBlockRole);
2000
CatchClause ConvertCatch (Catch ctch)
2002
CatchClause result = new CatchClause ();
2003
var location = LocationsBag.GetLocations (ctch);
2004
result.AddChild (new CSharpTokenNode (Convert (ctch.loc), CatchClause.CatchKeywordRole), CatchClause.CatchKeywordRole);
2005
if (ctch.TypeExpression != null) {
2006
if (location != null)
2007
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2009
if (ctch.TypeExpression != null)
2010
result.AddChild (ConvertToType (ctch.TypeExpression), Roles.Type);
2011
if (ctch.Variable != null && !string.IsNullOrEmpty (ctch.Variable.Name))
2012
result.AddChild (Identifier.Create (ctch.Variable.Name, Convert (ctch.Variable.Location)), Roles.Identifier);
2014
if (location != null && location.Count > 1)
2015
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2018
if (ctch.Block != null)
2019
result.AddChild ((BlockStatement)ctch.Block.Accept (this), Roles.Body);
2024
public override object Visit (TryCatch tryCatchStatement)
2026
var result = new TryCatchStatement ();
2027
result.AddChild (new CSharpTokenNode (Convert (tryCatchStatement.loc), TryCatchStatement.TryKeywordRole), TryCatchStatement.TryKeywordRole);
2028
if (tryCatchStatement.Block != null)
2029
result.AddChild ((BlockStatement)tryCatchStatement.Block.Accept (this), TryCatchStatement.TryBlockRole);
2030
if (tryCatchStatement.Clauses != null) {
2031
foreach (var ctch in tryCatchStatement.Clauses) {
2032
result.AddChild (ConvertCatch (ctch), TryCatchStatement.CatchClauseRole);
2035
// if (tryCatchStatement.General != null)
2036
// result.AddChild (ConvertCatch (tryCatchStatement.General), TryCatchStatement.CatchClauseRole);
2041
public override object Visit (Using usingStatement)
2043
var result = new UsingStatement ();
2044
var location = LocationsBag.GetLocations (usingStatement);
2046
result.AddChild (new CSharpTokenNode (Convert (usingStatement.loc), UsingStatement.UsingKeywordRole), UsingStatement.UsingKeywordRole);
2047
if (location != null)
2048
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2049
if (usingStatement.Expr != null)
2050
result.AddChild ((AstNode)usingStatement.Expr.Accept (this), UsingStatement.ResourceAcquisitionRole);
2052
if (location != null && location.Count > 1)
2053
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2055
if (usingStatement.Statement != null)
2056
result.AddChild ((Statement)usingStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2060
public override object Visit (Foreach foreachStatement)
2062
var result = new ForeachStatement ();
2064
var location = LocationsBag.GetLocations (foreachStatement);
2066
result.AddChild (new CSharpTokenNode (Convert (foreachStatement.loc), ForeachStatement.ForeachKeywordRole), ForeachStatement.ForeachKeywordRole);
2067
if (location != null)
2068
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2070
if (foreachStatement.TypeExpression != null)
2071
result.AddChild (ConvertToType (foreachStatement.TypeExpression), Roles.Type);
2073
if (foreachStatement.Variable != null)
2074
result.AddChild (Identifier.Create (foreachStatement.Variable.Name, Convert (foreachStatement.Variable.Location)), Roles.Identifier);
2076
if (location != null && location.Count > 1)
2077
result.AddChild (new CSharpTokenNode (Convert (location [1]), ForeachStatement.InKeywordRole), ForeachStatement.InKeywordRole);
2079
if (foreachStatement.Expr != null)
2080
result.AddChild ((Expression)foreachStatement.Expr.Accept (this), Roles.Expression);
2082
if (location != null && location.Count > 2)
2083
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RPar), Roles.RPar);
2085
if (foreachStatement.Statement != null)
2086
result.AddChild ((Statement)foreachStatement.Statement.Accept (this), Roles.EmbeddedStatement);
2091
public override object Visit (Yield yieldStatement)
2093
var result = new YieldReturnStatement ();
2094
var location = LocationsBag.GetLocations (yieldStatement);
2096
result.AddChild (new CSharpTokenNode (Convert (yieldStatement.loc), YieldReturnStatement.YieldKeywordRole), YieldReturnStatement.YieldKeywordRole);
2097
if (location != null)
2098
result.AddChild (new CSharpTokenNode (Convert (location [0]), YieldReturnStatement.ReturnKeywordRole), YieldReturnStatement.ReturnKeywordRole);
2099
if (yieldStatement.Expr != null)
2100
result.AddChild ((Expression)yieldStatement.Expr.Accept (this), Roles.Expression);
2101
if (location != null && location.Count > 1)
2102
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
2107
public override object Visit (YieldBreak yieldBreakStatement)
2109
var result = new YieldBreakStatement ();
2110
var location = LocationsBag.GetLocations (yieldBreakStatement);
2111
result.AddChild (new CSharpTokenNode (Convert (yieldBreakStatement.loc), YieldBreakStatement.YieldKeywordRole), YieldBreakStatement.YieldKeywordRole);
2112
if (location != null) {
2113
result.AddChild (new CSharpTokenNode (Convert (location [0]), YieldBreakStatement.BreakKeywordRole), YieldBreakStatement.BreakKeywordRole);
2114
if (location.Count > 1)
2115
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Semicolon), Roles.Semicolon);
2122
public override object Visit (Mono.CSharp.Expression expression)
2124
Console.WriteLine ("Visit unknown expression:" + expression);
2125
System.Console.WriteLine (Environment.StackTrace);
2129
public override object Visit (Mono.CSharp.DefaultParameterValueExpression defaultParameterValueExpression)
2131
return defaultParameterValueExpression.Child.Accept (this);
2134
public override object Visit (TypeExpression typeExpression)
2136
return new TypeReferenceExpression (new PrimitiveType (keywordTable [(int)typeExpression.Type.BuiltinType], Convert (typeExpression.Location)));
2139
public override object Visit (LocalVariableReference localVariableReference)
2141
return Identifier.Create (localVariableReference.Name, Convert (localVariableReference.Location));
2144
public override object Visit (MemberAccess memberAccess)
2147
if (memberAccess.LeftExpression is Indirection) {
2148
var ind = memberAccess.LeftExpression as Indirection;
2149
result = new PointerReferenceExpression ();
2150
result.AddChild ((Expression)ind.Expr.Accept (this), Roles.TargetExpression);
2151
result.AddChild (new CSharpTokenNode (Convert (ind.Location), PointerReferenceExpression.ArrowRole), PointerReferenceExpression.ArrowRole);
2153
result = new MemberReferenceExpression ();
2154
if (memberAccess.LeftExpression != null) {
2155
var leftExpr = memberAccess.LeftExpression.Accept (this);
2156
result.AddChild ((Expression)leftExpr, Roles.TargetExpression);
2158
if (!memberAccess.DotLocation.IsNull) {
2159
result.AddChild (new CSharpTokenNode (Convert (memberAccess.DotLocation), Roles.Dot), Roles.Dot);
2163
result.AddChild (Identifier.Create (memberAccess.Name, Convert (memberAccess.Location)), Roles.Identifier);
2165
AddTypeArguments (result, memberAccess);
2169
public override object Visit (QualifiedAliasMember qualifiedAliasMember)
2171
var result = new MemberType ();
2172
result.Target = new SimpleType (qualifiedAliasMember.alias, Convert (qualifiedAliasMember.Location));
2173
result.IsDoubleColon = true;
2174
var location = LocationsBag.GetLocations (qualifiedAliasMember);
2175
if (location != null && location.Count > 0)
2176
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.DoubleColon), Roles.DoubleColon);
2178
result.AddChild (Identifier.Create (qualifiedAliasMember.Name, location != null && location.Count > 1 ? Convert (location [1]) : TextLocation.Empty), Roles.Identifier);
2179
return new TypeReferenceExpression () { Type = result };
2182
public override object Visit (Constant constant)
2184
if (constant.GetValue () == null)
2185
return new NullReferenceExpression (Convert (constant.Location));
2186
string literalValue;
2187
if (constant is ILiteralConstant) {
2188
literalValue = new string (((ILiteralConstant)constant).ParsedValue);
2190
literalValue = constant.GetValueAsLiteral ();
2192
object val = constant.GetValue ();
2194
literalValue = (bool)val ? "true" : "false";
2195
var result = new PrimitiveExpression (val, Convert (constant.Location), literalValue);
2199
public override object Visit (SimpleName simpleName)
2201
var result = new IdentifierExpression ();
2202
result.AddChild (Identifier.Create (simpleName.Name, Convert (simpleName.Location)), Roles.Identifier);
2203
AddTypeArguments (result, simpleName);
2207
public override object Visit (BooleanExpression booleanExpression)
2209
return booleanExpression.Expr.Accept (this);
2212
public override object Visit (Mono.CSharp.ParenthesizedExpression parenthesizedExpression)
2214
var result = new ParenthesizedExpression ();
2215
var location = LocationsBag.GetLocations (parenthesizedExpression);
2216
if (location != null)
2217
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2218
if (parenthesizedExpression.Expr != null)
2219
result.AddChild ((Expression)parenthesizedExpression.Expr.Accept (this), Roles.Expression);
2220
if (location != null && location.Count > 1)
2221
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2225
public override object Visit (Unary unaryExpression)
2227
var result = new UnaryOperatorExpression ();
2228
switch (unaryExpression.Oper) {
2229
case Unary.Operator.UnaryPlus:
2230
result.Operator = UnaryOperatorType.Plus;
2232
case Unary.Operator.UnaryNegation:
2233
result.Operator = UnaryOperatorType.Minus;
2235
case Unary.Operator.LogicalNot:
2236
result.Operator = UnaryOperatorType.Not;
2238
case Unary.Operator.OnesComplement:
2239
result.Operator = UnaryOperatorType.BitNot;
2241
case Unary.Operator.AddressOf:
2242
result.Operator = UnaryOperatorType.AddressOf;
2245
var r = UnaryOperatorExpression.GetOperatorRole (result.Operator);
2246
result.AddChild (new CSharpTokenNode (Convert (unaryExpression.Location), r), r);
2247
if (unaryExpression.Expr != null)
2248
result.AddChild ((Expression)unaryExpression.Expr.Accept (this), Roles.Expression);
2252
public override object Visit (UnaryMutator unaryMutatorExpression)
2254
var result = new UnaryOperatorExpression ();
2255
if (unaryMutatorExpression.Expr == null)
2257
var expression = (Expression)unaryMutatorExpression.Expr.Accept (this);
2258
switch (unaryMutatorExpression.UnaryMutatorMode) {
2259
case UnaryMutator.Mode.PostDecrement:
2260
result.Operator = UnaryOperatorType.PostDecrement;
2261
result.AddChild (expression, Roles.Expression);
2262
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.DecrementRole), UnaryOperatorExpression.DecrementRole);
2264
case UnaryMutator.Mode.PostIncrement:
2265
result.Operator = UnaryOperatorType.PostIncrement;
2266
result.AddChild (expression, Roles.Expression);
2267
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.IncrementRole), UnaryOperatorExpression.IncrementRole);
2270
case UnaryMutator.Mode.PreIncrement:
2271
result.Operator = UnaryOperatorType.Increment;
2272
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.IncrementRole), UnaryOperatorExpression.IncrementRole);
2273
result.AddChild (expression, Roles.Expression);
2275
case UnaryMutator.Mode.PreDecrement:
2276
result.Operator = UnaryOperatorType.Decrement;
2277
result.AddChild (new CSharpTokenNode (Convert (unaryMutatorExpression.Location), UnaryOperatorExpression.DecrementRole), UnaryOperatorExpression.DecrementRole);
2278
result.AddChild (expression, Roles.Expression);
2285
public override object Visit (Indirection indirectionExpression)
2287
var result = new UnaryOperatorExpression ();
2288
result.Operator = UnaryOperatorType.Dereference;
2289
result.AddChild (new CSharpTokenNode (Convert (indirectionExpression.Location), UnaryOperatorExpression.DereferenceRole), UnaryOperatorExpression.DereferenceRole);
2290
if (indirectionExpression.Expr != null)
2291
result.AddChild ((Expression)indirectionExpression.Expr.Accept (this), Roles.Expression);
2295
public override object Visit (Is isExpression)
2297
var result = new IsExpression ();
2298
if (isExpression.Expr != null)
2299
result.AddChild ((Expression)isExpression.Expr.Accept (this), Roles.Expression);
2300
result.AddChild (new CSharpTokenNode (Convert (isExpression.Location), IsExpression.IsKeywordRole), IsExpression.IsKeywordRole);
2302
if (isExpression.ProbeType != null)
2303
result.AddChild (ConvertToType (isExpression.ProbeType), Roles.Type);
2307
public override object Visit (As asExpression)
2309
var result = new AsExpression ();
2310
if (asExpression.Expr != null)
2311
result.AddChild ((Expression)asExpression.Expr.Accept (this), Roles.Expression);
2312
result.AddChild (new CSharpTokenNode (Convert (asExpression.Location), AsExpression.AsKeywordRole), AsExpression.AsKeywordRole);
2313
if (asExpression.ProbeType != null)
2314
result.AddChild (ConvertToType (asExpression.ProbeType), Roles.Type);
2318
public override object Visit (Cast castExpression)
2320
var result = new CastExpression ();
2321
var location = LocationsBag.GetLocations (castExpression);
2323
result.AddChild (new CSharpTokenNode (Convert (castExpression.Location), Roles.LPar), Roles.LPar);
2324
if (castExpression.TargetType != null)
2325
result.AddChild (ConvertToType (castExpression.TargetType), Roles.Type);
2326
if (location != null)
2327
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RPar), Roles.RPar);
2328
if (castExpression.Expr != null)
2329
result.AddChild ((Expression)castExpression.Expr.Accept (this), Roles.Expression);
2333
public override object Visit (ComposedCast composedCast)
2335
var result = new ComposedType ();
2336
result.AddChild (ConvertToType (composedCast.Left), Roles.Type);
2338
var spec = composedCast.Spec;
2339
while (spec != null) {
2340
if (spec.IsNullable) {
2341
result.AddChild (new CSharpTokenNode (Convert (spec.Location), ComposedType.NullableRole), ComposedType.NullableRole);
2342
} else if (spec.IsPointer) {
2343
result.AddChild (new CSharpTokenNode (Convert (spec.Location), ComposedType.PointerRole), ComposedType.PointerRole);
2345
var aSpec = new ArraySpecifier ();
2346
aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location), Roles.LBracket), Roles.LBracket);
2347
var location = LocationsBag.GetLocations (spec);
2348
if (location != null)
2349
aSpec.AddChild (new CSharpTokenNode (Convert (spec.Location), Roles.RBracket), Roles.RBracket);
2350
result.AddChild (aSpec, ComposedType.ArraySpecifierRole);
2358
public override object Visit (Mono.CSharp.DefaultValueExpression defaultValueExpression)
2360
var result = new DefaultValueExpression ();
2361
result.AddChild (new CSharpTokenNode (Convert (defaultValueExpression.Location), DefaultValueExpression.DefaultKeywordRole), DefaultValueExpression.DefaultKeywordRole);
2362
var location = LocationsBag.GetLocations (defaultValueExpression);
2363
if (location != null)
2364
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2365
result.AddChild (ConvertToType (defaultValueExpression.Expr), Roles.Type);
2366
if (location != null && location.Count > 1)
2367
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2371
public override object Visit(Binary binaryExpression)
2373
var result = new BinaryOperatorExpression();
2374
switch (binaryExpression.Oper) {
2375
case Binary.Operator.Multiply:
2376
result.Operator = BinaryOperatorType.Multiply;
2378
case Binary.Operator.Division:
2379
result.Operator = BinaryOperatorType.Divide;
2381
case Binary.Operator.Modulus:
2382
result.Operator = BinaryOperatorType.Modulus;
2384
case Binary.Operator.Addition:
2385
result.Operator = BinaryOperatorType.Add;
2387
case Binary.Operator.Subtraction:
2388
result.Operator = BinaryOperatorType.Subtract;
2390
case Binary.Operator.LeftShift:
2391
result.Operator = BinaryOperatorType.ShiftLeft;
2393
case Binary.Operator.RightShift:
2394
result.Operator = BinaryOperatorType.ShiftRight;
2396
case Binary.Operator.LessThan:
2397
result.Operator = BinaryOperatorType.LessThan;
2399
case Binary.Operator.GreaterThan:
2400
result.Operator = BinaryOperatorType.GreaterThan;
2402
case Binary.Operator.LessThanOrEqual:
2403
result.Operator = BinaryOperatorType.LessThanOrEqual;
2405
case Binary.Operator.GreaterThanOrEqual:
2406
result.Operator = BinaryOperatorType.GreaterThanOrEqual;
2408
case Binary.Operator.Equality:
2409
result.Operator = BinaryOperatorType.Equality;
2411
case Binary.Operator.Inequality:
2412
result.Operator = BinaryOperatorType.InEquality;
2414
case Binary.Operator.BitwiseAnd:
2415
result.Operator = BinaryOperatorType.BitwiseAnd;
2417
case Binary.Operator.ExclusiveOr:
2418
result.Operator = BinaryOperatorType.ExclusiveOr;
2420
case Binary.Operator.BitwiseOr:
2421
result.Operator = BinaryOperatorType.BitwiseOr;
2423
case Binary.Operator.LogicalAnd:
2424
result.Operator = BinaryOperatorType.ConditionalAnd;
2426
case Binary.Operator.LogicalOr:
2427
result.Operator = BinaryOperatorType.ConditionalOr;
2431
if (binaryExpression.Left != null)
2432
result.AddChild((Expression)binaryExpression.Left.Accept(this), BinaryOperatorExpression.LeftRole);
2433
var location = LocationsBag.GetLocations(binaryExpression);
2434
if (location != null) {
2435
var r = BinaryOperatorExpression.GetOperatorRole (result.Operator);
2436
result.AddChild(new CSharpTokenNode(Convert(location [0]), r), r);
2438
if (binaryExpression.Right != null)
2439
result.AddChild ((Expression)binaryExpression.Right.Accept (this), BinaryOperatorExpression.RightRole);
2443
public override object Visit (Mono.CSharp.Nullable.NullCoalescingOperator nullCoalescingOperator)
2445
var result = new BinaryOperatorExpression ();
2446
result.Operator = BinaryOperatorType.NullCoalescing;
2447
if (nullCoalescingOperator.LeftExpression != null)
2448
result.AddChild ((Expression)nullCoalescingOperator.LeftExpression.Accept (this), BinaryOperatorExpression.LeftRole);
2449
var location = LocationsBag.GetLocations (nullCoalescingOperator);
2450
if (location != null)
2451
result.AddChild (new CSharpTokenNode (Convert (location[0]), BinaryOperatorExpression.NullCoalescingRole), BinaryOperatorExpression.NullCoalescingRole);
2452
if (nullCoalescingOperator.RightExpression != null)
2453
result.AddChild ((Expression)nullCoalescingOperator.RightExpression.Accept (this), BinaryOperatorExpression.RightRole);
2457
public override object Visit (Conditional conditionalExpression)
2459
var result = new ConditionalExpression ();
2461
if (conditionalExpression.Expr != null)
2462
result.AddChild ((Expression)conditionalExpression.Expr.Accept (this), Roles.Condition);
2463
var location = LocationsBag.GetLocations (conditionalExpression);
2465
result.AddChild (new CSharpTokenNode (Convert (conditionalExpression.Location), ConditionalExpression.QuestionMarkRole), ConditionalExpression.QuestionMarkRole);
2466
if (conditionalExpression.TrueExpr != null)
2467
result.AddChild ((Expression)conditionalExpression.TrueExpr.Accept (this), ConditionalExpression.TrueRole);
2468
if (location != null)
2469
result.AddChild (new CSharpTokenNode (Convert (location [0]), ConditionalExpression.ColonRole), ConditionalExpression.ColonRole);
2470
if (conditionalExpression.FalseExpr != null)
2471
result.AddChild ((Expression)conditionalExpression.FalseExpr.Accept (this), ConditionalExpression.FalseRole);
2475
void AddParameter (AstNode parent, Mono.CSharp.AParametersCollection parameters)
2477
if (parameters == null)
2479
var paramLocation = LocationsBag.GetLocations (parameters);
2481
for (int i = 0; i < parameters.Count; i++) {
2482
var p = (Parameter)parameters.FixedParameters [i];
2485
var location = LocationsBag.GetLocations (p);
2486
ParameterDeclaration parameterDeclarationExpression = new ParameterDeclaration ();
2487
AddAttributeSection (parameterDeclarationExpression, p);
2488
switch (p.ModFlags) {
2489
case Parameter.Modifier.OUT:
2490
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Out;
2491
if (location != null)
2492
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.OutModifierRole), ParameterDeclaration.OutModifierRole);
2494
case Parameter.Modifier.REF:
2495
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Ref;
2496
if (location != null)
2497
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.RefModifierRole), ParameterDeclaration.RefModifierRole);
2499
case Parameter.Modifier.PARAMS:
2500
parameterDeclarationExpression.ParameterModifier = ParameterModifier.Params;
2501
if (location != null)
2502
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.ParamsModifierRole), ParameterDeclaration.ParamsModifierRole);
2505
if (p.HasExtensionMethodModifier) {
2506
parameterDeclarationExpression.ParameterModifier = ParameterModifier.This;
2507
if (location != null) {
2508
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [0]), ParameterDeclaration.ThisModifierRole), ParameterDeclaration.ThisModifierRole);
2513
if (p.TypeExpression != null) // lambdas may have no types (a, b) => ...
2514
parameterDeclarationExpression.AddChild (ConvertToType (p.TypeExpression), Roles.Type);
2516
parameterDeclarationExpression.AddChild (Identifier.Create (p.Name, Convert (p.Location)), Roles.Identifier);
2517
if (p.HasDefaultValue) {
2518
if (location != null && location.Count > 1)
2519
parameterDeclarationExpression.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.Assign), Roles.Assign);
2520
parameterDeclarationExpression.AddChild ((Expression)p.DefaultValue.Accept (this), Roles.Expression);
2522
parent.AddChild (parameterDeclarationExpression, Roles.Parameter);
2523
if (paramLocation != null && i < paramLocation.Count) {
2524
parent.AddChild (new CSharpTokenNode (Convert (paramLocation [i]), Roles.Comma), Roles.Comma);
2529
void AddTypeParameters (AstNode parent, MemberName memberName)
2531
if (memberName == null || memberName.TypeParameters == null)
2533
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2534
if (chevronLocs != null)
2535
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
2536
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2537
if (chevronLocs != null && i > 0 && i - 1 < chevronLocs.Count)
2538
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i - 1]), Roles.Comma), Roles.Comma);
2539
var arg = memberName.TypeParameters [i];
2542
TypeParameterDeclaration tp = new TypeParameterDeclaration ();
2544
List<Location> varianceLocation;
2545
switch (arg.Variance) {
2546
case Variance.Contravariant:
2547
tp.Variance = VarianceModifier.Contravariant;
2548
varianceLocation = LocationsBag.GetLocations (arg);
2549
if (varianceLocation != null)
2550
tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0]), TypeParameterDeclaration.InVarianceKeywordRole), TypeParameterDeclaration.InVarianceKeywordRole);
2552
case Variance.Covariant:
2553
tp.Variance = VarianceModifier.Covariant;
2554
varianceLocation = LocationsBag.GetLocations (arg);
2555
if (varianceLocation != null)
2556
tp.AddChild (new CSharpTokenNode (Convert (varianceLocation [0]), TypeParameterDeclaration.OutVarianceKeywordRole), TypeParameterDeclaration.OutVarianceKeywordRole);
2559
tp.Variance = VarianceModifier.Invariant;
2564
AddAttributeSection (tp, arg.OptAttributes);
2566
switch (arg.Variance) {
2567
case Variance.Covariant:
2568
tp.Variance = VarianceModifier.Covariant;
2570
case Variance.Contravariant:
2571
tp.Variance = VarianceModifier.Contravariant;
2574
tp.AddChild (Identifier.Create (arg.Name, Convert (arg.Location)), Roles.Identifier);
2575
parent.AddChild (tp, Roles.TypeParameter);
2577
if (chevronLocs != null)
2578
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
2581
void AddTypeArguments (AstNode parent, MemberName memberName)
2583
if (memberName == null || memberName.TypeParameters == null)
2585
var chevronLocs = LocationsBag.GetLocations (memberName.TypeParameters);
2586
if (chevronLocs != null)
2587
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
2589
for (int i = 0; i < memberName.TypeParameters.Count; i++) {
2590
var arg = memberName.TypeParameters [i];
2593
parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2594
if (chevronLocs != null && i < chevronLocs.Count - 2)
2595
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
2598
if (chevronLocs != null)
2599
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
2602
void AddTypeArguments (AstNode parent, ATypeNameExpression memberName)
2604
if (memberName == null || !memberName.HasTypeArguments)
2606
var chevronLocs = LocationsBag.GetLocations (memberName.TypeArguments);
2607
if (chevronLocs != null)
2608
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 2]), Roles.LChevron), Roles.LChevron);
2610
for (int i = 0; i < memberName.TypeArguments.Count; i++) {
2611
var arg = memberName.TypeArguments.Args [i];
2614
parent.AddChild (ConvertToType (arg), Roles.TypeArgument);
2615
if (chevronLocs != null && i < chevronLocs.Count - 2)
2616
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [i]), Roles.Comma), Roles.Comma);
2619
if (chevronLocs != null)
2620
parent.AddChild (new CSharpTokenNode (Convert (chevronLocs [chevronLocs.Count - 1]), Roles.RChevron), Roles.RChevron);
2623
void AddConstraints(AstNode parent, TypeParameters d)
2627
for (int i = 0; i < d.Count; i++) {
2628
var typeParameter = d [i];
2629
if (typeParameter == null)
2631
var c = typeParameter.Constraints;
2634
var location = LocationsBag.GetLocations (c);
2635
var constraint = new Constraint ();
2636
constraint.AddChild (new CSharpTokenNode (Convert (c.Location), Roles.WhereKeyword), Roles.WhereKeyword);
2637
constraint.AddChild (new SimpleType (Identifier.Create (c.TypeParameter.Value, Convert (c.TypeParameter.Location))), Roles.ConstraintTypeParameter);
2638
if (location != null)
2639
constraint.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Colon), Roles.Colon);
2640
var commaLocs = LocationsBag.GetLocations (c.ConstraintExpressions);
2642
if (c.ConstraintExpressions != null) {
2643
foreach (var expr in c.ConstraintExpressions) {
2644
constraint.AddChild (ConvertToType (expr), Roles.BaseType);
2645
if (commaLocs != null && curComma < commaLocs.Count)
2646
constraint.AddChild (new CSharpTokenNode (Convert (commaLocs [curComma++]), Roles.Comma), Roles.Comma);
2650
// We need to sort the constraints by position; as they might be in a different order than the type parameters
2651
AstNode prevSibling = parent.LastChild;
2652
while (prevSibling.StartLocation > constraint.StartLocation && prevSibling.PrevSibling != null)
2653
prevSibling = prevSibling.PrevSibling;
2654
parent.InsertChildAfter (prevSibling, constraint, Roles.Constraint);
2658
Expression ConvertArgument (Argument arg)
2660
if (arg is NamedArgument) {
2661
var na = (NamedArgument)arg;
2662
NamedArgumentExpression newArg = new NamedArgumentExpression ();
2663
newArg.AddChild (Identifier.Create (na.Name, Convert (na.Location)), Roles.Identifier);
2665
var loc = LocationsBag.GetLocations (na);
2667
newArg.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.Colon), Roles.Colon);
2669
if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2670
DirectionExpression direction = new DirectionExpression ();
2671
direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2672
var argLocation = LocationsBag.GetLocations (arg);
2673
if (argLocation != null) {
2674
var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole;
2675
direction.AddChild (new CSharpTokenNode (Convert (argLocation [0]), r), r);
2677
direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2678
newArg.AddChild (direction, Roles.Expression);
2680
newArg.AddChild ((Expression)na.Expr.Accept (this), Roles.Expression);
2685
if (arg.ArgType == Argument.AType.Out || arg.ArgType == Argument.AType.Ref) {
2686
DirectionExpression direction = new DirectionExpression ();
2687
direction.FieldDirection = arg.ArgType == Argument.AType.Out ? FieldDirection.Out : FieldDirection.Ref;
2688
var argLocation = LocationsBag.GetLocations (arg);
2689
if (argLocation != null) {
2690
var r = arg.ArgType == Argument.AType.Out ? DirectionExpression.OutKeywordRole : DirectionExpression.RefKeywordRole;
2691
direction.AddChild (new CSharpTokenNode (Convert (argLocation [0]), r), r);
2693
direction.AddChild ((Expression)arg.Expr.Accept (this), Roles.Expression);
2697
return (Expression)arg.Expr.Accept (this);
2700
void AddArguments (AstNode parent, object location, Mono.CSharp.Arguments args)
2705
var commaLocations = LocationsBag.GetLocations (args);
2706
for (int i = 0; i < args.Count; i++) {
2707
parent.AddChild (ConvertArgument (args [i]), Roles.Argument);
2708
if (commaLocations != null && i < commaLocations.Count) {
2709
parent.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
2712
if (commaLocations != null && commaLocations.Count > args.Count)
2713
parent.AddChild (new CSharpTokenNode (Convert (commaLocations [args.Count]), Roles.Comma), Roles.Comma);
2716
public override object Visit (Invocation invocationExpression)
2718
var result = new InvocationExpression ();
2719
var location = LocationsBag.GetLocations (invocationExpression);
2720
if (invocationExpression.Exp != null)
2721
result.AddChild ((Expression)invocationExpression.Exp.Accept (this), Roles.TargetExpression);
2722
if (location != null)
2723
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2724
AddArguments (result, location, invocationExpression.Arguments);
2726
if (location != null && location.Count > 1)
2727
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2731
public override object Visit (New newExpression)
2733
var result = new ObjectCreateExpression ();
2734
var location = LocationsBag.GetLocations (newExpression);
2735
result.AddChild (new CSharpTokenNode (Convert (newExpression.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
2737
if (newExpression.TypeRequested != null)
2738
result.AddChild (ConvertToType (newExpression.TypeRequested), Roles.Type);
2739
if (location != null)
2740
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2741
AddArguments (result, location, newExpression.Arguments);
2743
if (location != null && location.Count > 1)
2744
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2749
public override object Visit (NewAnonymousType newAnonymousType)
2751
var result = new AnonymousTypeCreateExpression ();
2752
var location = LocationsBag.GetLocations (newAnonymousType);
2753
result.AddChild (new CSharpTokenNode (Convert (newAnonymousType.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
2754
if (location != null)
2755
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBrace), Roles.LBrace);
2756
if (newAnonymousType.Parameters != null) {
2757
foreach (var par in newAnonymousType.Parameters) {
2760
var parLocation = LocationsBag.GetLocations (par);
2762
if (parLocation == null) {
2763
if (par.Expr != null)
2764
result.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2766
var namedExpression = new NamedExpression ();
2767
namedExpression.AddChild (Identifier.Create (par.Name, Convert (par.Location)), Roles.Identifier);
2768
namedExpression.AddChild (new CSharpTokenNode (Convert (parLocation [0]), Roles.Assign), Roles.Assign);
2769
if (par.Expr != null)
2770
namedExpression.AddChild ((Expression)par.Expr.Accept (this), Roles.Expression);
2771
result.AddChild (namedExpression, Roles.Expression);
2775
if (location != null && location.Count > 1)
2776
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBrace), Roles.RBrace);
2780
ArrayInitializerExpression ConvertCollectionOrObjectInitializers(CollectionOrObjectInitializers minit)
2784
var init = new ArrayInitializerExpression();
2785
AddConvertCollectionOrObjectInitializers(init, minit);
2789
void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjectInitializers minit)
2791
var initLoc = LocationsBag.GetLocations(minit);
2792
var commaLoc = LocationsBag.GetLocations(minit.Initializers);
2794
init.AddChild(new CSharpTokenNode(Convert(minit.Location), Roles.LBrace), Roles.LBrace);
2795
foreach (var expr in minit.Initializers) {
2796
var collectionInit = expr as CollectionElementInitializer;
2797
if (collectionInit != null) {
2799
// For ease of use purposes in the resolver the ast representation
2800
// of { a, b, c } is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
2801
// can be identified by expr.IsSingleElement.
2802
if (!collectionInit.IsSingle) {
2803
parent = new ArrayInitializerExpression();
2804
parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location), Roles.LBrace), Roles.LBrace);
2806
parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
2808
for (int i = 0; i < collectionInit.Arguments.Count; i++) {
2809
var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
2813
(ICSharpCode.NRefactory.CSharp.Expression)arg.Expr.Accept(this),
2818
if (!collectionInit.IsSingle) {
2819
var braceLocs = LocationsBag.GetLocations(expr);
2820
if (braceLocs != null)
2821
parent.AddChild(new CSharpTokenNode(Convert(braceLocs [0]), Roles.RBrace), Roles.RBrace);
2823
init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
2825
var eleInit = expr as ElementInitializer;
2826
if (eleInit != null) {
2827
var nexpr = new NamedExpression();
2829
Identifier.Create(eleInit.Name, Convert(eleInit.Location)),
2832
var assignLoc = LocationsBag.GetLocations(eleInit);
2833
if (assignLoc != null)
2834
nexpr.AddChild(new CSharpTokenNode(Convert(assignLoc [0]), Roles.Assign), Roles.Assign);
2835
if (eleInit.Source != null) {
2836
if (eleInit.Source is CollectionOrObjectInitializers) {
2837
var arrInit = new ArrayInitializerExpression();
2838
AddConvertCollectionOrObjectInitializers(
2840
eleInit.Source as CollectionOrObjectInitializers
2842
nexpr.AddChild(arrInit, Roles.Expression);
2844
nexpr.AddChild((Expression)eleInit.Source.Accept(this), Roles.Expression);
2848
init.AddChild(nexpr, Roles.Expression);
2851
if (commaLoc != null && curComma < commaLoc.Count)
2852
init.AddChild(new CSharpTokenNode(Convert(commaLoc [curComma++]), Roles.Comma), Roles.Comma);
2855
if (initLoc != null) {
2856
if (initLoc.Count == 2) // optional comma
2857
init.AddChild(new CSharpTokenNode(Convert(initLoc [0]), Roles.Comma), Roles.Comma);
2858
init.AddChild(new CSharpTokenNode(Convert(initLoc [initLoc.Count - 1]), Roles.RBrace), Roles.RBrace);
2863
public override object Visit (NewInitialize newInitializeExpression)
2865
var result = new ObjectCreateExpression ();
2866
result.AddChild (new CSharpTokenNode (Convert (newInitializeExpression.Location), ObjectCreateExpression.NewKeywordRole), ObjectCreateExpression.NewKeywordRole);
2868
if (newInitializeExpression.TypeRequested != null)
2869
result.AddChild (ConvertToType (newInitializeExpression.TypeRequested), Roles.Type);
2871
var location = LocationsBag.GetLocations (newInitializeExpression);
2872
if (location != null)
2873
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2874
AddArguments (result, location, newInitializeExpression.Arguments);
2875
if (location != null && location.Count > 1)
2876
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2878
var init = ConvertCollectionOrObjectInitializers (newInitializeExpression.Initializers);
2880
result.AddChild (init, ObjectCreateExpression.InitializerRole);
2885
public override object Visit (ArrayCreation arrayCreationExpression)
2887
var result = new ArrayCreateExpression ();
2889
var location = LocationsBag.GetLocations (arrayCreationExpression);
2890
result.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Location), ArrayCreateExpression.NewKeywordRole), ArrayCreateExpression.NewKeywordRole);
2891
if (arrayCreationExpression.TypeExpression != null)
2892
result.AddChild (ConvertToType (arrayCreationExpression.TypeExpression), Roles.Type);
2894
var next = arrayCreationExpression.Rank;
2895
if (arrayCreationExpression.Arguments != null) {
2896
// skip first array rank.
2899
if (location != null)
2900
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LBracket), Roles.LBracket);
2902
var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Arguments);
2903
for (int i = 0; i < arrayCreationExpression.Arguments.Count; i++) {
2904
result.AddChild ((Expression)arrayCreationExpression.Arguments [i].Accept (this), Roles.Argument);
2905
if (commaLocations != null && i < commaLocations.Count)
2906
result.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
2908
if (location != null && location.Count > 1)
2909
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RBracket), Roles.RBracket);
2913
while (next != null) {
2914
ArraySpecifier spec = new ArraySpecifier (next.Dimension);
2915
var loc = LocationsBag.GetLocations (next);
2916
spec.AddChild (new CSharpTokenNode (Convert (next.Location), Roles.LBracket), Roles.LBracket);
2917
result.AddChild (spec, ArrayCreateExpression.AdditionalArraySpecifierRole);
2919
result.AddChild (new CSharpTokenNode (Convert (loc [0]), Roles.RBracket), Roles.RBracket);
2923
if (arrayCreationExpression.Initializers != null) {
2924
var initLocation = LocationsBag.GetLocations (arrayCreationExpression.Initializers);
2925
ArrayInitializerExpression initializer = new ArrayInitializerExpression ();
2927
initializer.AddChild (new CSharpTokenNode (Convert (arrayCreationExpression.Initializers.Location), Roles.LBrace), Roles.LBrace);
2928
var commaLocations = LocationsBag.GetLocations (arrayCreationExpression.Initializers.Elements);
2929
for (int i = 0; i < arrayCreationExpression.Initializers.Count; i++) {
2930
var init = arrayCreationExpression.Initializers [i];
2933
initializer.AddChild ((Expression)init.Accept (this), Roles.Expression);
2934
if (commaLocations != null && i < commaLocations.Count) {
2935
initializer.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
2938
if (initLocation != null) {
2939
if (initLocation.Count == 2) // optional comma
2940
initializer.AddChild (new CSharpTokenNode(Convert(initLocation [0]), Roles.Comma), Roles.Comma);
2941
initializer.AddChild (new CSharpTokenNode (Convert (initLocation [initLocation.Count - 1]), Roles.RBrace), Roles.RBrace);
2943
result.AddChild (initializer, ArrayCreateExpression.InitializerRole);
2949
public override object Visit (This thisExpression)
2951
var result = new ThisReferenceExpression ();
2952
result.Location = Convert (thisExpression.Location);
2956
public override object Visit (ArglistAccess argListAccessExpression)
2958
var result = new UndocumentedExpression () {
2959
UndocumentedExpressionType = UndocumentedExpressionType.ArgListAccess
2961
result.AddChild (new CSharpTokenNode (Convert (argListAccessExpression.Location), UndocumentedExpression.ArglistKeywordRole), UndocumentedExpression.ArglistKeywordRole);
2965
#region Undocumented expressions
2966
public override object Visit (Arglist argListExpression)
2968
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.ArgList };
2969
result.AddChild (new CSharpTokenNode (Convert (argListExpression.Location), UndocumentedExpression.ArglistKeywordRole), UndocumentedExpression.ArglistKeywordRole);
2970
var location = LocationsBag.GetLocations (argListExpression);
2971
if (location != null)
2972
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2974
AddArguments (result, location, argListExpression.Arguments);
2976
if (location != null && location.Count > 1)
2977
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2981
public override object Visit (MakeRefExpr makeRefExpr)
2983
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.MakeRef };
2984
result.AddChild (new CSharpTokenNode (Convert (makeRefExpr.Location), UndocumentedExpression.MakerefKeywordRole), UndocumentedExpression.MakerefKeywordRole);
2985
var location = LocationsBag.GetLocations (makeRefExpr);
2986
if (location != null)
2987
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
2988
if (makeRefExpr.Expr != null)
2989
result.AddChild ((Expression)makeRefExpr.Expr.Accept (this), Roles.Argument);
2990
if (location != null && location.Count > 1)
2991
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
2995
public override object Visit (RefTypeExpr refTypeExpr)
2997
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefType };
2998
result.AddChild (new CSharpTokenNode (Convert (refTypeExpr.Location), UndocumentedExpression.ReftypeKeywordRole), UndocumentedExpression.ReftypeKeywordRole);
2999
var location = LocationsBag.GetLocations (refTypeExpr);
3000
if (location != null)
3001
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3003
if (refTypeExpr.Expr != null)
3004
result.AddChild ((Expression)refTypeExpr.Expr.Accept (this), Roles.Argument);
3006
if (location != null && location.Count > 1)
3007
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3011
public override object Visit (RefValueExpr refValueExpr)
3013
var result = new UndocumentedExpression () { UndocumentedExpressionType = UndocumentedExpressionType.RefValue };
3014
result.AddChild (new CSharpTokenNode (Convert (refValueExpr.Location), UndocumentedExpression.RefvalueKeywordRole), UndocumentedExpression.RefvalueKeywordRole);
3015
var location = LocationsBag.GetLocations (refValueExpr);
3016
if (location != null)
3017
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3020
if (refValueExpr.Expr != null)
3021
result.AddChild ((Expression)refValueExpr.Expr.Accept (this), Roles.Argument);
3023
if (refValueExpr.FullNamedExpression != null)
3024
result.AddChild ((Expression)refValueExpr.FullNamedExpression.Accept (this), Roles.Argument);
3026
if (location != null && location.Count > 1)
3027
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3032
public override object Visit (TypeOf typeOfExpression)
3034
var result = new TypeOfExpression ();
3035
var location = LocationsBag.GetLocations (typeOfExpression);
3036
result.AddChild (new CSharpTokenNode (Convert (typeOfExpression.Location), TypeOfExpression.TypeofKeywordRole), TypeOfExpression.TypeofKeywordRole);
3037
if (location != null)
3038
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3039
if (typeOfExpression.TypeExpression != null)
3040
result.AddChild (ConvertToType (typeOfExpression.TypeExpression), Roles.Type);
3041
if (location != null && location.Count > 1)
3042
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3046
public override object Visit (SizeOf sizeOfExpression)
3048
var result = new SizeOfExpression ();
3049
var location = LocationsBag.GetLocations (sizeOfExpression);
3050
result.AddChild (new CSharpTokenNode (Convert (sizeOfExpression.Location), SizeOfExpression.SizeofKeywordRole), SizeOfExpression.SizeofKeywordRole);
3051
if (location != null)
3052
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3053
if (sizeOfExpression.TypeExpression != null)
3054
result.AddChild (ConvertToType (sizeOfExpression.TypeExpression), Roles.Type);
3055
if (location != null && location.Count > 1)
3056
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3060
public override object Visit (CheckedExpr checkedExpression)
3062
var result = new CheckedExpression ();
3063
var location = LocationsBag.GetLocations (checkedExpression);
3064
result.AddChild (new CSharpTokenNode (Convert (checkedExpression.Location), CheckedExpression.CheckedKeywordRole), CheckedExpression.CheckedKeywordRole);
3065
if (location != null)
3066
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3067
if (checkedExpression.Expr != null)
3068
result.AddChild ((Expression)checkedExpression.Expr.Accept (this), Roles.Expression);
3069
if (location != null && location.Count > 1)
3070
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3074
public override object Visit (UnCheckedExpr uncheckedExpression)
3076
var result = new UncheckedExpression ();
3077
var location = LocationsBag.GetLocations (uncheckedExpression);
3078
result.AddChild (new CSharpTokenNode (Convert (uncheckedExpression.Location), UncheckedExpression.UncheckedKeywordRole), UncheckedExpression.UncheckedKeywordRole);
3079
if (location != null)
3080
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.LPar), Roles.LPar);
3081
if (uncheckedExpression.Expr != null)
3082
result.AddChild ((Expression)uncheckedExpression.Expr.Accept (this), Roles.Expression);
3083
if (location != null && location.Count > 1)
3084
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.RPar), Roles.RPar);
3088
public override object Visit (ElementAccess elementAccessExpression)
3090
IndexerExpression result = new IndexerExpression ();
3091
var location = LocationsBag.GetLocations (elementAccessExpression);
3093
if (elementAccessExpression.Expr != null)
3094
result.AddChild ((Expression)elementAccessExpression.Expr.Accept (this), Roles.TargetExpression);
3095
result.AddChild (new CSharpTokenNode (Convert (elementAccessExpression.Location), Roles.LBracket), Roles.LBracket);
3096
AddArguments (result, location, elementAccessExpression.Arguments);
3097
if (location != null)
3098
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.RBracket), Roles.RBracket);
3102
public override object Visit (BaseThis baseAccessExpression)
3104
var result = new BaseReferenceExpression ();
3105
result.Location = Convert (baseAccessExpression.Location);
3109
public override object Visit (StackAlloc stackAllocExpression)
3111
var result = new StackAllocExpression ();
3113
var location = LocationsBag.GetLocations (stackAllocExpression);
3114
if (location != null)
3115
result.AddChild (new CSharpTokenNode (Convert (location [0]), StackAllocExpression.StackallocKeywordRole), StackAllocExpression.StackallocKeywordRole);
3116
if (stackAllocExpression.TypeExpression != null)
3117
result.AddChild (ConvertToType (stackAllocExpression.TypeExpression), Roles.Type);
3118
if (location != null && location.Count > 1)
3119
result.AddChild (new CSharpTokenNode (Convert (location [1]), Roles.LBracket), Roles.LBracket);
3120
if (stackAllocExpression.CountExpression != null)
3121
result.AddChild ((Expression)stackAllocExpression.CountExpression.Accept (this), Roles.Expression);
3122
if (location != null && location.Count > 2)
3123
result.AddChild (new CSharpTokenNode (Convert (location [2]), Roles.RBracket), Roles.RBracket);
3127
public override object Visit (SimpleAssign simpleAssign)
3129
var result = new AssignmentExpression ();
3131
result.Operator = AssignmentOperatorType.Assign;
3132
if (simpleAssign.Target != null)
3133
result.AddChild ((Expression)simpleAssign.Target.Accept (this), AssignmentExpression.LeftRole);
3134
var location = LocationsBag.GetLocations (simpleAssign);
3135
if (location != null)
3136
result.AddChild (new CSharpTokenNode (Convert (location[0]), AssignmentExpression.AssignRole), AssignmentExpression.AssignRole);
3137
if (simpleAssign.Source != null) {
3138
result.AddChild ((Expression)simpleAssign.Source.Accept (this), AssignmentExpression.RightRole);
3143
public override object Visit(CompoundAssign compoundAssign)
3145
var result = new AssignmentExpression();
3146
switch (compoundAssign.Op) {
3147
case Binary.Operator.Multiply:
3148
result.Operator = AssignmentOperatorType.Multiply;
3150
case Binary.Operator.Division:
3151
result.Operator = AssignmentOperatorType.Divide;
3153
case Binary.Operator.Modulus:
3154
result.Operator = AssignmentOperatorType.Modulus;
3156
case Binary.Operator.Addition:
3157
result.Operator = AssignmentOperatorType.Add;
3159
case Binary.Operator.Subtraction:
3160
result.Operator = AssignmentOperatorType.Subtract;
3162
case Binary.Operator.LeftShift:
3163
result.Operator = AssignmentOperatorType.ShiftLeft;
3165
case Binary.Operator.RightShift:
3166
result.Operator = AssignmentOperatorType.ShiftRight;
3168
case Binary.Operator.BitwiseAnd:
3169
result.Operator = AssignmentOperatorType.BitwiseAnd;
3171
case Binary.Operator.BitwiseOr:
3172
result.Operator = AssignmentOperatorType.BitwiseOr;
3174
case Binary.Operator.ExclusiveOr:
3175
result.Operator = AssignmentOperatorType.ExclusiveOr;
3179
if (compoundAssign.Target != null)
3180
result.AddChild((Expression)compoundAssign.Target.Accept(this), AssignmentExpression.LeftRole);
3181
var location = LocationsBag.GetLocations(compoundAssign);
3182
if (location != null) {
3183
var r = AssignmentExpression.GetOperatorRole (result.Operator);
3184
result.AddChild(new CSharpTokenNode(Convert(location [0]), r), r);
3186
if (compoundAssign.Source != null)
3187
result.AddChild ((Expression)compoundAssign.Source.Accept (this), AssignmentExpression.RightRole);
3191
public override object Visit (Mono.CSharp.AnonymousMethodExpression anonymousMethodExpression)
3193
var result = new AnonymousMethodExpression ();
3194
var location = LocationsBag.GetLocations (anonymousMethodExpression);
3196
if (anonymousMethodExpression.IsAsync) {
3197
result.IsAsync = true;
3198
result.AddChild (new CSharpTokenNode (Convert (location [l++]), AnonymousMethodExpression.AsyncModifierRole), AnonymousMethodExpression.AsyncModifierRole);
3200
if (location != null) {
3201
result.AddChild (new CSharpTokenNode (Convert (location [l++]), AnonymousMethodExpression.DelegateKeywordRole), AnonymousMethodExpression.DelegateKeywordRole);
3203
if (location.Count > l) {
3204
result.HasParameterList = true;
3205
result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.LPar), Roles.LPar);
3206
AddParameter (result, anonymousMethodExpression.Parameters);
3207
result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.RPar), Roles.RPar);
3210
if (anonymousMethodExpression.Block != null)
3211
result.AddChild ((BlockStatement)anonymousMethodExpression.Block.Accept (this), Roles.Body);
3215
public override object Visit (Mono.CSharp.LambdaExpression lambdaExpression)
3217
var result = new LambdaExpression ();
3218
var location = LocationsBag.GetLocations (lambdaExpression);
3220
if (lambdaExpression.IsAsync) {
3221
result.IsAsync = true;
3222
result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.AsyncModifierRole), LambdaExpression.AsyncModifierRole);
3224
if (location == null || location.Count == l + 1) {
3225
if (lambdaExpression.Block != null)
3226
AddParameter (result, lambdaExpression.Parameters);
3227
if (location != null)
3228
result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.ArrowRole), LambdaExpression.ArrowRole);
3230
result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.LPar), Roles.LPar);
3231
if (lambdaExpression.Block != null)
3232
AddParameter (result, lambdaExpression.Parameters);
3233
if (location != null) {
3234
result.AddChild (new CSharpTokenNode (Convert (location [l++]), Roles.RPar), Roles.RPar);
3235
result.AddChild (new CSharpTokenNode (Convert (location [l++]), LambdaExpression.ArrowRole), LambdaExpression.ArrowRole);
3238
if (lambdaExpression.Block != null) {
3239
if (lambdaExpression.Block.IsCompilerGenerated) {
3240
ContextualReturn generatedReturn = (ContextualReturn)lambdaExpression.Block.Statements [0];
3241
result.AddChild ((AstNode)generatedReturn.Expr.Accept (this), LambdaExpression.BodyRole);
3243
result.AddChild ((AstNode)lambdaExpression.Block.Accept (this), LambdaExpression.BodyRole);
3249
public override object Visit (ConstInitializer constInitializer)
3251
return constInitializer.Expr.Accept (this);
3254
public override object Visit (ArrayInitializer arrayInitializer)
3256
var result = new ArrayInitializerExpression ();
3257
var location = LocationsBag.GetLocations (arrayInitializer);
3258
result.AddChild (new CSharpTokenNode (Convert (arrayInitializer.Location), Roles.LBrace), Roles.LBrace);
3259
var commaLocations = LocationsBag.GetLocations (arrayInitializer.Elements);
3260
for (int i = 0; i < arrayInitializer.Count; i++) {
3261
var init = arrayInitializer [i];
3264
result.AddChild ((Expression)init.Accept (this), Roles.Expression);
3265
if (commaLocations != null && i < commaLocations.Count)
3266
result.AddChild (new CSharpTokenNode (Convert (commaLocations [i]), Roles.Comma), Roles.Comma);
3269
if (location != null) {
3270
if (location.Count == 2) // optional comma
3271
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Comma), Roles.Comma);
3272
result.AddChild (new CSharpTokenNode (Convert (location [location.Count - 1]), Roles.RBrace), Roles.RBrace);
3279
#region LINQ expressions
3280
QueryOrderClause currentQueryOrderClause;
3282
public override object Visit (Mono.CSharp.Linq.QueryExpression queryExpression)
3284
var oldQueryOrderClause = currentQueryOrderClause;
3286
currentQueryOrderClause = null;
3287
var result = new QueryExpression ();
3289
var currentClause = queryExpression.next;
3291
while (currentClause != null) {
3292
QueryClause clause = (QueryClause)currentClause.Accept (this);
3293
if (clause is QueryContinuationClause) {
3294
// insert preceding query at beginning of QueryContinuationClause
3295
clause.InsertChildAfter (null, result, QueryContinuationClause.PrecedingQueryRole);
3296
// create a new QueryExpression for the remaining query
3297
result = new QueryExpression ();
3299
if (clause != null) {
3300
result.AddChild (clause, QueryExpression.ClauseRole);
3302
currentClause = currentClause.next;
3308
currentQueryOrderClause = oldQueryOrderClause;
3312
public override object Visit (Mono.CSharp.Linq.QueryStartClause queryStart)
3314
if (queryStart.Expr == null) {
3315
var intoClause = new QueryContinuationClause ();
3316
intoClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryContinuationClause.IntoKeywordRole), QueryContinuationClause.IntoKeywordRole);
3317
intoClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3321
var fromClause = new QueryFromClause ();
3323
fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryFromClause.FromKeywordRole), QueryFromClause.FromKeywordRole);
3325
if (queryStart.IdentifierType != null)
3326
fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3328
fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3330
var location = LocationsBag.GetLocations (queryStart);
3331
if (location != null)
3332
fromClause.AddChild (new CSharpTokenNode (Convert (location [0]), QueryFromClause.InKeywordRole), QueryFromClause.InKeywordRole);
3334
if (queryStart.Expr != null)
3335
fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3339
public override object Visit (Mono.CSharp.Linq.SelectMany queryStart)
3341
var fromClause = new QueryFromClause ();
3343
fromClause.AddChild (new CSharpTokenNode (Convert (queryStart.Location), QueryFromClause.FromKeywordRole), QueryFromClause.FromKeywordRole);
3345
if (queryStart.IdentifierType != null)
3346
fromClause.AddChild (ConvertToType (queryStart.IdentifierType), Roles.Type);
3348
fromClause.AddChild (Identifier.Create (queryStart.IntoVariable.Name, Convert (queryStart.IntoVariable.Location)), Roles.Identifier);
3350
var location = LocationsBag.GetLocations (queryStart);
3351
if (location != null)
3352
fromClause.AddChild (new CSharpTokenNode (Convert (location [0]), QueryFromClause.InKeywordRole), QueryFromClause.InKeywordRole);
3354
if (queryStart.Expr != null)
3355
fromClause.AddChild ((Expression)queryStart.Expr.Accept (this), Roles.Expression);
3359
public override object Visit (Mono.CSharp.Linq.Select sel)
3361
var result = new QuerySelectClause ();
3362
result.AddChild (new CSharpTokenNode (Convert (sel.Location), QuerySelectClause.SelectKeywordRole), QuerySelectClause.SelectKeywordRole);
3363
if (sel.Expr != null)
3364
result.AddChild ((Expression)sel.Expr.Accept (this), Roles.Expression);
3368
public override object Visit (Mono.CSharp.Linq.GroupBy groupBy)
3370
var result = new QueryGroupClause ();
3371
var location = LocationsBag.GetLocations (groupBy);
3372
result.AddChild (new CSharpTokenNode (Convert (groupBy.Location), QueryGroupClause.GroupKeywordRole), QueryGroupClause.GroupKeywordRole);
3373
if (groupBy.ElementSelector != null)
3374
result.AddChild ((Expression)groupBy.ElementSelector.Accept (this), QueryGroupClause.ProjectionRole);
3375
if (location != null)
3376
result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryGroupClause.ByKeywordRole), QueryGroupClause.ByKeywordRole);
3377
if (groupBy.Expr != null)
3378
result.AddChild ((Expression)groupBy.Expr.Accept (this), QueryGroupClause.KeyRole);
3382
public override object Visit (Mono.CSharp.Linq.Let l)
3384
var result = new QueryLetClause ();
3385
var location = LocationsBag.GetLocations (l);
3387
result.AddChild (new CSharpTokenNode (Convert (l.Location), QueryLetClause.LetKeywordRole), QueryLetClause.LetKeywordRole);
3388
result.AddChild (Identifier.Create (l.IntoVariable.Name, Convert (l.IntoVariable.Location)), Roles.Identifier);
3389
if (location != null)
3390
result.AddChild (new CSharpTokenNode (Convert (location [0]), Roles.Assign), Roles.Assign);
3392
result.AddChild ((Expression)l.Expr.Accept (this), Roles.Expression);
3396
public override object Visit (Mono.CSharp.Linq.Where w)
3398
var result = new QueryWhereClause ();
3399
result.AddChild (new CSharpTokenNode (Convert (w.Location), QueryWhereClause.WhereKeywordRole), QueryWhereClause.WhereKeywordRole);
3401
result.AddChild ((Expression)w.Expr.Accept (this), Roles.Condition);
3405
public override object Visit (Mono.CSharp.Linq.Join join)
3407
var result = new QueryJoinClause ();
3408
var location = LocationsBag.GetLocations (join);
3409
result.AddChild (new CSharpTokenNode (Convert (join.Location), QueryJoinClause.JoinKeywordRole), QueryJoinClause.JoinKeywordRole);
3410
result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3412
if (location != null)
3413
result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryJoinClause.InKeywordRole), QueryJoinClause.InKeywordRole);
3415
if (join.Expr != null)
3416
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3418
if (location != null && location.Count > 1)
3419
result.AddChild (new CSharpTokenNode (Convert (location [1]), QueryJoinClause.OnKeywordRole), QueryJoinClause.OnKeywordRole);
3421
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3423
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3425
if (location != null && location.Count > 2)
3426
result.AddChild (new CSharpTokenNode (Convert (location [2]), QueryJoinClause.EqualsKeywordRole), QueryJoinClause.EqualsKeywordRole);
3428
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3430
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3435
public override object Visit (Mono.CSharp.Linq.GroupJoin join)
3437
var result = new QueryJoinClause ();
3438
var location = LocationsBag.GetLocations (join);
3439
result.AddChild (new CSharpTokenNode (Convert (join.Location), QueryJoinClause.JoinKeywordRole), QueryJoinClause.JoinKeywordRole);
3441
// mcs seems to have swapped IntoVariable with JoinVariable, so we'll swap it back here
3442
result.AddChild (Identifier.Create (join.IntoVariable.Name, Convert (join.IntoVariable.Location)), QueryJoinClause.JoinIdentifierRole);
3444
if (location != null)
3445
result.AddChild (new CSharpTokenNode (Convert (location [0]), QueryJoinClause.InKeywordRole), QueryJoinClause.InKeywordRole);
3447
if (join.Expr != null)
3448
result.AddChild ((Expression)join.Expr.Accept (this), QueryJoinClause.InExpressionRole);
3450
if (location != null && location.Count > 1)
3451
result.AddChild (new CSharpTokenNode (Convert (location [1]), QueryJoinClause.OnKeywordRole), QueryJoinClause.OnKeywordRole);
3453
var outer = join.OuterSelector.Statements.FirstOrDefault () as ContextualReturn;
3455
result.AddChild ((Expression)outer.Expr.Accept (this), QueryJoinClause.OnExpressionRole);
3458
if (location != null && location.Count > 2)
3459
result.AddChild (new CSharpTokenNode (Convert (location [2]), QueryJoinClause.EqualsKeywordRole), QueryJoinClause.EqualsKeywordRole);
3460
var inner = join.InnerSelector.Statements.FirstOrDefault () as ContextualReturn;
3462
result.AddChild ((Expression)inner.Expr.Accept (this), QueryJoinClause.EqualsExpressionRole);
3464
if (location != null && location.Count > 3)
3465
result.AddChild (new CSharpTokenNode (Convert (location [3]), QueryJoinClause.IntoKeywordRole), QueryJoinClause.IntoKeywordRole);
3467
result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.IntoIdentifierRole);
3471
public override object Visit (Mono.CSharp.Linq.OrderByAscending orderByAscending)
3473
currentQueryOrderClause = new QueryOrderClause();
3474
var location2 = LocationsBag.GetLocations (orderByAscending.block);
3475
if (location2 != null)
3476
currentQueryOrderClause.AddChild (new CSharpTokenNode (Convert (location2 [0]), QueryOrderClause.OrderbyKeywordRole), QueryOrderClause.OrderbyKeywordRole);
3477
var ordering = new QueryOrdering ();
3478
if (orderByAscending.Expr != null)
3479
ordering.AddChild ((Expression)orderByAscending.Expr.Accept (this), Roles.Expression);
3480
var location = LocationsBag.GetLocations (orderByAscending);
3481
if (location != null) {
3482
ordering.Direction = QueryOrderingDirection.Ascending;
3483
ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.AscendingKeywordRole), QueryOrdering.AscendingKeywordRole);
3485
currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
3486
return currentQueryOrderClause;
3489
public override object Visit (Mono.CSharp.Linq.OrderByDescending orderByDescending)
3491
currentQueryOrderClause = new QueryOrderClause ();
3493
var ordering = new QueryOrdering ();
3494
if (orderByDescending.Expr != null)
3495
ordering.AddChild ((Expression)orderByDescending.Expr.Accept (this), Roles.Expression);
3496
var location = LocationsBag.GetLocations (orderByDescending);
3497
if (location != null) {
3498
ordering.Direction = QueryOrderingDirection.Descending;
3499
ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.DescendingKeywordRole), QueryOrdering.DescendingKeywordRole);
3501
currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
3502
return currentQueryOrderClause;
3505
public override object Visit (Mono.CSharp.Linq.ThenByAscending thenByAscending)
3507
var ordering = new QueryOrdering ();
3508
if (thenByAscending.Expr != null)
3509
ordering.AddChild ((Expression)thenByAscending.Expr.Accept (this), Roles.Expression);
3510
var location = LocationsBag.GetLocations (thenByAscending);
3511
if (location != null) {
3512
ordering.Direction = QueryOrderingDirection.Ascending;
3513
ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.AscendingKeywordRole), QueryOrdering.AscendingKeywordRole);
3515
currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
3519
public override object Visit (Mono.CSharp.Linq.ThenByDescending thenByDescending)
3521
var ordering = new QueryOrdering ();
3522
if (thenByDescending.Expr != null)
3523
ordering.AddChild ((Expression)thenByDescending.Expr.Accept (this), Roles.Expression);
3524
var location = LocationsBag.GetLocations (thenByDescending);
3525
if (location != null) {
3526
ordering.Direction = QueryOrderingDirection.Descending;
3527
ordering.AddChild (new CSharpTokenNode (Convert (location [0]), QueryOrdering.DescendingKeywordRole), QueryOrdering.DescendingKeywordRole);
3529
currentQueryOrderClause.AddChild (ordering, QueryOrderClause.OrderingRole);
3533
public override object Visit (Await awaitExpr)
3535
var result = new UnaryOperatorExpression ();
3536
result.Operator = UnaryOperatorType.Await;
3537
result.AddChild (new CSharpTokenNode (Convert (awaitExpr.Location), UnaryOperatorExpression.AwaitRole), UnaryOperatorExpression.AwaitRole);
3538
if (awaitExpr.Expression != null)
3539
result.AddChild ((Expression)awaitExpr.Expression.Accept (this), Roles.Expression);
3545
public DocumentationReference ConvertXmlDoc(DocumentationBuilder doc)
3547
DocumentationReference result = new DocumentationReference();
3548
if (doc.ParsedName != null) {
3549
if (doc.ParsedName.Name == "<this>") {
3550
result.EntityType = EntityType.Indexer;
3552
result.MemberName = doc.ParsedName.Name;
3554
if (doc.ParsedName.Left != null) {
3555
result.DeclaringType = ConvertToType(doc.ParsedName.Left);
3556
} else if (doc.ParsedBuiltinType != null) {
3557
result.DeclaringType = ConvertToType(doc.ParsedBuiltinType);
3559
if (doc.ParsedName.TypeParameters != null) {
3560
for (int i = 0; i < doc.ParsedName.TypeParameters.Count; i++) {
3561
result.TypeArguments.Add(ConvertToType(doc.ParsedName.TypeParameters[i]));
3564
} else if (doc.ParsedBuiltinType != null) {
3565
result.EntityType = EntityType.TypeDefinition;
3566
result.DeclaringType = ConvertToType(doc.ParsedBuiltinType);
3568
if (doc.ParsedParameters != null) {
3569
result.HasParameterList = true;
3570
result.Parameters.AddRange(doc.ParsedParameters.Select(ConvertXmlDocParameter));
3572
if (doc.ParsedOperator != null) {
3573
result.EntityType = EntityType.Operator;
3574
result.OperatorType = (OperatorType)doc.ParsedOperator;
3575
if (result.OperatorType == OperatorType.Implicit || result.OperatorType == OperatorType.Explicit) {
3576
var returnTypeParam = result.Parameters.LastOrNullObject();
3577
returnTypeParam.Remove(); // detach from parameter list
3578
var returnType = returnTypeParam.Type;
3579
returnType.Remove();
3580
result.ConversionOperatorReturnType = returnType;
3582
if (result.Parameters.Count == 0) {
3583
// reset HasParameterList if necessary
3584
result.HasParameterList = false;
3590
ParameterDeclaration ConvertXmlDocParameter(DocumentationParameter p)
3592
ParameterDeclaration result = new ParameterDeclaration();
3593
switch (p.Modifier) {
3594
case Parameter.Modifier.OUT:
3595
result.ParameterModifier = ParameterModifier.Out;
3597
case Parameter.Modifier.REF:
3598
result.ParameterModifier = ParameterModifier.Ref;
3600
case Parameter.Modifier.PARAMS:
3601
result.ParameterModifier = ParameterModifier.Params;
3604
if (p.Type != null) {
3605
result.Type = ConvertToType(p.Type);
3612
public CSharpParser ()
3614
compilerSettings = new CompilerSettings();
3617
public CSharpParser (CompilerSettings args)
3619
compilerSettings = args ?? new CompilerSettings();
3622
static AstNode GetOuterLeft (AstNode node)
3624
var outerLeft = node;
3625
while (outerLeft.FirstChild != null)
3626
outerLeft = outerLeft.FirstChild;
3630
static AstNode NextLeaf (AstNode node)
3634
var next = node.NextSibling;
3637
return GetOuterLeft (next);
3640
void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor)
3642
var leaf = GetOuterLeft (conversionVisitor.Unit);
3643
for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) {
3644
var special = top.SpecialsBag.Specials [i];
3645
AstNode newLeaf = null;
3646
var comment = special as SpecialsBag.Comment;
3647
if (comment != null) {
3648
// HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer
3649
bool isMultilineDocumentationComment = (
3650
comment.CommentType == SpecialsBag.CommentType.Multi
3651
&& comment.Content.StartsWith("*", StringComparison.Ordinal)
3652
&& !comment.Content.StartsWith("**", StringComparison.Ordinal)
3654
if (conversionVisitor.convertTypeSystemMode && !(comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment))
3656
var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType;
3657
var start = new TextLocation (comment.Line, comment.Col);
3658
var end = new TextLocation (comment.EndLine, comment.EndCol);
3659
newLeaf = new Comment (type, start, end) {
3660
StartsLine = comment.StartsLine,
3661
Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content
3663
} else if (!GenerateTypeSystemMode) {
3664
var directive = special as SpecialsBag.PreProcessorDirective;
3665
if (directive != null) {
3666
newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) {
3667
Argument = directive.Arg,
3668
Take = directive.Take
3671
var newLine = special as SpecialsBag.NewLineToken;
3672
if (newLine != null) {
3673
if (newLine.NewLine == SpecialsBag.NewLine.Unix) {
3674
newLeaf = new UnixNewLine (new TextLocation (newLine.Line, newLine.Col + 1));
3676
newLeaf = new WindowsNewLine (new TextLocation (newLine.Line, newLine.Col + 1));
3681
if (newLeaf == null)
3685
var nextLeaf = NextLeaf (leaf);
3686
// insert comment at begin
3687
if (newLeaf.StartLocation < leaf.StartLocation) {
3688
var node = leaf.Parent ?? conversionVisitor.Unit;
3689
while (node.Parent != null && node.FirstChild == leaf) {
3693
if (newLeaf is NewLineNode) {
3694
node.InsertChildBefore (leaf, (NewLineNode)newLeaf, Roles.NewLine);
3695
} else if (newLeaf is Comment) {
3696
node.InsertChildBefore (leaf, (Comment)newLeaf, Roles.Comment);
3698
node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3704
// insert comment at the end
3705
if (nextLeaf == null) {
3706
var node = leaf.Parent ?? conversionVisitor.Unit;
3707
if (newLeaf is NewLineNode) {
3708
node.AddChild ((NewLineNode)newLeaf, Roles.NewLine);
3709
} else if (newLeaf is Comment) {
3710
node.AddChild ((Comment)newLeaf, Roles.Comment);
3712
node.AddChild ((PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3718
// comment is between 2 nodes
3719
if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) {
3720
var node = leaf.Parent ?? conversionVisitor.Unit;
3721
if (newLeaf is NewLineNode) {
3722
node.InsertChildAfter (leaf, (NewLineNode)newLeaf, Roles.NewLine);
3723
} else if (newLeaf is Comment) {
3724
node.InsertChildAfter (leaf, (Comment)newLeaf, Roles.Comment);
3726
node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective);
3736
public class ErrorReportPrinter : ReportPrinter
3738
readonly string fileName;
3739
public readonly List<Error> Errors = new List<Error> ();
3741
public ErrorReportPrinter (string fileName)
3743
this.fileName = fileName;
3746
public override void Print (AbstractMessage msg, bool showFullPath)
3748
base.Print (msg, showFullPath);
3749
var newError = new Error (msg.IsWarning ? ErrorType.Warning : ErrorType.Error, msg.Text, new DomRegion (fileName, msg.Location.Row, msg.Location.Column));
3750
Errors.Add (newError);
3753
ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (null);
3755
[Obsolete("Use the Errors/Warnings/ErrorsAndWarnings properties instead")]
3756
public ErrorReportPrinter ErrorPrinter {
3758
return errorReportPrinter;
3761
public bool HasErrors {
3763
return errorReportPrinter.ErrorsCount > 0;
3767
public bool HasWarnings {
3769
return errorReportPrinter.WarningsCount > 0;
3773
public IEnumerable<Error> Errors {
3775
return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Error);
3779
public IEnumerable<Error> Warnings {
3781
return errorReportPrinter.Errors.Where(e => e.ErrorType == ErrorType.Warning);
3785
public IEnumerable<Error> ErrorsAndWarnings {
3786
get { return errorReportPrinter.Errors; }
3790
/// Parses a C# code file.
3792
/// <param name="program">The source code to parse.</param>
3793
/// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
3794
/// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
3795
/// <returns>Returns the syntax tree.</returns>
3796
public SyntaxTree Parse (string program, string fileName = "")
3798
return Parse (new StringTextSource (program), fileName);
3802
/// Parses a C# code file.
3804
/// <param name="reader">The text reader containing the source code to parse.</param>
3805
/// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
3806
/// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
3807
/// <returns>Returns the syntax tree.</returns>
3808
public SyntaxTree Parse (TextReader reader, string fileName = "")
3810
return Parse(new StringTextSource (reader.ReadToEnd ()), fileName);
3814
/// Converts a Mono.CSharp syntax tree into an NRefactory syntax tree.
3816
public SyntaxTree Parse(CompilerCompilationUnit top, string fileName)
3821
CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag);
3822
top.ModuleCompiled.Accept(conversionVisitor);
3823
InsertComments(top, conversionVisitor);
3824
if (CompilationUnitCallback != null) {
3825
CompilationUnitCallback(top);
3827
if (top.LastYYValue is Mono.CSharp.Expression) {
3828
conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode;
3831
conversionVisitor.Unit.FileName = fileName;
3832
conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.Concat (compilerSettings.ConditionalSymbols).ToArray ();
3833
return conversionVisitor.Unit;
3836
public CompilerSettings CompilerSettings {
3837
get { return compilerSettings; }
3840
throw new ArgumentNullException();
3841
compilerSettings = value;
3846
/// Callback that gets called with the Mono.CSharp syntax tree whenever some code is parsed.
3848
public Action<CompilerCompilationUnit> CompilationUnitCallback {
3854
/// Specifies whether to run the parser in a special mode for generating the type system.
3855
/// If this property is true, the syntax tree will only contain nodes relevant for the
3856
/// <see cref="SyntaxTree.ToTypeSystem()"/> call and might be missing other nodes (e.g. method bodies).
3857
/// The default is false.
3859
public bool GenerateTypeSystemMode {
3864
TextLocation initialLocation = new TextLocation(1, 1);
3867
/// Specifies the text location where parsing starts.
3868
/// This property can be used when parsing a part of a file to make the locations of the AstNodes
3869
/// refer to the position in the whole file.
3870
/// The default is (1,1).
3872
public TextLocation InitialLocation {
3873
get { return initialLocation; }
3874
set { initialLocation = value; }
3877
internal static object parseLock = new object ();
3880
/// Parses a C# code file.
3882
/// <param name="stream">The stream containing the source code to parse.</param>
3883
/// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
3884
/// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
3885
/// <returns>Returns the syntax tree.</returns>
3886
public SyntaxTree Parse (Stream stream, string fileName = "")
3888
return Parse (new StreamReader (stream), fileName);
3892
/// Parses a C# code file.
3894
/// <param name="program">The source code to parse.</param>
3895
/// <param name="fileName">The file name. Used to identify the file (e.g. when building a type system).
3896
/// This can be an arbitrary identifier, NRefactory never tries to access the file on disk.</param>
3898
/// <returns>Returns the syntax tree.</returns>
3899
public SyntaxTree Parse(ITextSource program, string fileName = "")
3901
return Parse(program, fileName, initialLocation.Line, initialLocation.Column);
3904
SyntaxTree Parse(ITextSource program, string fileName, int initialLine, int initialColumn)
3907
errorReportPrinter = new ErrorReportPrinter ("");
3908
var ctx = new CompilerContext (compilerSettings.ToMono(), errorReportPrinter);
3909
ctx.Settings.TabSize = 1;
3910
var reader = new SeekableStreamReader (program);
3911
var file = new SourceFile (fileName, fileName, 0);
3912
Location.Initialize (new List<SourceFile> (new [] { file }));
3913
var module = new ModuleContainer (ctx);
3914
var session = new ParserSession ();
3915
session.LocationsBag = new LocationsBag ();
3916
var report = new Report (ctx, errorReportPrinter);
3917
var parser = Driver.Parse (reader, file, module, session, report, initialLine - 1, initialColumn - 1);
3918
var top = new CompilerCompilationUnit () {
3919
ModuleCompiled = module,
3920
LocationsBag = session.LocationsBag,
3921
SpecialsBag = parser.Lexer.sbag,
3922
Conditionals = parser.Lexer.SourceFile.Conditionals
3924
var unit = Parse (top, fileName);
3925
unit.Errors.AddRange (errorReportPrinter.Errors);
3926
CompilerCallableEntryPoint.Reset ();
3931
public IEnumerable<EntityDeclaration> ParseTypeMembers (string code)
3933
return ParseTypeMembers(code, initialLocation.Line, initialLocation.Column);
3936
IEnumerable<EntityDeclaration> ParseTypeMembers (string code, int initialLine, int initialColumn)
3938
const string prefix = "unsafe partial class MyClass { ";
3939
var syntaxTree = Parse (new StringTextSource (prefix + code + "}"), "parsed.cs", initialLine, initialColumn - prefix.Length);
3940
if (syntaxTree == null)
3941
return Enumerable.Empty<EntityDeclaration> ();
3942
var td = syntaxTree.FirstChild as TypeDeclaration;
3944
var members = td.Members.ToArray();
3945
// detach members from parent
3946
foreach (var m in members)
3950
return Enumerable.Empty<EntityDeclaration> ();
3953
public IEnumerable<Statement> ParseStatements (string code)
3955
return ParseStatements(code, initialLocation.Line, initialLocation.Column);
3958
IEnumerable<Statement> ParseStatements (string code, int initialLine, int initialColumn)
3960
// the dummy method is async so that 'await' expressions are parsed as expected
3961
const string prefix = "async void M() { ";
3962
var members = ParseTypeMembers (prefix + code + "}", initialLine, initialColumn - prefix.Length);
3963
var method = members.FirstOrDefault () as MethodDeclaration;
3964
if (method != null && method.Body != null) {
3965
var statements = method.Body.Statements.ToArray();
3966
// detach statements from parent
3967
foreach (var st in statements)
3971
return Enumerable.Empty<Statement> ();
3974
public AstType ParseTypeReference (string code)
3976
var members = ParseTypeMembers (code + " a;");
3977
var field = members.FirstOrDefault () as FieldDeclaration;
3978
if (field != null) {
3979
AstType type = field.ReturnType;
3983
return AstType.Null;
3986
public Expression ParseExpression (string code)
3988
const string prefix = "tmp = ";
3989
var statements = ParseStatements (prefix + code + ";", initialLocation.Line, initialLocation.Column - prefix.Length);
3990
var es = statements.FirstOrDefault () as ExpressionStatement;
3992
AssignmentExpression ae = es.Expression as AssignmentExpression;
3994
Expression expr = ae.Right;
3999
return Expression.Null;
4004
/// Parses a file snippet; guessing what the code snippet represents (whole file, type members, block, type reference, expression).
4006
public AstNode ParseSnippet (string code)
4008
// TODO: add support for parsing a part of a file
4009
throw new NotImplementedException ();
4013
public DocumentationReference ParseDocumentationReference (string cref)
4015
// see Mono.CSharp.DocumentationBuilder.HandleXrefCommon
4017
throw new ArgumentNullException ("cref");
4019
// Additional symbols for < and > are allowed for easier XML typing
4020
cref = cref.Replace ('{', '<').Replace ('}', '>');
4023
errorReportPrinter = new ErrorReportPrinter("");
4024
var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter);
4025
ctx.Settings.TabSize = 1;
4026
var reader = new SeekableStreamReader(new StringTextSource (cref));
4027
var file = new SourceFile("", "", 0);
4028
Location.Initialize(new List<SourceFile> (new [] { file }));
4029
var module = new ModuleContainer(ctx);
4030
module.DocumentationBuilder = new DocumentationBuilder(module);
4031
var source_file = new CompilationSourceFile (module);
4032
var report = new Report (ctx, errorReportPrinter);
4033
ParserSession session = new ParserSession ();
4034
session.LocationsBag = new LocationsBag ();
4035
var parser = new Mono.CSharp.CSharpParser (reader, source_file, report, session);
4036
parser.Lexer.Line += initialLocation.Line - 1;
4037
parser.Lexer.Column += initialLocation.Column - 1;
4038
parser.Lexer.putback_char = Tokenizer.DocumentationXref;
4039
parser.Lexer.parsing_generic_declaration_doc = true;
4041
if (report.Errors > 0) {
4042
// Report.Warning (1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'",
4043
// mc.GetSignatureForError (), cref);
4046
ConversionVisitor conversionVisitor = new ConversionVisitor (false, session.LocationsBag);
4047
DocumentationReference docRef = conversionVisitor.ConvertXmlDoc(module.DocumentationBuilder);
4048
CompilerCallableEntryPoint.Reset();