This commit was manufactured by cvs2svn to create tag 'r22c1'.
[python/dscho.git] / Python / compile.c
blob442128495d2dbbcfbb7a465a380d511b1e224d6d
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 PyMemberDef code_memberlist[] = {
77 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
79 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
80 {"co_flags", T_INT, OFF(co_flags), READONLY},
81 {"co_code", T_OBJECT, OFF(co_code), READONLY},
82 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
83 {"co_names", T_OBJECT, OFF(co_names), READONLY},
84 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
85 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
87 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
88 {"co_name", T_OBJECT, OFF(co_name), READONLY},
89 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
91 {NULL} /* Sentinel */
94 static void
95 code_dealloc(PyCodeObject *co)
97 Py_XDECREF(co->co_code);
98 Py_XDECREF(co->co_consts);
99 Py_XDECREF(co->co_names);
100 Py_XDECREF(co->co_varnames);
101 Py_XDECREF(co->co_freevars);
102 Py_XDECREF(co->co_cellvars);
103 Py_XDECREF(co->co_filename);
104 Py_XDECREF(co->co_name);
105 Py_XDECREF(co->co_lnotab);
106 PyObject_DEL(co);
109 static PyObject *
110 code_repr(PyCodeObject *co)
112 char buf[500];
113 int lineno = -1;
114 char *filename = "???";
115 char *name = "???";
117 if (co->co_firstlineno != 0)
118 lineno = co->co_firstlineno;
119 if (co->co_filename && PyString_Check(co->co_filename))
120 filename = PyString_AS_STRING(co->co_filename);
121 if (co->co_name && PyString_Check(co->co_name))
122 name = PyString_AS_STRING(co->co_name);
123 PyOS_snprintf(buf, sizeof(buf),
124 "<code object %.100s at %p, file \"%.300s\", line %d>",
125 name, co, filename, lineno);
126 return PyString_FromString(buf);
129 static int
130 code_compare(PyCodeObject *co, PyCodeObject *cp)
132 int cmp;
133 cmp = PyObject_Compare(co->co_name, cp->co_name);
134 if (cmp) return cmp;
135 cmp = co->co_argcount - cp->co_argcount;
136 if (cmp) return cmp;
137 cmp = co->co_nlocals - cp->co_nlocals;
138 if (cmp) return cmp;
139 cmp = co->co_flags - cp->co_flags;
140 if (cmp) return cmp;
141 cmp = PyObject_Compare(co->co_code, cp->co_code);
142 if (cmp) return cmp;
143 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
144 if (cmp) return cmp;
145 cmp = PyObject_Compare(co->co_names, cp->co_names);
146 if (cmp) return cmp;
147 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
148 if (cmp) return cmp;
149 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
150 if (cmp) return cmp;
151 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
152 return cmp;
155 static long
156 code_hash(PyCodeObject *co)
158 long h, h0, h1, h2, h3, h4, h5, h6;
159 h0 = PyObject_Hash(co->co_name);
160 if (h0 == -1) return -1;
161 h1 = PyObject_Hash(co->co_code);
162 if (h1 == -1) return -1;
163 h2 = PyObject_Hash(co->co_consts);
164 if (h2 == -1) return -1;
165 h3 = PyObject_Hash(co->co_names);
166 if (h3 == -1) return -1;
167 h4 = PyObject_Hash(co->co_varnames);
168 if (h4 == -1) return -1;
169 h5 = PyObject_Hash(co->co_freevars);
170 if (h5 == -1) return -1;
171 h6 = PyObject_Hash(co->co_cellvars);
172 if (h6 == -1) return -1;
173 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
174 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
175 if (h == -1) h = -2;
176 return h;
179 /* XXX code objects need to participate in GC? */
181 PyTypeObject PyCode_Type = {
182 PyObject_HEAD_INIT(&PyType_Type)
184 "code",
185 sizeof(PyCodeObject),
187 (destructor)code_dealloc, /* tp_dealloc */
188 0, /* tp_print */
189 0, /* tp_getattr */
190 0, /* tp_setattr */
191 (cmpfunc)code_compare, /* tp_compare */
192 (reprfunc)code_repr, /* tp_repr */
193 0, /* tp_as_number */
194 0, /* tp_as_sequence */
195 0, /* tp_as_mapping */
196 (hashfunc)code_hash, /* tp_hash */
197 0, /* tp_call */
198 0, /* tp_str */
199 PyObject_GenericGetAttr, /* tp_getattro */
200 0, /* tp_setattro */
201 0, /* tp_as_buffer */
202 Py_TPFLAGS_DEFAULT, /* tp_flags */
203 0, /* tp_doc */
204 0, /* tp_traverse */
205 0, /* tp_clear */
206 0, /* tp_richcompare */
207 0, /* tp_weaklistoffset */
208 0, /* tp_iter */
209 0, /* tp_iternext */
210 0, /* tp_methods */
211 code_memberlist, /* tp_members */
212 0, /* tp_getset */
213 0, /* tp_base */
214 0, /* tp_dict */
215 0, /* tp_descr_get */
216 0, /* tp_descr_set */
217 0, /* tp_dictoffset */
218 0, /* tp_init */
219 0, /* tp_alloc */
220 0, /* tp_new */
223 #define NAME_CHARS \
224 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
226 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
228 static int
229 all_name_chars(unsigned char *s)
231 static char ok_name_char[256];
232 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
234 if (ok_name_char[*name_chars] == 0) {
235 unsigned char *p;
236 for (p = name_chars; *p; p++)
237 ok_name_char[*p] = 1;
239 while (*s) {
240 if (ok_name_char[*s++] == 0)
241 return 0;
243 return 1;
246 static int
247 intern_strings(PyObject *tuple)
249 int i;
251 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
252 PyObject *v = PyTuple_GET_ITEM(tuple, i);
253 if (v == NULL || !PyString_Check(v)) {
254 Py_FatalError("non-string found in code slot");
255 PyErr_BadInternalCall();
256 return -1;
258 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
260 return 0;
263 PyCodeObject *
264 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
265 PyObject *code, PyObject *consts, PyObject *names,
266 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
267 PyObject *filename, PyObject *name, int firstlineno,
268 PyObject *lnotab)
270 PyCodeObject *co;
271 int i;
272 /* Check argument types */
273 if (argcount < 0 || nlocals < 0 ||
274 code == NULL ||
275 consts == NULL || !PyTuple_Check(consts) ||
276 names == NULL || !PyTuple_Check(names) ||
277 varnames == NULL || !PyTuple_Check(varnames) ||
278 freevars == NULL || !PyTuple_Check(freevars) ||
279 cellvars == NULL || !PyTuple_Check(cellvars) ||
280 name == NULL || !PyString_Check(name) ||
281 filename == NULL || !PyString_Check(filename) ||
282 lnotab == NULL || !PyString_Check(lnotab) ||
283 !PyObject_CheckReadBuffer(code)) {
284 PyErr_BadInternalCall();
285 return NULL;
287 intern_strings(names);
288 intern_strings(varnames);
289 if (freevars == NULL)
290 freevars = PyTuple_New(0);
291 intern_strings(freevars);
292 if (cellvars == NULL)
293 cellvars = PyTuple_New(0);
294 intern_strings(cellvars);
295 /* Intern selected string constants */
296 for (i = PyTuple_Size(consts); --i >= 0; ) {
297 PyObject *v = PyTuple_GetItem(consts, i);
298 if (!PyString_Check(v))
299 continue;
300 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
301 continue;
302 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
304 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
305 if (co != NULL) {
306 co->co_argcount = argcount;
307 co->co_nlocals = nlocals;
308 co->co_stacksize = stacksize;
309 co->co_flags = flags;
310 Py_INCREF(code);
311 co->co_code = code;
312 Py_INCREF(consts);
313 co->co_consts = consts;
314 Py_INCREF(names);
315 co->co_names = names;
316 Py_INCREF(varnames);
317 co->co_varnames = varnames;
318 Py_INCREF(freevars);
319 co->co_freevars = freevars;
320 Py_INCREF(cellvars);
321 co->co_cellvars = cellvars;
322 Py_INCREF(filename);
323 co->co_filename = filename;
324 Py_INCREF(name);
325 co->co_name = name;
326 co->co_firstlineno = firstlineno;
327 Py_INCREF(lnotab);
328 co->co_lnotab = lnotab;
330 return co;
334 /* Data structure used internally */
336 /* The compiler uses two passes to generate bytecodes. The first pass
337 builds the symbol table. The second pass generates the bytecode.
339 The first pass uses a single symtable struct. The second pass uses
340 a compiling struct for each code block. The compiling structs
341 share a reference to the symtable.
343 The two passes communicate via symtable_load_symbols() and via
344 is_local() and is_global(). The former initializes several slots
345 in the compiling struct: c_varnames, c_locals, c_nlocals,
346 c_argcount, c_globals, and c_flags.
349 /* All about c_lnotab.
351 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
352 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
353 to source code line #s (when needed for tracebacks) via c_lnotab instead.
354 The array is conceptually a list of
355 (bytecode offset increment, line number increment)
356 pairs. The details are important and delicate, best illustrated by example:
358 byte code offset source code line number
361 50 7
362 350 307
363 361 308
365 The first trick is that these numbers aren't stored, only the increments
366 from one row to the next (this doesn't really work, but it's a start):
368 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
370 The second trick is that an unsigned byte can't hold negative values, or
371 values larger than 255, so (a) there's a deep assumption that byte code
372 offsets and their corresponding line #s both increase monotonically, and (b)
373 if at least one column jumps by more than 255 from one row to the next, more
374 than one pair is written to the table. In case #b, there's no way to know
375 from looking at the table later how many were written. That's the delicate
376 part. A user of c_lnotab desiring to find the source line number
377 corresponding to a bytecode address A should do something like this
379 lineno = addr = 0
380 for addr_incr, line_incr in c_lnotab:
381 addr += addr_incr
382 if addr > A:
383 return lineno
384 lineno += line_incr
386 In order for this to work, when the addr field increments by more than 255,
387 the line # increment in each pair generated must be 0 until the remaining addr
388 increment is < 256. So, in the example above, com_set_lineno should not (as
389 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
390 255, 0, 45, 255, 0, 45.
393 struct compiling {
394 PyObject *c_code; /* string */
395 PyObject *c_consts; /* list of objects */
396 PyObject *c_const_dict; /* inverse of c_consts */
397 PyObject *c_names; /* list of strings (names) */
398 PyObject *c_name_dict; /* inverse of c_names */
399 PyObject *c_globals; /* dictionary (value=None) */
400 PyObject *c_locals; /* dictionary (value=localID) */
401 PyObject *c_varnames; /* list (inverse of c_locals) */
402 PyObject *c_freevars; /* dictionary (value=None) */
403 PyObject *c_cellvars; /* list */
404 int c_nlocals; /* index of next local */
405 int c_argcount; /* number of top-level arguments */
406 int c_flags; /* same as co_flags */
407 int c_nexti; /* index into c_code */
408 int c_errors; /* counts errors occurred */
409 int c_infunction; /* set when compiling a function */
410 int c_interactive; /* generating code for interactive command */
411 int c_loops; /* counts nested loops */
412 int c_begin; /* begin of current loop, for 'continue' */
413 int c_block[CO_MAXBLOCKS]; /* stack of block types */
414 int c_nblocks; /* current block stack level */
415 char *c_filename; /* filename of current node */
416 char *c_name; /* name of object (e.g. function) */
417 int c_lineno; /* Current line number */
418 int c_stacklevel; /* Current stack level */
419 int c_maxstacklevel; /* Maximum stack level */
420 int c_firstlineno;
421 PyObject *c_lnotab; /* Table mapping address to line number */
422 int c_last_addr, c_last_line, c_lnotab_next;
423 char *c_private; /* for private name mangling */
424 int c_tmpname; /* temporary local name counter */
425 int c_nested; /* Is block nested funcdef or lamdef? */
426 int c_closure; /* Is nested w/freevars? */
427 struct symtable *c_symtable; /* pointer to module symbol table */
428 PyFutureFeatures *c_future; /* pointer to module's __future__ */
431 static int
432 is_free(int v)
434 if ((v & (USE | DEF_FREE))
435 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
436 return 1;
437 if (v & DEF_FREE_CLASS)
438 return 1;
439 return 0;
442 static void
443 com_error(struct compiling *c, PyObject *exc, char *msg)
445 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
447 if (c == NULL) {
448 /* Error occurred via symtable call to
449 is_constant_false */
450 PyErr_SetString(exc, msg);
451 return;
453 c->c_errors++;
454 if (c->c_lineno < 1 || c->c_interactive) {
455 /* Unknown line number or interactive input */
456 PyErr_SetString(exc, msg);
457 return;
459 v = PyString_FromString(msg);
460 if (v == NULL)
461 return; /* MemoryError, too bad */
463 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
464 if (line == NULL) {
465 Py_INCREF(Py_None);
466 line = Py_None;
468 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
469 Py_None, line);
470 if (t == NULL)
471 goto exit;
472 w = Py_BuildValue("(OO)", v, t);
473 if (w == NULL)
474 goto exit;
475 PyErr_SetObject(exc, w);
476 exit:
477 Py_XDECREF(t);
478 Py_XDECREF(v);
479 Py_XDECREF(w);
480 Py_XDECREF(line);
483 /* Interface to the block stack */
485 static void
486 block_push(struct compiling *c, int type)
488 if (c->c_nblocks >= CO_MAXBLOCKS) {
489 com_error(c, PyExc_SystemError,
490 "too many statically nested blocks");
492 else {
493 c->c_block[c->c_nblocks++] = type;
497 static void
498 block_pop(struct compiling *c, int type)
500 if (c->c_nblocks > 0)
501 c->c_nblocks--;
502 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
503 com_error(c, PyExc_SystemError, "bad block pop");
507 /* Prototype forward declarations */
509 static int com_init(struct compiling *, char *);
510 static void com_free(struct compiling *);
511 static void com_push(struct compiling *, int);
512 static void com_pop(struct compiling *, int);
513 static void com_done(struct compiling *);
514 static void com_node(struct compiling *, node *);
515 static void com_factor(struct compiling *, node *);
516 static void com_addbyte(struct compiling *, int);
517 static void com_addint(struct compiling *, int);
518 static void com_addoparg(struct compiling *, int, int);
519 static void com_addfwref(struct compiling *, int, int *);
520 static void com_backpatch(struct compiling *, int);
521 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
522 static int com_addconst(struct compiling *, PyObject *);
523 static int com_addname(struct compiling *, PyObject *);
524 static void com_addopname(struct compiling *, int, node *);
525 static void com_list(struct compiling *, node *, int);
526 static void com_list_iter(struct compiling *, node *, node *, char *);
527 static int com_argdefs(struct compiling *, node *);
528 static void com_assign(struct compiling *, node *, int, node *);
529 static void com_assign_name(struct compiling *, node *, int);
530 static PyCodeObject *icompile(node *, struct compiling *);
531 static PyCodeObject *jcompile(node *, char *, struct compiling *,
532 PyCompilerFlags *);
533 static PyObject *parsestrplus(struct compiling*, node *);
534 static PyObject *parsestr(struct compiling *, char *);
535 static node *get_rawdocstring(node *);
537 static int get_ref_type(struct compiling *, char *);
539 /* symtable operations */
540 static int symtable_build(struct compiling *, node *);
541 static int symtable_load_symbols(struct compiling *);
542 static struct symtable *symtable_init(void);
543 static void symtable_enter_scope(struct symtable *, char *, int, int);
544 static int symtable_exit_scope(struct symtable *);
545 static int symtable_add_def(struct symtable *, char *, int);
546 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
548 static void symtable_node(struct symtable *, node *);
549 static void symtable_funcdef(struct symtable *, node *);
550 static void symtable_default_args(struct symtable *, node *);
551 static void symtable_params(struct symtable *, node *);
552 static void symtable_params_fplist(struct symtable *, node *n);
553 static void symtable_global(struct symtable *, node *);
554 static void symtable_import(struct symtable *, node *);
555 static void symtable_assign(struct symtable *, node *, int);
556 static void symtable_list_comprehension(struct symtable *, node *);
558 static int symtable_update_free_vars(struct symtable *);
559 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
560 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
562 /* helper */
563 static void
564 do_pad(int pad)
566 int i;
567 for (i = 0; i < pad; ++i)
568 fprintf(stderr, " ");
571 static void
572 dump(node *n, int pad, int depth)
574 int i;
575 if (depth == 0)
576 return;
577 do_pad(pad);
578 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
579 if (depth > 0)
580 depth--;
581 for (i = 0; i < NCH(n); ++i)
582 dump(CHILD(n, i), pad + 1, depth);
585 #define DUMP(N) dump(N, 0, -1)
587 static int
588 com_init(struct compiling *c, char *filename)
590 memset((void *)c, '\0', sizeof(struct compiling));
591 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
592 1000)) == NULL)
593 goto fail;
594 if ((c->c_consts = PyList_New(0)) == NULL)
595 goto fail;
596 if ((c->c_const_dict = PyDict_New()) == NULL)
597 goto fail;
598 if ((c->c_names = PyList_New(0)) == NULL)
599 goto fail;
600 if ((c->c_name_dict = PyDict_New()) == NULL)
601 goto fail;
602 if ((c->c_locals = PyDict_New()) == NULL)
603 goto fail;
604 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
605 1000)) == NULL)
606 goto fail;
607 c->c_globals = NULL;
608 c->c_varnames = NULL;
609 c->c_freevars = NULL;
610 c->c_cellvars = NULL;
611 c->c_nlocals = 0;
612 c->c_argcount = 0;
613 c->c_flags = 0;
614 c->c_nexti = 0;
615 c->c_errors = 0;
616 c->c_infunction = 0;
617 c->c_interactive = 0;
618 c->c_loops = 0;
619 c->c_begin = 0;
620 c->c_nblocks = 0;
621 c->c_filename = filename;
622 c->c_name = "?";
623 c->c_lineno = 0;
624 c->c_stacklevel = 0;
625 c->c_maxstacklevel = 0;
626 c->c_firstlineno = 0;
627 c->c_last_addr = 0;
628 c->c_last_line = 0;
629 c->c_lnotab_next = 0;
630 c->c_tmpname = 0;
631 c->c_nested = 0;
632 c->c_closure = 0;
633 c->c_symtable = NULL;
634 return 1;
636 fail:
637 com_free(c);
638 return 0;
641 static void
642 com_free(struct compiling *c)
644 Py_XDECREF(c->c_code);
645 Py_XDECREF(c->c_consts);
646 Py_XDECREF(c->c_const_dict);
647 Py_XDECREF(c->c_names);
648 Py_XDECREF(c->c_name_dict);
649 Py_XDECREF(c->c_globals);
650 Py_XDECREF(c->c_locals);
651 Py_XDECREF(c->c_varnames);
652 Py_XDECREF(c->c_freevars);
653 Py_XDECREF(c->c_cellvars);
654 Py_XDECREF(c->c_lnotab);
655 if (c->c_future)
656 PyMem_Free((void *)c->c_future);
659 static void
660 com_push(struct compiling *c, int n)
662 c->c_stacklevel += n;
663 if (c->c_stacklevel > c->c_maxstacklevel) {
664 c->c_maxstacklevel = c->c_stacklevel;
666 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
667 c->c_filename, c->c_name, c->c_lineno,
668 c->c_nexti, c->c_stacklevel, n);
673 static void
674 com_pop(struct compiling *c, int n)
676 if (c->c_stacklevel < n)
677 c->c_stacklevel = 0;
678 else
679 c->c_stacklevel -= n;
682 static void
683 com_done(struct compiling *c)
685 if (c->c_code != NULL)
686 _PyString_Resize(&c->c_code, c->c_nexti);
687 if (c->c_lnotab != NULL)
688 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
691 static int
692 com_check_size(PyObject **s, int offset)
694 int len = PyString_GET_SIZE(*s);
695 if (offset >= len)
696 return _PyString_Resize(s, len * 2);
697 return 0;
700 static void
701 com_addbyte(struct compiling *c, int byte)
703 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
704 assert(byte >= 0 && byte <= 255);
705 assert(c->c_code);
706 if (com_check_size(&c->c_code, c->c_nexti)) {
707 c->c_errors++;
708 return;
710 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
713 static void
714 com_addint(struct compiling *c, int x)
716 com_addbyte(c, x & 0xff);
717 com_addbyte(c, x >> 8); /* XXX x should be positive */
720 static void
721 com_add_lnotab(struct compiling *c, int addr, int line)
723 char *p;
724 if (c->c_lnotab == NULL)
725 return;
726 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
727 c->c_errors++;
728 return;
730 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
731 *p++ = addr;
732 *p++ = line;
733 c->c_lnotab_next += 2;
736 static void
737 com_set_lineno(struct compiling *c, int lineno)
739 c->c_lineno = lineno;
740 if (c->c_firstlineno == 0) {
741 c->c_firstlineno = c->c_last_line = lineno;
743 else {
744 int incr_addr = c->c_nexti - c->c_last_addr;
745 int incr_line = lineno - c->c_last_line;
746 while (incr_addr > 255) {
747 com_add_lnotab(c, 255, 0);
748 incr_addr -= 255;
750 while (incr_line > 255) {
751 com_add_lnotab(c, incr_addr, 255);
752 incr_line -=255;
753 incr_addr = 0;
755 if (incr_addr > 0 || incr_line > 0)
756 com_add_lnotab(c, incr_addr, incr_line);
757 c->c_last_addr = c->c_nexti;
758 c->c_last_line = lineno;
762 static void
763 com_addoparg(struct compiling *c, int op, int arg)
765 int extended_arg = arg >> 16;
766 if (op == SET_LINENO) {
767 com_set_lineno(c, arg);
768 if (Py_OptimizeFlag)
769 return;
771 if (extended_arg){
772 com_addbyte(c, EXTENDED_ARG);
773 com_addint(c, extended_arg);
774 arg &= 0xffff;
776 com_addbyte(c, op);
777 com_addint(c, arg);
780 static void
781 com_addfwref(struct compiling *c, int op, int *p_anchor)
783 /* Compile a forward reference for backpatching */
784 int here;
785 int anchor;
786 com_addbyte(c, op);
787 here = c->c_nexti;
788 anchor = *p_anchor;
789 *p_anchor = here;
790 com_addint(c, anchor == 0 ? 0 : here - anchor);
793 static void
794 com_backpatch(struct compiling *c, int anchor)
796 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
797 int target = c->c_nexti;
798 int dist;
799 int prev;
800 for (;;) {
801 /* Make the JUMP instruction at anchor point to target */
802 prev = code[anchor] + (code[anchor+1] << 8);
803 dist = target - (anchor+2);
804 code[anchor] = dist & 0xff;
805 dist >>= 8;
806 code[anchor+1] = dist;
807 dist >>= 8;
808 if (dist) {
809 com_error(c, PyExc_SystemError,
810 "com_backpatch: offset too large");
811 break;
813 if (!prev)
814 break;
815 anchor -= prev;
819 /* Handle literals and names uniformly */
821 static int
822 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
824 PyObject *w, *t, *np=NULL;
825 long n;
827 t = Py_BuildValue("(OO)", v, v->ob_type);
828 if (t == NULL)
829 goto fail;
830 w = PyDict_GetItem(dict, t);
831 if (w != NULL) {
832 n = PyInt_AsLong(w);
833 } else {
834 n = PyList_Size(list);
835 np = PyInt_FromLong(n);
836 if (np == NULL)
837 goto fail;
838 if (PyList_Append(list, v) != 0)
839 goto fail;
840 if (PyDict_SetItem(dict, t, np) != 0)
841 goto fail;
842 Py_DECREF(np);
844 Py_DECREF(t);
845 return n;
846 fail:
847 Py_XDECREF(np);
848 Py_XDECREF(t);
849 c->c_errors++;
850 return 0;
853 static int
854 com_addconst(struct compiling *c, PyObject *v)
856 return com_add(c, c->c_consts, c->c_const_dict, v);
859 static int
860 com_addname(struct compiling *c, PyObject *v)
862 return com_add(c, c->c_names, c->c_name_dict, v);
865 static int
866 mangle(char *p, char *name, char *buffer, size_t maxlen)
868 /* Name mangling: __private becomes _classname__private.
869 This is independent from how the name is used. */
870 size_t nlen, plen;
871 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
872 return 0;
873 nlen = strlen(name);
874 if (nlen+2 >= maxlen)
875 return 0; /* Don't mangle __extremely_long_names */
876 if (name[nlen-1] == '_' && name[nlen-2] == '_')
877 return 0; /* Don't mangle __whatever__ */
878 /* Strip leading underscores from class name */
879 while (*p == '_')
880 p++;
881 if (*p == '\0')
882 return 0; /* Don't mangle if class is just underscores */
883 plen = strlen(p);
884 if (plen + nlen >= maxlen)
885 plen = maxlen-nlen-2; /* Truncate class name if too long */
886 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
887 buffer[0] = '_';
888 strncpy(buffer+1, p, plen);
889 strcpy(buffer+1+plen, name);
890 return 1;
893 static void
894 com_addop_name(struct compiling *c, int op, char *name)
896 PyObject *v;
897 int i;
898 char buffer[MANGLE_LEN];
900 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
901 name = buffer;
902 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
903 c->c_errors++;
904 i = 255;
906 else {
907 i = com_addname(c, v);
908 Py_DECREF(v);
910 com_addoparg(c, op, i);
913 #define NAME_LOCAL 0
914 #define NAME_GLOBAL 1
915 #define NAME_DEFAULT 2
916 #define NAME_CLOSURE 3
918 static int
919 com_lookup_arg(PyObject *dict, PyObject *name)
921 PyObject *v = PyDict_GetItem(dict, name);
922 if (v == NULL)
923 return -1;
924 else
925 return PyInt_AS_LONG(v);
928 static void
929 com_addop_varname(struct compiling *c, int kind, char *name)
931 PyObject *v;
932 int i, reftype;
933 int scope = NAME_DEFAULT;
934 int op = STOP_CODE;
935 char buffer[MANGLE_LEN];
937 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
938 name = buffer;
939 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
940 c->c_errors++;
941 i = 255;
942 goto done;
945 reftype = get_ref_type(c, name);
946 switch (reftype) {
947 case LOCAL:
948 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
949 scope = NAME_LOCAL;
950 break;
951 case GLOBAL_EXPLICIT:
952 scope = NAME_GLOBAL;
953 break;
954 case GLOBAL_IMPLICIT:
955 if (c->c_flags & CO_OPTIMIZED)
956 scope = NAME_GLOBAL;
957 break;
958 case FREE:
959 case CELL:
960 scope = NAME_CLOSURE;
961 break;
964 i = com_addname(c, v);
965 if (scope == NAME_LOCAL)
966 i = com_lookup_arg(c->c_locals, v);
967 else if (reftype == FREE)
968 i = com_lookup_arg(c->c_freevars, v);
969 else if (reftype == CELL)
970 i = com_lookup_arg(c->c_cellvars, v);
971 if (i == -1) {
972 c->c_errors++; /* XXX no exception set */
973 i = 255;
974 goto done;
976 Py_DECREF(v);
978 switch (kind) {
979 case VAR_LOAD:
980 switch (scope) {
981 case NAME_LOCAL:
982 op = LOAD_FAST;
983 break;
984 case NAME_GLOBAL:
985 op = LOAD_GLOBAL;
986 break;
987 case NAME_DEFAULT:
988 op = LOAD_NAME;
989 break;
990 case NAME_CLOSURE:
991 op = LOAD_DEREF;
992 break;
994 break;
995 case VAR_STORE:
996 switch (scope) {
997 case NAME_LOCAL:
998 op = STORE_FAST;
999 break;
1000 case NAME_GLOBAL:
1001 op = STORE_GLOBAL;
1002 break;
1003 case NAME_DEFAULT:
1004 op = STORE_NAME;
1005 break;
1006 case NAME_CLOSURE:
1007 op = STORE_DEREF;
1008 break;
1010 break;
1011 case VAR_DELETE:
1012 switch (scope) {
1013 case NAME_LOCAL:
1014 op = DELETE_FAST;
1015 break;
1016 case NAME_GLOBAL:
1017 op = DELETE_GLOBAL;
1018 break;
1019 case NAME_DEFAULT:
1020 op = DELETE_NAME;
1021 break;
1022 case NAME_CLOSURE: {
1023 char buf[500];
1024 PyOS_snprintf(buf, sizeof(buf),
1025 DEL_CLOSURE_ERROR, name);
1026 com_error(c, PyExc_SyntaxError, buf);
1027 i = 255;
1028 break;
1031 break;
1033 done:
1034 com_addoparg(c, op, i);
1037 static void
1038 com_addopname(struct compiling *c, int op, node *n)
1040 char *name;
1041 char buffer[1000];
1042 /* XXX it is possible to write this code without the 1000
1043 chars on the total length of dotted names, I just can't be
1044 bothered right now */
1045 if (TYPE(n) == STAR)
1046 name = "*";
1047 else if (TYPE(n) == dotted_name) {
1048 char *p = buffer;
1049 int i;
1050 name = buffer;
1051 for (i = 0; i < NCH(n); i += 2) {
1052 char *s = STR(CHILD(n, i));
1053 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
1054 com_error(c, PyExc_MemoryError,
1055 "dotted_name too long");
1056 name = NULL;
1057 break;
1059 if (p != buffer)
1060 *p++ = '.';
1061 strcpy(p, s);
1062 p = strchr(p, '\0');
1065 else {
1066 REQ(n, NAME);
1067 name = STR(n);
1069 com_addop_name(c, op, name);
1072 static PyObject *
1073 parsenumber(struct compiling *co, char *s)
1075 char *end;
1076 long x;
1077 double dx;
1078 #ifndef WITHOUT_COMPLEX
1079 Py_complex c;
1080 int imflag;
1081 #endif
1083 errno = 0;
1084 end = s + strlen(s) - 1;
1085 #ifndef WITHOUT_COMPLEX
1086 imflag = *end == 'j' || *end == 'J';
1087 #endif
1088 if (*end == 'l' || *end == 'L')
1089 return PyLong_FromString(s, (char **)0, 0);
1090 if (s[0] == '0')
1091 x = (long) PyOS_strtoul(s, &end, 0);
1092 else
1093 x = PyOS_strtol(s, &end, 0);
1094 if (*end == '\0') {
1095 if (errno != 0)
1096 return PyLong_FromString(s, (char **)0, 0);
1097 return PyInt_FromLong(x);
1099 /* XXX Huge floats may silently fail */
1100 #ifndef WITHOUT_COMPLEX
1101 if (imflag) {
1102 c.real = 0.;
1103 PyFPE_START_PROTECT("atof", return 0)
1104 c.imag = atof(s);
1105 PyFPE_END_PROTECT(c)
1106 return PyComplex_FromCComplex(c);
1108 else
1109 #endif
1111 PyFPE_START_PROTECT("atof", return 0)
1112 dx = atof(s);
1113 PyFPE_END_PROTECT(dx)
1114 return PyFloat_FromDouble(dx);
1118 static PyObject *
1119 parsestr(struct compiling *com, char *s)
1121 PyObject *v;
1122 size_t len;
1123 char *buf;
1124 char *p;
1125 char *end;
1126 int c;
1127 int first = *s;
1128 int quote = first;
1129 int rawmode = 0;
1130 #ifdef Py_USING_UNICODE
1131 int unicode = 0;
1132 #endif
1133 if (isalpha(quote) || quote == '_') {
1134 if (quote == 'u' || quote == 'U') {
1135 #ifdef Py_USING_UNICODE
1136 quote = *++s;
1137 unicode = 1;
1138 #else
1139 com_error(com, PyExc_SyntaxError,
1140 "Unicode literals not supported in this Python");
1141 return NULL;
1142 #endif
1144 if (quote == 'r' || quote == 'R') {
1145 quote = *++s;
1146 rawmode = 1;
1149 if (quote != '\'' && quote != '\"') {
1150 PyErr_BadInternalCall();
1151 return NULL;
1153 s++;
1154 len = strlen(s);
1155 if (len > INT_MAX) {
1156 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1157 return NULL;
1159 if (s[--len] != quote) {
1160 PyErr_BadInternalCall();
1161 return NULL;
1163 if (len >= 4 && s[0] == quote && s[1] == quote) {
1164 s += 2;
1165 len -= 2;
1166 if (s[--len] != quote || s[--len] != quote) {
1167 PyErr_BadInternalCall();
1168 return NULL;
1171 #ifdef Py_USING_UNICODE
1172 if (unicode || Py_UnicodeFlag) {
1173 if (rawmode)
1174 return PyUnicode_DecodeRawUnicodeEscape(
1175 s, len, NULL);
1176 else
1177 return PyUnicode_DecodeUnicodeEscape(
1178 s, len, NULL);
1180 #endif
1181 if (rawmode || strchr(s, '\\') == NULL)
1182 return PyString_FromStringAndSize(s, len);
1183 v = PyString_FromStringAndSize((char *)NULL, len);
1184 if (v == NULL)
1185 return NULL;
1186 p = buf = PyString_AsString(v);
1187 end = s + len;
1188 while (s < end) {
1189 if (*s != '\\') {
1190 *p++ = *s++;
1191 continue;
1193 s++;
1194 switch (*s++) {
1195 /* XXX This assumes ASCII! */
1196 case '\n': break;
1197 case '\\': *p++ = '\\'; break;
1198 case '\'': *p++ = '\''; break;
1199 case '\"': *p++ = '\"'; break;
1200 case 'b': *p++ = '\b'; break;
1201 case 'f': *p++ = '\014'; break; /* FF */
1202 case 't': *p++ = '\t'; break;
1203 case 'n': *p++ = '\n'; break;
1204 case 'r': *p++ = '\r'; break;
1205 case 'v': *p++ = '\013'; break; /* VT */
1206 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1207 case '0': case '1': case '2': case '3':
1208 case '4': case '5': case '6': case '7':
1209 c = s[-1] - '0';
1210 if ('0' <= *s && *s <= '7') {
1211 c = (c<<3) + *s++ - '0';
1212 if ('0' <= *s && *s <= '7')
1213 c = (c<<3) + *s++ - '0';
1215 *p++ = c;
1216 break;
1217 case 'x':
1218 if (isxdigit(Py_CHARMASK(s[0]))
1219 && isxdigit(Py_CHARMASK(s[1]))) {
1220 unsigned int x = 0;
1221 c = Py_CHARMASK(*s);
1222 s++;
1223 if (isdigit(c))
1224 x = c - '0';
1225 else if (islower(c))
1226 x = 10 + c - 'a';
1227 else
1228 x = 10 + c - 'A';
1229 x = x << 4;
1230 c = Py_CHARMASK(*s);
1231 s++;
1232 if (isdigit(c))
1233 x += c - '0';
1234 else if (islower(c))
1235 x += 10 + c - 'a';
1236 else
1237 x += 10 + c - 'A';
1238 *p++ = x;
1239 break;
1241 PyErr_SetString(PyExc_ValueError,
1242 "invalid \\x escape");
1243 Py_DECREF(v);
1244 return NULL;
1245 default:
1246 *p++ = '\\';
1247 *p++ = s[-1];
1248 break;
1251 _PyString_Resize(&v, (int)(p - buf));
1252 return v;
1255 static PyObject *
1256 parsestrplus(struct compiling* c, node *n)
1258 PyObject *v;
1259 int i;
1260 REQ(CHILD(n, 0), STRING);
1261 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
1262 /* String literal concatenation */
1263 for (i = 1; i < NCH(n); i++) {
1264 PyObject *s;
1265 s = parsestr(c, STR(CHILD(n, i)));
1266 if (s == NULL)
1267 goto onError;
1268 if (PyString_Check(v) && PyString_Check(s)) {
1269 PyString_ConcatAndDel(&v, s);
1270 if (v == NULL)
1271 goto onError;
1273 #ifdef Py_USING_UNICODE
1274 else {
1275 PyObject *temp;
1276 temp = PyUnicode_Concat(v, s);
1277 Py_DECREF(s);
1278 if (temp == NULL)
1279 goto onError;
1280 Py_DECREF(v);
1281 v = temp;
1283 #endif
1286 return v;
1288 onError:
1289 Py_XDECREF(v);
1290 return NULL;
1293 static void
1294 com_list_for(struct compiling *c, node *n, node *e, char *t)
1296 int anchor = 0;
1297 int save_begin = c->c_begin;
1299 /* list_iter: for v in expr [list_iter] */
1300 com_node(c, CHILD(n, 3)); /* expr */
1301 com_addbyte(c, GET_ITER);
1302 c->c_begin = c->c_nexti;
1303 com_addoparg(c, SET_LINENO, n->n_lineno);
1304 com_addfwref(c, FOR_ITER, &anchor);
1305 com_push(c, 1);
1306 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1307 c->c_loops++;
1308 com_list_iter(c, n, e, t);
1309 c->c_loops--;
1310 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1311 c->c_begin = save_begin;
1312 com_backpatch(c, anchor);
1313 com_pop(c, 1); /* FOR_ITER has popped this */
1316 static void
1317 com_list_if(struct compiling *c, node *n, node *e, char *t)
1319 int anchor = 0;
1320 int a = 0;
1321 /* list_iter: 'if' test [list_iter] */
1322 com_addoparg(c, SET_LINENO, n->n_lineno);
1323 com_node(c, CHILD(n, 1));
1324 com_addfwref(c, JUMP_IF_FALSE, &a);
1325 com_addbyte(c, POP_TOP);
1326 com_pop(c, 1);
1327 com_list_iter(c, n, e, t);
1328 com_addfwref(c, JUMP_FORWARD, &anchor);
1329 com_backpatch(c, a);
1330 /* We jump here with an extra entry which we now pop */
1331 com_addbyte(c, POP_TOP);
1332 com_backpatch(c, anchor);
1335 static void
1336 com_list_iter(struct compiling *c,
1337 node *p, /* parent of list_iter node */
1338 node *e, /* element expression node */
1339 char *t /* name of result list temp local */)
1341 /* list_iter is the last child in a listmaker, list_for, or list_if */
1342 node *n = CHILD(p, NCH(p)-1);
1343 if (TYPE(n) == list_iter) {
1344 n = CHILD(n, 0);
1345 switch (TYPE(n)) {
1346 case list_for:
1347 com_list_for(c, n, e, t);
1348 break;
1349 case list_if:
1350 com_list_if(c, n, e, t);
1351 break;
1352 default:
1353 com_error(c, PyExc_SystemError,
1354 "invalid list_iter node type");
1357 else {
1358 com_addop_varname(c, VAR_LOAD, t);
1359 com_push(c, 1);
1360 com_node(c, e);
1361 com_addoparg(c, CALL_FUNCTION, 1);
1362 com_addbyte(c, POP_TOP);
1363 com_pop(c, 2);
1367 static void
1368 com_list_comprehension(struct compiling *c, node *n)
1370 /* listmaker: test list_for */
1371 char tmpname[30];
1372 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
1373 com_addoparg(c, BUILD_LIST, 0);
1374 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1375 com_push(c, 2);
1376 com_addop_name(c, LOAD_ATTR, "append");
1377 com_addop_varname(c, VAR_STORE, tmpname);
1378 com_pop(c, 1);
1379 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1380 com_addop_varname(c, VAR_DELETE, tmpname);
1381 --c->c_tmpname;
1384 static void
1385 com_listmaker(struct compiling *c, node *n)
1387 /* listmaker: test ( list_for | (',' test)* [','] ) */
1388 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1389 com_list_comprehension(c, n);
1390 else {
1391 int len = 0;
1392 int i;
1393 for (i = 0; i < NCH(n); i += 2, len++)
1394 com_node(c, CHILD(n, i));
1395 com_addoparg(c, BUILD_LIST, len);
1396 com_pop(c, len-1);
1400 static void
1401 com_dictmaker(struct compiling *c, node *n)
1403 int i;
1404 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1405 for (i = 0; i+2 < NCH(n); i += 4) {
1406 /* We must arrange things just right for STORE_SUBSCR.
1407 It wants the stack to look like (value) (dict) (key) */
1408 com_addbyte(c, DUP_TOP);
1409 com_push(c, 1);
1410 com_node(c, CHILD(n, i+2)); /* value */
1411 com_addbyte(c, ROT_TWO);
1412 com_node(c, CHILD(n, i)); /* key */
1413 com_addbyte(c, STORE_SUBSCR);
1414 com_pop(c, 3);
1418 static void
1419 com_atom(struct compiling *c, node *n)
1421 node *ch;
1422 PyObject *v;
1423 int i;
1424 REQ(n, atom);
1425 ch = CHILD(n, 0);
1426 switch (TYPE(ch)) {
1427 case LPAR:
1428 if (TYPE(CHILD(n, 1)) == RPAR) {
1429 com_addoparg(c, BUILD_TUPLE, 0);
1430 com_push(c, 1);
1432 else
1433 com_node(c, CHILD(n, 1));
1434 break;
1435 case LSQB: /* '[' [listmaker] ']' */
1436 if (TYPE(CHILD(n, 1)) == RSQB) {
1437 com_addoparg(c, BUILD_LIST, 0);
1438 com_push(c, 1);
1440 else
1441 com_listmaker(c, CHILD(n, 1));
1442 break;
1443 case LBRACE: /* '{' [dictmaker] '}' */
1444 com_addoparg(c, BUILD_MAP, 0);
1445 com_push(c, 1);
1446 if (TYPE(CHILD(n, 1)) == dictmaker)
1447 com_dictmaker(c, CHILD(n, 1));
1448 break;
1449 case BACKQUOTE:
1450 com_node(c, CHILD(n, 1));
1451 com_addbyte(c, UNARY_CONVERT);
1452 break;
1453 case NUMBER:
1454 if ((v = parsenumber(c, STR(ch))) == NULL) {
1455 i = 255;
1457 else {
1458 i = com_addconst(c, v);
1459 Py_DECREF(v);
1461 com_addoparg(c, LOAD_CONST, i);
1462 com_push(c, 1);
1463 break;
1464 case STRING:
1465 v = parsestrplus(c, n);
1466 if (v == NULL) {
1467 c->c_errors++;
1468 i = 255;
1470 else {
1471 i = com_addconst(c, v);
1472 Py_DECREF(v);
1474 com_addoparg(c, LOAD_CONST, i);
1475 com_push(c, 1);
1476 break;
1477 case NAME:
1478 com_addop_varname(c, VAR_LOAD, STR(ch));
1479 com_push(c, 1);
1480 break;
1481 default:
1482 com_error(c, PyExc_SystemError,
1483 "com_atom: unexpected node type");
1487 static void
1488 com_slice(struct compiling *c, node *n, int op)
1490 if (NCH(n) == 1) {
1491 com_addbyte(c, op);
1493 else if (NCH(n) == 2) {
1494 if (TYPE(CHILD(n, 0)) != COLON) {
1495 com_node(c, CHILD(n, 0));
1496 com_addbyte(c, op+1);
1498 else {
1499 com_node(c, CHILD(n, 1));
1500 com_addbyte(c, op+2);
1502 com_pop(c, 1);
1504 else {
1505 com_node(c, CHILD(n, 0));
1506 com_node(c, CHILD(n, 2));
1507 com_addbyte(c, op+3);
1508 com_pop(c, 2);
1512 static void
1513 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1515 if (NCH(n) == 1) {
1516 com_addbyte(c, DUP_TOP);
1517 com_push(c, 1);
1518 com_addbyte(c, SLICE);
1519 com_node(c, augn);
1520 com_addbyte(c, opcode);
1521 com_pop(c, 1);
1522 com_addbyte(c, ROT_TWO);
1523 com_addbyte(c, STORE_SLICE);
1524 com_pop(c, 2);
1525 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1526 com_node(c, CHILD(n, 0));
1527 com_addoparg(c, DUP_TOPX, 2);
1528 com_push(c, 2);
1529 com_addbyte(c, SLICE+1);
1530 com_pop(c, 1);
1531 com_node(c, augn);
1532 com_addbyte(c, opcode);
1533 com_pop(c, 1);
1534 com_addbyte(c, ROT_THREE);
1535 com_addbyte(c, STORE_SLICE+1);
1536 com_pop(c, 3);
1537 } else if (NCH(n) == 2) {
1538 com_node(c, CHILD(n, 1));
1539 com_addoparg(c, DUP_TOPX, 2);
1540 com_push(c, 2);
1541 com_addbyte(c, SLICE+2);
1542 com_pop(c, 1);
1543 com_node(c, augn);
1544 com_addbyte(c, opcode);
1545 com_pop(c, 1);
1546 com_addbyte(c, ROT_THREE);
1547 com_addbyte(c, STORE_SLICE+2);
1548 com_pop(c, 3);
1549 } else {
1550 com_node(c, CHILD(n, 0));
1551 com_node(c, CHILD(n, 2));
1552 com_addoparg(c, DUP_TOPX, 3);
1553 com_push(c, 3);
1554 com_addbyte(c, SLICE+3);
1555 com_pop(c, 2);
1556 com_node(c, augn);
1557 com_addbyte(c, opcode);
1558 com_pop(c, 1);
1559 com_addbyte(c, ROT_FOUR);
1560 com_addbyte(c, STORE_SLICE+3);
1561 com_pop(c, 4);
1565 static void
1566 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1568 node *m;
1569 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1570 if (NCH(n) == 1) {
1571 if (*pkeywords != NULL) {
1572 com_error(c, PyExc_SyntaxError,
1573 "non-keyword arg after keyword arg");
1575 else {
1576 com_node(c, CHILD(n, 0));
1578 return;
1580 m = n;
1581 do {
1582 m = CHILD(m, 0);
1583 } while (NCH(m) == 1);
1584 if (TYPE(m) != NAME) {
1585 /* f(lambda x: x[0] = 3) ends up getting parsed with
1586 * LHS test = lambda x: x[0], and RHS test = 3.
1587 * SF bug 132313 points out that complaining about a keyword
1588 * then is very confusing.
1590 com_error(c, PyExc_SyntaxError,
1591 TYPE(m) == lambdef ?
1592 "lambda cannot contain assignment" :
1593 "keyword can't be an expression");
1595 else {
1596 PyObject *v = PyString_InternFromString(STR(m));
1597 if (v != NULL && *pkeywords == NULL)
1598 *pkeywords = PyDict_New();
1599 if (v == NULL)
1600 c->c_errors++;
1601 else if (*pkeywords == NULL) {
1602 c->c_errors++;
1603 Py_DECREF(v);
1604 } else {
1605 if (PyDict_GetItem(*pkeywords, v) != NULL)
1606 com_error(c, PyExc_SyntaxError,
1607 "duplicate keyword argument");
1608 else
1609 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1610 c->c_errors++;
1611 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1612 com_push(c, 1);
1613 Py_DECREF(v);
1616 com_node(c, CHILD(n, 2));
1619 static void
1620 com_call_function(struct compiling *c, node *n)
1622 if (TYPE(n) == RPAR) {
1623 com_addoparg(c, CALL_FUNCTION, 0);
1625 else {
1626 PyObject *keywords = NULL;
1627 int i, na, nk;
1628 int lineno = n->n_lineno;
1629 int star_flag = 0;
1630 int starstar_flag = 0;
1631 int opcode;
1632 REQ(n, arglist);
1633 na = 0;
1634 nk = 0;
1635 for (i = 0; i < NCH(n); i += 2) {
1636 node *ch = CHILD(n, i);
1637 if (TYPE(ch) == STAR ||
1638 TYPE(ch) == DOUBLESTAR)
1639 break;
1640 if (ch->n_lineno != lineno) {
1641 lineno = ch->n_lineno;
1642 com_addoparg(c, SET_LINENO, lineno);
1644 com_argument(c, ch, &keywords);
1645 if (keywords == NULL)
1646 na++;
1647 else
1648 nk++;
1650 Py_XDECREF(keywords);
1651 while (i < NCH(n)) {
1652 node *tok = CHILD(n, i);
1653 node *ch = CHILD(n, i+1);
1654 i += 3;
1655 switch (TYPE(tok)) {
1656 case STAR: star_flag = 1; break;
1657 case DOUBLESTAR: starstar_flag = 1; break;
1659 com_node(c, ch);
1661 if (na > 255 || nk > 255) {
1662 com_error(c, PyExc_SyntaxError,
1663 "more than 255 arguments");
1665 if (star_flag || starstar_flag)
1666 opcode = CALL_FUNCTION_VAR - 1 +
1667 star_flag + (starstar_flag << 1);
1668 else
1669 opcode = CALL_FUNCTION;
1670 com_addoparg(c, opcode, na | (nk << 8));
1671 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1675 static void
1676 com_select_member(struct compiling *c, node *n)
1678 com_addopname(c, LOAD_ATTR, n);
1681 static void
1682 com_sliceobj(struct compiling *c, node *n)
1684 int i=0;
1685 int ns=2; /* number of slice arguments */
1686 node *ch;
1688 /* first argument */
1689 if (TYPE(CHILD(n,i)) == COLON) {
1690 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1691 com_push(c, 1);
1692 i++;
1694 else {
1695 com_node(c, CHILD(n,i));
1696 i++;
1697 REQ(CHILD(n,i),COLON);
1698 i++;
1700 /* second argument */
1701 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1702 com_node(c, CHILD(n,i));
1703 i++;
1705 else {
1706 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1707 com_push(c, 1);
1709 /* remaining arguments */
1710 for (; i < NCH(n); i++) {
1711 ns++;
1712 ch=CHILD(n,i);
1713 REQ(ch, sliceop);
1714 if (NCH(ch) == 1) {
1715 /* right argument of ':' missing */
1716 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1717 com_push(c, 1);
1719 else
1720 com_node(c, CHILD(ch,1));
1722 com_addoparg(c, BUILD_SLICE, ns);
1723 com_pop(c, 1 + (ns == 3));
1726 static void
1727 com_subscript(struct compiling *c, node *n)
1729 node *ch;
1730 REQ(n, subscript);
1731 ch = CHILD(n,0);
1732 /* check for rubber index */
1733 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1734 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1735 com_push(c, 1);
1737 else {
1738 /* check for slice */
1739 if ((TYPE(ch) == COLON || NCH(n) > 1))
1740 com_sliceobj(c, n);
1741 else {
1742 REQ(ch, test);
1743 com_node(c, ch);
1748 static void
1749 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1751 int i, op;
1752 REQ(n, subscriptlist);
1753 /* Check to make backward compatible slice behavior for '[i:j]' */
1754 if (NCH(n) == 1) {
1755 node *sub = CHILD(n, 0); /* subscript */
1756 /* 'Basic' slice, should have exactly one colon. */
1757 if ((TYPE(CHILD(sub, 0)) == COLON
1758 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1759 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1761 switch (assigning) {
1762 case OP_DELETE:
1763 op = DELETE_SLICE;
1764 break;
1765 case OP_ASSIGN:
1766 op = STORE_SLICE;
1767 break;
1768 case OP_APPLY:
1769 op = SLICE;
1770 break;
1771 default:
1772 com_augassign_slice(c, sub, assigning, augn);
1773 return;
1775 com_slice(c, sub, op);
1776 if (op == STORE_SLICE)
1777 com_pop(c, 2);
1778 else if (op == DELETE_SLICE)
1779 com_pop(c, 1);
1780 return;
1783 /* Else normal subscriptlist. Compile each subscript. */
1784 for (i = 0; i < NCH(n); i += 2)
1785 com_subscript(c, CHILD(n, i));
1786 /* Put multiple subscripts into a tuple */
1787 if (NCH(n) > 1) {
1788 i = (NCH(n)+1) / 2;
1789 com_addoparg(c, BUILD_TUPLE, i);
1790 com_pop(c, i-1);
1792 switch (assigning) {
1793 case OP_DELETE:
1794 op = DELETE_SUBSCR;
1795 i = 2;
1796 break;
1797 default:
1798 case OP_ASSIGN:
1799 op = STORE_SUBSCR;
1800 i = 3;
1801 break;
1802 case OP_APPLY:
1803 op = BINARY_SUBSCR;
1804 i = 1;
1805 break;
1807 if (assigning > OP_APPLY) {
1808 com_addoparg(c, DUP_TOPX, 2);
1809 com_push(c, 2);
1810 com_addbyte(c, BINARY_SUBSCR);
1811 com_pop(c, 1);
1812 com_node(c, augn);
1813 com_addbyte(c, assigning);
1814 com_pop(c, 1);
1815 com_addbyte(c, ROT_THREE);
1817 com_addbyte(c, op);
1818 com_pop(c, i);
1821 static void
1822 com_apply_trailer(struct compiling *c, node *n)
1824 REQ(n, trailer);
1825 switch (TYPE(CHILD(n, 0))) {
1826 case LPAR:
1827 com_call_function(c, CHILD(n, 1));
1828 break;
1829 case DOT:
1830 com_select_member(c, CHILD(n, 1));
1831 break;
1832 case LSQB:
1833 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1834 break;
1835 default:
1836 com_error(c, PyExc_SystemError,
1837 "com_apply_trailer: unknown trailer type");
1841 static void
1842 com_power(struct compiling *c, node *n)
1844 int i;
1845 REQ(n, power);
1846 com_atom(c, CHILD(n, 0));
1847 for (i = 1; i < NCH(n); i++) {
1848 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1849 com_factor(c, CHILD(n, i+1));
1850 com_addbyte(c, BINARY_POWER);
1851 com_pop(c, 1);
1852 break;
1854 else
1855 com_apply_trailer(c, CHILD(n, i));
1859 static void
1860 com_invert_constant(struct compiling *c, node *n)
1862 /* Compute the inverse of int and longs and use them directly,
1863 but be prepared to generate code for all other
1864 possibilities (invalid numbers, floats, complex).
1866 PyObject *num, *inv = NULL;
1867 int i;
1869 REQ(n, NUMBER);
1870 num = parsenumber(c, STR(n));
1871 if (num == NULL)
1872 i = 255;
1873 else {
1874 inv = PyNumber_Invert(num);
1875 if (inv == NULL) {
1876 PyErr_Clear();
1877 i = com_addconst(c, num);
1878 } else {
1879 i = com_addconst(c, inv);
1880 Py_DECREF(inv);
1882 Py_DECREF(num);
1884 com_addoparg(c, LOAD_CONST, i);
1885 com_push(c, 1);
1886 if (num != NULL && inv == NULL)
1887 com_addbyte(c, UNARY_INVERT);
1890 static int
1891 is_float_zero(const char *p)
1893 int found_radix_point = 0;
1894 int ch;
1895 while ((ch = Py_CHARMASK(*p++)) != '\0') {
1896 switch (ch) {
1897 case '0':
1898 /* no reason to believe it's not 0 -- continue */
1899 break;
1901 case 'e': case 'E': case 'j': case 'J':
1902 /* If this was a hex constant, we already would have
1903 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1904 must be an exponent marker, and we haven't yet
1905 seen a non-zero digit, and it doesn't matter what
1906 the exponent is then. For 'j' or 'J' similarly,
1907 except that this is an imaginary 0 then. */
1908 return 1;
1910 case '.':
1911 found_radix_point = 1;
1912 break;
1914 default:
1915 return 0;
1918 return found_radix_point;
1921 static void
1922 com_factor(struct compiling *c, node *n)
1924 int childtype = TYPE(CHILD(n, 0));
1925 node *pfactor, *ppower, *patom, *pnum;
1926 REQ(n, factor);
1927 /* If the unary +, -, or ~ operator is applied to a constant,
1928 don't generate a UNARY_xxx opcode. Just store the
1929 approriate value as a constant. If the value is negative,
1930 extend the string containing the constant and insert a
1931 negative in the 0th position -- unless we're doing unary minus
1932 of a floating zero! In that case the sign is significant, but
1933 the const dict can't distinguish +0.0 from -0.0.
1935 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
1936 && NCH(n) == 2
1937 && TYPE((pfactor = CHILD(n, 1))) == factor
1938 && NCH(pfactor) == 1
1939 && TYPE((ppower = CHILD(pfactor, 0))) == power
1940 && NCH(ppower) == 1
1941 && TYPE((patom = CHILD(ppower, 0))) == atom
1942 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
1943 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
1944 if (childtype == TILDE) {
1945 com_invert_constant(c, pnum);
1946 return;
1948 if (childtype == MINUS) {
1949 char *s = malloc(strlen(STR(pnum)) + 2);
1950 if (s == NULL) {
1951 com_error(c, PyExc_MemoryError, "");
1952 com_addbyte(c, 255);
1953 return;
1955 s[0] = '-';
1956 strcpy(s + 1, STR(pnum));
1957 free(STR(pnum));
1958 STR(pnum) = s;
1960 com_atom(c, patom);
1962 else if (childtype == PLUS) {
1963 com_factor(c, CHILD(n, 1));
1964 com_addbyte(c, UNARY_POSITIVE);
1966 else if (childtype == MINUS) {
1967 com_factor(c, CHILD(n, 1));
1968 com_addbyte(c, UNARY_NEGATIVE);
1970 else if (childtype == TILDE) {
1971 com_factor(c, CHILD(n, 1));
1972 com_addbyte(c, UNARY_INVERT);
1974 else {
1975 com_power(c, CHILD(n, 0));
1979 static void
1980 com_term(struct compiling *c, node *n)
1982 int i;
1983 int op;
1984 REQ(n, term);
1985 com_factor(c, CHILD(n, 0));
1986 for (i = 2; i < NCH(n); i += 2) {
1987 com_factor(c, CHILD(n, i));
1988 switch (TYPE(CHILD(n, i-1))) {
1989 case STAR:
1990 op = BINARY_MULTIPLY;
1991 break;
1992 case SLASH:
1993 if (c->c_flags & CO_FUTURE_DIVISION)
1994 op = BINARY_TRUE_DIVIDE;
1995 else
1996 op = BINARY_DIVIDE;
1997 break;
1998 case PERCENT:
1999 op = BINARY_MODULO;
2000 break;
2001 case DOUBLESLASH:
2002 op = BINARY_FLOOR_DIVIDE;
2003 break;
2004 default:
2005 com_error(c, PyExc_SystemError,
2006 "com_term: operator not *, /, // or %");
2007 op = 255;
2009 com_addbyte(c, op);
2010 com_pop(c, 1);
2014 static void
2015 com_arith_expr(struct compiling *c, node *n)
2017 int i;
2018 int op;
2019 REQ(n, arith_expr);
2020 com_term(c, CHILD(n, 0));
2021 for (i = 2; i < NCH(n); i += 2) {
2022 com_term(c, CHILD(n, i));
2023 switch (TYPE(CHILD(n, i-1))) {
2024 case PLUS:
2025 op = BINARY_ADD;
2026 break;
2027 case MINUS:
2028 op = BINARY_SUBTRACT;
2029 break;
2030 default:
2031 com_error(c, PyExc_SystemError,
2032 "com_arith_expr: operator not + or -");
2033 op = 255;
2035 com_addbyte(c, op);
2036 com_pop(c, 1);
2040 static void
2041 com_shift_expr(struct compiling *c, node *n)
2043 int i;
2044 int op;
2045 REQ(n, shift_expr);
2046 com_arith_expr(c, CHILD(n, 0));
2047 for (i = 2; i < NCH(n); i += 2) {
2048 com_arith_expr(c, CHILD(n, i));
2049 switch (TYPE(CHILD(n, i-1))) {
2050 case LEFTSHIFT:
2051 op = BINARY_LSHIFT;
2052 break;
2053 case RIGHTSHIFT:
2054 op = BINARY_RSHIFT;
2055 break;
2056 default:
2057 com_error(c, PyExc_SystemError,
2058 "com_shift_expr: operator not << or >>");
2059 op = 255;
2061 com_addbyte(c, op);
2062 com_pop(c, 1);
2066 static void
2067 com_and_expr(struct compiling *c, node *n)
2069 int i;
2070 int op;
2071 REQ(n, and_expr);
2072 com_shift_expr(c, CHILD(n, 0));
2073 for (i = 2; i < NCH(n); i += 2) {
2074 com_shift_expr(c, CHILD(n, i));
2075 if (TYPE(CHILD(n, i-1)) == AMPER) {
2076 op = BINARY_AND;
2078 else {
2079 com_error(c, PyExc_SystemError,
2080 "com_and_expr: operator not &");
2081 op = 255;
2083 com_addbyte(c, op);
2084 com_pop(c, 1);
2088 static void
2089 com_xor_expr(struct compiling *c, node *n)
2091 int i;
2092 int op;
2093 REQ(n, xor_expr);
2094 com_and_expr(c, CHILD(n, 0));
2095 for (i = 2; i < NCH(n); i += 2) {
2096 com_and_expr(c, CHILD(n, i));
2097 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2098 op = BINARY_XOR;
2100 else {
2101 com_error(c, PyExc_SystemError,
2102 "com_xor_expr: operator not ^");
2103 op = 255;
2105 com_addbyte(c, op);
2106 com_pop(c, 1);
2110 static void
2111 com_expr(struct compiling *c, node *n)
2113 int i;
2114 int op;
2115 REQ(n, expr);
2116 com_xor_expr(c, CHILD(n, 0));
2117 for (i = 2; i < NCH(n); i += 2) {
2118 com_xor_expr(c, CHILD(n, i));
2119 if (TYPE(CHILD(n, i-1)) == VBAR) {
2120 op = BINARY_OR;
2122 else {
2123 com_error(c, PyExc_SystemError,
2124 "com_expr: expr operator not |");
2125 op = 255;
2127 com_addbyte(c, op);
2128 com_pop(c, 1);
2132 static enum cmp_op
2133 cmp_type(node *n)
2135 REQ(n, comp_op);
2136 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2137 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2138 if (NCH(n) == 1) {
2139 n = CHILD(n, 0);
2140 switch (TYPE(n)) {
2141 case LESS: return LT;
2142 case GREATER: return GT;
2143 case EQEQUAL: /* == */
2144 case EQUAL: return EQ;
2145 case LESSEQUAL: return LE;
2146 case GREATEREQUAL: return GE;
2147 case NOTEQUAL: return NE; /* <> or != */
2148 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2149 if (strcmp(STR(n), "is") == 0) return IS;
2152 else if (NCH(n) == 2) {
2153 switch (TYPE(CHILD(n, 0))) {
2154 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2155 return NOT_IN;
2156 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2157 return IS_NOT;
2160 return BAD;
2163 static void
2164 com_comparison(struct compiling *c, node *n)
2166 int i;
2167 enum cmp_op op;
2168 int anchor;
2169 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2170 com_expr(c, CHILD(n, 0));
2171 if (NCH(n) == 1)
2172 return;
2174 /****************************************************************
2175 The following code is generated for all but the last
2176 comparison in a chain:
2178 label: on stack: opcode: jump to:
2180 a <code to load b>
2181 a, b DUP_TOP
2182 a, b, b ROT_THREE
2183 b, a, b COMPARE_OP
2184 b, 0-or-1 JUMP_IF_FALSE L1
2185 b, 1 POP_TOP
2188 We are now ready to repeat this sequence for the next
2189 comparison in the chain.
2191 For the last we generate:
2193 b <code to load c>
2194 b, c COMPARE_OP
2195 0-or-1
2197 If there were any jumps to L1 (i.e., there was more than one
2198 comparison), we generate:
2200 0-or-1 JUMP_FORWARD L2
2201 L1: b, 0 ROT_TWO
2202 0, b POP_TOP
2204 L2: 0-or-1
2205 ****************************************************************/
2207 anchor = 0;
2209 for (i = 2; i < NCH(n); i += 2) {
2210 com_expr(c, CHILD(n, i));
2211 if (i+2 < NCH(n)) {
2212 com_addbyte(c, DUP_TOP);
2213 com_push(c, 1);
2214 com_addbyte(c, ROT_THREE);
2216 op = cmp_type(CHILD(n, i-1));
2217 if (op == BAD) {
2218 com_error(c, PyExc_SystemError,
2219 "com_comparison: unknown comparison op");
2221 com_addoparg(c, COMPARE_OP, op);
2222 com_pop(c, 1);
2223 if (i+2 < NCH(n)) {
2224 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2225 com_addbyte(c, POP_TOP);
2226 com_pop(c, 1);
2230 if (anchor) {
2231 int anchor2 = 0;
2232 com_addfwref(c, JUMP_FORWARD, &anchor2);
2233 com_backpatch(c, anchor);
2234 com_addbyte(c, ROT_TWO);
2235 com_addbyte(c, POP_TOP);
2236 com_backpatch(c, anchor2);
2240 static void
2241 com_not_test(struct compiling *c, node *n)
2243 REQ(n, not_test); /* 'not' not_test | comparison */
2244 if (NCH(n) == 1) {
2245 com_comparison(c, CHILD(n, 0));
2247 else {
2248 com_not_test(c, CHILD(n, 1));
2249 com_addbyte(c, UNARY_NOT);
2253 static void
2254 com_and_test(struct compiling *c, node *n)
2256 int i;
2257 int anchor;
2258 REQ(n, and_test); /* not_test ('and' not_test)* */
2259 anchor = 0;
2260 i = 0;
2261 for (;;) {
2262 com_not_test(c, CHILD(n, i));
2263 if ((i += 2) >= NCH(n))
2264 break;
2265 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2266 com_addbyte(c, POP_TOP);
2267 com_pop(c, 1);
2269 if (anchor)
2270 com_backpatch(c, anchor);
2273 static int
2274 com_make_closure(struct compiling *c, PyCodeObject *co)
2276 int i, free = PyCode_GetNumFree(co);
2277 if (free == 0)
2278 return 0;
2279 for (i = 0; i < free; ++i) {
2280 /* Bypass com_addop_varname because it will generate
2281 LOAD_DEREF but LOAD_CLOSURE is needed.
2283 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2284 int arg, reftype;
2286 /* Special case: If a class contains a method with a
2287 free variable that has the same name as a method,
2288 the name will be considered free *and* local in the
2289 class. It should be handled by the closure, as
2290 well as by the normal name loookup logic.
2292 reftype = get_ref_type(c, PyString_AS_STRING(name));
2293 if (reftype == CELL)
2294 arg = com_lookup_arg(c->c_cellvars, name);
2295 else /* (reftype == FREE) */
2296 arg = com_lookup_arg(c->c_freevars, name);
2297 if (arg == -1) {
2298 fprintf(stderr, "lookup %s in %s %d %d\n"
2299 "freevars of %s: %s\n",
2300 PyObject_REPR(name),
2301 c->c_name,
2302 reftype, arg,
2303 PyString_AS_STRING(co->co_name),
2304 PyObject_REPR(co->co_freevars));
2305 Py_FatalError("com_make_closure()");
2307 com_addoparg(c, LOAD_CLOSURE, arg);
2310 com_push(c, free);
2311 return 1;
2314 static void
2315 com_test(struct compiling *c, node *n)
2317 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2318 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2319 PyCodeObject *co;
2320 int i, closure;
2321 int ndefs = com_argdefs(c, CHILD(n, 0));
2322 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2323 n->n_lineno);
2324 co = icompile(CHILD(n, 0), c);
2325 if (co == NULL) {
2326 c->c_errors++;
2327 return;
2329 symtable_exit_scope(c->c_symtable);
2330 i = com_addconst(c, (PyObject *)co);
2331 closure = com_make_closure(c, co);
2332 com_addoparg(c, LOAD_CONST, i);
2333 com_push(c, 1);
2334 if (closure) {
2335 com_addoparg(c, MAKE_CLOSURE, ndefs);
2336 com_pop(c, PyCode_GetNumFree(co));
2337 } else
2338 com_addoparg(c, MAKE_FUNCTION, ndefs);
2339 Py_DECREF(co);
2340 com_pop(c, ndefs);
2342 else {
2343 int anchor = 0;
2344 int i = 0;
2345 for (;;) {
2346 com_and_test(c, CHILD(n, i));
2347 if ((i += 2) >= NCH(n))
2348 break;
2349 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2350 com_addbyte(c, POP_TOP);
2351 com_pop(c, 1);
2353 if (anchor)
2354 com_backpatch(c, anchor);
2358 static void
2359 com_list(struct compiling *c, node *n, int toplevel)
2361 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2362 if (NCH(n) == 1 && !toplevel) {
2363 com_node(c, CHILD(n, 0));
2365 else {
2366 int i;
2367 int len;
2368 len = (NCH(n) + 1) / 2;
2369 for (i = 0; i < NCH(n); i += 2)
2370 com_node(c, CHILD(n, i));
2371 com_addoparg(c, BUILD_TUPLE, len);
2372 com_pop(c, len-1);
2377 /* Begin of assignment compilation */
2380 static void
2381 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2383 com_addbyte(c, DUP_TOP);
2384 com_push(c, 1);
2385 com_addopname(c, LOAD_ATTR, n);
2386 com_node(c, augn);
2387 com_addbyte(c, opcode);
2388 com_pop(c, 1);
2389 com_addbyte(c, ROT_TWO);
2390 com_addopname(c, STORE_ATTR, n);
2391 com_pop(c, 2);
2394 static void
2395 com_assign_attr(struct compiling *c, node *n, int assigning)
2397 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2398 com_pop(c, assigning ? 2 : 1);
2401 static void
2402 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2404 REQ(n, trailer);
2405 switch (TYPE(CHILD(n, 0))) {
2406 case LPAR: /* '(' [exprlist] ')' */
2407 com_error(c, PyExc_SyntaxError,
2408 "can't assign to function call");
2409 break;
2410 case DOT: /* '.' NAME */
2411 if (assigning > OP_APPLY)
2412 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2413 else
2414 com_assign_attr(c, CHILD(n, 1), assigning);
2415 break;
2416 case LSQB: /* '[' subscriptlist ']' */
2417 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2418 break;
2419 default:
2420 com_error(c, PyExc_SystemError, "unknown trailer type");
2424 static void
2425 com_assign_sequence(struct compiling *c, node *n, int assigning)
2427 int i;
2428 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2429 REQ(n, exprlist);
2430 if (assigning) {
2431 i = (NCH(n)+1)/2;
2432 com_addoparg(c, UNPACK_SEQUENCE, i);
2433 com_push(c, i-1);
2435 for (i = 0; i < NCH(n); i += 2)
2436 com_assign(c, CHILD(n, i), assigning, NULL);
2439 static void
2440 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2442 REQ(n, NAME);
2443 com_addop_varname(c, VAR_LOAD, STR(n));
2444 com_push(c, 1);
2445 com_node(c, augn);
2446 com_addbyte(c, opcode);
2447 com_pop(c, 1);
2448 com_assign_name(c, n, OP_ASSIGN);
2451 static void
2452 com_assign_name(struct compiling *c, node *n, int assigning)
2454 REQ(n, NAME);
2455 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2456 if (assigning)
2457 com_pop(c, 1);
2460 static void
2461 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2463 /* Loop to avoid trivial recursion */
2464 for (;;) {
2465 switch (TYPE(n)) {
2467 case exprlist:
2468 case testlist:
2469 if (NCH(n) > 1) {
2470 if (assigning > OP_APPLY) {
2471 com_error(c, PyExc_SyntaxError,
2472 "augmented assign to tuple not possible");
2473 return;
2475 com_assign_sequence(c, n, assigning);
2476 return;
2478 n = CHILD(n, 0);
2479 break;
2481 case test:
2482 case and_test:
2483 case not_test:
2484 case comparison:
2485 case expr:
2486 case xor_expr:
2487 case and_expr:
2488 case shift_expr:
2489 case arith_expr:
2490 case term:
2491 case factor:
2492 if (NCH(n) > 1) {
2493 com_error(c, PyExc_SyntaxError,
2494 "can't assign to operator");
2495 return;
2497 n = CHILD(n, 0);
2498 break;
2500 case power: /* atom trailer* ('**' power)*
2501 ('+'|'-'|'~') factor | atom trailer* */
2502 if (TYPE(CHILD(n, 0)) != atom) {
2503 com_error(c, PyExc_SyntaxError,
2504 "can't assign to operator");
2505 return;
2507 if (NCH(n) > 1) { /* trailer or exponent present */
2508 int i;
2509 com_node(c, CHILD(n, 0));
2510 for (i = 1; i+1 < NCH(n); i++) {
2511 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2512 com_error(c, PyExc_SyntaxError,
2513 "can't assign to operator");
2514 return;
2516 com_apply_trailer(c, CHILD(n, i));
2517 } /* NB i is still alive */
2518 com_assign_trailer(c,
2519 CHILD(n, i), assigning, augn);
2520 return;
2522 n = CHILD(n, 0);
2523 break;
2525 case atom:
2526 switch (TYPE(CHILD(n, 0))) {
2527 case LPAR:
2528 n = CHILD(n, 1);
2529 if (TYPE(n) == RPAR) {
2530 /* XXX Should allow () = () ??? */
2531 com_error(c, PyExc_SyntaxError,
2532 "can't assign to ()");
2533 return;
2535 if (assigning > OP_APPLY) {
2536 com_error(c, PyExc_SyntaxError,
2537 "augmented assign to tuple not possible");
2538 return;
2540 break;
2541 case LSQB:
2542 n = CHILD(n, 1);
2543 if (TYPE(n) == RSQB) {
2544 com_error(c, PyExc_SyntaxError,
2545 "can't assign to []");
2546 return;
2548 if (assigning > OP_APPLY) {
2549 com_error(c, PyExc_SyntaxError,
2550 "augmented assign to list not possible");
2551 return;
2553 if (NCH(n) > 1
2554 && TYPE(CHILD(n, 1)) == list_for) {
2555 com_error(c, PyExc_SyntaxError,
2556 "can't assign to list comprehension");
2557 return;
2559 com_assign_sequence(c, n, assigning);
2560 return;
2561 case NAME:
2562 if (assigning > OP_APPLY)
2563 com_augassign_name(c, CHILD(n, 0),
2564 assigning, augn);
2565 else
2566 com_assign_name(c, CHILD(n, 0),
2567 assigning);
2568 return;
2569 default:
2570 com_error(c, PyExc_SyntaxError,
2571 "can't assign to literal");
2572 return;
2574 break;
2576 case lambdef:
2577 com_error(c, PyExc_SyntaxError,
2578 "can't assign to lambda");
2579 return;
2581 default:
2582 com_error(c, PyExc_SystemError,
2583 "com_assign: bad node");
2584 return;
2590 static void
2591 com_augassign(struct compiling *c, node *n)
2593 int opcode;
2595 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2596 case '+': opcode = INPLACE_ADD; break;
2597 case '-': opcode = INPLACE_SUBTRACT; break;
2598 case '/':
2599 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2600 opcode = INPLACE_FLOOR_DIVIDE;
2601 else if (c->c_flags & CO_FUTURE_DIVISION)
2602 opcode = INPLACE_TRUE_DIVIDE;
2603 else
2604 opcode = INPLACE_DIVIDE;
2605 break;
2606 case '%': opcode = INPLACE_MODULO; break;
2607 case '<': opcode = INPLACE_LSHIFT; break;
2608 case '>': opcode = INPLACE_RSHIFT; break;
2609 case '&': opcode = INPLACE_AND; break;
2610 case '^': opcode = INPLACE_XOR; break;
2611 case '|': opcode = INPLACE_OR; break;
2612 case '*':
2613 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2614 opcode = INPLACE_POWER;
2615 else
2616 opcode = INPLACE_MULTIPLY;
2617 break;
2618 default:
2619 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2620 return;
2622 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2625 static void
2626 com_expr_stmt(struct compiling *c, node *n)
2628 REQ(n, expr_stmt);
2629 /* testlist (('=' testlist)* | augassign testlist) */
2630 /* Forget it if we have just a doc string here */
2631 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2632 return;
2633 if (NCH(n) == 1) {
2634 com_node(c, CHILD(n, NCH(n)-1));
2635 if (c->c_interactive)
2636 com_addbyte(c, PRINT_EXPR);
2637 else
2638 com_addbyte(c, POP_TOP);
2639 com_pop(c, 1);
2641 else if (TYPE(CHILD(n,1)) == augassign)
2642 com_augassign(c, n);
2643 else {
2644 int i;
2645 com_node(c, CHILD(n, NCH(n)-1));
2646 for (i = 0; i < NCH(n)-2; i+=2) {
2647 if (i+2 < NCH(n)-2) {
2648 com_addbyte(c, DUP_TOP);
2649 com_push(c, 1);
2651 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2656 static void
2657 com_assert_stmt(struct compiling *c, node *n)
2659 int a = 0, b = 0;
2660 int i;
2661 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2662 /* Generate code like for
2664 if __debug__:
2665 if not <test>:
2666 raise AssertionError [, <message>]
2668 where <message> is the second test, if present.
2671 if (Py_OptimizeFlag)
2672 return;
2673 com_addop_name(c, LOAD_GLOBAL, "__debug__");
2674 com_push(c, 1);
2675 com_addfwref(c, JUMP_IF_FALSE, &a);
2676 com_addbyte(c, POP_TOP);
2677 com_pop(c, 1);
2678 com_node(c, CHILD(n, 1));
2679 com_addfwref(c, JUMP_IF_TRUE, &b);
2680 com_addbyte(c, POP_TOP);
2681 com_pop(c, 1);
2682 /* Raise that exception! */
2683 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2684 com_push(c, 1);
2685 i = NCH(n)/2; /* Either 2 or 4 */
2686 if (i > 1)
2687 com_node(c, CHILD(n, 3));
2688 com_addoparg(c, RAISE_VARARGS, i);
2689 com_pop(c, i);
2690 /* The interpreter does not fall through */
2691 /* All jumps converge here */
2692 com_backpatch(c, a);
2693 com_backpatch(c, b);
2694 com_addbyte(c, POP_TOP);
2697 static void
2698 com_print_stmt(struct compiling *c, node *n)
2700 int i = 1;
2701 node* stream = NULL;
2703 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2705 /* are we using the extended print form? */
2706 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2707 stream = CHILD(n, 2);
2708 com_node(c, stream);
2709 /* stack: [...] => [... stream] */
2710 com_push(c, 1);
2711 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2712 i = 4;
2713 else
2714 i = 3;
2716 for (; i < NCH(n); i += 2) {
2717 if (stream != NULL) {
2718 com_addbyte(c, DUP_TOP);
2719 /* stack: [stream] => [stream stream] */
2720 com_push(c, 1);
2721 com_node(c, CHILD(n, i));
2722 /* stack: [stream stream] => [stream stream obj] */
2723 com_addbyte(c, ROT_TWO);
2724 /* stack: [stream stream obj] => [stream obj stream] */
2725 com_addbyte(c, PRINT_ITEM_TO);
2726 /* stack: [stream obj stream] => [stream] */
2727 com_pop(c, 2);
2729 else {
2730 com_node(c, CHILD(n, i));
2731 /* stack: [...] => [... obj] */
2732 com_addbyte(c, PRINT_ITEM);
2733 com_pop(c, 1);
2736 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2737 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2738 if (stream != NULL) {
2739 /* must pop the extra stream object off the stack */
2740 com_addbyte(c, POP_TOP);
2741 /* stack: [... stream] => [...] */
2742 com_pop(c, 1);
2745 else {
2746 if (stream != NULL) {
2747 /* this consumes the last stream object on stack */
2748 com_addbyte(c, PRINT_NEWLINE_TO);
2749 /* stack: [... stream] => [...] */
2750 com_pop(c, 1);
2752 else
2753 com_addbyte(c, PRINT_NEWLINE);
2757 static void
2758 com_return_stmt(struct compiling *c, node *n)
2760 REQ(n, return_stmt); /* 'return' [testlist] */
2761 if (!c->c_infunction) {
2762 com_error(c, PyExc_SyntaxError, "'return' outside function");
2764 if (c->c_flags & CO_GENERATOR) {
2765 if (NCH(n) > 1) {
2766 com_error(c, PyExc_SyntaxError,
2767 "'return' with argument inside generator");
2770 if (NCH(n) < 2) {
2771 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2772 com_push(c, 1);
2774 else
2775 com_node(c, CHILD(n, 1));
2776 com_addbyte(c, RETURN_VALUE);
2777 com_pop(c, 1);
2780 static void
2781 com_yield_stmt(struct compiling *c, node *n)
2783 int i;
2784 REQ(n, yield_stmt); /* 'yield' testlist */
2785 if (!c->c_infunction) {
2786 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2789 for (i = 0; i < c->c_nblocks; ++i) {
2790 if (c->c_block[i] == SETUP_FINALLY) {
2791 com_error(c, PyExc_SyntaxError,
2792 "'yield' not allowed in a 'try' block "
2793 "with a 'finally' clause");
2794 return;
2797 com_node(c, CHILD(n, 1));
2798 com_addbyte(c, YIELD_VALUE);
2799 com_pop(c, 1);
2802 static void
2803 com_raise_stmt(struct compiling *c, node *n)
2805 int i;
2806 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2807 if (NCH(n) > 1) {
2808 com_node(c, CHILD(n, 1));
2809 if (NCH(n) > 3) {
2810 com_node(c, CHILD(n, 3));
2811 if (NCH(n) > 5)
2812 com_node(c, CHILD(n, 5));
2815 i = NCH(n)/2;
2816 com_addoparg(c, RAISE_VARARGS, i);
2817 com_pop(c, i);
2820 static void
2821 com_from_import(struct compiling *c, node *n)
2823 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2824 com_push(c, 1);
2825 if (NCH(n) > 1) {
2826 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2827 com_error(c, PyExc_SyntaxError, "invalid syntax");
2828 return;
2830 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2831 } else
2832 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2833 com_pop(c, 1);
2836 static void
2837 com_import_stmt(struct compiling *c, node *n)
2839 int i;
2840 REQ(n, import_stmt);
2841 /* 'import' dotted_name (',' dotted_name)* |
2842 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2843 if (STR(CHILD(n, 0))[0] == 'f') {
2844 PyObject *tup;
2845 /* 'from' dotted_name 'import' ... */
2846 REQ(CHILD(n, 1), dotted_name);
2848 if (TYPE(CHILD(n, 3)) == STAR) {
2849 tup = Py_BuildValue("(s)", "*");
2850 } else {
2851 tup = PyTuple_New((NCH(n) - 2)/2);
2852 for (i = 3; i < NCH(n); i += 2) {
2853 PyTuple_SET_ITEM(tup, (i-3)/2,
2854 PyString_FromString(STR(
2855 CHILD(CHILD(n, i), 0))));
2858 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2859 Py_DECREF(tup);
2860 com_push(c, 1);
2861 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2862 if (TYPE(CHILD(n, 3)) == STAR)
2863 com_addbyte(c, IMPORT_STAR);
2864 else {
2865 for (i = 3; i < NCH(n); i += 2)
2866 com_from_import(c, CHILD(n, i));
2867 com_addbyte(c, POP_TOP);
2869 com_pop(c, 1);
2871 else {
2872 /* 'import' ... */
2873 for (i = 1; i < NCH(n); i += 2) {
2874 node *subn = CHILD(n, i);
2875 REQ(subn, dotted_as_name);
2876 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2877 com_push(c, 1);
2878 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2879 if (NCH(subn) > 1) {
2880 int j;
2881 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
2882 com_error(c, PyExc_SyntaxError,
2883 "invalid syntax");
2884 return;
2886 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2887 com_addopname(c, LOAD_ATTR,
2888 CHILD(CHILD(subn, 0),
2889 j));
2890 com_addop_varname(c, VAR_STORE,
2891 STR(CHILD(subn, 2)));
2892 } else
2893 com_addop_varname(c, VAR_STORE,
2894 STR(CHILD(CHILD(subn, 0),
2895 0)));
2896 com_pop(c, 1);
2901 static void
2902 com_exec_stmt(struct compiling *c, node *n)
2904 REQ(n, exec_stmt);
2905 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2906 com_node(c, CHILD(n, 1));
2907 if (NCH(n) >= 4)
2908 com_node(c, CHILD(n, 3));
2909 else {
2910 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2911 com_push(c, 1);
2913 if (NCH(n) >= 6)
2914 com_node(c, CHILD(n, 5));
2915 else {
2916 com_addbyte(c, DUP_TOP);
2917 com_push(c, 1);
2919 com_addbyte(c, EXEC_STMT);
2920 com_pop(c, 3);
2923 static int
2924 is_constant_false(struct compiling *c, node *n)
2926 PyObject *v;
2927 int i;
2928 /* argument c will be NULL when called from symtable_node() */
2930 /* Label to avoid tail recursion */
2931 next:
2932 switch (TYPE(n)) {
2934 case suite:
2935 if (NCH(n) == 1) {
2936 n = CHILD(n, 0);
2937 goto next;
2939 /* Fall through */
2940 case file_input:
2941 for (i = 0; i < NCH(n); i++) {
2942 node *ch = CHILD(n, i);
2943 if (TYPE(ch) == stmt) {
2944 n = ch;
2945 goto next;
2948 break;
2950 case stmt:
2951 case simple_stmt:
2952 case small_stmt:
2953 n = CHILD(n, 0);
2954 goto next;
2956 case expr_stmt:
2957 case testlist:
2958 case test:
2959 case and_test:
2960 case not_test:
2961 case comparison:
2962 case expr:
2963 case xor_expr:
2964 case and_expr:
2965 case shift_expr:
2966 case arith_expr:
2967 case term:
2968 case factor:
2969 case power:
2970 case atom:
2971 if (NCH(n) == 1) {
2972 n = CHILD(n, 0);
2973 goto next;
2975 break;
2977 case NAME:
2978 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2979 return 1;
2980 break;
2982 case NUMBER:
2983 v = parsenumber(c, STR(n));
2984 if (v == NULL) {
2985 PyErr_Clear();
2986 break;
2988 i = PyObject_IsTrue(v);
2989 Py_DECREF(v);
2990 return i == 0;
2992 case STRING:
2993 v = parsestr(c, STR(n));
2994 if (v == NULL) {
2995 PyErr_Clear();
2996 break;
2998 i = PyObject_IsTrue(v);
2999 Py_DECREF(v);
3000 return i == 0;
3003 return 0;
3007 /* Look under n for a return stmt with an expression.
3008 * This hack is used to find illegal returns under "if 0:" blocks in
3009 * functions already known to be generators (as determined by the symtable
3010 * pass).
3011 * Return the offending return node if found, else NULL.
3013 static node *
3014 look_for_offending_return(node *n)
3016 int i;
3018 for (i = 0; i < NCH(n); ++i) {
3019 node *kid = CHILD(n, i);
3021 switch (TYPE(kid)) {
3022 case classdef:
3023 case funcdef:
3024 case lambdef:
3025 /* Stuff in nested functions & classes doesn't
3026 affect the code block we started in. */
3027 return NULL;
3029 case return_stmt:
3030 if (NCH(kid) > 1)
3031 return kid;
3032 break;
3034 default: {
3035 node *bad = look_for_offending_return(kid);
3036 if (bad != NULL)
3037 return bad;
3042 return NULL;
3045 static void
3046 com_if_stmt(struct compiling *c, node *n)
3048 int i;
3049 int anchor = 0;
3050 REQ(n, if_stmt);
3051 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3052 for (i = 0; i+3 < NCH(n); i+=4) {
3053 int a = 0;
3054 node *ch = CHILD(n, i+1);
3055 if (is_constant_false(c, ch)) {
3056 /* We're going to skip this block. However, if this
3057 is a generator, we have to check the dead code
3058 anyway to make sure there aren't any return stmts
3059 with expressions, in the same scope. */
3060 if (c->c_flags & CO_GENERATOR) {
3061 node *p = look_for_offending_return(n);
3062 if (p != NULL) {
3063 int savelineno = c->c_lineno;
3064 c->c_lineno = p->n_lineno;
3065 com_error(c, PyExc_SyntaxError,
3066 "'return' with argument "
3067 "inside generator");
3068 c->c_lineno = savelineno;
3071 continue;
3073 if (i > 0)
3074 com_addoparg(c, SET_LINENO, ch->n_lineno);
3075 com_node(c, ch);
3076 com_addfwref(c, JUMP_IF_FALSE, &a);
3077 com_addbyte(c, POP_TOP);
3078 com_pop(c, 1);
3079 com_node(c, CHILD(n, i+3));
3080 com_addfwref(c, JUMP_FORWARD, &anchor);
3081 com_backpatch(c, a);
3082 /* We jump here with an extra entry which we now pop */
3083 com_addbyte(c, POP_TOP);
3085 if (i+2 < NCH(n))
3086 com_node(c, CHILD(n, i+2));
3087 if (anchor)
3088 com_backpatch(c, anchor);
3091 static void
3092 com_while_stmt(struct compiling *c, node *n)
3094 int break_anchor = 0;
3095 int anchor = 0;
3096 int save_begin = c->c_begin;
3097 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3098 com_addfwref(c, SETUP_LOOP, &break_anchor);
3099 block_push(c, SETUP_LOOP);
3100 c->c_begin = c->c_nexti;
3101 com_addoparg(c, SET_LINENO, n->n_lineno);
3102 com_node(c, CHILD(n, 1));
3103 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3104 com_addbyte(c, POP_TOP);
3105 com_pop(c, 1);
3106 c->c_loops++;
3107 com_node(c, CHILD(n, 3));
3108 c->c_loops--;
3109 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3110 c->c_begin = save_begin;
3111 com_backpatch(c, anchor);
3112 /* We jump here with one entry more on the stack */
3113 com_addbyte(c, POP_TOP);
3114 com_addbyte(c, POP_BLOCK);
3115 block_pop(c, SETUP_LOOP);
3116 if (NCH(n) > 4)
3117 com_node(c, CHILD(n, 6));
3118 com_backpatch(c, break_anchor);
3121 static void
3122 com_for_stmt(struct compiling *c, node *n)
3124 int break_anchor = 0;
3125 int anchor = 0;
3126 int save_begin = c->c_begin;
3127 REQ(n, for_stmt);
3128 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3129 com_addfwref(c, SETUP_LOOP, &break_anchor);
3130 block_push(c, SETUP_LOOP);
3131 com_node(c, CHILD(n, 3));
3132 com_addbyte(c, GET_ITER);
3133 c->c_begin = c->c_nexti;
3134 com_addoparg(c, SET_LINENO, n->n_lineno);
3135 com_addfwref(c, FOR_ITER, &anchor);
3136 com_push(c, 1);
3137 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
3138 c->c_loops++;
3139 com_node(c, CHILD(n, 5));
3140 c->c_loops--;
3141 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3142 c->c_begin = save_begin;
3143 com_backpatch(c, anchor);
3144 com_pop(c, 1); /* FOR_ITER has popped this */
3145 com_addbyte(c, POP_BLOCK);
3146 block_pop(c, SETUP_LOOP);
3147 if (NCH(n) > 8)
3148 com_node(c, CHILD(n, 8));
3149 com_backpatch(c, break_anchor);
3152 /* Code generated for "try: S finally: Sf" is as follows:
3154 SETUP_FINALLY L
3155 <code for S>
3156 POP_BLOCK
3157 LOAD_CONST <nil>
3158 L: <code for Sf>
3159 END_FINALLY
3161 The special instructions use the block stack. Each block
3162 stack entry contains the instruction that created it (here
3163 SETUP_FINALLY), the level of the value stack at the time the
3164 block stack entry was created, and a label (here L).
3166 SETUP_FINALLY:
3167 Pushes the current value stack level and the label
3168 onto the block stack.
3169 POP_BLOCK:
3170 Pops en entry from the block stack, and pops the value
3171 stack until its level is the same as indicated on the
3172 block stack. (The label is ignored.)
3173 END_FINALLY:
3174 Pops a variable number of entries from the *value* stack
3175 and re-raises the exception they specify. The number of
3176 entries popped depends on the (pseudo) exception type.
3178 The block stack is unwound when an exception is raised:
3179 when a SETUP_FINALLY entry is found, the exception is pushed
3180 onto the value stack (and the exception condition is cleared),
3181 and the interpreter jumps to the label gotten from the block
3182 stack.
3184 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3185 (The contents of the value stack is shown in [], with the top
3186 at the right; 'tb' is trace-back info, 'val' the exception's
3187 associated value, and 'exc' the exception.)
3189 Value stack Label Instruction Argument
3190 [] SETUP_EXCEPT L1
3191 [] <code for S>
3192 [] POP_BLOCK
3193 [] JUMP_FORWARD L0
3195 [tb, val, exc] L1: DUP )
3196 [tb, val, exc, exc] <evaluate E1> )
3197 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3198 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3199 [tb, val, exc, 1] POP )
3200 [tb, val, exc] POP
3201 [tb, val] <assign to V1> (or POP if no V1)
3202 [tb] POP
3203 [] <code for S1>
3204 JUMP_FORWARD L0
3206 [tb, val, exc, 0] L2: POP
3207 [tb, val, exc] DUP
3208 .............................etc.......................
3210 [tb, val, exc, 0] Ln+1: POP
3211 [tb, val, exc] END_FINALLY # re-raise exception
3213 [] L0: <next statement>
3215 Of course, parts are not generated if Vi or Ei is not present.
3218 static void
3219 com_try_except(struct compiling *c, node *n)
3221 int except_anchor = 0;
3222 int end_anchor = 0;
3223 int else_anchor = 0;
3224 int i;
3225 node *ch;
3227 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3228 block_push(c, SETUP_EXCEPT);
3229 com_node(c, CHILD(n, 2));
3230 com_addbyte(c, POP_BLOCK);
3231 block_pop(c, SETUP_EXCEPT);
3232 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3233 com_backpatch(c, except_anchor);
3234 for (i = 3;
3235 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3236 i += 3) {
3237 /* except_clause: 'except' [expr [',' var]] */
3238 if (except_anchor == 0) {
3239 com_error(c, PyExc_SyntaxError,
3240 "default 'except:' must be last");
3241 break;
3243 except_anchor = 0;
3244 com_push(c, 3); /* tb, val, exc pushed by exception */
3245 com_addoparg(c, SET_LINENO, ch->n_lineno);
3246 if (NCH(ch) > 1) {
3247 com_addbyte(c, DUP_TOP);
3248 com_push(c, 1);
3249 com_node(c, CHILD(ch, 1));
3250 com_addoparg(c, COMPARE_OP, EXC_MATCH);
3251 com_pop(c, 1);
3252 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3253 com_addbyte(c, POP_TOP);
3254 com_pop(c, 1);
3256 com_addbyte(c, POP_TOP);
3257 com_pop(c, 1);
3258 if (NCH(ch) > 3)
3259 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3260 else {
3261 com_addbyte(c, POP_TOP);
3262 com_pop(c, 1);
3264 com_addbyte(c, POP_TOP);
3265 com_pop(c, 1);
3266 com_node(c, CHILD(n, i+2));
3267 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3268 if (except_anchor) {
3269 com_backpatch(c, except_anchor);
3270 /* We come in with [tb, val, exc, 0] on the
3271 stack; one pop and it's the same as
3272 expected at the start of the loop */
3273 com_addbyte(c, POP_TOP);
3276 /* We actually come in here with [tb, val, exc] but the
3277 END_FINALLY will zap those and jump around.
3278 The c_stacklevel does not reflect them so we need not pop
3279 anything. */
3280 com_addbyte(c, END_FINALLY);
3281 com_backpatch(c, else_anchor);
3282 if (i < NCH(n))
3283 com_node(c, CHILD(n, i+2));
3284 com_backpatch(c, end_anchor);
3287 static void
3288 com_try_finally(struct compiling *c, node *n)
3290 int finally_anchor = 0;
3291 node *ch;
3293 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3294 block_push(c, SETUP_FINALLY);
3295 com_node(c, CHILD(n, 2));
3296 com_addbyte(c, POP_BLOCK);
3297 block_pop(c, SETUP_FINALLY);
3298 block_push(c, END_FINALLY);
3299 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3300 /* While the generated code pushes only one item,
3301 the try-finally handling can enter here with
3302 up to three items. OK, here are the details:
3303 3 for an exception, 2 for RETURN, 1 for BREAK. */
3304 com_push(c, 3);
3305 com_backpatch(c, finally_anchor);
3306 ch = CHILD(n, NCH(n)-1);
3307 com_addoparg(c, SET_LINENO, ch->n_lineno);
3308 com_node(c, ch);
3309 com_addbyte(c, END_FINALLY);
3310 block_pop(c, END_FINALLY);
3311 com_pop(c, 3); /* Matches the com_push above */
3314 static void
3315 com_try_stmt(struct compiling *c, node *n)
3317 REQ(n, try_stmt);
3318 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3319 | 'try' ':' suite 'finally' ':' suite */
3320 if (TYPE(CHILD(n, 3)) != except_clause)
3321 com_try_finally(c, n);
3322 else
3323 com_try_except(c, n);
3326 static node *
3327 get_rawdocstring(node *n)
3329 int i;
3331 /* Label to avoid tail recursion */
3332 next:
3333 switch (TYPE(n)) {
3335 case suite:
3336 if (NCH(n) == 1) {
3337 n = CHILD(n, 0);
3338 goto next;
3340 /* Fall through */
3341 case file_input:
3342 for (i = 0; i < NCH(n); i++) {
3343 node *ch = CHILD(n, i);
3344 if (TYPE(ch) == stmt) {
3345 n = ch;
3346 goto next;
3349 break;
3351 case stmt:
3352 case simple_stmt:
3353 case small_stmt:
3354 n = CHILD(n, 0);
3355 goto next;
3357 case expr_stmt:
3358 case testlist:
3359 case test:
3360 case and_test:
3361 case not_test:
3362 case comparison:
3363 case expr:
3364 case xor_expr:
3365 case and_expr:
3366 case shift_expr:
3367 case arith_expr:
3368 case term:
3369 case factor:
3370 case power:
3371 if (NCH(n) == 1) {
3372 n = CHILD(n, 0);
3373 goto next;
3375 break;
3377 case atom:
3378 if (TYPE(CHILD(n, 0)) == STRING)
3379 return n;
3380 break;
3383 return NULL;
3386 static PyObject *
3387 get_docstring(struct compiling *c, node *n)
3389 /* Don't generate doc-strings if run with -OO */
3390 if (Py_OptimizeFlag > 1)
3391 return NULL;
3392 n = get_rawdocstring(n);
3393 if (n == NULL)
3394 return NULL;
3395 return parsestrplus(c, n);
3398 static void
3399 com_suite(struct compiling *c, node *n)
3401 REQ(n, suite);
3402 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3403 if (NCH(n) == 1) {
3404 com_node(c, CHILD(n, 0));
3406 else {
3407 int i;
3408 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3409 node *ch = CHILD(n, i);
3410 if (TYPE(ch) == stmt)
3411 com_node(c, ch);
3416 /* ARGSUSED */
3417 static void
3418 com_continue_stmt(struct compiling *c, node *n)
3420 int i = c->c_nblocks;
3421 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3422 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3424 else if (i <= 0) {
3425 /* at the outer level */
3426 com_error(c, PyExc_SyntaxError,
3427 "'continue' not properly in loop");
3429 else {
3430 int j;
3431 for (j = i-1; j >= 0; --j) {
3432 if (c->c_block[j] == SETUP_LOOP)
3433 break;
3435 if (j >= 0) {
3436 /* there is a loop, but something interferes */
3437 for (; i > j; --i) {
3438 if (c->c_block[i] == SETUP_EXCEPT ||
3439 c->c_block[i] == SETUP_FINALLY) {
3440 com_addoparg(c, CONTINUE_LOOP,
3441 c->c_begin);
3442 return;
3444 if (c->c_block[i] == END_FINALLY) {
3445 com_error(c, PyExc_SyntaxError,
3446 "'continue' not supported inside 'finally' clause");
3447 return;
3451 com_error(c, PyExc_SyntaxError,
3452 "'continue' not properly in loop");
3454 /* XXX Could allow it inside a 'finally' clause
3455 XXX if we could pop the exception still on the stack */
3458 static int
3459 com_argdefs(struct compiling *c, node *n)
3461 int i, nch, nargs, ndefs;
3462 if (TYPE(n) == lambdef) {
3463 /* lambdef: 'lambda' [varargslist] ':' test */
3464 n = CHILD(n, 1);
3466 else {
3467 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3468 n = CHILD(n, 2);
3469 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3470 n = CHILD(n, 1);
3472 if (TYPE(n) != varargslist)
3473 return 0;
3474 /* varargslist:
3475 (fpdef ['=' test] ',')* '*' ....... |
3476 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3477 nch = NCH(n);
3478 nargs = 0;
3479 ndefs = 0;
3480 for (i = 0; i < nch; i++) {
3481 int t;
3482 if (TYPE(CHILD(n, i)) == STAR ||
3483 TYPE(CHILD(n, i)) == DOUBLESTAR)
3484 break;
3485 nargs++;
3486 i++;
3487 if (i >= nch)
3488 t = RPAR; /* Anything except EQUAL or COMMA */
3489 else
3490 t = TYPE(CHILD(n, i));
3491 if (t == EQUAL) {
3492 i++;
3493 ndefs++;
3494 com_node(c, CHILD(n, i));
3495 i++;
3496 if (i >= nch)
3497 break;
3498 t = TYPE(CHILD(n, i));
3500 else {
3501 /* Treat "(a=1, b)" as an error */
3502 if (ndefs)
3503 com_error(c, PyExc_SyntaxError,
3504 "non-default argument follows default argument");
3506 if (t != COMMA)
3507 break;
3509 return ndefs;
3512 static void
3513 com_funcdef(struct compiling *c, node *n)
3515 PyObject *co;
3516 int ndefs;
3517 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3518 ndefs = com_argdefs(c, n);
3519 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3520 n->n_lineno);
3521 co = (PyObject *)icompile(n, c);
3522 symtable_exit_scope(c->c_symtable);
3523 if (co == NULL)
3524 c->c_errors++;
3525 else {
3526 int closure = com_make_closure(c, (PyCodeObject *)co);
3527 int i = com_addconst(c, co);
3528 com_addoparg(c, LOAD_CONST, i);
3529 com_push(c, 1);
3530 if (closure)
3531 com_addoparg(c, MAKE_CLOSURE, ndefs);
3532 else
3533 com_addoparg(c, MAKE_FUNCTION, ndefs);
3534 com_pop(c, ndefs);
3535 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3536 com_pop(c, 1);
3537 Py_DECREF(co);
3541 static void
3542 com_bases(struct compiling *c, node *n)
3544 int i;
3545 REQ(n, testlist);
3546 /* testlist: test (',' test)* [','] */
3547 for (i = 0; i < NCH(n); i += 2)
3548 com_node(c, CHILD(n, i));
3549 i = (NCH(n)+1) / 2;
3550 com_addoparg(c, BUILD_TUPLE, i);
3551 com_pop(c, i-1);
3554 static void
3555 com_classdef(struct compiling *c, node *n)
3557 int i;
3558 PyObject *v;
3559 PyCodeObject *co;
3560 char *name;
3562 REQ(n, classdef);
3563 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3564 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3565 c->c_errors++;
3566 return;
3568 /* Push the class name on the stack */
3569 i = com_addconst(c, v);
3570 com_addoparg(c, LOAD_CONST, i);
3571 com_push(c, 1);
3572 Py_DECREF(v);
3573 /* Push the tuple of base classes on the stack */
3574 if (TYPE(CHILD(n, 2)) != LPAR) {
3575 com_addoparg(c, BUILD_TUPLE, 0);
3576 com_push(c, 1);
3578 else
3579 com_bases(c, CHILD(n, 3));
3580 name = STR(CHILD(n, 1));
3581 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3582 co = icompile(n, c);
3583 symtable_exit_scope(c->c_symtable);
3584 if (co == NULL)
3585 c->c_errors++;
3586 else {
3587 int closure = com_make_closure(c, co);
3588 i = com_addconst(c, (PyObject *)co);
3589 com_addoparg(c, LOAD_CONST, i);
3590 com_push(c, 1);
3591 if (closure) {
3592 com_addoparg(c, MAKE_CLOSURE, 0);
3593 com_pop(c, PyCode_GetNumFree(co));
3594 } else
3595 com_addoparg(c, MAKE_FUNCTION, 0);
3596 com_addoparg(c, CALL_FUNCTION, 0);
3597 com_addbyte(c, BUILD_CLASS);
3598 com_pop(c, 2);
3599 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3600 com_pop(c, 1);
3601 Py_DECREF(co);
3605 static void
3606 com_node(struct compiling *c, node *n)
3608 loop:
3609 if (c->c_errors)
3610 return;
3611 switch (TYPE(n)) {
3613 /* Definition nodes */
3615 case funcdef:
3616 com_funcdef(c, n);
3617 break;
3618 case classdef:
3619 com_classdef(c, n);
3620 break;
3622 /* Trivial parse tree nodes */
3624 case stmt:
3625 case small_stmt:
3626 case flow_stmt:
3627 n = CHILD(n, 0);
3628 goto loop;
3630 case simple_stmt:
3631 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3632 com_addoparg(c, SET_LINENO, n->n_lineno);
3634 int i;
3635 for (i = 0; i < NCH(n)-1; i += 2)
3636 com_node(c, CHILD(n, i));
3638 break;
3640 case compound_stmt:
3641 com_addoparg(c, SET_LINENO, n->n_lineno);
3642 n = CHILD(n, 0);
3643 goto loop;
3645 /* Statement nodes */
3647 case expr_stmt:
3648 com_expr_stmt(c, n);
3649 break;
3650 case print_stmt:
3651 com_print_stmt(c, n);
3652 break;
3653 case del_stmt: /* 'del' exprlist */
3654 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3655 break;
3656 case pass_stmt:
3657 break;
3658 case break_stmt:
3659 if (c->c_loops == 0) {
3660 com_error(c, PyExc_SyntaxError,
3661 "'break' outside loop");
3663 com_addbyte(c, BREAK_LOOP);
3664 break;
3665 case continue_stmt:
3666 com_continue_stmt(c, n);
3667 break;
3668 case return_stmt:
3669 com_return_stmt(c, n);
3670 break;
3671 case yield_stmt:
3672 com_yield_stmt(c, n);
3673 break;
3674 case raise_stmt:
3675 com_raise_stmt(c, n);
3676 break;
3677 case import_stmt:
3678 com_import_stmt(c, n);
3679 break;
3680 case global_stmt:
3681 break;
3682 case exec_stmt:
3683 com_exec_stmt(c, n);
3684 break;
3685 case assert_stmt:
3686 com_assert_stmt(c, n);
3687 break;
3688 case if_stmt:
3689 com_if_stmt(c, n);
3690 break;
3691 case while_stmt:
3692 com_while_stmt(c, n);
3693 break;
3694 case for_stmt:
3695 com_for_stmt(c, n);
3696 break;
3697 case try_stmt:
3698 com_try_stmt(c, n);
3699 break;
3700 case suite:
3701 com_suite(c, n);
3702 break;
3704 /* Expression nodes */
3706 case testlist:
3707 case testlist_safe:
3708 com_list(c, n, 0);
3709 break;
3710 case test:
3711 com_test(c, n);
3712 break;
3713 case and_test:
3714 com_and_test(c, n);
3715 break;
3716 case not_test:
3717 com_not_test(c, n);
3718 break;
3719 case comparison:
3720 com_comparison(c, n);
3721 break;
3722 case exprlist:
3723 com_list(c, n, 0);
3724 break;
3725 case expr:
3726 com_expr(c, n);
3727 break;
3728 case xor_expr:
3729 com_xor_expr(c, n);
3730 break;
3731 case and_expr:
3732 com_and_expr(c, n);
3733 break;
3734 case shift_expr:
3735 com_shift_expr(c, n);
3736 break;
3737 case arith_expr:
3738 com_arith_expr(c, n);
3739 break;
3740 case term:
3741 com_term(c, n);
3742 break;
3743 case factor:
3744 com_factor(c, n);
3745 break;
3746 case power:
3747 com_power(c, n);
3748 break;
3749 case atom:
3750 com_atom(c, n);
3751 break;
3753 default:
3754 com_error(c, PyExc_SystemError,
3755 "com_node: unexpected node type");
3759 static void com_fplist(struct compiling *, node *);
3761 static void
3762 com_fpdef(struct compiling *c, node *n)
3764 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3765 if (TYPE(CHILD(n, 0)) == LPAR)
3766 com_fplist(c, CHILD(n, 1));
3767 else {
3768 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3769 com_pop(c, 1);
3773 static void
3774 com_fplist(struct compiling *c, node *n)
3776 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3777 if (NCH(n) == 1) {
3778 com_fpdef(c, CHILD(n, 0));
3780 else {
3781 int i = (NCH(n)+1)/2;
3782 com_addoparg(c, UNPACK_SEQUENCE, i);
3783 com_push(c, i-1);
3784 for (i = 0; i < NCH(n); i += 2)
3785 com_fpdef(c, CHILD(n, i));
3789 static void
3790 com_arglist(struct compiling *c, node *n)
3792 int nch, i, narg;
3793 int complex = 0;
3794 char nbuf[30];
3795 REQ(n, varargslist);
3796 /* varargslist:
3797 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3798 nch = NCH(n);
3799 /* Enter all arguments in table of locals */
3800 for (i = 0, narg = 0; i < nch; i++) {
3801 node *ch = CHILD(n, i);
3802 node *fp;
3803 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3804 break;
3805 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3806 fp = CHILD(ch, 0);
3807 if (TYPE(fp) != NAME) {
3808 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
3809 complex = 1;
3811 narg++;
3812 /* all name updates handled by symtable */
3813 if (++i >= nch)
3814 break;
3815 ch = CHILD(n, i);
3816 if (TYPE(ch) == EQUAL)
3817 i += 2;
3818 else
3819 REQ(ch, COMMA);
3821 if (complex) {
3822 /* Generate code for complex arguments only after
3823 having counted the simple arguments */
3824 int ilocal = 0;
3825 for (i = 0; i < nch; i++) {
3826 node *ch = CHILD(n, i);
3827 node *fp;
3828 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3829 break;
3830 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3831 fp = CHILD(ch, 0);
3832 if (TYPE(fp) != NAME) {
3833 com_addoparg(c, LOAD_FAST, ilocal);
3834 com_push(c, 1);
3835 com_fpdef(c, ch);
3837 ilocal++;
3838 if (++i >= nch)
3839 break;
3840 ch = CHILD(n, i);
3841 if (TYPE(ch) == EQUAL)
3842 i += 2;
3843 else
3844 REQ(ch, COMMA);
3849 static void
3850 com_file_input(struct compiling *c, node *n)
3852 int i;
3853 PyObject *doc;
3854 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3855 doc = get_docstring(c, n);
3856 if (doc != NULL) {
3857 int i = com_addconst(c, doc);
3858 Py_DECREF(doc);
3859 com_addoparg(c, LOAD_CONST, i);
3860 com_push(c, 1);
3861 com_addop_name(c, STORE_NAME, "__doc__");
3862 com_pop(c, 1);
3864 for (i = 0; i < NCH(n); i++) {
3865 node *ch = CHILD(n, i);
3866 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3867 com_node(c, ch);
3871 /* Top-level compile-node interface */
3873 static void
3874 compile_funcdef(struct compiling *c, node *n)
3876 PyObject *doc;
3877 node *ch;
3878 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3879 c->c_name = STR(CHILD(n, 1));
3880 doc = get_docstring(c, CHILD(n, 4));
3881 if (doc != NULL) {
3882 (void) com_addconst(c, doc);
3883 Py_DECREF(doc);
3885 else
3886 (void) com_addconst(c, Py_None); /* No docstring */
3887 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3888 ch = CHILD(ch, 1); /* ')' | varargslist */
3889 if (TYPE(ch) == varargslist)
3890 com_arglist(c, ch);
3891 c->c_infunction = 1;
3892 com_node(c, CHILD(n, 4));
3893 c->c_infunction = 0;
3894 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3895 com_push(c, 1);
3896 com_addbyte(c, RETURN_VALUE);
3897 com_pop(c, 1);
3900 static void
3901 compile_lambdef(struct compiling *c, node *n)
3903 node *ch;
3904 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3905 c->c_name = "<lambda>";
3907 ch = CHILD(n, 1);
3908 (void) com_addconst(c, Py_None); /* No docstring */
3909 if (TYPE(ch) == varargslist) {
3910 com_arglist(c, ch);
3911 ch = CHILD(n, 3);
3913 else
3914 ch = CHILD(n, 2);
3915 com_node(c, ch);
3916 com_addbyte(c, RETURN_VALUE);
3917 com_pop(c, 1);
3920 static void
3921 compile_classdef(struct compiling *c, node *n)
3923 node *ch;
3924 PyObject *doc;
3925 REQ(n, classdef);
3926 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3927 c->c_name = STR(CHILD(n, 1));
3928 c->c_private = c->c_name;
3929 ch = CHILD(n, NCH(n)-1); /* The suite */
3930 doc = get_docstring(c, ch);
3931 if (doc != NULL) {
3932 int i = com_addconst(c, doc);
3933 Py_DECREF(doc);
3934 com_addoparg(c, LOAD_CONST, i);
3935 com_push(c, 1);
3936 com_addop_name(c, STORE_NAME, "__doc__");
3937 com_pop(c, 1);
3939 else
3940 (void) com_addconst(c, Py_None);
3941 com_node(c, ch);
3942 com_addbyte(c, LOAD_LOCALS);
3943 com_push(c, 1);
3944 com_addbyte(c, RETURN_VALUE);
3945 com_pop(c, 1);
3948 static void
3949 compile_node(struct compiling *c, node *n)
3951 com_addoparg(c, SET_LINENO, n->n_lineno);
3953 switch (TYPE(n)) {
3955 case single_input: /* One interactive command */
3956 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3957 c->c_interactive++;
3958 n = CHILD(n, 0);
3959 if (TYPE(n) != NEWLINE)
3960 com_node(c, n);
3961 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3962 com_push(c, 1);
3963 com_addbyte(c, RETURN_VALUE);
3964 com_pop(c, 1);
3965 c->c_interactive--;
3966 break;
3968 case file_input: /* A whole file, or built-in function exec() */
3969 com_file_input(c, n);
3970 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3971 com_push(c, 1);
3972 com_addbyte(c, RETURN_VALUE);
3973 com_pop(c, 1);
3974 break;
3976 case eval_input: /* Built-in function input() */
3977 com_node(c, CHILD(n, 0));
3978 com_addbyte(c, RETURN_VALUE);
3979 com_pop(c, 1);
3980 break;
3982 case lambdef: /* anonymous function definition */
3983 compile_lambdef(c, n);
3984 break;
3986 case funcdef: /* A function definition */
3987 compile_funcdef(c, n);
3988 break;
3990 case classdef: /* A class definition */
3991 compile_classdef(c, n);
3992 break;
3994 default:
3995 com_error(c, PyExc_SystemError,
3996 "compile_node: unexpected node type");
4000 static PyObject *
4001 dict_keys_inorder(PyObject *dict, int offset)
4003 PyObject *tuple, *k, *v;
4004 int i, pos = 0, size = PyDict_Size(dict);
4006 tuple = PyTuple_New(size);
4007 if (tuple == NULL)
4008 return NULL;
4009 while (PyDict_Next(dict, &pos, &k, &v)) {
4010 i = PyInt_AS_LONG(v);
4011 Py_INCREF(k);
4012 assert((i - offset) < size);
4013 PyTuple_SET_ITEM(tuple, i - offset, k);
4015 return tuple;
4018 PyCodeObject *
4019 PyNode_Compile(node *n, char *filename)
4021 return PyNode_CompileFlags(n, filename, NULL);
4024 PyCodeObject *
4025 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4027 return jcompile(n, filename, NULL, flags);
4030 struct symtable *
4031 PyNode_CompileSymtable(node *n, char *filename)
4033 struct symtable *st;
4034 PyFutureFeatures *ff;
4036 ff = PyNode_Future(n, filename);
4037 if (ff == NULL)
4038 return NULL;
4040 st = symtable_init();
4041 if (st == NULL) {
4042 PyMem_Free((void *)ff);
4043 return NULL;
4045 st->st_future = ff;
4046 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
4047 if (st->st_errors > 0)
4048 goto fail;
4049 symtable_node(st, n);
4050 if (st->st_errors > 0)
4051 goto fail;
4053 return st;
4054 fail:
4055 PyMem_Free((void *)ff);
4056 st->st_future = NULL;
4057 PySymtable_Free(st);
4058 return NULL;
4061 static PyCodeObject *
4062 icompile(node *n, struct compiling *base)
4064 return jcompile(n, base->c_filename, base, NULL);
4067 static PyCodeObject *
4068 jcompile(node *n, char *filename, struct compiling *base,
4069 PyCompilerFlags *flags)
4071 struct compiling sc;
4072 PyCodeObject *co;
4073 if (!com_init(&sc, filename))
4074 return NULL;
4075 if (base) {
4076 sc.c_private = base->c_private;
4077 sc.c_symtable = base->c_symtable;
4078 /* c_symtable still points to parent's symbols */
4079 if (base->c_nested
4080 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
4081 sc.c_nested = 1;
4082 sc.c_flags |= base->c_flags & PyCF_MASK;
4083 } else {
4084 sc.c_private = NULL;
4085 sc.c_future = PyNode_Future(n, filename);
4086 if (sc.c_future == NULL) {
4087 com_free(&sc);
4088 return NULL;
4090 if (flags) {
4091 int merged = sc.c_future->ff_features |
4092 flags->cf_flags;
4093 sc.c_future->ff_features = merged;
4094 flags->cf_flags = merged;
4096 if (symtable_build(&sc, n) < 0) {
4097 com_free(&sc);
4098 return NULL;
4101 co = NULL;
4102 if (symtable_load_symbols(&sc) < 0) {
4103 sc.c_errors++;
4104 goto exit;
4106 compile_node(&sc, n);
4107 com_done(&sc);
4108 if (sc.c_errors == 0) {
4109 PyObject *consts, *names, *varnames, *filename, *name,
4110 *freevars, *cellvars;
4111 consts = PyList_AsTuple(sc.c_consts);
4112 names = PyList_AsTuple(sc.c_names);
4113 varnames = PyList_AsTuple(sc.c_varnames);
4114 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4115 freevars = dict_keys_inorder(sc.c_freevars,
4116 PyTuple_GET_SIZE(cellvars));
4117 filename = PyString_InternFromString(sc.c_filename);
4118 name = PyString_InternFromString(sc.c_name);
4119 if (!PyErr_Occurred())
4120 co = PyCode_New(sc.c_argcount,
4121 sc.c_nlocals,
4122 sc.c_maxstacklevel,
4123 sc.c_flags,
4124 sc.c_code,
4125 consts,
4126 names,
4127 varnames,
4128 freevars,
4129 cellvars,
4130 filename,
4131 name,
4132 sc.c_firstlineno,
4133 sc.c_lnotab);
4134 Py_XDECREF(consts);
4135 Py_XDECREF(names);
4136 Py_XDECREF(varnames);
4137 Py_XDECREF(freevars);
4138 Py_XDECREF(cellvars);
4139 Py_XDECREF(filename);
4140 Py_XDECREF(name);
4142 else if (!PyErr_Occurred()) {
4143 /* This could happen if someone called PyErr_Clear() after an
4144 error was reported above. That's not supposed to happen,
4145 but I just plugged one case and I'm not sure there can't be
4146 others. In that case, raise SystemError so that at least
4147 it gets reported instead dumping core. */
4148 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4150 exit:
4151 if (base == NULL) {
4152 PySymtable_Free(sc.c_symtable);
4153 sc.c_symtable = NULL;
4155 com_free(&sc);
4156 return co;
4160 PyCode_Addr2Line(PyCodeObject *co, int addrq)
4162 int size = PyString_Size(co->co_lnotab) / 2;
4163 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
4164 int line = co->co_firstlineno;
4165 int addr = 0;
4166 while (--size >= 0) {
4167 addr += *p++;
4168 if (addr > addrq)
4169 break;
4170 line += *p++;
4172 return line;
4175 /* The test for LOCAL must come before the test for FREE in order to
4176 handle classes where name is both local and free. The local var is
4177 a method and the free var is a free var referenced within a method.
4180 static int
4181 get_ref_type(struct compiling *c, char *name)
4183 char buf[350];
4184 PyObject *v;
4186 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4187 return CELL;
4188 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4189 return LOCAL;
4190 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4191 return FREE;
4192 v = PyDict_GetItemString(c->c_globals, name);
4193 if (v) {
4194 if (v == Py_None)
4195 return GLOBAL_EXPLICIT;
4196 else {
4197 return GLOBAL_IMPLICIT;
4200 PyOS_snprintf(buf, sizeof(buf),
4201 "unknown scope for %.100s in %.100s(%s) "
4202 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4203 name, c->c_name,
4204 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4205 c->c_filename,
4206 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4207 PyObject_REPR(c->c_locals),
4208 PyObject_REPR(c->c_globals)
4211 Py_FatalError(buf);
4212 return -1;
4215 /* Helper functions to issue warnings */
4217 static int
4218 issue_warning(char *msg, char *filename, int lineno)
4220 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4221 lineno, NULL, NULL) < 0) {
4222 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4223 PyErr_SetString(PyExc_SyntaxError, msg);
4224 PyErr_SyntaxLocation(filename, lineno);
4226 return -1;
4228 return 0;
4231 static int
4232 symtable_warn(struct symtable *st, char *msg)
4234 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
4235 st->st_errors++;
4236 return -1;
4238 return 0;
4241 /* Helper function for setting lineno and filename */
4243 static int
4244 symtable_build(struct compiling *c, node *n)
4246 if ((c->c_symtable = symtable_init()) == NULL)
4247 return -1;
4248 c->c_symtable->st_future = c->c_future;
4249 c->c_symtable->st_filename = c->c_filename;
4250 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4251 if (c->c_symtable->st_errors > 0)
4252 return -1;
4253 symtable_node(c->c_symtable, n);
4254 if (c->c_symtable->st_errors > 0)
4255 return -1;
4256 /* reset for second pass */
4257 c->c_symtable->st_nscopes = 1;
4258 c->c_symtable->st_pass = 2;
4259 return 0;
4262 static int
4263 symtable_init_compiling_symbols(struct compiling *c)
4265 PyObject *varnames;
4267 varnames = c->c_symtable->st_cur->ste_varnames;
4268 if (varnames == NULL) {
4269 varnames = PyList_New(0);
4270 if (varnames == NULL)
4271 return -1;
4272 c->c_symtable->st_cur->ste_varnames = varnames;
4273 Py_INCREF(varnames);
4274 } else
4275 Py_INCREF(varnames);
4276 c->c_varnames = varnames;
4278 c->c_globals = PyDict_New();
4279 if (c->c_globals == NULL)
4280 return -1;
4281 c->c_freevars = PyDict_New();
4282 if (c->c_freevars == NULL)
4283 return -1;
4284 c->c_cellvars = PyDict_New();
4285 if (c->c_cellvars == NULL)
4286 return -1;
4287 return 0;
4290 struct symbol_info {
4291 int si_nlocals;
4292 int si_ncells;
4293 int si_nfrees;
4294 int si_nimplicit;
4297 static void
4298 symtable_init_info(struct symbol_info *si)
4300 si->si_nlocals = 0;
4301 si->si_ncells = 0;
4302 si->si_nfrees = 0;
4303 si->si_nimplicit = 0;
4306 static int
4307 symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
4308 struct symbol_info *si)
4310 PyObject *dict, *v;
4312 /* Seperate logic for DEF_FREE. If it occurs in a function,
4313 it indicates a local that we must allocate storage for (a
4314 cell var). If it occurs in a class, then the class has a
4315 method and a free variable with the same name.
4317 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4318 /* If it isn't declared locally, it can't be a cell. */
4319 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4320 return 0;
4321 v = PyInt_FromLong(si->si_ncells++);
4322 dict = c->c_cellvars;
4323 } else {
4324 /* If it is free anyway, then there is no need to do
4325 anything here.
4327 if (is_free(flags ^ DEF_FREE_CLASS)
4328 || (flags == DEF_FREE_CLASS))
4329 return 0;
4330 v = PyInt_FromLong(si->si_nfrees++);
4331 dict = c->c_freevars;
4333 if (v == NULL)
4334 return -1;
4335 if (PyDict_SetItem(dict, name, v) < 0) {
4336 Py_DECREF(v);
4337 return -1;
4339 Py_DECREF(v);
4340 return 0;
4343 /* If a variable is a cell and an argument, make sure that appears in
4344 co_cellvars before any variable to its right in varnames.
4348 static int
4349 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4350 PyObject *varnames, int flags)
4352 PyObject *v, *w, *d, *list = NULL;
4353 int i, pos;
4355 if (flags & CO_VARARGS)
4356 argcount++;
4357 if (flags & CO_VARKEYWORDS)
4358 argcount++;
4359 for (i = argcount; --i >= 0; ) {
4360 v = PyList_GET_ITEM(varnames, i);
4361 if (PyDict_GetItem(*cellvars, v)) {
4362 if (list == NULL) {
4363 list = PyList_New(1);
4364 if (list == NULL)
4365 return -1;
4366 PyList_SET_ITEM(list, 0, v);
4367 Py_INCREF(v);
4368 } else
4369 PyList_Insert(list, 0, v);
4372 if (list == NULL || PyList_GET_SIZE(list) == 0)
4373 return 0;
4374 /* There are cellvars that are also arguments. Create a dict
4375 to replace cellvars and put the args at the front.
4377 d = PyDict_New();
4378 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4379 v = PyInt_FromLong(i);
4380 if (v == NULL)
4381 goto fail;
4382 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4383 goto fail;
4384 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4385 goto fail;
4387 pos = 0;
4388 i = PyList_GET_SIZE(list);
4389 Py_DECREF(list);
4390 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4391 w = PyInt_FromLong(i++); /* don't care about the old key */
4392 if (PyDict_SetItem(d, v, w) < 0) {
4393 Py_DECREF(w);
4394 goto fail;
4396 Py_DECREF(w);
4398 Py_DECREF(*cellvars);
4399 *cellvars = d;
4400 return 1;
4401 fail:
4402 Py_DECREF(d);
4403 return -1;
4406 static int
4407 symtable_freevar_offsets(PyObject *freevars, int offset)
4409 PyObject *name, *v;
4410 int pos;
4412 /* The cell vars are the first elements of the closure,
4413 followed by the free vars. Update the offsets in
4414 c_freevars to account for number of cellvars. */
4415 pos = 0;
4416 while (PyDict_Next(freevars, &pos, &name, &v)) {
4417 int i = PyInt_AS_LONG(v) + offset;
4418 PyObject *o = PyInt_FromLong(i);
4419 if (o == NULL)
4420 return -1;
4421 if (PyDict_SetItem(freevars, name, o) < 0) {
4422 Py_DECREF(o);
4423 return -1;
4425 Py_DECREF(o);
4427 return 0;
4430 static int
4431 symtable_check_unoptimized(struct compiling *c,
4432 PySymtableEntryObject *ste,
4433 struct symbol_info *si)
4435 char buf[300];
4437 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4438 || (ste->ste_nested && si->si_nimplicit)))
4439 return 0;
4441 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4443 #define ILLEGAL_IS "is a nested function"
4445 #define ILLEGAL_IMPORT_STAR \
4446 "import * is not allowed in function '%.100s' because it %s"
4448 #define ILLEGAL_BARE_EXEC \
4449 "unqualified exec is not allowed in function '%.100s' it %s"
4451 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4452 "function '%.100s' uses import * and bare exec, which are illegal" \
4453 "because it %s"
4455 /* XXX perhaps the linenos for these opt-breaking statements
4456 should be stored so the exception can point to them. */
4458 if (ste->ste_child_free) {
4459 if (ste->ste_optimized == OPT_IMPORT_STAR)
4460 PyOS_snprintf(buf, sizeof(buf),
4461 ILLEGAL_IMPORT_STAR,
4462 PyString_AS_STRING(ste->ste_name),
4463 ILLEGAL_CONTAINS);
4464 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4465 PyOS_snprintf(buf, sizeof(buf),
4466 ILLEGAL_BARE_EXEC,
4467 PyString_AS_STRING(ste->ste_name),
4468 ILLEGAL_CONTAINS);
4469 else {
4470 PyOS_snprintf(buf, sizeof(buf),
4471 ILLEGAL_EXEC_AND_IMPORT_STAR,
4472 PyString_AS_STRING(ste->ste_name),
4473 ILLEGAL_CONTAINS);
4475 } else {
4476 if (ste->ste_optimized == OPT_IMPORT_STAR)
4477 PyOS_snprintf(buf, sizeof(buf),
4478 ILLEGAL_IMPORT_STAR,
4479 PyString_AS_STRING(ste->ste_name),
4480 ILLEGAL_IS);
4481 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4482 PyOS_snprintf(buf, sizeof(buf),
4483 ILLEGAL_BARE_EXEC,
4484 PyString_AS_STRING(ste->ste_name),
4485 ILLEGAL_IS);
4486 else {
4487 PyOS_snprintf(buf, sizeof(buf),
4488 ILLEGAL_EXEC_AND_IMPORT_STAR,
4489 PyString_AS_STRING(ste->ste_name),
4490 ILLEGAL_IS);
4494 PyErr_SetString(PyExc_SyntaxError, buf);
4495 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4496 ste->ste_opt_lineno);
4497 return -1;
4500 static int
4501 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4502 struct symbol_info *si)
4504 if (c->c_future)
4505 c->c_flags |= c->c_future->ff_features;
4506 if (ste->ste_generator)
4507 c->c_flags |= CO_GENERATOR;
4508 if (ste->ste_type != TYPE_MODULE)
4509 c->c_flags |= CO_NEWLOCALS;
4510 if (ste->ste_type == TYPE_FUNCTION) {
4511 c->c_nlocals = si->si_nlocals;
4512 if (ste->ste_optimized == 0)
4513 c->c_flags |= CO_OPTIMIZED;
4514 else if (ste->ste_optimized != OPT_EXEC)
4515 return symtable_check_unoptimized(c, ste, si);
4517 return 0;
4520 static int
4521 symtable_load_symbols(struct compiling *c)
4523 static PyObject *implicit = NULL;
4524 struct symtable *st = c->c_symtable;
4525 PySymtableEntryObject *ste = st->st_cur;
4526 PyObject *name, *varnames, *v;
4527 int i, flags, pos;
4528 struct symbol_info si;
4530 if (implicit == NULL) {
4531 implicit = PyInt_FromLong(1);
4532 if (implicit == NULL)
4533 return -1;
4535 v = NULL;
4537 if (symtable_init_compiling_symbols(c) < 0)
4538 goto fail;
4539 symtable_init_info(&si);
4540 varnames = st->st_cur->ste_varnames;
4541 si.si_nlocals = PyList_GET_SIZE(varnames);
4542 c->c_argcount = si.si_nlocals;
4544 for (i = 0; i < si.si_nlocals; ++i) {
4545 v = PyInt_FromLong(i);
4546 if (PyDict_SetItem(c->c_locals,
4547 PyList_GET_ITEM(varnames, i), v) < 0)
4548 goto fail;
4549 Py_DECREF(v);
4552 /* XXX The cases below define the rules for whether a name is
4553 local or global. The logic could probably be clearer. */
4554 pos = 0;
4555 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4556 flags = PyInt_AS_LONG(v);
4558 if (flags & DEF_FREE_GLOBAL)
4559 /* undo the original DEF_FREE */
4560 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4562 /* Deal with names that need two actions:
4563 1. Cell variables that are also locals.
4564 2. Free variables in methods that are also class
4565 variables or declared global.
4567 if (flags & (DEF_FREE | DEF_FREE_CLASS))
4568 symtable_resolve_free(c, name, flags, &si);
4570 if (flags & DEF_STAR) {
4571 c->c_argcount--;
4572 c->c_flags |= CO_VARARGS;
4573 } else if (flags & DEF_DOUBLESTAR) {
4574 c->c_argcount--;
4575 c->c_flags |= CO_VARKEYWORDS;
4576 } else if (flags & DEF_INTUPLE)
4577 c->c_argcount--;
4578 else if (flags & DEF_GLOBAL) {
4579 if (flags & DEF_PARAM) {
4580 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4581 PyString_AS_STRING(name));
4582 PyErr_SyntaxLocation(st->st_filename,
4583 ste->ste_lineno);
4584 st->st_errors++;
4585 goto fail;
4587 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4588 goto fail;
4589 } else if (flags & DEF_FREE_GLOBAL) {
4590 si.si_nimplicit++;
4591 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4592 goto fail;
4593 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4594 v = PyInt_FromLong(si.si_nlocals++);
4595 if (v == NULL)
4596 goto fail;
4597 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4598 goto fail;
4599 Py_DECREF(v);
4600 if (ste->ste_type != TYPE_CLASS)
4601 if (PyList_Append(c->c_varnames, name) < 0)
4602 goto fail;
4603 } else if (is_free(flags)) {
4604 if (ste->ste_nested) {
4605 v = PyInt_FromLong(si.si_nfrees++);
4606 if (v == NULL)
4607 goto fail;
4608 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4609 goto fail;
4610 Py_DECREF(v);
4611 } else {
4612 si.si_nimplicit++;
4613 if (PyDict_SetItem(c->c_globals, name,
4614 implicit) < 0)
4615 goto fail;
4616 if (st->st_nscopes != 1) {
4617 v = PyInt_FromLong(flags);
4618 if (PyDict_SetItem(st->st_global,
4619 name, v))
4620 goto fail;
4621 Py_DECREF(v);
4627 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4629 if (si.si_ncells > 1) { /* one cell is always in order */
4630 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4631 c->c_varnames, c->c_flags) < 0)
4632 return -1;
4634 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4635 return -1;
4636 return symtable_update_flags(c, ste, &si);
4637 fail:
4638 /* is this always the right thing to do? */
4639 Py_XDECREF(v);
4640 return -1;
4643 static struct symtable *
4644 symtable_init()
4646 struct symtable *st;
4648 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4649 if (st == NULL)
4650 return NULL;
4651 st->st_pass = 1;
4653 st->st_filename = NULL;
4654 if ((st->st_stack = PyList_New(0)) == NULL)
4655 goto fail;
4656 if ((st->st_symbols = PyDict_New()) == NULL)
4657 goto fail;
4658 st->st_cur = NULL;
4659 st->st_nscopes = 0;
4660 st->st_errors = 0;
4661 st->st_tmpname = 0;
4662 st->st_private = NULL;
4663 return st;
4664 fail:
4665 PySymtable_Free(st);
4666 return NULL;
4669 void
4670 PySymtable_Free(struct symtable *st)
4672 Py_XDECREF(st->st_symbols);
4673 Py_XDECREF(st->st_stack);
4674 Py_XDECREF(st->st_cur);
4675 PyMem_Free((void *)st);
4678 /* When the compiler exits a scope, it must should update the scope's
4679 free variable information with the list of free variables in its
4680 children.
4682 Variables that are free in children and defined in the current
4683 scope are cellvars.
4685 If the scope being exited is defined at the top-level (ste_nested is
4686 false), free variables in children that are not defined here are
4687 implicit globals.
4691 static int
4692 symtable_update_free_vars(struct symtable *st)
4694 int i, j, def;
4695 PyObject *o, *name, *list = NULL;
4696 PySymtableEntryObject *child, *ste = st->st_cur;
4698 if (ste->ste_type == TYPE_CLASS)
4699 def = DEF_FREE_CLASS;
4700 else
4701 def = DEF_FREE;
4702 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4703 int pos = 0;
4705 if (list)
4706 PyList_SetSlice(list, 0,
4707 ((PyVarObject*)list)->ob_size, 0);
4708 child = (PySymtableEntryObject *)
4709 PyList_GET_ITEM(ste->ste_children, i);
4710 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4711 int flags = PyInt_AS_LONG(o);
4712 if (!(is_free(flags)))
4713 continue; /* avoids indentation */
4714 if (list == NULL) {
4715 list = PyList_New(0);
4716 if (list == NULL)
4717 return -1;
4719 ste->ste_child_free = 1;
4720 if (PyList_Append(list, name) < 0) {
4721 Py_DECREF(list);
4722 return -1;
4725 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4726 PyObject *v;
4727 name = PyList_GET_ITEM(list, j);
4728 v = PyDict_GetItem(ste->ste_symbols, name);
4729 /* If a name N is declared global in scope A and
4730 referenced in scope B contained (perhaps
4731 indirectly) in A and there are no scopes
4732 with bindings for N between B and A, then N
4733 is global in B. Unless A is a class scope,
4734 because class scopes are not considered for
4735 nested scopes.
4737 if (v && (ste->ste_type != TYPE_CLASS)) {
4738 int flags = PyInt_AS_LONG(v);
4739 if (flags & DEF_GLOBAL) {
4740 symtable_undo_free(st, child->ste_id,
4741 name);
4742 continue;
4745 if (ste->ste_nested) {
4746 if (symtable_add_def_o(st, ste->ste_symbols,
4747 name, def) < 0) {
4748 Py_DECREF(list);
4749 return -1;
4751 } else {
4752 if (symtable_check_global(st, child->ste_id,
4753 name) < 0) {
4754 Py_DECREF(list);
4755 return -1;
4761 Py_XDECREF(list);
4762 return 0;
4765 /* If the current scope is a non-nested class or if name is not
4766 defined in the current, non-nested scope, then it is an implicit
4767 global in all nested scopes.
4770 static int
4771 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4773 PyObject *o;
4774 int v;
4775 PySymtableEntryObject *ste = st->st_cur;
4777 if (ste->ste_type == TYPE_CLASS)
4778 return symtable_undo_free(st, child, name);
4779 o = PyDict_GetItem(ste->ste_symbols, name);
4780 if (o == NULL)
4781 return symtable_undo_free(st, child, name);
4782 v = PyInt_AS_LONG(o);
4784 if (is_free(v) || (v & DEF_GLOBAL))
4785 return symtable_undo_free(st, child, name);
4786 else
4787 return symtable_add_def_o(st, ste->ste_symbols,
4788 name, DEF_FREE);
4791 static int
4792 symtable_undo_free(struct symtable *st, PyObject *id,
4793 PyObject *name)
4795 int i, v, x;
4796 PyObject *info;
4797 PySymtableEntryObject *ste;
4799 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4800 if (ste == NULL)
4801 return -1;
4803 info = PyDict_GetItem(ste->ste_symbols, name);
4804 if (info == NULL)
4805 return 0;
4806 v = PyInt_AS_LONG(info);
4807 if (is_free(v)) {
4808 if (symtable_add_def_o(st, ste->ste_symbols, name,
4809 DEF_FREE_GLOBAL) < 0)
4810 return -1;
4811 } else
4812 /* If the name is defined here or declared global,
4813 then the recursion stops. */
4814 return 0;
4816 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4817 PySymtableEntryObject *child;
4818 child = (PySymtableEntryObject *)
4819 PyList_GET_ITEM(ste->ste_children, i);
4820 x = symtable_undo_free(st, child->ste_id, name);
4821 if (x < 0)
4822 return x;
4824 return 0;
4827 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4828 This reference is released when the scope is exited, via the DECREF
4829 in symtable_exit_scope().
4832 static int
4833 symtable_exit_scope(struct symtable *st)
4835 int end;
4837 if (st->st_pass == 1)
4838 symtable_update_free_vars(st);
4839 Py_DECREF(st->st_cur);
4840 end = PyList_GET_SIZE(st->st_stack) - 1;
4841 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4842 end);
4843 if (PySequence_DelItem(st->st_stack, end) < 0)
4844 return -1;
4845 return 0;
4848 static void
4849 symtable_enter_scope(struct symtable *st, char *name, int type,
4850 int lineno)
4852 PySymtableEntryObject *prev = NULL;
4854 if (st->st_cur) {
4855 prev = st->st_cur;
4856 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4857 Py_DECREF(st->st_cur);
4858 st->st_errors++;
4859 return;
4862 st->st_cur = (PySymtableEntryObject *)
4863 PySymtableEntry_New(st, name, type, lineno);
4864 if (strcmp(name, TOP) == 0)
4865 st->st_global = st->st_cur->ste_symbols;
4866 if (prev && st->st_pass == 1) {
4867 if (PyList_Append(prev->ste_children,
4868 (PyObject *)st->st_cur) < 0)
4869 st->st_errors++;
4873 static int
4874 symtable_lookup(struct symtable *st, char *name)
4876 char buffer[MANGLE_LEN];
4877 PyObject *v;
4878 int flags;
4880 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4881 name = buffer;
4882 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4883 if (v == NULL) {
4884 if (PyErr_Occurred())
4885 return -1;
4886 else
4887 return 0;
4890 flags = PyInt_AS_LONG(v);
4891 return flags;
4894 static int
4895 symtable_add_def(struct symtable *st, char *name, int flag)
4897 PyObject *s;
4898 char buffer[MANGLE_LEN];
4899 int ret;
4901 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4902 name = buffer;
4903 if ((s = PyString_InternFromString(name)) == NULL)
4904 return -1;
4905 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4906 Py_DECREF(s);
4907 return ret;
4910 /* Must only be called with mangled names */
4912 static int
4913 symtable_add_def_o(struct symtable *st, PyObject *dict,
4914 PyObject *name, int flag)
4916 PyObject *o;
4917 int val;
4919 if ((o = PyDict_GetItem(dict, name))) {
4920 val = PyInt_AS_LONG(o);
4921 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
4922 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4923 PyString_AsString(name));
4924 PyErr_SyntaxLocation(st->st_filename,
4925 st->st_cur->ste_lineno);
4926 return -1;
4928 val |= flag;
4929 } else
4930 val = flag;
4931 o = PyInt_FromLong(val);
4932 if (PyDict_SetItem(dict, name, o) < 0) {
4933 Py_DECREF(o);
4934 return -1;
4936 Py_DECREF(o);
4938 if (flag & DEF_PARAM) {
4939 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
4940 return -1;
4941 } else if (flag & DEF_GLOBAL) {
4942 /* XXX need to update DEF_GLOBAL for other flags too;
4943 perhaps only DEF_FREE_GLOBAL */
4944 if ((o = PyDict_GetItem(st->st_global, name))) {
4945 val = PyInt_AS_LONG(o);
4946 val |= flag;
4947 } else
4948 val = flag;
4949 o = PyInt_FromLong(val);
4950 if (PyDict_SetItem(st->st_global, name, o) < 0) {
4951 Py_DECREF(o);
4952 return -1;
4954 Py_DECREF(o);
4956 return 0;
4959 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4961 /* Look for a yield stmt under n. Return 1 if found, else 0.
4962 This hack is used to look inside "if 0:" blocks (which are normally
4963 ignored) in case those are the only places a yield occurs (so that this
4964 function is a generator). */
4965 static int
4966 look_for_yield(node *n)
4968 int i;
4970 for (i = 0; i < NCH(n); ++i) {
4971 node *kid = CHILD(n, i);
4973 switch (TYPE(kid)) {
4975 case classdef:
4976 case funcdef:
4977 case lambdef:
4978 /* Stuff in nested functions and classes can't make
4979 the parent a generator. */
4980 return 0;
4982 case yield_stmt:
4983 return 1;
4985 default:
4986 if (look_for_yield(kid))
4987 return 1;
4990 return 0;
4993 static void
4994 symtable_node(struct symtable *st, node *n)
4996 int i;
4998 loop:
4999 switch (TYPE(n)) {
5000 case funcdef: {
5001 char *func_name = STR(CHILD(n, 1));
5002 symtable_add_def(st, func_name, DEF_LOCAL);
5003 symtable_default_args(st, CHILD(n, 2));
5004 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
5005 symtable_funcdef(st, n);
5006 symtable_exit_scope(st);
5007 break;
5009 case lambdef:
5010 if (NCH(n) == 4)
5011 symtable_default_args(st, CHILD(n, 1));
5012 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
5013 symtable_funcdef(st, n);
5014 symtable_exit_scope(st);
5015 break;
5016 case classdef: {
5017 char *tmp, *class_name = STR(CHILD(n, 1));
5018 symtable_add_def(st, class_name, DEF_LOCAL);
5019 if (TYPE(CHILD(n, 2)) == LPAR) {
5020 node *bases = CHILD(n, 3);
5021 int i;
5022 for (i = 0; i < NCH(bases); i += 2) {
5023 symtable_node(st, CHILD(bases, i));
5026 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
5027 tmp = st->st_private;
5028 st->st_private = class_name;
5029 symtable_node(st, CHILD(n, NCH(n) - 1));
5030 st->st_private = tmp;
5031 symtable_exit_scope(st);
5032 break;
5034 case if_stmt:
5035 for (i = 0; i + 3 < NCH(n); i += 4) {
5036 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5037 if (st->st_cur->ste_generator == 0)
5038 st->st_cur->ste_generator =
5039 look_for_yield(CHILD(n, i+3));
5040 continue;
5042 symtable_node(st, CHILD(n, i + 1));
5043 symtable_node(st, CHILD(n, i + 3));
5045 if (i + 2 < NCH(n))
5046 symtable_node(st, CHILD(n, i + 2));
5047 break;
5048 case global_stmt:
5049 symtable_global(st, n);
5050 break;
5051 case import_stmt:
5052 symtable_import(st, n);
5053 break;
5054 case exec_stmt: {
5055 st->st_cur->ste_optimized |= OPT_EXEC;
5056 symtable_node(st, CHILD(n, 1));
5057 if (NCH(n) > 2)
5058 symtable_node(st, CHILD(n, 3));
5059 else {
5060 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
5061 st->st_cur->ste_opt_lineno = n->n_lineno;
5063 if (NCH(n) > 4)
5064 symtable_node(st, CHILD(n, 5));
5065 break;
5068 case assert_stmt:
5069 if (Py_OptimizeFlag)
5070 return;
5071 if (NCH(n) == 2) {
5072 n = CHILD(n, 1);
5073 goto loop;
5074 } else {
5075 symtable_node(st, CHILD(n, 1));
5076 n = CHILD(n, 3);
5077 goto loop;
5079 case except_clause:
5080 if (NCH(n) == 4)
5081 symtable_assign(st, CHILD(n, 3), 0);
5082 if (NCH(n) > 1) {
5083 n = CHILD(n, 1);
5084 goto loop;
5086 break;
5087 case del_stmt:
5088 symtable_assign(st, CHILD(n, 1), 0);
5089 break;
5090 case yield_stmt:
5091 st->st_cur->ste_generator = 1;
5092 n = CHILD(n, 1);
5093 goto loop;
5094 case expr_stmt:
5095 if (NCH(n) == 1)
5096 n = CHILD(n, 0);
5097 else {
5098 if (TYPE(CHILD(n, 1)) == augassign) {
5099 symtable_assign(st, CHILD(n, 0), 0);
5100 symtable_node(st, CHILD(n, 2));
5101 break;
5102 } else {
5103 int i;
5104 for (i = 0; i < NCH(n) - 2; i += 2)
5105 symtable_assign(st, CHILD(n, i), 0);
5106 n = CHILD(n, NCH(n) - 1);
5109 goto loop;
5110 case list_iter:
5111 n = CHILD(n, 0);
5112 if (TYPE(n) == list_for) {
5113 st->st_tmpname++;
5114 symtable_list_comprehension(st, n);
5115 st->st_tmpname--;
5116 } else {
5117 REQ(n, list_if);
5118 symtable_node(st, CHILD(n, 1));
5119 if (NCH(n) == 3) {
5120 n = CHILD(n, 2);
5121 goto loop;
5124 break;
5125 case for_stmt:
5126 symtable_assign(st, CHILD(n, 1), 0);
5127 for (i = 3; i < NCH(n); ++i)
5128 if (TYPE(CHILD(n, i)) >= single_input)
5129 symtable_node(st, CHILD(n, i));
5130 break;
5131 /* The remaining cases fall through to default except in
5132 special circumstances. This requires the individual cases
5133 to be coded with great care, even though they look like
5134 rather innocuous. Each case must double-check TYPE(n).
5136 case argument:
5137 if (TYPE(n) == argument && NCH(n) == 3) {
5138 n = CHILD(n, 2);
5139 goto loop;
5141 /* fall through */
5142 case listmaker:
5143 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5144 st->st_tmpname++;
5145 symtable_list_comprehension(st, CHILD(n, 1));
5146 symtable_node(st, CHILD(n, 0));
5147 st->st_tmpname--;
5148 break;
5150 /* fall through */
5151 case atom:
5152 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5153 symtable_add_use(st, STR(CHILD(n, 0)));
5154 break;
5156 /* fall through */
5157 default:
5158 /* Walk over every non-token child with a special case
5159 for one child.
5161 if (NCH(n) == 1) {
5162 n = CHILD(n, 0);
5163 goto loop;
5165 for (i = 0; i < NCH(n); ++i)
5166 if (TYPE(CHILD(n, i)) >= single_input)
5167 symtable_node(st, CHILD(n, i));
5171 static void
5172 symtable_funcdef(struct symtable *st, node *n)
5174 node *body;
5176 if (TYPE(n) == lambdef) {
5177 if (NCH(n) == 4)
5178 symtable_params(st, CHILD(n, 1));
5179 } else
5180 symtable_params(st, CHILD(n, 2));
5181 body = CHILD(n, NCH(n) - 1);
5182 symtable_node(st, body);
5185 /* The next two functions parse the argument tuple.
5186 symtable_default_arg() checks for names in the default arguments,
5187 which are references in the defining scope. symtable_params()
5188 parses the parameter names, which are defined in the function's
5189 body.
5191 varargslist:
5192 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5193 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5196 static void
5197 symtable_default_args(struct symtable *st, node *n)
5199 node *c;
5200 int i;
5202 if (TYPE(n) == parameters) {
5203 n = CHILD(n, 1);
5204 if (TYPE(n) == RPAR)
5205 return;
5207 REQ(n, varargslist);
5208 for (i = 0; i < NCH(n); i += 2) {
5209 c = CHILD(n, i);
5210 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5211 break;
5213 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5214 symtable_node(st, CHILD(n, i));
5218 static void
5219 symtable_params(struct symtable *st, node *n)
5221 int i, complex = -1, ext = 0;
5222 node *c = NULL;
5224 if (TYPE(n) == parameters) {
5225 n = CHILD(n, 1);
5226 if (TYPE(n) == RPAR)
5227 return;
5229 REQ(n, varargslist);
5230 for (i = 0; i < NCH(n); i += 2) {
5231 c = CHILD(n, i);
5232 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5233 ext = 1;
5234 break;
5236 if (TYPE(c) == test) {
5237 continue;
5239 if (TYPE(CHILD(c, 0)) == NAME)
5240 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5241 else {
5242 char nbuf[30];
5243 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
5244 symtable_add_def(st, nbuf, DEF_PARAM);
5245 complex = i;
5248 if (ext) {
5249 c = CHILD(n, i);
5250 if (TYPE(c) == STAR) {
5251 i++;
5252 symtable_add_def(st, STR(CHILD(n, i)),
5253 DEF_PARAM | DEF_STAR);
5254 i += 2;
5255 if (i >= NCH(n))
5256 c = NULL;
5257 else
5258 c = CHILD(n, i);
5260 if (c && TYPE(c) == DOUBLESTAR) {
5261 i++;
5262 symtable_add_def(st, STR(CHILD(n, i)),
5263 DEF_PARAM | DEF_DOUBLESTAR);
5266 if (complex >= 0) {
5267 int j;
5268 for (j = 0; j <= complex; j++) {
5269 c = CHILD(n, j);
5270 if (TYPE(c) == COMMA)
5271 c = CHILD(n, ++j);
5272 else if (TYPE(c) == EQUAL)
5273 c = CHILD(n, j += 3);
5274 if (TYPE(CHILD(c, 0)) == LPAR)
5275 symtable_params_fplist(st, CHILD(c, 1));
5280 static void
5281 symtable_params_fplist(struct symtable *st, node *n)
5283 int i;
5284 node *c;
5286 REQ(n, fplist);
5287 for (i = 0; i < NCH(n); i += 2) {
5288 c = CHILD(n, i);
5289 REQ(c, fpdef);
5290 if (NCH(c) == 1)
5291 symtable_add_def(st, STR(CHILD(c, 0)),
5292 DEF_PARAM | DEF_INTUPLE);
5293 else
5294 symtable_params_fplist(st, CHILD(c, 1));
5299 static void
5300 symtable_global(struct symtable *st, node *n)
5302 int i;
5304 /* XXX It might be helpful to warn about module-level global
5305 statements, but it's hard to tell the difference between
5306 module-level and a string passed to exec.
5309 for (i = 1; i < NCH(n); i += 2) {
5310 char *name = STR(CHILD(n, i));
5311 int flags;
5313 flags = symtable_lookup(st, name);
5314 if (flags < 0)
5315 continue;
5316 if (flags && flags != DEF_GLOBAL) {
5317 char buf[500];
5318 if (flags & DEF_PARAM) {
5319 PyErr_Format(PyExc_SyntaxError,
5320 "name '%.400s' is local and global",
5321 name);
5322 PyErr_SyntaxLocation(st->st_filename,
5323 st->st_cur->ste_lineno);
5324 st->st_errors++;
5325 return;
5327 else {
5328 if (flags & DEF_LOCAL)
5329 PyOS_snprintf(buf, sizeof(buf),
5330 GLOBAL_AFTER_ASSIGN,
5331 name);
5332 else
5333 PyOS_snprintf(buf, sizeof(buf),
5334 GLOBAL_AFTER_USE, name);
5335 symtable_warn(st, buf);
5338 symtable_add_def(st, name, DEF_GLOBAL);
5342 static void
5343 symtable_list_comprehension(struct symtable *st, node *n)
5345 char tmpname[30];
5347 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
5348 symtable_add_def(st, tmpname, DEF_LOCAL);
5349 symtable_assign(st, CHILD(n, 1), 0);
5350 symtable_node(st, CHILD(n, 3));
5351 if (NCH(n) == 5)
5352 symtable_node(st, CHILD(n, 4));
5355 static void
5356 symtable_import(struct symtable *st, node *n)
5358 int i;
5359 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5360 | 'from' dotted_name 'import'
5361 ('*' | import_as_name (',' import_as_name)*)
5362 import_as_name: NAME [NAME NAME]
5364 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5365 node *dotname = CHILD(n, 1);
5366 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5367 /* check for bogus imports */
5368 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5369 PyErr_SetString(PyExc_SyntaxError,
5370 LATE_FUTURE);
5371 PyErr_SyntaxLocation(st->st_filename,
5372 n->n_lineno);
5373 st->st_errors++;
5374 return;
5377 if (TYPE(CHILD(n, 3)) == STAR) {
5378 if (st->st_cur->ste_type != TYPE_MODULE) {
5379 if (symtable_warn(st,
5380 "import * only allowed at module level") < 0)
5381 return;
5383 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5384 st->st_cur->ste_opt_lineno = n->n_lineno;
5385 } else {
5386 for (i = 3; i < NCH(n); i += 2) {
5387 node *c = CHILD(n, i);
5388 if (NCH(c) > 1) /* import as */
5389 symtable_assign(st, CHILD(c, 2),
5390 DEF_IMPORT);
5391 else
5392 symtable_assign(st, CHILD(c, 0),
5393 DEF_IMPORT);
5396 } else {
5397 for (i = 1; i < NCH(n); i += 2) {
5398 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5403 /* The third argument to symatble_assign() is a flag to be passed to
5404 symtable_add_def() if it is eventually called. The flag is useful
5405 to specify the particular type of assignment that should be
5406 recorded, e.g. an assignment caused by import.
5409 static void
5410 symtable_assign(struct symtable *st, node *n, int def_flag)
5412 node *tmp;
5413 int i;
5415 loop:
5416 switch (TYPE(n)) {
5417 case lambdef:
5418 /* invalid assignment, e.g. lambda x:x=2. The next
5419 pass will catch this error. */
5420 return;
5421 case power:
5422 if (NCH(n) > 2) {
5423 for (i = 2; i < NCH(n); ++i)
5424 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5425 symtable_node(st, CHILD(n, i));
5427 if (NCH(n) > 1) {
5428 symtable_node(st, CHILD(n, 0));
5429 symtable_node(st, CHILD(n, 1));
5430 } else {
5431 n = CHILD(n, 0);
5432 goto loop;
5434 return;
5435 case listmaker:
5436 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5437 /* XXX This is an error, but the next pass
5438 will catch it. */
5439 return;
5440 } else {
5441 for (i = 0; i < NCH(n); i += 2)
5442 symtable_assign(st, CHILD(n, i), def_flag);
5444 return;
5445 case exprlist:
5446 case testlist:
5447 if (NCH(n) == 1) {
5448 n = CHILD(n, 0);
5449 goto loop;
5451 else {
5452 int i;
5453 for (i = 0; i < NCH(n); i += 2)
5454 symtable_assign(st, CHILD(n, i), def_flag);
5455 return;
5457 case atom:
5458 tmp = CHILD(n, 0);
5459 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5460 n = CHILD(n, 1);
5461 goto loop;
5462 } else if (TYPE(tmp) == NAME) {
5463 if (strcmp(STR(tmp), "__debug__") == 0) {
5464 PyErr_SetString(PyExc_SyntaxError,
5465 ASSIGN_DEBUG);
5466 PyErr_SyntaxLocation(st->st_filename,
5467 n->n_lineno);
5468 st->st_errors++;
5470 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
5472 return;
5473 case dotted_as_name:
5474 if (NCH(n) == 3)
5475 symtable_add_def(st, STR(CHILD(n, 2)),
5476 DEF_LOCAL | def_flag);
5477 else
5478 symtable_add_def(st,
5479 STR(CHILD(CHILD(n,
5480 0), 0)),
5481 DEF_LOCAL | def_flag);
5482 return;
5483 case dotted_name:
5484 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
5485 return;
5486 case NAME:
5487 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
5488 return;
5489 default:
5490 if (NCH(n) == 0)
5491 return;
5492 if (NCH(n) == 1) {
5493 n = CHILD(n, 0);
5494 goto loop;
5496 /* Should only occur for errors like x + 1 = 1,
5497 which will be caught in the next pass. */
5498 for (i = 0; i < NCH(n); ++i)
5499 if (TYPE(CHILD(n, i)) >= single_input)
5500 symtable_assign(st, CHILD(n, i), def_flag);