Skip to content

Commit c91c112

Browse files
committed
Add support for shadowing information in the LocalResolver
1 parent 077a8f4 commit c91c112

3 files changed

Lines changed: 68 additions & 4 deletions

File tree

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,58 @@ FASTPyEntity >> isVariableWriteAccess [
195195
^ self variableDeclarator isNotNil
196196
]
197197

198+
{ #category : 'accessing' }
199+
FASTPyEntity >> shadowedBy [
200+
201+
<FMProperty: #shadowedBy type: #FASTPyEntity opposite: #shadowing>
202+
<FMComment:
203+
'Return the entity shadowing me, if it exists. Else, return nil. An entity is shadowed went a named entity of a different kind is defined after me with the same name. Be careful, this works only if the local resolution was done and it works only on the local declaration.'>
204+
^ self attributeAt: #shadowedBy ifAbsent: [ nil ]
205+
]
206+
207+
{ #category : 'accessing' }
208+
FASTPyEntity >> shadowedBy: aFASTEntity [
209+
210+
| otherSide |
211+
otherSide := self shadowedBy.
212+
213+
otherSide == aFASTEntity ifTrue: [ ^ aFASTEntity ].
214+
215+
aFASTEntity
216+
ifNil: [
217+
self attributeAt: #shadowedBy put: aFASTEntity.
218+
otherSide shadowing: nil ]
219+
ifNotNil: [
220+
self attributeAt: #shadowedBy put: aFASTEntity.
221+
aFASTEntity shadowing: self ]
222+
]
223+
224+
{ #category : 'accessing' }
225+
FASTPyEntity >> shadowing [
226+
227+
<FMProperty: #shadowing type: #FASTPyEntity opposite: #shadowedBy>
228+
<FMComment:
229+
'Return the entity I possibly shadow. Else, return nil. An entity is shadowed went a named entity of a different kind is defined after me with the same name. Be careful, this works only if the local resolution was done and it works only on the local declaration.'>
230+
^ self attributeAt: #shadowing ifAbsent: [ nil ]
231+
]
232+
233+
{ #category : 'accessing' }
234+
FASTPyEntity >> shadowing: aFASTEntity [
235+
236+
| otherSide |
237+
otherSide := self shadowing.
238+
239+
otherSide == aFASTEntity ifTrue: [ ^ aFASTEntity ].
240+
241+
aFASTEntity
242+
ifNil: [
243+
self attributeAt: #shadowing put: aFASTEntity.
244+
otherSide shadowedBy: nil ]
245+
ifNotNil: [
246+
self attributeAt: #shadowing put: aFASTEntity.
247+
aFASTEntity shadowedBy: self ]
248+
]
249+
198250
{ #category : 'accessing' }
199251
FASTPyEntity >> variableDeclarator [
200252
"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-Tests/FASTPythonLocalResolverTest.class.st

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,10 @@ print(x.y)'.
582582

583583
usedImportedEntity := model module statements fourth arguments first.
584584
self assert: usedImportedEntity localDeclaration equals: import.
585-
self assert: usedImportedEntity equals: importedEntity localDeclaration localUses second
585+
self assert: usedImportedEntity equals: importedEntity localDeclaration localUses second.
586+
587+
self assert: assignedVariable localDeclaration shadowedBy equals: import.
588+
self assert: importedEntity localDeclaration shadowing equals: assignedVariable
586589
]
587590

588591
{ #category : 'tests - shadowing' }
@@ -651,7 +654,10 @@ print(x)'.
651654

652655
usedFunction := model module statements fourth arguments first.
653656
self assert: usedFunction localDeclaration equals: function.
654-
self assert: usedFunction equals: function localDeclaration localUses second
657+
self assert: usedFunction equals: function localDeclaration localUses second.
658+
659+
self assert: assignedVariable localDeclaration shadowedBy equals: function.
660+
self assert: function localDeclaration shadowing equals: assignedVariable
655661
]
656662

657663
{ #category : 'tests - shadowing' }
@@ -690,7 +696,10 @@ print(x)'.
690696

691697
usedImportedEntity := model module statements fourth arguments first.
692698
self assert: usedImportedEntity localDeclaration equals: import.
693-
self assert: usedImportedEntity equals: importedEntity localDeclaration localUses second
699+
self assert: usedImportedEntity equals: importedEntity localDeclaration localUses second.
700+
701+
self assert: assignedVariable localDeclaration shadowedBy equals: import.
702+
self assert: importedEntity localDeclaration shadowing equals: assignedVariable
694703
]
695704

696705
{ #category : 'tests - shadowing' }

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ FASTPythonLocalResolverVisitor >> ensureDeclarationOf: aName declaration: aFASTN
6868
ifPresent: [ :declaration |
6969
declaration localResolverKind = aFASTNode localResolverKind
7070
ifTrue: [ ^ declaration ]
71-
ifFalse: [ "If the kind is different, we save a new declaration."
71+
ifFalse: [
72+
declaration shadowedBy: aFASTNode.
73+
74+
"If the kind is different, we save a new declaration."
7275
aFASTNode ensureLocalUses.
7376
scope at: aName put: aFASTNode ] ]
7477
ifAbsentPut: [

0 commit comments

Comments
 (0)