Skip to content

Commit 29ed78b

Browse files
committed
Add #allResolvedDeclarations, #allResolvedDeclarationsByName, #allResolvedVariableVersions and #allResolvedVariableVersionsByName
1 parent 826ee00 commit 29ed78b

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎src/FAST-Python-Model/FASTPyModel.class.st‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,48 @@ FASTPyModel >> allModules [
4343
^ self allWithType: FASTPyModule
4444
]
4545

46+
{ #category : 'accessing' }
47+
FASTPyModel >> allResolvedDeclarations [
48+
"This method needs the local resolution to be done.
49+
50+
I return all the local declarations of the resolved variables of the model.
51+
"
52+
53+
^ self allResolvedVariables collect: #localDeclaration as: Set
54+
]
55+
56+
{ #category : 'accessing' }
57+
FASTPyModel >> allResolvedDeclarationsByName [
58+
"This method needs the local resolution to be done.
59+
60+
I return a Dictionary grouping all the local declarations of the resolved variables of the model by their name.
61+
The keys are the name (String) and the values are the collection of declarations having this name.
62+
"
63+
64+
^ self allResolvedDeclarations groupedBy: #name
65+
]
66+
67+
{ #category : 'accessing' }
68+
FASTPyModel >> allResolvedVariableVersions [
69+
"This method needs the SSA resolution to be done.
70+
71+
I return all the SSA versions of the resolved variables of the model.
72+
"
73+
74+
^ self allResolvedVariables collect: #ssaVersion as: Set
75+
]
76+
77+
{ #category : 'accessing' }
78+
FASTPyModel >> allResolvedVariableVersionsByName [
79+
"This method needs the SSA resolution to be done.
80+
81+
I return a Dictionary grouping all the SSA versions of the resolved variables of the model by their name.
82+
The keys are the name (String) and the values are the collection of versions having this name.
83+
"
84+
85+
^ self allResolvedVariableVersions groupedBy: #name
86+
]
87+
4688
{ #category : 'accessing' }
4789
FASTPyModel >> allResolvedVariables [
4890
"This method needs the local resolution to be done.

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

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,111 @@ print(x)'.
6969
self assertCollection: model module statements fifth arguments first allReadAccesses hasSameElements: result
7070
]
7171

72+
{ #category : 'tests' }
73+
FASTPythonVariablesAnalysisTest >> testAllResolvedDeclarations [
74+
75+
self parseAndResolve: 'x = 1
76+
77+
y = 2
78+
79+
print(x)'.
80+
81+
self assert: model allResolvedDeclarations size equals: 2.
82+
self assertCollection: model allResolvedDeclarations hasSameElements: {
83+
model module statements first left.
84+
model module statements second left }
85+
]
86+
87+
{ #category : 'tests' }
88+
FASTPythonVariablesAnalysisTest >> testAllResolvedDeclarationsByName [
89+
90+
| byName |
91+
self parseAndResolve: 'x = 1
92+
93+
print(x)
94+
95+
x = 2
96+
97+
y = 3'.
98+
99+
byName := model allResolvedDeclarationsByName.
100+
101+
self assert: byName size equals: 2.
102+
self assert: byName keys asArray sort equals: #( 'x' 'y' ).
103+
self assert: (byName at: 'x') size equals: 1.
104+
self assert: (byName at: 'y') size equals: 1
105+
]
106+
107+
{ #category : 'tests' }
108+
FASTPythonVariablesAnalysisTest >> testAllResolvedDeclarationsForReassignedVariable [
109+
110+
self parseAndResolve: 'x = 1
111+
112+
print(x)
113+
114+
x = 2'.
115+
116+
self assert: model allResolvedDeclarations size equals: 1.
117+
self assert: model allResolvedDeclarations anyOne equals: model module statements first left
118+
]
119+
120+
{ #category : 'tests' }
121+
FASTPythonVariablesAnalysisTest >> testAllResolvedVariableVersions [
122+
123+
| versions |
124+
self parseAndResolve: 'x = 1
125+
126+
print(x)
127+
128+
y = 2
129+
130+
print(y)'.
131+
132+
versions := model allResolvedVariableVersions.
133+
134+
self assert: versions size equals: 2.
135+
self assertCollection: (versions collect: [ :each | each version ]) asArray hasSameElements: { 1. 1 }
136+
]
137+
138+
{ #category : 'tests' }
139+
FASTPythonVariablesAnalysisTest >> testAllResolvedVariableVersionsByName [
140+
141+
| byName |
142+
self parseAndResolve: 'x = 1
143+
144+
print(x)
145+
146+
x = 2
147+
148+
y = 3
149+
150+
print(y)'.
151+
152+
byName := model allResolvedVariableVersionsByName.
153+
154+
self assert: byName size equals: 3.
155+
self assert: byName keys asArray sort equals: #( 'x_1' 'x_2' 'y_1' ).
156+
self assert: (byName values allSatisfy: [ :each | each size = 1 ])
157+
]
158+
159+
{ #category : 'tests' }
160+
FASTPythonVariablesAnalysisTest >> testAllResolvedVariableVersionsForReassignedVariable [
161+
162+
| versions |
163+
self parseAndResolve: 'x = 1
164+
165+
print(x)
166+
167+
x = 2
168+
169+
print(x)'.
170+
171+
versions := model allResolvedVariableVersions.
172+
173+
self assert: versions size equals: 2.
174+
self assertCollection: (versions collect: [ :each | each version ]) asArray hasSameElements: { 1. 2 }
175+
]
176+
72177
{ #category : 'tests' }
73178
FASTPythonVariablesAnalysisTest >> testAllResolvedVariablesByNameEmptyModel [
74179
| byName |

0 commit comments

Comments
 (0)