Skip to content

Commit d23a74f

Browse files
committed
Refactore the context management to allow the build of CFG from multiple entry points (function def, method def, class def or module)
This will be done in a future commit
1 parent 52efd85 commit d23a74f

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

‎src/FAST-Python-Tools/FASTCFGContextEntry.class.st‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ FASTCFGContextEntry >> addBlock: aBlock [
3232
currentBlocks add: aBlock
3333
]
3434

35+
{ #category : 'adding' }
36+
FASTCFGContextEntry >> addNextBlock: aBlock [
37+
self conditional addNextBlock: aBlock
38+
]
39+
3540
{ #category : 'accessing' }
3641
FASTCFGContextEntry >> conditional [
3742
^ conditional
@@ -63,7 +68,7 @@ FASTCFGContextEntry >> flushCurrentBlocks [
6368
FASTCFGContextEntry >> initialize [
6469

6570
super initialize.
66-
self flushCurrentBlocks
71+
currentBlocks := OrderedCollection new
6772
]
6873

6974
{ #category : 'accessing' }
@@ -81,7 +86,7 @@ FASTCFGContextEntry >> printOn: aStream [
8186

8287
aStream nextPutAll: ' [ '.
8388
self conditional
84-
ifNil: [ aStream nextPutAll: 'Root' ]
89+
ifNil: [ aStream nextPutAll: 'No block' ]
8590
ifNotNil: [ self conditional printOn: aStream ].
8691
aStream nextPutAll: ' ]'
8792
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"
2+
I am the root context entry to represent the statement block that is given to prodiuce the CFG
3+
"
4+
Class {
5+
#name : 'FASTCFGRootContextEntry',
6+
#superclass : 'FASTCFGContextEntry',
7+
#category : 'FAST-Python-Tools-CFG/DataFlow',
8+
#package : 'FAST-Python-Tools',
9+
#tag : 'CFG/DataFlow'
10+
}
11+
12+
{ #category : 'adding' }
13+
FASTCFGRootContextEntry >> addNextBlock: aBlock [
14+
"Nothing to do at root."
15+
16+
17+
]
18+
19+
{ #category : 'accessing' }
20+
FASTCFGRootContextEntry >> conditional [
21+
22+
^ nil
23+
]
24+
25+
{ #category : 'accessing' }
26+
FASTCFGRootContextEntry >> flushCurrentBlocks [
27+
"In the root entry we do nothing because we want to keep the info of the root blocks to set the null block if needed."
28+
]
29+
30+
{ #category : 'printing' }
31+
FASTCFGRootContextEntry >> printOn: aStream [
32+
33+
super printOn: aStream.
34+
35+
aStream nextPutAll: ' - Root'
36+
]

‎src/FAST-Python-Tools/FASTPythonCFGVisitor.class.st‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ FASTPythonCFGVisitor >> visitFASTPyForStatement: aForStatement [
127127
FASTPythonCFGVisitor >> visitFASTPyFunctionDefinition: aFunction [
128128
"We do not manage the function in itself only its content"
129129

130-
(aFunction containedEntities sorted: #endPos ascending) do: [ :child | child accept: self ].
131-
132-
self buildBlockIfNeeded
130+
self visitFASTPyTDefinition: aFunction
133131
]
134132

135133
{ #category : 'visiting' }

‎src/FAST-Python-Tools/FASTTCFGUtility.trait.st‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ FASTTCFGUtility >> buildBlockOfType: aBlockClass [
136136
{ #category : 'running' }
137137
FASTTCFGUtility >> buildCFGOf: aFASTModel [
138138

139-
context push: FASTCFGContextEntry new.
139+
context push: FASTCFGRootContextEntry new.
140140
aFASTModel accept: self.
141141
self shouldBuildNullBlock ifTrue: [ self buildBlockOfType: FASTCFGNullBlock ].
142142
self assert: context size = 1.
@@ -160,7 +160,7 @@ FASTTCFGUtility >> endBlock [
160160

161161
"In case we are finishing the first block of a loop, we finilize the loop.
162162
In the past, this was done before popping loops from the context, but doing it here with this condition allows to easily manage the else clause of loops in Python."
163-
( self currentConditional isLoop and: [ self currentConditional nextBlocks size = 1 ]) ifTrue: [ self finializeLoop ].
163+
( context top isLoop and: [ self currentConditional nextBlocks size = 1 ]) ifTrue: [ self finializeLoop ].
164164

165165
"We are changing of statement block so we flush the blocks we saved for the previous statement block."
166166
context top flushCurrentBlocks
@@ -196,7 +196,7 @@ FASTTCFGUtility >> managePreviousBlocksOf: newBlock [
196196
newBlock isStart: true.
197197
^ self ].
198198

199-
context top currentBlocks ifEmpty: [ self currentConditional addNextBlock: newBlock ] ifNotEmpty: [ :blocksAtScope |
199+
context top currentBlocks ifEmpty: [ context top addNextBlock: newBlock ] ifNotEmpty: [ :blocksAtScope |
200200
(blocksAtScope flatCollectAsSet: #withAllFollowingBlocks)
201201
reject: [ :block | block isFull or: [ block shouldBreakOrContinueInContext: context ] ]
202202
thenDo: [ :block | block addNextBlock: newBlock ] ]

0 commit comments

Comments
 (0)