Skip to content

Commit

Permalink
Start to refactor management of assignations
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Nov 23, 2024
1 parent c2840e9 commit 5e30b9f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/Famix-Python-Importer/FamixPythonImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Class {
#classTraits : 'SRTSolverUserVisitor classTrait',
#instVars : [
'model',
'rootFilePath',
'isInLeftSideOfAssignation'
'rootFilePath'
],
#category : 'Famix-Python-Importer-Visitors',
#package : 'Famix-Python-Importer',
Expand Down Expand Up @@ -464,8 +463,7 @@ FamixPythonImporterVisitor >> initialize [

super initialize.
model := FamixPythonModel new name: 'default Python Model'.
self initialiseSolver.
isInLeftSideOfAssignation := false "In Python variables are created during their assignation. So it is good to know if some nodes are visited during this assignation."
self initialiseSolver
]

{ #category : 'private - searching' }
Expand Down Expand Up @@ -668,10 +666,6 @@ FamixPythonImporterVisitor >> unknownImportedNamed: aString [
{ #category : 'visiting' }
FamixPythonImporterVisitor >> visitAssignmentExpression: anAssignmentExpression [

| oldValue |
oldValue := isInLeftSideOfAssignation.
isInLeftSideOfAssignation := true.
[
| variable |
variable := self acceptNode: anAssignmentExpression variable.
variable isMooseEntity
Expand All @@ -683,17 +677,13 @@ FamixPythonImporterVisitor >> visitAssignmentExpression: anAssignmentExpression
accessor: self currentEntity;
isWrite: true.
self setSourceAnchor: access from: anAssignmentExpression ]
ifFalse: [ self flag: #todo "Is it normal?" ] ] ensure: [ isInLeftSideOfAssignation := oldValue ].
ifFalse: [ self flag: #todo "Is it normal?" ].
^ super visitAssignmentExpression: anAssignmentExpression
]

{ #category : 'visiting' }
FamixPythonImporterVisitor >> visitAssignmentStatement: anAssignmentStatement [

| oldValue |
oldValue := isInLeftSideOfAssignation.
isInLeftSideOfAssignation := true.
[
| variables |
variables := self acceptNode: anAssignmentStatement lhs.
"It is possible in case of multiple assignations that we end up with a collection of variables while visiting the left side of an assignation. Else we will have only one assignation. The next line is to make sure of the type of entity we have."
Expand All @@ -708,7 +698,7 @@ FamixPythonImporterVisitor >> visitAssignmentStatement: anAssignmentStatement [
accessor: self currentEntity;
isWrite: true.
self setSourceAnchor: access from: anAssignmentStatement ]
ifFalse: [ self flag: #todo "Is it normal?" ] ] ] ensure: [ isInLeftSideOfAssignation := oldValue ].
ifFalse: [ self flag: #todo "Is it normal?" ] ].
^ super visitAssignmentStatement: anAssignmentStatement
]

Expand All @@ -722,7 +712,7 @@ FamixPythonImporterVisitor >> visitClassDefinition: aClassDef [
FamixPythonImporterVisitor >> visitFieldAccessExpression: aFieldAccessExpression [
"If the receiver is self in an assignation then we have an assignation to an instance variable"

(isInLeftSideOfAssignation and: [ aFieldAccessExpression receiver isSelf ]) ifTrue: [
aFieldAccessExpression isInstanceVariableAssignation ifTrue: [
| class |
class := self currentEntityOfType: FamixPythonClass.

Expand Down Expand Up @@ -870,7 +860,7 @@ FamixPythonImporterVisitor >> visitString: aStringNode [
FamixPythonImporterVisitor >> visitVariableExpression: aVariableExpression [
"This node is used in multiple situations. If it is in an assignation we need to check if the variable we assign is created. Other uses can be for example in an import, in this case we want to only return the name."

isInLeftSideOfAssignation ifTrue: [ "If we have a variable of this name already, we should not recreate it. Except if this variable got shadowed! In that case, we should recreate it."
aVariableExpression isLeftSideOfAssignation ifTrue: [ "If we have a variable of this name already, we should not recreate it. Except if this variable got shadowed! In that case, we should recreate it."
^ (self currentEntity query descendants ofType: FamixTStructuralEntity)
detect: [ :child | child name = aVariableExpression name and: [ child isShadowable not or: [ child isShadowed not ] ] ]
ifNone: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : 'PyAssignmentExpressionNode' }

{ #category : '*Famix-Python-Importer' }
PyAssignmentExpressionNode >> isLeftSideOfAssignation: aNode [

^ self variable = aNode
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : 'PyAssignmentStatementNode' }

{ #category : '*Famix-Python-Importer' }
PyAssignmentStatementNode >> isLeftSideOfAssignation: aNode [

^ self lhs = aNode
]
10 changes: 10 additions & 0 deletions src/Famix-Python-Importer/PyFieldAccessExpressionNode.extension.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Extension { #name : 'PyFieldAccessExpressionNode' }

{ #category : '*Famix-Python-Importer' }
PyFieldAccessExpressionNode >> isInstanceVariableAssignation [

self parent
ifNil: [ ^ false ]
ifNotNil: [ :node | (node isLeftSideOfAssignation: self) ifFalse: [ ^ false ] ].

^ self receiver isSelf
]

{ #category : '*Famix-Python-Importer' }
PyFieldAccessExpressionNode >> isSelf [

Expand Down
9 changes: 9 additions & 0 deletions src/Famix-Python-Importer/PyListExpressionNode.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Extension { #name : 'PyListExpressionNode' }

{ #category : '*Famix-Python-Importer' }
PyListExpressionNode >> isLeftSideOfAssignation: aNode [

^ self parent
ifNil: [ false ]
ifNotNil: [ :node | node isLeftSideOfAssignation: self ]
]
12 changes: 12 additions & 0 deletions src/Famix-Python-Importer/PyRootNode.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ PyRootNode >> fileReference [
^ self filename
]

{ #category : '*Famix-Python-Importer' }
PyRootNode >> isAssignation [

^ false
]

{ #category : '*Famix-Python-Importer' }
PyRootNode >> isLeftSideOfAssignation: aNode [

^ false
]

{ #category : '*Famix-Python-Importer' }
PyRootNode >> isSubscriptExpression [

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Extension { #name : 'PyVariableExpressionNode' }

{ #category : '*Famix-Python-Importer' }
PyVariableExpressionNode >> isLeftSideOfAssignation [

^ self parent
ifNil: [ false ]
ifNotNil: [ :node | node isLeftSideOfAssignation: self ]
]

{ #category : '*Famix-Python-Importer' }
PyVariableExpressionNode >> isSelf [

Expand Down

0 comments on commit 5e30b9f

Please sign in to comment.