Merged release21-maint changes.
[python/dscho.git] / Python / compile.c
blob18f6c71f047f89e55984946c4aa31b7eae0b8a88
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 paramter 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 struct memberlist 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 static PyObject *
95 code_getattr(PyCodeObject *co, char *name)
97 return PyMember_Get((char *)co, code_memberlist, name);
100 static void
101 code_dealloc(PyCodeObject *co)
103 Py_XDECREF(co->co_code);
104 Py_XDECREF(co->co_consts);
105 Py_XDECREF(co->co_names);
106 Py_XDECREF(co->co_varnames);
107 Py_XDECREF(co->co_freevars);
108 Py_XDECREF(co->co_cellvars);
109 Py_XDECREF(co->co_filename);
110 Py_XDECREF(co->co_name);
111 Py_XDECREF(co->co_lnotab);
112 PyObject_DEL(co);
115 static PyObject *
116 code_repr(PyCodeObject *co)
118 char buf[500];
119 int lineno = -1;
120 char *filename = "???";
121 char *name = "???";
123 if (co->co_firstlineno != 0)
124 lineno = co->co_firstlineno;
125 if (co->co_filename && PyString_Check(co->co_filename))
126 filename = PyString_AS_STRING(co->co_filename);
127 if (co->co_name && PyString_Check(co->co_name))
128 name = PyString_AS_STRING(co->co_name);
129 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
130 name, co, filename, lineno);
131 return PyString_FromString(buf);
134 static int
135 code_compare(PyCodeObject *co, PyCodeObject *cp)
137 int cmp;
138 cmp = PyObject_Compare(co->co_name, cp->co_name);
139 if (cmp) return cmp;
140 cmp = co->co_argcount - cp->co_argcount;
141 if (cmp) return cmp;
142 cmp = co->co_nlocals - cp->co_nlocals;
143 if (cmp) return cmp;
144 cmp = co->co_flags - cp->co_flags;
145 if (cmp) return cmp;
146 cmp = PyObject_Compare(co->co_code, cp->co_code);
147 if (cmp) return cmp;
148 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
149 if (cmp) return cmp;
150 cmp = PyObject_Compare(co->co_names, cp->co_names);
151 if (cmp) return cmp;
152 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
153 if (cmp) return cmp;
154 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
155 if (cmp) return cmp;
156 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
157 return cmp;
160 static long
161 code_hash(PyCodeObject *co)
163 long h, h0, h1, h2, h3, h4, h5, h6;
164 h0 = PyObject_Hash(co->co_name);
165 if (h0 == -1) return -1;
166 h1 = PyObject_Hash(co->co_code);
167 if (h1 == -1) return -1;
168 h2 = PyObject_Hash(co->co_consts);
169 if (h2 == -1) return -1;
170 h3 = PyObject_Hash(co->co_names);
171 if (h3 == -1) return -1;
172 h4 = PyObject_Hash(co->co_varnames);
173 if (h4 == -1) return -1;
174 h5 = PyObject_Hash(co->co_freevars);
175 if (h5 == -1) return -1;
176 h6 = PyObject_Hash(co->co_cellvars);
177 if (h6 == -1) return -1;
178 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
179 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
180 if (h == -1) h = -2;
181 return h;
184 /* XXX code objects need to participate in GC? */
186 PyTypeObject PyCode_Type = {
187 PyObject_HEAD_INIT(&PyType_Type)
189 "code",
190 sizeof(PyCodeObject),
192 (destructor)code_dealloc, /*tp_dealloc*/
193 0, /*tp_print*/
194 (getattrfunc)code_getattr, /*tp_getattr*/
195 0, /*tp_setattr*/
196 (cmpfunc)code_compare, /*tp_compare*/
197 (reprfunc)code_repr, /*tp_repr*/
198 0, /*tp_as_number*/
199 0, /*tp_as_sequence*/
200 0, /*tp_as_mapping*/
201 (hashfunc)code_hash, /*tp_hash*/
204 #define NAME_CHARS \
205 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
207 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
209 static int
210 all_name_chars(unsigned char *s)
212 static char ok_name_char[256];
213 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
215 if (ok_name_char[*name_chars] == 0) {
216 unsigned char *p;
217 for (p = name_chars; *p; p++)
218 ok_name_char[*p] = 1;
220 while (*s) {
221 if (ok_name_char[*s++] == 0)
222 return 0;
224 return 1;
227 static int
228 intern_strings(PyObject *tuple)
230 int i;
232 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
233 PyObject *v = PyTuple_GET_ITEM(tuple, i);
234 if (v == NULL || !PyString_Check(v)) {
235 Py_FatalError("non-string found in code slot");
236 PyErr_BadInternalCall();
237 return -1;
239 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
241 return 0;
244 PyCodeObject *
245 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
246 PyObject *code, PyObject *consts, PyObject *names,
247 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
248 PyObject *filename, PyObject *name, int firstlineno,
249 PyObject *lnotab)
251 PyCodeObject *co;
252 int i;
253 PyBufferProcs *pb;
254 /* Check argument types */
255 if (argcount < 0 || nlocals < 0 ||
256 code == NULL ||
257 consts == NULL || !PyTuple_Check(consts) ||
258 names == NULL || !PyTuple_Check(names) ||
259 varnames == NULL || !PyTuple_Check(varnames) ||
260 freevars == NULL || !PyTuple_Check(freevars) ||
261 cellvars == NULL || !PyTuple_Check(cellvars) ||
262 name == NULL || !PyString_Check(name) ||
263 filename == NULL || !PyString_Check(filename) ||
264 lnotab == NULL || !PyString_Check(lnotab)) {
265 PyErr_BadInternalCall();
266 return NULL;
268 pb = code->ob_type->tp_as_buffer;
269 if (pb == NULL ||
270 pb->bf_getreadbuffer == NULL ||
271 pb->bf_getsegcount == NULL ||
272 (*pb->bf_getsegcount)(code, NULL) != 1)
274 PyErr_BadInternalCall();
275 return NULL;
277 intern_strings(names);
278 intern_strings(varnames);
279 if (freevars == NULL)
280 freevars = PyTuple_New(0);
281 intern_strings(freevars);
282 if (cellvars == NULL)
283 cellvars = PyTuple_New(0);
284 intern_strings(cellvars);
285 /* Intern selected string constants */
286 for (i = PyTuple_Size(consts); --i >= 0; ) {
287 PyObject *v = PyTuple_GetItem(consts, i);
288 if (!PyString_Check(v))
289 continue;
290 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
291 continue;
292 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
294 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
295 if (co != NULL) {
296 co->co_argcount = argcount;
297 co->co_nlocals = nlocals;
298 co->co_stacksize = stacksize;
299 co->co_flags = flags;
300 Py_INCREF(code);
301 co->co_code = code;
302 Py_INCREF(consts);
303 co->co_consts = consts;
304 Py_INCREF(names);
305 co->co_names = names;
306 Py_INCREF(varnames);
307 co->co_varnames = varnames;
308 Py_INCREF(freevars);
309 co->co_freevars = freevars;
310 Py_INCREF(cellvars);
311 co->co_cellvars = cellvars;
312 Py_INCREF(filename);
313 co->co_filename = filename;
314 Py_INCREF(name);
315 co->co_name = name;
316 co->co_firstlineno = firstlineno;
317 Py_INCREF(lnotab);
318 co->co_lnotab = lnotab;
320 return co;
324 /* Data structure used internally */
326 /* The compiler uses two passes to generate bytecodes. The first pass
327 builds the symbol table. The second pass generates the bytecode.
329 The first pass uses a single symtable struct. The second pass uses
330 a compiling struct for each code block. The compiling structs
331 share a reference to the symtable.
333 The two passes communicate via symtable_load_symbols() and via
334 is_local() and is_global(). The former initializes several slots
335 in the compiling struct: c_varnames, c_locals, c_nlocals,
336 c_argcount, c_globals, and c_flags.
339 /* All about c_lnotab.
341 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
342 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
343 to source code line #s (when needed for tracebacks) via c_lnotab instead.
344 The array is conceptually a list of
345 (bytecode offset increment, line number increment)
346 pairs. The details are important and delicate, best illustrated by example:
348 byte code offset source code line number
351 50 7
352 350 307
353 361 308
355 The first trick is that these numbers aren't stored, only the increments
356 from one row to the next (this doesn't really work, but it's a start):
358 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
360 The second trick is that an unsigned byte can't hold negative values, or
361 values larger than 255, so (a) there's a deep assumption that byte code
362 offsets and their corresponding line #s both increase monotonically, and (b)
363 if at least one column jumps by more than 255 from one row to the next, more
364 than one pair is written to the table. In case #b, there's no way to know
365 from looking at the table later how many were written. That's the delicate
366 part. A user of c_lnotab desiring to find the source line number
367 corresponding to a bytecode address A should do something like this
369 lineno = addr = 0
370 for addr_incr, line_incr in c_lnotab:
371 addr += addr_incr
372 if addr > A:
373 return lineno
374 lineno += line_incr
376 In order for this to work, when the addr field increments by more than 255,
377 the line # increment in each pair generated must be 0 until the remaining addr
378 increment is < 256. So, in the example above, com_set_lineno should not (as
379 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
380 255, 0, 45, 255, 0, 45.
383 struct compiling {
384 PyObject *c_code; /* string */
385 PyObject *c_consts; /* list of objects */
386 PyObject *c_const_dict; /* inverse of c_consts */
387 PyObject *c_names; /* list of strings (names) */
388 PyObject *c_name_dict; /* inverse of c_names */
389 PyObject *c_globals; /* dictionary (value=None) */
390 PyObject *c_locals; /* dictionary (value=localID) */
391 PyObject *c_varnames; /* list (inverse of c_locals) */
392 PyObject *c_freevars; /* dictionary (value=None) */
393 PyObject *c_cellvars; /* list */
394 int c_nlocals; /* index of next local */
395 int c_argcount; /* number of top-level arguments */
396 int c_flags; /* same as co_flags */
397 int c_nexti; /* index into c_code */
398 int c_errors; /* counts errors occurred */
399 int c_infunction; /* set when compiling a function */
400 int c_interactive; /* generating code for interactive command */
401 int c_loops; /* counts nested loops */
402 int c_begin; /* begin of current loop, for 'continue' */
403 int c_block[CO_MAXBLOCKS]; /* stack of block types */
404 int c_nblocks; /* current block stack level */
405 char *c_filename; /* filename of current node */
406 char *c_name; /* name of object (e.g. function) */
407 int c_lineno; /* Current line number */
408 int c_stacklevel; /* Current stack level */
409 int c_maxstacklevel; /* Maximum stack level */
410 int c_firstlineno;
411 PyObject *c_lnotab; /* Table mapping address to line number */
412 int c_last_addr, c_last_line, c_lnotab_next;
413 char *c_private; /* for private name mangling */
414 int c_tmpname; /* temporary local name counter */
415 int c_nested; /* Is block nested funcdef or lamdef? */
416 int c_closure; /* Is nested w/freevars? */
417 struct symtable *c_symtable; /* pointer to module symbol table */
418 PyFutureFeatures *c_future; /* pointer to module's __future__ */
421 static int
422 is_free(int v)
424 if ((v & (USE | DEF_FREE))
425 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
426 return 1;
427 if (v & DEF_FREE_CLASS)
428 return 1;
429 return 0;
432 static void
433 com_error(struct compiling *c, PyObject *exc, char *msg)
435 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
437 if (c == NULL) {
438 /* Error occurred via symtable call to
439 is_constant_false */
440 PyErr_SetString(exc, msg);
441 return;
443 c->c_errors++;
444 if (c->c_lineno < 1 || c->c_interactive) {
445 /* Unknown line number or interactive input */
446 PyErr_SetString(exc, msg);
447 return;
449 v = PyString_FromString(msg);
450 if (v == NULL)
451 return; /* MemoryError, too bad */
453 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
454 if (line == NULL) {
455 Py_INCREF(Py_None);
456 line = Py_None;
458 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
459 Py_None, line);
460 if (t == NULL)
461 goto exit;
462 w = Py_BuildValue("(OO)", v, t);
463 if (w == NULL)
464 goto exit;
465 PyErr_SetObject(exc, w);
466 exit:
467 Py_XDECREF(t);
468 Py_XDECREF(v);
469 Py_XDECREF(w);
470 Py_XDECREF(line);
473 /* Interface to the block stack */
475 static void
476 block_push(struct compiling *c, int type)
478 if (c->c_nblocks >= CO_MAXBLOCKS) {
479 com_error(c, PyExc_SystemError,
480 "too many statically nested blocks");
482 else {
483 c->c_block[c->c_nblocks++] = type;
487 static void
488 block_pop(struct compiling *c, int type)
490 if (c->c_nblocks > 0)
491 c->c_nblocks--;
492 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
493 com_error(c, PyExc_SystemError, "bad block pop");
497 /* Prototype forward declarations */
499 static int com_init(struct compiling *, char *);
500 static void com_free(struct compiling *);
501 static void com_push(struct compiling *, int);
502 static void com_pop(struct compiling *, int);
503 static void com_done(struct compiling *);
504 static void com_node(struct compiling *, node *);
505 static void com_factor(struct compiling *, node *);
506 static void com_addbyte(struct compiling *, int);
507 static void com_addint(struct compiling *, int);
508 static void com_addoparg(struct compiling *, int, int);
509 static void com_addfwref(struct compiling *, int, int *);
510 static void com_backpatch(struct compiling *, int);
511 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
512 static int com_addconst(struct compiling *, PyObject *);
513 static int com_addname(struct compiling *, PyObject *);
514 static void com_addopname(struct compiling *, int, node *);
515 static void com_list(struct compiling *, node *, int);
516 static void com_list_iter(struct compiling *, node *, node *, char *);
517 static int com_argdefs(struct compiling *, node *);
518 static void com_assign(struct compiling *, node *, int, node *);
519 static void com_assign_name(struct compiling *, node *, int);
520 static PyCodeObject *icompile(node *, struct compiling *);
521 static PyCodeObject *jcompile(node *, char *, struct compiling *,
522 PyCompilerFlags *);
523 static PyObject *parsestrplus(node *);
524 static PyObject *parsestr(char *);
525 static node *get_rawdocstring(node *);
527 static int get_ref_type(struct compiling *, char *);
529 /* symtable operations */
530 static int symtable_build(struct compiling *, node *);
531 static int symtable_load_symbols(struct compiling *);
532 static struct symtable *symtable_init(void);
533 static void symtable_enter_scope(struct symtable *, char *, int, int);
534 static int symtable_exit_scope(struct symtable *);
535 static int symtable_add_def(struct symtable *, char *, int);
536 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
538 static void symtable_node(struct symtable *, node *);
539 static void symtable_funcdef(struct symtable *, node *);
540 static void symtable_default_args(struct symtable *, node *);
541 static void symtable_params(struct symtable *, node *);
542 static void symtable_params_fplist(struct symtable *, node *n);
543 static void symtable_global(struct symtable *, node *);
544 static void symtable_import(struct symtable *, node *);
545 static void symtable_assign(struct symtable *, node *, int);
546 static void symtable_list_comprehension(struct symtable *, node *);
548 static int symtable_update_free_vars(struct symtable *);
549 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
550 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
552 /* helper */
553 static void
554 do_pad(int pad)
556 int i;
557 for (i = 0; i < pad; ++i)
558 fprintf(stderr, " ");
561 static void
562 dump(node *n, int pad, int depth)
564 int i;
565 if (depth == 0)
566 return;
567 do_pad(pad);
568 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
569 if (depth > 0)
570 depth--;
571 for (i = 0; i < NCH(n); ++i)
572 dump(CHILD(n, i), pad + 1, depth);
575 #define DUMP(N) dump(N, 0, -1)
577 static int
578 com_init(struct compiling *c, char *filename)
580 memset((void *)c, '\0', sizeof(struct compiling));
581 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
582 1000)) == NULL)
583 goto fail;
584 if ((c->c_consts = PyList_New(0)) == NULL)
585 goto fail;
586 if ((c->c_const_dict = PyDict_New()) == NULL)
587 goto fail;
588 if ((c->c_names = PyList_New(0)) == NULL)
589 goto fail;
590 if ((c->c_name_dict = PyDict_New()) == NULL)
591 goto fail;
592 if ((c->c_locals = PyDict_New()) == NULL)
593 goto fail;
594 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
595 1000)) == NULL)
596 goto fail;
597 c->c_globals = NULL;
598 c->c_varnames = NULL;
599 c->c_freevars = NULL;
600 c->c_cellvars = NULL;
601 c->c_nlocals = 0;
602 c->c_argcount = 0;
603 c->c_flags = 0;
604 c->c_nexti = 0;
605 c->c_errors = 0;
606 c->c_infunction = 0;
607 c->c_interactive = 0;
608 c->c_loops = 0;
609 c->c_begin = 0;
610 c->c_nblocks = 0;
611 c->c_filename = filename;
612 c->c_name = "?";
613 c->c_lineno = 0;
614 c->c_stacklevel = 0;
615 c->c_maxstacklevel = 0;
616 c->c_firstlineno = 0;
617 c->c_last_addr = 0;
618 c->c_last_line = 0;
619 c->c_lnotab_next = 0;
620 c->c_tmpname = 0;
621 c->c_nested = 0;
622 c->c_closure = 0;
623 c->c_symtable = NULL;
624 return 1;
626 fail:
627 com_free(c);
628 return 0;
631 static void
632 com_free(struct compiling *c)
634 Py_XDECREF(c->c_code);
635 Py_XDECREF(c->c_consts);
636 Py_XDECREF(c->c_const_dict);
637 Py_XDECREF(c->c_names);
638 Py_XDECREF(c->c_name_dict);
639 Py_XDECREF(c->c_globals);
640 Py_XDECREF(c->c_locals);
641 Py_XDECREF(c->c_varnames);
642 Py_XDECREF(c->c_freevars);
643 Py_XDECREF(c->c_cellvars);
644 Py_XDECREF(c->c_lnotab);
645 if (c->c_future)
646 PyMem_Free((void *)c->c_future);
649 static void
650 com_push(struct compiling *c, int n)
652 c->c_stacklevel += n;
653 if (c->c_stacklevel > c->c_maxstacklevel)
654 c->c_maxstacklevel = c->c_stacklevel;
657 static void
658 com_pop(struct compiling *c, int n)
660 if (c->c_stacklevel < n) {
661 /* fprintf(stderr,
662 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
663 c->c_filename, c->c_lineno,
664 c->c_nexti, c->c_stacklevel, n); */
665 c->c_stacklevel = 0;
667 else
668 c->c_stacklevel -= n;
671 static void
672 com_done(struct compiling *c)
674 if (c->c_code != NULL)
675 _PyString_Resize(&c->c_code, c->c_nexti);
676 if (c->c_lnotab != NULL)
677 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
680 static void
681 com_addbyte(struct compiling *c, int byte)
683 int len;
684 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
685 assert(byte >= 0 && byte <= 255);
686 if (byte < 0 || byte > 255) {
687 com_error(c, PyExc_SystemError,
688 "com_addbyte: byte out of range");
690 if (c->c_code == NULL)
691 return;
692 len = PyString_Size(c->c_code);
693 if (c->c_nexti >= len) {
694 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
695 c->c_errors++;
696 return;
699 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
702 static void
703 com_addint(struct compiling *c, int x)
705 com_addbyte(c, x & 0xff);
706 com_addbyte(c, x >> 8); /* XXX x should be positive */
709 static void
710 com_add_lnotab(struct compiling *c, int addr, int line)
712 int size;
713 char *p;
714 if (c->c_lnotab == NULL)
715 return;
716 size = PyString_Size(c->c_lnotab);
717 if (c->c_lnotab_next+2 > size) {
718 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
719 c->c_errors++;
720 return;
723 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
724 *p++ = addr;
725 *p++ = line;
726 c->c_lnotab_next += 2;
729 static void
730 com_set_lineno(struct compiling *c, int lineno)
732 c->c_lineno = lineno;
733 if (c->c_firstlineno == 0) {
734 c->c_firstlineno = c->c_last_line = lineno;
736 else {
737 int incr_addr = c->c_nexti - c->c_last_addr;
738 int incr_line = lineno - c->c_last_line;
739 while (incr_addr > 255) {
740 com_add_lnotab(c, 255, 0);
741 incr_addr -= 255;
743 while (incr_line > 255) {
744 com_add_lnotab(c, incr_addr, 255);
745 incr_line -=255;
746 incr_addr = 0;
748 if (incr_addr > 0 || incr_line > 0)
749 com_add_lnotab(c, incr_addr, incr_line);
750 c->c_last_addr = c->c_nexti;
751 c->c_last_line = lineno;
755 static void
756 com_addoparg(struct compiling *c, int op, int arg)
758 int extended_arg = arg >> 16;
759 if (op == SET_LINENO) {
760 com_set_lineno(c, arg);
761 if (Py_OptimizeFlag)
762 return;
764 if (extended_arg){
765 com_addbyte(c, EXTENDED_ARG);
766 com_addint(c, extended_arg);
767 arg &= 0xffff;
769 com_addbyte(c, op);
770 com_addint(c, arg);
773 static void
774 com_addfwref(struct compiling *c, int op, int *p_anchor)
776 /* Compile a forward reference for backpatching */
777 int here;
778 int anchor;
779 com_addbyte(c, op);
780 here = c->c_nexti;
781 anchor = *p_anchor;
782 *p_anchor = here;
783 com_addint(c, anchor == 0 ? 0 : here - anchor);
786 static void
787 com_backpatch(struct compiling *c, int anchor)
789 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
790 int target = c->c_nexti;
791 int dist;
792 int prev;
793 for (;;) {
794 /* Make the JUMP instruction at anchor point to target */
795 prev = code[anchor] + (code[anchor+1] << 8);
796 dist = target - (anchor+2);
797 code[anchor] = dist & 0xff;
798 dist >>= 8;
799 code[anchor+1] = dist;
800 dist >>= 8;
801 if (dist) {
802 com_error(c, PyExc_SystemError,
803 "com_backpatch: offset too large");
804 break;
806 if (!prev)
807 break;
808 anchor -= prev;
812 /* Handle literals and names uniformly */
814 static int
815 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
817 PyObject *w, *t, *np=NULL;
818 long n;
820 t = Py_BuildValue("(OO)", v, v->ob_type);
821 if (t == NULL)
822 goto fail;
823 w = PyDict_GetItem(dict, t);
824 if (w != NULL) {
825 n = PyInt_AsLong(w);
826 } else {
827 n = PyList_Size(list);
828 np = PyInt_FromLong(n);
829 if (np == NULL)
830 goto fail;
831 if (PyList_Append(list, v) != 0)
832 goto fail;
833 if (PyDict_SetItem(dict, t, np) != 0)
834 goto fail;
835 Py_DECREF(np);
837 Py_DECREF(t);
838 return n;
839 fail:
840 Py_XDECREF(np);
841 Py_XDECREF(t);
842 c->c_errors++;
843 return 0;
846 static int
847 com_addconst(struct compiling *c, PyObject *v)
849 return com_add(c, c->c_consts, c->c_const_dict, v);
852 static int
853 com_addname(struct compiling *c, PyObject *v)
855 return com_add(c, c->c_names, c->c_name_dict, v);
858 static int
859 mangle(char *p, char *name, char *buffer, size_t maxlen)
861 /* Name mangling: __private becomes _classname__private.
862 This is independent from how the name is used. */
863 size_t nlen, plen;
864 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
865 return 0;
866 nlen = strlen(name);
867 if (nlen+2 >= maxlen)
868 return 0; /* Don't mangle __extremely_long_names */
869 if (name[nlen-1] == '_' && name[nlen-2] == '_')
870 return 0; /* Don't mangle __whatever__ */
871 /* Strip leading underscores from class name */
872 while (*p == '_')
873 p++;
874 if (*p == '\0')
875 return 0; /* Don't mangle if class is just underscores */
876 plen = strlen(p);
877 if (plen + nlen >= maxlen)
878 plen = maxlen-nlen-2; /* Truncate class name if too long */
879 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
880 buffer[0] = '_';
881 strncpy(buffer+1, p, plen);
882 strcpy(buffer+1+plen, name);
883 return 1;
886 static void
887 com_addop_name(struct compiling *c, int op, char *name)
889 PyObject *v;
890 int i;
891 char buffer[MANGLE_LEN];
893 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
894 name = buffer;
895 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
896 c->c_errors++;
897 i = 255;
899 else {
900 i = com_addname(c, v);
901 Py_DECREF(v);
903 com_addoparg(c, op, i);
906 #define NAME_LOCAL 0
907 #define NAME_GLOBAL 1
908 #define NAME_DEFAULT 2
909 #define NAME_CLOSURE 3
911 static int
912 com_lookup_arg(PyObject *dict, PyObject *name)
914 PyObject *v = PyDict_GetItem(dict, name);
915 if (v == NULL)
916 return -1;
917 else
918 return PyInt_AS_LONG(v);
921 static void
922 com_addop_varname(struct compiling *c, int kind, char *name)
924 PyObject *v;
925 int i, reftype;
926 int scope = NAME_DEFAULT;
927 int op = STOP_CODE;
928 char buffer[MANGLE_LEN];
930 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
931 name = buffer;
932 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
933 c->c_errors++;
934 i = 255;
935 goto done;
938 reftype = get_ref_type(c, name);
939 switch (reftype) {
940 case LOCAL:
941 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
942 scope = NAME_LOCAL;
943 break;
944 case GLOBAL_EXPLICIT:
945 scope = NAME_GLOBAL;
946 break;
947 case GLOBAL_IMPLICIT:
948 if (c->c_flags & CO_OPTIMIZED)
949 scope = NAME_GLOBAL;
950 break;
951 case FREE:
952 case CELL:
953 scope = NAME_CLOSURE;
954 break;
957 i = com_addname(c, v);
958 if (scope == NAME_LOCAL)
959 i = com_lookup_arg(c->c_locals, v);
960 else if (reftype == FREE)
961 i = com_lookup_arg(c->c_freevars, v);
962 else if (reftype == CELL)
963 i = com_lookup_arg(c->c_cellvars, v);
964 if (i == -1) {
965 c->c_errors++; /* XXX no exception set */
966 i = 255;
967 goto done;
969 Py_DECREF(v);
971 switch (kind) {
972 case VAR_LOAD:
973 switch (scope) {
974 case NAME_LOCAL:
975 op = LOAD_FAST;
976 break;
977 case NAME_GLOBAL:
978 op = LOAD_GLOBAL;
979 break;
980 case NAME_DEFAULT:
981 op = LOAD_NAME;
982 break;
983 case NAME_CLOSURE:
984 op = LOAD_DEREF;
985 break;
987 break;
988 case VAR_STORE:
989 switch (scope) {
990 case NAME_LOCAL:
991 op = STORE_FAST;
992 break;
993 case NAME_GLOBAL:
994 op = STORE_GLOBAL;
995 break;
996 case NAME_DEFAULT:
997 op = STORE_NAME;
998 break;
999 case NAME_CLOSURE:
1000 op = STORE_DEREF;
1001 break;
1003 break;
1004 case VAR_DELETE:
1005 switch (scope) {
1006 case NAME_LOCAL:
1007 op = DELETE_FAST;
1008 break;
1009 case NAME_GLOBAL:
1010 op = DELETE_GLOBAL;
1011 break;
1012 case NAME_DEFAULT:
1013 op = DELETE_NAME;
1014 break;
1015 case NAME_CLOSURE: {
1016 char buf[500];
1017 sprintf(buf, DEL_CLOSURE_ERROR, name);
1018 com_error(c, PyExc_SyntaxError, buf);
1019 i = 255;
1020 break;
1023 break;
1025 done:
1026 com_addoparg(c, op, i);
1029 static void
1030 com_addopname(struct compiling *c, int op, node *n)
1032 char *name;
1033 char buffer[1000];
1034 /* XXX it is possible to write this code without the 1000
1035 chars on the total length of dotted names, I just can't be
1036 bothered right now */
1037 if (TYPE(n) == STAR)
1038 name = "*";
1039 else if (TYPE(n) == dotted_name) {
1040 char *p = buffer;
1041 int i;
1042 name = buffer;
1043 for (i = 0; i < NCH(n); i += 2) {
1044 char *s = STR(CHILD(n, i));
1045 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
1046 com_error(c, PyExc_MemoryError,
1047 "dotted_name too long");
1048 name = NULL;
1049 break;
1051 if (p != buffer)
1052 *p++ = '.';
1053 strcpy(p, s);
1054 p = strchr(p, '\0');
1057 else {
1058 REQ(n, NAME);
1059 name = STR(n);
1061 com_addop_name(c, op, name);
1064 static PyObject *
1065 parsenumber(struct compiling *co, char *s)
1067 char *end;
1068 long x;
1069 double dx;
1070 #ifndef WITHOUT_COMPLEX
1071 Py_complex c;
1072 int imflag;
1073 #endif
1075 errno = 0;
1076 end = s + strlen(s) - 1;
1077 #ifndef WITHOUT_COMPLEX
1078 imflag = *end == 'j' || *end == 'J';
1079 #endif
1080 if (*end == 'l' || *end == 'L')
1081 return PyLong_FromString(s, (char **)0, 0);
1082 if (s[0] == '0')
1083 x = (long) PyOS_strtoul(s, &end, 0);
1084 else
1085 x = PyOS_strtol(s, &end, 0);
1086 if (*end == '\0') {
1087 if (errno != 0) {
1088 com_error(co, PyExc_OverflowError,
1089 "integer literal too large");
1090 return NULL;
1092 return PyInt_FromLong(x);
1094 /* XXX Huge floats may silently fail */
1095 #ifndef WITHOUT_COMPLEX
1096 if (imflag) {
1097 c.real = 0.;
1098 PyFPE_START_PROTECT("atof", return 0)
1099 c.imag = atof(s);
1100 PyFPE_END_PROTECT(c)
1101 return PyComplex_FromCComplex(c);
1103 else
1104 #endif
1106 PyFPE_START_PROTECT("atof", return 0)
1107 dx = atof(s);
1108 PyFPE_END_PROTECT(dx)
1109 return PyFloat_FromDouble(dx);
1113 static PyObject *
1114 parsestr(char *s)
1116 PyObject *v;
1117 size_t len;
1118 char *buf;
1119 char *p;
1120 char *end;
1121 int c;
1122 int first = *s;
1123 int quote = first;
1124 int rawmode = 0;
1125 int unicode = 0;
1126 if (isalpha(quote) || quote == '_') {
1127 if (quote == 'u' || quote == 'U') {
1128 quote = *++s;
1129 unicode = 1;
1131 if (quote == 'r' || quote == 'R') {
1132 quote = *++s;
1133 rawmode = 1;
1136 if (quote != '\'' && quote != '\"') {
1137 PyErr_BadInternalCall();
1138 return NULL;
1140 s++;
1141 len = strlen(s);
1142 if (len > INT_MAX) {
1143 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1144 return NULL;
1146 if (s[--len] != quote) {
1147 PyErr_BadInternalCall();
1148 return NULL;
1150 if (len >= 4 && s[0] == quote && s[1] == quote) {
1151 s += 2;
1152 len -= 2;
1153 if (s[--len] != quote || s[--len] != quote) {
1154 PyErr_BadInternalCall();
1155 return NULL;
1158 if (unicode || Py_UnicodeFlag) {
1159 if (rawmode)
1160 return PyUnicode_DecodeRawUnicodeEscape(
1161 s, len, NULL);
1162 else
1163 return PyUnicode_DecodeUnicodeEscape(
1164 s, len, NULL);
1166 if (rawmode || strchr(s, '\\') == NULL)
1167 return PyString_FromStringAndSize(s, len);
1168 v = PyString_FromStringAndSize((char *)NULL, len);
1169 if (v == NULL)
1170 return NULL;
1171 p = buf = PyString_AsString(v);
1172 end = s + len;
1173 while (s < end) {
1174 if (*s != '\\') {
1175 *p++ = *s++;
1176 continue;
1178 s++;
1179 switch (*s++) {
1180 /* XXX This assumes ASCII! */
1181 case '\n': break;
1182 case '\\': *p++ = '\\'; break;
1183 case '\'': *p++ = '\''; break;
1184 case '\"': *p++ = '\"'; break;
1185 case 'b': *p++ = '\b'; break;
1186 case 'f': *p++ = '\014'; break; /* FF */
1187 case 't': *p++ = '\t'; break;
1188 case 'n': *p++ = '\n'; break;
1189 case 'r': *p++ = '\r'; break;
1190 case 'v': *p++ = '\013'; break; /* VT */
1191 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1192 case '0': case '1': case '2': case '3':
1193 case '4': case '5': case '6': case '7':
1194 c = s[-1] - '0';
1195 if ('0' <= *s && *s <= '7') {
1196 c = (c<<3) + *s++ - '0';
1197 if ('0' <= *s && *s <= '7')
1198 c = (c<<3) + *s++ - '0';
1200 *p++ = c;
1201 break;
1202 case 'x':
1203 if (isxdigit(Py_CHARMASK(s[0]))
1204 && isxdigit(Py_CHARMASK(s[1]))) {
1205 unsigned int x = 0;
1206 c = Py_CHARMASK(*s);
1207 s++;
1208 if (isdigit(c))
1209 x = c - '0';
1210 else if (islower(c))
1211 x = 10 + c - 'a';
1212 else
1213 x = 10 + c - 'A';
1214 x = x << 4;
1215 c = Py_CHARMASK(*s);
1216 s++;
1217 if (isdigit(c))
1218 x += c - '0';
1219 else if (islower(c))
1220 x += 10 + c - 'a';
1221 else
1222 x += 10 + c - 'A';
1223 *p++ = x;
1224 break;
1226 PyErr_SetString(PyExc_ValueError,
1227 "invalid \\x escape");
1228 Py_DECREF(v);
1229 return NULL;
1230 default:
1231 *p++ = '\\';
1232 *p++ = s[-1];
1233 break;
1236 _PyString_Resize(&v, (int)(p - buf));
1237 return v;
1240 static PyObject *
1241 parsestrplus(node *n)
1243 PyObject *v;
1244 int i;
1245 REQ(CHILD(n, 0), STRING);
1246 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
1247 /* String literal concatenation */
1248 for (i = 1; i < NCH(n); i++) {
1249 PyObject *s;
1250 s = parsestr(STR(CHILD(n, i)));
1251 if (s == NULL)
1252 goto onError;
1253 if (PyString_Check(v) && PyString_Check(s)) {
1254 PyString_ConcatAndDel(&v, s);
1255 if (v == NULL)
1256 goto onError;
1258 else {
1259 PyObject *temp;
1260 temp = PyUnicode_Concat(v, s);
1261 Py_DECREF(s);
1262 if (temp == NULL)
1263 goto onError;
1264 Py_DECREF(v);
1265 v = temp;
1269 return v;
1271 onError:
1272 Py_XDECREF(v);
1273 return NULL;
1276 static void
1277 com_list_for(struct compiling *c, node *n, node *e, char *t)
1279 int anchor = 0;
1280 int save_begin = c->c_begin;
1282 /* list_iter: for v in expr [list_iter] */
1283 com_node(c, CHILD(n, 3)); /* expr */
1284 com_addbyte(c, GET_ITER);
1285 c->c_begin = c->c_nexti;
1286 com_addoparg(c, SET_LINENO, n->n_lineno);
1287 com_addfwref(c, FOR_ITER, &anchor);
1288 com_push(c, 1);
1289 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1290 c->c_loops++;
1291 com_list_iter(c, n, e, t);
1292 c->c_loops--;
1293 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1294 c->c_begin = save_begin;
1295 com_backpatch(c, anchor);
1296 com_pop(c, 1); /* FOR_ITER has popped this */
1299 static void
1300 com_list_if(struct compiling *c, node *n, node *e, char *t)
1302 int anchor = 0;
1303 int a = 0;
1304 /* list_iter: 'if' test [list_iter] */
1305 com_addoparg(c, SET_LINENO, n->n_lineno);
1306 com_node(c, CHILD(n, 1));
1307 com_addfwref(c, JUMP_IF_FALSE, &a);
1308 com_addbyte(c, POP_TOP);
1309 com_pop(c, 1);
1310 com_list_iter(c, n, e, t);
1311 com_addfwref(c, JUMP_FORWARD, &anchor);
1312 com_backpatch(c, a);
1313 /* We jump here with an extra entry which we now pop */
1314 com_addbyte(c, POP_TOP);
1315 com_backpatch(c, anchor);
1318 static void
1319 com_list_iter(struct compiling *c,
1320 node *p, /* parent of list_iter node */
1321 node *e, /* element expression node */
1322 char *t /* name of result list temp local */)
1324 /* list_iter is the last child in a listmaker, list_for, or list_if */
1325 node *n = CHILD(p, NCH(p)-1);
1326 if (TYPE(n) == list_iter) {
1327 n = CHILD(n, 0);
1328 switch (TYPE(n)) {
1329 case list_for:
1330 com_list_for(c, n, e, t);
1331 break;
1332 case list_if:
1333 com_list_if(c, n, e, t);
1334 break;
1335 default:
1336 com_error(c, PyExc_SystemError,
1337 "invalid list_iter node type");
1340 else {
1341 com_addop_varname(c, VAR_LOAD, t);
1342 com_push(c, 1);
1343 com_node(c, e);
1344 com_addoparg(c, CALL_FUNCTION, 1);
1345 com_addbyte(c, POP_TOP);
1346 com_pop(c, 2);
1350 static void
1351 com_list_comprehension(struct compiling *c, node *n)
1353 /* listmaker: test list_for */
1354 char tmpname[12];
1355 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
1356 com_addoparg(c, BUILD_LIST, 0);
1357 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1358 com_push(c, 2);
1359 com_addop_name(c, LOAD_ATTR, "append");
1360 com_addop_varname(c, VAR_STORE, tmpname);
1361 com_pop(c, 1);
1362 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1363 com_addop_varname(c, VAR_DELETE, tmpname);
1364 --c->c_tmpname;
1367 static void
1368 com_listmaker(struct compiling *c, node *n)
1370 /* listmaker: test ( list_for | (',' test)* [','] ) */
1371 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1372 com_list_comprehension(c, n);
1373 else {
1374 int len = 0;
1375 int i;
1376 for (i = 0; i < NCH(n); i += 2, len++)
1377 com_node(c, CHILD(n, i));
1378 com_addoparg(c, BUILD_LIST, len);
1379 com_pop(c, len-1);
1383 static void
1384 com_dictmaker(struct compiling *c, node *n)
1386 int i;
1387 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1388 for (i = 0; i+2 < NCH(n); i += 4) {
1389 /* We must arrange things just right for STORE_SUBSCR.
1390 It wants the stack to look like (value) (dict) (key) */
1391 com_addbyte(c, DUP_TOP);
1392 com_push(c, 1);
1393 com_node(c, CHILD(n, i+2)); /* value */
1394 com_addbyte(c, ROT_TWO);
1395 com_node(c, CHILD(n, i)); /* key */
1396 com_addbyte(c, STORE_SUBSCR);
1397 com_pop(c, 3);
1401 static void
1402 com_atom(struct compiling *c, node *n)
1404 node *ch;
1405 PyObject *v;
1406 int i;
1407 REQ(n, atom);
1408 ch = CHILD(n, 0);
1409 switch (TYPE(ch)) {
1410 case LPAR:
1411 if (TYPE(CHILD(n, 1)) == RPAR) {
1412 com_addoparg(c, BUILD_TUPLE, 0);
1413 com_push(c, 1);
1415 else
1416 com_node(c, CHILD(n, 1));
1417 break;
1418 case LSQB: /* '[' [listmaker] ']' */
1419 if (TYPE(CHILD(n, 1)) == RSQB) {
1420 com_addoparg(c, BUILD_LIST, 0);
1421 com_push(c, 1);
1423 else
1424 com_listmaker(c, CHILD(n, 1));
1425 break;
1426 case LBRACE: /* '{' [dictmaker] '}' */
1427 com_addoparg(c, BUILD_MAP, 0);
1428 com_push(c, 1);
1429 if (TYPE(CHILD(n, 1)) == dictmaker)
1430 com_dictmaker(c, CHILD(n, 1));
1431 break;
1432 case BACKQUOTE:
1433 com_node(c, CHILD(n, 1));
1434 com_addbyte(c, UNARY_CONVERT);
1435 break;
1436 case NUMBER:
1437 if ((v = parsenumber(c, STR(ch))) == NULL) {
1438 i = 255;
1440 else {
1441 i = com_addconst(c, v);
1442 Py_DECREF(v);
1444 com_addoparg(c, LOAD_CONST, i);
1445 com_push(c, 1);
1446 break;
1447 case STRING:
1448 v = parsestrplus(n);
1449 if (v == NULL) {
1450 c->c_errors++;
1451 i = 255;
1453 else {
1454 i = com_addconst(c, v);
1455 Py_DECREF(v);
1457 com_addoparg(c, LOAD_CONST, i);
1458 com_push(c, 1);
1459 break;
1460 case NAME:
1461 com_addop_varname(c, VAR_LOAD, STR(ch));
1462 com_push(c, 1);
1463 break;
1464 default:
1465 com_error(c, PyExc_SystemError,
1466 "com_atom: unexpected node type");
1470 static void
1471 com_slice(struct compiling *c, node *n, int op)
1473 if (NCH(n) == 1) {
1474 com_addbyte(c, op);
1476 else if (NCH(n) == 2) {
1477 if (TYPE(CHILD(n, 0)) != COLON) {
1478 com_node(c, CHILD(n, 0));
1479 com_addbyte(c, op+1);
1481 else {
1482 com_node(c, CHILD(n, 1));
1483 com_addbyte(c, op+2);
1485 com_pop(c, 1);
1487 else {
1488 com_node(c, CHILD(n, 0));
1489 com_node(c, CHILD(n, 2));
1490 com_addbyte(c, op+3);
1491 com_pop(c, 2);
1495 static void
1496 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1498 if (NCH(n) == 1) {
1499 com_addbyte(c, DUP_TOP);
1500 com_push(c, 1);
1501 com_addbyte(c, SLICE);
1502 com_node(c, augn);
1503 com_addbyte(c, opcode);
1504 com_pop(c, 1);
1505 com_addbyte(c, ROT_TWO);
1506 com_addbyte(c, STORE_SLICE);
1507 com_pop(c, 2);
1508 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1509 com_node(c, CHILD(n, 0));
1510 com_addoparg(c, DUP_TOPX, 2);
1511 com_push(c, 2);
1512 com_addbyte(c, SLICE+1);
1513 com_pop(c, 1);
1514 com_node(c, augn);
1515 com_addbyte(c, opcode);
1516 com_pop(c, 1);
1517 com_addbyte(c, ROT_THREE);
1518 com_addbyte(c, STORE_SLICE+1);
1519 com_pop(c, 3);
1520 } else if (NCH(n) == 2) {
1521 com_node(c, CHILD(n, 1));
1522 com_addoparg(c, DUP_TOPX, 2);
1523 com_push(c, 2);
1524 com_addbyte(c, SLICE+2);
1525 com_pop(c, 1);
1526 com_node(c, augn);
1527 com_addbyte(c, opcode);
1528 com_pop(c, 1);
1529 com_addbyte(c, ROT_THREE);
1530 com_addbyte(c, STORE_SLICE+2);
1531 com_pop(c, 3);
1532 } else {
1533 com_node(c, CHILD(n, 0));
1534 com_node(c, CHILD(n, 2));
1535 com_addoparg(c, DUP_TOPX, 3);
1536 com_push(c, 3);
1537 com_addbyte(c, SLICE+3);
1538 com_pop(c, 2);
1539 com_node(c, augn);
1540 com_addbyte(c, opcode);
1541 com_pop(c, 1);
1542 com_addbyte(c, ROT_FOUR);
1543 com_addbyte(c, STORE_SLICE+3);
1544 com_pop(c, 4);
1548 static void
1549 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1551 node *m;
1552 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1553 if (NCH(n) == 1) {
1554 if (*pkeywords != NULL) {
1555 com_error(c, PyExc_SyntaxError,
1556 "non-keyword arg after keyword arg");
1558 else {
1559 com_node(c, CHILD(n, 0));
1561 return;
1563 m = n;
1564 do {
1565 m = CHILD(m, 0);
1566 } while (NCH(m) == 1);
1567 if (TYPE(m) != NAME) {
1568 /* f(lambda x: x[0] = 3) ends up getting parsed with
1569 * LHS test = lambda x: x[0], and RHS test = 3.
1570 * SF bug 132313 points out that complaining about a keyword
1571 * then is very confusing.
1573 com_error(c, PyExc_SyntaxError,
1574 TYPE(m) == lambdef ?
1575 "lambda cannot contain assignment" :
1576 "keyword can't be an expression");
1578 else {
1579 PyObject *v = PyString_InternFromString(STR(m));
1580 if (v != NULL && *pkeywords == NULL)
1581 *pkeywords = PyDict_New();
1582 if (v == NULL)
1583 c->c_errors++;
1584 else if (*pkeywords == NULL) {
1585 c->c_errors++;
1586 Py_DECREF(v);
1587 } else {
1588 if (PyDict_GetItem(*pkeywords, v) != NULL)
1589 com_error(c, PyExc_SyntaxError,
1590 "duplicate keyword argument");
1591 else
1592 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1593 c->c_errors++;
1594 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1595 com_push(c, 1);
1596 Py_DECREF(v);
1599 com_node(c, CHILD(n, 2));
1602 static void
1603 com_call_function(struct compiling *c, node *n)
1605 if (TYPE(n) == RPAR) {
1606 com_addoparg(c, CALL_FUNCTION, 0);
1608 else {
1609 PyObject *keywords = NULL;
1610 int i, na, nk;
1611 int lineno = n->n_lineno;
1612 int star_flag = 0;
1613 int starstar_flag = 0;
1614 int opcode;
1615 REQ(n, arglist);
1616 na = 0;
1617 nk = 0;
1618 for (i = 0; i < NCH(n); i += 2) {
1619 node *ch = CHILD(n, i);
1620 if (TYPE(ch) == STAR ||
1621 TYPE(ch) == DOUBLESTAR)
1622 break;
1623 if (ch->n_lineno != lineno) {
1624 lineno = ch->n_lineno;
1625 com_addoparg(c, SET_LINENO, lineno);
1627 com_argument(c, ch, &keywords);
1628 if (keywords == NULL)
1629 na++;
1630 else
1631 nk++;
1633 Py_XDECREF(keywords);
1634 while (i < NCH(n)) {
1635 node *tok = CHILD(n, i);
1636 node *ch = CHILD(n, i+1);
1637 i += 3;
1638 switch (TYPE(tok)) {
1639 case STAR: star_flag = 1; break;
1640 case DOUBLESTAR: starstar_flag = 1; break;
1642 com_node(c, ch);
1644 if (na > 255 || nk > 255) {
1645 com_error(c, PyExc_SyntaxError,
1646 "more than 255 arguments");
1648 if (star_flag || starstar_flag)
1649 opcode = CALL_FUNCTION_VAR - 1 +
1650 star_flag + (starstar_flag << 1);
1651 else
1652 opcode = CALL_FUNCTION;
1653 com_addoparg(c, opcode, na | (nk << 8));
1654 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1658 static void
1659 com_select_member(struct compiling *c, node *n)
1661 com_addopname(c, LOAD_ATTR, n);
1664 static void
1665 com_sliceobj(struct compiling *c, node *n)
1667 int i=0;
1668 int ns=2; /* number of slice arguments */
1669 node *ch;
1671 /* first argument */
1672 if (TYPE(CHILD(n,i)) == COLON) {
1673 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1674 com_push(c, 1);
1675 i++;
1677 else {
1678 com_node(c, CHILD(n,i));
1679 i++;
1680 REQ(CHILD(n,i),COLON);
1681 i++;
1683 /* second argument */
1684 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1685 com_node(c, CHILD(n,i));
1686 i++;
1688 else {
1689 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1690 com_push(c, 1);
1692 /* remaining arguments */
1693 for (; i < NCH(n); i++) {
1694 ns++;
1695 ch=CHILD(n,i);
1696 REQ(ch, sliceop);
1697 if (NCH(ch) == 1) {
1698 /* right argument of ':' missing */
1699 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1700 com_push(c, 1);
1702 else
1703 com_node(c, CHILD(ch,1));
1705 com_addoparg(c, BUILD_SLICE, ns);
1706 com_pop(c, 1 + (ns == 3));
1709 static void
1710 com_subscript(struct compiling *c, node *n)
1712 node *ch;
1713 REQ(n, subscript);
1714 ch = CHILD(n,0);
1715 /* check for rubber index */
1716 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1717 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1718 com_push(c, 1);
1720 else {
1721 /* check for slice */
1722 if ((TYPE(ch) == COLON || NCH(n) > 1))
1723 com_sliceobj(c, n);
1724 else {
1725 REQ(ch, test);
1726 com_node(c, ch);
1731 static void
1732 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1734 int i, op;
1735 REQ(n, subscriptlist);
1736 /* Check to make backward compatible slice behavior for '[i:j]' */
1737 if (NCH(n) == 1) {
1738 node *sub = CHILD(n, 0); /* subscript */
1739 /* 'Basic' slice, should have exactly one colon. */
1740 if ((TYPE(CHILD(sub, 0)) == COLON
1741 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1742 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1744 switch (assigning) {
1745 case OP_DELETE:
1746 op = DELETE_SLICE;
1747 break;
1748 case OP_ASSIGN:
1749 op = STORE_SLICE;
1750 break;
1751 case OP_APPLY:
1752 op = SLICE;
1753 break;
1754 default:
1755 com_augassign_slice(c, sub, assigning, augn);
1756 return;
1758 com_slice(c, sub, op);
1759 if (op == STORE_SLICE)
1760 com_pop(c, 2);
1761 else if (op == DELETE_SLICE)
1762 com_pop(c, 1);
1763 return;
1766 /* Else normal subscriptlist. Compile each subscript. */
1767 for (i = 0; i < NCH(n); i += 2)
1768 com_subscript(c, CHILD(n, i));
1769 /* Put multiple subscripts into a tuple */
1770 if (NCH(n) > 1) {
1771 i = (NCH(n)+1) / 2;
1772 com_addoparg(c, BUILD_TUPLE, i);
1773 com_pop(c, i-1);
1775 switch (assigning) {
1776 case OP_DELETE:
1777 op = DELETE_SUBSCR;
1778 i = 2;
1779 break;
1780 default:
1781 case OP_ASSIGN:
1782 op = STORE_SUBSCR;
1783 i = 3;
1784 break;
1785 case OP_APPLY:
1786 op = BINARY_SUBSCR;
1787 i = 1;
1788 break;
1790 if (assigning > OP_APPLY) {
1791 com_addoparg(c, DUP_TOPX, 2);
1792 com_push(c, 2);
1793 com_addbyte(c, BINARY_SUBSCR);
1794 com_pop(c, 1);
1795 com_node(c, augn);
1796 com_addbyte(c, assigning);
1797 com_pop(c, 1);
1798 com_addbyte(c, ROT_THREE);
1800 com_addbyte(c, op);
1801 com_pop(c, i);
1804 static void
1805 com_apply_trailer(struct compiling *c, node *n)
1807 REQ(n, trailer);
1808 switch (TYPE(CHILD(n, 0))) {
1809 case LPAR:
1810 com_call_function(c, CHILD(n, 1));
1811 break;
1812 case DOT:
1813 com_select_member(c, CHILD(n, 1));
1814 break;
1815 case LSQB:
1816 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1817 break;
1818 default:
1819 com_error(c, PyExc_SystemError,
1820 "com_apply_trailer: unknown trailer type");
1824 static void
1825 com_power(struct compiling *c, node *n)
1827 int i;
1828 REQ(n, power);
1829 com_atom(c, CHILD(n, 0));
1830 for (i = 1; i < NCH(n); i++) {
1831 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1832 com_factor(c, CHILD(n, i+1));
1833 com_addbyte(c, BINARY_POWER);
1834 com_pop(c, 1);
1835 break;
1837 else
1838 com_apply_trailer(c, CHILD(n, i));
1842 static void
1843 com_factor(struct compiling *c, node *n)
1845 REQ(n, factor);
1846 if (TYPE(CHILD(n, 0)) == PLUS) {
1847 com_factor(c, CHILD(n, 1));
1848 com_addbyte(c, UNARY_POSITIVE);
1850 else if (TYPE(CHILD(n, 0)) == MINUS) {
1851 com_factor(c, CHILD(n, 1));
1852 com_addbyte(c, UNARY_NEGATIVE);
1854 else if (TYPE(CHILD(n, 0)) == TILDE) {
1855 com_factor(c, CHILD(n, 1));
1856 com_addbyte(c, UNARY_INVERT);
1858 else {
1859 com_power(c, CHILD(n, 0));
1863 static void
1864 com_term(struct compiling *c, node *n)
1866 int i;
1867 int op;
1868 REQ(n, term);
1869 com_factor(c, CHILD(n, 0));
1870 for (i = 2; i < NCH(n); i += 2) {
1871 com_factor(c, CHILD(n, i));
1872 switch (TYPE(CHILD(n, i-1))) {
1873 case STAR:
1874 op = BINARY_MULTIPLY;
1875 break;
1876 case SLASH:
1877 if (c->c_flags & CO_FUTURE_DIVISION)
1878 op = BINARY_TRUE_DIVIDE;
1879 else
1880 op = BINARY_DIVIDE;
1881 break;
1882 case PERCENT:
1883 op = BINARY_MODULO;
1884 break;
1885 case DOUBLESLASH:
1886 op = BINARY_FLOOR_DIVIDE;
1887 break;
1888 default:
1889 com_error(c, PyExc_SystemError,
1890 "com_term: operator not *, /, // or %");
1891 op = 255;
1893 com_addbyte(c, op);
1894 com_pop(c, 1);
1898 static void
1899 com_arith_expr(struct compiling *c, node *n)
1901 int i;
1902 int op;
1903 REQ(n, arith_expr);
1904 com_term(c, CHILD(n, 0));
1905 for (i = 2; i < NCH(n); i += 2) {
1906 com_term(c, CHILD(n, i));
1907 switch (TYPE(CHILD(n, i-1))) {
1908 case PLUS:
1909 op = BINARY_ADD;
1910 break;
1911 case MINUS:
1912 op = BINARY_SUBTRACT;
1913 break;
1914 default:
1915 com_error(c, PyExc_SystemError,
1916 "com_arith_expr: operator not + or -");
1917 op = 255;
1919 com_addbyte(c, op);
1920 com_pop(c, 1);
1924 static void
1925 com_shift_expr(struct compiling *c, node *n)
1927 int i;
1928 int op;
1929 REQ(n, shift_expr);
1930 com_arith_expr(c, CHILD(n, 0));
1931 for (i = 2; i < NCH(n); i += 2) {
1932 com_arith_expr(c, CHILD(n, i));
1933 switch (TYPE(CHILD(n, i-1))) {
1934 case LEFTSHIFT:
1935 op = BINARY_LSHIFT;
1936 break;
1937 case RIGHTSHIFT:
1938 op = BINARY_RSHIFT;
1939 break;
1940 default:
1941 com_error(c, PyExc_SystemError,
1942 "com_shift_expr: operator not << or >>");
1943 op = 255;
1945 com_addbyte(c, op);
1946 com_pop(c, 1);
1950 static void
1951 com_and_expr(struct compiling *c, node *n)
1953 int i;
1954 int op;
1955 REQ(n, and_expr);
1956 com_shift_expr(c, CHILD(n, 0));
1957 for (i = 2; i < NCH(n); i += 2) {
1958 com_shift_expr(c, CHILD(n, i));
1959 if (TYPE(CHILD(n, i-1)) == AMPER) {
1960 op = BINARY_AND;
1962 else {
1963 com_error(c, PyExc_SystemError,
1964 "com_and_expr: operator not &");
1965 op = 255;
1967 com_addbyte(c, op);
1968 com_pop(c, 1);
1972 static void
1973 com_xor_expr(struct compiling *c, node *n)
1975 int i;
1976 int op;
1977 REQ(n, xor_expr);
1978 com_and_expr(c, CHILD(n, 0));
1979 for (i = 2; i < NCH(n); i += 2) {
1980 com_and_expr(c, CHILD(n, i));
1981 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1982 op = BINARY_XOR;
1984 else {
1985 com_error(c, PyExc_SystemError,
1986 "com_xor_expr: operator not ^");
1987 op = 255;
1989 com_addbyte(c, op);
1990 com_pop(c, 1);
1994 static void
1995 com_expr(struct compiling *c, node *n)
1997 int i;
1998 int op;
1999 REQ(n, expr);
2000 com_xor_expr(c, CHILD(n, 0));
2001 for (i = 2; i < NCH(n); i += 2) {
2002 com_xor_expr(c, CHILD(n, i));
2003 if (TYPE(CHILD(n, i-1)) == VBAR) {
2004 op = BINARY_OR;
2006 else {
2007 com_error(c, PyExc_SystemError,
2008 "com_expr: expr operator not |");
2009 op = 255;
2011 com_addbyte(c, op);
2012 com_pop(c, 1);
2016 static enum cmp_op
2017 cmp_type(node *n)
2019 REQ(n, comp_op);
2020 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2021 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2022 if (NCH(n) == 1) {
2023 n = CHILD(n, 0);
2024 switch (TYPE(n)) {
2025 case LESS: return LT;
2026 case GREATER: return GT;
2027 case EQEQUAL: /* == */
2028 case EQUAL: return EQ;
2029 case LESSEQUAL: return LE;
2030 case GREATEREQUAL: return GE;
2031 case NOTEQUAL: return NE; /* <> or != */
2032 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2033 if (strcmp(STR(n), "is") == 0) return IS;
2036 else if (NCH(n) == 2) {
2037 switch (TYPE(CHILD(n, 0))) {
2038 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2039 return NOT_IN;
2040 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2041 return IS_NOT;
2044 return BAD;
2047 static void
2048 com_comparison(struct compiling *c, node *n)
2050 int i;
2051 enum cmp_op op;
2052 int anchor;
2053 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2054 com_expr(c, CHILD(n, 0));
2055 if (NCH(n) == 1)
2056 return;
2058 /****************************************************************
2059 The following code is generated for all but the last
2060 comparison in a chain:
2062 label: on stack: opcode: jump to:
2064 a <code to load b>
2065 a, b DUP_TOP
2066 a, b, b ROT_THREE
2067 b, a, b COMPARE_OP
2068 b, 0-or-1 JUMP_IF_FALSE L1
2069 b, 1 POP_TOP
2072 We are now ready to repeat this sequence for the next
2073 comparison in the chain.
2075 For the last we generate:
2077 b <code to load c>
2078 b, c COMPARE_OP
2079 0-or-1
2081 If there were any jumps to L1 (i.e., there was more than one
2082 comparison), we generate:
2084 0-or-1 JUMP_FORWARD L2
2085 L1: b, 0 ROT_TWO
2086 0, b POP_TOP
2088 L2: 0-or-1
2089 ****************************************************************/
2091 anchor = 0;
2093 for (i = 2; i < NCH(n); i += 2) {
2094 com_expr(c, CHILD(n, i));
2095 if (i+2 < NCH(n)) {
2096 com_addbyte(c, DUP_TOP);
2097 com_push(c, 1);
2098 com_addbyte(c, ROT_THREE);
2100 op = cmp_type(CHILD(n, i-1));
2101 if (op == BAD) {
2102 com_error(c, PyExc_SystemError,
2103 "com_comparison: unknown comparison op");
2105 com_addoparg(c, COMPARE_OP, op);
2106 com_pop(c, 1);
2107 if (i+2 < NCH(n)) {
2108 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2109 com_addbyte(c, POP_TOP);
2110 com_pop(c, 1);
2114 if (anchor) {
2115 int anchor2 = 0;
2116 com_addfwref(c, JUMP_FORWARD, &anchor2);
2117 com_backpatch(c, anchor);
2118 com_addbyte(c, ROT_TWO);
2119 com_addbyte(c, POP_TOP);
2120 com_backpatch(c, anchor2);
2124 static void
2125 com_not_test(struct compiling *c, node *n)
2127 REQ(n, not_test); /* 'not' not_test | comparison */
2128 if (NCH(n) == 1) {
2129 com_comparison(c, CHILD(n, 0));
2131 else {
2132 com_not_test(c, CHILD(n, 1));
2133 com_addbyte(c, UNARY_NOT);
2137 static void
2138 com_and_test(struct compiling *c, node *n)
2140 int i;
2141 int anchor;
2142 REQ(n, and_test); /* not_test ('and' not_test)* */
2143 anchor = 0;
2144 i = 0;
2145 for (;;) {
2146 com_not_test(c, CHILD(n, i));
2147 if ((i += 2) >= NCH(n))
2148 break;
2149 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2150 com_addbyte(c, POP_TOP);
2151 com_pop(c, 1);
2153 if (anchor)
2154 com_backpatch(c, anchor);
2157 static int
2158 com_make_closure(struct compiling *c, PyCodeObject *co)
2160 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2161 if (free == 0)
2162 return 0;
2163 for (i = 0; i < free; ++i) {
2164 /* Bypass com_addop_varname because it will generate
2165 LOAD_DEREF but LOAD_CLOSURE is needed.
2167 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2168 int arg, reftype;
2170 /* Special case: If a class contains a method with a
2171 free variable that has the same name as a method,
2172 the name will be considered free *and* local in the
2173 class. It should be handled by the closure, as
2174 well as by the normal name loookup logic.
2176 reftype = get_ref_type(c, PyString_AS_STRING(name));
2177 if (reftype == CELL)
2178 arg = com_lookup_arg(c->c_cellvars, name);
2179 else /* (reftype == FREE) */
2180 arg = com_lookup_arg(c->c_freevars, name);
2181 if (arg == -1) {
2182 fprintf(stderr, "lookup %s in %s %d %d\n"
2183 "freevars of %s: %s\n",
2184 PyObject_REPR(name),
2185 c->c_name,
2186 reftype, arg,
2187 PyString_AS_STRING(co->co_name),
2188 PyObject_REPR(co->co_freevars));
2189 Py_FatalError("com_make_closure()");
2191 com_addoparg(c, LOAD_CLOSURE, arg);
2194 com_push(c, free);
2195 return 1;
2198 static void
2199 com_test(struct compiling *c, node *n)
2201 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2202 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2203 PyObject *co;
2204 int i, closure;
2205 int ndefs = com_argdefs(c, CHILD(n, 0));
2206 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2207 n->n_lineno);
2208 co = (PyObject *) icompile(CHILD(n, 0), c);
2209 if (co == NULL) {
2210 c->c_errors++;
2211 return;
2213 symtable_exit_scope(c->c_symtable);
2214 i = com_addconst(c, co);
2215 closure = com_make_closure(c, (PyCodeObject *)co);
2216 Py_DECREF(co);
2217 com_addoparg(c, LOAD_CONST, i);
2218 com_push(c, 1);
2219 if (closure)
2220 com_addoparg(c, MAKE_CLOSURE, ndefs);
2221 else
2222 com_addoparg(c, MAKE_FUNCTION, ndefs);
2223 com_pop(c, ndefs);
2225 else {
2226 int anchor = 0;
2227 int i = 0;
2228 for (;;) {
2229 com_and_test(c, CHILD(n, i));
2230 if ((i += 2) >= NCH(n))
2231 break;
2232 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2233 com_addbyte(c, POP_TOP);
2234 com_pop(c, 1);
2236 if (anchor)
2237 com_backpatch(c, anchor);
2241 static void
2242 com_list(struct compiling *c, node *n, int toplevel)
2244 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2245 if (NCH(n) == 1 && !toplevel) {
2246 com_node(c, CHILD(n, 0));
2248 else {
2249 int i;
2250 int len;
2251 len = (NCH(n) + 1) / 2;
2252 for (i = 0; i < NCH(n); i += 2)
2253 com_node(c, CHILD(n, i));
2254 com_addoparg(c, BUILD_TUPLE, len);
2255 com_pop(c, len-1);
2260 /* Begin of assignment compilation */
2263 static void
2264 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2266 com_addbyte(c, DUP_TOP);
2267 com_push(c, 1);
2268 com_addopname(c, LOAD_ATTR, n);
2269 com_node(c, augn);
2270 com_addbyte(c, opcode);
2271 com_pop(c, 1);
2272 com_addbyte(c, ROT_TWO);
2273 com_addopname(c, STORE_ATTR, n);
2274 com_pop(c, 2);
2277 static void
2278 com_assign_attr(struct compiling *c, node *n, int assigning)
2280 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2281 com_pop(c, assigning ? 2 : 1);
2284 static void
2285 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2287 REQ(n, trailer);
2288 switch (TYPE(CHILD(n, 0))) {
2289 case LPAR: /* '(' [exprlist] ')' */
2290 com_error(c, PyExc_SyntaxError,
2291 "can't assign to function call");
2292 break;
2293 case DOT: /* '.' NAME */
2294 if (assigning > OP_APPLY)
2295 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2296 else
2297 com_assign_attr(c, CHILD(n, 1), assigning);
2298 break;
2299 case LSQB: /* '[' subscriptlist ']' */
2300 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2301 break;
2302 default:
2303 com_error(c, PyExc_SystemError, "unknown trailer type");
2307 static void
2308 com_assign_sequence(struct compiling *c, node *n, int assigning)
2310 int i;
2311 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2312 REQ(n, exprlist);
2313 if (assigning) {
2314 i = (NCH(n)+1)/2;
2315 com_addoparg(c, UNPACK_SEQUENCE, i);
2316 com_push(c, i-1);
2318 for (i = 0; i < NCH(n); i += 2)
2319 com_assign(c, CHILD(n, i), assigning, NULL);
2322 static void
2323 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2325 REQ(n, NAME);
2326 com_addop_varname(c, VAR_LOAD, STR(n));
2327 com_push(c, 1);
2328 com_node(c, augn);
2329 com_addbyte(c, opcode);
2330 com_pop(c, 1);
2331 com_assign_name(c, n, OP_ASSIGN);
2334 static void
2335 com_assign_name(struct compiling *c, node *n, int assigning)
2337 REQ(n, NAME);
2338 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2339 if (assigning)
2340 com_pop(c, 1);
2343 static void
2344 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2346 /* Loop to avoid trivial recursion */
2347 for (;;) {
2348 switch (TYPE(n)) {
2350 case exprlist:
2351 case testlist:
2352 if (NCH(n) > 1) {
2353 if (assigning > OP_APPLY) {
2354 com_error(c, PyExc_SyntaxError,
2355 "augmented assign to tuple not possible");
2356 return;
2358 com_assign_sequence(c, n, assigning);
2359 return;
2361 n = CHILD(n, 0);
2362 break;
2364 case test:
2365 case and_test:
2366 case not_test:
2367 case comparison:
2368 case expr:
2369 case xor_expr:
2370 case and_expr:
2371 case shift_expr:
2372 case arith_expr:
2373 case term:
2374 case factor:
2375 if (NCH(n) > 1) {
2376 com_error(c, PyExc_SyntaxError,
2377 "can't assign to operator");
2378 return;
2380 n = CHILD(n, 0);
2381 break;
2383 case power: /* atom trailer* ('**' power)*
2384 ('+'|'-'|'~') factor | atom trailer* */
2385 if (TYPE(CHILD(n, 0)) != atom) {
2386 com_error(c, PyExc_SyntaxError,
2387 "can't assign to operator");
2388 return;
2390 if (NCH(n) > 1) { /* trailer or exponent present */
2391 int i;
2392 com_node(c, CHILD(n, 0));
2393 for (i = 1; i+1 < NCH(n); i++) {
2394 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2395 com_error(c, PyExc_SyntaxError,
2396 "can't assign to operator");
2397 return;
2399 com_apply_trailer(c, CHILD(n, i));
2400 } /* NB i is still alive */
2401 com_assign_trailer(c,
2402 CHILD(n, i), assigning, augn);
2403 return;
2405 n = CHILD(n, 0);
2406 break;
2408 case atom:
2409 switch (TYPE(CHILD(n, 0))) {
2410 case LPAR:
2411 n = CHILD(n, 1);
2412 if (TYPE(n) == RPAR) {
2413 /* XXX Should allow () = () ??? */
2414 com_error(c, PyExc_SyntaxError,
2415 "can't assign to ()");
2416 return;
2418 if (assigning > OP_APPLY) {
2419 com_error(c, PyExc_SyntaxError,
2420 "augmented assign to tuple not possible");
2421 return;
2423 break;
2424 case LSQB:
2425 n = CHILD(n, 1);
2426 if (TYPE(n) == RSQB) {
2427 com_error(c, PyExc_SyntaxError,
2428 "can't assign to []");
2429 return;
2431 if (assigning > OP_APPLY) {
2432 com_error(c, PyExc_SyntaxError,
2433 "augmented assign to list not possible");
2434 return;
2436 if (NCH(n) > 1
2437 && TYPE(CHILD(n, 1)) == list_for) {
2438 com_error(c, PyExc_SyntaxError,
2439 "can't assign to list comprehension");
2440 return;
2442 com_assign_sequence(c, n, assigning);
2443 return;
2444 case NAME:
2445 if (assigning > OP_APPLY)
2446 com_augassign_name(c, CHILD(n, 0),
2447 assigning, augn);
2448 else
2449 com_assign_name(c, CHILD(n, 0),
2450 assigning);
2451 return;
2452 default:
2453 com_error(c, PyExc_SyntaxError,
2454 "can't assign to literal");
2455 return;
2457 break;
2459 case lambdef:
2460 com_error(c, PyExc_SyntaxError,
2461 "can't assign to lambda");
2462 return;
2464 default:
2465 com_error(c, PyExc_SystemError,
2466 "com_assign: bad node");
2467 return;
2473 static void
2474 com_augassign(struct compiling *c, node *n)
2476 int opcode;
2478 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2479 case '+': opcode = INPLACE_ADD; break;
2480 case '-': opcode = INPLACE_SUBTRACT; break;
2481 case '/':
2482 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2483 opcode = INPLACE_FLOOR_DIVIDE;
2484 else if (c->c_flags & CO_FUTURE_DIVISION)
2485 opcode = INPLACE_TRUE_DIVIDE;
2486 else
2487 opcode = INPLACE_DIVIDE;
2488 break;
2489 case '%': opcode = INPLACE_MODULO; break;
2490 case '<': opcode = INPLACE_LSHIFT; break;
2491 case '>': opcode = INPLACE_RSHIFT; break;
2492 case '&': opcode = INPLACE_AND; break;
2493 case '^': opcode = INPLACE_XOR; break;
2494 case '|': opcode = INPLACE_OR; break;
2495 case '*':
2496 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2497 opcode = INPLACE_POWER;
2498 else
2499 opcode = INPLACE_MULTIPLY;
2500 break;
2501 default:
2502 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2503 return;
2505 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2508 static void
2509 com_expr_stmt(struct compiling *c, node *n)
2511 REQ(n, expr_stmt);
2512 /* testlist (('=' testlist)* | augassign testlist) */
2513 /* Forget it if we have just a doc string here */
2514 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2515 return;
2516 if (NCH(n) == 1) {
2517 com_node(c, CHILD(n, NCH(n)-1));
2518 if (c->c_interactive)
2519 com_addbyte(c, PRINT_EXPR);
2520 else
2521 com_addbyte(c, POP_TOP);
2522 com_pop(c, 1);
2524 else if (TYPE(CHILD(n,1)) == augassign)
2525 com_augassign(c, n);
2526 else {
2527 int i;
2528 com_node(c, CHILD(n, NCH(n)-1));
2529 for (i = 0; i < NCH(n)-2; i+=2) {
2530 if (i+2 < NCH(n)-2) {
2531 com_addbyte(c, DUP_TOP);
2532 com_push(c, 1);
2534 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2539 static void
2540 com_assert_stmt(struct compiling *c, node *n)
2542 int a = 0, b = 0;
2543 int i;
2544 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2545 /* Generate code like for
2547 if __debug__:
2548 if not <test>:
2549 raise AssertionError [, <message>]
2551 where <message> is the second test, if present.
2554 if (Py_OptimizeFlag)
2555 return;
2556 com_addop_name(c, LOAD_GLOBAL, "__debug__");
2557 com_push(c, 1);
2558 com_addfwref(c, JUMP_IF_FALSE, &a);
2559 com_addbyte(c, POP_TOP);
2560 com_pop(c, 1);
2561 com_node(c, CHILD(n, 1));
2562 com_addfwref(c, JUMP_IF_TRUE, &b);
2563 com_addbyte(c, POP_TOP);
2564 com_pop(c, 1);
2565 /* Raise that exception! */
2566 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2567 com_push(c, 1);
2568 i = NCH(n)/2; /* Either 2 or 4 */
2569 if (i > 1)
2570 com_node(c, CHILD(n, 3));
2571 com_addoparg(c, RAISE_VARARGS, i);
2572 com_pop(c, i);
2573 /* The interpreter does not fall through */
2574 /* All jumps converge here */
2575 com_backpatch(c, a);
2576 com_backpatch(c, b);
2577 com_addbyte(c, POP_TOP);
2580 static void
2581 com_print_stmt(struct compiling *c, node *n)
2583 int i = 1;
2584 node* stream = NULL;
2586 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2588 /* are we using the extended print form? */
2589 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2590 stream = CHILD(n, 2);
2591 com_node(c, stream);
2592 /* stack: [...] => [... stream] */
2593 com_push(c, 1);
2594 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2595 i = 4;
2596 else
2597 i = 3;
2599 for (; i < NCH(n); i += 2) {
2600 if (stream != NULL) {
2601 com_addbyte(c, DUP_TOP);
2602 /* stack: [stream] => [stream stream] */
2603 com_push(c, 1);
2604 com_node(c, CHILD(n, i));
2605 /* stack: [stream stream] => [stream stream obj] */
2606 com_addbyte(c, ROT_TWO);
2607 /* stack: [stream stream obj] => [stream obj stream] */
2608 com_addbyte(c, PRINT_ITEM_TO);
2609 /* stack: [stream obj stream] => [stream] */
2610 com_pop(c, 2);
2612 else {
2613 com_node(c, CHILD(n, i));
2614 /* stack: [...] => [... obj] */
2615 com_addbyte(c, PRINT_ITEM);
2616 com_pop(c, 1);
2619 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2620 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2621 if (stream != NULL) {
2622 /* must pop the extra stream object off the stack */
2623 com_addbyte(c, POP_TOP);
2624 /* stack: [... stream] => [...] */
2625 com_pop(c, 1);
2628 else {
2629 if (stream != NULL) {
2630 /* this consumes the last stream object on stack */
2631 com_addbyte(c, PRINT_NEWLINE_TO);
2632 /* stack: [... stream] => [...] */
2633 com_pop(c, 1);
2635 else
2636 com_addbyte(c, PRINT_NEWLINE);
2640 static void
2641 com_return_stmt(struct compiling *c, node *n)
2643 REQ(n, return_stmt); /* 'return' [testlist] */
2644 if (!c->c_infunction) {
2645 com_error(c, PyExc_SyntaxError, "'return' outside function");
2647 if (c->c_flags & CO_GENERATOR) {
2648 if (NCH(n) > 1) {
2649 com_error(c, PyExc_SyntaxError,
2650 "'return' with argument inside generator");
2653 if (NCH(n) < 2) {
2654 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2655 com_push(c, 1);
2657 else
2658 com_node(c, CHILD(n, 1));
2659 com_addbyte(c, RETURN_VALUE);
2660 com_pop(c, 1);
2663 static void
2664 com_yield_stmt(struct compiling *c, node *n)
2666 int i;
2667 REQ(n, yield_stmt); /* 'yield' testlist */
2668 if (!c->c_infunction) {
2669 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2672 for (i = 0; i < c->c_nblocks; ++i) {
2673 if (c->c_block[i] == SETUP_FINALLY) {
2674 com_error(c, PyExc_SyntaxError,
2675 "'yield' not allowed in a 'try' block "
2676 "with a 'finally' clause");
2677 return;
2680 com_node(c, CHILD(n, 1));
2681 com_addbyte(c, YIELD_VALUE);
2682 com_pop(c, 1);
2685 static void
2686 com_raise_stmt(struct compiling *c, node *n)
2688 int i;
2689 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2690 if (NCH(n) > 1) {
2691 com_node(c, CHILD(n, 1));
2692 if (NCH(n) > 3) {
2693 com_node(c, CHILD(n, 3));
2694 if (NCH(n) > 5)
2695 com_node(c, CHILD(n, 5));
2698 i = NCH(n)/2;
2699 com_addoparg(c, RAISE_VARARGS, i);
2700 com_pop(c, i);
2703 static void
2704 com_from_import(struct compiling *c, node *n)
2706 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2707 com_push(c, 1);
2708 if (NCH(n) > 1) {
2709 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2710 com_error(c, PyExc_SyntaxError, "invalid syntax");
2711 return;
2713 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2714 } else
2715 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2716 com_pop(c, 1);
2719 static void
2720 com_import_stmt(struct compiling *c, node *n)
2722 int i;
2723 REQ(n, import_stmt);
2724 /* 'import' dotted_name (',' dotted_name)* |
2725 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2726 if (STR(CHILD(n, 0))[0] == 'f') {
2727 PyObject *tup;
2728 /* 'from' dotted_name 'import' ... */
2729 REQ(CHILD(n, 1), dotted_name);
2731 if (TYPE(CHILD(n, 3)) == STAR) {
2732 tup = Py_BuildValue("(s)", "*");
2733 } else {
2734 tup = PyTuple_New((NCH(n) - 2)/2);
2735 for (i = 3; i < NCH(n); i += 2) {
2736 PyTuple_SET_ITEM(tup, (i-3)/2,
2737 PyString_FromString(STR(
2738 CHILD(CHILD(n, i), 0))));
2741 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2742 Py_DECREF(tup);
2743 com_push(c, 1);
2744 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2745 if (TYPE(CHILD(n, 3)) == STAR)
2746 com_addbyte(c, IMPORT_STAR);
2747 else {
2748 for (i = 3; i < NCH(n); i += 2)
2749 com_from_import(c, CHILD(n, i));
2750 com_addbyte(c, POP_TOP);
2752 com_pop(c, 1);
2754 else {
2755 /* 'import' ... */
2756 for (i = 1; i < NCH(n); i += 2) {
2757 node *subn = CHILD(n, i);
2758 REQ(subn, dotted_as_name);
2759 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2760 com_push(c, 1);
2761 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2762 if (NCH(subn) > 1) {
2763 int j;
2764 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
2765 com_error(c, PyExc_SyntaxError,
2766 "invalid syntax");
2767 return;
2769 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2770 com_addopname(c, LOAD_ATTR,
2771 CHILD(CHILD(subn, 0),
2772 j));
2773 com_addop_varname(c, VAR_STORE,
2774 STR(CHILD(subn, 2)));
2775 } else
2776 com_addop_varname(c, VAR_STORE,
2777 STR(CHILD(CHILD(subn, 0),
2778 0)));
2779 com_pop(c, 1);
2784 static void
2785 com_exec_stmt(struct compiling *c, node *n)
2787 REQ(n, exec_stmt);
2788 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2789 com_node(c, CHILD(n, 1));
2790 if (NCH(n) >= 4)
2791 com_node(c, CHILD(n, 3));
2792 else {
2793 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2794 com_push(c, 1);
2796 if (NCH(n) >= 6)
2797 com_node(c, CHILD(n, 5));
2798 else {
2799 com_addbyte(c, DUP_TOP);
2800 com_push(c, 1);
2802 com_addbyte(c, EXEC_STMT);
2803 com_pop(c, 3);
2806 static int
2807 is_constant_false(struct compiling *c, node *n)
2809 PyObject *v;
2810 int i;
2811 /* argument c will be NULL when called from symtable_node() */
2813 /* Label to avoid tail recursion */
2814 next:
2815 switch (TYPE(n)) {
2817 case suite:
2818 if (NCH(n) == 1) {
2819 n = CHILD(n, 0);
2820 goto next;
2822 /* Fall through */
2823 case file_input:
2824 for (i = 0; i < NCH(n); i++) {
2825 node *ch = CHILD(n, i);
2826 if (TYPE(ch) == stmt) {
2827 n = ch;
2828 goto next;
2831 break;
2833 case stmt:
2834 case simple_stmt:
2835 case small_stmt:
2836 n = CHILD(n, 0);
2837 goto next;
2839 case expr_stmt:
2840 case testlist:
2841 case test:
2842 case and_test:
2843 case not_test:
2844 case comparison:
2845 case expr:
2846 case xor_expr:
2847 case and_expr:
2848 case shift_expr:
2849 case arith_expr:
2850 case term:
2851 case factor:
2852 case power:
2853 case atom:
2854 if (NCH(n) == 1) {
2855 n = CHILD(n, 0);
2856 goto next;
2858 break;
2860 case NAME:
2861 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2862 return 1;
2863 break;
2865 case NUMBER:
2866 v = parsenumber(c, STR(n));
2867 if (v == NULL) {
2868 PyErr_Clear();
2869 break;
2871 i = PyObject_IsTrue(v);
2872 Py_DECREF(v);
2873 return i == 0;
2875 case STRING:
2876 v = parsestr(STR(n));
2877 if (v == NULL) {
2878 PyErr_Clear();
2879 break;
2881 i = PyObject_IsTrue(v);
2882 Py_DECREF(v);
2883 return i == 0;
2886 return 0;
2890 /* Look under n for a return stmt with an expression.
2891 * This hack is used to find illegal returns under "if 0:" blocks in
2892 * functions already known to be generators (as determined by the symtable
2893 * pass).
2894 * Return the offending return node if found, else NULL.
2896 static node *
2897 look_for_offending_return(node *n)
2899 int i;
2901 for (i = 0; i < NCH(n); ++i) {
2902 node *kid = CHILD(n, i);
2904 switch (TYPE(kid)) {
2905 case classdef:
2906 case funcdef:
2907 case lambdef:
2908 /* Stuff in nested functions & classes doesn't
2909 affect the code block we started in. */
2910 return NULL;
2912 case return_stmt:
2913 if (NCH(kid) > 1)
2914 return kid;
2915 break;
2917 default: {
2918 node *bad = look_for_offending_return(kid);
2919 if (bad != NULL)
2920 return bad;
2925 return NULL;
2928 static void
2929 com_if_stmt(struct compiling *c, node *n)
2931 int i;
2932 int anchor = 0;
2933 REQ(n, if_stmt);
2934 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2935 for (i = 0; i+3 < NCH(n); i+=4) {
2936 int a = 0;
2937 node *ch = CHILD(n, i+1);
2938 if (is_constant_false(c, ch)) {
2939 /* We're going to skip this block. However, if this
2940 is a generator, we have to check the dead code
2941 anyway to make sure there aren't any return stmts
2942 with expressions, in the same scope. */
2943 if (c->c_flags & CO_GENERATOR) {
2944 node *p = look_for_offending_return(n);
2945 if (p != NULL) {
2946 int savelineno = c->c_lineno;
2947 c->c_lineno = p->n_lineno;
2948 com_error(c, PyExc_SyntaxError,
2949 "'return' with argument "
2950 "inside generator");
2951 c->c_lineno = savelineno;
2954 continue;
2956 if (i > 0)
2957 com_addoparg(c, SET_LINENO, ch->n_lineno);
2958 com_node(c, ch);
2959 com_addfwref(c, JUMP_IF_FALSE, &a);
2960 com_addbyte(c, POP_TOP);
2961 com_pop(c, 1);
2962 com_node(c, CHILD(n, i+3));
2963 com_addfwref(c, JUMP_FORWARD, &anchor);
2964 com_backpatch(c, a);
2965 /* We jump here with an extra entry which we now pop */
2966 com_addbyte(c, POP_TOP);
2968 if (i+2 < NCH(n))
2969 com_node(c, CHILD(n, i+2));
2970 if (anchor)
2971 com_backpatch(c, anchor);
2974 static void
2975 com_while_stmt(struct compiling *c, node *n)
2977 int break_anchor = 0;
2978 int anchor = 0;
2979 int save_begin = c->c_begin;
2980 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2981 com_addfwref(c, SETUP_LOOP, &break_anchor);
2982 block_push(c, SETUP_LOOP);
2983 c->c_begin = c->c_nexti;
2984 com_addoparg(c, SET_LINENO, n->n_lineno);
2985 com_node(c, CHILD(n, 1));
2986 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2987 com_addbyte(c, POP_TOP);
2988 com_pop(c, 1);
2989 c->c_loops++;
2990 com_node(c, CHILD(n, 3));
2991 c->c_loops--;
2992 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2993 c->c_begin = save_begin;
2994 com_backpatch(c, anchor);
2995 /* We jump here with one entry more on the stack */
2996 com_addbyte(c, POP_TOP);
2997 com_addbyte(c, POP_BLOCK);
2998 block_pop(c, SETUP_LOOP);
2999 if (NCH(n) > 4)
3000 com_node(c, CHILD(n, 6));
3001 com_backpatch(c, break_anchor);
3004 static void
3005 com_for_stmt(struct compiling *c, node *n)
3007 int break_anchor = 0;
3008 int anchor = 0;
3009 int save_begin = c->c_begin;
3010 REQ(n, for_stmt);
3011 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3012 com_addfwref(c, SETUP_LOOP, &break_anchor);
3013 block_push(c, SETUP_LOOP);
3014 com_node(c, CHILD(n, 3));
3015 com_addbyte(c, GET_ITER);
3016 c->c_begin = c->c_nexti;
3017 com_addoparg(c, SET_LINENO, n->n_lineno);
3018 com_addfwref(c, FOR_ITER, &anchor);
3019 com_push(c, 1);
3020 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
3021 c->c_loops++;
3022 com_node(c, CHILD(n, 5));
3023 c->c_loops--;
3024 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3025 c->c_begin = save_begin;
3026 com_backpatch(c, anchor);
3027 com_pop(c, 1); /* FOR_ITER has popped this */
3028 com_addbyte(c, POP_BLOCK);
3029 block_pop(c, SETUP_LOOP);
3030 if (NCH(n) > 8)
3031 com_node(c, CHILD(n, 8));
3032 com_backpatch(c, break_anchor);
3035 /* Code generated for "try: S finally: Sf" is as follows:
3037 SETUP_FINALLY L
3038 <code for S>
3039 POP_BLOCK
3040 LOAD_CONST <nil>
3041 L: <code for Sf>
3042 END_FINALLY
3044 The special instructions use the block stack. Each block
3045 stack entry contains the instruction that created it (here
3046 SETUP_FINALLY), the level of the value stack at the time the
3047 block stack entry was created, and a label (here L).
3049 SETUP_FINALLY:
3050 Pushes the current value stack level and the label
3051 onto the block stack.
3052 POP_BLOCK:
3053 Pops en entry from the block stack, and pops the value
3054 stack until its level is the same as indicated on the
3055 block stack. (The label is ignored.)
3056 END_FINALLY:
3057 Pops a variable number of entries from the *value* stack
3058 and re-raises the exception they specify. The number of
3059 entries popped depends on the (pseudo) exception type.
3061 The block stack is unwound when an exception is raised:
3062 when a SETUP_FINALLY entry is found, the exception is pushed
3063 onto the value stack (and the exception condition is cleared),
3064 and the interpreter jumps to the label gotten from the block
3065 stack.
3067 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3068 (The contents of the value stack is shown in [], with the top
3069 at the right; 'tb' is trace-back info, 'val' the exception's
3070 associated value, and 'exc' the exception.)
3072 Value stack Label Instruction Argument
3073 [] SETUP_EXCEPT L1
3074 [] <code for S>
3075 [] POP_BLOCK
3076 [] JUMP_FORWARD L0
3078 [tb, val, exc] L1: DUP )
3079 [tb, val, exc, exc] <evaluate E1> )
3080 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3081 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3082 [tb, val, exc, 1] POP )
3083 [tb, val, exc] POP
3084 [tb, val] <assign to V1> (or POP if no V1)
3085 [tb] POP
3086 [] <code for S1>
3087 JUMP_FORWARD L0
3089 [tb, val, exc, 0] L2: POP
3090 [tb, val, exc] DUP
3091 .............................etc.......................
3093 [tb, val, exc, 0] Ln+1: POP
3094 [tb, val, exc] END_FINALLY # re-raise exception
3096 [] L0: <next statement>
3098 Of course, parts are not generated if Vi or Ei is not present.
3101 static void
3102 com_try_except(struct compiling *c, node *n)
3104 int except_anchor = 0;
3105 int end_anchor = 0;
3106 int else_anchor = 0;
3107 int i;
3108 node *ch;
3110 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3111 block_push(c, SETUP_EXCEPT);
3112 com_node(c, CHILD(n, 2));
3113 com_addbyte(c, POP_BLOCK);
3114 block_pop(c, SETUP_EXCEPT);
3115 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3116 com_backpatch(c, except_anchor);
3117 for (i = 3;
3118 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3119 i += 3) {
3120 /* except_clause: 'except' [expr [',' var]] */
3121 if (except_anchor == 0) {
3122 com_error(c, PyExc_SyntaxError,
3123 "default 'except:' must be last");
3124 break;
3126 except_anchor = 0;
3127 com_push(c, 3); /* tb, val, exc pushed by exception */
3128 com_addoparg(c, SET_LINENO, ch->n_lineno);
3129 if (NCH(ch) > 1) {
3130 com_addbyte(c, DUP_TOP);
3131 com_push(c, 1);
3132 com_node(c, CHILD(ch, 1));
3133 com_addoparg(c, COMPARE_OP, EXC_MATCH);
3134 com_pop(c, 1);
3135 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3136 com_addbyte(c, POP_TOP);
3137 com_pop(c, 1);
3139 com_addbyte(c, POP_TOP);
3140 com_pop(c, 1);
3141 if (NCH(ch) > 3)
3142 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3143 else {
3144 com_addbyte(c, POP_TOP);
3145 com_pop(c, 1);
3147 com_addbyte(c, POP_TOP);
3148 com_pop(c, 1);
3149 com_node(c, CHILD(n, i+2));
3150 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3151 if (except_anchor) {
3152 com_backpatch(c, except_anchor);
3153 /* We come in with [tb, val, exc, 0] on the
3154 stack; one pop and it's the same as
3155 expected at the start of the loop */
3156 com_addbyte(c, POP_TOP);
3159 /* We actually come in here with [tb, val, exc] but the
3160 END_FINALLY will zap those and jump around.
3161 The c_stacklevel does not reflect them so we need not pop
3162 anything. */
3163 com_addbyte(c, END_FINALLY);
3164 com_backpatch(c, else_anchor);
3165 if (i < NCH(n))
3166 com_node(c, CHILD(n, i+2));
3167 com_backpatch(c, end_anchor);
3170 static void
3171 com_try_finally(struct compiling *c, node *n)
3173 int finally_anchor = 0;
3174 node *ch;
3176 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3177 block_push(c, SETUP_FINALLY);
3178 com_node(c, CHILD(n, 2));
3179 com_addbyte(c, POP_BLOCK);
3180 block_pop(c, SETUP_FINALLY);
3181 block_push(c, END_FINALLY);
3182 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3183 /* While the generated code pushes only one item,
3184 the try-finally handling can enter here with
3185 up to three items. OK, here are the details:
3186 3 for an exception, 2 for RETURN, 1 for BREAK. */
3187 com_push(c, 3);
3188 com_backpatch(c, finally_anchor);
3189 ch = CHILD(n, NCH(n)-1);
3190 com_addoparg(c, SET_LINENO, ch->n_lineno);
3191 com_node(c, ch);
3192 com_addbyte(c, END_FINALLY);
3193 block_pop(c, END_FINALLY);
3194 com_pop(c, 3); /* Matches the com_push above */
3197 static void
3198 com_try_stmt(struct compiling *c, node *n)
3200 REQ(n, try_stmt);
3201 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3202 | 'try' ':' suite 'finally' ':' suite */
3203 if (TYPE(CHILD(n, 3)) != except_clause)
3204 com_try_finally(c, n);
3205 else
3206 com_try_except(c, n);
3209 static node *
3210 get_rawdocstring(node *n)
3212 int i;
3214 /* Label to avoid tail recursion */
3215 next:
3216 switch (TYPE(n)) {
3218 case suite:
3219 if (NCH(n) == 1) {
3220 n = CHILD(n, 0);
3221 goto next;
3223 /* Fall through */
3224 case file_input:
3225 for (i = 0; i < NCH(n); i++) {
3226 node *ch = CHILD(n, i);
3227 if (TYPE(ch) == stmt) {
3228 n = ch;
3229 goto next;
3232 break;
3234 case stmt:
3235 case simple_stmt:
3236 case small_stmt:
3237 n = CHILD(n, 0);
3238 goto next;
3240 case expr_stmt:
3241 case testlist:
3242 case test:
3243 case and_test:
3244 case not_test:
3245 case comparison:
3246 case expr:
3247 case xor_expr:
3248 case and_expr:
3249 case shift_expr:
3250 case arith_expr:
3251 case term:
3252 case factor:
3253 case power:
3254 if (NCH(n) == 1) {
3255 n = CHILD(n, 0);
3256 goto next;
3258 break;
3260 case atom:
3261 if (TYPE(CHILD(n, 0)) == STRING)
3262 return n;
3263 break;
3266 return NULL;
3269 static PyObject *
3270 get_docstring(node *n)
3272 /* Don't generate doc-strings if run with -OO */
3273 if (Py_OptimizeFlag > 1)
3274 return NULL;
3275 n = get_rawdocstring(n);
3276 if (n == NULL)
3277 return NULL;
3278 return parsestrplus(n);
3281 static void
3282 com_suite(struct compiling *c, node *n)
3284 REQ(n, suite);
3285 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3286 if (NCH(n) == 1) {
3287 com_node(c, CHILD(n, 0));
3289 else {
3290 int i;
3291 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3292 node *ch = CHILD(n, i);
3293 if (TYPE(ch) == stmt)
3294 com_node(c, ch);
3299 /* ARGSUSED */
3300 static void
3301 com_continue_stmt(struct compiling *c, node *n)
3303 int i = c->c_nblocks;
3304 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3305 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3307 else if (i <= 0) {
3308 /* at the outer level */
3309 com_error(c, PyExc_SyntaxError,
3310 "'continue' not properly in loop");
3312 else {
3313 int j;
3314 for (j = i-1; j >= 0; --j) {
3315 if (c->c_block[j] == SETUP_LOOP)
3316 break;
3318 if (j >= 0) {
3319 /* there is a loop, but something interferes */
3320 for (; i > j; --i) {
3321 if (c->c_block[i] == SETUP_EXCEPT ||
3322 c->c_block[i] == SETUP_FINALLY) {
3323 com_addoparg(c, CONTINUE_LOOP,
3324 c->c_begin);
3325 return;
3327 if (c->c_block[i] == END_FINALLY) {
3328 com_error(c, PyExc_SyntaxError,
3329 "'continue' not supported inside 'finally' clause");
3330 return;
3334 com_error(c, PyExc_SyntaxError,
3335 "'continue' not properly in loop");
3337 /* XXX Could allow it inside a 'finally' clause
3338 XXX if we could pop the exception still on the stack */
3341 static int
3342 com_argdefs(struct compiling *c, node *n)
3344 int i, nch, nargs, ndefs;
3345 if (TYPE(n) == lambdef) {
3346 /* lambdef: 'lambda' [varargslist] ':' test */
3347 n = CHILD(n, 1);
3349 else {
3350 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3351 n = CHILD(n, 2);
3352 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3353 n = CHILD(n, 1);
3355 if (TYPE(n) != varargslist)
3356 return 0;
3357 /* varargslist:
3358 (fpdef ['=' test] ',')* '*' ....... |
3359 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3360 nch = NCH(n);
3361 nargs = 0;
3362 ndefs = 0;
3363 for (i = 0; i < nch; i++) {
3364 int t;
3365 if (TYPE(CHILD(n, i)) == STAR ||
3366 TYPE(CHILD(n, i)) == DOUBLESTAR)
3367 break;
3368 nargs++;
3369 i++;
3370 if (i >= nch)
3371 t = RPAR; /* Anything except EQUAL or COMMA */
3372 else
3373 t = TYPE(CHILD(n, i));
3374 if (t == EQUAL) {
3375 i++;
3376 ndefs++;
3377 com_node(c, CHILD(n, i));
3378 i++;
3379 if (i >= nch)
3380 break;
3381 t = TYPE(CHILD(n, i));
3383 else {
3384 /* Treat "(a=1, b)" as an error */
3385 if (ndefs)
3386 com_error(c, PyExc_SyntaxError,
3387 "non-default argument follows default argument");
3389 if (t != COMMA)
3390 break;
3392 return ndefs;
3395 static void
3396 com_funcdef(struct compiling *c, node *n)
3398 PyObject *co;
3399 int ndefs;
3400 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3401 ndefs = com_argdefs(c, n);
3402 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3403 n->n_lineno);
3404 co = (PyObject *)icompile(n, c);
3405 symtable_exit_scope(c->c_symtable);
3406 if (co == NULL)
3407 c->c_errors++;
3408 else {
3409 int closure = com_make_closure(c, (PyCodeObject *)co);
3410 int i = com_addconst(c, co);
3411 com_addoparg(c, LOAD_CONST, i);
3412 com_push(c, 1);
3413 if (closure)
3414 com_addoparg(c, MAKE_CLOSURE, ndefs);
3415 else
3416 com_addoparg(c, MAKE_FUNCTION, ndefs);
3417 com_pop(c, ndefs);
3418 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3419 com_pop(c, 1);
3420 Py_DECREF(co);
3424 static void
3425 com_bases(struct compiling *c, node *n)
3427 int i;
3428 REQ(n, testlist);
3429 /* testlist: test (',' test)* [','] */
3430 for (i = 0; i < NCH(n); i += 2)
3431 com_node(c, CHILD(n, i));
3432 i = (NCH(n)+1) / 2;
3433 com_addoparg(c, BUILD_TUPLE, i);
3434 com_pop(c, i-1);
3437 static void
3438 com_classdef(struct compiling *c, node *n)
3440 int i;
3441 PyObject *co, *v;
3442 char *name;
3444 REQ(n, classdef);
3445 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3446 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3447 c->c_errors++;
3448 return;
3450 /* Push the class name on the stack */
3451 i = com_addconst(c, v);
3452 com_addoparg(c, LOAD_CONST, i);
3453 com_push(c, 1);
3454 Py_DECREF(v);
3455 /* Push the tuple of base classes on the stack */
3456 if (TYPE(CHILD(n, 2)) != LPAR) {
3457 com_addoparg(c, BUILD_TUPLE, 0);
3458 com_push(c, 1);
3460 else
3461 com_bases(c, CHILD(n, 3));
3462 name = STR(CHILD(n, 1));
3463 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3464 co = (PyObject *)icompile(n, c);
3465 symtable_exit_scope(c->c_symtable);
3466 if (co == NULL)
3467 c->c_errors++;
3468 else {
3469 int closure = com_make_closure(c, (PyCodeObject *)co);
3470 i = com_addconst(c, co);
3471 com_addoparg(c, LOAD_CONST, i);
3472 com_push(c, 1);
3473 if (closure)
3474 com_addoparg(c, MAKE_CLOSURE, 0);
3475 else
3476 com_addoparg(c, MAKE_FUNCTION, 0);
3477 com_addoparg(c, CALL_FUNCTION, 0);
3478 com_addbyte(c, BUILD_CLASS);
3479 com_pop(c, 2);
3480 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3481 Py_DECREF(co);
3485 static void
3486 com_node(struct compiling *c, node *n)
3488 loop:
3489 if (c->c_errors)
3490 return;
3491 switch (TYPE(n)) {
3493 /* Definition nodes */
3495 case funcdef:
3496 com_funcdef(c, n);
3497 break;
3498 case classdef:
3499 com_classdef(c, n);
3500 break;
3502 /* Trivial parse tree nodes */
3504 case stmt:
3505 case small_stmt:
3506 case flow_stmt:
3507 n = CHILD(n, 0);
3508 goto loop;
3510 case simple_stmt:
3511 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3512 com_addoparg(c, SET_LINENO, n->n_lineno);
3514 int i;
3515 for (i = 0; i < NCH(n)-1; i += 2)
3516 com_node(c, CHILD(n, i));
3518 break;
3520 case compound_stmt:
3521 com_addoparg(c, SET_LINENO, n->n_lineno);
3522 n = CHILD(n, 0);
3523 goto loop;
3525 /* Statement nodes */
3527 case expr_stmt:
3528 com_expr_stmt(c, n);
3529 break;
3530 case print_stmt:
3531 com_print_stmt(c, n);
3532 break;
3533 case del_stmt: /* 'del' exprlist */
3534 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3535 break;
3536 case pass_stmt:
3537 break;
3538 case break_stmt:
3539 if (c->c_loops == 0) {
3540 com_error(c, PyExc_SyntaxError,
3541 "'break' outside loop");
3543 com_addbyte(c, BREAK_LOOP);
3544 break;
3545 case continue_stmt:
3546 com_continue_stmt(c, n);
3547 break;
3548 case return_stmt:
3549 com_return_stmt(c, n);
3550 break;
3551 case yield_stmt:
3552 com_yield_stmt(c, n);
3553 break;
3554 case raise_stmt:
3555 com_raise_stmt(c, n);
3556 break;
3557 case import_stmt:
3558 com_import_stmt(c, n);
3559 break;
3560 case global_stmt:
3561 break;
3562 case exec_stmt:
3563 com_exec_stmt(c, n);
3564 break;
3565 case assert_stmt:
3566 com_assert_stmt(c, n);
3567 break;
3568 case if_stmt:
3569 com_if_stmt(c, n);
3570 break;
3571 case while_stmt:
3572 com_while_stmt(c, n);
3573 break;
3574 case for_stmt:
3575 com_for_stmt(c, n);
3576 break;
3577 case try_stmt:
3578 com_try_stmt(c, n);
3579 break;
3580 case suite:
3581 com_suite(c, n);
3582 break;
3584 /* Expression nodes */
3586 case testlist:
3587 com_list(c, n, 0);
3588 break;
3589 case test:
3590 com_test(c, n);
3591 break;
3592 case and_test:
3593 com_and_test(c, n);
3594 break;
3595 case not_test:
3596 com_not_test(c, n);
3597 break;
3598 case comparison:
3599 com_comparison(c, n);
3600 break;
3601 case exprlist:
3602 com_list(c, n, 0);
3603 break;
3604 case expr:
3605 com_expr(c, n);
3606 break;
3607 case xor_expr:
3608 com_xor_expr(c, n);
3609 break;
3610 case and_expr:
3611 com_and_expr(c, n);
3612 break;
3613 case shift_expr:
3614 com_shift_expr(c, n);
3615 break;
3616 case arith_expr:
3617 com_arith_expr(c, n);
3618 break;
3619 case term:
3620 com_term(c, n);
3621 break;
3622 case factor:
3623 com_factor(c, n);
3624 break;
3625 case power:
3626 com_power(c, n);
3627 break;
3628 case atom:
3629 com_atom(c, n);
3630 break;
3632 default:
3633 com_error(c, PyExc_SystemError,
3634 "com_node: unexpected node type");
3638 static void com_fplist(struct compiling *, node *);
3640 static void
3641 com_fpdef(struct compiling *c, node *n)
3643 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3644 if (TYPE(CHILD(n, 0)) == LPAR)
3645 com_fplist(c, CHILD(n, 1));
3646 else {
3647 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3648 com_pop(c, 1);
3652 static void
3653 com_fplist(struct compiling *c, node *n)
3655 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3656 if (NCH(n) == 1) {
3657 com_fpdef(c, CHILD(n, 0));
3659 else {
3660 int i = (NCH(n)+1)/2;
3661 com_addoparg(c, UNPACK_SEQUENCE, i);
3662 com_push(c, i-1);
3663 for (i = 0; i < NCH(n); i += 2)
3664 com_fpdef(c, CHILD(n, i));
3668 static void
3669 com_arglist(struct compiling *c, node *n)
3671 int nch, i, narg;
3672 int complex = 0;
3673 char nbuf[10];
3674 REQ(n, varargslist);
3675 /* varargslist:
3676 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3677 nch = NCH(n);
3678 /* Enter all arguments in table of locals */
3679 for (i = 0, narg = 0; i < nch; i++) {
3680 node *ch = CHILD(n, i);
3681 node *fp;
3682 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3683 break;
3684 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3685 fp = CHILD(ch, 0);
3686 if (TYPE(fp) != NAME) {
3687 sprintf(nbuf, ".%d", i);
3688 complex = 1;
3690 narg++;
3691 /* all name updates handled by symtable */
3692 if (++i >= nch)
3693 break;
3694 ch = CHILD(n, i);
3695 if (TYPE(ch) == EQUAL)
3696 i += 2;
3697 else
3698 REQ(ch, COMMA);
3700 if (complex) {
3701 /* Generate code for complex arguments only after
3702 having counted the simple arguments */
3703 int ilocal = 0;
3704 for (i = 0; i < nch; i++) {
3705 node *ch = CHILD(n, i);
3706 node *fp;
3707 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3708 break;
3709 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3710 fp = CHILD(ch, 0);
3711 if (TYPE(fp) != NAME) {
3712 com_addoparg(c, LOAD_FAST, ilocal);
3713 com_push(c, 1);
3714 com_fpdef(c, ch);
3716 ilocal++;
3717 if (++i >= nch)
3718 break;
3719 ch = CHILD(n, i);
3720 if (TYPE(ch) == EQUAL)
3721 i += 2;
3722 else
3723 REQ(ch, COMMA);
3728 static void
3729 com_file_input(struct compiling *c, node *n)
3731 int i;
3732 PyObject *doc;
3733 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3734 doc = get_docstring(n);
3735 if (doc != NULL) {
3736 int i = com_addconst(c, doc);
3737 Py_DECREF(doc);
3738 com_addoparg(c, LOAD_CONST, i);
3739 com_push(c, 1);
3740 com_addop_name(c, STORE_NAME, "__doc__");
3741 com_pop(c, 1);
3743 for (i = 0; i < NCH(n); i++) {
3744 node *ch = CHILD(n, i);
3745 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3746 com_node(c, ch);
3750 /* Top-level compile-node interface */
3752 static void
3753 compile_funcdef(struct compiling *c, node *n)
3755 PyObject *doc;
3756 node *ch;
3757 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3758 c->c_name = STR(CHILD(n, 1));
3759 doc = get_docstring(CHILD(n, 4));
3760 if (doc != NULL) {
3761 (void) com_addconst(c, doc);
3762 Py_DECREF(doc);
3764 else
3765 (void) com_addconst(c, Py_None); /* No docstring */
3766 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3767 ch = CHILD(ch, 1); /* ')' | varargslist */
3768 if (TYPE(ch) == varargslist)
3769 com_arglist(c, ch);
3770 c->c_infunction = 1;
3771 com_node(c, CHILD(n, 4));
3772 c->c_infunction = 0;
3773 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3774 com_push(c, 1);
3775 com_addbyte(c, RETURN_VALUE);
3776 com_pop(c, 1);
3779 static void
3780 compile_lambdef(struct compiling *c, node *n)
3782 node *ch;
3783 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3784 c->c_name = "<lambda>";
3786 ch = CHILD(n, 1);
3787 (void) com_addconst(c, Py_None); /* No docstring */
3788 if (TYPE(ch) == varargslist) {
3789 com_arglist(c, ch);
3790 ch = CHILD(n, 3);
3792 else
3793 ch = CHILD(n, 2);
3794 com_node(c, ch);
3795 com_addbyte(c, RETURN_VALUE);
3796 com_pop(c, 1);
3799 static void
3800 compile_classdef(struct compiling *c, node *n)
3802 node *ch;
3803 PyObject *doc;
3804 REQ(n, classdef);
3805 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3806 c->c_name = STR(CHILD(n, 1));
3807 c->c_private = c->c_name;
3808 ch = CHILD(n, NCH(n)-1); /* The suite */
3809 doc = get_docstring(ch);
3810 if (doc != NULL) {
3811 int i = com_addconst(c, doc);
3812 Py_DECREF(doc);
3813 com_addoparg(c, LOAD_CONST, i);
3814 com_push(c, 1);
3815 com_addop_name(c, STORE_NAME, "__doc__");
3816 com_pop(c, 1);
3818 else
3819 (void) com_addconst(c, Py_None);
3820 com_node(c, ch);
3821 com_addbyte(c, LOAD_LOCALS);
3822 com_push(c, 1);
3823 com_addbyte(c, RETURN_VALUE);
3824 com_pop(c, 1);
3827 static void
3828 compile_node(struct compiling *c, node *n)
3830 com_addoparg(c, SET_LINENO, n->n_lineno);
3832 switch (TYPE(n)) {
3834 case single_input: /* One interactive command */
3835 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3836 c->c_interactive++;
3837 n = CHILD(n, 0);
3838 if (TYPE(n) != NEWLINE)
3839 com_node(c, n);
3840 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3841 com_push(c, 1);
3842 com_addbyte(c, RETURN_VALUE);
3843 com_pop(c, 1);
3844 c->c_interactive--;
3845 break;
3847 case file_input: /* A whole file, or built-in function exec() */
3848 com_file_input(c, n);
3849 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3850 com_push(c, 1);
3851 com_addbyte(c, RETURN_VALUE);
3852 com_pop(c, 1);
3853 break;
3855 case eval_input: /* Built-in function input() */
3856 com_node(c, CHILD(n, 0));
3857 com_addbyte(c, RETURN_VALUE);
3858 com_pop(c, 1);
3859 break;
3861 case lambdef: /* anonymous function definition */
3862 compile_lambdef(c, n);
3863 break;
3865 case funcdef: /* A function definition */
3866 compile_funcdef(c, n);
3867 break;
3869 case classdef: /* A class definition */
3870 compile_classdef(c, n);
3871 break;
3873 default:
3874 com_error(c, PyExc_SystemError,
3875 "compile_node: unexpected node type");
3879 static PyObject *
3880 dict_keys_inorder(PyObject *dict, int offset)
3882 PyObject *tuple, *k, *v;
3883 int i, pos = 0, size = PyDict_Size(dict);
3885 tuple = PyTuple_New(size);
3886 if (tuple == NULL)
3887 return NULL;
3888 while (PyDict_Next(dict, &pos, &k, &v)) {
3889 i = PyInt_AS_LONG(v);
3890 Py_INCREF(k);
3891 assert((i - offset) < size);
3892 PyTuple_SET_ITEM(tuple, i - offset, k);
3894 return tuple;
3897 PyCodeObject *
3898 PyNode_Compile(node *n, char *filename)
3900 return PyNode_CompileFlags(n, filename, NULL);
3903 PyCodeObject *
3904 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
3906 return jcompile(n, filename, NULL, flags);
3909 struct symtable *
3910 PyNode_CompileSymtable(node *n, char *filename)
3912 struct symtable *st;
3913 PyFutureFeatures *ff;
3915 ff = PyNode_Future(n, filename);
3916 if (ff == NULL)
3917 return NULL;
3918 st = symtable_init();
3919 if (st == NULL)
3920 return NULL;
3921 st->st_future = ff;
3922 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
3923 if (st->st_errors > 0)
3924 goto fail;
3925 symtable_node(st, n);
3926 if (st->st_errors > 0)
3927 goto fail;
3929 return st;
3930 fail:
3931 PyMem_Free((void *)ff);
3932 st->st_future = NULL;
3933 PySymtable_Free(st);
3934 return NULL;
3937 static PyCodeObject *
3938 icompile(node *n, struct compiling *base)
3940 return jcompile(n, base->c_filename, base, NULL);
3943 static PyCodeObject *
3944 jcompile(node *n, char *filename, struct compiling *base,
3945 PyCompilerFlags *flags)
3947 struct compiling sc;
3948 PyCodeObject *co;
3949 if (!com_init(&sc, filename))
3950 return NULL;
3951 if (base) {
3952 sc.c_private = base->c_private;
3953 sc.c_symtable = base->c_symtable;
3954 /* c_symtable still points to parent's symbols */
3955 if (base->c_nested
3956 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
3957 sc.c_nested = 1;
3958 sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED |
3959 CO_FUTURE_DIVISION);
3960 } else {
3961 sc.c_private = NULL;
3962 sc.c_future = PyNode_Future(n, filename);
3963 if (sc.c_future == NULL) {
3964 com_free(&sc);
3965 return NULL;
3967 if (flags)
3968 sc.c_future->ff_features |= flags->cf_flags;
3969 if (symtable_build(&sc, n) < 0) {
3970 com_free(&sc);
3971 return NULL;
3974 co = NULL;
3975 if (symtable_load_symbols(&sc) < 0) {
3976 sc.c_errors++;
3977 goto exit;
3979 compile_node(&sc, n);
3980 com_done(&sc);
3981 if (sc.c_errors == 0) {
3982 PyObject *consts, *names, *varnames, *filename, *name,
3983 *freevars, *cellvars;
3984 consts = PyList_AsTuple(sc.c_consts);
3985 names = PyList_AsTuple(sc.c_names);
3986 varnames = PyList_AsTuple(sc.c_varnames);
3987 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
3988 freevars = dict_keys_inorder(sc.c_freevars,
3989 PyTuple_GET_SIZE(cellvars));
3990 filename = PyString_InternFromString(sc.c_filename);
3991 name = PyString_InternFromString(sc.c_name);
3992 if (!PyErr_Occurred())
3993 co = PyCode_New(sc.c_argcount,
3994 sc.c_nlocals,
3995 sc.c_maxstacklevel,
3996 sc.c_flags,
3997 sc.c_code,
3998 consts,
3999 names,
4000 varnames,
4001 freevars,
4002 cellvars,
4003 filename,
4004 name,
4005 sc.c_firstlineno,
4006 sc.c_lnotab);
4007 Py_XDECREF(consts);
4008 Py_XDECREF(names);
4009 Py_XDECREF(varnames);
4010 Py_XDECREF(freevars);
4011 Py_XDECREF(cellvars);
4012 Py_XDECREF(filename);
4013 Py_XDECREF(name);
4015 else if (!PyErr_Occurred()) {
4016 /* This could happen if someone called PyErr_Clear() after an
4017 error was reported above. That's not supposed to happen,
4018 but I just plugged one case and I'm not sure there can't be
4019 others. In that case, raise SystemError so that at least
4020 it gets reported instead dumping core. */
4021 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4023 exit:
4024 if (base == NULL) {
4025 PySymtable_Free(sc.c_symtable);
4026 sc.c_symtable = NULL;
4028 com_free(&sc);
4029 return co;
4033 PyCode_Addr2Line(PyCodeObject *co, int addrq)
4035 int size = PyString_Size(co->co_lnotab) / 2;
4036 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
4037 int line = co->co_firstlineno;
4038 int addr = 0;
4039 while (--size >= 0) {
4040 addr += *p++;
4041 if (addr > addrq)
4042 break;
4043 line += *p++;
4045 return line;
4048 /* The test for LOCAL must come before the test for FREE in order to
4049 handle classes where name is both local and free. The local var is
4050 a method and the free var is a free var referenced within a method.
4053 static int
4054 get_ref_type(struct compiling *c, char *name)
4056 char buf[350];
4057 PyObject *v;
4059 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4060 return CELL;
4061 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4062 return LOCAL;
4063 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4064 return FREE;
4065 v = PyDict_GetItemString(c->c_globals, name);
4066 if (v) {
4067 if (v == Py_None)
4068 return GLOBAL_EXPLICIT;
4069 else {
4070 return GLOBAL_IMPLICIT;
4073 sprintf(buf,
4074 "unknown scope for %.100s in %.100s(%s) "
4075 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4076 name, c->c_name,
4077 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4078 c->c_filename,
4079 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4080 PyObject_REPR(c->c_locals),
4081 PyObject_REPR(c->c_globals)
4084 Py_FatalError(buf);
4085 return -1;
4088 /* Helper functions to issue warnings */
4090 static int
4091 issue_warning(char *msg, char *filename, int lineno)
4093 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4094 lineno, NULL, NULL) < 0) {
4095 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4096 PyErr_SetString(PyExc_SyntaxError, msg);
4097 PyErr_SyntaxLocation(filename, lineno);
4099 return -1;
4101 return 0;
4104 static int
4105 symtable_warn(struct symtable *st, char *msg)
4107 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
4108 st->st_errors++;
4109 return -1;
4111 return 0;
4114 /* Helper function for setting lineno and filename */
4116 static int
4117 symtable_build(struct compiling *c, node *n)
4119 if ((c->c_symtable = symtable_init()) == NULL)
4120 return -1;
4121 c->c_symtable->st_future = c->c_future;
4122 c->c_symtable->st_filename = c->c_filename;
4123 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4124 if (c->c_symtable->st_errors > 0)
4125 return -1;
4126 symtable_node(c->c_symtable, n);
4127 if (c->c_symtable->st_errors > 0)
4128 return -1;
4129 /* reset for second pass */
4130 c->c_symtable->st_nscopes = 1;
4131 c->c_symtable->st_pass = 2;
4132 return 0;
4135 static int
4136 symtable_init_compiling_symbols(struct compiling *c)
4138 PyObject *varnames;
4140 varnames = c->c_symtable->st_cur->ste_varnames;
4141 if (varnames == NULL) {
4142 varnames = PyList_New(0);
4143 if (varnames == NULL)
4144 return -1;
4145 c->c_symtable->st_cur->ste_varnames = varnames;
4146 Py_INCREF(varnames);
4147 } else
4148 Py_INCREF(varnames);
4149 c->c_varnames = varnames;
4151 c->c_globals = PyDict_New();
4152 if (c->c_globals == NULL)
4153 return -1;
4154 c->c_freevars = PyDict_New();
4155 if (c->c_freevars == NULL)
4156 return -1;
4157 c->c_cellvars = PyDict_New();
4158 if (c->c_cellvars == NULL)
4159 return -1;
4160 return 0;
4163 struct symbol_info {
4164 int si_nlocals;
4165 int si_ncells;
4166 int si_nfrees;
4167 int si_nimplicit;
4170 static void
4171 symtable_init_info(struct symbol_info *si)
4173 si->si_nlocals = 0;
4174 si->si_ncells = 0;
4175 si->si_nfrees = 0;
4176 si->si_nimplicit = 0;
4179 static int
4180 symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
4181 struct symbol_info *si)
4183 PyObject *dict, *v;
4185 /* Seperate logic for DEF_FREE. If it occurs in a function,
4186 it indicates a local that we must allocate storage for (a
4187 cell var). If it occurs in a class, then the class has a
4188 method and a free variable with the same name.
4190 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4191 /* If it isn't declared locally, it can't be a cell. */
4192 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4193 return 0;
4194 v = PyInt_FromLong(si->si_ncells++);
4195 dict = c->c_cellvars;
4196 } else {
4197 /* If it is free anyway, then there is no need to do
4198 anything here.
4200 if (is_free(flags ^ DEF_FREE_CLASS)
4201 || (flags == DEF_FREE_CLASS))
4202 return 0;
4203 v = PyInt_FromLong(si->si_nfrees++);
4204 dict = c->c_freevars;
4206 if (v == NULL)
4207 return -1;
4208 if (PyDict_SetItem(dict, name, v) < 0) {
4209 Py_DECREF(v);
4210 return -1;
4212 Py_DECREF(v);
4213 return 0;
4216 /* If a variable is a cell and an argument, make sure that appears in
4217 co_cellvars before any variable to its right in varnames.
4221 static int
4222 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4223 PyObject *varnames, int flags)
4225 PyObject *v, *w, *d, *list = NULL;
4226 int i, pos;
4228 if (flags & CO_VARARGS)
4229 argcount++;
4230 if (flags & CO_VARKEYWORDS)
4231 argcount++;
4232 for (i = argcount; --i >= 0; ) {
4233 v = PyList_GET_ITEM(varnames, i);
4234 if (PyDict_GetItem(*cellvars, v)) {
4235 if (list == NULL) {
4236 list = PyList_New(1);
4237 if (list == NULL)
4238 return -1;
4239 PyList_SET_ITEM(list, 0, v);
4240 Py_INCREF(v);
4241 } else
4242 PyList_Insert(list, 0, v);
4245 if (list == NULL || PyList_GET_SIZE(list) == 0)
4246 return 0;
4247 /* There are cellvars that are also arguments. Create a dict
4248 to replace cellvars and put the args at the front.
4250 d = PyDict_New();
4251 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4252 v = PyInt_FromLong(i);
4253 if (v == NULL)
4254 goto fail;
4255 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4256 goto fail;
4257 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4258 goto fail;
4260 pos = 0;
4261 i = PyList_GET_SIZE(list);
4262 Py_DECREF(list);
4263 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4264 w = PyInt_FromLong(i++); /* don't care about the old key */
4265 if (PyDict_SetItem(d, v, w) < 0) {
4266 Py_DECREF(w);
4267 goto fail;
4269 Py_DECREF(w);
4271 Py_DECREF(*cellvars);
4272 *cellvars = d;
4273 return 1;
4274 fail:
4275 Py_DECREF(d);
4276 return -1;
4279 static int
4280 symtable_freevar_offsets(PyObject *freevars, int offset)
4282 PyObject *name, *v;
4283 int pos;
4285 /* The cell vars are the first elements of the closure,
4286 followed by the free vars. Update the offsets in
4287 c_freevars to account for number of cellvars. */
4288 pos = 0;
4289 while (PyDict_Next(freevars, &pos, &name, &v)) {
4290 int i = PyInt_AS_LONG(v) + offset;
4291 PyObject *o = PyInt_FromLong(i);
4292 if (o == NULL)
4293 return -1;
4294 if (PyDict_SetItem(freevars, name, o) < 0) {
4295 Py_DECREF(o);
4296 return -1;
4298 Py_DECREF(o);
4300 return 0;
4303 static int
4304 symtable_check_unoptimized(struct compiling *c,
4305 PySymtableEntryObject *ste,
4306 struct symbol_info *si)
4308 char buf[300];
4310 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4311 || (ste->ste_nested && si->si_nimplicit)))
4312 return 0;
4314 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4316 #define ILLEGAL_IS "is a nested function"
4318 #define ILLEGAL_IMPORT_STAR \
4319 "import * is not allowed in function '%.100s' because it %s"
4321 #define ILLEGAL_BARE_EXEC \
4322 "unqualified exec is not allowed in function '%.100s' it %s"
4324 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4325 "function '%.100s' uses import * and bare exec, which are illegal" \
4326 "because it %s"
4328 /* XXX perhaps the linenos for these opt-breaking statements
4329 should be stored so the exception can point to them. */
4331 if (ste->ste_child_free) {
4332 if (ste->ste_optimized == OPT_IMPORT_STAR)
4333 sprintf(buf, ILLEGAL_IMPORT_STAR,
4334 PyString_AS_STRING(ste->ste_name),
4335 ILLEGAL_CONTAINS);
4336 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4337 sprintf(buf, ILLEGAL_BARE_EXEC,
4338 PyString_AS_STRING(ste->ste_name),
4339 ILLEGAL_CONTAINS);
4340 else {
4341 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4342 PyString_AS_STRING(ste->ste_name),
4343 ILLEGAL_CONTAINS);
4345 } else {
4346 if (ste->ste_optimized == OPT_IMPORT_STAR)
4347 sprintf(buf, ILLEGAL_IMPORT_STAR,
4348 PyString_AS_STRING(ste->ste_name),
4349 ILLEGAL_IS);
4350 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4351 sprintf(buf, ILLEGAL_BARE_EXEC,
4352 PyString_AS_STRING(ste->ste_name),
4353 ILLEGAL_IS);
4354 else {
4355 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4356 PyString_AS_STRING(ste->ste_name),
4357 ILLEGAL_IS);
4361 PyErr_SetString(PyExc_SyntaxError, buf);
4362 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4363 ste->ste_opt_lineno);
4364 return -1;
4367 static int
4368 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4369 struct symbol_info *si)
4371 if (c->c_future)
4372 c->c_flags |= c->c_future->ff_features;
4373 if (ste->ste_generator)
4374 c->c_flags |= CO_GENERATOR;
4375 if (ste->ste_type != TYPE_MODULE)
4376 c->c_flags |= CO_NEWLOCALS;
4377 if (ste->ste_type == TYPE_FUNCTION) {
4378 c->c_nlocals = si->si_nlocals;
4379 if (ste->ste_optimized == 0)
4380 c->c_flags |= CO_OPTIMIZED;
4381 else if (ste->ste_optimized != OPT_EXEC)
4382 return symtable_check_unoptimized(c, ste, si);
4384 return 0;
4387 static int
4388 symtable_load_symbols(struct compiling *c)
4390 static PyObject *implicit = NULL;
4391 struct symtable *st = c->c_symtable;
4392 PySymtableEntryObject *ste = st->st_cur;
4393 PyObject *name, *varnames, *v;
4394 int i, flags, pos;
4395 struct symbol_info si;
4397 if (implicit == NULL) {
4398 implicit = PyInt_FromLong(1);
4399 if (implicit == NULL)
4400 return -1;
4402 v = NULL;
4404 if (symtable_init_compiling_symbols(c) < 0)
4405 goto fail;
4406 symtable_init_info(&si);
4407 varnames = st->st_cur->ste_varnames;
4408 si.si_nlocals = PyList_GET_SIZE(varnames);
4409 c->c_argcount = si.si_nlocals;
4411 for (i = 0; i < si.si_nlocals; ++i) {
4412 v = PyInt_FromLong(i);
4413 if (PyDict_SetItem(c->c_locals,
4414 PyList_GET_ITEM(varnames, i), v) < 0)
4415 goto fail;
4416 Py_DECREF(v);
4419 /* XXX The cases below define the rules for whether a name is
4420 local or global. The logic could probably be clearer. */
4421 pos = 0;
4422 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4423 flags = PyInt_AS_LONG(v);
4425 if (flags & DEF_FREE_GLOBAL)
4426 /* undo the original DEF_FREE */
4427 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4429 /* Deal with names that need two actions:
4430 1. Cell variables that are also locals.
4431 2. Free variables in methods that are also class
4432 variables or declared global.
4434 if (flags & (DEF_FREE | DEF_FREE_CLASS))
4435 symtable_resolve_free(c, name, flags, &si);
4437 if (flags & DEF_STAR) {
4438 c->c_argcount--;
4439 c->c_flags |= CO_VARARGS;
4440 } else if (flags & DEF_DOUBLESTAR) {
4441 c->c_argcount--;
4442 c->c_flags |= CO_VARKEYWORDS;
4443 } else if (flags & DEF_INTUPLE)
4444 c->c_argcount--;
4445 else if (flags & DEF_GLOBAL) {
4446 if (flags & DEF_PARAM) {
4447 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4448 PyString_AS_STRING(name));
4449 PyErr_SyntaxLocation(st->st_filename,
4450 ste->ste_lineno);
4451 st->st_errors++;
4452 goto fail;
4454 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4455 goto fail;
4456 } else if (flags & DEF_FREE_GLOBAL) {
4457 si.si_nimplicit++;
4458 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4459 goto fail;
4460 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4461 v = PyInt_FromLong(si.si_nlocals++);
4462 if (v == NULL)
4463 goto fail;
4464 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4465 goto fail;
4466 Py_DECREF(v);
4467 if (ste->ste_type != TYPE_CLASS)
4468 if (PyList_Append(c->c_varnames, name) < 0)
4469 goto fail;
4470 } else if (is_free(flags)) {
4471 if (ste->ste_nested) {
4472 v = PyInt_FromLong(si.si_nfrees++);
4473 if (v == NULL)
4474 goto fail;
4475 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4476 goto fail;
4477 Py_DECREF(v);
4478 } else {
4479 si.si_nimplicit++;
4480 if (PyDict_SetItem(c->c_globals, name,
4481 implicit) < 0)
4482 goto fail;
4483 if (st->st_nscopes != 1) {
4484 v = PyInt_FromLong(flags);
4485 if (PyDict_SetItem(st->st_global,
4486 name, v))
4487 goto fail;
4488 Py_DECREF(v);
4494 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4496 if (si.si_ncells > 1) { /* one cell is always in order */
4497 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4498 c->c_varnames, c->c_flags) < 0)
4499 return -1;
4501 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4502 return -1;
4503 return symtable_update_flags(c, ste, &si);
4504 fail:
4505 /* is this always the right thing to do? */
4506 Py_XDECREF(v);
4507 return -1;
4510 static struct symtable *
4511 symtable_init()
4513 struct symtable *st;
4515 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4516 if (st == NULL)
4517 return NULL;
4518 st->st_pass = 1;
4520 st->st_filename = NULL;
4521 if ((st->st_stack = PyList_New(0)) == NULL)
4522 goto fail;
4523 if ((st->st_symbols = PyDict_New()) == NULL)
4524 goto fail;
4525 st->st_cur = NULL;
4526 st->st_nscopes = 0;
4527 st->st_errors = 0;
4528 st->st_tmpname = 0;
4529 st->st_private = NULL;
4530 return st;
4531 fail:
4532 PySymtable_Free(st);
4533 return NULL;
4536 void
4537 PySymtable_Free(struct symtable *st)
4539 Py_XDECREF(st->st_symbols);
4540 Py_XDECREF(st->st_stack);
4541 Py_XDECREF(st->st_cur);
4542 PyMem_Free((void *)st);
4545 /* When the compiler exits a scope, it must should update the scope's
4546 free variable information with the list of free variables in its
4547 children.
4549 Variables that are free in children and defined in the current
4550 scope are cellvars.
4552 If the scope being exited is defined at the top-level (ste_nested is
4553 false), free variables in children that are not defined here are
4554 implicit globals.
4558 static int
4559 symtable_update_free_vars(struct symtable *st)
4561 int i, j, def;
4562 PyObject *o, *name, *list = NULL;
4563 PySymtableEntryObject *child, *ste = st->st_cur;
4565 if (ste->ste_type == TYPE_CLASS)
4566 def = DEF_FREE_CLASS;
4567 else
4568 def = DEF_FREE;
4569 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4570 int pos = 0;
4572 if (list)
4573 PyList_SetSlice(list, 0,
4574 ((PyVarObject*)list)->ob_size, 0);
4575 child = (PySymtableEntryObject *)
4576 PyList_GET_ITEM(ste->ste_children, i);
4577 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4578 int flags = PyInt_AS_LONG(o);
4579 if (!(is_free(flags)))
4580 continue; /* avoids indentation */
4581 if (list == NULL) {
4582 list = PyList_New(0);
4583 if (list == NULL)
4584 return -1;
4586 ste->ste_child_free = 1;
4587 if (PyList_Append(list, name) < 0) {
4588 Py_DECREF(list);
4589 return -1;
4592 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4593 PyObject *v;
4594 name = PyList_GET_ITEM(list, j);
4595 v = PyDict_GetItem(ste->ste_symbols, name);
4596 /* If a name N is declared global in scope A and
4597 referenced in scope B contained (perhaps
4598 indirectly) in A and there are no scopes
4599 with bindings for N between B and A, then N
4600 is global in B. Unless A is a class scope,
4601 because class scopes are not considered for
4602 nested scopes.
4604 if (v && (ste->ste_type != TYPE_CLASS)) {
4605 int flags = PyInt_AS_LONG(v);
4606 if (flags & DEF_GLOBAL) {
4607 symtable_undo_free(st, child->ste_id,
4608 name);
4609 continue;
4612 if (ste->ste_nested) {
4613 if (symtable_add_def_o(st, ste->ste_symbols,
4614 name, def) < 0) {
4615 Py_DECREF(list);
4616 return -1;
4618 } else {
4619 if (symtable_check_global(st, child->ste_id,
4620 name) < 0) {
4621 Py_DECREF(list);
4622 return -1;
4628 Py_XDECREF(list);
4629 return 0;
4632 /* If the current scope is a non-nested class or if name is not
4633 defined in the current, non-nested scope, then it is an implicit
4634 global in all nested scopes.
4637 static int
4638 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4640 PyObject *o;
4641 int v;
4642 PySymtableEntryObject *ste = st->st_cur;
4644 if (ste->ste_type == TYPE_CLASS)
4645 return symtable_undo_free(st, child, name);
4646 o = PyDict_GetItem(ste->ste_symbols, name);
4647 if (o == NULL)
4648 return symtable_undo_free(st, child, name);
4649 v = PyInt_AS_LONG(o);
4651 if (is_free(v) || (v & DEF_GLOBAL))
4652 return symtable_undo_free(st, child, name);
4653 else
4654 return symtable_add_def_o(st, ste->ste_symbols,
4655 name, DEF_FREE);
4658 static int
4659 symtable_undo_free(struct symtable *st, PyObject *id,
4660 PyObject *name)
4662 int i, v, x;
4663 PyObject *info;
4664 PySymtableEntryObject *ste;
4666 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4667 if (ste == NULL)
4668 return -1;
4670 info = PyDict_GetItem(ste->ste_symbols, name);
4671 if (info == NULL)
4672 return 0;
4673 v = PyInt_AS_LONG(info);
4674 if (is_free(v)) {
4675 if (symtable_add_def_o(st, ste->ste_symbols, name,
4676 DEF_FREE_GLOBAL) < 0)
4677 return -1;
4678 } else
4679 /* If the name is defined here or declared global,
4680 then the recursion stops. */
4681 return 0;
4683 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4684 PySymtableEntryObject *child;
4685 child = (PySymtableEntryObject *)
4686 PyList_GET_ITEM(ste->ste_children, i);
4687 x = symtable_undo_free(st, child->ste_id, name);
4688 if (x < 0)
4689 return x;
4691 return 0;
4694 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4695 This reference is released when the scope is exited, via the DECREF
4696 in symtable_exit_scope().
4699 static int
4700 symtable_exit_scope(struct symtable *st)
4702 int end;
4704 if (st->st_pass == 1)
4705 symtable_update_free_vars(st);
4706 Py_DECREF(st->st_cur);
4707 end = PyList_GET_SIZE(st->st_stack) - 1;
4708 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4709 end);
4710 if (PySequence_DelItem(st->st_stack, end) < 0)
4711 return -1;
4712 return 0;
4715 static void
4716 symtable_enter_scope(struct symtable *st, char *name, int type,
4717 int lineno)
4719 PySymtableEntryObject *prev = NULL;
4721 if (st->st_cur) {
4722 prev = st->st_cur;
4723 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4724 Py_DECREF(st->st_cur);
4725 st->st_errors++;
4726 return;
4729 st->st_cur = (PySymtableEntryObject *)
4730 PySymtableEntry_New(st, name, type, lineno);
4731 if (strcmp(name, TOP) == 0)
4732 st->st_global = st->st_cur->ste_symbols;
4733 if (prev && st->st_pass == 1) {
4734 if (PyList_Append(prev->ste_children,
4735 (PyObject *)st->st_cur) < 0)
4736 st->st_errors++;
4740 static int
4741 symtable_lookup(struct symtable *st, char *name)
4743 char buffer[MANGLE_LEN];
4744 PyObject *v;
4745 int flags;
4747 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4748 name = buffer;
4749 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4750 if (v == NULL) {
4751 if (PyErr_Occurred())
4752 return -1;
4753 else
4754 return 0;
4757 flags = PyInt_AS_LONG(v);
4758 return flags;
4761 static int
4762 symtable_add_def(struct symtable *st, char *name, int flag)
4764 PyObject *s;
4765 char buffer[MANGLE_LEN];
4766 int ret;
4768 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4769 name = buffer;
4770 if ((s = PyString_InternFromString(name)) == NULL)
4771 return -1;
4772 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4773 Py_DECREF(s);
4774 return ret;
4777 /* Must only be called with mangled names */
4779 static int
4780 symtable_add_def_o(struct symtable *st, PyObject *dict,
4781 PyObject *name, int flag)
4783 PyObject *o;
4784 int val;
4786 if ((o = PyDict_GetItem(dict, name))) {
4787 val = PyInt_AS_LONG(o);
4788 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
4789 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4790 PyString_AsString(name));
4791 PyErr_SyntaxLocation(st->st_filename,
4792 st->st_cur->ste_lineno);
4793 return -1;
4795 val |= flag;
4796 } else
4797 val = flag;
4798 o = PyInt_FromLong(val);
4799 if (PyDict_SetItem(dict, name, o) < 0) {
4800 Py_DECREF(o);
4801 return -1;
4803 Py_DECREF(o);
4805 if (flag & DEF_PARAM) {
4806 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
4807 return -1;
4808 } else if (flag & DEF_GLOBAL) {
4809 /* XXX need to update DEF_GLOBAL for other flags too;
4810 perhaps only DEF_FREE_GLOBAL */
4811 if ((o = PyDict_GetItem(st->st_global, name))) {
4812 val = PyInt_AS_LONG(o);
4813 val |= flag;
4814 } else
4815 val = flag;
4816 o = PyInt_FromLong(val);
4817 if (PyDict_SetItem(st->st_global, name, o) < 0) {
4818 Py_DECREF(o);
4819 return -1;
4821 Py_DECREF(o);
4823 return 0;
4826 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4828 /* Look for a yield stmt under n. Return 1 if found, else 0.
4829 This hack is used to look inside "if 0:" blocks (which are normally
4830 ignored) in case those are the only places a yield occurs (so that this
4831 function is a generator). */
4832 static int
4833 look_for_yield(node *n)
4835 int i;
4837 for (i = 0; i < NCH(n); ++i) {
4838 node *kid = CHILD(n, i);
4840 switch (TYPE(kid)) {
4842 case classdef:
4843 case funcdef:
4844 case lambdef:
4845 /* Stuff in nested functions and classes can't make
4846 the parent a generator. */
4847 return 0;
4849 case yield_stmt:
4850 return 1;
4852 default:
4853 if (look_for_yield(kid))
4854 return 1;
4857 return 0;
4860 static void
4861 symtable_node(struct symtable *st, node *n)
4863 int i, start = 0;
4865 loop:
4866 switch (TYPE(n)) {
4867 case funcdef: {
4868 char *func_name = STR(CHILD(n, 1));
4869 symtable_add_def(st, func_name, DEF_LOCAL);
4870 symtable_default_args(st, CHILD(n, 2));
4871 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
4872 symtable_funcdef(st, n);
4873 symtable_exit_scope(st);
4874 break;
4876 case lambdef:
4877 if (NCH(n) == 4)
4878 symtable_default_args(st, CHILD(n, 1));
4879 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
4880 symtable_funcdef(st, n);
4881 symtable_exit_scope(st);
4882 break;
4883 case classdef: {
4884 char *tmp, *class_name = STR(CHILD(n, 1));
4885 symtable_add_def(st, class_name, DEF_LOCAL);
4886 if (TYPE(CHILD(n, 2)) == LPAR) {
4887 node *bases = CHILD(n, 3);
4888 int i;
4889 for (i = 0; i < NCH(bases); i += 2) {
4890 symtable_node(st, CHILD(bases, i));
4893 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
4894 tmp = st->st_private;
4895 st->st_private = class_name;
4896 symtable_node(st, CHILD(n, NCH(n) - 1));
4897 st->st_private = tmp;
4898 symtable_exit_scope(st);
4899 break;
4901 case if_stmt:
4902 for (i = 0; i + 3 < NCH(n); i += 4) {
4903 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
4904 if (st->st_cur->ste_generator == 0)
4905 st->st_cur->ste_generator =
4906 look_for_yield(CHILD(n, i+3));
4907 continue;
4909 symtable_node(st, CHILD(n, i + 1));
4910 symtable_node(st, CHILD(n, i + 3));
4912 if (i + 2 < NCH(n))
4913 symtable_node(st, CHILD(n, i + 2));
4914 break;
4915 case global_stmt:
4916 symtable_global(st, n);
4917 break;
4918 case import_stmt:
4919 symtable_import(st, n);
4920 break;
4921 case exec_stmt: {
4922 st->st_cur->ste_optimized |= OPT_EXEC;
4923 symtable_node(st, CHILD(n, 1));
4924 if (NCH(n) > 2)
4925 symtable_node(st, CHILD(n, 3));
4926 else {
4927 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
4928 st->st_cur->ste_opt_lineno = n->n_lineno;
4930 if (NCH(n) > 4)
4931 symtable_node(st, CHILD(n, 5));
4932 break;
4935 case assert_stmt:
4936 if (Py_OptimizeFlag)
4937 return;
4938 if (NCH(n) == 2) {
4939 n = CHILD(n, 1);
4940 goto loop;
4941 } else {
4942 symtable_node(st, CHILD(n, 1));
4943 n = CHILD(n, 3);
4944 goto loop;
4946 case except_clause:
4947 if (NCH(n) == 4)
4948 symtable_assign(st, CHILD(n, 3), 0);
4949 if (NCH(n) > 1) {
4950 n = CHILD(n, 1);
4951 goto loop;
4953 break;
4954 case del_stmt:
4955 symtable_assign(st, CHILD(n, 1), 0);
4956 break;
4957 case yield_stmt:
4958 st->st_cur->ste_generator = 1;
4959 n = CHILD(n, 1);
4960 goto loop;
4961 case expr_stmt:
4962 if (NCH(n) == 1)
4963 n = CHILD(n, 0);
4964 else {
4965 if (TYPE(CHILD(n, 1)) == augassign) {
4966 symtable_assign(st, CHILD(n, 0), 0);
4967 symtable_node(st, CHILD(n, 2));
4968 break;
4969 } else {
4970 int i;
4971 for (i = 0; i < NCH(n) - 2; i += 2)
4972 symtable_assign(st, CHILD(n, i), 0);
4973 n = CHILD(n, NCH(n) - 1);
4976 goto loop;
4977 /* watchout for fall-through logic below */
4978 case argument:
4979 if (NCH(n) == 3) {
4980 n = CHILD(n, 2);
4981 goto loop;
4983 case listmaker:
4984 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
4985 st->st_tmpname++;
4986 symtable_list_comprehension(st, CHILD(n, 1));
4987 symtable_node(st, CHILD(n, 0));
4988 st->st_tmpname--;
4989 return;
4991 case atom:
4992 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
4993 symtable_add_use(st, STR(CHILD(n, 0)));
4994 break;
4996 case for_stmt:
4997 if (TYPE(n) == for_stmt) {
4998 symtable_assign(st, CHILD(n, 1), 0);
4999 start = 3;
5001 default:
5002 if (NCH(n) == 1) {
5003 n = CHILD(n, 0);
5004 goto loop;
5006 for (i = start; i < NCH(n); ++i)
5007 if (TYPE(CHILD(n, i)) >= single_input)
5008 symtable_node(st, CHILD(n, i));
5012 static void
5013 symtable_funcdef(struct symtable *st, node *n)
5015 node *body;
5017 if (TYPE(n) == lambdef) {
5018 if (NCH(n) == 4)
5019 symtable_params(st, CHILD(n, 1));
5020 } else
5021 symtable_params(st, CHILD(n, 2));
5022 body = CHILD(n, NCH(n) - 1);
5023 symtable_node(st, body);
5026 /* The next two functions parse the argument tuple.
5027 symtable_default_arg() checks for names in the default arguments,
5028 which are references in the defining scope. symtable_params()
5029 parses the parameter names, which are defined in the function's
5030 body.
5032 varargslist:
5033 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5034 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5037 static void
5038 symtable_default_args(struct symtable *st, node *n)
5040 node *c;
5041 int i;
5043 if (TYPE(n) == parameters) {
5044 n = CHILD(n, 1);
5045 if (TYPE(n) == RPAR)
5046 return;
5048 REQ(n, varargslist);
5049 for (i = 0; i < NCH(n); i += 2) {
5050 c = CHILD(n, i);
5051 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5052 break;
5054 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5055 symtable_node(st, CHILD(n, i));
5059 static void
5060 symtable_params(struct symtable *st, node *n)
5062 int i, complex = -1, ext = 0;
5063 node *c = NULL;
5065 if (TYPE(n) == parameters) {
5066 n = CHILD(n, 1);
5067 if (TYPE(n) == RPAR)
5068 return;
5070 REQ(n, varargslist);
5071 for (i = 0; i < NCH(n); i += 2) {
5072 c = CHILD(n, i);
5073 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5074 ext = 1;
5075 break;
5077 if (TYPE(c) == test) {
5078 continue;
5080 if (TYPE(CHILD(c, 0)) == NAME)
5081 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5082 else {
5083 char nbuf[10];
5084 sprintf(nbuf, ".%d", i);
5085 symtable_add_def(st, nbuf, DEF_PARAM);
5086 complex = i;
5089 if (ext) {
5090 c = CHILD(n, i);
5091 if (TYPE(c) == STAR) {
5092 i++;
5093 symtable_add_def(st, STR(CHILD(n, i)),
5094 DEF_PARAM | DEF_STAR);
5095 i += 2;
5096 if (i >= NCH(n))
5097 c = NULL;
5098 else
5099 c = CHILD(n, i);
5101 if (c && TYPE(c) == DOUBLESTAR) {
5102 i++;
5103 symtable_add_def(st, STR(CHILD(n, i)),
5104 DEF_PARAM | DEF_DOUBLESTAR);
5107 if (complex >= 0) {
5108 int j;
5109 for (j = 0; j <= complex; j++) {
5110 c = CHILD(n, j);
5111 if (TYPE(c) == COMMA)
5112 c = CHILD(n, ++j);
5113 else if (TYPE(c) == EQUAL)
5114 c = CHILD(n, j += 3);
5115 if (TYPE(CHILD(c, 0)) == LPAR)
5116 symtable_params_fplist(st, CHILD(c, 1));
5121 static void
5122 symtable_params_fplist(struct symtable *st, node *n)
5124 int i;
5125 node *c;
5127 REQ(n, fplist);
5128 for (i = 0; i < NCH(n); i += 2) {
5129 c = CHILD(n, i);
5130 REQ(c, fpdef);
5131 if (NCH(c) == 1)
5132 symtable_add_def(st, STR(CHILD(c, 0)),
5133 DEF_PARAM | DEF_INTUPLE);
5134 else
5135 symtable_params_fplist(st, CHILD(c, 1));
5140 static void
5141 symtable_global(struct symtable *st, node *n)
5143 int i;
5145 /* XXX It might be helpful to warn about module-level global
5146 statements, but it's hard to tell the difference between
5147 module-level and a string passed to exec.
5150 for (i = 1; i < NCH(n); i += 2) {
5151 char *name = STR(CHILD(n, i));
5152 int flags;
5154 flags = symtable_lookup(st, name);
5155 if (flags < 0)
5156 continue;
5157 if (flags && flags != DEF_GLOBAL) {
5158 char buf[500];
5159 if (flags & DEF_PARAM) {
5160 PyErr_Format(PyExc_SyntaxError,
5161 "name '%.400s' is local and global",
5162 name);
5163 PyErr_SyntaxLocation(st->st_filename,
5164 st->st_cur->ste_lineno);
5165 st->st_errors++;
5166 return;
5168 else {
5169 if (flags & DEF_LOCAL)
5170 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5171 name);
5172 else
5173 sprintf(buf, GLOBAL_AFTER_USE, name);
5174 symtable_warn(st, buf);
5177 symtable_add_def(st, name, DEF_GLOBAL);
5181 static void
5182 symtable_list_comprehension(struct symtable *st, node *n)
5184 char tmpname[12];
5186 sprintf(tmpname, "_[%d]", st->st_tmpname);
5187 symtable_add_def(st, tmpname, DEF_LOCAL);
5188 symtable_assign(st, CHILD(n, 1), 0);
5189 symtable_node(st, CHILD(n, 3));
5190 if (NCH(n) == 5)
5191 symtable_node(st, CHILD(n, 4));
5194 static void
5195 symtable_import(struct symtable *st, node *n)
5197 int i;
5198 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5199 | 'from' dotted_name 'import'
5200 ('*' | import_as_name (',' import_as_name)*)
5201 import_as_name: NAME [NAME NAME]
5203 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5204 node *dotname = CHILD(n, 1);
5205 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5206 /* check for bogus imports */
5207 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5208 PyErr_SetString(PyExc_SyntaxError,
5209 LATE_FUTURE);
5210 PyErr_SyntaxLocation(st->st_filename,
5211 n->n_lineno);
5212 st->st_errors++;
5213 return;
5216 if (TYPE(CHILD(n, 3)) == STAR) {
5217 if (st->st_cur->ste_type != TYPE_MODULE) {
5218 if (symtable_warn(st,
5219 "import * only allowed at module level") < 0)
5220 return;
5222 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5223 st->st_cur->ste_opt_lineno = n->n_lineno;
5224 } else {
5225 for (i = 3; i < NCH(n); i += 2) {
5226 node *c = CHILD(n, i);
5227 if (NCH(c) > 1) /* import as */
5228 symtable_assign(st, CHILD(c, 2),
5229 DEF_IMPORT);
5230 else
5231 symtable_assign(st, CHILD(c, 0),
5232 DEF_IMPORT);
5235 } else {
5236 for (i = 1; i < NCH(n); i += 2) {
5237 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5242 static void
5243 symtable_assign(struct symtable *st, node *n, int flag)
5245 node *tmp;
5246 int i;
5248 loop:
5249 switch (TYPE(n)) {
5250 case lambdef:
5251 /* invalid assignment, e.g. lambda x:x=2. The next
5252 pass will catch this error. */
5253 return;
5254 case power:
5255 if (NCH(n) > 2) {
5256 for (i = 2; i < NCH(n); ++i)
5257 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5258 symtable_node(st, CHILD(n, i));
5260 if (NCH(n) > 1) {
5261 symtable_node(st, CHILD(n, 0));
5262 symtable_node(st, CHILD(n, 1));
5263 } else {
5264 n = CHILD(n, 0);
5265 goto loop;
5267 return;
5268 case listmaker:
5269 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5270 /* XXX This is an error, but the next pass
5271 will catch it. */
5272 return;
5273 } else {
5274 for (i = 0; i < NCH(n); i += 2)
5275 symtable_assign(st, CHILD(n, i), flag);
5277 return;
5278 case exprlist:
5279 case testlist:
5280 if (NCH(n) == 1) {
5281 n = CHILD(n, 0);
5282 goto loop;
5284 else {
5285 int i;
5286 for (i = 0; i < NCH(n); i += 2)
5287 symtable_assign(st, CHILD(n, i), flag);
5288 return;
5290 goto loop;
5291 case atom:
5292 tmp = CHILD(n, 0);
5293 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5294 n = CHILD(n, 1);
5295 goto loop;
5296 } else if (TYPE(tmp) == NAME) {
5297 if (strcmp(STR(tmp), "__debug__") == 0)
5298 symtable_warn(st, ASSIGN_DEBUG);
5299 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
5301 return;
5302 case dotted_as_name:
5303 if (NCH(n) == 3)
5304 symtable_add_def(st, STR(CHILD(n, 2)),
5305 DEF_LOCAL | flag);
5306 else
5307 symtable_add_def(st,
5308 STR(CHILD(CHILD(n,
5309 0), 0)),
5310 DEF_LOCAL | flag);
5311 return;
5312 case dotted_name:
5313 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
5314 return;
5315 case NAME:
5316 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
5317 return;
5318 default:
5319 if (NCH(n) == 0)
5320 return;
5321 if (NCH(n) == 1) {
5322 n = CHILD(n, 0);
5323 goto loop;
5325 /* Should only occur for errors like x + 1 = 1,
5326 which will be caught in the next pass. */
5327 for (i = 0; i < NCH(n); ++i)
5328 if (TYPE(CHILD(n, i)) >= single_input)
5329 symtable_assign(st, CHILD(n, i), flag);