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#include "wordcode_helpers.h"
33
34#define DEFAULT_BLOCK_SIZE 16
35#define DEFAULT_BLOCKS 8
36#define DEFAULT_CODE_SIZE 128
37#define DEFAULT_LNOTAB_SIZE 16
38
39#define COMP_GENEXP   0
40#define COMP_LISTCOMP 1
41#define COMP_SETCOMP  2
42#define COMP_DICTCOMP 3
43
44struct instr {
45    unsigned i_jabs : 1;
46    unsigned i_jrel : 1;
47    unsigned char i_opcode;
48    int i_oparg;
49    struct basicblock_ *i_target; /* target block (if jump instruction) */
50    int i_lineno;
51};
52
53typedef struct basicblock_ {
54    /* Each basicblock in a compilation unit is linked via b_list in the
55       reverse order that the block are allocated.  b_list points to the next
56       block, not to be confused with b_next, which is next by control flow. */
57    struct basicblock_ *b_list;
58    /* number of instructions used */
59    int b_iused;
60    /* length of instruction array (b_instr) */
61    int b_ialloc;
62    /* pointer to an array of instructions, initially NULL */
63    struct instr *b_instr;
64    /* If b_next is non-NULL, it is a pointer to the next
65       block reached by normal control flow. */
66    struct basicblock_ *b_next;
67    /* b_seen is used to perform a DFS of basicblocks. */
68    unsigned b_seen : 1;
69    /* b_return is true if a RETURN_VALUE opcode is inserted. */
70    unsigned b_return : 1;
71    /* depth of stack upon entry of block, computed by stackdepth() */
72    int b_startdepth;
73    /* instruction offset for block, computed by assemble_jump_offsets() */
74    int b_offset;
75} basicblock;
76
77/* fblockinfo tracks the current frame block.
78
79A frame block is used to handle loops, try/except, and try/finally.
80It's called a frame block to distinguish it from a basic block in the
81compiler IR.
82*/
83
84enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85
86struct fblockinfo {
87    enum fblocktype fb_type;
88    basicblock *fb_block;
89};
90
91enum {
92    COMPILER_SCOPE_MODULE,
93    COMPILER_SCOPE_CLASS,
94    COMPILER_SCOPE_FUNCTION,
95    COMPILER_SCOPE_ASYNC_FUNCTION,
96    COMPILER_SCOPE_LAMBDA,
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*/
103struct 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    Py_ssize_t u_argcount;        /* number of arguments for block */
123    Py_ssize_t 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
141The u pointer points to the current compilation unit, while units
142for enclosing blocks are stored in c_stack.     The u and c_stack are
143managed by compiler_enter_scope() and compiler_exit_scope().
144
145Note that we don't track recursion levels during compilation - the
146task of detecting and rejecting excessive levels of nesting is
147handled by the symbol analysis pass.
148
149*/
150
151struct compiler {
152    PyObject *c_filename;
153    struct symtable *c_st;
154    PyFutureFeatures *c_future; /* pointer to module's __future__ */
155    PyCompilerFlags *c_flags;
156
157    int c_optimize;              /* optimization level */
158    int c_interactive;           /* true if in interactive mode */
159    int c_nestlevel;
160
161    struct compiler_unit *u; /* compiler state for current block */
162    PyObject *c_stack;           /* Python list holding compiler_unit ptrs */
163    PyArena *c_arena;            /* pointer to memory allocation arena */
164};
165
166static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
167static void compiler_free(struct compiler *);
168static basicblock *compiler_new_block(struct compiler *);
169static int compiler_next_instr(struct compiler *, basicblock *);
170static int compiler_addop(struct compiler *, int);
171static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
172static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
173static int compiler_addop_j(struct compiler *, int, basicblock *, int);
174static int compiler_error(struct compiler *, const char *);
175static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176
177static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178static int compiler_visit_stmt(struct compiler *, stmt_ty);
179static int compiler_visit_keyword(struct compiler *, keyword_ty);
180static int compiler_visit_expr(struct compiler *, expr_ty);
181static int compiler_augassign(struct compiler *, stmt_ty);
182static int compiler_annassign(struct compiler *, stmt_ty);
183static int compiler_visit_slice(struct compiler *, slice_ty,
184                                expr_context_ty);
185
186static int compiler_push_fblock(struct compiler *, enum fblocktype,
187                                basicblock *);
188static void compiler_pop_fblock(struct compiler *, enum fblocktype,
189                                basicblock *);
190/* Returns true if there is a loop on the fblock stack. */
191static int compiler_in_loop(struct compiler *);
192
193static int inplace_binop(struct compiler *, operator_ty);
194static int expr_constant(struct compiler *, expr_ty);
195
196static int compiler_with(struct compiler *, stmt_ty, int);
197static int compiler_async_with(struct compiler *, stmt_ty, int);
198static int compiler_async_for(struct compiler *, stmt_ty);
199static int compiler_call_helper(struct compiler *c, int n,
200                                asdl_seq *args,
201                                asdl_seq *keywords);
202static int compiler_try_except(struct compiler *, stmt_ty);
203static int compiler_set_qualname(struct compiler *);
204
205static int compiler_sync_comprehension_generator(
206                                      struct compiler *c,
207                                      asdl_seq *generators, int gen_index,
208                                      expr_ty elt, expr_ty val, int type);
209
210static int compiler_async_comprehension_generator(
211                                      struct compiler *c,
212                                      asdl_seq *generators, int gen_index,
213                                      expr_ty elt, expr_ty val, int type);
214
215static PyCodeObject *assemble(struct compiler *, int addNone);
216static PyObject *__doc__;
217
218#define CAPSULE_NAME "compile.c compiler unit"
219
220PyObject *
221_Py_Mangle(PyObject *privateobj, PyObject *ident)
222{
223    /* Name mangling: __private becomes _classname__private.
224       This is independent from how the name is used. */
225    PyObject *result;
226    size_t nlen, plen, ipriv;
227    Py_UCS4 maxchar;
228    if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
229        PyUnicode_READ_CHAR(ident, 0) != '_' ||
230        PyUnicode_READ_CHAR(ident, 1) != '_') {
231        Py_INCREF(ident);
232        return ident;
233    }
234    nlen = PyUnicode_GET_LENGTH(ident);
235    plen = PyUnicode_GET_LENGTH(privateobj);
236    /* Don't mangle __id__ or names with dots.
237
238       The only time a name with a dot can occur is when
239       we are compiling an import statement that has a
240       package name.
241
242       TODO(jhylton): Decide whether we want to support
243       mangling of the module name, e.g. __M.X.
244    */
245    if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
246         PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
247        PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
248        Py_INCREF(ident);
249        return ident; /* Don't mangle __whatever__ */
250    }
251    /* Strip leading underscores from class name */
252    ipriv = 0;
253    while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
254        ipriv++;
255    if (ipriv == plen) {
256        Py_INCREF(ident);
257        return ident; /* Don't mangle if class is just underscores */
258    }
259    plen -= ipriv;
260
261    if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
262        PyErr_SetString(PyExc_OverflowError,
263                        "private identifier too large to be mangled");
264        return NULL;
265    }
266
267    maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
268    if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
269        maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
270
271    result = PyUnicode_New(1 + nlen + plen, maxchar);
272    if (!result)
273        return 0;
274    /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
275    PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
276    if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
277        Py_DECREF(result);
278        return NULL;
279    }
280    if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
281        Py_DECREF(result);
282        return NULL;
283    }
284    assert(_PyUnicode_CheckConsistency(result, 1));
285    return result;
286}
287
288static int
289compiler_init(struct compiler *c)
290{
291    memset(c, 0, sizeof(struct compiler));
292
293    c->c_stack = PyList_New(0);
294    if (!c->c_stack)
295        return 0;
296
297    return 1;
298}
299
300PyCodeObject *
301PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
302                   int optimize, PyArena *arena)
303{
304    struct compiler c;
305    PyCodeObject *co = NULL;
306    PyCompilerFlags local_flags;
307    int merged;
308
309    if (!__doc__) {
310        __doc__ = PyUnicode_InternFromString("__doc__");
311        if (!__doc__)
312            return NULL;
313    }
314
315    if (!compiler_init(&c))
316        return NULL;
317    Py_INCREF(filename);
318    c.c_filename = filename;
319    c.c_arena = arena;
320    c.c_future = PyFuture_FromASTObject(mod, filename);
321    if (c.c_future == NULL)
322        goto finally;
323    if (!flags) {
324        local_flags.cf_flags = 0;
325        flags = &local_flags;
326    }
327    merged = c.c_future->ff_features | flags->cf_flags;
328    c.c_future->ff_features = merged;
329    flags->cf_flags = merged;
330    c.c_flags = flags;
331    c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
332    c.c_nestlevel = 0;
333
334    c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
335    if (c.c_st == NULL) {
336        if (!PyErr_Occurred())
337            PyErr_SetString(PyExc_SystemError, "no symtable");
338        goto finally;
339    }
340
341    co = compiler_mod(&c, mod);
342
343 finally:
344    compiler_free(&c);
345    assert(co || PyErr_Occurred());
346    return co;
347}
348
349PyCodeObject *
350PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
351                int optimize, PyArena *arena)
352{
353    PyObject *filename;
354    PyCodeObject *co;
355    filename = PyUnicode_DecodeFSDefault(filename_str);
356    if (filename == NULL)
357        return NULL;
358    co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
359    Py_DECREF(filename);
360    return co;
361
362}
363
364PyCodeObject *
365PyNode_Compile(struct _node *n, const char *filename)
366{
367    PyCodeObject *co = NULL;
368    mod_ty mod;
369    PyArena *arena = PyArena_New();
370    if (!arena)
371        return NULL;
372    mod = PyAST_FromNode(n, NULL, filename, arena);
373    if (mod)
374        co = PyAST_Compile(mod, filename, NULL, arena);
375    PyArena_Free(arena);
376    return co;
377}
378
379static void
380compiler_free(struct compiler *c)
381{
382    if (c->c_st)
383        PySymtable_Free(c->c_st);
384    if (c->c_future)
385        PyObject_Free(c->c_future);
386    Py_XDECREF(c->c_filename);
387    Py_DECREF(c->c_stack);
388}
389
390static PyObject *
391list2dict(PyObject *list)
392{
393    Py_ssize_t i, n;
394    PyObject *v, *k;
395    PyObject *dict = PyDict_New();
396    if (!dict) return NULL;
397
398    n = PyList_Size(list);
399    for (i = 0; i < n; i++) {
400        v = PyLong_FromSsize_t(i);
401        if (!v) {
402            Py_DECREF(dict);
403            return NULL;
404        }
405        k = PyList_GET_ITEM(list, i);
406        k = _PyCode_ConstantKey(k);
407        if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
408            Py_XDECREF(k);
409            Py_DECREF(v);
410            Py_DECREF(dict);
411            return NULL;
412        }
413        Py_DECREF(k);
414        Py_DECREF(v);
415    }
416    return dict;
417}
418
419/* Return new dict containing names from src that match scope(s).
420
421src is a symbol table dictionary.  If the scope of a name matches
422either scope_type or flag is set, insert it into the new dict.  The
423values are integers, starting at offset and increasing by one for
424each key.
425*/
426
427static PyObject *
428dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
429{
430    Py_ssize_t i = offset, scope, num_keys, key_i;
431    PyObject *k, *v, *dest = PyDict_New();
432    PyObject *sorted_keys;
433
434    assert(offset >= 0);
435    if (dest == NULL)
436        return NULL;
437
438    /* Sort the keys so that we have a deterministic order on the indexes
439       saved in the returned dictionary.  These indexes are used as indexes
440       into the free and cell var storage.  Therefore if they aren't
441       deterministic, then the generated bytecode is not deterministic.
442    */
443    sorted_keys = PyDict_Keys(src);
444    if (sorted_keys == NULL)
445        return NULL;
446    if (PyList_Sort(sorted_keys) != 0) {
447        Py_DECREF(sorted_keys);
448        return NULL;
449    }
450    num_keys = PyList_GET_SIZE(sorted_keys);
451
452    for (key_i = 0; key_i < num_keys; key_i++) {
453        /* XXX this should probably be a macro in symtable.h */
454        long vi;
455        k = PyList_GET_ITEM(sorted_keys, key_i);
456        v = PyDict_GetItem(src, k);
457        assert(PyLong_Check(v));
458        vi = PyLong_AS_LONG(v);
459        scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
460
461        if (scope == scope_type || vi & flag) {
462            PyObject *tuple, *item = PyLong_FromSsize_t(i);
463            if (item == NULL) {
464                Py_DECREF(sorted_keys);
465                Py_DECREF(dest);
466                return NULL;
467            }
468            i++;
469            tuple = _PyCode_ConstantKey(k);
470            if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
471                Py_DECREF(sorted_keys);
472                Py_DECREF(item);
473                Py_DECREF(dest);
474                Py_XDECREF(tuple);
475                return NULL;
476            }
477            Py_DECREF(item);
478            Py_DECREF(tuple);
479        }
480    }
481    Py_DECREF(sorted_keys);
482    return dest;
483}
484
485static void
486compiler_unit_check(struct compiler_unit *u)
487{
488    basicblock *block;
489    for (block = u->u_blocks; block != NULL; block = block->b_list) {
490        assert((uintptr_t)block != 0xcbcbcbcbU);
491        assert((uintptr_t)block != 0xfbfbfbfbU);
492        assert((uintptr_t)block != 0xdbdbdbdbU);
493        if (block->b_instr != NULL) {
494            assert(block->b_ialloc > 0);
495            assert(block->b_iused > 0);
496            assert(block->b_ialloc >= block->b_iused);
497        }
498        else {
499            assert (block->b_iused == 0);
500            assert (block->b_ialloc == 0);
501        }
502    }
503}
504
505static void
506compiler_unit_free(struct compiler_unit *u)
507{
508    basicblock *b, *next;
509
510    compiler_unit_check(u);
511    b = u->u_blocks;
512    while (b != NULL) {
513        if (b->b_instr)
514            PyObject_Free((void *)b->b_instr);
515        next = b->b_list;
516        PyObject_Free((void *)b);
517        b = next;
518    }
519    Py_CLEAR(u->u_ste);
520    Py_CLEAR(u->u_name);
521    Py_CLEAR(u->u_qualname);
522    Py_CLEAR(u->u_consts);
523    Py_CLEAR(u->u_names);
524    Py_CLEAR(u->u_varnames);
525    Py_CLEAR(u->u_freevars);
526    Py_CLEAR(u->u_cellvars);
527    Py_CLEAR(u->u_private);
528    PyObject_Free(u);
529}
530
531static int
532compiler_enter_scope(struct compiler *c, identifier name,
533                     int scope_type, void *key, int lineno)
534{
535    struct compiler_unit *u;
536    basicblock *block;
537
538    u = (struct compiler_unit *)PyObject_Malloc(sizeof(
539                                            struct compiler_unit));
540    if (!u) {
541        PyErr_NoMemory();
542        return 0;
543    }
544    memset(u, 0, sizeof(struct compiler_unit));
545    u->u_scope_type = scope_type;
546    u->u_argcount = 0;
547    u->u_kwonlyargcount = 0;
548    u->u_ste = PySymtable_Lookup(c->c_st, key);
549    if (!u->u_ste) {
550        compiler_unit_free(u);
551        return 0;
552    }
553    Py_INCREF(name);
554    u->u_name = name;
555    u->u_varnames = list2dict(u->u_ste->ste_varnames);
556    u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
557    if (!u->u_varnames || !u->u_cellvars) {
558        compiler_unit_free(u);
559        return 0;
560    }
561    if (u->u_ste->ste_needs_class_closure) {
562        /* Cook up an implicit __class__ cell. */
563        _Py_IDENTIFIER(__class__);
564        PyObject *tuple, *name, *zero;
565        int res;
566        assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
567        assert(PyDict_Size(u->u_cellvars) == 0);
568        name = _PyUnicode_FromId(&PyId___class__);
569        if (!name) {
570            compiler_unit_free(u);
571            return 0;
572        }
573        tuple = _PyCode_ConstantKey(name);
574        if (!tuple) {
575            compiler_unit_free(u);
576            return 0;
577        }
578        zero = PyLong_FromLong(0);
579        if (!zero) {
580            Py_DECREF(tuple);
581            compiler_unit_free(u);
582            return 0;
583        }
584        res = PyDict_SetItem(u->u_cellvars, tuple, zero);
585        Py_DECREF(tuple);
586        Py_DECREF(zero);
587        if (res < 0) {
588            compiler_unit_free(u);
589            return 0;
590        }
591    }
592
593    u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
594                               PyDict_Size(u->u_cellvars));
595    if (!u->u_freevars) {
596        compiler_unit_free(u);
597        return 0;
598    }
599
600    u->u_blocks = NULL;
601    u->u_nfblocks = 0;
602    u->u_firstlineno = lineno;
603    u->u_lineno = 0;
604    u->u_col_offset = 0;
605    u->u_lineno_set = 0;
606    u->u_consts = PyDict_New();
607    if (!u->u_consts) {
608        compiler_unit_free(u);
609        return 0;
610    }
611    u->u_names = PyDict_New();
612    if (!u->u_names) {
613        compiler_unit_free(u);
614        return 0;
615    }
616
617    u->u_private = NULL;
618
619    /* Push the old compiler_unit on the stack. */
620    if (c->u) {
621        PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
622        if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
623            Py_XDECREF(capsule);
624            compiler_unit_free(u);
625            return 0;
626        }
627        Py_DECREF(capsule);
628        u->u_private = c->u->u_private;
629        Py_XINCREF(u->u_private);
630    }
631    c->u = u;
632
633    c->c_nestlevel++;
634
635    block = compiler_new_block(c);
636    if (block == NULL)
637        return 0;
638    c->u->u_curblock = block;
639
640    if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
641        if (!compiler_set_qualname(c))
642            return 0;
643    }
644
645    return 1;
646}
647
648static void
649compiler_exit_scope(struct compiler *c)
650{
651    Py_ssize_t n;
652    PyObject *capsule;
653
654    c->c_nestlevel--;
655    compiler_unit_free(c->u);
656    /* Restore c->u to the parent unit. */
657    n = PyList_GET_SIZE(c->c_stack) - 1;
658    if (n >= 0) {
659        capsule = PyList_GET_ITEM(c->c_stack, n);
660        c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
661        assert(c->u);
662        /* we are deleting from a list so this really shouldn't fail */
663        if (PySequence_DelItem(c->c_stack, n) < 0)
664            Py_FatalError("compiler_exit_scope()");
665        compiler_unit_check(c->u);
666    }
667    else
668        c->u = NULL;
669
670}
671
672static int
673compiler_set_qualname(struct compiler *c)
674{
675    _Py_static_string(dot, ".");
676    _Py_static_string(dot_locals, ".<locals>");
677    Py_ssize_t stack_size;
678    struct compiler_unit *u = c->u;
679    PyObject *name, *base, *dot_str, *dot_locals_str;
680
681    base = NULL;
682    stack_size = PyList_GET_SIZE(c->c_stack);
683    assert(stack_size >= 1);
684    if (stack_size > 1) {
685        int scope, force_global = 0;
686        struct compiler_unit *parent;
687        PyObject *mangled, *capsule;
688
689        capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
690        parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
691        assert(parent);
692
693        if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
694            || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
695            || u->u_scope_type == COMPILER_SCOPE_CLASS) {
696            assert(u->u_name);
697            mangled = _Py_Mangle(parent->u_private, u->u_name);
698            if (!mangled)
699                return 0;
700            scope = PyST_GetScope(parent->u_ste, mangled);
701            Py_DECREF(mangled);
702            assert(scope != GLOBAL_IMPLICIT);
703            if (scope == GLOBAL_EXPLICIT)
704                force_global = 1;
705        }
706
707        if (!force_global) {
708            if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
709                || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
710                || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
711                dot_locals_str = _PyUnicode_FromId(&dot_locals);
712                if (dot_locals_str == NULL)
713                    return 0;
714                base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
715                if (base == NULL)
716                    return 0;
717            }
718            else {
719                Py_INCREF(parent->u_qualname);
720                base = parent->u_qualname;
721            }
722        }
723    }
724
725    if (base != NULL) {
726        dot_str = _PyUnicode_FromId(&dot);
727        if (dot_str == NULL) {
728            Py_DECREF(base);
729            return 0;
730        }
731        name = PyUnicode_Concat(base, dot_str);
732        Py_DECREF(base);
733        if (name == NULL)
734            return 0;
735        PyUnicode_Append(&name, u->u_name);
736        if (name == NULL)
737            return 0;
738    }
739    else {
740        Py_INCREF(u->u_name);
741        name = u->u_name;
742    }
743    u->u_qualname = name;
744
745    return 1;
746}
747
748
749/* Allocate a new block and return a pointer to it.
750   Returns NULL on error.
751*/
752
753static basicblock *
754compiler_new_block(struct compiler *c)
755{
756    basicblock *b;
757    struct compiler_unit *u;
758
759    u = c->u;
760    b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
761    if (b == NULL) {
762        PyErr_NoMemory();
763        return NULL;
764    }
765    memset((void *)b, 0, sizeof(basicblock));
766    /* Extend the singly linked list of blocks with new block. */
767    b->b_list = u->u_blocks;
768    u->u_blocks = b;
769    return b;
770}
771
772static basicblock *
773compiler_next_block(struct compiler *c)
774{
775    basicblock *block = compiler_new_block(c);
776    if (block == NULL)
777        return NULL;
778    c->u->u_curblock->b_next = block;
779    c->u->u_curblock = block;
780    return block;
781}
782
783static basicblock *
784compiler_use_next_block(struct compiler *c, basicblock *block)
785{
786    assert(block != NULL);
787    c->u->u_curblock->b_next = block;
788    c->u->u_curblock = block;
789    return block;
790}
791
792/* Returns the offset of the next instruction in the current block's
793   b_instr array.  Resizes the b_instr as necessary.
794   Returns -1 on failure.
795*/
796
797static int
798compiler_next_instr(struct compiler *c, basicblock *b)
799{
800    assert(b != NULL);
801    if (b->b_instr == NULL) {
802        b->b_instr = (struct instr *)PyObject_Malloc(
803                         sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
804        if (b->b_instr == NULL) {
805            PyErr_NoMemory();
806            return -1;
807        }
808        b->b_ialloc = DEFAULT_BLOCK_SIZE;
809        memset((char *)b->b_instr, 0,
810               sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
811    }
812    else if (b->b_iused == b->b_ialloc) {
813        struct instr *tmp;
814        size_t oldsize, newsize;
815        oldsize = b->b_ialloc * sizeof(struct instr);
816        newsize = oldsize << 1;
817
818        if (oldsize > (SIZE_MAX >> 1)) {
819            PyErr_NoMemory();
820            return -1;
821        }
822
823        if (newsize == 0) {
824            PyErr_NoMemory();
825            return -1;
826        }
827        b->b_ialloc <<= 1;
828        tmp = (struct instr *)PyObject_Realloc(
829                                        (void *)b->b_instr, newsize);
830        if (tmp == NULL) {
831            PyErr_NoMemory();
832            return -1;
833        }
834        b->b_instr = tmp;
835        memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
836    }
837    return b->b_iused++;
838}
839
840/* Set the i_lineno member of the instruction at offset off if the
841   line number for the current expression/statement has not
842   already been set.  If it has been set, the call has no effect.
843
844   The line number is reset in the following cases:
845   - when entering a new scope
846   - on each statement
847   - on each expression that start a new line
848   - before the "except" clause
849   - before the "for" and "while" expressions
850*/
851
852static void
853compiler_set_lineno(struct compiler *c, int off)
854{
855    basicblock *b;
856    if (c->u->u_lineno_set)
857        return;
858    c->u->u_lineno_set = 1;
859    b = c->u->u_curblock;
860    b->b_instr[off].i_lineno = c->u->u_lineno;
861}
862
863int
864PyCompile_OpcodeStackEffect(int opcode, int oparg)
865{
866    switch (opcode) {
867        case POP_TOP:
868            return -1;
869        case ROT_TWO:
870        case ROT_THREE:
871            return 0;
872        case DUP_TOP:
873            return 1;
874        case DUP_TOP_TWO:
875            return 2;
876
877        case UNARY_POSITIVE:
878        case UNARY_NEGATIVE:
879        case UNARY_NOT:
880        case UNARY_INVERT:
881            return 0;
882
883        case SET_ADD:
884        case LIST_APPEND:
885            return -1;
886        case MAP_ADD:
887            return -2;
888
889        case BINARY_POWER:
890        case BINARY_MULTIPLY:
891        case BINARY_MATRIX_MULTIPLY:
892        case BINARY_MODULO:
893        case BINARY_ADD:
894        case BINARY_SUBTRACT:
895        case BINARY_SUBSCR:
896        case BINARY_FLOOR_DIVIDE:
897        case BINARY_TRUE_DIVIDE:
898            return -1;
899        case INPLACE_FLOOR_DIVIDE:
900        case INPLACE_TRUE_DIVIDE:
901            return -1;
902
903        case INPLACE_ADD:
904        case INPLACE_SUBTRACT:
905        case INPLACE_MULTIPLY:
906        case INPLACE_MATRIX_MULTIPLY:
907        case INPLACE_MODULO:
908            return -1;
909        case STORE_SUBSCR:
910            return -3;
911        case DELETE_SUBSCR:
912            return -2;
913
914        case BINARY_LSHIFT:
915        case BINARY_RSHIFT:
916        case BINARY_AND:
917        case BINARY_XOR:
918        case BINARY_OR:
919            return -1;
920        case INPLACE_POWER:
921            return -1;
922        case GET_ITER:
923            return 0;
924
925        case PRINT_EXPR:
926            return -1;
927        case LOAD_BUILD_CLASS:
928            return 1;
929        case INPLACE_LSHIFT:
930        case INPLACE_RSHIFT:
931        case INPLACE_AND:
932        case INPLACE_XOR:
933        case INPLACE_OR:
934            return -1;
935        case BREAK_LOOP:
936            return 0;
937        case SETUP_WITH:
938            return 7;
939        case WITH_CLEANUP_START:
940            return 1;
941        case WITH_CLEANUP_FINISH:
942            return -1; /* XXX Sometimes more */
943        case RETURN_VALUE:
944            return -1;
945        case IMPORT_STAR:
946            return -1;
947        case SETUP_ANNOTATIONS:
948            return 0;
949        case YIELD_VALUE:
950            return 0;
951        case YIELD_FROM:
952            return -1;
953        case POP_BLOCK:
954            return 0;
955        case POP_EXCEPT:
956            return 0;  /* -3 except if bad bytecode */
957        case END_FINALLY:
958            return -1; /* or -2 or -3 if exception occurred */
959
960        case STORE_NAME:
961            return -1;
962        case DELETE_NAME:
963            return 0;
964        case UNPACK_SEQUENCE:
965            return oparg-1;
966        case UNPACK_EX:
967            return (oparg&0xFF) + (oparg>>8);
968        case FOR_ITER:
969            return 1; /* or -1, at end of iterator */
970
971        case STORE_ATTR:
972            return -2;
973        case DELETE_ATTR:
974            return -1;
975        case STORE_GLOBAL:
976            return -1;
977        case DELETE_GLOBAL:
978            return 0;
979        case LOAD_CONST:
980            return 1;
981        case LOAD_NAME:
982            return 1;
983        case BUILD_TUPLE:
984        case BUILD_LIST:
985        case BUILD_SET:
986        case BUILD_STRING:
987            return 1-oparg;
988        case BUILD_LIST_UNPACK:
989        case BUILD_TUPLE_UNPACK:
990        case BUILD_TUPLE_UNPACK_WITH_CALL:
991        case BUILD_SET_UNPACK:
992        case BUILD_MAP_UNPACK:
993        case BUILD_MAP_UNPACK_WITH_CALL:
994            return 1 - oparg;
995        case BUILD_MAP:
996            return 1 - 2*oparg;
997        case BUILD_CONST_KEY_MAP:
998            return -oparg;
999        case LOAD_ATTR:
1000            return 0;
1001        case COMPARE_OP:
1002            return -1;
1003        case IMPORT_NAME:
1004            return -1;
1005        case IMPORT_FROM:
1006            return 1;
1007
1008        case JUMP_FORWARD:
1009        case JUMP_IF_TRUE_OR_POP:  /* -1 if jump not taken */
1010        case JUMP_IF_FALSE_OR_POP:  /*  "" */
1011        case JUMP_ABSOLUTE:
1012            return 0;
1013
1014        case POP_JUMP_IF_FALSE:
1015        case POP_JUMP_IF_TRUE:
1016            return -1;
1017
1018        case LOAD_GLOBAL:
1019            return 1;
1020
1021        case CONTINUE_LOOP:
1022            return 0;
1023        case SETUP_LOOP:
1024            return 0;
1025        case SETUP_EXCEPT:
1026        case SETUP_FINALLY:
1027            return 6; /* can push 3 values for the new exception
1028                + 3 others for the previous exception state */
1029
1030        case LOAD_FAST:
1031            return 1;
1032        case STORE_FAST:
1033            return -1;
1034        case DELETE_FAST:
1035            return 0;
1036        case STORE_ANNOTATION:
1037            return -1;
1038
1039        case RAISE_VARARGS:
1040            return -oparg;
1041        case CALL_FUNCTION:
1042            return -oparg;
1043        case CALL_FUNCTION_KW:
1044            return -oparg-1;
1045        case CALL_FUNCTION_EX:
1046            return -1 - ((oparg & 0x01) != 0);
1047        case MAKE_FUNCTION:
1048            return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1049                ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1050        case BUILD_SLICE:
1051            if (oparg == 3)
1052                return -2;
1053            else
1054                return -1;
1055
1056        case LOAD_CLOSURE:
1057            return 1;
1058        case LOAD_DEREF:
1059        case LOAD_CLASSDEREF:
1060            return 1;
1061        case STORE_DEREF:
1062            return -1;
1063        case DELETE_DEREF:
1064            return 0;
1065        case GET_AWAITABLE:
1066            return 0;
1067        case SETUP_ASYNC_WITH:
1068            return 6;
1069        case BEFORE_ASYNC_WITH:
1070            return 1;
1071        case GET_AITER:
1072            return 0;
1073        case GET_ANEXT:
1074            return 1;
1075        case GET_YIELD_FROM_ITER:
1076            return 0;
1077        case FORMAT_VALUE:
1078            /* If there's a fmt_spec on the stack, we go from 2->1,
1079               else 1->1. */
1080            return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
1081        default:
1082            return PY_INVALID_STACK_EFFECT;
1083    }
1084    return PY_INVALID_STACK_EFFECT; /* not reachable */
1085}
1086
1087/* Add an opcode with no argument.
1088   Returns 0 on failure, 1 on success.
1089*/
1090
1091static int
1092compiler_addop(struct compiler *c, int opcode)
1093{
1094    basicblock *b;
1095    struct instr *i;
1096    int off;
1097    assert(!HAS_ARG(opcode));
1098    off = compiler_next_instr(c, c->u->u_curblock);
1099    if (off < 0)
1100        return 0;
1101    b = c->u->u_curblock;
1102    i = &b->b_instr[off];
1103    i->i_opcode = opcode;
1104    i->i_oparg = 0;
1105    if (opcode == RETURN_VALUE)
1106        b->b_return = 1;
1107    compiler_set_lineno(c, off);
1108    return 1;
1109}
1110
1111static Py_ssize_t
1112compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1113{
1114    PyObject *t, *v;
1115    Py_ssize_t arg;
1116
1117    t = _PyCode_ConstantKey(o);
1118    if (t == NULL)
1119        return -1;
1120
1121    v = PyDict_GetItem(dict, t);
1122    if (!v) {
1123        if (PyErr_Occurred()) {
1124            Py_DECREF(t);
1125            return -1;
1126        }
1127        arg = PyDict_Size(dict);
1128        v = PyLong_FromSsize_t(arg);
1129        if (!v) {
1130            Py_DECREF(t);
1131            return -1;
1132        }
1133        if (PyDict_SetItem(dict, t, v) < 0) {
1134            Py_DECREF(t);
1135            Py_DECREF(v);
1136            return -1;
1137        }
1138        Py_DECREF(v);
1139    }
1140    else
1141        arg = PyLong_AsLong(v);
1142    Py_DECREF(t);
1143    return arg;
1144}
1145
1146static int
1147compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1148                     PyObject *o)
1149{
1150    Py_ssize_t arg = compiler_add_o(c, dict, o);
1151    if (arg < 0)
1152        return 0;
1153    return compiler_addop_i(c, opcode, arg);
1154}
1155
1156static int
1157compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1158                    PyObject *o)
1159{
1160    Py_ssize_t arg;
1161    PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1162    if (!mangled)
1163        return 0;
1164    arg = compiler_add_o(c, dict, mangled);
1165    Py_DECREF(mangled);
1166    if (arg < 0)
1167        return 0;
1168    return compiler_addop_i(c, opcode, arg);
1169}
1170
1171/* Add an opcode with an integer argument.
1172   Returns 0 on failure, 1 on success.
1173*/
1174
1175static int
1176compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1177{
1178    struct instr *i;
1179    int off;
1180
1181    /* oparg value is unsigned, but a signed C int is usually used to store
1182       it in the C code (like Python/ceval.c).
1183
1184       Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1185
1186       The argument of a concrete bytecode instruction is limited to 8-bit.
1187       EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1188    assert(HAS_ARG(opcode));
1189    assert(0 <= oparg && oparg <= 2147483647);
1190
1191    off = compiler_next_instr(c, c->u->u_curblock);
1192    if (off < 0)
1193        return 0;
1194    i = &c->u->u_curblock->b_instr[off];
1195    i->i_opcode = opcode;
1196    i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1197    compiler_set_lineno(c, off);
1198    return 1;
1199}
1200
1201static int
1202compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1203{
1204    struct instr *i;
1205    int off;
1206
1207    assert(HAS_ARG(opcode));
1208    assert(b != NULL);
1209    off = compiler_next_instr(c, c->u->u_curblock);
1210    if (off < 0)
1211        return 0;
1212    i = &c->u->u_curblock->b_instr[off];
1213    i->i_opcode = opcode;
1214    i->i_target = b;
1215    if (absolute)
1216        i->i_jabs = 1;
1217    else
1218        i->i_jrel = 1;
1219    compiler_set_lineno(c, off);
1220    return 1;
1221}
1222
1223/* NEXT_BLOCK() creates an implicit jump from the current block
1224   to the new block.
1225
1226   The returns inside this macro make it impossible to decref objects
1227   created in the local function. Local objects should use the arena.
1228*/
1229#define NEXT_BLOCK(C) { \
1230    if (compiler_next_block((C)) == NULL) \
1231        return 0; \
1232}
1233
1234#define ADDOP(C, OP) { \
1235    if (!compiler_addop((C), (OP))) \
1236        return 0; \
1237}
1238
1239#define ADDOP_IN_SCOPE(C, OP) { \
1240    if (!compiler_addop((C), (OP))) { \
1241        compiler_exit_scope(c); \
1242        return 0; \
1243    } \
1244}
1245
1246#define ADDOP_O(C, OP, O, TYPE) { \
1247    if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1248        return 0; \
1249}
1250
1251/* Same as ADDOP_O, but steals a reference. */
1252#define ADDOP_N(C, OP, O, TYPE) { \
1253    if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1254        Py_DECREF((O)); \
1255        return 0; \
1256    } \
1257    Py_DECREF((O)); \
1258}
1259
1260#define ADDOP_NAME(C, OP, O, TYPE) { \
1261    if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1262        return 0; \
1263}
1264
1265#define ADDOP_I(C, OP, O) { \
1266    if (!compiler_addop_i((C), (OP), (O))) \
1267        return 0; \
1268}
1269
1270#define ADDOP_JABS(C, OP, O) { \
1271    if (!compiler_addop_j((C), (OP), (O), 1)) \
1272        return 0; \
1273}
1274
1275#define ADDOP_JREL(C, OP, O) { \
1276    if (!compiler_addop_j((C), (OP), (O), 0)) \
1277        return 0; \
1278}
1279
1280/* VISIT and VISIT_SEQ takes an ASDL type as their second argument.  They use
1281   the ASDL name to synthesize the name of the C type and the visit function.
1282*/
1283
1284#define VISIT(C, TYPE, V) {\
1285    if (!compiler_visit_ ## TYPE((C), (V))) \
1286        return 0; \
1287}
1288
1289#define VISIT_IN_SCOPE(C, TYPE, V) {\
1290    if (!compiler_visit_ ## TYPE((C), (V))) { \
1291        compiler_exit_scope(c); \
1292        return 0; \
1293    } \
1294}
1295
1296#define VISIT_SLICE(C, V, CTX) {\
1297    if (!compiler_visit_slice((C), (V), (CTX))) \
1298        return 0; \
1299}
1300
1301#define VISIT_SEQ(C, TYPE, SEQ) { \
1302    int _i; \
1303    asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1304    for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1305        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1306        if (!compiler_visit_ ## TYPE((C), elt)) \
1307            return 0; \
1308    } \
1309}
1310
1311#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1312    int _i; \
1313    asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1314    for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1315        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1316        if (!compiler_visit_ ## TYPE((C), elt)) { \
1317            compiler_exit_scope(c); \
1318            return 0; \
1319        } \
1320    } \
1321}
1322
1323static int
1324compiler_isdocstring(stmt_ty s)
1325{
1326    if (s->kind != Expr_kind)
1327        return 0;
1328    if (s->v.Expr.value->kind == Str_kind)
1329        return 1;
1330    if (s->v.Expr.value->kind == Constant_kind)
1331        return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1332    return 0;
1333}
1334
1335static int
1336is_const(expr_ty e)
1337{
1338    switch (e->kind) {
1339    case Constant_kind:
1340    case Num_kind:
1341    case Str_kind:
1342    case Bytes_kind:
1343    case Ellipsis_kind:
1344    case NameConstant_kind:
1345        return 1;
1346    default:
1347        return 0;
1348    }
1349}
1350
1351static PyObject *
1352get_const_value(expr_ty e)
1353{
1354    switch (e->kind) {
1355    case Constant_kind:
1356        return e->v.Constant.value;
1357    case Num_kind:
1358        return e->v.Num.n;
1359    case Str_kind:
1360        return e->v.Str.s;
1361    case Bytes_kind:
1362        return e->v.Bytes.s;
1363    case Ellipsis_kind:
1364        return Py_Ellipsis;
1365    case NameConstant_kind:
1366        return e->v.NameConstant.value;
1367    default:
1368        assert(!is_const(e));
1369        return NULL;
1370    }
1371}
1372
1373/* Search if variable annotations are present statically in a block. */
1374
1375static int
1376find_ann(asdl_seq *stmts)
1377{
1378    int i, j, res = 0;
1379    stmt_ty st;
1380
1381    for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1382        st = (stmt_ty)asdl_seq_GET(stmts, i);
1383        switch (st->kind) {
1384        case AnnAssign_kind:
1385            return 1;
1386        case For_kind:
1387            res = find_ann(st->v.For.body) ||
1388                  find_ann(st->v.For.orelse);
1389            break;
1390        case AsyncFor_kind:
1391            res = find_ann(st->v.AsyncFor.body) ||
1392                  find_ann(st->v.AsyncFor.orelse);
1393            break;
1394        case While_kind:
1395            res = find_ann(st->v.While.body) ||
1396                  find_ann(st->v.While.orelse);
1397            break;
1398        case If_kind:
1399            res = find_ann(st->v.If.body) ||
1400                  find_ann(st->v.If.orelse);
1401            break;
1402        case With_kind:
1403            res = find_ann(st->v.With.body);
1404            break;
1405        case AsyncWith_kind:
1406            res = find_ann(st->v.AsyncWith.body);
1407            break;
1408        case Try_kind:
1409            for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1410                excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1411                    st->v.Try.handlers, j);
1412                if (find_ann(handler->v.ExceptHandler.body)) {
1413                    return 1;
1414                }
1415            }
1416            res = find_ann(st->v.Try.body) ||
1417                  find_ann(st->v.Try.finalbody) ||
1418                  find_ann(st->v.Try.orelse);
1419            break;
1420        default:
1421            res = 0;
1422        }
1423        if (res) {
1424            break;
1425        }
1426    }
1427    return res;
1428}
1429
1430/* Compile a sequence of statements, checking for a docstring
1431   and for annotations. */
1432
1433static int
1434compiler_body(struct compiler *c, asdl_seq *stmts)
1435{
1436    int i = 0;
1437    stmt_ty st;
1438
1439    /* Set current line number to the line number of first statement.
1440       This way line number for SETUP_ANNOTATIONS will always
1441       coincide with the line number of first "real" statement in module.
1442       If body is empy, then lineno will be set later in assemble. */
1443    if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1444        !c->u->u_lineno && asdl_seq_LEN(stmts)) {
1445        st = (stmt_ty)asdl_seq_GET(stmts, 0);
1446        c->u->u_lineno = st->lineno;
1447    }
1448    /* Every annotated class and module should have __annotations__. */
1449    if (find_ann(stmts)) {
1450        ADDOP(c, SETUP_ANNOTATIONS);
1451    }
1452    if (!asdl_seq_LEN(stmts))
1453        return 1;
1454    st = (stmt_ty)asdl_seq_GET(stmts, 0);
1455    if (compiler_isdocstring(st) && c->c_optimize < 2) {
1456        /* don't generate docstrings if -OO */
1457        i = 1;
1458        VISIT(c, expr, st->v.Expr.value);
1459        if (!compiler_nameop(c, __doc__, Store))
1460            return 0;
1461    }
1462    for (; i < asdl_seq_LEN(stmts); i++)
1463        VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1464    return 1;
1465}
1466
1467static PyCodeObject *
1468compiler_mod(struct compiler *c, mod_ty mod)
1469{
1470    PyCodeObject *co;
1471    int addNone = 1;
1472    static PyObject *module;
1473    if (!module) {
1474        module = PyUnicode_InternFromString("<module>");
1475        if (!module)
1476            return NULL;
1477    }
1478    /* Use 0 for firstlineno initially, will fixup in assemble(). */
1479    if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1480        return NULL;
1481    switch (mod->kind) {
1482    case Module_kind:
1483        if (!compiler_body(c, mod->v.Module.body)) {
1484            compiler_exit_scope(c);
1485            return 0;
1486        }
1487        break;
1488    case Interactive_kind:
1489        if (find_ann(mod->v.Interactive.body)) {
1490            ADDOP(c, SETUP_ANNOTATIONS);
1491        }
1492        c->c_interactive = 1;
1493        VISIT_SEQ_IN_SCOPE(c, stmt,
1494                                mod->v.Interactive.body);
1495        break;
1496    case Expression_kind:
1497        VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1498        addNone = 0;
1499        break;
1500    case Suite_kind:
1501        PyErr_SetString(PyExc_SystemError,
1502                        "suite should not be possible");
1503        return 0;
1504    default:
1505        PyErr_Format(PyExc_SystemError,
1506                     "module kind %d should not be possible",
1507                     mod->kind);
1508        return 0;
1509    }
1510    co = assemble(c, addNone);
1511    compiler_exit_scope(c);
1512    return co;
1513}
1514
1515/* The test for LOCAL must come before the test for FREE in order to
1516   handle classes where name is both local and free.  The local var is
1517   a method and the free var is a free var referenced within a method.
1518*/
1519
1520static int
1521get_ref_type(struct compiler *c, PyObject *name)
1522{
1523    int scope;
1524    if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1525        _PyUnicode_EqualToASCIIString(name, "__class__"))
1526        return CELL;
1527    scope = PyST_GetScope(c->u->u_ste, name);
1528    if (scope == 0) {
1529        char buf[350];
1530        PyOS_snprintf(buf, sizeof(buf),
1531                      "unknown scope for %.100s in %.100s(%s)\n"
1532                      "symbols: %s\nlocals: %s\nglobals: %s",
1533                      PyUnicode_AsUTF8(name),
1534                      PyUnicode_AsUTF8(c->u->u_name),
1535                      PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1536                      PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1537                      PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1538                      PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
1539        );
1540        Py_FatalError(buf);
1541    }
1542
1543    return scope;
1544}
1545
1546static int
1547compiler_lookup_arg(PyObject *dict, PyObject *name)
1548{
1549    PyObject *k, *v;
1550    k = _PyCode_ConstantKey(name);
1551    if (k == NULL)
1552        return -1;
1553    v = PyDict_GetItem(dict, k);
1554    Py_DECREF(k);
1555    if (v == NULL)
1556        return -1;
1557    return PyLong_AS_LONG(v);
1558}
1559
1560static int
1561compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
1562{
1563    Py_ssize_t i, free = PyCode_GetNumFree(co);
1564    if (qualname == NULL)
1565        qualname = co->co_name;
1566
1567    if (free) {
1568        for (i = 0; i < free; ++i) {
1569            /* Bypass com_addop_varname because it will generate
1570               LOAD_DEREF but LOAD_CLOSURE is needed.
1571            */
1572            PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1573            int arg, reftype;
1574
1575            /* Special case: If a class contains a method with a
1576               free variable that has the same name as a method,
1577               the name will be considered free *and* local in the
1578               class.  It should be handled by the closure, as
1579               well as by the normal name loookup logic.
1580            */
1581            reftype = get_ref_type(c, name);
1582            if (reftype == CELL)
1583                arg = compiler_lookup_arg(c->u->u_cellvars, name);
1584            else /* (reftype == FREE) */
1585                arg = compiler_lookup_arg(c->u->u_freevars, name);
1586            if (arg == -1) {
1587                fprintf(stderr,
1588                    "lookup %s in %s %d %d\n"
1589                    "freevars of %s: %s\n",
1590                    PyUnicode_AsUTF8(PyObject_Repr(name)),
1591                    PyUnicode_AsUTF8(c->u->u_name),
1592                    reftype, arg,
1593                    PyUnicode_AsUTF8(co->co_name),
1594                    PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1595                Py_FatalError("compiler_make_closure()");
1596            }
1597            ADDOP_I(c, LOAD_CLOSURE, arg);
1598        }
1599        flags |= 0x08;
1600        ADDOP_I(c, BUILD_TUPLE, free);
1601    }
1602    ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1603    ADDOP_O(c, LOAD_CONST, qualname, consts);
1604    ADDOP_I(c, MAKE_FUNCTION, flags);
1605    return 1;
1606}
1607
1608static int
1609compiler_decorators(struct compiler *c, asdl_seq* decos)
1610{
1611    int i;
1612
1613    if (!decos)
1614        return 1;
1615
1616    for (i = 0; i < asdl_seq_LEN(decos); i++) {
1617        VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1618    }
1619    return 1;
1620}
1621
1622static int
1623compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1624                              asdl_seq *kw_defaults)
1625{
1626    /* Push a dict of keyword-only default values.
1627
1628       Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1629       */
1630    int i;
1631    PyObject *keys = NULL;
1632
1633    for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1634        arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1635        expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1636        if (default_) {
1637            PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1638            if (!mangled) {
1639                goto error;
1640            }
1641            if (keys == NULL) {
1642                keys = PyList_New(1);
1643                if (keys == NULL) {
1644                    Py_DECREF(mangled);
1645                    return 0;
1646                }
1647                PyList_SET_ITEM(keys, 0, mangled);
1648            }
1649            else {
1650                int res = PyList_Append(keys, mangled);
1651                Py_DECREF(mangled);
1652                if (res == -1) {
1653                    goto error;
1654                }
1655            }
1656            if (!compiler_visit_expr(c, default_)) {
1657                goto error;
1658            }
1659        }
1660    }
1661    if (keys != NULL) {
1662        Py_ssize_t default_count = PyList_GET_SIZE(keys);
1663        PyObject *keys_tuple = PyList_AsTuple(keys);
1664        Py_DECREF(keys);
1665        if (keys_tuple == NULL) {
1666            return 0;
1667        }
1668        ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1669        ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1670        assert(default_count > 0);
1671        return 1;
1672    }
1673    else {
1674        return -1;
1675    }
1676
1677error:
1678    Py_XDECREF(keys);
1679    return 0;
1680}
1681
1682static int
1683compiler_visit_argannotation(struct compiler *c, identifier id,
1684    expr_ty annotation, PyObject *names)
1685{
1686    if (annotation) {
1687        PyObject *mangled;
1688        VISIT(c, expr, annotation);
1689        mangled = _Py_Mangle(c->u->u_private, id);
1690        if (!mangled)
1691            return 0;
1692        if (PyList_Append(names, mangled) < 0) {
1693            Py_DECREF(mangled);
1694            return 0;
1695        }
1696        Py_DECREF(mangled);
1697    }
1698    return 1;
1699}
1700
1701static int
1702compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1703                              PyObject *names)
1704{
1705    int i;
1706    for (i = 0; i < asdl_seq_LEN(args); i++) {
1707        arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1708        if (!compiler_visit_argannotation(
1709                        c,
1710                        arg->arg,
1711                        arg->annotation,
1712                        names))
1713            return 0;
1714    }
1715    return 1;
1716}
1717
1718static int
1719compiler_visit_annotations(struct compiler *c, arguments_ty args,
1720                           expr_ty returns)
1721{
1722    /* Push arg annotation dict.
1723       The expressions are evaluated out-of-order wrt the source code.
1724
1725       Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1726       */
1727    static identifier return_str;
1728    PyObject *names;
1729    Py_ssize_t len;
1730    names = PyList_New(0);
1731    if (!names)
1732        return 0;
1733
1734    if (!compiler_visit_argannotations(c, args->args, names))
1735        goto error;
1736    if (args->vararg && args->vararg->annotation &&
1737        !compiler_visit_argannotation(c, args->vararg->arg,
1738                                     args->vararg->annotation, names))
1739        goto error;
1740    if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1741        goto error;
1742    if (args->kwarg && args->kwarg->annotation &&
1743        !compiler_visit_argannotation(c, args->kwarg->arg,
1744                                     args->kwarg->annotation, names))
1745        goto error;
1746
1747    if (!return_str) {
1748        return_str = PyUnicode_InternFromString("return");
1749        if (!return_str)
1750            goto error;
1751    }
1752    if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1753        goto error;
1754    }
1755
1756    len = PyList_GET_SIZE(names);
1757    if (len) {
1758        PyObject *keytuple = PyList_AsTuple(names);
1759        Py_DECREF(names);
1760        if (keytuple == NULL) {
1761            return 0;
1762        }
1763        ADDOP_N(c, LOAD_CONST, keytuple, consts);
1764        ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1765        return 1;
1766    }
1767    else {
1768        Py_DECREF(names);
1769        return -1;
1770    }
1771
1772error:
1773    Py_DECREF(names);
1774    return 0;
1775}
1776
1777static int
1778compiler_visit_defaults(struct compiler *c, arguments_ty args)
1779{
1780    VISIT_SEQ(c, expr, args->defaults);
1781    ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1782    return 1;
1783}
1784
1785static Py_ssize_t
1786compiler_default_arguments(struct compiler *c, arguments_ty args)
1787{
1788    Py_ssize_t funcflags = 0;
1789    if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1790        if (!compiler_visit_defaults(c, args))
1791            return -1;
1792        funcflags |= 0x01;
1793    }
1794    if (args->kwonlyargs) {
1795        int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1796                                                args->kw_defaults);
1797        if (res == 0) {
1798            return -1;
1799        }
1800        else if (res > 0) {
1801            funcflags |= 0x02;
1802        }
1803    }
1804    return funcflags;
1805}
1806
1807static int
1808compiler_function(struct compiler *c, stmt_ty s, int is_async)
1809{
1810    PyCodeObject *co;
1811    PyObject *qualname, *first_const = Py_None;
1812    arguments_ty args;
1813    expr_ty returns;
1814    identifier name;
1815    asdl_seq* decos;
1816    asdl_seq *body;
1817    stmt_ty st;
1818    Py_ssize_t i, n, funcflags;
1819    int docstring;
1820    int annotations;
1821    int scope_type;
1822
1823    if (is_async) {
1824        assert(s->kind == AsyncFunctionDef_kind);
1825
1826        args = s->v.AsyncFunctionDef.args;
1827        returns = s->v.AsyncFunctionDef.returns;
1828        decos = s->v.AsyncFunctionDef.decorator_list;
1829        name = s->v.AsyncFunctionDef.name;
1830        body = s->v.AsyncFunctionDef.body;
1831
1832        scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1833    } else {
1834        assert(s->kind == FunctionDef_kind);
1835
1836        args = s->v.FunctionDef.args;
1837        returns = s->v.FunctionDef.returns;
1838        decos = s->v.FunctionDef.decorator_list;
1839        name = s->v.FunctionDef.name;
1840        body = s->v.FunctionDef.body;
1841
1842        scope_type = COMPILER_SCOPE_FUNCTION;
1843    }
1844
1845    if (!compiler_decorators(c, decos))
1846        return 0;
1847
1848    funcflags = compiler_default_arguments(c, args);
1849    if (funcflags == -1) {
1850        return 0;
1851    }
1852
1853    annotations = compiler_visit_annotations(c, args, returns);
1854    if (annotations == 0) {
1855        return 0;
1856    }
1857    else if (annotations > 0) {
1858        funcflags |= 0x04;
1859    }
1860
1861    if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1862        return 0;
1863    }
1864
1865    st = (stmt_ty)asdl_seq_GET(body, 0);
1866    docstring = compiler_isdocstring(st);
1867    if (docstring && c->c_optimize < 2) {
1868        if (st->v.Expr.value->kind == Constant_kind)
1869            first_const = st->v.Expr.value->v.Constant.value;
1870        else
1871            first_const = st->v.Expr.value->v.Str.s;
1872    }
1873    if (compiler_add_o(c, c->u->u_consts, first_const) < 0)      {
1874        compiler_exit_scope(c);
1875        return 0;
1876    }
1877
1878    c->u->u_argcount = asdl_seq_LEN(args->args);
1879    c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1880    n = asdl_seq_LEN(body);
1881    /* if there was a docstring, we need to skip the first statement */
1882    for (i = docstring; i < n; i++) {
1883        st = (stmt_ty)asdl_seq_GET(body, i);
1884        VISIT_IN_SCOPE(c, stmt, st);
1885    }
1886    co = assemble(c, 1);
1887    qualname = c->u->u_qualname;
1888    Py_INCREF(qualname);
1889    compiler_exit_scope(c);
1890    if (co == NULL) {
1891        Py_XDECREF(qualname);
1892        Py_XDECREF(co);
1893        return 0;
1894    }
1895
1896    compiler_make_closure(c, co, funcflags, qualname);
1897    Py_DECREF(qualname);
1898    Py_DECREF(co);
1899
1900    /* decorators */
1901    for (i = 0; i < asdl_seq_LEN(decos); i++) {
1902        ADDOP_I(c, CALL_FUNCTION, 1);
1903    }
1904
1905    return compiler_nameop(c, name, Store);
1906}
1907
1908static int
1909compiler_class(struct compiler *c, stmt_ty s)
1910{
1911    PyCodeObject *co;
1912    PyObject *str;
1913    int i;
1914    asdl_seq* decos = s->v.ClassDef.decorator_list;
1915
1916    if (!compiler_decorators(c, decos))
1917        return 0;
1918
1919    /* ultimately generate code for:
1920         <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1921       where:
1922         <func> is a function/closure created from the class body;
1923            it has a single argument (__locals__) where the dict
1924            (or MutableSequence) representing the locals is passed
1925         <name> is the class name
1926         <bases> is the positional arguments and *varargs argument
1927         <keywords> is the keyword arguments and **kwds argument
1928       This borrows from compiler_call.
1929    */
1930
1931    /* 1. compile the class body into a code object */
1932    if (!compiler_enter_scope(c, s->v.ClassDef.name,
1933                              COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1934        return 0;
1935    /* this block represents what we do in the new scope */
1936    {
1937        /* use the class name for name mangling */
1938        Py_INCREF(s->v.ClassDef.name);
1939        Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
1940        /* load (global) __name__ ... */
1941        str = PyUnicode_InternFromString("__name__");
1942        if (!str || !compiler_nameop(c, str, Load)) {
1943            Py_XDECREF(str);
1944            compiler_exit_scope(c);
1945            return 0;
1946        }
1947        Py_DECREF(str);
1948        /* ... and store it as __module__ */
1949        str = PyUnicode_InternFromString("__module__");
1950        if (!str || !compiler_nameop(c, str, Store)) {
1951            Py_XDECREF(str);
1952            compiler_exit_scope(c);
1953            return 0;
1954        }
1955        Py_DECREF(str);
1956        assert(c->u->u_qualname);
1957        ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
1958        str = PyUnicode_InternFromString("__qualname__");
1959        if (!str || !compiler_nameop(c, str, Store)) {
1960            Py_XDECREF(str);
1961            compiler_exit_scope(c);
1962            return 0;
1963        }
1964        Py_DECREF(str);
1965        /* compile the body proper */
1966        if (!compiler_body(c, s->v.ClassDef.body)) {
1967            compiler_exit_scope(c);
1968            return 0;
1969        }
1970        /* Return __classcell__ if it is referenced, otherwise return None */
1971        if (c->u->u_ste->ste_needs_class_closure) {
1972            /* Store __classcell__ into class namespace & return it */
1973            str = PyUnicode_InternFromString("__class__");
1974            if (str == NULL) {
1975                compiler_exit_scope(c);
1976                return 0;
1977            }
1978            i = compiler_lookup_arg(c->u->u_cellvars, str);
1979            Py_DECREF(str);
1980            if (i < 0) {
1981                compiler_exit_scope(c);
1982                return 0;
1983            }
1984            assert(i == 0);
1985
1986            ADDOP_I(c, LOAD_CLOSURE, i);
1987            ADDOP(c, DUP_TOP);
1988            str = PyUnicode_InternFromString("__classcell__");
1989            if (!str || !compiler_nameop(c, str, Store)) {
1990                Py_XDECREF(str);
1991                compiler_exit_scope(c);
1992                return 0;
1993            }
1994            Py_DECREF(str);
1995        }
1996        else {
1997            /* No methods referenced __class__, so just return None */
1998            assert(PyDict_Size(c->u->u_cellvars) == 0);
1999            ADDOP_O(c, LOAD_CONST, Py_None, consts);
2000        }
2001        ADDOP_IN_SCOPE(c, RETURN_VALUE);
2002        /* create the code object */
2003        co = assemble(c, 1);
2004    }
2005    /* leave the new scope */
2006    compiler_exit_scope(c);
2007    if (co == NULL)
2008        return 0;
2009
2010    /* 2. load the 'build_class' function */
2011    ADDOP(c, LOAD_BUILD_CLASS);
2012
2013    /* 3. load a function (or closure) made from the code object */
2014    compiler_make_closure(c, co, 0, NULL);
2015    Py_DECREF(co);
2016
2017    /* 4. load class name */
2018    ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
2019
2020    /* 5. generate the rest of the code for the call */
2021    if (!compiler_call_helper(c, 2,
2022                              s->v.ClassDef.bases,
2023                              s->v.ClassDef.keywords))
2024        return 0;
2025
2026    /* 6. apply decorators */
2027    for (i = 0; i < asdl_seq_LEN(decos); i++) {
2028        ADDOP_I(c, CALL_FUNCTION, 1);
2029    }
2030
2031    /* 7. store into <name> */
2032    if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2033        return 0;
2034    return 1;
2035}
2036
2037static int
2038compiler_ifexp(struct compiler *c, expr_ty e)
2039{
2040    basicblock *end, *next;
2041
2042    assert(e->kind == IfExp_kind);
2043    end = compiler_new_block(c);
2044    if (end == NULL)
2045        return 0;
2046    next = compiler_new_block(c);
2047    if (next == NULL)
2048        return 0;
2049    VISIT(c, expr, e->v.IfExp.test);
2050    ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
2051    VISIT(c, expr, e->v.IfExp.body);
2052    ADDOP_JREL(c, JUMP_FORWARD, end);
2053    compiler_use_next_block(c, next);
2054    VISIT(c, expr, e->v.IfExp.orelse);
2055    compiler_use_next_block(c, end);
2056    return 1;
2057}
2058
2059static int
2060compiler_lambda(struct compiler *c, expr_ty e)
2061{
2062    PyCodeObject *co;
2063    PyObject *qualname;
2064    static identifier name;
2065    Py_ssize_t funcflags;
2066    arguments_ty args = e->v.Lambda.args;
2067    assert(e->kind == Lambda_kind);
2068
2069    if (!name) {
2070        name = PyUnicode_InternFromString("<lambda>");
2071        if (!name)
2072            return 0;
2073    }
2074
2075    funcflags = compiler_default_arguments(c, args);
2076    if (funcflags == -1) {
2077        return 0;
2078    }
2079
2080    if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
2081                              (void *)e, e->lineno))
2082        return 0;
2083
2084    /* Make None the first constant, so the lambda can't have a
2085       docstring. */
2086    if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
2087        return 0;
2088
2089    c->u->u_argcount = asdl_seq_LEN(args->args);
2090    c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2091    VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2092    if (c->u->u_ste->ste_generator) {
2093        co = assemble(c, 0);
2094    }
2095    else {
2096        ADDOP_IN_SCOPE(c, RETURN_VALUE);
2097        co = assemble(c, 1);
2098    }
2099    qualname = c->u->u_qualname;
2100    Py_INCREF(qualname);
2101    compiler_exit_scope(c);
2102    if (co == NULL)
2103        return 0;
2104
2105    compiler_make_closure(c, co, funcflags, qualname);
2106    Py_DECREF(qualname);
2107    Py_DECREF(co);
2108
2109    return 1;
2110}
2111
2112static int
2113compiler_if(struct compiler *c, stmt_ty s)
2114{
2115    basicblock *end, *next;
2116    int constant;
2117    assert(s->kind == If_kind);
2118    end = compiler_new_block(c);
2119    if (end == NULL)
2120        return 0;
2121
2122    constant = expr_constant(c, s->v.If.test);
2123    /* constant = 0: "if 0"
2124     * constant = 1: "if 1", "if 2", ...
2125     * constant = -1: rest */
2126    if (constant == 0) {
2127        if (s->v.If.orelse)
2128            VISIT_SEQ(c, stmt, s->v.If.orelse);
2129    } else if (constant == 1) {
2130        VISIT_SEQ(c, stmt, s->v.If.body);
2131    } else {
2132        if (asdl_seq_LEN(s->v.If.orelse)) {
2133            next = compiler_new_block(c);
2134            if (next == NULL)
2135                return 0;
2136        }
2137        else
2138            next = end;
2139        VISIT(c, expr, s->v.If.test);
2140        ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
2141        VISIT_SEQ(c, stmt, s->v.If.body);
2142        if (asdl_seq_LEN(s->v.If.orelse)) {
2143            ADDOP_JREL(c, JUMP_FORWARD, end);
2144            compiler_use_next_block(c, next);
2145            VISIT_SEQ(c, stmt, s->v.If.orelse);
2146        }
2147    }
2148    compiler_use_next_block(c, end);
2149    return 1;
2150}
2151
2152static int
2153compiler_for(struct compiler *c, stmt_ty s)
2154{
2155    basicblock *start, *cleanup, *end;
2156
2157    start = compiler_new_block(c);
2158    cleanup = compiler_new_block(c);
2159    end = compiler_new_block(c);
2160    if (start == NULL || end == NULL || cleanup == NULL)
2161        return 0;
2162    ADDOP_JREL(c, SETUP_LOOP, end);
2163    if (!compiler_push_fblock(c, LOOP, start))
2164        return 0;
2165    VISIT(c, expr, s->v.For.iter);
2166    ADDOP(c, GET_ITER);
2167    compiler_use_next_block(c, start);
2168    ADDOP_JREL(c, FOR_ITER, cleanup);
2169    VISIT(c, expr, s->v.For.target);
2170    VISIT_SEQ(c, stmt, s->v.For.body);
2171    ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2172    compiler_use_next_block(c, cleanup);
2173    ADDOP(c, POP_BLOCK);
2174    compiler_pop_fblock(c, LOOP, start);
2175    VISIT_SEQ(c, stmt, s->v.For.orelse);
2176    compiler_use_next_block(c, end);
2177    return 1;
2178}
2179
2180
2181static int
2182compiler_async_for(struct compiler *c, stmt_ty s)
2183{
2184    _Py_IDENTIFIER(StopAsyncIteration);
2185
2186    basicblock *try, *except, *end, *after_try, *try_cleanup,
2187               *after_loop, *after_loop_else;
2188
2189    PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2190    if (stop_aiter_error == NULL) {
2191        return 0;
2192    }
2193
2194    try = compiler_new_block(c);
2195    except = compiler_new_block(c);
2196    end = compiler_new_block(c);
2197    after_try = compiler_new_block(c);
2198    try_cleanup = compiler_new_block(c);
2199    after_loop = compiler_new_block(c);
2200    after_loop_else = compiler_new_block(c);
2201
2202    if (try == NULL || except == NULL || end == NULL
2203            || after_try == NULL || try_cleanup == NULL)
2204        return 0;
2205
2206    ADDOP_JREL(c, SETUP_LOOP, after_loop);
2207    if (!compiler_push_fblock(c, LOOP, try))
2208        return 0;
2209
2210    VISIT(c, expr, s->v.AsyncFor.iter);
2211    ADDOP(c, GET_AITER);
2212    ADDOP_O(c, LOAD_CONST, Py_None, consts);
2213    ADDOP(c, YIELD_FROM);
2214
2215    compiler_use_next_block(c, try);
2216
2217
2218    ADDOP_JREL(c, SETUP_EXCEPT, except);
2219    if (!compiler_push_fblock(c, EXCEPT, try))
2220        return 0;
2221
2222    ADDOP(c, GET_ANEXT);
2223    ADDOP_O(c, LOAD_CONST, Py_None, consts);
2224    ADDOP(c, YIELD_FROM);
2225    VISIT(c, expr, s->v.AsyncFor.target);
2226    ADDOP(c, POP_BLOCK);
2227    compiler_pop_fblock(c, EXCEPT, try);
2228    ADDOP_JREL(c, JUMP_FORWARD, after_try);
2229
2230
2231    compiler_use_next_block(c, except);
2232    ADDOP(c, DUP_TOP);
2233    ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
2234    ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2235    ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2236
2237    ADDOP(c, POP_TOP);
2238    ADDOP(c, POP_TOP);
2239    ADDOP(c, POP_TOP);
2240    ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2241    ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2242    ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2243
2244
2245    compiler_use_next_block(c, try_cleanup);
2246    ADDOP(c, END_FINALLY);
2247
2248    compiler_use_next_block(c, after_try);
2249    VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2250    ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2251
2252    ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2253    compiler_pop_fblock(c, LOOP, try);
2254
2255    compiler_use_next_block(c, after_loop);
2256    ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2257
2258    compiler_use_next_block(c, after_loop_else);
2259    VISIT_SEQ(c, stmt, s->v.For.orelse);
2260
2261    compiler_use_next_block(c, end);
2262
2263    return 1;
2264}
2265
2266static int
2267compiler_while(struct compiler *c, stmt_ty s)
2268{
2269    basicblock *loop, *orelse, *end, *anchor = NULL;
2270    int constant = expr_constant(c, s->v.While.test);
2271
2272    if (constant == 0) {
2273        if (s->v.While.orelse)
2274            VISIT_SEQ(c, stmt, s->v.While.orelse);
2275        return 1;
2276    }
2277    loop = compiler_new_block(c);
2278    end = compiler_new_block(c);
2279    if (constant == -1) {
2280        anchor = compiler_new_block(c);
2281        if (anchor == NULL)
2282            return 0;
2283    }
2284    if (loop == NULL || end == NULL)
2285        return 0;
2286    if (s->v.While.orelse) {
2287        orelse = compiler_new_block(c);
2288        if (orelse == NULL)
2289            return 0;
2290    }
2291    else
2292        orelse = NULL;
2293
2294    ADDOP_JREL(c, SETUP_LOOP, end);
2295    compiler_use_next_block(c, loop);
2296    if (!compiler_push_fblock(c, LOOP, loop))
2297        return 0;
2298    if (constant == -1) {
2299        VISIT(c, expr, s->v.While.test);
2300        ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
2301    }
2302    VISIT_SEQ(c, stmt, s->v.While.body);
2303    ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
2304
2305    /* XXX should the two POP instructions be in a separate block
2306       if there is no else clause ?
2307    */
2308
2309    if (constant == -1)
2310        compiler_use_next_block(c, anchor);
2311    ADDOP(c, POP_BLOCK);
2312    compiler_pop_fblock(c, LOOP, loop);
2313    if (orelse != NULL) /* what if orelse is just pass? */
2314        VISIT_SEQ(c, stmt, s->v.While.orelse);
2315    compiler_use_next_block(c, end);
2316
2317    return 1;
2318}
2319
2320static int
2321compiler_continue(struct compiler *c)
2322{
2323    static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2324    static const char IN_FINALLY_ERROR_MSG[] =
2325                    "'continue' not supported inside 'finally' clause";
2326    int i;
2327
2328    if (!c->u->u_nfblocks)
2329        return compiler_error(c, LOOP_ERROR_MSG);
2330    i = c->u->u_nfblocks - 1;
2331    switch (c->u->u_fblock[i].fb_type) {
2332    case LOOP:
2333        ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2334        break;
2335    case EXCEPT:
2336    case FINALLY_TRY:
2337        while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2338            /* Prevent continue anywhere under a finally
2339                  even if hidden in a sub-try or except. */
2340            if (c->u->u_fblock[i].fb_type == FINALLY_END)
2341                return compiler_error(c, IN_FINALLY_ERROR_MSG);
2342        }
2343        if (i == -1)
2344            return compiler_error(c, LOOP_ERROR_MSG);
2345        ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2346        break;
2347    case FINALLY_END:
2348        return compiler_error(c, IN_FINALLY_ERROR_MSG);
2349    }
2350
2351    return 1;
2352}
2353
2354/* Code generated for "try: <body> finally: <finalbody>" is as follows:
2355
2356        SETUP_FINALLY           L
2357        <code for body>
2358        POP_BLOCK
2359        LOAD_CONST              <None>
2360    L:          <code for finalbody>
2361        END_FINALLY
2362
2363   The special instructions use the block stack.  Each block
2364   stack entry contains the instruction that created it (here
2365   SETUP_FINALLY), the level of the value stack at the time the
2366   block stack entry was created, and a label (here L).
2367
2368   SETUP_FINALLY:
2369    Pushes the current value stack level and the label
2370    onto the block stack.
2371   POP_BLOCK:
2372    Pops en entry from the block stack, and pops the value
2373    stack until its level is the same as indicated on the
2374    block stack.  (The label is ignored.)
2375   END_FINALLY:
2376    Pops a variable number of entries from the *value* stack
2377    and re-raises the exception they specify.  The number of
2378    entries popped depends on the (pseudo) exception type.
2379
2380   The block stack is unwound when an exception is raised:
2381   when a SETUP_FINALLY entry is found, the exception is pushed
2382   onto the value stack (and the exception condition is cleared),
2383   and the interpreter jumps to the label gotten from the block
2384   stack.
2385*/
2386
2387static int
2388compiler_try_finally(struct compiler *c, stmt_ty s)
2389{
2390    basicblock *body, *end;
2391    body = compiler_new_block(c);
2392    end = compiler_new_block(c);
2393    if (body == NULL || end == NULL)
2394        return 0;
2395
2396    ADDOP_JREL(c, SETUP_FINALLY, end);
2397    compiler_use_next_block(c, body);
2398    if (!compiler_push_fblock(c, FINALLY_TRY, body))
2399        return 0;
2400    if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2401        if (!compiler_try_except(c, s))
2402            return 0;
2403    }
2404    else {
2405        VISIT_SEQ(c, stmt, s->v.Try.body);
2406    }
2407    ADDOP(c, POP_BLOCK);
2408    compiler_pop_fblock(c, FINALLY_TRY, body);
2409
2410    ADDOP_O(c, LOAD_CONST, Py_None, consts);
2411    compiler_use_next_block(c, end);
2412    if (!compiler_push_fblock(c, FINALLY_END, end))
2413        return 0;
2414    VISIT_SEQ(c, stmt, s->v.Try.finalbody);
2415    ADDOP(c, END_FINALLY);
2416    compiler_pop_fblock(c, FINALLY_END, end);
2417
2418    return 1;
2419}
2420
2421/*
2422   Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
2423   (The contents of the value stack is shown in [], with the top
2424   at the right; 'tb' is trace-back info, 'val' the exception's
2425   associated value, and 'exc' the exception.)
2426
2427   Value stack          Label   Instruction     Argument
2428   []                           SETUP_EXCEPT    L1
2429   []                           <code for S>
2430   []                           POP_BLOCK
2431   []                           JUMP_FORWARD    L0
2432
2433   [tb, val, exc]       L1:     DUP                             )
2434   [tb, val, exc, exc]          <evaluate E1>                   )
2435   [tb, val, exc, exc, E1]      COMPARE_OP      EXC_MATCH       ) only if E1
2436   [tb, val, exc, 1-or-0]       POP_JUMP_IF_FALSE       L2      )
2437   [tb, val, exc]               POP
2438   [tb, val]                    <assign to V1>  (or POP if no V1)
2439   [tb]                         POP
2440   []                           <code for S1>
2441                                JUMP_FORWARD    L0
2442
2443   [tb, val, exc]       L2:     DUP
2444   .............................etc.......................
2445
2446   [tb, val, exc]       Ln+1:   END_FINALLY     # re-raise exception
2447
2448   []                   L0:     <next statement>
2449
2450   Of course, parts are not generated if Vi or Ei is not present.
2451*/
2452static int
2453compiler_try_except(struct compiler *c, stmt_ty s)
2454{
2455    basicblock *body, *orelse, *except, *end;
2456    Py_ssize_t i, n;
2457
2458    body = compiler_new_block(c);
2459    except = compiler_new_block(c);
2460    orelse = compiler_new_block(c);
2461    end = compiler_new_block(c);
2462    if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2463        return 0;
2464    ADDOP_JREL(c, SETUP_EXCEPT, except);
2465    compiler_use_next_block(c, body);
2466    if (!compiler_push_fblock(c, EXCEPT, body))
2467        return 0;
2468    VISIT_SEQ(c, stmt, s->v.Try.body);
2469    ADDOP(c, POP_BLOCK);
2470    compiler_pop_fblock(c, EXCEPT, body);
2471    ADDOP_JREL(c, JUMP_FORWARD, orelse);
2472    n = asdl_seq_LEN(s->v.Try.handlers);
2473    compiler_use_next_block(c, except);
2474    for (i = 0; i < n; i++) {
2475        excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
2476            s->v.Try.handlers, i);
2477        if (!handler->v.ExceptHandler.type && i < n-1)
2478            return compiler_error(c, "default 'except:' must be last");
2479        c->u->u_lineno_set = 0;
2480        c->u->u_lineno = handler->lineno;
2481        c->u->u_col_offset = handler->col_offset;
2482        except = compiler_new_block(c);
2483        if (except == NULL)
2484            return 0;
2485        if (handler->v.ExceptHandler.type) {
2486            ADDOP(c, DUP_TOP);
2487            VISIT(c, expr, handler->v.ExceptHandler.type);
2488            ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2489            ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2490        }
2491        ADDOP(c, POP_TOP);
2492        if (handler->v.ExceptHandler.name) {
2493            basicblock *cleanup_end, *cleanup_body;
2494
2495            cleanup_end = compiler_new_block(c);
2496            cleanup_body = compiler_new_block(c);
2497            if (!(cleanup_end || cleanup_body))
2498                return 0;
2499
2500            compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2501            ADDOP(c, POP_TOP);
2502
2503            /*
2504              try:
2505                  # body
2506              except type as name:
2507                  try:
2508                      # body
2509                  finally:
2510                      name = None
2511                      del name
2512            */
2513
2514            /* second try: */
2515            ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2516            compiler_use_next_block(c, cleanup_body);
2517            if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2518                return 0;
2519
2520            /* second # body */
2521            VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2522            ADDOP(c, POP_BLOCK);
2523            ADDOP(c, POP_EXCEPT);
2524            compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2525
2526            /* finally: */
2527            ADDOP_O(c, LOAD_CONST, Py_None, consts);
2528            compiler_use_next_block(c, cleanup_end);
2529            if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2530                return 0;
2531
2532            /* name = None */
2533            ADDOP_O(c, LOAD_CONST, Py_None, consts);
2534            compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2535
2536            /* del name */
2537            compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2538
2539            ADDOP(c, END_FINALLY);
2540            compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2541        }
2542        else {
2543            basicblock *cleanup_body;
2544
2545            cleanup_body = compiler_new_block(c);
2546            if (!cleanup_body)
2547                return 0;
2548
2549            ADDOP(c, POP_TOP);
2550            ADDOP(c, POP_TOP);
2551            compiler_use_next_block(c, cleanup_body);
2552            if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2553                return 0;
2554            VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2555            ADDOP(c, POP_EXCEPT);
2556            compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2557        }
2558        ADDOP_JREL(c, JUMP_FORWARD, end);
2559        compiler_use_next_block(c, except);
2560    }
2561    ADDOP(c, END_FINALLY);
2562    compiler_use_next_block(c, orelse);
2563    VISIT_SEQ(c, stmt, s->v.Try.orelse);
2564    compiler_use_next_block(c, end);
2565    return 1;
2566}
2567
2568static int
2569compiler_try(struct compiler *c, stmt_ty s) {
2570    if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2571        return compiler_try_finally(c, s);
2572    else
2573        return compiler_try_except(c, s);
2574}
2575
2576
2577static int
2578compiler_import_as(struct compiler *c, identifier name, identifier asname)
2579{
2580    /* The IMPORT_NAME opcode was already generated.  This function
2581       merely needs to bind the result to a name.
2582
2583       If there is a dot in name, we need to split it and emit a
2584       LOAD_ATTR for each name.
2585    */
2586    Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2587                                        PyUnicode_GET_LENGTH(name), 1);
2588    if (dot == -2)
2589        return 0;
2590    if (dot != -1) {
2591        /* Consume the base module name to get the first attribute */
2592        Py_ssize_t pos = dot + 1;
2593        while (dot != -1) {
2594            PyObject *attr;
2595            dot = PyUnicode_FindChar(name, '.', pos,
2596                                     PyUnicode_GET_LENGTH(name), 1);
2597            if (dot == -2)
2598                return 0;
2599            attr = PyUnicode_Substring(name, pos,
2600                                       (dot != -1) ? dot :
2601                                       PyUnicode_GET_LENGTH(name));
2602            if (!attr)
2603                return 0;
2604            ADDOP_O(c, LOAD_ATTR, attr, names);
2605            Py_DECREF(attr);
2606            pos = dot + 1;
2607        }
2608    }
2609    return compiler_nameop(c, asname, Store);
2610}
2611
2612static int
2613compiler_import(struct compiler *c, stmt_ty s)
2614{
2615    /* The Import node stores a module name like a.b.c as a single
2616       string.  This is convenient for all cases except
2617         import a.b.c as d
2618       where we need to parse that string to extract the individual
2619       module names.
2620       XXX Perhaps change the representation to make this case simpler?
2621     */
2622    Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
2623
2624    for (i = 0; i < n; i++) {
2625        alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2626        int r;
2627        PyObject *level;
2628
2629        level = PyLong_FromLong(0);
2630        if (level == NULL)
2631            return 0;
2632
2633        ADDOP_O(c, LOAD_CONST, level, consts);
2634        Py_DECREF(level);
2635        ADDOP_O(c, LOAD_CONST, Py_None, consts);
2636        ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2637
2638        if (alias->asname) {
2639            r = compiler_import_as(c, alias->name, alias->asname);
2640            if (!r)
2641                return r;
2642        }
2643        else {
2644            identifier tmp = alias->name;
2645            Py_ssize_t dot = PyUnicode_FindChar(
2646                alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2647            if (dot != -1) {
2648                tmp = PyUnicode_Substring(alias->name, 0, dot);
2649                if (tmp == NULL)
2650                    return 0;
2651            }
2652            r = compiler_nameop(c, tmp, Store);
2653            if (dot != -1) {
2654                Py_DECREF(tmp);
2655            }
2656            if (!r)
2657                return r;
2658        }
2659    }
2660    return 1;
2661}
2662
2663static int
2664compiler_from_import(struct compiler *c, stmt_ty s)
2665{
2666    Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
2667
2668    PyObject *names = PyTuple_New(n);
2669    PyObject *level;
2670    static PyObject *empty_string;
2671
2672    if (!empty_string) {
2673        empty_string = PyUnicode_FromString("");
2674        if (!empty_string)
2675            return 0;
2676    }
2677
2678    if (!names)
2679        return 0;
2680
2681    level = PyLong_FromLong(s->v.ImportFrom.level);
2682    if (!level) {
2683        Py_DECREF(names);
2684        return 0;
2685    }
2686
2687    /* build up the names */
2688    for (i = 0; i < n; i++) {
2689        alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2690        Py_INCREF(alias->name);
2691        PyTuple_SET_ITEM(names, i, alias->name);
2692    }
2693
2694    if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2695        _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2696        Py_DECREF(level);
2697        Py_DECREF(names);
2698        return compiler_error(c, "from __future__ imports must occur "
2699                              "at the beginning of the file");
2700    }
2701
2702    ADDOP_O(c, LOAD_CONST, level, consts);
2703    Py_DECREF(level);
2704    ADDOP_O(c, LOAD_CONST, names, consts);
2705    Py_DECREF(names);
2706    if (s->v.ImportFrom.module) {
2707        ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2708    }
2709    else {
2710        ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2711    }
2712    for (i = 0; i < n; i++) {
2713        alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2714        identifier store_name;
2715
2716        if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
2717            assert(n == 1);
2718            ADDOP(c, IMPORT_STAR);
2719            return 1;
2720        }
2721
2722        ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2723        store_name = alias->name;
2724        if (alias->asname)
2725            store_name = alias->asname;
2726
2727        if (!compiler_nameop(c, store_name, Store)) {
2728            Py_DECREF(names);
2729            return 0;
2730        }
2731    }
2732    /* remove imported module */
2733    ADDOP(c, POP_TOP);
2734    return 1;
2735}
2736
2737static int
2738compiler_assert(struct compiler *c, stmt_ty s)
2739{
2740    static PyObject *assertion_error = NULL;
2741    basicblock *end;
2742    PyObject* msg;
2743
2744    if (c->c_optimize)
2745        return 1;
2746    if (assertion_error == NULL) {
2747        assertion_error = PyUnicode_InternFromString("AssertionError");
2748        if (assertion_error == NULL)
2749            return 0;
2750    }
2751    if (s->v.Assert.test->kind == Tuple_kind &&
2752        asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2753        msg = PyUnicode_FromString("assertion is always true, "
2754                                   "perhaps remove parentheses?");
2755        if (msg == NULL)
2756            return 0;
2757        if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2758                                     c->c_filename, c->u->u_lineno,
2759                                     NULL, NULL) == -1) {
2760            Py_DECREF(msg);
2761            return 0;
2762        }
2763        Py_DECREF(msg);
2764    }
2765    VISIT(c, expr, s->v.Assert.test);
2766    end = compiler_new_block(c);
2767    if (end == NULL)
2768        return 0;
2769    ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2770    ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2771    if (s->v.Assert.msg) {
2772        VISIT(c, expr, s->v.Assert.msg);
2773        ADDOP_I(c, CALL_FUNCTION, 1);
2774    }
2775    ADDOP_I(c, RAISE_VARARGS, 1);
2776    compiler_use_next_block(c, end);
2777    return 1;
2778}
2779
2780static int
2781compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2782{
2783    if (c->c_interactive && c->c_nestlevel <= 1) {
2784        VISIT(c, expr, value);
2785        ADDOP(c, PRINT_EXPR);
2786        return 1;
2787    }
2788
2789    if (is_const(value)) {
2790        /* ignore constant statement */
2791        return 1;
2792    }
2793
2794    VISIT(c, expr, value);
2795    ADDOP(c, POP_TOP);
2796    return 1;
2797}
2798
2799static int
2800compiler_visit_stmt(struct compiler *c, stmt_ty s)
2801{
2802    Py_ssize_t i, n;
2803
2804    /* Always assign a lineno to the next instruction for a stmt. */
2805    c->u->u_lineno = s->lineno;
2806    c->u->u_col_offset = s->col_offset;
2807    c->u->u_lineno_set = 0;
2808
2809    switch (s->kind) {
2810    case FunctionDef_kind:
2811        return compiler_function(c, s, 0);
2812    case ClassDef_kind:
2813        return compiler_class(c, s);
2814    case Return_kind:
2815        if (c->u->u_ste->ste_type != FunctionBlock)
2816            return compiler_error(c, "'return' outside function");
2817        if (s->v.Return.value) {
2818            if (c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2819                return compiler_error(
2820                    c, "'return' with value in async generator");
2821            VISIT(c, expr, s->v.Return.value);
2822        }
2823        else
2824            ADDOP_O(c, LOAD_CONST, Py_None, consts);
2825        ADDOP(c, RETURN_VALUE);
2826        break;
2827    case Delete_kind:
2828        VISIT_SEQ(c, expr, s->v.Delete.targets)
2829        break;
2830    case Assign_kind:
2831        n = asdl_seq_LEN(s->v.Assign.targets);
2832        VISIT(c, expr, s->v.Assign.value);
2833        for (i = 0; i < n; i++) {
2834            if (i < n - 1)
2835                ADDOP(c, DUP_TOP);
2836            VISIT(c, expr,
2837                  (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2838        }
2839        break;
2840    case AugAssign_kind:
2841        return compiler_augassign(c, s);
2842    case AnnAssign_kind:
2843        return compiler_annassign(c, s);
2844    case For_kind:
2845        return compiler_for(c, s);
2846    case While_kind:
2847        return compiler_while(c, s);
2848    case If_kind:
2849        return compiler_if(c, s);
2850    case Raise_kind:
2851        n = 0;
2852        if (s->v.Raise.exc) {
2853            VISIT(c, expr, s->v.Raise.exc);
2854            n++;
2855            if (s->v.Raise.cause) {
2856                VISIT(c, expr, s->v.Raise.cause);
2857                n++;
2858            }
2859        }
2860        ADDOP_I(c, RAISE_VARARGS, (int)n);
2861        break;
2862    case Try_kind:
2863        return compiler_try(c, s);
2864    case Assert_kind:
2865        return compiler_assert(c, s);
2866    case Import_kind:
2867        return compiler_import(c, s);
2868    case ImportFrom_kind:
2869        return compiler_from_import(c, s);
2870    case Global_kind:
2871    case Nonlocal_kind:
2872        break;
2873    case Expr_kind:
2874        return compiler_visit_stmt_expr(c, s->v.Expr.value);
2875    case Pass_kind:
2876        break;
2877    case Break_kind:
2878        if (!compiler_in_loop(c))
2879            return compiler_error(c, "'break' outside loop");
2880        ADDOP(c, BREAK_LOOP);
2881        break;
2882    case Continue_kind:
2883        return compiler_continue(c);
2884    case With_kind:
2885        return compiler_with(c, s, 0);
2886    case AsyncFunctionDef_kind:
2887        return compiler_function(c, s, 1);
2888    case AsyncWith_kind:
2889        return compiler_async_with(c, s, 0);
2890    case AsyncFor_kind:
2891        return compiler_async_for(c, s);
2892    }
2893
2894    return 1;
2895}
2896
2897static int
2898unaryop(unaryop_ty op)
2899{
2900    switch (op) {
2901    case Invert:
2902        return UNARY_INVERT;
2903    case Not:
2904        return UNARY_NOT;
2905    case UAdd:
2906        return UNARY_POSITIVE;
2907    case USub:
2908        return UNARY_NEGATIVE;
2909    default:
2910        PyErr_Format(PyExc_SystemError,
2911            "unary op %d should not be possible", op);
2912        return 0;
2913    }
2914}
2915
2916static int
2917binop(struct compiler *c, operator_ty op)
2918{
2919    switch (op) {
2920    case Add:
2921        return BINARY_ADD;
2922    case Sub:
2923        return BINARY_SUBTRACT;
2924    case Mult:
2925        return BINARY_MULTIPLY;
2926    case MatMult:
2927        return BINARY_MATRIX_MULTIPLY;
2928    case Div:
2929        return BINARY_TRUE_DIVIDE;
2930    case Mod:
2931        return BINARY_MODULO;
2932    case Pow:
2933        return BINARY_POWER;
2934    case LShift:
2935        return BINARY_LSHIFT;
2936    case RShift:
2937        return BINARY_RSHIFT;
2938    case BitOr:
2939        return BINARY_OR;
2940    case BitXor:
2941        return BINARY_XOR;
2942    case BitAnd:
2943        return BINARY_AND;
2944    case FloorDiv:
2945        return BINARY_FLOOR_DIVIDE;
2946    default:
2947        PyErr_Format(PyExc_SystemError,
2948            "binary op %d should not be possible", op);
2949        return 0;
2950    }
2951}
2952
2953static int
2954cmpop(cmpop_ty op)
2955{
2956    switch (op) {
2957    case Eq:
2958        return PyCmp_EQ;
2959    case NotEq:
2960        return PyCmp_NE;
2961    case Lt:
2962        return PyCmp_LT;
2963    case LtE:
2964        return PyCmp_LE;
2965    case Gt:
2966        return PyCmp_GT;
2967    case GtE:
2968        return PyCmp_GE;
2969    case Is:
2970        return PyCmp_IS;
2971    case IsNot:
2972        return PyCmp_IS_NOT;
2973    case In:
2974        return PyCmp_IN;
2975    case NotIn:
2976        return PyCmp_NOT_IN;
2977    default:
2978        return PyCmp_BAD;
2979    }
2980}
2981
2982static int
2983inplace_binop(struct compiler *c, operator_ty op)
2984{
2985    switch (op) {
2986    case Add:
2987        return INPLACE_ADD;
2988    case Sub:
2989        return INPLACE_SUBTRACT;
2990    case Mult:
2991        return INPLACE_MULTIPLY;
2992    case MatMult:
2993        return INPLACE_MATRIX_MULTIPLY;
2994    case Div:
2995        return INPLACE_TRUE_DIVIDE;
2996    case Mod:
2997        return INPLACE_MODULO;
2998    case Pow:
2999        return INPLACE_POWER;
3000    case LShift:
3001        return INPLACE_LSHIFT;
3002    case RShift:
3003        return INPLACE_RSHIFT;
3004    case BitOr:
3005        return INPLACE_OR;
3006    case BitXor:
3007        return INPLACE_XOR;
3008    case BitAnd:
3009        return INPLACE_AND;
3010    case FloorDiv:
3011        return INPLACE_FLOOR_DIVIDE;
3012    default:
3013        PyErr_Format(PyExc_SystemError,
3014            "inplace binary op %d should not be possible", op);
3015        return 0;
3016    }
3017}
3018
3019static int
3020compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3021{
3022    int op, scope;
3023    Py_ssize_t arg;
3024    enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
3025
3026    PyObject *dict = c->u->u_names;
3027    PyObject *mangled;
3028    /* XXX AugStore isn't used anywhere! */
3029
3030    mangled = _Py_Mangle(c->u->u_private, name);
3031    if (!mangled)
3032        return 0;
3033
3034    assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3035           !_PyUnicode_EqualToASCIIString(name, "True") &&
3036           !_PyUnicode_EqualToASCIIString(name, "False"));
3037
3038    op = 0;
3039    optype = OP_NAME;
3040    scope = PyST_GetScope(c->u->u_ste, mangled);
3041    switch (scope) {
3042    case FREE:
3043        dict = c->u->u_freevars;
3044        optype = OP_DEREF;
3045        break;
3046    case CELL:
3047        dict = c->u->u_cellvars;
3048        optype = OP_DEREF;
3049        break;
3050    case LOCAL:
3051        if (c->u->u_ste->ste_type == FunctionBlock)
3052            optype = OP_FAST;
3053        break;
3054    case GLOBAL_IMPLICIT:
3055        if (c->u->u_ste->ste_type == FunctionBlock)
3056            optype = OP_GLOBAL;
3057        break;
3058    case GLOBAL_EXPLICIT:
3059        optype = OP_GLOBAL;
3060        break;
3061    default:
3062        /* scope can be 0 */
3063        break;
3064    }
3065
3066    /* XXX Leave assert here, but handle __doc__ and the like better */
3067    assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
3068
3069    switch (optype) {
3070    case OP_DEREF:
3071        switch (ctx) {
3072        case Load:
3073            op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3074            break;
3075        case Store: op = STORE_DEREF; break;
3076        case AugLoad:
3077        case AugStore:
3078            break;
3079        case Del: op = DELETE_DEREF; break;
3080        case Param:
3081        default:
3082            PyErr_SetString(PyExc_SystemError,
3083                            "param invalid for deref variable");
3084            return 0;
3085        }
3086        break;
3087    case OP_FAST:
3088        switch (ctx) {
3089        case Load: op = LOAD_FAST; break;
3090        case Store: op = STORE_FAST; break;
3091        case Del: op = DELETE_FAST; break;
3092        case AugLoad:
3093        case AugStore:
3094            break;
3095        case Param:
3096        default:
3097            PyErr_SetString(PyExc_SystemError,
3098                            "param invalid for local variable");
3099            return 0;
3100        }
3101        ADDOP_O(c, op, mangled, varnames);
3102        Py_DECREF(mangled);
3103        return 1;
3104    case OP_GLOBAL:
3105        switch (ctx) {
3106        case Load: op = LOAD_GLOBAL; break;
3107        case Store: op = STORE_GLOBAL; break;
3108        case Del: op = DELETE_GLOBAL; break;
3109        case AugLoad:
3110        case AugStore:
3111            break;
3112        case Param:
3113        default:
3114            PyErr_SetString(PyExc_SystemError,
3115                            "param invalid for global variable");
3116            return 0;
3117        }
3118        break;
3119    case OP_NAME:
3120        switch (ctx) {
3121        case Load: op = LOAD_NAME; break;
3122        case Store: op = STORE_NAME; break;
3123        case Del: op = DELETE_NAME; break;
3124        case AugLoad:
3125        case AugStore:
3126            break;
3127        case Param:
3128        default:
3129            PyErr_SetString(PyExc_SystemError,
3130                            "param invalid for name variable");
3131            return 0;
3132        }
3133        break;
3134    }
3135
3136    assert(op);
3137    arg = compiler_add_o(c, dict, mangled);
3138    Py_DECREF(mangled);
3139    if (arg < 0)
3140        return 0;
3141    return compiler_addop_i(c, op, arg);
3142}
3143
3144static int
3145compiler_boolop(struct compiler *c, expr_ty e)
3146{
3147    basicblock *end;
3148    int jumpi;
3149    Py_ssize_t i, n;
3150    asdl_seq *s;
3151
3152    assert(e->kind == BoolOp_kind);
3153    if (e->v.BoolOp.op == And)
3154        jumpi = JUMP_IF_FALSE_OR_POP;
3155    else
3156        jumpi = JUMP_IF_TRUE_OR_POP;
3157    end = compiler_new_block(c);
3158    if (end == NULL)
3159        return 0;
3160    s = e->v.BoolOp.values;
3161    n = asdl_seq_LEN(s) - 1;
3162    assert(n >= 0);
3163    for (i = 0; i < n; ++i) {
3164        VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3165        ADDOP_JABS(c, jumpi, end);
3166    }
3167    VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3168    compiler_use_next_block(c, end);
3169    return 1;
3170}
3171
3172static int
3173starunpack_helper(struct compiler *c, asdl_seq *elts,
3174                  int single_op, int inner_op, int outer_op)
3175{
3176    Py_ssize_t n = asdl_seq_LEN(elts);
3177    Py_ssize_t i, nsubitems = 0, nseen = 0;
3178    for (i = 0; i < n; i++) {
3179        expr_ty elt = asdl_seq_GET(elts, i);
3180        if (elt->kind == Starred_kind) {
3181            if (nseen) {
3182                ADDOP_I(c, inner_op, nseen);
3183                nseen = 0;
3184                nsubitems++;
3185            }
3186            VISIT(c, expr, elt->v.Starred.value);
3187            nsubitems++;
3188        }
3189        else {
3190            VISIT(c, expr, elt);
3191            nseen++;
3192        }
3193    }
3194    if (nsubitems) {
3195        if (nseen) {
3196            ADDOP_I(c, inner_op, nseen);
3197            nsubitems++;
3198        }
3199        ADDOP_I(c, outer_op, nsubitems);
3200    }
3201    else
3202        ADDOP_I(c, single_op, nseen);
3203    return 1;
3204}
3205
3206static int
3207assignment_helper(struct compiler *c, asdl_seq *elts)
3208{
3209    Py_ssize_t n = asdl_seq_LEN(elts);
3210    Py_ssize_t i;
3211    int seen_star = 0;
3212    for (i = 0; i < n; i++) {
3213        expr_ty elt = asdl_seq_GET(elts, i);
3214        if (elt->kind == Starred_kind && !seen_star) {
3215            if ((i >= (1 << 8)) ||
3216                (n-i-1 >= (INT_MAX >> 8)))
3217                return compiler_error(c,
3218                    "too many expressions in "
3219                    "star-unpacking assignment");
3220            ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3221            seen_star = 1;
3222            asdl_seq_SET(elts, i, elt->v.Starred.value);
3223        }
3224        else if (elt->kind == Starred_kind) {
3225            return compiler_error(c,
3226                "two starred expressions in assignment");
3227        }
3228    }
3229    if (!seen_star) {
3230        ADDOP_I(c, UNPACK_SEQUENCE, n);
3231    }
3232    VISIT_SEQ(c, expr, elts);
3233    return 1;
3234}
3235
3236static int
3237compiler_list(struct compiler *c, expr_ty e)
3238{
3239    asdl_seq *elts = e->v.List.elts;
3240    if (e->v.List.ctx == Store) {
3241        return assignment_helper(c, elts);
3242    }
3243    else if (e->v.List.ctx == Load) {
3244        return starunpack_helper(c, elts,
3245                                 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
3246    }
3247    else
3248        VISIT_SEQ(c, expr, elts);
3249    return 1;
3250}
3251
3252static int
3253compiler_tuple(struct compiler *c, expr_ty e)
3254{
3255    asdl_seq *elts = e->v.Tuple.elts;
3256    if (e->v.Tuple.ctx == Store) {
3257        return assignment_helper(c, elts);
3258    }
3259    else if (e->v.Tuple.ctx == Load) {
3260        return starunpack_helper(c, elts,
3261                                 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3262    }
3263    else
3264        VISIT_SEQ(c, expr, elts);
3265    return 1;
3266}
3267
3268static int
3269compiler_set(struct compiler *c, expr_ty e)
3270{
3271    return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3272                             BUILD_SET, BUILD_SET_UNPACK);
3273}
3274
3275static int
3276are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3277{
3278    Py_ssize_t i;
3279    for (i = begin; i < end; i++) {
3280        expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3281        if (key == NULL || !is_const(key))
3282            return 0;
3283    }
3284    return 1;
3285}
3286
3287static int
3288compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3289{
3290    Py_ssize_t i, n = end - begin;
3291    PyObject *keys, *key;
3292    if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3293        for (i = begin; i < end; i++) {
3294            VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3295        }
3296        keys = PyTuple_New(n);
3297        if (keys == NULL) {
3298            return 0;
3299        }
3300        for (i = begin; i < end; i++) {
3301            key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3302            Py_INCREF(key);
3303            PyTuple_SET_ITEM(keys, i - begin, key);
3304        }
3305        ADDOP_N(c, LOAD_CONST, keys, consts);
3306        ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3307    }
3308    else {
3309        for (i = begin; i < end; i++) {
3310            VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3311            VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3312        }
3313        ADDOP_I(c, BUILD_MAP, n);
3314    }
3315    return 1;
3316}
3317
3318static int
3319compiler_dict(struct compiler *c, expr_ty e)
3320{
3321    Py_ssize_t i, n, elements;
3322    int containers;
3323    int is_unpacking = 0;
3324    n = asdl_seq_LEN(e->v.Dict.values);
3325    containers = 0;
3326    elements = 0;
3327    for (i = 0; i < n; i++) {
3328        is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3329        if (elements == 0xFFFF || (elements && is_unpacking)) {
3330            if (!compiler_subdict(c, e, i - elements, i))
3331                return 0;
3332            containers++;
3333            elements = 0;
3334        }
3335        if (is_unpacking) {
3336            VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3337            containers++;
3338        }
3339        else {
3340            elements++;
3341        }
3342    }
3343    if (elements || containers == 0) {
3344        if (!compiler_subdict(c, e, n - elements, n))
3345            return 0;
3346        containers++;
3347    }
3348    /* If there is more than one dict, they need to be merged into a new
3349     * dict.  If there is one dict and it's an unpacking, then it needs
3350     * to be copied into a new dict." */
3351    while (containers > 1 || is_unpacking) {
3352        int oparg = containers < 255 ? containers : 255;
3353        ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3354        containers -= (oparg - 1);
3355        is_unpacking = 0;
3356    }
3357    return 1;
3358}
3359
3360static int
3361compiler_compare(struct compiler *c, expr_ty e)
3362{
3363    Py_ssize_t i, n;
3364    basicblock *cleanup = NULL;
3365
3366    /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3367    VISIT(c, expr, e->v.Compare.left);
3368    n = asdl_seq_LEN(e->v.Compare.ops);
3369    assert(n > 0);
3370    if (n > 1) {
3371        cleanup = compiler_new_block(c);
3372        if (cleanup == NULL)
3373            return 0;
3374        VISIT(c, expr,
3375            (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3376    }
3377    for (i = 1; i < n; i++) {
3378        ADDOP(c, DUP_TOP);
3379        ADDOP(c, ROT_THREE);
3380        ADDOP_I(c, COMPARE_OP,
3381            cmpop((cmpop_ty)(asdl_seq_GET(
3382                                      e->v.Compare.ops, i - 1))));
3383        ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3384        NEXT_BLOCK(c);
3385        if (i < (n - 1))
3386            VISIT(c, expr,
3387                (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3388    }
3389    VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
3390    ADDOP_I(c, COMPARE_OP,
3391           cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
3392    if (n > 1) {
3393        basicblock *end = compiler_new_block(c);
3394        if (end == NULL)
3395            return 0;
3396        ADDOP_JREL(c, JUMP_FORWARD, end);
3397        compiler_use_next_block(c, cleanup);
3398        ADDOP(c, ROT_TWO);
3399        ADDOP(c, POP_TOP);
3400        compiler_use_next_block(c, end);
3401    }
3402    return 1;
3403}
3404
3405static int
3406compiler_call(struct compiler *c, expr_ty e)
3407{
3408    VISIT(c, expr, e->v.Call.func);
3409    return compiler_call_helper(c, 0,
3410                                e->v.Call.args,
3411                                e->v.Call.keywords);
3412}
3413
3414static int
3415compiler_joined_str(struct compiler *c, expr_ty e)
3416{
3417    VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3418    if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3419        ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
3420    return 1;
3421}
3422
3423/* Used to implement f-strings. Format a single value. */
3424static int
3425compiler_formatted_value(struct compiler *c, expr_ty e)
3426{
3427    /* Our oparg encodes 2 pieces of information: the conversion
3428       character, and whether or not a format_spec was provided.
3429
3430       Convert the conversion char to 2 bits:
3431       None: 000  0x0  FVC_NONE
3432       !s  : 001  0x1  FVC_STR
3433       !r  : 010  0x2  FVC_REPR
3434       !a  : 011  0x3  FVC_ASCII
3435
3436       next bit is whether or not we have a format spec:
3437       yes : 100  0x4
3438       no  : 000  0x0
3439    */
3440
3441    int oparg;
3442
3443    /* Evaluate the expression to be formatted. */
3444    VISIT(c, expr, e->v.FormattedValue.value);
3445
3446    switch (e->v.FormattedValue.conversion) {
3447    case 's': oparg = FVC_STR;   break;
3448    case 'r': oparg = FVC_REPR;  break;
3449    case 'a': oparg = FVC_ASCII; break;
3450    case -1:  oparg = FVC_NONE;  break;
3451    default:
3452        PyErr_SetString(PyExc_SystemError,
3453                        "Unrecognized conversion character");
3454        return 0;
3455    }
3456    if (e->v.FormattedValue.format_spec) {
3457        /* Evaluate the format spec, and update our opcode arg. */
3458        VISIT(c, expr, e->v.FormattedValue.format_spec);
3459        oparg |= FVS_HAVE_SPEC;
3460    }
3461
3462    /* And push our opcode and oparg */
3463    ADDOP_I(c, FORMAT_VALUE, oparg);
3464    return 1;
3465}
3466
3467static int
3468compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
3469{
3470    Py_ssize_t i, n = end - begin;
3471    keyword_ty kw;
3472    PyObject *keys, *key;
3473    assert(n > 0);
3474    if (n > 1) {
3475        for (i = begin; i < end; i++) {
3476            kw = asdl_seq_GET(keywords, i);
3477            VISIT(c, expr, kw->value);
3478        }
3479        keys = PyTuple_New(n);
3480        if (keys == NULL) {
3481            return 0;
3482        }
3483        for (i = begin; i < end; i++) {
3484            key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3485            Py_INCREF(key);
3486            PyTuple_SET_ITEM(keys, i - begin, key);
3487        }
3488        ADDOP_N(c, LOAD_CONST, keys, consts);
3489        ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3490    }
3491    else {
3492        /* a for loop only executes once */
3493        for (i = begin; i < end; i++) {
3494            kw = asdl_seq_GET(keywords, i);
3495            ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3496            VISIT(c, expr, kw->value);
3497        }
3498        ADDOP_I(c, BUILD_MAP, n);
3499    }
3500    return 1;
3501}
3502
3503/* shared code between compiler_call and compiler_class */
3504static int
3505compiler_call_helper(struct compiler *c,
3506                     int n, /* Args already pushed */
3507                     asdl_seq *args,
3508                     asdl_seq *keywords)
3509{
3510    Py_ssize_t i, nseen, nelts, nkwelts;
3511    int mustdictunpack = 0;
3512
3513    /* the number of tuples and dictionaries on the stack */
3514    Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3515
3516    nelts = asdl_seq_LEN(args);
3517    nkwelts = asdl_seq_LEN(keywords);
3518
3519    for (i = 0; i < nkwelts; i++) {
3520        keyword_ty kw = asdl_seq_GET(keywords, i);
3521        if (kw->arg == NULL) {
3522            mustdictunpack = 1;
3523            break;
3524        }
3525    }
3526
3527    nseen = n;  /* the number of positional arguments on the stack */
3528    for (i = 0; i < nelts; i++) {
3529        expr_ty elt = asdl_seq_GET(args, i);
3530        if (elt->kind == Starred_kind) {
3531            /* A star-arg. If we've seen positional arguments,
3532               pack the positional arguments into a tuple. */
3533            if (nseen) {
3534                ADDOP_I(c, BUILD_TUPLE, nseen);
3535                nseen = 0;
3536                nsubargs++;
3537            }
3538            VISIT(c, expr, elt->v.Starred.value);
3539            nsubargs++;
3540        }
3541        else {
3542            VISIT(c, expr, elt);
3543            nseen++;
3544        }
3545    }
3546
3547    /* Same dance again for keyword arguments */
3548    if (nsubargs || mustdictunpack) {
3549        if (nseen) {
3550            /* Pack up any trailing positional arguments. */
3551            ADDOP_I(c, BUILD_TUPLE, nseen);
3552            nsubargs++;
3553        }
3554        if (nsubargs > 1) {
3555            /* If we ended up with more than one stararg, we need
3556               to concatenate them into a single sequence. */
3557            ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
3558        }
3559        else if (nsubargs == 0) {
3560            ADDOP_I(c, BUILD_TUPLE, 0);
3561        }
3562        nseen = 0;  /* the number of keyword arguments on the stack following */
3563        for (i = 0; i < nkwelts; i++) {
3564            keyword_ty kw = asdl_seq_GET(keywords, i);
3565            if (kw->arg == NULL) {
3566                /* A keyword argument unpacking. */
3567                if (nseen) {
3568                    if (!compiler_subkwargs(c, keywords, i - nseen, i))
3569                        return 0;
3570                    nsubkwargs++;
3571                    nseen = 0;
3572                }
3573                VISIT(c, expr, kw->value);
3574                nsubkwargs++;
3575            }
3576            else {
3577                nseen++;
3578            }
3579        }
3580        if (nseen) {
3581            /* Pack up any trailing keyword arguments. */
3582            if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
3583                return 0;
3584            nsubkwargs++;
3585        }
3586        if (nsubkwargs > 1) {
3587            /* Pack it all up */
3588            ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
3589        }
3590        ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3591        return 1;
3592    }
3593    else if (nkwelts) {
3594        PyObject *names;
3595        VISIT_SEQ(c, keyword, keywords);
3596        names = PyTuple_New(nkwelts);
3597        if (names == NULL) {
3598            return 0;
3599        }
3600        for (i = 0; i < nkwelts; i++) {
3601            keyword_ty kw = asdl_seq_GET(keywords, i);
3602            Py_INCREF(kw->arg);
3603            PyTuple_SET_ITEM(names, i, kw->arg);
3604        }
3605        ADDOP_N(c, LOAD_CONST, names, consts);
3606        ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3607        return 1;
3608    }
3609    else {
3610        ADDOP_I(c, CALL_FUNCTION, n + nelts);
3611        return 1;
3612    }
3613}
3614
3615
3616/* List and set comprehensions and generator expressions work by creating a
3617  nested function to perform the actual iteration. This means that the
3618  iteration variables don't leak into the current scope.
3619  The defined function is called immediately following its definition, with the
3620  result of that call being the result of the expression.
3621  The LC/SC version returns the populated container, while the GE version is
3622  flagged in symtable.c as a generator, so it returns the generator object
3623  when the function is called.
3624  This code *knows* that the loop cannot contain break, continue, or return,
3625  so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3626
3627  Possible cleanups:
3628    - iterate over the generator sequence instead of using recursion
3629*/
3630
3631
3632static int
3633compiler_comprehension_generator(struct compiler *c,
3634                                 asdl_seq *generators, int gen_index,
3635                                 expr_ty elt, expr_ty val, int type)
3636{
3637    comprehension_ty gen;
3638    gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3639    if (gen->is_async) {
3640        return compiler_async_comprehension_generator(
3641            c, generators, gen_index, elt, val, type);
3642    } else {
3643        return compiler_sync_comprehension_generator(
3644            c, generators, gen_index, elt, val, type);
3645    }
3646}
3647
3648static int
3649compiler_sync_comprehension_generator(struct compiler *c,
3650                                      asdl_seq *generators, int gen_index,
3651                                      expr_ty elt, expr_ty val, int type)
3652{
3653    /* generate code for the iterator, then each of the ifs,
3654       and then write to the element */
3655
3656    comprehension_ty gen;
3657    basicblock *start, *anchor, *skip, *if_cleanup;
3658    Py_ssize_t i, n;
3659
3660    start = compiler_new_block(c);
3661    skip = compiler_new_block(c);
3662    if_cleanup = compiler_new_block(c);
3663    anchor = compiler_new_block(c);
3664
3665    if (start == NULL || skip == NULL || if_cleanup == NULL ||
3666        anchor == NULL)
3667        return 0;
3668
3669    gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3670
3671    if (gen_index == 0) {
3672        /* Receive outermost iter as an implicit argument */
3673        c->u->u_argcount = 1;
3674        ADDOP_I(c, LOAD_FAST, 0);
3675    }
3676    else {
3677        /* Sub-iter - calculate on the fly */
3678        VISIT(c, expr, gen->iter);
3679        ADDOP(c, GET_ITER);
3680    }
3681    compiler_use_next_block(c, start);
3682    ADDOP_JREL(c, FOR_ITER, anchor);
3683    NEXT_BLOCK(c);
3684    VISIT(c, expr, gen->target);
3685
3686    /* XXX this needs to be cleaned up...a lot! */
3687    n = asdl_seq_LEN(gen->ifs);
3688    for (i = 0; i < n; i++) {
3689        expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3690        VISIT(c, expr, e);
3691        ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3692        NEXT_BLOCK(c);
3693    }
3694
3695    if (++gen_index < asdl_seq_LEN(generators))
3696        if (!compiler_comprehension_generator(c,
3697                                              generators, gen_index,
3698                                              elt, val, type))
3699        return 0;
3700
3701    /* only append after the last for generator */
3702    if (gen_index >= asdl_seq_LEN(generators)) {
3703        /* comprehension specific code */
3704        switch (type) {
3705        case COMP_GENEXP:
3706            VISIT(c, expr, elt);
3707            ADDOP(c, YIELD_VALUE);
3708            ADDOP(c, POP_TOP);
3709            break;
3710        case COMP_LISTCOMP:
3711            VISIT(c, expr, elt);
3712            ADDOP_I(c, LIST_APPEND, gen_index + 1);
3713            break;
3714        case COMP_SETCOMP:
3715            VISIT(c, expr, elt);
3716            ADDOP_I(c, SET_ADD, gen_index + 1);
3717            break;
3718        case COMP_DICTCOMP:
3719            /* With 'd[k] = v', v is evaluated before k, so we do
3720               the same. */
3721            VISIT(c, expr, val);
3722            VISIT(c, expr, elt);
3723            ADDOP_I(c, MAP_ADD, gen_index + 1);
3724            break;
3725        default:
3726            return 0;
3727        }
3728
3729        compiler_use_next_block(c, skip);
3730    }
3731    compiler_use_next_block(c, if_cleanup);
3732    ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3733    compiler_use_next_block(c, anchor);
3734
3735    return 1;
3736}
3737
3738static int
3739compiler_async_comprehension_generator(struct compiler *c,
3740                                      asdl_seq *generators, int gen_index,
3741                                      expr_ty elt, expr_ty val, int type)
3742{
3743    _Py_IDENTIFIER(StopAsyncIteration);
3744
3745    comprehension_ty gen;
3746    basicblock *anchor, *skip, *if_cleanup, *try,
3747               *after_try, *except, *try_cleanup;
3748    Py_ssize_t i, n;
3749
3750    PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
3751    if (stop_aiter_error == NULL) {
3752        return 0;
3753    }
3754
3755    try = compiler_new_block(c);
3756    after_try = compiler_new_block(c);
3757    try_cleanup = compiler_new_block(c);
3758    except = compiler_new_block(c);
3759    skip = compiler_new_block(c);
3760    if_cleanup = compiler_new_block(c);
3761    anchor = compiler_new_block(c);
3762
3763    if (skip == NULL || if_cleanup == NULL || anchor == NULL ||
3764            try == NULL || after_try == NULL ||
3765            except == NULL || after_try == NULL) {
3766        return 0;
3767    }
3768
3769    gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3770
3771    if (gen_index == 0) {
3772        /* Receive outermost iter as an implicit argument */
3773        c->u->u_argcount = 1;
3774        ADDOP_I(c, LOAD_FAST, 0);
3775    }
3776    else {
3777        /* Sub-iter - calculate on the fly */
3778        VISIT(c, expr, gen->iter);
3779        ADDOP(c, GET_AITER);
3780        ADDOP_O(c, LOAD_CONST, Py_None, consts);
3781        ADDOP(c, YIELD_FROM);
3782    }
3783
3784    compiler_use_next_block(c, try);
3785
3786
3787    ADDOP_JREL(c, SETUP_EXCEPT, except);
3788    if (!compiler_push_fblock(c, EXCEPT, try))
3789        return 0;
3790
3791    ADDOP(c, GET_ANEXT);
3792    ADDOP_O(c, LOAD_CONST, Py_None, consts);
3793    ADDOP(c, YIELD_FROM);
3794    VISIT(c, expr, gen->target);
3795    ADDOP(c, POP_BLOCK);
3796    compiler_pop_fblock(c, EXCEPT, try);
3797    ADDOP_JREL(c, JUMP_FORWARD, after_try);
3798
3799
3800    compiler_use_next_block(c, except);
3801    ADDOP(c, DUP_TOP);
3802    ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
3803    ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
3804    ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
3805
3806    ADDOP(c, POP_TOP);
3807    ADDOP(c, POP_TOP);
3808    ADDOP(c, POP_TOP);
3809    ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
3810    ADDOP_JABS(c, JUMP_ABSOLUTE, anchor);
3811
3812
3813    compiler_use_next_block(c, try_cleanup);
3814    ADDOP(c, END_FINALLY);
3815
3816    compiler_use_next_block(c, after_try);
3817
3818    n = asdl_seq_LEN(gen->ifs);
3819    for (i = 0; i < n; i++) {
3820        expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3821        VISIT(c, expr, e);
3822        ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3823        NEXT_BLOCK(c);
3824    }
3825
3826    if (++gen_index < asdl_seq_LEN(generators))
3827        if (!compiler_comprehension_generator(c,
3828                                              generators, gen_index,
3829                                              elt, val, type))
3830        return 0;
3831
3832    /* only append after the last for generator */
3833    if (gen_index >= asdl_seq_LEN(generators)) {
3834        /* comprehension specific code */
3835        switch (type) {
3836        case COMP_GENEXP:
3837            VISIT(c, expr, elt);
3838            ADDOP(c, YIELD_VALUE);
3839            ADDOP(c, POP_TOP);
3840            break;
3841        case COMP_LISTCOMP:
3842            VISIT(c, expr, elt);
3843            ADDOP_I(c, LIST_APPEND, gen_index + 1);
3844            break;
3845        case COMP_SETCOMP:
3846            VISIT(c, expr, elt);
3847            ADDOP_I(c, SET_ADD, gen_index + 1);
3848            break;
3849        case COMP_DICTCOMP:
3850            /* With 'd[k] = v', v is evaluated before k, so we do
3851               the same. */
3852            VISIT(c, expr, val);
3853            VISIT(c, expr, elt);
3854            ADDOP_I(c, MAP_ADD, gen_index + 1);
3855            break;
3856        default:
3857            return 0;
3858        }
3859
3860        compiler_use_next_block(c, skip);
3861    }
3862    compiler_use_next_block(c, if_cleanup);
3863    ADDOP_JABS(c, JUMP_ABSOLUTE, try);
3864    compiler_use_next_block(c, anchor);
3865    ADDOP(c, POP_TOP);
3866
3867    return 1;
3868}
3869
3870static int
3871compiler_comprehension(struct compiler *c, expr_ty e, int type,
3872                       identifier name, asdl_seq *generators, expr_ty elt,
3873                       expr_ty val)
3874{
3875    PyCodeObject *co = NULL;
3876    comprehension_ty outermost;
3877    PyObject *qualname = NULL;
3878    int is_async_function = c->u->u_ste->ste_coroutine;
3879    int is_async_generator = 0;
3880
3881    outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
3882
3883    if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3884                              (void *)e, e->lineno))
3885    {
3886        goto error;
3887    }
3888
3889    is_async_generator = c->u->u_ste->ste_coroutine;
3890
3891    if (is_async_generator && !is_async_function) {
3892        if (e->lineno > c->u->u_lineno) {
3893            c->u->u_lineno = e->lineno;
3894            c->u->u_lineno_set = 0;
3895        }
3896        compiler_error(c, "asynchronous comprehension outside of "
3897                          "an asynchronous function");
3898        goto error_in_scope;
3899    }
3900
3901    if (type != COMP_GENEXP) {
3902        int op;
3903        switch (type) {
3904        case COMP_LISTCOMP:
3905            op = BUILD_LIST;
3906            break;
3907        case COMP_SETCOMP:
3908            op = BUILD_SET;
3909            break;
3910        case COMP_DICTCOMP:
3911            op = BUILD_MAP;
3912            break;
3913        default:
3914            PyErr_Format(PyExc_SystemError,
3915                         "unknown comprehension type %d", type);
3916            goto error_in_scope;
3917        }
3918
3919        ADDOP_I(c, op, 0);
3920    }
3921
3922    if (!compiler_comprehension_generator(c, generators, 0, elt,
3923                                          val, type))
3924        goto error_in_scope;
3925
3926    if (type != COMP_GENEXP) {
3927        ADDOP(c, RETURN_VALUE);
3928    }
3929
3930    co = assemble(c, 1);
3931    qualname = c->u->u_qualname;
3932    Py_INCREF(qualname);
3933    compiler_exit_scope(c);
3934    if (co == NULL)
3935        goto error;
3936
3937    if (!compiler_make_closure(c, co, 0, qualname))
3938        goto error;
3939    Py_DECREF(qualname);
3940    Py_DECREF(co);
3941
3942    VISIT(c, expr, outermost->iter);
3943
3944    if (outermost->is_async) {
3945        ADDOP(c, GET_AITER);
3946        ADDOP_O(c, LOAD_CONST, Py_None, consts);
3947        ADDOP(c, YIELD_FROM);
3948    } else {
3949        ADDOP(c, GET_ITER);
3950    }
3951
3952    ADDOP_I(c, CALL_FUNCTION, 1);
3953
3954    if (is_async_generator && type != COMP_GENEXP) {
3955        ADDOP(c, GET_AWAITABLE);
3956        ADDOP_O(c, LOAD_CONST, Py_None, consts);
3957        ADDOP(c, YIELD_FROM);
3958    }
3959
3960    return 1;
3961error_in_scope:
3962    compiler_exit_scope(c);
3963error:
3964    Py_XDECREF(qualname);
3965    Py_XDECREF(co);
3966    return 0;
3967}
3968
3969static int
3970compiler_genexp(struct compiler *c, expr_ty e)
3971{
3972    static identifier name;
3973    if (!name) {
3974        name = PyUnicode_FromString("<genexpr>");
3975        if (!name)
3976            return 0;
3977    }
3978    assert(e->kind == GeneratorExp_kind);
3979    return compiler_comprehension(c, e, COMP_GENEXP, name,
3980                                  e->v.GeneratorExp.generators,
3981                                  e->v.GeneratorExp.elt, NULL);
3982}
3983
3984static int
3985compiler_listcomp(struct compiler *c, expr_ty e)
3986{
3987    static identifier name;
3988    if (!name) {
3989        name = PyUnicode_FromString("<listcomp>");
3990        if (!name)
3991            return 0;
3992    }
3993    assert(e->kind == ListComp_kind);
3994    return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3995                                  e->v.ListComp.generators,
3996                                  e->v.ListComp.elt, NULL);
3997}
3998
3999static int
4000compiler_setcomp(struct compiler *c, expr_ty e)
4001{
4002    static identifier name;
4003    if (!name) {
4004        name = PyUnicode_FromString("<setcomp>");
4005        if (!name)
4006            return 0;
4007    }
4008    assert(e->kind == SetComp_kind);
4009    return compiler_comprehension(c, e, COMP_SETCOMP, name,
4010                                  e->v.SetComp.generators,
4011                                  e->v.SetComp.elt, NULL);
4012}
4013
4014
4015static int
4016compiler_dictcomp(struct compiler *c, expr_ty e)
4017{
4018    static identifier name;
4019    if (!name) {
4020        name = PyUnicode_FromString("<dictcomp>");
4021        if (!name)
4022            return 0;
4023    }
4024    assert(e->kind == DictComp_kind);
4025    return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4026                                  e->v.DictComp.generators,
4027                                  e->v.DictComp.key, e->v.DictComp.value);
4028}
4029
4030
4031static int
4032compiler_visit_keyword(struct compiler *c, keyword_ty k)
4033{
4034    VISIT(c, expr, k->value);
4035    return 1;
4036}
4037
4038/* Test whether expression is constant.  For constants, report
4039   whether they are true or false.
4040
4041   Return values: 1 for true, 0 for false, -1 for non-constant.
4042 */
4043
4044static int
4045expr_constant(struct compiler *c, expr_ty e)
4046{
4047    char *id;
4048    switch (e->kind) {
4049    case Ellipsis_kind:
4050        return 1;
4051    case Constant_kind:
4052        return PyObject_IsTrue(e->v.Constant.value);
4053    case Num_kind:
4054        return PyObject_IsTrue(e->v.Num.n);
4055    case Str_kind:
4056        return PyObject_IsTrue(e->v.Str.s);
4057    case Name_kind:
4058        /* optimize away names that can't be reassigned */
4059        id = PyUnicode_AsUTF8(e->v.Name.id);
4060        if (id && strcmp(id, "__debug__") == 0)
4061            return !c->c_optimize;
4062        return -1;
4063    case NameConstant_kind: {
4064        PyObject *o = e->v.NameConstant.value;
4065        if (o == Py_None)
4066            return 0;
4067        else if (o == Py_True)
4068            return 1;
4069        else if (o == Py_False)
4070            return 0;
4071    }
4072    default:
4073        return -1;
4074    }
4075}
4076
4077
4078/*
4079   Implements the async with statement.
4080
4081   The semantics outlined in that PEP are as follows:
4082
4083   async with EXPR as VAR:
4084       BLOCK
4085
4086   It is implemented roughly as:
4087
4088   context = EXPR
4089   exit = context.__aexit__  # not calling it
4090   value = await context.__aenter__()
4091   try:
4092       VAR = value  # if VAR present in the syntax
4093       BLOCK
4094   finally:
4095       if an exception was raised:
4096           exc = copy of (exception, instance, traceback)
4097       else:
4098           exc = (None, None, None)
4099       if not (await exit(*exc)):
4100           raise
4101 */
4102static int
4103compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4104{
4105    basicblock *block, *finally;
4106    withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4107
4108    assert(s->kind == AsyncWith_kind);
4109
4110    block = compiler_new_block(c);
4111    finally = compiler_new_block(c);
4112    if (!block || !finally)
4113        return 0;
4114
4115    /* Evaluate EXPR */
4116    VISIT(c, expr, item->context_expr);
4117
4118    ADDOP(c, BEFORE_ASYNC_WITH);
4119    ADDOP(c, GET_AWAITABLE);
4120    ADDOP_O(c, LOAD_CONST, Py_None, consts);
4121    ADDOP(c, YIELD_FROM);
4122
4123    ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4124
4125    /* SETUP_ASYNC_WITH pushes a finally block. */
4126    compiler_use_next_block(c, block);
4127    if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
4128        return 0;
4129    }
4130
4131    if (item->optional_vars) {
4132        VISIT(c, expr, item->optional_vars);
4133    }
4134    else {
4135    /* Discard result from context.__aenter__() */
4136        ADDOP(c, POP_TOP);
4137    }
4138
4139    pos++;
4140    if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4141        /* BLOCK code */
4142        VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4143    else if (!compiler_async_with(c, s, pos))
4144            return 0;
4145
4146    /* End of try block; start the finally block */
4147    ADDOP(c, POP_BLOCK);
4148    compiler_pop_fblock(c, FINALLY_TRY, block);
4149
4150    ADDOP_O(c, LOAD_CONST, Py_None, consts);
4151    compiler_use_next_block(c, finally);
4152    if (!compiler_push_fblock(c, FINALLY_END, finally))
4153        return 0;
4154
4155    /* Finally block starts; context.__exit__ is on the stack under
4156       the exception or return information. Just issue our magic
4157       opcode. */
4158    ADDOP(c, WITH_CLEANUP_START);
4159
4160    ADDOP(c, GET_AWAITABLE);
4161    ADDOP_O(c, LOAD_CONST, Py_None, consts);
4162    ADDOP(c, YIELD_FROM);
4163
4164    ADDOP(c, WITH_CLEANUP_FINISH);
4165
4166    /* Finally block ends. */
4167    ADDOP(c, END_FINALLY);
4168    compiler_pop_fblock(c, FINALLY_END, finally);
4169    return 1;
4170}
4171
4172
4173/*
4174   Implements the with statement from PEP 343.
4175
4176   The semantics outlined in that PEP are as follows:
4177
4178   with EXPR as VAR:
4179       BLOCK
4180
4181   It is implemented roughly as:
4182
4183   context = EXPR
4184   exit = context.__exit__  # not calling it
4185   value = context.__enter__()
4186   try:
4187       VAR = value  # if VAR present in the syntax
4188       BLOCK
4189   finally:
4190       if an exception was raised:
4191           exc = copy of (exception, instance, traceback)
4192       else:
4193           exc = (None, None, None)
4194       exit(*exc)
4195 */
4196static int
4197compiler_with(struct compiler *c, stmt_ty s, int pos)
4198{
4199    basicblock *block, *finally;
4200    withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
4201
4202    assert(s->kind == With_kind);
4203
4204    block = compiler_new_block(c);
4205    finally = compiler_new_block(c);
4206    if (!block || !finally)
4207        return 0;
4208
4209    /* Evaluate EXPR */
4210    VISIT(c, expr, item->context_expr);
4211    ADDOP_JREL(c, SETUP_WITH, finally);
4212
4213    /* SETUP_WITH pushes a finally block. */
4214    compiler_use_next_block(c, block);
4215    if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
4216        return 0;
4217    }
4218
4219    if (item->optional_vars) {
4220        VISIT(c, expr, item->optional_vars);
4221    }
4222    else {
4223    /* Discard result from context.__enter__() */
4224        ADDOP(c, POP_TOP);
4225    }
4226
4227    pos++;
4228    if (pos == asdl_seq_LEN(s->v.With.items))
4229        /* BLOCK code */
4230        VISIT_SEQ(c, stmt, s->v.With.body)
4231    else if (!compiler_with(c, s, pos))
4232            return 0;
4233
4234    /* End of try block; start the finally block */
4235    ADDOP(c, POP_BLOCK);
4236    compiler_pop_fblock(c, FINALLY_TRY, block);
4237
4238    ADDOP_O(c, LOAD_CONST, Py_None, consts);
4239    compiler_use_next_block(c, finally);
4240    if (!compiler_push_fblock(c, FINALLY_END, finally))
4241        return 0;
4242
4243    /* Finally block starts; context.__exit__ is on the stack under
4244       the exception or return information. Just issue our magic
4245       opcode. */
4246    ADDOP(c, WITH_CLEANUP_START);
4247    ADDOP(c, WITH_CLEANUP_FINISH);
4248
4249    /* Finally block ends. */
4250    ADDOP(c, END_FINALLY);
4251    compiler_pop_fblock(c, FINALLY_END, finally);
4252    return 1;
4253}
4254
4255static int
4256compiler_visit_expr(struct compiler *c, expr_ty e)
4257{
4258    /* If expr e has a different line number than the last expr/stmt,
4259       set a new line number for the next instruction.
4260    */
4261    if (e->lineno > c->u->u_lineno) {
4262        c->u->u_lineno = e->lineno;
4263        c->u->u_lineno_set = 0;
4264    }
4265    /* Updating the column offset is always harmless. */
4266    c->u->u_col_offset = e->col_offset;
4267    switch (e->kind) {
4268    case BoolOp_kind:
4269        return compiler_boolop(c, e);
4270    case BinOp_kind:
4271        VISIT(c, expr, e->v.BinOp.left);
4272        VISIT(c, expr, e->v.BinOp.right);
4273        ADDOP(c, binop(c, e->v.BinOp.op));
4274        break;
4275    case UnaryOp_kind:
4276        VISIT(c, expr, e->v.UnaryOp.operand);
4277        ADDOP(c, unaryop(e->v.UnaryOp.op));
4278        break;
4279    case Lambda_kind:
4280        return compiler_lambda(c, e);
4281    case IfExp_kind:
4282        return compiler_ifexp(c, e);
4283    case Dict_kind:
4284        return compiler_dict(c, e);
4285    case Set_kind:
4286        return compiler_set(c, e);
4287    case GeneratorExp_kind:
4288        return compiler_genexp(c, e);
4289    case ListComp_kind:
4290        return compiler_listcomp(c, e);
4291    case SetComp_kind:
4292        return compiler_setcomp(c, e);
4293    case DictComp_kind:
4294        return compiler_dictcomp(c, e);
4295    case Yield_kind:
4296        if (c->u->u_ste->ste_type != FunctionBlock)
4297            return compiler_error(c, "'yield' outside function");
4298        if (e->v.Yield.value) {
4299            VISIT(c, expr, e->v.Yield.value);
4300        }
4301        else {
4302            ADDOP_O(c, LOAD_CONST, Py_None, consts);
4303        }
4304        ADDOP(c, YIELD_VALUE);
4305        break;
4306    case YieldFrom_kind:
4307        if (c->u->u_ste->ste_type != FunctionBlock)
4308            return compiler_error(c, "'yield' outside function");
4309
4310        if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4311            return compiler_error(c, "'yield from' inside async function");
4312
4313        VISIT(c, expr, e->v.YieldFrom.value);
4314        ADDOP(c, GET_YIELD_FROM_ITER);
4315        ADDOP_O(c, LOAD_CONST, Py_None, consts);
4316        ADDOP(c, YIELD_FROM);
4317        break;
4318    case Await_kind:
4319        if (c->u->u_ste->ste_type != FunctionBlock)
4320            return compiler_error(c, "'await' outside function");
4321
4322        if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4323                c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
4324            return compiler_error(c, "'await' outside async function");
4325
4326        VISIT(c, expr, e->v.Await.value);
4327        ADDOP(c, GET_AWAITABLE);
4328        ADDOP_O(c, LOAD_CONST, Py_None, consts);
4329        ADDOP(c, YIELD_FROM);
4330        break;
4331    case Compare_kind:
4332        return compiler_compare(c, e);
4333    case Call_kind:
4334        return compiler_call(c, e);
4335    case Constant_kind:
4336        ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
4337        break;
4338    case Num_kind:
4339        ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
4340        break;
4341    case Str_kind:
4342        ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
4343        break;
4344    case JoinedStr_kind:
4345        return compiler_joined_str(c, e);
4346    case FormattedValue_kind:
4347        return compiler_formatted_value(c, e);
4348    case Bytes_kind:
4349        ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
4350        break;
4351    case Ellipsis_kind:
4352        ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
4353        break;
4354    case NameConstant_kind:
4355        ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
4356        break;
4357    /* The following exprs can be assignment targets. */
4358    case Attribute_kind:
4359        if (e->v.Attribute.ctx != AugStore)
4360            VISIT(c, expr, e->v.Attribute.value);
4361        switch (e->v.Attribute.ctx) {
4362        case AugLoad:
4363            ADDOP(c, DUP_TOP);
4364            /* Fall through to load */
4365        case Load:
4366            ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
4367            break;
4368        case AugStore:
4369            ADDOP(c, ROT_TWO);
4370            /* Fall through to save */
4371        case Store:
4372            ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
4373            break;
4374        case Del:
4375            ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
4376            break;
4377        case Param:
4378        default:
4379            PyErr_SetString(PyExc_SystemError,
4380                            "param invalid in attribute expression");
4381            return 0;
4382        }
4383        break;
4384    case Subscript_kind:
4385        switch (e->v.Subscript.ctx) {
4386        case AugLoad:
4387            VISIT(c, expr, e->v.Subscript.value);
4388            VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
4389            break;
4390        case Load:
4391            VISIT(c, expr, e->v.Subscript.value);
4392            VISIT_SLICE(c, e->v.Subscript.slice, Load);
4393            break;
4394        case AugStore:
4395            VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
4396            break;
4397        case Store:
4398            VISIT(c, expr, e->v.Subscript.value);
4399            VISIT_SLICE(c, e->v.Subscript.slice, Store);
4400            break;
4401        case Del:
4402            VISIT(c, expr, e->v.Subscript.value);
4403            VISIT_SLICE(c, e->v.Subscript.slice, Del);
4404            break;
4405        case Param:
4406        default:
4407            PyErr_SetString(PyExc_SystemError,
4408                "param invalid in subscript expression");
4409            return 0;
4410        }
4411        break;
4412    case Starred_kind:
4413        switch (e->v.Starred.ctx) {
4414        case Store:
4415            /* In all legitimate cases, the Starred node was already replaced
4416             * by compiler_list/compiler_tuple. XXX: is that okay? */
4417            return compiler_error(c,
4418                "starred assignment target must be in a list or tuple");
4419        default:
4420            return compiler_error(c,
4421                "can't use starred expression here");
4422        }
4423        break;
4424    case Name_kind:
4425        return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4426    /* child nodes of List and Tuple will have expr_context set */
4427    case List_kind:
4428        return compiler_list(c, e);
4429    case Tuple_kind:
4430        return compiler_tuple(c, e);
4431    }
4432    return 1;
4433}
4434
4435static int
4436compiler_augassign(struct compiler *c, stmt_ty s)
4437{
4438    expr_ty e = s->v.AugAssign.target;
4439    expr_ty auge;
4440
4441    assert(s->kind == AugAssign_kind);
4442
4443    switch (e->kind) {
4444    case Attribute_kind:
4445        auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4446                         AugLoad, e->lineno, e->col_offset, c->c_arena);
4447        if (auge == NULL)
4448            return 0;
4449        VISIT(c, expr, auge);
4450        VISIT(c, expr, s->v.AugAssign.value);
4451        ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4452        auge->v.Attribute.ctx = AugStore;
4453        VISIT(c, expr, auge);
4454        break;
4455    case Subscript_kind:
4456        auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4457                         AugLoad, e->lineno, e->col_offset, c->c_arena);
4458        if (auge == NULL)
4459            return 0;
4460        VISIT(c, expr, auge);
4461        VISIT(c, expr, s->v.AugAssign.value);
4462        ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4463        auge->v.Subscript.ctx = AugStore;
4464        VISIT(c, expr, auge);
4465        break;
4466    case Name_kind:
4467        if (!compiler_nameop(c, e->v.Name.id, Load))
4468            return 0;
4469        VISIT(c, expr, s->v.AugAssign.value);
4470        ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4471        return compiler_nameop(c, e->v.Name.id, Store);
4472    default:
4473        PyErr_Format(PyExc_SystemError,
4474            "invalid node type (%d) for augmented assignment",
4475            e->kind);
4476        return 0;
4477    }
4478    return 1;
4479}
4480
4481static int
4482check_ann_expr(struct compiler *c, expr_ty e)
4483{
4484    VISIT(c, expr, e);
4485    ADDOP(c, POP_TOP);
4486    return 1;
4487}
4488
4489static int
4490check_annotation(struct compiler *c, stmt_ty s)
4491{
4492    /* Annotations are only evaluated in a module or class. */
4493    if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4494        c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4495        return check_ann_expr(c, s->v.AnnAssign.annotation);
4496    }
4497    return 1;
4498}
4499
4500static int
4501check_ann_slice(struct compiler *c, slice_ty sl)
4502{
4503    switch(sl->kind) {
4504    case Index_kind:
4505        return check_ann_expr(c, sl->v.Index.value);
4506    case Slice_kind:
4507        if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4508            return 0;
4509        }
4510        if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4511            return 0;
4512        }
4513        if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4514            return 0;
4515        }
4516        break;
4517    default:
4518        PyErr_SetString(PyExc_SystemError,
4519                        "unexpected slice kind");
4520        return 0;
4521    }
4522    return 1;
4523}
4524
4525static int
4526check_ann_subscr(struct compiler *c, slice_ty sl)
4527{
4528    /* We check that everything in a subscript is defined at runtime. */
4529    Py_ssize_t i, n;
4530
4531    switch (sl->kind) {
4532    case Index_kind:
4533    case Slice_kind:
4534        if (!check_ann_slice(c, sl)) {
4535            return 0;
4536        }
4537        break;
4538    case ExtSlice_kind:
4539        n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4540        for (i = 0; i < n; i++) {
4541            slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4542            switch (subsl->kind) {
4543            case Index_kind:
4544            case Slice_kind:
4545                if (!check_ann_slice(c, subsl)) {
4546                    return 0;
4547                }
4548                break;
4549            case ExtSlice_kind:
4550            default:
4551                PyErr_SetString(PyExc_SystemError,
4552                                "extended slice invalid in nested slice");
4553                return 0;
4554            }
4555        }
4556        break;
4557    default:
4558        PyErr_Format(PyExc_SystemError,
4559                     "invalid subscript kind %d", sl->kind);
4560        return 0;
4561    }
4562    return 1;
4563}
4564
4565static int
4566compiler_annassign(struct compiler *c, stmt_ty s)
4567{
4568    expr_ty targ = s->v.AnnAssign.target;
4569    PyObject* mangled;
4570
4571    assert(s->kind == AnnAssign_kind);
4572
4573    /* We perform the actual assignment first. */
4574    if (s->v.AnnAssign.value) {
4575        VISIT(c, expr, s->v.AnnAssign.value);
4576        VISIT(c, expr, targ);
4577    }
4578    switch (targ->kind) {
4579    case Name_kind:
4580        /* If we have a simple name in a module or class, store annotation. */
4581        if (s->v.AnnAssign.simple &&
4582            (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4583             c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
4584            mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4585            if (!mangled) {
4586                return 0;
4587            }
4588            VISIT(c, expr, s->v.AnnAssign.annotation);
4589            /* ADDOP_N decrefs its argument */
4590            ADDOP_N(c, STORE_ANNOTATION, mangled, names);
4591        }
4592        break;
4593    case Attribute_kind:
4594        if (!s->v.AnnAssign.value &&
4595            !check_ann_expr(c, targ->v.Attribute.value)) {
4596            return 0;
4597        }
4598        break;
4599    case Subscript_kind:
4600        if (!s->v.AnnAssign.value &&
4601            (!check_ann_expr(c, targ->v.Subscript.value) ||
4602             !check_ann_subscr(c, targ->v.Subscript.slice))) {
4603                return 0;
4604        }
4605        break;
4606    default:
4607        PyErr_Format(PyExc_SystemError,
4608                     "invalid node type (%d) for annotated assignment",
4609                     targ->kind);
4610            return 0;
4611    }
4612    /* Annotation is evaluated last. */
4613    if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4614        return 0;
4615    }
4616    return 1;
4617}
4618
4619static int
4620compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4621{
4622    struct fblockinfo *f;
4623    if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4624        PyErr_SetString(PyExc_SyntaxError,
4625                        "too many statically nested blocks");
4626        return 0;
4627    }
4628    f = &c->u->u_fblock[c->u->u_nfblocks++];
4629    f->fb_type = t;
4630    f->fb_block = b;
4631    return 1;
4632}
4633
4634static void
4635compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4636{
4637    struct compiler_unit *u = c->u;
4638    assert(u->u_nfblocks > 0);
4639    u->u_nfblocks--;
4640    assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4641    assert(u->u_fblock[u->u_nfblocks].fb_block == b);
4642}
4643
4644static int
4645compiler_in_loop(struct compiler *c) {
4646    int i;
4647    struct compiler_unit *u = c->u;
4648    for (i = 0; i < u->u_nfblocks; ++i) {
4649        if (u->u_fblock[i].fb_type == LOOP)
4650            return 1;
4651    }
4652    return 0;
4653}
4654/* Raises a SyntaxError and returns 0.
4655   If something goes wrong, a different exception may be raised.
4656*/
4657
4658static int
4659compiler_error(struct compiler *c, const char *errstr)
4660{
4661    PyObject *loc;
4662    PyObject *u = NULL, *v = NULL;
4663
4664    loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
4665    if (!loc) {
4666        Py_INCREF(Py_None);
4667        loc = Py_None;
4668    }
4669    u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
4670                      c->u->u_col_offset, loc);
4671    if (!u)
4672        goto exit;
4673    v = Py_BuildValue("(zO)", errstr, u);
4674    if (!v)
4675        goto exit;
4676    PyErr_SetObject(PyExc_SyntaxError, v);
4677 exit:
4678    Py_DECREF(loc);
4679    Py_XDECREF(u);
4680    Py_XDECREF(v);
4681    return 0;
4682}
4683
4684static int
4685compiler_handle_subscr(struct compiler *c, const char *kind,
4686                       expr_context_ty ctx)
4687{
4688    int op = 0;
4689
4690    /* XXX this code is duplicated */
4691    switch (ctx) {
4692        case AugLoad: /* fall through to Load */
4693        case Load:    op = BINARY_SUBSCR; break;
4694        case AugStore:/* fall through to Store */
4695        case Store:   op = STORE_SUBSCR; break;
4696        case Del:     op = DELETE_SUBSCR; break;
4697        case Param:
4698            PyErr_Format(PyExc_SystemError,
4699                         "invalid %s kind %d in subscript\n",
4700                         kind, ctx);
4701            return 0;
4702    }
4703    if (ctx == AugLoad) {
4704        ADDOP(c, DUP_TOP_TWO);
4705    }
4706    else if (ctx == AugStore) {
4707        ADDOP(c, ROT_THREE);
4708    }
4709    ADDOP(c, op);
4710    return 1;
4711}
4712
4713static int
4714compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4715{
4716    int n = 2;
4717    assert(s->kind == Slice_kind);
4718
4719    /* only handles the cases where BUILD_SLICE is emitted */
4720    if (s->v.Slice.lower) {
4721        VISIT(c, expr, s->v.Slice.lower);
4722    }
4723    else {
4724        ADDOP_O(c, LOAD_CONST, Py_None, consts);
4725    }
4726
4727    if (s->v.Slice.upper) {
4728        VISIT(c, expr, s->v.Slice.upper);
4729    }
4730    else {
4731        ADDOP_O(c, LOAD_CONST, Py_None, consts);
4732    }
4733
4734    if (s->v.Slice.step) {
4735        n++;
4736        VISIT(c, expr, s->v.Slice.step);
4737    }
4738    ADDOP_I(c, BUILD_SLICE, n);
4739    return 1;
4740}
4741
4742static int
4743compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4744                            expr_context_ty ctx)
4745{
4746    switch (s->kind) {
4747    case Slice_kind:
4748        return compiler_slice(c, s, ctx);
4749    case Index_kind:
4750        VISIT(c, expr, s->v.Index.value);
4751        break;
4752    case ExtSlice_kind:
4753    default:
4754        PyErr_SetString(PyExc_SystemError,
4755                        "extended slice invalid in nested slice");
4756        return 0;
4757    }
4758    return 1;
4759}
4760
4761static int
4762compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4763{
4764    char * kindname = NULL;
4765    switch (s->kind) {
4766    case Index_kind:
4767        kindname = "index";
4768        if (ctx != AugStore) {
4769            VISIT(c, expr, s->v.Index.value);
4770        }
4771        break;
4772    case Slice_kind:
4773        kindname = "slice";
4774        if (ctx != AugStore) {
4775            if (!compiler_slice(c, s, ctx))
4776                return 0;
4777        }
4778        break;
4779    case ExtSlice_kind:
4780        kindname = "extended slice";
4781        if (ctx != AugStore) {
4782            Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
4783            for (i = 0; i < n; i++) {
4784                slice_ty sub = (slice_ty)asdl_seq_GET(
4785                    s->v.ExtSlice.dims, i);
4786                if (!compiler_visit_nested_slice(c, sub, ctx))
4787                    return 0;
4788            }
4789            ADDOP_I(c, BUILD_TUPLE, n);
4790        }
4791        break;
4792    default:
4793        PyErr_Format(PyExc_SystemError,
4794                     "invalid subscript kind %d", s->kind);
4795        return 0;
4796    }
4797    return compiler_handle_subscr(c, kindname, ctx);
4798}
4799
4800/* End of the compiler section, beginning of the assembler section */
4801
4802/* do depth-first search of basic block graph, starting with block.
4803   post records the block indices in post-order.
4804
4805   XXX must handle implicit jumps from one block to next
4806*/
4807
4808struct assembler {
4809    PyObject *a_bytecode;  /* string containing bytecode */
4810    int a_offset;              /* offset into bytecode */
4811    int a_nblocks;             /* number of reachable blocks */
4812    basicblock **a_postorder; /* list of blocks in dfs postorder */
4813    PyObject *a_lnotab;    /* string containing lnotab */
4814    int a_lnotab_off;      /* offset into lnotab */
4815    int a_lineno;              /* last lineno of emitted instruction */
4816    int a_lineno_off;      /* bytecode offset of last lineno */
4817};
4818
4819static void
4820dfs(struct compiler *c, basicblock *b, struct assembler *a)
4821{
4822    int i;
4823    struct instr *instr = NULL;
4824
4825    if (b->b_seen)
4826        return;
4827    b->b_seen = 1;
4828    if (b->b_next != NULL)
4829        dfs(c, b->b_next, a);
4830    for (i = 0; i < b->b_iused; i++) {
4831        instr = &b->b_instr[i];
4832        if (instr->i_jrel || instr->i_jabs)
4833            dfs(c, instr->i_target, a);
4834    }
4835    a->a_postorder[a->a_nblocks++] = b;
4836}
4837
4838static int
4839stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
4840{
4841    int i, target_depth, effect;
4842    struct instr *instr;
4843    if (b->b_seen || b->b_startdepth >= depth)
4844        return maxdepth;
4845    b->b_seen = 1;
4846    b->b_startdepth = depth;
4847    for (i = 0; i < b->b_iused; i++) {
4848        instr = &b->b_instr[i];
4849        effect = PyCompile_OpcodeStackEffect(instr->i_opcode, instr->i_oparg);
4850        if (effect == PY_INVALID_STACK_EFFECT) {
4851            fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4852            Py_FatalError("PyCompile_OpcodeStackEffect()");
4853        }
4854        depth += effect;
4855
4856        if (depth > maxdepth)
4857            maxdepth = depth;
4858        assert(depth >= 0); /* invalid code or bug in stackdepth() */
4859        if (instr->i_jrel || instr->i_jabs) {
4860            target_depth = depth;
4861            if (instr->i_opcode == FOR_ITER) {
4862                target_depth = depth-2;
4863            }
4864            else if (instr->i_opcode == SETUP_FINALLY ||
4865                     instr->i_opcode == SETUP_EXCEPT) {
4866                target_depth = depth+3;
4867                if (target_depth > maxdepth)
4868                    maxdepth = target_depth;
4869            }
4870            else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
4871                     instr->i_opcode == JUMP_IF_FALSE_OR_POP)
4872                depth = depth - 1;
4873            maxdepth = stackdepth_walk(c, instr->i_target,
4874                                       target_depth, maxdepth);
4875            if (instr->i_opcode == JUMP_ABSOLUTE ||
4876                instr->i_opcode == JUMP_FORWARD) {
4877                goto out; /* remaining code is dead */
4878            }
4879        }
4880    }
4881    if (b->b_next)
4882        maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
4883out:
4884    b->b_seen = 0;
4885    return maxdepth;
4886}
4887
4888/* Find the flow path that needs the largest stack.  We assume that
4889 * cycles in the flow graph have no net effect on the stack depth.
4890 */
4891static int
4892stackdepth(struct compiler *c)
4893{
4894    basicblock *b, *entryblock;
4895    entryblock = NULL;
4896    for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4897        b->b_seen = 0;
4898        b->b_startdepth = INT_MIN;
4899        entryblock = b;
4900    }
4901    if (!entryblock)
4902        return 0;
4903    return stackdepth_walk(c, entryblock, 0, 0);
4904}
4905
4906static int
4907assemble_init(struct assembler *a, int nblocks, int firstlineno)
4908{
4909    memset(a, 0, sizeof(struct assembler));
4910    a->a_lineno = firstlineno;
4911    a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4912    if (!a->a_bytecode)
4913        return 0;
4914    a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4915    if (!a->a_lnotab)
4916        return 0;
4917    if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
4918        PyErr_NoMemory();
4919        return 0;
4920    }
4921    a->a_postorder = (basicblock **)PyObject_Malloc(
4922                                        sizeof(basicblock *) * nblocks);
4923    if (!a->a_postorder) {
4924        PyErr_NoMemory();
4925        return 0;
4926    }
4927    return 1;
4928}
4929
4930static void
4931assemble_free(struct assembler *a)
4932{
4933    Py_XDECREF(a->a_bytecode);
4934    Py_XDECREF(a->a_lnotab);
4935    if (a->a_postorder)
4936        PyObject_Free(a->a_postorder);
4937}
4938
4939static int
4940blocksize(basicblock *b)
4941{
4942    int i;
4943    int size = 0;
4944
4945    for (i = 0; i < b->b_iused; i++)
4946        size += instrsize(b->b_instr[i].i_oparg);
4947    return size;
4948}
4949
4950/* Appends a pair to the end of the line number table, a_lnotab, representing
4951   the instruction's bytecode offset and line number.  See
4952   Objects/lnotab_notes.txt for the description of the line number table. */
4953
4954static int
4955assemble_lnotab(struct assembler *a, struct instr *i)
4956{
4957    int d_bytecode, d_lineno;
4958    Py_ssize_t len;
4959    unsigned char *lnotab;
4960
4961    d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
4962    d_lineno = i->i_lineno - a->a_lineno;
4963
4964    assert(d_bytecode >= 0);
4965
4966    if(d_bytecode == 0 && d_lineno == 0)
4967        return 1;
4968
4969    if (d_bytecode > 255) {
4970        int j, nbytes, ncodes = d_bytecode / 255;
4971        nbytes = a->a_lnotab_off + 2 * ncodes;
4972        len = PyBytes_GET_SIZE(a->a_lnotab);
4973        if (nbytes >= len) {
4974            if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4975                len = nbytes;
4976            else if (len <= INT_MAX / 2)
4977                len *= 2;
4978            else {
4979                PyErr_NoMemory();
4980                return 0;
4981            }
4982            if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4983                return 0;
4984        }
4985        lnotab = (unsigned char *)
4986                   PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4987        for (j = 0; j < ncodes; j++) {
4988            *lnotab++ = 255;
4989            *lnotab++ = 0;
4990        }
4991        d_bytecode -= ncodes * 255;
4992        a->a_lnotab_off += ncodes * 2;
4993    }
4994    assert(0 <= d_bytecode && d_bytecode <= 255);
4995
4996    if (d_lineno < -128 || 127 < d_lineno) {
4997        int j, nbytes, ncodes, k;
4998        if (d_lineno < 0) {
4999            k = -128;
5000            /* use division on positive numbers */
5001            ncodes = (-d_lineno) / 128;
5002        }
5003        else {
5004            k = 127;
5005            ncodes = d_lineno / 127;
5006        }
5007        d_lineno -= ncodes * k;
5008        assert(ncodes >= 1);
5009        nbytes = a->a_lnotab_off + 2 * ncodes;
5010        len = PyBytes_GET_SIZE(a->a_lnotab);
5011        if (nbytes >= len) {
5012            if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5013                len = nbytes;
5014            else if (len <= INT_MAX / 2)
5015                len *= 2;
5016            else {
5017                PyErr_NoMemory();
5018                return 0;
5019            }
5020            if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5021                return 0;
5022        }
5023        lnotab = (unsigned char *)
5024                   PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5025        *lnotab++ = d_bytecode;
5026        *lnotab++ = k;
5027        d_bytecode = 0;
5028        for (j = 1; j < ncodes; j++) {
5029            *lnotab++ = 0;
5030            *lnotab++ = k;
5031        }
5032        a->a_lnotab_off += ncodes * 2;
5033    }
5034    assert(-128 <= d_lineno && d_lineno <= 127);
5035
5036    len = PyBytes_GET_SIZE(a->a_lnotab);
5037    if (a->a_lnotab_off + 2 >= len) {
5038        if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5039            return 0;
5040    }
5041    lnotab = (unsigned char *)
5042                    PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5043
5044    a->a_lnotab_off += 2;
5045    if (d_bytecode) {
5046        *lnotab++ = d_bytecode;
5047        *lnotab++ = d_lineno;
5048    }
5049    else {      /* First line of a block; def stmt, etc. */
5050        *lnotab++ = 0;
5051        *lnotab++ = d_lineno;
5052    }
5053    a->a_lineno = i->i_lineno;
5054    a->a_lineno_off = a->a_offset;
5055    return 1;
5056}
5057
5058/* assemble_emit()
5059   Extend the bytecode with a new instruction.
5060   Update lnotab if necessary.
5061*/
5062
5063static int
5064assemble_emit(struct assembler *a, struct instr *i)
5065{
5066    int size, arg = 0;
5067    Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
5068    _Py_CODEUNIT *code;
5069
5070    arg = i->i_oparg;
5071    size = instrsize(arg);
5072    if (i->i_lineno && !assemble_lnotab(a, i))
5073        return 0;
5074    if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
5075        if (len > PY_SSIZE_T_MAX / 2)
5076            return 0;
5077        if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5078            return 0;
5079    }
5080    code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
5081    a->a_offset += size;
5082    write_op_arg(code, i->i_opcode, arg, size);
5083    return 1;
5084}
5085
5086static void
5087assemble_jump_offsets(struct assembler *a, struct compiler *c)
5088{
5089    basicblock *b;
5090    int bsize, totsize, extended_arg_recompile;
5091    int i;
5092
5093    /* Compute the size of each block and fixup jump args.
5094       Replace block pointer with position in bytecode. */
5095    do {
5096        totsize = 0;
5097        for (i = a->a_nblocks - 1; i >= 0; i--) {
5098            b = a->a_postorder[i];
5099            bsize = blocksize(b);
5100            b->b_offset = totsize;
5101            totsize += bsize;
5102        }
5103        extended_arg_recompile = 0;
5104        for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5105            bsize = b->b_offset;
5106            for (i = 0; i < b->b_iused; i++) {
5107                struct instr *instr = &b->b_instr[i];
5108                int isize = instrsize(instr->i_oparg);
5109                /* Relative jumps are computed relative to
5110                   the instruction pointer after fetching
5111                   the jump instruction.
5112                */
5113                bsize += isize;
5114                if (instr->i_jabs || instr->i_jrel) {
5115                    instr->i_oparg = instr->i_target->b_offset;
5116                    if (instr->i_jrel) {
5117                        instr->i_oparg -= bsize;
5118                    }
5119                    instr->i_oparg *= sizeof(_Py_CODEUNIT);
5120                    if (instrsize(instr->i_oparg) != isize) {
5121                        extended_arg_recompile = 1;
5122                    }
5123                }
5124            }
5125        }
5126
5127    /* XXX: This is an awful hack that could hurt performance, but
5128        on the bright side it should work until we come up
5129        with a better solution.
5130
5131        The issue is that in the first loop blocksize() is called
5132        which calls instrsize() which requires i_oparg be set
5133        appropriately. There is a bootstrap problem because
5134        i_oparg is calculated in the second loop above.
5135
5136        So we loop until we stop seeing new EXTENDED_ARGs.
5137        The only EXTENDED_ARGs that could be popping up are
5138        ones in jump instructions.  So this should converge
5139        fairly quickly.
5140    */
5141    } while (extended_arg_recompile);
5142}
5143
5144static PyObject *
5145dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
5146{
5147    PyObject *tuple, *k, *v;
5148    Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
5149
5150    tuple = PyTuple_New(size);
5151    if (tuple == NULL)
5152        return NULL;
5153    while (PyDict_Next(dict, &pos, &k, &v)) {
5154        i = PyLong_AS_LONG(v);
5155        /* The keys of the dictionary are tuples. (see compiler_add_o
5156         * and _PyCode_ConstantKey). The object we want is always second,
5157         * though. */
5158        k = PyTuple_GET_ITEM(k, 1);
5159        Py_INCREF(k);
5160        assert((i - offset) < size);
5161        assert((i - offset) >= 0);
5162        PyTuple_SET_ITEM(tuple, i - offset, k);
5163    }
5164    return tuple;
5165}
5166
5167static int
5168compute_code_flags(struct compiler *c)
5169{
5170    PySTEntryObject *ste = c->u->u_ste;
5171    int flags = 0;
5172    Py_ssize_t n;
5173    if (ste->ste_type == FunctionBlock) {
5174        flags |= CO_NEWLOCALS | CO_OPTIMIZED;
5175        if (ste->ste_nested)
5176            flags |= CO_NESTED;
5177        if (ste->ste_generator && !ste->ste_coroutine)
5178            flags |= CO_GENERATOR;
5179        if (!ste->ste_generator && ste->ste_coroutine)
5180            flags |= CO_COROUTINE;
5181        if (ste->ste_generator && ste->ste_coroutine)
5182            flags |= CO_ASYNC_GENERATOR;
5183        if (ste->ste_varargs)
5184            flags |= CO_VARARGS;
5185        if (ste->ste_varkeywords)
5186            flags |= CO_VARKEYWORDS;
5187    }
5188
5189    /* (Only) inherit compilerflags in PyCF_MASK */
5190    flags |= (c->c_flags->cf_flags & PyCF_MASK);
5191
5192    n = PyDict_Size(c->u->u_freevars);
5193    if (n < 0)
5194        return -1;
5195    if (n == 0) {
5196        n = PyDict_Size(c->u->u_cellvars);
5197        if (n < 0)
5198            return -1;
5199        if (n == 0) {
5200            flags |= CO_NOFREE;
5201        }
5202    }
5203
5204    return flags;
5205}
5206
5207static PyCodeObject *
5208makecode(struct compiler *c, struct assembler *a)
5209{
5210    PyObject *tmp;
5211    PyCodeObject *co = NULL;
5212    PyObject *consts = NULL;
5213    PyObject *names = NULL;
5214    PyObject *varnames = NULL;
5215    PyObject *name = NULL;
5216    PyObject *freevars = NULL;
5217    PyObject *cellvars = NULL;
5218    PyObject *bytecode = NULL;
5219    Py_ssize_t nlocals;
5220    int nlocals_int;
5221    int flags;
5222    int argcount, kwonlyargcount;
5223
5224    tmp = dict_keys_inorder(c->u->u_consts, 0);
5225    if (!tmp)
5226        goto error;
5227    consts = PySequence_List(tmp); /* optimize_code requires a list */
5228    Py_DECREF(tmp);
5229
5230    names = dict_keys_inorder(c->u->u_names, 0);
5231    varnames = dict_keys_inorder(c->u->u_varnames, 0);
5232    if (!consts || !names || !varnames)
5233        goto error;
5234
5235    cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5236    if (!cellvars)
5237        goto error;
5238    freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
5239    if (!freevars)
5240        goto error;
5241
5242    nlocals = PyDict_Size(c->u->u_varnames);
5243    assert(nlocals < INT_MAX);
5244    nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5245
5246    flags = compute_code_flags(c);
5247    if (flags < 0)
5248        goto error;
5249
5250    bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5251    if (!bytecode)
5252        goto error;
5253
5254    tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5255    if (!tmp)
5256        goto error;
5257    Py_DECREF(consts);
5258    consts = tmp;
5259
5260    argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
5261    kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
5262    co = PyCode_New(argcount, kwonlyargcount,
5263                    nlocals_int, stackdepth(c), flags,
5264                    bytecode, consts, names, varnames,
5265                    freevars, cellvars,
5266                    c->c_filename, c->u->u_name,
5267                    c->u->u_firstlineno,
5268                    a->a_lnotab);
5269 error:
5270    Py_XDECREF(consts);
5271    Py_XDECREF(names);
5272    Py_XDECREF(varnames);
5273    Py_XDECREF(name);
5274    Py_XDECREF(freevars);
5275    Py_XDECREF(cellvars);
5276    Py_XDECREF(bytecode);
5277    return co;
5278}
5279
5280
5281/* For debugging purposes only */
5282#if 0
5283static void
5284dump_instr(const struct instr *i)
5285{
5286    const char *jrel = i->i_jrel ? "jrel " : "";
5287    const char *jabs = i->i_jabs ? "jabs " : "";
5288    char arg[128];
5289
5290    *arg = '\0';
5291    if (HAS_ARG(i->i_opcode)) {
5292        sprintf(arg, "arg: %d ", i->i_oparg);
5293    }
5294    fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5295                    i->i_lineno, i->i_opcode, arg, jabs, jrel);
5296}
5297
5298static void
5299dump_basicblock(const basicblock *b)
5300{
5301    const char *seen = b->b_seen ? "seen " : "";
5302    const char *b_return = b->b_return ? "return " : "";
5303    fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
5304        b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
5305    if (b->b_instr) {
5306        int i;
5307        for (i = 0; i < b->b_iused; i++) {
5308            fprintf(stderr, "  [%02d] ", i);
5309            dump_instr(b->b_instr + i);
5310        }
5311    }
5312}
5313#endif
5314
5315static PyCodeObject *
5316assemble(struct compiler *c, int addNone)
5317{
5318    basicblock *b, *entryblock;
5319    struct assembler a;
5320    int i, j, nblocks;
5321    PyCodeObject *co = NULL;
5322
5323    /* Make sure every block that falls off the end returns None.
5324       XXX NEXT_BLOCK() isn't quite right, because if the last
5325       block ends with a jump or return b_next shouldn't set.
5326     */
5327    if (!c->u->u_curblock->b_return) {
5328        NEXT_BLOCK(c);
5329        if (addNone)
5330            ADDOP_O(c, LOAD_CONST, Py_None, consts);
5331        ADDOP(c, RETURN_VALUE);
5332    }
5333
5334    nblocks = 0;
5335    entryblock = NULL;
5336    for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5337        nblocks++;
5338        entryblock = b;
5339    }
5340
5341    /* Set firstlineno if it wasn't explicitly set. */
5342    if (!c->u->u_firstlineno) {
5343        if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
5344            c->u->u_firstlineno = entryblock->b_instr->i_lineno;
5345        else
5346            c->u->u_firstlineno = 1;
5347    }
5348    if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
5349        goto error;
5350    dfs(c, entryblock, &a);
5351
5352    /* Can't modify the bytecode after computing jump offsets. */
5353    assemble_jump_offsets(&a, c);
5354
5355    /* Emit code in reverse postorder from dfs. */
5356    for (i = a.a_nblocks - 1; i >= 0; i--) {
5357        b = a.a_postorder[i];
5358        for (j = 0; j < b->b_iused; j++)
5359            if (!assemble_emit(&a, &b->b_instr[j]))
5360                goto error;
5361    }
5362
5363    if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
5364        goto error;
5365    if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
5366        goto error;
5367
5368    co = makecode(c, &a);
5369 error:
5370    assemble_free(&a);
5371    return co;
5372}
5373
5374#undef PyAST_Compile
5375PyAPI_FUNC(PyCodeObject *)
5376PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
5377              PyArena *arena)
5378{
5379    return PyAST_CompileEx(mod, filename, flags, -1, arena);
5380}
5381