Updated for 2.1b2 distribution.
[python/dscho.git] / Python / compile.c
blobdf514c776d95ce72fef10d011b456772b3199a37
1 /* Compile an expression node to intermediate code */
3 /* XXX TO DO:
4 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
6 XXX Generate simple jump for break/return outside 'try...finally'
7 XXX Allow 'continue' inside finally clause of try-finally
8 XXX New opcode for loading the initial index for a for loop
9 XXX other JAR tricks?
12 #include "Python.h"
14 #include "node.h"
15 #include "token.h"
16 #include "graminit.h"
17 #include "compile.h"
18 #include "symtable.h"
19 #include "opcode.h"
20 #include "structmember.h"
22 #include <ctype.h>
24 /* Three symbols from graminit.h are also defined in Python.h, with
25 Py_ prefixes to their names. Python.h can't include graminit.h
26 (which defines too many confusing symbols), but we can check here
27 that they haven't changed (which is very unlikely, but possible). */
28 #if Py_single_input != single_input
29 #error "single_input has changed -- update Py_single_input in Python.h"
30 #endif
31 #if Py_file_input != file_input
32 #error "file_input has changed -- update Py_file_input in Python.h"
33 #endif
34 #if Py_eval_input != eval_input
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
36 #endif
38 int Py_OptimizeFlag = 0;
40 #define OP_DELETE 0
41 #define OP_ASSIGN 1
42 #define OP_APPLY 2
44 #define VAR_LOAD 0
45 #define VAR_STORE 1
46 #define VAR_DELETE 2
48 #define DEL_CLOSURE_ERROR \
49 "can not delete variable '%.400s' referenced in nested scope"
51 #define DUPLICATE_ARGUMENT \
52 "duplicate argument '%s' in function definition"
54 #define ILLEGAL_DYNAMIC_SCOPE \
55 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
57 #define GLOBAL_AFTER_ASSIGN \
58 "name '%.400s' is assigned to before global declaration"
60 #define GLOBAL_AFTER_USE \
61 "name '%.400s' is used prior to global declaration"
63 #define LOCAL_GLOBAL \
64 "name '%.400s' is a function paramter and declared global"
66 #define LATE_FUTURE \
67 "from __future__ imports must occur at the beginning of the file"
69 #define ASSIGN_DEBUG \
70 "can not assign to __debug__"
72 #define MANGLE_LEN 256
74 #define OFF(x) offsetof(PyCodeObject, x)
76 static struct memberlist code_memberlist[] = {
77 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
79 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
80 {"co_flags", T_INT, OFF(co_flags), READONLY},
81 {"co_code", T_OBJECT, OFF(co_code), READONLY},
82 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
83 {"co_names", T_OBJECT, OFF(co_names), READONLY},
84 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
85 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
87 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
88 {"co_name", T_OBJECT, OFF(co_name), READONLY},
89 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
91 {NULL} /* Sentinel */
94 static PyObject *
95 code_getattr(PyCodeObject *co, char *name)
97 return PyMember_Get((char *)co, code_memberlist, name);
100 static void
101 code_dealloc(PyCodeObject *co)
103 Py_XDECREF(co->co_code);
104 Py_XDECREF(co->co_consts);
105 Py_XDECREF(co->co_names);
106 Py_XDECREF(co->co_varnames);
107 Py_XDECREF(co->co_freevars);
108 Py_XDECREF(co->co_cellvars);
109 Py_XDECREF(co->co_filename);
110 Py_XDECREF(co->co_name);
111 Py_XDECREF(co->co_lnotab);
112 PyObject_DEL(co);
115 static PyObject *
116 code_repr(PyCodeObject *co)
118 char buf[500];
119 int lineno = -1;
120 char *filename = "???";
121 char *name = "???";
123 if (co->co_firstlineno != 0)
124 lineno = co->co_firstlineno;
125 if (co->co_filename && PyString_Check(co->co_filename))
126 filename = PyString_AS_STRING(co->co_filename);
127 if (co->co_name && PyString_Check(co->co_name))
128 name = PyString_AS_STRING(co->co_name);
129 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
130 name, co, filename, lineno);
131 return PyString_FromString(buf);
134 static int
135 code_compare(PyCodeObject *co, PyCodeObject *cp)
137 int cmp;
138 cmp = PyObject_Compare(co->co_name, cp->co_name);
139 if (cmp) return cmp;
140 cmp = co->co_argcount - cp->co_argcount;
141 if (cmp) return cmp;
142 cmp = co->co_nlocals - cp->co_nlocals;
143 if (cmp) return cmp;
144 cmp = co->co_flags - cp->co_flags;
145 if (cmp) return cmp;
146 cmp = PyObject_Compare(co->co_code, cp->co_code);
147 if (cmp) return cmp;
148 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
149 if (cmp) return cmp;
150 cmp = PyObject_Compare(co->co_names, cp->co_names);
151 if (cmp) return cmp;
152 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
153 if (cmp) return cmp;
154 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
155 if (cmp) return cmp;
156 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
157 return cmp;
160 static long
161 code_hash(PyCodeObject *co)
163 long h, h0, h1, h2, h3, h4, h5, h6;
164 h0 = PyObject_Hash(co->co_name);
165 if (h0 == -1) return -1;
166 h1 = PyObject_Hash(co->co_code);
167 if (h1 == -1) return -1;
168 h2 = PyObject_Hash(co->co_consts);
169 if (h2 == -1) return -1;
170 h3 = PyObject_Hash(co->co_names);
171 if (h3 == -1) return -1;
172 h4 = PyObject_Hash(co->co_varnames);
173 if (h4 == -1) return -1;
174 h5 = PyObject_Hash(co->co_freevars);
175 if (h5 == -1) return -1;
176 h6 = PyObject_Hash(co->co_cellvars);
177 if (h6 == -1) return -1;
178 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
179 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
180 if (h == -1) h = -2;
181 return h;
184 /* XXX code objects need to participate in GC? */
186 PyTypeObject PyCode_Type = {
187 PyObject_HEAD_INIT(&PyType_Type)
189 "code",
190 sizeof(PyCodeObject),
192 (destructor)code_dealloc, /*tp_dealloc*/
193 0, /*tp_print*/
194 (getattrfunc)code_getattr, /*tp_getattr*/
195 0, /*tp_setattr*/
196 (cmpfunc)code_compare, /*tp_compare*/
197 (reprfunc)code_repr, /*tp_repr*/
198 0, /*tp_as_number*/
199 0, /*tp_as_sequence*/
200 0, /*tp_as_mapping*/
201 (hashfunc)code_hash, /*tp_hash*/
204 #define NAME_CHARS \
205 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
207 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
209 static int
210 all_name_chars(unsigned char *s)
212 static char ok_name_char[256];
213 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
215 if (ok_name_char[*name_chars] == 0) {
216 unsigned char *p;
217 for (p = name_chars; *p; p++)
218 ok_name_char[*p] = 1;
220 while (*s) {
221 if (ok_name_char[*s++] == 0)
222 return 0;
224 return 1;
227 static int
228 intern_strings(PyObject *tuple)
230 int i;
232 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
233 PyObject *v = PyTuple_GET_ITEM(tuple, i);
234 if (v == NULL || !PyString_Check(v)) {
235 Py_FatalError("non-string found in code slot");
236 PyErr_BadInternalCall();
237 return -1;
239 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
241 return 0;
244 PyCodeObject *
245 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
246 PyObject *code, PyObject *consts, PyObject *names,
247 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
248 PyObject *filename, PyObject *name, int firstlineno,
249 PyObject *lnotab)
251 PyCodeObject *co;
252 int i;
253 PyBufferProcs *pb;
254 /* Check argument types */
255 if (argcount < 0 || nlocals < 0 ||
256 code == NULL ||
257 consts == NULL || !PyTuple_Check(consts) ||
258 names == NULL || !PyTuple_Check(names) ||
259 varnames == NULL || !PyTuple_Check(varnames) ||
260 freevars == NULL || !PyTuple_Check(freevars) ||
261 cellvars == NULL || !PyTuple_Check(cellvars) ||
262 name == NULL || !PyString_Check(name) ||
263 filename == NULL || !PyString_Check(filename) ||
264 lnotab == NULL || !PyString_Check(lnotab)) {
265 PyErr_BadInternalCall();
266 return NULL;
268 pb = code->ob_type->tp_as_buffer;
269 if (pb == NULL ||
270 pb->bf_getreadbuffer == NULL ||
271 pb->bf_getsegcount == NULL ||
272 (*pb->bf_getsegcount)(code, NULL) != 1)
274 PyErr_BadInternalCall();
275 return NULL;
277 intern_strings(names);
278 intern_strings(varnames);
279 if (freevars == NULL)
280 freevars = PyTuple_New(0);
281 intern_strings(freevars);
282 if (cellvars == NULL)
283 cellvars = PyTuple_New(0);
284 intern_strings(cellvars);
285 /* Intern selected string constants */
286 for (i = PyTuple_Size(consts); --i >= 0; ) {
287 PyObject *v = PyTuple_GetItem(consts, i);
288 if (!PyString_Check(v))
289 continue;
290 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
291 continue;
292 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
294 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
295 if (co != NULL) {
296 co->co_argcount = argcount;
297 co->co_nlocals = nlocals;
298 co->co_stacksize = stacksize;
299 co->co_flags = flags;
300 Py_INCREF(code);
301 co->co_code = code;
302 Py_INCREF(consts);
303 co->co_consts = consts;
304 Py_INCREF(names);
305 co->co_names = names;
306 Py_INCREF(varnames);
307 co->co_varnames = varnames;
308 Py_INCREF(freevars);
309 co->co_freevars = freevars;
310 Py_INCREF(cellvars);
311 co->co_cellvars = cellvars;
312 Py_INCREF(filename);
313 co->co_filename = filename;
314 Py_INCREF(name);
315 co->co_name = name;
316 co->co_firstlineno = firstlineno;
317 Py_INCREF(lnotab);
318 co->co_lnotab = lnotab;
320 return co;
324 /* Data structure used internally */
326 /* The compiler uses two passes to generate bytecodes. The first pass
327 builds the symbol table. The second pass generates the bytecode.
329 The first pass uses a single symtable struct. The second pass uses
330 a compiling struct for each code block. The compiling structs
331 share a reference to the symtable.
333 The two passes communicate via symtable_load_symbols() and via
334 is_local() and is_global(). The former initializes several slots
335 in the compiling struct: c_varnames, c_locals, c_nlocals,
336 c_argcount, c_globals, and c_flags.
339 struct compiling {
340 PyObject *c_code; /* string */
341 PyObject *c_consts; /* list of objects */
342 PyObject *c_const_dict; /* inverse of c_consts */
343 PyObject *c_names; /* list of strings (names) */
344 PyObject *c_name_dict; /* inverse of c_names */
345 PyObject *c_globals; /* dictionary (value=None) */
346 PyObject *c_locals; /* dictionary (value=localID) */
347 PyObject *c_varnames; /* list (inverse of c_locals) */
348 PyObject *c_freevars; /* dictionary (value=None) */
349 PyObject *c_cellvars; /* list */
350 int c_nlocals; /* index of next local */
351 int c_argcount; /* number of top-level arguments */
352 int c_flags; /* same as co_flags */
353 int c_nexti; /* index into c_code */
354 int c_errors; /* counts errors occurred */
355 int c_infunction; /* set when compiling a function */
356 int c_interactive; /* generating code for interactive command */
357 int c_loops; /* counts nested loops */
358 int c_begin; /* begin of current loop, for 'continue' */
359 int c_block[CO_MAXBLOCKS]; /* stack of block types */
360 int c_nblocks; /* current block stack level */
361 char *c_filename; /* filename of current node */
362 char *c_name; /* name of object (e.g. function) */
363 int c_lineno; /* Current line number */
364 int c_stacklevel; /* Current stack level */
365 int c_maxstacklevel; /* Maximum stack level */
366 int c_firstlineno;
367 PyObject *c_lnotab; /* Table mapping address to line number */
368 int c_last_addr, c_last_line, c_lnotab_next;
369 char *c_private; /* for private name mangling */
370 int c_tmpname; /* temporary local name counter */
371 int c_nested; /* Is block nested funcdef or lamdef? */
372 int c_closure; /* Is nested w/freevars? */
373 struct symtable *c_symtable; /* pointer to module symbol table */
374 PyFutureFeatures *c_future; /* pointer to module's __future__ */
377 int is_free(int v)
379 if ((v & (USE | DEF_FREE))
380 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
381 return 1;
382 if (v & DEF_FREE_CLASS)
383 return 1;
384 return 0;
387 static void
388 com_error(struct compiling *c, PyObject *exc, char *msg)
390 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
392 if (c == NULL) {
393 /* Error occurred via symtable call to
394 is_constant_false */
395 PyErr_SetString(exc, msg);
396 return;
398 c->c_errors++;
399 if (c->c_lineno < 1 || c->c_interactive) {
400 /* Unknown line number or interactive input */
401 PyErr_SetString(exc, msg);
402 return;
404 v = PyString_FromString(msg);
405 if (v == NULL)
406 return; /* MemoryError, too bad */
408 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
409 if (line == NULL) {
410 Py_INCREF(Py_None);
411 line = Py_None;
413 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
414 Py_None, line);
415 if (t == NULL)
416 goto exit;
417 w = Py_BuildValue("(OO)", v, t);
418 if (w == NULL)
419 goto exit;
420 PyErr_SetObject(exc, w);
421 exit:
422 Py_XDECREF(t);
423 Py_XDECREF(v);
424 Py_XDECREF(w);
425 Py_XDECREF(line);
428 /* Interface to the block stack */
430 static void
431 block_push(struct compiling *c, int type)
433 if (c->c_nblocks >= CO_MAXBLOCKS) {
434 com_error(c, PyExc_SystemError,
435 "too many statically nested blocks");
437 else {
438 c->c_block[c->c_nblocks++] = type;
442 static void
443 block_pop(struct compiling *c, int type)
445 if (c->c_nblocks > 0)
446 c->c_nblocks--;
447 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
448 com_error(c, PyExc_SystemError, "bad block pop");
452 /* Prototype forward declarations */
454 static int com_init(struct compiling *, char *);
455 static void com_free(struct compiling *);
456 static void com_push(struct compiling *, int);
457 static void com_pop(struct compiling *, int);
458 static void com_done(struct compiling *);
459 static void com_node(struct compiling *, node *);
460 static void com_factor(struct compiling *, node *);
461 static void com_addbyte(struct compiling *, int);
462 static void com_addint(struct compiling *, int);
463 static void com_addoparg(struct compiling *, int, int);
464 static void com_addfwref(struct compiling *, int, int *);
465 static void com_backpatch(struct compiling *, int);
466 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
467 static int com_addconst(struct compiling *, PyObject *);
468 static int com_addname(struct compiling *, PyObject *);
469 static void com_addopname(struct compiling *, int, node *);
470 static void com_list(struct compiling *, node *, int);
471 static void com_list_iter(struct compiling *, node *, node *, char *);
472 static int com_argdefs(struct compiling *, node *);
473 static void com_assign(struct compiling *, node *, int, node *);
474 static void com_assign_name(struct compiling *, node *, int);
475 static PyCodeObject *icompile(node *, struct compiling *);
476 static PyCodeObject *jcompile(node *, char *, struct compiling *,
477 PyCompilerFlags *);
478 static PyObject *parsestrplus(node *);
479 static PyObject *parsestr(char *);
480 static node *get_rawdocstring(node *);
482 static int get_ref_type(struct compiling *, char *);
484 /* symtable operations */
485 static int symtable_build(struct compiling *, node *);
486 static int symtable_load_symbols(struct compiling *);
487 static struct symtable *symtable_init(void);
488 static void symtable_enter_scope(struct symtable *, char *, int, int);
489 static int symtable_exit_scope(struct symtable *);
490 static int symtable_add_def(struct symtable *, char *, int);
491 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
493 static void symtable_node(struct symtable *, node *);
494 static void symtable_funcdef(struct symtable *, node *);
495 static void symtable_default_args(struct symtable *, node *);
496 static void symtable_params(struct symtable *, node *);
497 static void symtable_params_fplist(struct symtable *, node *n);
498 static void symtable_global(struct symtable *, node *);
499 static void symtable_import(struct symtable *, node *);
500 static void symtable_assign(struct symtable *, node *, int);
501 static void symtable_list_comprehension(struct symtable *, node *);
503 static int symtable_update_free_vars(struct symtable *);
504 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
505 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
507 /* helper */
508 static void
509 do_pad(int pad)
511 int i;
512 for (i = 0; i < pad; ++i)
513 fprintf(stderr, " ");
516 static void
517 dump(node *n, int pad, int depth)
519 int i;
520 if (depth == 0)
521 return;
522 do_pad(pad);
523 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
524 if (depth > 0)
525 depth--;
526 for (i = 0; i < NCH(n); ++i)
527 dump(CHILD(n, i), pad + 1, depth);
530 #define DUMP(N) dump(N, 0, -1)
532 static int
533 com_init(struct compiling *c, char *filename)
535 memset((void *)c, '\0', sizeof(struct compiling));
536 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
537 1000)) == NULL)
538 goto fail;
539 if ((c->c_consts = PyList_New(0)) == NULL)
540 goto fail;
541 if ((c->c_const_dict = PyDict_New()) == NULL)
542 goto fail;
543 if ((c->c_names = PyList_New(0)) == NULL)
544 goto fail;
545 if ((c->c_name_dict = PyDict_New()) == NULL)
546 goto fail;
547 if ((c->c_locals = PyDict_New()) == NULL)
548 goto fail;
549 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
550 1000)) == NULL)
551 goto fail;
552 c->c_globals = NULL;
553 c->c_varnames = NULL;
554 c->c_freevars = NULL;
555 c->c_cellvars = NULL;
556 c->c_nlocals = 0;
557 c->c_argcount = 0;
558 c->c_flags = 0;
559 c->c_nexti = 0;
560 c->c_errors = 0;
561 c->c_infunction = 0;
562 c->c_interactive = 0;
563 c->c_loops = 0;
564 c->c_begin = 0;
565 c->c_nblocks = 0;
566 c->c_filename = filename;
567 c->c_name = "?";
568 c->c_lineno = 0;
569 c->c_stacklevel = 0;
570 c->c_maxstacklevel = 0;
571 c->c_firstlineno = 0;
572 c->c_last_addr = 0;
573 c->c_last_line = 0;
574 c->c_lnotab_next = 0;
575 c->c_tmpname = 0;
576 c->c_nested = 0;
577 c->c_closure = 0;
578 c->c_symtable = NULL;
579 return 1;
581 fail:
582 com_free(c);
583 return 0;
586 static void
587 com_free(struct compiling *c)
589 Py_XDECREF(c->c_code);
590 Py_XDECREF(c->c_consts);
591 Py_XDECREF(c->c_const_dict);
592 Py_XDECREF(c->c_names);
593 Py_XDECREF(c->c_name_dict);
594 Py_XDECREF(c->c_globals);
595 Py_XDECREF(c->c_locals);
596 Py_XDECREF(c->c_varnames);
597 Py_XDECREF(c->c_freevars);
598 Py_XDECREF(c->c_cellvars);
599 Py_XDECREF(c->c_lnotab);
600 if (c->c_future)
601 PyMem_Free((void *)c->c_future);
604 static void
605 com_push(struct compiling *c, int n)
607 c->c_stacklevel += n;
608 if (c->c_stacklevel > c->c_maxstacklevel)
609 c->c_maxstacklevel = c->c_stacklevel;
612 static void
613 com_pop(struct compiling *c, int n)
615 if (c->c_stacklevel < n) {
616 /* fprintf(stderr,
617 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
618 c->c_filename, c->c_lineno,
619 c->c_nexti, c->c_stacklevel, n); */
620 c->c_stacklevel = 0;
622 else
623 c->c_stacklevel -= n;
626 static void
627 com_done(struct compiling *c)
629 if (c->c_code != NULL)
630 _PyString_Resize(&c->c_code, c->c_nexti);
631 if (c->c_lnotab != NULL)
632 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
635 static void
636 com_addbyte(struct compiling *c, int byte)
638 int len;
639 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
640 assert(byte >= 0 && byte <= 255);
641 if (byte < 0 || byte > 255) {
642 com_error(c, PyExc_SystemError,
643 "com_addbyte: byte out of range");
645 if (c->c_code == NULL)
646 return;
647 len = PyString_Size(c->c_code);
648 if (c->c_nexti >= len) {
649 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
650 c->c_errors++;
651 return;
654 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
657 static void
658 com_addint(struct compiling *c, int x)
660 com_addbyte(c, x & 0xff);
661 com_addbyte(c, x >> 8); /* XXX x should be positive */
664 static void
665 com_add_lnotab(struct compiling *c, int addr, int line)
667 int size;
668 char *p;
669 if (c->c_lnotab == NULL)
670 return;
671 size = PyString_Size(c->c_lnotab);
672 if (c->c_lnotab_next+2 > size) {
673 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
674 c->c_errors++;
675 return;
678 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
679 *p++ = addr;
680 *p++ = line;
681 c->c_lnotab_next += 2;
684 static void
685 com_set_lineno(struct compiling *c, int lineno)
687 c->c_lineno = lineno;
688 if (c->c_firstlineno == 0) {
689 c->c_firstlineno = c->c_last_line = lineno;
691 else {
692 int incr_addr = c->c_nexti - c->c_last_addr;
693 int incr_line = lineno - c->c_last_line;
694 while (incr_addr > 0 || incr_line > 0) {
695 int trunc_addr = incr_addr;
696 int trunc_line = incr_line;
697 if (trunc_addr > 255)
698 trunc_addr = 255;
699 if (trunc_line > 255)
700 trunc_line = 255;
701 com_add_lnotab(c, trunc_addr, trunc_line);
702 incr_addr -= trunc_addr;
703 incr_line -= trunc_line;
705 c->c_last_addr = c->c_nexti;
706 c->c_last_line = lineno;
710 static void
711 com_addoparg(struct compiling *c, int op, int arg)
713 int extended_arg = arg >> 16;
714 if (op == SET_LINENO) {
715 com_set_lineno(c, arg);
716 if (Py_OptimizeFlag)
717 return;
719 if (extended_arg){
720 com_addbyte(c, EXTENDED_ARG);
721 com_addint(c, extended_arg);
722 arg &= 0xffff;
724 com_addbyte(c, op);
725 com_addint(c, arg);
728 static void
729 com_addfwref(struct compiling *c, int op, int *p_anchor)
731 /* Compile a forward reference for backpatching */
732 int here;
733 int anchor;
734 com_addbyte(c, op);
735 here = c->c_nexti;
736 anchor = *p_anchor;
737 *p_anchor = here;
738 com_addint(c, anchor == 0 ? 0 : here - anchor);
741 static void
742 com_backpatch(struct compiling *c, int anchor)
744 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
745 int target = c->c_nexti;
746 int dist;
747 int prev;
748 for (;;) {
749 /* Make the JUMP instruction at anchor point to target */
750 prev = code[anchor] + (code[anchor+1] << 8);
751 dist = target - (anchor+2);
752 code[anchor] = dist & 0xff;
753 dist >>= 8;
754 code[anchor+1] = dist;
755 dist >>= 8;
756 if (dist) {
757 com_error(c, PyExc_SystemError,
758 "com_backpatch: offset too large");
759 break;
761 if (!prev)
762 break;
763 anchor -= prev;
767 /* Handle literals and names uniformly */
769 static int
770 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
772 PyObject *w, *t, *np=NULL;
773 long n;
775 t = Py_BuildValue("(OO)", v, v->ob_type);
776 if (t == NULL)
777 goto fail;
778 w = PyDict_GetItem(dict, t);
779 if (w != NULL) {
780 n = PyInt_AsLong(w);
781 } else {
782 n = PyList_Size(list);
783 np = PyInt_FromLong(n);
784 if (np == NULL)
785 goto fail;
786 if (PyList_Append(list, v) != 0)
787 goto fail;
788 if (PyDict_SetItem(dict, t, np) != 0)
789 goto fail;
790 Py_DECREF(np);
792 Py_DECREF(t);
793 return n;
794 fail:
795 Py_XDECREF(np);
796 Py_XDECREF(t);
797 c->c_errors++;
798 return 0;
801 static int
802 com_addconst(struct compiling *c, PyObject *v)
804 return com_add(c, c->c_consts, c->c_const_dict, v);
807 static int
808 com_addname(struct compiling *c, PyObject *v)
810 return com_add(c, c->c_names, c->c_name_dict, v);
813 static int
814 mangle(char *p, char *name, char *buffer, size_t maxlen)
816 /* Name mangling: __private becomes _classname__private.
817 This is independent from how the name is used. */
818 size_t nlen, plen;
819 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
820 return 0;
821 nlen = strlen(name);
822 if (nlen+2 >= maxlen)
823 return 0; /* Don't mangle __extremely_long_names */
824 if (name[nlen-1] == '_' && name[nlen-2] == '_')
825 return 0; /* Don't mangle __whatever__ */
826 /* Strip leading underscores from class name */
827 while (*p == '_')
828 p++;
829 if (*p == '\0')
830 return 0; /* Don't mangle if class is just underscores */
831 plen = strlen(p);
832 if (plen + nlen >= maxlen)
833 plen = maxlen-nlen-2; /* Truncate class name if too long */
834 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
835 buffer[0] = '_';
836 strncpy(buffer+1, p, plen);
837 strcpy(buffer+1+plen, name);
838 return 1;
841 static void
842 com_addop_name(struct compiling *c, int op, char *name)
844 PyObject *v;
845 int i;
846 char buffer[MANGLE_LEN];
848 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
849 name = buffer;
850 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
851 c->c_errors++;
852 i = 255;
854 else {
855 i = com_addname(c, v);
856 Py_DECREF(v);
858 com_addoparg(c, op, i);
861 #define NAME_LOCAL 0
862 #define NAME_GLOBAL 1
863 #define NAME_DEFAULT 2
864 #define NAME_CLOSURE 3
866 static int
867 com_lookup_arg(PyObject *dict, PyObject *name)
869 PyObject *v = PyDict_GetItem(dict, name);
870 if (v == NULL)
871 return -1;
872 else
873 return PyInt_AS_LONG(v);
876 static void
877 com_addop_varname(struct compiling *c, int kind, char *name)
879 PyObject *v;
880 int i, reftype;
881 int scope = NAME_DEFAULT;
882 int op = STOP_CODE;
883 char buffer[MANGLE_LEN];
885 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
886 name = buffer;
887 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
888 c->c_errors++;
889 i = 255;
890 goto done;
893 reftype = get_ref_type(c, name);
894 switch (reftype) {
895 case LOCAL:
896 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
897 scope = NAME_LOCAL;
898 break;
899 case GLOBAL_EXPLICIT:
900 scope = NAME_GLOBAL;
901 break;
902 case GLOBAL_IMPLICIT:
903 if (c->c_flags & CO_OPTIMIZED)
904 scope = NAME_GLOBAL;
905 break;
906 case FREE:
907 case CELL:
908 scope = NAME_CLOSURE;
909 break;
912 i = com_addname(c, v);
913 if (scope == NAME_LOCAL)
914 i = com_lookup_arg(c->c_locals, v);
915 else if (reftype == FREE)
916 i = com_lookup_arg(c->c_freevars, v);
917 else if (reftype == CELL)
918 i = com_lookup_arg(c->c_cellvars, v);
919 if (i == -1) {
920 c->c_errors++; /* XXX no exception set */
921 i = 255;
922 goto done;
924 Py_DECREF(v);
926 switch (kind) {
927 case VAR_LOAD:
928 switch (scope) {
929 case NAME_LOCAL:
930 op = LOAD_FAST;
931 break;
932 case NAME_GLOBAL:
933 op = LOAD_GLOBAL;
934 break;
935 case NAME_DEFAULT:
936 op = LOAD_NAME;
937 break;
938 case NAME_CLOSURE:
939 op = LOAD_DEREF;
940 break;
942 break;
943 case VAR_STORE:
944 switch (scope) {
945 case NAME_LOCAL:
946 op = STORE_FAST;
947 break;
948 case NAME_GLOBAL:
949 op = STORE_GLOBAL;
950 break;
951 case NAME_DEFAULT:
952 op = STORE_NAME;
953 break;
954 case NAME_CLOSURE:
955 op = STORE_DEREF;
956 break;
958 break;
959 case VAR_DELETE:
960 switch (scope) {
961 case NAME_LOCAL:
962 op = DELETE_FAST;
963 break;
964 case NAME_GLOBAL:
965 op = DELETE_GLOBAL;
966 break;
967 case NAME_DEFAULT:
968 op = DELETE_NAME;
969 break;
970 case NAME_CLOSURE: {
971 char buf[500];
972 sprintf(buf, DEL_CLOSURE_ERROR, name);
973 com_error(c, PyExc_SyntaxError, buf);
974 i = 255;
975 break;
978 break;
980 done:
981 com_addoparg(c, op, i);
984 static void
985 com_addopname(struct compiling *c, int op, node *n)
987 char *name;
988 char buffer[1000];
989 /* XXX it is possible to write this code without the 1000
990 chars on the total length of dotted names, I just can't be
991 bothered right now */
992 if (TYPE(n) == STAR)
993 name = "*";
994 else if (TYPE(n) == dotted_name) {
995 char *p = buffer;
996 int i;
997 name = buffer;
998 for (i = 0; i < NCH(n); i += 2) {
999 char *s = STR(CHILD(n, i));
1000 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
1001 com_error(c, PyExc_MemoryError,
1002 "dotted_name too long");
1003 name = NULL;
1004 break;
1006 if (p != buffer)
1007 *p++ = '.';
1008 strcpy(p, s);
1009 p = strchr(p, '\0');
1012 else {
1013 REQ(n, NAME);
1014 name = STR(n);
1016 com_addop_name(c, op, name);
1019 static PyObject *
1020 parsenumber(struct compiling *co, char *s)
1022 char *end;
1023 long x;
1024 double dx;
1025 #ifndef WITHOUT_COMPLEX
1026 Py_complex c;
1027 int imflag;
1028 #endif
1030 errno = 0;
1031 end = s + strlen(s) - 1;
1032 #ifndef WITHOUT_COMPLEX
1033 imflag = *end == 'j' || *end == 'J';
1034 #endif
1035 if (*end == 'l' || *end == 'L')
1036 return PyLong_FromString(s, (char **)0, 0);
1037 if (s[0] == '0')
1038 x = (long) PyOS_strtoul(s, &end, 0);
1039 else
1040 x = PyOS_strtol(s, &end, 0);
1041 if (*end == '\0') {
1042 if (errno != 0) {
1043 com_error(co, PyExc_OverflowError,
1044 "integer literal too large");
1045 return NULL;
1047 return PyInt_FromLong(x);
1049 /* XXX Huge floats may silently fail */
1050 #ifndef WITHOUT_COMPLEX
1051 if (imflag) {
1052 c.real = 0.;
1053 PyFPE_START_PROTECT("atof", return 0)
1054 c.imag = atof(s);
1055 PyFPE_END_PROTECT(c)
1056 return PyComplex_FromCComplex(c);
1058 else
1059 #endif
1061 PyFPE_START_PROTECT("atof", return 0)
1062 dx = atof(s);
1063 PyFPE_END_PROTECT(dx)
1064 return PyFloat_FromDouble(dx);
1068 static PyObject *
1069 parsestr(char *s)
1071 PyObject *v;
1072 size_t len;
1073 char *buf;
1074 char *p;
1075 char *end;
1076 int c;
1077 int first = *s;
1078 int quote = first;
1079 int rawmode = 0;
1080 int unicode = 0;
1081 if (isalpha(quote) || quote == '_') {
1082 if (quote == 'u' || quote == 'U') {
1083 quote = *++s;
1084 unicode = 1;
1086 if (quote == 'r' || quote == 'R') {
1087 quote = *++s;
1088 rawmode = 1;
1091 if (quote != '\'' && quote != '\"') {
1092 PyErr_BadInternalCall();
1093 return NULL;
1095 s++;
1096 len = strlen(s);
1097 if (len > INT_MAX) {
1098 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1099 return NULL;
1101 if (s[--len] != quote) {
1102 PyErr_BadInternalCall();
1103 return NULL;
1105 if (len >= 4 && s[0] == quote && s[1] == quote) {
1106 s += 2;
1107 len -= 2;
1108 if (s[--len] != quote || s[--len] != quote) {
1109 PyErr_BadInternalCall();
1110 return NULL;
1113 if (unicode || Py_UnicodeFlag) {
1114 if (rawmode)
1115 return PyUnicode_DecodeRawUnicodeEscape(
1116 s, len, NULL);
1117 else
1118 return PyUnicode_DecodeUnicodeEscape(
1119 s, len, NULL);
1121 if (rawmode || strchr(s, '\\') == NULL)
1122 return PyString_FromStringAndSize(s, len);
1123 v = PyString_FromStringAndSize((char *)NULL, len);
1124 if (v == NULL)
1125 return NULL;
1126 p = buf = PyString_AsString(v);
1127 end = s + len;
1128 while (s < end) {
1129 if (*s != '\\') {
1130 *p++ = *s++;
1131 continue;
1133 s++;
1134 switch (*s++) {
1135 /* XXX This assumes ASCII! */
1136 case '\n': break;
1137 case '\\': *p++ = '\\'; break;
1138 case '\'': *p++ = '\''; break;
1139 case '\"': *p++ = '\"'; break;
1140 case 'b': *p++ = '\b'; break;
1141 case 'f': *p++ = '\014'; break; /* FF */
1142 case 't': *p++ = '\t'; break;
1143 case 'n': *p++ = '\n'; break;
1144 case 'r': *p++ = '\r'; break;
1145 case 'v': *p++ = '\013'; break; /* VT */
1146 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1147 case '0': case '1': case '2': case '3':
1148 case '4': case '5': case '6': case '7':
1149 c = s[-1] - '0';
1150 if ('0' <= *s && *s <= '7') {
1151 c = (c<<3) + *s++ - '0';
1152 if ('0' <= *s && *s <= '7')
1153 c = (c<<3) + *s++ - '0';
1155 *p++ = c;
1156 break;
1157 case 'x':
1158 if (isxdigit(Py_CHARMASK(s[0]))
1159 && isxdigit(Py_CHARMASK(s[1]))) {
1160 unsigned int x = 0;
1161 c = Py_CHARMASK(*s);
1162 s++;
1163 if (isdigit(c))
1164 x = c - '0';
1165 else if (islower(c))
1166 x = 10 + c - 'a';
1167 else
1168 x = 10 + c - 'A';
1169 x = x << 4;
1170 c = Py_CHARMASK(*s);
1171 s++;
1172 if (isdigit(c))
1173 x += c - '0';
1174 else if (islower(c))
1175 x += 10 + c - 'a';
1176 else
1177 x += 10 + c - 'A';
1178 *p++ = x;
1179 break;
1181 PyErr_SetString(PyExc_ValueError,
1182 "invalid \\x escape");
1183 Py_DECREF(v);
1184 return NULL;
1185 default:
1186 *p++ = '\\';
1187 *p++ = s[-1];
1188 break;
1191 _PyString_Resize(&v, (int)(p - buf));
1192 return v;
1195 static PyObject *
1196 parsestrplus(node *n)
1198 PyObject *v;
1199 int i;
1200 REQ(CHILD(n, 0), STRING);
1201 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
1202 /* String literal concatenation */
1203 for (i = 1; i < NCH(n); i++) {
1204 PyObject *s;
1205 s = parsestr(STR(CHILD(n, i)));
1206 if (s == NULL)
1207 goto onError;
1208 if (PyString_Check(v) && PyString_Check(s)) {
1209 PyString_ConcatAndDel(&v, s);
1210 if (v == NULL)
1211 goto onError;
1213 else {
1214 PyObject *temp;
1215 temp = PyUnicode_Concat(v, s);
1216 Py_DECREF(s);
1217 if (temp == NULL)
1218 goto onError;
1219 Py_DECREF(v);
1220 v = temp;
1224 return v;
1226 onError:
1227 Py_XDECREF(v);
1228 return NULL;
1231 static void
1232 com_list_for(struct compiling *c, node *n, node *e, char *t)
1234 PyObject *v;
1235 int anchor = 0;
1236 int save_begin = c->c_begin;
1238 /* list_iter: for v in expr [list_iter] */
1239 com_node(c, CHILD(n, 3)); /* expr */
1240 v = PyInt_FromLong(0L);
1241 if (v == NULL)
1242 c->c_errors++;
1243 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1244 com_push(c, 1);
1245 Py_XDECREF(v);
1246 c->c_begin = c->c_nexti;
1247 com_addoparg(c, SET_LINENO, n->n_lineno);
1248 com_addfwref(c, FOR_LOOP, &anchor);
1249 com_push(c, 1);
1250 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1251 c->c_loops++;
1252 com_list_iter(c, n, e, t);
1253 c->c_loops--;
1254 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1255 c->c_begin = save_begin;
1256 com_backpatch(c, anchor);
1257 com_pop(c, 2); /* FOR_LOOP has popped these */
1260 static void
1261 com_list_if(struct compiling *c, node *n, node *e, char *t)
1263 int anchor = 0;
1264 int a = 0;
1265 /* list_iter: 'if' test [list_iter] */
1266 com_addoparg(c, SET_LINENO, n->n_lineno);
1267 com_node(c, CHILD(n, 1));
1268 com_addfwref(c, JUMP_IF_FALSE, &a);
1269 com_addbyte(c, POP_TOP);
1270 com_pop(c, 1);
1271 com_list_iter(c, n, e, t);
1272 com_addfwref(c, JUMP_FORWARD, &anchor);
1273 com_backpatch(c, a);
1274 /* We jump here with an extra entry which we now pop */
1275 com_addbyte(c, POP_TOP);
1276 com_backpatch(c, anchor);
1279 static void
1280 com_list_iter(struct compiling *c,
1281 node *p, /* parent of list_iter node */
1282 node *e, /* element expression node */
1283 char *t /* name of result list temp local */)
1285 /* list_iter is the last child in a listmaker, list_for, or list_if */
1286 node *n = CHILD(p, NCH(p)-1);
1287 if (TYPE(n) == list_iter) {
1288 n = CHILD(n, 0);
1289 switch (TYPE(n)) {
1290 case list_for:
1291 com_list_for(c, n, e, t);
1292 break;
1293 case list_if:
1294 com_list_if(c, n, e, t);
1295 break;
1296 default:
1297 com_error(c, PyExc_SystemError,
1298 "invalid list_iter node type");
1301 else {
1302 com_addop_varname(c, VAR_LOAD, t);
1303 com_push(c, 1);
1304 com_node(c, e);
1305 com_addoparg(c, CALL_FUNCTION, 1);
1306 com_addbyte(c, POP_TOP);
1307 com_pop(c, 2);
1311 static void
1312 com_list_comprehension(struct compiling *c, node *n)
1314 /* listmaker: test list_for */
1315 char tmpname[12];
1316 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
1317 com_addoparg(c, BUILD_LIST, 0);
1318 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1319 com_push(c, 2);
1320 com_addop_name(c, LOAD_ATTR, "append");
1321 com_addop_varname(c, VAR_STORE, tmpname);
1322 com_pop(c, 1);
1323 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1324 com_addop_varname(c, VAR_DELETE, tmpname);
1325 --c->c_tmpname;
1328 static void
1329 com_listmaker(struct compiling *c, node *n)
1331 /* listmaker: test ( list_for | (',' test)* [','] ) */
1332 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1333 com_list_comprehension(c, n);
1334 else {
1335 int len = 0;
1336 int i;
1337 for (i = 0; i < NCH(n); i += 2, len++)
1338 com_node(c, CHILD(n, i));
1339 com_addoparg(c, BUILD_LIST, len);
1340 com_pop(c, len-1);
1344 static void
1345 com_dictmaker(struct compiling *c, node *n)
1347 int i;
1348 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1349 for (i = 0; i+2 < NCH(n); i += 4) {
1350 /* We must arrange things just right for STORE_SUBSCR.
1351 It wants the stack to look like (value) (dict) (key) */
1352 com_addbyte(c, DUP_TOP);
1353 com_push(c, 1);
1354 com_node(c, CHILD(n, i+2)); /* value */
1355 com_addbyte(c, ROT_TWO);
1356 com_node(c, CHILD(n, i)); /* key */
1357 com_addbyte(c, STORE_SUBSCR);
1358 com_pop(c, 3);
1362 static void
1363 com_atom(struct compiling *c, node *n)
1365 node *ch;
1366 PyObject *v;
1367 int i;
1368 REQ(n, atom);
1369 ch = CHILD(n, 0);
1370 switch (TYPE(ch)) {
1371 case LPAR:
1372 if (TYPE(CHILD(n, 1)) == RPAR) {
1373 com_addoparg(c, BUILD_TUPLE, 0);
1374 com_push(c, 1);
1376 else
1377 com_node(c, CHILD(n, 1));
1378 break;
1379 case LSQB: /* '[' [listmaker] ']' */
1380 if (TYPE(CHILD(n, 1)) == RSQB) {
1381 com_addoparg(c, BUILD_LIST, 0);
1382 com_push(c, 1);
1384 else
1385 com_listmaker(c, CHILD(n, 1));
1386 break;
1387 case LBRACE: /* '{' [dictmaker] '}' */
1388 com_addoparg(c, BUILD_MAP, 0);
1389 com_push(c, 1);
1390 if (TYPE(CHILD(n, 1)) == dictmaker)
1391 com_dictmaker(c, CHILD(n, 1));
1392 break;
1393 case BACKQUOTE:
1394 com_node(c, CHILD(n, 1));
1395 com_addbyte(c, UNARY_CONVERT);
1396 break;
1397 case NUMBER:
1398 if ((v = parsenumber(c, STR(ch))) == NULL) {
1399 i = 255;
1401 else {
1402 i = com_addconst(c, v);
1403 Py_DECREF(v);
1405 com_addoparg(c, LOAD_CONST, i);
1406 com_push(c, 1);
1407 break;
1408 case STRING:
1409 v = parsestrplus(n);
1410 if (v == NULL) {
1411 c->c_errors++;
1412 i = 255;
1414 else {
1415 i = com_addconst(c, v);
1416 Py_DECREF(v);
1418 com_addoparg(c, LOAD_CONST, i);
1419 com_push(c, 1);
1420 break;
1421 case NAME:
1422 com_addop_varname(c, VAR_LOAD, STR(ch));
1423 com_push(c, 1);
1424 break;
1425 default:
1426 com_error(c, PyExc_SystemError,
1427 "com_atom: unexpected node type");
1431 static void
1432 com_slice(struct compiling *c, node *n, int op)
1434 if (NCH(n) == 1) {
1435 com_addbyte(c, op);
1437 else if (NCH(n) == 2) {
1438 if (TYPE(CHILD(n, 0)) != COLON) {
1439 com_node(c, CHILD(n, 0));
1440 com_addbyte(c, op+1);
1442 else {
1443 com_node(c, CHILD(n, 1));
1444 com_addbyte(c, op+2);
1446 com_pop(c, 1);
1448 else {
1449 com_node(c, CHILD(n, 0));
1450 com_node(c, CHILD(n, 2));
1451 com_addbyte(c, op+3);
1452 com_pop(c, 2);
1456 static void
1457 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1459 if (NCH(n) == 1) {
1460 com_addbyte(c, DUP_TOP);
1461 com_push(c, 1);
1462 com_addbyte(c, SLICE);
1463 com_node(c, augn);
1464 com_addbyte(c, opcode);
1465 com_pop(c, 1);
1466 com_addbyte(c, ROT_TWO);
1467 com_addbyte(c, STORE_SLICE);
1468 com_pop(c, 2);
1469 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1470 com_node(c, CHILD(n, 0));
1471 com_addoparg(c, DUP_TOPX, 2);
1472 com_push(c, 2);
1473 com_addbyte(c, SLICE+1);
1474 com_pop(c, 1);
1475 com_node(c, augn);
1476 com_addbyte(c, opcode);
1477 com_pop(c, 1);
1478 com_addbyte(c, ROT_THREE);
1479 com_addbyte(c, STORE_SLICE+1);
1480 com_pop(c, 3);
1481 } else if (NCH(n) == 2) {
1482 com_node(c, CHILD(n, 1));
1483 com_addoparg(c, DUP_TOPX, 2);
1484 com_push(c, 2);
1485 com_addbyte(c, SLICE+2);
1486 com_pop(c, 1);
1487 com_node(c, augn);
1488 com_addbyte(c, opcode);
1489 com_pop(c, 1);
1490 com_addbyte(c, ROT_THREE);
1491 com_addbyte(c, STORE_SLICE+2);
1492 com_pop(c, 3);
1493 } else {
1494 com_node(c, CHILD(n, 0));
1495 com_node(c, CHILD(n, 2));
1496 com_addoparg(c, DUP_TOPX, 3);
1497 com_push(c, 3);
1498 com_addbyte(c, SLICE+3);
1499 com_pop(c, 2);
1500 com_node(c, augn);
1501 com_addbyte(c, opcode);
1502 com_pop(c, 1);
1503 com_addbyte(c, ROT_FOUR);
1504 com_addbyte(c, STORE_SLICE+3);
1505 com_pop(c, 4);
1509 static void
1510 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1512 node *m;
1513 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1514 if (NCH(n) == 1) {
1515 if (*pkeywords != NULL) {
1516 com_error(c, PyExc_SyntaxError,
1517 "non-keyword arg after keyword arg");
1519 else {
1520 com_node(c, CHILD(n, 0));
1522 return;
1524 m = n;
1525 do {
1526 m = CHILD(m, 0);
1527 } while (NCH(m) == 1);
1528 if (TYPE(m) != NAME) {
1529 /* f(lambda x: x[0] = 3) ends up getting parsed with
1530 * LHS test = lambda x: x[0], and RHS test = 3.
1531 * SF bug 132313 points out that complaining about a keyword
1532 * then is very confusing.
1534 com_error(c, PyExc_SyntaxError,
1535 TYPE(m) == lambdef ?
1536 "lambda cannot contain assignment" :
1537 "keyword can't be an expression");
1539 else {
1540 PyObject *v = PyString_InternFromString(STR(m));
1541 if (v != NULL && *pkeywords == NULL)
1542 *pkeywords = PyDict_New();
1543 if (v == NULL)
1544 c->c_errors++;
1545 else if (*pkeywords == NULL) {
1546 c->c_errors++;
1547 Py_DECREF(v);
1548 } else {
1549 if (PyDict_GetItem(*pkeywords, v) != NULL)
1550 com_error(c, PyExc_SyntaxError,
1551 "duplicate keyword argument");
1552 else
1553 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1554 c->c_errors++;
1555 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1556 com_push(c, 1);
1557 Py_DECREF(v);
1560 com_node(c, CHILD(n, 2));
1563 static void
1564 com_call_function(struct compiling *c, node *n)
1566 if (TYPE(n) == RPAR) {
1567 com_addoparg(c, CALL_FUNCTION, 0);
1569 else {
1570 PyObject *keywords = NULL;
1571 int i, na, nk;
1572 int lineno = n->n_lineno;
1573 int star_flag = 0;
1574 int starstar_flag = 0;
1575 int opcode;
1576 REQ(n, arglist);
1577 na = 0;
1578 nk = 0;
1579 for (i = 0; i < NCH(n); i += 2) {
1580 node *ch = CHILD(n, i);
1581 if (TYPE(ch) == STAR ||
1582 TYPE(ch) == DOUBLESTAR)
1583 break;
1584 if (ch->n_lineno != lineno) {
1585 lineno = ch->n_lineno;
1586 com_addoparg(c, SET_LINENO, lineno);
1588 com_argument(c, ch, &keywords);
1589 if (keywords == NULL)
1590 na++;
1591 else
1592 nk++;
1594 Py_XDECREF(keywords);
1595 while (i < NCH(n)) {
1596 node *tok = CHILD(n, i);
1597 node *ch = CHILD(n, i+1);
1598 i += 3;
1599 switch (TYPE(tok)) {
1600 case STAR: star_flag = 1; break;
1601 case DOUBLESTAR: starstar_flag = 1; break;
1603 com_node(c, ch);
1605 if (na > 255 || nk > 255) {
1606 com_error(c, PyExc_SyntaxError,
1607 "more than 255 arguments");
1609 if (star_flag || starstar_flag)
1610 opcode = CALL_FUNCTION_VAR - 1 +
1611 star_flag + (starstar_flag << 1);
1612 else
1613 opcode = CALL_FUNCTION;
1614 com_addoparg(c, opcode, na | (nk << 8));
1615 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1619 static void
1620 com_select_member(struct compiling *c, node *n)
1622 com_addopname(c, LOAD_ATTR, n);
1625 static void
1626 com_sliceobj(struct compiling *c, node *n)
1628 int i=0;
1629 int ns=2; /* number of slice arguments */
1630 node *ch;
1632 /* first argument */
1633 if (TYPE(CHILD(n,i)) == COLON) {
1634 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1635 com_push(c, 1);
1636 i++;
1638 else {
1639 com_node(c, CHILD(n,i));
1640 i++;
1641 REQ(CHILD(n,i),COLON);
1642 i++;
1644 /* second argument */
1645 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1646 com_node(c, CHILD(n,i));
1647 i++;
1649 else {
1650 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1651 com_push(c, 1);
1653 /* remaining arguments */
1654 for (; i < NCH(n); i++) {
1655 ns++;
1656 ch=CHILD(n,i);
1657 REQ(ch, sliceop);
1658 if (NCH(ch) == 1) {
1659 /* right argument of ':' missing */
1660 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1661 com_push(c, 1);
1663 else
1664 com_node(c, CHILD(ch,1));
1666 com_addoparg(c, BUILD_SLICE, ns);
1667 com_pop(c, 1 + (ns == 3));
1670 static void
1671 com_subscript(struct compiling *c, node *n)
1673 node *ch;
1674 REQ(n, subscript);
1675 ch = CHILD(n,0);
1676 /* check for rubber index */
1677 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1678 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1679 com_push(c, 1);
1681 else {
1682 /* check for slice */
1683 if ((TYPE(ch) == COLON || NCH(n) > 1))
1684 com_sliceobj(c, n);
1685 else {
1686 REQ(ch, test);
1687 com_node(c, ch);
1692 static void
1693 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1695 int i, op;
1696 REQ(n, subscriptlist);
1697 /* Check to make backward compatible slice behavior for '[i:j]' */
1698 if (NCH(n) == 1) {
1699 node *sub = CHILD(n, 0); /* subscript */
1700 /* 'Basic' slice, should have exactly one colon. */
1701 if ((TYPE(CHILD(sub, 0)) == COLON
1702 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1703 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1705 switch (assigning) {
1706 case OP_DELETE:
1707 op = DELETE_SLICE;
1708 break;
1709 case OP_ASSIGN:
1710 op = STORE_SLICE;
1711 break;
1712 case OP_APPLY:
1713 op = SLICE;
1714 break;
1715 default:
1716 com_augassign_slice(c, sub, assigning, augn);
1717 return;
1719 com_slice(c, sub, op);
1720 if (op == STORE_SLICE)
1721 com_pop(c, 2);
1722 else if (op == DELETE_SLICE)
1723 com_pop(c, 1);
1724 return;
1727 /* Else normal subscriptlist. Compile each subscript. */
1728 for (i = 0; i < NCH(n); i += 2)
1729 com_subscript(c, CHILD(n, i));
1730 /* Put multiple subscripts into a tuple */
1731 if (NCH(n) > 1) {
1732 i = (NCH(n)+1) / 2;
1733 com_addoparg(c, BUILD_TUPLE, i);
1734 com_pop(c, i-1);
1736 switch (assigning) {
1737 case OP_DELETE:
1738 op = DELETE_SUBSCR;
1739 i = 2;
1740 break;
1741 default:
1742 case OP_ASSIGN:
1743 op = STORE_SUBSCR;
1744 i = 3;
1745 break;
1746 case OP_APPLY:
1747 op = BINARY_SUBSCR;
1748 i = 1;
1749 break;
1751 if (assigning > OP_APPLY) {
1752 com_addoparg(c, DUP_TOPX, 2);
1753 com_push(c, 2);
1754 com_addbyte(c, BINARY_SUBSCR);
1755 com_pop(c, 1);
1756 com_node(c, augn);
1757 com_addbyte(c, assigning);
1758 com_pop(c, 1);
1759 com_addbyte(c, ROT_THREE);
1761 com_addbyte(c, op);
1762 com_pop(c, i);
1765 static void
1766 com_apply_trailer(struct compiling *c, node *n)
1768 REQ(n, trailer);
1769 switch (TYPE(CHILD(n, 0))) {
1770 case LPAR:
1771 com_call_function(c, CHILD(n, 1));
1772 break;
1773 case DOT:
1774 com_select_member(c, CHILD(n, 1));
1775 break;
1776 case LSQB:
1777 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1778 break;
1779 default:
1780 com_error(c, PyExc_SystemError,
1781 "com_apply_trailer: unknown trailer type");
1785 static void
1786 com_power(struct compiling *c, node *n)
1788 int i;
1789 REQ(n, power);
1790 com_atom(c, CHILD(n, 0));
1791 for (i = 1; i < NCH(n); i++) {
1792 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1793 com_factor(c, CHILD(n, i+1));
1794 com_addbyte(c, BINARY_POWER);
1795 com_pop(c, 1);
1796 break;
1798 else
1799 com_apply_trailer(c, CHILD(n, i));
1803 static void
1804 com_factor(struct compiling *c, node *n)
1806 REQ(n, factor);
1807 if (TYPE(CHILD(n, 0)) == PLUS) {
1808 com_factor(c, CHILD(n, 1));
1809 com_addbyte(c, UNARY_POSITIVE);
1811 else if (TYPE(CHILD(n, 0)) == MINUS) {
1812 com_factor(c, CHILD(n, 1));
1813 com_addbyte(c, UNARY_NEGATIVE);
1815 else if (TYPE(CHILD(n, 0)) == TILDE) {
1816 com_factor(c, CHILD(n, 1));
1817 com_addbyte(c, UNARY_INVERT);
1819 else {
1820 com_power(c, CHILD(n, 0));
1824 static void
1825 com_term(struct compiling *c, node *n)
1827 int i;
1828 int op;
1829 REQ(n, term);
1830 com_factor(c, CHILD(n, 0));
1831 for (i = 2; i < NCH(n); i += 2) {
1832 com_factor(c, CHILD(n, i));
1833 switch (TYPE(CHILD(n, i-1))) {
1834 case STAR:
1835 op = BINARY_MULTIPLY;
1836 break;
1837 case SLASH:
1838 op = BINARY_DIVIDE;
1839 break;
1840 case PERCENT:
1841 op = BINARY_MODULO;
1842 break;
1843 default:
1844 com_error(c, PyExc_SystemError,
1845 "com_term: operator not *, / or %");
1846 op = 255;
1848 com_addbyte(c, op);
1849 com_pop(c, 1);
1853 static void
1854 com_arith_expr(struct compiling *c, node *n)
1856 int i;
1857 int op;
1858 REQ(n, arith_expr);
1859 com_term(c, CHILD(n, 0));
1860 for (i = 2; i < NCH(n); i += 2) {
1861 com_term(c, CHILD(n, i));
1862 switch (TYPE(CHILD(n, i-1))) {
1863 case PLUS:
1864 op = BINARY_ADD;
1865 break;
1866 case MINUS:
1867 op = BINARY_SUBTRACT;
1868 break;
1869 default:
1870 com_error(c, PyExc_SystemError,
1871 "com_arith_expr: operator not + or -");
1872 op = 255;
1874 com_addbyte(c, op);
1875 com_pop(c, 1);
1879 static void
1880 com_shift_expr(struct compiling *c, node *n)
1882 int i;
1883 int op;
1884 REQ(n, shift_expr);
1885 com_arith_expr(c, CHILD(n, 0));
1886 for (i = 2; i < NCH(n); i += 2) {
1887 com_arith_expr(c, CHILD(n, i));
1888 switch (TYPE(CHILD(n, i-1))) {
1889 case LEFTSHIFT:
1890 op = BINARY_LSHIFT;
1891 break;
1892 case RIGHTSHIFT:
1893 op = BINARY_RSHIFT;
1894 break;
1895 default:
1896 com_error(c, PyExc_SystemError,
1897 "com_shift_expr: operator not << or >>");
1898 op = 255;
1900 com_addbyte(c, op);
1901 com_pop(c, 1);
1905 static void
1906 com_and_expr(struct compiling *c, node *n)
1908 int i;
1909 int op;
1910 REQ(n, and_expr);
1911 com_shift_expr(c, CHILD(n, 0));
1912 for (i = 2; i < NCH(n); i += 2) {
1913 com_shift_expr(c, CHILD(n, i));
1914 if (TYPE(CHILD(n, i-1)) == AMPER) {
1915 op = BINARY_AND;
1917 else {
1918 com_error(c, PyExc_SystemError,
1919 "com_and_expr: operator not &");
1920 op = 255;
1922 com_addbyte(c, op);
1923 com_pop(c, 1);
1927 static void
1928 com_xor_expr(struct compiling *c, node *n)
1930 int i;
1931 int op;
1932 REQ(n, xor_expr);
1933 com_and_expr(c, CHILD(n, 0));
1934 for (i = 2; i < NCH(n); i += 2) {
1935 com_and_expr(c, CHILD(n, i));
1936 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1937 op = BINARY_XOR;
1939 else {
1940 com_error(c, PyExc_SystemError,
1941 "com_xor_expr: operator not ^");
1942 op = 255;
1944 com_addbyte(c, op);
1945 com_pop(c, 1);
1949 static void
1950 com_expr(struct compiling *c, node *n)
1952 int i;
1953 int op;
1954 REQ(n, expr);
1955 com_xor_expr(c, CHILD(n, 0));
1956 for (i = 2; i < NCH(n); i += 2) {
1957 com_xor_expr(c, CHILD(n, i));
1958 if (TYPE(CHILD(n, i-1)) == VBAR) {
1959 op = BINARY_OR;
1961 else {
1962 com_error(c, PyExc_SystemError,
1963 "com_expr: expr operator not |");
1964 op = 255;
1966 com_addbyte(c, op);
1967 com_pop(c, 1);
1971 static enum cmp_op
1972 cmp_type(node *n)
1974 REQ(n, comp_op);
1975 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1976 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1977 if (NCH(n) == 1) {
1978 n = CHILD(n, 0);
1979 switch (TYPE(n)) {
1980 case LESS: return LT;
1981 case GREATER: return GT;
1982 case EQEQUAL: /* == */
1983 case EQUAL: return EQ;
1984 case LESSEQUAL: return LE;
1985 case GREATEREQUAL: return GE;
1986 case NOTEQUAL: return NE; /* <> or != */
1987 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1988 if (strcmp(STR(n), "is") == 0) return IS;
1991 else if (NCH(n) == 2) {
1992 switch (TYPE(CHILD(n, 0))) {
1993 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1994 return NOT_IN;
1995 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1996 return IS_NOT;
1999 return BAD;
2002 static void
2003 com_comparison(struct compiling *c, node *n)
2005 int i;
2006 enum cmp_op op;
2007 int anchor;
2008 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2009 com_expr(c, CHILD(n, 0));
2010 if (NCH(n) == 1)
2011 return;
2013 /****************************************************************
2014 The following code is generated for all but the last
2015 comparison in a chain:
2017 label: on stack: opcode: jump to:
2019 a <code to load b>
2020 a, b DUP_TOP
2021 a, b, b ROT_THREE
2022 b, a, b COMPARE_OP
2023 b, 0-or-1 JUMP_IF_FALSE L1
2024 b, 1 POP_TOP
2027 We are now ready to repeat this sequence for the next
2028 comparison in the chain.
2030 For the last we generate:
2032 b <code to load c>
2033 b, c COMPARE_OP
2034 0-or-1
2036 If there were any jumps to L1 (i.e., there was more than one
2037 comparison), we generate:
2039 0-or-1 JUMP_FORWARD L2
2040 L1: b, 0 ROT_TWO
2041 0, b POP_TOP
2043 L2: 0-or-1
2044 ****************************************************************/
2046 anchor = 0;
2048 for (i = 2; i < NCH(n); i += 2) {
2049 com_expr(c, CHILD(n, i));
2050 if (i+2 < NCH(n)) {
2051 com_addbyte(c, DUP_TOP);
2052 com_push(c, 1);
2053 com_addbyte(c, ROT_THREE);
2055 op = cmp_type(CHILD(n, i-1));
2056 if (op == BAD) {
2057 com_error(c, PyExc_SystemError,
2058 "com_comparison: unknown comparison op");
2060 com_addoparg(c, COMPARE_OP, op);
2061 com_pop(c, 1);
2062 if (i+2 < NCH(n)) {
2063 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2064 com_addbyte(c, POP_TOP);
2065 com_pop(c, 1);
2069 if (anchor) {
2070 int anchor2 = 0;
2071 com_addfwref(c, JUMP_FORWARD, &anchor2);
2072 com_backpatch(c, anchor);
2073 com_addbyte(c, ROT_TWO);
2074 com_addbyte(c, POP_TOP);
2075 com_backpatch(c, anchor2);
2079 static void
2080 com_not_test(struct compiling *c, node *n)
2082 REQ(n, not_test); /* 'not' not_test | comparison */
2083 if (NCH(n) == 1) {
2084 com_comparison(c, CHILD(n, 0));
2086 else {
2087 com_not_test(c, CHILD(n, 1));
2088 com_addbyte(c, UNARY_NOT);
2092 static void
2093 com_and_test(struct compiling *c, node *n)
2095 int i;
2096 int anchor;
2097 REQ(n, and_test); /* not_test ('and' not_test)* */
2098 anchor = 0;
2099 i = 0;
2100 for (;;) {
2101 com_not_test(c, CHILD(n, i));
2102 if ((i += 2) >= NCH(n))
2103 break;
2104 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2105 com_addbyte(c, POP_TOP);
2106 com_pop(c, 1);
2108 if (anchor)
2109 com_backpatch(c, anchor);
2112 static int
2113 com_make_closure(struct compiling *c, PyCodeObject *co)
2115 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2116 /* If the code is compiled with st->st_nested_scopes == 0,
2117 then no variable will ever be added to co_freevars.
2119 if (free == 0)
2120 return 0;
2121 for (i = 0; i < free; ++i) {
2122 /* Bypass com_addop_varname because it will generate
2123 LOAD_DEREF but LOAD_CLOSURE is needed.
2125 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2126 int arg, reftype;
2128 /* Special case: If a class contains a method with a
2129 free variable that has the same name as a method,
2130 the name will be considered free *and* local in the
2131 class. It should be handled by the closure, as
2132 well as by the normal name loookup logic.
2134 reftype = get_ref_type(c, PyString_AS_STRING(name));
2135 if (reftype == CELL)
2136 arg = com_lookup_arg(c->c_cellvars, name);
2137 else /* (reftype == FREE) */
2138 arg = com_lookup_arg(c->c_freevars, name);
2139 if (arg == -1) {
2140 fprintf(stderr, "lookup %s in %s %d %d\n"
2141 "freevars of %s: %s\n",
2142 PyObject_REPR(name),
2143 c->c_name,
2144 reftype, arg,
2145 PyString_AS_STRING(co->co_name),
2146 PyObject_REPR(co->co_freevars));
2147 Py_FatalError("com_make_closure()");
2149 com_addoparg(c, LOAD_CLOSURE, arg);
2152 com_push(c, free);
2153 return 1;
2156 static void
2157 com_test(struct compiling *c, node *n)
2159 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2160 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2161 PyObject *co;
2162 int i, closure;
2163 int ndefs = com_argdefs(c, CHILD(n, 0));
2164 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2165 n->n_lineno);
2166 co = (PyObject *) icompile(CHILD(n, 0), c);
2167 if (co == NULL) {
2168 c->c_errors++;
2169 return;
2171 symtable_exit_scope(c->c_symtable);
2172 i = com_addconst(c, co);
2173 closure = com_make_closure(c, (PyCodeObject *)co);
2174 Py_DECREF(co);
2175 com_addoparg(c, LOAD_CONST, i);
2176 com_push(c, 1);
2177 if (closure)
2178 com_addoparg(c, MAKE_CLOSURE, ndefs);
2179 else
2180 com_addoparg(c, MAKE_FUNCTION, ndefs);
2181 com_pop(c, ndefs);
2183 else {
2184 int anchor = 0;
2185 int i = 0;
2186 for (;;) {
2187 com_and_test(c, CHILD(n, i));
2188 if ((i += 2) >= NCH(n))
2189 break;
2190 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2191 com_addbyte(c, POP_TOP);
2192 com_pop(c, 1);
2194 if (anchor)
2195 com_backpatch(c, anchor);
2199 static void
2200 com_list(struct compiling *c, node *n, int toplevel)
2202 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2203 if (NCH(n) == 1 && !toplevel) {
2204 com_node(c, CHILD(n, 0));
2206 else {
2207 int i;
2208 int len;
2209 len = (NCH(n) + 1) / 2;
2210 for (i = 0; i < NCH(n); i += 2)
2211 com_node(c, CHILD(n, i));
2212 com_addoparg(c, BUILD_TUPLE, len);
2213 com_pop(c, len-1);
2218 /* Begin of assignment compilation */
2221 static void
2222 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2224 com_addbyte(c, DUP_TOP);
2225 com_push(c, 1);
2226 com_addopname(c, LOAD_ATTR, n);
2227 com_node(c, augn);
2228 com_addbyte(c, opcode);
2229 com_pop(c, 1);
2230 com_addbyte(c, ROT_TWO);
2231 com_addopname(c, STORE_ATTR, n);
2232 com_pop(c, 2);
2235 static void
2236 com_assign_attr(struct compiling *c, node *n, int assigning)
2238 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2239 com_pop(c, assigning ? 2 : 1);
2242 static void
2243 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2245 REQ(n, trailer);
2246 switch (TYPE(CHILD(n, 0))) {
2247 case LPAR: /* '(' [exprlist] ')' */
2248 com_error(c, PyExc_SyntaxError,
2249 "can't assign to function call");
2250 break;
2251 case DOT: /* '.' NAME */
2252 if (assigning > OP_APPLY)
2253 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2254 else
2255 com_assign_attr(c, CHILD(n, 1), assigning);
2256 break;
2257 case LSQB: /* '[' subscriptlist ']' */
2258 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2259 break;
2260 default:
2261 com_error(c, PyExc_SystemError, "unknown trailer type");
2265 static void
2266 com_assign_sequence(struct compiling *c, node *n, int assigning)
2268 int i;
2269 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2270 REQ(n, exprlist);
2271 if (assigning) {
2272 i = (NCH(n)+1)/2;
2273 com_addoparg(c, UNPACK_SEQUENCE, i);
2274 com_push(c, i-1);
2276 for (i = 0; i < NCH(n); i += 2)
2277 com_assign(c, CHILD(n, i), assigning, NULL);
2280 static void
2281 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2283 REQ(n, NAME);
2284 com_addop_varname(c, VAR_LOAD, STR(n));
2285 com_push(c, 1);
2286 com_node(c, augn);
2287 com_addbyte(c, opcode);
2288 com_pop(c, 1);
2289 com_assign_name(c, n, OP_ASSIGN);
2292 static void
2293 com_assign_name(struct compiling *c, node *n, int assigning)
2295 REQ(n, NAME);
2296 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2297 if (assigning)
2298 com_pop(c, 1);
2301 static void
2302 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2304 /* Loop to avoid trivial recursion */
2305 for (;;) {
2306 switch (TYPE(n)) {
2308 case exprlist:
2309 case testlist:
2310 if (NCH(n) > 1) {
2311 if (assigning > OP_APPLY) {
2312 com_error(c, PyExc_SyntaxError,
2313 "augmented assign to tuple not possible");
2314 return;
2316 com_assign_sequence(c, n, assigning);
2317 return;
2319 n = CHILD(n, 0);
2320 break;
2322 case test:
2323 case and_test:
2324 case not_test:
2325 case comparison:
2326 case expr:
2327 case xor_expr:
2328 case and_expr:
2329 case shift_expr:
2330 case arith_expr:
2331 case term:
2332 case factor:
2333 if (NCH(n) > 1) {
2334 com_error(c, PyExc_SyntaxError,
2335 "can't assign to operator");
2336 return;
2338 n = CHILD(n, 0);
2339 break;
2341 case power: /* atom trailer* ('**' power)*
2342 ('+'|'-'|'~') factor | atom trailer* */
2343 if (TYPE(CHILD(n, 0)) != atom) {
2344 com_error(c, PyExc_SyntaxError,
2345 "can't assign to operator");
2346 return;
2348 if (NCH(n) > 1) { /* trailer or exponent present */
2349 int i;
2350 com_node(c, CHILD(n, 0));
2351 for (i = 1; i+1 < NCH(n); i++) {
2352 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2353 com_error(c, PyExc_SyntaxError,
2354 "can't assign to operator");
2355 return;
2357 com_apply_trailer(c, CHILD(n, i));
2358 } /* NB i is still alive */
2359 com_assign_trailer(c,
2360 CHILD(n, i), assigning, augn);
2361 return;
2363 n = CHILD(n, 0);
2364 break;
2366 case atom:
2367 switch (TYPE(CHILD(n, 0))) {
2368 case LPAR:
2369 n = CHILD(n, 1);
2370 if (TYPE(n) == RPAR) {
2371 /* XXX Should allow () = () ??? */
2372 com_error(c, PyExc_SyntaxError,
2373 "can't assign to ()");
2374 return;
2376 if (assigning > OP_APPLY) {
2377 com_error(c, PyExc_SyntaxError,
2378 "augmented assign to tuple not possible");
2379 return;
2381 break;
2382 case LSQB:
2383 n = CHILD(n, 1);
2384 if (TYPE(n) == RSQB) {
2385 com_error(c, PyExc_SyntaxError,
2386 "can't assign to []");
2387 return;
2389 if (assigning > OP_APPLY) {
2390 com_error(c, PyExc_SyntaxError,
2391 "augmented assign to list not possible");
2392 return;
2394 if (NCH(n) > 1
2395 && TYPE(CHILD(n, 1)) == list_for) {
2396 com_error(c, PyExc_SyntaxError,
2397 "can't assign to list comprehension");
2398 return;
2400 com_assign_sequence(c, n, assigning);
2401 return;
2402 case NAME:
2403 if (assigning > OP_APPLY)
2404 com_augassign_name(c, CHILD(n, 0),
2405 assigning, augn);
2406 else
2407 com_assign_name(c, CHILD(n, 0),
2408 assigning);
2409 return;
2410 default:
2411 com_error(c, PyExc_SyntaxError,
2412 "can't assign to literal");
2413 return;
2415 break;
2417 case lambdef:
2418 com_error(c, PyExc_SyntaxError,
2419 "can't assign to lambda");
2420 return;
2422 default:
2423 com_error(c, PyExc_SystemError,
2424 "com_assign: bad node");
2425 return;
2431 static void
2432 com_augassign(struct compiling *c, node *n)
2434 int opcode;
2436 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2437 case '+': opcode = INPLACE_ADD; break;
2438 case '-': opcode = INPLACE_SUBTRACT; break;
2439 case '/': opcode = INPLACE_DIVIDE; break;
2440 case '%': opcode = INPLACE_MODULO; break;
2441 case '<': opcode = INPLACE_LSHIFT; break;
2442 case '>': opcode = INPLACE_RSHIFT; break;
2443 case '&': opcode = INPLACE_AND; break;
2444 case '^': opcode = INPLACE_XOR; break;
2445 case '|': opcode = INPLACE_OR; break;
2446 case '*':
2447 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2448 opcode = INPLACE_POWER;
2449 else
2450 opcode = INPLACE_MULTIPLY;
2451 break;
2452 default:
2453 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2454 return;
2456 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2459 static void
2460 com_expr_stmt(struct compiling *c, node *n)
2462 REQ(n, expr_stmt);
2463 /* testlist (('=' testlist)* | augassign testlist) */
2464 /* Forget it if we have just a doc string here */
2465 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2466 return;
2467 if (NCH(n) == 1) {
2468 com_node(c, CHILD(n, NCH(n)-1));
2469 if (c->c_interactive)
2470 com_addbyte(c, PRINT_EXPR);
2471 else
2472 com_addbyte(c, POP_TOP);
2473 com_pop(c, 1);
2475 else if (TYPE(CHILD(n,1)) == augassign)
2476 com_augassign(c, n);
2477 else {
2478 int i;
2479 com_node(c, CHILD(n, NCH(n)-1));
2480 for (i = 0; i < NCH(n)-2; i+=2) {
2481 if (i+2 < NCH(n)-2) {
2482 com_addbyte(c, DUP_TOP);
2483 com_push(c, 1);
2485 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2490 static void
2491 com_assert_stmt(struct compiling *c, node *n)
2493 int a = 0, b = 0;
2494 int i;
2495 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2496 /* Generate code like for
2498 if __debug__:
2499 if not <test>:
2500 raise AssertionError [, <message>]
2502 where <message> is the second test, if present.
2505 if (Py_OptimizeFlag)
2506 return;
2507 com_addop_name(c, LOAD_GLOBAL, "__debug__");
2508 com_push(c, 1);
2509 com_addfwref(c, JUMP_IF_FALSE, &a);
2510 com_addbyte(c, POP_TOP);
2511 com_pop(c, 1);
2512 com_node(c, CHILD(n, 1));
2513 com_addfwref(c, JUMP_IF_TRUE, &b);
2514 com_addbyte(c, POP_TOP);
2515 com_pop(c, 1);
2516 /* Raise that exception! */
2517 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2518 com_push(c, 1);
2519 i = NCH(n)/2; /* Either 2 or 4 */
2520 if (i > 1)
2521 com_node(c, CHILD(n, 3));
2522 com_addoparg(c, RAISE_VARARGS, i);
2523 com_pop(c, i);
2524 /* The interpreter does not fall through */
2525 /* All jumps converge here */
2526 com_backpatch(c, a);
2527 com_backpatch(c, b);
2528 com_addbyte(c, POP_TOP);
2531 static void
2532 com_print_stmt(struct compiling *c, node *n)
2534 int i = 1;
2535 node* stream = NULL;
2537 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2539 /* are we using the extended print form? */
2540 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2541 stream = CHILD(n, 2);
2542 com_node(c, stream);
2543 /* stack: [...] => [... stream] */
2544 com_push(c, 1);
2545 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2546 i = 4;
2547 else
2548 i = 3;
2550 for (; i < NCH(n); i += 2) {
2551 if (stream != NULL) {
2552 com_addbyte(c, DUP_TOP);
2553 /* stack: [stream] => [stream stream] */
2554 com_push(c, 1);
2555 com_node(c, CHILD(n, i));
2556 /* stack: [stream stream] => [stream stream obj] */
2557 com_addbyte(c, ROT_TWO);
2558 /* stack: [stream stream obj] => [stream obj stream] */
2559 com_addbyte(c, PRINT_ITEM_TO);
2560 /* stack: [stream obj stream] => [stream] */
2561 com_pop(c, 2);
2563 else {
2564 com_node(c, CHILD(n, i));
2565 /* stack: [...] => [... obj] */
2566 com_addbyte(c, PRINT_ITEM);
2567 com_pop(c, 1);
2570 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2571 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2572 if (stream != NULL) {
2573 /* must pop the extra stream object off the stack */
2574 com_addbyte(c, POP_TOP);
2575 /* stack: [... stream] => [...] */
2576 com_pop(c, 1);
2579 else {
2580 if (stream != NULL) {
2581 /* this consumes the last stream object on stack */
2582 com_addbyte(c, PRINT_NEWLINE_TO);
2583 /* stack: [... stream] => [...] */
2584 com_pop(c, 1);
2586 else
2587 com_addbyte(c, PRINT_NEWLINE);
2591 static void
2592 com_return_stmt(struct compiling *c, node *n)
2594 REQ(n, return_stmt); /* 'return' [testlist] */
2595 if (!c->c_infunction) {
2596 com_error(c, PyExc_SyntaxError, "'return' outside function");
2598 if (NCH(n) < 2) {
2599 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2600 com_push(c, 1);
2602 else
2603 com_node(c, CHILD(n, 1));
2604 com_addbyte(c, RETURN_VALUE);
2605 com_pop(c, 1);
2608 static void
2609 com_raise_stmt(struct compiling *c, node *n)
2611 int i;
2612 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2613 if (NCH(n) > 1) {
2614 com_node(c, CHILD(n, 1));
2615 if (NCH(n) > 3) {
2616 com_node(c, CHILD(n, 3));
2617 if (NCH(n) > 5)
2618 com_node(c, CHILD(n, 5));
2621 i = NCH(n)/2;
2622 com_addoparg(c, RAISE_VARARGS, i);
2623 com_pop(c, i);
2626 static void
2627 com_from_import(struct compiling *c, node *n)
2629 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2630 com_push(c, 1);
2631 if (NCH(n) > 1) {
2632 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2633 com_error(c, PyExc_SyntaxError, "invalid syntax");
2634 return;
2636 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2637 } else
2638 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2639 com_pop(c, 1);
2642 static void
2643 com_import_stmt(struct compiling *c, node *n)
2645 int i;
2646 REQ(n, import_stmt);
2647 /* 'import' dotted_name (',' dotted_name)* |
2648 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2649 if (STR(CHILD(n, 0))[0] == 'f') {
2650 PyObject *tup;
2651 /* 'from' dotted_name 'import' ... */
2652 REQ(CHILD(n, 1), dotted_name);
2654 if (TYPE(CHILD(n, 3)) == STAR) {
2655 tup = Py_BuildValue("(s)", "*");
2656 } else {
2657 tup = PyTuple_New((NCH(n) - 2)/2);
2658 for (i = 3; i < NCH(n); i += 2) {
2659 PyTuple_SET_ITEM(tup, (i-3)/2,
2660 PyString_FromString(STR(
2661 CHILD(CHILD(n, i), 0))));
2664 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2665 Py_DECREF(tup);
2666 com_push(c, 1);
2667 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2668 if (TYPE(CHILD(n, 3)) == STAR)
2669 com_addbyte(c, IMPORT_STAR);
2670 else {
2671 for (i = 3; i < NCH(n); i += 2)
2672 com_from_import(c, CHILD(n, i));
2673 com_addbyte(c, POP_TOP);
2675 com_pop(c, 1);
2677 else {
2678 /* 'import' ... */
2679 for (i = 1; i < NCH(n); i += 2) {
2680 node *subn = CHILD(n, i);
2681 REQ(subn, dotted_as_name);
2682 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2683 com_push(c, 1);
2684 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2685 if (NCH(subn) > 1) {
2686 int j;
2687 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
2688 com_error(c, PyExc_SyntaxError,
2689 "invalid syntax");
2690 return;
2692 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2693 com_addopname(c, LOAD_ATTR,
2694 CHILD(CHILD(subn, 0),
2695 j));
2696 com_addop_varname(c, VAR_STORE,
2697 STR(CHILD(subn, 2)));
2698 } else
2699 com_addop_varname(c, VAR_STORE,
2700 STR(CHILD(CHILD(subn, 0),
2701 0)));
2702 com_pop(c, 1);
2707 static void
2708 com_exec_stmt(struct compiling *c, node *n)
2710 REQ(n, exec_stmt);
2711 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2712 com_node(c, CHILD(n, 1));
2713 if (NCH(n) >= 4)
2714 com_node(c, CHILD(n, 3));
2715 else {
2716 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2717 com_push(c, 1);
2719 if (NCH(n) >= 6)
2720 com_node(c, CHILD(n, 5));
2721 else {
2722 com_addbyte(c, DUP_TOP);
2723 com_push(c, 1);
2725 com_addbyte(c, EXEC_STMT);
2726 com_pop(c, 3);
2729 static int
2730 is_constant_false(struct compiling *c, node *n)
2732 PyObject *v;
2733 int i;
2734 /* argument c will be NULL when called from symtable_node() */
2736 /* Label to avoid tail recursion */
2737 next:
2738 switch (TYPE(n)) {
2740 case suite:
2741 if (NCH(n) == 1) {
2742 n = CHILD(n, 0);
2743 goto next;
2745 /* Fall through */
2746 case file_input:
2747 for (i = 0; i < NCH(n); i++) {
2748 node *ch = CHILD(n, i);
2749 if (TYPE(ch) == stmt) {
2750 n = ch;
2751 goto next;
2754 break;
2756 case stmt:
2757 case simple_stmt:
2758 case small_stmt:
2759 n = CHILD(n, 0);
2760 goto next;
2762 case expr_stmt:
2763 case testlist:
2764 case test:
2765 case and_test:
2766 case not_test:
2767 case comparison:
2768 case expr:
2769 case xor_expr:
2770 case and_expr:
2771 case shift_expr:
2772 case arith_expr:
2773 case term:
2774 case factor:
2775 case power:
2776 case atom:
2777 if (NCH(n) == 1) {
2778 n = CHILD(n, 0);
2779 goto next;
2781 break;
2783 case NAME:
2784 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2785 return 1;
2786 break;
2788 case NUMBER:
2789 v = parsenumber(c, STR(n));
2790 if (v == NULL) {
2791 PyErr_Clear();
2792 break;
2794 i = PyObject_IsTrue(v);
2795 Py_DECREF(v);
2796 return i == 0;
2798 case STRING:
2799 v = parsestr(STR(n));
2800 if (v == NULL) {
2801 PyErr_Clear();
2802 break;
2804 i = PyObject_IsTrue(v);
2805 Py_DECREF(v);
2806 return i == 0;
2809 return 0;
2812 static void
2813 com_if_stmt(struct compiling *c, node *n)
2815 int i;
2816 int anchor = 0;
2817 REQ(n, if_stmt);
2818 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2819 for (i = 0; i+3 < NCH(n); i+=4) {
2820 int a = 0;
2821 node *ch = CHILD(n, i+1);
2822 if (is_constant_false(c, ch))
2823 continue;
2824 if (i > 0)
2825 com_addoparg(c, SET_LINENO, ch->n_lineno);
2826 com_node(c, ch);
2827 com_addfwref(c, JUMP_IF_FALSE, &a);
2828 com_addbyte(c, POP_TOP);
2829 com_pop(c, 1);
2830 com_node(c, CHILD(n, i+3));
2831 com_addfwref(c, JUMP_FORWARD, &anchor);
2832 com_backpatch(c, a);
2833 /* We jump here with an extra entry which we now pop */
2834 com_addbyte(c, POP_TOP);
2836 if (i+2 < NCH(n))
2837 com_node(c, CHILD(n, i+2));
2838 if (anchor)
2839 com_backpatch(c, anchor);
2842 static void
2843 com_while_stmt(struct compiling *c, node *n)
2845 int break_anchor = 0;
2846 int anchor = 0;
2847 int save_begin = c->c_begin;
2848 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2849 com_addfwref(c, SETUP_LOOP, &break_anchor);
2850 block_push(c, SETUP_LOOP);
2851 c->c_begin = c->c_nexti;
2852 com_addoparg(c, SET_LINENO, n->n_lineno);
2853 com_node(c, CHILD(n, 1));
2854 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2855 com_addbyte(c, POP_TOP);
2856 com_pop(c, 1);
2857 c->c_loops++;
2858 com_node(c, CHILD(n, 3));
2859 c->c_loops--;
2860 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2861 c->c_begin = save_begin;
2862 com_backpatch(c, anchor);
2863 /* We jump here with one entry more on the stack */
2864 com_addbyte(c, POP_TOP);
2865 com_addbyte(c, POP_BLOCK);
2866 block_pop(c, SETUP_LOOP);
2867 if (NCH(n) > 4)
2868 com_node(c, CHILD(n, 6));
2869 com_backpatch(c, break_anchor);
2872 static void
2873 com_for_stmt(struct compiling *c, node *n)
2875 PyObject *v;
2876 int break_anchor = 0;
2877 int anchor = 0;
2878 int save_begin = c->c_begin;
2879 REQ(n, for_stmt);
2880 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2881 com_addfwref(c, SETUP_LOOP, &break_anchor);
2882 block_push(c, SETUP_LOOP);
2883 com_node(c, CHILD(n, 3));
2884 v = PyInt_FromLong(0L);
2885 if (v == NULL)
2886 c->c_errors++;
2887 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
2888 com_push(c, 1);
2889 Py_XDECREF(v);
2890 c->c_begin = c->c_nexti;
2891 com_addoparg(c, SET_LINENO, n->n_lineno);
2892 com_addfwref(c, FOR_LOOP, &anchor);
2893 com_push(c, 1);
2894 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
2895 c->c_loops++;
2896 com_node(c, CHILD(n, 5));
2897 c->c_loops--;
2898 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2899 c->c_begin = save_begin;
2900 com_backpatch(c, anchor);
2901 com_pop(c, 2); /* FOR_LOOP has popped these */
2902 com_addbyte(c, POP_BLOCK);
2903 block_pop(c, SETUP_LOOP);
2904 if (NCH(n) > 8)
2905 com_node(c, CHILD(n, 8));
2906 com_backpatch(c, break_anchor);
2909 /* Code generated for "try: S finally: Sf" is as follows:
2911 SETUP_FINALLY L
2912 <code for S>
2913 POP_BLOCK
2914 LOAD_CONST <nil>
2915 L: <code for Sf>
2916 END_FINALLY
2918 The special instructions use the block stack. Each block
2919 stack entry contains the instruction that created it (here
2920 SETUP_FINALLY), the level of the value stack at the time the
2921 block stack entry was created, and a label (here L).
2923 SETUP_FINALLY:
2924 Pushes the current value stack level and the label
2925 onto the block stack.
2926 POP_BLOCK:
2927 Pops en entry from the block stack, and pops the value
2928 stack until its level is the same as indicated on the
2929 block stack. (The label is ignored.)
2930 END_FINALLY:
2931 Pops a variable number of entries from the *value* stack
2932 and re-raises the exception they specify. The number of
2933 entries popped depends on the (pseudo) exception type.
2935 The block stack is unwound when an exception is raised:
2936 when a SETUP_FINALLY entry is found, the exception is pushed
2937 onto the value stack (and the exception condition is cleared),
2938 and the interpreter jumps to the label gotten from the block
2939 stack.
2941 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2942 (The contents of the value stack is shown in [], with the top
2943 at the right; 'tb' is trace-back info, 'val' the exception's
2944 associated value, and 'exc' the exception.)
2946 Value stack Label Instruction Argument
2947 [] SETUP_EXCEPT L1
2948 [] <code for S>
2949 [] POP_BLOCK
2950 [] JUMP_FORWARD L0
2952 [tb, val, exc] L1: DUP )
2953 [tb, val, exc, exc] <evaluate E1> )
2954 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2955 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2956 [tb, val, exc, 1] POP )
2957 [tb, val, exc] POP
2958 [tb, val] <assign to V1> (or POP if no V1)
2959 [tb] POP
2960 [] <code for S1>
2961 JUMP_FORWARD L0
2963 [tb, val, exc, 0] L2: POP
2964 [tb, val, exc] DUP
2965 .............................etc.......................
2967 [tb, val, exc, 0] Ln+1: POP
2968 [tb, val, exc] END_FINALLY # re-raise exception
2970 [] L0: <next statement>
2972 Of course, parts are not generated if Vi or Ei is not present.
2975 static void
2976 com_try_except(struct compiling *c, node *n)
2978 int except_anchor = 0;
2979 int end_anchor = 0;
2980 int else_anchor = 0;
2981 int i;
2982 node *ch;
2984 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2985 block_push(c, SETUP_EXCEPT);
2986 com_node(c, CHILD(n, 2));
2987 com_addbyte(c, POP_BLOCK);
2988 block_pop(c, SETUP_EXCEPT);
2989 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2990 com_backpatch(c, except_anchor);
2991 for (i = 3;
2992 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2993 i += 3) {
2994 /* except_clause: 'except' [expr [',' var]] */
2995 if (except_anchor == 0) {
2996 com_error(c, PyExc_SyntaxError,
2997 "default 'except:' must be last");
2998 break;
3000 except_anchor = 0;
3001 com_push(c, 3); /* tb, val, exc pushed by exception */
3002 com_addoparg(c, SET_LINENO, ch->n_lineno);
3003 if (NCH(ch) > 1) {
3004 com_addbyte(c, DUP_TOP);
3005 com_push(c, 1);
3006 com_node(c, CHILD(ch, 1));
3007 com_addoparg(c, COMPARE_OP, EXC_MATCH);
3008 com_pop(c, 1);
3009 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3010 com_addbyte(c, POP_TOP);
3011 com_pop(c, 1);
3013 com_addbyte(c, POP_TOP);
3014 com_pop(c, 1);
3015 if (NCH(ch) > 3)
3016 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3017 else {
3018 com_addbyte(c, POP_TOP);
3019 com_pop(c, 1);
3021 com_addbyte(c, POP_TOP);
3022 com_pop(c, 1);
3023 com_node(c, CHILD(n, i+2));
3024 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3025 if (except_anchor) {
3026 com_backpatch(c, except_anchor);
3027 /* We come in with [tb, val, exc, 0] on the
3028 stack; one pop and it's the same as
3029 expected at the start of the loop */
3030 com_addbyte(c, POP_TOP);
3033 /* We actually come in here with [tb, val, exc] but the
3034 END_FINALLY will zap those and jump around.
3035 The c_stacklevel does not reflect them so we need not pop
3036 anything. */
3037 com_addbyte(c, END_FINALLY);
3038 com_backpatch(c, else_anchor);
3039 if (i < NCH(n))
3040 com_node(c, CHILD(n, i+2));
3041 com_backpatch(c, end_anchor);
3044 static void
3045 com_try_finally(struct compiling *c, node *n)
3047 int finally_anchor = 0;
3048 node *ch;
3050 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3051 block_push(c, SETUP_FINALLY);
3052 com_node(c, CHILD(n, 2));
3053 com_addbyte(c, POP_BLOCK);
3054 block_pop(c, SETUP_FINALLY);
3055 block_push(c, END_FINALLY);
3056 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3057 /* While the generated code pushes only one item,
3058 the try-finally handling can enter here with
3059 up to three items. OK, here are the details:
3060 3 for an exception, 2 for RETURN, 1 for BREAK. */
3061 com_push(c, 3);
3062 com_backpatch(c, finally_anchor);
3063 ch = CHILD(n, NCH(n)-1);
3064 com_addoparg(c, SET_LINENO, ch->n_lineno);
3065 com_node(c, ch);
3066 com_addbyte(c, END_FINALLY);
3067 block_pop(c, END_FINALLY);
3068 com_pop(c, 3); /* Matches the com_push above */
3071 static void
3072 com_try_stmt(struct compiling *c, node *n)
3074 REQ(n, try_stmt);
3075 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3076 | 'try' ':' suite 'finally' ':' suite */
3077 if (TYPE(CHILD(n, 3)) != except_clause)
3078 com_try_finally(c, n);
3079 else
3080 com_try_except(c, n);
3083 static node *
3084 get_rawdocstring(node *n)
3086 int i;
3088 /* Label to avoid tail recursion */
3089 next:
3090 switch (TYPE(n)) {
3092 case suite:
3093 if (NCH(n) == 1) {
3094 n = CHILD(n, 0);
3095 goto next;
3097 /* Fall through */
3098 case file_input:
3099 for (i = 0; i < NCH(n); i++) {
3100 node *ch = CHILD(n, i);
3101 if (TYPE(ch) == stmt) {
3102 n = ch;
3103 goto next;
3106 break;
3108 case stmt:
3109 case simple_stmt:
3110 case small_stmt:
3111 n = CHILD(n, 0);
3112 goto next;
3114 case expr_stmt:
3115 case testlist:
3116 case test:
3117 case and_test:
3118 case not_test:
3119 case comparison:
3120 case expr:
3121 case xor_expr:
3122 case and_expr:
3123 case shift_expr:
3124 case arith_expr:
3125 case term:
3126 case factor:
3127 case power:
3128 if (NCH(n) == 1) {
3129 n = CHILD(n, 0);
3130 goto next;
3132 break;
3134 case atom:
3135 if (TYPE(CHILD(n, 0)) == STRING)
3136 return n;
3137 break;
3140 return NULL;
3143 static PyObject *
3144 get_docstring(node *n)
3146 /* Don't generate doc-strings if run with -OO */
3147 if (Py_OptimizeFlag > 1)
3148 return NULL;
3149 n = get_rawdocstring(n);
3150 if (n == NULL)
3151 return NULL;
3152 return parsestrplus(n);
3155 static void
3156 com_suite(struct compiling *c, node *n)
3158 REQ(n, suite);
3159 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3160 if (NCH(n) == 1) {
3161 com_node(c, CHILD(n, 0));
3163 else {
3164 int i;
3165 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3166 node *ch = CHILD(n, i);
3167 if (TYPE(ch) == stmt)
3168 com_node(c, ch);
3173 /* ARGSUSED */
3174 static void
3175 com_continue_stmt(struct compiling *c, node *n)
3177 int i = c->c_nblocks;
3178 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3179 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3181 else if (i <= 0) {
3182 /* at the outer level */
3183 com_error(c, PyExc_SyntaxError,
3184 "'continue' not properly in loop");
3186 else {
3187 int j;
3188 for (j = i-1; j >= 0; --j) {
3189 if (c->c_block[j] == SETUP_LOOP)
3190 break;
3192 if (j >= 0) {
3193 /* there is a loop, but something interferes */
3194 for (; i > j; --i) {
3195 if (c->c_block[i] == SETUP_EXCEPT ||
3196 c->c_block[i] == SETUP_FINALLY) {
3197 com_addoparg(c, CONTINUE_LOOP,
3198 c->c_begin);
3199 return;
3201 if (c->c_block[i] == END_FINALLY) {
3202 com_error(c, PyExc_SyntaxError,
3203 "'continue' not supported inside 'finally' clause");
3204 return;
3208 com_error(c, PyExc_SyntaxError,
3209 "'continue' not properly in loop");
3211 /* XXX Could allow it inside a 'finally' clause
3212 XXX if we could pop the exception still on the stack */
3215 static int
3216 com_argdefs(struct compiling *c, node *n)
3218 int i, nch, nargs, ndefs;
3219 if (TYPE(n) == lambdef) {
3220 /* lambdef: 'lambda' [varargslist] ':' test */
3221 n = CHILD(n, 1);
3223 else {
3224 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3225 n = CHILD(n, 2);
3226 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3227 n = CHILD(n, 1);
3229 if (TYPE(n) != varargslist)
3230 return 0;
3231 /* varargslist:
3232 (fpdef ['=' test] ',')* '*' ....... |
3233 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3234 nch = NCH(n);
3235 nargs = 0;
3236 ndefs = 0;
3237 for (i = 0; i < nch; i++) {
3238 int t;
3239 if (TYPE(CHILD(n, i)) == STAR ||
3240 TYPE(CHILD(n, i)) == DOUBLESTAR)
3241 break;
3242 nargs++;
3243 i++;
3244 if (i >= nch)
3245 t = RPAR; /* Anything except EQUAL or COMMA */
3246 else
3247 t = TYPE(CHILD(n, i));
3248 if (t == EQUAL) {
3249 i++;
3250 ndefs++;
3251 com_node(c, CHILD(n, i));
3252 i++;
3253 if (i >= nch)
3254 break;
3255 t = TYPE(CHILD(n, i));
3257 else {
3258 /* Treat "(a=1, b)" as an error */
3259 if (ndefs)
3260 com_error(c, PyExc_SyntaxError,
3261 "non-default argument follows default argument");
3263 if (t != COMMA)
3264 break;
3266 return ndefs;
3269 static void
3270 com_funcdef(struct compiling *c, node *n)
3272 PyObject *co;
3273 int ndefs;
3274 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3275 ndefs = com_argdefs(c, n);
3276 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3277 n->n_lineno);
3278 co = (PyObject *)icompile(n, c);
3279 symtable_exit_scope(c->c_symtable);
3280 if (co == NULL)
3281 c->c_errors++;
3282 else {
3283 int closure = com_make_closure(c, (PyCodeObject *)co);
3284 int i = com_addconst(c, co);
3285 com_addoparg(c, LOAD_CONST, i);
3286 com_push(c, 1);
3287 if (closure)
3288 com_addoparg(c, MAKE_CLOSURE, ndefs);
3289 else
3290 com_addoparg(c, MAKE_FUNCTION, ndefs);
3291 com_pop(c, ndefs);
3292 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3293 com_pop(c, 1);
3294 Py_DECREF(co);
3298 static void
3299 com_bases(struct compiling *c, node *n)
3301 int i;
3302 REQ(n, testlist);
3303 /* testlist: test (',' test)* [','] */
3304 for (i = 0; i < NCH(n); i += 2)
3305 com_node(c, CHILD(n, i));
3306 i = (NCH(n)+1) / 2;
3307 com_addoparg(c, BUILD_TUPLE, i);
3308 com_pop(c, i-1);
3311 static void
3312 com_classdef(struct compiling *c, node *n)
3314 int i;
3315 PyObject *co, *v;
3316 char *name;
3318 REQ(n, classdef);
3319 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3320 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3321 c->c_errors++;
3322 return;
3324 /* Push the class name on the stack */
3325 i = com_addconst(c, v);
3326 com_addoparg(c, LOAD_CONST, i);
3327 com_push(c, 1);
3328 Py_DECREF(v);
3329 /* Push the tuple of base classes on the stack */
3330 if (TYPE(CHILD(n, 2)) != LPAR) {
3331 com_addoparg(c, BUILD_TUPLE, 0);
3332 com_push(c, 1);
3334 else
3335 com_bases(c, CHILD(n, 3));
3336 name = STR(CHILD(n, 1));
3337 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3338 co = (PyObject *)icompile(n, c);
3339 symtable_exit_scope(c->c_symtable);
3340 if (co == NULL)
3341 c->c_errors++;
3342 else {
3343 int closure = com_make_closure(c, (PyCodeObject *)co);
3344 i = com_addconst(c, co);
3345 com_addoparg(c, LOAD_CONST, i);
3346 com_push(c, 1);
3347 if (closure)
3348 com_addoparg(c, MAKE_CLOSURE, 0);
3349 else
3350 com_addoparg(c, MAKE_FUNCTION, 0);
3351 com_addoparg(c, CALL_FUNCTION, 0);
3352 com_addbyte(c, BUILD_CLASS);
3353 com_pop(c, 2);
3354 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3355 Py_DECREF(co);
3359 static void
3360 com_node(struct compiling *c, node *n)
3362 loop:
3363 if (c->c_errors)
3364 return;
3365 switch (TYPE(n)) {
3367 /* Definition nodes */
3369 case funcdef:
3370 com_funcdef(c, n);
3371 break;
3372 case classdef:
3373 com_classdef(c, n);
3374 break;
3376 /* Trivial parse tree nodes */
3378 case stmt:
3379 case small_stmt:
3380 case flow_stmt:
3381 n = CHILD(n, 0);
3382 goto loop;
3384 case simple_stmt:
3385 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3386 com_addoparg(c, SET_LINENO, n->n_lineno);
3388 int i;
3389 for (i = 0; i < NCH(n)-1; i += 2)
3390 com_node(c, CHILD(n, i));
3392 break;
3394 case compound_stmt:
3395 com_addoparg(c, SET_LINENO, n->n_lineno);
3396 n = CHILD(n, 0);
3397 goto loop;
3399 /* Statement nodes */
3401 case expr_stmt:
3402 com_expr_stmt(c, n);
3403 break;
3404 case print_stmt:
3405 com_print_stmt(c, n);
3406 break;
3407 case del_stmt: /* 'del' exprlist */
3408 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3409 break;
3410 case pass_stmt:
3411 break;
3412 case break_stmt:
3413 if (c->c_loops == 0) {
3414 com_error(c, PyExc_SyntaxError,
3415 "'break' outside loop");
3417 com_addbyte(c, BREAK_LOOP);
3418 break;
3419 case continue_stmt:
3420 com_continue_stmt(c, n);
3421 break;
3422 case return_stmt:
3423 com_return_stmt(c, n);
3424 break;
3425 case raise_stmt:
3426 com_raise_stmt(c, n);
3427 break;
3428 case import_stmt:
3429 com_import_stmt(c, n);
3430 break;
3431 case global_stmt:
3432 break;
3433 case exec_stmt:
3434 com_exec_stmt(c, n);
3435 break;
3436 case assert_stmt:
3437 com_assert_stmt(c, n);
3438 break;
3439 case if_stmt:
3440 com_if_stmt(c, n);
3441 break;
3442 case while_stmt:
3443 com_while_stmt(c, n);
3444 break;
3445 case for_stmt:
3446 com_for_stmt(c, n);
3447 break;
3448 case try_stmt:
3449 com_try_stmt(c, n);
3450 break;
3451 case suite:
3452 com_suite(c, n);
3453 break;
3455 /* Expression nodes */
3457 case testlist:
3458 com_list(c, n, 0);
3459 break;
3460 case test:
3461 com_test(c, n);
3462 break;
3463 case and_test:
3464 com_and_test(c, n);
3465 break;
3466 case not_test:
3467 com_not_test(c, n);
3468 break;
3469 case comparison:
3470 com_comparison(c, n);
3471 break;
3472 case exprlist:
3473 com_list(c, n, 0);
3474 break;
3475 case expr:
3476 com_expr(c, n);
3477 break;
3478 case xor_expr:
3479 com_xor_expr(c, n);
3480 break;
3481 case and_expr:
3482 com_and_expr(c, n);
3483 break;
3484 case shift_expr:
3485 com_shift_expr(c, n);
3486 break;
3487 case arith_expr:
3488 com_arith_expr(c, n);
3489 break;
3490 case term:
3491 com_term(c, n);
3492 break;
3493 case factor:
3494 com_factor(c, n);
3495 break;
3496 case power:
3497 com_power(c, n);
3498 break;
3499 case atom:
3500 com_atom(c, n);
3501 break;
3503 default:
3504 com_error(c, PyExc_SystemError,
3505 "com_node: unexpected node type");
3509 static void com_fplist(struct compiling *, node *);
3511 static void
3512 com_fpdef(struct compiling *c, node *n)
3514 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3515 if (TYPE(CHILD(n, 0)) == LPAR)
3516 com_fplist(c, CHILD(n, 1));
3517 else {
3518 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3519 com_pop(c, 1);
3523 static void
3524 com_fplist(struct compiling *c, node *n)
3526 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3527 if (NCH(n) == 1) {
3528 com_fpdef(c, CHILD(n, 0));
3530 else {
3531 int i = (NCH(n)+1)/2;
3532 com_addoparg(c, UNPACK_SEQUENCE, i);
3533 com_push(c, i-1);
3534 for (i = 0; i < NCH(n); i += 2)
3535 com_fpdef(c, CHILD(n, i));
3539 static void
3540 com_arglist(struct compiling *c, node *n)
3542 int nch, i, narg;
3543 int complex = 0;
3544 char nbuf[10];
3545 REQ(n, varargslist);
3546 /* varargslist:
3547 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3548 nch = NCH(n);
3549 /* Enter all arguments in table of locals */
3550 for (i = 0, narg = 0; i < nch; i++) {
3551 node *ch = CHILD(n, i);
3552 node *fp;
3553 char *name;
3554 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3555 break;
3556 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3557 fp = CHILD(ch, 0);
3558 if (TYPE(fp) != NAME) {
3559 name = nbuf;
3560 sprintf(nbuf, ".%d", i);
3561 complex = 1;
3563 narg++;
3564 /* all name updates handled by symtable */
3565 if (++i >= nch)
3566 break;
3567 ch = CHILD(n, i);
3568 if (TYPE(ch) == EQUAL)
3569 i += 2;
3570 else
3571 REQ(ch, COMMA);
3573 if (complex) {
3574 /* Generate code for complex arguments only after
3575 having counted the simple arguments */
3576 int ilocal = 0;
3577 for (i = 0; i < nch; i++) {
3578 node *ch = CHILD(n, i);
3579 node *fp;
3580 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3581 break;
3582 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3583 fp = CHILD(ch, 0);
3584 if (TYPE(fp) != NAME) {
3585 com_addoparg(c, LOAD_FAST, ilocal);
3586 com_push(c, 1);
3587 com_fpdef(c, ch);
3589 ilocal++;
3590 if (++i >= nch)
3591 break;
3592 ch = CHILD(n, i);
3593 if (TYPE(ch) == EQUAL)
3594 i += 2;
3595 else
3596 REQ(ch, COMMA);
3601 static void
3602 com_file_input(struct compiling *c, node *n)
3604 int i;
3605 PyObject *doc;
3606 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3607 doc = get_docstring(n);
3608 if (doc != NULL) {
3609 int i = com_addconst(c, doc);
3610 Py_DECREF(doc);
3611 com_addoparg(c, LOAD_CONST, i);
3612 com_push(c, 1);
3613 com_addop_name(c, STORE_NAME, "__doc__");
3614 com_pop(c, 1);
3616 for (i = 0; i < NCH(n); i++) {
3617 node *ch = CHILD(n, i);
3618 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3619 com_node(c, ch);
3623 /* Top-level compile-node interface */
3625 static void
3626 compile_funcdef(struct compiling *c, node *n)
3628 PyObject *doc;
3629 node *ch;
3630 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3631 c->c_name = STR(CHILD(n, 1));
3632 doc = get_docstring(CHILD(n, 4));
3633 if (doc != NULL) {
3634 (void) com_addconst(c, doc);
3635 Py_DECREF(doc);
3637 else
3638 (void) com_addconst(c, Py_None); /* No docstring */
3639 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3640 ch = CHILD(ch, 1); /* ')' | varargslist */
3641 if (TYPE(ch) == varargslist)
3642 com_arglist(c, ch);
3643 c->c_infunction = 1;
3644 com_node(c, CHILD(n, 4));
3645 c->c_infunction = 0;
3646 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3647 com_push(c, 1);
3648 com_addbyte(c, RETURN_VALUE);
3649 com_pop(c, 1);
3652 static void
3653 compile_lambdef(struct compiling *c, node *n)
3655 node *ch;
3656 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3657 c->c_name = "<lambda>";
3659 ch = CHILD(n, 1);
3660 (void) com_addconst(c, Py_None); /* No docstring */
3661 if (TYPE(ch) == varargslist) {
3662 com_arglist(c, ch);
3663 ch = CHILD(n, 3);
3665 else
3666 ch = CHILD(n, 2);
3667 com_node(c, ch);
3668 com_addbyte(c, RETURN_VALUE);
3669 com_pop(c, 1);
3672 static void
3673 compile_classdef(struct compiling *c, node *n)
3675 node *ch;
3676 PyObject *doc;
3677 REQ(n, classdef);
3678 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3679 c->c_name = STR(CHILD(n, 1));
3680 c->c_private = c->c_name;
3681 ch = CHILD(n, NCH(n)-1); /* The suite */
3682 doc = get_docstring(ch);
3683 if (doc != NULL) {
3684 int i = com_addconst(c, doc);
3685 Py_DECREF(doc);
3686 com_addoparg(c, LOAD_CONST, i);
3687 com_push(c, 1);
3688 com_addop_name(c, STORE_NAME, "__doc__");
3689 com_pop(c, 1);
3691 else
3692 (void) com_addconst(c, Py_None);
3693 com_node(c, ch);
3694 com_addbyte(c, LOAD_LOCALS);
3695 com_push(c, 1);
3696 com_addbyte(c, RETURN_VALUE);
3697 com_pop(c, 1);
3700 static void
3701 compile_node(struct compiling *c, node *n)
3703 com_addoparg(c, SET_LINENO, n->n_lineno);
3705 switch (TYPE(n)) {
3707 case single_input: /* One interactive command */
3708 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3709 c->c_interactive++;
3710 n = CHILD(n, 0);
3711 if (TYPE(n) != NEWLINE)
3712 com_node(c, n);
3713 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3714 com_push(c, 1);
3715 com_addbyte(c, RETURN_VALUE);
3716 com_pop(c, 1);
3717 c->c_interactive--;
3718 break;
3720 case file_input: /* A whole file, or built-in function exec() */
3721 com_file_input(c, n);
3722 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3723 com_push(c, 1);
3724 com_addbyte(c, RETURN_VALUE);
3725 com_pop(c, 1);
3726 break;
3728 case eval_input: /* Built-in function input() */
3729 com_node(c, CHILD(n, 0));
3730 com_addbyte(c, RETURN_VALUE);
3731 com_pop(c, 1);
3732 break;
3734 case lambdef: /* anonymous function definition */
3735 compile_lambdef(c, n);
3736 break;
3738 case funcdef: /* A function definition */
3739 compile_funcdef(c, n);
3740 break;
3742 case classdef: /* A class definition */
3743 compile_classdef(c, n);
3744 break;
3746 default:
3747 com_error(c, PyExc_SystemError,
3748 "compile_node: unexpected node type");
3752 static PyObject *
3753 dict_keys_inorder(PyObject *dict, int offset)
3755 PyObject *tuple, *k, *v;
3756 int i, pos = 0, size = PyDict_Size(dict);
3758 tuple = PyTuple_New(size);
3759 if (tuple == NULL)
3760 return NULL;
3761 while (PyDict_Next(dict, &pos, &k, &v)) {
3762 i = PyInt_AS_LONG(v);
3763 Py_INCREF(k);
3764 assert((i - offset) < size);
3765 PyTuple_SET_ITEM(tuple, i - offset, k);
3767 return tuple;
3770 PyCodeObject *
3771 PyNode_Compile(node *n, char *filename)
3773 return PyNode_CompileFlags(n, filename, NULL);
3776 PyCodeObject *
3777 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
3779 return jcompile(n, filename, NULL, flags);
3782 struct symtable *
3783 PyNode_CompileSymtable(node *n, char *filename)
3785 struct symtable *st;
3786 PyFutureFeatures *ff;
3788 ff = PyNode_Future(n, filename);
3789 if (ff == NULL)
3790 return NULL;
3791 st = symtable_init();
3792 if (st == NULL)
3793 return NULL;
3794 st->st_future = ff;
3795 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
3796 if (st->st_errors > 0)
3797 goto fail;
3798 symtable_node(st, n);
3799 if (st->st_errors > 0)
3800 goto fail;
3802 return st;
3803 fail:
3804 PyMem_Free((void *)ff);
3805 st->st_future = NULL;
3806 PySymtable_Free(st);
3807 return NULL;
3810 static PyCodeObject *
3811 icompile(node *n, struct compiling *base)
3813 return jcompile(n, base->c_filename, base, NULL);
3816 static PyCodeObject *
3817 jcompile(node *n, char *filename, struct compiling *base,
3818 PyCompilerFlags *flags)
3820 struct compiling sc;
3821 PyCodeObject *co;
3822 if (!com_init(&sc, filename))
3823 return NULL;
3824 if (base) {
3825 sc.c_private = base->c_private;
3826 sc.c_symtable = base->c_symtable;
3827 /* c_symtable still points to parent's symbols */
3828 if (base->c_nested
3829 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
3830 sc.c_nested = 1;
3831 } else {
3832 sc.c_private = NULL;
3833 sc.c_future = PyNode_Future(n, filename);
3834 if (sc.c_future == NULL) {
3835 com_free(&sc);
3836 return NULL;
3838 if (flags) {
3839 if (flags->cf_nested_scopes)
3840 sc.c_future->ff_nested_scopes = 1;
3841 else if (sc.c_future->ff_nested_scopes)
3842 flags->cf_nested_scopes = 1;
3844 if (symtable_build(&sc, n) < 0) {
3845 com_free(&sc);
3846 return NULL;
3849 co = NULL;
3850 if (symtable_load_symbols(&sc) < 0) {
3851 sc.c_errors++;
3852 goto exit;
3854 compile_node(&sc, n);
3855 com_done(&sc);
3856 if (sc.c_errors == 0) {
3857 PyObject *consts, *names, *varnames, *filename, *name,
3858 *freevars, *cellvars;
3859 consts = PyList_AsTuple(sc.c_consts);
3860 names = PyList_AsTuple(sc.c_names);
3861 varnames = PyList_AsTuple(sc.c_varnames);
3862 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
3863 freevars = dict_keys_inorder(sc.c_freevars,
3864 PyTuple_GET_SIZE(cellvars));
3865 filename = PyString_InternFromString(sc.c_filename);
3866 name = PyString_InternFromString(sc.c_name);
3867 if (!PyErr_Occurred())
3868 co = PyCode_New(sc.c_argcount,
3869 sc.c_nlocals,
3870 sc.c_maxstacklevel,
3871 sc.c_flags,
3872 sc.c_code,
3873 consts,
3874 names,
3875 varnames,
3876 freevars,
3877 cellvars,
3878 filename,
3879 name,
3880 sc.c_firstlineno,
3881 sc.c_lnotab);
3882 Py_XDECREF(consts);
3883 Py_XDECREF(names);
3884 Py_XDECREF(varnames);
3885 Py_XDECREF(freevars);
3886 Py_XDECREF(cellvars);
3887 Py_XDECREF(filename);
3888 Py_XDECREF(name);
3890 else if (!PyErr_Occurred()) {
3891 /* This could happen if someone called PyErr_Clear() after an
3892 error was reported above. That's not supposed to happen,
3893 but I just plugged one case and I'm not sure there can't be
3894 others. In that case, raise SystemError so that at least
3895 it gets reported instead dumping core. */
3896 PyErr_SetString(PyExc_SystemError, "lost syntax error");
3898 exit:
3899 if (base == NULL) {
3900 PySymtable_Free(sc.c_symtable);
3901 sc.c_symtable = NULL;
3903 com_free(&sc);
3904 return co;
3908 PyCode_Addr2Line(PyCodeObject *co, int addrq)
3910 int size = PyString_Size(co->co_lnotab) / 2;
3911 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
3912 int line = co->co_firstlineno;
3913 int addr = 0;
3914 while (--size >= 0) {
3915 addr += *p++;
3916 if (addr > addrq)
3917 break;
3918 line += *p++;
3920 return line;
3923 /* The test for LOCAL must come before the test for FREE in order to
3924 handle classes where name is both local and free. The local var is
3925 a method and the free var is a free var referenced within a method.
3928 static int
3929 get_ref_type(struct compiling *c, char *name)
3931 PyObject *v;
3932 if (c->c_symtable->st_nested_scopes) {
3933 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
3934 return CELL;
3935 if (PyDict_GetItemString(c->c_locals, name) != NULL)
3936 return LOCAL;
3937 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
3938 return FREE;
3939 v = PyDict_GetItemString(c->c_globals, name);
3940 if (v) {
3941 if (v == Py_None)
3942 return GLOBAL_EXPLICIT;
3943 else {
3944 return GLOBAL_IMPLICIT;
3947 } else {
3948 if (PyDict_GetItemString(c->c_locals, name) != NULL)
3949 return LOCAL;
3950 v = PyDict_GetItemString(c->c_globals, name);
3951 if (v) {
3952 if (v == Py_None)
3953 return GLOBAL_EXPLICIT;
3954 else {
3955 return GLOBAL_IMPLICIT;
3960 char buf[350];
3961 sprintf(buf,
3962 "unknown scope for %.100s in %.100s(%s) "
3963 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
3964 name, c->c_name,
3965 PyObject_REPR(c->c_symtable->st_cur->ste_id),
3966 c->c_filename,
3967 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
3968 PyObject_REPR(c->c_locals),
3969 PyObject_REPR(c->c_globals)
3972 Py_FatalError(buf);
3974 return -1; /* can't get here */
3977 /* Helper functions to issue warnings */
3979 static int
3980 issue_warning(char *msg, char *filename, int lineno)
3982 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
3983 lineno, NULL, NULL) < 0) {
3984 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
3985 PyErr_SetString(PyExc_SyntaxError, msg);
3986 PyErr_SyntaxLocation(filename, lineno);
3988 return -1;
3990 return 0;
3993 static int
3994 symtable_warn(struct symtable *st, char *msg)
3996 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
3997 st->st_errors++;
3998 return -1;
4000 return 0;
4003 /* Helper function for setting lineno and filename */
4005 static int
4006 symtable_build(struct compiling *c, node *n)
4008 if ((c->c_symtable = symtable_init()) == NULL)
4009 return -1;
4010 c->c_symtable->st_future = c->c_future;
4011 if (c->c_future->ff_nested_scopes)
4012 c->c_symtable->st_nested_scopes = 1;
4013 c->c_symtable->st_filename = c->c_filename;
4014 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4015 if (c->c_symtable->st_errors > 0)
4016 return -1;
4017 symtable_node(c->c_symtable, n);
4018 if (c->c_symtable->st_errors > 0)
4019 return -1;
4020 /* reset for second pass */
4021 c->c_symtable->st_nscopes = 1;
4022 c->c_symtable->st_pass = 2;
4023 return 0;
4026 static int
4027 symtable_init_compiling_symbols(struct compiling *c)
4029 PyObject *varnames;
4031 varnames = c->c_symtable->st_cur->ste_varnames;
4032 if (varnames == NULL) {
4033 varnames = PyList_New(0);
4034 if (varnames == NULL)
4035 return -1;
4036 c->c_symtable->st_cur->ste_varnames = varnames;
4037 Py_INCREF(varnames);
4038 } else
4039 Py_INCREF(varnames);
4040 c->c_varnames = varnames;
4042 c->c_globals = PyDict_New();
4043 if (c->c_globals == NULL)
4044 return -1;
4045 c->c_freevars = PyDict_New();
4046 if (c->c_freevars == NULL)
4047 return -1;
4048 c->c_cellvars = PyDict_New();
4049 if (c->c_cellvars == NULL)
4050 return -1;
4051 return 0;
4054 struct symbol_info {
4055 int si_nlocals;
4056 int si_ncells;
4057 int si_nfrees;
4058 int si_nimplicit;
4061 static void
4062 symtable_init_info(struct symbol_info *si)
4064 si->si_nlocals = 0;
4065 si->si_ncells = 0;
4066 si->si_nfrees = 0;
4067 si->si_nimplicit = 0;
4070 static int
4071 symtable_resolve_free(struct compiling *c, PyObject *name,
4072 struct symbol_info *si)
4074 PyObject *dict, *v;
4076 /* Seperate logic for DEF_FREE. If it occurs in a function,
4077 it indicates a local that we must allocate storage for (a
4078 cell var). If it occurs in a class, then the class has a
4079 method and a free variable with the same name.
4082 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4083 v = PyInt_FromLong(si->si_ncells++);
4084 dict = c->c_cellvars;
4085 } else {
4086 v = PyInt_FromLong(si->si_nfrees++);
4087 dict = c->c_freevars;
4089 if (v == NULL)
4090 return -1;
4091 if (PyDict_SetItem(dict, name, v) < 0) {
4092 Py_DECREF(v);
4093 return -1;
4095 Py_DECREF(v);
4096 return 0;
4099 /* If a variable is a cell and an argument, make sure that appears in
4100 co_cellvars before any variable to its right in varnames.
4104 static int
4105 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4106 PyObject *varnames, int flags)
4108 PyObject *v, *w, *d, *list = NULL;
4109 int i, pos;
4111 if (flags & CO_VARARGS)
4112 argcount++;
4113 if (flags & CO_VARKEYWORDS)
4114 argcount++;
4115 for (i = argcount; --i >= 0; ) {
4116 v = PyList_GET_ITEM(varnames, i);
4117 if (PyDict_GetItem(*cellvars, v)) {
4118 if (list == NULL) {
4119 list = PyList_New(1);
4120 if (list == NULL)
4121 return -1;
4122 PyList_SET_ITEM(list, 0, v);
4123 Py_INCREF(v);
4124 } else
4125 PyList_Insert(list, 0, v);
4128 if (list == NULL || PyList_GET_SIZE(list) == 0)
4129 return 0;
4130 /* There are cellvars that are also arguments. Create a dict
4131 to replace cellvars and put the args at the front.
4133 d = PyDict_New();
4134 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4135 v = PyInt_FromLong(i);
4136 if (v == NULL)
4137 goto fail;
4138 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4139 goto fail;
4140 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4141 goto fail;
4143 pos = 0;
4144 i = PyList_GET_SIZE(list);
4145 Py_DECREF(list);
4146 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4147 w = PyInt_FromLong(i++); /* don't care about the old key */
4148 if (PyDict_SetItem(d, v, w) < 0) {
4149 Py_DECREF(w);
4150 goto fail;
4152 Py_DECREF(w);
4154 Py_DECREF(*cellvars);
4155 *cellvars = d;
4156 return 1;
4157 fail:
4158 Py_DECREF(d);
4159 return -1;
4162 static int
4163 symtable_freevar_offsets(PyObject *freevars, int offset)
4165 PyObject *name, *v;
4166 int pos;
4168 /* The cell vars are the first elements of the closure,
4169 followed by the free vars. Update the offsets in
4170 c_freevars to account for number of cellvars. */
4171 pos = 0;
4172 while (PyDict_Next(freevars, &pos, &name, &v)) {
4173 int i = PyInt_AS_LONG(v) + offset;
4174 PyObject *o = PyInt_FromLong(i);
4175 if (o == NULL)
4176 return -1;
4177 if (PyDict_SetItem(freevars, name, o) < 0) {
4178 Py_DECREF(o);
4179 return -1;
4181 Py_DECREF(o);
4183 return 0;
4186 static int
4187 symtable_check_unoptimized(struct compiling *c,
4188 PySymtableEntryObject *ste,
4189 struct symbol_info *si)
4191 char buf[300];
4193 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4194 || (ste->ste_nested && si->si_nimplicit)))
4195 return 0;
4197 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4199 #define ILLEGAL_IS "is a nested function"
4201 #define ILLEGAL_IMPORT_STAR \
4202 "import * is not allowed in function '%.100s' because it %s"
4204 #define ILLEGAL_BARE_EXEC \
4205 "unqualified exec is not allowed in function '%.100s' it %s"
4207 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4208 "function '%.100s' uses import * and bare exec, which are illegal" \
4209 "because it %s"
4211 /* XXX perhaps the linenos for these opt-breaking statements
4212 should be stored so the exception can point to them. */
4214 if (ste->ste_child_free) {
4215 if (ste->ste_optimized == OPT_IMPORT_STAR)
4216 sprintf(buf, ILLEGAL_IMPORT_STAR,
4217 PyString_AS_STRING(ste->ste_name),
4218 ILLEGAL_CONTAINS);
4219 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4220 sprintf(buf, ILLEGAL_BARE_EXEC,
4221 PyString_AS_STRING(ste->ste_name),
4222 ILLEGAL_CONTAINS);
4223 else {
4224 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4225 PyString_AS_STRING(ste->ste_name),
4226 ILLEGAL_CONTAINS);
4228 } else {
4229 if (ste->ste_optimized == OPT_IMPORT_STAR)
4230 sprintf(buf, ILLEGAL_IMPORT_STAR,
4231 PyString_AS_STRING(ste->ste_name),
4232 ILLEGAL_IS);
4233 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4234 sprintf(buf, ILLEGAL_BARE_EXEC,
4235 PyString_AS_STRING(ste->ste_name),
4236 ILLEGAL_IS);
4237 else {
4238 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4239 PyString_AS_STRING(ste->ste_name),
4240 ILLEGAL_IS);
4244 if (c->c_symtable->st_nested_scopes) {
4245 PyErr_SetString(PyExc_SyntaxError, buf);
4246 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4247 ste->ste_opt_lineno);
4248 return -1;
4250 else {
4251 return issue_warning(buf, c->c_filename, ste->ste_lineno);
4253 return 0;
4256 static int
4257 symtable_check_shadow(struct symtable *st, PyObject *name, int flags)
4259 char buf[500];
4260 PyObject *children, *v;
4261 PySymtableEntryObject *child = NULL;
4262 int i;
4264 if (!(flags & DEF_BOUND))
4265 return 0;
4266 /* The semantics of this code will change with nested scopes.
4267 It is defined in the current scope and referenced in a
4268 child scope. Under the old rules, the child will see a
4269 global. Under the new rules, the child will see the
4270 binding in the current scope.
4273 /* Find name of child function that has free variable */
4274 children = st->st_cur->ste_children;
4275 for (i = 0; i < PyList_GET_SIZE(children); i++) {
4276 int cflags;
4277 child = (PySymtableEntryObject *)PyList_GET_ITEM(children, i);
4278 v = PyDict_GetItem(child->ste_symbols, name);
4279 if (v == NULL)
4280 continue;
4281 cflags = PyInt_AS_LONG(v);
4282 if (!(cflags & DEF_BOUND))
4283 break;
4286 assert(child != NULL);
4288 sprintf(buf, "local name '%.100s' in '%.100s' shadows "
4289 "use of '%.100s' as global in nested scope '%.100s'",
4290 PyString_AS_STRING(name),
4291 PyString_AS_STRING(st->st_cur->ste_name),
4292 PyString_AS_STRING(name),
4293 PyString_AS_STRING(child->ste_name)
4296 return symtable_warn(st, buf);
4299 static int
4300 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4301 struct symbol_info *si)
4303 if (c->c_future && c->c_future->ff_nested_scopes)
4304 c->c_flags |= CO_NESTED;
4305 if (ste->ste_type != TYPE_MODULE)
4306 c->c_flags |= CO_NEWLOCALS;
4307 if (ste->ste_type == TYPE_FUNCTION) {
4308 c->c_nlocals = si->si_nlocals;
4309 if (ste->ste_optimized == 0)
4310 c->c_flags |= CO_OPTIMIZED;
4311 else if (ste->ste_optimized != OPT_EXEC)
4312 return symtable_check_unoptimized(c, ste, si);
4314 return 0;
4317 static int
4318 symtable_load_symbols(struct compiling *c)
4320 static PyObject *implicit = NULL;
4321 struct symtable *st = c->c_symtable;
4322 PySymtableEntryObject *ste = st->st_cur;
4323 PyObject *name, *varnames, *v;
4324 int i, flags, pos;
4325 struct symbol_info si;
4327 if (implicit == NULL) {
4328 implicit = PyInt_FromLong(1);
4329 if (implicit == NULL)
4330 return -1;
4332 v = NULL;
4334 if (symtable_init_compiling_symbols(c) < 0)
4335 goto fail;
4336 symtable_init_info(&si);
4337 varnames = st->st_cur->ste_varnames;
4338 si.si_nlocals = PyList_GET_SIZE(varnames);
4339 c->c_argcount = si.si_nlocals;
4341 for (i = 0; i < si.si_nlocals; ++i) {
4342 v = PyInt_FromLong(i);
4343 if (PyDict_SetItem(c->c_locals,
4344 PyList_GET_ITEM(varnames, i), v) < 0)
4345 goto fail;
4346 Py_DECREF(v);
4349 /* XXX The cases below define the rules for whether a name is
4350 local or global. The logic could probably be clearer. */
4351 pos = 0;
4352 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4353 flags = PyInt_AS_LONG(v);
4355 if (st->st_nested_scopes == 0
4356 && (flags & (DEF_FREE | DEF_FREE_CLASS))) {
4357 if (symtable_check_shadow(st, name, flags) < 0)
4358 goto fail;
4361 if (flags & DEF_FREE_GLOBAL)
4362 /* undo the original DEF_FREE */
4363 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4365 /* Deal with names that need two actions:
4366 1. Cell variables, which are also locals.
4367 2. Free variables in methods that are also class
4368 variables or declared global.
4370 if (flags & (DEF_FREE | DEF_FREE_CLASS)) {
4371 if ((ste->ste_type == TYPE_CLASS
4372 && flags != DEF_FREE_CLASS)
4373 || (flags & (DEF_LOCAL | DEF_PARAM)))
4374 symtable_resolve_free(c, name, &si);
4377 if (flags & DEF_STAR) {
4378 c->c_argcount--;
4379 c->c_flags |= CO_VARARGS;
4380 } else if (flags & DEF_DOUBLESTAR) {
4381 c->c_argcount--;
4382 c->c_flags |= CO_VARKEYWORDS;
4383 } else if (flags & DEF_INTUPLE)
4384 c->c_argcount--;
4385 else if (flags & DEF_GLOBAL) {
4386 if (flags & DEF_PARAM) {
4387 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4388 PyString_AS_STRING(name));
4389 PyErr_SyntaxLocation(st->st_filename,
4390 ste->ste_lineno);
4391 st->st_errors++;
4392 goto fail;
4394 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4395 goto fail;
4396 } else if (flags & DEF_FREE_GLOBAL) {
4397 si.si_nimplicit++;
4398 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4399 goto fail;
4400 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4401 v = PyInt_FromLong(si.si_nlocals++);
4402 if (v == NULL)
4403 goto fail;
4404 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4405 goto fail;
4406 Py_DECREF(v);
4407 if (ste->ste_type != TYPE_CLASS)
4408 if (PyList_Append(c->c_varnames, name) < 0)
4409 goto fail;
4410 } else if (is_free(flags)) {
4411 if (ste->ste_nested && st->st_nested_scopes) {
4412 v = PyInt_FromLong(si.si_nfrees++);
4413 if (v == NULL)
4414 goto fail;
4415 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4416 goto fail;
4417 Py_DECREF(v);
4418 } else {
4419 si.si_nimplicit++;
4420 if (PyDict_SetItem(c->c_globals, name,
4421 implicit) < 0)
4422 goto fail;
4423 if (st->st_nscopes != 1) {
4424 v = PyInt_FromLong(flags);
4425 if (PyDict_SetItem(st->st_global,
4426 name, v))
4427 goto fail;
4428 Py_DECREF(v);
4434 if (si.si_ncells > 1) { /* one cell is always in order */
4435 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4436 c->c_varnames, c->c_flags) < 0)
4437 return -1;
4439 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4440 return -1;
4441 return symtable_update_flags(c, ste, &si);
4442 fail:
4443 /* is this always the right thing to do? */
4444 Py_XDECREF(v);
4445 return -1;
4448 static struct symtable *
4449 symtable_init()
4451 struct symtable *st;
4453 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4454 if (st == NULL)
4455 return NULL;
4456 st->st_pass = 1;
4457 st->st_nested_scopes = NESTED_SCOPES_DEFAULT;
4458 st->st_filename = NULL;
4459 if ((st->st_stack = PyList_New(0)) == NULL)
4460 goto fail;
4461 if ((st->st_symbols = PyDict_New()) == NULL)
4462 goto fail;
4463 st->st_cur = NULL;
4464 st->st_nscopes = 0;
4465 st->st_errors = 0;
4466 st->st_tmpname = 0;
4467 st->st_private = NULL;
4468 return st;
4469 fail:
4470 PySymtable_Free(st);
4471 return NULL;
4474 void
4475 PySymtable_Free(struct symtable *st)
4477 Py_XDECREF(st->st_symbols);
4478 Py_XDECREF(st->st_stack);
4479 Py_XDECREF(st->st_cur);
4480 PyMem_Free((void *)st);
4483 /* When the compiler exits a scope, it must should update the scope's
4484 free variable information with the list of free variables in its
4485 children.
4487 Variables that are free in children and defined in the current
4488 scope are cellvars.
4490 If the scope being exited is defined at the top-level (ste_nested is
4491 false), free variables in children that are not defined here are
4492 implicit globals.
4496 static int
4497 symtable_update_free_vars(struct symtable *st)
4499 int i, j, def;
4500 PyObject *o, *name, *list = NULL;
4501 PySymtableEntryObject *child, *ste = st->st_cur;
4503 if (ste->ste_type == TYPE_CLASS)
4504 def = DEF_FREE_CLASS;
4505 else
4506 def = DEF_FREE;
4507 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4508 int pos = 0;
4510 if (list)
4511 PyList_SetSlice(list, 0,
4512 ((PyVarObject*)list)->ob_size, 0);
4513 child = (PySymtableEntryObject *)
4514 PyList_GET_ITEM(ste->ste_children, i);
4515 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4516 int flags = PyInt_AS_LONG(o);
4517 if (!(is_free(flags)))
4518 continue; /* avoids indentation */
4519 if (list == NULL) {
4520 list = PyList_New(0);
4521 if (list == NULL)
4522 return -1;
4524 ste->ste_child_free = 1;
4525 if (PyList_Append(list, name) < 0) {
4526 Py_DECREF(list);
4527 return -1;
4530 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4531 PyObject *v;
4532 name = PyList_GET_ITEM(list, j);
4533 v = PyDict_GetItem(ste->ste_symbols, name);
4534 /* If a name N is declared global in scope A and
4535 referenced in scope B contained (perhaps
4536 indirectly) in A and there are no scopes
4537 with bindings for N between B and A, then N
4538 is global in B.
4540 if (v && (ste->ste_type != TYPE_CLASS)) {
4541 int flags = PyInt_AS_LONG(v);
4542 if (flags & DEF_GLOBAL) {
4543 symtable_undo_free(st, child->ste_id,
4544 name);
4545 continue;
4548 if (ste->ste_nested) {
4549 if (symtable_add_def_o(st, ste->ste_symbols,
4550 name, def) < 0) {
4551 Py_DECREF(list);
4552 return -1;
4554 } else {
4555 if (symtable_check_global(st, child->ste_id,
4556 name) < 0) {
4557 Py_DECREF(list);
4558 return -1;
4564 Py_XDECREF(list);
4565 return 0;
4568 /* If the current scope is a non-nested class or if name is not
4569 defined in the current, non-nested scope, then it is an implicit
4570 global in all nested scopes.
4573 static int
4574 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4576 PyObject *o;
4577 int v;
4578 PySymtableEntryObject *ste = st->st_cur;
4580 if (ste->ste_type == TYPE_CLASS)
4581 return symtable_undo_free(st, child, name);
4582 o = PyDict_GetItem(ste->ste_symbols, name);
4583 if (o == NULL)
4584 return symtable_undo_free(st, child, name);
4585 v = PyInt_AS_LONG(o);
4587 if (is_free(v) || (v & DEF_GLOBAL))
4588 return symtable_undo_free(st, child, name);
4589 else
4590 return symtable_add_def_o(st, ste->ste_symbols,
4591 name, DEF_FREE);
4594 static int
4595 symtable_undo_free(struct symtable *st, PyObject *id,
4596 PyObject *name)
4598 int i, v, x;
4599 PyObject *info;
4600 PySymtableEntryObject *ste;
4602 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4603 if (ste == NULL)
4604 return -1;
4606 info = PyDict_GetItem(ste->ste_symbols, name);
4607 if (info == NULL)
4608 return 0;
4609 v = PyInt_AS_LONG(info);
4610 if (is_free(v)) {
4611 if (symtable_add_def_o(st, ste->ste_symbols, name,
4612 DEF_FREE_GLOBAL) < 0)
4613 return -1;
4614 } else
4615 /* If the name is defined here or declared global,
4616 then the recursion stops. */
4617 return 0;
4619 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4620 PySymtableEntryObject *child;
4621 child = (PySymtableEntryObject *)
4622 PyList_GET_ITEM(ste->ste_children, i);
4623 x = symtable_undo_free(st, child->ste_id, name);
4624 if (x < 0)
4625 return x;
4627 return 0;
4630 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4631 This reference is released when the scope is exited, via the DECREF
4632 in symtable_exit_scope().
4635 static int
4636 symtable_exit_scope(struct symtable *st)
4638 int end;
4640 if (st->st_pass == 1)
4641 symtable_update_free_vars(st);
4642 Py_DECREF(st->st_cur);
4643 end = PyList_GET_SIZE(st->st_stack) - 1;
4644 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4645 end);
4646 if (PySequence_DelItem(st->st_stack, end) < 0)
4647 return -1;
4648 return 0;
4651 static void
4652 symtable_enter_scope(struct symtable *st, char *name, int type,
4653 int lineno)
4655 PySymtableEntryObject *prev = NULL;
4657 if (st->st_cur) {
4658 prev = st->st_cur;
4659 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4660 Py_DECREF(st->st_cur);
4661 st->st_errors++;
4662 return;
4665 st->st_cur = (PySymtableEntryObject *)
4666 PySymtableEntry_New(st, name, type, lineno);
4667 if (strcmp(name, TOP) == 0)
4668 st->st_global = st->st_cur->ste_symbols;
4669 if (prev && st->st_pass == 1) {
4670 if (PyList_Append(prev->ste_children,
4671 (PyObject *)st->st_cur) < 0)
4672 st->st_errors++;
4676 static int
4677 symtable_lookup(struct symtable *st, char *name)
4679 char buffer[MANGLE_LEN];
4680 PyObject *v;
4681 int flags;
4683 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4684 name = buffer;
4685 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4686 if (v == NULL) {
4687 if (PyErr_Occurred())
4688 return -1;
4689 else
4690 return 0;
4693 flags = PyInt_AS_LONG(v);
4694 return flags;
4697 static int
4698 symtable_add_def(struct symtable *st, char *name, int flag)
4700 PyObject *s;
4701 char buffer[MANGLE_LEN];
4702 int ret;
4704 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4705 name = buffer;
4706 if ((s = PyString_InternFromString(name)) == NULL)
4707 return -1;
4708 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4709 Py_DECREF(s);
4710 return ret;
4713 /* Must only be called with mangled names */
4715 static int
4716 symtable_add_def_o(struct symtable *st, PyObject *dict,
4717 PyObject *name, int flag)
4719 PyObject *o;
4720 int val;
4722 if ((o = PyDict_GetItem(dict, name))) {
4723 val = PyInt_AS_LONG(o);
4724 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
4725 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4726 PyString_AsString(name));
4727 PyErr_SyntaxLocation(st->st_filename,
4728 st->st_cur->ste_lineno);
4729 return -1;
4731 val |= flag;
4732 } else
4733 val = flag;
4734 o = PyInt_FromLong(val);
4735 if (PyDict_SetItem(dict, name, o) < 0) {
4736 Py_DECREF(o);
4737 return -1;
4739 Py_DECREF(o);
4741 if (flag & DEF_PARAM) {
4742 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
4743 return -1;
4744 } else if (flag & DEF_GLOBAL) {
4745 /* XXX need to update DEF_GLOBAL for other flags too;
4746 perhaps only DEF_FREE_GLOBAL */
4747 if ((o = PyDict_GetItem(st->st_global, name))) {
4748 val = PyInt_AS_LONG(o);
4749 val |= flag;
4750 } else
4751 val = flag;
4752 o = PyInt_FromLong(val);
4753 if (PyDict_SetItem(st->st_global, name, o) < 0) {
4754 Py_DECREF(o);
4755 return -1;
4757 Py_DECREF(o);
4759 return 0;
4762 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4764 static void
4765 symtable_node(struct symtable *st, node *n)
4767 int i, start = 0;
4769 loop:
4770 switch (TYPE(n)) {
4771 case funcdef: {
4772 char *func_name = STR(CHILD(n, 1));
4773 symtable_add_def(st, func_name, DEF_LOCAL);
4774 symtable_default_args(st, CHILD(n, 2));
4775 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
4776 symtable_funcdef(st, n);
4777 symtable_exit_scope(st);
4778 break;
4780 case lambdef:
4781 if (NCH(n) == 4)
4782 symtable_default_args(st, CHILD(n, 1));
4783 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
4784 symtable_funcdef(st, n);
4785 symtable_exit_scope(st);
4786 break;
4787 case classdef: {
4788 char *tmp, *class_name = STR(CHILD(n, 1));
4789 symtable_add_def(st, class_name, DEF_LOCAL);
4790 if (TYPE(CHILD(n, 2)) == LPAR) {
4791 node *bases = CHILD(n, 3);
4792 int i;
4793 for (i = 0; i < NCH(bases); i += 2) {
4794 symtable_node(st, CHILD(bases, i));
4797 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
4798 tmp = st->st_private;
4799 st->st_private = class_name;
4800 symtable_node(st, CHILD(n, NCH(n) - 1));
4801 st->st_private = tmp;
4802 symtable_exit_scope(st);
4803 break;
4805 case if_stmt:
4806 for (i = 0; i + 3 < NCH(n); i += 4) {
4807 if (is_constant_false(NULL, (CHILD(n, i + 1))))
4808 continue;
4809 symtable_node(st, CHILD(n, i + 1));
4810 symtable_node(st, CHILD(n, i + 3));
4812 if (i + 2 < NCH(n))
4813 symtable_node(st, CHILD(n, i + 2));
4814 break;
4815 case global_stmt:
4816 symtable_global(st, n);
4817 break;
4818 case import_stmt:
4819 symtable_import(st, n);
4820 break;
4821 case exec_stmt: {
4822 st->st_cur->ste_optimized |= OPT_EXEC;
4823 symtable_node(st, CHILD(n, 1));
4824 if (NCH(n) > 2)
4825 symtable_node(st, CHILD(n, 3));
4826 else {
4827 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
4828 st->st_cur->ste_opt_lineno = n->n_lineno;
4830 if (NCH(n) > 4)
4831 symtable_node(st, CHILD(n, 5));
4832 break;
4835 case assert_stmt:
4836 if (Py_OptimizeFlag)
4837 return;
4838 if (NCH(n) == 2) {
4839 n = CHILD(n, 1);
4840 goto loop;
4841 } else {
4842 symtable_node(st, CHILD(n, 1));
4843 n = CHILD(n, 3);
4844 goto loop;
4846 case except_clause:
4847 if (NCH(n) == 4)
4848 symtable_assign(st, CHILD(n, 3), 0);
4849 if (NCH(n) > 1) {
4850 n = CHILD(n, 1);
4851 goto loop;
4853 break;
4854 case del_stmt:
4855 symtable_assign(st, CHILD(n, 1), 0);
4856 break;
4857 case expr_stmt:
4858 if (NCH(n) == 1)
4859 n = CHILD(n, 0);
4860 else {
4861 if (TYPE(CHILD(n, 1)) == augassign) {
4862 symtable_assign(st, CHILD(n, 0), 0);
4863 symtable_node(st, CHILD(n, 2));
4864 break;
4865 } else {
4866 int i;
4867 for (i = 0; i < NCH(n) - 2; i += 2)
4868 symtable_assign(st, CHILD(n, i), 0);
4869 n = CHILD(n, NCH(n) - 1);
4872 goto loop;
4873 /* watchout for fall-through logic below */
4874 case argument:
4875 if (NCH(n) == 3) {
4876 n = CHILD(n, 2);
4877 goto loop;
4879 case listmaker:
4880 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
4881 st->st_tmpname++;
4882 symtable_list_comprehension(st, CHILD(n, 1));
4883 symtable_node(st, CHILD(n, 0));
4884 st->st_tmpname--;
4885 return;
4887 case atom:
4888 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
4889 symtable_add_use(st, STR(CHILD(n, 0)));
4890 break;
4892 case for_stmt:
4893 if (TYPE(n) == for_stmt) {
4894 symtable_assign(st, CHILD(n, 1), 0);
4895 start = 3;
4897 default:
4898 if (NCH(n) == 1) {
4899 n = CHILD(n, 0);
4900 goto loop;
4902 for (i = start; i < NCH(n); ++i)
4903 if (TYPE(CHILD(n, i)) >= single_input)
4904 symtable_node(st, CHILD(n, i));
4908 static void
4909 symtable_funcdef(struct symtable *st, node *n)
4911 node *body;
4913 if (TYPE(n) == lambdef) {
4914 if (NCH(n) == 4)
4915 symtable_params(st, CHILD(n, 1));
4916 } else
4917 symtable_params(st, CHILD(n, 2));
4918 body = CHILD(n, NCH(n) - 1);
4919 symtable_node(st, body);
4922 /* The next two functions parse the argument tuple.
4923 symtable_default_arg() checks for names in the default arguments,
4924 which are references in the defining scope. symtable_params()
4925 parses the parameter names, which are defined in the function's
4926 body.
4928 varargslist:
4929 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
4930 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4933 static void
4934 symtable_default_args(struct symtable *st, node *n)
4936 node *c;
4937 int i;
4939 if (TYPE(n) == parameters) {
4940 n = CHILD(n, 1);
4941 if (TYPE(n) == RPAR)
4942 return;
4944 REQ(n, varargslist);
4945 for (i = 0; i < NCH(n); i += 2) {
4946 c = CHILD(n, i);
4947 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4948 break;
4950 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
4951 symtable_node(st, CHILD(n, i));
4955 static void
4956 symtable_params(struct symtable *st, node *n)
4958 int i, complex = -1, ext = 0;
4959 node *c = NULL;
4961 if (TYPE(n) == parameters) {
4962 n = CHILD(n, 1);
4963 if (TYPE(n) == RPAR)
4964 return;
4966 REQ(n, varargslist);
4967 for (i = 0; i < NCH(n); i += 2) {
4968 c = CHILD(n, i);
4969 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4970 ext = 1;
4971 break;
4973 if (TYPE(c) == test) {
4974 continue;
4976 if (TYPE(CHILD(c, 0)) == NAME)
4977 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
4978 else {
4979 char nbuf[10];
4980 sprintf(nbuf, ".%d", i);
4981 symtable_add_def(st, nbuf, DEF_PARAM);
4982 complex = i;
4985 if (ext) {
4986 c = CHILD(n, i);
4987 if (TYPE(c) == STAR) {
4988 i++;
4989 symtable_add_def(st, STR(CHILD(n, i)),
4990 DEF_PARAM | DEF_STAR);
4991 i += 2;
4992 if (i >= NCH(n))
4993 c = NULL;
4994 else
4995 c = CHILD(n, i);
4997 if (c && TYPE(c) == DOUBLESTAR) {
4998 i++;
4999 symtable_add_def(st, STR(CHILD(n, i)),
5000 DEF_PARAM | DEF_DOUBLESTAR);
5003 if (complex >= 0) {
5004 int j;
5005 for (j = 0; j <= complex; j++) {
5006 c = CHILD(n, j);
5007 if (TYPE(c) == COMMA)
5008 c = CHILD(n, ++j);
5009 else if (TYPE(c) == EQUAL)
5010 c = CHILD(n, j += 3);
5011 if (TYPE(CHILD(c, 0)) == LPAR)
5012 symtable_params_fplist(st, CHILD(c, 1));
5017 static void
5018 symtable_params_fplist(struct symtable *st, node *n)
5020 int i;
5021 node *c;
5023 REQ(n, fplist);
5024 for (i = 0; i < NCH(n); i += 2) {
5025 c = CHILD(n, i);
5026 REQ(c, fpdef);
5027 if (NCH(c) == 1)
5028 symtable_add_def(st, STR(CHILD(c, 0)),
5029 DEF_PARAM | DEF_INTUPLE);
5030 else
5031 symtable_params_fplist(st, CHILD(c, 1));
5036 static void
5037 symtable_global(struct symtable *st, node *n)
5039 int i;
5041 /* XXX It might be helpful to warn about module-level global
5042 statements, but it's hard to tell the difference between
5043 module-level and a string passed to exec.
5046 for (i = 1; i < NCH(n); i += 2) {
5047 char *name = STR(CHILD(n, i));
5048 int flags;
5050 flags = symtable_lookup(st, name);
5051 if (flags < 0)
5052 continue;
5053 if (flags && flags != DEF_GLOBAL) {
5054 char buf[500];
5055 if (flags & DEF_PARAM) {
5056 PyErr_Format(PyExc_SyntaxError,
5057 "name '%.400s' is local and global",
5058 name);
5059 PyErr_SyntaxLocation(st->st_filename,
5060 st->st_cur->ste_lineno);
5061 st->st_errors++;
5062 return;
5064 else {
5065 if (flags & DEF_LOCAL)
5066 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5067 name);
5068 else
5069 sprintf(buf, GLOBAL_AFTER_USE, name);
5070 symtable_warn(st, buf);
5073 symtable_add_def(st, name, DEF_GLOBAL);
5077 static void
5078 symtable_list_comprehension(struct symtable *st, node *n)
5080 char tmpname[12];
5082 sprintf(tmpname, "_[%d]", st->st_tmpname);
5083 symtable_add_def(st, tmpname, DEF_LOCAL);
5084 symtable_assign(st, CHILD(n, 1), 0);
5085 symtable_node(st, CHILD(n, 3));
5086 if (NCH(n) == 5)
5087 symtable_node(st, CHILD(n, 4));
5090 static void
5091 symtable_import(struct symtable *st, node *n)
5093 int i;
5094 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5095 | 'from' dotted_name 'import'
5096 ('*' | import_as_name (',' import_as_name)*)
5097 import_as_name: NAME [NAME NAME]
5099 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5100 node *dotname = CHILD(n, 1);
5101 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5102 /* check for bogus imports */
5103 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5104 PyErr_SetString(PyExc_SyntaxError,
5105 LATE_FUTURE);
5106 PyErr_SyntaxLocation(st->st_filename,
5107 n->n_lineno);
5108 st->st_errors++;
5109 return;
5112 if (TYPE(CHILD(n, 3)) == STAR) {
5113 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5114 st->st_cur->ste_opt_lineno = n->n_lineno;
5115 } else {
5116 for (i = 3; i < NCH(n); i += 2) {
5117 node *c = CHILD(n, i);
5118 if (NCH(c) > 1) /* import as */
5119 symtable_assign(st, CHILD(c, 2),
5120 DEF_IMPORT);
5121 else
5122 symtable_assign(st, CHILD(c, 0),
5123 DEF_IMPORT);
5126 } else {
5127 for (i = 1; i < NCH(n); i += 2) {
5128 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5133 static void
5134 symtable_assign(struct symtable *st, node *n, int flag)
5136 node *tmp;
5137 int i;
5139 loop:
5140 switch (TYPE(n)) {
5141 case lambdef:
5142 /* invalid assignment, e.g. lambda x:x=2. The next
5143 pass will catch this error. */
5144 return;
5145 case power:
5146 if (NCH(n) > 2) {
5147 for (i = 2; i < NCH(n); ++i)
5148 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5149 symtable_node(st, CHILD(n, i));
5151 if (NCH(n) > 1) {
5152 symtable_node(st, CHILD(n, 0));
5153 symtable_node(st, CHILD(n, 1));
5154 } else {
5155 n = CHILD(n, 0);
5156 goto loop;
5158 return;
5159 case listmaker:
5160 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5161 /* XXX This is an error, but the next pass
5162 will catch it. */
5163 return;
5164 } else {
5165 for (i = 0; i < NCH(n); i += 2)
5166 symtable_assign(st, CHILD(n, i), flag);
5168 return;
5169 case exprlist:
5170 case testlist:
5171 if (NCH(n) == 1) {
5172 n = CHILD(n, 0);
5173 goto loop;
5175 else {
5176 int i;
5177 for (i = 0; i < NCH(n); i += 2)
5178 symtable_assign(st, CHILD(n, i), flag);
5179 return;
5181 goto loop;
5182 case atom:
5183 tmp = CHILD(n, 0);
5184 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5185 n = CHILD(n, 1);
5186 goto loop;
5187 } else if (TYPE(tmp) == NAME) {
5188 if (strcmp(STR(tmp), "__debug__") == 0) {
5189 PyErr_SetString(PyExc_SyntaxError,
5190 ASSIGN_DEBUG);
5191 PyErr_SyntaxLocation(st->st_filename,
5192 n->n_lineno);
5193 st->st_errors++;
5195 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
5197 return;
5198 case dotted_as_name:
5199 if (NCH(n) == 3)
5200 symtable_add_def(st, STR(CHILD(n, 2)),
5201 DEF_LOCAL | flag);
5202 else
5203 symtable_add_def(st,
5204 STR(CHILD(CHILD(n,
5205 0), 0)),
5206 DEF_LOCAL | flag);
5207 return;
5208 case dotted_name:
5209 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
5210 return;
5211 case NAME:
5212 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
5213 return;
5214 default:
5215 if (NCH(n) == 0)
5216 return;
5217 if (NCH(n) == 1) {
5218 n = CHILD(n, 0);
5219 goto loop;
5221 /* Should only occur for errors like x + 1 = 1,
5222 which will be caught in the next pass. */
5223 for (i = 0; i < NCH(n); ++i)
5224 if (TYPE(CHILD(n, i)) >= single_input)
5225 symtable_assign(st, CHILD(n, i), flag);