ยปCore Development>Code coverage>Python/compile.c

Python code coverage for Python/compile.c

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