-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFASTCFGAbstractBlock.class.st
More file actions
299 lines (229 loc) · 7.15 KB
/
Copy pathFASTCFGAbstractBlock.class.st
File metadata and controls
299 lines (229 loc) · 7.15 KB
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
"
I am an abstract bloc in a CFG graph. I define the common API of all blocks.
All nodes store:
- A boolean to know if they are the start of a CFG graph via `#isStart`
- A list of statements that are in the block. At the exception of a `FASTCFGTryBlock` that can be stopped at any statement, all statements of a block will be executed if the execution enter this block
- The previous blocks pointing to me in the graph
All nodes should be able to provide their next blocks via `#nextBlocks`, but the number of blocks possible vary depending on the node.
A node should return true to `#isFull` if it has all its required next blocks.
A node should return true to `#isFinal` if it is the exit of the CFG graph. A CFG graph will always have one exit. In case of multiple exits, we create a `FASTCFGNullBlock` to join the exits.
"
Class {
#name : 'FASTCFGAbstractBlock',
#superclass : 'Object',
#instVars : [
'isStart',
'statements',
'previousBlocks'
],
#category : 'FAST-Core-Tools-CFG',
#package : 'FAST-Core-Tools',
#tag : 'CFG'
}
{ #category : 'visiting' }
FASTCFGAbstractBlock >> accept: aVisitor [
^ self subclassResponsibility
]
{ #category : 'adding' }
FASTCFGAbstractBlock >> addNextBlock: aBlock [
^ self subclassResponsibility
]
{ #category : 'adding' }
FASTCFGAbstractBlock >> addPreviousBlock: aBlock [
(self previousBlocks includes: aBlock) ifFalse: [ self previousBlocks add: aBlock ]
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> allEffectiveBreakingBlocks [
"Return all breaking nodes. If we encounter a loop, we will ignore the breaking nodes inside since they apply to this loop and not the englobing loop in case of a loop in a loop"
| childrenBreakingBlocks |
childrenBreakingBlocks := (self nextBlocks reject: #isLoop) flatCollect: #allEffectiveBreakingBlocks as: OrderedCollection.
self endsWithBreakStatement ifTrue: [ childrenBreakingBlocks add: self ].
^ childrenBreakingBlocks
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> allFollowingBlocks [
^ self deep: #nextBlocks collect: #yourself
]
{ #category : 'mermaid' }
FASTCFGAbstractBlock >> asMermaidScript [
"I genrerate a mermaid script to display the flowchart"
| nodesIDs |
self isStart ifFalse: [ self error: 'We should start by the start node' ].
nodesIDs := Dictionary new.
self withAllFollowingBlocks do: [ :block | nodesIDs at: block put: (nodesIDs size + 1) asString ].
^ String streamContents: [ :aStream |
aStream
nextPutAll: 'flowchart TD';
lf;
tab;
nextPutAll: 'subgraph Entry';
lf;
tab;
tab;
nextPutAll: 'direction TB';
lf;
tab;
tab;
nextPutAll: (nodesIDs at: self);
lf;
tab;
nextPutAll: 'end';
lf;
lf;
tab.
self isFinal ifFalse: [
aStream
nextPutAll: 'subgraph Exit';
lf;
tab;
tab;
nextPutAll: 'direction TB';
lf;
tab;
tab;
nextPutAll: (nodesIDs at: (self withAllFollowingBlocks detect: #isFinal));
lf;
tab;
nextPutAll: 'end';
lf;
lf;
tab ].
self withAllFollowingBlocks do: [ :block |
aStream
nextPutAll: (nodesIDs at: block);
nextPutAll: block mermaidDelimiters key;
nextPut: $";
nextPutAll: ('<br/>' join: (block sourceCode copyReplaceAll: '"' with: '"') lines);
nextPutAll: ' "';
nextPutAll: block mermaidDelimiters value;
lf;
tab ].
self withAllFollowingBlocks do: [ :block |
block nextBlockForValues do: [ :asso |
aStream
nextPutAll: (nodesIDs at: block);
nextPutAll: ' -'.
asso key asString ifNotEmpty: [
aStream
nextPutAll: '- ';
nextPutAll: asso key asString capitalized;
nextPutAll: ' -' ].
aStream
nextPutAll: '-> ';
nextPutAll: (nodesIDs at: asso value);
lf;
tab ] ] ]
]
{ #category : 'private' }
FASTCFGAbstractBlock >> endsWithBreakStatement [
^ self subclassResponsibility
]
{ #category : 'private' }
FASTCFGAbstractBlock >> endsWithContinueStatement [
^ self subclassResponsibility
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> firstParentLoop [
"I return the first loop containing myself.
This is useful to know which loop a node ending with a break statement will break."
self deep: #previousBlocks do: [ :parent | parent isLoop ifTrue: [ ^ parent ] ].
^ nil
]
{ #category : 'initialization' }
FASTCFGAbstractBlock >> initialize [
super initialize.
previousBlocks := OrderedCollection new.
isStart := false
]
{ #category : 'inspector' }
FASTCFGAbstractBlock >> inspectionVisualization: aBuilder [
<inspectorPresentationOrder: 3 title: 'Visu'>
^ (aBuilder instantiate: SpRoassalInspectorPresenter)
canvas: (FASTCFGVisualizationBuilder on: self);
yourself
]
{ #category : 'inspector' }
FASTCFGAbstractBlock >> inspectionVisualizationContext: aContext [
aContext active: self isStart
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isConditional [
^ false
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isFinal [
^ self nextBlocks isEmpty
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isFull [
"A node is full when it had all its next block set."
^ self subclassResponsibility
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isLoop [
^ false
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isNullBlock [
^ false
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> isStart [
^ isStart
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> isStart: anObject [
isStart := anObject
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isSwitch [
^ false
]
{ #category : 'testing' }
FASTCFGAbstractBlock >> isTry [
^ false
]
{ #category : 'mermaid' }
FASTCFGAbstractBlock >> mermaidDelimiters [
^ self subclassResponsibility
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> nextBlockForValues [
^ self subclassResponsibility
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> nextBlocks [
^ self subclassResponsibility
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> previousBlocks [
^ previousBlocks
]
{ #category : 'private' }
FASTCFGAbstractBlock >> shouldBreakOrContinueInContext: context [
"I should break or continue this node if i'm a break or continue and that we are currently visiting the first loop containing me to not mix loops in loops."
| firstLoop |
firstLoop := self firstParentLoop ifNil: [ ^ false ].
(self endsWithBreakStatement or: [ self endsWithContinueStatement ]) ifFalse: [ ^ false ].
^ context
detect: #isLoop
ifFound: [ :loop | loop conditional = firstLoop ]
ifNone: [ false ]
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> sourceCode [
self statements isEmptyOrNil ifTrue: [ ^ '' ].
^ self statements first sourceText copyFrom: self statements first startPos to: self statements last endPos
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> statements [
^ statements
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> statements: anObject [
statements := anObject
]
{ #category : 'accessing' }
FASTCFGAbstractBlock >> withAllFollowingBlocks [
^ self withDeep: #nextBlocks collect: #yourself
]