append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Python / compile.c
blobd916751ef7df9e3c0d9183150e7820bb5de393ea
1 /* Compile an expression node to intermediate code */
3 /* XXX TO DO:
4 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
6 XXX Generate simple jump for break/return outside 'try...finally'
7 XXX Allow 'continue' inside finally clause of try-finally
8 XXX New opcode for loading the initial index for a for loop
9 XXX other JAR tricks?
12 #include "Python.h"
14 #include "node.h"
15 #include "token.h"
16 #include "graminit.h"
17 #include "compile.h"
18 #include "symtable.h"
19 #include "opcode.h"
20 #include "structmember.h"
22 #include <ctype.h>
24 /* Three symbols from graminit.h are also defined in Python.h, with
25 Py_ prefixes to their names. Python.h can't include graminit.h
26 (which defines too many confusing symbols), but we can check here
27 that they haven't changed (which is very unlikely, but possible). */
28 #if Py_single_input != single_input
29 #error "single_input has changed -- update Py_single_input in Python.h"
30 #endif
31 #if Py_file_input != file_input
32 #error "file_input has changed -- update Py_file_input in Python.h"
33 #endif
34 #if Py_eval_input != eval_input
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
36 #endif
38 int Py_OptimizeFlag = 0;
40 #define OP_DELETE 0
41 #define OP_ASSIGN 1
42 #define OP_APPLY 2
44 #define VAR_LOAD 0
45 #define VAR_STORE 1
46 #define VAR_DELETE 2
48 #define DEL_CLOSURE_ERROR \
49 "can not delete variable '%.400s' referenced in nested scope"
51 #define DUPLICATE_ARGUMENT \
52 "duplicate argument '%s' in function definition"
54 #define ILLEGAL_DYNAMIC_SCOPE \
55 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
57 #define GLOBAL_AFTER_ASSIGN \
58 "name '%.400s' is assigned to before global declaration"
60 #define GLOBAL_AFTER_USE \
61 "name '%.400s' is used prior to global declaration"
63 #define LOCAL_GLOBAL \
64 "name '%.400s' is a function parameter and declared global"
66 #define LATE_FUTURE \
67 "from __future__ imports must occur at the beginning of the file"
69 #define ASSIGN_DEBUG \
70 "can not assign to __debug__"
72 #define MANGLE_LEN 256
74 #define OFF(x) offsetof(PyCodeObject, x)
76 static PyMemberDef code_memberlist[] = {
77 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
79 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
80 {"co_flags", T_INT, OFF(co_flags), READONLY},
81 {"co_code", T_OBJECT, OFF(co_code), READONLY},
82 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
83 {"co_names", T_OBJECT, OFF(co_names), READONLY},
84 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
85 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
87 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
88 {"co_name", T_OBJECT, OFF(co_name), READONLY},
89 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
91 {NULL} /* Sentinel */
94 PyDoc_STRVAR(code_doc,
95 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
96 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
97 \n\
98 Create a code object. Not for the faint of heart.");
100 static PyObject *
101 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
103 int argcount;
104 int nlocals;
105 int stacksize;
106 int flags;
107 PyObject *code;
108 PyObject *consts;
109 PyObject *names;
110 PyObject *varnames;
111 PyObject *freevars = NULL;
112 PyObject *cellvars = NULL;
113 PyObject *filename;
114 PyObject *name;
115 int firstlineno;
116 PyObject *lnotab;
118 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
119 &argcount, &nlocals, &stacksize, &flags,
120 &code,
121 &PyTuple_Type, &consts,
122 &PyTuple_Type, &names,
123 &PyTuple_Type, &varnames,
124 &filename, &name,
125 &firstlineno, &lnotab,
126 &PyTuple_Type, &freevars,
127 &PyTuple_Type, &cellvars))
128 return NULL;
130 if (freevars == NULL || cellvars == NULL) {
131 PyObject *empty = PyTuple_New(0);
132 if (empty == NULL)
133 return NULL;
134 if (freevars == NULL) {
135 freevars = empty;
136 Py_INCREF(freevars);
138 if (cellvars == NULL) {
139 cellvars = empty;
140 Py_INCREF(cellvars);
142 Py_DECREF(empty);
145 if (!PyObject_CheckReadBuffer(code)) {
146 PyErr_SetString(PyExc_TypeError,
147 "bytecode object must be a single-segment read-only buffer");
148 return NULL;
151 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
152 code, consts, names, varnames,
153 freevars, cellvars, filename, name,
154 firstlineno, lnotab);
157 static void
158 code_dealloc(PyCodeObject *co)
160 Py_XDECREF(co->co_code);
161 Py_XDECREF(co->co_consts);
162 Py_XDECREF(co->co_names);
163 Py_XDECREF(co->co_varnames);
164 Py_XDECREF(co->co_freevars);
165 Py_XDECREF(co->co_cellvars);
166 Py_XDECREF(co->co_filename);
167 Py_XDECREF(co->co_name);
168 Py_XDECREF(co->co_lnotab);
169 PyObject_DEL(co);
172 static PyObject *
173 code_repr(PyCodeObject *co)
175 char buf[500];
176 int lineno = -1;
177 char *filename = "???";
178 char *name = "???";
180 if (co->co_firstlineno != 0)
181 lineno = co->co_firstlineno;
182 if (co->co_filename && PyString_Check(co->co_filename))
183 filename = PyString_AS_STRING(co->co_filename);
184 if (co->co_name && PyString_Check(co->co_name))
185 name = PyString_AS_STRING(co->co_name);
186 PyOS_snprintf(buf, sizeof(buf),
187 "<code object %.100s at %p, file \"%.300s\", line %d>",
188 name, co, filename, lineno);
189 return PyString_FromString(buf);
192 static int
193 code_compare(PyCodeObject *co, PyCodeObject *cp)
195 int cmp;
196 cmp = PyObject_Compare(co->co_name, cp->co_name);
197 if (cmp) return cmp;
198 cmp = co->co_argcount - cp->co_argcount;
199 if (cmp) return (cmp<0)?-1:1;
200 cmp = co->co_nlocals - cp->co_nlocals;
201 if (cmp) return (cmp<0)?-1:1;
202 cmp = co->co_flags - cp->co_flags;
203 if (cmp) return (cmp<0)?-1:1;
204 cmp = PyObject_Compare(co->co_code, cp->co_code);
205 if (cmp) return cmp;
206 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
207 if (cmp) return cmp;
208 cmp = PyObject_Compare(co->co_names, cp->co_names);
209 if (cmp) return cmp;
210 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
211 if (cmp) return cmp;
212 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
213 if (cmp) return cmp;
214 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
215 return cmp;
218 static long
219 code_hash(PyCodeObject *co)
221 long h, h0, h1, h2, h3, h4, h5, h6;
222 h0 = PyObject_Hash(co->co_name);
223 if (h0 == -1) return -1;
224 h1 = PyObject_Hash(co->co_code);
225 if (h1 == -1) return -1;
226 h2 = PyObject_Hash(co->co_consts);
227 if (h2 == -1) return -1;
228 h3 = PyObject_Hash(co->co_names);
229 if (h3 == -1) return -1;
230 h4 = PyObject_Hash(co->co_varnames);
231 if (h4 == -1) return -1;
232 h5 = PyObject_Hash(co->co_freevars);
233 if (h5 == -1) return -1;
234 h6 = PyObject_Hash(co->co_cellvars);
235 if (h6 == -1) return -1;
236 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
237 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
238 if (h == -1) h = -2;
239 return h;
242 /* XXX code objects need to participate in GC? */
244 PyTypeObject PyCode_Type = {
245 PyObject_HEAD_INIT(&PyType_Type)
247 "code",
248 sizeof(PyCodeObject),
250 (destructor)code_dealloc, /* tp_dealloc */
251 0, /* tp_print */
252 0, /* tp_getattr */
253 0, /* tp_setattr */
254 (cmpfunc)code_compare, /* tp_compare */
255 (reprfunc)code_repr, /* tp_repr */
256 0, /* tp_as_number */
257 0, /* tp_as_sequence */
258 0, /* tp_as_mapping */
259 (hashfunc)code_hash, /* tp_hash */
260 0, /* tp_call */
261 0, /* tp_str */
262 PyObject_GenericGetAttr, /* tp_getattro */
263 0, /* tp_setattro */
264 0, /* tp_as_buffer */
265 Py_TPFLAGS_DEFAULT, /* tp_flags */
266 code_doc, /* tp_doc */
267 0, /* tp_traverse */
268 0, /* tp_clear */
269 0, /* tp_richcompare */
270 0, /* tp_weaklistoffset */
271 0, /* tp_iter */
272 0, /* tp_iternext */
273 0, /* tp_methods */
274 code_memberlist, /* tp_members */
275 0, /* tp_getset */
276 0, /* tp_base */
277 0, /* tp_dict */
278 0, /* tp_descr_get */
279 0, /* tp_descr_set */
280 0, /* tp_dictoffset */
281 0, /* tp_init */
282 0, /* tp_alloc */
283 code_new, /* tp_new */
286 #define NAME_CHARS \
287 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
289 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
291 static int
292 all_name_chars(unsigned char *s)
294 static char ok_name_char[256];
295 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
297 if (ok_name_char[*name_chars] == 0) {
298 unsigned char *p;
299 for (p = name_chars; *p; p++)
300 ok_name_char[*p] = 1;
302 while (*s) {
303 if (ok_name_char[*s++] == 0)
304 return 0;
306 return 1;
309 static int
310 intern_strings(PyObject *tuple)
312 int i;
314 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
315 PyObject *v = PyTuple_GET_ITEM(tuple, i);
316 if (v == NULL || !PyString_Check(v)) {
317 Py_FatalError("non-string found in code slot");
318 PyErr_BadInternalCall();
319 return -1;
321 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
323 return 0;
326 PyCodeObject *
327 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
328 PyObject *code, PyObject *consts, PyObject *names,
329 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
330 PyObject *filename, PyObject *name, int firstlineno,
331 PyObject *lnotab)
333 PyCodeObject *co;
334 int i;
335 /* Check argument types */
336 if (argcount < 0 || nlocals < 0 ||
337 code == NULL ||
338 consts == NULL || !PyTuple_Check(consts) ||
339 names == NULL || !PyTuple_Check(names) ||
340 varnames == NULL || !PyTuple_Check(varnames) ||
341 freevars == NULL || !PyTuple_Check(freevars) ||
342 cellvars == NULL || !PyTuple_Check(cellvars) ||
343 name == NULL || !PyString_Check(name) ||
344 filename == NULL || !PyString_Check(filename) ||
345 lnotab == NULL || !PyString_Check(lnotab) ||
346 !PyObject_CheckReadBuffer(code)) {
347 PyErr_BadInternalCall();
348 return NULL;
350 intern_strings(names);
351 intern_strings(varnames);
352 intern_strings(freevars);
353 intern_strings(cellvars);
354 /* Intern selected string constants */
355 for (i = PyTuple_Size(consts); --i >= 0; ) {
356 PyObject *v = PyTuple_GetItem(consts, i);
357 if (!PyString_Check(v))
358 continue;
359 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
360 continue;
361 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
363 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
364 if (co != NULL) {
365 co->co_argcount = argcount;
366 co->co_nlocals = nlocals;
367 co->co_stacksize = stacksize;
368 co->co_flags = flags;
369 Py_INCREF(code);
370 co->co_code = code;
371 Py_INCREF(consts);
372 co->co_consts = consts;
373 Py_INCREF(names);
374 co->co_names = names;
375 Py_INCREF(varnames);
376 co->co_varnames = varnames;
377 Py_INCREF(freevars);
378 co->co_freevars = freevars;
379 Py_INCREF(cellvars);
380 co->co_cellvars = cellvars;
381 Py_INCREF(filename);
382 co->co_filename = filename;
383 Py_INCREF(name);
384 co->co_name = name;
385 co->co_firstlineno = firstlineno;
386 Py_INCREF(lnotab);
387 co->co_lnotab = lnotab;
389 return co;
393 /* Data structure used internally */
395 /* The compiler uses two passes to generate bytecodes. The first pass
396 builds the symbol table. The second pass generates the bytecode.
398 The first pass uses a single symtable struct. The second pass uses
399 a compiling struct for each code block. The compiling structs
400 share a reference to the symtable.
402 The two passes communicate via symtable_load_symbols() and via
403 is_local() and is_global(). The former initializes several slots
404 in the compiling struct: c_varnames, c_locals, c_nlocals,
405 c_argcount, c_globals, and c_flags.
408 /* All about c_lnotab.
410 c_lnotab is an array of unsigned bytes disguised as a Python string. Since
411 version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are
412 mapped to source code line #s via c_lnotab instead.
414 The array is conceptually a list of
415 (bytecode offset increment, line number increment)
416 pairs. The details are important and delicate, best illustrated by example:
418 byte code offset source code line number
421 50 7
422 350 307
423 361 308
425 The first trick is that these numbers aren't stored, only the increments
426 from one row to the next (this doesn't really work, but it's a start):
428 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
430 The second trick is that an unsigned byte can't hold negative values, or
431 values larger than 255, so (a) there's a deep assumption that byte code
432 offsets and their corresponding line #s both increase monotonically, and (b)
433 if at least one column jumps by more than 255 from one row to the next, more
434 than one pair is written to the table. In case #b, there's no way to know
435 from looking at the table later how many were written. That's the delicate
436 part. A user of c_lnotab desiring to find the source line number
437 corresponding to a bytecode address A should do something like this
439 lineno = addr = 0
440 for addr_incr, line_incr in c_lnotab:
441 addr += addr_incr
442 if addr > A:
443 return lineno
444 lineno += line_incr
446 In order for this to work, when the addr field increments by more than 255,
447 the line # increment in each pair generated must be 0 until the remaining addr
448 increment is < 256. So, in the example above, com_set_lineno should not (as
449 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
450 255, 0, 45, 255, 0, 45.
453 struct compiling {
454 PyObject *c_code; /* string */
455 PyObject *c_consts; /* list of objects */
456 PyObject *c_const_dict; /* inverse of c_consts */
457 PyObject *c_names; /* list of strings (names) */
458 PyObject *c_name_dict; /* inverse of c_names */
459 PyObject *c_globals; /* dictionary (value=None) */
460 PyObject *c_locals; /* dictionary (value=localID) */
461 PyObject *c_varnames; /* list (inverse of c_locals) */
462 PyObject *c_freevars; /* dictionary (value=None) */
463 PyObject *c_cellvars; /* list */
464 int c_nlocals; /* index of next local */
465 int c_argcount; /* number of top-level arguments */
466 int c_flags; /* same as co_flags */
467 int c_nexti; /* index into c_code */
468 int c_errors; /* counts errors occurred */
469 int c_infunction; /* set when compiling a function */
470 int c_interactive; /* generating code for interactive command */
471 int c_loops; /* counts nested loops */
472 int c_begin; /* begin of current loop, for 'continue' */
473 int c_block[CO_MAXBLOCKS]; /* stack of block types */
474 int c_nblocks; /* current block stack level */
475 char *c_filename; /* filename of current node */
476 char *c_name; /* name of object (e.g. function) */
477 int c_lineno; /* Current line number */
478 int c_stacklevel; /* Current stack level */
479 int c_maxstacklevel; /* Maximum stack level */
480 int c_firstlineno;
481 PyObject *c_lnotab; /* Table mapping address to line number */
482 int c_last_addr, c_last_line, c_lnotab_next;
483 char *c_private; /* for private name mangling */
484 int c_tmpname; /* temporary local name counter */
485 int c_nested; /* Is block nested funcdef or lamdef? */
486 int c_closure; /* Is nested w/freevars? */
487 struct symtable *c_symtable; /* pointer to module symbol table */
488 PyFutureFeatures *c_future; /* pointer to module's __future__ */
489 char *c_encoding; /* source encoding (a borrowed reference) */
492 static int
493 is_free(int v)
495 if ((v & (USE | DEF_FREE))
496 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
497 return 1;
498 if (v & DEF_FREE_CLASS)
499 return 1;
500 return 0;
503 static void
504 com_error(struct compiling *c, PyObject *exc, char *msg)
506 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
508 if (c == NULL) {
509 /* Error occurred via symtable call to
510 is_constant_false */
511 PyErr_SetString(exc, msg);
512 return;
514 c->c_errors++;
515 if (c->c_lineno < 1 || c->c_interactive) {
516 /* Unknown line number or interactive input */
517 PyErr_SetString(exc, msg);
518 return;
520 v = PyString_FromString(msg);
521 if (v == NULL)
522 return; /* MemoryError, too bad */
524 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
525 if (line == NULL) {
526 Py_INCREF(Py_None);
527 line = Py_None;
529 if (exc == PyExc_SyntaxError) {
530 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
531 Py_None, line);
532 if (t == NULL)
533 goto exit;
534 w = Py_BuildValue("(OO)", v, t);
535 if (w == NULL)
536 goto exit;
537 PyErr_SetObject(exc, w);
538 } else {
539 /* Make sure additional exceptions are printed with
540 file and line, also. */
541 PyErr_SetObject(exc, v);
542 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
544 exit:
545 Py_XDECREF(t);
546 Py_XDECREF(v);
547 Py_XDECREF(w);
548 Py_XDECREF(line);
551 /* Interface to the block stack */
553 static void
554 block_push(struct compiling *c, int type)
556 if (c->c_nblocks >= CO_MAXBLOCKS) {
557 com_error(c, PyExc_SystemError,
558 "too many statically nested blocks");
560 else {
561 c->c_block[c->c_nblocks++] = type;
565 static void
566 block_pop(struct compiling *c, int type)
568 if (c->c_nblocks > 0)
569 c->c_nblocks--;
570 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
571 com_error(c, PyExc_SystemError, "bad block pop");
575 /* Prototype forward declarations */
577 static int issue_warning(char *, char *, int);
578 static int com_init(struct compiling *, char *);
579 static void com_free(struct compiling *);
580 static void com_push(struct compiling *, int);
581 static void com_pop(struct compiling *, int);
582 static void com_done(struct compiling *);
583 static void com_node(struct compiling *, node *);
584 static void com_factor(struct compiling *, node *);
585 static void com_addbyte(struct compiling *, int);
586 static void com_addint(struct compiling *, int);
587 static void com_addoparg(struct compiling *, int, int);
588 static void com_addfwref(struct compiling *, int, int *);
589 static void com_backpatch(struct compiling *, int);
590 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
591 static int com_addconst(struct compiling *, PyObject *);
592 static int com_addname(struct compiling *, PyObject *);
593 static void com_addopname(struct compiling *, int, node *);
594 static void com_list(struct compiling *, node *, int);
595 static void com_list_iter(struct compiling *, node *, node *, char *);
596 static int com_argdefs(struct compiling *, node *);
597 static void com_assign(struct compiling *, node *, int, node *);
598 static void com_assign_name(struct compiling *, node *, int);
599 static PyCodeObject *icompile(node *, struct compiling *);
600 static PyCodeObject *jcompile(node *, char *, struct compiling *,
601 PyCompilerFlags *);
602 static PyObject *parsestrplus(struct compiling*, node *);
603 static PyObject *parsestr(struct compiling *, char *);
604 static node *get_rawdocstring(node *);
606 static int get_ref_type(struct compiling *, char *);
608 /* symtable operations */
609 static int symtable_build(struct compiling *, node *);
610 static int symtable_load_symbols(struct compiling *);
611 static struct symtable *symtable_init(void);
612 static void symtable_enter_scope(struct symtable *, char *, int, int);
613 static int symtable_exit_scope(struct symtable *);
614 static int symtable_add_def(struct symtable *, char *, int);
615 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
617 static void symtable_node(struct symtable *, node *);
618 static void symtable_funcdef(struct symtable *, node *);
619 static void symtable_default_args(struct symtable *, node *);
620 static void symtable_params(struct symtable *, node *);
621 static void symtable_params_fplist(struct symtable *, node *n);
622 static void symtable_global(struct symtable *, node *);
623 static void symtable_import(struct symtable *, node *);
624 static void symtable_assign(struct symtable *, node *, int);
625 static void symtable_list_comprehension(struct symtable *, node *);
627 static int symtable_update_free_vars(struct symtable *);
628 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
629 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
631 /* helper */
632 static void
633 do_pad(int pad)
635 int i;
636 for (i = 0; i < pad; ++i)
637 fprintf(stderr, " ");
640 static void
641 dump(node *n, int pad, int depth)
643 int i;
644 if (depth == 0)
645 return;
646 do_pad(pad);
647 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
648 if (depth > 0)
649 depth--;
650 for (i = 0; i < NCH(n); ++i)
651 dump(CHILD(n, i), pad + 1, depth);
654 #define DUMP(N) dump(N, 0, -1)
656 static int
657 com_init(struct compiling *c, char *filename)
659 memset((void *)c, '\0', sizeof(struct compiling));
660 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
661 1000)) == NULL)
662 goto fail;
663 if ((c->c_consts = PyList_New(0)) == NULL)
664 goto fail;
665 if ((c->c_const_dict = PyDict_New()) == NULL)
666 goto fail;
667 if ((c->c_names = PyList_New(0)) == NULL)
668 goto fail;
669 if ((c->c_name_dict = PyDict_New()) == NULL)
670 goto fail;
671 if ((c->c_locals = PyDict_New()) == NULL)
672 goto fail;
673 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
674 1000)) == NULL)
675 goto fail;
676 c->c_globals = NULL;
677 c->c_varnames = NULL;
678 c->c_freevars = NULL;
679 c->c_cellvars = NULL;
680 c->c_nlocals = 0;
681 c->c_argcount = 0;
682 c->c_flags = 0;
683 c->c_nexti = 0;
684 c->c_errors = 0;
685 c->c_infunction = 0;
686 c->c_interactive = 0;
687 c->c_loops = 0;
688 c->c_begin = 0;
689 c->c_nblocks = 0;
690 c->c_filename = filename;
691 c->c_name = "?";
692 c->c_lineno = 0;
693 c->c_stacklevel = 0;
694 c->c_maxstacklevel = 0;
695 c->c_firstlineno = 0;
696 c->c_last_addr = 0;
697 c->c_last_line = 0;
698 c->c_lnotab_next = 0;
699 c->c_tmpname = 0;
700 c->c_nested = 0;
701 c->c_closure = 0;
702 c->c_symtable = NULL;
703 return 1;
705 fail:
706 com_free(c);
707 return 0;
710 static void
711 com_free(struct compiling *c)
713 Py_XDECREF(c->c_code);
714 Py_XDECREF(c->c_consts);
715 Py_XDECREF(c->c_const_dict);
716 Py_XDECREF(c->c_names);
717 Py_XDECREF(c->c_name_dict);
718 Py_XDECREF(c->c_globals);
719 Py_XDECREF(c->c_locals);
720 Py_XDECREF(c->c_varnames);
721 Py_XDECREF(c->c_freevars);
722 Py_XDECREF(c->c_cellvars);
723 Py_XDECREF(c->c_lnotab);
724 if (c->c_future)
725 PyObject_FREE((void *)c->c_future);
728 static void
729 com_push(struct compiling *c, int n)
731 c->c_stacklevel += n;
732 if (c->c_stacklevel > c->c_maxstacklevel) {
733 c->c_maxstacklevel = c->c_stacklevel;
735 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
736 c->c_filename, c->c_name, c->c_lineno,
737 c->c_nexti, c->c_stacklevel, n);
742 static void
743 com_pop(struct compiling *c, int n)
745 if (c->c_stacklevel < n)
746 c->c_stacklevel = 0;
747 else
748 c->c_stacklevel -= n;
751 static void
752 com_done(struct compiling *c)
754 if (c->c_code != NULL)
755 _PyString_Resize(&c->c_code, c->c_nexti);
756 if (c->c_lnotab != NULL)
757 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
760 static int
761 com_check_size(PyObject **s, int offset)
763 int len = PyString_GET_SIZE(*s);
764 if (offset >= len)
765 return _PyString_Resize(s, len * 2);
766 return 0;
769 static void
770 com_addbyte(struct compiling *c, int byte)
772 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
773 assert(byte >= 0 && byte <= 255);
774 assert(c->c_code != 0);
775 if (com_check_size(&c->c_code, c->c_nexti)) {
776 c->c_errors++;
777 return;
779 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
782 static void
783 com_addint(struct compiling *c, int x)
785 com_addbyte(c, x & 0xff);
786 com_addbyte(c, x >> 8); /* XXX x should be positive */
789 static void
790 com_add_lnotab(struct compiling *c, int addr, int line)
792 char *p;
793 if (c->c_lnotab == NULL)
794 return;
795 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
796 c->c_errors++;
797 return;
799 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
800 *p++ = addr;
801 *p++ = line;
802 c->c_lnotab_next += 2;
805 static void
806 com_set_lineno(struct compiling *c, int lineno)
808 c->c_lineno = lineno;
809 if (c->c_firstlineno == 0) {
810 c->c_firstlineno = c->c_last_line = lineno;
812 else {
813 int incr_addr = c->c_nexti - c->c_last_addr;
814 int incr_line = lineno - c->c_last_line;
815 while (incr_addr > 255) {
816 com_add_lnotab(c, 255, 0);
817 incr_addr -= 255;
819 while (incr_line > 255) {
820 com_add_lnotab(c, incr_addr, 255);
821 incr_line -=255;
822 incr_addr = 0;
824 if (incr_addr > 0 || incr_line > 0)
825 com_add_lnotab(c, incr_addr, incr_line);
826 c->c_last_addr = c->c_nexti;
827 c->c_last_line = lineno;
831 static void
832 com_addoparg(struct compiling *c, int op, int arg)
834 int extended_arg = arg >> 16;
835 if (extended_arg){
836 com_addbyte(c, EXTENDED_ARG);
837 com_addint(c, extended_arg);
838 arg &= 0xffff;
840 com_addbyte(c, op);
841 com_addint(c, arg);
844 static void
845 com_addfwref(struct compiling *c, int op, int *p_anchor)
847 /* Compile a forward reference for backpatching */
848 int here;
849 int anchor;
850 com_addbyte(c, op);
851 here = c->c_nexti;
852 anchor = *p_anchor;
853 *p_anchor = here;
854 com_addint(c, anchor == 0 ? 0 : here - anchor);
857 static void
858 com_backpatch(struct compiling *c, int anchor)
860 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
861 int target = c->c_nexti;
862 int dist;
863 int prev;
864 for (;;) {
865 /* Make the JUMP instruction at anchor point to target */
866 prev = code[anchor] + (code[anchor+1] << 8);
867 dist = target - (anchor+2);
868 code[anchor] = dist & 0xff;
869 dist >>= 8;
870 code[anchor+1] = dist;
871 dist >>= 8;
872 if (dist) {
873 com_error(c, PyExc_SystemError,
874 "com_backpatch: offset too large");
875 break;
877 if (!prev)
878 break;
879 anchor -= prev;
883 /* Handle literals and names uniformly */
885 static int
886 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
888 PyObject *w, *t, *np=NULL;
889 long n;
891 t = Py_BuildValue("(OO)", v, v->ob_type);
892 if (t == NULL)
893 goto fail;
894 w = PyDict_GetItem(dict, t);
895 if (w != NULL) {
896 n = PyInt_AsLong(w);
897 } else {
898 n = PyList_Size(list);
899 np = PyInt_FromLong(n);
900 if (np == NULL)
901 goto fail;
902 if (PyList_Append(list, v) != 0)
903 goto fail;
904 if (PyDict_SetItem(dict, t, np) != 0)
905 goto fail;
906 Py_DECREF(np);
908 Py_DECREF(t);
909 return n;
910 fail:
911 Py_XDECREF(np);
912 Py_XDECREF(t);
913 c->c_errors++;
914 return 0;
917 static int
918 com_addconst(struct compiling *c, PyObject *v)
920 return com_add(c, c->c_consts, c->c_const_dict, v);
923 static int
924 com_addname(struct compiling *c, PyObject *v)
926 return com_add(c, c->c_names, c->c_name_dict, v);
930 _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
932 /* Name mangling: __private becomes _classname__private.
933 This is independent from how the name is used. */
934 size_t nlen, plen;
935 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
936 return 0;
937 nlen = strlen(name);
938 if (nlen+2 >= maxlen)
939 return 0; /* Don't mangle __extremely_long_names */
940 if (name[nlen-1] == '_' && name[nlen-2] == '_')
941 return 0; /* Don't mangle __whatever__ */
942 /* Strip leading underscores from class name */
943 while (*p == '_')
944 p++;
945 if (*p == '\0')
946 return 0; /* Don't mangle if class is just underscores */
947 plen = strlen(p);
948 if (plen + nlen >= maxlen)
949 plen = maxlen-nlen-2; /* Truncate class name if too long */
950 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
951 buffer[0] = '_';
952 strncpy(buffer+1, p, plen);
953 strcpy(buffer+1+plen, name);
954 return 1;
957 static void
958 com_addop_name(struct compiling *c, int op, char *name)
960 PyObject *v;
961 int i;
962 char buffer[MANGLE_LEN];
964 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
965 name = buffer;
966 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
967 c->c_errors++;
968 i = 255;
970 else {
971 i = com_addname(c, v);
972 Py_DECREF(v);
974 com_addoparg(c, op, i);
977 #define NAME_LOCAL 0
978 #define NAME_GLOBAL 1
979 #define NAME_DEFAULT 2
980 #define NAME_CLOSURE 3
982 static int
983 com_lookup_arg(PyObject *dict, PyObject *name)
985 PyObject *v = PyDict_GetItem(dict, name);
986 if (v == NULL)
987 return -1;
988 else
989 return PyInt_AS_LONG(v);
992 static int
993 none_assignment_check(struct compiling *c, char *name, int assigning)
995 if (name[0] == 'N' && strcmp(name, "None") == 0) {
996 char *msg;
997 if (assigning)
998 msg = "assigment to None";
999 else
1000 msg = "deleting None";
1001 if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) {
1002 c->c_errors++;
1003 return -1;
1006 return 0;
1009 static void
1010 com_addop_varname(struct compiling *c, int kind, char *name)
1012 PyObject *v;
1013 int i, reftype;
1014 int scope = NAME_DEFAULT;
1015 int op = STOP_CODE;
1016 char buffer[MANGLE_LEN];
1018 if (kind != VAR_LOAD &&
1019 none_assignment_check(c, name, kind == VAR_STORE))
1021 c->c_errors++;
1022 i = 255;
1023 goto done;
1025 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
1026 name = buffer;
1027 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
1028 c->c_errors++;
1029 i = 255;
1030 goto done;
1033 reftype = get_ref_type(c, name);
1034 switch (reftype) {
1035 case LOCAL:
1036 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
1037 scope = NAME_LOCAL;
1038 break;
1039 case GLOBAL_EXPLICIT:
1040 scope = NAME_GLOBAL;
1041 break;
1042 case GLOBAL_IMPLICIT:
1043 if (c->c_flags & CO_OPTIMIZED)
1044 scope = NAME_GLOBAL;
1045 break;
1046 case FREE:
1047 case CELL:
1048 scope = NAME_CLOSURE;
1049 break;
1052 i = com_addname(c, v);
1053 if (scope == NAME_LOCAL)
1054 i = com_lookup_arg(c->c_locals, v);
1055 else if (reftype == FREE)
1056 i = com_lookup_arg(c->c_freevars, v);
1057 else if (reftype == CELL)
1058 i = com_lookup_arg(c->c_cellvars, v);
1059 if (i == -1) {
1060 c->c_errors++; /* XXX no exception set */
1061 i = 255;
1062 goto done;
1064 Py_DECREF(v);
1066 switch (kind) {
1067 case VAR_LOAD:
1068 switch (scope) {
1069 case NAME_LOCAL:
1070 op = LOAD_FAST;
1071 break;
1072 case NAME_GLOBAL:
1073 op = LOAD_GLOBAL;
1074 break;
1075 case NAME_DEFAULT:
1076 op = LOAD_NAME;
1077 break;
1078 case NAME_CLOSURE:
1079 op = LOAD_DEREF;
1080 break;
1082 break;
1083 case VAR_STORE:
1084 switch (scope) {
1085 case NAME_LOCAL:
1086 op = STORE_FAST;
1087 break;
1088 case NAME_GLOBAL:
1089 op = STORE_GLOBAL;
1090 break;
1091 case NAME_DEFAULT:
1092 op = STORE_NAME;
1093 break;
1094 case NAME_CLOSURE:
1095 op = STORE_DEREF;
1096 break;
1098 break;
1099 case VAR_DELETE:
1100 switch (scope) {
1101 case NAME_LOCAL:
1102 op = DELETE_FAST;
1103 break;
1104 case NAME_GLOBAL:
1105 op = DELETE_GLOBAL;
1106 break;
1107 case NAME_DEFAULT:
1108 op = DELETE_NAME;
1109 break;
1110 case NAME_CLOSURE: {
1111 char buf[500];
1112 PyOS_snprintf(buf, sizeof(buf),
1113 DEL_CLOSURE_ERROR, name);
1114 com_error(c, PyExc_SyntaxError, buf);
1115 i = 255;
1116 break;
1119 break;
1121 done:
1122 com_addoparg(c, op, i);
1125 static void
1126 com_addopname(struct compiling *c, int op, node *n)
1128 char *name;
1129 char buffer[1000];
1130 /* XXX it is possible to write this code without the 1000
1131 chars on the total length of dotted names, I just can't be
1132 bothered right now */
1133 if (TYPE(n) == STAR)
1134 name = "*";
1135 else if (TYPE(n) == dotted_name) {
1136 char *p = buffer;
1137 int i;
1138 name = buffer;
1139 for (i = 0; i < NCH(n); i += 2) {
1140 char *s = STR(CHILD(n, i));
1141 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
1142 com_error(c, PyExc_MemoryError,
1143 "dotted_name too long");
1144 name = NULL;
1145 break;
1147 if (p != buffer)
1148 *p++ = '.';
1149 strcpy(p, s);
1150 p = strchr(p, '\0');
1153 else {
1154 REQ(n, NAME);
1155 name = STR(n);
1157 com_addop_name(c, op, name);
1160 static PyObject *
1161 parsenumber(struct compiling *c, char *s)
1163 char *end;
1164 long x;
1165 double dx;
1166 #ifndef WITHOUT_COMPLEX
1167 int imflag;
1168 #endif
1170 errno = 0;
1171 end = s + strlen(s) - 1;
1172 #ifndef WITHOUT_COMPLEX
1173 imflag = *end == 'j' || *end == 'J';
1174 #endif
1175 if (*end == 'l' || *end == 'L')
1176 return PyLong_FromString(s, (char **)0, 0);
1177 if (s[0] == '0') {
1178 x = (long) PyOS_strtoul(s, &end, 0);
1179 if (x < 0 && errno == 0) {
1180 if (PyErr_WarnExplicit(
1181 PyExc_FutureWarning,
1182 "hex/oct constants > sys.maxint "
1183 "will return positive values "
1184 "in Python 2.4 and up",
1185 c->c_filename,
1186 c->c_lineno,
1187 NULL,
1188 NULL) < 0)
1189 return NULL;
1190 errno = 0; /* Might be changed by PyErr_Warn() */
1193 else
1194 x = PyOS_strtol(s, &end, 0);
1195 if (*end == '\0') {
1196 if (errno != 0)
1197 return PyLong_FromString(s, (char **)0, 0);
1198 return PyInt_FromLong(x);
1200 /* XXX Huge floats may silently fail */
1201 #ifndef WITHOUT_COMPLEX
1202 if (imflag) {
1203 Py_complex z;
1204 z.real = 0.;
1205 PyFPE_START_PROTECT("atof", return 0)
1206 z.imag = atof(s);
1207 PyFPE_END_PROTECT(z)
1208 return PyComplex_FromCComplex(z);
1210 else
1211 #endif
1213 PyFPE_START_PROTECT("atof", return 0)
1214 dx = atof(s);
1215 PyFPE_END_PROTECT(dx)
1216 return PyFloat_FromDouble(dx);
1220 static PyObject *
1221 decode_utf8(char **sPtr, char *end, char* encoding)
1223 #ifndef Py_USING_UNICODE
1224 Py_FatalError("decode_utf8 should not be called in this build.");
1225 return NULL;
1226 #else
1227 PyObject *u, *v;
1228 char *s, *t;
1229 t = s = *sPtr;
1230 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
1231 while (s < end && (*s & 0x80)) s++;
1232 *sPtr = s;
1233 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
1234 if (u == NULL)
1235 return NULL;
1236 v = PyUnicode_AsEncodedString(u, encoding, NULL);
1237 Py_DECREF(u);
1238 return v;
1239 #endif
1242 static PyObject *
1243 parsestr(struct compiling *c, char *s)
1245 PyObject *v;
1246 size_t len;
1247 char *buf;
1248 char *p;
1249 char *end;
1250 int quote = *s;
1251 int rawmode = 0;
1252 char* encoding = ((c == NULL) ? NULL : c->c_encoding);
1253 int need_encoding;
1254 int unicode = 0;
1256 if (isalpha(quote) || quote == '_') {
1257 if (quote == 'u' || quote == 'U') {
1258 quote = *++s;
1259 unicode = 1;
1261 if (quote == 'r' || quote == 'R') {
1262 quote = *++s;
1263 rawmode = 1;
1266 if (quote != '\'' && quote != '\"') {
1267 PyErr_BadInternalCall();
1268 return NULL;
1270 s++;
1271 len = strlen(s);
1272 if (len > INT_MAX) {
1273 com_error(c, PyExc_OverflowError,
1274 "string to parse is too long");
1275 return NULL;
1277 if (s[--len] != quote) {
1278 PyErr_BadInternalCall();
1279 return NULL;
1281 if (len >= 4 && s[0] == quote && s[1] == quote) {
1282 s += 2;
1283 len -= 2;
1284 if (s[--len] != quote || s[--len] != quote) {
1285 PyErr_BadInternalCall();
1286 return NULL;
1289 #ifdef Py_USING_UNICODE
1290 if (unicode || Py_UnicodeFlag) {
1291 PyObject *u, *w;
1292 if (encoding == NULL) {
1293 buf = s;
1294 u = NULL;
1295 } else if (strcmp(encoding, "iso-8859-1") == 0) {
1296 buf = s;
1297 u = NULL;
1298 } else {
1299 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
1300 u = PyString_FromStringAndSize((char *)NULL, len * 4);
1301 if (u == NULL)
1302 return NULL;
1303 p = buf = PyString_AsString(u);
1304 end = s + len;
1305 while (s < end) {
1306 if (*s == '\\') {
1307 *p++ = *s++;
1308 if (*s & 0x80) {
1309 strcpy(p, "u005c");
1310 p += 5;
1313 if (*s & 0x80) { /* XXX inefficient */
1314 char *r;
1315 int rn, i;
1316 w = decode_utf8(&s, end, "utf-16-be");
1317 if (w == NULL) {
1318 Py_DECREF(u);
1319 return NULL;
1321 r = PyString_AsString(w);
1322 rn = PyString_Size(w);
1323 assert(rn % 2 == 0);
1324 for (i = 0; i < rn; i += 2) {
1325 sprintf(p, "\\u%02x%02x",
1326 r[i + 0] & 0xFF,
1327 r[i + 1] & 0xFF);
1328 p += 6;
1330 Py_DECREF(w);
1331 } else {
1332 *p++ = *s++;
1335 len = p - buf;
1337 if (rawmode)
1338 v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL);
1339 else
1340 v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL);
1341 Py_XDECREF(u);
1342 if (v == NULL)
1343 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
1344 return v;
1347 #endif
1348 need_encoding = (encoding != NULL &&
1349 strcmp(encoding, "utf-8") != 0 &&
1350 strcmp(encoding, "iso-8859-1") != 0);
1351 if (rawmode || strchr(s, '\\') == NULL) {
1352 if (need_encoding) {
1353 #ifndef Py_USING_UNICODE
1354 /* This should not happen - we never see any other
1355 encoding. */
1356 Py_FatalError("cannot deal with encodings in this build.");
1357 #else
1358 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
1359 if (u == NULL)
1360 return NULL;
1361 v = PyUnicode_AsEncodedString(u, encoding, NULL);
1362 Py_DECREF(u);
1363 return v;
1364 #endif
1365 } else {
1366 return PyString_FromStringAndSize(s, len);
1370 v = PyString_DecodeEscape(s, len, NULL, unicode,
1371 need_encoding ? encoding : NULL);
1372 if (v == NULL)
1373 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
1374 return v;
1377 static PyObject *
1378 parsestrplus(struct compiling* c, node *n)
1380 PyObject *v;
1381 int i;
1382 REQ(CHILD(n, 0), STRING);
1383 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
1384 /* String literal concatenation */
1385 for (i = 1; i < NCH(n); i++) {
1386 PyObject *s;
1387 s = parsestr(c, STR(CHILD(n, i)));
1388 if (s == NULL)
1389 goto onError;
1390 if (PyString_Check(v) && PyString_Check(s)) {
1391 PyString_ConcatAndDel(&v, s);
1392 if (v == NULL)
1393 goto onError;
1395 #ifdef Py_USING_UNICODE
1396 else {
1397 PyObject *temp;
1398 temp = PyUnicode_Concat(v, s);
1399 Py_DECREF(s);
1400 if (temp == NULL)
1401 goto onError;
1402 Py_DECREF(v);
1403 v = temp;
1405 #endif
1408 return v;
1410 onError:
1411 Py_XDECREF(v);
1412 return NULL;
1415 static void
1416 com_list_for(struct compiling *c, node *n, node *e, char *t)
1418 int anchor = 0;
1419 int save_begin = c->c_begin;
1421 /* list_iter: for v in expr [list_iter] */
1422 com_node(c, CHILD(n, 3)); /* expr */
1423 com_addbyte(c, GET_ITER);
1424 c->c_begin = c->c_nexti;
1425 com_addfwref(c, FOR_ITER, &anchor);
1426 com_push(c, 1);
1427 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1428 c->c_loops++;
1429 com_list_iter(c, n, e, t);
1430 c->c_loops--;
1431 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1432 c->c_begin = save_begin;
1433 com_backpatch(c, anchor);
1434 com_pop(c, 1); /* FOR_ITER has popped this */
1437 static void
1438 com_list_if(struct compiling *c, node *n, node *e, char *t)
1440 int anchor = 0;
1441 int a = 0;
1442 /* list_iter: 'if' test [list_iter] */
1443 com_node(c, CHILD(n, 1));
1444 com_addfwref(c, JUMP_IF_FALSE, &a);
1445 com_addbyte(c, POP_TOP);
1446 com_pop(c, 1);
1447 com_list_iter(c, n, e, t);
1448 com_addfwref(c, JUMP_FORWARD, &anchor);
1449 com_backpatch(c, a);
1450 /* We jump here with an extra entry which we now pop */
1451 com_addbyte(c, POP_TOP);
1452 com_backpatch(c, anchor);
1455 static void
1456 com_list_iter(struct compiling *c,
1457 node *p, /* parent of list_iter node */
1458 node *e, /* element expression node */
1459 char *t /* name of result list temp local */)
1461 /* list_iter is the last child in a listmaker, list_for, or list_if */
1462 node *n = CHILD(p, NCH(p)-1);
1463 if (TYPE(n) == list_iter) {
1464 n = CHILD(n, 0);
1465 switch (TYPE(n)) {
1466 case list_for:
1467 com_list_for(c, n, e, t);
1468 break;
1469 case list_if:
1470 com_list_if(c, n, e, t);
1471 break;
1472 default:
1473 com_error(c, PyExc_SystemError,
1474 "invalid list_iter node type");
1477 else {
1478 com_addop_varname(c, VAR_LOAD, t);
1479 com_push(c, 1);
1480 com_node(c, e);
1481 com_addoparg(c, CALL_FUNCTION, 1);
1482 com_addbyte(c, POP_TOP);
1483 com_pop(c, 2);
1487 static void
1488 com_list_comprehension(struct compiling *c, node *n)
1490 /* listmaker: test list_for */
1491 char tmpname[30];
1492 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
1493 com_addoparg(c, BUILD_LIST, 0);
1494 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1495 com_push(c, 2);
1496 com_addop_name(c, LOAD_ATTR, "append");
1497 com_addop_varname(c, VAR_STORE, tmpname);
1498 com_pop(c, 1);
1499 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1500 com_addop_varname(c, VAR_DELETE, tmpname);
1501 --c->c_tmpname;
1504 static void
1505 com_listmaker(struct compiling *c, node *n)
1507 /* listmaker: test ( list_for | (',' test)* [','] ) */
1508 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1509 com_list_comprehension(c, n);
1510 else {
1511 int len = 0;
1512 int i;
1513 for (i = 0; i < NCH(n); i += 2, len++)
1514 com_node(c, CHILD(n, i));
1515 com_addoparg(c, BUILD_LIST, len);
1516 com_pop(c, len-1);
1520 static void
1521 com_dictmaker(struct compiling *c, node *n)
1523 int i;
1524 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1525 for (i = 0; i+2 < NCH(n); i += 4) {
1526 /* We must arrange things just right for STORE_SUBSCR.
1527 It wants the stack to look like (value) (dict) (key) */
1528 com_addbyte(c, DUP_TOP);
1529 com_push(c, 1);
1530 com_node(c, CHILD(n, i+2)); /* value */
1531 com_addbyte(c, ROT_TWO);
1532 com_node(c, CHILD(n, i)); /* key */
1533 com_addbyte(c, STORE_SUBSCR);
1534 com_pop(c, 3);
1538 static void
1539 com_atom(struct compiling *c, node *n)
1541 node *ch;
1542 PyObject *v;
1543 int i;
1544 REQ(n, atom);
1545 ch = CHILD(n, 0);
1546 switch (TYPE(ch)) {
1547 case LPAR:
1548 if (TYPE(CHILD(n, 1)) == RPAR) {
1549 com_addoparg(c, BUILD_TUPLE, 0);
1550 com_push(c, 1);
1552 else
1553 com_node(c, CHILD(n, 1));
1554 break;
1555 case LSQB: /* '[' [listmaker] ']' */
1556 if (TYPE(CHILD(n, 1)) == RSQB) {
1557 com_addoparg(c, BUILD_LIST, 0);
1558 com_push(c, 1);
1560 else
1561 com_listmaker(c, CHILD(n, 1));
1562 break;
1563 case LBRACE: /* '{' [dictmaker] '}' */
1564 com_addoparg(c, BUILD_MAP, 0);
1565 com_push(c, 1);
1566 if (TYPE(CHILD(n, 1)) == dictmaker)
1567 com_dictmaker(c, CHILD(n, 1));
1568 break;
1569 case BACKQUOTE:
1570 com_node(c, CHILD(n, 1));
1571 com_addbyte(c, UNARY_CONVERT);
1572 break;
1573 case NUMBER:
1574 if ((v = parsenumber(c, STR(ch))) == NULL) {
1575 i = 255;
1577 else {
1578 i = com_addconst(c, v);
1579 Py_DECREF(v);
1581 com_addoparg(c, LOAD_CONST, i);
1582 com_push(c, 1);
1583 break;
1584 case STRING:
1585 v = parsestrplus(c, n);
1586 if (v == NULL) {
1587 c->c_errors++;
1588 i = 255;
1590 else {
1591 i = com_addconst(c, v);
1592 Py_DECREF(v);
1594 com_addoparg(c, LOAD_CONST, i);
1595 com_push(c, 1);
1596 break;
1597 case NAME:
1598 com_addop_varname(c, VAR_LOAD, STR(ch));
1599 com_push(c, 1);
1600 break;
1601 default:
1602 com_error(c, PyExc_SystemError,
1603 "com_atom: unexpected node type");
1607 static void
1608 com_slice(struct compiling *c, node *n, int op)
1610 if (NCH(n) == 1) {
1611 com_addbyte(c, op);
1613 else if (NCH(n) == 2) {
1614 if (TYPE(CHILD(n, 0)) != COLON) {
1615 com_node(c, CHILD(n, 0));
1616 com_addbyte(c, op+1);
1618 else {
1619 com_node(c, CHILD(n, 1));
1620 com_addbyte(c, op+2);
1622 com_pop(c, 1);
1624 else {
1625 com_node(c, CHILD(n, 0));
1626 com_node(c, CHILD(n, 2));
1627 com_addbyte(c, op+3);
1628 com_pop(c, 2);
1632 static void
1633 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1635 if (NCH(n) == 1) {
1636 com_addbyte(c, DUP_TOP);
1637 com_push(c, 1);
1638 com_addbyte(c, SLICE);
1639 com_node(c, augn);
1640 com_addbyte(c, opcode);
1641 com_pop(c, 1);
1642 com_addbyte(c, ROT_TWO);
1643 com_addbyte(c, STORE_SLICE);
1644 com_pop(c, 2);
1645 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1646 com_node(c, CHILD(n, 0));
1647 com_addoparg(c, DUP_TOPX, 2);
1648 com_push(c, 2);
1649 com_addbyte(c, SLICE+1);
1650 com_pop(c, 1);
1651 com_node(c, augn);
1652 com_addbyte(c, opcode);
1653 com_pop(c, 1);
1654 com_addbyte(c, ROT_THREE);
1655 com_addbyte(c, STORE_SLICE+1);
1656 com_pop(c, 3);
1657 } else if (NCH(n) == 2) {
1658 com_node(c, CHILD(n, 1));
1659 com_addoparg(c, DUP_TOPX, 2);
1660 com_push(c, 2);
1661 com_addbyte(c, SLICE+2);
1662 com_pop(c, 1);
1663 com_node(c, augn);
1664 com_addbyte(c, opcode);
1665 com_pop(c, 1);
1666 com_addbyte(c, ROT_THREE);
1667 com_addbyte(c, STORE_SLICE+2);
1668 com_pop(c, 3);
1669 } else {
1670 com_node(c, CHILD(n, 0));
1671 com_node(c, CHILD(n, 2));
1672 com_addoparg(c, DUP_TOPX, 3);
1673 com_push(c, 3);
1674 com_addbyte(c, SLICE+3);
1675 com_pop(c, 2);
1676 com_node(c, augn);
1677 com_addbyte(c, opcode);
1678 com_pop(c, 1);
1679 com_addbyte(c, ROT_FOUR);
1680 com_addbyte(c, STORE_SLICE+3);
1681 com_pop(c, 4);
1685 static void
1686 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1688 node *m;
1689 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1690 if (NCH(n) == 1) {
1691 if (*pkeywords != NULL) {
1692 com_error(c, PyExc_SyntaxError,
1693 "non-keyword arg after keyword arg");
1695 else {
1696 com_node(c, CHILD(n, 0));
1698 return;
1700 m = n;
1701 do {
1702 m = CHILD(m, 0);
1703 } while (NCH(m) == 1);
1704 if (TYPE(m) != NAME) {
1705 /* f(lambda x: x[0] = 3) ends up getting parsed with
1706 * LHS test = lambda x: x[0], and RHS test = 3.
1707 * SF bug 132313 points out that complaining about a keyword
1708 * then is very confusing.
1710 com_error(c, PyExc_SyntaxError,
1711 TYPE(m) == lambdef ?
1712 "lambda cannot contain assignment" :
1713 "keyword can't be an expression");
1715 else {
1716 PyObject *v = PyString_InternFromString(STR(m));
1717 (void) none_assignment_check(c, STR(m), 1);
1718 if (v != NULL && *pkeywords == NULL)
1719 *pkeywords = PyDict_New();
1720 if (v == NULL)
1721 c->c_errors++;
1722 else if (*pkeywords == NULL) {
1723 c->c_errors++;
1724 Py_DECREF(v);
1725 } else {
1726 if (PyDict_GetItem(*pkeywords, v) != NULL)
1727 com_error(c, PyExc_SyntaxError,
1728 "duplicate keyword argument");
1729 else
1730 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1731 c->c_errors++;
1732 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1733 com_push(c, 1);
1734 Py_DECREF(v);
1737 com_node(c, CHILD(n, 2));
1740 static void
1741 com_call_function(struct compiling *c, node *n)
1743 if (TYPE(n) == RPAR) {
1744 com_addoparg(c, CALL_FUNCTION, 0);
1746 else {
1747 PyObject *keywords = NULL;
1748 int i, na, nk;
1749 int lineno = n->n_lineno;
1750 int star_flag = 0;
1751 int starstar_flag = 0;
1752 int opcode;
1753 REQ(n, arglist);
1754 na = 0;
1755 nk = 0;
1756 for (i = 0; i < NCH(n); i += 2) {
1757 node *ch = CHILD(n, i);
1758 if (TYPE(ch) == STAR ||
1759 TYPE(ch) == DOUBLESTAR)
1760 break;
1761 if (ch->n_lineno != lineno) {
1762 lineno = ch->n_lineno;
1763 com_set_lineno(c, lineno);
1765 com_argument(c, ch, &keywords);
1766 if (keywords == NULL)
1767 na++;
1768 else
1769 nk++;
1771 Py_XDECREF(keywords);
1772 while (i < NCH(n)) {
1773 node *tok = CHILD(n, i);
1774 node *ch = CHILD(n, i+1);
1775 i += 3;
1776 switch (TYPE(tok)) {
1777 case STAR: star_flag = 1; break;
1778 case DOUBLESTAR: starstar_flag = 1; break;
1780 com_node(c, ch);
1782 if (na > 255 || nk > 255) {
1783 com_error(c, PyExc_SyntaxError,
1784 "more than 255 arguments");
1786 if (star_flag || starstar_flag)
1787 opcode = CALL_FUNCTION_VAR - 1 +
1788 star_flag + (starstar_flag << 1);
1789 else
1790 opcode = CALL_FUNCTION;
1791 com_addoparg(c, opcode, na | (nk << 8));
1792 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1796 static void
1797 com_select_member(struct compiling *c, node *n)
1799 com_addopname(c, LOAD_ATTR, n);
1802 static void
1803 com_sliceobj(struct compiling *c, node *n)
1805 int i=0;
1806 int ns=2; /* number of slice arguments */
1807 node *ch;
1809 /* first argument */
1810 if (TYPE(CHILD(n,i)) == COLON) {
1811 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1812 com_push(c, 1);
1813 i++;
1815 else {
1816 com_node(c, CHILD(n,i));
1817 i++;
1818 REQ(CHILD(n,i),COLON);
1819 i++;
1821 /* second argument */
1822 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1823 com_node(c, CHILD(n,i));
1824 i++;
1826 else {
1827 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1828 com_push(c, 1);
1830 /* remaining arguments */
1831 for (; i < NCH(n); i++) {
1832 ns++;
1833 ch=CHILD(n,i);
1834 REQ(ch, sliceop);
1835 if (NCH(ch) == 1) {
1836 /* right argument of ':' missing */
1837 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1838 com_push(c, 1);
1840 else
1841 com_node(c, CHILD(ch,1));
1843 com_addoparg(c, BUILD_SLICE, ns);
1844 com_pop(c, 1 + (ns == 3));
1847 static void
1848 com_subscript(struct compiling *c, node *n)
1850 node *ch;
1851 REQ(n, subscript);
1852 ch = CHILD(n,0);
1853 /* check for rubber index */
1854 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1855 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1856 com_push(c, 1);
1858 else {
1859 /* check for slice */
1860 if ((TYPE(ch) == COLON || NCH(n) > 1))
1861 com_sliceobj(c, n);
1862 else {
1863 REQ(ch, test);
1864 com_node(c, ch);
1869 static void
1870 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1872 int i, op;
1873 REQ(n, subscriptlist);
1874 /* Check to make backward compatible slice behavior for '[i:j]' */
1875 if (NCH(n) == 1) {
1876 node *sub = CHILD(n, 0); /* subscript */
1877 /* 'Basic' slice, should have exactly one colon. */
1878 if ((TYPE(CHILD(sub, 0)) == COLON
1879 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1880 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1882 switch (assigning) {
1883 case OP_DELETE:
1884 op = DELETE_SLICE;
1885 break;
1886 case OP_ASSIGN:
1887 op = STORE_SLICE;
1888 break;
1889 case OP_APPLY:
1890 op = SLICE;
1891 break;
1892 default:
1893 com_augassign_slice(c, sub, assigning, augn);
1894 return;
1896 com_slice(c, sub, op);
1897 if (op == STORE_SLICE)
1898 com_pop(c, 2);
1899 else if (op == DELETE_SLICE)
1900 com_pop(c, 1);
1901 return;
1904 /* Else normal subscriptlist. Compile each subscript. */
1905 for (i = 0; i < NCH(n); i += 2)
1906 com_subscript(c, CHILD(n, i));
1907 /* Put multiple subscripts into a tuple */
1908 if (NCH(n) > 1) {
1909 i = (NCH(n)+1) / 2;
1910 com_addoparg(c, BUILD_TUPLE, i);
1911 com_pop(c, i-1);
1913 switch (assigning) {
1914 case OP_DELETE:
1915 op = DELETE_SUBSCR;
1916 i = 2;
1917 break;
1918 default:
1919 case OP_ASSIGN:
1920 op = STORE_SUBSCR;
1921 i = 3;
1922 break;
1923 case OP_APPLY:
1924 op = BINARY_SUBSCR;
1925 i = 1;
1926 break;
1928 if (assigning > OP_APPLY) {
1929 com_addoparg(c, DUP_TOPX, 2);
1930 com_push(c, 2);
1931 com_addbyte(c, BINARY_SUBSCR);
1932 com_pop(c, 1);
1933 com_node(c, augn);
1934 com_addbyte(c, assigning);
1935 com_pop(c, 1);
1936 com_addbyte(c, ROT_THREE);
1938 com_addbyte(c, op);
1939 com_pop(c, i);
1942 static void
1943 com_apply_trailer(struct compiling *c, node *n)
1945 REQ(n, trailer);
1946 switch (TYPE(CHILD(n, 0))) {
1947 case LPAR:
1948 com_call_function(c, CHILD(n, 1));
1949 break;
1950 case DOT:
1951 com_select_member(c, CHILD(n, 1));
1952 break;
1953 case LSQB:
1954 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1955 break;
1956 default:
1957 com_error(c, PyExc_SystemError,
1958 "com_apply_trailer: unknown trailer type");
1962 static void
1963 com_power(struct compiling *c, node *n)
1965 int i;
1966 REQ(n, power);
1967 com_atom(c, CHILD(n, 0));
1968 for (i = 1; i < NCH(n); i++) {
1969 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1970 com_factor(c, CHILD(n, i+1));
1971 com_addbyte(c, BINARY_POWER);
1972 com_pop(c, 1);
1973 break;
1975 else
1976 com_apply_trailer(c, CHILD(n, i));
1980 static void
1981 com_invert_constant(struct compiling *c, node *n)
1983 /* Compute the inverse of int and longs and use them directly,
1984 but be prepared to generate code for all other
1985 possibilities (invalid numbers, floats, complex).
1987 PyObject *num, *inv = NULL;
1988 int i;
1990 REQ(n, NUMBER);
1991 num = parsenumber(c, STR(n));
1992 if (num == NULL)
1993 i = 255;
1994 else {
1995 inv = PyNumber_Invert(num);
1996 if (inv == NULL) {
1997 PyErr_Clear();
1998 i = com_addconst(c, num);
1999 } else {
2000 i = com_addconst(c, inv);
2001 Py_DECREF(inv);
2003 Py_DECREF(num);
2005 com_addoparg(c, LOAD_CONST, i);
2006 com_push(c, 1);
2007 if (num != NULL && inv == NULL)
2008 com_addbyte(c, UNARY_INVERT);
2011 static int
2012 is_float_zero(const char *p)
2014 int found_radix_point = 0;
2015 int ch;
2016 while ((ch = Py_CHARMASK(*p++)) != '\0') {
2017 switch (ch) {
2018 case '0':
2019 /* no reason to believe it's not 0 -- continue */
2020 break;
2022 case 'e': case 'E': case 'j': case 'J':
2023 /* If this was a hex constant, we already would have
2024 returned 0 due to the 'x' or 'X', so 'e' or 'E'
2025 must be an exponent marker, and we haven't yet
2026 seen a non-zero digit, and it doesn't matter what
2027 the exponent is then. For 'j' or 'J' similarly,
2028 except that this is an imaginary 0 then. */
2029 return 1;
2031 case '.':
2032 found_radix_point = 1;
2033 break;
2035 default:
2036 return 0;
2039 return found_radix_point;
2042 static void
2043 com_factor(struct compiling *c, node *n)
2045 int childtype = TYPE(CHILD(n, 0));
2046 node *pfactor, *ppower, *patom, *pnum;
2047 REQ(n, factor);
2048 /* If the unary +, -, or ~ operator is applied to a constant,
2049 don't generate a UNARY_xxx opcode. Just store the
2050 approriate value as a constant. If the value is negative,
2051 extend the string containing the constant and insert a
2052 negative in the 0th position -- unless we're doing unary minus
2053 of a floating zero! In that case the sign is significant, but
2054 the const dict can't distinguish +0.0 from -0.0.
2056 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
2057 && NCH(n) == 2
2058 && TYPE((pfactor = CHILD(n, 1))) == factor
2059 && NCH(pfactor) == 1
2060 && TYPE((ppower = CHILD(pfactor, 0))) == power
2061 && NCH(ppower) == 1
2062 && TYPE((patom = CHILD(ppower, 0))) == atom
2063 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
2064 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
2065 if (childtype == TILDE) {
2066 com_invert_constant(c, pnum);
2067 return;
2069 if (childtype == MINUS) {
2070 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
2071 if (s == NULL) {
2072 com_error(c, PyExc_MemoryError, "");
2073 com_addbyte(c, 255);
2074 return;
2076 s[0] = '-';
2077 strcpy(s + 1, STR(pnum));
2078 PyObject_FREE(STR(pnum));
2079 STR(pnum) = s;
2081 com_atom(c, patom);
2083 else if (childtype == PLUS) {
2084 com_factor(c, CHILD(n, 1));
2085 com_addbyte(c, UNARY_POSITIVE);
2087 else if (childtype == MINUS) {
2088 com_factor(c, CHILD(n, 1));
2089 com_addbyte(c, UNARY_NEGATIVE);
2091 else if (childtype == TILDE) {
2092 com_factor(c, CHILD(n, 1));
2093 com_addbyte(c, UNARY_INVERT);
2095 else {
2096 com_power(c, CHILD(n, 0));
2100 static void
2101 com_term(struct compiling *c, node *n)
2103 int i;
2104 int op;
2105 REQ(n, term);
2106 com_factor(c, CHILD(n, 0));
2107 for (i = 2; i < NCH(n); i += 2) {
2108 com_factor(c, CHILD(n, i));
2109 switch (TYPE(CHILD(n, i-1))) {
2110 case STAR:
2111 op = BINARY_MULTIPLY;
2112 break;
2113 case SLASH:
2114 if (c->c_flags & CO_FUTURE_DIVISION)
2115 op = BINARY_TRUE_DIVIDE;
2116 else
2117 op = BINARY_DIVIDE;
2118 break;
2119 case PERCENT:
2120 op = BINARY_MODULO;
2121 break;
2122 case DOUBLESLASH:
2123 op = BINARY_FLOOR_DIVIDE;
2124 break;
2125 default:
2126 com_error(c, PyExc_SystemError,
2127 "com_term: operator not *, /, // or %");
2128 op = 255;
2130 com_addbyte(c, op);
2131 com_pop(c, 1);
2135 static void
2136 com_arith_expr(struct compiling *c, node *n)
2138 int i;
2139 int op;
2140 REQ(n, arith_expr);
2141 com_term(c, CHILD(n, 0));
2142 for (i = 2; i < NCH(n); i += 2) {
2143 com_term(c, CHILD(n, i));
2144 switch (TYPE(CHILD(n, i-1))) {
2145 case PLUS:
2146 op = BINARY_ADD;
2147 break;
2148 case MINUS:
2149 op = BINARY_SUBTRACT;
2150 break;
2151 default:
2152 com_error(c, PyExc_SystemError,
2153 "com_arith_expr: operator not + or -");
2154 op = 255;
2156 com_addbyte(c, op);
2157 com_pop(c, 1);
2161 static void
2162 com_shift_expr(struct compiling *c, node *n)
2164 int i;
2165 int op;
2166 REQ(n, shift_expr);
2167 com_arith_expr(c, CHILD(n, 0));
2168 for (i = 2; i < NCH(n); i += 2) {
2169 com_arith_expr(c, CHILD(n, i));
2170 switch (TYPE(CHILD(n, i-1))) {
2171 case LEFTSHIFT:
2172 op = BINARY_LSHIFT;
2173 break;
2174 case RIGHTSHIFT:
2175 op = BINARY_RSHIFT;
2176 break;
2177 default:
2178 com_error(c, PyExc_SystemError,
2179 "com_shift_expr: operator not << or >>");
2180 op = 255;
2182 com_addbyte(c, op);
2183 com_pop(c, 1);
2187 static void
2188 com_and_expr(struct compiling *c, node *n)
2190 int i;
2191 int op;
2192 REQ(n, and_expr);
2193 com_shift_expr(c, CHILD(n, 0));
2194 for (i = 2; i < NCH(n); i += 2) {
2195 com_shift_expr(c, CHILD(n, i));
2196 if (TYPE(CHILD(n, i-1)) == AMPER) {
2197 op = BINARY_AND;
2199 else {
2200 com_error(c, PyExc_SystemError,
2201 "com_and_expr: operator not &");
2202 op = 255;
2204 com_addbyte(c, op);
2205 com_pop(c, 1);
2209 static void
2210 com_xor_expr(struct compiling *c, node *n)
2212 int i;
2213 int op;
2214 REQ(n, xor_expr);
2215 com_and_expr(c, CHILD(n, 0));
2216 for (i = 2; i < NCH(n); i += 2) {
2217 com_and_expr(c, CHILD(n, i));
2218 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2219 op = BINARY_XOR;
2221 else {
2222 com_error(c, PyExc_SystemError,
2223 "com_xor_expr: operator not ^");
2224 op = 255;
2226 com_addbyte(c, op);
2227 com_pop(c, 1);
2231 static void
2232 com_expr(struct compiling *c, node *n)
2234 int i;
2235 int op;
2236 REQ(n, expr);
2237 com_xor_expr(c, CHILD(n, 0));
2238 for (i = 2; i < NCH(n); i += 2) {
2239 com_xor_expr(c, CHILD(n, i));
2240 if (TYPE(CHILD(n, i-1)) == VBAR) {
2241 op = BINARY_OR;
2243 else {
2244 com_error(c, PyExc_SystemError,
2245 "com_expr: expr operator not |");
2246 op = 255;
2248 com_addbyte(c, op);
2249 com_pop(c, 1);
2253 static enum cmp_op
2254 cmp_type(node *n)
2256 REQ(n, comp_op);
2257 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2258 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2259 if (NCH(n) == 1) {
2260 n = CHILD(n, 0);
2261 switch (TYPE(n)) {
2262 case LESS: return PyCmp_LT;
2263 case GREATER: return PyCmp_GT;
2264 case EQEQUAL: /* == */
2265 case EQUAL: return PyCmp_EQ;
2266 case LESSEQUAL: return PyCmp_LE;
2267 case GREATEREQUAL: return PyCmp_GE;
2268 case NOTEQUAL: return PyCmp_NE; /* <> or != */
2269 case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
2270 if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
2273 else if (NCH(n) == 2) {
2274 switch (TYPE(CHILD(n, 0))) {
2275 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2276 return PyCmp_NOT_IN;
2277 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2278 return PyCmp_IS_NOT;
2281 return PyCmp_BAD;
2284 static void
2285 com_comparison(struct compiling *c, node *n)
2287 int i;
2288 enum cmp_op op;
2289 int anchor;
2290 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2291 com_expr(c, CHILD(n, 0));
2292 if (NCH(n) == 1)
2293 return;
2295 /****************************************************************
2296 The following code is generated for all but the last
2297 comparison in a chain:
2299 label: on stack: opcode: jump to:
2301 a <code to load b>
2302 a, b DUP_TOP
2303 a, b, b ROT_THREE
2304 b, a, b COMPARE_OP
2305 b, 0-or-1 JUMP_IF_FALSE L1
2306 b, 1 POP_TOP
2309 We are now ready to repeat this sequence for the next
2310 comparison in the chain.
2312 For the last we generate:
2314 b <code to load c>
2315 b, c COMPARE_OP
2316 0-or-1
2318 If there were any jumps to L1 (i.e., there was more than one
2319 comparison), we generate:
2321 0-or-1 JUMP_FORWARD L2
2322 L1: b, 0 ROT_TWO
2323 0, b POP_TOP
2325 L2: 0-or-1
2326 ****************************************************************/
2328 anchor = 0;
2330 for (i = 2; i < NCH(n); i += 2) {
2331 com_expr(c, CHILD(n, i));
2332 if (i+2 < NCH(n)) {
2333 com_addbyte(c, DUP_TOP);
2334 com_push(c, 1);
2335 com_addbyte(c, ROT_THREE);
2337 op = cmp_type(CHILD(n, i-1));
2338 if (op == PyCmp_BAD) {
2339 com_error(c, PyExc_SystemError,
2340 "com_comparison: unknown comparison op");
2342 com_addoparg(c, COMPARE_OP, op);
2343 com_pop(c, 1);
2344 if (i+2 < NCH(n)) {
2345 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2346 com_addbyte(c, POP_TOP);
2347 com_pop(c, 1);
2351 if (anchor) {
2352 int anchor2 = 0;
2353 com_addfwref(c, JUMP_FORWARD, &anchor2);
2354 com_backpatch(c, anchor);
2355 com_addbyte(c, ROT_TWO);
2356 com_addbyte(c, POP_TOP);
2357 com_backpatch(c, anchor2);
2361 static void
2362 com_not_test(struct compiling *c, node *n)
2364 REQ(n, not_test); /* 'not' not_test | comparison */
2365 if (NCH(n) == 1) {
2366 com_comparison(c, CHILD(n, 0));
2368 else {
2369 com_not_test(c, CHILD(n, 1));
2370 com_addbyte(c, UNARY_NOT);
2374 static void
2375 com_and_test(struct compiling *c, node *n)
2377 int i;
2378 int anchor;
2379 REQ(n, and_test); /* not_test ('and' not_test)* */
2380 anchor = 0;
2381 i = 0;
2382 for (;;) {
2383 com_not_test(c, CHILD(n, i));
2384 if ((i += 2) >= NCH(n))
2385 break;
2386 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2387 com_addbyte(c, POP_TOP);
2388 com_pop(c, 1);
2390 if (anchor)
2391 com_backpatch(c, anchor);
2394 static int
2395 com_make_closure(struct compiling *c, PyCodeObject *co)
2397 int i, free = PyCode_GetNumFree(co);
2398 if (free == 0)
2399 return 0;
2400 for (i = 0; i < free; ++i) {
2401 /* Bypass com_addop_varname because it will generate
2402 LOAD_DEREF but LOAD_CLOSURE is needed.
2404 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2405 int arg, reftype;
2407 /* Special case: If a class contains a method with a
2408 free variable that has the same name as a method,
2409 the name will be considered free *and* local in the
2410 class. It should be handled by the closure, as
2411 well as by the normal name loookup logic.
2413 reftype = get_ref_type(c, PyString_AS_STRING(name));
2414 if (reftype == CELL)
2415 arg = com_lookup_arg(c->c_cellvars, name);
2416 else /* (reftype == FREE) */
2417 arg = com_lookup_arg(c->c_freevars, name);
2418 if (arg == -1) {
2419 fprintf(stderr, "lookup %s in %s %d %d\n"
2420 "freevars of %s: %s\n",
2421 PyObject_REPR(name),
2422 c->c_name,
2423 reftype, arg,
2424 PyString_AS_STRING(co->co_name),
2425 PyObject_REPR(co->co_freevars));
2426 Py_FatalError("com_make_closure()");
2428 com_addoparg(c, LOAD_CLOSURE, arg);
2431 com_push(c, free);
2432 return 1;
2435 static void
2436 com_test(struct compiling *c, node *n)
2438 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2439 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2440 PyCodeObject *co;
2441 int i, closure;
2442 int ndefs = com_argdefs(c, CHILD(n, 0));
2443 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2444 n->n_lineno);
2445 co = icompile(CHILD(n, 0), c);
2446 if (co == NULL) {
2447 c->c_errors++;
2448 return;
2450 symtable_exit_scope(c->c_symtable);
2451 i = com_addconst(c, (PyObject *)co);
2452 closure = com_make_closure(c, co);
2453 com_addoparg(c, LOAD_CONST, i);
2454 com_push(c, 1);
2455 if (closure) {
2456 com_addoparg(c, MAKE_CLOSURE, ndefs);
2457 com_pop(c, PyCode_GetNumFree(co));
2458 } else
2459 com_addoparg(c, MAKE_FUNCTION, ndefs);
2460 Py_DECREF(co);
2461 com_pop(c, ndefs);
2463 else {
2464 int anchor = 0;
2465 int i = 0;
2466 for (;;) {
2467 com_and_test(c, CHILD(n, i));
2468 if ((i += 2) >= NCH(n))
2469 break;
2470 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2471 com_addbyte(c, POP_TOP);
2472 com_pop(c, 1);
2474 if (anchor)
2475 com_backpatch(c, anchor);
2479 static void
2480 com_list(struct compiling *c, node *n, int toplevel)
2482 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2483 if (NCH(n) == 1 && !toplevel) {
2484 com_node(c, CHILD(n, 0));
2486 else {
2487 int i;
2488 int len;
2489 len = (NCH(n) + 1) / 2;
2490 for (i = 0; i < NCH(n); i += 2)
2491 com_node(c, CHILD(n, i));
2492 com_addoparg(c, BUILD_TUPLE, len);
2493 com_pop(c, len-1);
2498 /* Begin of assignment compilation */
2501 static void
2502 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2504 com_addbyte(c, DUP_TOP);
2505 com_push(c, 1);
2506 com_addopname(c, LOAD_ATTR, n);
2507 com_node(c, augn);
2508 com_addbyte(c, opcode);
2509 com_pop(c, 1);
2510 com_addbyte(c, ROT_TWO);
2511 com_addopname(c, STORE_ATTR, n);
2512 com_pop(c, 2);
2515 static void
2516 com_assign_attr(struct compiling *c, node *n, int assigning)
2518 if (none_assignment_check(c, STR(n), assigning))
2519 return;
2520 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2521 com_pop(c, assigning ? 2 : 1);
2524 static void
2525 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2527 REQ(n, trailer);
2528 switch (TYPE(CHILD(n, 0))) {
2529 case LPAR: /* '(' [exprlist] ')' */
2530 if (assigning == OP_DELETE)
2531 com_error(c, PyExc_SyntaxError,
2532 "can't delete function call");
2533 else
2534 com_error(c, PyExc_SyntaxError,
2535 "can't assign to function call");
2536 break;
2537 case DOT: /* '.' NAME */
2538 if (assigning > OP_APPLY)
2539 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2540 else
2541 com_assign_attr(c, CHILD(n, 1), assigning);
2542 break;
2543 case LSQB: /* '[' subscriptlist ']' */
2544 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2545 break;
2546 default:
2547 com_error(c, PyExc_SystemError, "unknown trailer type");
2551 static void
2552 com_assign_sequence(struct compiling *c, node *n, int assigning)
2554 int i;
2555 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2556 REQ(n, exprlist);
2557 if (assigning) {
2558 i = (NCH(n)+1)/2;
2559 com_addoparg(c, UNPACK_SEQUENCE, i);
2560 com_push(c, i-1);
2562 for (i = 0; i < NCH(n); i += 2)
2563 com_assign(c, CHILD(n, i), assigning, NULL);
2566 static void
2567 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2569 REQ(n, NAME);
2570 com_addop_varname(c, VAR_LOAD, STR(n));
2571 com_push(c, 1);
2572 com_node(c, augn);
2573 com_addbyte(c, opcode);
2574 com_pop(c, 1);
2575 com_assign_name(c, n, OP_ASSIGN);
2578 static void
2579 com_assign_name(struct compiling *c, node *n, int assigning)
2581 REQ(n, NAME);
2582 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2583 if (assigning)
2584 com_pop(c, 1);
2587 static void
2588 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2590 /* Loop to avoid trivial recursion */
2591 for (;;) {
2592 switch (TYPE(n)) {
2594 case exprlist:
2595 case testlist:
2596 case testlist1:
2597 if (NCH(n) > 1) {
2598 if (assigning > OP_APPLY) {
2599 com_error(c, PyExc_SyntaxError,
2600 "augmented assign to tuple not possible");
2601 return;
2603 com_assign_sequence(c, n, assigning);
2604 return;
2606 n = CHILD(n, 0);
2607 break;
2609 case test:
2610 case and_test:
2611 case not_test:
2612 case comparison:
2613 case expr:
2614 case xor_expr:
2615 case and_expr:
2616 case shift_expr:
2617 case arith_expr:
2618 case term:
2619 case factor:
2620 if (NCH(n) > 1) {
2621 com_error(c, PyExc_SyntaxError,
2622 "can't assign to operator");
2623 return;
2625 n = CHILD(n, 0);
2626 break;
2628 case power: /* atom trailer* ('**' power)*
2629 ('+'|'-'|'~') factor | atom trailer* */
2630 if (TYPE(CHILD(n, 0)) != atom) {
2631 com_error(c, PyExc_SyntaxError,
2632 "can't assign to operator");
2633 return;
2635 if (NCH(n) > 1) { /* trailer or exponent present */
2636 int i;
2637 com_node(c, CHILD(n, 0));
2638 for (i = 1; i+1 < NCH(n); i++) {
2639 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2640 com_error(c, PyExc_SyntaxError,
2641 "can't assign to operator");
2642 return;
2644 com_apply_trailer(c, CHILD(n, i));
2645 } /* NB i is still alive */
2646 com_assign_trailer(c,
2647 CHILD(n, i), assigning, augn);
2648 return;
2650 n = CHILD(n, 0);
2651 break;
2653 case atom:
2654 switch (TYPE(CHILD(n, 0))) {
2655 case LPAR:
2656 n = CHILD(n, 1);
2657 if (TYPE(n) == RPAR) {
2658 /* XXX Should allow () = () ??? */
2659 com_error(c, PyExc_SyntaxError,
2660 "can't assign to ()");
2661 return;
2663 if (assigning > OP_APPLY) {
2664 com_error(c, PyExc_SyntaxError,
2665 "augmented assign to tuple not possible");
2666 return;
2668 break;
2669 case LSQB:
2670 n = CHILD(n, 1);
2671 if (TYPE(n) == RSQB) {
2672 com_error(c, PyExc_SyntaxError,
2673 "can't assign to []");
2674 return;
2676 if (assigning > OP_APPLY) {
2677 com_error(c, PyExc_SyntaxError,
2678 "augmented assign to list not possible");
2679 return;
2681 if (NCH(n) > 1
2682 && TYPE(CHILD(n, 1)) == list_for) {
2683 com_error(c, PyExc_SyntaxError,
2684 "can't assign to list comprehension");
2685 return;
2687 com_assign_sequence(c, n, assigning);
2688 return;
2689 case NAME:
2690 if (assigning > OP_APPLY)
2691 com_augassign_name(c, CHILD(n, 0),
2692 assigning, augn);
2693 else
2694 com_assign_name(c, CHILD(n, 0),
2695 assigning);
2696 return;
2697 default:
2698 com_error(c, PyExc_SyntaxError,
2699 "can't assign to literal");
2700 return;
2702 break;
2704 case lambdef:
2705 com_error(c, PyExc_SyntaxError,
2706 "can't assign to lambda");
2707 return;
2709 default:
2710 com_error(c, PyExc_SystemError,
2711 "com_assign: bad node");
2712 return;
2718 static void
2719 com_augassign(struct compiling *c, node *n)
2721 int opcode;
2723 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2724 case '+': opcode = INPLACE_ADD; break;
2725 case '-': opcode = INPLACE_SUBTRACT; break;
2726 case '/':
2727 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2728 opcode = INPLACE_FLOOR_DIVIDE;
2729 else if (c->c_flags & CO_FUTURE_DIVISION)
2730 opcode = INPLACE_TRUE_DIVIDE;
2731 else
2732 opcode = INPLACE_DIVIDE;
2733 break;
2734 case '%': opcode = INPLACE_MODULO; break;
2735 case '<': opcode = INPLACE_LSHIFT; break;
2736 case '>': opcode = INPLACE_RSHIFT; break;
2737 case '&': opcode = INPLACE_AND; break;
2738 case '^': opcode = INPLACE_XOR; break;
2739 case '|': opcode = INPLACE_OR; break;
2740 case '*':
2741 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2742 opcode = INPLACE_POWER;
2743 else
2744 opcode = INPLACE_MULTIPLY;
2745 break;
2746 default:
2747 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2748 return;
2750 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2753 static void
2754 com_expr_stmt(struct compiling *c, node *n)
2756 REQ(n, expr_stmt);
2757 /* testlist (('=' testlist)* | augassign testlist) */
2758 /* Forget it if we have just a doc string here */
2759 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2760 return;
2761 if (NCH(n) == 1) {
2762 com_node(c, CHILD(n, NCH(n)-1));
2763 if (c->c_interactive)
2764 com_addbyte(c, PRINT_EXPR);
2765 else
2766 com_addbyte(c, POP_TOP);
2767 com_pop(c, 1);
2769 else if (TYPE(CHILD(n,1)) == augassign)
2770 com_augassign(c, n);
2771 else {
2772 int i;
2773 com_node(c, CHILD(n, NCH(n)-1));
2774 for (i = 0; i < NCH(n)-2; i+=2) {
2775 if (i+2 < NCH(n)-2) {
2776 com_addbyte(c, DUP_TOP);
2777 com_push(c, 1);
2779 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2784 static void
2785 com_assert_stmt(struct compiling *c, node *n)
2787 int a = 0;
2788 int i;
2789 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2790 if (Py_OptimizeFlag)
2791 return;
2792 /* Generate code like
2794 if not <test>:
2795 raise AssertionError [, <message>]
2797 where <message> is the second test, if present.
2799 com_node(c, CHILD(n, 1));
2800 com_addfwref(c, JUMP_IF_TRUE, &a);
2801 com_addbyte(c, POP_TOP);
2802 com_pop(c, 1);
2803 /* Raise that exception! */
2804 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2805 com_push(c, 1);
2806 i = NCH(n)/2; /* Either 2 or 4 */
2807 if (i > 1)
2808 com_node(c, CHILD(n, 3));
2809 com_addoparg(c, RAISE_VARARGS, i);
2810 com_pop(c, i);
2811 /* The interpreter does not fall through */
2812 /* Jump ends up here */
2813 com_backpatch(c, a);
2814 com_addbyte(c, POP_TOP);
2817 static void
2818 com_print_stmt(struct compiling *c, node *n)
2820 int i = 1;
2821 node* stream = NULL;
2823 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2825 /* are we using the extended print form? */
2826 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2827 stream = CHILD(n, 2);
2828 com_node(c, stream);
2829 /* stack: [...] => [... stream] */
2830 com_push(c, 1);
2831 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2832 i = 4;
2833 else
2834 i = 3;
2836 for (; i < NCH(n); i += 2) {
2837 if (stream != NULL) {
2838 com_addbyte(c, DUP_TOP);
2839 /* stack: [stream] => [stream stream] */
2840 com_push(c, 1);
2841 com_node(c, CHILD(n, i));
2842 /* stack: [stream stream] => [stream stream obj] */
2843 com_addbyte(c, ROT_TWO);
2844 /* stack: [stream stream obj] => [stream obj stream] */
2845 com_addbyte(c, PRINT_ITEM_TO);
2846 /* stack: [stream obj stream] => [stream] */
2847 com_pop(c, 2);
2849 else {
2850 com_node(c, CHILD(n, i));
2851 /* stack: [...] => [... obj] */
2852 com_addbyte(c, PRINT_ITEM);
2853 com_pop(c, 1);
2856 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2857 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2858 if (stream != NULL) {
2859 /* must pop the extra stream object off the stack */
2860 com_addbyte(c, POP_TOP);
2861 /* stack: [... stream] => [...] */
2862 com_pop(c, 1);
2865 else {
2866 if (stream != NULL) {
2867 /* this consumes the last stream object on stack */
2868 com_addbyte(c, PRINT_NEWLINE_TO);
2869 /* stack: [... stream] => [...] */
2870 com_pop(c, 1);
2872 else
2873 com_addbyte(c, PRINT_NEWLINE);
2877 static void
2878 com_return_stmt(struct compiling *c, node *n)
2880 REQ(n, return_stmt); /* 'return' [testlist] */
2881 if (!c->c_infunction) {
2882 com_error(c, PyExc_SyntaxError, "'return' outside function");
2884 if (c->c_flags & CO_GENERATOR) {
2885 if (NCH(n) > 1) {
2886 com_error(c, PyExc_SyntaxError,
2887 "'return' with argument inside generator");
2890 if (NCH(n) < 2) {
2891 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2892 com_push(c, 1);
2894 else
2895 com_node(c, CHILD(n, 1));
2896 com_addbyte(c, RETURN_VALUE);
2897 com_pop(c, 1);
2900 static void
2901 com_yield_stmt(struct compiling *c, node *n)
2903 int i;
2904 REQ(n, yield_stmt); /* 'yield' testlist */
2905 if (!c->c_infunction) {
2906 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2909 for (i = 0; i < c->c_nblocks; ++i) {
2910 if (c->c_block[i] == SETUP_FINALLY) {
2911 com_error(c, PyExc_SyntaxError,
2912 "'yield' not allowed in a 'try' block "
2913 "with a 'finally' clause");
2914 return;
2917 com_node(c, CHILD(n, 1));
2918 com_addbyte(c, YIELD_VALUE);
2919 com_pop(c, 1);
2922 static void
2923 com_raise_stmt(struct compiling *c, node *n)
2925 int i;
2926 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2927 if (NCH(n) > 1) {
2928 com_node(c, CHILD(n, 1));
2929 if (NCH(n) > 3) {
2930 com_node(c, CHILD(n, 3));
2931 if (NCH(n) > 5)
2932 com_node(c, CHILD(n, 5));
2935 i = NCH(n)/2;
2936 com_addoparg(c, RAISE_VARARGS, i);
2937 com_pop(c, i);
2940 static void
2941 com_from_import(struct compiling *c, node *n)
2943 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2944 com_push(c, 1);
2945 if (NCH(n) > 1) {
2946 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2947 com_error(c, PyExc_SyntaxError, "invalid syntax");
2948 return;
2950 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2951 } else
2952 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2953 com_pop(c, 1);
2956 static void
2957 com_import_stmt(struct compiling *c, node *n)
2959 int i;
2960 REQ(n, import_stmt);
2961 /* 'import' dotted_name (',' dotted_name)* |
2962 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2963 if (STR(CHILD(n, 0))[0] == 'f') {
2964 PyObject *tup;
2965 /* 'from' dotted_name 'import' ... */
2966 REQ(CHILD(n, 1), dotted_name);
2968 if (TYPE(CHILD(n, 3)) == STAR) {
2969 tup = Py_BuildValue("(s)", "*");
2970 } else {
2971 tup = PyTuple_New((NCH(n) - 2)/2);
2972 for (i = 3; i < NCH(n); i += 2) {
2973 PyTuple_SET_ITEM(tup, (i-3)/2,
2974 PyString_FromString(STR(
2975 CHILD(CHILD(n, i), 0))));
2978 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2979 Py_DECREF(tup);
2980 com_push(c, 1);
2981 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2982 if (TYPE(CHILD(n, 3)) == STAR)
2983 com_addbyte(c, IMPORT_STAR);
2984 else {
2985 for (i = 3; i < NCH(n); i += 2)
2986 com_from_import(c, CHILD(n, i));
2987 com_addbyte(c, POP_TOP);
2989 com_pop(c, 1);
2991 else {
2992 /* 'import' ... */
2993 for (i = 1; i < NCH(n); i += 2) {
2994 node *subn = CHILD(n, i);
2995 REQ(subn, dotted_as_name);
2996 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2997 com_push(c, 1);
2998 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2999 if (NCH(subn) > 1) {
3000 int j;
3001 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
3002 com_error(c, PyExc_SyntaxError,
3003 "invalid syntax");
3004 return;
3006 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
3007 com_addopname(c, LOAD_ATTR,
3008 CHILD(CHILD(subn, 0),
3009 j));
3010 com_addop_varname(c, VAR_STORE,
3011 STR(CHILD(subn, 2)));
3012 } else
3013 com_addop_varname(c, VAR_STORE,
3014 STR(CHILD(CHILD(subn, 0),
3015 0)));
3016 com_pop(c, 1);
3021 static void
3022 com_exec_stmt(struct compiling *c, node *n)
3024 REQ(n, exec_stmt);
3025 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
3026 com_node(c, CHILD(n, 1));
3027 if (NCH(n) >= 4)
3028 com_node(c, CHILD(n, 3));
3029 else {
3030 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3031 com_push(c, 1);
3033 if (NCH(n) >= 6)
3034 com_node(c, CHILD(n, 5));
3035 else {
3036 com_addbyte(c, DUP_TOP);
3037 com_push(c, 1);
3039 com_addbyte(c, EXEC_STMT);
3040 com_pop(c, 3);
3043 static int
3044 is_constant_false(struct compiling *c, node *n)
3046 PyObject *v;
3047 int i;
3048 /* argument c will be NULL when called from symtable_node() */
3050 /* Label to avoid tail recursion */
3051 next:
3052 switch (TYPE(n)) {
3054 case suite:
3055 if (NCH(n) == 1) {
3056 n = CHILD(n, 0);
3057 goto next;
3059 /* Fall through */
3060 case file_input:
3061 for (i = 0; i < NCH(n); i++) {
3062 node *ch = CHILD(n, i);
3063 if (TYPE(ch) == stmt) {
3064 n = ch;
3065 goto next;
3068 break;
3070 case stmt:
3071 case simple_stmt:
3072 case small_stmt:
3073 n = CHILD(n, 0);
3074 goto next;
3076 case expr_stmt:
3077 case testlist:
3078 case testlist1:
3079 case test:
3080 case and_test:
3081 case not_test:
3082 case comparison:
3083 case expr:
3084 case xor_expr:
3085 case and_expr:
3086 case shift_expr:
3087 case arith_expr:
3088 case term:
3089 case factor:
3090 case power:
3091 case atom:
3092 if (NCH(n) == 1) {
3093 n = CHILD(n, 0);
3094 goto next;
3096 break;
3098 case NAME:
3099 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
3100 return 1;
3101 break;
3103 case NUMBER:
3104 v = parsenumber(c, STR(n));
3105 if (v == NULL) {
3106 PyErr_Clear();
3107 break;
3109 i = PyObject_IsTrue(v);
3110 Py_DECREF(v);
3111 return i == 0;
3113 case STRING:
3114 v = parsestr(c, STR(n));
3115 if (v == NULL) {
3116 PyErr_Clear();
3117 break;
3119 i = PyObject_IsTrue(v);
3120 Py_DECREF(v);
3121 return i == 0;
3124 return 0;
3128 /* Look under n for a return stmt with an expression.
3129 * This hack is used to find illegal returns under "if 0:" blocks in
3130 * functions already known to be generators (as determined by the symtable
3131 * pass).
3132 * Return the offending return node if found, else NULL.
3134 static node *
3135 look_for_offending_return(node *n)
3137 int i;
3139 for (i = 0; i < NCH(n); ++i) {
3140 node *kid = CHILD(n, i);
3142 switch (TYPE(kid)) {
3143 case classdef:
3144 case funcdef:
3145 case lambdef:
3146 /* Stuff in nested functions & classes doesn't
3147 affect the code block we started in. */
3148 return NULL;
3150 case return_stmt:
3151 if (NCH(kid) > 1)
3152 return kid;
3153 break;
3155 default: {
3156 node *bad = look_for_offending_return(kid);
3157 if (bad != NULL)
3158 return bad;
3163 return NULL;
3166 static void
3167 com_if_stmt(struct compiling *c, node *n)
3169 int i;
3170 int anchor = 0;
3171 REQ(n, if_stmt);
3172 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3173 for (i = 0; i+3 < NCH(n); i+=4) {
3174 int a = 0;
3175 node *ch = CHILD(n, i+1);
3176 if (is_constant_false(c, ch)) {
3177 /* We're going to skip this block. However, if this
3178 is a generator, we have to check the dead code
3179 anyway to make sure there aren't any return stmts
3180 with expressions, in the same scope. */
3181 if (c->c_flags & CO_GENERATOR) {
3182 node *p = look_for_offending_return(n);
3183 if (p != NULL) {
3184 int savelineno = c->c_lineno;
3185 c->c_lineno = p->n_lineno;
3186 com_error(c, PyExc_SyntaxError,
3187 "'return' with argument "
3188 "inside generator");
3189 c->c_lineno = savelineno;
3192 continue;
3194 if (i > 0)
3195 com_set_lineno(c, ch->n_lineno);
3196 com_node(c, ch);
3197 com_addfwref(c, JUMP_IF_FALSE, &a);
3198 com_addbyte(c, POP_TOP);
3199 com_pop(c, 1);
3200 com_node(c, CHILD(n, i+3));
3201 com_addfwref(c, JUMP_FORWARD, &anchor);
3202 com_backpatch(c, a);
3203 /* We jump here with an extra entry which we now pop */
3204 com_addbyte(c, POP_TOP);
3206 if (i+2 < NCH(n))
3207 com_node(c, CHILD(n, i+2));
3208 if (anchor)
3209 com_backpatch(c, anchor);
3212 static void
3213 com_while_stmt(struct compiling *c, node *n)
3215 int break_anchor = 0;
3216 int anchor = 0;
3217 int save_begin = c->c_begin;
3218 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3219 com_addfwref(c, SETUP_LOOP, &break_anchor);
3220 block_push(c, SETUP_LOOP);
3221 c->c_begin = c->c_nexti;
3222 com_set_lineno(c, n->n_lineno);
3223 com_node(c, CHILD(n, 1));
3224 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3225 com_addbyte(c, POP_TOP);
3226 com_pop(c, 1);
3227 c->c_loops++;
3228 com_node(c, CHILD(n, 3));
3229 c->c_loops--;
3230 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3231 c->c_begin = save_begin;
3232 com_backpatch(c, anchor);
3233 /* We jump here with one entry more on the stack */
3234 com_addbyte(c, POP_TOP);
3235 com_addbyte(c, POP_BLOCK);
3236 block_pop(c, SETUP_LOOP);
3237 if (NCH(n) > 4)
3238 com_node(c, CHILD(n, 6));
3239 com_backpatch(c, break_anchor);
3242 static void
3243 com_for_stmt(struct compiling *c, node *n)
3245 int break_anchor = 0;
3246 int anchor = 0;
3247 int save_begin = c->c_begin;
3248 REQ(n, for_stmt);
3249 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3250 com_addfwref(c, SETUP_LOOP, &break_anchor);
3251 block_push(c, SETUP_LOOP);
3252 com_node(c, CHILD(n, 3));
3253 com_addbyte(c, GET_ITER);
3254 c->c_begin = c->c_nexti;
3255 com_set_lineno(c, n->n_lineno);
3256 com_addfwref(c, FOR_ITER, &anchor);
3257 com_push(c, 1);
3258 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
3259 c->c_loops++;
3260 com_node(c, CHILD(n, 5));
3261 c->c_loops--;
3262 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3263 c->c_begin = save_begin;
3264 com_backpatch(c, anchor);
3265 com_pop(c, 1); /* FOR_ITER has popped this */
3266 com_addbyte(c, POP_BLOCK);
3267 block_pop(c, SETUP_LOOP);
3268 if (NCH(n) > 8)
3269 com_node(c, CHILD(n, 8));
3270 com_backpatch(c, break_anchor);
3273 /* Code generated for "try: S finally: Sf" is as follows:
3275 SETUP_FINALLY L
3276 <code for S>
3277 POP_BLOCK
3278 LOAD_CONST <nil>
3279 L: <code for Sf>
3280 END_FINALLY
3282 The special instructions use the block stack. Each block
3283 stack entry contains the instruction that created it (here
3284 SETUP_FINALLY), the level of the value stack at the time the
3285 block stack entry was created, and a label (here L).
3287 SETUP_FINALLY:
3288 Pushes the current value stack level and the label
3289 onto the block stack.
3290 POP_BLOCK:
3291 Pops en entry from the block stack, and pops the value
3292 stack until its level is the same as indicated on the
3293 block stack. (The label is ignored.)
3294 END_FINALLY:
3295 Pops a variable number of entries from the *value* stack
3296 and re-raises the exception they specify. The number of
3297 entries popped depends on the (pseudo) exception type.
3299 The block stack is unwound when an exception is raised:
3300 when a SETUP_FINALLY entry is found, the exception is pushed
3301 onto the value stack (and the exception condition is cleared),
3302 and the interpreter jumps to the label gotten from the block
3303 stack.
3305 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3306 (The contents of the value stack is shown in [], with the top
3307 at the right; 'tb' is trace-back info, 'val' the exception's
3308 associated value, and 'exc' the exception.)
3310 Value stack Label Instruction Argument
3311 [] SETUP_EXCEPT L1
3312 [] <code for S>
3313 [] POP_BLOCK
3314 [] JUMP_FORWARD L0
3316 [tb, val, exc] L1: DUP )
3317 [tb, val, exc, exc] <evaluate E1> )
3318 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3319 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3320 [tb, val, exc, 1] POP )
3321 [tb, val, exc] POP
3322 [tb, val] <assign to V1> (or POP if no V1)
3323 [tb] POP
3324 [] <code for S1>
3325 JUMP_FORWARD L0
3327 [tb, val, exc, 0] L2: POP
3328 [tb, val, exc] DUP
3329 .............................etc.......................
3331 [tb, val, exc, 0] Ln+1: POP
3332 [tb, val, exc] END_FINALLY # re-raise exception
3334 [] L0: <next statement>
3336 Of course, parts are not generated if Vi or Ei is not present.
3339 static void
3340 com_try_except(struct compiling *c, node *n)
3342 int except_anchor = 0;
3343 int end_anchor = 0;
3344 int else_anchor = 0;
3345 int i;
3346 node *ch;
3348 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3349 block_push(c, SETUP_EXCEPT);
3350 com_node(c, CHILD(n, 2));
3351 com_addbyte(c, POP_BLOCK);
3352 block_pop(c, SETUP_EXCEPT);
3353 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3354 com_backpatch(c, except_anchor);
3355 for (i = 3;
3356 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3357 i += 3) {
3358 /* except_clause: 'except' [expr [',' var]] */
3359 if (except_anchor == 0) {
3360 com_error(c, PyExc_SyntaxError,
3361 "default 'except:' must be last");
3362 break;
3364 except_anchor = 0;
3365 com_push(c, 3); /* tb, val, exc pushed by exception */
3366 com_set_lineno(c, ch->n_lineno);
3367 if (NCH(ch) > 1) {
3368 com_addbyte(c, DUP_TOP);
3369 com_push(c, 1);
3370 com_node(c, CHILD(ch, 1));
3371 com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
3372 com_pop(c, 1);
3373 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3374 com_addbyte(c, POP_TOP);
3375 com_pop(c, 1);
3377 com_addbyte(c, POP_TOP);
3378 com_pop(c, 1);
3379 if (NCH(ch) > 3)
3380 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3381 else {
3382 com_addbyte(c, POP_TOP);
3383 com_pop(c, 1);
3385 com_addbyte(c, POP_TOP);
3386 com_pop(c, 1);
3387 com_node(c, CHILD(n, i+2));
3388 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3389 if (except_anchor) {
3390 com_backpatch(c, except_anchor);
3391 /* We come in with [tb, val, exc, 0] on the
3392 stack; one pop and it's the same as
3393 expected at the start of the loop */
3394 com_addbyte(c, POP_TOP);
3397 /* We actually come in here with [tb, val, exc] but the
3398 END_FINALLY will zap those and jump around.
3399 The c_stacklevel does not reflect them so we need not pop
3400 anything. */
3401 com_addbyte(c, END_FINALLY);
3402 com_backpatch(c, else_anchor);
3403 if (i < NCH(n))
3404 com_node(c, CHILD(n, i+2));
3405 com_backpatch(c, end_anchor);
3408 static void
3409 com_try_finally(struct compiling *c, node *n)
3411 int finally_anchor = 0;
3412 node *ch;
3414 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3415 block_push(c, SETUP_FINALLY);
3416 com_node(c, CHILD(n, 2));
3417 com_addbyte(c, POP_BLOCK);
3418 block_pop(c, SETUP_FINALLY);
3419 block_push(c, END_FINALLY);
3420 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3421 /* While the generated code pushes only one item,
3422 the try-finally handling can enter here with
3423 up to three items. OK, here are the details:
3424 3 for an exception, 2 for RETURN, 1 for BREAK. */
3425 com_push(c, 3);
3426 com_backpatch(c, finally_anchor);
3427 ch = CHILD(n, NCH(n)-1);
3428 com_set_lineno(c, ch->n_lineno);
3429 com_node(c, ch);
3430 com_addbyte(c, END_FINALLY);
3431 block_pop(c, END_FINALLY);
3432 com_pop(c, 3); /* Matches the com_push above */
3435 static void
3436 com_try_stmt(struct compiling *c, node *n)
3438 REQ(n, try_stmt);
3439 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3440 | 'try' ':' suite 'finally' ':' suite */
3441 if (TYPE(CHILD(n, 3)) != except_clause)
3442 com_try_finally(c, n);
3443 else
3444 com_try_except(c, n);
3447 static node *
3448 get_rawdocstring(node *n)
3450 int i;
3452 /* Label to avoid tail recursion */
3453 next:
3454 switch (TYPE(n)) {
3456 case suite:
3457 if (NCH(n) == 1) {
3458 n = CHILD(n, 0);
3459 goto next;
3461 /* Fall through */
3462 case file_input:
3463 for (i = 0; i < NCH(n); i++) {
3464 node *ch = CHILD(n, i);
3465 if (TYPE(ch) == stmt) {
3466 n = ch;
3467 goto next;
3470 break;
3472 case stmt:
3473 case simple_stmt:
3474 case small_stmt:
3475 n = CHILD(n, 0);
3476 goto next;
3478 case expr_stmt:
3479 case testlist:
3480 case testlist1:
3481 case test:
3482 case and_test:
3483 case not_test:
3484 case comparison:
3485 case expr:
3486 case xor_expr:
3487 case and_expr:
3488 case shift_expr:
3489 case arith_expr:
3490 case term:
3491 case factor:
3492 case power:
3493 if (NCH(n) == 1) {
3494 n = CHILD(n, 0);
3495 goto next;
3497 break;
3499 case atom:
3500 if (TYPE(CHILD(n, 0)) == STRING)
3501 return n;
3502 break;
3505 return NULL;
3508 static PyObject *
3509 get_docstring(struct compiling *c, node *n)
3511 /* Don't generate doc-strings if run with -OO */
3512 if (Py_OptimizeFlag > 1)
3513 return NULL;
3514 n = get_rawdocstring(n);
3515 if (n == NULL)
3516 return NULL;
3517 return parsestrplus(c, n);
3520 static void
3521 com_suite(struct compiling *c, node *n)
3523 REQ(n, suite);
3524 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3525 if (NCH(n) == 1) {
3526 com_node(c, CHILD(n, 0));
3528 else {
3529 int i;
3530 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3531 node *ch = CHILD(n, i);
3532 if (TYPE(ch) == stmt)
3533 com_node(c, ch);
3538 /* ARGSUSED */
3539 static void
3540 com_continue_stmt(struct compiling *c, node *n)
3542 int i = c->c_nblocks;
3543 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3544 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3546 else if (i <= 0) {
3547 /* at the outer level */
3548 com_error(c, PyExc_SyntaxError,
3549 "'continue' not properly in loop");
3551 else {
3552 int j;
3553 for (j = i-1; j >= 0; --j) {
3554 if (c->c_block[j] == SETUP_LOOP)
3555 break;
3557 if (j >= 0) {
3558 /* there is a loop, but something interferes */
3559 for (; i > j; --i) {
3560 if (c->c_block[i] == SETUP_EXCEPT ||
3561 c->c_block[i] == SETUP_FINALLY) {
3562 com_addoparg(c, CONTINUE_LOOP,
3563 c->c_begin);
3564 return;
3566 if (c->c_block[i] == END_FINALLY) {
3567 com_error(c, PyExc_SyntaxError,
3568 "'continue' not supported inside 'finally' clause");
3569 return;
3573 com_error(c, PyExc_SyntaxError,
3574 "'continue' not properly in loop");
3576 /* XXX Could allow it inside a 'finally' clause
3577 XXX if we could pop the exception still on the stack */
3580 static int
3581 com_argdefs(struct compiling *c, node *n)
3583 int i, nch, nargs, ndefs;
3584 if (TYPE(n) == lambdef) {
3585 /* lambdef: 'lambda' [varargslist] ':' test */
3586 n = CHILD(n, 1);
3588 else {
3589 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3590 n = CHILD(n, 2);
3591 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3592 n = CHILD(n, 1);
3594 if (TYPE(n) != varargslist)
3595 return 0;
3596 /* varargslist:
3597 (fpdef ['=' test] ',')* '*' ....... |
3598 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3599 nch = NCH(n);
3600 nargs = 0;
3601 ndefs = 0;
3602 for (i = 0; i < nch; i++) {
3603 int t;
3604 if (TYPE(CHILD(n, i)) == STAR ||
3605 TYPE(CHILD(n, i)) == DOUBLESTAR)
3606 break;
3607 nargs++;
3608 i++;
3609 if (i >= nch)
3610 t = RPAR; /* Anything except EQUAL or COMMA */
3611 else
3612 t = TYPE(CHILD(n, i));
3613 if (t == EQUAL) {
3614 i++;
3615 ndefs++;
3616 com_node(c, CHILD(n, i));
3617 i++;
3618 if (i >= nch)
3619 break;
3620 t = TYPE(CHILD(n, i));
3622 else {
3623 /* Treat "(a=1, b)" as an error */
3624 if (ndefs)
3625 com_error(c, PyExc_SyntaxError,
3626 "non-default argument follows default argument");
3628 if (t != COMMA)
3629 break;
3631 return ndefs;
3634 static void
3635 com_funcdef(struct compiling *c, node *n)
3637 PyObject *co;
3638 int ndefs;
3639 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3640 ndefs = com_argdefs(c, n);
3641 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3642 n->n_lineno);
3643 co = (PyObject *)icompile(n, c);
3644 symtable_exit_scope(c->c_symtable);
3645 if (co == NULL)
3646 c->c_errors++;
3647 else {
3648 int closure = com_make_closure(c, (PyCodeObject *)co);
3649 int i = com_addconst(c, co);
3650 com_addoparg(c, LOAD_CONST, i);
3651 com_push(c, 1);
3652 if (closure)
3653 com_addoparg(c, MAKE_CLOSURE, ndefs);
3654 else
3655 com_addoparg(c, MAKE_FUNCTION, ndefs);
3656 com_pop(c, ndefs);
3657 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3658 com_pop(c, 1);
3659 Py_DECREF(co);
3663 static void
3664 com_bases(struct compiling *c, node *n)
3666 int i;
3667 REQ(n, testlist);
3668 /* testlist: test (',' test)* [','] */
3669 for (i = 0; i < NCH(n); i += 2)
3670 com_node(c, CHILD(n, i));
3671 i = (NCH(n)+1) / 2;
3672 com_addoparg(c, BUILD_TUPLE, i);
3673 com_pop(c, i-1);
3676 static void
3677 com_classdef(struct compiling *c, node *n)
3679 int i;
3680 PyObject *v;
3681 PyCodeObject *co;
3682 char *name;
3684 REQ(n, classdef);
3685 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3686 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3687 c->c_errors++;
3688 return;
3690 /* Push the class name on the stack */
3691 i = com_addconst(c, v);
3692 com_addoparg(c, LOAD_CONST, i);
3693 com_push(c, 1);
3694 Py_DECREF(v);
3695 /* Push the tuple of base classes on the stack */
3696 if (TYPE(CHILD(n, 2)) != LPAR) {
3697 com_addoparg(c, BUILD_TUPLE, 0);
3698 com_push(c, 1);
3700 else
3701 com_bases(c, CHILD(n, 3));
3702 name = STR(CHILD(n, 1));
3703 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3704 co = icompile(n, c);
3705 symtable_exit_scope(c->c_symtable);
3706 if (co == NULL)
3707 c->c_errors++;
3708 else {
3709 int closure = com_make_closure(c, co);
3710 i = com_addconst(c, (PyObject *)co);
3711 com_addoparg(c, LOAD_CONST, i);
3712 com_push(c, 1);
3713 if (closure) {
3714 com_addoparg(c, MAKE_CLOSURE, 0);
3715 com_pop(c, PyCode_GetNumFree(co));
3716 } else
3717 com_addoparg(c, MAKE_FUNCTION, 0);
3718 com_addoparg(c, CALL_FUNCTION, 0);
3719 com_addbyte(c, BUILD_CLASS);
3720 com_pop(c, 2);
3721 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3722 com_pop(c, 1);
3723 Py_DECREF(co);
3727 static void
3728 com_node(struct compiling *c, node *n)
3730 loop:
3731 if (c->c_errors)
3732 return;
3733 switch (TYPE(n)) {
3735 /* Definition nodes */
3737 case funcdef:
3738 com_funcdef(c, n);
3739 break;
3740 case classdef:
3741 com_classdef(c, n);
3742 break;
3744 /* Trivial parse tree nodes */
3746 case stmt:
3747 case small_stmt:
3748 case flow_stmt:
3749 n = CHILD(n, 0);
3750 goto loop;
3752 case simple_stmt:
3753 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3754 com_set_lineno(c, n->n_lineno);
3756 int i;
3757 for (i = 0; i < NCH(n)-1; i += 2)
3758 com_node(c, CHILD(n, i));
3760 break;
3762 case compound_stmt:
3763 com_set_lineno(c, n->n_lineno);
3764 n = CHILD(n, 0);
3765 goto loop;
3767 /* Statement nodes */
3769 case expr_stmt:
3770 com_expr_stmt(c, n);
3771 break;
3772 case print_stmt:
3773 com_print_stmt(c, n);
3774 break;
3775 case del_stmt: /* 'del' exprlist */
3776 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3777 break;
3778 case pass_stmt:
3779 break;
3780 case break_stmt:
3781 if (c->c_loops == 0) {
3782 com_error(c, PyExc_SyntaxError,
3783 "'break' outside loop");
3785 com_addbyte(c, BREAK_LOOP);
3786 break;
3787 case continue_stmt:
3788 com_continue_stmt(c, n);
3789 break;
3790 case return_stmt:
3791 com_return_stmt(c, n);
3792 break;
3793 case yield_stmt:
3794 com_yield_stmt(c, n);
3795 break;
3796 case raise_stmt:
3797 com_raise_stmt(c, n);
3798 break;
3799 case import_stmt:
3800 com_import_stmt(c, n);
3801 break;
3802 case global_stmt:
3803 break;
3804 case exec_stmt:
3805 com_exec_stmt(c, n);
3806 break;
3807 case assert_stmt:
3808 com_assert_stmt(c, n);
3809 break;
3810 case if_stmt:
3811 com_if_stmt(c, n);
3812 break;
3813 case while_stmt:
3814 com_while_stmt(c, n);
3815 break;
3816 case for_stmt:
3817 com_for_stmt(c, n);
3818 break;
3819 case try_stmt:
3820 com_try_stmt(c, n);
3821 break;
3822 case suite:
3823 com_suite(c, n);
3824 break;
3826 /* Expression nodes */
3828 case testlist:
3829 case testlist1:
3830 case testlist_safe:
3831 com_list(c, n, 0);
3832 break;
3833 case test:
3834 com_test(c, n);
3835 break;
3836 case and_test:
3837 com_and_test(c, n);
3838 break;
3839 case not_test:
3840 com_not_test(c, n);
3841 break;
3842 case comparison:
3843 com_comparison(c, n);
3844 break;
3845 case exprlist:
3846 com_list(c, n, 0);
3847 break;
3848 case expr:
3849 com_expr(c, n);
3850 break;
3851 case xor_expr:
3852 com_xor_expr(c, n);
3853 break;
3854 case and_expr:
3855 com_and_expr(c, n);
3856 break;
3857 case shift_expr:
3858 com_shift_expr(c, n);
3859 break;
3860 case arith_expr:
3861 com_arith_expr(c, n);
3862 break;
3863 case term:
3864 com_term(c, n);
3865 break;
3866 case factor:
3867 com_factor(c, n);
3868 break;
3869 case power:
3870 com_power(c, n);
3871 break;
3872 case atom:
3873 com_atom(c, n);
3874 break;
3876 default:
3877 com_error(c, PyExc_SystemError,
3878 "com_node: unexpected node type");
3882 static void com_fplist(struct compiling *, node *);
3884 static void
3885 com_fpdef(struct compiling *c, node *n)
3887 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3888 if (TYPE(CHILD(n, 0)) == LPAR)
3889 com_fplist(c, CHILD(n, 1));
3890 else {
3891 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3892 com_pop(c, 1);
3896 static void
3897 com_fplist(struct compiling *c, node *n)
3899 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3900 if (NCH(n) == 1) {
3901 com_fpdef(c, CHILD(n, 0));
3903 else {
3904 int i = (NCH(n)+1)/2;
3905 com_addoparg(c, UNPACK_SEQUENCE, i);
3906 com_push(c, i-1);
3907 for (i = 0; i < NCH(n); i += 2)
3908 com_fpdef(c, CHILD(n, i));
3912 static void
3913 com_arglist(struct compiling *c, node *n)
3915 int nch, i, narg;
3916 int complex = 0;
3917 char nbuf[30];
3918 REQ(n, varargslist);
3919 /* varargslist:
3920 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3921 nch = NCH(n);
3922 /* Enter all arguments in table of locals */
3923 for (i = 0, narg = 0; i < nch; i++) {
3924 node *ch = CHILD(n, i);
3925 node *fp;
3926 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3927 break;
3928 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3929 fp = CHILD(ch, 0);
3930 if (TYPE(fp) != NAME) {
3931 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
3932 complex = 1;
3934 narg++;
3935 /* all name updates handled by symtable */
3936 if (++i >= nch)
3937 break;
3938 ch = CHILD(n, i);
3939 if (TYPE(ch) == EQUAL)
3940 i += 2;
3941 else
3942 REQ(ch, COMMA);
3944 if (complex) {
3945 /* Generate code for complex arguments only after
3946 having counted the simple arguments */
3947 int ilocal = 0;
3948 for (i = 0; i < nch; i++) {
3949 node *ch = CHILD(n, i);
3950 node *fp;
3951 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3952 break;
3953 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3954 fp = CHILD(ch, 0);
3955 if (TYPE(fp) != NAME) {
3956 com_addoparg(c, LOAD_FAST, ilocal);
3957 com_push(c, 1);
3958 com_fpdef(c, ch);
3960 ilocal++;
3961 if (++i >= nch)
3962 break;
3963 ch = CHILD(n, i);
3964 if (TYPE(ch) == EQUAL)
3965 i += 2;
3966 else
3967 REQ(ch, COMMA);
3972 static void
3973 com_file_input(struct compiling *c, node *n)
3975 int i;
3976 PyObject *doc;
3977 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3978 doc = get_docstring(c, n);
3979 if (doc != NULL) {
3980 int i = com_addconst(c, doc);
3981 Py_DECREF(doc);
3982 com_addoparg(c, LOAD_CONST, i);
3983 com_push(c, 1);
3984 com_addop_name(c, STORE_NAME, "__doc__");
3985 com_pop(c, 1);
3987 for (i = 0; i < NCH(n); i++) {
3988 node *ch = CHILD(n, i);
3989 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3990 com_node(c, ch);
3994 /* Top-level compile-node interface */
3996 static void
3997 compile_funcdef(struct compiling *c, node *n)
3999 PyObject *doc;
4000 node *ch;
4001 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
4002 c->c_name = STR(CHILD(n, 1));
4003 doc = get_docstring(c, CHILD(n, 4));
4004 if (doc != NULL) {
4005 (void) com_addconst(c, doc);
4006 Py_DECREF(doc);
4008 else
4009 (void) com_addconst(c, Py_None); /* No docstring */
4010 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
4011 ch = CHILD(ch, 1); /* ')' | varargslist */
4012 if (TYPE(ch) == varargslist)
4013 com_arglist(c, ch);
4014 c->c_infunction = 1;
4015 com_node(c, CHILD(n, 4));
4016 c->c_infunction = 0;
4017 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4018 com_push(c, 1);
4019 com_addbyte(c, RETURN_VALUE);
4020 com_pop(c, 1);
4023 static void
4024 compile_lambdef(struct compiling *c, node *n)
4026 node *ch;
4027 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
4028 c->c_name = "<lambda>";
4030 ch = CHILD(n, 1);
4031 (void) com_addconst(c, Py_None); /* No docstring */
4032 if (TYPE(ch) == varargslist) {
4033 com_arglist(c, ch);
4034 ch = CHILD(n, 3);
4036 else
4037 ch = CHILD(n, 2);
4038 com_node(c, ch);
4039 com_addbyte(c, RETURN_VALUE);
4040 com_pop(c, 1);
4043 static void
4044 compile_classdef(struct compiling *c, node *n)
4046 node *ch;
4047 PyObject *doc;
4048 REQ(n, classdef);
4049 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4050 c->c_name = STR(CHILD(n, 1));
4051 c->c_private = c->c_name;
4052 /* Initialize local __module__ from global __name__ */
4053 com_addop_name(c, LOAD_GLOBAL, "__name__");
4054 com_addop_name(c, STORE_NAME, "__module__");
4055 ch = CHILD(n, NCH(n)-1); /* The suite */
4056 doc = get_docstring(c, ch);
4057 if (doc != NULL) {
4058 int i = com_addconst(c, doc);
4059 Py_DECREF(doc);
4060 com_addoparg(c, LOAD_CONST, i);
4061 com_push(c, 1);
4062 com_addop_name(c, STORE_NAME, "__doc__");
4063 com_pop(c, 1);
4065 else
4066 (void) com_addconst(c, Py_None);
4067 com_node(c, ch);
4068 com_addbyte(c, LOAD_LOCALS);
4069 com_push(c, 1);
4070 com_addbyte(c, RETURN_VALUE);
4071 com_pop(c, 1);
4074 static void
4075 compile_node(struct compiling *c, node *n)
4077 com_set_lineno(c, n->n_lineno);
4079 switch (TYPE(n)) {
4081 case single_input: /* One interactive command */
4082 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
4083 c->c_interactive++;
4084 n = CHILD(n, 0);
4085 if (TYPE(n) != NEWLINE)
4086 com_node(c, n);
4087 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4088 com_push(c, 1);
4089 com_addbyte(c, RETURN_VALUE);
4090 com_pop(c, 1);
4091 c->c_interactive--;
4092 break;
4094 case file_input: /* A whole file, or built-in function exec() */
4095 com_file_input(c, n);
4096 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4097 com_push(c, 1);
4098 com_addbyte(c, RETURN_VALUE);
4099 com_pop(c, 1);
4100 break;
4102 case eval_input: /* Built-in function input() */
4103 com_node(c, CHILD(n, 0));
4104 com_addbyte(c, RETURN_VALUE);
4105 com_pop(c, 1);
4106 break;
4108 case lambdef: /* anonymous function definition */
4109 compile_lambdef(c, n);
4110 break;
4112 case funcdef: /* A function definition */
4113 compile_funcdef(c, n);
4114 break;
4116 case classdef: /* A class definition */
4117 compile_classdef(c, n);
4118 break;
4120 default:
4121 com_error(c, PyExc_SystemError,
4122 "compile_node: unexpected node type");
4126 static PyObject *
4127 dict_keys_inorder(PyObject *dict, int offset)
4129 PyObject *tuple, *k, *v;
4130 int i, pos = 0, size = PyDict_Size(dict);
4132 tuple = PyTuple_New(size);
4133 if (tuple == NULL)
4134 return NULL;
4135 while (PyDict_Next(dict, &pos, &k, &v)) {
4136 i = PyInt_AS_LONG(v);
4137 Py_INCREF(k);
4138 assert((i - offset) < size);
4139 PyTuple_SET_ITEM(tuple, i - offset, k);
4141 return tuple;
4144 PyCodeObject *
4145 PyNode_Compile(node *n, char *filename)
4147 return PyNode_CompileFlags(n, filename, NULL);
4150 PyCodeObject *
4151 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4153 return jcompile(n, filename, NULL, flags);
4156 struct symtable *
4157 PyNode_CompileSymtable(node *n, char *filename)
4159 struct symtable *st;
4160 PyFutureFeatures *ff;
4162 ff = PyNode_Future(n, filename);
4163 if (ff == NULL)
4164 return NULL;
4166 st = symtable_init();
4167 if (st == NULL) {
4168 PyObject_FREE((void *)ff);
4169 return NULL;
4171 st->st_future = ff;
4172 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
4173 if (st->st_errors > 0)
4174 goto fail;
4175 symtable_node(st, n);
4176 if (st->st_errors > 0)
4177 goto fail;
4179 return st;
4180 fail:
4181 PyObject_FREE((void *)ff);
4182 st->st_future = NULL;
4183 PySymtable_Free(st);
4184 return NULL;
4187 static PyCodeObject *
4188 icompile(node *n, struct compiling *base)
4190 return jcompile(n, base->c_filename, base, NULL);
4193 static PyCodeObject *
4194 jcompile(node *n, char *filename, struct compiling *base,
4195 PyCompilerFlags *flags)
4197 struct compiling sc;
4198 PyCodeObject *co;
4199 if (!com_init(&sc, filename))
4200 return NULL;
4201 if (TYPE(n) == encoding_decl) {
4202 sc.c_encoding = STR(n);
4203 n = CHILD(n, 0);
4204 } else {
4205 sc.c_encoding = NULL;
4207 if (base) {
4208 sc.c_private = base->c_private;
4209 sc.c_symtable = base->c_symtable;
4210 /* c_symtable still points to parent's symbols */
4211 if (base->c_nested
4212 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
4213 sc.c_nested = 1;
4214 sc.c_flags |= base->c_flags & PyCF_MASK;
4215 if (base->c_encoding != NULL) {
4216 assert(sc.c_encoding == NULL);
4217 sc.c_encoding = base->c_encoding;
4219 } else {
4220 sc.c_private = NULL;
4221 sc.c_future = PyNode_Future(n, filename);
4222 if (sc.c_future == NULL) {
4223 com_free(&sc);
4224 return NULL;
4226 if (flags) {
4227 int merged = sc.c_future->ff_features |
4228 flags->cf_flags;
4229 sc.c_future->ff_features = merged;
4230 flags->cf_flags = merged;
4232 if (symtable_build(&sc, n) < 0) {
4233 com_free(&sc);
4234 return NULL;
4237 co = NULL;
4238 if (symtable_load_symbols(&sc) < 0) {
4239 sc.c_errors++;
4240 goto exit;
4242 compile_node(&sc, n);
4243 com_done(&sc);
4244 if (sc.c_errors == 0) {
4245 PyObject *consts, *names, *varnames, *filename, *name,
4246 *freevars, *cellvars;
4247 consts = PyList_AsTuple(sc.c_consts);
4248 names = PyList_AsTuple(sc.c_names);
4249 varnames = PyList_AsTuple(sc.c_varnames);
4250 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4251 freevars = dict_keys_inorder(sc.c_freevars,
4252 PyTuple_GET_SIZE(cellvars));
4253 filename = PyString_InternFromString(sc.c_filename);
4254 name = PyString_InternFromString(sc.c_name);
4255 if (!PyErr_Occurred())
4256 co = PyCode_New(sc.c_argcount,
4257 sc.c_nlocals,
4258 sc.c_maxstacklevel,
4259 sc.c_flags,
4260 sc.c_code,
4261 consts,
4262 names,
4263 varnames,
4264 freevars,
4265 cellvars,
4266 filename,
4267 name,
4268 sc.c_firstlineno,
4269 sc.c_lnotab);
4270 Py_XDECREF(consts);
4271 Py_XDECREF(names);
4272 Py_XDECREF(varnames);
4273 Py_XDECREF(freevars);
4274 Py_XDECREF(cellvars);
4275 Py_XDECREF(filename);
4276 Py_XDECREF(name);
4278 else if (!PyErr_Occurred()) {
4279 /* This could happen if someone called PyErr_Clear() after an
4280 error was reported above. That's not supposed to happen,
4281 but I just plugged one case and I'm not sure there can't be
4282 others. In that case, raise SystemError so that at least
4283 it gets reported instead dumping core. */
4284 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4286 exit:
4287 if (base == NULL) {
4288 PySymtable_Free(sc.c_symtable);
4289 sc.c_symtable = NULL;
4291 com_free(&sc);
4292 return co;
4296 PyCode_Addr2Line(PyCodeObject *co, int addrq)
4298 int size = PyString_Size(co->co_lnotab) / 2;
4299 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
4300 int line = co->co_firstlineno;
4301 int addr = 0;
4302 while (--size >= 0) {
4303 addr += *p++;
4304 if (addr > addrq)
4305 break;
4306 line += *p++;
4308 return line;
4311 /* The test for LOCAL must come before the test for FREE in order to
4312 handle classes where name is both local and free. The local var is
4313 a method and the free var is a free var referenced within a method.
4316 static int
4317 get_ref_type(struct compiling *c, char *name)
4319 char buf[350];
4320 PyObject *v;
4322 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4323 return CELL;
4324 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4325 return LOCAL;
4326 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4327 return FREE;
4328 v = PyDict_GetItemString(c->c_globals, name);
4329 if (v) {
4330 if (v == Py_None)
4331 return GLOBAL_EXPLICIT;
4332 else {
4333 return GLOBAL_IMPLICIT;
4336 PyOS_snprintf(buf, sizeof(buf),
4337 "unknown scope for %.100s in %.100s(%s) "
4338 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4339 name, c->c_name,
4340 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4341 c->c_filename,
4342 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4343 PyObject_REPR(c->c_locals),
4344 PyObject_REPR(c->c_globals)
4347 Py_FatalError(buf);
4348 return -1;
4351 /* Helper functions to issue warnings */
4353 static int
4354 issue_warning(char *msg, char *filename, int lineno)
4356 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4357 lineno, NULL, NULL) < 0) {
4358 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4359 PyErr_SetString(PyExc_SyntaxError, msg);
4360 PyErr_SyntaxLocation(filename, lineno);
4362 return -1;
4364 return 0;
4367 static int
4368 symtable_warn(struct symtable *st, char *msg)
4370 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
4371 st->st_errors++;
4372 return -1;
4374 return 0;
4377 /* Helper function for setting lineno and filename */
4379 static int
4380 symtable_build(struct compiling *c, node *n)
4382 if ((c->c_symtable = symtable_init()) == NULL)
4383 return -1;
4384 c->c_symtable->st_future = c->c_future;
4385 c->c_symtable->st_filename = c->c_filename;
4386 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4387 if (c->c_symtable->st_errors > 0)
4388 return -1;
4389 symtable_node(c->c_symtable, n);
4390 if (c->c_symtable->st_errors > 0)
4391 return -1;
4392 /* reset for second pass */
4393 c->c_symtable->st_nscopes = 1;
4394 c->c_symtable->st_pass = 2;
4395 return 0;
4398 static int
4399 symtable_init_compiling_symbols(struct compiling *c)
4401 PyObject *varnames;
4403 varnames = c->c_symtable->st_cur->ste_varnames;
4404 if (varnames == NULL) {
4405 varnames = PyList_New(0);
4406 if (varnames == NULL)
4407 return -1;
4408 c->c_symtable->st_cur->ste_varnames = varnames;
4409 Py_INCREF(varnames);
4410 } else
4411 Py_INCREF(varnames);
4412 c->c_varnames = varnames;
4414 c->c_globals = PyDict_New();
4415 if (c->c_globals == NULL)
4416 return -1;
4417 c->c_freevars = PyDict_New();
4418 if (c->c_freevars == NULL)
4419 return -1;
4420 c->c_cellvars = PyDict_New();
4421 if (c->c_cellvars == NULL)
4422 return -1;
4423 return 0;
4426 struct symbol_info {
4427 int si_nlocals;
4428 int si_ncells;
4429 int si_nfrees;
4430 int si_nimplicit;
4433 static void
4434 symtable_init_info(struct symbol_info *si)
4436 si->si_nlocals = 0;
4437 si->si_ncells = 0;
4438 si->si_nfrees = 0;
4439 si->si_nimplicit = 0;
4442 static int
4443 symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
4444 struct symbol_info *si)
4446 PyObject *dict, *v;
4448 /* Seperate logic for DEF_FREE. If it occurs in a function,
4449 it indicates a local that we must allocate storage for (a
4450 cell var). If it occurs in a class, then the class has a
4451 method and a free variable with the same name.
4453 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4454 /* If it isn't declared locally, it can't be a cell. */
4455 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4456 return 0;
4457 v = PyInt_FromLong(si->si_ncells++);
4458 dict = c->c_cellvars;
4459 } else {
4460 /* If it is free anyway, then there is no need to do
4461 anything here.
4463 if (is_free(flags ^ DEF_FREE_CLASS)
4464 || (flags == DEF_FREE_CLASS))
4465 return 0;
4466 v = PyInt_FromLong(si->si_nfrees++);
4467 dict = c->c_freevars;
4469 if (v == NULL)
4470 return -1;
4471 if (PyDict_SetItem(dict, name, v) < 0) {
4472 Py_DECREF(v);
4473 return -1;
4475 Py_DECREF(v);
4476 return 0;
4479 /* If a variable is a cell and an argument, make sure that appears in
4480 co_cellvars before any variable to its right in varnames.
4484 static int
4485 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4486 PyObject *varnames, int flags)
4488 PyObject *v, *w, *d, *list = NULL;
4489 int i, pos;
4491 if (flags & CO_VARARGS)
4492 argcount++;
4493 if (flags & CO_VARKEYWORDS)
4494 argcount++;
4495 for (i = argcount; --i >= 0; ) {
4496 v = PyList_GET_ITEM(varnames, i);
4497 if (PyDict_GetItem(*cellvars, v)) {
4498 if (list == NULL) {
4499 list = PyList_New(1);
4500 if (list == NULL)
4501 return -1;
4502 PyList_SET_ITEM(list, 0, v);
4503 Py_INCREF(v);
4504 } else
4505 PyList_Insert(list, 0, v);
4508 if (list == NULL || PyList_GET_SIZE(list) == 0)
4509 return 0;
4510 /* There are cellvars that are also arguments. Create a dict
4511 to replace cellvars and put the args at the front.
4513 d = PyDict_New();
4514 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4515 v = PyInt_FromLong(i);
4516 if (v == NULL)
4517 goto fail;
4518 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4519 goto fail;
4520 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4521 goto fail;
4523 pos = 0;
4524 i = PyList_GET_SIZE(list);
4525 Py_DECREF(list);
4526 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4527 w = PyInt_FromLong(i++); /* don't care about the old key */
4528 if (PyDict_SetItem(d, v, w) < 0) {
4529 Py_DECREF(w);
4530 goto fail;
4532 Py_DECREF(w);
4534 Py_DECREF(*cellvars);
4535 *cellvars = d;
4536 return 1;
4537 fail:
4538 Py_DECREF(d);
4539 return -1;
4542 static int
4543 symtable_freevar_offsets(PyObject *freevars, int offset)
4545 PyObject *name, *v;
4546 int pos;
4548 /* The cell vars are the first elements of the closure,
4549 followed by the free vars. Update the offsets in
4550 c_freevars to account for number of cellvars. */
4551 pos = 0;
4552 while (PyDict_Next(freevars, &pos, &name, &v)) {
4553 int i = PyInt_AS_LONG(v) + offset;
4554 PyObject *o = PyInt_FromLong(i);
4555 if (o == NULL)
4556 return -1;
4557 if (PyDict_SetItem(freevars, name, o) < 0) {
4558 Py_DECREF(o);
4559 return -1;
4561 Py_DECREF(o);
4563 return 0;
4566 static int
4567 symtable_check_unoptimized(struct compiling *c,
4568 PySymtableEntryObject *ste,
4569 struct symbol_info *si)
4571 char buf[300];
4573 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4574 || (ste->ste_nested && si->si_nimplicit)))
4575 return 0;
4577 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4579 #define ILLEGAL_IS "is a nested function"
4581 #define ILLEGAL_IMPORT_STAR \
4582 "import * is not allowed in function '%.100s' because it %s"
4584 #define ILLEGAL_BARE_EXEC \
4585 "unqualified exec is not allowed in function '%.100s' it %s"
4587 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4588 "function '%.100s' uses import * and bare exec, which are illegal " \
4589 "because it %s"
4591 /* XXX perhaps the linenos for these opt-breaking statements
4592 should be stored so the exception can point to them. */
4594 if (ste->ste_child_free) {
4595 if (ste->ste_optimized == OPT_IMPORT_STAR)
4596 PyOS_snprintf(buf, sizeof(buf),
4597 ILLEGAL_IMPORT_STAR,
4598 PyString_AS_STRING(ste->ste_name),
4599 ILLEGAL_CONTAINS);
4600 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4601 PyOS_snprintf(buf, sizeof(buf),
4602 ILLEGAL_BARE_EXEC,
4603 PyString_AS_STRING(ste->ste_name),
4604 ILLEGAL_CONTAINS);
4605 else {
4606 PyOS_snprintf(buf, sizeof(buf),
4607 ILLEGAL_EXEC_AND_IMPORT_STAR,
4608 PyString_AS_STRING(ste->ste_name),
4609 ILLEGAL_CONTAINS);
4611 } else {
4612 if (ste->ste_optimized == OPT_IMPORT_STAR)
4613 PyOS_snprintf(buf, sizeof(buf),
4614 ILLEGAL_IMPORT_STAR,
4615 PyString_AS_STRING(ste->ste_name),
4616 ILLEGAL_IS);
4617 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4618 PyOS_snprintf(buf, sizeof(buf),
4619 ILLEGAL_BARE_EXEC,
4620 PyString_AS_STRING(ste->ste_name),
4621 ILLEGAL_IS);
4622 else {
4623 PyOS_snprintf(buf, sizeof(buf),
4624 ILLEGAL_EXEC_AND_IMPORT_STAR,
4625 PyString_AS_STRING(ste->ste_name),
4626 ILLEGAL_IS);
4630 PyErr_SetString(PyExc_SyntaxError, buf);
4631 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4632 ste->ste_opt_lineno);
4633 return -1;
4636 static int
4637 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4638 struct symbol_info *si)
4640 if (c->c_future)
4641 c->c_flags |= c->c_future->ff_features;
4642 if (ste->ste_generator)
4643 c->c_flags |= CO_GENERATOR;
4644 if (ste->ste_type != TYPE_MODULE)
4645 c->c_flags |= CO_NEWLOCALS;
4646 if (ste->ste_type == TYPE_FUNCTION) {
4647 c->c_nlocals = si->si_nlocals;
4648 if (ste->ste_optimized == 0)
4649 c->c_flags |= CO_OPTIMIZED;
4650 else if (ste->ste_optimized != OPT_EXEC)
4651 return symtable_check_unoptimized(c, ste, si);
4653 return 0;
4656 static int
4657 symtable_load_symbols(struct compiling *c)
4659 static PyObject *implicit = NULL;
4660 struct symtable *st = c->c_symtable;
4661 PySymtableEntryObject *ste = st->st_cur;
4662 PyObject *name, *varnames, *v;
4663 int i, flags, pos;
4664 struct symbol_info si;
4666 if (implicit == NULL) {
4667 implicit = PyInt_FromLong(1);
4668 if (implicit == NULL)
4669 return -1;
4671 v = NULL;
4673 if (symtable_init_compiling_symbols(c) < 0)
4674 goto fail;
4675 symtable_init_info(&si);
4676 varnames = st->st_cur->ste_varnames;
4677 si.si_nlocals = PyList_GET_SIZE(varnames);
4678 c->c_argcount = si.si_nlocals;
4680 for (i = 0; i < si.si_nlocals; ++i) {
4681 v = PyInt_FromLong(i);
4682 if (PyDict_SetItem(c->c_locals,
4683 PyList_GET_ITEM(varnames, i), v) < 0)
4684 goto fail;
4685 Py_DECREF(v);
4688 /* XXX The cases below define the rules for whether a name is
4689 local or global. The logic could probably be clearer. */
4690 pos = 0;
4691 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4692 flags = PyInt_AS_LONG(v);
4694 if (flags & DEF_FREE_GLOBAL)
4695 /* undo the original DEF_FREE */
4696 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4698 /* Deal with names that need two actions:
4699 1. Cell variables that are also locals.
4700 2. Free variables in methods that are also class
4701 variables or declared global.
4703 if (flags & (DEF_FREE | DEF_FREE_CLASS))
4704 symtable_resolve_free(c, name, flags, &si);
4706 if (flags & DEF_STAR) {
4707 c->c_argcount--;
4708 c->c_flags |= CO_VARARGS;
4709 } else if (flags & DEF_DOUBLESTAR) {
4710 c->c_argcount--;
4711 c->c_flags |= CO_VARKEYWORDS;
4712 } else if (flags & DEF_INTUPLE)
4713 c->c_argcount--;
4714 else if (flags & DEF_GLOBAL) {
4715 if (flags & DEF_PARAM) {
4716 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4717 PyString_AS_STRING(name));
4718 PyErr_SyntaxLocation(st->st_filename,
4719 ste->ste_lineno);
4720 st->st_errors++;
4721 goto fail;
4723 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4724 goto fail;
4725 } else if (flags & DEF_FREE_GLOBAL) {
4726 si.si_nimplicit++;
4727 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4728 goto fail;
4729 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4730 v = PyInt_FromLong(si.si_nlocals++);
4731 if (v == NULL)
4732 goto fail;
4733 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4734 goto fail;
4735 Py_DECREF(v);
4736 if (ste->ste_type != TYPE_CLASS)
4737 if (PyList_Append(c->c_varnames, name) < 0)
4738 goto fail;
4739 } else if (is_free(flags)) {
4740 if (ste->ste_nested) {
4741 v = PyInt_FromLong(si.si_nfrees++);
4742 if (v == NULL)
4743 goto fail;
4744 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4745 goto fail;
4746 Py_DECREF(v);
4747 } else {
4748 si.si_nimplicit++;
4749 if (PyDict_SetItem(c->c_globals, name,
4750 implicit) < 0)
4751 goto fail;
4752 if (st->st_nscopes != 1) {
4753 v = PyInt_FromLong(flags);
4754 if (PyDict_SetItem(st->st_global,
4755 name, v))
4756 goto fail;
4757 Py_DECREF(v);
4763 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4765 if (si.si_ncells > 1) { /* one cell is always in order */
4766 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4767 c->c_varnames, c->c_flags) < 0)
4768 return -1;
4770 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4771 return -1;
4772 return symtable_update_flags(c, ste, &si);
4773 fail:
4774 /* is this always the right thing to do? */
4775 Py_XDECREF(v);
4776 return -1;
4779 static struct symtable *
4780 symtable_init()
4782 struct symtable *st;
4784 st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable));
4785 if (st == NULL)
4786 return NULL;
4787 st->st_pass = 1;
4789 st->st_filename = NULL;
4790 if ((st->st_stack = PyList_New(0)) == NULL)
4791 goto fail;
4792 if ((st->st_symbols = PyDict_New()) == NULL)
4793 goto fail;
4794 st->st_cur = NULL;
4795 st->st_nscopes = 0;
4796 st->st_errors = 0;
4797 st->st_tmpname = 0;
4798 st->st_private = NULL;
4799 return st;
4800 fail:
4801 PySymtable_Free(st);
4802 return NULL;
4805 void
4806 PySymtable_Free(struct symtable *st)
4808 Py_XDECREF(st->st_symbols);
4809 Py_XDECREF(st->st_stack);
4810 Py_XDECREF(st->st_cur);
4811 PyObject_FREE((void *)st);
4814 /* When the compiler exits a scope, it must should update the scope's
4815 free variable information with the list of free variables in its
4816 children.
4818 Variables that are free in children and defined in the current
4819 scope are cellvars.
4821 If the scope being exited is defined at the top-level (ste_nested is
4822 false), free variables in children that are not defined here are
4823 implicit globals.
4827 static int
4828 symtable_update_free_vars(struct symtable *st)
4830 int i, j, def;
4831 PyObject *o, *name, *list = NULL;
4832 PySymtableEntryObject *child, *ste = st->st_cur;
4834 if (ste->ste_type == TYPE_CLASS)
4835 def = DEF_FREE_CLASS;
4836 else
4837 def = DEF_FREE;
4838 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4839 int pos = 0;
4841 if (list)
4842 PyList_SetSlice(list, 0,
4843 ((PyVarObject*)list)->ob_size, 0);
4844 child = (PySymtableEntryObject *)
4845 PyList_GET_ITEM(ste->ste_children, i);
4846 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4847 int flags = PyInt_AS_LONG(o);
4848 if (!(is_free(flags)))
4849 continue; /* avoids indentation */
4850 if (list == NULL) {
4851 list = PyList_New(0);
4852 if (list == NULL)
4853 return -1;
4855 ste->ste_child_free = 1;
4856 if (PyList_Append(list, name) < 0) {
4857 Py_DECREF(list);
4858 return -1;
4861 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4862 PyObject *v;
4863 name = PyList_GET_ITEM(list, j);
4864 v = PyDict_GetItem(ste->ste_symbols, name);
4865 /* If a name N is declared global in scope A and
4866 referenced in scope B contained (perhaps
4867 indirectly) in A and there are no scopes
4868 with bindings for N between B and A, then N
4869 is global in B. Unless A is a class scope,
4870 because class scopes are not considered for
4871 nested scopes.
4873 if (v && (ste->ste_type != TYPE_CLASS)) {
4874 int flags = PyInt_AS_LONG(v);
4875 if (flags & DEF_GLOBAL) {
4876 symtable_undo_free(st, child->ste_id,
4877 name);
4878 continue;
4881 if (ste->ste_nested) {
4882 if (symtable_add_def_o(st, ste->ste_symbols,
4883 name, def) < 0) {
4884 Py_DECREF(list);
4885 return -1;
4887 } else {
4888 if (symtable_check_global(st, child->ste_id,
4889 name) < 0) {
4890 Py_DECREF(list);
4891 return -1;
4897 Py_XDECREF(list);
4898 return 0;
4901 /* If the current scope is a non-nested class or if name is not
4902 defined in the current, non-nested scope, then it is an implicit
4903 global in all nested scopes.
4906 static int
4907 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4909 PyObject *o;
4910 int v;
4911 PySymtableEntryObject *ste = st->st_cur;
4913 if (ste->ste_type == TYPE_CLASS)
4914 return symtable_undo_free(st, child, name);
4915 o = PyDict_GetItem(ste->ste_symbols, name);
4916 if (o == NULL)
4917 return symtable_undo_free(st, child, name);
4918 v = PyInt_AS_LONG(o);
4920 if (is_free(v) || (v & DEF_GLOBAL))
4921 return symtable_undo_free(st, child, name);
4922 else
4923 return symtable_add_def_o(st, ste->ste_symbols,
4924 name, DEF_FREE);
4927 static int
4928 symtable_undo_free(struct symtable *st, PyObject *id,
4929 PyObject *name)
4931 int i, v, x;
4932 PyObject *info;
4933 PySymtableEntryObject *ste;
4935 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4936 if (ste == NULL)
4937 return -1;
4939 info = PyDict_GetItem(ste->ste_symbols, name);
4940 if (info == NULL)
4941 return 0;
4942 v = PyInt_AS_LONG(info);
4943 if (is_free(v)) {
4944 if (symtable_add_def_o(st, ste->ste_symbols, name,
4945 DEF_FREE_GLOBAL) < 0)
4946 return -1;
4947 } else
4948 /* If the name is defined here or declared global,
4949 then the recursion stops. */
4950 return 0;
4952 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4953 PySymtableEntryObject *child;
4954 child = (PySymtableEntryObject *)
4955 PyList_GET_ITEM(ste->ste_children, i);
4956 x = symtable_undo_free(st, child->ste_id, name);
4957 if (x < 0)
4958 return x;
4960 return 0;
4963 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4964 This reference is released when the scope is exited, via the DECREF
4965 in symtable_exit_scope().
4968 static int
4969 symtable_exit_scope(struct symtable *st)
4971 int end;
4973 if (st->st_pass == 1)
4974 symtable_update_free_vars(st);
4975 Py_DECREF(st->st_cur);
4976 end = PyList_GET_SIZE(st->st_stack) - 1;
4977 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4978 end);
4979 if (PySequence_DelItem(st->st_stack, end) < 0)
4980 return -1;
4981 return 0;
4984 static void
4985 symtable_enter_scope(struct symtable *st, char *name, int type,
4986 int lineno)
4988 PySymtableEntryObject *prev = NULL;
4990 if (st->st_cur) {
4991 prev = st->st_cur;
4992 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4993 Py_DECREF(st->st_cur);
4994 st->st_errors++;
4995 return;
4998 st->st_cur = (PySymtableEntryObject *)
4999 PySymtableEntry_New(st, name, type, lineno);
5000 if (strcmp(name, TOP) == 0)
5001 st->st_global = st->st_cur->ste_symbols;
5002 if (prev && st->st_pass == 1) {
5003 if (PyList_Append(prev->ste_children,
5004 (PyObject *)st->st_cur) < 0)
5005 st->st_errors++;
5009 static int
5010 symtable_lookup(struct symtable *st, char *name)
5012 char buffer[MANGLE_LEN];
5013 PyObject *v;
5014 int flags;
5016 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
5017 name = buffer;
5018 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
5019 if (v == NULL) {
5020 if (PyErr_Occurred())
5021 return -1;
5022 else
5023 return 0;
5026 flags = PyInt_AS_LONG(v);
5027 return flags;
5030 static int
5031 symtable_add_def(struct symtable *st, char *name, int flag)
5033 PyObject *s;
5034 char buffer[MANGLE_LEN];
5035 int ret;
5037 /* Warn about None, except inside a tuple (where the assignment
5038 code already issues a warning). */
5039 if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) &&
5040 *name == 'N' && strcmp(name, "None") == 0)
5042 if (symtable_warn(st, "argument named None"))
5043 return -1;
5045 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
5046 name = buffer;
5047 if ((s = PyString_InternFromString(name)) == NULL)
5048 return -1;
5049 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
5050 Py_DECREF(s);
5051 return ret;
5054 /* Must only be called with mangled names */
5056 static int
5057 symtable_add_def_o(struct symtable *st, PyObject *dict,
5058 PyObject *name, int flag)
5060 PyObject *o;
5061 int val;
5063 if ((o = PyDict_GetItem(dict, name))) {
5064 val = PyInt_AS_LONG(o);
5065 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
5066 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
5067 PyString_AsString(name));
5068 PyErr_SyntaxLocation(st->st_filename,
5069 st->st_cur->ste_lineno);
5070 return -1;
5072 val |= flag;
5073 } else
5074 val = flag;
5075 o = PyInt_FromLong(val);
5076 if (PyDict_SetItem(dict, name, o) < 0) {
5077 Py_DECREF(o);
5078 return -1;
5080 Py_DECREF(o);
5082 if (flag & DEF_PARAM) {
5083 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
5084 return -1;
5085 } else if (flag & DEF_GLOBAL) {
5086 /* XXX need to update DEF_GLOBAL for other flags too;
5087 perhaps only DEF_FREE_GLOBAL */
5088 if ((o = PyDict_GetItem(st->st_global, name))) {
5089 val = PyInt_AS_LONG(o);
5090 val |= flag;
5091 } else
5092 val = flag;
5093 o = PyInt_FromLong(val);
5094 if (PyDict_SetItem(st->st_global, name, o) < 0) {
5095 Py_DECREF(o);
5096 return -1;
5098 Py_DECREF(o);
5100 return 0;
5103 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
5105 /* Look for a yield stmt under n. Return 1 if found, else 0.
5106 This hack is used to look inside "if 0:" blocks (which are normally
5107 ignored) in case those are the only places a yield occurs (so that this
5108 function is a generator). */
5109 static int
5110 look_for_yield(node *n)
5112 int i;
5114 for (i = 0; i < NCH(n); ++i) {
5115 node *kid = CHILD(n, i);
5117 switch (TYPE(kid)) {
5119 case classdef:
5120 case funcdef:
5121 case lambdef:
5122 /* Stuff in nested functions and classes can't make
5123 the parent a generator. */
5124 return 0;
5126 case yield_stmt:
5127 return 1;
5129 default:
5130 if (look_for_yield(kid))
5131 return 1;
5134 return 0;
5137 static void
5138 symtable_node(struct symtable *st, node *n)
5140 int i;
5142 loop:
5143 switch (TYPE(n)) {
5144 case funcdef: {
5145 char *func_name = STR(CHILD(n, 1));
5146 symtable_add_def(st, func_name, DEF_LOCAL);
5147 symtable_default_args(st, CHILD(n, 2));
5148 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
5149 symtable_funcdef(st, n);
5150 symtable_exit_scope(st);
5151 break;
5153 case lambdef:
5154 if (NCH(n) == 4)
5155 symtable_default_args(st, CHILD(n, 1));
5156 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
5157 symtable_funcdef(st, n);
5158 symtable_exit_scope(st);
5159 break;
5160 case classdef: {
5161 char *tmp, *class_name = STR(CHILD(n, 1));
5162 symtable_add_def(st, class_name, DEF_LOCAL);
5163 if (TYPE(CHILD(n, 2)) == LPAR) {
5164 node *bases = CHILD(n, 3);
5165 int i;
5166 for (i = 0; i < NCH(bases); i += 2) {
5167 symtable_node(st, CHILD(bases, i));
5170 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
5171 tmp = st->st_private;
5172 st->st_private = class_name;
5173 symtable_node(st, CHILD(n, NCH(n) - 1));
5174 st->st_private = tmp;
5175 symtable_exit_scope(st);
5176 break;
5178 case if_stmt:
5179 for (i = 0; i + 3 < NCH(n); i += 4) {
5180 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5181 if (st->st_cur->ste_generator == 0)
5182 st->st_cur->ste_generator =
5183 look_for_yield(CHILD(n, i+3));
5184 continue;
5186 symtable_node(st, CHILD(n, i + 1));
5187 symtable_node(st, CHILD(n, i + 3));
5189 if (i + 2 < NCH(n))
5190 symtable_node(st, CHILD(n, i + 2));
5191 break;
5192 case global_stmt:
5193 symtable_global(st, n);
5194 break;
5195 case import_stmt:
5196 symtable_import(st, n);
5197 break;
5198 case exec_stmt: {
5199 st->st_cur->ste_optimized |= OPT_EXEC;
5200 symtable_node(st, CHILD(n, 1));
5201 if (NCH(n) > 2)
5202 symtable_node(st, CHILD(n, 3));
5203 else {
5204 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
5205 st->st_cur->ste_opt_lineno = n->n_lineno;
5207 if (NCH(n) > 4)
5208 symtable_node(st, CHILD(n, 5));
5209 break;
5212 case assert_stmt:
5213 if (Py_OptimizeFlag)
5214 return;
5215 if (NCH(n) == 2) {
5216 n = CHILD(n, 1);
5217 goto loop;
5218 } else {
5219 symtable_node(st, CHILD(n, 1));
5220 n = CHILD(n, 3);
5221 goto loop;
5223 case except_clause:
5224 if (NCH(n) == 4)
5225 symtable_assign(st, CHILD(n, 3), 0);
5226 if (NCH(n) > 1) {
5227 n = CHILD(n, 1);
5228 goto loop;
5230 break;
5231 case del_stmt:
5232 symtable_assign(st, CHILD(n, 1), 0);
5233 break;
5234 case yield_stmt:
5235 st->st_cur->ste_generator = 1;
5236 n = CHILD(n, 1);
5237 goto loop;
5238 case expr_stmt:
5239 if (NCH(n) == 1)
5240 n = CHILD(n, 0);
5241 else {
5242 if (TYPE(CHILD(n, 1)) == augassign) {
5243 symtable_assign(st, CHILD(n, 0), 0);
5244 symtable_node(st, CHILD(n, 2));
5245 break;
5246 } else {
5247 int i;
5248 for (i = 0; i < NCH(n) - 2; i += 2)
5249 symtable_assign(st, CHILD(n, i), 0);
5250 n = CHILD(n, NCH(n) - 1);
5253 goto loop;
5254 case list_iter:
5255 n = CHILD(n, 0);
5256 if (TYPE(n) == list_for) {
5257 st->st_tmpname++;
5258 symtable_list_comprehension(st, n);
5259 st->st_tmpname--;
5260 } else {
5261 REQ(n, list_if);
5262 symtable_node(st, CHILD(n, 1));
5263 if (NCH(n) == 3) {
5264 n = CHILD(n, 2);
5265 goto loop;
5268 break;
5269 case for_stmt:
5270 symtable_assign(st, CHILD(n, 1), 0);
5271 for (i = 3; i < NCH(n); ++i)
5272 if (TYPE(CHILD(n, i)) >= single_input)
5273 symtable_node(st, CHILD(n, i));
5274 break;
5275 /* The remaining cases fall through to default except in
5276 special circumstances. This requires the individual cases
5277 to be coded with great care, even though they look like
5278 rather innocuous. Each case must double-check TYPE(n).
5280 case argument:
5281 if (TYPE(n) == argument && NCH(n) == 3) {
5282 n = CHILD(n, 2);
5283 goto loop;
5285 /* fall through */
5286 case listmaker:
5287 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5288 st->st_tmpname++;
5289 symtable_list_comprehension(st, CHILD(n, 1));
5290 symtable_node(st, CHILD(n, 0));
5291 st->st_tmpname--;
5292 break;
5294 /* fall through */
5295 case atom:
5296 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5297 symtable_add_use(st, STR(CHILD(n, 0)));
5298 break;
5300 /* fall through */
5301 default:
5302 /* Walk over every non-token child with a special case
5303 for one child.
5305 if (NCH(n) == 1) {
5306 n = CHILD(n, 0);
5307 goto loop;
5309 for (i = 0; i < NCH(n); ++i)
5310 if (TYPE(CHILD(n, i)) >= single_input)
5311 symtable_node(st, CHILD(n, i));
5315 static void
5316 symtable_funcdef(struct symtable *st, node *n)
5318 node *body;
5320 if (TYPE(n) == lambdef) {
5321 if (NCH(n) == 4)
5322 symtable_params(st, CHILD(n, 1));
5323 } else
5324 symtable_params(st, CHILD(n, 2));
5325 body = CHILD(n, NCH(n) - 1);
5326 symtable_node(st, body);
5329 /* The next two functions parse the argument tuple.
5330 symtable_default_args() checks for names in the default arguments,
5331 which are references in the defining scope. symtable_params()
5332 parses the parameter names, which are defined in the function's
5333 body.
5335 varargslist:
5336 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5337 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5340 static void
5341 symtable_default_args(struct symtable *st, node *n)
5343 node *c;
5344 int i;
5346 if (TYPE(n) == parameters) {
5347 n = CHILD(n, 1);
5348 if (TYPE(n) == RPAR)
5349 return;
5351 REQ(n, varargslist);
5352 for (i = 0; i < NCH(n); i += 2) {
5353 c = CHILD(n, i);
5354 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5355 break;
5357 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5358 symtable_node(st, CHILD(n, i));
5362 static void
5363 symtable_params(struct symtable *st, node *n)
5365 int i, complex = -1, ext = 0;
5366 node *c = NULL;
5368 if (TYPE(n) == parameters) {
5369 n = CHILD(n, 1);
5370 if (TYPE(n) == RPAR)
5371 return;
5373 REQ(n, varargslist);
5374 for (i = 0; i < NCH(n); i += 2) {
5375 c = CHILD(n, i);
5376 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5377 ext = 1;
5378 break;
5380 if (TYPE(c) == test) {
5381 continue;
5383 if (TYPE(CHILD(c, 0)) == NAME)
5384 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5385 else {
5386 char nbuf[30];
5387 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
5388 symtable_add_def(st, nbuf, DEF_PARAM);
5389 complex = i;
5392 if (ext) {
5393 c = CHILD(n, i);
5394 if (TYPE(c) == STAR) {
5395 i++;
5396 symtable_add_def(st, STR(CHILD(n, i)),
5397 DEF_PARAM | DEF_STAR);
5398 i += 2;
5399 if (i >= NCH(n))
5400 c = NULL;
5401 else
5402 c = CHILD(n, i);
5404 if (c && TYPE(c) == DOUBLESTAR) {
5405 i++;
5406 symtable_add_def(st, STR(CHILD(n, i)),
5407 DEF_PARAM | DEF_DOUBLESTAR);
5410 if (complex >= 0) {
5411 int j;
5412 for (j = 0; j <= complex; j++) {
5413 c = CHILD(n, j);
5414 if (TYPE(c) == COMMA)
5415 c = CHILD(n, ++j);
5416 else if (TYPE(c) == EQUAL)
5417 c = CHILD(n, j += 3);
5418 if (TYPE(CHILD(c, 0)) == LPAR)
5419 symtable_params_fplist(st, CHILD(c, 1));
5424 static void
5425 symtable_params_fplist(struct symtable *st, node *n)
5427 int i;
5428 node *c;
5430 REQ(n, fplist);
5431 for (i = 0; i < NCH(n); i += 2) {
5432 c = CHILD(n, i);
5433 REQ(c, fpdef);
5434 if (NCH(c) == 1)
5435 symtable_add_def(st, STR(CHILD(c, 0)),
5436 DEF_PARAM | DEF_INTUPLE);
5437 else
5438 symtable_params_fplist(st, CHILD(c, 1));
5443 static void
5444 symtable_global(struct symtable *st, node *n)
5446 int i;
5448 /* XXX It might be helpful to warn about module-level global
5449 statements, but it's hard to tell the difference between
5450 module-level and a string passed to exec.
5453 for (i = 1; i < NCH(n); i += 2) {
5454 char *name = STR(CHILD(n, i));
5455 int flags;
5457 flags = symtable_lookup(st, name);
5458 if (flags < 0)
5459 continue;
5460 if (flags && flags != DEF_GLOBAL) {
5461 char buf[500];
5462 if (flags & DEF_PARAM) {
5463 PyErr_Format(PyExc_SyntaxError,
5464 "name '%.400s' is local and global",
5465 name);
5466 PyErr_SyntaxLocation(st->st_filename,
5467 st->st_cur->ste_lineno);
5468 st->st_errors++;
5469 return;
5471 else {
5472 if (flags & DEF_LOCAL)
5473 PyOS_snprintf(buf, sizeof(buf),
5474 GLOBAL_AFTER_ASSIGN,
5475 name);
5476 else
5477 PyOS_snprintf(buf, sizeof(buf),
5478 GLOBAL_AFTER_USE, name);
5479 symtable_warn(st, buf);
5482 symtable_add_def(st, name, DEF_GLOBAL);
5486 static void
5487 symtable_list_comprehension(struct symtable *st, node *n)
5489 char tmpname[30];
5491 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
5492 symtable_add_def(st, tmpname, DEF_LOCAL);
5493 symtable_assign(st, CHILD(n, 1), 0);
5494 symtable_node(st, CHILD(n, 3));
5495 if (NCH(n) == 5)
5496 symtable_node(st, CHILD(n, 4));
5499 static void
5500 symtable_import(struct symtable *st, node *n)
5502 int i;
5503 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5504 | 'from' dotted_name 'import'
5505 ('*' | import_as_name (',' import_as_name)*)
5506 import_as_name: NAME [NAME NAME]
5508 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5509 node *dotname = CHILD(n, 1);
5510 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5511 /* check for bogus imports */
5512 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5513 PyErr_SetString(PyExc_SyntaxError,
5514 LATE_FUTURE);
5515 PyErr_SyntaxLocation(st->st_filename,
5516 n->n_lineno);
5517 st->st_errors++;
5518 return;
5521 if (TYPE(CHILD(n, 3)) == STAR) {
5522 if (st->st_cur->ste_type != TYPE_MODULE) {
5523 if (symtable_warn(st,
5524 "import * only allowed at module level") < 0)
5525 return;
5527 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5528 st->st_cur->ste_opt_lineno = n->n_lineno;
5529 } else {
5530 for (i = 3; i < NCH(n); i += 2) {
5531 node *c = CHILD(n, i);
5532 if (NCH(c) > 1) /* import as */
5533 symtable_assign(st, CHILD(c, 2),
5534 DEF_IMPORT);
5535 else
5536 symtable_assign(st, CHILD(c, 0),
5537 DEF_IMPORT);
5540 } else {
5541 for (i = 1; i < NCH(n); i += 2) {
5542 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5547 /* The third argument to symatble_assign() is a flag to be passed to
5548 symtable_add_def() if it is eventually called. The flag is useful
5549 to specify the particular type of assignment that should be
5550 recorded, e.g. an assignment caused by import.
5553 static void
5554 symtable_assign(struct symtable *st, node *n, int def_flag)
5556 node *tmp;
5557 int i;
5559 loop:
5560 switch (TYPE(n)) {
5561 case lambdef:
5562 /* invalid assignment, e.g. lambda x:x=2. The next
5563 pass will catch this error. */
5564 return;
5565 case power:
5566 if (NCH(n) > 2) {
5567 for (i = 2; i < NCH(n); ++i)
5568 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5569 symtable_node(st, CHILD(n, i));
5571 if (NCH(n) > 1) {
5572 symtable_node(st, CHILD(n, 0));
5573 symtable_node(st, CHILD(n, 1));
5574 } else {
5575 n = CHILD(n, 0);
5576 goto loop;
5578 return;
5579 case listmaker:
5580 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5581 /* XXX This is an error, but the next pass
5582 will catch it. */
5583 return;
5584 } else {
5585 for (i = 0; i < NCH(n); i += 2)
5586 symtable_assign(st, CHILD(n, i), def_flag);
5588 return;
5589 case exprlist:
5590 case testlist:
5591 case testlist1:
5592 if (NCH(n) == 1) {
5593 n = CHILD(n, 0);
5594 goto loop;
5596 else {
5597 int i;
5598 for (i = 0; i < NCH(n); i += 2)
5599 symtable_assign(st, CHILD(n, i), def_flag);
5600 return;
5602 case atom:
5603 tmp = CHILD(n, 0);
5604 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5605 n = CHILD(n, 1);
5606 goto loop;
5607 } else if (TYPE(tmp) == NAME) {
5608 if (strcmp(STR(tmp), "__debug__") == 0) {
5609 PyErr_SetString(PyExc_SyntaxError,
5610 ASSIGN_DEBUG);
5611 PyErr_SyntaxLocation(st->st_filename,
5612 n->n_lineno);
5613 st->st_errors++;
5615 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
5617 return;
5618 case dotted_as_name:
5619 if (NCH(n) == 3)
5620 symtable_add_def(st, STR(CHILD(n, 2)),
5621 DEF_LOCAL | def_flag);
5622 else
5623 symtable_add_def(st,
5624 STR(CHILD(CHILD(n,
5625 0), 0)),
5626 DEF_LOCAL | def_flag);
5627 return;
5628 case dotted_name:
5629 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
5630 return;
5631 case NAME:
5632 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
5633 return;
5634 default:
5635 if (NCH(n) == 0)
5636 return;
5637 if (NCH(n) == 1) {
5638 n = CHILD(n, 0);
5639 goto loop;
5641 /* Should only occur for errors like x + 1 = 1,
5642 which will be caught in the next pass. */
5643 for (i = 0; i < NCH(n); ++i)
5644 if (TYPE(CHILD(n, i)) >= single_input)
5645 symtable_assign(st, CHILD(n, i), def_flag);