Line data Source code
1 : /*
2 : * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 : *
4 : * The primary entry point is PyAST_Compile(), which returns a
5 : * PyCodeObject. The compiler makes several passes to build the code
6 : * object:
7 : * 1. Checks for future statements. See future.c
8 : * 2. Builds a symbol table. See symtable.c.
9 : * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 : * 4. Assemble the basic blocks into final code. See assemble() in
11 : * this file.
12 : * 5. Optimize the byte code (peephole optimizations). See peephole.c
13 : *
14 : * Note that compiler_mod() suggests module, but the module ast type
15 : * (mod_ty) has cases for expressions and interactive statements.
16 : *
17 : * CAUTION: The VISIT_* macros abort the current function when they
18 : * encounter a problem. So don't invoke them when there is memory
19 : * which needs to be released. Code blocks are OK, as the compiler
20 : * structure takes care of releasing those. Use the arena to manage
21 : * objects.
22 : */
23 :
24 : #include "Python.h"
25 :
26 : #include "Python-ast.h"
27 : #include "node.h"
28 : #include "ast.h"
29 : #include "code.h"
30 : #include "symtable.h"
31 : #include "opcode.h"
32 :
33 : int Py_OptimizeFlag = 0;
34 :
35 : #define DEFAULT_BLOCK_SIZE 16
36 : #define DEFAULT_BLOCKS 8
37 : #define DEFAULT_CODE_SIZE 128
38 : #define DEFAULT_LNOTAB_SIZE 16
39 :
40 : #define COMP_GENEXP 0
41 : #define COMP_LISTCOMP 1
42 : #define COMP_SETCOMP 2
43 : #define COMP_DICTCOMP 3
44 :
45 : struct instr {
46 : unsigned i_jabs : 1;
47 : unsigned i_jrel : 1;
48 : unsigned i_hasarg : 1;
49 : unsigned char i_opcode;
50 : int i_oparg;
51 : struct basicblock_ *i_target; /* target block (if jump instruction) */
52 : int i_lineno;
53 : };
54 :
55 : typedef struct basicblock_ {
56 : /* Each basicblock in a compilation unit is linked via b_list in the
57 : reverse order that the block are allocated. b_list points to the next
58 : block, not to be confused with b_next, which is next by control flow. */
59 : struct basicblock_ *b_list;
60 : /* number of instructions used */
61 : int b_iused;
62 : /* length of instruction array (b_instr) */
63 : int b_ialloc;
64 : /* pointer to an array of instructions, initially NULL */
65 : struct instr *b_instr;
66 : /* If b_next is non-NULL, it is a pointer to the next
67 : block reached by normal control flow. */
68 : struct basicblock_ *b_next;
69 : /* b_seen is used to perform a DFS of basicblocks. */
70 : unsigned b_seen : 1;
71 : /* b_return is true if a RETURN_VALUE opcode is inserted. */
72 : unsigned b_return : 1;
73 : /* depth of stack upon entry of block, computed by stackdepth() */
74 : int b_startdepth;
75 : /* instruction offset for block, computed by assemble_jump_offsets() */
76 : int b_offset;
77 : } basicblock;
78 :
79 : /* fblockinfo tracks the current frame block.
80 :
81 : A frame block is used to handle loops, try/except, and try/finally.
82 : It's called a frame block to distinguish it from a basic block in the
83 : compiler IR.
84 : */
85 :
86 : enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
87 :
88 : struct fblockinfo {
89 : enum fblocktype fb_type;
90 : basicblock *fb_block;
91 : };
92 :
93 : enum {
94 : COMPILER_SCOPE_MODULE,
95 : COMPILER_SCOPE_CLASS,
96 : COMPILER_SCOPE_FUNCTION,
97 : COMPILER_SCOPE_COMPREHENSION,
98 : };
99 :
100 : /* The following items change on entry and exit of code blocks.
101 : They must be saved and restored when returning to a block.
102 : */
103 : struct compiler_unit {
104 : PySTEntryObject *u_ste;
105 :
106 : PyObject *u_name;
107 : PyObject *u_qualname; /* dot-separated qualified name (lazy) */
108 : int u_scope_type;
109 :
110 : /* The following fields are dicts that map objects to
111 : the index of them in co_XXX. The index is used as
112 : the argument for opcodes that refer to those collections.
113 : */
114 : PyObject *u_consts; /* all constants */
115 : PyObject *u_names; /* all names */
116 : PyObject *u_varnames; /* local variables */
117 : PyObject *u_cellvars; /* cell variables */
118 : PyObject *u_freevars; /* free variables */
119 :
120 : PyObject *u_private; /* for private name mangling */
121 :
122 : int u_argcount; /* number of arguments for block */
123 : int u_kwonlyargcount; /* number of keyword only arguments for block */
124 : /* Pointer to the most recently allocated block. By following b_list
125 : members, you can reach all early allocated blocks. */
126 : basicblock *u_blocks;
127 : basicblock *u_curblock; /* pointer to current block */
128 :
129 : int u_nfblocks;
130 : struct fblockinfo u_fblock[CO_MAXBLOCKS];
131 :
132 : int u_firstlineno; /* the first lineno of the block */
133 : int u_lineno; /* the lineno for the current stmt */
134 : int u_col_offset; /* the offset of the current stmt */
135 : int u_lineno_set; /* boolean to indicate whether instr
136 : has been generated with current lineno */
137 : };
138 :
139 : /* This struct captures the global state of a compilation.
140 :
141 : The u pointer points to the current compilation unit, while units
142 : for enclosing blocks are stored in c_stack. The u and c_stack are
143 : managed by compiler_enter_scope() and compiler_exit_scope().
144 : */
145 :
146 : struct compiler {
147 : const char *c_filename;
148 : PyObject *c_filename_obj;
149 : struct symtable *c_st;
150 : PyFutureFeatures *c_future; /* pointer to module's __future__ */
151 : PyCompilerFlags *c_flags;
152 :
153 : int c_optimize; /* optimization level */
154 : int c_interactive; /* true if in interactive mode */
155 : int c_nestlevel;
156 :
157 : struct compiler_unit *u; /* compiler state for current block */
158 : PyObject *c_stack; /* Python list holding compiler_unit ptrs */
159 : PyArena *c_arena; /* pointer to memory allocation arena */
160 : };
161 :
162 : static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
163 : static void compiler_free(struct compiler *);
164 : static basicblock *compiler_new_block(struct compiler *);
165 : static int compiler_next_instr(struct compiler *, basicblock *);
166 : static int compiler_addop(struct compiler *, int);
167 : static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
168 : static int compiler_addop_i(struct compiler *, int, int);
169 : static int compiler_addop_j(struct compiler *, int, basicblock *, int);
170 : static basicblock *compiler_use_new_block(struct compiler *);
171 : static int compiler_error(struct compiler *, const char *);
172 : static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
173 :
174 : static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
175 : static int compiler_visit_stmt(struct compiler *, stmt_ty);
176 : static int compiler_visit_keyword(struct compiler *, keyword_ty);
177 : static int compiler_visit_expr(struct compiler *, expr_ty);
178 : static int compiler_augassign(struct compiler *, stmt_ty);
179 : static int compiler_visit_slice(struct compiler *, slice_ty,
180 : expr_context_ty);
181 :
182 : static int compiler_push_fblock(struct compiler *, enum fblocktype,
183 : basicblock *);
184 : static void compiler_pop_fblock(struct compiler *, enum fblocktype,
185 : basicblock *);
186 : /* Returns true if there is a loop on the fblock stack. */
187 : static int compiler_in_loop(struct compiler *);
188 :
189 : static int inplace_binop(struct compiler *, operator_ty);
190 : static int expr_constant(struct compiler *, expr_ty);
191 :
192 : static int compiler_with(struct compiler *, stmt_ty, int);
193 : static int compiler_call_helper(struct compiler *c, int n,
194 : asdl_seq *args,
195 : asdl_seq *keywords,
196 : expr_ty starargs,
197 : expr_ty kwargs);
198 : static int compiler_try_except(struct compiler *, stmt_ty);
199 :
200 : static PyCodeObject *assemble(struct compiler *, int addNone);
201 : static PyObject *__doc__;
202 :
203 : #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
204 :
205 : PyObject *
206 951 : _Py_Mangle(PyObject *privateobj, PyObject *ident)
207 : {
208 : /* Name mangling: __private becomes _classname__private.
209 : This is independent from how the name is used. */
210 : PyObject *result;
211 : size_t nlen, plen, ipriv;
212 : Py_UCS4 maxchar;
213 2696 : if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
214 1745 : PyUnicode_READ_CHAR(ident, 0) != '_' ||
215 246 : PyUnicode_READ_CHAR(ident, 1) != '_') {
216 910 : Py_INCREF(ident);
217 910 : return ident;
218 : }
219 41 : nlen = PyUnicode_GET_LENGTH(ident);
220 41 : plen = PyUnicode_GET_LENGTH(privateobj);
221 : /* Don't mangle __id__ or names with dots.
222 :
223 : The only time a name with a dot can occur is when
224 : we are compiling an import statement that has a
225 : package name.
226 :
227 : TODO(jhylton): Decide whether we want to support
228 : mangling of the module name, e.g. __M.X.
229 : */
230 123 : if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
231 82 : PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
232 0 : PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
233 41 : Py_INCREF(ident);
234 41 : return ident; /* Don't mangle __whatever__ */
235 : }
236 : /* Strip leading underscores from class name */
237 0 : ipriv = 0;
238 0 : while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
239 0 : ipriv++;
240 0 : if (ipriv == plen) {
241 0 : Py_INCREF(ident);
242 0 : return ident; /* Don't mangle if class is just underscores */
243 : }
244 0 : plen -= ipriv;
245 :
246 : assert(1 <= PY_SSIZE_T_MAX - nlen);
247 : assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
248 :
249 0 : maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
250 0 : if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
251 0 : maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
252 :
253 0 : result = PyUnicode_New(1 + nlen + plen, maxchar);
254 0 : if (!result)
255 0 : return 0;
256 : /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
257 0 : PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
258 0 : if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
259 0 : Py_DECREF(result);
260 0 : return NULL;
261 : }
262 0 : if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
263 0 : Py_DECREF(result);
264 0 : return NULL;
265 : }
266 : assert(_PyUnicode_CheckConsistency(result, 1));
267 0 : return result;
268 : }
269 :
270 : static int
271 3 : compiler_init(struct compiler *c)
272 : {
273 3 : memset(c, 0, sizeof(struct compiler));
274 :
275 3 : c->c_stack = PyList_New(0);
276 3 : if (!c->c_stack)
277 0 : return 0;
278 :
279 3 : return 1;
280 : }
281 :
282 : PyCodeObject *
283 3 : PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
284 : int optimize, PyArena *arena)
285 : {
286 : struct compiler c;
287 3 : PyCodeObject *co = NULL;
288 : PyCompilerFlags local_flags;
289 : int merged;
290 :
291 3 : if (!__doc__) {
292 1 : __doc__ = PyUnicode_InternFromString("__doc__");
293 1 : if (!__doc__)
294 0 : return NULL;
295 : }
296 :
297 3 : if (!compiler_init(&c))
298 0 : return NULL;
299 3 : c.c_filename = filename;
300 3 : c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
301 3 : if (!c.c_filename_obj)
302 0 : goto finally;
303 3 : c.c_arena = arena;
304 3 : c.c_future = PyFuture_FromAST(mod, filename);
305 3 : if (c.c_future == NULL)
306 0 : goto finally;
307 3 : if (!flags) {
308 0 : local_flags.cf_flags = 0;
309 0 : flags = &local_flags;
310 : }
311 3 : merged = c.c_future->ff_features | flags->cf_flags;
312 3 : c.c_future->ff_features = merged;
313 3 : flags->cf_flags = merged;
314 3 : c.c_flags = flags;
315 3 : c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
316 3 : c.c_nestlevel = 0;
317 :
318 3 : c.c_st = PySymtable_Build(mod, filename, c.c_future);
319 3 : if (c.c_st == NULL) {
320 0 : if (!PyErr_Occurred())
321 0 : PyErr_SetString(PyExc_SystemError, "no symtable");
322 0 : goto finally;
323 : }
324 :
325 3 : co = compiler_mod(&c, mod);
326 :
327 : finally:
328 3 : compiler_free(&c);
329 : assert(co || PyErr_Occurred());
330 3 : return co;
331 : }
332 :
333 : PyCodeObject *
334 0 : PyNode_Compile(struct _node *n, const char *filename)
335 : {
336 0 : PyCodeObject *co = NULL;
337 : mod_ty mod;
338 0 : PyArena *arena = PyArena_New();
339 0 : if (!arena)
340 0 : return NULL;
341 0 : mod = PyAST_FromNode(n, NULL, filename, arena);
342 0 : if (mod)
343 0 : co = PyAST_Compile(mod, filename, NULL, arena);
344 0 : PyArena_Free(arena);
345 0 : return co;
346 : }
347 :
348 : static void
349 3 : compiler_free(struct compiler *c)
350 : {
351 3 : if (c->c_st)
352 3 : PySymtable_Free(c->c_st);
353 3 : if (c->c_future)
354 3 : PyObject_Free(c->c_future);
355 3 : if (c->c_filename_obj)
356 3 : Py_DECREF(c->c_filename_obj);
357 3 : Py_DECREF(c->c_stack);
358 3 : }
359 :
360 : static PyObject *
361 30 : list2dict(PyObject *list)
362 : {
363 : Py_ssize_t i, n;
364 : PyObject *v, *k;
365 30 : PyObject *dict = PyDict_New();
366 30 : if (!dict) return NULL;
367 :
368 30 : n = PyList_Size(list);
369 87 : for (i = 0; i < n; i++) {
370 57 : v = PyLong_FromLong(i);
371 57 : if (!v) {
372 0 : Py_DECREF(dict);
373 0 : return NULL;
374 : }
375 57 : k = PyList_GET_ITEM(list, i);
376 57 : k = PyTuple_Pack(2, k, k->ob_type);
377 57 : if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
378 0 : Py_XDECREF(k);
379 0 : Py_DECREF(v);
380 0 : Py_DECREF(dict);
381 0 : return NULL;
382 : }
383 57 : Py_DECREF(k);
384 57 : Py_DECREF(v);
385 : }
386 30 : return dict;
387 : }
388 :
389 : /* Return new dict containing names from src that match scope(s).
390 :
391 : src is a symbol table dictionary. If the scope of a name matches
392 : either scope_type or flag is set, insert it into the new dict. The
393 : values are integers, starting at offset and increasing by one for
394 : each key.
395 : */
396 :
397 : static PyObject *
398 60 : dictbytype(PyObject *src, int scope_type, int flag, int offset)
399 : {
400 60 : Py_ssize_t i = offset, scope, num_keys, key_i;
401 60 : PyObject *k, *v, *dest = PyDict_New();
402 : PyObject *sorted_keys;
403 :
404 : assert(offset >= 0);
405 60 : if (dest == NULL)
406 0 : return NULL;
407 :
408 : /* Sort the keys so that we have a deterministic order on the indexes
409 : saved in the returned dictionary. These indexes are used as indexes
410 : into the free and cell var storage. Therefore if they aren't
411 : deterministic, then the generated bytecode is not deterministic.
412 : */
413 60 : sorted_keys = PyDict_Keys(src);
414 60 : if (sorted_keys == NULL)
415 0 : return NULL;
416 60 : if (PyList_Sort(sorted_keys) != 0) {
417 0 : Py_DECREF(sorted_keys);
418 0 : return NULL;
419 : }
420 60 : num_keys = PyList_GET_SIZE(sorted_keys);
421 :
422 460 : for (key_i = 0; key_i < num_keys; key_i++) {
423 : /* XXX this should probably be a macro in symtable.h */
424 : long vi;
425 400 : k = PyList_GET_ITEM(sorted_keys, key_i);
426 400 : v = PyDict_GetItem(src, k);
427 : assert(PyLong_Check(v));
428 400 : vi = PyLong_AS_LONG(v);
429 400 : scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
430 :
431 400 : if (scope == scope_type || vi & flag) {
432 0 : PyObject *tuple, *item = PyLong_FromLong(i);
433 0 : if (item == NULL) {
434 0 : Py_DECREF(sorted_keys);
435 0 : Py_DECREF(dest);
436 0 : return NULL;
437 : }
438 0 : i++;
439 0 : tuple = PyTuple_Pack(2, k, k->ob_type);
440 0 : if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
441 0 : Py_DECREF(sorted_keys);
442 0 : Py_DECREF(item);
443 0 : Py_DECREF(dest);
444 0 : Py_XDECREF(tuple);
445 0 : return NULL;
446 : }
447 0 : Py_DECREF(item);
448 0 : Py_DECREF(tuple);
449 : }
450 : }
451 60 : Py_DECREF(sorted_keys);
452 60 : return dest;
453 : }
454 :
455 : static void
456 57 : compiler_unit_check(struct compiler_unit *u)
457 : {
458 : basicblock *block;
459 189 : for (block = u->u_blocks; block != NULL; block = block->b_list) {
460 : assert((void *)block != (void *)0xcbcbcbcb);
461 : assert((void *)block != (void *)0xfbfbfbfb);
462 : assert((void *)block != (void *)0xdbdbdbdb);
463 132 : if (block->b_instr != NULL) {
464 : assert(block->b_ialloc > 0);
465 : assert(block->b_iused > 0);
466 : assert(block->b_ialloc >= block->b_iused);
467 : }
468 : else {
469 : assert (block->b_iused == 0);
470 : assert (block->b_ialloc == 0);
471 : }
472 : }
473 57 : }
474 :
475 : static void
476 30 : compiler_unit_free(struct compiler_unit *u)
477 : {
478 : basicblock *b, *next;
479 :
480 30 : compiler_unit_check(u);
481 30 : b = u->u_blocks;
482 159 : while (b != NULL) {
483 99 : if (b->b_instr)
484 93 : PyObject_Free((void *)b->b_instr);
485 99 : next = b->b_list;
486 99 : PyObject_Free((void *)b);
487 99 : b = next;
488 : }
489 30 : Py_CLEAR(u->u_ste);
490 30 : Py_CLEAR(u->u_name);
491 30 : Py_CLEAR(u->u_qualname);
492 30 : Py_CLEAR(u->u_consts);
493 30 : Py_CLEAR(u->u_names);
494 30 : Py_CLEAR(u->u_varnames);
495 30 : Py_CLEAR(u->u_freevars);
496 30 : Py_CLEAR(u->u_cellvars);
497 30 : Py_CLEAR(u->u_private);
498 30 : PyObject_Free(u);
499 30 : }
500 :
501 : static int
502 30 : compiler_enter_scope(struct compiler *c, identifier name,
503 : int scope_type, void *key, int lineno)
504 : {
505 : struct compiler_unit *u;
506 :
507 30 : u = (struct compiler_unit *)PyObject_Malloc(sizeof(
508 : struct compiler_unit));
509 30 : if (!u) {
510 0 : PyErr_NoMemory();
511 0 : return 0;
512 : }
513 30 : memset(u, 0, sizeof(struct compiler_unit));
514 30 : u->u_scope_type = scope_type;
515 30 : u->u_argcount = 0;
516 30 : u->u_kwonlyargcount = 0;
517 30 : u->u_ste = PySymtable_Lookup(c->c_st, key);
518 30 : if (!u->u_ste) {
519 0 : compiler_unit_free(u);
520 0 : return 0;
521 : }
522 30 : Py_INCREF(name);
523 30 : u->u_name = name;
524 30 : u->u_varnames = list2dict(u->u_ste->ste_varnames);
525 30 : u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
526 30 : if (!u->u_varnames || !u->u_cellvars) {
527 0 : compiler_unit_free(u);
528 0 : return 0;
529 : }
530 :
531 30 : u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
532 : PyDict_Size(u->u_cellvars));
533 30 : if (!u->u_freevars) {
534 0 : compiler_unit_free(u);
535 0 : return 0;
536 : }
537 :
538 30 : u->u_blocks = NULL;
539 30 : u->u_nfblocks = 0;
540 30 : u->u_firstlineno = lineno;
541 30 : u->u_lineno = 0;
542 30 : u->u_col_offset = 0;
543 30 : u->u_lineno_set = 0;
544 30 : u->u_consts = PyDict_New();
545 30 : if (!u->u_consts) {
546 0 : compiler_unit_free(u);
547 0 : return 0;
548 : }
549 30 : u->u_names = PyDict_New();
550 30 : if (!u->u_names) {
551 0 : compiler_unit_free(u);
552 0 : return 0;
553 : }
554 :
555 30 : u->u_private = NULL;
556 :
557 : /* Push the old compiler_unit on the stack. */
558 30 : if (c->u) {
559 27 : PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
560 27 : if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
561 0 : Py_XDECREF(capsule);
562 0 : compiler_unit_free(u);
563 0 : return 0;
564 : }
565 27 : Py_DECREF(capsule);
566 27 : u->u_private = c->u->u_private;
567 27 : Py_XINCREF(u->u_private);
568 : }
569 30 : c->u = u;
570 :
571 30 : c->c_nestlevel++;
572 30 : if (compiler_use_new_block(c) == NULL)
573 0 : return 0;
574 :
575 30 : return 1;
576 : }
577 :
578 : static void
579 30 : compiler_exit_scope(struct compiler *c)
580 : {
581 : int n;
582 : PyObject *capsule;
583 :
584 30 : c->c_nestlevel--;
585 30 : compiler_unit_free(c->u);
586 : /* Restore c->u to the parent unit. */
587 30 : n = PyList_GET_SIZE(c->c_stack) - 1;
588 30 : if (n >= 0) {
589 27 : capsule = PyList_GET_ITEM(c->c_stack, n);
590 27 : c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
591 : assert(c->u);
592 : /* we are deleting from a list so this really shouldn't fail */
593 27 : if (PySequence_DelItem(c->c_stack, n) < 0)
594 0 : Py_FatalError("compiler_exit_scope()");
595 27 : compiler_unit_check(c->u);
596 : }
597 : else
598 3 : c->u = NULL;
599 :
600 30 : }
601 :
602 : static PyObject *
603 27 : compiler_scope_qualname(struct compiler *c)
604 : {
605 : Py_ssize_t stack_size, i;
606 : _Py_static_string(dot, ".");
607 : _Py_static_string(locals, "<locals>");
608 : struct compiler_unit *u;
609 : PyObject *capsule, *name, *seq, *dot_str, *locals_str;
610 :
611 27 : u = c->u;
612 27 : if (u->u_qualname != NULL) {
613 0 : Py_INCREF(u->u_qualname);
614 0 : return u->u_qualname;
615 : }
616 :
617 27 : seq = PyList_New(0);
618 27 : if (seq == NULL)
619 0 : return NULL;
620 :
621 27 : stack_size = PyList_GET_SIZE(c->c_stack);
622 78 : for (i = 0; i < stack_size; i++) {
623 51 : capsule = PyList_GET_ITEM(c->c_stack, i);
624 51 : u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
625 : assert(u);
626 51 : if (u->u_scope_type == COMPILER_SCOPE_MODULE)
627 27 : continue;
628 24 : if (PyList_Append(seq, u->u_name))
629 0 : goto _error;
630 24 : if (u->u_scope_type == COMPILER_SCOPE_FUNCTION) {
631 0 : locals_str = _PyUnicode_FromId(&locals);
632 0 : if (locals_str == NULL)
633 0 : goto _error;
634 0 : if (PyList_Append(seq, locals_str))
635 0 : goto _error;
636 : }
637 : }
638 27 : u = c->u;
639 27 : if (PyList_Append(seq, u->u_name))
640 0 : goto _error;
641 27 : dot_str = _PyUnicode_FromId(&dot);
642 27 : if (dot_str == NULL)
643 0 : goto _error;
644 27 : name = PyUnicode_Join(dot_str, seq);
645 27 : Py_DECREF(seq);
646 27 : u->u_qualname = name;
647 27 : Py_XINCREF(name);
648 27 : return name;
649 :
650 : _error:
651 0 : Py_XDECREF(seq);
652 0 : return NULL;
653 : }
654 :
655 : /* Allocate a new block and return a pointer to it.
656 : Returns NULL on error.
657 : */
658 :
659 : static basicblock *
660 99 : compiler_new_block(struct compiler *c)
661 : {
662 : basicblock *b;
663 : struct compiler_unit *u;
664 :
665 99 : u = c->u;
666 99 : b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
667 99 : if (b == NULL) {
668 0 : PyErr_NoMemory();
669 0 : return NULL;
670 : }
671 99 : memset((void *)b, 0, sizeof(basicblock));
672 : /* Extend the singly linked list of blocks with new block. */
673 99 : b->b_list = u->u_blocks;
674 99 : u->u_blocks = b;
675 99 : return b;
676 : }
677 :
678 : static basicblock *
679 30 : compiler_use_new_block(struct compiler *c)
680 : {
681 30 : basicblock *block = compiler_new_block(c);
682 30 : if (block == NULL)
683 0 : return NULL;
684 30 : c->u->u_curblock = block;
685 30 : return block;
686 : }
687 :
688 : static basicblock *
689 6 : compiler_next_block(struct compiler *c)
690 : {
691 6 : basicblock *block = compiler_new_block(c);
692 6 : if (block == NULL)
693 0 : return NULL;
694 6 : c->u->u_curblock->b_next = block;
695 6 : c->u->u_curblock = block;
696 6 : return block;
697 : }
698 :
699 : static basicblock *
700 63 : compiler_use_next_block(struct compiler *c, basicblock *block)
701 : {
702 : assert(block != NULL);
703 63 : c->u->u_curblock->b_next = block;
704 63 : c->u->u_curblock = block;
705 63 : return block;
706 : }
707 :
708 : /* Returns the offset of the next instruction in the current block's
709 : b_instr array. Resizes the b_instr as necessary.
710 : Returns -1 on failure.
711 : */
712 :
713 : static int
714 1133 : compiler_next_instr(struct compiler *c, basicblock *b)
715 : {
716 : assert(b != NULL);
717 1133 : if (b->b_instr == NULL) {
718 93 : b->b_instr = (struct instr *)PyObject_Malloc(
719 : sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
720 93 : if (b->b_instr == NULL) {
721 0 : PyErr_NoMemory();
722 0 : return -1;
723 : }
724 93 : b->b_ialloc = DEFAULT_BLOCK_SIZE;
725 93 : memset((char *)b->b_instr, 0,
726 : sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
727 : }
728 1040 : else if (b->b_iused == b->b_ialloc) {
729 : struct instr *tmp;
730 : size_t oldsize, newsize;
731 32 : oldsize = b->b_ialloc * sizeof(struct instr);
732 32 : newsize = oldsize << 1;
733 :
734 32 : if (oldsize > (PY_SIZE_MAX >> 1)) {
735 0 : PyErr_NoMemory();
736 0 : return -1;
737 : }
738 :
739 32 : if (newsize == 0) {
740 0 : PyErr_NoMemory();
741 0 : return -1;
742 : }
743 32 : b->b_ialloc <<= 1;
744 32 : tmp = (struct instr *)PyObject_Realloc(
745 32 : (void *)b->b_instr, newsize);
746 32 : if (tmp == NULL) {
747 0 : PyErr_NoMemory();
748 0 : return -1;
749 : }
750 32 : b->b_instr = tmp;
751 32 : memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
752 : }
753 1133 : return b->b_iused++;
754 : }
755 :
756 : /* Set the i_lineno member of the instruction at offset off if the
757 : line number for the current expression/statement has not
758 : already been set. If it has been set, the call has no effect.
759 :
760 : The line number is reset in the following cases:
761 : - when entering a new scope
762 : - on each statement
763 : - on each expression that start a new line
764 : - before the "except" clause
765 : - before the "for" and "while" expressions
766 : */
767 :
768 : static void
769 1133 : compiler_set_lineno(struct compiler *c, int off)
770 : {
771 : basicblock *b;
772 1133 : if (c->u->u_lineno_set)
773 2077 : return;
774 189 : c->u->u_lineno_set = 1;
775 189 : b = c->u->u_curblock;
776 189 : b->b_instr[off].i_lineno = c->u->u_lineno;
777 : }
778 :
779 : static int
780 3107 : opcode_stack_effect(int opcode, int oparg)
781 : {
782 3107 : switch (opcode) {
783 : case POP_TOP:
784 112 : return -1;
785 : case ROT_TWO:
786 : case ROT_THREE:
787 1 : return 0;
788 : case DUP_TOP:
789 19 : return 1;
790 : case DUP_TOP_TWO:
791 0 : return 2;
792 :
793 : case UNARY_POSITIVE:
794 : case UNARY_NEGATIVE:
795 : case UNARY_NOT:
796 : case UNARY_INVERT:
797 3 : return 0;
798 :
799 : case SET_ADD:
800 : case LIST_APPEND:
801 0 : return -1;
802 : case MAP_ADD:
803 0 : return -2;
804 :
805 : case BINARY_POWER:
806 : case BINARY_MULTIPLY:
807 : case BINARY_MODULO:
808 : case BINARY_ADD:
809 : case BINARY_SUBTRACT:
810 : case BINARY_SUBSCR:
811 : case BINARY_FLOOR_DIVIDE:
812 : case BINARY_TRUE_DIVIDE:
813 124 : return -1;
814 : case INPLACE_FLOOR_DIVIDE:
815 : case INPLACE_TRUE_DIVIDE:
816 0 : return -1;
817 :
818 : case INPLACE_ADD:
819 : case INPLACE_SUBTRACT:
820 : case INPLACE_MULTIPLY:
821 : case INPLACE_MODULO:
822 1 : return -1;
823 : case STORE_SUBSCR:
824 4 : return -3;
825 : case STORE_MAP:
826 0 : return -2;
827 : case DELETE_SUBSCR:
828 0 : return -2;
829 :
830 : case BINARY_LSHIFT:
831 : case BINARY_RSHIFT:
832 : case BINARY_AND:
833 : case BINARY_XOR:
834 : case BINARY_OR:
835 0 : return -1;
836 : case INPLACE_POWER:
837 0 : return -1;
838 : case GET_ITER:
839 2 : return 0;
840 :
841 : case PRINT_EXPR:
842 0 : return -1;
843 : case LOAD_BUILD_CLASS:
844 3 : return 1;
845 : case INPLACE_LSHIFT:
846 : case INPLACE_RSHIFT:
847 : case INPLACE_AND:
848 : case INPLACE_XOR:
849 : case INPLACE_OR:
850 0 : return -1;
851 : case BREAK_LOOP:
852 0 : return 0;
853 : case SETUP_WITH:
854 0 : return 7;
855 : case WITH_CLEANUP:
856 0 : return -1; /* XXX Sometimes more */
857 : case STORE_LOCALS:
858 3 : return -1;
859 : case RETURN_VALUE:
860 59 : return -1;
861 : case IMPORT_STAR:
862 0 : return -1;
863 : case YIELD_VALUE:
864 0 : return 0;
865 : case YIELD_FROM:
866 0 : return -1;
867 : case POP_BLOCK:
868 63 : return 0;
869 : case POP_EXCEPT:
870 33 : return 0; /* -3 except if bad bytecode */
871 : case END_FINALLY:
872 36 : return -1; /* or -2 or -3 if exception occurred */
873 :
874 : case STORE_NAME:
875 77 : return -1;
876 : case DELETE_NAME:
877 0 : return 0;
878 : case UNPACK_SEQUENCE:
879 27 : return oparg-1;
880 : case UNPACK_EX:
881 0 : return (oparg&0xFF) + (oparg>>8);
882 : case FOR_ITER:
883 2 : return 1; /* or -1, at end of iterator */
884 :
885 : case STORE_ATTR:
886 132 : return -2;
887 : case DELETE_ATTR:
888 0 : return -1;
889 : case STORE_GLOBAL:
890 0 : return -1;
891 : case DELETE_GLOBAL:
892 0 : return 0;
893 : case LOAD_CONST:
894 438 : return 1;
895 : case LOAD_NAME:
896 46 : return 1;
897 : case BUILD_TUPLE:
898 : case BUILD_LIST:
899 : case BUILD_SET:
900 15 : return 1-oparg;
901 : case BUILD_MAP:
902 1 : return 1;
903 : case LOAD_ATTR:
904 258 : return 0;
905 : case COMPARE_OP:
906 124 : return -1;
907 : case IMPORT_NAME:
908 22 : return -1;
909 : case IMPORT_FROM:
910 19 : return 1;
911 :
912 : case JUMP_FORWARD:
913 : case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
914 : case JUMP_IF_FALSE_OR_POP: /* "" */
915 : case JUMP_ABSOLUTE:
916 182 : return 0;
917 :
918 : case POP_JUMP_IF_FALSE:
919 : case POP_JUMP_IF_TRUE:
920 109 : return -1;
921 :
922 : case LOAD_GLOBAL:
923 222 : return 1;
924 :
925 : case CONTINUE_LOOP:
926 0 : return 0;
927 : case SETUP_LOOP:
928 9 : return 0;
929 : case SETUP_EXCEPT:
930 : case SETUP_FINALLY:
931 39 : return 6; /* can push 3 values for the new exception
932 : + 3 others for the previous exception state */
933 :
934 : case LOAD_FAST:
935 561 : return 1;
936 : case STORE_FAST:
937 81 : return -1;
938 : case DELETE_FAST:
939 18 : return 0;
940 :
941 : case RAISE_VARARGS:
942 4 : return -oparg;
943 : #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
944 : case CALL_FUNCTION:
945 211 : return -NARGS(oparg);
946 : case CALL_FUNCTION_VAR:
947 : case CALL_FUNCTION_KW:
948 0 : return -NARGS(oparg)-1;
949 : case CALL_FUNCTION_VAR_KW:
950 0 : return -NARGS(oparg)-2;
951 : case MAKE_FUNCTION:
952 27 : return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
953 : case MAKE_CLOSURE:
954 0 : return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
955 : #undef NARGS
956 : case BUILD_SLICE:
957 20 : if (oparg == 3)
958 0 : return -2;
959 : else
960 20 : return -1;
961 :
962 : case LOAD_CLOSURE:
963 0 : return 1;
964 : case LOAD_DEREF:
965 0 : return 1;
966 : case STORE_DEREF:
967 0 : return -1;
968 : case DELETE_DEREF:
969 0 : return 0;
970 : default:
971 0 : fprintf(stderr, "opcode = %d\n", opcode);
972 0 : Py_FatalError("opcode_stack_effect()");
973 :
974 : }
975 0 : return 0; /* not reachable */
976 : }
977 :
978 : /* Add an opcode with no argument.
979 : Returns 0 on failure, 1 on success.
980 : */
981 :
982 : static int
983 153 : compiler_addop(struct compiler *c, int opcode)
984 : {
985 : basicblock *b;
986 : struct instr *i;
987 : int off;
988 153 : off = compiler_next_instr(c, c->u->u_curblock);
989 153 : if (off < 0)
990 0 : return 0;
991 153 : b = c->u->u_curblock;
992 153 : i = &b->b_instr[off];
993 153 : i->i_opcode = opcode;
994 153 : i->i_hasarg = 0;
995 153 : if (opcode == RETURN_VALUE)
996 35 : b->b_return = 1;
997 153 : compiler_set_lineno(c, off);
998 153 : return 1;
999 : }
1000 :
1001 : static int
1002 775 : compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1003 : {
1004 : PyObject *t, *v;
1005 : Py_ssize_t arg;
1006 : double d;
1007 :
1008 : /* necessary to make sure types aren't coerced (e.g., int and long) */
1009 : /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1010 775 : if (PyFloat_Check(o)) {
1011 0 : d = PyFloat_AS_DOUBLE(o);
1012 : /* all we need is to make the tuple different in either the 0.0
1013 : * or -0.0 case from all others, just to avoid the "coercion".
1014 : */
1015 0 : if (d == 0.0 && copysign(1.0, d) < 0.0)
1016 0 : t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1017 : else
1018 0 : t = PyTuple_Pack(2, o, o->ob_type);
1019 : }
1020 775 : else if (PyComplex_Check(o)) {
1021 : Py_complex z;
1022 : int real_negzero, imag_negzero;
1023 : /* For the complex case we must make complex(x, 0.)
1024 : different from complex(x, -0.) and complex(0., y)
1025 : different from complex(-0., y), for any x and y.
1026 : All four complex zeros must be distinguished.*/
1027 0 : z = PyComplex_AsCComplex(o);
1028 0 : real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1029 0 : imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1030 0 : if (real_negzero && imag_negzero) {
1031 0 : t = PyTuple_Pack(5, o, o->ob_type,
1032 : Py_None, Py_None, Py_None);
1033 : }
1034 0 : else if (imag_negzero) {
1035 0 : t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
1036 : }
1037 0 : else if (real_negzero) {
1038 0 : t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1039 : }
1040 : else {
1041 0 : t = PyTuple_Pack(2, o, o->ob_type);
1042 : }
1043 : }
1044 : else {
1045 775 : t = PyTuple_Pack(2, o, o->ob_type);
1046 : }
1047 775 : if (t == NULL)
1048 0 : return -1;
1049 :
1050 775 : v = PyDict_GetItem(dict, t);
1051 775 : if (!v) {
1052 413 : if (PyErr_Occurred())
1053 0 : return -1;
1054 413 : arg = PyDict_Size(dict);
1055 413 : v = PyLong_FromLong(arg);
1056 413 : if (!v) {
1057 0 : Py_DECREF(t);
1058 0 : return -1;
1059 : }
1060 413 : if (PyDict_SetItem(dict, t, v) < 0) {
1061 0 : Py_DECREF(t);
1062 0 : Py_DECREF(v);
1063 0 : return -1;
1064 : }
1065 413 : Py_DECREF(v);
1066 : }
1067 : else
1068 362 : arg = PyLong_AsLong(v);
1069 775 : Py_DECREF(t);
1070 775 : return arg;
1071 : }
1072 :
1073 : static int
1074 416 : compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1075 : PyObject *o)
1076 : {
1077 416 : int arg = compiler_add_o(c, dict, o);
1078 416 : if (arg < 0)
1079 0 : return 0;
1080 416 : return compiler_addop_i(c, opcode, arg);
1081 : }
1082 :
1083 : static int
1084 149 : compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1085 : PyObject *o)
1086 : {
1087 : int arg;
1088 149 : PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1089 149 : if (!mangled)
1090 0 : return 0;
1091 149 : arg = compiler_add_o(c, dict, mangled);
1092 149 : Py_DECREF(mangled);
1093 149 : if (arg < 0)
1094 0 : return 0;
1095 149 : return compiler_addop_i(c, opcode, arg);
1096 : }
1097 :
1098 : /* Add an opcode with an integer argument.
1099 : Returns 0 on failure, 1 on success.
1100 : */
1101 :
1102 : static int
1103 916 : compiler_addop_i(struct compiler *c, int opcode, int oparg)
1104 : {
1105 : struct instr *i;
1106 : int off;
1107 916 : off = compiler_next_instr(c, c->u->u_curblock);
1108 916 : if (off < 0)
1109 0 : return 0;
1110 916 : i = &c->u->u_curblock->b_instr[off];
1111 916 : i->i_opcode = opcode;
1112 916 : i->i_oparg = oparg;
1113 916 : i->i_hasarg = 1;
1114 916 : compiler_set_lineno(c, off);
1115 916 : return 1;
1116 : }
1117 :
1118 : static int
1119 64 : compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1120 : {
1121 : struct instr *i;
1122 : int off;
1123 :
1124 : assert(b != NULL);
1125 64 : off = compiler_next_instr(c, c->u->u_curblock);
1126 64 : if (off < 0)
1127 0 : return 0;
1128 64 : i = &c->u->u_curblock->b_instr[off];
1129 64 : i->i_opcode = opcode;
1130 64 : i->i_target = b;
1131 64 : i->i_hasarg = 1;
1132 64 : if (absolute)
1133 27 : i->i_jabs = 1;
1134 : else
1135 37 : i->i_jrel = 1;
1136 64 : compiler_set_lineno(c, off);
1137 64 : return 1;
1138 : }
1139 :
1140 : /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1141 : like to find better names.) NEW_BLOCK() creates a new block and sets
1142 : it as the current block. NEXT_BLOCK() also creates an implicit jump
1143 : from the current block to the new block.
1144 : */
1145 :
1146 : /* The returns inside these macros make it impossible to decref objects
1147 : created in the local function. Local objects should use the arena.
1148 : */
1149 :
1150 :
1151 : #define NEW_BLOCK(C) { \
1152 : if (compiler_use_new_block((C)) == NULL) \
1153 : return 0; \
1154 : }
1155 :
1156 : #define NEXT_BLOCK(C) { \
1157 : if (compiler_next_block((C)) == NULL) \
1158 : return 0; \
1159 : }
1160 :
1161 : #define ADDOP(C, OP) { \
1162 : if (!compiler_addop((C), (OP))) \
1163 : return 0; \
1164 : }
1165 :
1166 : #define ADDOP_IN_SCOPE(C, OP) { \
1167 : if (!compiler_addop((C), (OP))) { \
1168 : compiler_exit_scope(c); \
1169 : return 0; \
1170 : } \
1171 : }
1172 :
1173 : #define ADDOP_O(C, OP, O, TYPE) { \
1174 : if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1175 : return 0; \
1176 : }
1177 :
1178 : #define ADDOP_NAME(C, OP, O, TYPE) { \
1179 : if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1180 : return 0; \
1181 : }
1182 :
1183 : #define ADDOP_I(C, OP, O) { \
1184 : if (!compiler_addop_i((C), (OP), (O))) \
1185 : return 0; \
1186 : }
1187 :
1188 : #define ADDOP_JABS(C, OP, O) { \
1189 : if (!compiler_addop_j((C), (OP), (O), 1)) \
1190 : return 0; \
1191 : }
1192 :
1193 : #define ADDOP_JREL(C, OP, O) { \
1194 : if (!compiler_addop_j((C), (OP), (O), 0)) \
1195 : return 0; \
1196 : }
1197 :
1198 : /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1199 : the ASDL name to synthesize the name of the C type and the visit function.
1200 : */
1201 :
1202 : #define VISIT(C, TYPE, V) {\
1203 : if (!compiler_visit_ ## TYPE((C), (V))) \
1204 : return 0; \
1205 : }
1206 :
1207 : #define VISIT_IN_SCOPE(C, TYPE, V) {\
1208 : if (!compiler_visit_ ## TYPE((C), (V))) { \
1209 : compiler_exit_scope(c); \
1210 : return 0; \
1211 : } \
1212 : }
1213 :
1214 : #define VISIT_SLICE(C, V, CTX) {\
1215 : if (!compiler_visit_slice((C), (V), (CTX))) \
1216 : return 0; \
1217 : }
1218 :
1219 : #define VISIT_SEQ(C, TYPE, SEQ) { \
1220 : int _i; \
1221 : asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1222 : for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1223 : TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1224 : if (!compiler_visit_ ## TYPE((C), elt)) \
1225 : return 0; \
1226 : } \
1227 : }
1228 :
1229 : #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1230 : int _i; \
1231 : asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1232 : for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1233 : TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1234 : if (!compiler_visit_ ## TYPE((C), elt)) { \
1235 : compiler_exit_scope(c); \
1236 : return 0; \
1237 : } \
1238 : } \
1239 : }
1240 :
1241 : static int
1242 30 : compiler_isdocstring(stmt_ty s)
1243 : {
1244 30 : if (s->kind != Expr_kind)
1245 16 : return 0;
1246 14 : return s->v.Expr.value->kind == Str_kind;
1247 : }
1248 :
1249 : /* Compile a sequence of statements, checking for a docstring. */
1250 :
1251 : static int
1252 6 : compiler_body(struct compiler *c, asdl_seq *stmts)
1253 : {
1254 6 : int i = 0;
1255 : stmt_ty st;
1256 :
1257 6 : if (!asdl_seq_LEN(stmts))
1258 0 : return 1;
1259 6 : st = (stmt_ty)asdl_seq_GET(stmts, 0);
1260 6 : if (compiler_isdocstring(st) && c->c_optimize < 2) {
1261 : /* don't generate docstrings if -OO */
1262 2 : i = 1;
1263 2 : VISIT(c, expr, st->v.Expr.value);
1264 2 : if (!compiler_nameop(c, __doc__, Store))
1265 0 : return 0;
1266 : }
1267 67 : for (; i < asdl_seq_LEN(stmts); i++)
1268 61 : VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1269 6 : return 1;
1270 : }
1271 :
1272 : static PyCodeObject *
1273 3 : compiler_mod(struct compiler *c, mod_ty mod)
1274 : {
1275 : PyCodeObject *co;
1276 3 : int addNone = 1;
1277 : static PyObject *module;
1278 3 : if (!module) {
1279 1 : module = PyUnicode_InternFromString("<module>");
1280 1 : if (!module)
1281 0 : return NULL;
1282 : }
1283 : /* Use 0 for firstlineno initially, will fixup in assemble(). */
1284 3 : if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1285 0 : return NULL;
1286 3 : switch (mod->kind) {
1287 : case Module_kind:
1288 3 : if (!compiler_body(c, mod->v.Module.body)) {
1289 0 : compiler_exit_scope(c);
1290 0 : return 0;
1291 : }
1292 3 : break;
1293 : case Interactive_kind:
1294 0 : c->c_interactive = 1;
1295 0 : VISIT_SEQ_IN_SCOPE(c, stmt,
1296 : mod->v.Interactive.body);
1297 0 : break;
1298 : case Expression_kind:
1299 0 : VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1300 0 : addNone = 0;
1301 0 : break;
1302 : case Suite_kind:
1303 0 : PyErr_SetString(PyExc_SystemError,
1304 : "suite should not be possible");
1305 0 : return 0;
1306 : default:
1307 0 : PyErr_Format(PyExc_SystemError,
1308 : "module kind %d should not be possible",
1309 0 : mod->kind);
1310 0 : return 0;
1311 : }
1312 3 : co = assemble(c, addNone);
1313 3 : compiler_exit_scope(c);
1314 3 : return co;
1315 : }
1316 :
1317 : /* The test for LOCAL must come before the test for FREE in order to
1318 : handle classes where name is both local and free. The local var is
1319 : a method and the free var is a free var referenced within a method.
1320 : */
1321 :
1322 : static int
1323 0 : get_ref_type(struct compiler *c, PyObject *name)
1324 : {
1325 0 : int scope = PyST_GetScope(c->u->u_ste, name);
1326 0 : if (scope == 0) {
1327 : char buf[350];
1328 0 : PyOS_snprintf(buf, sizeof(buf),
1329 : "unknown scope for %.100s in %.100s(%s) in %s\n"
1330 : "symbols: %s\nlocals: %s\nglobals: %s",
1331 0 : PyBytes_AS_STRING(name),
1332 0 : PyBytes_AS_STRING(c->u->u_name),
1333 0 : PyObject_REPR(c->u->u_ste->ste_id),
1334 : c->c_filename,
1335 0 : PyObject_REPR(c->u->u_ste->ste_symbols),
1336 0 : PyObject_REPR(c->u->u_varnames),
1337 0 : PyObject_REPR(c->u->u_names)
1338 : );
1339 0 : Py_FatalError(buf);
1340 : }
1341 :
1342 0 : return scope;
1343 : }
1344 :
1345 : static int
1346 3 : compiler_lookup_arg(PyObject *dict, PyObject *name)
1347 : {
1348 : PyObject *k, *v;
1349 3 : k = PyTuple_Pack(2, name, name->ob_type);
1350 3 : if (k == NULL)
1351 0 : return -1;
1352 3 : v = PyDict_GetItem(dict, k);
1353 3 : Py_DECREF(k);
1354 3 : if (v == NULL)
1355 3 : return -1;
1356 0 : return PyLong_AS_LONG(v);
1357 : }
1358 :
1359 : static int
1360 27 : compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject *qualname)
1361 : {
1362 27 : int i, free = PyCode_GetNumFree(co);
1363 27 : if (qualname == NULL)
1364 3 : qualname = co->co_name;
1365 :
1366 27 : if (free == 0) {
1367 27 : ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1368 27 : ADDOP_O(c, LOAD_CONST, qualname, consts);
1369 27 : ADDOP_I(c, MAKE_FUNCTION, args);
1370 27 : return 1;
1371 : }
1372 0 : for (i = 0; i < free; ++i) {
1373 : /* Bypass com_addop_varname because it will generate
1374 : LOAD_DEREF but LOAD_CLOSURE is needed.
1375 : */
1376 0 : PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1377 : int arg, reftype;
1378 :
1379 : /* Special case: If a class contains a method with a
1380 : free variable that has the same name as a method,
1381 : the name will be considered free *and* local in the
1382 : class. It should be handled by the closure, as
1383 : well as by the normal name loookup logic.
1384 : */
1385 0 : reftype = get_ref_type(c, name);
1386 0 : if (reftype == CELL)
1387 0 : arg = compiler_lookup_arg(c->u->u_cellvars, name);
1388 : else /* (reftype == FREE) */
1389 0 : arg = compiler_lookup_arg(c->u->u_freevars, name);
1390 0 : if (arg == -1) {
1391 0 : fprintf(stderr,
1392 : "lookup %s in %s %d %d\n"
1393 : "freevars of %s: %s\n",
1394 : PyObject_REPR(name),
1395 0 : PyBytes_AS_STRING(c->u->u_name),
1396 : reftype, arg,
1397 : _PyUnicode_AsString(co->co_name),
1398 : PyObject_REPR(co->co_freevars));
1399 0 : Py_FatalError("compiler_make_closure()");
1400 : }
1401 0 : ADDOP_I(c, LOAD_CLOSURE, arg);
1402 : }
1403 0 : ADDOP_I(c, BUILD_TUPLE, free);
1404 0 : ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1405 0 : ADDOP_O(c, LOAD_CONST, qualname, consts);
1406 0 : ADDOP_I(c, MAKE_CLOSURE, args);
1407 0 : return 1;
1408 : }
1409 :
1410 : static int
1411 27 : compiler_decorators(struct compiler *c, asdl_seq* decos)
1412 : {
1413 : int i;
1414 :
1415 27 : if (!decos)
1416 25 : return 1;
1417 :
1418 4 : for (i = 0; i < asdl_seq_LEN(decos); i++) {
1419 2 : VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1420 : }
1421 2 : return 1;
1422 : }
1423 :
1424 : static int
1425 0 : compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1426 : asdl_seq *kw_defaults)
1427 : {
1428 0 : int i, default_count = 0;
1429 0 : for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1430 0 : arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1431 0 : expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1432 0 : if (default_) {
1433 0 : PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1434 0 : if (!mangled)
1435 0 : return -1;
1436 0 : ADDOP_O(c, LOAD_CONST, mangled, consts);
1437 0 : Py_DECREF(mangled);
1438 0 : if (!compiler_visit_expr(c, default_)) {
1439 0 : return -1;
1440 : }
1441 0 : default_count++;
1442 : }
1443 : }
1444 0 : return default_count;
1445 : }
1446 :
1447 : static int
1448 75 : compiler_visit_argannotation(struct compiler *c, identifier id,
1449 : expr_ty annotation, PyObject *names)
1450 : {
1451 75 : if (annotation) {
1452 0 : VISIT(c, expr, annotation);
1453 0 : if (PyList_Append(names, id))
1454 0 : return -1;
1455 : }
1456 75 : return 0;
1457 : }
1458 :
1459 : static int
1460 48 : compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1461 : PyObject *names)
1462 : {
1463 : int i, error;
1464 99 : for (i = 0; i < asdl_seq_LEN(args); i++) {
1465 51 : arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1466 51 : error = compiler_visit_argannotation(
1467 : c,
1468 : arg->arg,
1469 : arg->annotation,
1470 : names);
1471 51 : if (error)
1472 0 : return error;
1473 : }
1474 48 : return 0;
1475 : }
1476 :
1477 : static int
1478 24 : compiler_visit_annotations(struct compiler *c, arguments_ty args,
1479 : expr_ty returns)
1480 : {
1481 : /* Push arg annotations and a list of the argument names. Return the #
1482 : of items pushed. The expressions are evaluated out-of-order wrt the
1483 : source code.
1484 :
1485 : More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1486 : */
1487 : static identifier return_str;
1488 : PyObject *names;
1489 : int len;
1490 24 : names = PyList_New(0);
1491 24 : if (!names)
1492 0 : return -1;
1493 :
1494 24 : if (compiler_visit_argannotations(c, args->args, names))
1495 0 : goto error;
1496 24 : if (args->varargannotation &&
1497 0 : compiler_visit_argannotation(c, args->vararg,
1498 : args->varargannotation, names))
1499 0 : goto error;
1500 24 : if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1501 0 : goto error;
1502 24 : if (args->kwargannotation &&
1503 0 : compiler_visit_argannotation(c, args->kwarg,
1504 : args->kwargannotation, names))
1505 0 : goto error;
1506 :
1507 24 : if (!return_str) {
1508 1 : return_str = PyUnicode_InternFromString("return");
1509 1 : if (!return_str)
1510 0 : goto error;
1511 : }
1512 24 : if (compiler_visit_argannotation(c, return_str, returns, names)) {
1513 0 : goto error;
1514 : }
1515 :
1516 24 : len = PyList_GET_SIZE(names);
1517 24 : if (len > 65534) {
1518 : /* len must fit in 16 bits, and len is incremented below */
1519 0 : PyErr_SetString(PyExc_SyntaxError,
1520 : "too many annotations");
1521 0 : goto error;
1522 : }
1523 24 : if (len) {
1524 : /* convert names to a tuple and place on stack */
1525 : PyObject *elt;
1526 : int i;
1527 0 : PyObject *s = PyTuple_New(len);
1528 0 : if (!s)
1529 0 : goto error;
1530 0 : for (i = 0; i < len; i++) {
1531 0 : elt = PyList_GET_ITEM(names, i);
1532 0 : Py_INCREF(elt);
1533 0 : PyTuple_SET_ITEM(s, i, elt);
1534 : }
1535 0 : ADDOP_O(c, LOAD_CONST, s, consts);
1536 0 : Py_DECREF(s);
1537 0 : len++; /* include the just-pushed tuple */
1538 : }
1539 24 : Py_DECREF(names);
1540 24 : return len;
1541 :
1542 : error:
1543 0 : Py_DECREF(names);
1544 0 : return -1;
1545 : }
1546 :
1547 : static int
1548 24 : compiler_function(struct compiler *c, stmt_ty s)
1549 : {
1550 : PyCodeObject *co;
1551 24 : PyObject *qualname, *first_const = Py_None;
1552 24 : arguments_ty args = s->v.FunctionDef.args;
1553 24 : expr_ty returns = s->v.FunctionDef.returns;
1554 24 : asdl_seq* decos = s->v.FunctionDef.decorator_list;
1555 : stmt_ty st;
1556 24 : int i, n, docstring, kw_default_count = 0, arglength;
1557 : int num_annotations;
1558 :
1559 : assert(s->kind == FunctionDef_kind);
1560 :
1561 24 : if (!compiler_decorators(c, decos))
1562 0 : return 0;
1563 24 : if (args->kwonlyargs) {
1564 0 : int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1565 : args->kw_defaults);
1566 0 : if (res < 0)
1567 0 : return 0;
1568 0 : kw_default_count = res;
1569 : }
1570 24 : if (args->defaults)
1571 2 : VISIT_SEQ(c, expr, args->defaults);
1572 24 : num_annotations = compiler_visit_annotations(c, args, returns);
1573 24 : if (num_annotations < 0)
1574 0 : return 0;
1575 : assert((num_annotations & 0xFFFF) == num_annotations);
1576 :
1577 24 : if (!compiler_enter_scope(c, s->v.FunctionDef.name,
1578 : COMPILER_SCOPE_FUNCTION, (void *)s,
1579 : s->lineno))
1580 0 : return 0;
1581 :
1582 24 : st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1583 24 : docstring = compiler_isdocstring(st);
1584 24 : if (docstring && c->c_optimize < 2)
1585 12 : first_const = st->v.Expr.value->v.Str.s;
1586 24 : if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1587 0 : compiler_exit_scope(c);
1588 0 : return 0;
1589 : }
1590 :
1591 24 : c->u->u_argcount = asdl_seq_LEN(args->args);
1592 24 : c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1593 24 : n = asdl_seq_LEN(s->v.FunctionDef.body);
1594 : /* if there was a docstring, we need to skip the first statement */
1595 83 : for (i = docstring; i < n; i++) {
1596 59 : st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1597 59 : VISIT_IN_SCOPE(c, stmt, st);
1598 : }
1599 24 : co = assemble(c, 1);
1600 24 : qualname = compiler_scope_qualname(c);
1601 24 : compiler_exit_scope(c);
1602 24 : if (qualname == NULL || co == NULL) {
1603 0 : Py_XDECREF(qualname);
1604 0 : Py_XDECREF(co);
1605 0 : return 0;
1606 : }
1607 :
1608 24 : arglength = asdl_seq_LEN(args->defaults);
1609 24 : arglength |= kw_default_count << 8;
1610 24 : arglength |= num_annotations << 16;
1611 24 : compiler_make_closure(c, co, arglength, qualname);
1612 24 : Py_DECREF(qualname);
1613 24 : Py_DECREF(co);
1614 :
1615 : /* decorators */
1616 26 : for (i = 0; i < asdl_seq_LEN(decos); i++) {
1617 2 : ADDOP_I(c, CALL_FUNCTION, 1);
1618 : }
1619 :
1620 24 : return compiler_nameop(c, s->v.FunctionDef.name, Store);
1621 : }
1622 :
1623 : static int
1624 3 : compiler_class(struct compiler *c, stmt_ty s)
1625 : {
1626 : PyCodeObject *co;
1627 : PyObject *str;
1628 : int i;
1629 3 : asdl_seq* decos = s->v.ClassDef.decorator_list;
1630 :
1631 3 : if (!compiler_decorators(c, decos))
1632 0 : return 0;
1633 :
1634 : /* ultimately generate code for:
1635 : <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1636 : where:
1637 : <func> is a function/closure created from the class body;
1638 : it has a single argument (__locals__) where the dict
1639 : (or MutableSequence) representing the locals is passed
1640 : <name> is the class name
1641 : <bases> is the positional arguments and *varargs argument
1642 : <keywords> is the keyword arguments and **kwds argument
1643 : This borrows from compiler_call.
1644 : */
1645 :
1646 : /* 1. compile the class body into a code object */
1647 3 : if (!compiler_enter_scope(c, s->v.ClassDef.name,
1648 : COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1649 0 : return 0;
1650 : /* this block represents what we do in the new scope */
1651 : {
1652 : /* use the class name for name mangling */
1653 3 : Py_INCREF(s->v.ClassDef.name);
1654 3 : Py_XDECREF(c->u->u_private);
1655 3 : c->u->u_private = s->v.ClassDef.name;
1656 : /* force it to have one mandatory argument */
1657 3 : c->u->u_argcount = 1;
1658 : /* load the first argument (__locals__) ... */
1659 3 : ADDOP_I(c, LOAD_FAST, 0);
1660 : /* ... and store it into f_locals */
1661 3 : ADDOP_IN_SCOPE(c, STORE_LOCALS);
1662 : /* load (global) __name__ ... */
1663 3 : str = PyUnicode_InternFromString("__name__");
1664 3 : if (!str || !compiler_nameop(c, str, Load)) {
1665 0 : Py_XDECREF(str);
1666 0 : compiler_exit_scope(c);
1667 0 : return 0;
1668 : }
1669 3 : Py_DECREF(str);
1670 : /* ... and store it as __module__ */
1671 3 : str = PyUnicode_InternFromString("__module__");
1672 3 : if (!str || !compiler_nameop(c, str, Store)) {
1673 0 : Py_XDECREF(str);
1674 0 : compiler_exit_scope(c);
1675 0 : return 0;
1676 : }
1677 3 : Py_DECREF(str);
1678 : /* store the __qualname__ */
1679 3 : str = compiler_scope_qualname(c);
1680 3 : if (!str) {
1681 0 : compiler_exit_scope(c);
1682 0 : return 0;
1683 : }
1684 3 : ADDOP_O(c, LOAD_CONST, str, consts);
1685 3 : Py_DECREF(str);
1686 3 : str = PyUnicode_InternFromString("__qualname__");
1687 3 : if (!str || !compiler_nameop(c, str, Store)) {
1688 0 : Py_XDECREF(str);
1689 0 : compiler_exit_scope(c);
1690 0 : return 0;
1691 : }
1692 3 : Py_DECREF(str);
1693 : /* compile the body proper */
1694 3 : if (!compiler_body(c, s->v.ClassDef.body)) {
1695 0 : compiler_exit_scope(c);
1696 0 : return 0;
1697 : }
1698 : /* return the (empty) __class__ cell */
1699 3 : str = PyUnicode_InternFromString("__class__");
1700 3 : if (str == NULL) {
1701 0 : compiler_exit_scope(c);
1702 0 : return 0;
1703 : }
1704 3 : i = compiler_lookup_arg(c->u->u_cellvars, str);
1705 3 : Py_DECREF(str);
1706 3 : if (i == -1) {
1707 : /* This happens when nobody references the cell */
1708 3 : PyErr_Clear();
1709 : /* Return None */
1710 3 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
1711 : }
1712 : else {
1713 : /* Return the cell where to store __class__ */
1714 0 : ADDOP_I(c, LOAD_CLOSURE, i);
1715 : }
1716 3 : ADDOP_IN_SCOPE(c, RETURN_VALUE);
1717 : /* create the code object */
1718 3 : co = assemble(c, 1);
1719 : }
1720 : /* leave the new scope */
1721 3 : compiler_exit_scope(c);
1722 3 : if (co == NULL)
1723 0 : return 0;
1724 :
1725 : /* 2. load the 'build_class' function */
1726 3 : ADDOP(c, LOAD_BUILD_CLASS);
1727 :
1728 : /* 3. load a function (or closure) made from the code object */
1729 3 : compiler_make_closure(c, co, 0, NULL);
1730 3 : Py_DECREF(co);
1731 :
1732 : /* 4. load class name */
1733 3 : ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1734 :
1735 : /* 5. generate the rest of the code for the call */
1736 3 : if (!compiler_call_helper(c, 2,
1737 : s->v.ClassDef.bases,
1738 : s->v.ClassDef.keywords,
1739 : s->v.ClassDef.starargs,
1740 : s->v.ClassDef.kwargs))
1741 0 : return 0;
1742 :
1743 : /* 6. apply decorators */
1744 3 : for (i = 0; i < asdl_seq_LEN(decos); i++) {
1745 0 : ADDOP_I(c, CALL_FUNCTION, 1);
1746 : }
1747 :
1748 : /* 7. store into <name> */
1749 3 : if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1750 0 : return 0;
1751 3 : return 1;
1752 : }
1753 :
1754 : static int
1755 0 : compiler_ifexp(struct compiler *c, expr_ty e)
1756 : {
1757 : basicblock *end, *next;
1758 :
1759 : assert(e->kind == IfExp_kind);
1760 0 : end = compiler_new_block(c);
1761 0 : if (end == NULL)
1762 0 : return 0;
1763 0 : next = compiler_new_block(c);
1764 0 : if (next == NULL)
1765 0 : return 0;
1766 0 : VISIT(c, expr, e->v.IfExp.test);
1767 0 : ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1768 0 : VISIT(c, expr, e->v.IfExp.body);
1769 0 : ADDOP_JREL(c, JUMP_FORWARD, end);
1770 0 : compiler_use_next_block(c, next);
1771 0 : VISIT(c, expr, e->v.IfExp.orelse);
1772 0 : compiler_use_next_block(c, end);
1773 0 : return 1;
1774 : }
1775 :
1776 : static int
1777 0 : compiler_lambda(struct compiler *c, expr_ty e)
1778 : {
1779 : PyCodeObject *co;
1780 : PyObject *qualname;
1781 : static identifier name;
1782 0 : int kw_default_count = 0, arglength;
1783 0 : arguments_ty args = e->v.Lambda.args;
1784 : assert(e->kind == Lambda_kind);
1785 :
1786 0 : if (!name) {
1787 0 : name = PyUnicode_InternFromString("<lambda>");
1788 0 : if (!name)
1789 0 : return 0;
1790 : }
1791 :
1792 0 : if (args->kwonlyargs) {
1793 0 : int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1794 : args->kw_defaults);
1795 0 : if (res < 0) return 0;
1796 0 : kw_default_count = res;
1797 : }
1798 0 : if (args->defaults)
1799 0 : VISIT_SEQ(c, expr, args->defaults);
1800 0 : if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION,
1801 : (void *)e, e->lineno))
1802 0 : return 0;
1803 :
1804 : /* Make None the first constant, so the lambda can't have a
1805 : docstring. */
1806 0 : if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1807 0 : return 0;
1808 :
1809 0 : c->u->u_argcount = asdl_seq_LEN(args->args);
1810 0 : c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1811 0 : VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1812 0 : if (c->u->u_ste->ste_generator) {
1813 0 : ADDOP_IN_SCOPE(c, POP_TOP);
1814 : }
1815 : else {
1816 0 : ADDOP_IN_SCOPE(c, RETURN_VALUE);
1817 : }
1818 0 : co = assemble(c, 1);
1819 0 : qualname = compiler_scope_qualname(c);
1820 0 : compiler_exit_scope(c);
1821 0 : if (qualname == NULL || co == NULL)
1822 0 : return 0;
1823 :
1824 0 : arglength = asdl_seq_LEN(args->defaults);
1825 0 : arglength |= kw_default_count << 8;
1826 0 : compiler_make_closure(c, co, arglength, qualname);
1827 0 : Py_DECREF(qualname);
1828 0 : Py_DECREF(co);
1829 :
1830 0 : return 1;
1831 : }
1832 :
1833 : static int
1834 14 : compiler_if(struct compiler *c, stmt_ty s)
1835 : {
1836 : basicblock *end, *next;
1837 : int constant;
1838 : assert(s->kind == If_kind);
1839 14 : end = compiler_new_block(c);
1840 14 : if (end == NULL)
1841 0 : return 0;
1842 :
1843 14 : constant = expr_constant(c, s->v.If.test);
1844 : /* constant = 0: "if 0"
1845 : * constant = 1: "if 1", "if 2", ...
1846 : * constant = -1: rest */
1847 14 : if (constant == 0) {
1848 0 : if (s->v.If.orelse)
1849 0 : VISIT_SEQ(c, stmt, s->v.If.orelse);
1850 14 : } else if (constant == 1) {
1851 0 : VISIT_SEQ(c, stmt, s->v.If.body);
1852 : } else {
1853 14 : if (s->v.If.orelse) {
1854 2 : next = compiler_new_block(c);
1855 2 : if (next == NULL)
1856 0 : return 0;
1857 : }
1858 : else
1859 12 : next = end;
1860 14 : VISIT(c, expr, s->v.If.test);
1861 14 : ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1862 14 : VISIT_SEQ(c, stmt, s->v.If.body);
1863 14 : ADDOP_JREL(c, JUMP_FORWARD, end);
1864 14 : if (s->v.If.orelse) {
1865 2 : compiler_use_next_block(c, next);
1866 2 : VISIT_SEQ(c, stmt, s->v.If.orelse);
1867 : }
1868 : }
1869 14 : compiler_use_next_block(c, end);
1870 14 : return 1;
1871 : }
1872 :
1873 : static int
1874 2 : compiler_for(struct compiler *c, stmt_ty s)
1875 : {
1876 : basicblock *start, *cleanup, *end;
1877 :
1878 2 : start = compiler_new_block(c);
1879 2 : cleanup = compiler_new_block(c);
1880 2 : end = compiler_new_block(c);
1881 2 : if (start == NULL || end == NULL || cleanup == NULL)
1882 0 : return 0;
1883 2 : ADDOP_JREL(c, SETUP_LOOP, end);
1884 2 : if (!compiler_push_fblock(c, LOOP, start))
1885 0 : return 0;
1886 2 : VISIT(c, expr, s->v.For.iter);
1887 2 : ADDOP(c, GET_ITER);
1888 2 : compiler_use_next_block(c, start);
1889 2 : ADDOP_JREL(c, FOR_ITER, cleanup);
1890 2 : VISIT(c, expr, s->v.For.target);
1891 2 : VISIT_SEQ(c, stmt, s->v.For.body);
1892 2 : ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1893 2 : compiler_use_next_block(c, cleanup);
1894 2 : ADDOP(c, POP_BLOCK);
1895 2 : compiler_pop_fblock(c, LOOP, start);
1896 2 : VISIT_SEQ(c, stmt, s->v.For.orelse);
1897 2 : compiler_use_next_block(c, end);
1898 2 : return 1;
1899 : }
1900 :
1901 : static int
1902 1 : compiler_while(struct compiler *c, stmt_ty s)
1903 : {
1904 1 : basicblock *loop, *orelse, *end, *anchor = NULL;
1905 1 : int constant = expr_constant(c, s->v.While.test);
1906 :
1907 1 : if (constant == 0) {
1908 0 : if (s->v.While.orelse)
1909 0 : VISIT_SEQ(c, stmt, s->v.While.orelse);
1910 0 : return 1;
1911 : }
1912 1 : loop = compiler_new_block(c);
1913 1 : end = compiler_new_block(c);
1914 1 : if (constant == -1) {
1915 1 : anchor = compiler_new_block(c);
1916 1 : if (anchor == NULL)
1917 0 : return 0;
1918 : }
1919 1 : if (loop == NULL || end == NULL)
1920 0 : return 0;
1921 1 : if (s->v.While.orelse) {
1922 0 : orelse = compiler_new_block(c);
1923 0 : if (orelse == NULL)
1924 0 : return 0;
1925 : }
1926 : else
1927 1 : orelse = NULL;
1928 :
1929 1 : ADDOP_JREL(c, SETUP_LOOP, end);
1930 1 : compiler_use_next_block(c, loop);
1931 1 : if (!compiler_push_fblock(c, LOOP, loop))
1932 0 : return 0;
1933 1 : if (constant == -1) {
1934 1 : VISIT(c, expr, s->v.While.test);
1935 1 : ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1936 : }
1937 1 : VISIT_SEQ(c, stmt, s->v.While.body);
1938 1 : ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1939 :
1940 : /* XXX should the two POP instructions be in a separate block
1941 : if there is no else clause ?
1942 : */
1943 :
1944 1 : if (constant == -1) {
1945 1 : compiler_use_next_block(c, anchor);
1946 1 : ADDOP(c, POP_BLOCK);
1947 : }
1948 1 : compiler_pop_fblock(c, LOOP, loop);
1949 1 : if (orelse != NULL) /* what if orelse is just pass? */
1950 0 : VISIT_SEQ(c, stmt, s->v.While.orelse);
1951 1 : compiler_use_next_block(c, end);
1952 :
1953 1 : return 1;
1954 : }
1955 :
1956 : static int
1957 0 : compiler_continue(struct compiler *c)
1958 : {
1959 : static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1960 : static const char IN_FINALLY_ERROR_MSG[] =
1961 : "'continue' not supported inside 'finally' clause";
1962 : int i;
1963 :
1964 0 : if (!c->u->u_nfblocks)
1965 0 : return compiler_error(c, LOOP_ERROR_MSG);
1966 0 : i = c->u->u_nfblocks - 1;
1967 0 : switch (c->u->u_fblock[i].fb_type) {
1968 : case LOOP:
1969 0 : ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1970 0 : break;
1971 : case EXCEPT:
1972 : case FINALLY_TRY:
1973 0 : while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1974 : /* Prevent continue anywhere under a finally
1975 : even if hidden in a sub-try or except. */
1976 0 : if (c->u->u_fblock[i].fb_type == FINALLY_END)
1977 0 : return compiler_error(c, IN_FINALLY_ERROR_MSG);
1978 : }
1979 0 : if (i == -1)
1980 0 : return compiler_error(c, LOOP_ERROR_MSG);
1981 0 : ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1982 0 : break;
1983 : case FINALLY_END:
1984 0 : return compiler_error(c, IN_FINALLY_ERROR_MSG);
1985 : }
1986 :
1987 0 : return 1;
1988 : }
1989 :
1990 : /* Code generated for "try: <body> finally: <finalbody>" is as follows:
1991 :
1992 : SETUP_FINALLY L
1993 : <code for body>
1994 : POP_BLOCK
1995 : LOAD_CONST <None>
1996 : L: <code for finalbody>
1997 : END_FINALLY
1998 :
1999 : The special instructions use the block stack. Each block
2000 : stack entry contains the instruction that created it (here
2001 : SETUP_FINALLY), the level of the value stack at the time the
2002 : block stack entry was created, and a label (here L).
2003 :
2004 : SETUP_FINALLY:
2005 : Pushes the current value stack level and the label
2006 : onto the block stack.
2007 : POP_BLOCK:
2008 : Pops en entry from the block stack, and pops the value
2009 : stack until its level is the same as indicated on the
2010 : block stack. (The label is ignored.)
2011 : END_FINALLY:
2012 : Pops a variable number of entries from the *value* stack
2013 : and re-raises the exception they specify. The number of
2014 : entries popped depends on the (pseudo) exception type.
2015 :
2016 : The block stack is unwound when an exception is raised:
2017 : when a SETUP_FINALLY entry is found, the exception is pushed
2018 : onto the value stack (and the exception condition is cleared),
2019 : and the interpreter jumps to the label gotten from the block
2020 : stack.
2021 : */
2022 :
2023 : static int
2024 0 : compiler_try_finally(struct compiler *c, stmt_ty s)
2025 : {
2026 : basicblock *body, *end;
2027 0 : body = compiler_new_block(c);
2028 0 : end = compiler_new_block(c);
2029 0 : if (body == NULL || end == NULL)
2030 0 : return 0;
2031 :
2032 0 : ADDOP_JREL(c, SETUP_FINALLY, end);
2033 0 : compiler_use_next_block(c, body);
2034 0 : if (!compiler_push_fblock(c, FINALLY_TRY, body))
2035 0 : return 0;
2036 0 : if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2037 0 : if (!compiler_try_except(c, s))
2038 0 : return 0;
2039 : }
2040 : else {
2041 0 : VISIT_SEQ(c, stmt, s->v.Try.body);
2042 : }
2043 0 : ADDOP(c, POP_BLOCK);
2044 0 : compiler_pop_fblock(c, FINALLY_TRY, body);
2045 :
2046 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
2047 0 : compiler_use_next_block(c, end);
2048 0 : if (!compiler_push_fblock(c, FINALLY_END, end))
2049 0 : return 0;
2050 0 : VISIT_SEQ(c, stmt, s->v.Try.finalbody);
2051 0 : ADDOP(c, END_FINALLY);
2052 0 : compiler_pop_fblock(c, FINALLY_END, end);
2053 :
2054 0 : return 1;
2055 : }
2056 :
2057 : /*
2058 : Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
2059 : (The contents of the value stack is shown in [], with the top
2060 : at the right; 'tb' is trace-back info, 'val' the exception's
2061 : associated value, and 'exc' the exception.)
2062 :
2063 : Value stack Label Instruction Argument
2064 : [] SETUP_EXCEPT L1
2065 : [] <code for S>
2066 : [] POP_BLOCK
2067 : [] JUMP_FORWARD L0
2068 :
2069 : [tb, val, exc] L1: DUP )
2070 : [tb, val, exc, exc] <evaluate E1> )
2071 : [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2072 : [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2073 : [tb, val, exc] POP
2074 : [tb, val] <assign to V1> (or POP if no V1)
2075 : [tb] POP
2076 : [] <code for S1>
2077 : JUMP_FORWARD L0
2078 :
2079 : [tb, val, exc] L2: DUP
2080 : .............................etc.......................
2081 :
2082 : [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2083 :
2084 : [] L0: <next statement>
2085 :
2086 : Of course, parts are not generated if Vi or Ei is not present.
2087 : */
2088 : static int
2089 5 : compiler_try_except(struct compiler *c, stmt_ty s)
2090 : {
2091 : basicblock *body, *orelse, *except, *end;
2092 : int i, n;
2093 :
2094 5 : body = compiler_new_block(c);
2095 5 : except = compiler_new_block(c);
2096 5 : orelse = compiler_new_block(c);
2097 5 : end = compiler_new_block(c);
2098 5 : if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2099 0 : return 0;
2100 5 : ADDOP_JREL(c, SETUP_EXCEPT, except);
2101 5 : compiler_use_next_block(c, body);
2102 5 : if (!compiler_push_fblock(c, EXCEPT, body))
2103 0 : return 0;
2104 5 : VISIT_SEQ(c, stmt, s->v.Try.body);
2105 5 : ADDOP(c, POP_BLOCK);
2106 5 : compiler_pop_fblock(c, EXCEPT, body);
2107 5 : ADDOP_JREL(c, JUMP_FORWARD, orelse);
2108 5 : n = asdl_seq_LEN(s->v.Try.handlers);
2109 5 : compiler_use_next_block(c, except);
2110 10 : for (i = 0; i < n; i++) {
2111 5 : excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
2112 : s->v.Try.handlers, i);
2113 5 : if (!handler->v.ExceptHandler.type && i < n-1)
2114 0 : return compiler_error(c, "default 'except:' must be last");
2115 5 : c->u->u_lineno_set = 0;
2116 5 : c->u->u_lineno = handler->lineno;
2117 5 : c->u->u_col_offset = handler->col_offset;
2118 5 : except = compiler_new_block(c);
2119 5 : if (except == NULL)
2120 0 : return 0;
2121 5 : if (handler->v.ExceptHandler.type) {
2122 3 : ADDOP(c, DUP_TOP);
2123 3 : VISIT(c, expr, handler->v.ExceptHandler.type);
2124 3 : ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2125 3 : ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2126 : }
2127 5 : ADDOP(c, POP_TOP);
2128 5 : if (handler->v.ExceptHandler.name) {
2129 : basicblock *cleanup_end, *cleanup_body;
2130 :
2131 3 : cleanup_end = compiler_new_block(c);
2132 3 : cleanup_body = compiler_new_block(c);
2133 3 : if (!(cleanup_end || cleanup_body))
2134 0 : return 0;
2135 :
2136 3 : compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2137 3 : ADDOP(c, POP_TOP);
2138 :
2139 : /*
2140 : try:
2141 : # body
2142 : except type as name:
2143 : try:
2144 : # body
2145 : finally:
2146 : name = None
2147 : del name
2148 : */
2149 :
2150 : /* second try: */
2151 3 : ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2152 3 : compiler_use_next_block(c, cleanup_body);
2153 3 : if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2154 0 : return 0;
2155 :
2156 : /* second # body */
2157 3 : VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2158 3 : ADDOP(c, POP_BLOCK);
2159 3 : ADDOP(c, POP_EXCEPT);
2160 3 : compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2161 :
2162 : /* finally: */
2163 3 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
2164 3 : compiler_use_next_block(c, cleanup_end);
2165 3 : if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2166 0 : return 0;
2167 :
2168 : /* name = None */
2169 3 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
2170 3 : compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2171 :
2172 : /* del name */
2173 3 : compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2174 :
2175 3 : ADDOP(c, END_FINALLY);
2176 3 : compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2177 : }
2178 : else {
2179 : basicblock *cleanup_body;
2180 :
2181 2 : cleanup_body = compiler_new_block(c);
2182 2 : if (!cleanup_body)
2183 0 : return 0;
2184 :
2185 2 : ADDOP(c, POP_TOP);
2186 2 : ADDOP(c, POP_TOP);
2187 2 : compiler_use_next_block(c, cleanup_body);
2188 2 : if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2189 0 : return 0;
2190 2 : VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2191 2 : ADDOP(c, POP_EXCEPT);
2192 2 : compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2193 : }
2194 5 : ADDOP_JREL(c, JUMP_FORWARD, end);
2195 5 : compiler_use_next_block(c, except);
2196 : }
2197 5 : ADDOP(c, END_FINALLY);
2198 5 : compiler_use_next_block(c, orelse);
2199 5 : VISIT_SEQ(c, stmt, s->v.Try.orelse);
2200 5 : compiler_use_next_block(c, end);
2201 5 : return 1;
2202 : }
2203 :
2204 : static int
2205 5 : compiler_try(struct compiler *c, stmt_ty s) {
2206 5 : if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2207 0 : return compiler_try_finally(c, s);
2208 : else
2209 5 : return compiler_try_except(c, s);
2210 : }
2211 :
2212 :
2213 : static int
2214 0 : compiler_import_as(struct compiler *c, identifier name, identifier asname)
2215 : {
2216 : /* The IMPORT_NAME opcode was already generated. This function
2217 : merely needs to bind the result to a name.
2218 :
2219 : If there is a dot in name, we need to split it and emit a
2220 : LOAD_ATTR for each name.
2221 : */
2222 0 : Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2223 : PyUnicode_GET_LENGTH(name), 1);
2224 0 : if (dot == -2)
2225 0 : return -1;
2226 0 : if (dot != -1) {
2227 : /* Consume the base module name to get the first attribute */
2228 0 : Py_ssize_t pos = dot + 1;
2229 0 : while (dot != -1) {
2230 : PyObject *attr;
2231 0 : dot = PyUnicode_FindChar(name, '.', pos,
2232 : PyUnicode_GET_LENGTH(name), 1);
2233 0 : if (dot == -2)
2234 0 : return -1;
2235 0 : attr = PyUnicode_Substring(name, pos,
2236 : (dot != -1) ? dot :
2237 : PyUnicode_GET_LENGTH(name));
2238 0 : if (!attr)
2239 0 : return -1;
2240 0 : ADDOP_O(c, LOAD_ATTR, attr, names);
2241 0 : Py_DECREF(attr);
2242 0 : pos = dot + 1;
2243 : }
2244 : }
2245 0 : return compiler_nameop(c, asname, Store);
2246 : }
2247 :
2248 : static int
2249 4 : compiler_import(struct compiler *c, stmt_ty s)
2250 : {
2251 : /* The Import node stores a module name like a.b.c as a single
2252 : string. This is convenient for all cases except
2253 : import a.b.c as d
2254 : where we need to parse that string to extract the individual
2255 : module names.
2256 : XXX Perhaps change the representation to make this case simpler?
2257 : */
2258 4 : int i, n = asdl_seq_LEN(s->v.Import.names);
2259 :
2260 12 : for (i = 0; i < n; i++) {
2261 8 : alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2262 : int r;
2263 : PyObject *level;
2264 :
2265 8 : level = PyLong_FromLong(0);
2266 8 : if (level == NULL)
2267 0 : return 0;
2268 :
2269 8 : ADDOP_O(c, LOAD_CONST, level, consts);
2270 8 : Py_DECREF(level);
2271 8 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
2272 8 : ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2273 :
2274 8 : if (alias->asname) {
2275 0 : r = compiler_import_as(c, alias->name, alias->asname);
2276 0 : if (!r)
2277 0 : return r;
2278 : }
2279 : else {
2280 8 : identifier tmp = alias->name;
2281 8 : Py_ssize_t dot = PyUnicode_FindChar(
2282 8 : alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2283 8 : if (dot != -1)
2284 0 : tmp = PyUnicode_Substring(alias->name, 0, dot);
2285 8 : r = compiler_nameop(c, tmp, Store);
2286 8 : if (dot != -1) {
2287 0 : Py_DECREF(tmp);
2288 : }
2289 8 : if (!r)
2290 0 : return r;
2291 : }
2292 : }
2293 4 : return 1;
2294 : }
2295 :
2296 : static int
2297 13 : compiler_from_import(struct compiler *c, stmt_ty s)
2298 : {
2299 13 : int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2300 :
2301 13 : PyObject *names = PyTuple_New(n);
2302 : PyObject *level;
2303 : static PyObject *empty_string;
2304 :
2305 13 : if (!empty_string) {
2306 1 : empty_string = PyUnicode_FromString("");
2307 1 : if (!empty_string)
2308 0 : return 0;
2309 : }
2310 :
2311 13 : if (!names)
2312 0 : return 0;
2313 :
2314 13 : level = PyLong_FromLong(s->v.ImportFrom.level);
2315 13 : if (!level) {
2316 0 : Py_DECREF(names);
2317 0 : return 0;
2318 : }
2319 :
2320 : /* build up the names */
2321 32 : for (i = 0; i < n; i++) {
2322 19 : alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2323 19 : Py_INCREF(alias->name);
2324 19 : PyTuple_SET_ITEM(names, i, alias->name);
2325 : }
2326 :
2327 26 : if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2328 13 : !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2329 0 : Py_DECREF(level);
2330 0 : Py_DECREF(names);
2331 0 : return compiler_error(c, "from __future__ imports must occur "
2332 : "at the beginning of the file");
2333 : }
2334 :
2335 13 : ADDOP_O(c, LOAD_CONST, level, consts);
2336 13 : Py_DECREF(level);
2337 13 : ADDOP_O(c, LOAD_CONST, names, consts);
2338 13 : Py_DECREF(names);
2339 13 : if (s->v.ImportFrom.module) {
2340 13 : ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2341 : }
2342 : else {
2343 0 : ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2344 : }
2345 32 : for (i = 0; i < n; i++) {
2346 19 : alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2347 : identifier store_name;
2348 :
2349 19 : if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
2350 : assert(n == 1);
2351 0 : ADDOP(c, IMPORT_STAR);
2352 0 : return 1;
2353 : }
2354 :
2355 19 : ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2356 19 : store_name = alias->name;
2357 19 : if (alias->asname)
2358 6 : store_name = alias->asname;
2359 :
2360 19 : if (!compiler_nameop(c, store_name, Store)) {
2361 0 : Py_DECREF(names);
2362 0 : return 0;
2363 : }
2364 : }
2365 : /* remove imported module */
2366 13 : ADDOP(c, POP_TOP);
2367 13 : return 1;
2368 : }
2369 :
2370 : static int
2371 0 : compiler_assert(struct compiler *c, stmt_ty s)
2372 : {
2373 : static PyObject *assertion_error = NULL;
2374 : basicblock *end;
2375 :
2376 0 : if (c->c_optimize)
2377 0 : return 1;
2378 0 : if (assertion_error == NULL) {
2379 0 : assertion_error = PyUnicode_InternFromString("AssertionError");
2380 0 : if (assertion_error == NULL)
2381 0 : return 0;
2382 : }
2383 0 : if (s->v.Assert.test->kind == Tuple_kind &&
2384 0 : asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2385 0 : const char* msg =
2386 : "assertion is always true, perhaps remove parentheses?";
2387 0 : if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2388 0 : c->u->u_lineno, NULL, NULL) == -1)
2389 0 : return 0;
2390 : }
2391 0 : VISIT(c, expr, s->v.Assert.test);
2392 0 : end = compiler_new_block(c);
2393 0 : if (end == NULL)
2394 0 : return 0;
2395 0 : ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2396 0 : ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2397 0 : if (s->v.Assert.msg) {
2398 0 : VISIT(c, expr, s->v.Assert.msg);
2399 0 : ADDOP_I(c, CALL_FUNCTION, 1);
2400 : }
2401 0 : ADDOP_I(c, RAISE_VARARGS, 1);
2402 0 : compiler_use_next_block(c, end);
2403 0 : return 1;
2404 : }
2405 :
2406 : static int
2407 172 : compiler_visit_stmt(struct compiler *c, stmt_ty s)
2408 : {
2409 : int i, n;
2410 :
2411 : /* Always assign a lineno to the next instruction for a stmt. */
2412 172 : c->u->u_lineno = s->lineno;
2413 172 : c->u->u_col_offset = s->col_offset;
2414 172 : c->u->u_lineno_set = 0;
2415 :
2416 172 : switch (s->kind) {
2417 : case FunctionDef_kind:
2418 24 : return compiler_function(c, s);
2419 : case ClassDef_kind:
2420 3 : return compiler_class(c, s);
2421 : case Return_kind:
2422 26 : if (c->u->u_ste->ste_type != FunctionBlock)
2423 0 : return compiler_error(c, "'return' outside function");
2424 26 : if (s->v.Return.value) {
2425 26 : VISIT(c, expr, s->v.Return.value);
2426 : }
2427 : else
2428 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
2429 26 : ADDOP(c, RETURN_VALUE);
2430 26 : break;
2431 : case Delete_kind:
2432 0 : VISIT_SEQ(c, expr, s->v.Delete.targets)
2433 0 : break;
2434 : case Assign_kind:
2435 61 : n = asdl_seq_LEN(s->v.Assign.targets);
2436 61 : VISIT(c, expr, s->v.Assign.value);
2437 122 : for (i = 0; i < n; i++) {
2438 61 : if (i < n - 1)
2439 0 : ADDOP(c, DUP_TOP);
2440 61 : VISIT(c, expr,
2441 : (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2442 : }
2443 61 : break;
2444 : case AugAssign_kind:
2445 1 : return compiler_augassign(c, s);
2446 : case For_kind:
2447 2 : return compiler_for(c, s);
2448 : case While_kind:
2449 1 : return compiler_while(c, s);
2450 : case If_kind:
2451 14 : return compiler_if(c, s);
2452 : case Raise_kind:
2453 4 : n = 0;
2454 4 : if (s->v.Raise.exc) {
2455 4 : VISIT(c, expr, s->v.Raise.exc);
2456 4 : n++;
2457 4 : if (s->v.Raise.cause) {
2458 0 : VISIT(c, expr, s->v.Raise.cause);
2459 0 : n++;
2460 : }
2461 : }
2462 4 : ADDOP_I(c, RAISE_VARARGS, n);
2463 4 : break;
2464 : case Try_kind:
2465 5 : return compiler_try(c, s);
2466 : case Assert_kind:
2467 0 : return compiler_assert(c, s);
2468 : case Import_kind:
2469 4 : return compiler_import(c, s);
2470 : case ImportFrom_kind:
2471 13 : return compiler_from_import(c, s);
2472 : case Global_kind:
2473 : case Nonlocal_kind:
2474 0 : break;
2475 : case Expr_kind:
2476 13 : if (c->c_interactive && c->c_nestlevel <= 1) {
2477 0 : VISIT(c, expr, s->v.Expr.value);
2478 0 : ADDOP(c, PRINT_EXPR);
2479 : }
2480 26 : else if (s->v.Expr.value->kind != Str_kind &&
2481 13 : s->v.Expr.value->kind != Num_kind) {
2482 13 : VISIT(c, expr, s->v.Expr.value);
2483 13 : ADDOP(c, POP_TOP);
2484 : }
2485 13 : break;
2486 : case Pass_kind:
2487 1 : break;
2488 : case Break_kind:
2489 0 : if (!compiler_in_loop(c))
2490 0 : return compiler_error(c, "'break' outside loop");
2491 0 : ADDOP(c, BREAK_LOOP);
2492 0 : break;
2493 : case Continue_kind:
2494 0 : return compiler_continue(c);
2495 : case With_kind:
2496 0 : return compiler_with(c, s, 0);
2497 : }
2498 105 : return 1;
2499 : }
2500 :
2501 : static int
2502 1 : unaryop(unaryop_ty op)
2503 : {
2504 1 : switch (op) {
2505 : case Invert:
2506 0 : return UNARY_INVERT;
2507 : case Not:
2508 0 : return UNARY_NOT;
2509 : case UAdd:
2510 0 : return UNARY_POSITIVE;
2511 : case USub:
2512 1 : return UNARY_NEGATIVE;
2513 : default:
2514 0 : PyErr_Format(PyExc_SystemError,
2515 : "unary op %d should not be possible", op);
2516 0 : return 0;
2517 : }
2518 : }
2519 :
2520 : static int
2521 20 : binop(struct compiler *c, operator_ty op)
2522 : {
2523 20 : switch (op) {
2524 : case Add:
2525 13 : return BINARY_ADD;
2526 : case Sub:
2527 0 : return BINARY_SUBTRACT;
2528 : case Mult:
2529 0 : return BINARY_MULTIPLY;
2530 : case Div:
2531 0 : return BINARY_TRUE_DIVIDE;
2532 : case Mod:
2533 7 : return BINARY_MODULO;
2534 : case Pow:
2535 0 : return BINARY_POWER;
2536 : case LShift:
2537 0 : return BINARY_LSHIFT;
2538 : case RShift:
2539 0 : return BINARY_RSHIFT;
2540 : case BitOr:
2541 0 : return BINARY_OR;
2542 : case BitXor:
2543 0 : return BINARY_XOR;
2544 : case BitAnd:
2545 0 : return BINARY_AND;
2546 : case FloorDiv:
2547 0 : return BINARY_FLOOR_DIVIDE;
2548 : default:
2549 0 : PyErr_Format(PyExc_SystemError,
2550 : "binary op %d should not be possible", op);
2551 0 : return 0;
2552 : }
2553 : }
2554 :
2555 : static int
2556 20 : cmpop(cmpop_ty op)
2557 : {
2558 20 : switch (op) {
2559 : case Eq:
2560 9 : return PyCmp_EQ;
2561 : case NotEq:
2562 3 : return PyCmp_NE;
2563 : case Lt:
2564 0 : return PyCmp_LT;
2565 : case LtE:
2566 0 : return PyCmp_LE;
2567 : case Gt:
2568 2 : return PyCmp_GT;
2569 : case GtE:
2570 0 : return PyCmp_GE;
2571 : case Is:
2572 0 : return PyCmp_IS;
2573 : case IsNot:
2574 0 : return PyCmp_IS_NOT;
2575 : case In:
2576 6 : return PyCmp_IN;
2577 : case NotIn:
2578 0 : return PyCmp_NOT_IN;
2579 : default:
2580 0 : return PyCmp_BAD;
2581 : }
2582 : }
2583 :
2584 : static int
2585 1 : inplace_binop(struct compiler *c, operator_ty op)
2586 : {
2587 1 : switch (op) {
2588 : case Add:
2589 1 : return INPLACE_ADD;
2590 : case Sub:
2591 0 : return INPLACE_SUBTRACT;
2592 : case Mult:
2593 0 : return INPLACE_MULTIPLY;
2594 : case Div:
2595 0 : return INPLACE_TRUE_DIVIDE;
2596 : case Mod:
2597 0 : return INPLACE_MODULO;
2598 : case Pow:
2599 0 : return INPLACE_POWER;
2600 : case LShift:
2601 0 : return INPLACE_LSHIFT;
2602 : case RShift:
2603 0 : return INPLACE_RSHIFT;
2604 : case BitOr:
2605 0 : return INPLACE_OR;
2606 : case BitXor:
2607 0 : return INPLACE_XOR;
2608 : case BitAnd:
2609 0 : return INPLACE_AND;
2610 : case FloorDiv:
2611 0 : return INPLACE_FLOOR_DIVIDE;
2612 : default:
2613 0 : PyErr_Format(PyExc_SystemError,
2614 : "inplace binary op %d should not be possible", op);
2615 0 : return 0;
2616 : }
2617 : }
2618 :
2619 : static int
2620 376 : compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2621 : {
2622 : int op, scope, arg;
2623 : enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2624 :
2625 376 : PyObject *dict = c->u->u_names;
2626 : PyObject *mangled;
2627 : /* XXX AugStore isn't used anywhere! */
2628 :
2629 376 : mangled = _Py_Mangle(c->u->u_private, name);
2630 376 : if (!mangled)
2631 0 : return 0;
2632 :
2633 376 : op = 0;
2634 376 : optype = OP_NAME;
2635 376 : scope = PyST_GetScope(c->u->u_ste, mangled);
2636 376 : switch (scope) {
2637 : case FREE:
2638 0 : dict = c->u->u_freevars;
2639 0 : optype = OP_DEREF;
2640 0 : break;
2641 : case CELL:
2642 0 : dict = c->u->u_cellvars;
2643 0 : optype = OP_DEREF;
2644 0 : break;
2645 : case LOCAL:
2646 274 : if (c->u->u_ste->ste_type == FunctionBlock)
2647 190 : optype = OP_FAST;
2648 274 : break;
2649 : case GLOBAL_IMPLICIT:
2650 154 : if (c->u->u_ste->ste_type == FunctionBlock &&
2651 63 : !c->u->u_ste->ste_unoptimized)
2652 63 : optype = OP_GLOBAL;
2653 91 : break;
2654 : case GLOBAL_EXPLICIT:
2655 0 : optype = OP_GLOBAL;
2656 0 : break;
2657 : default:
2658 : /* scope can be 0 */
2659 11 : break;
2660 : }
2661 :
2662 : /* XXX Leave assert here, but handle __doc__ and the like better */
2663 : assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
2664 :
2665 376 : switch (optype) {
2666 : case OP_DEREF:
2667 0 : switch (ctx) {
2668 0 : case Load: op = LOAD_DEREF; break;
2669 0 : case Store: op = STORE_DEREF; break;
2670 : case AugLoad:
2671 : case AugStore:
2672 0 : break;
2673 0 : case Del: op = DELETE_DEREF; break;
2674 : case Param:
2675 : default:
2676 0 : PyErr_SetString(PyExc_SystemError,
2677 : "param invalid for deref variable");
2678 0 : return 0;
2679 : }
2680 0 : break;
2681 : case OP_FAST:
2682 190 : switch (ctx) {
2683 162 : case Load: op = LOAD_FAST; break;
2684 25 : case Store: op = STORE_FAST; break;
2685 3 : case Del: op = DELETE_FAST; break;
2686 : case AugLoad:
2687 : case AugStore:
2688 0 : break;
2689 : case Param:
2690 : default:
2691 0 : PyErr_SetString(PyExc_SystemError,
2692 : "param invalid for local variable");
2693 0 : return 0;
2694 : }
2695 190 : ADDOP_O(c, op, mangled, varnames);
2696 190 : Py_DECREF(mangled);
2697 190 : return 1;
2698 : case OP_GLOBAL:
2699 63 : switch (ctx) {
2700 63 : case Load: op = LOAD_GLOBAL; break;
2701 0 : case Store: op = STORE_GLOBAL; break;
2702 0 : case Del: op = DELETE_GLOBAL; break;
2703 : case AugLoad:
2704 : case AugStore:
2705 0 : break;
2706 : case Param:
2707 : default:
2708 0 : PyErr_SetString(PyExc_SystemError,
2709 : "param invalid for global variable");
2710 0 : return 0;
2711 : }
2712 63 : break;
2713 : case OP_NAME:
2714 123 : switch (ctx) {
2715 46 : case Load: op = LOAD_NAME; break;
2716 77 : case Store: op = STORE_NAME; break;
2717 0 : case Del: op = DELETE_NAME; break;
2718 : case AugLoad:
2719 : case AugStore:
2720 0 : break;
2721 : case Param:
2722 : default:
2723 0 : PyErr_SetString(PyExc_SystemError,
2724 : "param invalid for name variable");
2725 0 : return 0;
2726 : }
2727 123 : break;
2728 : }
2729 :
2730 : assert(op);
2731 186 : arg = compiler_add_o(c, dict, mangled);
2732 186 : Py_DECREF(mangled);
2733 186 : if (arg < 0)
2734 0 : return 0;
2735 186 : return compiler_addop_i(c, op, arg);
2736 : }
2737 :
2738 : static int
2739 5 : compiler_boolop(struct compiler *c, expr_ty e)
2740 : {
2741 : basicblock *end;
2742 : int jumpi, i, n;
2743 : asdl_seq *s;
2744 :
2745 : assert(e->kind == BoolOp_kind);
2746 5 : if (e->v.BoolOp.op == And)
2747 4 : jumpi = JUMP_IF_FALSE_OR_POP;
2748 : else
2749 1 : jumpi = JUMP_IF_TRUE_OR_POP;
2750 5 : end = compiler_new_block(c);
2751 5 : if (end == NULL)
2752 0 : return 0;
2753 5 : s = e->v.BoolOp.values;
2754 5 : n = asdl_seq_LEN(s) - 1;
2755 : assert(n >= 0);
2756 11 : for (i = 0; i < n; ++i) {
2757 6 : VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2758 6 : ADDOP_JABS(c, jumpi, end);
2759 : }
2760 5 : VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2761 5 : compiler_use_next_block(c, end);
2762 5 : return 1;
2763 : }
2764 :
2765 : static int
2766 2 : compiler_list(struct compiler *c, expr_ty e)
2767 : {
2768 2 : int n = asdl_seq_LEN(e->v.List.elts);
2769 2 : if (e->v.List.ctx == Store) {
2770 0 : int i, seen_star = 0;
2771 0 : for (i = 0; i < n; i++) {
2772 0 : expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2773 0 : if (elt->kind == Starred_kind && !seen_star) {
2774 0 : if ((i >= (1 << 8)) ||
2775 0 : (n-i-1 >= (INT_MAX >> 8)))
2776 0 : return compiler_error(c,
2777 : "too many expressions in "
2778 : "star-unpacking assignment");
2779 0 : ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2780 0 : seen_star = 1;
2781 0 : asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2782 0 : } else if (elt->kind == Starred_kind) {
2783 0 : return compiler_error(c,
2784 : "two starred expressions in assignment");
2785 : }
2786 : }
2787 0 : if (!seen_star) {
2788 0 : ADDOP_I(c, UNPACK_SEQUENCE, n);
2789 : }
2790 : }
2791 2 : VISIT_SEQ(c, expr, e->v.List.elts);
2792 2 : if (e->v.List.ctx == Load) {
2793 2 : ADDOP_I(c, BUILD_LIST, n);
2794 : }
2795 2 : return 1;
2796 : }
2797 :
2798 : static int
2799 15 : compiler_tuple(struct compiler *c, expr_ty e)
2800 : {
2801 15 : int n = asdl_seq_LEN(e->v.Tuple.elts);
2802 15 : if (e->v.Tuple.ctx == Store) {
2803 2 : int i, seen_star = 0;
2804 6 : for (i = 0; i < n; i++) {
2805 4 : expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2806 4 : if (elt->kind == Starred_kind && !seen_star) {
2807 0 : if ((i >= (1 << 8)) ||
2808 0 : (n-i-1 >= (INT_MAX >> 8)))
2809 0 : return compiler_error(c,
2810 : "too many expressions in "
2811 : "star-unpacking assignment");
2812 0 : ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2813 0 : seen_star = 1;
2814 0 : asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2815 4 : } else if (elt->kind == Starred_kind) {
2816 0 : return compiler_error(c,
2817 : "two starred expressions in assignment");
2818 : }
2819 : }
2820 2 : if (!seen_star) {
2821 2 : ADDOP_I(c, UNPACK_SEQUENCE, n);
2822 : }
2823 : }
2824 15 : VISIT_SEQ(c, expr, e->v.Tuple.elts);
2825 15 : if (e->v.Tuple.ctx == Load) {
2826 13 : ADDOP_I(c, BUILD_TUPLE, n);
2827 : }
2828 15 : return 1;
2829 : }
2830 :
2831 : static int
2832 20 : compiler_compare(struct compiler *c, expr_ty e)
2833 : {
2834 : int i, n;
2835 20 : basicblock *cleanup = NULL;
2836 :
2837 : /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2838 20 : VISIT(c, expr, e->v.Compare.left);
2839 20 : n = asdl_seq_LEN(e->v.Compare.ops);
2840 : assert(n > 0);
2841 20 : if (n > 1) {
2842 0 : cleanup = compiler_new_block(c);
2843 0 : if (cleanup == NULL)
2844 0 : return 0;
2845 0 : VISIT(c, expr,
2846 : (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2847 : }
2848 20 : for (i = 1; i < n; i++) {
2849 0 : ADDOP(c, DUP_TOP);
2850 0 : ADDOP(c, ROT_THREE);
2851 0 : ADDOP_I(c, COMPARE_OP,
2852 : cmpop((cmpop_ty)(asdl_seq_GET(
2853 : e->v.Compare.ops, i - 1))));
2854 0 : ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2855 0 : NEXT_BLOCK(c);
2856 0 : if (i < (n - 1))
2857 0 : VISIT(c, expr,
2858 : (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2859 : }
2860 20 : VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2861 20 : ADDOP_I(c, COMPARE_OP,
2862 : cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2863 20 : if (n > 1) {
2864 0 : basicblock *end = compiler_new_block(c);
2865 0 : if (end == NULL)
2866 0 : return 0;
2867 0 : ADDOP_JREL(c, JUMP_FORWARD, end);
2868 0 : compiler_use_next_block(c, cleanup);
2869 0 : ADDOP(c, ROT_TWO);
2870 0 : ADDOP(c, POP_TOP);
2871 0 : compiler_use_next_block(c, end);
2872 : }
2873 20 : return 1;
2874 : }
2875 :
2876 : static int
2877 81 : compiler_call(struct compiler *c, expr_ty e)
2878 : {
2879 81 : VISIT(c, expr, e->v.Call.func);
2880 81 : return compiler_call_helper(c, 0,
2881 : e->v.Call.args,
2882 : e->v.Call.keywords,
2883 : e->v.Call.starargs,
2884 : e->v.Call.kwargs);
2885 : }
2886 :
2887 : /* shared code between compiler_call and compiler_class */
2888 : static int
2889 84 : compiler_call_helper(struct compiler *c,
2890 : int n, /* Args already pushed */
2891 : asdl_seq *args,
2892 : asdl_seq *keywords,
2893 : expr_ty starargs,
2894 : expr_ty kwargs)
2895 : {
2896 84 : int code = 0;
2897 :
2898 84 : n += asdl_seq_LEN(args);
2899 84 : VISIT_SEQ(c, expr, args);
2900 84 : if (keywords) {
2901 75 : VISIT_SEQ(c, keyword, keywords);
2902 75 : n |= asdl_seq_LEN(keywords) << 8;
2903 : }
2904 84 : if (starargs) {
2905 0 : VISIT(c, expr, starargs);
2906 0 : code |= 1;
2907 : }
2908 84 : if (kwargs) {
2909 0 : VISIT(c, expr, kwargs);
2910 0 : code |= 2;
2911 : }
2912 84 : switch (code) {
2913 : case 0:
2914 84 : ADDOP_I(c, CALL_FUNCTION, n);
2915 84 : break;
2916 : case 1:
2917 0 : ADDOP_I(c, CALL_FUNCTION_VAR, n);
2918 0 : break;
2919 : case 2:
2920 0 : ADDOP_I(c, CALL_FUNCTION_KW, n);
2921 0 : break;
2922 : case 3:
2923 0 : ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2924 0 : break;
2925 : }
2926 84 : return 1;
2927 : }
2928 :
2929 :
2930 : /* List and set comprehensions and generator expressions work by creating a
2931 : nested function to perform the actual iteration. This means that the
2932 : iteration variables don't leak into the current scope.
2933 : The defined function is called immediately following its definition, with the
2934 : result of that call being the result of the expression.
2935 : The LC/SC version returns the populated container, while the GE version is
2936 : flagged in symtable.c as a generator, so it returns the generator object
2937 : when the function is called.
2938 : This code *knows* that the loop cannot contain break, continue, or return,
2939 : so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2940 :
2941 : Possible cleanups:
2942 : - iterate over the generator sequence instead of using recursion
2943 : */
2944 :
2945 : static int
2946 0 : compiler_comprehension_generator(struct compiler *c,
2947 : asdl_seq *generators, int gen_index,
2948 : expr_ty elt, expr_ty val, int type)
2949 : {
2950 : /* generate code for the iterator, then each of the ifs,
2951 : and then write to the element */
2952 :
2953 : comprehension_ty gen;
2954 : basicblock *start, *anchor, *skip, *if_cleanup;
2955 : int i, n;
2956 :
2957 0 : start = compiler_new_block(c);
2958 0 : skip = compiler_new_block(c);
2959 0 : if_cleanup = compiler_new_block(c);
2960 0 : anchor = compiler_new_block(c);
2961 :
2962 0 : if (start == NULL || skip == NULL || if_cleanup == NULL ||
2963 : anchor == NULL)
2964 0 : return 0;
2965 :
2966 0 : gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2967 :
2968 0 : if (gen_index == 0) {
2969 : /* Receive outermost iter as an implicit argument */
2970 0 : c->u->u_argcount = 1;
2971 0 : ADDOP_I(c, LOAD_FAST, 0);
2972 : }
2973 : else {
2974 : /* Sub-iter - calculate on the fly */
2975 0 : VISIT(c, expr, gen->iter);
2976 0 : ADDOP(c, GET_ITER);
2977 : }
2978 0 : compiler_use_next_block(c, start);
2979 0 : ADDOP_JREL(c, FOR_ITER, anchor);
2980 0 : NEXT_BLOCK(c);
2981 0 : VISIT(c, expr, gen->target);
2982 :
2983 : /* XXX this needs to be cleaned up...a lot! */
2984 0 : n = asdl_seq_LEN(gen->ifs);
2985 0 : for (i = 0; i < n; i++) {
2986 0 : expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2987 0 : VISIT(c, expr, e);
2988 0 : ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2989 0 : NEXT_BLOCK(c);
2990 : }
2991 :
2992 0 : if (++gen_index < asdl_seq_LEN(generators))
2993 0 : if (!compiler_comprehension_generator(c,
2994 : generators, gen_index,
2995 : elt, val, type))
2996 0 : return 0;
2997 :
2998 : /* only append after the last for generator */
2999 0 : if (gen_index >= asdl_seq_LEN(generators)) {
3000 : /* comprehension specific code */
3001 0 : switch (type) {
3002 : case COMP_GENEXP:
3003 0 : VISIT(c, expr, elt);
3004 0 : ADDOP(c, YIELD_VALUE);
3005 0 : ADDOP(c, POP_TOP);
3006 0 : break;
3007 : case COMP_LISTCOMP:
3008 0 : VISIT(c, expr, elt);
3009 0 : ADDOP_I(c, LIST_APPEND, gen_index + 1);
3010 0 : break;
3011 : case COMP_SETCOMP:
3012 0 : VISIT(c, expr, elt);
3013 0 : ADDOP_I(c, SET_ADD, gen_index + 1);
3014 0 : break;
3015 : case COMP_DICTCOMP:
3016 : /* With 'd[k] = v', v is evaluated before k, so we do
3017 : the same. */
3018 0 : VISIT(c, expr, val);
3019 0 : VISIT(c, expr, elt);
3020 0 : ADDOP_I(c, MAP_ADD, gen_index + 1);
3021 0 : break;
3022 : default:
3023 0 : return 0;
3024 : }
3025 :
3026 0 : compiler_use_next_block(c, skip);
3027 : }
3028 0 : compiler_use_next_block(c, if_cleanup);
3029 0 : ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3030 0 : compiler_use_next_block(c, anchor);
3031 :
3032 0 : return 1;
3033 : }
3034 :
3035 : static int
3036 0 : compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
3037 : asdl_seq *generators, expr_ty elt, expr_ty val)
3038 : {
3039 0 : PyCodeObject *co = NULL;
3040 : expr_ty outermost_iter;
3041 0 : PyObject *qualname = NULL;
3042 :
3043 0 : outermost_iter = ((comprehension_ty)
3044 0 : asdl_seq_GET(generators, 0))->iter;
3045 :
3046 0 : if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3047 : (void *)e, e->lineno))
3048 0 : goto error;
3049 :
3050 0 : if (type != COMP_GENEXP) {
3051 : int op;
3052 0 : switch (type) {
3053 : case COMP_LISTCOMP:
3054 0 : op = BUILD_LIST;
3055 0 : break;
3056 : case COMP_SETCOMP:
3057 0 : op = BUILD_SET;
3058 0 : break;
3059 : case COMP_DICTCOMP:
3060 0 : op = BUILD_MAP;
3061 0 : break;
3062 : default:
3063 0 : PyErr_Format(PyExc_SystemError,
3064 : "unknown comprehension type %d", type);
3065 0 : goto error_in_scope;
3066 : }
3067 :
3068 0 : ADDOP_I(c, op, 0);
3069 : }
3070 :
3071 0 : if (!compiler_comprehension_generator(c, generators, 0, elt,
3072 : val, type))
3073 0 : goto error_in_scope;
3074 :
3075 0 : if (type != COMP_GENEXP) {
3076 0 : ADDOP(c, RETURN_VALUE);
3077 : }
3078 :
3079 0 : co = assemble(c, 1);
3080 0 : qualname = compiler_scope_qualname(c);
3081 0 : compiler_exit_scope(c);
3082 0 : if (qualname == NULL || co == NULL)
3083 : goto error;
3084 :
3085 0 : if (!compiler_make_closure(c, co, 0, qualname))
3086 0 : goto error;
3087 0 : Py_DECREF(qualname);
3088 0 : Py_DECREF(co);
3089 :
3090 0 : VISIT(c, expr, outermost_iter);
3091 0 : ADDOP(c, GET_ITER);
3092 0 : ADDOP_I(c, CALL_FUNCTION, 1);
3093 0 : return 1;
3094 : error_in_scope:
3095 0 : compiler_exit_scope(c);
3096 : error:
3097 0 : Py_XDECREF(qualname);
3098 0 : Py_XDECREF(co);
3099 0 : return 0;
3100 : }
3101 :
3102 : static int
3103 0 : compiler_genexp(struct compiler *c, expr_ty e)
3104 : {
3105 : static identifier name;
3106 0 : if (!name) {
3107 0 : name = PyUnicode_FromString("<genexpr>");
3108 0 : if (!name)
3109 0 : return 0;
3110 : }
3111 : assert(e->kind == GeneratorExp_kind);
3112 0 : return compiler_comprehension(c, e, COMP_GENEXP, name,
3113 : e->v.GeneratorExp.generators,
3114 : e->v.GeneratorExp.elt, NULL);
3115 : }
3116 :
3117 : static int
3118 0 : compiler_listcomp(struct compiler *c, expr_ty e)
3119 : {
3120 : static identifier name;
3121 0 : if (!name) {
3122 0 : name = PyUnicode_FromString("<listcomp>");
3123 0 : if (!name)
3124 0 : return 0;
3125 : }
3126 : assert(e->kind == ListComp_kind);
3127 0 : return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3128 : e->v.ListComp.generators,
3129 : e->v.ListComp.elt, NULL);
3130 : }
3131 :
3132 : static int
3133 0 : compiler_setcomp(struct compiler *c, expr_ty e)
3134 : {
3135 : static identifier name;
3136 0 : if (!name) {
3137 0 : name = PyUnicode_FromString("<setcomp>");
3138 0 : if (!name)
3139 0 : return 0;
3140 : }
3141 : assert(e->kind == SetComp_kind);
3142 0 : return compiler_comprehension(c, e, COMP_SETCOMP, name,
3143 : e->v.SetComp.generators,
3144 : e->v.SetComp.elt, NULL);
3145 : }
3146 :
3147 :
3148 : static int
3149 0 : compiler_dictcomp(struct compiler *c, expr_ty e)
3150 : {
3151 : static identifier name;
3152 0 : if (!name) {
3153 0 : name = PyUnicode_FromString("<dictcomp>");
3154 0 : if (!name)
3155 0 : return 0;
3156 : }
3157 : assert(e->kind == DictComp_kind);
3158 0 : return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3159 : e->v.DictComp.generators,
3160 : e->v.DictComp.key, e->v.DictComp.value);
3161 : }
3162 :
3163 :
3164 : static int
3165 9 : compiler_visit_keyword(struct compiler *c, keyword_ty k)
3166 : {
3167 9 : ADDOP_O(c, LOAD_CONST, k->arg, consts);
3168 9 : VISIT(c, expr, k->value);
3169 9 : return 1;
3170 : }
3171 :
3172 : /* Test whether expression is constant. For constants, report
3173 : whether they are true or false.
3174 :
3175 : Return values: 1 for true, 0 for false, -1 for non-constant.
3176 : */
3177 :
3178 : static int
3179 15 : expr_constant(struct compiler *c, expr_ty e)
3180 : {
3181 : char *id;
3182 15 : switch (e->kind) {
3183 : case Ellipsis_kind:
3184 0 : return 1;
3185 : case Num_kind:
3186 0 : return PyObject_IsTrue(e->v.Num.n);
3187 : case Str_kind:
3188 0 : return PyObject_IsTrue(e->v.Str.s);
3189 : case Name_kind:
3190 : /* optimize away names that can't be reassigned */
3191 2 : id = PyUnicode_AsUTF8(e->v.Name.id);
3192 2 : if (strcmp(id, "True") == 0) return 1;
3193 2 : if (strcmp(id, "False") == 0) return 0;
3194 2 : if (strcmp(id, "None") == 0) return 0;
3195 2 : if (strcmp(id, "__debug__") == 0)
3196 0 : return ! c->c_optimize;
3197 : /* fall through */
3198 : default:
3199 15 : return -1;
3200 : }
3201 : }
3202 :
3203 : /*
3204 : Implements the with statement from PEP 343.
3205 :
3206 : The semantics outlined in that PEP are as follows:
3207 :
3208 : with EXPR as VAR:
3209 : BLOCK
3210 :
3211 : It is implemented roughly as:
3212 :
3213 : context = EXPR
3214 : exit = context.__exit__ # not calling it
3215 : value = context.__enter__()
3216 : try:
3217 : VAR = value # if VAR present in the syntax
3218 : BLOCK
3219 : finally:
3220 : if an exception was raised:
3221 : exc = copy of (exception, instance, traceback)
3222 : else:
3223 : exc = (None, None, None)
3224 : exit(*exc)
3225 : */
3226 : static int
3227 0 : compiler_with(struct compiler *c, stmt_ty s, int pos)
3228 : {
3229 : basicblock *block, *finally;
3230 0 : withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
3231 :
3232 : assert(s->kind == With_kind);
3233 :
3234 0 : block = compiler_new_block(c);
3235 0 : finally = compiler_new_block(c);
3236 0 : if (!block || !finally)
3237 0 : return 0;
3238 :
3239 : /* Evaluate EXPR */
3240 0 : VISIT(c, expr, item->context_expr);
3241 0 : ADDOP_JREL(c, SETUP_WITH, finally);
3242 :
3243 : /* SETUP_WITH pushes a finally block. */
3244 0 : compiler_use_next_block(c, block);
3245 0 : if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3246 0 : return 0;
3247 : }
3248 :
3249 0 : if (item->optional_vars) {
3250 0 : VISIT(c, expr, item->optional_vars);
3251 : }
3252 : else {
3253 : /* Discard result from context.__enter__() */
3254 0 : ADDOP(c, POP_TOP);
3255 : }
3256 :
3257 0 : pos++;
3258 0 : if (pos == asdl_seq_LEN(s->v.With.items))
3259 : /* BLOCK code */
3260 0 : VISIT_SEQ(c, stmt, s->v.With.body)
3261 0 : else if (!compiler_with(c, s, pos))
3262 0 : return 0;
3263 :
3264 : /* End of try block; start the finally block */
3265 0 : ADDOP(c, POP_BLOCK);
3266 0 : compiler_pop_fblock(c, FINALLY_TRY, block);
3267 :
3268 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
3269 0 : compiler_use_next_block(c, finally);
3270 0 : if (!compiler_push_fblock(c, FINALLY_END, finally))
3271 0 : return 0;
3272 :
3273 : /* Finally block starts; context.__exit__ is on the stack under
3274 : the exception or return information. Just issue our magic
3275 : opcode. */
3276 0 : ADDOP(c, WITH_CLEANUP);
3277 :
3278 : /* Finally block ends. */
3279 0 : ADDOP(c, END_FINALLY);
3280 0 : compiler_pop_fblock(c, FINALLY_END, finally);
3281 0 : return 1;
3282 : }
3283 :
3284 : static int
3285 675 : compiler_visit_expr(struct compiler *c, expr_ty e)
3286 : {
3287 : int i, n;
3288 :
3289 : /* If expr e has a different line number than the last expr/stmt,
3290 : set a new line number for the next instruction.
3291 : */
3292 675 : if (e->lineno > c->u->u_lineno) {
3293 10 : c->u->u_lineno = e->lineno;
3294 10 : c->u->u_lineno_set = 0;
3295 : }
3296 : /* Updating the column offset is always harmless. */
3297 675 : c->u->u_col_offset = e->col_offset;
3298 675 : switch (e->kind) {
3299 : case BoolOp_kind:
3300 5 : return compiler_boolop(c, e);
3301 : case BinOp_kind:
3302 20 : VISIT(c, expr, e->v.BinOp.left);
3303 20 : VISIT(c, expr, e->v.BinOp.right);
3304 20 : ADDOP(c, binop(c, e->v.BinOp.op));
3305 20 : break;
3306 : case UnaryOp_kind:
3307 1 : VISIT(c, expr, e->v.UnaryOp.operand);
3308 1 : ADDOP(c, unaryop(e->v.UnaryOp.op));
3309 1 : break;
3310 : case Lambda_kind:
3311 0 : return compiler_lambda(c, e);
3312 : case IfExp_kind:
3313 0 : return compiler_ifexp(c, e);
3314 : case Dict_kind:
3315 1 : n = asdl_seq_LEN(e->v.Dict.values);
3316 1 : ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3317 1 : for (i = 0; i < n; i++) {
3318 0 : VISIT(c, expr,
3319 : (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3320 0 : VISIT(c, expr,
3321 : (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3322 0 : ADDOP(c, STORE_MAP);
3323 : }
3324 1 : break;
3325 : case Set_kind:
3326 0 : n = asdl_seq_LEN(e->v.Set.elts);
3327 0 : VISIT_SEQ(c, expr, e->v.Set.elts);
3328 0 : ADDOP_I(c, BUILD_SET, n);
3329 0 : break;
3330 : case GeneratorExp_kind:
3331 0 : return compiler_genexp(c, e);
3332 : case ListComp_kind:
3333 0 : return compiler_listcomp(c, e);
3334 : case SetComp_kind:
3335 0 : return compiler_setcomp(c, e);
3336 : case DictComp_kind:
3337 0 : return compiler_dictcomp(c, e);
3338 : case Yield_kind:
3339 : case YieldFrom_kind: {
3340 : expr_ty value;
3341 0 : if (c->u->u_ste->ste_type != FunctionBlock)
3342 0 : return compiler_error(c, "'yield' outside function");
3343 0 : value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
3344 0 : if (value) {
3345 0 : VISIT(c, expr, value);
3346 : }
3347 : else {
3348 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
3349 : }
3350 0 : if (e->kind == YieldFrom_kind) {
3351 0 : ADDOP(c, GET_ITER);
3352 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
3353 0 : ADDOP(c, YIELD_FROM);
3354 : }
3355 : else {
3356 0 : ADDOP(c, YIELD_VALUE);
3357 : }
3358 0 : break;
3359 : }
3360 : case Compare_kind:
3361 20 : return compiler_compare(c, e);
3362 : case Call_kind:
3363 81 : return compiler_call(c, e);
3364 : case Num_kind:
3365 33 : ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3366 33 : break;
3367 : case Str_kind:
3368 65 : ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3369 65 : break;
3370 : case Bytes_kind:
3371 0 : ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3372 0 : break;
3373 : case Ellipsis_kind:
3374 0 : ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3375 0 : break;
3376 : /* The following exprs can be assignment targets. */
3377 : case Attribute_kind:
3378 109 : if (e->v.Attribute.ctx != AugStore)
3379 108 : VISIT(c, expr, e->v.Attribute.value);
3380 109 : switch (e->v.Attribute.ctx) {
3381 : case AugLoad:
3382 1 : ADDOP(c, DUP_TOP);
3383 : /* Fall through to load */
3384 : case Load:
3385 79 : ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3386 79 : break;
3387 : case AugStore:
3388 1 : ADDOP(c, ROT_TWO);
3389 : /* Fall through to save */
3390 : case Store:
3391 30 : ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3392 30 : break;
3393 : case Del:
3394 0 : ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3395 0 : break;
3396 : case Param:
3397 : default:
3398 0 : PyErr_SetString(PyExc_SystemError,
3399 : "param invalid in attribute expression");
3400 0 : return 0;
3401 : }
3402 109 : break;
3403 : case Subscript_kind:
3404 21 : switch (e->v.Subscript.ctx) {
3405 : case AugLoad:
3406 0 : VISIT(c, expr, e->v.Subscript.value);
3407 0 : VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3408 0 : break;
3409 : case Load:
3410 19 : VISIT(c, expr, e->v.Subscript.value);
3411 19 : VISIT_SLICE(c, e->v.Subscript.slice, Load);
3412 19 : break;
3413 : case AugStore:
3414 0 : VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3415 0 : break;
3416 : case Store:
3417 2 : VISIT(c, expr, e->v.Subscript.value);
3418 2 : VISIT_SLICE(c, e->v.Subscript.slice, Store);
3419 2 : break;
3420 : case Del:
3421 0 : VISIT(c, expr, e->v.Subscript.value);
3422 0 : VISIT_SLICE(c, e->v.Subscript.slice, Del);
3423 0 : break;
3424 : case Param:
3425 : default:
3426 0 : PyErr_SetString(PyExc_SystemError,
3427 : "param invalid in subscript expression");
3428 0 : return 0;
3429 : }
3430 21 : break;
3431 : case Starred_kind:
3432 0 : switch (e->v.Starred.ctx) {
3433 : case Store:
3434 : /* In all legitimate cases, the Starred node was already replaced
3435 : * by compiler_list/compiler_tuple. XXX: is that okay? */
3436 0 : return compiler_error(c,
3437 : "starred assignment target must be in a list or tuple");
3438 : default:
3439 0 : return compiler_error(c,
3440 : "can use starred expression only as assignment target");
3441 : }
3442 : break;
3443 : case Name_kind:
3444 302 : return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3445 : /* child nodes of List and Tuple will have expr_context set */
3446 : case List_kind:
3447 2 : return compiler_list(c, e);
3448 : case Tuple_kind:
3449 15 : return compiler_tuple(c, e);
3450 : }
3451 250 : return 1;
3452 : }
3453 :
3454 : static int
3455 1 : compiler_augassign(struct compiler *c, stmt_ty s)
3456 : {
3457 1 : expr_ty e = s->v.AugAssign.target;
3458 : expr_ty auge;
3459 :
3460 : assert(s->kind == AugAssign_kind);
3461 :
3462 1 : switch (e->kind) {
3463 : case Attribute_kind:
3464 1 : auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3465 : AugLoad, e->lineno, e->col_offset, c->c_arena);
3466 1 : if (auge == NULL)
3467 0 : return 0;
3468 1 : VISIT(c, expr, auge);
3469 1 : VISIT(c, expr, s->v.AugAssign.value);
3470 1 : ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3471 1 : auge->v.Attribute.ctx = AugStore;
3472 1 : VISIT(c, expr, auge);
3473 1 : break;
3474 : case Subscript_kind:
3475 0 : auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3476 : AugLoad, e->lineno, e->col_offset, c->c_arena);
3477 0 : if (auge == NULL)
3478 0 : return 0;
3479 0 : VISIT(c, expr, auge);
3480 0 : VISIT(c, expr, s->v.AugAssign.value);
3481 0 : ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3482 0 : auge->v.Subscript.ctx = AugStore;
3483 0 : VISIT(c, expr, auge);
3484 0 : break;
3485 : case Name_kind:
3486 0 : if (!compiler_nameop(c, e->v.Name.id, Load))
3487 0 : return 0;
3488 0 : VISIT(c, expr, s->v.AugAssign.value);
3489 0 : ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3490 0 : return compiler_nameop(c, e->v.Name.id, Store);
3491 : default:
3492 0 : PyErr_Format(PyExc_SystemError,
3493 : "invalid node type (%d) for augmented assignment",
3494 0 : e->kind);
3495 0 : return 0;
3496 : }
3497 1 : return 1;
3498 : }
3499 :
3500 : static int
3501 16 : compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3502 : {
3503 : struct fblockinfo *f;
3504 16 : if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3505 0 : PyErr_SetString(PyExc_SystemError,
3506 : "too many statically nested blocks");
3507 0 : return 0;
3508 : }
3509 16 : f = &c->u->u_fblock[c->u->u_nfblocks++];
3510 16 : f->fb_type = t;
3511 16 : f->fb_block = b;
3512 16 : return 1;
3513 : }
3514 :
3515 : static void
3516 16 : compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3517 : {
3518 16 : struct compiler_unit *u = c->u;
3519 : assert(u->u_nfblocks > 0);
3520 16 : u->u_nfblocks--;
3521 : assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3522 : assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3523 16 : }
3524 :
3525 : static int
3526 0 : compiler_in_loop(struct compiler *c) {
3527 : int i;
3528 0 : struct compiler_unit *u = c->u;
3529 0 : for (i = 0; i < u->u_nfblocks; ++i) {
3530 0 : if (u->u_fblock[i].fb_type == LOOP)
3531 0 : return 1;
3532 : }
3533 0 : return 0;
3534 : }
3535 : /* Raises a SyntaxError and returns 0.
3536 : If something goes wrong, a different exception may be raised.
3537 : */
3538 :
3539 : static int
3540 0 : compiler_error(struct compiler *c, const char *errstr)
3541 : {
3542 : PyObject *loc;
3543 0 : PyObject *u = NULL, *v = NULL;
3544 :
3545 0 : loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3546 0 : if (!loc) {
3547 0 : Py_INCREF(Py_None);
3548 0 : loc = Py_None;
3549 : }
3550 0 : u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
3551 0 : c->u->u_col_offset, loc);
3552 0 : if (!u)
3553 0 : goto exit;
3554 0 : v = Py_BuildValue("(zO)", errstr, u);
3555 0 : if (!v)
3556 0 : goto exit;
3557 0 : PyErr_SetObject(PyExc_SyntaxError, v);
3558 : exit:
3559 0 : Py_DECREF(loc);
3560 0 : Py_XDECREF(u);
3561 0 : Py_XDECREF(v);
3562 0 : return 0;
3563 : }
3564 :
3565 : static int
3566 21 : compiler_handle_subscr(struct compiler *c, const char *kind,
3567 : expr_context_ty ctx)
3568 : {
3569 21 : int op = 0;
3570 :
3571 : /* XXX this code is duplicated */
3572 21 : switch (ctx) {
3573 : case AugLoad: /* fall through to Load */
3574 19 : case Load: op = BINARY_SUBSCR; break;
3575 : case AugStore:/* fall through to Store */
3576 2 : case Store: op = STORE_SUBSCR; break;
3577 0 : case Del: op = DELETE_SUBSCR; break;
3578 : case Param:
3579 0 : PyErr_Format(PyExc_SystemError,
3580 : "invalid %s kind %d in subscript\n",
3581 : kind, ctx);
3582 0 : return 0;
3583 : }
3584 21 : if (ctx == AugLoad) {
3585 0 : ADDOP(c, DUP_TOP_TWO);
3586 : }
3587 21 : else if (ctx == AugStore) {
3588 0 : ADDOP(c, ROT_THREE);
3589 : }
3590 21 : ADDOP(c, op);
3591 21 : return 1;
3592 : }
3593 :
3594 : static int
3595 4 : compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3596 : {
3597 4 : int n = 2;
3598 : assert(s->kind == Slice_kind);
3599 :
3600 : /* only handles the cases where BUILD_SLICE is emitted */
3601 4 : if (s->v.Slice.lower) {
3602 2 : VISIT(c, expr, s->v.Slice.lower);
3603 : }
3604 : else {
3605 2 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
3606 : }
3607 :
3608 4 : if (s->v.Slice.upper) {
3609 4 : VISIT(c, expr, s->v.Slice.upper);
3610 : }
3611 : else {
3612 0 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
3613 : }
3614 :
3615 4 : if (s->v.Slice.step) {
3616 0 : n++;
3617 0 : VISIT(c, expr, s->v.Slice.step);
3618 : }
3619 4 : ADDOP_I(c, BUILD_SLICE, n);
3620 4 : return 1;
3621 : }
3622 :
3623 : static int
3624 0 : compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3625 : expr_context_ty ctx)
3626 : {
3627 0 : switch (s->kind) {
3628 : case Slice_kind:
3629 0 : return compiler_slice(c, s, ctx);
3630 : case Index_kind:
3631 0 : VISIT(c, expr, s->v.Index.value);
3632 0 : break;
3633 : case ExtSlice_kind:
3634 : default:
3635 0 : PyErr_SetString(PyExc_SystemError,
3636 : "extended slice invalid in nested slice");
3637 0 : return 0;
3638 : }
3639 0 : return 1;
3640 : }
3641 :
3642 : static int
3643 21 : compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3644 : {
3645 21 : char * kindname = NULL;
3646 21 : switch (s->kind) {
3647 : case Index_kind:
3648 17 : kindname = "index";
3649 17 : if (ctx != AugStore) {
3650 17 : VISIT(c, expr, s->v.Index.value);
3651 : }
3652 17 : break;
3653 : case Slice_kind:
3654 4 : kindname = "slice";
3655 4 : if (ctx != AugStore) {
3656 4 : if (!compiler_slice(c, s, ctx))
3657 0 : return 0;
3658 : }
3659 4 : break;
3660 : case ExtSlice_kind:
3661 0 : kindname = "extended slice";
3662 0 : if (ctx != AugStore) {
3663 0 : int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3664 0 : for (i = 0; i < n; i++) {
3665 0 : slice_ty sub = (slice_ty)asdl_seq_GET(
3666 : s->v.ExtSlice.dims, i);
3667 0 : if (!compiler_visit_nested_slice(c, sub, ctx))
3668 0 : return 0;
3669 : }
3670 0 : ADDOP_I(c, BUILD_TUPLE, n);
3671 : }
3672 0 : break;
3673 : default:
3674 0 : PyErr_Format(PyExc_SystemError,
3675 0 : "invalid subscript kind %d", s->kind);
3676 0 : return 0;
3677 : }
3678 21 : return compiler_handle_subscr(c, kindname, ctx);
3679 : }
3680 :
3681 : /* End of the compiler section, beginning of the assembler section */
3682 :
3683 : /* do depth-first search of basic block graph, starting with block.
3684 : post records the block indices in post-order.
3685 :
3686 : XXX must handle implicit jumps from one block to next
3687 : */
3688 :
3689 : struct assembler {
3690 : PyObject *a_bytecode; /* string containing bytecode */
3691 : int a_offset; /* offset into bytecode */
3692 : int a_nblocks; /* number of reachable blocks */
3693 : basicblock **a_postorder; /* list of blocks in dfs postorder */
3694 : PyObject *a_lnotab; /* string containing lnotab */
3695 : int a_lnotab_off; /* offset into lnotab */
3696 : int a_lineno; /* last lineno of emitted instruction */
3697 : int a_lineno_off; /* bytecode offset of last lineno */
3698 : };
3699 :
3700 : static void
3701 163 : dfs(struct compiler *c, basicblock *b, struct assembler *a)
3702 : {
3703 : int i;
3704 163 : struct instr *instr = NULL;
3705 :
3706 163 : if (b->b_seen)
3707 227 : return;
3708 99 : b->b_seen = 1;
3709 99 : if (b->b_next != NULL)
3710 69 : dfs(c, b->b_next, a);
3711 1232 : for (i = 0; i < b->b_iused; i++) {
3712 1133 : instr = &b->b_instr[i];
3713 1133 : if (instr->i_jrel || instr->i_jabs)
3714 64 : dfs(c, instr->i_target, a);
3715 : }
3716 99 : a->a_postorder[a->a_nblocks++] = b;
3717 : }
3718 :
3719 : static int
3720 561 : stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3721 : {
3722 : int i, target_depth;
3723 : struct instr *instr;
3724 561 : if (b->b_seen || b->b_startdepth >= depth)
3725 181 : return maxdepth;
3726 380 : b->b_seen = 1;
3727 380 : b->b_startdepth = depth;
3728 3342 : for (i = 0; i < b->b_iused; i++) {
3729 3107 : instr = &b->b_instr[i];
3730 3107 : depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3731 3107 : if (depth > maxdepth)
3732 140 : maxdepth = depth;
3733 : assert(depth >= 0); /* invalid code or bug in stackdepth() */
3734 3107 : if (instr->i_jrel || instr->i_jabs) {
3735 341 : target_depth = depth;
3736 341 : if (instr->i_opcode == FOR_ITER) {
3737 2 : target_depth = depth-2;
3738 660 : } else if (instr->i_opcode == SETUP_FINALLY ||
3739 321 : instr->i_opcode == SETUP_EXCEPT) {
3740 39 : target_depth = depth+3;
3741 39 : if (target_depth > maxdepth)
3742 8 : maxdepth = target_depth;
3743 : }
3744 341 : maxdepth = stackdepth_walk(c, instr->i_target,
3745 : target_depth, maxdepth);
3746 671 : if (instr->i_opcode == JUMP_ABSOLUTE ||
3747 330 : instr->i_opcode == JUMP_FORWARD) {
3748 : goto out; /* remaining code is dead */
3749 : }
3750 : }
3751 : }
3752 235 : if (b->b_next)
3753 190 : maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3754 : out:
3755 380 : b->b_seen = 0;
3756 380 : return maxdepth;
3757 : }
3758 :
3759 : /* Find the flow path that needs the largest stack. We assume that
3760 : * cycles in the flow graph have no net effect on the stack depth.
3761 : */
3762 : static int
3763 30 : stackdepth(struct compiler *c)
3764 : {
3765 : basicblock *b, *entryblock;
3766 30 : entryblock = NULL;
3767 129 : for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3768 99 : b->b_seen = 0;
3769 99 : b->b_startdepth = INT_MIN;
3770 99 : entryblock = b;
3771 : }
3772 30 : if (!entryblock)
3773 0 : return 0;
3774 30 : return stackdepth_walk(c, entryblock, 0, 0);
3775 : }
3776 :
3777 : static int
3778 30 : assemble_init(struct assembler *a, int nblocks, int firstlineno)
3779 : {
3780 30 : memset(a, 0, sizeof(struct assembler));
3781 30 : a->a_lineno = firstlineno;
3782 30 : a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3783 30 : if (!a->a_bytecode)
3784 0 : return 0;
3785 30 : a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3786 30 : if (!a->a_lnotab)
3787 0 : return 0;
3788 30 : if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3789 0 : PyErr_NoMemory();
3790 0 : return 0;
3791 : }
3792 30 : a->a_postorder = (basicblock **)PyObject_Malloc(
3793 : sizeof(basicblock *) * nblocks);
3794 30 : if (!a->a_postorder) {
3795 0 : PyErr_NoMemory();
3796 0 : return 0;
3797 : }
3798 30 : return 1;
3799 : }
3800 :
3801 : static void
3802 30 : assemble_free(struct assembler *a)
3803 : {
3804 30 : Py_XDECREF(a->a_bytecode);
3805 30 : Py_XDECREF(a->a_lnotab);
3806 30 : if (a->a_postorder)
3807 30 : PyObject_Free(a->a_postorder);
3808 30 : }
3809 :
3810 : /* Return the size of a basic block in bytes. */
3811 :
3812 : static int
3813 3399 : instrsize(struct instr *instr)
3814 : {
3815 3399 : if (!instr->i_hasarg)
3816 459 : return 1; /* 1 byte for the opcode*/
3817 2940 : if (instr->i_oparg > 0xffff)
3818 0 : return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3819 2940 : return 3; /* 1 (opcode) + 2 (oparg) */
3820 : }
3821 :
3822 : static int
3823 99 : blocksize(basicblock *b)
3824 : {
3825 : int i;
3826 99 : int size = 0;
3827 :
3828 1232 : for (i = 0; i < b->b_iused; i++)
3829 1133 : size += instrsize(&b->b_instr[i]);
3830 99 : return size;
3831 : }
3832 :
3833 : /* Appends a pair to the end of the line number table, a_lnotab, representing
3834 : the instruction's bytecode offset and line number. See
3835 : Objects/lnotab_notes.txt for the description of the line number table. */
3836 :
3837 : static int
3838 186 : assemble_lnotab(struct assembler *a, struct instr *i)
3839 : {
3840 : int d_bytecode, d_lineno;
3841 : int len;
3842 : unsigned char *lnotab;
3843 :
3844 186 : d_bytecode = a->a_offset - a->a_lineno_off;
3845 186 : d_lineno = i->i_lineno - a->a_lineno;
3846 :
3847 : assert(d_bytecode >= 0);
3848 : assert(d_lineno >= 0);
3849 :
3850 186 : if(d_bytecode == 0 && d_lineno == 0)
3851 3 : return 1;
3852 :
3853 183 : if (d_bytecode > 255) {
3854 0 : int j, nbytes, ncodes = d_bytecode / 255;
3855 0 : nbytes = a->a_lnotab_off + 2 * ncodes;
3856 0 : len = PyBytes_GET_SIZE(a->a_lnotab);
3857 0 : if (nbytes >= len) {
3858 0 : if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3859 0 : len = nbytes;
3860 0 : else if (len <= INT_MAX / 2)
3861 0 : len *= 2;
3862 : else {
3863 0 : PyErr_NoMemory();
3864 0 : return 0;
3865 : }
3866 0 : if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3867 0 : return 0;
3868 : }
3869 0 : lnotab = (unsigned char *)
3870 0 : PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3871 0 : for (j = 0; j < ncodes; j++) {
3872 0 : *lnotab++ = 255;
3873 0 : *lnotab++ = 0;
3874 : }
3875 0 : d_bytecode -= ncodes * 255;
3876 0 : a->a_lnotab_off += ncodes * 2;
3877 : }
3878 : assert(d_bytecode <= 255);
3879 183 : if (d_lineno > 255) {
3880 0 : int j, nbytes, ncodes = d_lineno / 255;
3881 0 : nbytes = a->a_lnotab_off + 2 * ncodes;
3882 0 : len = PyBytes_GET_SIZE(a->a_lnotab);
3883 0 : if (nbytes >= len) {
3884 0 : if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3885 0 : len = nbytes;
3886 0 : else if (len <= INT_MAX / 2)
3887 0 : len *= 2;
3888 : else {
3889 0 : PyErr_NoMemory();
3890 0 : return 0;
3891 : }
3892 0 : if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3893 0 : return 0;
3894 : }
3895 0 : lnotab = (unsigned char *)
3896 0 : PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3897 0 : *lnotab++ = d_bytecode;
3898 0 : *lnotab++ = 255;
3899 0 : d_bytecode = 0;
3900 0 : for (j = 1; j < ncodes; j++) {
3901 0 : *lnotab++ = 0;
3902 0 : *lnotab++ = 255;
3903 : }
3904 0 : d_lineno -= ncodes * 255;
3905 0 : a->a_lnotab_off += ncodes * 2;
3906 : }
3907 :
3908 183 : len = PyBytes_GET_SIZE(a->a_lnotab);
3909 183 : if (a->a_lnotab_off + 2 >= len) {
3910 10 : if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3911 0 : return 0;
3912 : }
3913 183 : lnotab = (unsigned char *)
3914 183 : PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3915 :
3916 183 : a->a_lnotab_off += 2;
3917 183 : if (d_bytecode) {
3918 159 : *lnotab++ = d_bytecode;
3919 159 : *lnotab++ = d_lineno;
3920 : }
3921 : else { /* First line of a block; def stmt, etc. */
3922 24 : *lnotab++ = 0;
3923 24 : *lnotab++ = d_lineno;
3924 : }
3925 183 : a->a_lineno = i->i_lineno;
3926 183 : a->a_lineno_off = a->a_offset;
3927 183 : return 1;
3928 : }
3929 :
3930 : /* assemble_emit()
3931 : Extend the bytecode with a new instruction.
3932 : Update lnotab if necessary.
3933 : */
3934 :
3935 : static int
3936 1133 : assemble_emit(struct assembler *a, struct instr *i)
3937 : {
3938 1133 : int size, arg = 0, ext = 0;
3939 1133 : Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3940 : char *code;
3941 :
3942 1133 : size = instrsize(i);
3943 1133 : if (i->i_hasarg) {
3944 980 : arg = i->i_oparg;
3945 980 : ext = arg >> 16;
3946 : }
3947 1133 : if (i->i_lineno && !assemble_lnotab(a, i))
3948 0 : return 0;
3949 1133 : if (a->a_offset + size >= len) {
3950 11 : if (len > PY_SSIZE_T_MAX / 2)
3951 0 : return 0;
3952 11 : if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3953 0 : return 0;
3954 : }
3955 1133 : code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3956 1133 : a->a_offset += size;
3957 1133 : if (size == 6) {
3958 : assert(i->i_hasarg);
3959 0 : *code++ = (char)EXTENDED_ARG;
3960 0 : *code++ = ext & 0xff;
3961 0 : *code++ = ext >> 8;
3962 0 : arg &= 0xffff;
3963 : }
3964 1133 : *code++ = i->i_opcode;
3965 1133 : if (i->i_hasarg) {
3966 : assert(size == 3 || size == 6);
3967 980 : *code++ = arg & 0xff;
3968 980 : *code++ = arg >> 8;
3969 : }
3970 1133 : return 1;
3971 : }
3972 :
3973 : static void
3974 30 : assemble_jump_offsets(struct assembler *a, struct compiler *c)
3975 : {
3976 : basicblock *b;
3977 30 : int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3978 : int i;
3979 :
3980 : /* Compute the size of each block and fixup jump args.
3981 : Replace block pointer with position in bytecode. */
3982 : do {
3983 30 : totsize = 0;
3984 129 : for (i = a->a_nblocks - 1; i >= 0; i--) {
3985 99 : b = a->a_postorder[i];
3986 99 : bsize = blocksize(b);
3987 99 : b->b_offset = totsize;
3988 99 : totsize += bsize;
3989 : }
3990 30 : last_extended_arg_count = extended_arg_count;
3991 30 : extended_arg_count = 0;
3992 129 : for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3993 99 : bsize = b->b_offset;
3994 1232 : for (i = 0; i < b->b_iused; i++) {
3995 1133 : struct instr *instr = &b->b_instr[i];
3996 : /* Relative jumps are computed relative to
3997 : the instruction pointer after fetching
3998 : the jump instruction.
3999 : */
4000 1133 : bsize += instrsize(instr);
4001 1133 : if (instr->i_jabs)
4002 27 : instr->i_oparg = instr->i_target->b_offset;
4003 1106 : else if (instr->i_jrel) {
4004 37 : int delta = instr->i_target->b_offset - bsize;
4005 37 : instr->i_oparg = delta;
4006 : }
4007 : else
4008 1069 : continue;
4009 64 : if (instr->i_oparg > 0xffff)
4010 0 : extended_arg_count++;
4011 : }
4012 : }
4013 :
4014 : /* XXX: This is an awful hack that could hurt performance, but
4015 : on the bright side it should work until we come up
4016 : with a better solution.
4017 :
4018 : The issue is that in the first loop blocksize() is called
4019 : which calls instrsize() which requires i_oparg be set
4020 : appropriately. There is a bootstrap problem because
4021 : i_oparg is calculated in the second loop above.
4022 :
4023 : So we loop until we stop seeing new EXTENDED_ARGs.
4024 : The only EXTENDED_ARGs that could be popping up are
4025 : ones in jump instructions. So this should converge
4026 : fairly quickly.
4027 : */
4028 30 : } while (last_extended_arg_count != extended_arg_count);
4029 30 : }
4030 :
4031 : static PyObject *
4032 150 : dict_keys_inorder(PyObject *dict, int offset)
4033 : {
4034 : PyObject *tuple, *k, *v;
4035 150 : Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
4036 :
4037 150 : tuple = PyTuple_New(size);
4038 150 : if (tuple == NULL)
4039 0 : return NULL;
4040 770 : while (PyDict_Next(dict, &pos, &k, &v)) {
4041 470 : i = PyLong_AS_LONG(v);
4042 : /* The keys of the dictionary are tuples. (see compiler_add_o)
4043 : The object we want is always first, though. */
4044 470 : k = PyTuple_GET_ITEM(k, 0);
4045 470 : Py_INCREF(k);
4046 : assert((i - offset) < size);
4047 : assert((i - offset) >= 0);
4048 470 : PyTuple_SET_ITEM(tuple, i - offset, k);
4049 : }
4050 150 : return tuple;
4051 : }
4052 :
4053 : static int
4054 30 : compute_code_flags(struct compiler *c)
4055 : {
4056 30 : PySTEntryObject *ste = c->u->u_ste;
4057 30 : int flags = 0, n;
4058 30 : if (ste->ste_type != ModuleBlock)
4059 27 : flags |= CO_NEWLOCALS;
4060 30 : if (ste->ste_type == FunctionBlock) {
4061 24 : if (!ste->ste_unoptimized)
4062 24 : flags |= CO_OPTIMIZED;
4063 24 : if (ste->ste_nested)
4064 0 : flags |= CO_NESTED;
4065 24 : if (ste->ste_generator)
4066 0 : flags |= CO_GENERATOR;
4067 24 : if (ste->ste_varargs)
4068 1 : flags |= CO_VARARGS;
4069 24 : if (ste->ste_varkeywords)
4070 2 : flags |= CO_VARKEYWORDS;
4071 : }
4072 :
4073 : /* (Only) inherit compilerflags in PyCF_MASK */
4074 30 : flags |= (c->c_flags->cf_flags & PyCF_MASK);
4075 :
4076 30 : n = PyDict_Size(c->u->u_freevars);
4077 30 : if (n < 0)
4078 0 : return -1;
4079 30 : if (n == 0) {
4080 30 : n = PyDict_Size(c->u->u_cellvars);
4081 30 : if (n < 0)
4082 0 : return -1;
4083 30 : if (n == 0) {
4084 30 : flags |= CO_NOFREE;
4085 : }
4086 : }
4087 :
4088 30 : return flags;
4089 : }
4090 :
4091 : static PyCodeObject *
4092 30 : makecode(struct compiler *c, struct assembler *a)
4093 : {
4094 : PyObject *tmp;
4095 30 : PyCodeObject *co = NULL;
4096 30 : PyObject *consts = NULL;
4097 30 : PyObject *names = NULL;
4098 30 : PyObject *varnames = NULL;
4099 30 : PyObject *name = NULL;
4100 30 : PyObject *freevars = NULL;
4101 30 : PyObject *cellvars = NULL;
4102 30 : PyObject *bytecode = NULL;
4103 : int nlocals, flags;
4104 :
4105 30 : tmp = dict_keys_inorder(c->u->u_consts, 0);
4106 30 : if (!tmp)
4107 0 : goto error;
4108 30 : consts = PySequence_List(tmp); /* optimize_code requires a list */
4109 30 : Py_DECREF(tmp);
4110 :
4111 30 : names = dict_keys_inorder(c->u->u_names, 0);
4112 30 : varnames = dict_keys_inorder(c->u->u_varnames, 0);
4113 30 : if (!consts || !names || !varnames)
4114 : goto error;
4115 :
4116 30 : cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4117 30 : if (!cellvars)
4118 0 : goto error;
4119 30 : freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4120 30 : if (!freevars)
4121 0 : goto error;
4122 30 : nlocals = PyDict_Size(c->u->u_varnames);
4123 30 : flags = compute_code_flags(c);
4124 30 : if (flags < 0)
4125 0 : goto error;
4126 :
4127 30 : bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4128 30 : if (!bytecode)
4129 0 : goto error;
4130 :
4131 30 : tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4132 30 : if (!tmp)
4133 0 : goto error;
4134 30 : Py_DECREF(consts);
4135 30 : consts = tmp;
4136 :
4137 90 : co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4138 : nlocals, stackdepth(c), flags,
4139 : bytecode, consts, names, varnames,
4140 : freevars, cellvars,
4141 30 : c->c_filename_obj, c->u->u_name,
4142 30 : c->u->u_firstlineno,
4143 : a->a_lnotab);
4144 : error:
4145 30 : Py_XDECREF(consts);
4146 30 : Py_XDECREF(names);
4147 30 : Py_XDECREF(varnames);
4148 30 : Py_XDECREF(name);
4149 30 : Py_XDECREF(freevars);
4150 30 : Py_XDECREF(cellvars);
4151 30 : Py_XDECREF(bytecode);
4152 30 : return co;
4153 : }
4154 :
4155 :
4156 : /* For debugging purposes only */
4157 : #if 0
4158 : static void
4159 : dump_instr(const struct instr *i)
4160 : {
4161 : const char *jrel = i->i_jrel ? "jrel " : "";
4162 : const char *jabs = i->i_jabs ? "jabs " : "";
4163 : char arg[128];
4164 :
4165 : *arg = '\0';
4166 : if (i->i_hasarg)
4167 : sprintf(arg, "arg: %d ", i->i_oparg);
4168 :
4169 : fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4170 : i->i_lineno, i->i_opcode, arg, jabs, jrel);
4171 : }
4172 :
4173 : static void
4174 : dump_basicblock(const basicblock *b)
4175 : {
4176 : const char *seen = b->b_seen ? "seen " : "";
4177 : const char *b_return = b->b_return ? "return " : "";
4178 : fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4179 : b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4180 : if (b->b_instr) {
4181 : int i;
4182 : for (i = 0; i < b->b_iused; i++) {
4183 : fprintf(stderr, " [%02d] ", i);
4184 : dump_instr(b->b_instr + i);
4185 : }
4186 : }
4187 : }
4188 : #endif
4189 :
4190 : static PyCodeObject *
4191 30 : assemble(struct compiler *c, int addNone)
4192 : {
4193 : basicblock *b, *entryblock;
4194 : struct assembler a;
4195 : int i, j, nblocks;
4196 30 : PyCodeObject *co = NULL;
4197 :
4198 : /* Make sure every block that falls off the end returns None.
4199 : XXX NEXT_BLOCK() isn't quite right, because if the last
4200 : block ends with a jump or return b_next shouldn't set.
4201 : */
4202 30 : if (!c->u->u_curblock->b_return) {
4203 6 : NEXT_BLOCK(c);
4204 6 : if (addNone)
4205 6 : ADDOP_O(c, LOAD_CONST, Py_None, consts);
4206 6 : ADDOP(c, RETURN_VALUE);
4207 : }
4208 :
4209 30 : nblocks = 0;
4210 30 : entryblock = NULL;
4211 129 : for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4212 99 : nblocks++;
4213 99 : entryblock = b;
4214 : }
4215 :
4216 : /* Set firstlineno if it wasn't explicitly set. */
4217 30 : if (!c->u->u_firstlineno) {
4218 3 : if (entryblock && entryblock->b_instr)
4219 3 : c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4220 : else
4221 0 : c->u->u_firstlineno = 1;
4222 : }
4223 30 : if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4224 0 : goto error;
4225 30 : dfs(c, entryblock, &a);
4226 :
4227 : /* Can't modify the bytecode after computing jump offsets. */
4228 30 : assemble_jump_offsets(&a, c);
4229 :
4230 : /* Emit code in reverse postorder from dfs. */
4231 129 : for (i = a.a_nblocks - 1; i >= 0; i--) {
4232 99 : b = a.a_postorder[i];
4233 1232 : for (j = 0; j < b->b_iused; j++)
4234 1133 : if (!assemble_emit(&a, &b->b_instr[j]))
4235 0 : goto error;
4236 : }
4237 :
4238 30 : if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4239 0 : goto error;
4240 30 : if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4241 0 : goto error;
4242 :
4243 30 : co = makecode(c, &a);
4244 : error:
4245 30 : assemble_free(&a);
4246 30 : return co;
4247 : }
4248 :
4249 : #undef PyAST_Compile
4250 : PyAPI_FUNC(PyCodeObject *)
4251 0 : PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4252 : PyArena *arena)
4253 : {
4254 0 : return PyAST_CompileEx(mod, filename, flags, -1, arena);
4255 : }
4256 :
4257 :
|