Skip to content

Commit a3bb891

Browse files
committed
Manage subscripts in LR (only if the indexes are exactly the same source code)
1 parent ba3bc31 commit a3bb891

7 files changed

Lines changed: 60 additions & 15 deletions

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,27 @@ FASTPythonLocalResolverTest >> testParameterShadowedByAssignement [
973973
self assert: usedVariable equals: assignedVariable localDeclaration localUses fourth
974974
]
975975

976+
{ #category : 'tests - subscripts' }
977+
FASTPythonLocalResolverTest >> testSubscriptUsageInCall [
978+
979+
| assignedVariable usedVariable |
980+
self parseAndResolve: 'x[3] = 1
981+
print(x[3])'.
982+
983+
assignedVariable := model module statements first left.
984+
self assert: assignedVariable localDeclaration equals: assignedVariable.
985+
self assert: assignedVariable localDeclaration value name equals: 'x'.
986+
self assert: assignedVariable localDeclaration indices first sourceCode equals: '3'.
987+
self assert: assignedVariable localDeclaration variableDeclarator equals: model module statements first.
988+
self deny: assignedVariable localDeclaration isNonLocalDeclaration.
989+
self assert: assignedVariable localDeclaration localUses size equals: 2.
990+
self assert: assignedVariable localDeclaration localUses first equals: assignedVariable.
991+
992+
usedVariable := model module statements second arguments first.
993+
self assert: usedVariable localDeclaration equals: assignedVariable.
994+
self assert: usedVariable equals: assignedVariable localDeclaration localUses second
995+
]
996+
976997
{ #category : 'tests' }
977998
FASTPythonLocalResolverTest >> testTwoAssignationsCreateOneDeclaration [
978999

‎src/FAST-Python-Tools/FASTPyAttributeAccess.extension.st‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ FASTPyAttributeAccess >> localDeclarationName [
88

99
{ #category : '*FAST-Python-Tools' }
1010
FASTPyAttributeAccess >> variableDeclarator [
11-
"In the case I represent a write access to a variable, I return the node representing my assignment.
12-
13-
I should be similar to the implementation of FASTPyIdentifier! We should check later to remove the duplication."
11+
"In the case I represent a write access to a variable, I return the node representing my assignment."
1412

15-
| assignedNode |
1613
"If I am a variable defined in tuples, I should return the node initializing this tuple. Can be recursive (tuple in tuple)"
17-
assignedNode := self.
18-
[ assignedNode collectionInitializer isNotNil and: [ assignedNode collectionInitializer isTuple ] ] whileTrue: [
19-
assignedNode := assignedNode collectionInitializer ].
2014

21-
assignedNode parentAssignmentLeft ifNotNil: [ :assignment | ^ assignment ].
15+
self selfOrTopmostTupleContainer parentAssignmentLeft ifNotNil: [ :assignment | ^ assignment ].
2216

2317
^ super variableDeclarator
2418
]

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ FASTPyEntity >> localResolverKind [
2828
^ self error: 'This entity cannot be a local declaration or is missing the overriding method.'
2929
]
3030

31+
{ #category : '*FAST-Python-Tools' }
32+
FASTPyEntity >> selfOrTopmostTupleContainer [
33+
"If I am in a tuple, I return the tuple (this applies recursively, if I am in a tuple in a tuple I return the topmost tuple).
34+
35+
Else, return myself."
36+
37+
| node |
38+
"If I am a variable defined in tuples, I should return the node initializing this tuple. Can be recursive (tuple in tuple)"
39+
node := self.
40+
[ node collectionInitializer isNotNil and: [ node collectionInitializer isTuple ] ] whileTrue: [ node := node collectionInitializer ].
41+
42+
^ node
43+
]
44+
3145
{ #category : '*FAST-Python-Tools' }
3246
FASTPyEntity >> variableDeclarator [
3347
"In case I am a write access to a variable, I return the node categorized as my declaration. Else I return nil.

‎src/FAST-Python-Tools/FASTPyIdentifier.extension.st‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ FASTPyIdentifier >> localResolverKind [
1010

1111
{ #category : '*FAST-Python-Tools' }
1212
FASTPyIdentifier >> variableDeclarator [
13-
"In the case I represent a write access to a variable, I return the node representing my assignment.
14-
15-
I should be similar to the implementation of FASTPyAttributeAccess! We should check later to remove the duplication."
13+
"In the case I represent a write access to a variable, I return the node representing my assignment."
1614

1715
| assignedNode |
1816
"If I am a variable defined in tuples, I should return the node initializing this tuple. Can be recursive (tuple in tuple)"
19-
assignedNode := self.
20-
[ assignedNode collectionInitializer isNotNil and: [ assignedNode collectionInitializer isTuple ] ] whileTrue: [ assignedNode := assignedNode collectionInitializer ].
17+
assignedNode := self selfOrTopmostTupleContainer.
2118

2219
assignedNode parentAssignmentLeft ifNotNil: [ :assignment | ^ assignment ].
2320

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Extension { #name : 'FASTPySubscript' }
2+
3+
{ #category : '*FAST-Python-Tools' }
4+
FASTPySubscript >> variableDeclarator [
5+
"In the case I represent a write access to a variable, I return the node representing my assignment."
6+
7+
"If I am a variable defined in tuples, I should return the node initializing this tuple. Can be recursive (tuple in tuple)"
8+
self selfOrTopmostTupleContainer parentAssignmentLeft ifNotNil: [ :assignment | ^ assignment ].
9+
10+
^ super variableDeclarator
11+
]

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,13 @@ FASTPythonLocalResolverVisitor >> visitFASTPyParameter: aParameter [
183183
self ensureDeclarationOf: aParameter named: aParameter name declaration: aParameter.
184184
super visitFASTPyParameter: aParameter
185185
]
186+
187+
{ #category : 'visiting' }
188+
FASTPythonLocalResolverVisitor >> visitFASTPySubscript: aSubscript [
189+
190+
aSubscript isVariableWriteAccess
191+
ifTrue: [ self ensureDeclarationOf: aSubscript named: aSubscript sourceCode declaration: aSubscript ]
192+
ifFalse: [ self bind: aSubscript toDeclarationNamed: aSubscript sourceCode ].
193+
194+
super visitFASTPySubscript: aSubscript
195+
]

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,4 @@ FASTPythonSSAVisitor >> visitFASTPyTDefinition: aTDefinition [
189189

190190
cfg := aTDefinition cfg.
191191
self visit: cfg
192-
193-
" super visitFASTPyTDefinition: aTDefinition"
194192
]

0 commit comments

Comments
 (0)