- Got rid of newmodule.c
[python/dscho.git] / Python / compile.c
blobfa53b1ea85a326649c0d8b099a2fe0eae7b2d5b5
1 /* Compile an expression node to intermediate code */
3 /* XXX TO DO:
4 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
6 XXX Generate simple jump for break/return outside 'try...finally'
7 XXX Allow 'continue' inside finally clause of try-finally
8 XXX New opcode for loading the initial index for a for loop
9 XXX other JAR tricks?
12 #include "Python.h"
14 #include "node.h"
15 #include "token.h"
16 #include "graminit.h"
17 #include "compile.h"
18 #include "symtable.h"
19 #include "opcode.h"
20 #include "structmember.h"
22 #include <ctype.h>
24 /* Three symbols from graminit.h are also defined in Python.h, with
25 Py_ prefixes to their names. Python.h can't include graminit.h
26 (which defines too many confusing symbols), but we can check here
27 that they haven't changed (which is very unlikely, but possible). */
28 #if Py_single_input != single_input
29 #error "single_input has changed -- update Py_single_input in Python.h"
30 #endif
31 #if Py_file_input != file_input
32 #error "file_input has changed -- update Py_file_input in Python.h"
33 #endif
34 #if Py_eval_input != eval_input
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
36 #endif
38 int Py_OptimizeFlag = 0;
40 #define OP_DELETE 0
41 #define OP_ASSIGN 1
42 #define OP_APPLY 2
44 #define VAR_LOAD 0
45 #define VAR_STORE 1
46 #define VAR_DELETE 2
48 #define DEL_CLOSURE_ERROR \
49 "can not delete variable '%.400s' referenced in nested scope"
51 #define DUPLICATE_ARGUMENT \
52 "duplicate argument '%s' in function definition"
54 #define ILLEGAL_DYNAMIC_SCOPE \
55 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
57 #define GLOBAL_AFTER_ASSIGN \
58 "name '%.400s' is assigned to before global declaration"
60 #define GLOBAL_AFTER_USE \
61 "name '%.400s' is used prior to global declaration"
63 #define LOCAL_GLOBAL \
64 "name '%.400s' is a function parameter and declared global"
66 #define LATE_FUTURE \
67 "from __future__ imports must occur at the beginning of the file"
69 #define ASSIGN_DEBUG \
70 "can not assign to __debug__"
72 #define MANGLE_LEN 256
74 #define OFF(x) offsetof(PyCodeObject, x)
76 static PyMemberDef code_memberlist[] = {
77 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
79 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
80 {"co_flags", T_INT, OFF(co_flags), READONLY},
81 {"co_code", T_OBJECT, OFF(co_code), READONLY},
82 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
83 {"co_names", T_OBJECT, OFF(co_names), READONLY},
84 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
85 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
87 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
88 {"co_name", T_OBJECT, OFF(co_name), READONLY},
89 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
91 {NULL} /* Sentinel */
94 PyDoc_STRVAR(code_doc,
95 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
96 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
97 \n\
98 Create a code object. Not for the faint of heart.");
100 static PyObject *
101 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
103 int argcount;
104 int nlocals;
105 int stacksize;
106 int flags;
107 PyObject *code;
108 PyObject *consts;
109 PyObject *names;
110 PyObject *varnames;
111 PyObject *freevars = NULL;
112 PyObject *cellvars = NULL;
113 PyObject *filename;
114 PyObject *name;
115 int firstlineno;
116 PyObject *lnotab;
118 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
119 &argcount, &nlocals, &stacksize, &flags,
120 &code,
121 &PyTuple_Type, &consts,
122 &PyTuple_Type, &names,
123 &PyTuple_Type, &varnames,
124 &filename, &name,
125 &firstlineno, &lnotab,
126 &PyTuple_Type, &freevars,
127 &PyTuple_Type, &cellvars))
128 return NULL;
130 if (freevars == NULL || cellvars == NULL) {
131 PyObject *empty = PyTuple_New(0);
132 if (empty == NULL)
133 return NULL;
134 if (freevars == NULL) {
135 freevars = empty;
136 Py_INCREF(freevars);
138 if (cellvars == NULL) {
139 cellvars = empty;
140 Py_INCREF(cellvars);
142 Py_DECREF(empty);
145 if (!PyObject_CheckReadBuffer(code)) {
146 PyErr_SetString(PyExc_TypeError,
147 "bytecode object must be a single-segment read-only buffer");
148 return NULL;
151 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
152 code, consts, names, varnames,
153 freevars, cellvars, filename, name,
154 firstlineno, lnotab);
157 static void
158 code_dealloc(PyCodeObject *co)
160 Py_XDECREF(co->co_code);
161 Py_XDECREF(co->co_consts);
162 Py_XDECREF(co->co_names);
163 Py_XDECREF(co->co_varnames);
164 Py_XDECREF(co->co_freevars);
165 Py_XDECREF(co->co_cellvars);
166 Py_XDECREF(co->co_filename);
167 Py_XDECREF(co->co_name);
168 Py_XDECREF(co->co_lnotab);
169 PyObject_DEL(co);
172 static PyObject *
173 code_repr(PyCodeObject *co)
175 char buf[500];
176 int lineno = -1;
177 char *filename = "???";
178 char *name = "???";
180 if (co->co_firstlineno != 0)
181 lineno = co->co_firstlineno;
182 if (co->co_filename && PyString_Check(co->co_filename))
183 filename = PyString_AS_STRING(co->co_filename);
184 if (co->co_name && PyString_Check(co->co_name))
185 name = PyString_AS_STRING(co->co_name);
186 PyOS_snprintf(buf, sizeof(buf),
187 "<code object %.100s at %p, file \"%.300s\", line %d>",
188 name, co, filename, lineno);
189 return PyString_FromString(buf);
192 static int
193 code_compare(PyCodeObject *co, PyCodeObject *cp)
195 int cmp;
196 cmp = PyObject_Compare(co->co_name, cp->co_name);
197 if (cmp) return cmp;
198 cmp = co->co_argcount - cp->co_argcount;
199 if (cmp) return cmp;
200 cmp = co->co_nlocals - cp->co_nlocals;
201 if (cmp) return cmp;
202 cmp = co->co_flags - cp->co_flags;
203 if (cmp) return cmp;
204 cmp = PyObject_Compare(co->co_code, cp->co_code);
205 if (cmp) return cmp;
206 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
207 if (cmp) return cmp;
208 cmp = PyObject_Compare(co->co_names, cp->co_names);
209 if (cmp) return cmp;
210 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
211 if (cmp) return cmp;
212 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
213 if (cmp) return cmp;
214 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
215 return cmp;
218 static long
219 code_hash(PyCodeObject *co)
221 long h, h0, h1, h2, h3, h4, h5, h6;
222 h0 = PyObject_Hash(co->co_name);
223 if (h0 == -1) return -1;
224 h1 = PyObject_Hash(co->co_code);
225 if (h1 == -1) return -1;
226 h2 = PyObject_Hash(co->co_consts);
227 if (h2 == -1) return -1;
228 h3 = PyObject_Hash(co->co_names);
229 if (h3 == -1) return -1;
230 h4 = PyObject_Hash(co->co_varnames);
231 if (h4 == -1) return -1;
232 h5 = PyObject_Hash(co->co_freevars);
233 if (h5 == -1) return -1;
234 h6 = PyObject_Hash(co->co_cellvars);
235 if (h6 == -1) return -1;
236 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
237 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
238 if (h == -1) h = -2;
239 return h;
242 /* XXX code objects need to participate in GC? */
244 PyTypeObject PyCode_Type = {
245 PyObject_HEAD_INIT(&PyType_Type)
247 "code",
248 sizeof(PyCodeObject),
250 (destructor)code_dealloc, /* tp_dealloc */
251 0, /* tp_print */
252 0, /* tp_getattr */
253 0, /* tp_setattr */
254 (cmpfunc)code_compare, /* tp_compare */
255 (reprfunc)code_repr, /* tp_repr */
256 0, /* tp_as_number */
257 0, /* tp_as_sequence */
258 0, /* tp_as_mapping */
259 (hashfunc)code_hash, /* tp_hash */
260 0, /* tp_call */
261 0, /* tp_str */
262 PyObject_GenericGetAttr, /* tp_getattro */
263 0, /* tp_setattro */
264 0, /* tp_as_buffer */
265 Py_TPFLAGS_DEFAULT, /* tp_flags */
266 code_doc, /* tp_doc */
267 0, /* tp_traverse */
268 0, /* tp_clear */
269 0, /* tp_richcompare */
270 0, /* tp_weaklistoffset */
271 0, /* tp_iter */
272 0, /* tp_iternext */
273 0, /* tp_methods */
274 code_memberlist, /* tp_members */
275 0, /* tp_getset */
276 0, /* tp_base */
277 0, /* tp_dict */
278 0, /* tp_descr_get */
279 0, /* tp_descr_set */
280 0, /* tp_dictoffset */
281 0, /* tp_init */
282 0, /* tp_alloc */
283 code_new, /* tp_new */
286 #define NAME_CHARS \
287 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
289 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
291 static int
292 all_name_chars(unsigned char *s)
294 static char ok_name_char[256];
295 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
297 if (ok_name_char[*name_chars] == 0) {
298 unsigned char *p;
299 for (p = name_chars; *p; p++)
300 ok_name_char[*p] = 1;
302 while (*s) {
303 if (ok_name_char[*s++] == 0)
304 return 0;
306 return 1;
309 static int
310 intern_strings(PyObject *tuple)
312 int i;
314 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
315 PyObject *v = PyTuple_GET_ITEM(tuple, i);
316 if (v == NULL || !PyString_Check(v)) {
317 Py_FatalError("non-string found in code slot");
318 PyErr_BadInternalCall();
319 return -1;
321 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
323 return 0;
326 PyCodeObject *
327 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
328 PyObject *code, PyObject *consts, PyObject *names,
329 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
330 PyObject *filename, PyObject *name, int firstlineno,
331 PyObject *lnotab)
333 PyCodeObject *co;
334 int i;
335 /* Check argument types */
336 if (argcount < 0 || nlocals < 0 ||
337 code == NULL ||
338 consts == NULL || !PyTuple_Check(consts) ||
339 names == NULL || !PyTuple_Check(names) ||
340 varnames == NULL || !PyTuple_Check(varnames) ||
341 freevars == NULL || !PyTuple_Check(freevars) ||
342 cellvars == NULL || !PyTuple_Check(cellvars) ||
343 name == NULL || !PyString_Check(name) ||
344 filename == NULL || !PyString_Check(filename) ||
345 lnotab == NULL || !PyString_Check(lnotab) ||
346 !PyObject_CheckReadBuffer(code)) {
347 PyErr_BadInternalCall();
348 return NULL;
350 intern_strings(names);
351 intern_strings(varnames);
352 intern_strings(freevars);
353 intern_strings(cellvars);
354 /* Intern selected string constants */
355 for (i = PyTuple_Size(consts); --i >= 0; ) {
356 PyObject *v = PyTuple_GetItem(consts, i);
357 if (!PyString_Check(v))
358 continue;
359 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
360 continue;
361 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
363 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
364 if (co != NULL) {
365 co->co_argcount = argcount;
366 co->co_nlocals = nlocals;
367 co->co_stacksize = stacksize;
368 co->co_flags = flags;
369 Py_INCREF(code);
370 co->co_code = code;
371 Py_INCREF(consts);
372 co->co_consts = consts;
373 Py_INCREF(names);
374 co->co_names = names;
375 Py_INCREF(varnames);
376 co->co_varnames = varnames;
377 Py_INCREF(freevars);
378 co->co_freevars = freevars;
379 Py_INCREF(cellvars);
380 co->co_cellvars = cellvars;
381 Py_INCREF(filename);
382 co->co_filename = filename;
383 Py_INCREF(name);
384 co->co_name = name;
385 co->co_firstlineno = firstlineno;
386 Py_INCREF(lnotab);
387 co->co_lnotab = lnotab;
389 return co;
393 /* Data structure used internally */
395 /* The compiler uses two passes to generate bytecodes. The first pass
396 builds the symbol table. The second pass generates the bytecode.
398 The first pass uses a single symtable struct. The second pass uses
399 a compiling struct for each code block. The compiling structs
400 share a reference to the symtable.
402 The two passes communicate via symtable_load_symbols() and via
403 is_local() and is_global(). The former initializes several slots
404 in the compiling struct: c_varnames, c_locals, c_nlocals,
405 c_argcount, c_globals, and c_flags.
408 /* All about c_lnotab.
410 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
411 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
412 to source code line #s (when needed for tracebacks) via c_lnotab instead.
413 The array is conceptually a list of
414 (bytecode offset increment, line number increment)
415 pairs. The details are important and delicate, best illustrated by example:
417 byte code offset source code line number
420 50 7
421 350 307
422 361 308
424 The first trick is that these numbers aren't stored, only the increments
425 from one row to the next (this doesn't really work, but it's a start):
427 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
429 The second trick is that an unsigned byte can't hold negative values, or
430 values larger than 255, so (a) there's a deep assumption that byte code
431 offsets and their corresponding line #s both increase monotonically, and (b)
432 if at least one column jumps by more than 255 from one row to the next, more
433 than one pair is written to the table. In case #b, there's no way to know
434 from looking at the table later how many were written. That's the delicate
435 part. A user of c_lnotab desiring to find the source line number
436 corresponding to a bytecode address A should do something like this
438 lineno = addr = 0
439 for addr_incr, line_incr in c_lnotab:
440 addr += addr_incr
441 if addr > A:
442 return lineno
443 lineno += line_incr
445 In order for this to work, when the addr field increments by more than 255,
446 the line # increment in each pair generated must be 0 until the remaining addr
447 increment is < 256. So, in the example above, com_set_lineno should not (as
448 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
449 255, 0, 45, 255, 0, 45.
452 struct compiling {
453 PyObject *c_code; /* string */
454 PyObject *c_consts; /* list of objects */
455 PyObject *c_const_dict; /* inverse of c_consts */
456 PyObject *c_names; /* list of strings (names) */
457 PyObject *c_name_dict; /* inverse of c_names */
458 PyObject *c_globals; /* dictionary (value=None) */
459 PyObject *c_locals; /* dictionary (value=localID) */
460 PyObject *c_varnames; /* list (inverse of c_locals) */
461 PyObject *c_freevars; /* dictionary (value=None) */
462 PyObject *c_cellvars; /* list */
463 int c_nlocals; /* index of next local */
464 int c_argcount; /* number of top-level arguments */
465 int c_flags; /* same as co_flags */
466 int c_nexti; /* index into c_code */
467 int c_errors; /* counts errors occurred */
468 int c_infunction; /* set when compiling a function */
469 int c_interactive; /* generating code for interactive command */
470 int c_loops; /* counts nested loops */
471 int c_begin; /* begin of current loop, for 'continue' */
472 int c_block[CO_MAXBLOCKS]; /* stack of block types */
473 int c_nblocks; /* current block stack level */
474 char *c_filename; /* filename of current node */
475 char *c_name; /* name of object (e.g. function) */
476 int c_lineno; /* Current line number */
477 int c_stacklevel; /* Current stack level */
478 int c_maxstacklevel; /* Maximum stack level */
479 int c_firstlineno;
480 PyObject *c_lnotab; /* Table mapping address to line number */
481 int c_last_addr, c_last_line, c_lnotab_next;
482 char *c_private; /* for private name mangling */
483 int c_tmpname; /* temporary local name counter */
484 int c_nested; /* Is block nested funcdef or lamdef? */
485 int c_closure; /* Is nested w/freevars? */
486 struct symtable *c_symtable; /* pointer to module symbol table */
487 PyFutureFeatures *c_future; /* pointer to module's __future__ */
490 static int
491 is_free(int v)
493 if ((v & (USE | DEF_FREE))
494 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
495 return 1;
496 if (v & DEF_FREE_CLASS)
497 return 1;
498 return 0;
501 static void
502 com_error(struct compiling *c, PyObject *exc, char *msg)
504 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
506 if (c == NULL) {
507 /* Error occurred via symtable call to
508 is_constant_false */
509 PyErr_SetString(exc, msg);
510 return;
512 c->c_errors++;
513 if (c->c_lineno < 1 || c->c_interactive) {
514 /* Unknown line number or interactive input */
515 PyErr_SetString(exc, msg);
516 return;
518 v = PyString_FromString(msg);
519 if (v == NULL)
520 return; /* MemoryError, too bad */
522 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
523 if (line == NULL) {
524 Py_INCREF(Py_None);
525 line = Py_None;
527 if (exc == PyExc_SyntaxError) {
528 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
529 Py_None, line);
530 if (t == NULL)
531 goto exit;
532 w = Py_BuildValue("(OO)", v, t);
533 if (w == NULL)
534 goto exit;
535 PyErr_SetObject(exc, w);
536 } else {
537 /* Make sure additional exceptions are printed with
538 file and line, also. */
539 PyErr_SetObject(exc, v);
540 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
542 exit:
543 Py_XDECREF(t);
544 Py_XDECREF(v);
545 Py_XDECREF(w);
546 Py_XDECREF(line);
549 /* Interface to the block stack */
551 static void
552 block_push(struct compiling *c, int type)
554 if (c->c_nblocks >= CO_MAXBLOCKS) {
555 com_error(c, PyExc_SystemError,
556 "too many statically nested blocks");
558 else {
559 c->c_block[c->c_nblocks++] = type;
563 static void
564 block_pop(struct compiling *c, int type)
566 if (c->c_nblocks > 0)
567 c->c_nblocks--;
568 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
569 com_error(c, PyExc_SystemError, "bad block pop");
573 /* Prototype forward declarations */
575 static int com_init(struct compiling *, char *);
576 static void com_free(struct compiling *);
577 static void com_push(struct compiling *, int);
578 static void com_pop(struct compiling *, int);
579 static void com_done(struct compiling *);
580 static void com_node(struct compiling *, node *);
581 static void com_factor(struct compiling *, node *);
582 static void com_addbyte(struct compiling *, int);
583 static void com_addint(struct compiling *, int);
584 static void com_addoparg(struct compiling *, int, int);
585 static void com_addfwref(struct compiling *, int, int *);
586 static void com_backpatch(struct compiling *, int);
587 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
588 static int com_addconst(struct compiling *, PyObject *);
589 static int com_addname(struct compiling *, PyObject *);
590 static void com_addopname(struct compiling *, int, node *);
591 static void com_list(struct compiling *, node *, int);
592 static void com_list_iter(struct compiling *, node *, node *, char *);
593 static int com_argdefs(struct compiling *, node *);
594 static void com_assign(struct compiling *, node *, int, node *);
595 static void com_assign_name(struct compiling *, node *, int);
596 static PyCodeObject *icompile(node *, struct compiling *);
597 static PyCodeObject *jcompile(node *, char *, struct compiling *,
598 PyCompilerFlags *);
599 static PyObject *parsestrplus(struct compiling*, node *);
600 static PyObject *parsestr(struct compiling *, char *);
601 static node *get_rawdocstring(node *);
603 static int get_ref_type(struct compiling *, char *);
605 /* symtable operations */
606 static int symtable_build(struct compiling *, node *);
607 static int symtable_load_symbols(struct compiling *);
608 static struct symtable *symtable_init(void);
609 static void symtable_enter_scope(struct symtable *, char *, int, int);
610 static int symtable_exit_scope(struct symtable *);
611 static int symtable_add_def(struct symtable *, char *, int);
612 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
614 static void symtable_node(struct symtable *, node *);
615 static void symtable_funcdef(struct symtable *, node *);
616 static void symtable_default_args(struct symtable *, node *);
617 static void symtable_params(struct symtable *, node *);
618 static void symtable_params_fplist(struct symtable *, node *n);
619 static void symtable_global(struct symtable *, node *);
620 static void symtable_import(struct symtable *, node *);
621 static void symtable_assign(struct symtable *, node *, int);
622 static void symtable_list_comprehension(struct symtable *, node *);
624 static int symtable_update_free_vars(struct symtable *);
625 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
626 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
628 /* helper */
629 static void
630 do_pad(int pad)
632 int i;
633 for (i = 0; i < pad; ++i)
634 fprintf(stderr, " ");
637 static void
638 dump(node *n, int pad, int depth)
640 int i;
641 if (depth == 0)
642 return;
643 do_pad(pad);
644 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
645 if (depth > 0)
646 depth--;
647 for (i = 0; i < NCH(n); ++i)
648 dump(CHILD(n, i), pad + 1, depth);
651 #define DUMP(N) dump(N, 0, -1)
653 static int
654 com_init(struct compiling *c, char *filename)
656 memset((void *)c, '\0', sizeof(struct compiling));
657 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
658 1000)) == NULL)
659 goto fail;
660 if ((c->c_consts = PyList_New(0)) == NULL)
661 goto fail;
662 if ((c->c_const_dict = PyDict_New()) == NULL)
663 goto fail;
664 if ((c->c_names = PyList_New(0)) == NULL)
665 goto fail;
666 if ((c->c_name_dict = PyDict_New()) == NULL)
667 goto fail;
668 if ((c->c_locals = PyDict_New()) == NULL)
669 goto fail;
670 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
671 1000)) == NULL)
672 goto fail;
673 c->c_globals = NULL;
674 c->c_varnames = NULL;
675 c->c_freevars = NULL;
676 c->c_cellvars = NULL;
677 c->c_nlocals = 0;
678 c->c_argcount = 0;
679 c->c_flags = 0;
680 c->c_nexti = 0;
681 c->c_errors = 0;
682 c->c_infunction = 0;
683 c->c_interactive = 0;
684 c->c_loops = 0;
685 c->c_begin = 0;
686 c->c_nblocks = 0;
687 c->c_filename = filename;
688 c->c_name = "?";
689 c->c_lineno = 0;
690 c->c_stacklevel = 0;
691 c->c_maxstacklevel = 0;
692 c->c_firstlineno = 0;
693 c->c_last_addr = 0;
694 c->c_last_line = 0;
695 c->c_lnotab_next = 0;
696 c->c_tmpname = 0;
697 c->c_nested = 0;
698 c->c_closure = 0;
699 c->c_symtable = NULL;
700 return 1;
702 fail:
703 com_free(c);
704 return 0;
707 static void
708 com_free(struct compiling *c)
710 Py_XDECREF(c->c_code);
711 Py_XDECREF(c->c_consts);
712 Py_XDECREF(c->c_const_dict);
713 Py_XDECREF(c->c_names);
714 Py_XDECREF(c->c_name_dict);
715 Py_XDECREF(c->c_globals);
716 Py_XDECREF(c->c_locals);
717 Py_XDECREF(c->c_varnames);
718 Py_XDECREF(c->c_freevars);
719 Py_XDECREF(c->c_cellvars);
720 Py_XDECREF(c->c_lnotab);
721 if (c->c_future)
722 PyMem_Free((void *)c->c_future);
725 static void
726 com_push(struct compiling *c, int n)
728 c->c_stacklevel += n;
729 if (c->c_stacklevel > c->c_maxstacklevel) {
730 c->c_maxstacklevel = c->c_stacklevel;
732 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
733 c->c_filename, c->c_name, c->c_lineno,
734 c->c_nexti, c->c_stacklevel, n);
739 static void
740 com_pop(struct compiling *c, int n)
742 if (c->c_stacklevel < n)
743 c->c_stacklevel = 0;
744 else
745 c->c_stacklevel -= n;
748 static void
749 com_done(struct compiling *c)
751 if (c->c_code != NULL)
752 _PyString_Resize(&c->c_code, c->c_nexti);
753 if (c->c_lnotab != NULL)
754 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
757 static int
758 com_check_size(PyObject **s, int offset)
760 int len = PyString_GET_SIZE(*s);
761 if (offset >= len)
762 return _PyString_Resize(s, len * 2);
763 return 0;
766 static void
767 com_addbyte(struct compiling *c, int byte)
769 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
770 assert(byte >= 0 && byte <= 255);
771 assert(c->c_code != 0);
772 if (com_check_size(&c->c_code, c->c_nexti)) {
773 c->c_errors++;
774 return;
776 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
779 static void
780 com_addint(struct compiling *c, int x)
782 com_addbyte(c, x & 0xff);
783 com_addbyte(c, x >> 8); /* XXX x should be positive */
786 static void
787 com_add_lnotab(struct compiling *c, int addr, int line)
789 char *p;
790 if (c->c_lnotab == NULL)
791 return;
792 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
793 c->c_errors++;
794 return;
796 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
797 *p++ = addr;
798 *p++ = line;
799 c->c_lnotab_next += 2;
802 static void
803 com_set_lineno(struct compiling *c, int lineno)
805 c->c_lineno = lineno;
806 if (c->c_firstlineno == 0) {
807 c->c_firstlineno = c->c_last_line = lineno;
809 else {
810 int incr_addr = c->c_nexti - c->c_last_addr;
811 int incr_line = lineno - c->c_last_line;
812 while (incr_addr > 255) {
813 com_add_lnotab(c, 255, 0);
814 incr_addr -= 255;
816 while (incr_line > 255) {
817 com_add_lnotab(c, incr_addr, 255);
818 incr_line -=255;
819 incr_addr = 0;
821 if (incr_addr > 0 || incr_line > 0)
822 com_add_lnotab(c, incr_addr, incr_line);
823 c->c_last_addr = c->c_nexti;
824 c->c_last_line = lineno;
828 static void
829 com_addoparg(struct compiling *c, int op, int arg)
831 int extended_arg = arg >> 16;
832 if (op == SET_LINENO) {
833 com_set_lineno(c, arg);
834 if (Py_OptimizeFlag)
835 return;
837 if (extended_arg){
838 com_addbyte(c, EXTENDED_ARG);
839 com_addint(c, extended_arg);
840 arg &= 0xffff;
842 com_addbyte(c, op);
843 com_addint(c, arg);
846 static void
847 com_addfwref(struct compiling *c, int op, int *p_anchor)
849 /* Compile a forward reference for backpatching */
850 int here;
851 int anchor;
852 com_addbyte(c, op);
853 here = c->c_nexti;
854 anchor = *p_anchor;
855 *p_anchor = here;
856 com_addint(c, anchor == 0 ? 0 : here - anchor);
859 static void
860 com_backpatch(struct compiling *c, int anchor)
862 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
863 int target = c->c_nexti;
864 int dist;
865 int prev;
866 for (;;) {
867 /* Make the JUMP instruction at anchor point to target */
868 prev = code[anchor] + (code[anchor+1] << 8);
869 dist = target - (anchor+2);
870 code[anchor] = dist & 0xff;
871 dist >>= 8;
872 code[anchor+1] = dist;
873 dist >>= 8;
874 if (dist) {
875 com_error(c, PyExc_SystemError,
876 "com_backpatch: offset too large");
877 break;
879 if (!prev)
880 break;
881 anchor -= prev;
885 /* Handle literals and names uniformly */
887 static int
888 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
890 PyObject *w, *t, *np=NULL;
891 long n;
893 t = Py_BuildValue("(OO)", v, v->ob_type);
894 if (t == NULL)
895 goto fail;
896 w = PyDict_GetItem(dict, t);
897 if (w != NULL) {
898 n = PyInt_AsLong(w);
899 } else {
900 n = PyList_Size(list);
901 np = PyInt_FromLong(n);
902 if (np == NULL)
903 goto fail;
904 if (PyList_Append(list, v) != 0)
905 goto fail;
906 if (PyDict_SetItem(dict, t, np) != 0)
907 goto fail;
908 Py_DECREF(np);
910 Py_DECREF(t);
911 return n;
912 fail:
913 Py_XDECREF(np);
914 Py_XDECREF(t);
915 c->c_errors++;
916 return 0;
919 static int
920 com_addconst(struct compiling *c, PyObject *v)
922 return com_add(c, c->c_consts, c->c_const_dict, v);
925 static int
926 com_addname(struct compiling *c, PyObject *v)
928 return com_add(c, c->c_names, c->c_name_dict, v);
932 _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
934 /* Name mangling: __private becomes _classname__private.
935 This is independent from how the name is used. */
936 size_t nlen, plen;
937 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
938 return 0;
939 nlen = strlen(name);
940 if (nlen+2 >= maxlen)
941 return 0; /* Don't mangle __extremely_long_names */
942 if (name[nlen-1] == '_' && name[nlen-2] == '_')
943 return 0; /* Don't mangle __whatever__ */
944 /* Strip leading underscores from class name */
945 while (*p == '_')
946 p++;
947 if (*p == '\0')
948 return 0; /* Don't mangle if class is just underscores */
949 plen = strlen(p);
950 if (plen + nlen >= maxlen)
951 plen = maxlen-nlen-2; /* Truncate class name if too long */
952 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
953 buffer[0] = '_';
954 strncpy(buffer+1, p, plen);
955 strcpy(buffer+1+plen, name);
956 return 1;
959 static void
960 com_addop_name(struct compiling *c, int op, char *name)
962 PyObject *v;
963 int i;
964 char buffer[MANGLE_LEN];
966 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
967 name = buffer;
968 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
969 c->c_errors++;
970 i = 255;
972 else {
973 i = com_addname(c, v);
974 Py_DECREF(v);
976 com_addoparg(c, op, i);
979 #define NAME_LOCAL 0
980 #define NAME_GLOBAL 1
981 #define NAME_DEFAULT 2
982 #define NAME_CLOSURE 3
984 static int
985 com_lookup_arg(PyObject *dict, PyObject *name)
987 PyObject *v = PyDict_GetItem(dict, name);
988 if (v == NULL)
989 return -1;
990 else
991 return PyInt_AS_LONG(v);
994 static void
995 com_addop_varname(struct compiling *c, int kind, char *name)
997 PyObject *v;
998 int i, reftype;
999 int scope = NAME_DEFAULT;
1000 int op = STOP_CODE;
1001 char buffer[MANGLE_LEN];
1003 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
1004 name = buffer;
1005 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
1006 c->c_errors++;
1007 i = 255;
1008 goto done;
1011 reftype = get_ref_type(c, name);
1012 switch (reftype) {
1013 case LOCAL:
1014 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
1015 scope = NAME_LOCAL;
1016 break;
1017 case GLOBAL_EXPLICIT:
1018 scope = NAME_GLOBAL;
1019 break;
1020 case GLOBAL_IMPLICIT:
1021 if (c->c_flags & CO_OPTIMIZED)
1022 scope = NAME_GLOBAL;
1023 break;
1024 case FREE:
1025 case CELL:
1026 scope = NAME_CLOSURE;
1027 break;
1030 i = com_addname(c, v);
1031 if (scope == NAME_LOCAL)
1032 i = com_lookup_arg(c->c_locals, v);
1033 else if (reftype == FREE)
1034 i = com_lookup_arg(c->c_freevars, v);
1035 else if (reftype == CELL)
1036 i = com_lookup_arg(c->c_cellvars, v);
1037 if (i == -1) {
1038 c->c_errors++; /* XXX no exception set */
1039 i = 255;
1040 goto done;
1042 Py_DECREF(v);
1044 switch (kind) {
1045 case VAR_LOAD:
1046 switch (scope) {
1047 case NAME_LOCAL:
1048 op = LOAD_FAST;
1049 break;
1050 case NAME_GLOBAL:
1051 op = LOAD_GLOBAL;
1052 break;
1053 case NAME_DEFAULT:
1054 op = LOAD_NAME;
1055 break;
1056 case NAME_CLOSURE:
1057 op = LOAD_DEREF;
1058 break;
1060 break;
1061 case VAR_STORE:
1062 switch (scope) {
1063 case NAME_LOCAL:
1064 op = STORE_FAST;
1065 break;
1066 case NAME_GLOBAL:
1067 op = STORE_GLOBAL;
1068 break;
1069 case NAME_DEFAULT:
1070 op = STORE_NAME;
1071 break;
1072 case NAME_CLOSURE:
1073 op = STORE_DEREF;
1074 break;
1076 break;
1077 case VAR_DELETE:
1078 switch (scope) {
1079 case NAME_LOCAL:
1080 op = DELETE_FAST;
1081 break;
1082 case NAME_GLOBAL:
1083 op = DELETE_GLOBAL;
1084 break;
1085 case NAME_DEFAULT:
1086 op = DELETE_NAME;
1087 break;
1088 case NAME_CLOSURE: {
1089 char buf[500];
1090 PyOS_snprintf(buf, sizeof(buf),
1091 DEL_CLOSURE_ERROR, name);
1092 com_error(c, PyExc_SyntaxError, buf);
1093 i = 255;
1094 break;
1097 break;
1099 done:
1100 com_addoparg(c, op, i);
1103 static void
1104 com_addopname(struct compiling *c, int op, node *n)
1106 char *name;
1107 char buffer[1000];
1108 /* XXX it is possible to write this code without the 1000
1109 chars on the total length of dotted names, I just can't be
1110 bothered right now */
1111 if (TYPE(n) == STAR)
1112 name = "*";
1113 else if (TYPE(n) == dotted_name) {
1114 char *p = buffer;
1115 int i;
1116 name = buffer;
1117 for (i = 0; i < NCH(n); i += 2) {
1118 char *s = STR(CHILD(n, i));
1119 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
1120 com_error(c, PyExc_MemoryError,
1121 "dotted_name too long");
1122 name = NULL;
1123 break;
1125 if (p != buffer)
1126 *p++ = '.';
1127 strcpy(p, s);
1128 p = strchr(p, '\0');
1131 else {
1132 REQ(n, NAME);
1133 name = STR(n);
1135 com_addop_name(c, op, name);
1138 static PyObject *
1139 parsenumber(struct compiling *co, char *s)
1141 char *end;
1142 long x;
1143 double dx;
1144 #ifndef WITHOUT_COMPLEX
1145 Py_complex c;
1146 int imflag;
1147 #endif
1149 errno = 0;
1150 end = s + strlen(s) - 1;
1151 #ifndef WITHOUT_COMPLEX
1152 imflag = *end == 'j' || *end == 'J';
1153 #endif
1154 if (*end == 'l' || *end == 'L')
1155 return PyLong_FromString(s, (char **)0, 0);
1156 if (s[0] == '0')
1157 x = (long) PyOS_strtoul(s, &end, 0);
1158 else
1159 x = PyOS_strtol(s, &end, 0);
1160 if (*end == '\0') {
1161 if (errno != 0)
1162 return PyLong_FromString(s, (char **)0, 0);
1163 return PyInt_FromLong(x);
1165 /* XXX Huge floats may silently fail */
1166 #ifndef WITHOUT_COMPLEX
1167 if (imflag) {
1168 c.real = 0.;
1169 PyFPE_START_PROTECT("atof", return 0)
1170 c.imag = atof(s);
1171 PyFPE_END_PROTECT(c)
1172 return PyComplex_FromCComplex(c);
1174 else
1175 #endif
1177 PyFPE_START_PROTECT("atof", return 0)
1178 dx = atof(s);
1179 PyFPE_END_PROTECT(dx)
1180 return PyFloat_FromDouble(dx);
1184 static PyObject *
1185 parsestr(struct compiling *com, char *s)
1187 PyObject *v;
1188 size_t len;
1189 char *buf;
1190 char *p;
1191 char *end;
1192 int c;
1193 int first = *s;
1194 int quote = first;
1195 int rawmode = 0;
1196 int unicode = 0;
1198 if (isalpha(quote) || quote == '_') {
1199 if (quote == 'u' || quote == 'U') {
1200 quote = *++s;
1201 unicode = 1;
1203 if (quote == 'r' || quote == 'R') {
1204 quote = *++s;
1205 rawmode = 1;
1208 if (quote != '\'' && quote != '\"') {
1209 PyErr_BadInternalCall();
1210 return NULL;
1212 s++;
1213 len = strlen(s);
1214 if (len > INT_MAX) {
1215 com_error(com, PyExc_OverflowError,
1216 "string to parse is too long");
1217 return NULL;
1219 if (s[--len] != quote) {
1220 PyErr_BadInternalCall();
1221 return NULL;
1223 if (len >= 4 && s[0] == quote && s[1] == quote) {
1224 s += 2;
1225 len -= 2;
1226 if (s[--len] != quote || s[--len] != quote) {
1227 PyErr_BadInternalCall();
1228 return NULL;
1231 #ifdef Py_USING_UNICODE
1232 if (unicode || Py_UnicodeFlag) {
1233 if (rawmode)
1234 v = PyUnicode_DecodeRawUnicodeEscape(
1235 s, len, NULL);
1236 else
1237 v = PyUnicode_DecodeUnicodeEscape(
1238 s, len, NULL);
1239 if (v == NULL)
1240 PyErr_SyntaxLocation(com->c_filename, com->c_lineno);
1241 return v;
1244 #endif
1245 if (rawmode || strchr(s, '\\') == NULL)
1246 return PyString_FromStringAndSize(s, len);
1247 v = PyString_FromStringAndSize((char *)NULL, len);
1248 if (v == NULL)
1249 return NULL;
1250 p = buf = PyString_AsString(v);
1251 end = s + len;
1252 while (s < end) {
1253 if (*s != '\\') {
1254 *p++ = *s++;
1255 continue;
1257 s++;
1258 switch (*s++) {
1259 /* XXX This assumes ASCII! */
1260 case '\n': break;
1261 case '\\': *p++ = '\\'; break;
1262 case '\'': *p++ = '\''; break;
1263 case '\"': *p++ = '\"'; break;
1264 case 'b': *p++ = '\b'; break;
1265 case 'f': *p++ = '\014'; break; /* FF */
1266 case 't': *p++ = '\t'; break;
1267 case 'n': *p++ = '\n'; break;
1268 case 'r': *p++ = '\r'; break;
1269 case 'v': *p++ = '\013'; break; /* VT */
1270 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1271 case '0': case '1': case '2': case '3':
1272 case '4': case '5': case '6': case '7':
1273 c = s[-1] - '0';
1274 if ('0' <= *s && *s <= '7') {
1275 c = (c<<3) + *s++ - '0';
1276 if ('0' <= *s && *s <= '7')
1277 c = (c<<3) + *s++ - '0';
1279 *p++ = c;
1280 break;
1281 case 'x':
1282 if (isxdigit(Py_CHARMASK(s[0]))
1283 && isxdigit(Py_CHARMASK(s[1]))) {
1284 unsigned int x = 0;
1285 c = Py_CHARMASK(*s);
1286 s++;
1287 if (isdigit(c))
1288 x = c - '0';
1289 else if (islower(c))
1290 x = 10 + c - 'a';
1291 else
1292 x = 10 + c - 'A';
1293 x = x << 4;
1294 c = Py_CHARMASK(*s);
1295 s++;
1296 if (isdigit(c))
1297 x += c - '0';
1298 else if (islower(c))
1299 x += 10 + c - 'a';
1300 else
1301 x += 10 + c - 'A';
1302 *p++ = x;
1303 break;
1305 Py_DECREF(v);
1306 com_error(com, PyExc_ValueError,
1307 "invalid \\x escape");
1308 return NULL;
1309 #ifndef Py_USING_UNICODE
1310 case 'u':
1311 case 'U':
1312 case 'N':
1313 if (unicode) {
1314 Py_DECREF(v);
1315 com_error(com, PyExc_ValueError,
1316 "Unicode escapes not legal "
1317 "when Unicode disabled");
1318 return NULL;
1320 #endif
1321 default:
1322 *p++ = '\\';
1323 *p++ = s[-1];
1324 break;
1327 _PyString_Resize(&v, (int)(p - buf));
1328 return v;
1331 static PyObject *
1332 parsestrplus(struct compiling* c, node *n)
1334 PyObject *v;
1335 int i;
1336 REQ(CHILD(n, 0), STRING);
1337 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
1338 /* String literal concatenation */
1339 for (i = 1; i < NCH(n); i++) {
1340 PyObject *s;
1341 s = parsestr(c, STR(CHILD(n, i)));
1342 if (s == NULL)
1343 goto onError;
1344 if (PyString_Check(v) && PyString_Check(s)) {
1345 PyString_ConcatAndDel(&v, s);
1346 if (v == NULL)
1347 goto onError;
1349 #ifdef Py_USING_UNICODE
1350 else {
1351 PyObject *temp;
1352 temp = PyUnicode_Concat(v, s);
1353 Py_DECREF(s);
1354 if (temp == NULL)
1355 goto onError;
1356 Py_DECREF(v);
1357 v = temp;
1359 #endif
1362 return v;
1364 onError:
1365 Py_XDECREF(v);
1366 return NULL;
1369 static void
1370 com_list_for(struct compiling *c, node *n, node *e, char *t)
1372 int anchor = 0;
1373 int save_begin = c->c_begin;
1375 /* list_iter: for v in expr [list_iter] */
1376 com_node(c, CHILD(n, 3)); /* expr */
1377 com_addbyte(c, GET_ITER);
1378 c->c_begin = c->c_nexti;
1379 com_addoparg(c, SET_LINENO, n->n_lineno);
1380 com_addfwref(c, FOR_ITER, &anchor);
1381 com_push(c, 1);
1382 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1383 c->c_loops++;
1384 com_list_iter(c, n, e, t);
1385 c->c_loops--;
1386 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1387 c->c_begin = save_begin;
1388 com_backpatch(c, anchor);
1389 com_pop(c, 1); /* FOR_ITER has popped this */
1392 static void
1393 com_list_if(struct compiling *c, node *n, node *e, char *t)
1395 int anchor = 0;
1396 int a = 0;
1397 /* list_iter: 'if' test [list_iter] */
1398 com_addoparg(c, SET_LINENO, n->n_lineno);
1399 com_node(c, CHILD(n, 1));
1400 com_addfwref(c, JUMP_IF_FALSE, &a);
1401 com_addbyte(c, POP_TOP);
1402 com_pop(c, 1);
1403 com_list_iter(c, n, e, t);
1404 com_addfwref(c, JUMP_FORWARD, &anchor);
1405 com_backpatch(c, a);
1406 /* We jump here with an extra entry which we now pop */
1407 com_addbyte(c, POP_TOP);
1408 com_backpatch(c, anchor);
1411 static void
1412 com_list_iter(struct compiling *c,
1413 node *p, /* parent of list_iter node */
1414 node *e, /* element expression node */
1415 char *t /* name of result list temp local */)
1417 /* list_iter is the last child in a listmaker, list_for, or list_if */
1418 node *n = CHILD(p, NCH(p)-1);
1419 if (TYPE(n) == list_iter) {
1420 n = CHILD(n, 0);
1421 switch (TYPE(n)) {
1422 case list_for:
1423 com_list_for(c, n, e, t);
1424 break;
1425 case list_if:
1426 com_list_if(c, n, e, t);
1427 break;
1428 default:
1429 com_error(c, PyExc_SystemError,
1430 "invalid list_iter node type");
1433 else {
1434 com_addop_varname(c, VAR_LOAD, t);
1435 com_push(c, 1);
1436 com_node(c, e);
1437 com_addoparg(c, CALL_FUNCTION, 1);
1438 com_addbyte(c, POP_TOP);
1439 com_pop(c, 2);
1443 static void
1444 com_list_comprehension(struct compiling *c, node *n)
1446 /* listmaker: test list_for */
1447 char tmpname[30];
1448 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
1449 com_addoparg(c, BUILD_LIST, 0);
1450 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1451 com_push(c, 2);
1452 com_addop_name(c, LOAD_ATTR, "append");
1453 com_addop_varname(c, VAR_STORE, tmpname);
1454 com_pop(c, 1);
1455 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1456 com_addop_varname(c, VAR_DELETE, tmpname);
1457 --c->c_tmpname;
1460 static void
1461 com_listmaker(struct compiling *c, node *n)
1463 /* listmaker: test ( list_for | (',' test)* [','] ) */
1464 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1465 com_list_comprehension(c, n);
1466 else {
1467 int len = 0;
1468 int i;
1469 for (i = 0; i < NCH(n); i += 2, len++)
1470 com_node(c, CHILD(n, i));
1471 com_addoparg(c, BUILD_LIST, len);
1472 com_pop(c, len-1);
1476 static void
1477 com_dictmaker(struct compiling *c, node *n)
1479 int i;
1480 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1481 for (i = 0; i+2 < NCH(n); i += 4) {
1482 /* We must arrange things just right for STORE_SUBSCR.
1483 It wants the stack to look like (value) (dict) (key) */
1484 com_addbyte(c, DUP_TOP);
1485 com_push(c, 1);
1486 com_node(c, CHILD(n, i+2)); /* value */
1487 com_addbyte(c, ROT_TWO);
1488 com_node(c, CHILD(n, i)); /* key */
1489 com_addbyte(c, STORE_SUBSCR);
1490 com_pop(c, 3);
1494 static void
1495 com_atom(struct compiling *c, node *n)
1497 node *ch;
1498 PyObject *v;
1499 int i;
1500 REQ(n, atom);
1501 ch = CHILD(n, 0);
1502 switch (TYPE(ch)) {
1503 case LPAR:
1504 if (TYPE(CHILD(n, 1)) == RPAR) {
1505 com_addoparg(c, BUILD_TUPLE, 0);
1506 com_push(c, 1);
1508 else
1509 com_node(c, CHILD(n, 1));
1510 break;
1511 case LSQB: /* '[' [listmaker] ']' */
1512 if (TYPE(CHILD(n, 1)) == RSQB) {
1513 com_addoparg(c, BUILD_LIST, 0);
1514 com_push(c, 1);
1516 else
1517 com_listmaker(c, CHILD(n, 1));
1518 break;
1519 case LBRACE: /* '{' [dictmaker] '}' */
1520 com_addoparg(c, BUILD_MAP, 0);
1521 com_push(c, 1);
1522 if (TYPE(CHILD(n, 1)) == dictmaker)
1523 com_dictmaker(c, CHILD(n, 1));
1524 break;
1525 case BACKQUOTE:
1526 com_node(c, CHILD(n, 1));
1527 com_addbyte(c, UNARY_CONVERT);
1528 break;
1529 case NUMBER:
1530 if ((v = parsenumber(c, STR(ch))) == NULL) {
1531 i = 255;
1533 else {
1534 i = com_addconst(c, v);
1535 Py_DECREF(v);
1537 com_addoparg(c, LOAD_CONST, i);
1538 com_push(c, 1);
1539 break;
1540 case STRING:
1541 v = parsestrplus(c, n);
1542 if (v == NULL) {
1543 c->c_errors++;
1544 i = 255;
1546 else {
1547 i = com_addconst(c, v);
1548 Py_DECREF(v);
1550 com_addoparg(c, LOAD_CONST, i);
1551 com_push(c, 1);
1552 break;
1553 case NAME:
1554 com_addop_varname(c, VAR_LOAD, STR(ch));
1555 com_push(c, 1);
1556 break;
1557 default:
1558 com_error(c, PyExc_SystemError,
1559 "com_atom: unexpected node type");
1563 static void
1564 com_slice(struct compiling *c, node *n, int op)
1566 if (NCH(n) == 1) {
1567 com_addbyte(c, op);
1569 else if (NCH(n) == 2) {
1570 if (TYPE(CHILD(n, 0)) != COLON) {
1571 com_node(c, CHILD(n, 0));
1572 com_addbyte(c, op+1);
1574 else {
1575 com_node(c, CHILD(n, 1));
1576 com_addbyte(c, op+2);
1578 com_pop(c, 1);
1580 else {
1581 com_node(c, CHILD(n, 0));
1582 com_node(c, CHILD(n, 2));
1583 com_addbyte(c, op+3);
1584 com_pop(c, 2);
1588 static void
1589 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1591 if (NCH(n) == 1) {
1592 com_addbyte(c, DUP_TOP);
1593 com_push(c, 1);
1594 com_addbyte(c, SLICE);
1595 com_node(c, augn);
1596 com_addbyte(c, opcode);
1597 com_pop(c, 1);
1598 com_addbyte(c, ROT_TWO);
1599 com_addbyte(c, STORE_SLICE);
1600 com_pop(c, 2);
1601 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1602 com_node(c, CHILD(n, 0));
1603 com_addoparg(c, DUP_TOPX, 2);
1604 com_push(c, 2);
1605 com_addbyte(c, SLICE+1);
1606 com_pop(c, 1);
1607 com_node(c, augn);
1608 com_addbyte(c, opcode);
1609 com_pop(c, 1);
1610 com_addbyte(c, ROT_THREE);
1611 com_addbyte(c, STORE_SLICE+1);
1612 com_pop(c, 3);
1613 } else if (NCH(n) == 2) {
1614 com_node(c, CHILD(n, 1));
1615 com_addoparg(c, DUP_TOPX, 2);
1616 com_push(c, 2);
1617 com_addbyte(c, SLICE+2);
1618 com_pop(c, 1);
1619 com_node(c, augn);
1620 com_addbyte(c, opcode);
1621 com_pop(c, 1);
1622 com_addbyte(c, ROT_THREE);
1623 com_addbyte(c, STORE_SLICE+2);
1624 com_pop(c, 3);
1625 } else {
1626 com_node(c, CHILD(n, 0));
1627 com_node(c, CHILD(n, 2));
1628 com_addoparg(c, DUP_TOPX, 3);
1629 com_push(c, 3);
1630 com_addbyte(c, SLICE+3);
1631 com_pop(c, 2);
1632 com_node(c, augn);
1633 com_addbyte(c, opcode);
1634 com_pop(c, 1);
1635 com_addbyte(c, ROT_FOUR);
1636 com_addbyte(c, STORE_SLICE+3);
1637 com_pop(c, 4);
1641 static void
1642 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1644 node *m;
1645 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1646 if (NCH(n) == 1) {
1647 if (*pkeywords != NULL) {
1648 com_error(c, PyExc_SyntaxError,
1649 "non-keyword arg after keyword arg");
1651 else {
1652 com_node(c, CHILD(n, 0));
1654 return;
1656 m = n;
1657 do {
1658 m = CHILD(m, 0);
1659 } while (NCH(m) == 1);
1660 if (TYPE(m) != NAME) {
1661 /* f(lambda x: x[0] = 3) ends up getting parsed with
1662 * LHS test = lambda x: x[0], and RHS test = 3.
1663 * SF bug 132313 points out that complaining about a keyword
1664 * then is very confusing.
1666 com_error(c, PyExc_SyntaxError,
1667 TYPE(m) == lambdef ?
1668 "lambda cannot contain assignment" :
1669 "keyword can't be an expression");
1671 else {
1672 PyObject *v = PyString_InternFromString(STR(m));
1673 if (v != NULL && *pkeywords == NULL)
1674 *pkeywords = PyDict_New();
1675 if (v == NULL)
1676 c->c_errors++;
1677 else if (*pkeywords == NULL) {
1678 c->c_errors++;
1679 Py_DECREF(v);
1680 } else {
1681 if (PyDict_GetItem(*pkeywords, v) != NULL)
1682 com_error(c, PyExc_SyntaxError,
1683 "duplicate keyword argument");
1684 else
1685 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1686 c->c_errors++;
1687 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1688 com_push(c, 1);
1689 Py_DECREF(v);
1692 com_node(c, CHILD(n, 2));
1695 static void
1696 com_call_function(struct compiling *c, node *n)
1698 if (TYPE(n) == RPAR) {
1699 com_addoparg(c, CALL_FUNCTION, 0);
1701 else {
1702 PyObject *keywords = NULL;
1703 int i, na, nk;
1704 int lineno = n->n_lineno;
1705 int star_flag = 0;
1706 int starstar_flag = 0;
1707 int opcode;
1708 REQ(n, arglist);
1709 na = 0;
1710 nk = 0;
1711 for (i = 0; i < NCH(n); i += 2) {
1712 node *ch = CHILD(n, i);
1713 if (TYPE(ch) == STAR ||
1714 TYPE(ch) == DOUBLESTAR)
1715 break;
1716 if (ch->n_lineno != lineno) {
1717 lineno = ch->n_lineno;
1718 com_addoparg(c, SET_LINENO, lineno);
1720 com_argument(c, ch, &keywords);
1721 if (keywords == NULL)
1722 na++;
1723 else
1724 nk++;
1726 Py_XDECREF(keywords);
1727 while (i < NCH(n)) {
1728 node *tok = CHILD(n, i);
1729 node *ch = CHILD(n, i+1);
1730 i += 3;
1731 switch (TYPE(tok)) {
1732 case STAR: star_flag = 1; break;
1733 case DOUBLESTAR: starstar_flag = 1; break;
1735 com_node(c, ch);
1737 if (na > 255 || nk > 255) {
1738 com_error(c, PyExc_SyntaxError,
1739 "more than 255 arguments");
1741 if (star_flag || starstar_flag)
1742 opcode = CALL_FUNCTION_VAR - 1 +
1743 star_flag + (starstar_flag << 1);
1744 else
1745 opcode = CALL_FUNCTION;
1746 com_addoparg(c, opcode, na | (nk << 8));
1747 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1751 static void
1752 com_select_member(struct compiling *c, node *n)
1754 com_addopname(c, LOAD_ATTR, n);
1757 static void
1758 com_sliceobj(struct compiling *c, node *n)
1760 int i=0;
1761 int ns=2; /* number of slice arguments */
1762 node *ch;
1764 /* first argument */
1765 if (TYPE(CHILD(n,i)) == COLON) {
1766 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1767 com_push(c, 1);
1768 i++;
1770 else {
1771 com_node(c, CHILD(n,i));
1772 i++;
1773 REQ(CHILD(n,i),COLON);
1774 i++;
1776 /* second argument */
1777 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1778 com_node(c, CHILD(n,i));
1779 i++;
1781 else {
1782 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1783 com_push(c, 1);
1785 /* remaining arguments */
1786 for (; i < NCH(n); i++) {
1787 ns++;
1788 ch=CHILD(n,i);
1789 REQ(ch, sliceop);
1790 if (NCH(ch) == 1) {
1791 /* right argument of ':' missing */
1792 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1793 com_push(c, 1);
1795 else
1796 com_node(c, CHILD(ch,1));
1798 com_addoparg(c, BUILD_SLICE, ns);
1799 com_pop(c, 1 + (ns == 3));
1802 static void
1803 com_subscript(struct compiling *c, node *n)
1805 node *ch;
1806 REQ(n, subscript);
1807 ch = CHILD(n,0);
1808 /* check for rubber index */
1809 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1810 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1811 com_push(c, 1);
1813 else {
1814 /* check for slice */
1815 if ((TYPE(ch) == COLON || NCH(n) > 1))
1816 com_sliceobj(c, n);
1817 else {
1818 REQ(ch, test);
1819 com_node(c, ch);
1824 static void
1825 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1827 int i, op;
1828 REQ(n, subscriptlist);
1829 /* Check to make backward compatible slice behavior for '[i:j]' */
1830 if (NCH(n) == 1) {
1831 node *sub = CHILD(n, 0); /* subscript */
1832 /* 'Basic' slice, should have exactly one colon. */
1833 if ((TYPE(CHILD(sub, 0)) == COLON
1834 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1835 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1837 switch (assigning) {
1838 case OP_DELETE:
1839 op = DELETE_SLICE;
1840 break;
1841 case OP_ASSIGN:
1842 op = STORE_SLICE;
1843 break;
1844 case OP_APPLY:
1845 op = SLICE;
1846 break;
1847 default:
1848 com_augassign_slice(c, sub, assigning, augn);
1849 return;
1851 com_slice(c, sub, op);
1852 if (op == STORE_SLICE)
1853 com_pop(c, 2);
1854 else if (op == DELETE_SLICE)
1855 com_pop(c, 1);
1856 return;
1859 /* Else normal subscriptlist. Compile each subscript. */
1860 for (i = 0; i < NCH(n); i += 2)
1861 com_subscript(c, CHILD(n, i));
1862 /* Put multiple subscripts into a tuple */
1863 if (NCH(n) > 1) {
1864 i = (NCH(n)+1) / 2;
1865 com_addoparg(c, BUILD_TUPLE, i);
1866 com_pop(c, i-1);
1868 switch (assigning) {
1869 case OP_DELETE:
1870 op = DELETE_SUBSCR;
1871 i = 2;
1872 break;
1873 default:
1874 case OP_ASSIGN:
1875 op = STORE_SUBSCR;
1876 i = 3;
1877 break;
1878 case OP_APPLY:
1879 op = BINARY_SUBSCR;
1880 i = 1;
1881 break;
1883 if (assigning > OP_APPLY) {
1884 com_addoparg(c, DUP_TOPX, 2);
1885 com_push(c, 2);
1886 com_addbyte(c, BINARY_SUBSCR);
1887 com_pop(c, 1);
1888 com_node(c, augn);
1889 com_addbyte(c, assigning);
1890 com_pop(c, 1);
1891 com_addbyte(c, ROT_THREE);
1893 com_addbyte(c, op);
1894 com_pop(c, i);
1897 static void
1898 com_apply_trailer(struct compiling *c, node *n)
1900 REQ(n, trailer);
1901 switch (TYPE(CHILD(n, 0))) {
1902 case LPAR:
1903 com_call_function(c, CHILD(n, 1));
1904 break;
1905 case DOT:
1906 com_select_member(c, CHILD(n, 1));
1907 break;
1908 case LSQB:
1909 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1910 break;
1911 default:
1912 com_error(c, PyExc_SystemError,
1913 "com_apply_trailer: unknown trailer type");
1917 static void
1918 com_power(struct compiling *c, node *n)
1920 int i;
1921 REQ(n, power);
1922 com_atom(c, CHILD(n, 0));
1923 for (i = 1; i < NCH(n); i++) {
1924 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1925 com_factor(c, CHILD(n, i+1));
1926 com_addbyte(c, BINARY_POWER);
1927 com_pop(c, 1);
1928 break;
1930 else
1931 com_apply_trailer(c, CHILD(n, i));
1935 static void
1936 com_invert_constant(struct compiling *c, node *n)
1938 /* Compute the inverse of int and longs and use them directly,
1939 but be prepared to generate code for all other
1940 possibilities (invalid numbers, floats, complex).
1942 PyObject *num, *inv = NULL;
1943 int i;
1945 REQ(n, NUMBER);
1946 num = parsenumber(c, STR(n));
1947 if (num == NULL)
1948 i = 255;
1949 else {
1950 inv = PyNumber_Invert(num);
1951 if (inv == NULL) {
1952 PyErr_Clear();
1953 i = com_addconst(c, num);
1954 } else {
1955 i = com_addconst(c, inv);
1956 Py_DECREF(inv);
1958 Py_DECREF(num);
1960 com_addoparg(c, LOAD_CONST, i);
1961 com_push(c, 1);
1962 if (num != NULL && inv == NULL)
1963 com_addbyte(c, UNARY_INVERT);
1966 static int
1967 is_float_zero(const char *p)
1969 int found_radix_point = 0;
1970 int ch;
1971 while ((ch = Py_CHARMASK(*p++)) != '\0') {
1972 switch (ch) {
1973 case '0':
1974 /* no reason to believe it's not 0 -- continue */
1975 break;
1977 case 'e': case 'E': case 'j': case 'J':
1978 /* If this was a hex constant, we already would have
1979 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1980 must be an exponent marker, and we haven't yet
1981 seen a non-zero digit, and it doesn't matter what
1982 the exponent is then. For 'j' or 'J' similarly,
1983 except that this is an imaginary 0 then. */
1984 return 1;
1986 case '.':
1987 found_radix_point = 1;
1988 break;
1990 default:
1991 return 0;
1994 return found_radix_point;
1997 static void
1998 com_factor(struct compiling *c, node *n)
2000 int childtype = TYPE(CHILD(n, 0));
2001 node *pfactor, *ppower, *patom, *pnum;
2002 REQ(n, factor);
2003 /* If the unary +, -, or ~ operator is applied to a constant,
2004 don't generate a UNARY_xxx opcode. Just store the
2005 approriate value as a constant. If the value is negative,
2006 extend the string containing the constant and insert a
2007 negative in the 0th position -- unless we're doing unary minus
2008 of a floating zero! In that case the sign is significant, but
2009 the const dict can't distinguish +0.0 from -0.0.
2011 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
2012 && NCH(n) == 2
2013 && TYPE((pfactor = CHILD(n, 1))) == factor
2014 && NCH(pfactor) == 1
2015 && TYPE((ppower = CHILD(pfactor, 0))) == power
2016 && NCH(ppower) == 1
2017 && TYPE((patom = CHILD(ppower, 0))) == atom
2018 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
2019 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
2020 if (childtype == TILDE) {
2021 com_invert_constant(c, pnum);
2022 return;
2024 if (childtype == MINUS) {
2025 char *s = PyMem_Malloc(strlen(STR(pnum)) + 2);
2026 if (s == NULL) {
2027 com_error(c, PyExc_MemoryError, "");
2028 com_addbyte(c, 255);
2029 return;
2031 s[0] = '-';
2032 strcpy(s + 1, STR(pnum));
2033 PyMem_Free(STR(pnum));
2034 STR(pnum) = s;
2036 com_atom(c, patom);
2038 else if (childtype == PLUS) {
2039 com_factor(c, CHILD(n, 1));
2040 com_addbyte(c, UNARY_POSITIVE);
2042 else if (childtype == MINUS) {
2043 com_factor(c, CHILD(n, 1));
2044 com_addbyte(c, UNARY_NEGATIVE);
2046 else if (childtype == TILDE) {
2047 com_factor(c, CHILD(n, 1));
2048 com_addbyte(c, UNARY_INVERT);
2050 else {
2051 com_power(c, CHILD(n, 0));
2055 static void
2056 com_term(struct compiling *c, node *n)
2058 int i;
2059 int op;
2060 REQ(n, term);
2061 com_factor(c, CHILD(n, 0));
2062 for (i = 2; i < NCH(n); i += 2) {
2063 com_factor(c, CHILD(n, i));
2064 switch (TYPE(CHILD(n, i-1))) {
2065 case STAR:
2066 op = BINARY_MULTIPLY;
2067 break;
2068 case SLASH:
2069 if (c->c_flags & CO_FUTURE_DIVISION)
2070 op = BINARY_TRUE_DIVIDE;
2071 else
2072 op = BINARY_DIVIDE;
2073 break;
2074 case PERCENT:
2075 op = BINARY_MODULO;
2076 break;
2077 case DOUBLESLASH:
2078 op = BINARY_FLOOR_DIVIDE;
2079 break;
2080 default:
2081 com_error(c, PyExc_SystemError,
2082 "com_term: operator not *, /, // or %");
2083 op = 255;
2085 com_addbyte(c, op);
2086 com_pop(c, 1);
2090 static void
2091 com_arith_expr(struct compiling *c, node *n)
2093 int i;
2094 int op;
2095 REQ(n, arith_expr);
2096 com_term(c, CHILD(n, 0));
2097 for (i = 2; i < NCH(n); i += 2) {
2098 com_term(c, CHILD(n, i));
2099 switch (TYPE(CHILD(n, i-1))) {
2100 case PLUS:
2101 op = BINARY_ADD;
2102 break;
2103 case MINUS:
2104 op = BINARY_SUBTRACT;
2105 break;
2106 default:
2107 com_error(c, PyExc_SystemError,
2108 "com_arith_expr: operator not + or -");
2109 op = 255;
2111 com_addbyte(c, op);
2112 com_pop(c, 1);
2116 static void
2117 com_shift_expr(struct compiling *c, node *n)
2119 int i;
2120 int op;
2121 REQ(n, shift_expr);
2122 com_arith_expr(c, CHILD(n, 0));
2123 for (i = 2; i < NCH(n); i += 2) {
2124 com_arith_expr(c, CHILD(n, i));
2125 switch (TYPE(CHILD(n, i-1))) {
2126 case LEFTSHIFT:
2127 op = BINARY_LSHIFT;
2128 break;
2129 case RIGHTSHIFT:
2130 op = BINARY_RSHIFT;
2131 break;
2132 default:
2133 com_error(c, PyExc_SystemError,
2134 "com_shift_expr: operator not << or >>");
2135 op = 255;
2137 com_addbyte(c, op);
2138 com_pop(c, 1);
2142 static void
2143 com_and_expr(struct compiling *c, node *n)
2145 int i;
2146 int op;
2147 REQ(n, and_expr);
2148 com_shift_expr(c, CHILD(n, 0));
2149 for (i = 2; i < NCH(n); i += 2) {
2150 com_shift_expr(c, CHILD(n, i));
2151 if (TYPE(CHILD(n, i-1)) == AMPER) {
2152 op = BINARY_AND;
2154 else {
2155 com_error(c, PyExc_SystemError,
2156 "com_and_expr: operator not &");
2157 op = 255;
2159 com_addbyte(c, op);
2160 com_pop(c, 1);
2164 static void
2165 com_xor_expr(struct compiling *c, node *n)
2167 int i;
2168 int op;
2169 REQ(n, xor_expr);
2170 com_and_expr(c, CHILD(n, 0));
2171 for (i = 2; i < NCH(n); i += 2) {
2172 com_and_expr(c, CHILD(n, i));
2173 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2174 op = BINARY_XOR;
2176 else {
2177 com_error(c, PyExc_SystemError,
2178 "com_xor_expr: operator not ^");
2179 op = 255;
2181 com_addbyte(c, op);
2182 com_pop(c, 1);
2186 static void
2187 com_expr(struct compiling *c, node *n)
2189 int i;
2190 int op;
2191 REQ(n, expr);
2192 com_xor_expr(c, CHILD(n, 0));
2193 for (i = 2; i < NCH(n); i += 2) {
2194 com_xor_expr(c, CHILD(n, i));
2195 if (TYPE(CHILD(n, i-1)) == VBAR) {
2196 op = BINARY_OR;
2198 else {
2199 com_error(c, PyExc_SystemError,
2200 "com_expr: expr operator not |");
2201 op = 255;
2203 com_addbyte(c, op);
2204 com_pop(c, 1);
2208 static enum cmp_op
2209 cmp_type(node *n)
2211 REQ(n, comp_op);
2212 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2213 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2214 if (NCH(n) == 1) {
2215 n = CHILD(n, 0);
2216 switch (TYPE(n)) {
2217 case LESS: return PyCmp_LT;
2218 case GREATER: return PyCmp_GT;
2219 case EQEQUAL: /* == */
2220 case EQUAL: return PyCmp_EQ;
2221 case LESSEQUAL: return PyCmp_LE;
2222 case GREATEREQUAL: return PyCmp_GE;
2223 case NOTEQUAL: return PyCmp_NE; /* <> or != */
2224 case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
2225 if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
2228 else if (NCH(n) == 2) {
2229 switch (TYPE(CHILD(n, 0))) {
2230 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2231 return PyCmp_NOT_IN;
2232 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2233 return PyCmp_IS_NOT;
2236 return PyCmp_BAD;
2239 static void
2240 com_comparison(struct compiling *c, node *n)
2242 int i;
2243 enum cmp_op op;
2244 int anchor;
2245 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2246 com_expr(c, CHILD(n, 0));
2247 if (NCH(n) == 1)
2248 return;
2250 /****************************************************************
2251 The following code is generated for all but the last
2252 comparison in a chain:
2254 label: on stack: opcode: jump to:
2256 a <code to load b>
2257 a, b DUP_TOP
2258 a, b, b ROT_THREE
2259 b, a, b COMPARE_OP
2260 b, 0-or-1 JUMP_IF_FALSE L1
2261 b, 1 POP_TOP
2264 We are now ready to repeat this sequence for the next
2265 comparison in the chain.
2267 For the last we generate:
2269 b <code to load c>
2270 b, c COMPARE_OP
2271 0-or-1
2273 If there were any jumps to L1 (i.e., there was more than one
2274 comparison), we generate:
2276 0-or-1 JUMP_FORWARD L2
2277 L1: b, 0 ROT_TWO
2278 0, b POP_TOP
2280 L2: 0-or-1
2281 ****************************************************************/
2283 anchor = 0;
2285 for (i = 2; i < NCH(n); i += 2) {
2286 com_expr(c, CHILD(n, i));
2287 if (i+2 < NCH(n)) {
2288 com_addbyte(c, DUP_TOP);
2289 com_push(c, 1);
2290 com_addbyte(c, ROT_THREE);
2292 op = cmp_type(CHILD(n, i-1));
2293 if (op == PyCmp_BAD) {
2294 com_error(c, PyExc_SystemError,
2295 "com_comparison: unknown comparison op");
2297 com_addoparg(c, COMPARE_OP, op);
2298 com_pop(c, 1);
2299 if (i+2 < NCH(n)) {
2300 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2301 com_addbyte(c, POP_TOP);
2302 com_pop(c, 1);
2306 if (anchor) {
2307 int anchor2 = 0;
2308 com_addfwref(c, JUMP_FORWARD, &anchor2);
2309 com_backpatch(c, anchor);
2310 com_addbyte(c, ROT_TWO);
2311 com_addbyte(c, POP_TOP);
2312 com_backpatch(c, anchor2);
2316 static void
2317 com_not_test(struct compiling *c, node *n)
2319 REQ(n, not_test); /* 'not' not_test | comparison */
2320 if (NCH(n) == 1) {
2321 com_comparison(c, CHILD(n, 0));
2323 else {
2324 com_not_test(c, CHILD(n, 1));
2325 com_addbyte(c, UNARY_NOT);
2329 static void
2330 com_and_test(struct compiling *c, node *n)
2332 int i;
2333 int anchor;
2334 REQ(n, and_test); /* not_test ('and' not_test)* */
2335 anchor = 0;
2336 i = 0;
2337 for (;;) {
2338 com_not_test(c, CHILD(n, i));
2339 if ((i += 2) >= NCH(n))
2340 break;
2341 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2342 com_addbyte(c, POP_TOP);
2343 com_pop(c, 1);
2345 if (anchor)
2346 com_backpatch(c, anchor);
2349 static int
2350 com_make_closure(struct compiling *c, PyCodeObject *co)
2352 int i, free = PyCode_GetNumFree(co);
2353 if (free == 0)
2354 return 0;
2355 for (i = 0; i < free; ++i) {
2356 /* Bypass com_addop_varname because it will generate
2357 LOAD_DEREF but LOAD_CLOSURE is needed.
2359 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2360 int arg, reftype;
2362 /* Special case: If a class contains a method with a
2363 free variable that has the same name as a method,
2364 the name will be considered free *and* local in the
2365 class. It should be handled by the closure, as
2366 well as by the normal name loookup logic.
2368 reftype = get_ref_type(c, PyString_AS_STRING(name));
2369 if (reftype == CELL)
2370 arg = com_lookup_arg(c->c_cellvars, name);
2371 else /* (reftype == FREE) */
2372 arg = com_lookup_arg(c->c_freevars, name);
2373 if (arg == -1) {
2374 fprintf(stderr, "lookup %s in %s %d %d\n"
2375 "freevars of %s: %s\n",
2376 PyObject_REPR(name),
2377 c->c_name,
2378 reftype, arg,
2379 PyString_AS_STRING(co->co_name),
2380 PyObject_REPR(co->co_freevars));
2381 Py_FatalError("com_make_closure()");
2383 com_addoparg(c, LOAD_CLOSURE, arg);
2386 com_push(c, free);
2387 return 1;
2390 static void
2391 com_test(struct compiling *c, node *n)
2393 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2394 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2395 PyCodeObject *co;
2396 int i, closure;
2397 int ndefs = com_argdefs(c, CHILD(n, 0));
2398 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2399 n->n_lineno);
2400 co = icompile(CHILD(n, 0), c);
2401 if (co == NULL) {
2402 c->c_errors++;
2403 return;
2405 symtable_exit_scope(c->c_symtable);
2406 i = com_addconst(c, (PyObject *)co);
2407 closure = com_make_closure(c, co);
2408 com_addoparg(c, LOAD_CONST, i);
2409 com_push(c, 1);
2410 if (closure) {
2411 com_addoparg(c, MAKE_CLOSURE, ndefs);
2412 com_pop(c, PyCode_GetNumFree(co));
2413 } else
2414 com_addoparg(c, MAKE_FUNCTION, ndefs);
2415 Py_DECREF(co);
2416 com_pop(c, ndefs);
2418 else {
2419 int anchor = 0;
2420 int i = 0;
2421 for (;;) {
2422 com_and_test(c, CHILD(n, i));
2423 if ((i += 2) >= NCH(n))
2424 break;
2425 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2426 com_addbyte(c, POP_TOP);
2427 com_pop(c, 1);
2429 if (anchor)
2430 com_backpatch(c, anchor);
2434 static void
2435 com_list(struct compiling *c, node *n, int toplevel)
2437 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2438 if (NCH(n) == 1 && !toplevel) {
2439 com_node(c, CHILD(n, 0));
2441 else {
2442 int i;
2443 int len;
2444 len = (NCH(n) + 1) / 2;
2445 for (i = 0; i < NCH(n); i += 2)
2446 com_node(c, CHILD(n, i));
2447 com_addoparg(c, BUILD_TUPLE, len);
2448 com_pop(c, len-1);
2453 /* Begin of assignment compilation */
2456 static void
2457 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2459 com_addbyte(c, DUP_TOP);
2460 com_push(c, 1);
2461 com_addopname(c, LOAD_ATTR, n);
2462 com_node(c, augn);
2463 com_addbyte(c, opcode);
2464 com_pop(c, 1);
2465 com_addbyte(c, ROT_TWO);
2466 com_addopname(c, STORE_ATTR, n);
2467 com_pop(c, 2);
2470 static void
2471 com_assign_attr(struct compiling *c, node *n, int assigning)
2473 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2474 com_pop(c, assigning ? 2 : 1);
2477 static void
2478 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2480 REQ(n, trailer);
2481 switch (TYPE(CHILD(n, 0))) {
2482 case LPAR: /* '(' [exprlist] ')' */
2483 if (assigning == OP_DELETE)
2484 com_error(c, PyExc_SyntaxError,
2485 "can't delete function call");
2486 else
2487 com_error(c, PyExc_SyntaxError,
2488 "can't assign to function call");
2489 break;
2490 case DOT: /* '.' NAME */
2491 if (assigning > OP_APPLY)
2492 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2493 else
2494 com_assign_attr(c, CHILD(n, 1), assigning);
2495 break;
2496 case LSQB: /* '[' subscriptlist ']' */
2497 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2498 break;
2499 default:
2500 com_error(c, PyExc_SystemError, "unknown trailer type");
2504 static void
2505 com_assign_sequence(struct compiling *c, node *n, int assigning)
2507 int i;
2508 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2509 REQ(n, exprlist);
2510 if (assigning) {
2511 i = (NCH(n)+1)/2;
2512 com_addoparg(c, UNPACK_SEQUENCE, i);
2513 com_push(c, i-1);
2515 for (i = 0; i < NCH(n); i += 2)
2516 com_assign(c, CHILD(n, i), assigning, NULL);
2519 static void
2520 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2522 REQ(n, NAME);
2523 com_addop_varname(c, VAR_LOAD, STR(n));
2524 com_push(c, 1);
2525 com_node(c, augn);
2526 com_addbyte(c, opcode);
2527 com_pop(c, 1);
2528 com_assign_name(c, n, OP_ASSIGN);
2531 static void
2532 com_assign_name(struct compiling *c, node *n, int assigning)
2534 REQ(n, NAME);
2535 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2536 if (assigning)
2537 com_pop(c, 1);
2540 static void
2541 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2543 /* Loop to avoid trivial recursion */
2544 for (;;) {
2545 switch (TYPE(n)) {
2547 case exprlist:
2548 case testlist:
2549 case testlist1:
2550 if (NCH(n) > 1) {
2551 if (assigning > OP_APPLY) {
2552 com_error(c, PyExc_SyntaxError,
2553 "augmented assign to tuple not possible");
2554 return;
2556 com_assign_sequence(c, n, assigning);
2557 return;
2559 n = CHILD(n, 0);
2560 break;
2562 case test:
2563 case and_test:
2564 case not_test:
2565 case comparison:
2566 case expr:
2567 case xor_expr:
2568 case and_expr:
2569 case shift_expr:
2570 case arith_expr:
2571 case term:
2572 case factor:
2573 if (NCH(n) > 1) {
2574 com_error(c, PyExc_SyntaxError,
2575 "can't assign to operator");
2576 return;
2578 n = CHILD(n, 0);
2579 break;
2581 case power: /* atom trailer* ('**' power)*
2582 ('+'|'-'|'~') factor | atom trailer* */
2583 if (TYPE(CHILD(n, 0)) != atom) {
2584 com_error(c, PyExc_SyntaxError,
2585 "can't assign to operator");
2586 return;
2588 if (NCH(n) > 1) { /* trailer or exponent present */
2589 int i;
2590 com_node(c, CHILD(n, 0));
2591 for (i = 1; i+1 < NCH(n); i++) {
2592 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2593 com_error(c, PyExc_SyntaxError,
2594 "can't assign to operator");
2595 return;
2597 com_apply_trailer(c, CHILD(n, i));
2598 } /* NB i is still alive */
2599 com_assign_trailer(c,
2600 CHILD(n, i), assigning, augn);
2601 return;
2603 n = CHILD(n, 0);
2604 break;
2606 case atom:
2607 switch (TYPE(CHILD(n, 0))) {
2608 case LPAR:
2609 n = CHILD(n, 1);
2610 if (TYPE(n) == RPAR) {
2611 /* XXX Should allow () = () ??? */
2612 com_error(c, PyExc_SyntaxError,
2613 "can't assign to ()");
2614 return;
2616 if (assigning > OP_APPLY) {
2617 com_error(c, PyExc_SyntaxError,
2618 "augmented assign to tuple not possible");
2619 return;
2621 break;
2622 case LSQB:
2623 n = CHILD(n, 1);
2624 if (TYPE(n) == RSQB) {
2625 com_error(c, PyExc_SyntaxError,
2626 "can't assign to []");
2627 return;
2629 if (assigning > OP_APPLY) {
2630 com_error(c, PyExc_SyntaxError,
2631 "augmented assign to list not possible");
2632 return;
2634 if (NCH(n) > 1
2635 && TYPE(CHILD(n, 1)) == list_for) {
2636 com_error(c, PyExc_SyntaxError,
2637 "can't assign to list comprehension");
2638 return;
2640 com_assign_sequence(c, n, assigning);
2641 return;
2642 case NAME:
2643 if (assigning > OP_APPLY)
2644 com_augassign_name(c, CHILD(n, 0),
2645 assigning, augn);
2646 else
2647 com_assign_name(c, CHILD(n, 0),
2648 assigning);
2649 return;
2650 default:
2651 com_error(c, PyExc_SyntaxError,
2652 "can't assign to literal");
2653 return;
2655 break;
2657 case lambdef:
2658 com_error(c, PyExc_SyntaxError,
2659 "can't assign to lambda");
2660 return;
2662 default:
2663 com_error(c, PyExc_SystemError,
2664 "com_assign: bad node");
2665 return;
2671 static void
2672 com_augassign(struct compiling *c, node *n)
2674 int opcode;
2676 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2677 case '+': opcode = INPLACE_ADD; break;
2678 case '-': opcode = INPLACE_SUBTRACT; break;
2679 case '/':
2680 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2681 opcode = INPLACE_FLOOR_DIVIDE;
2682 else if (c->c_flags & CO_FUTURE_DIVISION)
2683 opcode = INPLACE_TRUE_DIVIDE;
2684 else
2685 opcode = INPLACE_DIVIDE;
2686 break;
2687 case '%': opcode = INPLACE_MODULO; break;
2688 case '<': opcode = INPLACE_LSHIFT; break;
2689 case '>': opcode = INPLACE_RSHIFT; break;
2690 case '&': opcode = INPLACE_AND; break;
2691 case '^': opcode = INPLACE_XOR; break;
2692 case '|': opcode = INPLACE_OR; break;
2693 case '*':
2694 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2695 opcode = INPLACE_POWER;
2696 else
2697 opcode = INPLACE_MULTIPLY;
2698 break;
2699 default:
2700 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2701 return;
2703 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2706 static void
2707 com_expr_stmt(struct compiling *c, node *n)
2709 REQ(n, expr_stmt);
2710 /* testlist (('=' testlist)* | augassign testlist) */
2711 /* Forget it if we have just a doc string here */
2712 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2713 return;
2714 if (NCH(n) == 1) {
2715 com_node(c, CHILD(n, NCH(n)-1));
2716 if (c->c_interactive)
2717 com_addbyte(c, PRINT_EXPR);
2718 else
2719 com_addbyte(c, POP_TOP);
2720 com_pop(c, 1);
2722 else if (TYPE(CHILD(n,1)) == augassign)
2723 com_augassign(c, n);
2724 else {
2725 int i;
2726 com_node(c, CHILD(n, NCH(n)-1));
2727 for (i = 0; i < NCH(n)-2; i+=2) {
2728 if (i+2 < NCH(n)-2) {
2729 com_addbyte(c, DUP_TOP);
2730 com_push(c, 1);
2732 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2737 static void
2738 com_assert_stmt(struct compiling *c, node *n)
2740 int a = 0;
2741 int i;
2742 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2743 if (Py_OptimizeFlag)
2744 return;
2745 /* Generate code like
2747 if not <test>:
2748 raise AssertionError [, <message>]
2750 where <message> is the second test, if present.
2752 com_node(c, CHILD(n, 1));
2753 com_addfwref(c, JUMP_IF_TRUE, &a);
2754 com_addbyte(c, POP_TOP);
2755 com_pop(c, 1);
2756 /* Raise that exception! */
2757 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2758 com_push(c, 1);
2759 i = NCH(n)/2; /* Either 2 or 4 */
2760 if (i > 1)
2761 com_node(c, CHILD(n, 3));
2762 com_addoparg(c, RAISE_VARARGS, i);
2763 com_pop(c, i);
2764 /* The interpreter does not fall through */
2765 /* Jump ends up here */
2766 com_backpatch(c, a);
2767 com_addbyte(c, POP_TOP);
2770 static void
2771 com_print_stmt(struct compiling *c, node *n)
2773 int i = 1;
2774 node* stream = NULL;
2776 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2778 /* are we using the extended print form? */
2779 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2780 stream = CHILD(n, 2);
2781 com_node(c, stream);
2782 /* stack: [...] => [... stream] */
2783 com_push(c, 1);
2784 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2785 i = 4;
2786 else
2787 i = 3;
2789 for (; i < NCH(n); i += 2) {
2790 if (stream != NULL) {
2791 com_addbyte(c, DUP_TOP);
2792 /* stack: [stream] => [stream stream] */
2793 com_push(c, 1);
2794 com_node(c, CHILD(n, i));
2795 /* stack: [stream stream] => [stream stream obj] */
2796 com_addbyte(c, ROT_TWO);
2797 /* stack: [stream stream obj] => [stream obj stream] */
2798 com_addbyte(c, PRINT_ITEM_TO);
2799 /* stack: [stream obj stream] => [stream] */
2800 com_pop(c, 2);
2802 else {
2803 com_node(c, CHILD(n, i));
2804 /* stack: [...] => [... obj] */
2805 com_addbyte(c, PRINT_ITEM);
2806 com_pop(c, 1);
2809 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2810 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2811 if (stream != NULL) {
2812 /* must pop the extra stream object off the stack */
2813 com_addbyte(c, POP_TOP);
2814 /* stack: [... stream] => [...] */
2815 com_pop(c, 1);
2818 else {
2819 if (stream != NULL) {
2820 /* this consumes the last stream object on stack */
2821 com_addbyte(c, PRINT_NEWLINE_TO);
2822 /* stack: [... stream] => [...] */
2823 com_pop(c, 1);
2825 else
2826 com_addbyte(c, PRINT_NEWLINE);
2830 static void
2831 com_return_stmt(struct compiling *c, node *n)
2833 REQ(n, return_stmt); /* 'return' [testlist] */
2834 if (!c->c_infunction) {
2835 com_error(c, PyExc_SyntaxError, "'return' outside function");
2837 if (c->c_flags & CO_GENERATOR) {
2838 if (NCH(n) > 1) {
2839 com_error(c, PyExc_SyntaxError,
2840 "'return' with argument inside generator");
2843 if (NCH(n) < 2) {
2844 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2845 com_push(c, 1);
2847 else
2848 com_node(c, CHILD(n, 1));
2849 com_addbyte(c, RETURN_VALUE);
2850 com_pop(c, 1);
2853 static void
2854 com_yield_stmt(struct compiling *c, node *n)
2856 int i;
2857 REQ(n, yield_stmt); /* 'yield' testlist */
2858 if (!c->c_infunction) {
2859 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2862 for (i = 0; i < c->c_nblocks; ++i) {
2863 if (c->c_block[i] == SETUP_FINALLY) {
2864 com_error(c, PyExc_SyntaxError,
2865 "'yield' not allowed in a 'try' block "
2866 "with a 'finally' clause");
2867 return;
2870 com_node(c, CHILD(n, 1));
2871 com_addbyte(c, YIELD_VALUE);
2872 com_pop(c, 1);
2875 static void
2876 com_raise_stmt(struct compiling *c, node *n)
2878 int i;
2879 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2880 if (NCH(n) > 1) {
2881 com_node(c, CHILD(n, 1));
2882 if (NCH(n) > 3) {
2883 com_node(c, CHILD(n, 3));
2884 if (NCH(n) > 5)
2885 com_node(c, CHILD(n, 5));
2888 i = NCH(n)/2;
2889 com_addoparg(c, RAISE_VARARGS, i);
2890 com_pop(c, i);
2893 static void
2894 com_from_import(struct compiling *c, node *n)
2896 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2897 com_push(c, 1);
2898 if (NCH(n) > 1) {
2899 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2900 com_error(c, PyExc_SyntaxError, "invalid syntax");
2901 return;
2903 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2904 } else
2905 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2906 com_pop(c, 1);
2909 static void
2910 com_import_stmt(struct compiling *c, node *n)
2912 int i;
2913 REQ(n, import_stmt);
2914 /* 'import' dotted_name (',' dotted_name)* |
2915 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2916 if (STR(CHILD(n, 0))[0] == 'f') {
2917 PyObject *tup;
2918 /* 'from' dotted_name 'import' ... */
2919 REQ(CHILD(n, 1), dotted_name);
2921 if (TYPE(CHILD(n, 3)) == STAR) {
2922 tup = Py_BuildValue("(s)", "*");
2923 } else {
2924 tup = PyTuple_New((NCH(n) - 2)/2);
2925 for (i = 3; i < NCH(n); i += 2) {
2926 PyTuple_SET_ITEM(tup, (i-3)/2,
2927 PyString_FromString(STR(
2928 CHILD(CHILD(n, i), 0))));
2931 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2932 Py_DECREF(tup);
2933 com_push(c, 1);
2934 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2935 if (TYPE(CHILD(n, 3)) == STAR)
2936 com_addbyte(c, IMPORT_STAR);
2937 else {
2938 for (i = 3; i < NCH(n); i += 2)
2939 com_from_import(c, CHILD(n, i));
2940 com_addbyte(c, POP_TOP);
2942 com_pop(c, 1);
2944 else {
2945 /* 'import' ... */
2946 for (i = 1; i < NCH(n); i += 2) {
2947 node *subn = CHILD(n, i);
2948 REQ(subn, dotted_as_name);
2949 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2950 com_push(c, 1);
2951 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2952 if (NCH(subn) > 1) {
2953 int j;
2954 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
2955 com_error(c, PyExc_SyntaxError,
2956 "invalid syntax");
2957 return;
2959 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2960 com_addopname(c, LOAD_ATTR,
2961 CHILD(CHILD(subn, 0),
2962 j));
2963 com_addop_varname(c, VAR_STORE,
2964 STR(CHILD(subn, 2)));
2965 } else
2966 com_addop_varname(c, VAR_STORE,
2967 STR(CHILD(CHILD(subn, 0),
2968 0)));
2969 com_pop(c, 1);
2974 static void
2975 com_exec_stmt(struct compiling *c, node *n)
2977 REQ(n, exec_stmt);
2978 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2979 com_node(c, CHILD(n, 1));
2980 if (NCH(n) >= 4)
2981 com_node(c, CHILD(n, 3));
2982 else {
2983 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2984 com_push(c, 1);
2986 if (NCH(n) >= 6)
2987 com_node(c, CHILD(n, 5));
2988 else {
2989 com_addbyte(c, DUP_TOP);
2990 com_push(c, 1);
2992 com_addbyte(c, EXEC_STMT);
2993 com_pop(c, 3);
2996 static int
2997 is_constant_false(struct compiling *c, node *n)
2999 PyObject *v;
3000 int i;
3001 /* argument c will be NULL when called from symtable_node() */
3003 /* Label to avoid tail recursion */
3004 next:
3005 switch (TYPE(n)) {
3007 case suite:
3008 if (NCH(n) == 1) {
3009 n = CHILD(n, 0);
3010 goto next;
3012 /* Fall through */
3013 case file_input:
3014 for (i = 0; i < NCH(n); i++) {
3015 node *ch = CHILD(n, i);
3016 if (TYPE(ch) == stmt) {
3017 n = ch;
3018 goto next;
3021 break;
3023 case stmt:
3024 case simple_stmt:
3025 case small_stmt:
3026 n = CHILD(n, 0);
3027 goto next;
3029 case expr_stmt:
3030 case testlist:
3031 case testlist1:
3032 case test:
3033 case and_test:
3034 case not_test:
3035 case comparison:
3036 case expr:
3037 case xor_expr:
3038 case and_expr:
3039 case shift_expr:
3040 case arith_expr:
3041 case term:
3042 case factor:
3043 case power:
3044 case atom:
3045 if (NCH(n) == 1) {
3046 n = CHILD(n, 0);
3047 goto next;
3049 break;
3051 case NAME:
3052 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
3053 return 1;
3054 break;
3056 case NUMBER:
3057 v = parsenumber(c, STR(n));
3058 if (v == NULL) {
3059 PyErr_Clear();
3060 break;
3062 i = PyObject_IsTrue(v);
3063 Py_DECREF(v);
3064 return i == 0;
3066 case STRING:
3067 v = parsestr(c, STR(n));
3068 if (v == NULL) {
3069 PyErr_Clear();
3070 break;
3072 i = PyObject_IsTrue(v);
3073 Py_DECREF(v);
3074 return i == 0;
3077 return 0;
3081 /* Look under n for a return stmt with an expression.
3082 * This hack is used to find illegal returns under "if 0:" blocks in
3083 * functions already known to be generators (as determined by the symtable
3084 * pass).
3085 * Return the offending return node if found, else NULL.
3087 static node *
3088 look_for_offending_return(node *n)
3090 int i;
3092 for (i = 0; i < NCH(n); ++i) {
3093 node *kid = CHILD(n, i);
3095 switch (TYPE(kid)) {
3096 case classdef:
3097 case funcdef:
3098 case lambdef:
3099 /* Stuff in nested functions & classes doesn't
3100 affect the code block we started in. */
3101 return NULL;
3103 case return_stmt:
3104 if (NCH(kid) > 1)
3105 return kid;
3106 break;
3108 default: {
3109 node *bad = look_for_offending_return(kid);
3110 if (bad != NULL)
3111 return bad;
3116 return NULL;
3119 static void
3120 com_if_stmt(struct compiling *c, node *n)
3122 int i;
3123 int anchor = 0;
3124 REQ(n, if_stmt);
3125 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3126 for (i = 0; i+3 < NCH(n); i+=4) {
3127 int a = 0;
3128 node *ch = CHILD(n, i+1);
3129 if (is_constant_false(c, ch)) {
3130 /* We're going to skip this block. However, if this
3131 is a generator, we have to check the dead code
3132 anyway to make sure there aren't any return stmts
3133 with expressions, in the same scope. */
3134 if (c->c_flags & CO_GENERATOR) {
3135 node *p = look_for_offending_return(n);
3136 if (p != NULL) {
3137 int savelineno = c->c_lineno;
3138 c->c_lineno = p->n_lineno;
3139 com_error(c, PyExc_SyntaxError,
3140 "'return' with argument "
3141 "inside generator");
3142 c->c_lineno = savelineno;
3145 continue;
3147 if (i > 0)
3148 com_addoparg(c, SET_LINENO, ch->n_lineno);
3149 com_node(c, ch);
3150 com_addfwref(c, JUMP_IF_FALSE, &a);
3151 com_addbyte(c, POP_TOP);
3152 com_pop(c, 1);
3153 com_node(c, CHILD(n, i+3));
3154 com_addfwref(c, JUMP_FORWARD, &anchor);
3155 com_backpatch(c, a);
3156 /* We jump here with an extra entry which we now pop */
3157 com_addbyte(c, POP_TOP);
3159 if (i+2 < NCH(n))
3160 com_node(c, CHILD(n, i+2));
3161 if (anchor)
3162 com_backpatch(c, anchor);
3165 static void
3166 com_while_stmt(struct compiling *c, node *n)
3168 int break_anchor = 0;
3169 int anchor = 0;
3170 int save_begin = c->c_begin;
3171 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3172 com_addfwref(c, SETUP_LOOP, &break_anchor);
3173 block_push(c, SETUP_LOOP);
3174 c->c_begin = c->c_nexti;
3175 com_addoparg(c, SET_LINENO, n->n_lineno);
3176 com_node(c, CHILD(n, 1));
3177 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3178 com_addbyte(c, POP_TOP);
3179 com_pop(c, 1);
3180 c->c_loops++;
3181 com_node(c, CHILD(n, 3));
3182 c->c_loops--;
3183 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3184 c->c_begin = save_begin;
3185 com_backpatch(c, anchor);
3186 /* We jump here with one entry more on the stack */
3187 com_addbyte(c, POP_TOP);
3188 com_addbyte(c, POP_BLOCK);
3189 block_pop(c, SETUP_LOOP);
3190 if (NCH(n) > 4)
3191 com_node(c, CHILD(n, 6));
3192 com_backpatch(c, break_anchor);
3195 static void
3196 com_for_stmt(struct compiling *c, node *n)
3198 int break_anchor = 0;
3199 int anchor = 0;
3200 int save_begin = c->c_begin;
3201 REQ(n, for_stmt);
3202 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3203 com_addfwref(c, SETUP_LOOP, &break_anchor);
3204 block_push(c, SETUP_LOOP);
3205 com_node(c, CHILD(n, 3));
3206 com_addbyte(c, GET_ITER);
3207 c->c_begin = c->c_nexti;
3208 com_addoparg(c, SET_LINENO, n->n_lineno);
3209 com_addfwref(c, FOR_ITER, &anchor);
3210 com_push(c, 1);
3211 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
3212 c->c_loops++;
3213 com_node(c, CHILD(n, 5));
3214 c->c_loops--;
3215 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3216 c->c_begin = save_begin;
3217 com_backpatch(c, anchor);
3218 com_pop(c, 1); /* FOR_ITER has popped this */
3219 com_addbyte(c, POP_BLOCK);
3220 block_pop(c, SETUP_LOOP);
3221 if (NCH(n) > 8)
3222 com_node(c, CHILD(n, 8));
3223 com_backpatch(c, break_anchor);
3226 /* Code generated for "try: S finally: Sf" is as follows:
3228 SETUP_FINALLY L
3229 <code for S>
3230 POP_BLOCK
3231 LOAD_CONST <nil>
3232 L: <code for Sf>
3233 END_FINALLY
3235 The special instructions use the block stack. Each block
3236 stack entry contains the instruction that created it (here
3237 SETUP_FINALLY), the level of the value stack at the time the
3238 block stack entry was created, and a label (here L).
3240 SETUP_FINALLY:
3241 Pushes the current value stack level and the label
3242 onto the block stack.
3243 POP_BLOCK:
3244 Pops en entry from the block stack, and pops the value
3245 stack until its level is the same as indicated on the
3246 block stack. (The label is ignored.)
3247 END_FINALLY:
3248 Pops a variable number of entries from the *value* stack
3249 and re-raises the exception they specify. The number of
3250 entries popped depends on the (pseudo) exception type.
3252 The block stack is unwound when an exception is raised:
3253 when a SETUP_FINALLY entry is found, the exception is pushed
3254 onto the value stack (and the exception condition is cleared),
3255 and the interpreter jumps to the label gotten from the block
3256 stack.
3258 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3259 (The contents of the value stack is shown in [], with the top
3260 at the right; 'tb' is trace-back info, 'val' the exception's
3261 associated value, and 'exc' the exception.)
3263 Value stack Label Instruction Argument
3264 [] SETUP_EXCEPT L1
3265 [] <code for S>
3266 [] POP_BLOCK
3267 [] JUMP_FORWARD L0
3269 [tb, val, exc] L1: DUP )
3270 [tb, val, exc, exc] <evaluate E1> )
3271 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3272 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3273 [tb, val, exc, 1] POP )
3274 [tb, val, exc] POP
3275 [tb, val] <assign to V1> (or POP if no V1)
3276 [tb] POP
3277 [] <code for S1>
3278 JUMP_FORWARD L0
3280 [tb, val, exc, 0] L2: POP
3281 [tb, val, exc] DUP
3282 .............................etc.......................
3284 [tb, val, exc, 0] Ln+1: POP
3285 [tb, val, exc] END_FINALLY # re-raise exception
3287 [] L0: <next statement>
3289 Of course, parts are not generated if Vi or Ei is not present.
3292 static void
3293 com_try_except(struct compiling *c, node *n)
3295 int except_anchor = 0;
3296 int end_anchor = 0;
3297 int else_anchor = 0;
3298 int i;
3299 node *ch;
3301 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3302 block_push(c, SETUP_EXCEPT);
3303 com_node(c, CHILD(n, 2));
3304 com_addbyte(c, POP_BLOCK);
3305 block_pop(c, SETUP_EXCEPT);
3306 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3307 com_backpatch(c, except_anchor);
3308 for (i = 3;
3309 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3310 i += 3) {
3311 /* except_clause: 'except' [expr [',' var]] */
3312 if (except_anchor == 0) {
3313 com_error(c, PyExc_SyntaxError,
3314 "default 'except:' must be last");
3315 break;
3317 except_anchor = 0;
3318 com_push(c, 3); /* tb, val, exc pushed by exception */
3319 com_addoparg(c, SET_LINENO, ch->n_lineno);
3320 if (NCH(ch) > 1) {
3321 com_addbyte(c, DUP_TOP);
3322 com_push(c, 1);
3323 com_node(c, CHILD(ch, 1));
3324 com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
3325 com_pop(c, 1);
3326 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3327 com_addbyte(c, POP_TOP);
3328 com_pop(c, 1);
3330 com_addbyte(c, POP_TOP);
3331 com_pop(c, 1);
3332 if (NCH(ch) > 3)
3333 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3334 else {
3335 com_addbyte(c, POP_TOP);
3336 com_pop(c, 1);
3338 com_addbyte(c, POP_TOP);
3339 com_pop(c, 1);
3340 com_node(c, CHILD(n, i+2));
3341 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3342 if (except_anchor) {
3343 com_backpatch(c, except_anchor);
3344 /* We come in with [tb, val, exc, 0] on the
3345 stack; one pop and it's the same as
3346 expected at the start of the loop */
3347 com_addbyte(c, POP_TOP);
3350 /* We actually come in here with [tb, val, exc] but the
3351 END_FINALLY will zap those and jump around.
3352 The c_stacklevel does not reflect them so we need not pop
3353 anything. */
3354 com_addbyte(c, END_FINALLY);
3355 com_backpatch(c, else_anchor);
3356 if (i < NCH(n))
3357 com_node(c, CHILD(n, i+2));
3358 com_backpatch(c, end_anchor);
3361 static void
3362 com_try_finally(struct compiling *c, node *n)
3364 int finally_anchor = 0;
3365 node *ch;
3367 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3368 block_push(c, SETUP_FINALLY);
3369 com_node(c, CHILD(n, 2));
3370 com_addbyte(c, POP_BLOCK);
3371 block_pop(c, SETUP_FINALLY);
3372 block_push(c, END_FINALLY);
3373 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3374 /* While the generated code pushes only one item,
3375 the try-finally handling can enter here with
3376 up to three items. OK, here are the details:
3377 3 for an exception, 2 for RETURN, 1 for BREAK. */
3378 com_push(c, 3);
3379 com_backpatch(c, finally_anchor);
3380 ch = CHILD(n, NCH(n)-1);
3381 com_addoparg(c, SET_LINENO, ch->n_lineno);
3382 com_node(c, ch);
3383 com_addbyte(c, END_FINALLY);
3384 block_pop(c, END_FINALLY);
3385 com_pop(c, 3); /* Matches the com_push above */
3388 static void
3389 com_try_stmt(struct compiling *c, node *n)
3391 REQ(n, try_stmt);
3392 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3393 | 'try' ':' suite 'finally' ':' suite */
3394 if (TYPE(CHILD(n, 3)) != except_clause)
3395 com_try_finally(c, n);
3396 else
3397 com_try_except(c, n);
3400 static node *
3401 get_rawdocstring(node *n)
3403 int i;
3405 /* Label to avoid tail recursion */
3406 next:
3407 switch (TYPE(n)) {
3409 case suite:
3410 if (NCH(n) == 1) {
3411 n = CHILD(n, 0);
3412 goto next;
3414 /* Fall through */
3415 case file_input:
3416 for (i = 0; i < NCH(n); i++) {
3417 node *ch = CHILD(n, i);
3418 if (TYPE(ch) == stmt) {
3419 n = ch;
3420 goto next;
3423 break;
3425 case stmt:
3426 case simple_stmt:
3427 case small_stmt:
3428 n = CHILD(n, 0);
3429 goto next;
3431 case expr_stmt:
3432 case testlist:
3433 case testlist1:
3434 case test:
3435 case and_test:
3436 case not_test:
3437 case comparison:
3438 case expr:
3439 case xor_expr:
3440 case and_expr:
3441 case shift_expr:
3442 case arith_expr:
3443 case term:
3444 case factor:
3445 case power:
3446 if (NCH(n) == 1) {
3447 n = CHILD(n, 0);
3448 goto next;
3450 break;
3452 case atom:
3453 if (TYPE(CHILD(n, 0)) == STRING)
3454 return n;
3455 break;
3458 return NULL;
3461 static PyObject *
3462 get_docstring(struct compiling *c, node *n)
3464 /* Don't generate doc-strings if run with -OO */
3465 if (Py_OptimizeFlag > 1)
3466 return NULL;
3467 n = get_rawdocstring(n);
3468 if (n == NULL)
3469 return NULL;
3470 return parsestrplus(c, n);
3473 static void
3474 com_suite(struct compiling *c, node *n)
3476 REQ(n, suite);
3477 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3478 if (NCH(n) == 1) {
3479 com_node(c, CHILD(n, 0));
3481 else {
3482 int i;
3483 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3484 node *ch = CHILD(n, i);
3485 if (TYPE(ch) == stmt)
3486 com_node(c, ch);
3491 /* ARGSUSED */
3492 static void
3493 com_continue_stmt(struct compiling *c, node *n)
3495 int i = c->c_nblocks;
3496 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3497 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3499 else if (i <= 0) {
3500 /* at the outer level */
3501 com_error(c, PyExc_SyntaxError,
3502 "'continue' not properly in loop");
3504 else {
3505 int j;
3506 for (j = i-1; j >= 0; --j) {
3507 if (c->c_block[j] == SETUP_LOOP)
3508 break;
3510 if (j >= 0) {
3511 /* there is a loop, but something interferes */
3512 for (; i > j; --i) {
3513 if (c->c_block[i] == SETUP_EXCEPT ||
3514 c->c_block[i] == SETUP_FINALLY) {
3515 com_addoparg(c, CONTINUE_LOOP,
3516 c->c_begin);
3517 return;
3519 if (c->c_block[i] == END_FINALLY) {
3520 com_error(c, PyExc_SyntaxError,
3521 "'continue' not supported inside 'finally' clause");
3522 return;
3526 com_error(c, PyExc_SyntaxError,
3527 "'continue' not properly in loop");
3529 /* XXX Could allow it inside a 'finally' clause
3530 XXX if we could pop the exception still on the stack */
3533 static int
3534 com_argdefs(struct compiling *c, node *n)
3536 int i, nch, nargs, ndefs;
3537 if (TYPE(n) == lambdef) {
3538 /* lambdef: 'lambda' [varargslist] ':' test */
3539 n = CHILD(n, 1);
3541 else {
3542 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3543 n = CHILD(n, 2);
3544 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3545 n = CHILD(n, 1);
3547 if (TYPE(n) != varargslist)
3548 return 0;
3549 /* varargslist:
3550 (fpdef ['=' test] ',')* '*' ....... |
3551 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3552 nch = NCH(n);
3553 nargs = 0;
3554 ndefs = 0;
3555 for (i = 0; i < nch; i++) {
3556 int t;
3557 if (TYPE(CHILD(n, i)) == STAR ||
3558 TYPE(CHILD(n, i)) == DOUBLESTAR)
3559 break;
3560 nargs++;
3561 i++;
3562 if (i >= nch)
3563 t = RPAR; /* Anything except EQUAL or COMMA */
3564 else
3565 t = TYPE(CHILD(n, i));
3566 if (t == EQUAL) {
3567 i++;
3568 ndefs++;
3569 com_node(c, CHILD(n, i));
3570 i++;
3571 if (i >= nch)
3572 break;
3573 t = TYPE(CHILD(n, i));
3575 else {
3576 /* Treat "(a=1, b)" as an error */
3577 if (ndefs)
3578 com_error(c, PyExc_SyntaxError,
3579 "non-default argument follows default argument");
3581 if (t != COMMA)
3582 break;
3584 return ndefs;
3587 static void
3588 com_funcdef(struct compiling *c, node *n)
3590 PyObject *co;
3591 int ndefs;
3592 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3593 ndefs = com_argdefs(c, n);
3594 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3595 n->n_lineno);
3596 co = (PyObject *)icompile(n, c);
3597 symtable_exit_scope(c->c_symtable);
3598 if (co == NULL)
3599 c->c_errors++;
3600 else {
3601 int closure = com_make_closure(c, (PyCodeObject *)co);
3602 int i = com_addconst(c, co);
3603 com_addoparg(c, LOAD_CONST, i);
3604 com_push(c, 1);
3605 if (closure)
3606 com_addoparg(c, MAKE_CLOSURE, ndefs);
3607 else
3608 com_addoparg(c, MAKE_FUNCTION, ndefs);
3609 com_pop(c, ndefs);
3610 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3611 com_pop(c, 1);
3612 Py_DECREF(co);
3616 static void
3617 com_bases(struct compiling *c, node *n)
3619 int i;
3620 REQ(n, testlist);
3621 /* testlist: test (',' test)* [','] */
3622 for (i = 0; i < NCH(n); i += 2)
3623 com_node(c, CHILD(n, i));
3624 i = (NCH(n)+1) / 2;
3625 com_addoparg(c, BUILD_TUPLE, i);
3626 com_pop(c, i-1);
3629 static void
3630 com_classdef(struct compiling *c, node *n)
3632 int i;
3633 PyObject *v;
3634 PyCodeObject *co;
3635 char *name;
3637 REQ(n, classdef);
3638 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3639 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3640 c->c_errors++;
3641 return;
3643 /* Push the class name on the stack */
3644 i = com_addconst(c, v);
3645 com_addoparg(c, LOAD_CONST, i);
3646 com_push(c, 1);
3647 Py_DECREF(v);
3648 /* Push the tuple of base classes on the stack */
3649 if (TYPE(CHILD(n, 2)) != LPAR) {
3650 com_addoparg(c, BUILD_TUPLE, 0);
3651 com_push(c, 1);
3653 else
3654 com_bases(c, CHILD(n, 3));
3655 name = STR(CHILD(n, 1));
3656 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3657 co = icompile(n, c);
3658 symtable_exit_scope(c->c_symtable);
3659 if (co == NULL)
3660 c->c_errors++;
3661 else {
3662 int closure = com_make_closure(c, co);
3663 i = com_addconst(c, (PyObject *)co);
3664 com_addoparg(c, LOAD_CONST, i);
3665 com_push(c, 1);
3666 if (closure) {
3667 com_addoparg(c, MAKE_CLOSURE, 0);
3668 com_pop(c, PyCode_GetNumFree(co));
3669 } else
3670 com_addoparg(c, MAKE_FUNCTION, 0);
3671 com_addoparg(c, CALL_FUNCTION, 0);
3672 com_addbyte(c, BUILD_CLASS);
3673 com_pop(c, 2);
3674 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3675 com_pop(c, 1);
3676 Py_DECREF(co);
3680 static void
3681 com_node(struct compiling *c, node *n)
3683 loop:
3684 if (c->c_errors)
3685 return;
3686 switch (TYPE(n)) {
3688 /* Definition nodes */
3690 case funcdef:
3691 com_funcdef(c, n);
3692 break;
3693 case classdef:
3694 com_classdef(c, n);
3695 break;
3697 /* Trivial parse tree nodes */
3699 case stmt:
3700 case small_stmt:
3701 case flow_stmt:
3702 n = CHILD(n, 0);
3703 goto loop;
3705 case simple_stmt:
3706 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3707 com_addoparg(c, SET_LINENO, n->n_lineno);
3709 int i;
3710 for (i = 0; i < NCH(n)-1; i += 2)
3711 com_node(c, CHILD(n, i));
3713 break;
3715 case compound_stmt:
3716 com_addoparg(c, SET_LINENO, n->n_lineno);
3717 n = CHILD(n, 0);
3718 goto loop;
3720 /* Statement nodes */
3722 case expr_stmt:
3723 com_expr_stmt(c, n);
3724 break;
3725 case print_stmt:
3726 com_print_stmt(c, n);
3727 break;
3728 case del_stmt: /* 'del' exprlist */
3729 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3730 break;
3731 case pass_stmt:
3732 break;
3733 case break_stmt:
3734 if (c->c_loops == 0) {
3735 com_error(c, PyExc_SyntaxError,
3736 "'break' outside loop");
3738 com_addbyte(c, BREAK_LOOP);
3739 break;
3740 case continue_stmt:
3741 com_continue_stmt(c, n);
3742 break;
3743 case return_stmt:
3744 com_return_stmt(c, n);
3745 break;
3746 case yield_stmt:
3747 com_yield_stmt(c, n);
3748 break;
3749 case raise_stmt:
3750 com_raise_stmt(c, n);
3751 break;
3752 case import_stmt:
3753 com_import_stmt(c, n);
3754 break;
3755 case global_stmt:
3756 break;
3757 case exec_stmt:
3758 com_exec_stmt(c, n);
3759 break;
3760 case assert_stmt:
3761 com_assert_stmt(c, n);
3762 break;
3763 case if_stmt:
3764 com_if_stmt(c, n);
3765 break;
3766 case while_stmt:
3767 com_while_stmt(c, n);
3768 break;
3769 case for_stmt:
3770 com_for_stmt(c, n);
3771 break;
3772 case try_stmt:
3773 com_try_stmt(c, n);
3774 break;
3775 case suite:
3776 com_suite(c, n);
3777 break;
3779 /* Expression nodes */
3781 case testlist:
3782 case testlist1:
3783 case testlist_safe:
3784 com_list(c, n, 0);
3785 break;
3786 case test:
3787 com_test(c, n);
3788 break;
3789 case and_test:
3790 com_and_test(c, n);
3791 break;
3792 case not_test:
3793 com_not_test(c, n);
3794 break;
3795 case comparison:
3796 com_comparison(c, n);
3797 break;
3798 case exprlist:
3799 com_list(c, n, 0);
3800 break;
3801 case expr:
3802 com_expr(c, n);
3803 break;
3804 case xor_expr:
3805 com_xor_expr(c, n);
3806 break;
3807 case and_expr:
3808 com_and_expr(c, n);
3809 break;
3810 case shift_expr:
3811 com_shift_expr(c, n);
3812 break;
3813 case arith_expr:
3814 com_arith_expr(c, n);
3815 break;
3816 case term:
3817 com_term(c, n);
3818 break;
3819 case factor:
3820 com_factor(c, n);
3821 break;
3822 case power:
3823 com_power(c, n);
3824 break;
3825 case atom:
3826 com_atom(c, n);
3827 break;
3829 default:
3830 com_error(c, PyExc_SystemError,
3831 "com_node: unexpected node type");
3835 static void com_fplist(struct compiling *, node *);
3837 static void
3838 com_fpdef(struct compiling *c, node *n)
3840 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3841 if (TYPE(CHILD(n, 0)) == LPAR)
3842 com_fplist(c, CHILD(n, 1));
3843 else {
3844 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3845 com_pop(c, 1);
3849 static void
3850 com_fplist(struct compiling *c, node *n)
3852 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3853 if (NCH(n) == 1) {
3854 com_fpdef(c, CHILD(n, 0));
3856 else {
3857 int i = (NCH(n)+1)/2;
3858 com_addoparg(c, UNPACK_SEQUENCE, i);
3859 com_push(c, i-1);
3860 for (i = 0; i < NCH(n); i += 2)
3861 com_fpdef(c, CHILD(n, i));
3865 static void
3866 com_arglist(struct compiling *c, node *n)
3868 int nch, i, narg;
3869 int complex = 0;
3870 char nbuf[30];
3871 REQ(n, varargslist);
3872 /* varargslist:
3873 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3874 nch = NCH(n);
3875 /* Enter all arguments in table of locals */
3876 for (i = 0, narg = 0; i < nch; i++) {
3877 node *ch = CHILD(n, i);
3878 node *fp;
3879 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3880 break;
3881 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3882 fp = CHILD(ch, 0);
3883 if (TYPE(fp) != NAME) {
3884 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
3885 complex = 1;
3887 narg++;
3888 /* all name updates handled by symtable */
3889 if (++i >= nch)
3890 break;
3891 ch = CHILD(n, i);
3892 if (TYPE(ch) == EQUAL)
3893 i += 2;
3894 else
3895 REQ(ch, COMMA);
3897 if (complex) {
3898 /* Generate code for complex arguments only after
3899 having counted the simple arguments */
3900 int ilocal = 0;
3901 for (i = 0; i < nch; i++) {
3902 node *ch = CHILD(n, i);
3903 node *fp;
3904 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3905 break;
3906 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3907 fp = CHILD(ch, 0);
3908 if (TYPE(fp) != NAME) {
3909 com_addoparg(c, LOAD_FAST, ilocal);
3910 com_push(c, 1);
3911 com_fpdef(c, ch);
3913 ilocal++;
3914 if (++i >= nch)
3915 break;
3916 ch = CHILD(n, i);
3917 if (TYPE(ch) == EQUAL)
3918 i += 2;
3919 else
3920 REQ(ch, COMMA);
3925 static void
3926 com_file_input(struct compiling *c, node *n)
3928 int i;
3929 PyObject *doc;
3930 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3931 doc = get_docstring(c, n);
3932 if (doc != NULL) {
3933 int i = com_addconst(c, doc);
3934 Py_DECREF(doc);
3935 com_addoparg(c, LOAD_CONST, i);
3936 com_push(c, 1);
3937 com_addop_name(c, STORE_NAME, "__doc__");
3938 com_pop(c, 1);
3940 for (i = 0; i < NCH(n); i++) {
3941 node *ch = CHILD(n, i);
3942 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3943 com_node(c, ch);
3947 /* Top-level compile-node interface */
3949 static void
3950 compile_funcdef(struct compiling *c, node *n)
3952 PyObject *doc;
3953 node *ch;
3954 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3955 c->c_name = STR(CHILD(n, 1));
3956 doc = get_docstring(c, CHILD(n, 4));
3957 if (doc != NULL) {
3958 (void) com_addconst(c, doc);
3959 Py_DECREF(doc);
3961 else
3962 (void) com_addconst(c, Py_None); /* No docstring */
3963 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3964 ch = CHILD(ch, 1); /* ')' | varargslist */
3965 if (TYPE(ch) == varargslist)
3966 com_arglist(c, ch);
3967 c->c_infunction = 1;
3968 com_node(c, CHILD(n, 4));
3969 c->c_infunction = 0;
3970 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3971 com_push(c, 1);
3972 com_addbyte(c, RETURN_VALUE);
3973 com_pop(c, 1);
3976 static void
3977 compile_lambdef(struct compiling *c, node *n)
3979 node *ch;
3980 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3981 c->c_name = "<lambda>";
3983 ch = CHILD(n, 1);
3984 (void) com_addconst(c, Py_None); /* No docstring */
3985 if (TYPE(ch) == varargslist) {
3986 com_arglist(c, ch);
3987 ch = CHILD(n, 3);
3989 else
3990 ch = CHILD(n, 2);
3991 com_node(c, ch);
3992 com_addbyte(c, RETURN_VALUE);
3993 com_pop(c, 1);
3996 static void
3997 compile_classdef(struct compiling *c, node *n)
3999 node *ch;
4000 PyObject *doc;
4001 REQ(n, classdef);
4002 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4003 c->c_name = STR(CHILD(n, 1));
4004 c->c_private = c->c_name;
4005 /* Initialize local __module__ from global __name__ */
4006 com_addop_name(c, LOAD_GLOBAL, "__name__");
4007 com_addop_name(c, STORE_NAME, "__module__");
4008 ch = CHILD(n, NCH(n)-1); /* The suite */
4009 doc = get_docstring(c, ch);
4010 if (doc != NULL) {
4011 int i = com_addconst(c, doc);
4012 Py_DECREF(doc);
4013 com_addoparg(c, LOAD_CONST, i);
4014 com_push(c, 1);
4015 com_addop_name(c, STORE_NAME, "__doc__");
4016 com_pop(c, 1);
4018 else
4019 (void) com_addconst(c, Py_None);
4020 com_node(c, ch);
4021 com_addbyte(c, LOAD_LOCALS);
4022 com_push(c, 1);
4023 com_addbyte(c, RETURN_VALUE);
4024 com_pop(c, 1);
4027 static void
4028 compile_node(struct compiling *c, node *n)
4030 com_addoparg(c, SET_LINENO, n->n_lineno);
4032 switch (TYPE(n)) {
4034 case single_input: /* One interactive command */
4035 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
4036 c->c_interactive++;
4037 n = CHILD(n, 0);
4038 if (TYPE(n) != NEWLINE)
4039 com_node(c, n);
4040 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4041 com_push(c, 1);
4042 com_addbyte(c, RETURN_VALUE);
4043 com_pop(c, 1);
4044 c->c_interactive--;
4045 break;
4047 case file_input: /* A whole file, or built-in function exec() */
4048 com_file_input(c, n);
4049 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4050 com_push(c, 1);
4051 com_addbyte(c, RETURN_VALUE);
4052 com_pop(c, 1);
4053 break;
4055 case eval_input: /* Built-in function input() */
4056 com_node(c, CHILD(n, 0));
4057 com_addbyte(c, RETURN_VALUE);
4058 com_pop(c, 1);
4059 break;
4061 case lambdef: /* anonymous function definition */
4062 compile_lambdef(c, n);
4063 break;
4065 case funcdef: /* A function definition */
4066 compile_funcdef(c, n);
4067 break;
4069 case classdef: /* A class definition */
4070 compile_classdef(c, n);
4071 break;
4073 default:
4074 com_error(c, PyExc_SystemError,
4075 "compile_node: unexpected node type");
4079 static PyObject *
4080 dict_keys_inorder(PyObject *dict, int offset)
4082 PyObject *tuple, *k, *v;
4083 int i, pos = 0, size = PyDict_Size(dict);
4085 tuple = PyTuple_New(size);
4086 if (tuple == NULL)
4087 return NULL;
4088 while (PyDict_Next(dict, &pos, &k, &v)) {
4089 i = PyInt_AS_LONG(v);
4090 Py_INCREF(k);
4091 assert((i - offset) < size);
4092 PyTuple_SET_ITEM(tuple, i - offset, k);
4094 return tuple;
4097 PyCodeObject *
4098 PyNode_Compile(node *n, char *filename)
4100 return PyNode_CompileFlags(n, filename, NULL);
4103 PyCodeObject *
4104 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4106 return jcompile(n, filename, NULL, flags);
4109 struct symtable *
4110 PyNode_CompileSymtable(node *n, char *filename)
4112 struct symtable *st;
4113 PyFutureFeatures *ff;
4115 ff = PyNode_Future(n, filename);
4116 if (ff == NULL)
4117 return NULL;
4119 st = symtable_init();
4120 if (st == NULL) {
4121 PyMem_Free((void *)ff);
4122 return NULL;
4124 st->st_future = ff;
4125 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
4126 if (st->st_errors > 0)
4127 goto fail;
4128 symtable_node(st, n);
4129 if (st->st_errors > 0)
4130 goto fail;
4132 return st;
4133 fail:
4134 PyMem_Free((void *)ff);
4135 st->st_future = NULL;
4136 PySymtable_Free(st);
4137 return NULL;
4140 static PyCodeObject *
4141 icompile(node *n, struct compiling *base)
4143 return jcompile(n, base->c_filename, base, NULL);
4146 static PyCodeObject *
4147 jcompile(node *n, char *filename, struct compiling *base,
4148 PyCompilerFlags *flags)
4150 struct compiling sc;
4151 PyCodeObject *co;
4152 if (!com_init(&sc, filename))
4153 return NULL;
4154 if (base) {
4155 sc.c_private = base->c_private;
4156 sc.c_symtable = base->c_symtable;
4157 /* c_symtable still points to parent's symbols */
4158 if (base->c_nested
4159 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
4160 sc.c_nested = 1;
4161 sc.c_flags |= base->c_flags & PyCF_MASK;
4162 } else {
4163 sc.c_private = NULL;
4164 sc.c_future = PyNode_Future(n, filename);
4165 if (sc.c_future == NULL) {
4166 com_free(&sc);
4167 return NULL;
4169 if (flags) {
4170 int merged = sc.c_future->ff_features |
4171 flags->cf_flags;
4172 sc.c_future->ff_features = merged;
4173 flags->cf_flags = merged;
4175 if (symtable_build(&sc, n) < 0) {
4176 com_free(&sc);
4177 return NULL;
4180 co = NULL;
4181 if (symtable_load_symbols(&sc) < 0) {
4182 sc.c_errors++;
4183 goto exit;
4185 compile_node(&sc, n);
4186 com_done(&sc);
4187 if (sc.c_errors == 0) {
4188 PyObject *consts, *names, *varnames, *filename, *name,
4189 *freevars, *cellvars;
4190 consts = PyList_AsTuple(sc.c_consts);
4191 names = PyList_AsTuple(sc.c_names);
4192 varnames = PyList_AsTuple(sc.c_varnames);
4193 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4194 freevars = dict_keys_inorder(sc.c_freevars,
4195 PyTuple_GET_SIZE(cellvars));
4196 filename = PyString_InternFromString(sc.c_filename);
4197 name = PyString_InternFromString(sc.c_name);
4198 if (!PyErr_Occurred())
4199 co = PyCode_New(sc.c_argcount,
4200 sc.c_nlocals,
4201 sc.c_maxstacklevel,
4202 sc.c_flags,
4203 sc.c_code,
4204 consts,
4205 names,
4206 varnames,
4207 freevars,
4208 cellvars,
4209 filename,
4210 name,
4211 sc.c_firstlineno,
4212 sc.c_lnotab);
4213 Py_XDECREF(consts);
4214 Py_XDECREF(names);
4215 Py_XDECREF(varnames);
4216 Py_XDECREF(freevars);
4217 Py_XDECREF(cellvars);
4218 Py_XDECREF(filename);
4219 Py_XDECREF(name);
4221 else if (!PyErr_Occurred()) {
4222 /* This could happen if someone called PyErr_Clear() after an
4223 error was reported above. That's not supposed to happen,
4224 but I just plugged one case and I'm not sure there can't be
4225 others. In that case, raise SystemError so that at least
4226 it gets reported instead dumping core. */
4227 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4229 exit:
4230 if (base == NULL) {
4231 PySymtable_Free(sc.c_symtable);
4232 sc.c_symtable = NULL;
4234 com_free(&sc);
4235 return co;
4239 PyCode_Addr2Line(PyCodeObject *co, int addrq)
4241 int size = PyString_Size(co->co_lnotab) / 2;
4242 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
4243 int line = co->co_firstlineno;
4244 int addr = 0;
4245 while (--size >= 0) {
4246 addr += *p++;
4247 if (addr > addrq)
4248 break;
4249 line += *p++;
4251 return line;
4254 /* The test for LOCAL must come before the test for FREE in order to
4255 handle classes where name is both local and free. The local var is
4256 a method and the free var is a free var referenced within a method.
4259 static int
4260 get_ref_type(struct compiling *c, char *name)
4262 char buf[350];
4263 PyObject *v;
4265 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4266 return CELL;
4267 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4268 return LOCAL;
4269 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4270 return FREE;
4271 v = PyDict_GetItemString(c->c_globals, name);
4272 if (v) {
4273 if (v == Py_None)
4274 return GLOBAL_EXPLICIT;
4275 else {
4276 return GLOBAL_IMPLICIT;
4279 PyOS_snprintf(buf, sizeof(buf),
4280 "unknown scope for %.100s in %.100s(%s) "
4281 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4282 name, c->c_name,
4283 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4284 c->c_filename,
4285 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4286 PyObject_REPR(c->c_locals),
4287 PyObject_REPR(c->c_globals)
4290 Py_FatalError(buf);
4291 return -1;
4294 /* Helper functions to issue warnings */
4296 static int
4297 issue_warning(char *msg, char *filename, int lineno)
4299 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4300 lineno, NULL, NULL) < 0) {
4301 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4302 PyErr_SetString(PyExc_SyntaxError, msg);
4303 PyErr_SyntaxLocation(filename, lineno);
4305 return -1;
4307 return 0;
4310 static int
4311 symtable_warn(struct symtable *st, char *msg)
4313 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
4314 st->st_errors++;
4315 return -1;
4317 return 0;
4320 /* Helper function for setting lineno and filename */
4322 static int
4323 symtable_build(struct compiling *c, node *n)
4325 if ((c->c_symtable = symtable_init()) == NULL)
4326 return -1;
4327 c->c_symtable->st_future = c->c_future;
4328 c->c_symtable->st_filename = c->c_filename;
4329 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4330 if (c->c_symtable->st_errors > 0)
4331 return -1;
4332 symtable_node(c->c_symtable, n);
4333 if (c->c_symtable->st_errors > 0)
4334 return -1;
4335 /* reset for second pass */
4336 c->c_symtable->st_nscopes = 1;
4337 c->c_symtable->st_pass = 2;
4338 return 0;
4341 static int
4342 symtable_init_compiling_symbols(struct compiling *c)
4344 PyObject *varnames;
4346 varnames = c->c_symtable->st_cur->ste_varnames;
4347 if (varnames == NULL) {
4348 varnames = PyList_New(0);
4349 if (varnames == NULL)
4350 return -1;
4351 c->c_symtable->st_cur->ste_varnames = varnames;
4352 Py_INCREF(varnames);
4353 } else
4354 Py_INCREF(varnames);
4355 c->c_varnames = varnames;
4357 c->c_globals = PyDict_New();
4358 if (c->c_globals == NULL)
4359 return -1;
4360 c->c_freevars = PyDict_New();
4361 if (c->c_freevars == NULL)
4362 return -1;
4363 c->c_cellvars = PyDict_New();
4364 if (c->c_cellvars == NULL)
4365 return -1;
4366 return 0;
4369 struct symbol_info {
4370 int si_nlocals;
4371 int si_ncells;
4372 int si_nfrees;
4373 int si_nimplicit;
4376 static void
4377 symtable_init_info(struct symbol_info *si)
4379 si->si_nlocals = 0;
4380 si->si_ncells = 0;
4381 si->si_nfrees = 0;
4382 si->si_nimplicit = 0;
4385 static int
4386 symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
4387 struct symbol_info *si)
4389 PyObject *dict, *v;
4391 /* Seperate logic for DEF_FREE. If it occurs in a function,
4392 it indicates a local that we must allocate storage for (a
4393 cell var). If it occurs in a class, then the class has a
4394 method and a free variable with the same name.
4396 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4397 /* If it isn't declared locally, it can't be a cell. */
4398 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4399 return 0;
4400 v = PyInt_FromLong(si->si_ncells++);
4401 dict = c->c_cellvars;
4402 } else {
4403 /* If it is free anyway, then there is no need to do
4404 anything here.
4406 if (is_free(flags ^ DEF_FREE_CLASS)
4407 || (flags == DEF_FREE_CLASS))
4408 return 0;
4409 v = PyInt_FromLong(si->si_nfrees++);
4410 dict = c->c_freevars;
4412 if (v == NULL)
4413 return -1;
4414 if (PyDict_SetItem(dict, name, v) < 0) {
4415 Py_DECREF(v);
4416 return -1;
4418 Py_DECREF(v);
4419 return 0;
4422 /* If a variable is a cell and an argument, make sure that appears in
4423 co_cellvars before any variable to its right in varnames.
4427 static int
4428 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4429 PyObject *varnames, int flags)
4431 PyObject *v, *w, *d, *list = NULL;
4432 int i, pos;
4434 if (flags & CO_VARARGS)
4435 argcount++;
4436 if (flags & CO_VARKEYWORDS)
4437 argcount++;
4438 for (i = argcount; --i >= 0; ) {
4439 v = PyList_GET_ITEM(varnames, i);
4440 if (PyDict_GetItem(*cellvars, v)) {
4441 if (list == NULL) {
4442 list = PyList_New(1);
4443 if (list == NULL)
4444 return -1;
4445 PyList_SET_ITEM(list, 0, v);
4446 Py_INCREF(v);
4447 } else
4448 PyList_Insert(list, 0, v);
4451 if (list == NULL || PyList_GET_SIZE(list) == 0)
4452 return 0;
4453 /* There are cellvars that are also arguments. Create a dict
4454 to replace cellvars and put the args at the front.
4456 d = PyDict_New();
4457 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4458 v = PyInt_FromLong(i);
4459 if (v == NULL)
4460 goto fail;
4461 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4462 goto fail;
4463 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4464 goto fail;
4466 pos = 0;
4467 i = PyList_GET_SIZE(list);
4468 Py_DECREF(list);
4469 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4470 w = PyInt_FromLong(i++); /* don't care about the old key */
4471 if (PyDict_SetItem(d, v, w) < 0) {
4472 Py_DECREF(w);
4473 goto fail;
4475 Py_DECREF(w);
4477 Py_DECREF(*cellvars);
4478 *cellvars = d;
4479 return 1;
4480 fail:
4481 Py_DECREF(d);
4482 return -1;
4485 static int
4486 symtable_freevar_offsets(PyObject *freevars, int offset)
4488 PyObject *name, *v;
4489 int pos;
4491 /* The cell vars are the first elements of the closure,
4492 followed by the free vars. Update the offsets in
4493 c_freevars to account for number of cellvars. */
4494 pos = 0;
4495 while (PyDict_Next(freevars, &pos, &name, &v)) {
4496 int i = PyInt_AS_LONG(v) + offset;
4497 PyObject *o = PyInt_FromLong(i);
4498 if (o == NULL)
4499 return -1;
4500 if (PyDict_SetItem(freevars, name, o) < 0) {
4501 Py_DECREF(o);
4502 return -1;
4504 Py_DECREF(o);
4506 return 0;
4509 static int
4510 symtable_check_unoptimized(struct compiling *c,
4511 PySymtableEntryObject *ste,
4512 struct symbol_info *si)
4514 char buf[300];
4516 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4517 || (ste->ste_nested && si->si_nimplicit)))
4518 return 0;
4520 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4522 #define ILLEGAL_IS "is a nested function"
4524 #define ILLEGAL_IMPORT_STAR \
4525 "import * is not allowed in function '%.100s' because it %s"
4527 #define ILLEGAL_BARE_EXEC \
4528 "unqualified exec is not allowed in function '%.100s' it %s"
4530 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4531 "function '%.100s' uses import * and bare exec, which are illegal " \
4532 "because it %s"
4534 /* XXX perhaps the linenos for these opt-breaking statements
4535 should be stored so the exception can point to them. */
4537 if (ste->ste_child_free) {
4538 if (ste->ste_optimized == OPT_IMPORT_STAR)
4539 PyOS_snprintf(buf, sizeof(buf),
4540 ILLEGAL_IMPORT_STAR,
4541 PyString_AS_STRING(ste->ste_name),
4542 ILLEGAL_CONTAINS);
4543 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4544 PyOS_snprintf(buf, sizeof(buf),
4545 ILLEGAL_BARE_EXEC,
4546 PyString_AS_STRING(ste->ste_name),
4547 ILLEGAL_CONTAINS);
4548 else {
4549 PyOS_snprintf(buf, sizeof(buf),
4550 ILLEGAL_EXEC_AND_IMPORT_STAR,
4551 PyString_AS_STRING(ste->ste_name),
4552 ILLEGAL_CONTAINS);
4554 } else {
4555 if (ste->ste_optimized == OPT_IMPORT_STAR)
4556 PyOS_snprintf(buf, sizeof(buf),
4557 ILLEGAL_IMPORT_STAR,
4558 PyString_AS_STRING(ste->ste_name),
4559 ILLEGAL_IS);
4560 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4561 PyOS_snprintf(buf, sizeof(buf),
4562 ILLEGAL_BARE_EXEC,
4563 PyString_AS_STRING(ste->ste_name),
4564 ILLEGAL_IS);
4565 else {
4566 PyOS_snprintf(buf, sizeof(buf),
4567 ILLEGAL_EXEC_AND_IMPORT_STAR,
4568 PyString_AS_STRING(ste->ste_name),
4569 ILLEGAL_IS);
4573 PyErr_SetString(PyExc_SyntaxError, buf);
4574 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4575 ste->ste_opt_lineno);
4576 return -1;
4579 static int
4580 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4581 struct symbol_info *si)
4583 if (c->c_future)
4584 c->c_flags |= c->c_future->ff_features;
4585 if (ste->ste_generator)
4586 c->c_flags |= CO_GENERATOR;
4587 if (ste->ste_type != TYPE_MODULE)
4588 c->c_flags |= CO_NEWLOCALS;
4589 if (ste->ste_type == TYPE_FUNCTION) {
4590 c->c_nlocals = si->si_nlocals;
4591 if (ste->ste_optimized == 0)
4592 c->c_flags |= CO_OPTIMIZED;
4593 else if (ste->ste_optimized != OPT_EXEC)
4594 return symtable_check_unoptimized(c, ste, si);
4596 return 0;
4599 static int
4600 symtable_load_symbols(struct compiling *c)
4602 static PyObject *implicit = NULL;
4603 struct symtable *st = c->c_symtable;
4604 PySymtableEntryObject *ste = st->st_cur;
4605 PyObject *name, *varnames, *v;
4606 int i, flags, pos;
4607 struct symbol_info si;
4609 if (implicit == NULL) {
4610 implicit = PyInt_FromLong(1);
4611 if (implicit == NULL)
4612 return -1;
4614 v = NULL;
4616 if (symtable_init_compiling_symbols(c) < 0)
4617 goto fail;
4618 symtable_init_info(&si);
4619 varnames = st->st_cur->ste_varnames;
4620 si.si_nlocals = PyList_GET_SIZE(varnames);
4621 c->c_argcount = si.si_nlocals;
4623 for (i = 0; i < si.si_nlocals; ++i) {
4624 v = PyInt_FromLong(i);
4625 if (PyDict_SetItem(c->c_locals,
4626 PyList_GET_ITEM(varnames, i), v) < 0)
4627 goto fail;
4628 Py_DECREF(v);
4631 /* XXX The cases below define the rules for whether a name is
4632 local or global. The logic could probably be clearer. */
4633 pos = 0;
4634 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4635 flags = PyInt_AS_LONG(v);
4637 if (flags & DEF_FREE_GLOBAL)
4638 /* undo the original DEF_FREE */
4639 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4641 /* Deal with names that need two actions:
4642 1. Cell variables that are also locals.
4643 2. Free variables in methods that are also class
4644 variables or declared global.
4646 if (flags & (DEF_FREE | DEF_FREE_CLASS))
4647 symtable_resolve_free(c, name, flags, &si);
4649 if (flags & DEF_STAR) {
4650 c->c_argcount--;
4651 c->c_flags |= CO_VARARGS;
4652 } else if (flags & DEF_DOUBLESTAR) {
4653 c->c_argcount--;
4654 c->c_flags |= CO_VARKEYWORDS;
4655 } else if (flags & DEF_INTUPLE)
4656 c->c_argcount--;
4657 else if (flags & DEF_GLOBAL) {
4658 if (flags & DEF_PARAM) {
4659 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4660 PyString_AS_STRING(name));
4661 PyErr_SyntaxLocation(st->st_filename,
4662 ste->ste_lineno);
4663 st->st_errors++;
4664 goto fail;
4666 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4667 goto fail;
4668 } else if (flags & DEF_FREE_GLOBAL) {
4669 si.si_nimplicit++;
4670 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4671 goto fail;
4672 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4673 v = PyInt_FromLong(si.si_nlocals++);
4674 if (v == NULL)
4675 goto fail;
4676 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4677 goto fail;
4678 Py_DECREF(v);
4679 if (ste->ste_type != TYPE_CLASS)
4680 if (PyList_Append(c->c_varnames, name) < 0)
4681 goto fail;
4682 } else if (is_free(flags)) {
4683 if (ste->ste_nested) {
4684 v = PyInt_FromLong(si.si_nfrees++);
4685 if (v == NULL)
4686 goto fail;
4687 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4688 goto fail;
4689 Py_DECREF(v);
4690 } else {
4691 si.si_nimplicit++;
4692 if (PyDict_SetItem(c->c_globals, name,
4693 implicit) < 0)
4694 goto fail;
4695 if (st->st_nscopes != 1) {
4696 v = PyInt_FromLong(flags);
4697 if (PyDict_SetItem(st->st_global,
4698 name, v))
4699 goto fail;
4700 Py_DECREF(v);
4706 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4708 if (si.si_ncells > 1) { /* one cell is always in order */
4709 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4710 c->c_varnames, c->c_flags) < 0)
4711 return -1;
4713 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4714 return -1;
4715 return symtable_update_flags(c, ste, &si);
4716 fail:
4717 /* is this always the right thing to do? */
4718 Py_XDECREF(v);
4719 return -1;
4722 static struct symtable *
4723 symtable_init()
4725 struct symtable *st;
4727 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4728 if (st == NULL)
4729 return NULL;
4730 st->st_pass = 1;
4732 st->st_filename = NULL;
4733 if ((st->st_stack = PyList_New(0)) == NULL)
4734 goto fail;
4735 if ((st->st_symbols = PyDict_New()) == NULL)
4736 goto fail;
4737 st->st_cur = NULL;
4738 st->st_nscopes = 0;
4739 st->st_errors = 0;
4740 st->st_tmpname = 0;
4741 st->st_private = NULL;
4742 return st;
4743 fail:
4744 PySymtable_Free(st);
4745 return NULL;
4748 void
4749 PySymtable_Free(struct symtable *st)
4751 Py_XDECREF(st->st_symbols);
4752 Py_XDECREF(st->st_stack);
4753 Py_XDECREF(st->st_cur);
4754 PyMem_Free((void *)st);
4757 /* When the compiler exits a scope, it must should update the scope's
4758 free variable information with the list of free variables in its
4759 children.
4761 Variables that are free in children and defined in the current
4762 scope are cellvars.
4764 If the scope being exited is defined at the top-level (ste_nested is
4765 false), free variables in children that are not defined here are
4766 implicit globals.
4770 static int
4771 symtable_update_free_vars(struct symtable *st)
4773 int i, j, def;
4774 PyObject *o, *name, *list = NULL;
4775 PySymtableEntryObject *child, *ste = st->st_cur;
4777 if (ste->ste_type == TYPE_CLASS)
4778 def = DEF_FREE_CLASS;
4779 else
4780 def = DEF_FREE;
4781 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4782 int pos = 0;
4784 if (list)
4785 PyList_SetSlice(list, 0,
4786 ((PyVarObject*)list)->ob_size, 0);
4787 child = (PySymtableEntryObject *)
4788 PyList_GET_ITEM(ste->ste_children, i);
4789 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4790 int flags = PyInt_AS_LONG(o);
4791 if (!(is_free(flags)))
4792 continue; /* avoids indentation */
4793 if (list == NULL) {
4794 list = PyList_New(0);
4795 if (list == NULL)
4796 return -1;
4798 ste->ste_child_free = 1;
4799 if (PyList_Append(list, name) < 0) {
4800 Py_DECREF(list);
4801 return -1;
4804 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4805 PyObject *v;
4806 name = PyList_GET_ITEM(list, j);
4807 v = PyDict_GetItem(ste->ste_symbols, name);
4808 /* If a name N is declared global in scope A and
4809 referenced in scope B contained (perhaps
4810 indirectly) in A and there are no scopes
4811 with bindings for N between B and A, then N
4812 is global in B. Unless A is a class scope,
4813 because class scopes are not considered for
4814 nested scopes.
4816 if (v && (ste->ste_type != TYPE_CLASS)) {
4817 int flags = PyInt_AS_LONG(v);
4818 if (flags & DEF_GLOBAL) {
4819 symtable_undo_free(st, child->ste_id,
4820 name);
4821 continue;
4824 if (ste->ste_nested) {
4825 if (symtable_add_def_o(st, ste->ste_symbols,
4826 name, def) < 0) {
4827 Py_DECREF(list);
4828 return -1;
4830 } else {
4831 if (symtable_check_global(st, child->ste_id,
4832 name) < 0) {
4833 Py_DECREF(list);
4834 return -1;
4840 Py_XDECREF(list);
4841 return 0;
4844 /* If the current scope is a non-nested class or if name is not
4845 defined in the current, non-nested scope, then it is an implicit
4846 global in all nested scopes.
4849 static int
4850 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4852 PyObject *o;
4853 int v;
4854 PySymtableEntryObject *ste = st->st_cur;
4856 if (ste->ste_type == TYPE_CLASS)
4857 return symtable_undo_free(st, child, name);
4858 o = PyDict_GetItem(ste->ste_symbols, name);
4859 if (o == NULL)
4860 return symtable_undo_free(st, child, name);
4861 v = PyInt_AS_LONG(o);
4863 if (is_free(v) || (v & DEF_GLOBAL))
4864 return symtable_undo_free(st, child, name);
4865 else
4866 return symtable_add_def_o(st, ste->ste_symbols,
4867 name, DEF_FREE);
4870 static int
4871 symtable_undo_free(struct symtable *st, PyObject *id,
4872 PyObject *name)
4874 int i, v, x;
4875 PyObject *info;
4876 PySymtableEntryObject *ste;
4878 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4879 if (ste == NULL)
4880 return -1;
4882 info = PyDict_GetItem(ste->ste_symbols, name);
4883 if (info == NULL)
4884 return 0;
4885 v = PyInt_AS_LONG(info);
4886 if (is_free(v)) {
4887 if (symtable_add_def_o(st, ste->ste_symbols, name,
4888 DEF_FREE_GLOBAL) < 0)
4889 return -1;
4890 } else
4891 /* If the name is defined here or declared global,
4892 then the recursion stops. */
4893 return 0;
4895 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4896 PySymtableEntryObject *child;
4897 child = (PySymtableEntryObject *)
4898 PyList_GET_ITEM(ste->ste_children, i);
4899 x = symtable_undo_free(st, child->ste_id, name);
4900 if (x < 0)
4901 return x;
4903 return 0;
4906 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4907 This reference is released when the scope is exited, via the DECREF
4908 in symtable_exit_scope().
4911 static int
4912 symtable_exit_scope(struct symtable *st)
4914 int end;
4916 if (st->st_pass == 1)
4917 symtable_update_free_vars(st);
4918 Py_DECREF(st->st_cur);
4919 end = PyList_GET_SIZE(st->st_stack) - 1;
4920 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4921 end);
4922 if (PySequence_DelItem(st->st_stack, end) < 0)
4923 return -1;
4924 return 0;
4927 static void
4928 symtable_enter_scope(struct symtable *st, char *name, int type,
4929 int lineno)
4931 PySymtableEntryObject *prev = NULL;
4933 if (st->st_cur) {
4934 prev = st->st_cur;
4935 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4936 Py_DECREF(st->st_cur);
4937 st->st_errors++;
4938 return;
4941 st->st_cur = (PySymtableEntryObject *)
4942 PySymtableEntry_New(st, name, type, lineno);
4943 if (strcmp(name, TOP) == 0)
4944 st->st_global = st->st_cur->ste_symbols;
4945 if (prev && st->st_pass == 1) {
4946 if (PyList_Append(prev->ste_children,
4947 (PyObject *)st->st_cur) < 0)
4948 st->st_errors++;
4952 static int
4953 symtable_lookup(struct symtable *st, char *name)
4955 char buffer[MANGLE_LEN];
4956 PyObject *v;
4957 int flags;
4959 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
4960 name = buffer;
4961 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4962 if (v == NULL) {
4963 if (PyErr_Occurred())
4964 return -1;
4965 else
4966 return 0;
4969 flags = PyInt_AS_LONG(v);
4970 return flags;
4973 static int
4974 symtable_add_def(struct symtable *st, char *name, int flag)
4976 PyObject *s;
4977 char buffer[MANGLE_LEN];
4978 int ret;
4980 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
4981 name = buffer;
4982 if ((s = PyString_InternFromString(name)) == NULL)
4983 return -1;
4984 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4985 Py_DECREF(s);
4986 return ret;
4989 /* Must only be called with mangled names */
4991 static int
4992 symtable_add_def_o(struct symtable *st, PyObject *dict,
4993 PyObject *name, int flag)
4995 PyObject *o;
4996 int val;
4998 if ((o = PyDict_GetItem(dict, name))) {
4999 val = PyInt_AS_LONG(o);
5000 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
5001 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
5002 PyString_AsString(name));
5003 PyErr_SyntaxLocation(st->st_filename,
5004 st->st_cur->ste_lineno);
5005 return -1;
5007 val |= flag;
5008 } else
5009 val = flag;
5010 o = PyInt_FromLong(val);
5011 if (PyDict_SetItem(dict, name, o) < 0) {
5012 Py_DECREF(o);
5013 return -1;
5015 Py_DECREF(o);
5017 if (flag & DEF_PARAM) {
5018 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
5019 return -1;
5020 } else if (flag & DEF_GLOBAL) {
5021 /* XXX need to update DEF_GLOBAL for other flags too;
5022 perhaps only DEF_FREE_GLOBAL */
5023 if ((o = PyDict_GetItem(st->st_global, name))) {
5024 val = PyInt_AS_LONG(o);
5025 val |= flag;
5026 } else
5027 val = flag;
5028 o = PyInt_FromLong(val);
5029 if (PyDict_SetItem(st->st_global, name, o) < 0) {
5030 Py_DECREF(o);
5031 return -1;
5033 Py_DECREF(o);
5035 return 0;
5038 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
5040 /* Look for a yield stmt under n. Return 1 if found, else 0.
5041 This hack is used to look inside "if 0:" blocks (which are normally
5042 ignored) in case those are the only places a yield occurs (so that this
5043 function is a generator). */
5044 static int
5045 look_for_yield(node *n)
5047 int i;
5049 for (i = 0; i < NCH(n); ++i) {
5050 node *kid = CHILD(n, i);
5052 switch (TYPE(kid)) {
5054 case classdef:
5055 case funcdef:
5056 case lambdef:
5057 /* Stuff in nested functions and classes can't make
5058 the parent a generator. */
5059 return 0;
5061 case yield_stmt:
5062 return 1;
5064 default:
5065 if (look_for_yield(kid))
5066 return 1;
5069 return 0;
5072 static void
5073 symtable_node(struct symtable *st, node *n)
5075 int i;
5077 loop:
5078 switch (TYPE(n)) {
5079 case funcdef: {
5080 char *func_name = STR(CHILD(n, 1));
5081 symtable_add_def(st, func_name, DEF_LOCAL);
5082 symtable_default_args(st, CHILD(n, 2));
5083 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
5084 symtable_funcdef(st, n);
5085 symtable_exit_scope(st);
5086 break;
5088 case lambdef:
5089 if (NCH(n) == 4)
5090 symtable_default_args(st, CHILD(n, 1));
5091 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
5092 symtable_funcdef(st, n);
5093 symtable_exit_scope(st);
5094 break;
5095 case classdef: {
5096 char *tmp, *class_name = STR(CHILD(n, 1));
5097 symtable_add_def(st, class_name, DEF_LOCAL);
5098 if (TYPE(CHILD(n, 2)) == LPAR) {
5099 node *bases = CHILD(n, 3);
5100 int i;
5101 for (i = 0; i < NCH(bases); i += 2) {
5102 symtable_node(st, CHILD(bases, i));
5105 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
5106 tmp = st->st_private;
5107 st->st_private = class_name;
5108 symtable_node(st, CHILD(n, NCH(n) - 1));
5109 st->st_private = tmp;
5110 symtable_exit_scope(st);
5111 break;
5113 case if_stmt:
5114 for (i = 0; i + 3 < NCH(n); i += 4) {
5115 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5116 if (st->st_cur->ste_generator == 0)
5117 st->st_cur->ste_generator =
5118 look_for_yield(CHILD(n, i+3));
5119 continue;
5121 symtable_node(st, CHILD(n, i + 1));
5122 symtable_node(st, CHILD(n, i + 3));
5124 if (i + 2 < NCH(n))
5125 symtable_node(st, CHILD(n, i + 2));
5126 break;
5127 case global_stmt:
5128 symtable_global(st, n);
5129 break;
5130 case import_stmt:
5131 symtable_import(st, n);
5132 break;
5133 case exec_stmt: {
5134 st->st_cur->ste_optimized |= OPT_EXEC;
5135 symtable_node(st, CHILD(n, 1));
5136 if (NCH(n) > 2)
5137 symtable_node(st, CHILD(n, 3));
5138 else {
5139 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
5140 st->st_cur->ste_opt_lineno = n->n_lineno;
5142 if (NCH(n) > 4)
5143 symtable_node(st, CHILD(n, 5));
5144 break;
5147 case assert_stmt:
5148 if (Py_OptimizeFlag)
5149 return;
5150 if (NCH(n) == 2) {
5151 n = CHILD(n, 1);
5152 goto loop;
5153 } else {
5154 symtable_node(st, CHILD(n, 1));
5155 n = CHILD(n, 3);
5156 goto loop;
5158 case except_clause:
5159 if (NCH(n) == 4)
5160 symtable_assign(st, CHILD(n, 3), 0);
5161 if (NCH(n) > 1) {
5162 n = CHILD(n, 1);
5163 goto loop;
5165 break;
5166 case del_stmt:
5167 symtable_assign(st, CHILD(n, 1), 0);
5168 break;
5169 case yield_stmt:
5170 st->st_cur->ste_generator = 1;
5171 n = CHILD(n, 1);
5172 goto loop;
5173 case expr_stmt:
5174 if (NCH(n) == 1)
5175 n = CHILD(n, 0);
5176 else {
5177 if (TYPE(CHILD(n, 1)) == augassign) {
5178 symtable_assign(st, CHILD(n, 0), 0);
5179 symtable_node(st, CHILD(n, 2));
5180 break;
5181 } else {
5182 int i;
5183 for (i = 0; i < NCH(n) - 2; i += 2)
5184 symtable_assign(st, CHILD(n, i), 0);
5185 n = CHILD(n, NCH(n) - 1);
5188 goto loop;
5189 case list_iter:
5190 n = CHILD(n, 0);
5191 if (TYPE(n) == list_for) {
5192 st->st_tmpname++;
5193 symtable_list_comprehension(st, n);
5194 st->st_tmpname--;
5195 } else {
5196 REQ(n, list_if);
5197 symtable_node(st, CHILD(n, 1));
5198 if (NCH(n) == 3) {
5199 n = CHILD(n, 2);
5200 goto loop;
5203 break;
5204 case for_stmt:
5205 symtable_assign(st, CHILD(n, 1), 0);
5206 for (i = 3; i < NCH(n); ++i)
5207 if (TYPE(CHILD(n, i)) >= single_input)
5208 symtable_node(st, CHILD(n, i));
5209 break;
5210 /* The remaining cases fall through to default except in
5211 special circumstances. This requires the individual cases
5212 to be coded with great care, even though they look like
5213 rather innocuous. Each case must double-check TYPE(n).
5215 case argument:
5216 if (TYPE(n) == argument && NCH(n) == 3) {
5217 n = CHILD(n, 2);
5218 goto loop;
5220 /* fall through */
5221 case listmaker:
5222 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5223 st->st_tmpname++;
5224 symtable_list_comprehension(st, CHILD(n, 1));
5225 symtable_node(st, CHILD(n, 0));
5226 st->st_tmpname--;
5227 break;
5229 /* fall through */
5230 case atom:
5231 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5232 symtable_add_use(st, STR(CHILD(n, 0)));
5233 break;
5235 /* fall through */
5236 default:
5237 /* Walk over every non-token child with a special case
5238 for one child.
5240 if (NCH(n) == 1) {
5241 n = CHILD(n, 0);
5242 goto loop;
5244 for (i = 0; i < NCH(n); ++i)
5245 if (TYPE(CHILD(n, i)) >= single_input)
5246 symtable_node(st, CHILD(n, i));
5250 static void
5251 symtable_funcdef(struct symtable *st, node *n)
5253 node *body;
5255 if (TYPE(n) == lambdef) {
5256 if (NCH(n) == 4)
5257 symtable_params(st, CHILD(n, 1));
5258 } else
5259 symtable_params(st, CHILD(n, 2));
5260 body = CHILD(n, NCH(n) - 1);
5261 symtable_node(st, body);
5264 /* The next two functions parse the argument tuple.
5265 symtable_default_arg() checks for names in the default arguments,
5266 which are references in the defining scope. symtable_params()
5267 parses the parameter names, which are defined in the function's
5268 body.
5270 varargslist:
5271 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5272 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5275 static void
5276 symtable_default_args(struct symtable *st, node *n)
5278 node *c;
5279 int i;
5281 if (TYPE(n) == parameters) {
5282 n = CHILD(n, 1);
5283 if (TYPE(n) == RPAR)
5284 return;
5286 REQ(n, varargslist);
5287 for (i = 0; i < NCH(n); i += 2) {
5288 c = CHILD(n, i);
5289 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5290 break;
5292 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5293 symtable_node(st, CHILD(n, i));
5297 static void
5298 symtable_params(struct symtable *st, node *n)
5300 int i, complex = -1, ext = 0;
5301 node *c = NULL;
5303 if (TYPE(n) == parameters) {
5304 n = CHILD(n, 1);
5305 if (TYPE(n) == RPAR)
5306 return;
5308 REQ(n, varargslist);
5309 for (i = 0; i < NCH(n); i += 2) {
5310 c = CHILD(n, i);
5311 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5312 ext = 1;
5313 break;
5315 if (TYPE(c) == test) {
5316 continue;
5318 if (TYPE(CHILD(c, 0)) == NAME)
5319 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5320 else {
5321 char nbuf[30];
5322 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
5323 symtable_add_def(st, nbuf, DEF_PARAM);
5324 complex = i;
5327 if (ext) {
5328 c = CHILD(n, i);
5329 if (TYPE(c) == STAR) {
5330 i++;
5331 symtable_add_def(st, STR(CHILD(n, i)),
5332 DEF_PARAM | DEF_STAR);
5333 i += 2;
5334 if (i >= NCH(n))
5335 c = NULL;
5336 else
5337 c = CHILD(n, i);
5339 if (c && TYPE(c) == DOUBLESTAR) {
5340 i++;
5341 symtable_add_def(st, STR(CHILD(n, i)),
5342 DEF_PARAM | DEF_DOUBLESTAR);
5345 if (complex >= 0) {
5346 int j;
5347 for (j = 0; j <= complex; j++) {
5348 c = CHILD(n, j);
5349 if (TYPE(c) == COMMA)
5350 c = CHILD(n, ++j);
5351 else if (TYPE(c) == EQUAL)
5352 c = CHILD(n, j += 3);
5353 if (TYPE(CHILD(c, 0)) == LPAR)
5354 symtable_params_fplist(st, CHILD(c, 1));
5359 static void
5360 symtable_params_fplist(struct symtable *st, node *n)
5362 int i;
5363 node *c;
5365 REQ(n, fplist);
5366 for (i = 0; i < NCH(n); i += 2) {
5367 c = CHILD(n, i);
5368 REQ(c, fpdef);
5369 if (NCH(c) == 1)
5370 symtable_add_def(st, STR(CHILD(c, 0)),
5371 DEF_PARAM | DEF_INTUPLE);
5372 else
5373 symtable_params_fplist(st, CHILD(c, 1));
5378 static void
5379 symtable_global(struct symtable *st, node *n)
5381 int i;
5383 /* XXX It might be helpful to warn about module-level global
5384 statements, but it's hard to tell the difference between
5385 module-level and a string passed to exec.
5388 for (i = 1; i < NCH(n); i += 2) {
5389 char *name = STR(CHILD(n, i));
5390 int flags;
5392 flags = symtable_lookup(st, name);
5393 if (flags < 0)
5394 continue;
5395 if (flags && flags != DEF_GLOBAL) {
5396 char buf[500];
5397 if (flags & DEF_PARAM) {
5398 PyErr_Format(PyExc_SyntaxError,
5399 "name '%.400s' is local and global",
5400 name);
5401 PyErr_SyntaxLocation(st->st_filename,
5402 st->st_cur->ste_lineno);
5403 st->st_errors++;
5404 return;
5406 else {
5407 if (flags & DEF_LOCAL)
5408 PyOS_snprintf(buf, sizeof(buf),
5409 GLOBAL_AFTER_ASSIGN,
5410 name);
5411 else
5412 PyOS_snprintf(buf, sizeof(buf),
5413 GLOBAL_AFTER_USE, name);
5414 symtable_warn(st, buf);
5417 symtable_add_def(st, name, DEF_GLOBAL);
5421 static void
5422 symtable_list_comprehension(struct symtable *st, node *n)
5424 char tmpname[30];
5426 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
5427 symtable_add_def(st, tmpname, DEF_LOCAL);
5428 symtable_assign(st, CHILD(n, 1), 0);
5429 symtable_node(st, CHILD(n, 3));
5430 if (NCH(n) == 5)
5431 symtable_node(st, CHILD(n, 4));
5434 static void
5435 symtable_import(struct symtable *st, node *n)
5437 int i;
5438 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5439 | 'from' dotted_name 'import'
5440 ('*' | import_as_name (',' import_as_name)*)
5441 import_as_name: NAME [NAME NAME]
5443 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5444 node *dotname = CHILD(n, 1);
5445 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5446 /* check for bogus imports */
5447 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5448 PyErr_SetString(PyExc_SyntaxError,
5449 LATE_FUTURE);
5450 PyErr_SyntaxLocation(st->st_filename,
5451 n->n_lineno);
5452 st->st_errors++;
5453 return;
5456 if (TYPE(CHILD(n, 3)) == STAR) {
5457 if (st->st_cur->ste_type != TYPE_MODULE) {
5458 if (symtable_warn(st,
5459 "import * only allowed at module level") < 0)
5460 return;
5462 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5463 st->st_cur->ste_opt_lineno = n->n_lineno;
5464 } else {
5465 for (i = 3; i < NCH(n); i += 2) {
5466 node *c = CHILD(n, i);
5467 if (NCH(c) > 1) /* import as */
5468 symtable_assign(st, CHILD(c, 2),
5469 DEF_IMPORT);
5470 else
5471 symtable_assign(st, CHILD(c, 0),
5472 DEF_IMPORT);
5475 } else {
5476 for (i = 1; i < NCH(n); i += 2) {
5477 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5482 /* The third argument to symatble_assign() is a flag to be passed to
5483 symtable_add_def() if it is eventually called. The flag is useful
5484 to specify the particular type of assignment that should be
5485 recorded, e.g. an assignment caused by import.
5488 static void
5489 symtable_assign(struct symtable *st, node *n, int def_flag)
5491 node *tmp;
5492 int i;
5494 loop:
5495 switch (TYPE(n)) {
5496 case lambdef:
5497 /* invalid assignment, e.g. lambda x:x=2. The next
5498 pass will catch this error. */
5499 return;
5500 case power:
5501 if (NCH(n) > 2) {
5502 for (i = 2; i < NCH(n); ++i)
5503 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5504 symtable_node(st, CHILD(n, i));
5506 if (NCH(n) > 1) {
5507 symtable_node(st, CHILD(n, 0));
5508 symtable_node(st, CHILD(n, 1));
5509 } else {
5510 n = CHILD(n, 0);
5511 goto loop;
5513 return;
5514 case listmaker:
5515 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5516 /* XXX This is an error, but the next pass
5517 will catch it. */
5518 return;
5519 } else {
5520 for (i = 0; i < NCH(n); i += 2)
5521 symtable_assign(st, CHILD(n, i), def_flag);
5523 return;
5524 case exprlist:
5525 case testlist:
5526 case testlist1:
5527 if (NCH(n) == 1) {
5528 n = CHILD(n, 0);
5529 goto loop;
5531 else {
5532 int i;
5533 for (i = 0; i < NCH(n); i += 2)
5534 symtable_assign(st, CHILD(n, i), def_flag);
5535 return;
5537 case atom:
5538 tmp = CHILD(n, 0);
5539 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5540 n = CHILD(n, 1);
5541 goto loop;
5542 } else if (TYPE(tmp) == NAME) {
5543 if (strcmp(STR(tmp), "__debug__") == 0) {
5544 PyErr_SetString(PyExc_SyntaxError,
5545 ASSIGN_DEBUG);
5546 PyErr_SyntaxLocation(st->st_filename,
5547 n->n_lineno);
5548 st->st_errors++;
5550 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
5552 return;
5553 case dotted_as_name:
5554 if (NCH(n) == 3)
5555 symtable_add_def(st, STR(CHILD(n, 2)),
5556 DEF_LOCAL | def_flag);
5557 else
5558 symtable_add_def(st,
5559 STR(CHILD(CHILD(n,
5560 0), 0)),
5561 DEF_LOCAL | def_flag);
5562 return;
5563 case dotted_name:
5564 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
5565 return;
5566 case NAME:
5567 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
5568 return;
5569 default:
5570 if (NCH(n) == 0)
5571 return;
5572 if (NCH(n) == 1) {
5573 n = CHILD(n, 0);
5574 goto loop;
5576 /* Should only occur for errors like x + 1 = 1,
5577 which will be caught in the next pass. */
5578 for (i = 0; i < NCH(n); ++i)
5579 if (TYPE(CHILD(n, i)) >= single_input)
5580 symtable_assign(st, CHILD(n, i), def_flag);