Skip to content

Commit

Permalink
Start to manage internal accesses to ivar
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Dec 11, 2024
1 parent f04f996 commit 019f471
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
37 changes: 35 additions & 2 deletions src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,26 @@ FamixPythonProject1Test >> testReadAccessFromFunction [
self assert: (function accesses anySatisfy: [ :anAccess | anAccess variable = temporary ])
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromImportedGlobalWithNamespace [

| global module access |
self skip.
global := self globalVariableNamed: 'rootPackage2Variable'.
module := self moduleNamed: 'moduleAtRoot'.

access := global incomingAccesses detect: [ :anAccess | anAccess accessor = module ].

self assert: access class equals: FamixPythonAccess.
self assert: access source equals: module.
self assert: access accessor equals: module.
self assert: access target equals: global.
self assert: access variable equals: global.
self deny: access isWrite.
self assert: access isRead.
self assert: (module accesses anySatisfy: [ :anAccess | anAccess variable = global ])
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessFromLambda [

Expand Down Expand Up @@ -2277,10 +2297,9 @@ FamixPythonProject1Test >> testReadAccessToGlobal [
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessToIVar [
FamixPythonProject1Test >> testReadAccessToIVarInternalyToTheClass [

| ivar method access |
self skip. "Todo"
ivar := self attributeNamed: 'dog_color'.
method := self methodNamed: 'get_dog_color'.

Expand All @@ -2297,6 +2316,20 @@ FamixPythonProject1Test >> testReadAccessToIVar [
self assert: (method accesses anySatisfy: [ :anAccess | anAccess variable = ivar ])
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessToIVarInternalyToTheClassSourceAnchor [

| ivar method access |
ivar := self attributeNamed: 'dog_color'.
method := self methodNamed: 'get_dog_color'.

"We want to have the reading and not the writing."
access := (ivar incomingAccesses select: [ :anAccess | anAccess accessor = method ]) detectMax: [ :entity | entity sourceAnchor startPos ].

self assert: access sourceAnchor isNotNil.
self assert: access sourceText equals: 'self.dog_color'
]

{ #category : 'tests - accesses' }
FamixPythonProject1Test >> testReadAccessToImportedGlobal [

Expand Down
20 changes: 20 additions & 0 deletions src/Famix-Python-Importer/FamixPythonImporterVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,10 @@ FamixPythonImporterVisitor >> visitClassDefinition: aClassDef [
{ #category : 'generated' }
FamixPythonImporterVisitor >> visitFieldAccessExpression: aFieldAccessExpression [

self flag: #todo. "Manage invocations."
aFieldAccessExpression parent isFunctionCallExpression ifTrue: [ ^ aFieldAccessExpression source ].

aFieldAccessExpression source traceCr.
^ aFieldAccessExpression source
]

Expand Down Expand Up @@ -819,6 +823,22 @@ FamixPythonImporterVisitor >> visitImportVariableExpression: anImportVariableExp
^ anImportVariableExpression name
]

{ #category : 'visiting' }
FamixPythonImporterVisitor >> visitInternalInstanceVariableAccess: anInternalInstanceVariableAccess [

self
resolve: ((SRIdentifierWithNode identifier: anInternalInstanceVariableAccess name)
expectedKind: { FamixPythonAttribute };
notFoundReplacementEntity: [ :unresolved :currentEntity | self flag: #todo. 1 halt "self ensureStubUnknownAccessedOrReferencedEntityNamed: unresolved identifier" ];
yourself)
foundAction: [ :entity :currentEntity |
| association |
association := entity createAccessOrReferenceFrom: currentEntity node: anInternalInstanceVariableAccess.
self setSourceAnchor: association from: anInternalInstanceVariableAccess ].

^ anInternalInstanceVariableAccess source
]

{ #category : 'visiting' }
FamixPythonImporterVisitor >> visitLambdaExpression: aLambdaExpression [

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"
I reperesent a specialization of `PyFieldAccessExpressionNode`. I represent a read access to an instance variable inside its class declaration with `self` as receiver.
"
Class {
#name : 'FamixPythonInternalInstanceVariableAccessNode',
#superclass : 'PyFieldAccessExpressionNode',
#category : 'Famix-Python-Importer-Nodes',
#package : 'Famix-Python-Importer',
#tag : 'Nodes'
}

{ #category : 'generated' }
FamixPythonInternalInstanceVariableAccessNode >> acceptVisitor: aRootVisitor [

^ aRootVisitor visitInternalInstanceVariableAccess: self
]
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ FamixPythonSmaCCASTSpecializer >> visitFieldAccessExpression: aFieldAccessExpres

aFieldAccessExpression isInstanceVariableAssignation ifTrue: [ ^ FamixPythonAssignedInstanceVariableNode adoptInstance: aFieldAccessExpression ].

aFieldAccessExpression isInImport ifTrue: [ ^ FamixPythonImportPathNode adoptInstance: aFieldAccessExpression ]
aFieldAccessExpression isInImport ifTrue: [ ^ FamixPythonImportPathNode adoptInstance: aFieldAccessExpression ].

"This one needs to be after #isInstanceVariableAssignation."
aFieldAccessExpression isInternalInstanceVariableAccess ifTrue: [ ^ FamixPythonInternalInstanceVariableAccessNode adoptInstance: aFieldAccessExpression ]
]

{ #category : 'generated' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ PyFieldAccessExpressionNode >> isInstanceVariableAssignation [
^ self receiver isSelf
]

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

^ self receiver isSelf
]

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

Expand Down
5 changes: 5 additions & 0 deletions src/Famix-Python-Importer/TPyRootNodeVisitor.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ TPyRootNodeVisitor >> visitImportVariableExpression: anImportVariableExpression
^ self visitVariableExpression: anImportVariableExpression
]

{ #category : '*Famix-Python-Importer' }
TPyRootNodeVisitor >> visitInternalInstanceVariableAccess: anInternalInstanceVariableAccess [
^ self visitExpression: anInternalInstanceVariableAccess
]

{ #category : '*Famix-Python-Importer' }
TPyRootNodeVisitor >> visitPlaceholderVariable: aPlaceholderVariable [

Expand Down

0 comments on commit 019f471

Please sign in to comment.