-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathGreenNode.cs
1026 lines (863 loc) · 30.4 KB
/
GreenNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Syntax.InternalSyntax;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
[DebuggerDisplay("{GetDebuggerDisplay(), nq}")]
internal abstract class GreenNode : IObjectWritable
{
private string GetDebuggerDisplay()
{
return this.GetType().Name + " " + this.KindText + " " + this.ToString();
}
internal const int ListKind = 1;
private readonly ushort _kind;
protected NodeFlags flags;
private byte _slotCount;
private int _fullWidth;
private static readonly ConditionalWeakTable<GreenNode, DiagnosticInfo[]> s_diagnosticsTable =
new ConditionalWeakTable<GreenNode, DiagnosticInfo[]>();
private static readonly ConditionalWeakTable<GreenNode, SyntaxAnnotation[]> s_annotationsTable =
new ConditionalWeakTable<GreenNode, SyntaxAnnotation[]>();
private static readonly DiagnosticInfo[] s_noDiagnostics = Array.Empty<DiagnosticInfo>();
private static readonly SyntaxAnnotation[] s_noAnnotations = Array.Empty<SyntaxAnnotation>();
private static readonly IEnumerable<SyntaxAnnotation> s_noAnnotationsEnumerable = SpecializedCollections.EmptyEnumerable<SyntaxAnnotation>();
protected GreenNode(ushort kind)
{
_kind = kind;
}
protected GreenNode(ushort kind, int fullWidth)
{
_kind = kind;
_fullWidth = fullWidth;
}
protected GreenNode(ushort kind, DiagnosticInfo[] diagnostics, int fullWidth)
{
_kind = kind;
_fullWidth = fullWidth;
if (diagnostics?.Length > 0)
{
this.flags |= NodeFlags.ContainsDiagnostics;
s_diagnosticsTable.Add(this, diagnostics);
}
}
protected GreenNode(ushort kind, DiagnosticInfo[] diagnostics)
{
_kind = kind;
if (diagnostics?.Length > 0)
{
this.flags |= NodeFlags.ContainsDiagnostics;
s_diagnosticsTable.Add(this, diagnostics);
}
}
protected GreenNode(ushort kind, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations) :
this(kind, diagnostics)
{
if (annotations?.Length > 0)
{
foreach (var annotation in annotations)
{
if (annotation == null) throw new ArgumentException(paramName: nameof(annotations), message: "" /*CSharpResources.ElementsCannotBeNull*/);
}
this.flags |= NodeFlags.ContainsAnnotations;
s_annotationsTable.Add(this, annotations);
}
}
protected GreenNode(ushort kind, DiagnosticInfo[] diagnostics, SyntaxAnnotation[] annotations, int fullWidth) :
this(kind, diagnostics, fullWidth)
{
if (annotations?.Length > 0)
{
foreach (var annotation in annotations)
{
if (annotation == null) throw new ArgumentException(paramName: nameof(annotations), message: "" /*CSharpResources.ElementsCannotBeNull*/);
}
this.flags |= NodeFlags.ContainsAnnotations;
s_annotationsTable.Add(this, annotations);
}
}
protected void AdjustFlagsAndWidth(GreenNode node)
{
Debug.Assert(node != null, "PERF: caller must ensure that node!=null, we do not want to re-check that here.");
this.flags |= (node.flags & NodeFlags.InheritMask);
_fullWidth += node._fullWidth;
}
public abstract string Language { get; }
#region Kind
public int RawKind
{
get { return _kind; }
}
public bool IsList
{
get
{
return RawKind == ListKind;
}
}
public abstract string KindText { get; }
public virtual bool IsStructuredTrivia => false;
public virtual bool IsDirective => false;
public virtual bool IsToken => false;
public virtual bool IsTrivia => false;
public virtual bool IsSkippedTokensTrivia => false;
public virtual bool IsDocumentationCommentTrivia => false;
#endregion
#region Slots
public int SlotCount
{
get
{
int count = _slotCount;
if (count == byte.MaxValue)
{
count = GetSlotCount();
}
return count;
}
protected set
{
_slotCount = (byte)value;
}
}
internal abstract GreenNode GetSlot(int index);
// for slot counts >= byte.MaxValue
protected virtual int GetSlotCount()
{
return _slotCount;
}
public virtual int GetSlotOffset(int index)
{
int offset = 0;
for (int i = 0; i < index; i++)
{
var child = this.GetSlot(i);
if (child != null)
{
offset += child.FullWidth;
}
}
return offset;
}
internal Syntax.InternalSyntax.ChildSyntaxList ChildNodesAndTokens()
{
return new Syntax.InternalSyntax.ChildSyntaxList(this);
}
/// <summary>
/// Enumerates all nodes of the tree rooted by this node (including this node).
/// </summary>
internal IEnumerable<GreenNode> EnumerateNodes()
{
yield return this;
var stack = new Stack<Syntax.InternalSyntax.ChildSyntaxList.Enumerator>(24);
stack.Push(this.ChildNodesAndTokens().GetEnumerator());
while (stack.Count > 0)
{
var en = stack.Pop();
if (!en.MoveNext())
{
// no more down this branch
continue;
}
var current = en.Current;
stack.Push(en); // put it back on stack (struct enumerator)
yield return current;
if (!current.IsToken)
{
// not token, so consider children
stack.Push(current.ChildNodesAndTokens().GetEnumerator());
continue;
}
}
}
/// <summary>
/// Find the slot that contains the given offset.
/// </summary>
/// <param name="offset">The target offset. Must be between 0 and <see cref="FullWidth"/>.</param>
/// <returns>The slot index of the slot containing the given offset.</returns>
/// <remarks>
/// The base implementation is a linear search. This should be overridden
/// if a derived class can implement it more efficiently.
/// </remarks>
public virtual int FindSlotIndexContainingOffset(int offset)
{
Debug.Assert(0 <= offset && offset < FullWidth);
int i;
int accumulatedWidth = 0;
for (i = 0; ; i++)
{
Debug.Assert(i < SlotCount);
var child = GetSlot(i);
if (child != null)
{
accumulatedWidth += child.FullWidth;
if (offset < accumulatedWidth)
{
break;
}
}
}
return i;
}
#endregion
#region Flags
[Flags]
internal enum NodeFlags : byte
{
None = 0,
ContainsDiagnostics = 1 << 0,
ContainsStructuredTrivia = 1 << 1,
ContainsDirectives = 1 << 2,
ContainsSkippedText = 1 << 3,
ContainsAnnotations = 1 << 4,
IsNotMissing = 1 << 5,
FactoryContextIsInAsync = 1 << 6,
FactoryContextIsInQuery = 1 << 7,
FactoryContextIsInIterator = FactoryContextIsInQuery, // VB does not use "InQuery", but uses "InIterator" instead
InheritMask = ContainsDiagnostics | ContainsStructuredTrivia | ContainsDirectives | ContainsSkippedText | ContainsAnnotations | IsNotMissing,
}
internal NodeFlags Flags
{
get { return this.flags; }
}
internal void SetFlags(NodeFlags flags)
{
this.flags |= flags;
}
internal void ClearFlags(NodeFlags flags)
{
this.flags &= ~flags;
}
internal bool IsMissing
{
get
{
// flag has reversed meaning hence "=="
return (this.flags & NodeFlags.IsNotMissing) == 0;
}
}
internal bool ParsedInAsync
{
get
{
return (this.flags & NodeFlags.FactoryContextIsInAsync) != 0;
}
}
internal bool ParsedInQuery
{
get
{
return (this.flags & NodeFlags.FactoryContextIsInQuery) != 0;
}
}
internal bool ParsedInIterator
{
get
{
return (this.flags & NodeFlags.FactoryContextIsInIterator) != 0;
}
}
public bool ContainsSkippedText
{
get
{
return (this.flags & NodeFlags.ContainsSkippedText) != 0;
}
}
public bool ContainsStructuredTrivia
{
get
{
return (this.flags & NodeFlags.ContainsStructuredTrivia) != 0;
}
}
public bool ContainsDirectives
{
get
{
return (this.flags & NodeFlags.ContainsDirectives) != 0;
}
}
public bool ContainsDiagnostics
{
get
{
return (this.flags & NodeFlags.ContainsDiagnostics) != 0;
}
}
public bool ContainsAnnotations
{
get
{
return (this.flags & NodeFlags.ContainsAnnotations) != 0;
}
}
#endregion
#region Spans
public int FullWidth
{
get
{
return _fullWidth;
}
protected set
{
_fullWidth = value;
}
}
public virtual int Width
{
get
{
return _fullWidth - this.GetLeadingTriviaWidth() - this.GetTrailingTriviaWidth();
}
}
public virtual int GetLeadingTriviaWidth()
{
return this.FullWidth != 0 ?
this.GetFirstTerminal().GetLeadingTriviaWidth() :
0;
}
public virtual int GetTrailingTriviaWidth()
{
return this.FullWidth != 0 ?
this.GetLastTerminal().GetTrailingTriviaWidth() :
0;
}
public bool HasLeadingTrivia
{
get
{
return this.GetLeadingTriviaWidth() != 0;
}
}
public bool HasTrailingTrivia
{
get
{
return this.GetTrailingTriviaWidth() != 0;
}
}
#endregion
#region Serialization
// use high-bit on Kind to identify serialization of extra info
private const UInt16 ExtendedSerializationInfoMask = unchecked((UInt16)(1u << 15));
internal GreenNode(ObjectReader reader)
{
var kindBits = reader.ReadUInt16();
_kind = (ushort)(kindBits & ~ExtendedSerializationInfoMask);
if ((kindBits & ExtendedSerializationInfoMask) != 0)
{
var diagnostics = (DiagnosticInfo[])reader.ReadValue();
if (diagnostics != null && diagnostics.Length > 0)
{
this.flags |= NodeFlags.ContainsDiagnostics;
s_diagnosticsTable.Add(this, diagnostics);
}
var annotations = (SyntaxAnnotation[])reader.ReadValue();
if (annotations != null && annotations.Length > 0)
{
this.flags |= NodeFlags.ContainsAnnotations;
s_annotationsTable.Add(this, annotations);
}
}
}
bool IObjectWritable.ShouldReuseInSerialization => ShouldReuseInSerialization;
internal virtual bool ShouldReuseInSerialization => this.IsCacheable;
void IObjectWritable.WriteTo(ObjectWriter writer)
{
this.WriteTo(writer);
}
internal virtual void WriteTo(ObjectWriter writer)
{
var kindBits = (UInt16)_kind;
var hasDiagnostics = this.GetDiagnostics().Length > 0;
var hasAnnotations = this.GetAnnotations().Length > 0;
if (hasDiagnostics || hasAnnotations)
{
kindBits |= ExtendedSerializationInfoMask;
writer.WriteUInt16(kindBits);
writer.WriteValue(hasDiagnostics ? this.GetDiagnostics() : null);
writer.WriteValue(hasAnnotations ? this.GetAnnotations() : null);
}
else
{
writer.WriteUInt16(kindBits);
}
}
#endregion
#region Annotations
public bool HasAnnotations(string annotationKind)
{
var annotations = this.GetAnnotations();
if (annotations == s_noAnnotations)
{
return false;
}
foreach (var a in annotations)
{
if (a.Kind == annotationKind)
{
return true;
}
}
return false;
}
public bool HasAnnotations(IEnumerable<string> annotationKinds)
{
var annotations = this.GetAnnotations();
if (annotations == s_noAnnotations)
{
return false;
}
foreach (var a in annotations)
{
if (annotationKinds.Contains(a.Kind))
{
return true;
}
}
return false;
}
public bool HasAnnotation(SyntaxAnnotation annotation)
{
var annotations = this.GetAnnotations();
if (annotations == s_noAnnotations)
{
return false;
}
foreach (var a in annotations)
{
if (a == annotation)
{
return true;
}
}
return false;
}
public IEnumerable<SyntaxAnnotation> GetAnnotations(string annotationKind)
{
if (string.IsNullOrWhiteSpace(annotationKind))
{
throw new ArgumentNullException(nameof(annotationKind));
}
var annotations = this.GetAnnotations();
if (annotations == s_noAnnotations)
{
return s_noAnnotationsEnumerable;
}
return GetAnnotationsSlow(annotations, annotationKind);
}
private static IEnumerable<SyntaxAnnotation> GetAnnotationsSlow(SyntaxAnnotation[] annotations, string annotationKind)
{
foreach (var annotation in annotations)
{
if (annotation.Kind == annotationKind)
{
yield return annotation;
}
}
}
public IEnumerable<SyntaxAnnotation> GetAnnotations(IEnumerable<string> annotationKinds)
{
if (annotationKinds == null)
{
throw new ArgumentNullException(nameof(annotationKinds));
}
var annotations = this.GetAnnotations();
if (annotations == s_noAnnotations)
{
return s_noAnnotationsEnumerable;
}
return GetAnnotationsSlow(annotations, annotationKinds);
}
private static IEnumerable<SyntaxAnnotation> GetAnnotationsSlow(SyntaxAnnotation[] annotations, IEnumerable<string> annotationKinds)
{
foreach (var annotation in annotations)
{
if (annotationKinds.Contains(annotation.Kind))
{
yield return annotation;
}
}
}
public SyntaxAnnotation[] GetAnnotations()
{
if (this.ContainsAnnotations)
{
SyntaxAnnotation[] annotations;
if (s_annotationsTable.TryGetValue(this, out annotations))
{
System.Diagnostics.Debug.Assert(annotations.Length != 0, "we should return nonempty annotations or NoAnnotations");
return annotations;
}
}
return s_noAnnotations;
}
internal abstract GreenNode SetAnnotations(SyntaxAnnotation[] annotations);
#endregion
#region Diagnostics
internal DiagnosticInfo[] GetDiagnostics()
{
if (this.ContainsDiagnostics)
{
DiagnosticInfo[] diags;
if (s_diagnosticsTable.TryGetValue(this, out diags))
{
return diags;
}
}
return s_noDiagnostics;
}
internal abstract GreenNode SetDiagnostics(DiagnosticInfo[] diagnostics);
#endregion
#region Text
public virtual string ToFullString()
{
var sb = PooledStringBuilder.GetInstance();
var writer = new System.IO.StringWriter(sb.Builder, System.Globalization.CultureInfo.InvariantCulture);
this.WriteTo(writer, leading: true, trailing: true);
return sb.ToStringAndFree();
}
public override string ToString()
{
var sb = PooledStringBuilder.GetInstance();
var writer = new System.IO.StringWriter(sb.Builder, System.Globalization.CultureInfo.InvariantCulture);
this.WriteTo(writer, leading: false, trailing: false);
return sb.ToStringAndFree();
}
public void WriteTo(System.IO.TextWriter writer)
{
this.WriteTo(writer, leading: true, trailing: true);
}
protected internal void WriteTo(TextWriter writer, bool leading, bool trailing)
{
// Use an actual Stack so we can write out deeply recursive structures without overflowing.
var stack = new Stack<(GreenNode node, bool leading, bool trailing)>();
stack.Push((this, leading, trailing));
// Separated out stack processing logic so that it does not unintentionally refer to
// "this", "leading" or "trailing.
ProcessStack(writer, stack);
}
private static void ProcessStack(TextWriter writer,
Stack<(GreenNode node, bool leading, bool trailing)> stack)
{
while (stack.Count > 0)
{
var current = stack.Pop();
var currentNode = current.node;
var currentLeading = current.leading;
var currentTrailing = current.trailing;
if (currentNode.IsToken)
{
currentNode.WriteTokenTo(writer, currentLeading, currentTrailing);
continue;
}
if (currentNode.IsTrivia)
{
currentNode.WriteTriviaTo(writer);
continue;
}
var firstIndex = GetFirstNonNullChildIndex(currentNode);
var lastIndex = GetLastNonNullChildIndex(currentNode);
for (var i = lastIndex; i >= firstIndex; i--)
{
var child = currentNode.GetSlot(i);
if (child != null)
{
var first = i == firstIndex;
var last = i == lastIndex;
stack.Push((child, currentLeading | !first, currentTrailing | !last));
}
}
}
}
private static int GetFirstNonNullChildIndex(GreenNode node)
{
int n = node.SlotCount;
int firstIndex = 0;
for (; firstIndex < n; firstIndex++)
{
var child = node.GetSlot(firstIndex);
if (child != null)
{
break;
}
}
return firstIndex;
}
private static int GetLastNonNullChildIndex(GreenNode node)
{
int n = node.SlotCount;
int lastIndex = n - 1;
for (; lastIndex >= 0; lastIndex--)
{
var child = node.GetSlot(lastIndex);
if (child != null)
{
break;
}
}
return lastIndex;
}
protected virtual void WriteTriviaTo(TextWriter writer)
{
throw new NotImplementedException();
}
protected virtual void WriteTokenTo(TextWriter writer, bool leading, bool trailing)
{
throw new NotImplementedException();
}
#endregion
#region Tokens
public virtual int RawContextualKind { get { return this.RawKind; } }
public virtual object GetValue() { return null; }
public virtual string GetValueText() { return string.Empty; }
public virtual GreenNode GetLeadingTriviaCore() { return null; }
public virtual GreenNode GetTrailingTriviaCore() { return null; }
public virtual GreenNode WithLeadingTrivia(GreenNode trivia)
{
return this;
}
public virtual GreenNode WithTrailingTrivia(GreenNode trivia)
{
return this;
}
internal GreenNode GetFirstTerminal()
{
GreenNode node = this;
do
{
GreenNode firstChild = null;
for (int i = 0, n = node.SlotCount; i < n; i++)
{
var child = node.GetSlot(i);
if (child != null)
{
firstChild = child;
break;
}
}
node = firstChild;
} while (node?._slotCount > 0);
return node;
}
internal GreenNode GetLastTerminal()
{
GreenNode node = this;
do
{
GreenNode lastChild = null;
for (int i = node.SlotCount - 1; i >= 0; i--)
{
var child = node.GetSlot(i);
if (child != null)
{
lastChild = child;
break;
}
}
node = lastChild;
} while (node?._slotCount > 0);
return node;
}
internal GreenNode GetLastNonmissingTerminal()
{
GreenNode node = this;
do
{
GreenNode nonmissingChild = null;
for (int i = node.SlotCount - 1; i >= 0; i--)
{
var child = node.GetSlot(i);
if (child != null && !child.IsMissing)
{
nonmissingChild = child;
break;
}
}
node = nonmissingChild;
}
while (node?._slotCount > 0);
return node;
}
#endregion
#region Equivalence
public virtual bool IsEquivalentTo(GreenNode other)
{
if (this == other)
{
return true;
}
if (other == null)
{
return false;
}
return EquivalentToInternal(this, other);
}
private static bool EquivalentToInternal(GreenNode node1, GreenNode node2)
{
if (node1.RawKind != node2.RawKind)
{
// A single-element list is usually represented as just a single node,
// but can be represented as a List node with one child. Move to that
// child if necessary.
if (node1.IsList && node1.SlotCount == 1)
{
node1 = node1.GetSlot(0);
}
if (node2.IsList && node2.SlotCount == 1)
{
node2 = node2.GetSlot(0);
}
if (node1.RawKind != node2.RawKind)
{
return false;
}
}
if (node1._fullWidth != node2._fullWidth)
{
return false;
}
var n = node1.SlotCount;
if (n != node2.SlotCount)
{
return false;
}
for (int i = 0; i < n; i++)
{
var node1Child = node1.GetSlot(i);
var node2Child = node2.GetSlot(i);
if (node1Child != null && node2Child != null && !node1Child.IsEquivalentTo(node2Child))
{
return false;
}
}
return true;
}
#endregion
public abstract SyntaxNode GetStructure(SyntaxTrivia parentTrivia);
#region Factories
public abstract SyntaxToken CreateSeparator<TNode>(SyntaxNode element) where TNode : SyntaxNode;
public abstract bool IsTriviaWithEndOfLine(); // trivia node has end of line
public virtual GreenNode CreateList(IEnumerable<GreenNode> nodes, bool alwaysCreateListNode = false)
{
if (nodes == null)
{
return null;
}
var list = nodes.ToArray();
switch (list.Length)
{
case 0:
return null;
case 1:
if (alwaysCreateListNode)
{
goto default;
}
else
{
return list[0];
}
case 2:
return SyntaxList.List(list[0], list[1]);
case 3:
return SyntaxList.List(list[0], list[1], list[2]);
default:
return SyntaxList.List(list);
}
}
public SyntaxNode CreateRed()
{
return CreateRed(null, 0);
}
internal abstract SyntaxNode CreateRed(SyntaxNode parent, int position);
#endregion
#region Caching
internal const int MaxCachedChildNum = 3;
internal bool IsCacheable
{
get
{
return ((this.flags & NodeFlags.InheritMask) == NodeFlags.IsNotMissing) &&
this.SlotCount <= GreenNode.MaxCachedChildNum;
}
}
internal int GetCacheHash()
{
Debug.Assert(this.IsCacheable);
int code = (int)(this.flags) ^ this.RawKind;
int cnt = this.SlotCount;
for (int i = 0; i < cnt; i++)
{
var child = GetSlot(i);
if (child != null)
{
code = Hash.Combine(RuntimeHelpers.GetHashCode(child), code);
}
}
return code & Int32.MaxValue;
}
internal bool IsCacheEquivalent(int kind, NodeFlags flags, GreenNode child1)
{
Debug.Assert(this.IsCacheable);
return this.RawKind == kind &&
this.flags == flags &&
this.GetSlot(0) == child1;
}
internal bool IsCacheEquivalent(int kind, NodeFlags flags, GreenNode child1, GreenNode child2)
{
Debug.Assert(this.IsCacheable);
return this.RawKind == kind &&
this.flags == flags &&
this.GetSlot(0) == child1 &&
this.GetSlot(1) == child2;
}
internal bool IsCacheEquivalent(int kind, NodeFlags flags, GreenNode child1, GreenNode child2, GreenNode child3)
{
Debug.Assert(this.IsCacheable);
return this.RawKind == kind &&
this.flags == flags &&
this.GetSlot(0) == child1 &&
this.GetSlot(1) == child2 &&
this.GetSlot(2) == child3;
}
#endregion //Caching
/// <summary>
/// Add an error to the given node, creating a new node that is the same except it has no parent,
/// and has the given error attached to it. The error span is the entire span of this node.
/// </summary>
/// <param name="err">The error to attach to this node</param>