Files for 2.1b1 distribution.
[python/dscho.git] / Python / compile.c
blobf553262de14bdc53162b602cfed1b549d335fb49
2 /* Compile an expression node to intermediate code */
4 /* XXX TO DO:
5 XXX add __doc__ attribute == co_doc to code object attributes?
6 XXX (it's currently the first item of the co_const tuple)
7 XXX Generate simple jump for break/return outside 'try...finally'
8 XXX Allow 'continue' inside finally clause of try-finally
9 XXX New opcode for loading the initial index for a for loop
10 XXX other JAR tricks?
13 #include "Python.h"
15 #include "node.h"
16 #include "token.h"
17 #include "graminit.h"
18 #include "compile.h"
19 #include "symtable.h"
20 #include "opcode.h"
21 #include "structmember.h"
23 #include <ctype.h>
25 /* Three symbols from graminit.h are also defined in Python.h, with
26 Py_ prefixes to their names. Python.h can't include graminit.h
27 (which defines too many confusing symbols), but we can check here
28 that they haven't changed (which is very unlikely, but possible). */
29 #if Py_single_input != single_input
30 #error "single_input has changed -- update Py_single_input in Python.h"
31 #endif
32 #if Py_file_input != file_input
33 #error "file_input has changed -- update Py_file_input in Python.h"
34 #endif
35 #if Py_eval_input != eval_input
36 #error "eval_input has changed -- update Py_eval_input in Python.h"
37 #endif
39 int Py_OptimizeFlag = 0;
41 #define OP_DELETE 0
42 #define OP_ASSIGN 1
43 #define OP_APPLY 2
45 #define VAR_LOAD 0
46 #define VAR_STORE 1
47 #define VAR_DELETE 2
49 #define DEL_CLOSURE_ERROR \
50 "can not delete variable '%.400s' referenced in nested scope"
52 #define DUPLICATE_ARGUMENT \
53 "duplicate argument '%s' in function definition"
55 #define ILLEGAL_DYNAMIC_SCOPE \
56 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
58 #define GLOBAL_AFTER_ASSIGN \
59 "name '%.400s' is assigned to before global declaration"
61 #define GLOBAL_AFTER_USE \
62 "name '%.400s' is used prior to global declaration"
64 #define LOCAL_GLOBAL \
65 "name '%.400s' is a function paramter and declared global"
67 #define LATE_FUTURE \
68 "from __future__ imports must occur at the beginning of the file"
70 #define MANGLE_LEN 256
72 #define OFF(x) offsetof(PyCodeObject, x)
74 static struct memberlist code_memberlist[] = {
75 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
76 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
77 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
78 {"co_flags", T_INT, OFF(co_flags), READONLY},
79 {"co_code", T_OBJECT, OFF(co_code), READONLY},
80 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
81 {"co_names", T_OBJECT, OFF(co_names), READONLY},
82 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
83 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
84 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
85 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
86 {"co_name", T_OBJECT, OFF(co_name), READONLY},
87 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
88 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
89 {NULL} /* Sentinel */
92 static PyObject *
93 code_getattr(PyCodeObject *co, char *name)
95 return PyMember_Get((char *)co, code_memberlist, name);
98 static void
99 code_dealloc(PyCodeObject *co)
101 Py_XDECREF(co->co_code);
102 Py_XDECREF(co->co_consts);
103 Py_XDECREF(co->co_names);
104 Py_XDECREF(co->co_varnames);
105 Py_XDECREF(co->co_freevars);
106 Py_XDECREF(co->co_cellvars);
107 Py_XDECREF(co->co_filename);
108 Py_XDECREF(co->co_name);
109 Py_XDECREF(co->co_lnotab);
110 PyObject_DEL(co);
113 static PyObject *
114 code_repr(PyCodeObject *co)
116 char buf[500];
117 int lineno = -1;
118 char *filename = "???";
119 char *name = "???";
121 if (co->co_firstlineno != 0)
122 lineno = co->co_firstlineno;
123 if (co->co_filename && PyString_Check(co->co_filename))
124 filename = PyString_AS_STRING(co->co_filename);
125 if (co->co_name && PyString_Check(co->co_name))
126 name = PyString_AS_STRING(co->co_name);
127 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
128 name, co, filename, lineno);
129 return PyString_FromString(buf);
132 static int
133 code_compare(PyCodeObject *co, PyCodeObject *cp)
135 int cmp;
136 cmp = PyObject_Compare(co->co_name, cp->co_name);
137 if (cmp) return cmp;
138 cmp = co->co_argcount - cp->co_argcount;
139 if (cmp) return cmp;
140 cmp = co->co_nlocals - cp->co_nlocals;
141 if (cmp) return cmp;
142 cmp = co->co_flags - cp->co_flags;
143 if (cmp) return cmp;
144 cmp = PyObject_Compare(co->co_code, cp->co_code);
145 if (cmp) return cmp;
146 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
147 if (cmp) return cmp;
148 cmp = PyObject_Compare(co->co_names, cp->co_names);
149 if (cmp) return cmp;
150 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
151 if (cmp) return cmp;
152 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
153 if (cmp) return cmp;
154 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
155 return cmp;
158 static long
159 code_hash(PyCodeObject *co)
161 long h, h0, h1, h2, h3, h4, h5, h6;
162 h0 = PyObject_Hash(co->co_name);
163 if (h0 == -1) return -1;
164 h1 = PyObject_Hash(co->co_code);
165 if (h1 == -1) return -1;
166 h2 = PyObject_Hash(co->co_consts);
167 if (h2 == -1) return -1;
168 h3 = PyObject_Hash(co->co_names);
169 if (h3 == -1) return -1;
170 h4 = PyObject_Hash(co->co_varnames);
171 if (h4 == -1) return -1;
172 h5 = PyObject_Hash(co->co_freevars);
173 if (h5 == -1) return -1;
174 h6 = PyObject_Hash(co->co_cellvars);
175 if (h6 == -1) return -1;
176 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
177 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
178 if (h == -1) h = -2;
179 return h;
182 /* XXX code objects need to participate in GC? */
184 PyTypeObject PyCode_Type = {
185 PyObject_HEAD_INIT(&PyType_Type)
187 "code",
188 sizeof(PyCodeObject),
190 (destructor)code_dealloc, /*tp_dealloc*/
191 0, /*tp_print*/
192 (getattrfunc)code_getattr, /*tp_getattr*/
193 0, /*tp_setattr*/
194 (cmpfunc)code_compare, /*tp_compare*/
195 (reprfunc)code_repr, /*tp_repr*/
196 0, /*tp_as_number*/
197 0, /*tp_as_sequence*/
198 0, /*tp_as_mapping*/
199 (hashfunc)code_hash, /*tp_hash*/
202 #define NAME_CHARS \
203 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
205 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
207 static int
208 all_name_chars(unsigned char *s)
210 static char ok_name_char[256];
211 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
213 if (ok_name_char[*name_chars] == 0) {
214 unsigned char *p;
215 for (p = name_chars; *p; p++)
216 ok_name_char[*p] = 1;
218 while (*s) {
219 if (ok_name_char[*s++] == 0)
220 return 0;
222 return 1;
225 static int
226 intern_strings(PyObject *tuple)
228 int i;
230 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
231 PyObject *v = PyTuple_GET_ITEM(tuple, i);
232 if (v == NULL || !PyString_Check(v)) {
233 Py_FatalError("non-string found in code slot");
234 PyErr_BadInternalCall();
235 return -1;
237 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
239 return 0;
242 PyCodeObject *
243 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
244 PyObject *code, PyObject *consts, PyObject *names,
245 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
246 PyObject *filename, PyObject *name, int firstlineno,
247 PyObject *lnotab)
249 PyCodeObject *co;
250 int i;
251 PyBufferProcs *pb;
252 /* Check argument types */
253 if (argcount < 0 || nlocals < 0 ||
254 code == NULL ||
255 consts == NULL || !PyTuple_Check(consts) ||
256 names == NULL || !PyTuple_Check(names) ||
257 varnames == NULL || !PyTuple_Check(varnames) ||
258 freevars == NULL || !PyTuple_Check(freevars) ||
259 cellvars == NULL || !PyTuple_Check(cellvars) ||
260 name == NULL || !PyString_Check(name) ||
261 filename == NULL || !PyString_Check(filename) ||
262 lnotab == NULL || !PyString_Check(lnotab)) {
263 PyErr_BadInternalCall();
264 return NULL;
266 pb = code->ob_type->tp_as_buffer;
267 if (pb == NULL ||
268 pb->bf_getreadbuffer == NULL ||
269 pb->bf_getsegcount == NULL ||
270 (*pb->bf_getsegcount)(code, NULL) != 1)
272 PyErr_BadInternalCall();
273 return NULL;
275 intern_strings(names);
276 intern_strings(varnames);
277 if (freevars == NULL)
278 freevars = PyTuple_New(0);
279 intern_strings(freevars);
280 if (cellvars == NULL)
281 cellvars = PyTuple_New(0);
282 intern_strings(cellvars);
283 /* Intern selected string constants */
284 for (i = PyTuple_Size(consts); --i >= 0; ) {
285 PyObject *v = PyTuple_GetItem(consts, i);
286 if (!PyString_Check(v))
287 continue;
288 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
289 continue;
290 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
292 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
293 if (co != NULL) {
294 co->co_argcount = argcount;
295 co->co_nlocals = nlocals;
296 co->co_stacksize = stacksize;
297 co->co_flags = flags;
298 Py_INCREF(code);
299 co->co_code = code;
300 Py_INCREF(consts);
301 co->co_consts = consts;
302 Py_INCREF(names);
303 co->co_names = names;
304 Py_INCREF(varnames);
305 co->co_varnames = varnames;
306 Py_INCREF(freevars);
307 co->co_freevars = freevars;
308 Py_INCREF(cellvars);
309 co->co_cellvars = cellvars;
310 Py_INCREF(filename);
311 co->co_filename = filename;
312 Py_INCREF(name);
313 co->co_name = name;
314 co->co_firstlineno = firstlineno;
315 Py_INCREF(lnotab);
316 co->co_lnotab = lnotab;
318 return co;
322 /* Data structure used internally */
324 /* The compiler uses two passes to generate bytecodes. The first pass
325 builds the symbol table. The second pass generates the bytecode.
327 The first pass uses a single symtable struct. The second pass uses
328 a compiling struct for each code block. The compiling structs
329 share a reference to the symtable.
331 The two passes communicate via symtable_load_symbols() and via
332 is_local() and is_global(). The former initializes several slots
333 in the compiling struct: c_varnames, c_locals, c_nlocals,
334 c_argcount, c_globals, and c_flags.
337 struct compiling {
338 PyObject *c_code; /* string */
339 PyObject *c_consts; /* list of objects */
340 PyObject *c_const_dict; /* inverse of c_consts */
341 PyObject *c_names; /* list of strings (names) */
342 PyObject *c_name_dict; /* inverse of c_names */
343 PyObject *c_globals; /* dictionary (value=None) */
344 PyObject *c_locals; /* dictionary (value=localID) */
345 PyObject *c_varnames; /* list (inverse of c_locals) */
346 PyObject *c_freevars; /* dictionary (value=None) */
347 PyObject *c_cellvars; /* list */
348 int c_nlocals; /* index of next local */
349 int c_argcount; /* number of top-level arguments */
350 int c_flags; /* same as co_flags */
351 int c_nexti; /* index into c_code */
352 int c_errors; /* counts errors occurred */
353 int c_infunction; /* set when compiling a function */
354 int c_interactive; /* generating code for interactive command */
355 int c_loops; /* counts nested loops */
356 int c_begin; /* begin of current loop, for 'continue' */
357 int c_block[CO_MAXBLOCKS]; /* stack of block types */
358 int c_nblocks; /* current block stack level */
359 char *c_filename; /* filename of current node */
360 char *c_name; /* name of object (e.g. function) */
361 int c_lineno; /* Current line number */
362 int c_stacklevel; /* Current stack level */
363 int c_maxstacklevel; /* Maximum stack level */
364 int c_firstlineno;
365 PyObject *c_lnotab; /* Table mapping address to line number */
366 int c_last_addr, c_last_line, c_lnotab_next;
367 char *c_private; /* for private name mangling */
368 int c_tmpname; /* temporary local name counter */
369 int c_nested; /* Is block nested funcdef or lamdef? */
370 int c_closure; /* Is nested w/freevars? */
371 struct symtable *c_symtable; /* pointer to module symbol table */
372 PyFutureFeatures *c_future; /* pointer to module's __future__ */
375 int is_free(int v)
377 if ((v & (USE | DEF_FREE))
378 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
379 return 1;
380 if (v & DEF_FREE_CLASS)
381 return 1;
382 return 0;
385 static void
386 com_error(struct compiling *c, PyObject *exc, char *msg)
388 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
390 if (c == NULL) {
391 /* Error occurred via symtable call to
392 is_constant_false */
393 PyErr_SetString(exc, msg);
394 return;
396 c->c_errors++;
397 if (c->c_lineno < 1 || c->c_interactive) {
398 /* Unknown line number or interactive input */
399 PyErr_SetString(exc, msg);
400 return;
402 v = PyString_FromString(msg);
403 if (v == NULL)
404 return; /* MemoryError, too bad */
406 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
407 if (line == NULL) {
408 Py_INCREF(Py_None);
409 line = Py_None;
411 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
412 Py_None, line);
413 if (t == NULL)
414 goto exit;
415 w = Py_BuildValue("(OO)", v, t);
416 if (w == NULL)
417 goto exit;
418 PyErr_SetObject(exc, w);
419 exit:
420 Py_XDECREF(t);
421 Py_XDECREF(v);
422 Py_XDECREF(w);
423 Py_XDECREF(line);
426 /* Interface to the block stack */
428 static void
429 block_push(struct compiling *c, int type)
431 if (c->c_nblocks >= CO_MAXBLOCKS) {
432 com_error(c, PyExc_SystemError,
433 "too many statically nested blocks");
435 else {
436 c->c_block[c->c_nblocks++] = type;
440 static void
441 block_pop(struct compiling *c, int type)
443 if (c->c_nblocks > 0)
444 c->c_nblocks--;
445 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
446 com_error(c, PyExc_SystemError, "bad block pop");
450 /* Prototype forward declarations */
452 static int com_init(struct compiling *, char *);
453 static void com_free(struct compiling *);
454 static void com_push(struct compiling *, int);
455 static void com_pop(struct compiling *, int);
456 static void com_done(struct compiling *);
457 static void com_node(struct compiling *, node *);
458 static void com_factor(struct compiling *, node *);
459 static void com_addbyte(struct compiling *, int);
460 static void com_addint(struct compiling *, int);
461 static void com_addoparg(struct compiling *, int, int);
462 static void com_addfwref(struct compiling *, int, int *);
463 static void com_backpatch(struct compiling *, int);
464 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
465 static int com_addconst(struct compiling *, PyObject *);
466 static int com_addname(struct compiling *, PyObject *);
467 static void com_addopname(struct compiling *, int, node *);
468 static void com_list(struct compiling *, node *, int);
469 static void com_list_iter(struct compiling *, node *, node *, char *);
470 static int com_argdefs(struct compiling *, node *);
471 static void com_assign(struct compiling *, node *, int, node *);
472 static void com_assign_name(struct compiling *, node *, int);
473 static PyCodeObject *icompile(node *, struct compiling *);
474 static PyCodeObject *jcompile(node *, char *, struct compiling *,
475 PyCompilerFlags *);
476 static PyObject *parsestrplus(node *);
477 static PyObject *parsestr(char *);
478 static node *get_rawdocstring(node *);
480 static int get_ref_type(struct compiling *, char *);
482 /* symtable operations */
483 static int symtable_build(struct compiling *, node *);
484 static int symtable_load_symbols(struct compiling *);
485 static struct symtable *symtable_init(void);
486 static void symtable_enter_scope(struct symtable *, char *, int, int);
487 static int symtable_exit_scope(struct symtable *);
488 static int symtable_add_def(struct symtable *, char *, int);
489 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
491 static void symtable_node(struct symtable *, node *);
492 static void symtable_funcdef(struct symtable *, node *);
493 static void symtable_default_args(struct symtable *, node *);
494 static void symtable_params(struct symtable *, node *);
495 static void symtable_params_fplist(struct symtable *, node *n);
496 static void symtable_global(struct symtable *, node *);
497 static void symtable_import(struct symtable *, node *);
498 static void symtable_assign(struct symtable *, node *, int);
499 static void symtable_list_comprehension(struct symtable *, node *);
501 static int symtable_update_free_vars(struct symtable *);
502 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
503 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
505 /* helper */
506 static void
507 do_pad(int pad)
509 int i;
510 for (i = 0; i < pad; ++i)
511 fprintf(stderr, " ");
514 static void
515 dump(node *n, int pad, int depth)
517 int i;
518 if (depth == 0)
519 return;
520 do_pad(pad);
521 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
522 if (depth > 0)
523 depth--;
524 for (i = 0; i < NCH(n); ++i)
525 dump(CHILD(n, i), pad + 1, depth);
528 #define DUMP(N) dump(N, 0, -1)
530 static int
531 com_init(struct compiling *c, char *filename)
533 memset((void *)c, '\0', sizeof(struct compiling));
534 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
535 1000)) == NULL)
536 goto fail;
537 if ((c->c_consts = PyList_New(0)) == NULL)
538 goto fail;
539 if ((c->c_const_dict = PyDict_New()) == NULL)
540 goto fail;
541 if ((c->c_names = PyList_New(0)) == NULL)
542 goto fail;
543 if ((c->c_name_dict = PyDict_New()) == NULL)
544 goto fail;
545 if ((c->c_locals = PyDict_New()) == NULL)
546 goto fail;
547 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
548 1000)) == NULL)
549 goto fail;
550 c->c_globals = NULL;
551 c->c_varnames = NULL;
552 c->c_freevars = NULL;
553 c->c_cellvars = NULL;
554 c->c_nlocals = 0;
555 c->c_argcount = 0;
556 c->c_flags = 0;
557 c->c_nexti = 0;
558 c->c_errors = 0;
559 c->c_infunction = 0;
560 c->c_interactive = 0;
561 c->c_loops = 0;
562 c->c_begin = 0;
563 c->c_nblocks = 0;
564 c->c_filename = filename;
565 c->c_name = "?";
566 c->c_lineno = 0;
567 c->c_stacklevel = 0;
568 c->c_maxstacklevel = 0;
569 c->c_firstlineno = 0;
570 c->c_last_addr = 0;
571 c->c_last_line = 0;
572 c->c_lnotab_next = 0;
573 c->c_tmpname = 0;
574 c->c_nested = 0;
575 c->c_closure = 0;
576 c->c_symtable = NULL;
577 return 1;
579 fail:
580 com_free(c);
581 return 0;
584 static void
585 com_free(struct compiling *c)
587 Py_XDECREF(c->c_code);
588 Py_XDECREF(c->c_consts);
589 Py_XDECREF(c->c_const_dict);
590 Py_XDECREF(c->c_names);
591 Py_XDECREF(c->c_name_dict);
592 Py_XDECREF(c->c_globals);
593 Py_XDECREF(c->c_locals);
594 Py_XDECREF(c->c_varnames);
595 Py_XDECREF(c->c_freevars);
596 Py_XDECREF(c->c_cellvars);
597 Py_XDECREF(c->c_lnotab);
598 if (c->c_future)
599 PyMem_Free((void *)c->c_future);
602 static void
603 com_push(struct compiling *c, int n)
605 c->c_stacklevel += n;
606 if (c->c_stacklevel > c->c_maxstacklevel)
607 c->c_maxstacklevel = c->c_stacklevel;
610 static void
611 com_pop(struct compiling *c, int n)
613 if (c->c_stacklevel < n) {
614 /* fprintf(stderr,
615 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
616 c->c_filename, c->c_lineno,
617 c->c_nexti, c->c_stacklevel, n); */
618 c->c_stacklevel = 0;
620 else
621 c->c_stacklevel -= n;
624 static void
625 com_done(struct compiling *c)
627 if (c->c_code != NULL)
628 _PyString_Resize(&c->c_code, c->c_nexti);
629 if (c->c_lnotab != NULL)
630 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
633 static void
634 com_addbyte(struct compiling *c, int byte)
636 int len;
637 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
638 assert(byte >= 0 && byte <= 255);
639 if (byte < 0 || byte > 255) {
640 com_error(c, PyExc_SystemError,
641 "com_addbyte: byte out of range");
643 if (c->c_code == NULL)
644 return;
645 len = PyString_Size(c->c_code);
646 if (c->c_nexti >= len) {
647 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
648 c->c_errors++;
649 return;
652 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
655 static void
656 com_addint(struct compiling *c, int x)
658 com_addbyte(c, x & 0xff);
659 com_addbyte(c, x >> 8); /* XXX x should be positive */
662 static void
663 com_add_lnotab(struct compiling *c, int addr, int line)
665 int size;
666 char *p;
667 if (c->c_lnotab == NULL)
668 return;
669 size = PyString_Size(c->c_lnotab);
670 if (c->c_lnotab_next+2 > size) {
671 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
672 c->c_errors++;
673 return;
676 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
677 *p++ = addr;
678 *p++ = line;
679 c->c_lnotab_next += 2;
682 static void
683 com_set_lineno(struct compiling *c, int lineno)
685 c->c_lineno = lineno;
686 if (c->c_firstlineno == 0) {
687 c->c_firstlineno = c->c_last_line = lineno;
689 else {
690 int incr_addr = c->c_nexti - c->c_last_addr;
691 int incr_line = lineno - c->c_last_line;
692 while (incr_addr > 0 || incr_line > 0) {
693 int trunc_addr = incr_addr;
694 int trunc_line = incr_line;
695 if (trunc_addr > 255)
696 trunc_addr = 255;
697 if (trunc_line > 255)
698 trunc_line = 255;
699 com_add_lnotab(c, trunc_addr, trunc_line);
700 incr_addr -= trunc_addr;
701 incr_line -= trunc_line;
703 c->c_last_addr = c->c_nexti;
704 c->c_last_line = lineno;
708 static void
709 com_addoparg(struct compiling *c, int op, int arg)
711 int extended_arg = arg >> 16;
712 if (op == SET_LINENO) {
713 com_set_lineno(c, arg);
714 if (Py_OptimizeFlag)
715 return;
717 if (extended_arg){
718 com_addbyte(c, EXTENDED_ARG);
719 com_addint(c, extended_arg);
720 arg &= 0xffff;
722 com_addbyte(c, op);
723 com_addint(c, arg);
726 static void
727 com_addfwref(struct compiling *c, int op, int *p_anchor)
729 /* Compile a forward reference for backpatching */
730 int here;
731 int anchor;
732 com_addbyte(c, op);
733 here = c->c_nexti;
734 anchor = *p_anchor;
735 *p_anchor = here;
736 com_addint(c, anchor == 0 ? 0 : here - anchor);
739 static void
740 com_backpatch(struct compiling *c, int anchor)
742 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
743 int target = c->c_nexti;
744 int dist;
745 int prev;
746 for (;;) {
747 /* Make the JUMP instruction at anchor point to target */
748 prev = code[anchor] + (code[anchor+1] << 8);
749 dist = target - (anchor+2);
750 code[anchor] = dist & 0xff;
751 dist >>= 8;
752 code[anchor+1] = dist;
753 dist >>= 8;
754 if (dist) {
755 com_error(c, PyExc_SystemError,
756 "com_backpatch: offset too large");
757 break;
759 if (!prev)
760 break;
761 anchor -= prev;
765 /* Handle literals and names uniformly */
767 static int
768 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
770 PyObject *w, *t, *np=NULL;
771 long n;
773 t = Py_BuildValue("(OO)", v, v->ob_type);
774 if (t == NULL)
775 goto fail;
776 w = PyDict_GetItem(dict, t);
777 if (w != NULL) {
778 n = PyInt_AsLong(w);
779 } else {
780 n = PyList_Size(list);
781 np = PyInt_FromLong(n);
782 if (np == NULL)
783 goto fail;
784 if (PyList_Append(list, v) != 0)
785 goto fail;
786 if (PyDict_SetItem(dict, t, np) != 0)
787 goto fail;
788 Py_DECREF(np);
790 Py_DECREF(t);
791 return n;
792 fail:
793 Py_XDECREF(np);
794 Py_XDECREF(t);
795 c->c_errors++;
796 return 0;
799 static int
800 com_addconst(struct compiling *c, PyObject *v)
802 return com_add(c, c->c_consts, c->c_const_dict, v);
805 static int
806 com_addname(struct compiling *c, PyObject *v)
808 return com_add(c, c->c_names, c->c_name_dict, v);
811 static int
812 mangle(char *p, char *name, char *buffer, size_t maxlen)
814 /* Name mangling: __private becomes _classname__private.
815 This is independent from how the name is used. */
816 size_t nlen, plen;
817 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
818 return 0;
819 nlen = strlen(name);
820 if (nlen+2 >= maxlen)
821 return 0; /* Don't mangle __extremely_long_names */
822 if (name[nlen-1] == '_' && name[nlen-2] == '_')
823 return 0; /* Don't mangle __whatever__ */
824 /* Strip leading underscores from class name */
825 while (*p == '_')
826 p++;
827 if (*p == '\0')
828 return 0; /* Don't mangle if class is just underscores */
829 plen = strlen(p);
830 if (plen + nlen >= maxlen)
831 plen = maxlen-nlen-2; /* Truncate class name if too long */
832 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
833 buffer[0] = '_';
834 strncpy(buffer+1, p, plen);
835 strcpy(buffer+1+plen, name);
836 return 1;
839 static void
840 com_addop_name(struct compiling *c, int op, char *name)
842 PyObject *v;
843 int i;
844 char buffer[MANGLE_LEN];
846 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
847 name = buffer;
848 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
849 c->c_errors++;
850 i = 255;
852 else {
853 i = com_addname(c, v);
854 Py_DECREF(v);
856 com_addoparg(c, op, i);
859 #define NAME_LOCAL 0
860 #define NAME_GLOBAL 1
861 #define NAME_DEFAULT 2
862 #define NAME_CLOSURE 3
864 static int
865 com_lookup_arg(PyObject *dict, PyObject *name)
867 PyObject *v = PyDict_GetItem(dict, name);
868 if (v == NULL)
869 return -1;
870 else
871 return PyInt_AS_LONG(v);
874 static void
875 com_addop_varname(struct compiling *c, int kind, char *name)
877 PyObject *v;
878 int i, reftype;
879 int scope = NAME_DEFAULT;
880 int op = STOP_CODE;
881 char buffer[MANGLE_LEN];
883 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
884 name = buffer;
885 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
886 c->c_errors++;
887 i = 255;
888 goto done;
891 reftype = get_ref_type(c, name);
892 switch (reftype) {
893 case LOCAL:
894 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
895 scope = NAME_LOCAL;
896 break;
897 case GLOBAL_EXPLICIT:
898 scope = NAME_GLOBAL;
899 break;
900 case GLOBAL_IMPLICIT:
901 if (c->c_flags & CO_OPTIMIZED)
902 scope = NAME_GLOBAL;
903 break;
904 case FREE:
905 case CELL:
906 scope = NAME_CLOSURE;
907 break;
910 i = com_addname(c, v);
911 if (scope == NAME_LOCAL)
912 i = com_lookup_arg(c->c_locals, v);
913 else if (reftype == FREE)
914 i = com_lookup_arg(c->c_freevars, v);
915 else if (reftype == CELL)
916 i = com_lookup_arg(c->c_cellvars, v);
917 if (i == -1) {
918 c->c_errors++; /* XXX no exception set */
919 i = 255;
920 goto done;
922 Py_DECREF(v);
924 switch (kind) {
925 case VAR_LOAD:
926 switch (scope) {
927 case NAME_LOCAL:
928 op = LOAD_FAST;
929 break;
930 case NAME_GLOBAL:
931 op = LOAD_GLOBAL;
932 break;
933 case NAME_DEFAULT:
934 op = LOAD_NAME;
935 break;
936 case NAME_CLOSURE:
937 op = LOAD_DEREF;
938 break;
940 break;
941 case VAR_STORE:
942 switch (scope) {
943 case NAME_LOCAL:
944 op = STORE_FAST;
945 break;
946 case NAME_GLOBAL:
947 op = STORE_GLOBAL;
948 break;
949 case NAME_DEFAULT:
950 op = STORE_NAME;
951 break;
952 case NAME_CLOSURE:
953 op = STORE_DEREF;
954 break;
956 break;
957 case VAR_DELETE:
958 switch (scope) {
959 case NAME_LOCAL:
960 op = DELETE_FAST;
961 break;
962 case NAME_GLOBAL:
963 op = DELETE_GLOBAL;
964 break;
965 case NAME_DEFAULT:
966 op = DELETE_NAME;
967 break;
968 case NAME_CLOSURE: {
969 char buf[500];
970 sprintf(buf, DEL_CLOSURE_ERROR, name);
971 com_error(c, PyExc_SyntaxError, buf);
972 i = 255;
973 break;
976 break;
978 done:
979 com_addoparg(c, op, i);
982 static void
983 com_addopname(struct compiling *c, int op, node *n)
985 char *name;
986 char buffer[1000];
987 /* XXX it is possible to write this code without the 1000
988 chars on the total length of dotted names, I just can't be
989 bothered right now */
990 if (TYPE(n) == STAR)
991 name = "*";
992 else if (TYPE(n) == dotted_name) {
993 char *p = buffer;
994 int i;
995 name = buffer;
996 for (i = 0; i < NCH(n); i += 2) {
997 char *s = STR(CHILD(n, i));
998 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
999 com_error(c, PyExc_MemoryError,
1000 "dotted_name too long");
1001 name = NULL;
1002 break;
1004 if (p != buffer)
1005 *p++ = '.';
1006 strcpy(p, s);
1007 p = strchr(p, '\0');
1010 else {
1011 REQ(n, NAME);
1012 name = STR(n);
1014 com_addop_name(c, op, name);
1017 static PyObject *
1018 parsenumber(struct compiling *co, char *s)
1020 char *end;
1021 long x;
1022 double dx;
1023 #ifndef WITHOUT_COMPLEX
1024 Py_complex c;
1025 int imflag;
1026 #endif
1028 errno = 0;
1029 end = s + strlen(s) - 1;
1030 #ifndef WITHOUT_COMPLEX
1031 imflag = *end == 'j' || *end == 'J';
1032 #endif
1033 if (*end == 'l' || *end == 'L')
1034 return PyLong_FromString(s, (char **)0, 0);
1035 if (s[0] == '0')
1036 x = (long) PyOS_strtoul(s, &end, 0);
1037 else
1038 x = PyOS_strtol(s, &end, 0);
1039 if (*end == '\0') {
1040 if (errno != 0) {
1041 com_error(co, PyExc_OverflowError,
1042 "integer literal too large");
1043 return NULL;
1045 return PyInt_FromLong(x);
1047 /* XXX Huge floats may silently fail */
1048 #ifndef WITHOUT_COMPLEX
1049 if (imflag) {
1050 c.real = 0.;
1051 PyFPE_START_PROTECT("atof", return 0)
1052 c.imag = atof(s);
1053 PyFPE_END_PROTECT(c)
1054 return PyComplex_FromCComplex(c);
1056 else
1057 #endif
1059 PyFPE_START_PROTECT("atof", return 0)
1060 dx = atof(s);
1061 PyFPE_END_PROTECT(dx)
1062 return PyFloat_FromDouble(dx);
1066 static PyObject *
1067 parsestr(char *s)
1069 PyObject *v;
1070 size_t len;
1071 char *buf;
1072 char *p;
1073 char *end;
1074 int c;
1075 int first = *s;
1076 int quote = first;
1077 int rawmode = 0;
1078 int unicode = 0;
1079 if (isalpha(quote) || quote == '_') {
1080 if (quote == 'u' || quote == 'U') {
1081 quote = *++s;
1082 unicode = 1;
1084 if (quote == 'r' || quote == 'R') {
1085 quote = *++s;
1086 rawmode = 1;
1089 if (quote != '\'' && quote != '\"') {
1090 PyErr_BadInternalCall();
1091 return NULL;
1093 s++;
1094 len = strlen(s);
1095 if (len > INT_MAX) {
1096 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1097 return NULL;
1099 if (s[--len] != quote) {
1100 PyErr_BadInternalCall();
1101 return NULL;
1103 if (len >= 4 && s[0] == quote && s[1] == quote) {
1104 s += 2;
1105 len -= 2;
1106 if (s[--len] != quote || s[--len] != quote) {
1107 PyErr_BadInternalCall();
1108 return NULL;
1111 if (unicode || Py_UnicodeFlag) {
1112 if (rawmode)
1113 return PyUnicode_DecodeRawUnicodeEscape(
1114 s, len, NULL);
1115 else
1116 return PyUnicode_DecodeUnicodeEscape(
1117 s, len, NULL);
1119 if (rawmode || strchr(s, '\\') == NULL)
1120 return PyString_FromStringAndSize(s, len);
1121 v = PyString_FromStringAndSize((char *)NULL, len);
1122 if (v == NULL)
1123 return NULL;
1124 p = buf = PyString_AsString(v);
1125 end = s + len;
1126 while (s < end) {
1127 if (*s != '\\') {
1128 *p++ = *s++;
1129 continue;
1131 s++;
1132 switch (*s++) {
1133 /* XXX This assumes ASCII! */
1134 case '\n': break;
1135 case '\\': *p++ = '\\'; break;
1136 case '\'': *p++ = '\''; break;
1137 case '\"': *p++ = '\"'; break;
1138 case 'b': *p++ = '\b'; break;
1139 case 'f': *p++ = '\014'; break; /* FF */
1140 case 't': *p++ = '\t'; break;
1141 case 'n': *p++ = '\n'; break;
1142 case 'r': *p++ = '\r'; break;
1143 case 'v': *p++ = '\013'; break; /* VT */
1144 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1145 case '0': case '1': case '2': case '3':
1146 case '4': case '5': case '6': case '7':
1147 c = s[-1] - '0';
1148 if ('0' <= *s && *s <= '7') {
1149 c = (c<<3) + *s++ - '0';
1150 if ('0' <= *s && *s <= '7')
1151 c = (c<<3) + *s++ - '0';
1153 *p++ = c;
1154 break;
1155 case 'x':
1156 if (isxdigit(Py_CHARMASK(s[0]))
1157 && isxdigit(Py_CHARMASK(s[1]))) {
1158 unsigned int x = 0;
1159 c = Py_CHARMASK(*s);
1160 s++;
1161 if (isdigit(c))
1162 x = c - '0';
1163 else if (islower(c))
1164 x = 10 + c - 'a';
1165 else
1166 x = 10 + c - 'A';
1167 x = x << 4;
1168 c = Py_CHARMASK(*s);
1169 s++;
1170 if (isdigit(c))
1171 x += c - '0';
1172 else if (islower(c))
1173 x += 10 + c - 'a';
1174 else
1175 x += 10 + c - 'A';
1176 *p++ = x;
1177 break;
1179 PyErr_SetString(PyExc_ValueError,
1180 "invalid \\x escape");
1181 Py_DECREF(v);
1182 return NULL;
1183 default:
1184 *p++ = '\\';
1185 *p++ = s[-1];
1186 break;
1189 _PyString_Resize(&v, (int)(p - buf));
1190 return v;
1193 static PyObject *
1194 parsestrplus(node *n)
1196 PyObject *v;
1197 int i;
1198 REQ(CHILD(n, 0), STRING);
1199 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
1200 /* String literal concatenation */
1201 for (i = 1; i < NCH(n); i++) {
1202 PyObject *s;
1203 s = parsestr(STR(CHILD(n, i)));
1204 if (s == NULL)
1205 goto onError;
1206 if (PyString_Check(v) && PyString_Check(s)) {
1207 PyString_ConcatAndDel(&v, s);
1208 if (v == NULL)
1209 goto onError;
1211 else {
1212 PyObject *temp;
1213 temp = PyUnicode_Concat(v, s);
1214 Py_DECREF(s);
1215 if (temp == NULL)
1216 goto onError;
1217 Py_DECREF(v);
1218 v = temp;
1222 return v;
1224 onError:
1225 Py_XDECREF(v);
1226 return NULL;
1229 static void
1230 com_list_for(struct compiling *c, node *n, node *e, char *t)
1232 PyObject *v;
1233 int anchor = 0;
1234 int save_begin = c->c_begin;
1236 /* list_iter: for v in expr [list_iter] */
1237 com_node(c, CHILD(n, 3)); /* expr */
1238 v = PyInt_FromLong(0L);
1239 if (v == NULL)
1240 c->c_errors++;
1241 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1242 com_push(c, 1);
1243 Py_XDECREF(v);
1244 c->c_begin = c->c_nexti;
1245 com_addoparg(c, SET_LINENO, n->n_lineno);
1246 com_addfwref(c, FOR_LOOP, &anchor);
1247 com_push(c, 1);
1248 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
1249 c->c_loops++;
1250 com_list_iter(c, n, e, t);
1251 c->c_loops--;
1252 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1253 c->c_begin = save_begin;
1254 com_backpatch(c, anchor);
1255 com_pop(c, 2); /* FOR_LOOP has popped these */
1258 static void
1259 com_list_if(struct compiling *c, node *n, node *e, char *t)
1261 int anchor = 0;
1262 int a = 0;
1263 /* list_iter: 'if' test [list_iter] */
1264 com_addoparg(c, SET_LINENO, n->n_lineno);
1265 com_node(c, CHILD(n, 1));
1266 com_addfwref(c, JUMP_IF_FALSE, &a);
1267 com_addbyte(c, POP_TOP);
1268 com_pop(c, 1);
1269 com_list_iter(c, n, e, t);
1270 com_addfwref(c, JUMP_FORWARD, &anchor);
1271 com_backpatch(c, a);
1272 /* We jump here with an extra entry which we now pop */
1273 com_addbyte(c, POP_TOP);
1274 com_backpatch(c, anchor);
1277 static void
1278 com_list_iter(struct compiling *c,
1279 node *p, /* parent of list_iter node */
1280 node *e, /* element expression node */
1281 char *t /* name of result list temp local */)
1283 /* list_iter is the last child in a listmaker, list_for, or list_if */
1284 node *n = CHILD(p, NCH(p)-1);
1285 if (TYPE(n) == list_iter) {
1286 n = CHILD(n, 0);
1287 switch (TYPE(n)) {
1288 case list_for:
1289 com_list_for(c, n, e, t);
1290 break;
1291 case list_if:
1292 com_list_if(c, n, e, t);
1293 break;
1294 default:
1295 com_error(c, PyExc_SystemError,
1296 "invalid list_iter node type");
1299 else {
1300 com_addop_varname(c, VAR_LOAD, t);
1301 com_push(c, 1);
1302 com_node(c, e);
1303 com_addoparg(c, CALL_FUNCTION, 1);
1304 com_addbyte(c, POP_TOP);
1305 com_pop(c, 2);
1309 static void
1310 com_list_comprehension(struct compiling *c, node *n)
1312 /* listmaker: test list_for */
1313 char tmpname[12];
1314 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
1315 com_addoparg(c, BUILD_LIST, 0);
1316 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1317 com_push(c, 2);
1318 com_addop_name(c, LOAD_ATTR, "append");
1319 com_addop_varname(c, VAR_STORE, tmpname);
1320 com_pop(c, 1);
1321 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
1322 com_addop_varname(c, VAR_DELETE, tmpname);
1323 --c->c_tmpname;
1326 static void
1327 com_listmaker(struct compiling *c, node *n)
1329 /* listmaker: test ( list_for | (',' test)* [','] ) */
1330 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
1331 com_list_comprehension(c, n);
1332 else {
1333 int len = 0;
1334 int i;
1335 for (i = 0; i < NCH(n); i += 2, len++)
1336 com_node(c, CHILD(n, i));
1337 com_addoparg(c, BUILD_LIST, len);
1338 com_pop(c, len-1);
1342 static void
1343 com_dictmaker(struct compiling *c, node *n)
1345 int i;
1346 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1347 for (i = 0; i+2 < NCH(n); i += 4) {
1348 /* We must arrange things just right for STORE_SUBSCR.
1349 It wants the stack to look like (value) (dict) (key) */
1350 com_addbyte(c, DUP_TOP);
1351 com_push(c, 1);
1352 com_node(c, CHILD(n, i+2)); /* value */
1353 com_addbyte(c, ROT_TWO);
1354 com_node(c, CHILD(n, i)); /* key */
1355 com_addbyte(c, STORE_SUBSCR);
1356 com_pop(c, 3);
1360 static void
1361 com_atom(struct compiling *c, node *n)
1363 node *ch;
1364 PyObject *v;
1365 int i;
1366 REQ(n, atom);
1367 ch = CHILD(n, 0);
1368 switch (TYPE(ch)) {
1369 case LPAR:
1370 if (TYPE(CHILD(n, 1)) == RPAR) {
1371 com_addoparg(c, BUILD_TUPLE, 0);
1372 com_push(c, 1);
1374 else
1375 com_node(c, CHILD(n, 1));
1376 break;
1377 case LSQB: /* '[' [listmaker] ']' */
1378 if (TYPE(CHILD(n, 1)) == RSQB) {
1379 com_addoparg(c, BUILD_LIST, 0);
1380 com_push(c, 1);
1382 else
1383 com_listmaker(c, CHILD(n, 1));
1384 break;
1385 case LBRACE: /* '{' [dictmaker] '}' */
1386 com_addoparg(c, BUILD_MAP, 0);
1387 com_push(c, 1);
1388 if (TYPE(CHILD(n, 1)) == dictmaker)
1389 com_dictmaker(c, CHILD(n, 1));
1390 break;
1391 case BACKQUOTE:
1392 com_node(c, CHILD(n, 1));
1393 com_addbyte(c, UNARY_CONVERT);
1394 break;
1395 case NUMBER:
1396 if ((v = parsenumber(c, STR(ch))) == NULL) {
1397 i = 255;
1399 else {
1400 i = com_addconst(c, v);
1401 Py_DECREF(v);
1403 com_addoparg(c, LOAD_CONST, i);
1404 com_push(c, 1);
1405 break;
1406 case STRING:
1407 v = parsestrplus(n);
1408 if (v == NULL) {
1409 c->c_errors++;
1410 i = 255;
1412 else {
1413 i = com_addconst(c, v);
1414 Py_DECREF(v);
1416 com_addoparg(c, LOAD_CONST, i);
1417 com_push(c, 1);
1418 break;
1419 case NAME:
1420 com_addop_varname(c, VAR_LOAD, STR(ch));
1421 com_push(c, 1);
1422 break;
1423 default:
1424 com_error(c, PyExc_SystemError,
1425 "com_atom: unexpected node type");
1429 static void
1430 com_slice(struct compiling *c, node *n, int op)
1432 if (NCH(n) == 1) {
1433 com_addbyte(c, op);
1435 else if (NCH(n) == 2) {
1436 if (TYPE(CHILD(n, 0)) != COLON) {
1437 com_node(c, CHILD(n, 0));
1438 com_addbyte(c, op+1);
1440 else {
1441 com_node(c, CHILD(n, 1));
1442 com_addbyte(c, op+2);
1444 com_pop(c, 1);
1446 else {
1447 com_node(c, CHILD(n, 0));
1448 com_node(c, CHILD(n, 2));
1449 com_addbyte(c, op+3);
1450 com_pop(c, 2);
1454 static void
1455 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1457 if (NCH(n) == 1) {
1458 com_addbyte(c, DUP_TOP);
1459 com_push(c, 1);
1460 com_addbyte(c, SLICE);
1461 com_node(c, augn);
1462 com_addbyte(c, opcode);
1463 com_pop(c, 1);
1464 com_addbyte(c, ROT_TWO);
1465 com_addbyte(c, STORE_SLICE);
1466 com_pop(c, 2);
1467 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1468 com_node(c, CHILD(n, 0));
1469 com_addoparg(c, DUP_TOPX, 2);
1470 com_push(c, 2);
1471 com_addbyte(c, SLICE+1);
1472 com_pop(c, 1);
1473 com_node(c, augn);
1474 com_addbyte(c, opcode);
1475 com_pop(c, 1);
1476 com_addbyte(c, ROT_THREE);
1477 com_addbyte(c, STORE_SLICE+1);
1478 com_pop(c, 3);
1479 } else if (NCH(n) == 2) {
1480 com_node(c, CHILD(n, 1));
1481 com_addoparg(c, DUP_TOPX, 2);
1482 com_push(c, 2);
1483 com_addbyte(c, SLICE+2);
1484 com_pop(c, 1);
1485 com_node(c, augn);
1486 com_addbyte(c, opcode);
1487 com_pop(c, 1);
1488 com_addbyte(c, ROT_THREE);
1489 com_addbyte(c, STORE_SLICE+2);
1490 com_pop(c, 3);
1491 } else {
1492 com_node(c, CHILD(n, 0));
1493 com_node(c, CHILD(n, 2));
1494 com_addoparg(c, DUP_TOPX, 3);
1495 com_push(c, 3);
1496 com_addbyte(c, SLICE+3);
1497 com_pop(c, 2);
1498 com_node(c, augn);
1499 com_addbyte(c, opcode);
1500 com_pop(c, 1);
1501 com_addbyte(c, ROT_FOUR);
1502 com_addbyte(c, STORE_SLICE+3);
1503 com_pop(c, 4);
1507 static void
1508 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
1510 node *m;
1511 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
1512 if (NCH(n) == 1) {
1513 if (*pkeywords != NULL) {
1514 com_error(c, PyExc_SyntaxError,
1515 "non-keyword arg after keyword arg");
1517 else {
1518 com_node(c, CHILD(n, 0));
1520 return;
1522 m = n;
1523 do {
1524 m = CHILD(m, 0);
1525 } while (NCH(m) == 1);
1526 if (TYPE(m) != NAME) {
1527 /* f(lambda x: x[0] = 3) ends up getting parsed with
1528 * LHS test = lambda x: x[0], and RHS test = 3.
1529 * SF bug 132313 points out that complaining about a keyword
1530 * then is very confusing.
1532 com_error(c, PyExc_SyntaxError,
1533 TYPE(m) == lambdef ?
1534 "lambda cannot contain assignment" :
1535 "keyword can't be an expression");
1537 else {
1538 PyObject *v = PyString_InternFromString(STR(m));
1539 if (v != NULL && *pkeywords == NULL)
1540 *pkeywords = PyDict_New();
1541 if (v == NULL)
1542 c->c_errors++;
1543 else if (*pkeywords == NULL) {
1544 c->c_errors++;
1545 Py_DECREF(v);
1546 } else {
1547 if (PyDict_GetItem(*pkeywords, v) != NULL)
1548 com_error(c, PyExc_SyntaxError,
1549 "duplicate keyword argument");
1550 else
1551 if (PyDict_SetItem(*pkeywords, v, v) != 0)
1552 c->c_errors++;
1553 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
1554 com_push(c, 1);
1555 Py_DECREF(v);
1558 com_node(c, CHILD(n, 2));
1561 static void
1562 com_call_function(struct compiling *c, node *n)
1564 if (TYPE(n) == RPAR) {
1565 com_addoparg(c, CALL_FUNCTION, 0);
1567 else {
1568 PyObject *keywords = NULL;
1569 int i, na, nk;
1570 int lineno = n->n_lineno;
1571 int star_flag = 0;
1572 int starstar_flag = 0;
1573 int opcode;
1574 REQ(n, arglist);
1575 na = 0;
1576 nk = 0;
1577 for (i = 0; i < NCH(n); i += 2) {
1578 node *ch = CHILD(n, i);
1579 if (TYPE(ch) == STAR ||
1580 TYPE(ch) == DOUBLESTAR)
1581 break;
1582 if (ch->n_lineno != lineno) {
1583 lineno = ch->n_lineno;
1584 com_addoparg(c, SET_LINENO, lineno);
1586 com_argument(c, ch, &keywords);
1587 if (keywords == NULL)
1588 na++;
1589 else
1590 nk++;
1592 Py_XDECREF(keywords);
1593 while (i < NCH(n)) {
1594 node *tok = CHILD(n, i);
1595 node *ch = CHILD(n, i+1);
1596 i += 3;
1597 switch (TYPE(tok)) {
1598 case STAR: star_flag = 1; break;
1599 case DOUBLESTAR: starstar_flag = 1; break;
1601 com_node(c, ch);
1603 if (na > 255 || nk > 255) {
1604 com_error(c, PyExc_SyntaxError,
1605 "more than 255 arguments");
1607 if (star_flag || starstar_flag)
1608 opcode = CALL_FUNCTION_VAR - 1 +
1609 star_flag + (starstar_flag << 1);
1610 else
1611 opcode = CALL_FUNCTION;
1612 com_addoparg(c, opcode, na | (nk << 8));
1613 com_pop(c, na + 2*nk + star_flag + starstar_flag);
1617 static void
1618 com_select_member(struct compiling *c, node *n)
1620 com_addopname(c, LOAD_ATTR, n);
1623 static void
1624 com_sliceobj(struct compiling *c, node *n)
1626 int i=0;
1627 int ns=2; /* number of slice arguments */
1628 node *ch;
1630 /* first argument */
1631 if (TYPE(CHILD(n,i)) == COLON) {
1632 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1633 com_push(c, 1);
1634 i++;
1636 else {
1637 com_node(c, CHILD(n,i));
1638 i++;
1639 REQ(CHILD(n,i),COLON);
1640 i++;
1642 /* second argument */
1643 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1644 com_node(c, CHILD(n,i));
1645 i++;
1647 else {
1648 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1649 com_push(c, 1);
1651 /* remaining arguments */
1652 for (; i < NCH(n); i++) {
1653 ns++;
1654 ch=CHILD(n,i);
1655 REQ(ch, sliceop);
1656 if (NCH(ch) == 1) {
1657 /* right argument of ':' missing */
1658 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1659 com_push(c, 1);
1661 else
1662 com_node(c, CHILD(ch,1));
1664 com_addoparg(c, BUILD_SLICE, ns);
1665 com_pop(c, 1 + (ns == 3));
1668 static void
1669 com_subscript(struct compiling *c, node *n)
1671 node *ch;
1672 REQ(n, subscript);
1673 ch = CHILD(n,0);
1674 /* check for rubber index */
1675 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
1676 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
1677 com_push(c, 1);
1679 else {
1680 /* check for slice */
1681 if ((TYPE(ch) == COLON || NCH(n) > 1))
1682 com_sliceobj(c, n);
1683 else {
1684 REQ(ch, test);
1685 com_node(c, ch);
1690 static void
1691 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
1693 int i, op;
1694 REQ(n, subscriptlist);
1695 /* Check to make backward compatible slice behavior for '[i:j]' */
1696 if (NCH(n) == 1) {
1697 node *sub = CHILD(n, 0); /* subscript */
1698 /* 'Basic' slice, should have exactly one colon. */
1699 if ((TYPE(CHILD(sub, 0)) == COLON
1700 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1701 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1703 switch (assigning) {
1704 case OP_DELETE:
1705 op = DELETE_SLICE;
1706 break;
1707 case OP_ASSIGN:
1708 op = STORE_SLICE;
1709 break;
1710 case OP_APPLY:
1711 op = SLICE;
1712 break;
1713 default:
1714 com_augassign_slice(c, sub, assigning, augn);
1715 return;
1717 com_slice(c, sub, op);
1718 if (op == STORE_SLICE)
1719 com_pop(c, 2);
1720 else if (op == DELETE_SLICE)
1721 com_pop(c, 1);
1722 return;
1725 /* Else normal subscriptlist. Compile each subscript. */
1726 for (i = 0; i < NCH(n); i += 2)
1727 com_subscript(c, CHILD(n, i));
1728 /* Put multiple subscripts into a tuple */
1729 if (NCH(n) > 1) {
1730 i = (NCH(n)+1) / 2;
1731 com_addoparg(c, BUILD_TUPLE, i);
1732 com_pop(c, i-1);
1734 switch (assigning) {
1735 case OP_DELETE:
1736 op = DELETE_SUBSCR;
1737 i = 2;
1738 break;
1739 default:
1740 case OP_ASSIGN:
1741 op = STORE_SUBSCR;
1742 i = 3;
1743 break;
1744 case OP_APPLY:
1745 op = BINARY_SUBSCR;
1746 i = 1;
1747 break;
1749 if (assigning > OP_APPLY) {
1750 com_addoparg(c, DUP_TOPX, 2);
1751 com_push(c, 2);
1752 com_addbyte(c, BINARY_SUBSCR);
1753 com_pop(c, 1);
1754 com_node(c, augn);
1755 com_addbyte(c, assigning);
1756 com_pop(c, 1);
1757 com_addbyte(c, ROT_THREE);
1759 com_addbyte(c, op);
1760 com_pop(c, i);
1763 static void
1764 com_apply_trailer(struct compiling *c, node *n)
1766 REQ(n, trailer);
1767 switch (TYPE(CHILD(n, 0))) {
1768 case LPAR:
1769 com_call_function(c, CHILD(n, 1));
1770 break;
1771 case DOT:
1772 com_select_member(c, CHILD(n, 1));
1773 break;
1774 case LSQB:
1775 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
1776 break;
1777 default:
1778 com_error(c, PyExc_SystemError,
1779 "com_apply_trailer: unknown trailer type");
1783 static void
1784 com_power(struct compiling *c, node *n)
1786 int i;
1787 REQ(n, power);
1788 com_atom(c, CHILD(n, 0));
1789 for (i = 1; i < NCH(n); i++) {
1790 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1791 com_factor(c, CHILD(n, i+1));
1792 com_addbyte(c, BINARY_POWER);
1793 com_pop(c, 1);
1794 break;
1796 else
1797 com_apply_trailer(c, CHILD(n, i));
1801 static void
1802 com_factor(struct compiling *c, node *n)
1804 REQ(n, factor);
1805 if (TYPE(CHILD(n, 0)) == PLUS) {
1806 com_factor(c, CHILD(n, 1));
1807 com_addbyte(c, UNARY_POSITIVE);
1809 else if (TYPE(CHILD(n, 0)) == MINUS) {
1810 com_factor(c, CHILD(n, 1));
1811 com_addbyte(c, UNARY_NEGATIVE);
1813 else if (TYPE(CHILD(n, 0)) == TILDE) {
1814 com_factor(c, CHILD(n, 1));
1815 com_addbyte(c, UNARY_INVERT);
1817 else {
1818 com_power(c, CHILD(n, 0));
1822 static void
1823 com_term(struct compiling *c, node *n)
1825 int i;
1826 int op;
1827 REQ(n, term);
1828 com_factor(c, CHILD(n, 0));
1829 for (i = 2; i < NCH(n); i += 2) {
1830 com_factor(c, CHILD(n, i));
1831 switch (TYPE(CHILD(n, i-1))) {
1832 case STAR:
1833 op = BINARY_MULTIPLY;
1834 break;
1835 case SLASH:
1836 op = BINARY_DIVIDE;
1837 break;
1838 case PERCENT:
1839 op = BINARY_MODULO;
1840 break;
1841 default:
1842 com_error(c, PyExc_SystemError,
1843 "com_term: operator not *, / or %");
1844 op = 255;
1846 com_addbyte(c, op);
1847 com_pop(c, 1);
1851 static void
1852 com_arith_expr(struct compiling *c, node *n)
1854 int i;
1855 int op;
1856 REQ(n, arith_expr);
1857 com_term(c, CHILD(n, 0));
1858 for (i = 2; i < NCH(n); i += 2) {
1859 com_term(c, CHILD(n, i));
1860 switch (TYPE(CHILD(n, i-1))) {
1861 case PLUS:
1862 op = BINARY_ADD;
1863 break;
1864 case MINUS:
1865 op = BINARY_SUBTRACT;
1866 break;
1867 default:
1868 com_error(c, PyExc_SystemError,
1869 "com_arith_expr: operator not + or -");
1870 op = 255;
1872 com_addbyte(c, op);
1873 com_pop(c, 1);
1877 static void
1878 com_shift_expr(struct compiling *c, node *n)
1880 int i;
1881 int op;
1882 REQ(n, shift_expr);
1883 com_arith_expr(c, CHILD(n, 0));
1884 for (i = 2; i < NCH(n); i += 2) {
1885 com_arith_expr(c, CHILD(n, i));
1886 switch (TYPE(CHILD(n, i-1))) {
1887 case LEFTSHIFT:
1888 op = BINARY_LSHIFT;
1889 break;
1890 case RIGHTSHIFT:
1891 op = BINARY_RSHIFT;
1892 break;
1893 default:
1894 com_error(c, PyExc_SystemError,
1895 "com_shift_expr: operator not << or >>");
1896 op = 255;
1898 com_addbyte(c, op);
1899 com_pop(c, 1);
1903 static void
1904 com_and_expr(struct compiling *c, node *n)
1906 int i;
1907 int op;
1908 REQ(n, and_expr);
1909 com_shift_expr(c, CHILD(n, 0));
1910 for (i = 2; i < NCH(n); i += 2) {
1911 com_shift_expr(c, CHILD(n, i));
1912 if (TYPE(CHILD(n, i-1)) == AMPER) {
1913 op = BINARY_AND;
1915 else {
1916 com_error(c, PyExc_SystemError,
1917 "com_and_expr: operator not &");
1918 op = 255;
1920 com_addbyte(c, op);
1921 com_pop(c, 1);
1925 static void
1926 com_xor_expr(struct compiling *c, node *n)
1928 int i;
1929 int op;
1930 REQ(n, xor_expr);
1931 com_and_expr(c, CHILD(n, 0));
1932 for (i = 2; i < NCH(n); i += 2) {
1933 com_and_expr(c, CHILD(n, i));
1934 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1935 op = BINARY_XOR;
1937 else {
1938 com_error(c, PyExc_SystemError,
1939 "com_xor_expr: operator not ^");
1940 op = 255;
1942 com_addbyte(c, op);
1943 com_pop(c, 1);
1947 static void
1948 com_expr(struct compiling *c, node *n)
1950 int i;
1951 int op;
1952 REQ(n, expr);
1953 com_xor_expr(c, CHILD(n, 0));
1954 for (i = 2; i < NCH(n); i += 2) {
1955 com_xor_expr(c, CHILD(n, i));
1956 if (TYPE(CHILD(n, i-1)) == VBAR) {
1957 op = BINARY_OR;
1959 else {
1960 com_error(c, PyExc_SystemError,
1961 "com_expr: expr operator not |");
1962 op = 255;
1964 com_addbyte(c, op);
1965 com_pop(c, 1);
1969 static enum cmp_op
1970 cmp_type(node *n)
1972 REQ(n, comp_op);
1973 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1974 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1975 if (NCH(n) == 1) {
1976 n = CHILD(n, 0);
1977 switch (TYPE(n)) {
1978 case LESS: return LT;
1979 case GREATER: return GT;
1980 case EQEQUAL: /* == */
1981 case EQUAL: return EQ;
1982 case LESSEQUAL: return LE;
1983 case GREATEREQUAL: return GE;
1984 case NOTEQUAL: return NE; /* <> or != */
1985 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
1986 if (strcmp(STR(n), "is") == 0) return IS;
1989 else if (NCH(n) == 2) {
1990 switch (TYPE(CHILD(n, 0))) {
1991 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1992 return NOT_IN;
1993 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1994 return IS_NOT;
1997 return BAD;
2000 static void
2001 com_comparison(struct compiling *c, node *n)
2003 int i;
2004 enum cmp_op op;
2005 int anchor;
2006 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2007 com_expr(c, CHILD(n, 0));
2008 if (NCH(n) == 1)
2009 return;
2011 /****************************************************************
2012 The following code is generated for all but the last
2013 comparison in a chain:
2015 label: on stack: opcode: jump to:
2017 a <code to load b>
2018 a, b DUP_TOP
2019 a, b, b ROT_THREE
2020 b, a, b COMPARE_OP
2021 b, 0-or-1 JUMP_IF_FALSE L1
2022 b, 1 POP_TOP
2025 We are now ready to repeat this sequence for the next
2026 comparison in the chain.
2028 For the last we generate:
2030 b <code to load c>
2031 b, c COMPARE_OP
2032 0-or-1
2034 If there were any jumps to L1 (i.e., there was more than one
2035 comparison), we generate:
2037 0-or-1 JUMP_FORWARD L2
2038 L1: b, 0 ROT_TWO
2039 0, b POP_TOP
2041 L2: 0-or-1
2042 ****************************************************************/
2044 anchor = 0;
2046 for (i = 2; i < NCH(n); i += 2) {
2047 com_expr(c, CHILD(n, i));
2048 if (i+2 < NCH(n)) {
2049 com_addbyte(c, DUP_TOP);
2050 com_push(c, 1);
2051 com_addbyte(c, ROT_THREE);
2053 op = cmp_type(CHILD(n, i-1));
2054 if (op == BAD) {
2055 com_error(c, PyExc_SystemError,
2056 "com_comparison: unknown comparison op");
2058 com_addoparg(c, COMPARE_OP, op);
2059 com_pop(c, 1);
2060 if (i+2 < NCH(n)) {
2061 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2062 com_addbyte(c, POP_TOP);
2063 com_pop(c, 1);
2067 if (anchor) {
2068 int anchor2 = 0;
2069 com_addfwref(c, JUMP_FORWARD, &anchor2);
2070 com_backpatch(c, anchor);
2071 com_addbyte(c, ROT_TWO);
2072 com_addbyte(c, POP_TOP);
2073 com_backpatch(c, anchor2);
2077 static void
2078 com_not_test(struct compiling *c, node *n)
2080 REQ(n, not_test); /* 'not' not_test | comparison */
2081 if (NCH(n) == 1) {
2082 com_comparison(c, CHILD(n, 0));
2084 else {
2085 com_not_test(c, CHILD(n, 1));
2086 com_addbyte(c, UNARY_NOT);
2090 static void
2091 com_and_test(struct compiling *c, node *n)
2093 int i;
2094 int anchor;
2095 REQ(n, and_test); /* not_test ('and' not_test)* */
2096 anchor = 0;
2097 i = 0;
2098 for (;;) {
2099 com_not_test(c, CHILD(n, i));
2100 if ((i += 2) >= NCH(n))
2101 break;
2102 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2103 com_addbyte(c, POP_TOP);
2104 com_pop(c, 1);
2106 if (anchor)
2107 com_backpatch(c, anchor);
2110 static int
2111 com_make_closure(struct compiling *c, PyCodeObject *co)
2113 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2114 /* If the code is compiled with st->st_nested_scopes == 0,
2115 then no variable will ever be added to co_freevars.
2117 if (free == 0)
2118 return 0;
2119 for (i = 0; i < free; ++i) {
2120 /* Bypass com_addop_varname because it will generate
2121 LOAD_DEREF but LOAD_CLOSURE is needed.
2123 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2124 int arg, reftype;
2126 /* Special case: If a class contains a method with a
2127 free variable that has the same name as a method,
2128 the name will be considered free *and* local in the
2129 class. It should be handled by the closure, as
2130 well as by the normal name loookup logic.
2132 reftype = get_ref_type(c, PyString_AS_STRING(name));
2133 if (reftype == CELL)
2134 arg = com_lookup_arg(c->c_cellvars, name);
2135 else /* (reftype == FREE) */
2136 arg = com_lookup_arg(c->c_freevars, name);
2137 if (arg == -1) {
2138 fprintf(stderr, "lookup %s in %s %d %d\n"
2139 "freevars of %s: %s\n",
2140 PyObject_REPR(name),
2141 c->c_name,
2142 reftype, arg,
2143 PyString_AS_STRING(co->co_name),
2144 PyObject_REPR(co->co_freevars));
2145 Py_FatalError("com_make_closure()");
2147 com_addoparg(c, LOAD_CLOSURE, arg);
2150 com_push(c, free);
2151 return 1;
2154 static void
2155 com_test(struct compiling *c, node *n)
2157 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
2158 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
2159 PyObject *co;
2160 int i, closure;
2161 int ndefs = com_argdefs(c, CHILD(n, 0));
2162 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2163 n->n_lineno);
2164 co = (PyObject *) icompile(CHILD(n, 0), c);
2165 if (co == NULL) {
2166 c->c_errors++;
2167 return;
2169 symtable_exit_scope(c->c_symtable);
2170 i = com_addconst(c, co);
2171 closure = com_make_closure(c, (PyCodeObject *)co);
2172 Py_DECREF(co);
2173 com_addoparg(c, LOAD_CONST, i);
2174 com_push(c, 1);
2175 if (closure)
2176 com_addoparg(c, MAKE_CLOSURE, ndefs);
2177 else
2178 com_addoparg(c, MAKE_FUNCTION, ndefs);
2179 com_pop(c, ndefs);
2181 else {
2182 int anchor = 0;
2183 int i = 0;
2184 for (;;) {
2185 com_and_test(c, CHILD(n, i));
2186 if ((i += 2) >= NCH(n))
2187 break;
2188 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2189 com_addbyte(c, POP_TOP);
2190 com_pop(c, 1);
2192 if (anchor)
2193 com_backpatch(c, anchor);
2197 static void
2198 com_list(struct compiling *c, node *n, int toplevel)
2200 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2201 if (NCH(n) == 1 && !toplevel) {
2202 com_node(c, CHILD(n, 0));
2204 else {
2205 int i;
2206 int len;
2207 len = (NCH(n) + 1) / 2;
2208 for (i = 0; i < NCH(n); i += 2)
2209 com_node(c, CHILD(n, i));
2210 com_addoparg(c, BUILD_TUPLE, len);
2211 com_pop(c, len-1);
2216 /* Begin of assignment compilation */
2219 static void
2220 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2222 com_addbyte(c, DUP_TOP);
2223 com_push(c, 1);
2224 com_addopname(c, LOAD_ATTR, n);
2225 com_node(c, augn);
2226 com_addbyte(c, opcode);
2227 com_pop(c, 1);
2228 com_addbyte(c, ROT_TWO);
2229 com_addopname(c, STORE_ATTR, n);
2230 com_pop(c, 2);
2233 static void
2234 com_assign_attr(struct compiling *c, node *n, int assigning)
2236 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
2237 com_pop(c, assigning ? 2 : 1);
2240 static void
2241 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
2243 REQ(n, trailer);
2244 switch (TYPE(CHILD(n, 0))) {
2245 case LPAR: /* '(' [exprlist] ')' */
2246 com_error(c, PyExc_SyntaxError,
2247 "can't assign to function call");
2248 break;
2249 case DOT: /* '.' NAME */
2250 if (assigning > OP_APPLY)
2251 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2252 else
2253 com_assign_attr(c, CHILD(n, 1), assigning);
2254 break;
2255 case LSQB: /* '[' subscriptlist ']' */
2256 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
2257 break;
2258 default:
2259 com_error(c, PyExc_SystemError, "unknown trailer type");
2263 static void
2264 com_assign_sequence(struct compiling *c, node *n, int assigning)
2266 int i;
2267 if (TYPE(n) != testlist && TYPE(n) != listmaker)
2268 REQ(n, exprlist);
2269 if (assigning) {
2270 i = (NCH(n)+1)/2;
2271 com_addoparg(c, UNPACK_SEQUENCE, i);
2272 com_push(c, i-1);
2274 for (i = 0; i < NCH(n); i += 2)
2275 com_assign(c, CHILD(n, i), assigning, NULL);
2278 static void
2279 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2281 REQ(n, NAME);
2282 com_addop_varname(c, VAR_LOAD, STR(n));
2283 com_push(c, 1);
2284 com_node(c, augn);
2285 com_addbyte(c, opcode);
2286 com_pop(c, 1);
2287 com_assign_name(c, n, OP_ASSIGN);
2290 static void
2291 com_assign_name(struct compiling *c, node *n, int assigning)
2293 REQ(n, NAME);
2294 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
2295 if (assigning)
2296 com_pop(c, 1);
2299 static void
2300 com_assign(struct compiling *c, node *n, int assigning, node *augn)
2302 /* Loop to avoid trivial recursion */
2303 for (;;) {
2304 switch (TYPE(n)) {
2306 case exprlist:
2307 case testlist:
2308 if (NCH(n) > 1) {
2309 if (assigning > OP_APPLY) {
2310 com_error(c, PyExc_SyntaxError,
2311 "augmented assign to tuple not possible");
2312 return;
2314 com_assign_sequence(c, n, assigning);
2315 return;
2317 n = CHILD(n, 0);
2318 break;
2320 case test:
2321 case and_test:
2322 case not_test:
2323 case comparison:
2324 case expr:
2325 case xor_expr:
2326 case and_expr:
2327 case shift_expr:
2328 case arith_expr:
2329 case term:
2330 case factor:
2331 if (NCH(n) > 1) {
2332 com_error(c, PyExc_SyntaxError,
2333 "can't assign to operator");
2334 return;
2336 n = CHILD(n, 0);
2337 break;
2339 case power: /* atom trailer* ('**' power)*
2340 ('+'|'-'|'~') factor | atom trailer* */
2341 if (TYPE(CHILD(n, 0)) != atom) {
2342 com_error(c, PyExc_SyntaxError,
2343 "can't assign to operator");
2344 return;
2346 if (NCH(n) > 1) { /* trailer or exponent present */
2347 int i;
2348 com_node(c, CHILD(n, 0));
2349 for (i = 1; i+1 < NCH(n); i++) {
2350 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2351 com_error(c, PyExc_SyntaxError,
2352 "can't assign to operator");
2353 return;
2355 com_apply_trailer(c, CHILD(n, i));
2356 } /* NB i is still alive */
2357 com_assign_trailer(c,
2358 CHILD(n, i), assigning, augn);
2359 return;
2361 n = CHILD(n, 0);
2362 break;
2364 case atom:
2365 switch (TYPE(CHILD(n, 0))) {
2366 case LPAR:
2367 n = CHILD(n, 1);
2368 if (TYPE(n) == RPAR) {
2369 /* XXX Should allow () = () ??? */
2370 com_error(c, PyExc_SyntaxError,
2371 "can't assign to ()");
2372 return;
2374 if (assigning > OP_APPLY) {
2375 com_error(c, PyExc_SyntaxError,
2376 "augmented assign to tuple not possible");
2377 return;
2379 break;
2380 case LSQB:
2381 n = CHILD(n, 1);
2382 if (TYPE(n) == RSQB) {
2383 com_error(c, PyExc_SyntaxError,
2384 "can't assign to []");
2385 return;
2387 if (assigning > OP_APPLY) {
2388 com_error(c, PyExc_SyntaxError,
2389 "augmented assign to list not possible");
2390 return;
2392 if (NCH(n) > 1
2393 && TYPE(CHILD(n, 1)) == list_for) {
2394 com_error(c, PyExc_SyntaxError,
2395 "can't assign to list comprehension");
2396 return;
2398 com_assign_sequence(c, n, assigning);
2399 return;
2400 case NAME:
2401 if (assigning > OP_APPLY)
2402 com_augassign_name(c, CHILD(n, 0),
2403 assigning, augn);
2404 else
2405 com_assign_name(c, CHILD(n, 0),
2406 assigning);
2407 return;
2408 default:
2409 com_error(c, PyExc_SyntaxError,
2410 "can't assign to literal");
2411 return;
2413 break;
2415 case lambdef:
2416 com_error(c, PyExc_SyntaxError,
2417 "can't assign to lambda");
2418 return;
2420 default:
2421 com_error(c, PyExc_SystemError,
2422 "com_assign: bad node");
2423 return;
2429 static void
2430 com_augassign(struct compiling *c, node *n)
2432 int opcode;
2434 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2435 case '+': opcode = INPLACE_ADD; break;
2436 case '-': opcode = INPLACE_SUBTRACT; break;
2437 case '/': opcode = INPLACE_DIVIDE; break;
2438 case '%': opcode = INPLACE_MODULO; break;
2439 case '<': opcode = INPLACE_LSHIFT; break;
2440 case '>': opcode = INPLACE_RSHIFT; break;
2441 case '&': opcode = INPLACE_AND; break;
2442 case '^': opcode = INPLACE_XOR; break;
2443 case '|': opcode = INPLACE_OR; break;
2444 case '*':
2445 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2446 opcode = INPLACE_POWER;
2447 else
2448 opcode = INPLACE_MULTIPLY;
2449 break;
2450 default:
2451 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2452 return;
2454 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2457 static void
2458 com_expr_stmt(struct compiling *c, node *n)
2460 REQ(n, expr_stmt);
2461 /* testlist (('=' testlist)* | augassign testlist) */
2462 /* Forget it if we have just a doc string here */
2463 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
2464 return;
2465 if (NCH(n) == 1) {
2466 com_node(c, CHILD(n, NCH(n)-1));
2467 if (c->c_interactive)
2468 com_addbyte(c, PRINT_EXPR);
2469 else
2470 com_addbyte(c, POP_TOP);
2471 com_pop(c, 1);
2473 else if (TYPE(CHILD(n,1)) == augassign)
2474 com_augassign(c, n);
2475 else {
2476 int i;
2477 com_node(c, CHILD(n, NCH(n)-1));
2478 for (i = 0; i < NCH(n)-2; i+=2) {
2479 if (i+2 < NCH(n)-2) {
2480 com_addbyte(c, DUP_TOP);
2481 com_push(c, 1);
2483 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
2488 static void
2489 com_assert_stmt(struct compiling *c, node *n)
2491 int a = 0, b = 0;
2492 int i;
2493 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2494 /* Generate code like for
2496 if __debug__:
2497 if not <test>:
2498 raise AssertionError [, <message>]
2500 where <message> is the second test, if present.
2503 if (Py_OptimizeFlag)
2504 return;
2505 com_addop_name(c, LOAD_GLOBAL, "__debug__");
2506 com_push(c, 1);
2507 com_addfwref(c, JUMP_IF_FALSE, &a);
2508 com_addbyte(c, POP_TOP);
2509 com_pop(c, 1);
2510 com_node(c, CHILD(n, 1));
2511 com_addfwref(c, JUMP_IF_TRUE, &b);
2512 com_addbyte(c, POP_TOP);
2513 com_pop(c, 1);
2514 /* Raise that exception! */
2515 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
2516 com_push(c, 1);
2517 i = NCH(n)/2; /* Either 2 or 4 */
2518 if (i > 1)
2519 com_node(c, CHILD(n, 3));
2520 com_addoparg(c, RAISE_VARARGS, i);
2521 com_pop(c, i);
2522 /* The interpreter does not fall through */
2523 /* All jumps converge here */
2524 com_backpatch(c, a);
2525 com_backpatch(c, b);
2526 com_addbyte(c, POP_TOP);
2529 static void
2530 com_print_stmt(struct compiling *c, node *n)
2532 int i = 1;
2533 node* stream = NULL;
2535 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
2537 /* are we using the extended print form? */
2538 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2539 stream = CHILD(n, 2);
2540 com_node(c, stream);
2541 /* stack: [...] => [... stream] */
2542 com_push(c, 1);
2543 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2544 i = 4;
2545 else
2546 i = 3;
2548 for (; i < NCH(n); i += 2) {
2549 if (stream != NULL) {
2550 com_addbyte(c, DUP_TOP);
2551 /* stack: [stream] => [stream stream] */
2552 com_push(c, 1);
2553 com_node(c, CHILD(n, i));
2554 /* stack: [stream stream] => [stream stream obj] */
2555 com_addbyte(c, ROT_TWO);
2556 /* stack: [stream stream obj] => [stream obj stream] */
2557 com_addbyte(c, PRINT_ITEM_TO);
2558 /* stack: [stream obj stream] => [stream] */
2559 com_pop(c, 2);
2561 else {
2562 com_node(c, CHILD(n, i));
2563 /* stack: [...] => [... obj] */
2564 com_addbyte(c, PRINT_ITEM);
2565 com_pop(c, 1);
2568 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2569 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
2570 if (stream != NULL) {
2571 /* must pop the extra stream object off the stack */
2572 com_addbyte(c, POP_TOP);
2573 /* stack: [... stream] => [...] */
2574 com_pop(c, 1);
2577 else {
2578 if (stream != NULL) {
2579 /* this consumes the last stream object on stack */
2580 com_addbyte(c, PRINT_NEWLINE_TO);
2581 /* stack: [... stream] => [...] */
2582 com_pop(c, 1);
2584 else
2585 com_addbyte(c, PRINT_NEWLINE);
2589 static void
2590 com_return_stmt(struct compiling *c, node *n)
2592 REQ(n, return_stmt); /* 'return' [testlist] */
2593 if (!c->c_infunction) {
2594 com_error(c, PyExc_SyntaxError, "'return' outside function");
2596 if (NCH(n) < 2) {
2597 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2598 com_push(c, 1);
2600 else
2601 com_node(c, CHILD(n, 1));
2602 com_addbyte(c, RETURN_VALUE);
2603 com_pop(c, 1);
2606 static void
2607 com_raise_stmt(struct compiling *c, node *n)
2609 int i;
2610 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2611 if (NCH(n) > 1) {
2612 com_node(c, CHILD(n, 1));
2613 if (NCH(n) > 3) {
2614 com_node(c, CHILD(n, 3));
2615 if (NCH(n) > 5)
2616 com_node(c, CHILD(n, 5));
2619 i = NCH(n)/2;
2620 com_addoparg(c, RAISE_VARARGS, i);
2621 com_pop(c, i);
2624 static void
2625 com_from_import(struct compiling *c, node *n)
2627 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2628 com_push(c, 1);
2629 if (NCH(n) > 1) {
2630 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2631 com_error(c, PyExc_SyntaxError, "invalid syntax");
2632 return;
2634 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
2635 } else
2636 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
2637 com_pop(c, 1);
2640 static void
2641 com_import_stmt(struct compiling *c, node *n)
2643 int i;
2644 REQ(n, import_stmt);
2645 /* 'import' dotted_name (',' dotted_name)* |
2646 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2647 if (STR(CHILD(n, 0))[0] == 'f') {
2648 PyObject *tup;
2649 /* 'from' dotted_name 'import' ... */
2650 REQ(CHILD(n, 1), dotted_name);
2652 if (TYPE(CHILD(n, 3)) == STAR) {
2653 tup = Py_BuildValue("(s)", "*");
2654 } else {
2655 tup = PyTuple_New((NCH(n) - 2)/2);
2656 for (i = 3; i < NCH(n); i += 2) {
2657 PyTuple_SET_ITEM(tup, (i-3)/2,
2658 PyString_FromString(STR(
2659 CHILD(CHILD(n, i), 0))));
2662 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
2663 Py_DECREF(tup);
2664 com_push(c, 1);
2665 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
2666 if (TYPE(CHILD(n, 3)) == STAR)
2667 com_addbyte(c, IMPORT_STAR);
2668 else {
2669 for (i = 3; i < NCH(n); i += 2)
2670 com_from_import(c, CHILD(n, i));
2671 com_addbyte(c, POP_TOP);
2673 com_pop(c, 1);
2675 else {
2676 /* 'import' ... */
2677 for (i = 1; i < NCH(n); i += 2) {
2678 node *subn = CHILD(n, i);
2679 REQ(subn, dotted_as_name);
2680 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2681 com_push(c, 1);
2682 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
2683 if (NCH(subn) > 1) {
2684 int j;
2685 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
2686 com_error(c, PyExc_SyntaxError,
2687 "invalid syntax");
2688 return;
2690 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2691 com_addopname(c, LOAD_ATTR,
2692 CHILD(CHILD(subn, 0),
2693 j));
2694 com_addop_varname(c, VAR_STORE,
2695 STR(CHILD(subn, 2)));
2696 } else
2697 com_addop_varname(c, VAR_STORE,
2698 STR(CHILD(CHILD(subn, 0),
2699 0)));
2700 com_pop(c, 1);
2705 static void
2706 com_exec_stmt(struct compiling *c, node *n)
2708 REQ(n, exec_stmt);
2709 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2710 com_node(c, CHILD(n, 1));
2711 if (NCH(n) >= 4)
2712 com_node(c, CHILD(n, 3));
2713 else {
2714 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
2715 com_push(c, 1);
2717 if (NCH(n) >= 6)
2718 com_node(c, CHILD(n, 5));
2719 else {
2720 com_addbyte(c, DUP_TOP);
2721 com_push(c, 1);
2723 com_addbyte(c, EXEC_STMT);
2724 com_pop(c, 3);
2727 static int
2728 is_constant_false(struct compiling *c, node *n)
2730 PyObject *v;
2731 int i;
2732 /* argument c will be NULL when called from symtable_node() */
2734 /* Label to avoid tail recursion */
2735 next:
2736 switch (TYPE(n)) {
2738 case suite:
2739 if (NCH(n) == 1) {
2740 n = CHILD(n, 0);
2741 goto next;
2743 /* Fall through */
2744 case file_input:
2745 for (i = 0; i < NCH(n); i++) {
2746 node *ch = CHILD(n, i);
2747 if (TYPE(ch) == stmt) {
2748 n = ch;
2749 goto next;
2752 break;
2754 case stmt:
2755 case simple_stmt:
2756 case small_stmt:
2757 n = CHILD(n, 0);
2758 goto next;
2760 case expr_stmt:
2761 case testlist:
2762 case test:
2763 case and_test:
2764 case not_test:
2765 case comparison:
2766 case expr:
2767 case xor_expr:
2768 case and_expr:
2769 case shift_expr:
2770 case arith_expr:
2771 case term:
2772 case factor:
2773 case power:
2774 case atom:
2775 if (NCH(n) == 1) {
2776 n = CHILD(n, 0);
2777 goto next;
2779 break;
2781 case NAME:
2782 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2783 return 1;
2784 break;
2786 case NUMBER:
2787 v = parsenumber(c, STR(n));
2788 if (v == NULL) {
2789 PyErr_Clear();
2790 break;
2792 i = PyObject_IsTrue(v);
2793 Py_DECREF(v);
2794 return i == 0;
2796 case STRING:
2797 v = parsestr(STR(n));
2798 if (v == NULL) {
2799 PyErr_Clear();
2800 break;
2802 i = PyObject_IsTrue(v);
2803 Py_DECREF(v);
2804 return i == 0;
2807 return 0;
2810 static void
2811 com_if_stmt(struct compiling *c, node *n)
2813 int i;
2814 int anchor = 0;
2815 REQ(n, if_stmt);
2816 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2817 for (i = 0; i+3 < NCH(n); i+=4) {
2818 int a = 0;
2819 node *ch = CHILD(n, i+1);
2820 if (is_constant_false(c, ch))
2821 continue;
2822 if (i > 0)
2823 com_addoparg(c, SET_LINENO, ch->n_lineno);
2824 com_node(c, ch);
2825 com_addfwref(c, JUMP_IF_FALSE, &a);
2826 com_addbyte(c, POP_TOP);
2827 com_pop(c, 1);
2828 com_node(c, CHILD(n, i+3));
2829 com_addfwref(c, JUMP_FORWARD, &anchor);
2830 com_backpatch(c, a);
2831 /* We jump here with an extra entry which we now pop */
2832 com_addbyte(c, POP_TOP);
2834 if (i+2 < NCH(n))
2835 com_node(c, CHILD(n, i+2));
2836 if (anchor)
2837 com_backpatch(c, anchor);
2840 static void
2841 com_while_stmt(struct compiling *c, node *n)
2843 int break_anchor = 0;
2844 int anchor = 0;
2845 int save_begin = c->c_begin;
2846 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2847 com_addfwref(c, SETUP_LOOP, &break_anchor);
2848 block_push(c, SETUP_LOOP);
2849 c->c_begin = c->c_nexti;
2850 com_addoparg(c, SET_LINENO, n->n_lineno);
2851 com_node(c, CHILD(n, 1));
2852 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2853 com_addbyte(c, POP_TOP);
2854 com_pop(c, 1);
2855 c->c_loops++;
2856 com_node(c, CHILD(n, 3));
2857 c->c_loops--;
2858 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2859 c->c_begin = save_begin;
2860 com_backpatch(c, anchor);
2861 /* We jump here with one entry more on the stack */
2862 com_addbyte(c, POP_TOP);
2863 com_addbyte(c, POP_BLOCK);
2864 block_pop(c, SETUP_LOOP);
2865 if (NCH(n) > 4)
2866 com_node(c, CHILD(n, 6));
2867 com_backpatch(c, break_anchor);
2870 static void
2871 com_for_stmt(struct compiling *c, node *n)
2873 PyObject *v;
2874 int break_anchor = 0;
2875 int anchor = 0;
2876 int save_begin = c->c_begin;
2877 REQ(n, for_stmt);
2878 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2879 com_addfwref(c, SETUP_LOOP, &break_anchor);
2880 block_push(c, SETUP_LOOP);
2881 com_node(c, CHILD(n, 3));
2882 v = PyInt_FromLong(0L);
2883 if (v == NULL)
2884 c->c_errors++;
2885 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
2886 com_push(c, 1);
2887 Py_XDECREF(v);
2888 c->c_begin = c->c_nexti;
2889 com_addoparg(c, SET_LINENO, n->n_lineno);
2890 com_addfwref(c, FOR_LOOP, &anchor);
2891 com_push(c, 1);
2892 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
2893 c->c_loops++;
2894 com_node(c, CHILD(n, 5));
2895 c->c_loops--;
2896 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2897 c->c_begin = save_begin;
2898 com_backpatch(c, anchor);
2899 com_pop(c, 2); /* FOR_LOOP has popped these */
2900 com_addbyte(c, POP_BLOCK);
2901 block_pop(c, SETUP_LOOP);
2902 if (NCH(n) > 8)
2903 com_node(c, CHILD(n, 8));
2904 com_backpatch(c, break_anchor);
2907 /* Code generated for "try: S finally: Sf" is as follows:
2909 SETUP_FINALLY L
2910 <code for S>
2911 POP_BLOCK
2912 LOAD_CONST <nil>
2913 L: <code for Sf>
2914 END_FINALLY
2916 The special instructions use the block stack. Each block
2917 stack entry contains the instruction that created it (here
2918 SETUP_FINALLY), the level of the value stack at the time the
2919 block stack entry was created, and a label (here L).
2921 SETUP_FINALLY:
2922 Pushes the current value stack level and the label
2923 onto the block stack.
2924 POP_BLOCK:
2925 Pops en entry from the block stack, and pops the value
2926 stack until its level is the same as indicated on the
2927 block stack. (The label is ignored.)
2928 END_FINALLY:
2929 Pops a variable number of entries from the *value* stack
2930 and re-raises the exception they specify. The number of
2931 entries popped depends on the (pseudo) exception type.
2933 The block stack is unwound when an exception is raised:
2934 when a SETUP_FINALLY entry is found, the exception is pushed
2935 onto the value stack (and the exception condition is cleared),
2936 and the interpreter jumps to the label gotten from the block
2937 stack.
2939 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2940 (The contents of the value stack is shown in [], with the top
2941 at the right; 'tb' is trace-back info, 'val' the exception's
2942 associated value, and 'exc' the exception.)
2944 Value stack Label Instruction Argument
2945 [] SETUP_EXCEPT L1
2946 [] <code for S>
2947 [] POP_BLOCK
2948 [] JUMP_FORWARD L0
2950 [tb, val, exc] L1: DUP )
2951 [tb, val, exc, exc] <evaluate E1> )
2952 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2953 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2954 [tb, val, exc, 1] POP )
2955 [tb, val, exc] POP
2956 [tb, val] <assign to V1> (or POP if no V1)
2957 [tb] POP
2958 [] <code for S1>
2959 JUMP_FORWARD L0
2961 [tb, val, exc, 0] L2: POP
2962 [tb, val, exc] DUP
2963 .............................etc.......................
2965 [tb, val, exc, 0] Ln+1: POP
2966 [tb, val, exc] END_FINALLY # re-raise exception
2968 [] L0: <next statement>
2970 Of course, parts are not generated if Vi or Ei is not present.
2973 static void
2974 com_try_except(struct compiling *c, node *n)
2976 int except_anchor = 0;
2977 int end_anchor = 0;
2978 int else_anchor = 0;
2979 int i;
2980 node *ch;
2982 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
2983 block_push(c, SETUP_EXCEPT);
2984 com_node(c, CHILD(n, 2));
2985 com_addbyte(c, POP_BLOCK);
2986 block_pop(c, SETUP_EXCEPT);
2987 com_addfwref(c, JUMP_FORWARD, &else_anchor);
2988 com_backpatch(c, except_anchor);
2989 for (i = 3;
2990 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
2991 i += 3) {
2992 /* except_clause: 'except' [expr [',' var]] */
2993 if (except_anchor == 0) {
2994 com_error(c, PyExc_SyntaxError,
2995 "default 'except:' must be last");
2996 break;
2998 except_anchor = 0;
2999 com_push(c, 3); /* tb, val, exc pushed by exception */
3000 com_addoparg(c, SET_LINENO, ch->n_lineno);
3001 if (NCH(ch) > 1) {
3002 com_addbyte(c, DUP_TOP);
3003 com_push(c, 1);
3004 com_node(c, CHILD(ch, 1));
3005 com_addoparg(c, COMPARE_OP, EXC_MATCH);
3006 com_pop(c, 1);
3007 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3008 com_addbyte(c, POP_TOP);
3009 com_pop(c, 1);
3011 com_addbyte(c, POP_TOP);
3012 com_pop(c, 1);
3013 if (NCH(ch) > 3)
3014 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
3015 else {
3016 com_addbyte(c, POP_TOP);
3017 com_pop(c, 1);
3019 com_addbyte(c, POP_TOP);
3020 com_pop(c, 1);
3021 com_node(c, CHILD(n, i+2));
3022 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3023 if (except_anchor) {
3024 com_backpatch(c, except_anchor);
3025 /* We come in with [tb, val, exc, 0] on the
3026 stack; one pop and it's the same as
3027 expected at the start of the loop */
3028 com_addbyte(c, POP_TOP);
3031 /* We actually come in here with [tb, val, exc] but the
3032 END_FINALLY will zap those and jump around.
3033 The c_stacklevel does not reflect them so we need not pop
3034 anything. */
3035 com_addbyte(c, END_FINALLY);
3036 com_backpatch(c, else_anchor);
3037 if (i < NCH(n))
3038 com_node(c, CHILD(n, i+2));
3039 com_backpatch(c, end_anchor);
3042 static void
3043 com_try_finally(struct compiling *c, node *n)
3045 int finally_anchor = 0;
3046 node *ch;
3048 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3049 block_push(c, SETUP_FINALLY);
3050 com_node(c, CHILD(n, 2));
3051 com_addbyte(c, POP_BLOCK);
3052 block_pop(c, SETUP_FINALLY);
3053 block_push(c, END_FINALLY);
3054 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3055 /* While the generated code pushes only one item,
3056 the try-finally handling can enter here with
3057 up to three items. OK, here are the details:
3058 3 for an exception, 2 for RETURN, 1 for BREAK. */
3059 com_push(c, 3);
3060 com_backpatch(c, finally_anchor);
3061 ch = CHILD(n, NCH(n)-1);
3062 com_addoparg(c, SET_LINENO, ch->n_lineno);
3063 com_node(c, ch);
3064 com_addbyte(c, END_FINALLY);
3065 block_pop(c, END_FINALLY);
3066 com_pop(c, 3); /* Matches the com_push above */
3069 static void
3070 com_try_stmt(struct compiling *c, node *n)
3072 REQ(n, try_stmt);
3073 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3074 | 'try' ':' suite 'finally' ':' suite */
3075 if (TYPE(CHILD(n, 3)) != except_clause)
3076 com_try_finally(c, n);
3077 else
3078 com_try_except(c, n);
3081 static node *
3082 get_rawdocstring(node *n)
3084 int i;
3086 /* Label to avoid tail recursion */
3087 next:
3088 switch (TYPE(n)) {
3090 case suite:
3091 if (NCH(n) == 1) {
3092 n = CHILD(n, 0);
3093 goto next;
3095 /* Fall through */
3096 case file_input:
3097 for (i = 0; i < NCH(n); i++) {
3098 node *ch = CHILD(n, i);
3099 if (TYPE(ch) == stmt) {
3100 n = ch;
3101 goto next;
3104 break;
3106 case stmt:
3107 case simple_stmt:
3108 case small_stmt:
3109 n = CHILD(n, 0);
3110 goto next;
3112 case expr_stmt:
3113 case testlist:
3114 case test:
3115 case and_test:
3116 case not_test:
3117 case comparison:
3118 case expr:
3119 case xor_expr:
3120 case and_expr:
3121 case shift_expr:
3122 case arith_expr:
3123 case term:
3124 case factor:
3125 case power:
3126 if (NCH(n) == 1) {
3127 n = CHILD(n, 0);
3128 goto next;
3130 break;
3132 case atom:
3133 if (TYPE(CHILD(n, 0)) == STRING)
3134 return n;
3135 break;
3138 return NULL;
3141 static PyObject *
3142 get_docstring(node *n)
3144 /* Don't generate doc-strings if run with -OO */
3145 if (Py_OptimizeFlag > 1)
3146 return NULL;
3147 n = get_rawdocstring(n);
3148 if (n == NULL)
3149 return NULL;
3150 return parsestrplus(n);
3153 static void
3154 com_suite(struct compiling *c, node *n)
3156 REQ(n, suite);
3157 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3158 if (NCH(n) == 1) {
3159 com_node(c, CHILD(n, 0));
3161 else {
3162 int i;
3163 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
3164 node *ch = CHILD(n, i);
3165 if (TYPE(ch) == stmt)
3166 com_node(c, ch);
3171 /* ARGSUSED */
3172 static void
3173 com_continue_stmt(struct compiling *c, node *n)
3175 int i = c->c_nblocks;
3176 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3177 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3179 else if (i <= 0) {
3180 /* at the outer level */
3181 com_error(c, PyExc_SyntaxError,
3182 "'continue' not properly in loop");
3184 else {
3185 int j;
3186 for (j = i-1; j >= 0; --j) {
3187 if (c->c_block[j] == SETUP_LOOP)
3188 break;
3190 if (j >= 0) {
3191 /* there is a loop, but something interferes */
3192 for (; i > j; --i) {
3193 if (c->c_block[i] == SETUP_EXCEPT ||
3194 c->c_block[i] == SETUP_FINALLY) {
3195 com_addoparg(c, CONTINUE_LOOP,
3196 c->c_begin);
3197 return;
3199 if (c->c_block[i] == END_FINALLY) {
3200 com_error(c, PyExc_SyntaxError,
3201 "'continue' not supported inside 'finally' clause");
3202 return;
3206 com_error(c, PyExc_SyntaxError,
3207 "'continue' not properly in loop");
3209 /* XXX Could allow it inside a 'finally' clause
3210 XXX if we could pop the exception still on the stack */
3213 static int
3214 com_argdefs(struct compiling *c, node *n)
3216 int i, nch, nargs, ndefs;
3217 if (TYPE(n) == lambdef) {
3218 /* lambdef: 'lambda' [varargslist] ':' test */
3219 n = CHILD(n, 1);
3221 else {
3222 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3223 n = CHILD(n, 2);
3224 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3225 n = CHILD(n, 1);
3227 if (TYPE(n) != varargslist)
3228 return 0;
3229 /* varargslist:
3230 (fpdef ['=' test] ',')* '*' ....... |
3231 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3232 nch = NCH(n);
3233 nargs = 0;
3234 ndefs = 0;
3235 for (i = 0; i < nch; i++) {
3236 int t;
3237 if (TYPE(CHILD(n, i)) == STAR ||
3238 TYPE(CHILD(n, i)) == DOUBLESTAR)
3239 break;
3240 nargs++;
3241 i++;
3242 if (i >= nch)
3243 t = RPAR; /* Anything except EQUAL or COMMA */
3244 else
3245 t = TYPE(CHILD(n, i));
3246 if (t == EQUAL) {
3247 i++;
3248 ndefs++;
3249 com_node(c, CHILD(n, i));
3250 i++;
3251 if (i >= nch)
3252 break;
3253 t = TYPE(CHILD(n, i));
3255 else {
3256 /* Treat "(a=1, b)" as an error */
3257 if (ndefs)
3258 com_error(c, PyExc_SyntaxError,
3259 "non-default argument follows default argument");
3261 if (t != COMMA)
3262 break;
3264 return ndefs;
3267 static void
3268 com_funcdef(struct compiling *c, node *n)
3270 PyObject *co;
3271 int ndefs;
3272 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3273 ndefs = com_argdefs(c, n);
3274 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3275 n->n_lineno);
3276 co = (PyObject *)icompile(n, c);
3277 symtable_exit_scope(c->c_symtable);
3278 if (co == NULL)
3279 c->c_errors++;
3280 else {
3281 int closure = com_make_closure(c, (PyCodeObject *)co);
3282 int i = com_addconst(c, co);
3283 com_addoparg(c, LOAD_CONST, i);
3284 com_push(c, 1);
3285 if (closure)
3286 com_addoparg(c, MAKE_CLOSURE, ndefs);
3287 else
3288 com_addoparg(c, MAKE_FUNCTION, ndefs);
3289 com_pop(c, ndefs);
3290 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3291 com_pop(c, 1);
3292 Py_DECREF(co);
3296 static void
3297 com_bases(struct compiling *c, node *n)
3299 int i;
3300 REQ(n, testlist);
3301 /* testlist: test (',' test)* [','] */
3302 for (i = 0; i < NCH(n); i += 2)
3303 com_node(c, CHILD(n, i));
3304 i = (NCH(n)+1) / 2;
3305 com_addoparg(c, BUILD_TUPLE, i);
3306 com_pop(c, i-1);
3309 static void
3310 com_classdef(struct compiling *c, node *n)
3312 int i;
3313 PyObject *co, *v;
3314 char *name;
3316 REQ(n, classdef);
3317 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3318 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
3319 c->c_errors++;
3320 return;
3322 /* Push the class name on the stack */
3323 i = com_addconst(c, v);
3324 com_addoparg(c, LOAD_CONST, i);
3325 com_push(c, 1);
3326 Py_DECREF(v);
3327 /* Push the tuple of base classes on the stack */
3328 if (TYPE(CHILD(n, 2)) != LPAR) {
3329 com_addoparg(c, BUILD_TUPLE, 0);
3330 com_push(c, 1);
3332 else
3333 com_bases(c, CHILD(n, 3));
3334 name = STR(CHILD(n, 1));
3335 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
3336 co = (PyObject *)icompile(n, c);
3337 symtable_exit_scope(c->c_symtable);
3338 if (co == NULL)
3339 c->c_errors++;
3340 else {
3341 int closure = com_make_closure(c, (PyCodeObject *)co);
3342 i = com_addconst(c, co);
3343 com_addoparg(c, LOAD_CONST, i);
3344 com_push(c, 1);
3345 if (closure)
3346 com_addoparg(c, MAKE_CLOSURE, 0);
3347 else
3348 com_addoparg(c, MAKE_FUNCTION, 0);
3349 com_addoparg(c, CALL_FUNCTION, 0);
3350 com_addbyte(c, BUILD_CLASS);
3351 com_pop(c, 2);
3352 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
3353 Py_DECREF(co);
3357 static void
3358 com_node(struct compiling *c, node *n)
3360 loop:
3361 if (c->c_errors)
3362 return;
3363 switch (TYPE(n)) {
3365 /* Definition nodes */
3367 case funcdef:
3368 com_funcdef(c, n);
3369 break;
3370 case classdef:
3371 com_classdef(c, n);
3372 break;
3374 /* Trivial parse tree nodes */
3376 case stmt:
3377 case small_stmt:
3378 case flow_stmt:
3379 n = CHILD(n, 0);
3380 goto loop;
3382 case simple_stmt:
3383 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3384 com_addoparg(c, SET_LINENO, n->n_lineno);
3386 int i;
3387 for (i = 0; i < NCH(n)-1; i += 2)
3388 com_node(c, CHILD(n, i));
3390 break;
3392 case compound_stmt:
3393 com_addoparg(c, SET_LINENO, n->n_lineno);
3394 n = CHILD(n, 0);
3395 goto loop;
3397 /* Statement nodes */
3399 case expr_stmt:
3400 com_expr_stmt(c, n);
3401 break;
3402 case print_stmt:
3403 com_print_stmt(c, n);
3404 break;
3405 case del_stmt: /* 'del' exprlist */
3406 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
3407 break;
3408 case pass_stmt:
3409 break;
3410 case break_stmt:
3411 if (c->c_loops == 0) {
3412 com_error(c, PyExc_SyntaxError,
3413 "'break' outside loop");
3415 com_addbyte(c, BREAK_LOOP);
3416 break;
3417 case continue_stmt:
3418 com_continue_stmt(c, n);
3419 break;
3420 case return_stmt:
3421 com_return_stmt(c, n);
3422 break;
3423 case raise_stmt:
3424 com_raise_stmt(c, n);
3425 break;
3426 case import_stmt:
3427 com_import_stmt(c, n);
3428 break;
3429 case global_stmt:
3430 break;
3431 case exec_stmt:
3432 com_exec_stmt(c, n);
3433 break;
3434 case assert_stmt:
3435 com_assert_stmt(c, n);
3436 break;
3437 case if_stmt:
3438 com_if_stmt(c, n);
3439 break;
3440 case while_stmt:
3441 com_while_stmt(c, n);
3442 break;
3443 case for_stmt:
3444 com_for_stmt(c, n);
3445 break;
3446 case try_stmt:
3447 com_try_stmt(c, n);
3448 break;
3449 case suite:
3450 com_suite(c, n);
3451 break;
3453 /* Expression nodes */
3455 case testlist:
3456 com_list(c, n, 0);
3457 break;
3458 case test:
3459 com_test(c, n);
3460 break;
3461 case and_test:
3462 com_and_test(c, n);
3463 break;
3464 case not_test:
3465 com_not_test(c, n);
3466 break;
3467 case comparison:
3468 com_comparison(c, n);
3469 break;
3470 case exprlist:
3471 com_list(c, n, 0);
3472 break;
3473 case expr:
3474 com_expr(c, n);
3475 break;
3476 case xor_expr:
3477 com_xor_expr(c, n);
3478 break;
3479 case and_expr:
3480 com_and_expr(c, n);
3481 break;
3482 case shift_expr:
3483 com_shift_expr(c, n);
3484 break;
3485 case arith_expr:
3486 com_arith_expr(c, n);
3487 break;
3488 case term:
3489 com_term(c, n);
3490 break;
3491 case factor:
3492 com_factor(c, n);
3493 break;
3494 case power:
3495 com_power(c, n);
3496 break;
3497 case atom:
3498 com_atom(c, n);
3499 break;
3501 default:
3502 com_error(c, PyExc_SystemError,
3503 "com_node: unexpected node type");
3507 static void com_fplist(struct compiling *, node *);
3509 static void
3510 com_fpdef(struct compiling *c, node *n)
3512 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3513 if (TYPE(CHILD(n, 0)) == LPAR)
3514 com_fplist(c, CHILD(n, 1));
3515 else {
3516 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
3517 com_pop(c, 1);
3521 static void
3522 com_fplist(struct compiling *c, node *n)
3524 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
3525 if (NCH(n) == 1) {
3526 com_fpdef(c, CHILD(n, 0));
3528 else {
3529 int i = (NCH(n)+1)/2;
3530 com_addoparg(c, UNPACK_SEQUENCE, i);
3531 com_push(c, i-1);
3532 for (i = 0; i < NCH(n); i += 2)
3533 com_fpdef(c, CHILD(n, i));
3537 static void
3538 com_arglist(struct compiling *c, node *n)
3540 int nch, i, narg;
3541 int complex = 0;
3542 char nbuf[10];
3543 REQ(n, varargslist);
3544 /* varargslist:
3545 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3546 nch = NCH(n);
3547 /* Enter all arguments in table of locals */
3548 for (i = 0, narg = 0; i < nch; i++) {
3549 node *ch = CHILD(n, i);
3550 node *fp;
3551 char *name;
3552 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3553 break;
3554 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3555 fp = CHILD(ch, 0);
3556 if (TYPE(fp) == NAME) {
3557 PyObject *v;
3558 name = STR(fp);
3559 v = PyDict_GetItemString(c->c_cellvars, name);
3560 if (v) {
3561 com_addoparg(c, LOAD_FAST, narg);
3562 com_addoparg(c, STORE_DEREF,
3563 PyInt_AS_LONG(v));
3565 } else {
3566 name = nbuf;
3567 sprintf(nbuf, ".%d", i);
3568 complex = 1;
3570 narg++;
3571 /* all name updates handled by symtable */
3572 if (++i >= nch)
3573 break;
3574 ch = CHILD(n, i);
3575 if (TYPE(ch) == EQUAL)
3576 i += 2;
3577 else
3578 REQ(ch, COMMA);
3580 /* Handle *arguments */
3581 if (i < nch) {
3582 node *ch;
3583 ch = CHILD(n, i);
3584 if (TYPE(ch) != DOUBLESTAR) {
3585 REQ(ch, STAR);
3586 ch = CHILD(n, i+1);
3587 if (TYPE(ch) == NAME) {
3588 PyObject *v;
3589 i += 3;
3590 v = PyDict_GetItemString(c->c_cellvars,
3591 STR(ch));
3592 if (v) {
3593 com_addoparg(c, LOAD_FAST, narg);
3594 com_addoparg(c, STORE_DEREF,
3595 PyInt_AS_LONG(v));
3597 narg++;
3601 /* Handle **keywords */
3602 if (i < nch) {
3603 PyObject *v;
3604 node *ch;
3605 ch = CHILD(n, i);
3606 if (TYPE(ch) != DOUBLESTAR) {
3607 REQ(ch, STAR);
3608 ch = CHILD(n, i+1);
3609 REQ(ch, STAR);
3610 ch = CHILD(n, i+2);
3612 else
3613 ch = CHILD(n, i+1);
3614 REQ(ch, NAME);
3615 v = PyDict_GetItemString(c->c_cellvars, STR(ch));
3616 if (v) {
3617 com_addoparg(c, LOAD_FAST, narg);
3618 com_addoparg(c, STORE_DEREF, PyInt_AS_LONG(v));
3621 if (complex) {
3622 /* Generate code for complex arguments only after
3623 having counted the simple arguments */
3624 int ilocal = 0;
3625 for (i = 0; i < nch; i++) {
3626 node *ch = CHILD(n, i);
3627 node *fp;
3628 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
3629 break;
3630 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3631 fp = CHILD(ch, 0);
3632 if (TYPE(fp) != NAME) {
3633 com_addoparg(c, LOAD_FAST, ilocal);
3634 com_push(c, 1);
3635 com_fpdef(c, ch);
3637 ilocal++;
3638 if (++i >= nch)
3639 break;
3640 ch = CHILD(n, i);
3641 if (TYPE(ch) == EQUAL)
3642 i += 2;
3643 else
3644 REQ(ch, COMMA);
3649 static void
3650 com_file_input(struct compiling *c, node *n)
3652 int i;
3653 PyObject *doc;
3654 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
3655 doc = get_docstring(n);
3656 if (doc != NULL) {
3657 int i = com_addconst(c, doc);
3658 Py_DECREF(doc);
3659 com_addoparg(c, LOAD_CONST, i);
3660 com_push(c, 1);
3661 com_addop_name(c, STORE_NAME, "__doc__");
3662 com_pop(c, 1);
3664 for (i = 0; i < NCH(n); i++) {
3665 node *ch = CHILD(n, i);
3666 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3667 com_node(c, ch);
3671 /* Top-level compile-node interface */
3673 static void
3674 compile_funcdef(struct compiling *c, node *n)
3676 PyObject *doc;
3677 node *ch;
3678 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
3679 c->c_name = STR(CHILD(n, 1));
3680 doc = get_docstring(CHILD(n, 4));
3681 if (doc != NULL) {
3682 (void) com_addconst(c, doc);
3683 Py_DECREF(doc);
3685 else
3686 (void) com_addconst(c, Py_None); /* No docstring */
3687 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3688 ch = CHILD(ch, 1); /* ')' | varargslist */
3689 if (TYPE(ch) == varargslist)
3690 com_arglist(c, ch);
3691 c->c_infunction = 1;
3692 com_node(c, CHILD(n, 4));
3693 c->c_infunction = 0;
3694 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3695 com_push(c, 1);
3696 com_addbyte(c, RETURN_VALUE);
3697 com_pop(c, 1);
3700 static void
3701 compile_lambdef(struct compiling *c, node *n)
3703 node *ch;
3704 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
3705 c->c_name = "<lambda>";
3707 ch = CHILD(n, 1);
3708 (void) com_addconst(c, Py_None); /* No docstring */
3709 if (TYPE(ch) == varargslist) {
3710 com_arglist(c, ch);
3711 ch = CHILD(n, 3);
3713 else
3714 ch = CHILD(n, 2);
3715 com_node(c, ch);
3716 com_addbyte(c, RETURN_VALUE);
3717 com_pop(c, 1);
3720 static void
3721 compile_classdef(struct compiling *c, node *n)
3723 node *ch;
3724 PyObject *doc;
3725 REQ(n, classdef);
3726 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3727 c->c_name = STR(CHILD(n, 1));
3728 c->c_private = c->c_name;
3729 ch = CHILD(n, NCH(n)-1); /* The suite */
3730 doc = get_docstring(ch);
3731 if (doc != NULL) {
3732 int i = com_addconst(c, doc);
3733 Py_DECREF(doc);
3734 com_addoparg(c, LOAD_CONST, i);
3735 com_push(c, 1);
3736 com_addop_name(c, STORE_NAME, "__doc__");
3737 com_pop(c, 1);
3739 else
3740 (void) com_addconst(c, Py_None);
3741 com_node(c, ch);
3742 com_addbyte(c, LOAD_LOCALS);
3743 com_push(c, 1);
3744 com_addbyte(c, RETURN_VALUE);
3745 com_pop(c, 1);
3748 static void
3749 compile_node(struct compiling *c, node *n)
3751 com_addoparg(c, SET_LINENO, n->n_lineno);
3753 switch (TYPE(n)) {
3755 case single_input: /* One interactive command */
3756 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3757 c->c_interactive++;
3758 n = CHILD(n, 0);
3759 if (TYPE(n) != NEWLINE)
3760 com_node(c, n);
3761 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3762 com_push(c, 1);
3763 com_addbyte(c, RETURN_VALUE);
3764 com_pop(c, 1);
3765 c->c_interactive--;
3766 break;
3768 case file_input: /* A whole file, or built-in function exec() */
3769 com_file_input(c, n);
3770 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3771 com_push(c, 1);
3772 com_addbyte(c, RETURN_VALUE);
3773 com_pop(c, 1);
3774 break;
3776 case eval_input: /* Built-in function input() */
3777 com_node(c, CHILD(n, 0));
3778 com_addbyte(c, RETURN_VALUE);
3779 com_pop(c, 1);
3780 break;
3782 case lambdef: /* anonymous function definition */
3783 compile_lambdef(c, n);
3784 break;
3786 case funcdef: /* A function definition */
3787 compile_funcdef(c, n);
3788 break;
3790 case classdef: /* A class definition */
3791 compile_classdef(c, n);
3792 break;
3794 default:
3795 com_error(c, PyExc_SystemError,
3796 "compile_node: unexpected node type");
3800 static PyObject *
3801 dict_keys_inorder(PyObject *dict, int offset)
3803 PyObject *tuple, *k, *v;
3804 int i, pos = 0, size = PyDict_Size(dict);
3806 tuple = PyTuple_New(size);
3807 if (tuple == NULL)
3808 return NULL;
3809 while (PyDict_Next(dict, &pos, &k, &v)) {
3810 i = PyInt_AS_LONG(v);
3811 Py_INCREF(k);
3812 PyTuple_SET_ITEM(tuple, i - offset, k);
3814 return tuple;
3817 PyCodeObject *
3818 PyNode_Compile(node *n, char *filename)
3820 return PyNode_CompileFlags(n, filename, NULL);
3823 PyCodeObject *
3824 PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
3826 return jcompile(n, filename, NULL, flags);
3829 struct symtable *
3830 PyNode_CompileSymtable(node *n, char *filename)
3832 struct symtable *st;
3834 st = symtable_init();
3835 if (st == NULL)
3836 return NULL;
3837 assert(st->st_symbols != NULL);
3838 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
3839 if (st->st_errors > 0) {
3840 PySymtable_Free(st);
3841 return NULL;
3843 symtable_node(st, n);
3844 if (st->st_errors > 0) {
3845 PySymtable_Free(st);
3846 return NULL;
3848 return st;
3851 static PyCodeObject *
3852 icompile(node *n, struct compiling *base)
3854 return jcompile(n, base->c_filename, base, NULL);
3857 static PyCodeObject *
3858 jcompile(node *n, char *filename, struct compiling *base,
3859 PyCompilerFlags *flags)
3861 struct compiling sc;
3862 PyCodeObject *co;
3863 if (!com_init(&sc, filename))
3864 return NULL;
3865 if (base) {
3866 sc.c_private = base->c_private;
3867 sc.c_symtable = base->c_symtable;
3868 /* c_symtable still points to parent's symbols */
3869 if (base->c_nested
3870 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
3871 sc.c_nested = 1;
3872 } else {
3873 sc.c_private = NULL;
3874 sc.c_future = PyNode_Future(n, filename);
3875 if (sc.c_future == NULL) {
3876 com_free(&sc);
3877 return NULL;
3879 if (flags) {
3880 if (flags->cf_nested_scopes)
3881 sc.c_future->ff_nested_scopes = 1;
3882 else if (sc.c_future->ff_nested_scopes)
3883 flags->cf_nested_scopes = 1;
3885 if (symtable_build(&sc, n) < 0) {
3886 com_free(&sc);
3887 return NULL;
3890 co = NULL;
3891 if (symtable_load_symbols(&sc) < 0) {
3892 sc.c_errors++;
3893 goto exit;
3895 compile_node(&sc, n);
3896 com_done(&sc);
3897 if (sc.c_errors == 0) {
3898 PyObject *consts, *names, *varnames, *filename, *name,
3899 *freevars, *cellvars;
3900 consts = PyList_AsTuple(sc.c_consts);
3901 names = PyList_AsTuple(sc.c_names);
3902 varnames = PyList_AsTuple(sc.c_varnames);
3903 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
3904 freevars = dict_keys_inorder(sc.c_freevars,
3905 PyTuple_GET_SIZE(cellvars));
3906 filename = PyString_InternFromString(sc.c_filename);
3907 name = PyString_InternFromString(sc.c_name);
3908 if (!PyErr_Occurred())
3909 co = PyCode_New(sc.c_argcount,
3910 sc.c_nlocals,
3911 sc.c_maxstacklevel,
3912 sc.c_flags,
3913 sc.c_code,
3914 consts,
3915 names,
3916 varnames,
3917 freevars,
3918 cellvars,
3919 filename,
3920 name,
3921 sc.c_firstlineno,
3922 sc.c_lnotab);
3923 Py_XDECREF(consts);
3924 Py_XDECREF(names);
3925 Py_XDECREF(varnames);
3926 Py_XDECREF(freevars);
3927 Py_XDECREF(cellvars);
3928 Py_XDECREF(filename);
3929 Py_XDECREF(name);
3931 else if (!PyErr_Occurred()) {
3932 /* This could happen if someone called PyErr_Clear() after an
3933 error was reported above. That's not supposed to happen,
3934 but I just plugged one case and I'm not sure there can't be
3935 others. In that case, raise SystemError so that at least
3936 it gets reported instead dumping core. */
3937 PyErr_SetString(PyExc_SystemError, "lost syntax error");
3939 exit:
3940 if (base == NULL) {
3941 PySymtable_Free(sc.c_symtable);
3942 sc.c_symtable = NULL;
3944 com_free(&sc);
3945 return co;
3949 PyCode_Addr2Line(PyCodeObject *co, int addrq)
3951 int size = PyString_Size(co->co_lnotab) / 2;
3952 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
3953 int line = co->co_firstlineno;
3954 int addr = 0;
3955 while (--size >= 0) {
3956 addr += *p++;
3957 if (addr > addrq)
3958 break;
3959 line += *p++;
3961 return line;
3964 /* The test for LOCAL must come before the test for FREE in order to
3965 handle classes where name is both local and free. The local var is
3966 a method and the free var is a free var referenced within a method.
3969 static int
3970 get_ref_type(struct compiling *c, char *name)
3972 PyObject *v;
3973 if (c->c_symtable->st_nested_scopes) {
3974 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
3975 return CELL;
3976 if (PyDict_GetItemString(c->c_locals, name) != NULL)
3977 return LOCAL;
3978 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
3979 return FREE;
3980 v = PyDict_GetItemString(c->c_globals, name);
3981 if (v) {
3982 if (v == Py_None)
3983 return GLOBAL_EXPLICIT;
3984 else {
3985 return GLOBAL_IMPLICIT;
3988 } else {
3989 if (PyDict_GetItemString(c->c_locals, name) != NULL)
3990 return LOCAL;
3991 v = PyDict_GetItemString(c->c_globals, name);
3992 if (v) {
3993 if (v == Py_None)
3994 return GLOBAL_EXPLICIT;
3995 else {
3996 return GLOBAL_IMPLICIT;
4001 char buf[350];
4002 sprintf(buf,
4003 "unknown scope for %.100s in %.100s(%s) "
4004 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4005 name, c->c_name,
4006 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4007 c->c_filename,
4008 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4009 PyObject_REPR(c->c_locals),
4010 PyObject_REPR(c->c_globals)
4013 Py_FatalError(buf);
4015 return -1; /* can't get here */
4018 /* Helper functions to issue warnings */
4020 static int
4021 issue_warning(char *msg, char *filename, int lineno)
4023 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4024 lineno, NULL, NULL) < 0) {
4025 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4026 PyErr_SetString(PyExc_SyntaxError, msg);
4027 PyErr_SyntaxLocation(filename, lineno);
4029 return -1;
4031 return 0;
4034 static int
4035 symtable_warn(struct symtable *st, char *msg)
4037 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
4038 st->st_errors++;
4039 return -1;
4041 return 0;
4044 /* Helper function for setting lineno and filename */
4046 static int
4047 symtable_build(struct compiling *c, node *n)
4049 if ((c->c_symtable = symtable_init()) == NULL)
4050 return -1;
4051 c->c_symtable->st_future = c->c_future;
4052 if (c->c_future->ff_nested_scopes)
4053 c->c_symtable->st_nested_scopes = 1;
4054 c->c_symtable->st_filename = c->c_filename;
4055 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4056 if (c->c_symtable->st_errors > 0)
4057 return -1;
4058 symtable_node(c->c_symtable, n);
4059 if (c->c_symtable->st_errors > 0)
4060 return -1;
4061 /* reset for second pass */
4062 c->c_symtable->st_nscopes = 1;
4063 c->c_symtable->st_pass = 2;
4064 return 0;
4067 static int
4068 symtable_init_compiling_symbols(struct compiling *c)
4070 PyObject *varnames;
4072 varnames = c->c_symtable->st_cur->ste_varnames;
4073 if (varnames == NULL) {
4074 varnames = PyList_New(0);
4075 if (varnames == NULL)
4076 return -1;
4077 c->c_symtable->st_cur->ste_varnames = varnames;
4078 Py_INCREF(varnames);
4079 } else
4080 Py_INCREF(varnames);
4081 c->c_varnames = varnames;
4083 c->c_globals = PyDict_New();
4084 if (c->c_globals == NULL)
4085 return -1;
4086 c->c_freevars = PyDict_New();
4087 if (c->c_freevars == NULL)
4088 return -1;
4089 c->c_cellvars = PyDict_New();
4090 if (c->c_cellvars == NULL)
4091 return -1;
4092 return 0;
4095 struct symbol_info {
4096 int si_nlocals;
4097 int si_ncells;
4098 int si_nfrees;
4099 int si_nimplicit;
4102 static void
4103 symtable_init_info(struct symbol_info *si)
4105 si->si_nlocals = 0;
4106 si->si_ncells = 0;
4107 si->si_nfrees = 0;
4108 si->si_nimplicit = 0;
4111 static int
4112 symtable_resolve_free(struct compiling *c, PyObject *name,
4113 struct symbol_info *si)
4115 PyObject *dict, *v;
4117 /* Seperate logic for DEF_FREE. If it occurs in a function,
4118 it indicates a local that we must allocate storage for (a
4119 cell var). If it occurs in a class, then the class has a
4120 method and a free variable with the same name.
4123 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
4124 v = PyInt_FromLong(si->si_ncells++);
4125 dict = c->c_cellvars;
4126 } else {
4127 v = PyInt_FromLong(si->si_nfrees++);
4128 dict = c->c_freevars;
4130 if (v == NULL)
4131 return -1;
4132 if (PyDict_SetItem(dict, name, v) < 0) {
4133 Py_DECREF(v);
4134 return -1;
4136 Py_DECREF(v);
4137 return 0;
4140 static int
4141 symtable_freevar_offsets(PyObject *freevars, int offset)
4143 PyObject *name, *v;
4144 int pos;
4146 /* The cell vars are the first elements of the closure,
4147 followed by the free vars. Update the offsets in
4148 c_freevars to account for number of cellvars. */
4149 pos = 0;
4150 while (PyDict_Next(freevars, &pos, &name, &v)) {
4151 int i = PyInt_AS_LONG(v) + offset;
4152 PyObject *o = PyInt_FromLong(i);
4153 if (o == NULL)
4154 return -1;
4155 if (PyDict_SetItem(freevars, name, o) < 0) {
4156 Py_DECREF(o);
4157 return -1;
4159 Py_DECREF(o);
4161 return 0;
4164 static int
4165 symtable_check_unoptimized(struct compiling *c,
4166 PySymtableEntryObject *ste,
4167 struct symbol_info *si)
4169 char buf[300];
4171 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4172 || (ste->ste_nested && si->si_nimplicit)))
4173 return 0;
4175 #define ILLEGAL_IMPORT_STAR \
4176 "import * is not allowed in function '%.100s' " \
4177 "because it contains a nested function with free variables"
4179 #define ILLEGAL_BARE_EXEC \
4180 "unqualified exec is not allowed in function '%.100s' " \
4181 "because it contains a nested function with free variables"
4183 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4184 "function '%.100s' uses import * and bare exec, which are illegal" \
4185 "because it contains a nested function with free variables"
4187 /* XXX perhaps the linenos for these opt-breaking statements
4188 should be stored so the exception can point to them. */
4190 if (ste->ste_optimized == OPT_IMPORT_STAR)
4191 sprintf(buf, ILLEGAL_IMPORT_STAR,
4192 PyString_AS_STRING(ste->ste_name));
4193 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4194 sprintf(buf, ILLEGAL_BARE_EXEC,
4195 PyString_AS_STRING(ste->ste_name));
4196 else {
4197 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4198 PyString_AS_STRING(ste->ste_name));
4201 if (c->c_symtable->st_nested_scopes) {
4202 PyErr_SetString(PyExc_SyntaxError, buf);
4203 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4204 ste->ste_lineno);
4205 return -1;
4207 else {
4208 return issue_warning(buf, c->c_filename, ste->ste_lineno);
4210 return 0;
4213 static int
4214 symtable_check_shadow(struct symtable *st, PyObject *name, int flags)
4216 char buf[500];
4217 PyObject *children, *v;
4218 PySymtableEntryObject *child = NULL;
4219 int i;
4221 if (!(flags & DEF_BOUND))
4222 return 0;
4223 /* The semantics of this code will change with nested scopes.
4224 It is defined in the current scope and referenced in a
4225 child scope. Under the old rules, the child will see a
4226 global. Under the new rules, the child will see the
4227 binding in the current scope.
4230 /* Find name of child function that has free variable */
4231 children = st->st_cur->ste_children;
4232 for (i = 0; i < PyList_GET_SIZE(children); i++) {
4233 int cflags;
4234 child = (PySymtableEntryObject *)PyList_GET_ITEM(children, i);
4235 v = PyDict_GetItem(child->ste_symbols, name);
4236 if (v == NULL)
4237 continue;
4238 cflags = PyInt_AS_LONG(v);
4239 if (!(cflags & DEF_BOUND))
4240 break;
4243 assert(child != NULL);
4245 sprintf(buf, "local name '%.100s' in '%.100s' shadows "
4246 "use of '%.100s' as global in nested scope '%.100s'",
4247 PyString_AS_STRING(name),
4248 PyString_AS_STRING(st->st_cur->ste_name),
4249 PyString_AS_STRING(name),
4250 PyString_AS_STRING(child->ste_name)
4253 return symtable_warn(st, buf);
4256 static int
4257 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4258 struct symbol_info *si)
4260 if (ste->ste_type != TYPE_MODULE)
4261 c->c_flags |= CO_NEWLOCALS;
4262 if (ste->ste_type == TYPE_FUNCTION) {
4263 c->c_nlocals = si->si_nlocals;
4264 if (ste->ste_optimized == 0)
4265 c->c_flags |= CO_OPTIMIZED;
4266 else if (ste->ste_optimized != OPT_EXEC)
4267 return symtable_check_unoptimized(c, ste, si);
4269 return 0;
4272 static int
4273 symtable_load_symbols(struct compiling *c)
4275 static PyObject *implicit = NULL;
4276 struct symtable *st = c->c_symtable;
4277 PySymtableEntryObject *ste = st->st_cur;
4278 PyObject *name, *varnames, *v;
4279 int i, flags, pos;
4280 struct symbol_info si;
4282 if (implicit == NULL) {
4283 implicit = PyInt_FromLong(1);
4284 if (implicit == NULL)
4285 return -1;
4287 v = NULL;
4289 if (symtable_init_compiling_symbols(c) < 0)
4290 goto fail;
4291 symtable_init_info(&si);
4292 varnames = st->st_cur->ste_varnames;
4293 si.si_nlocals = PyList_GET_SIZE(varnames);
4294 c->c_argcount = si.si_nlocals;
4296 for (i = 0; i < si.si_nlocals; ++i) {
4297 v = PyInt_FromLong(i);
4298 if (PyDict_SetItem(c->c_locals,
4299 PyList_GET_ITEM(varnames, i), v) < 0)
4300 goto fail;
4301 Py_DECREF(v);
4304 /* XXX The cases below define the rules for whether a name is
4305 local or global. The logic could probably be clearer. */
4306 pos = 0;
4307 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4308 flags = PyInt_AS_LONG(v);
4310 if (st->st_nested_scopes == 0
4311 && (flags & (DEF_FREE | DEF_FREE_CLASS))) {
4312 if (symtable_check_shadow(st, name, flags) < 0)
4313 goto fail;
4316 if (flags & DEF_FREE_GLOBAL)
4317 /* undo the original DEF_FREE */
4318 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
4320 if ((flags & (DEF_FREE | DEF_FREE_CLASS))
4321 && (flags & (DEF_LOCAL | DEF_PARAM)))
4322 symtable_resolve_free(c, name, &si);
4324 if (flags & DEF_STAR) {
4325 c->c_argcount--;
4326 c->c_flags |= CO_VARARGS;
4327 } else if (flags & DEF_DOUBLESTAR) {
4328 c->c_argcount--;
4329 c->c_flags |= CO_VARKEYWORDS;
4330 } else if (flags & DEF_INTUPLE)
4331 c->c_argcount--;
4332 else if (flags & DEF_GLOBAL) {
4333 if (flags & DEF_PARAM) {
4334 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
4335 PyString_AS_STRING(name));
4336 PyErr_SyntaxLocation(st->st_filename,
4337 ste->ste_lineno);
4338 st->st_errors++;
4339 goto fail;
4341 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4342 goto fail;
4343 } else if (flags & DEF_FREE_GLOBAL) {
4344 si.si_nimplicit++;
4345 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4346 goto fail;
4347 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
4348 v = PyInt_FromLong(si.si_nlocals++);
4349 if (v == NULL)
4350 goto fail;
4351 if (PyDict_SetItem(c->c_locals, name, v) < 0)
4352 goto fail;
4353 Py_DECREF(v);
4354 if (ste->ste_type != TYPE_CLASS)
4355 if (PyList_Append(c->c_varnames, name) < 0)
4356 goto fail;
4357 } else if (is_free(flags)) {
4358 if (ste->ste_nested && st->st_nested_scopes) {
4359 v = PyInt_FromLong(si.si_nfrees++);
4360 if (v == NULL)
4361 goto fail;
4362 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
4363 goto fail;
4364 Py_DECREF(v);
4365 } else {
4366 si.si_nimplicit++;
4367 if (PyDict_SetItem(c->c_globals, name,
4368 implicit) < 0)
4369 goto fail;
4370 if (st->st_nscopes != 1) {
4371 v = PyInt_FromLong(flags);
4372 if (PyDict_SetItem(st->st_global,
4373 name, v))
4374 goto fail;
4375 Py_DECREF(v);
4381 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4382 return -1;
4383 return symtable_update_flags(c, ste, &si);
4384 fail:
4385 /* is this always the right thing to do? */
4386 Py_XDECREF(v);
4387 return -1;
4390 static struct symtable *
4391 symtable_init()
4393 struct symtable *st;
4395 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4396 if (st == NULL)
4397 return NULL;
4398 st->st_pass = 1;
4399 st->st_nested_scopes = NESTED_SCOPES_DEFAULT;
4400 st->st_filename = NULL;
4401 if ((st->st_stack = PyList_New(0)) == NULL)
4402 goto fail;
4403 if ((st->st_symbols = PyDict_New()) == NULL)
4404 goto fail;
4405 st->st_cur = NULL;
4406 st->st_nscopes = 0;
4407 st->st_errors = 0;
4408 st->st_tmpname = 0;
4409 st->st_private = NULL;
4410 return st;
4411 fail:
4412 PySymtable_Free(st);
4413 return NULL;
4416 void
4417 PySymtable_Free(struct symtable *st)
4419 Py_XDECREF(st->st_symbols);
4420 Py_XDECREF(st->st_stack);
4421 Py_XDECREF(st->st_cur);
4422 PyMem_Free((void *)st);
4425 /* When the compiler exits a scope, it must should update the scope's
4426 free variable information with the list of free variables in its
4427 children.
4429 Variables that are free in children and defined in the current
4430 scope are cellvars.
4432 If the scope being exited is defined at the top-level (ste_nested is
4433 false), free variables in children that are not defined here are
4434 implicit globals.
4438 static int
4439 symtable_update_free_vars(struct symtable *st)
4441 int i, j, def;
4442 PyObject *o, *name, *list = NULL;
4443 PySymtableEntryObject *child, *ste = st->st_cur;
4445 if (ste->ste_type == TYPE_CLASS)
4446 def = DEF_FREE_CLASS;
4447 else
4448 def = DEF_FREE;
4449 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4450 int pos = 0;
4452 if (list)
4453 PyList_SetSlice(list, 0,
4454 ((PyVarObject*)list)->ob_size, 0);
4455 child = (PySymtableEntryObject *)
4456 PyList_GET_ITEM(ste->ste_children, i);
4457 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
4458 int flags = PyInt_AS_LONG(o);
4459 if (!(is_free(flags)))
4460 continue; /* avoids indentation */
4461 if (list == NULL) {
4462 list = PyList_New(0);
4463 if (list == NULL)
4464 return -1;
4466 ste->ste_child_free = 1;
4467 if (PyList_Append(list, name) < 0) {
4468 Py_DECREF(list);
4469 return -1;
4472 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
4473 PyObject *v;
4474 name = PyList_GET_ITEM(list, j);
4475 v = PyDict_GetItem(ste->ste_symbols, name);
4476 /* If a name N is declared global in scope A and
4477 referenced in scope B contained (perhaps
4478 indirectly) in A and there are no scopes
4479 with bindings for N between B and A, then N
4480 is global in B.
4482 if (v) {
4483 int flags = PyInt_AS_LONG(v);
4484 if (flags & DEF_GLOBAL) {
4485 symtable_undo_free(st, child->ste_id,
4486 name);
4487 continue;
4490 if (ste->ste_nested) {
4491 if (symtable_add_def_o(st, ste->ste_symbols,
4492 name, def) < 0) {
4493 Py_DECREF(list);
4494 return -1;
4496 } else {
4497 if (symtable_check_global(st, child->ste_id,
4498 name) < 0) {
4499 Py_DECREF(list);
4500 return -1;
4506 Py_XDECREF(list);
4507 return 0;
4510 /* If the current scope is a non-nested class or if name is not
4511 defined in the current, non-nested scope, then it is an implicit
4512 global in all nested scopes.
4515 static int
4516 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4518 PyObject *o;
4519 int v;
4520 PySymtableEntryObject *ste = st->st_cur;
4522 if (ste->ste_type == TYPE_CLASS)
4523 return symtable_undo_free(st, child, name);
4524 o = PyDict_GetItem(ste->ste_symbols, name);
4525 if (o == NULL)
4526 return symtable_undo_free(st, child, name);
4527 v = PyInt_AS_LONG(o);
4529 if (is_free(v) || (v & DEF_GLOBAL))
4530 return symtable_undo_free(st, child, name);
4531 else
4532 return symtable_add_def_o(st, ste->ste_symbols,
4533 name, DEF_FREE);
4536 static int
4537 symtable_undo_free(struct symtable *st, PyObject *id,
4538 PyObject *name)
4540 int i, v, x;
4541 PyObject *info;
4542 PySymtableEntryObject *ste;
4544 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4545 if (ste == NULL)
4546 return -1;
4548 info = PyDict_GetItem(ste->ste_symbols, name);
4549 if (info == NULL)
4550 return 0;
4551 v = PyInt_AS_LONG(info);
4552 if (is_free(v)) {
4553 if (symtable_add_def_o(st, ste->ste_symbols, name,
4554 DEF_FREE_GLOBAL) < 0)
4555 return -1;
4556 } else
4557 /* If the name is defined here or declared global,
4558 then the recursion stops. */
4559 return 0;
4561 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4562 PySymtableEntryObject *child;
4563 child = (PySymtableEntryObject *)
4564 PyList_GET_ITEM(ste->ste_children, i);
4565 x = symtable_undo_free(st, child->ste_id, name);
4566 if (x < 0)
4567 return x;
4569 return 0;
4572 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4573 This reference is released when the scope is exited, via the DECREF
4574 in symtable_exit_scope().
4577 static int
4578 symtable_exit_scope(struct symtable *st)
4580 int end;
4582 if (st->st_pass == 1)
4583 symtable_update_free_vars(st);
4584 Py_DECREF(st->st_cur);
4585 end = PyList_GET_SIZE(st->st_stack) - 1;
4586 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4587 end);
4588 if (PySequence_DelItem(st->st_stack, end) < 0)
4589 return -1;
4590 return 0;
4593 static void
4594 symtable_enter_scope(struct symtable *st, char *name, int type,
4595 int lineno)
4597 PySymtableEntryObject *prev = NULL;
4599 if (st->st_cur) {
4600 prev = st->st_cur;
4601 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4602 Py_DECREF(st->st_cur);
4603 st->st_errors++;
4604 return;
4607 st->st_cur = (PySymtableEntryObject *)
4608 PySymtableEntry_New(st, name, type, lineno);
4609 if (strcmp(name, TOP) == 0)
4610 st->st_global = st->st_cur->ste_symbols;
4611 if (prev && st->st_pass == 1) {
4612 if (PyList_Append(prev->ste_children,
4613 (PyObject *)st->st_cur) < 0)
4614 st->st_errors++;
4618 static int
4619 symtable_lookup(struct symtable *st, char *name)
4621 char buffer[MANGLE_LEN];
4622 PyObject *v;
4623 int flags;
4625 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4626 name = buffer;
4627 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4628 if (v == NULL) {
4629 if (PyErr_Occurred())
4630 return -1;
4631 else
4632 return 0;
4635 flags = PyInt_AS_LONG(v);
4636 return flags;
4639 static int
4640 symtable_add_def(struct symtable *st, char *name, int flag)
4642 PyObject *s;
4643 char buffer[MANGLE_LEN];
4644 int ret;
4646 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4647 name = buffer;
4648 if ((s = PyString_InternFromString(name)) == NULL)
4649 return -1;
4650 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4651 Py_DECREF(s);
4652 return ret;
4655 /* Must only be called with mangled names */
4657 static int
4658 symtable_add_def_o(struct symtable *st, PyObject *dict,
4659 PyObject *name, int flag)
4661 PyObject *o;
4662 int val;
4664 if ((o = PyDict_GetItem(dict, name))) {
4665 val = PyInt_AS_LONG(o);
4666 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
4667 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4668 PyString_AsString(name));
4669 PyErr_SyntaxLocation(st->st_filename,
4670 st->st_cur->ste_lineno);
4671 return -1;
4673 val |= flag;
4674 } else
4675 val = flag;
4676 o = PyInt_FromLong(val);
4677 if (PyDict_SetItem(dict, name, o) < 0) {
4678 Py_DECREF(o);
4679 return -1;
4681 Py_DECREF(o);
4683 if (flag & DEF_PARAM) {
4684 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
4685 return -1;
4686 } else if (flag & DEF_GLOBAL) {
4687 /* XXX need to update DEF_GLOBAL for other flags too;
4688 perhaps only DEF_FREE_GLOBAL */
4689 if ((o = PyDict_GetItem(st->st_global, name))) {
4690 val = PyInt_AS_LONG(o);
4691 val |= flag;
4692 } else
4693 val = flag;
4694 o = PyInt_FromLong(val);
4695 if (PyDict_SetItem(st->st_global, name, o) < 0) {
4696 Py_DECREF(o);
4697 return -1;
4699 Py_DECREF(o);
4701 return 0;
4704 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4706 static void
4707 symtable_node(struct symtable *st, node *n)
4709 int i, start = 0;
4711 loop:
4712 switch (TYPE(n)) {
4713 case funcdef: {
4714 char *func_name = STR(CHILD(n, 1));
4715 symtable_add_def(st, func_name, DEF_LOCAL);
4716 symtable_default_args(st, CHILD(n, 2));
4717 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
4718 symtable_funcdef(st, n);
4719 symtable_exit_scope(st);
4720 break;
4722 case lambdef:
4723 if (NCH(n) == 4)
4724 symtable_default_args(st, CHILD(n, 1));
4725 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
4726 symtable_funcdef(st, n);
4727 symtable_exit_scope(st);
4728 break;
4729 case classdef: {
4730 char *tmp, *class_name = STR(CHILD(n, 1));
4731 symtable_add_def(st, class_name, DEF_LOCAL);
4732 if (TYPE(CHILD(n, 2)) == LPAR) {
4733 node *bases = CHILD(n, 3);
4734 int i;
4735 for (i = 0; i < NCH(bases); i += 2) {
4736 symtable_node(st, CHILD(bases, i));
4739 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
4740 tmp = st->st_private;
4741 st->st_private = class_name;
4742 symtable_node(st, CHILD(n, NCH(n) - 1));
4743 st->st_private = tmp;
4744 symtable_exit_scope(st);
4745 break;
4747 case if_stmt:
4748 for (i = 0; i + 3 < NCH(n); i += 4) {
4749 if (is_constant_false(NULL, (CHILD(n, i + 1))))
4750 continue;
4751 symtable_node(st, CHILD(n, i + 1));
4752 symtable_node(st, CHILD(n, i + 3));
4754 if (i + 2 < NCH(n))
4755 symtable_node(st, CHILD(n, i + 2));
4756 break;
4757 case global_stmt:
4758 symtable_global(st, n);
4759 break;
4760 case import_stmt:
4761 symtable_import(st, n);
4762 break;
4763 case exec_stmt: {
4764 st->st_cur->ste_optimized |= OPT_EXEC;
4765 symtable_node(st, CHILD(n, 1));
4766 if (NCH(n) > 2)
4767 symtable_node(st, CHILD(n, 3));
4768 else
4769 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
4770 if (NCH(n) > 4)
4771 symtable_node(st, CHILD(n, 5));
4772 break;
4775 case assert_stmt:
4776 if (Py_OptimizeFlag)
4777 return;
4778 if (NCH(n) == 2) {
4779 n = CHILD(n, 1);
4780 goto loop;
4781 } else {
4782 symtable_node(st, CHILD(n, 1));
4783 n = CHILD(n, 3);
4784 goto loop;
4786 case except_clause:
4787 if (NCH(n) == 4)
4788 symtable_assign(st, CHILD(n, 3), 0);
4789 if (NCH(n) > 1) {
4790 n = CHILD(n, 1);
4791 goto loop;
4793 break;
4794 case del_stmt:
4795 symtable_assign(st, CHILD(n, 1), 0);
4796 break;
4797 case expr_stmt:
4798 if (NCH(n) == 1)
4799 n = CHILD(n, 0);
4800 else {
4801 if (TYPE(CHILD(n, 1)) == augassign) {
4802 symtable_assign(st, CHILD(n, 0), 0);
4803 symtable_node(st, CHILD(n, 2));
4804 break;
4805 } else {
4806 int i;
4807 for (i = 0; i < NCH(n) - 2; i += 2)
4808 symtable_assign(st, CHILD(n, i), 0);
4809 n = CHILD(n, NCH(n) - 1);
4812 goto loop;
4813 /* watchout for fall-through logic below */
4814 case argument:
4815 if (NCH(n) == 3) {
4816 n = CHILD(n, 2);
4817 goto loop;
4819 case listmaker:
4820 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
4821 symtable_list_comprehension(st, CHILD(n, 1));
4822 n = CHILD(n, 0);
4823 goto loop;
4825 case atom:
4826 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
4827 symtable_add_use(st, STR(CHILD(n, 0)));
4828 break;
4830 case for_stmt:
4831 if (TYPE(n) == for_stmt) {
4832 symtable_assign(st, CHILD(n, 1), 0);
4833 start = 3;
4835 default:
4836 if (NCH(n) == 1) {
4837 n = CHILD(n, 0);
4838 goto loop;
4840 for (i = start; i < NCH(n); ++i)
4841 if (TYPE(CHILD(n, i)) >= single_input)
4842 symtable_node(st, CHILD(n, i));
4846 static void
4847 symtable_funcdef(struct symtable *st, node *n)
4849 node *body;
4851 if (TYPE(n) == lambdef) {
4852 if (NCH(n) == 4)
4853 symtable_params(st, CHILD(n, 1));
4854 } else
4855 symtable_params(st, CHILD(n, 2));
4856 body = CHILD(n, NCH(n) - 1);
4857 symtable_node(st, body);
4860 /* The next two functions parse the argument tuple.
4861 symtable_default_arg() checks for names in the default arguments,
4862 which are references in the defining scope. symtable_params()
4863 parses the parameter names, which are defined in the function's
4864 body.
4866 varargslist:
4867 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
4868 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4871 static void
4872 symtable_default_args(struct symtable *st, node *n)
4874 node *c;
4875 int i;
4877 if (TYPE(n) == parameters) {
4878 n = CHILD(n, 1);
4879 if (TYPE(n) == RPAR)
4880 return;
4882 REQ(n, varargslist);
4883 for (i = 0; i < NCH(n); i += 2) {
4884 c = CHILD(n, i);
4885 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4886 break;
4888 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
4889 symtable_node(st, CHILD(n, i));
4893 static void
4894 symtable_params(struct symtable *st, node *n)
4896 int i, complex = -1, ext = 0;
4897 node *c = NULL;
4899 if (TYPE(n) == parameters) {
4900 n = CHILD(n, 1);
4901 if (TYPE(n) == RPAR)
4902 return;
4904 REQ(n, varargslist);
4905 for (i = 0; i < NCH(n); i += 2) {
4906 c = CHILD(n, i);
4907 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
4908 ext = 1;
4909 break;
4911 if (TYPE(c) == test) {
4912 continue;
4914 if (TYPE(CHILD(c, 0)) == NAME)
4915 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
4916 else {
4917 char nbuf[10];
4918 sprintf(nbuf, ".%d", i);
4919 symtable_add_def(st, nbuf, DEF_PARAM);
4920 complex = i;
4923 if (ext) {
4924 c = CHILD(n, i);
4925 if (TYPE(c) == STAR) {
4926 i++;
4927 symtable_add_def(st, STR(CHILD(n, i)),
4928 DEF_PARAM | DEF_STAR);
4929 i += 2;
4930 if (i >= NCH(n))
4931 c = NULL;
4932 else
4933 c = CHILD(n, i);
4935 if (c && TYPE(c) == DOUBLESTAR) {
4936 i++;
4937 symtable_add_def(st, STR(CHILD(n, i)),
4938 DEF_PARAM | DEF_DOUBLESTAR);
4941 if (complex >= 0) {
4942 int j;
4943 for (j = 0; j <= complex; j++) {
4944 c = CHILD(n, j);
4945 if (TYPE(c) == COMMA)
4946 c = CHILD(n, ++j);
4947 else if (TYPE(c) == EQUAL)
4948 c = CHILD(n, j += 3);
4949 if (TYPE(CHILD(c, 0)) == LPAR)
4950 symtable_params_fplist(st, CHILD(c, 1));
4955 static void
4956 symtable_params_fplist(struct symtable *st, node *n)
4958 int i;
4959 node *c;
4961 REQ(n, fplist);
4962 for (i = 0; i < NCH(n); i += 2) {
4963 c = CHILD(n, i);
4964 REQ(c, fpdef);
4965 if (NCH(c) == 1)
4966 symtable_add_def(st, STR(CHILD(c, 0)),
4967 DEF_PARAM | DEF_INTUPLE);
4968 else
4969 symtable_params_fplist(st, CHILD(c, 1));
4974 static void
4975 symtable_global(struct symtable *st, node *n)
4977 int i;
4979 /* XXX It might be helpful to warn about module-level global
4980 statements, but it's hard to tell the difference between
4981 module-level and a string passed to exec.
4984 for (i = 1; i < NCH(n); i += 2) {
4985 char *name = STR(CHILD(n, i));
4986 int flags;
4988 flags = symtable_lookup(st, name);
4989 if (flags < 0)
4990 continue;
4991 if (flags && flags != DEF_GLOBAL) {
4992 char buf[500];
4993 if (flags & DEF_PARAM) {
4994 PyErr_Format(PyExc_SyntaxError,
4995 "name '%.400s' is local and global",
4996 name);
4997 PyErr_SyntaxLocation(st->st_filename,
4998 st->st_cur->ste_lineno);
4999 st->st_errors++;
5000 return;
5002 else {
5003 if (flags & DEF_LOCAL)
5004 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5005 name);
5006 else
5007 sprintf(buf, GLOBAL_AFTER_USE, name);
5008 symtable_warn(st, buf);
5011 symtable_add_def(st, name, DEF_GLOBAL);
5015 static void
5016 symtable_list_comprehension(struct symtable *st, node *n)
5018 char tmpname[12];
5020 sprintf(tmpname, "_[%d]", ++st->st_tmpname);
5021 symtable_add_def(st, tmpname, DEF_LOCAL);
5022 symtable_assign(st, CHILD(n, 1), 0);
5023 symtable_node(st, CHILD(n, 3));
5024 if (NCH(n) == 5)
5025 symtable_node(st, CHILD(n, 4));
5026 --st->st_tmpname;
5029 static void
5030 symtable_import(struct symtable *st, node *n)
5032 int i;
5033 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5034 | 'from' dotted_name 'import'
5035 ('*' | import_as_name (',' import_as_name)*)
5036 import_as_name: NAME [NAME NAME]
5038 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
5039 node *dotname = CHILD(n, 1);
5040 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5041 /* check for bogus imports */
5042 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5043 PyErr_SetString(PyExc_SyntaxError,
5044 LATE_FUTURE);
5045 PyErr_SyntaxLocation(st->st_filename,
5046 n->n_lineno);
5047 st->st_errors++;
5048 return;
5051 if (TYPE(CHILD(n, 3)) == STAR) {
5052 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
5053 } else {
5054 for (i = 3; i < NCH(n); i += 2) {
5055 node *c = CHILD(n, i);
5056 if (NCH(c) > 1) /* import as */
5057 symtable_assign(st, CHILD(c, 2),
5058 DEF_IMPORT);
5059 else
5060 symtable_assign(st, CHILD(c, 0),
5061 DEF_IMPORT);
5064 } else {
5065 for (i = 1; i < NCH(n); i += 2) {
5066 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
5071 static void
5072 symtable_assign(struct symtable *st, node *n, int flag)
5074 node *tmp;
5075 int i;
5077 loop:
5078 switch (TYPE(n)) {
5079 case lambdef:
5080 /* invalid assignment, e.g. lambda x:x=2. The next
5081 pass will catch this error. */
5082 return;
5083 case power:
5084 if (NCH(n) > 2) {
5085 for (i = 2; i < NCH(n); ++i)
5086 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5087 symtable_node(st, CHILD(n, i));
5089 if (NCH(n) > 1) {
5090 symtable_node(st, CHILD(n, 0));
5091 symtable_node(st, CHILD(n, 1));
5092 } else {
5093 n = CHILD(n, 0);
5094 goto loop;
5096 return;
5097 case listmaker:
5098 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
5099 symtable_list_comprehension(st, CHILD(n, 1));
5100 else {
5101 for (i = 0; i < NCH(n); i += 2)
5102 symtable_assign(st, CHILD(n, i), flag);
5104 return;
5105 case exprlist:
5106 case testlist:
5107 if (NCH(n) == 1) {
5108 n = CHILD(n, 0);
5109 goto loop;
5111 else {
5112 int i;
5113 for (i = 0; i < NCH(n); i += 2)
5114 symtable_assign(st, CHILD(n, i), flag);
5115 return;
5117 goto loop;
5118 case atom:
5119 tmp = CHILD(n, 0);
5120 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5121 n = CHILD(n, 1);
5122 goto loop;
5123 } else if (TYPE(tmp) == NAME)
5124 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
5125 return;
5126 case dotted_as_name:
5127 if (NCH(n) == 3)
5128 symtable_add_def(st, STR(CHILD(n, 2)),
5129 DEF_LOCAL | flag);
5130 else
5131 symtable_add_def(st,
5132 STR(CHILD(CHILD(n,
5133 0), 0)),
5134 DEF_LOCAL | flag);
5135 return;
5136 case dotted_name:
5137 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
5138 return;
5139 case NAME:
5140 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
5141 return;
5142 default:
5143 if (NCH(n) == 0)
5144 return;
5145 if (NCH(n) == 1) {
5146 n = CHILD(n, 0);
5147 goto loop;
5149 /* Should only occur for errors like x + 1 = 1,
5150 which will be caught in the next pass. */
5151 for (i = 0; i < NCH(n); ++i)
5152 if (TYPE(CHILD(n, i)) >= single_input)
5153 symtable_assign(st, CHILD(n, i), flag);