@@ -40,19 +40,31 @@ FASTPythonLocalResolverVisitor class >> resolve: aPythonEntity [
4040{ #category : ' declarations' }
4141FASTPythonLocalResolverVisitor >> 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' }
5769FASTPythonLocalResolverVisitor >> 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' }
95118FASTPythonLocalResolverVisitor >> 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]
0 commit comments