-
Notifications
You must be signed in to change notification settings - Fork 69
/
TParseNode.class.st
506 lines (392 loc) · 10.8 KB
/
TParseNode.class.st
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
"
A TParseNode is node in the parse tree of a TMethod. Subclasses correspond to different types of nodes in a method parse tree. The tree of translation parse nodes mirrors the parse tree of a Smalltalk method, and is used for translating a Smalltalk method to C source.
"
Class {
#name : #TParseNode,
#superclass : #Object,
#instVars : [
'parent',
'comment'
],
#category : #'Slang-AST'
}
{ #category : #visiting }
TParseNode >> accept: aVisitor [
self subclassResponsibility
]
{ #category : #utilities }
TParseNode >> allCalls [
"Answer a collection of selectors for the messages sent in this parse tree."
| calls |
calls := Set new: 32.
self nodesDo:
[:node|
node isSend ifTrue:
[calls add: node selector].
(node isConstant and: [node value isSymbol]) ifTrue:
[calls add: node value]].
^calls
]
{ #category : #enumerating }
TParseNode >> allSatisfy: aBlock [
self nodesDo: [:n| (aBlock value: n) ifFalse: [^false]].
^true
]
{ #category : #enumerating }
TParseNode >> allStatementsDo: aFullBlockClosure [
self children do: [ :child | child allStatementsDo: aFullBlockClosure ]
]
{ #category : #enumerating }
TParseNode >> anySatisfy: aBlock [
self nodesDo: [:n| (aBlock value: n) ifTrue: [^true]].
^false
]
{ #category : #transforming }
TParseNode >> asCASTExpressionIn: aBuilder [
"By default use the normal translation.
Override for those translations that are not expressions by default"
^ self asCASTIn: aBuilder
]
{ #category : #tranforming }
TParseNode >> asCASTIn: aBuilder [
self subclassResponsibility
]
{ #category : #'C code generation' }
TParseNode >> asCASTIn: aBuilder prependToEnd: aNodeOrNil [
aNodeOrNil ifNotNil:
[^ aNodeOrNil prependCASTIn: aBuilder expression: self].
^ self asCASTExpressionIn: aBuilder
]
{ #category : #linearisation }
TParseNode >> asExpressionIn: aMLStatementListBuider [
"A TNode returns itself as an expression"
^ self
]
{ #category : #transformations }
TParseNode >> asReturnNode [
^TReturnNode new
setExpression: self;
yourself
]
{ #category : #linearisation }
TParseNode >> asStatementIn: aMLStatementListBuider [
"A TNode returns itself as a statement"
^ self
]
{ #category : #transformations }
TParseNode >> assignLastExpressionTo: aTVariableNode [
"An expression just assigns itself"
^ TAssignmentNode new
setVariable: aTVariableNode
expression: self
]
{ #category : #transformations }
TParseNode >> bindVariableUsesIn: aDictionary [
"Bind uses of all variables in the given dictionary, but do not change variables that appear on the left-hand-side of an assignment statement."
^ self
]
{ #category : #transformations }
TParseNode >> bindVariableUsesIn: aDictionary andConstantFoldIf: constantFold in: codeGen [
"Answer either the receiver, if it contains no references to the given variables, or a new node with the given variables rebound.
But do not change variables that appear on the left-hand-side of an assignment statement."
^self
]
{ #category : #transformations }
TParseNode >> bindVariablesIn: aDictionary [
^self
]
{ #category : #transformations }
TParseNode >> bindVariablesIn: aDictionary unless: cautionaryBlock [
^self
]
{ #category : #enumerating }
TParseNode >> collect: aBlock [
| nodes |
nodes := OrderedCollection new.
self nodesDo: [:n| nodes add: (aBlock value: n) ].
^nodes
]
{ #category : #accessing }
TParseNode >> comment [
^comment
]
{ #category : #accessing }
TParseNode >> comment: aComment [
comment := aComment
]
{ #category : #accessing }
TParseNode >> constantNumbericValueIfAtAllPossibleOrNilIn: aCCodeGen [
"This is a version of constantNumbericValueOrNil for type checking rather than code generation.
It aims to yield a value if at all possible."
^self constantNumbericValueOrNil
]
{ #category : #accessing }
TParseNode >> constantNumbericValueOrNil [
^nil
]
{ #category : #enumerating }
TParseNode >> detect: aBlock [
^self detect: aBlock ifNone: [self error: 'Node is not in the tree']
]
{ #category : #enumerating }
TParseNode >> detect: aBlock ifNone: defaultBlock [
self nodesDo: [:n| (aBlock value: n) ifTrue: [^n]].
^defaultBlock value
]
{ #category : #testing }
TParseNode >> endsWithReturn [
^false
]
{ #category : #flattenin }
TParseNode >> flattened [
^ { self }
]
{ #category : #testing }
TParseNode >> hasExplicitReturn [
self nodesDo: [ :node |
node isReturn ifTrue: [ ^true ].
].
^false
]
{ #category : #testing }
TParseNode >> hasSideEffect [
"Answer if the parse tree rooted at this node has a side-effect or not. By default assume it has. Nodes that don't override."
^true
]
{ #category : #testing }
TParseNode >> isAssertion [
^false
]
{ #category : #testing }
TParseNode >> isAssignment [
^false
]
{ #category : #testing }
TParseNode >> isCaseStmt [
^false
]
{ #category : #testing }
TParseNode >> isComment [
^false
]
{ #category : #testing }
TParseNode >> isConditionalSend [
"Answer if the receiver is a send of any of the conditionals, ifTrue: ifTrue:ifFalse: et al"
^false
]
{ #category : #testing }
TParseNode >> isConstant [
^false
]
{ #category : #testing }
TParseNode >> isDefine [
^false
]
{ #category : #testing }
TParseNode >> isGoTo [
^false
]
{ #category : #testing }
TParseNode >> isInline [
^false
]
{ #category : #testing }
TParseNode >> isLabel [
^false
]
{ #category : #testing }
TParseNode >> isLeaf [
"Answer true if the receiver is a variable or a constant node."
^false
]
{ #category : #testing }
TParseNode >> isLiteralArrayDeclaration [
^false
]
{ #category : #testing }
TParseNode >> isNonNullCCode [
"overridden in TSendNode"
^false
]
{ #category : #testing }
TParseNode >> isReturn [
^false
]
{ #category : #testing }
TParseNode >> isReturningIf [
^false
]
{ #category : #comparing }
TParseNode >> isSameAs: aTParseNode [
"Answer if the ParseTree rooted at this node is the same as aTParseNode.
By default answer false and have subclasses override as appropriate."
^ self subclassResponsibility
]
{ #category : #testing }
TParseNode >> isSend [
^false
]
{ #category : #testing }
TParseNode >> isStatementList [
^false
]
{ #category : #testing }
TParseNode >> isSwitch [
^false
]
{ #category : #testing }
TParseNode >> isTMethod [
^false
]
{ #category : #testing }
TParseNode >> isTParseNode [
^true
]
{ #category : #testing }
TParseNode >> isVariable [
^false
]
{ #category : #accessing }
TParseNode >> nameOrValue [
self error: 'object is neither variable nor constant'
]
{ #category : #testing }
TParseNode >> needsTrailingSemicolon [
"Answer if, when emitted as a statement (in particular in a TStmtList), the
receiver needs a trailing semicolon. Subclasses redefine as appropriate."
^true
]
{ #category : #enumerating }
TParseNode >> nodesDo: aBlock [
"Apply aBlock to all nodes in the receiver.
N.B. This is assumed to be bottom-up, leaves first."
aBlock value: self
]
{ #category : #enumerating }
TParseNode >> nodesDo: aBlock parent: parent [
"Apply aBlock to all nodes in the receiver with each node's parent.
N.B. This is assumed to be bottom-up, leaves first."
self subclassResponsibility
]
{ #category : #enumerating }
TParseNode >> nodesDo: aBlock parent: parent unless: cautionaryBlock [
"Evaluate aBlock for all nodes in the tree except those for which cautionaryBlock
answers true or are children of those for which cautionaryBlock answers true."
(cautionaryBlock value: self value: parent) ifFalse:
[aBlock value: self value: parent]
]
{ #category : #enumerating }
TParseNode >> nodesDo: aBlock unless: cautionaryBlock [
"Evaluate aBlock for all nodes in the tree except those for which cautionaryBlock
answers true or are children of those for which cautionaryBlock answers true."
(cautionaryBlock value: self) ifFalse:
[aBlock value: self]
]
{ #category : #enumerating }
TParseNode >> nodesWithParentsDo: aBlock [
"Apply aBlock to all nodes in the receiver with each node's parent.
N.B. This is assumed to be bottom-up, leaves first."
self nodesDo: aBlock parent: nil
]
{ #category : #enumerating }
TParseNode >> nodesWithParentsDo: aBlock unless: cautionaryBlock [
"Evaluate aBlock for all nodes in the tree and their parent except those for which
cautionaryBlock answers true or are children of those for which cautionaryBlock
answers true. Cautionary block is invoked with node and parent. parent may be nil."
self nodesDo: aBlock parent: nil unless: cautionaryBlock
]
{ #category : #enumerating }
TParseNode >> noneSatisfy: aBlock [
self nodesDo: [:n| (aBlock value: n) ifTrue: [^false]].
^true
]
{ #category : #enumerating }
TParseNode >> noneSatisfy: aBlock unless: cautionaryBlock [
self
nodesDo: [:n| (aBlock value: n) ifTrue: [^false]]
unless: cautionaryBlock.
^true
]
{ #category : #enumerating }
TParseNode >> occurrencesOf: aNode [
| count |
count := 0.
self nodesDo:
[:node|
node = aNode ifTrue: [count := count + 1]].
^count
]
{ #category : #accessing }
TParseNode >> parent [
^ parent
]
{ #category : #accessing }
TParseNode >> parent: anObject [
parent := anObject
]
{ #category : #printing }
TParseNode >> printOn: aStream [
"Append a description of the receiver onto the given stream."
self printOn: aStream level: 0.
]
{ #category : #printing }
TParseNode >> printOn: aStream level: anInteger [
"Typically overridden. If control actually gets here, avoid recursion loop by sending to super."
super printOn: aStream.
]
{ #category : #transformations }
TParseNode >> removeAssertions [
"default: do nothing"
]
{ #category : #transformations }
TParseNode >> replaceBy: aNode [
self parent replaceChild: self with: aNode
]
{ #category : #transformations }
TParseNode >> replaceChild: aNode with: aReplacementNode [
self subclassResponsibility
]
{ #category : #transformations }
TParseNode >> replaceNodesIn: aDictionary [
^aDictionary at: self ifAbsent: [self]
]
{ #category : #enumerating }
TParseNode >> select: aBlock [
| nodes |
nodes := OrderedCollection new.
self nodesDo: [:n| (aBlock value: n) ifTrue: [nodes add: n]].
^nodes
]
{ #category : #testing }
TParseNode >> structTargetKindIn: aCodeGen [
"Answer if the recever evaluates to a struct or struct pointer
and hence can be dereferenced using . or ->. Answer any of
#struct #pointer or nil"
^nil
]
{ #category : #accessing }
TParseNode >> tMethod [
"Climb up the parent links up to the TMethod node"
| n |
n := self.
[ true ] whileTrue: [
n ifNil: [ ^ nil ].
n class == TMethod ifTrue: [ ^ n ].
n := n parent ]
]
{ #category : #'type inference' }
TParseNode >> typeFrom: aCodeGenerator in: aTMethod [
"This is the default type in case of doubt"
^(self typeOrNilFrom: aCodeGenerator in: aTMethod)
ifNil: [ aCodeGenerator defaultType ]
]
{ #category : #'type inference' }
TParseNode >> typeOrNilFrom: aCodeGenerator in: aTMethod [
"When we don't know, it's nil"
^nil
]
{ #category : #accessing }
TParseNode >> variableNameOrNil [
"Overridden in TAssignmentNode & TVariableNode to answer their variable name."
^nil
]