48
48
import com .oracle .graal .python .builtins .objects .object .PythonBuiltinObject ;
49
49
import com .oracle .graal .python .builtins .objects .type .PythonClass ;
50
50
import com .oracle .graal .python .nodes .ModuleRootNode ;
51
+ import com .oracle .graal .python .nodes .PNode ;
51
52
import com .oracle .graal .python .nodes .argument .ReadIndexedArgumentNode ;
52
53
import com .oracle .graal .python .nodes .argument .ReadKeywordNode ;
53
54
import com .oracle .graal .python .nodes .argument .ReadVarArgsNode ;
@@ -101,7 +102,6 @@ public class PCode extends PythonBuiltinObject {
101
102
// tuple of names of cell variables (referenced by containing scopes)
102
103
private Object [] cellvars ;
103
104
104
- @ TruffleBoundary
105
105
public PCode (PythonClass cls , RootNode rootNode , PythonCore core ) {
106
106
super (cls );
107
107
this .rootNode = rootNode ;
@@ -135,11 +135,12 @@ public PCode(PythonClass cls, int argcount, int kwonlyargcount,
135
135
this .cellvars = cellvars ;
136
136
}
137
137
138
+ @ TruffleBoundary
138
139
private static Set <String > asSet (String [] values ) {
139
140
return (values != null ) ? new HashSet <>(Arrays .asList (values )) : new HashSet <>();
140
141
}
141
142
142
- private static String [] getFreeVars (RootNode rootNode ) {
143
+ private static String [] extractFreeVars (RootNode rootNode ) {
143
144
if (rootNode instanceof FunctionRootNode ) {
144
145
return ((FunctionRootNode ) rootNode ).getFreeVars ();
145
146
} else if (rootNode instanceof GeneratorFunctionRootNode ) {
@@ -149,7 +150,7 @@ private static String[] getFreeVars(RootNode rootNode) {
149
150
}
150
151
}
151
152
152
- private static String [] getCellVars (RootNode rootNode ) {
153
+ private static String [] extractCellVars (RootNode rootNode ) {
153
154
if (rootNode instanceof FunctionRootNode ) {
154
155
return ((FunctionRootNode ) rootNode ).getCellVars ();
155
156
} else if (rootNode instanceof GeneratorFunctionRootNode ) {
@@ -160,18 +161,20 @@ private static String[] getCellVars(RootNode rootNode) {
160
161
}
161
162
162
163
private static String extractFileName (RootNode rootNode ) {
163
- SourceSection src = rootNode .getSourceSection ();
164
+ RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode () : rootNode ;
165
+ SourceSection src = funcRootNode .getSourceSection ();
164
166
if (src != null ) {
165
167
return src .getSource ().getName ();
166
- } else if (rootNode instanceof ModuleRootNode ) {
167
- return rootNode .getName ();
168
+ } else if (funcRootNode instanceof ModuleRootNode ) {
169
+ return funcRootNode .getName ();
168
170
} else {
169
171
return null ;
170
172
}
171
173
}
172
174
173
175
private static int extractFirstLineno (RootNode rootNode ) {
174
- SourceSection sourceSection = rootNode .getSourceSection ();
176
+ RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode ) ? ((GeneratorFunctionRootNode ) rootNode ).getFunctionRootNode () : rootNode ;
177
+ SourceSection sourceSection = funcRootNode .getSourceSection ();
175
178
return (sourceSection != null ) ? sourceSection .getStartLine () : 1 ;
176
179
}
177
180
@@ -187,6 +190,7 @@ private static String extractName(RootNode rootNode) {
187
190
return name ;
188
191
}
189
192
193
+ @ TruffleBoundary
190
194
private static Set <String > getKeywordArgumentNames (List <ReadKeywordNode > readKeywordNodes ) {
191
195
Set <String > kwArgNames = new HashSet <>();
192
196
for (ReadKeywordNode node : readKeywordNodes ) {
@@ -195,9 +199,10 @@ private static Set<String> getKeywordArgumentNames(List<ReadKeywordNode> readKey
195
199
return kwArgNames ;
196
200
}
197
201
198
- private static Set <String > getArgumentNames (List <ReadIndexedArgumentNode > readIndexedArgumentNodes ) {
202
+ @ TruffleBoundary
203
+ private static Set <String > extractArgumentNames (List <? extends PNode > readIndexedArgumentNodes ) {
199
204
Set <String > argNames = new HashSet <>();
200
- for (ReadIndexedArgumentNode node : readIndexedArgumentNodes ) {
205
+ for (PNode node : readIndexedArgumentNodes ) {
201
206
Node parent = node .getParent ();
202
207
if (parent instanceof WriteIdentifierNode ) {
203
208
Object identifier = ((WriteIdentifierNode ) parent ).getIdentifier ();
@@ -213,25 +218,42 @@ private static int extractStackSize(RootNode rootNode) {
213
218
return rootNode .getFrameDescriptor ().getSize ();
214
219
}
215
220
221
+ @ TruffleBoundary
216
222
private void extractArgStats () {
217
- this .freevars = getFreeVars (rootNode );
218
- this .cellvars = getCellVars (rootNode );
219
- Set <String > freeVarsSet = asSet ((String [])freevars );
220
- Set <String > cellVarsSet = asSet ((String [])cellvars );
223
+ // 0x20 - generator
224
+ this .flags = 0 ;
225
+ RootNode funcRootNode = rootNode ;
226
+ if (funcRootNode instanceof GeneratorFunctionRootNode ) {
227
+ flags |= (1 << 5 );
228
+ funcRootNode = ((GeneratorFunctionRootNode ) funcRootNode ).getFunctionRootNode ();
229
+ }
221
230
222
- List <ReadKeywordNode > readKeywordNodes = NodeUtil .findAllNodeInstances (rootNode , ReadKeywordNode .class );
223
- List <ReadIndexedArgumentNode > readIndexedArgumentNodes = NodeUtil .findAllNodeInstances (rootNode , ReadIndexedArgumentNode .class );
231
+ // 0x04 - *arguments
232
+ if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarArgsNode .class ).size () == 1 ) {
233
+ flags |= (1 << 2 );
234
+ }
235
+ // 0x08 - **keywords
236
+ if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarKeywordsNode .class ).size () == 1 ) {
237
+ flags |= (1 << 3 );
238
+ }
239
+
240
+ this .freevars = extractFreeVars (rootNode );
241
+ this .cellvars = extractCellVars (rootNode );
242
+ Set <String > freeVarsSet = asSet ((String []) freevars );
243
+ Set <String > cellVarsSet = asSet ((String []) cellvars );
244
+
245
+ List <ReadKeywordNode > readKeywordNodes = NodeUtil .findAllNodeInstances (funcRootNode , ReadKeywordNode .class );
246
+ List <ReadIndexedArgumentNode > readIndexedArgumentNodes = NodeUtil .findAllNodeInstances (funcRootNode , ReadIndexedArgumentNode .class );
224
247
225
248
Set <String > kwNames = getKeywordArgumentNames (readKeywordNodes );
226
- Set <String > argNames = getArgumentNames (readIndexedArgumentNodes );
249
+ Set <String > argNames = extractArgumentNames (readIndexedArgumentNodes );
227
250
228
251
Set <String > allArgNames = new HashSet <>();
229
252
allArgNames .addAll (kwNames );
230
253
allArgNames .addAll (argNames );
231
254
232
255
this .argcount = readIndexedArgumentNodes .size ();
233
256
this .kwonlyargcount = 0 ;
234
- this .flags = 0 ;
235
257
236
258
for (ReadKeywordNode kwNode : readKeywordNodes ) {
237
259
if (!kwNode .canBePositional ()) {
@@ -254,22 +276,6 @@ private void extractArgStats() {
254
276
}
255
277
}
256
278
257
- // 0x20 - generator
258
- RootNode funcRootNode = rootNode ;
259
- if (funcRootNode instanceof GeneratorFunctionRootNode ) {
260
- flags |= (1 << 5 );
261
- funcRootNode = ((GeneratorFunctionRootNode ) funcRootNode ).getFunctionRootNode ();
262
- }
263
-
264
- // 0x04 - *arguments
265
- if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarArgsNode .class ).size () == 1 ) {
266
- flags |= (1 << 2 );
267
- }
268
- // 0x08 - **keywords
269
- if (NodeUtil .findAllNodeInstances (funcRootNode , ReadVarKeywordsNode .class ).size () == 1 ) {
270
- flags |= (1 << 3 );
271
- }
272
-
273
279
this .varnames = varnamesSet .toArray ();
274
280
this .nlocals = varnamesSet .size ();
275
281
}
0 commit comments