Skip to content

Commit daef98b

Browse files
committed
Manage linking of SSA version even if the variable got shadowed in the middle
1 parent c91c112 commit daef98b

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/FAST-Python-Tools-Tests/FASTPythonSSATest.class.st

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,44 @@ print(x)'.
10231023
assignedVar5 ssaVersion }
10241024
]
10251025

1026+
{ #category : 'tests' }
1027+
FASTPythonSSATest >> testSSAVersionFollowPreviouslyShadowedName [
1028+
1029+
| assignedVar1 assignedVar2 usedVar1 usedFunction usedVar2 |
1030+
self parseAndResolve: 'x = 3
1031+
1032+
print(x)
1033+
1034+
def x(): pass
1035+
1036+
print(x)
1037+
1038+
x = 4
1039+
1040+
print(x)'.
1041+
1042+
assignedVar1 := model module statements first left.
1043+
self assert: assignedVar1 ssaVersion version equals: 1.
1044+
self assert: assignedVar1 ssaName equals: 'x_1'.
1045+
self assert: assignedVar1 ssaVersion localDeclaration equals: assignedVar1 localDeclaration.
1046+
self assert: assignedVar1 ssaVersion localUses size equals: 2.
1047+
1048+
usedVar1 := model module statements second arguments first.
1049+
self assert: usedVar1 ssaVersion identicalTo: assignedVar1 ssaVersion.
1050+
1051+
usedFunction := model module statements fourth arguments first.
1052+
self assert: usedFunction ssaVersion isNil.
1053+
1054+
assignedVar2 := model module statements fifth left.
1055+
self assert: assignedVar2 ssaVersion version equals: 2.
1056+
self assert: assignedVar2 ssaName equals: 'x_2'.
1057+
self assert: assignedVar2 ssaVersion localDeclaration equals: assignedVar2 localDeclaration.
1058+
self assert: assignedVar2 ssaVersion localUses size equals: 2.
1059+
1060+
usedVar2 := model module statements sixth arguments first.
1061+
self assert: usedVar2 ssaVersion identicalTo: assignedVar2 ssaVersion
1062+
]
1063+
10261064
{ #category : 'tests - assignments' }
10271065
FASTPythonSSATest >> testSingleAssignement [
10281066

src/FAST-Python-Tools/FASTPyEntity.extension.st

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ FASTPyEntity >> fullCfg [
1212
^ FASTPythonCFGVisitor new buildFullCFGOf: self
1313
]
1414

15+
{ #category : '*FAST-Python-Tools' }
16+
FASTPyEntity >> lastSSAVersion [
17+
"Return the last active SSA version if there is one. In Python lots of things can shadow each other, so we check if there was not a variable shadowed previously.
18+
19+
For example:
20+
21+
x = 3
22+
23+
def x(): pass
24+
25+
x = 4
26+
27+
Here the second assignment follow the first one but was interrupted by the function. But we still need to link them in the SSA."
28+
29+
| entity |
30+
entity := self localDeclaration.
31+
[
32+
entity := entity shadowing.
33+
entity isNotNil ] whileTrue: [ entity activeVersion ifNotNil: [ :version | ^ version ] ].
34+
35+
^ nil
36+
]
37+
1538
{ #category : '*FAST-Python-Tools' }
1639
FASTPyEntity >> localResolverKind [
1740
"In the local resolver, if we redefine an entity with an entity of the same kind, we keep the same declaration. Else we create a new one.

src/FAST-Python-Tools/FASTPythonSSAVisitor.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ FASTPythonSSAVisitor >> createVariableVersionFor: aFASTEntity [
3535

3636
| newSSA |
3737
newSSA := FASTVariableVersionSSA for: aFASTEntity.
38-
aFASTEntity localDeclaration activeVersion ifNotNil: [ :lastSSA | newSSA version: lastSSA version ].
38+
aFASTEntity lastSSAVersion ifNotNil: [ :lastSSA | newSSA version: lastSSA version ].
3939

4040
aFASTEntity localDeclaration activeVersion: newSSA.
4141

0 commit comments

Comments
 (0)