Skip to content

Commit a213361

Browse files
committed
Begin to fix bug when we encounter a subscript that has no assignemnt
1 parent 2ac177a commit a213361

7 files changed

Lines changed: 102 additions & 9 deletions

File tree

‎src/FAST-Python-Model-Generator/FASTPythonMetamodelGenerator.class.st‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ FASTPythonMetamodelGenerator >> defineClasses [
346346
statement := builder newAbstractClassNamed: #Statement.
347347
string := builder newClassNamed: #String.
348348
subscript := builder newClassNamed: #Subscript.
349+
subscript withTesting.
349350
thenClause := builder newClassNamed: #ThenClause.
350351
tryStatement := builder newClassNamed: #TryStatement.
351352
tuple := builder newClassNamed: #Tuple.

‎src/FAST-Python-Model/FASTPyEntity.class.st‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ FASTPyEntity >> isStringLiteral [
222222
^ false
223223
]
224224

225+
{ #category : 'testing' }
226+
FASTPyEntity >> isSubscript [
227+
228+
<ignoreForCoverage>
229+
<generated>
230+
^ false
231+
]
232+
225233
{ #category : 'testing' }
226234
FASTPyEntity >> isTuple [
227235

‎src/FAST-Python-Model/FASTPySubscript.class.st‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ FASTPySubscript >> indices: anObject [
7676
indices value: anObject
7777
]
7878

79+
{ #category : 'testing' }
80+
FASTPySubscript >> isSubscript [
81+
82+
<ignoreForCoverage>
83+
<generated>
84+
^ true
85+
]
86+
7987
{ #category : 'accessing' }
8088
FASTPySubscript >> value [
8189
"Relation named: #value type: #FASTPyExpression opposite: #parentSubscriptValue"
@@ -99,6 +107,7 @@ FASTPySubscript >> variableDeclarator [
99107
"In the case I represent a write access to a variable, I return the node representing my assignment."
100108

101109
"If I am a variable defined in tuples, I should return the node initializing this tuple. Can be recursive (tuple in tuple)"
110+
102111
self selfOrTopmostAssignableCollection parentAssignmentLeft ifNotNil: [ :assignment | ^ assignment ].
103112

104113
^ super variableDeclarator

‎src/FAST-Python-Tools-Tests/FASTPythonLocalResolverTest.class.st‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,24 @@ FASTPythonLocalResolverTest >> testParameterShadowedByAssignement [
19861986
self assert: usedVariable equals: assignedVariable localDeclaration localUses fourth
19871987
]
19881988

1989+
{ #category : 'tests - subscripts' }
1990+
FASTPythonLocalResolverTest >> testSubscriptReadAfterReceiverAssignation [
1991+
"In case we read a subscript before writing anything on it, since we cannot use the variable declaration as declaration, we point the first read."
1992+
1993+
| subscript |
1994+
self parseAndResolve: 'x = coll
1995+
print(x[3])'.
1996+
1997+
subscript := model module statements second arguments first.
1998+
self assert: subscript localDeclaration equals: subscript.
1999+
self assert: subscript localDeclaration value name equals: 'x'.
2000+
self assert: subscript localDeclaration indices first sourceCode equals: '3'.
2001+
self assert: subscript localDeclaration variableDeclarator isNil.
2002+
self deny: subscript localDeclaration isNonLocalDeclaration.
2003+
self assert: subscript localDeclaration localUses size equals: 1.
2004+
self assert: subscript localDeclaration localUses first equals: subscript
2005+
]
2006+
19892007
{ #category : 'tests - subscripts' }
19902008
FASTPythonLocalResolverTest >> testSubscriptUsageInCall [
19912009

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,32 @@ print(x[3])'.
19361936
self assert: usedVar ssaVersion identicalTo: assignedVar ssaVersion
19371937
]
19381938

1939+
{ #category : 'tests - subscripts' }
1940+
FASTPythonSSATest >> testSubscriptReadBeforeAssignment [
1941+
1942+
| usedSubscript1 assignedSubscript usedSubscript2 |
1943+
self parseAndResolve: 'x = coll
1944+
print(x[3])
1945+
x[3] = 5
1946+
print(x[3])'.
1947+
1948+
usedSubscript1 := model module statements second arguments first.
1949+
self assert: usedSubscript1 ssaVersion version equals: 1.
1950+
self assert: usedSubscript1 ssaName equals: 'x[3]_1'.
1951+
self assert: usedSubscript1 ssaVersion localDeclaration equals: usedSubscript1 localDeclaration.
1952+
self assert: usedSubscript1 ssaVersion localUses size equals: 1.
1953+
1954+
assignedSubscript := model module statements third left.
1955+
self assert: assignedSubscript ssaVersion version equals: 2.
1956+
self assert: assignedSubscript ssaName equals: 'x[3]_2'.
1957+
self assert: assignedSubscript ssaVersion localDeclaration equals: assignedSubscript localDeclaration.
1958+
self assert: assignedSubscript ssaVersion localUses size equals: 2.
1959+
1960+
usedSubscript2 := model module statements fourth arguments first.
1961+
self assert: usedSubscript2 ssaVersion identicalTo: assignedSubscript ssaVersion.
1962+
self assert: usedSubscript2 equals: assignedSubscript ssaVersion localUses second
1963+
]
1964+
19391965
{ #category : 'tests - try' }
19401966
FASTPythonSSATest >> testTryAssignAndUsage [
19411967

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,31 @@ FASTPythonLocalResolverVisitor class >> resolve: aPythonEntity [
4040
{ #category : 'declarations' }
4141
FASTPythonLocalResolverVisitor >> bind: aFASTNode toDeclarationNamed: aName [
4242

43+
^ self bind: aFASTNode toDeclarationNamed: aName ifAbsentUse: [ self ensureNonlocalDeclarationNamed: aName ]
44+
]
45+
46+
{ #category : 'declarations' }
47+
FASTPythonLocalResolverVisitor >> bind: aFASTNode toDeclarationNamed: aName ifAbsentUse: aBlock [
48+
4349
| declaration |
44-
declaration := (self declarationNamed: aName) ifNil: [ "If it is undeclared, we create an undeclared in the lowest scope in the stack."
45-
self
46-
ensureDeclarationOf: aName
47-
declaration: (FASTNonLocalDeclaration new
48-
name: aName;
49-
yourself)
50-
in: namesContext first ].
50+
declaration := (self declarationNamed: aName) ifNil: [ aBlock cull: aFASTNode cull: aName ].
5151

5252
aFASTNode localDeclaration: declaration.
5353
declaration addLocalUse: aFASTNode
5454
]
5555

56+
{ #category : 'declarations' }
57+
FASTPythonLocalResolverVisitor >> declarationForUndeclaredNode: aNode named: aName [
58+
"In case of a subscript, if the receiving variable exists, we consider that the first read access to the subscript is the declaration."
59+
60+
aNode isSubscript ifTrue: [
61+
aNode value
62+
localDeclarationifPresent: [ ^ self ensureDeclarationOf: aName declaration: aNode in: namesContext top ]
63+
ifAbsent: [ "Nothing, just let the non local declaration." ] ].
64+
65+
^ self ensureNonlocalDeclarationNamed: aName
66+
]
67+
5668
{ #category : 'declarations' }
5769
FASTPythonLocalResolverVisitor >> declarationNamed: aName [
5870

@@ -91,6 +103,17 @@ FASTPythonLocalResolverVisitor >> ensureDeclarationOf: anEntity named: aName dec
91103
self bind: anEntity toDeclarationNamed: aName
92104
]
93105

106+
{ #category : 'declarations' }
107+
FASTPythonLocalResolverVisitor >> ensureNonlocalDeclarationNamed: aName [
108+
109+
^ self
110+
ensureDeclarationOf: aName
111+
declaration: (FASTNonLocalDeclaration new
112+
name: aName;
113+
yourself)
114+
in: namesContext first
115+
]
116+
94117
{ #category : 'initialization' }
95118
FASTPythonLocalResolverVisitor >> initialize [
96119

@@ -201,7 +224,11 @@ FASTPythonLocalResolverVisitor >> visitFASTPyTCanBeVariable: aTCanBeVariable [
201224

202225
aTCanBeVariable isVariableWriteAccess
203226
ifTrue: [ self ensureDeclarationOf: aTCanBeVariable named: aTCanBeVariable localDeclarationName declaration: aTCanBeVariable ]
204-
ifFalse: [ self bind: aTCanBeVariable toDeclarationNamed: aTCanBeVariable localDeclarationName ].
227+
ifFalse: [
228+
self
229+
bind: aTCanBeVariable
230+
toDeclarationNamed: aTCanBeVariable localDeclarationName
231+
ifAbsentUse: [ :node :name | self declarationForUndeclaredNode: node named: name ] ].
205232

206233
super visitFASTPyTCanBeVariable: aTCanBeVariable
207234
]

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ FASTPythonSSAVisitor >> visitFASTPyTCanBeVariable: aTCanBeVariable [
188188

189189
aTCanBeVariable isVariableWriteAccess
190190
ifTrue: [ self handleNewAssignemntTo: aTCanBeVariable ]
191-
ifFalse: [ aTCanBeVariable localDeclaration isNonLocalDeclaration ifFalse: [ aTCanBeVariable ssaVersion: aTCanBeVariable localDeclaration activeVersion ] ].
191+
ifFalse: [
192+
aTCanBeVariable localDeclaration isNonLocalDeclaration ifFalse: [ "Subscript is a special case since it is not directly assigned. So thfe first read act as a new declaration."
193+
(aTCanBeVariable isSubscript and: [ aTCanBeVariable localDeclaration activeVersion isNil ])
194+
ifTrue: [ self handleNewAssignemntTo: aTCanBeVariable ]
195+
ifFalse: [ aTCanBeVariable ssaVersion: aTCanBeVariable localDeclaration activeVersion ] ] ].
192196

193197
super visitFASTPyTCanBeVariable: aTCanBeVariable
194198
]

0 commit comments

Comments
 (0)