-
Notifications
You must be signed in to change notification settings - Fork 4k
/
VisualBasicSyntaxNode.vb
477 lines (408 loc) · 22.9 KB
/
VisualBasicSyntaxNode.vb
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
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
' See the LICENSE file in the project root for more information.
Imports System.Collections.Immutable
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Reflection
Imports System.Threading
Imports Microsoft.CodeAnalysis.ErrorReporting
Imports Microsoft.CodeAnalysis.PooledObjects
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic
''' <summary>
''' The base class for all nodes in the VB syntax tree.
''' </summary>
Partial Public MustInherit Class VisualBasicSyntaxNode
Inherits SyntaxNode
' Constructor. Called only by derived classes.
Friend Sub New(green As GreenNode, parent As SyntaxNode, position As Integer)
MyBase.New(green, parent, position)
End Sub
''' <summary>
''' Used by structured trivia which has no parent node, so need to know syntax tree explicitly
''' </summary>
Friend Sub New(green As GreenNode, position As Integer, syntaxTree As SyntaxTree)
MyBase.New(green, Nothing, position)
_syntaxTree = syntaxTree
End Sub
'TODO: may be eventually not needed
Friend ReadOnly Property VbGreen As InternalSyntax.VisualBasicSyntaxNode
Get
Return DirectCast(Me.Green, InternalSyntax.VisualBasicSyntaxNode)
End Get
End Property
''' <summary>
''' Returns a non-null SyntaxTree that owns this node.
''' If this node was created with an explicit non-null SyntaxTree, returns that tree.
''' Otherwise, if this node has a non-null parent, then returns the parent's SyntaxTree.
''' Otherwise, returns a newly created SyntaxTree rooted at this node, preserving this node's reference identity.
''' </summary>
Friend Shadows ReadOnly Property SyntaxTree As SyntaxTree
Get
If Me._syntaxTree Is Nothing Then
Dim stack = ArrayBuilder(Of SyntaxNode).GetInstance()
Dim tree As SyntaxTree = Nothing
Dim current As SyntaxNode = Me
Dim rootCandidate As SyntaxNode = Nothing
While current IsNot Nothing
tree = current._syntaxTree
If tree IsNot Nothing Then
Exit While
End If
rootCandidate = current
stack.Push(current)
current = rootCandidate.Parent
End While
If tree Is Nothing Then
Debug.Assert(rootCandidate IsNot Nothing)
#Disable Warning RS0030 ' Do not use banned APIs (CreateWithoutClone is intended to be used from this call site only)
tree = VisualBasicSyntaxTree.CreateWithoutClone(DirectCast(rootCandidate, VisualBasicSyntaxNode))
#Enable Warning RS0030
End If
Debug.Assert(tree IsNot Nothing)
While stack.Count > 0
Dim alternativeTree As SyntaxTree = Interlocked.CompareExchange(stack.Pop()._syntaxTree, tree, Nothing)
If alternativeTree IsNot Nothing Then
tree = alternativeTree
End If
End While
stack.Free()
End If
Return Me._syntaxTree
End Get
End Property
Public MustOverride Function Accept(Of TResult)(visitor As VisualBasicSyntaxVisitor(Of TResult)) As TResult
Public MustOverride Sub Accept(visitor As VisualBasicSyntaxVisitor)
''' <summary>
''' Returns the <see cref="SyntaxKind"/> of the node.
''' </summary>
Public Function Kind() As SyntaxKind
Return CType(Me.Green.RawKind, SyntaxKind)
End Function
''' <summary>
''' The language name this node is syntax of.
''' </summary>
Public Overrides ReadOnly Property Language As String
Get
Return LanguageNames.VisualBasic
End Get
End Property
''' <summary>
''' The parent of this node.
''' </summary>
''' <value>The parent node of this node, or Nothing if this node is the root.</value>
Friend Shadows ReadOnly Property Parent As VisualBasicSyntaxNode
Get
Return DirectCast(MyBase.Parent, VisualBasicSyntaxNode)
End Get
End Property
#Region "Serialization"
''' <summary>
''' Deserialize a syntax node from a byte stream.
''' </summary>
<Obsolete(SerializationDeprecationException.Text, True)>
Public Shared Function DeserializeFrom(stream As IO.Stream, Optional cancellationToken As CancellationToken = Nothing) As SyntaxNode
Throw New SerializationDeprecationException()
End Function
#End Region
''' <summary>
''' Returns True if this node represents a directive.
''' </summary>
Public ReadOnly Property IsDirective As Boolean
Get
Return Me.Green.IsDirective
End Get
End Property
''' <summary>
''' Same as accessing <see cref="TextSpan.Start"/> on <see cref="Span"/>.
''' </summary>
''' <remarks>
''' Slight performance improvement.
''' </remarks>
Public Shadows ReadOnly Property SpanStart As Integer
Get
Return Position + Me.Green.GetLeadingTriviaWidth()
End Get
End Property
''' <summary>
''' Get the preceding trivia nodes of this node. If this node is a token, returns the preceding trivia
''' associated with this node. If this is a non-terminal, returns the preceding trivia of the first token
''' of this node.
''' </summary>
''' <returns>A list of the preceding trivia.</returns>
''' <remarks>If this node is a non-terminal, the parents of the trivia will be the first token of this
''' non-terminal; NOT this node.</remarks>
Public Shadows Function GetLeadingTrivia() As SyntaxTriviaList
Return GetFirstToken(includeZeroWidth:=True).LeadingTrivia
End Function
''' <summary>
''' Get the following trivia nodes of this node. If this node is a token, returns the following trivia
''' associated with this node. If this is a non-terminal, returns the following trivia of the last token
''' of this node.
''' </summary>
''' <returns>A list of the following trivia.</returns>
''' <remarks>If this node is a non-terminal, the parents of the trivia will be the first token of this
''' non-terminal; NOT this node.</remarks>
Public Shadows Function GetTrailingTrivia() As SyntaxTriviaList
Return GetLastToken(includeZeroWidth:=True).TrailingTrivia
End Function
' an empty collection of syntax errors.
Friend Shared EmptyErrorCollection As New ReadOnlyCollection(Of Diagnostic)(Array.Empty(Of Diagnostic))
''' <summary>
''' Get all syntax errors associated with this node, or any child nodes, grand-child nodes, etc. The errors
''' are not in order.
''' </summary>
Friend Function GetSyntaxErrors(tree As SyntaxTree) As ReadOnlyCollection(Of Diagnostic)
Return DoGetSyntaxErrors(tree, Me)
End Function
Friend Shared Function DoGetSyntaxErrors(tree As SyntaxTree, nodeOrToken As SyntaxNodeOrToken) As ReadOnlyCollection(Of Diagnostic)
If Not nodeOrToken.ContainsDiagnostics Then
Return EmptyErrorCollection
Else
' Accumulated a stack of nodes with errors to process.
Dim nodesToProcess As New Stack(Of SyntaxNodeOrToken)
Dim errorList As New List(Of Diagnostic)
nodesToProcess.Push(nodeOrToken)
While nodesToProcess.Count > 0
' Add errors from current node being processed to the list
nodeOrToken = nodesToProcess.Pop()
Dim node = nodeOrToken.UnderlyingNode
If node.ContainsDiagnostics Then
Dim errors = DirectCast(node, Syntax.InternalSyntax.VisualBasicSyntaxNode).GetDiagnostics
If errors IsNot Nothing Then
For i = 0 To errors.Length - 1
Dim greenError = errors(i)
Debug.Assert(greenError IsNot Nothing)
errorList.Add(CreateSyntaxError(tree, nodeOrToken, greenError))
Next
End If
End If
' Children or trivia must have errors too, based on the count. Add them.
If Not nodeOrToken.IsToken Then
PushNodesWithErrors(nodesToProcess, nodeOrToken.ChildNodesAndTokens())
ElseIf nodeOrToken.IsToken Then
ProcessTrivia(tree, errorList, nodesToProcess, nodeOrToken.GetLeadingTrivia())
ProcessTrivia(tree, errorList, nodesToProcess, nodeOrToken.GetTrailingTrivia())
End If
End While
Return New ReadOnlyCollection(Of Diagnostic)(errorList)
End If
End Function
''' <summary>
''' Push any nodes that have errors in the given collection onto a stack
''' </summary>
Private Shared Sub PushNodesWithErrors(stack As Stack(Of SyntaxNodeOrToken), nodes As ChildSyntaxList)
Debug.Assert(stack IsNot Nothing)
For Each n In nodes
Debug.Assert(Not n.IsKind(SyntaxKind.None))
If n.ContainsDiagnostics Then
stack.Push(n)
End If
Next
End Sub
Private Shared Sub ProcessTrivia(tree As SyntaxTree,
errorList As List(Of Diagnostic),
stack As Stack(Of SyntaxNodeOrToken),
nodes As SyntaxTriviaList)
Debug.Assert(stack IsNot Nothing)
For Each n In nodes
Debug.Assert(n.Kind <> SyntaxKind.None)
If n.UnderlyingNode.ContainsDiagnostics Then
If n.HasStructure Then
stack.Push(DirectCast(n.GetStructure, VisualBasicSyntaxNode))
Else
Dim errors = DirectCast(n.UnderlyingNode, InternalSyntax.VisualBasicSyntaxNode).GetDiagnostics
If errors IsNot Nothing Then
For i = 0 To errors.Length - 1
Dim e = errors(i)
errorList.Add(CreateSyntaxError(tree, n, e))
Next
End If
End If
End If
Next
End Sub
''' <summary>
''' Given a error info from this node, create the corresponding syntax error with the right span.
''' </summary>
Private Shared Function CreateSyntaxError(tree As SyntaxTree, nodeOrToken As SyntaxNodeOrToken, errorInfo As DiagnosticInfo) As Diagnostic
Debug.Assert(errorInfo IsNot Nothing)
' Translate the green error offset/width relative to my location.
Return New VBDiagnostic(errorInfo, If(tree Is Nothing, New SourceLocation(tree, nodeOrToken.Span), tree.GetLocation(nodeOrToken.Span)))
End Function
Private Shared Function CreateSyntaxError(tree As SyntaxTree, nodeOrToken As SyntaxTrivia, errorInfo As DiagnosticInfo) As Diagnostic
Debug.Assert(errorInfo IsNot Nothing)
' Translate the green error offset/width relative to my location.
Return New VBDiagnostic(errorInfo, If(tree Is Nothing, New SourceLocation(tree, nodeOrToken.Span), tree.GetLocation(nodeOrToken.Span)))
End Function
''' <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>
''' <returns>A new node, with no parent, that has this error added to it.</returns>
''' <remarks>Since nodes are immutable, the only way to create nodes with errors attached is to create a node without an error,
''' then add an error with this method to create another node.</remarks>
Friend Function AddError(err As DiagnosticInfo) As VisualBasicSyntaxNode
Dim errorInfos() As DiagnosticInfo
' If the green node already has errors, add those on.
If Me.Green.GetDiagnostics Is Nothing Then
errorInfos = {err}
Else
' Add the error to the error list.
errorInfos = Me.Green.GetDiagnostics
Dim length As Integer = errorInfos.Length
ReDim Preserve errorInfos(length)
errorInfos(length) = err
End If
' Get a new green node with the errors added on.
Dim greenWithDiagnostics = Me.Green.SetDiagnostics(errorInfos)
' convert to red node with no parent.
Dim result = greenWithDiagnostics.CreateRed(Nothing, 0)
Debug.Assert(result IsNot Nothing)
Return DirectCast(result, VisualBasicSyntaxNode)
End Function
Public Shadows Function GetFirstToken(Optional includeZeroWidth As Boolean = False,
Optional includeSkipped As Boolean = False,
Optional includeDirectives As Boolean = False,
Optional includeDocumentationComments As Boolean = False) As SyntaxToken
Return CType(MyBase.GetFirstToken(includeZeroWidth, includeSkipped, includeDirectives, includeDocumentationComments), SyntaxToken)
End Function
Public Shadows Function GetLastToken(Optional includeZeroWidth As Boolean = False,
Optional includeSkipped As Boolean = False,
Optional includeDirectives As Boolean = False,
Optional includeDocumentationComments As Boolean = False) As SyntaxToken
Return CType(MyBase.GetLastToken(includeZeroWidth, includeSkipped, includeDirectives, includeDocumentationComments), SyntaxToken)
End Function
Public Function GetDirectives(Optional filter As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As IList(Of DirectiveTriviaSyntax)
Return (CType(Me, SyntaxNodeOrToken)).GetDirectives(Of DirectiveTriviaSyntax)(filter)
End Function
Public Function GetFirstDirective(Optional predicate As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As DirectiveTriviaSyntax
Dim child As SyntaxNodeOrToken
For Each child In Me.ChildNodesAndTokens()
If child.ContainsDirectives Then
If child.IsNode Then
Dim d As DirectiveTriviaSyntax = DirectCast(child.AsNode, VisualBasicSyntaxNode).GetFirstDirective(predicate)
If d IsNot Nothing Then
Return d
End If
Else
Dim tr As SyntaxTrivia
For Each tr In child.AsToken.LeadingTrivia
If tr.IsDirective Then
Dim d As DirectiveTriviaSyntax = DirectCast(tr.GetStructure, DirectiveTriviaSyntax)
If ((predicate Is Nothing) OrElse predicate(d)) Then
Return d
End If
End If
Next
Continue For
End If
End If
Next
Return Nothing
End Function
Public Function GetLastDirective(Optional predicate As Func(Of DirectiveTriviaSyntax, Boolean) = Nothing) As DirectiveTriviaSyntax
Dim child As SyntaxNodeOrToken
For Each child In Me.ChildNodesAndTokens().Reverse
If child.ContainsDirectives Then
If child.IsNode Then
Dim d As DirectiveTriviaSyntax = DirectCast(child.AsNode, VisualBasicSyntaxNode).GetLastDirective(predicate)
If d IsNot Nothing Then
Return d
End If
Else
Dim token As SyntaxToken = child.AsToken
For Each tr In token.LeadingTrivia.Reverse()
If tr.IsDirective Then
Dim d As DirectiveTriviaSyntax = DirectCast(tr.GetStructure, DirectiveTriviaSyntax)
If ((predicate Is Nothing) OrElse predicate(d)) Then
Return d
End If
End If
Next
End If
End If
Next
Return Nothing
End Function
#Region "Core Overloads"
Protected Overrides ReadOnly Property SyntaxTreeCore As SyntaxTree
Get
Return Me.SyntaxTree
End Get
End Property
Protected Overrides Function ReplaceCore(Of TNode As SyntaxNode)(
Optional nodes As IEnumerable(Of TNode) = Nothing,
Optional computeReplacementNode As Func(Of TNode, TNode, SyntaxNode) = Nothing,
Optional tokens As IEnumerable(Of SyntaxToken) = Nothing,
Optional computeReplacementToken As Func(Of SyntaxToken, SyntaxToken, SyntaxToken) = Nothing,
Optional trivia As IEnumerable(Of SyntaxTrivia) = Nothing,
Optional computeReplacementTrivia As Func(Of SyntaxTrivia, SyntaxTrivia, SyntaxTrivia) = Nothing) As SyntaxNode
Return SyntaxReplacer.Replace(Me, nodes, computeReplacementNode, tokens, computeReplacementToken, trivia, computeReplacementTrivia).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function RemoveNodesCore(nodes As IEnumerable(Of SyntaxNode), options As SyntaxRemoveOptions) As SyntaxNode
Return SyntaxNodeRemover.RemoveNodes(Me, nodes, options).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function ReplaceNodeInListCore(originalNode As SyntaxNode, replacementNodes As IEnumerable(Of SyntaxNode)) As SyntaxNode
Return SyntaxReplacer.ReplaceNodeInList(Me, originalNode, replacementNodes).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function InsertNodesInListCore(nodeInList As SyntaxNode, nodesToInsert As IEnumerable(Of SyntaxNode), insertBefore As Boolean) As SyntaxNode
Return SyntaxReplacer.InsertNodeInList(Me, nodeInList, nodesToInsert, insertBefore).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function ReplaceTokenInListCore(originalToken As SyntaxToken, newTokens As IEnumerable(Of SyntaxToken)) As SyntaxNode
Return SyntaxReplacer.ReplaceTokenInList(Me, originalToken, newTokens).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function InsertTokensInListCore(originalToken As SyntaxToken, newTokens As IEnumerable(Of SyntaxToken), insertBefore As Boolean) As SyntaxNode
Return SyntaxReplacer.InsertTokenInList(Me, originalToken, newTokens, insertBefore).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function ReplaceTriviaInListCore(originalTrivia As SyntaxTrivia, newTrivia As IEnumerable(Of SyntaxTrivia)) As SyntaxNode
Return SyntaxReplacer.ReplaceTriviaInList(Me, originalTrivia, newTrivia).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function InsertTriviaInListCore(originalTrivia As SyntaxTrivia, newTrivia As IEnumerable(Of SyntaxTrivia), insertBefore As Boolean) As SyntaxNode
Return SyntaxReplacer.InsertTriviaInList(Me, originalTrivia, newTrivia, insertBefore).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
Protected Overrides Function NormalizeWhitespaceCore(indentation As String, eol As String, elasticTrivia As Boolean) As SyntaxNode
Return SyntaxNormalizer.Normalize(Me, indentation, eol, elasticTrivia, useDefaultCasing:=False).AsRootOfNewTreeWithOptionsFrom(Me.SyntaxTree)
End Function
#End Region
''' <summary>
''' Gets the location of this node.
''' </summary>
Public Shadows Function GetLocation() As Location
' Note that we want to return 'no location' for all nodes from embedded syntax trees
If Me.SyntaxTree IsNot Nothing Then
Dim tree = Me.SyntaxTree
If tree.IsEmbeddedSyntaxTree Then
Return New EmbeddedTreeLocation(tree.GetEmbeddedKind, Me.Span)
ElseIf tree.IsMyTemplate Then
Return New MyTemplateLocation(tree, Me.Span)
End If
End If
Return New SourceLocation(Me)
End Function
''' <summary>
''' Gets a SyntaxReference for this syntax node. SyntaxReferences can be used to regain access to a
''' syntax node without keeping the entire tree and source text in memory.
''' </summary>
Friend Shadows Function GetReference() As SyntaxReference
Return SyntaxTree.GetReference(Me)
End Function
''' <summary>
''' Gets a list of all the diagnostics in the sub tree that has this node as its root.
''' This method does not filter diagnostics based on compiler options like nowarn, warnaserror etc.
''' </summary>
Public Shadows Function GetDiagnostics() As IEnumerable(Of Diagnostic)
Return SyntaxTree.GetDiagnostics(Me)
End Function
Protected Overrides Function IsEquivalentToCore(node As SyntaxNode, Optional topLevel As Boolean = False) As Boolean
Return SyntaxFactory.AreEquivalent(Me, DirectCast(node, VisualBasicSyntaxNode), topLevel)
End Function
Friend Overrides Function ShouldCreateWeakList() As Boolean
Return TypeOf Me Is MethodBlockBaseSyntax
End Function
End Class
End Namespace