1 /* Compile an expression node to intermediate code */
4 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
6 XXX Generate simple jump for break/return outside 'try...finally'
7 XXX Allow 'continue' inside finally clause of try-finally
8 XXX New opcode for loading the initial index for a for loop
20 #include "structmember.h"
24 /* Three symbols from graminit.h are also defined in Python.h, with
25 Py_ prefixes to their names. Python.h can't include graminit.h
26 (which defines too many confusing symbols), but we can check here
27 that they haven't changed (which is very unlikely, but possible). */
28 #if Py_single_input != single_input
29 #error "single_input has changed -- update Py_single_input in Python.h"
31 #if Py_file_input != file_input
32 #error "file_input has changed -- update Py_file_input in Python.h"
34 #if Py_eval_input != eval_input
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
38 int Py_OptimizeFlag
= 0;
48 #define DEL_CLOSURE_ERROR \
49 "can not delete variable '%.400s' referenced in nested scope"
51 #define DUPLICATE_ARGUMENT \
52 "duplicate argument '%s' in function definition"
54 #define ILLEGAL_DYNAMIC_SCOPE \
55 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
57 #define GLOBAL_AFTER_ASSIGN \
58 "name '%.400s' is assigned to before global declaration"
60 #define GLOBAL_AFTER_USE \
61 "name '%.400s' is used prior to global declaration"
63 #define LOCAL_GLOBAL \
64 "name '%.400s' is a function parameter and declared global"
67 "from __future__ imports must occur at the beginning of the file"
69 #define ASSIGN_DEBUG \
70 "can not assign to __debug__"
72 #define MANGLE_LEN 256
74 #define OFF(x) offsetof(PyCodeObject, x)
76 static PyMemberDef code_memberlist
[] = {
77 {"co_argcount", T_INT
, OFF(co_argcount
), READONLY
},
78 {"co_nlocals", T_INT
, OFF(co_nlocals
), READONLY
},
79 {"co_stacksize",T_INT
, OFF(co_stacksize
), READONLY
},
80 {"co_flags", T_INT
, OFF(co_flags
), READONLY
},
81 {"co_code", T_OBJECT
, OFF(co_code
), READONLY
},
82 {"co_consts", T_OBJECT
, OFF(co_consts
), READONLY
},
83 {"co_names", T_OBJECT
, OFF(co_names
), READONLY
},
84 {"co_varnames", T_OBJECT
, OFF(co_varnames
), READONLY
},
85 {"co_freevars", T_OBJECT
, OFF(co_freevars
), READONLY
},
86 {"co_cellvars", T_OBJECT
, OFF(co_cellvars
), READONLY
},
87 {"co_filename", T_OBJECT
, OFF(co_filename
), READONLY
},
88 {"co_name", T_OBJECT
, OFF(co_name
), READONLY
},
89 {"co_firstlineno", T_INT
, OFF(co_firstlineno
), READONLY
},
90 {"co_lnotab", T_OBJECT
, OFF(co_lnotab
), READONLY
},
94 PyDoc_STRVAR(code_doc
,
95 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
96 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
98 Create a code object. Not for the faint of heart.");
101 code_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
111 PyObject
*freevars
= NULL
;
112 PyObject
*cellvars
= NULL
;
118 if (!PyArg_ParseTuple(args
, "iiiiSO!O!O!SSiS|O!O!:code",
119 &argcount
, &nlocals
, &stacksize
, &flags
,
121 &PyTuple_Type
, &consts
,
122 &PyTuple_Type
, &names
,
123 &PyTuple_Type
, &varnames
,
125 &firstlineno
, &lnotab
,
126 &PyTuple_Type
, &freevars
,
127 &PyTuple_Type
, &cellvars
))
130 if (freevars
== NULL
|| cellvars
== NULL
) {
131 PyObject
*empty
= PyTuple_New(0);
134 if (freevars
== NULL
) {
138 if (cellvars
== NULL
) {
145 if (!PyObject_CheckReadBuffer(code
)) {
146 PyErr_SetString(PyExc_TypeError
,
147 "bytecode object must be a single-segment read-only buffer");
151 return (PyObject
*)PyCode_New(argcount
, nlocals
, stacksize
, flags
,
152 code
, consts
, names
, varnames
,
153 freevars
, cellvars
, filename
, name
,
154 firstlineno
, lnotab
);
158 code_dealloc(PyCodeObject
*co
)
160 Py_XDECREF(co
->co_code
);
161 Py_XDECREF(co
->co_consts
);
162 Py_XDECREF(co
->co_names
);
163 Py_XDECREF(co
->co_varnames
);
164 Py_XDECREF(co
->co_freevars
);
165 Py_XDECREF(co
->co_cellvars
);
166 Py_XDECREF(co
->co_filename
);
167 Py_XDECREF(co
->co_name
);
168 Py_XDECREF(co
->co_lnotab
);
173 code_repr(PyCodeObject
*co
)
177 char *filename
= "???";
180 if (co
->co_firstlineno
!= 0)
181 lineno
= co
->co_firstlineno
;
182 if (co
->co_filename
&& PyString_Check(co
->co_filename
))
183 filename
= PyString_AS_STRING(co
->co_filename
);
184 if (co
->co_name
&& PyString_Check(co
->co_name
))
185 name
= PyString_AS_STRING(co
->co_name
);
186 PyOS_snprintf(buf
, sizeof(buf
),
187 "<code object %.100s at %p, file \"%.300s\", line %d>",
188 name
, co
, filename
, lineno
);
189 return PyString_FromString(buf
);
193 code_compare(PyCodeObject
*co
, PyCodeObject
*cp
)
196 cmp
= PyObject_Compare(co
->co_name
, cp
->co_name
);
198 cmp
= co
->co_argcount
- cp
->co_argcount
;
200 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
202 cmp
= co
->co_flags
- cp
->co_flags
;
204 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
206 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
208 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
210 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
212 cmp
= PyObject_Compare(co
->co_freevars
, cp
->co_freevars
);
214 cmp
= PyObject_Compare(co
->co_cellvars
, cp
->co_cellvars
);
219 code_hash(PyCodeObject
*co
)
221 long h
, h0
, h1
, h2
, h3
, h4
, h5
, h6
;
222 h0
= PyObject_Hash(co
->co_name
);
223 if (h0
== -1) return -1;
224 h1
= PyObject_Hash(co
->co_code
);
225 if (h1
== -1) return -1;
226 h2
= PyObject_Hash(co
->co_consts
);
227 if (h2
== -1) return -1;
228 h3
= PyObject_Hash(co
->co_names
);
229 if (h3
== -1) return -1;
230 h4
= PyObject_Hash(co
->co_varnames
);
231 if (h4
== -1) return -1;
232 h5
= PyObject_Hash(co
->co_freevars
);
233 if (h5
== -1) return -1;
234 h6
= PyObject_Hash(co
->co_cellvars
);
235 if (h6
== -1) return -1;
236 h
= h0
^ h1
^ h2
^ h3
^ h4
^ h5
^ h6
^
237 co
->co_argcount
^ co
->co_nlocals
^ co
->co_flags
;
242 /* XXX code objects need to participate in GC? */
244 PyTypeObject PyCode_Type
= {
245 PyObject_HEAD_INIT(&PyType_Type
)
248 sizeof(PyCodeObject
),
250 (destructor
)code_dealloc
, /* tp_dealloc */
254 (cmpfunc
)code_compare
, /* tp_compare */
255 (reprfunc
)code_repr
, /* tp_repr */
256 0, /* tp_as_number */
257 0, /* tp_as_sequence */
258 0, /* tp_as_mapping */
259 (hashfunc
)code_hash
, /* tp_hash */
262 PyObject_GenericGetAttr
, /* tp_getattro */
264 0, /* tp_as_buffer */
265 Py_TPFLAGS_DEFAULT
, /* tp_flags */
266 code_doc
, /* tp_doc */
269 0, /* tp_richcompare */
270 0, /* tp_weaklistoffset */
274 code_memberlist
, /* tp_members */
278 0, /* tp_descr_get */
279 0, /* tp_descr_set */
280 0, /* tp_dictoffset */
283 code_new
, /* tp_new */
287 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
289 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
292 all_name_chars(unsigned char *s
)
294 static char ok_name_char
[256];
295 static unsigned char *name_chars
= (unsigned char *)NAME_CHARS
;
297 if (ok_name_char
[*name_chars
] == 0) {
299 for (p
= name_chars
; *p
; p
++)
300 ok_name_char
[*p
] = 1;
303 if (ok_name_char
[*s
++] == 0)
310 intern_strings(PyObject
*tuple
)
314 for (i
= PyTuple_GET_SIZE(tuple
); --i
>= 0; ) {
315 PyObject
*v
= PyTuple_GET_ITEM(tuple
, i
);
316 if (v
== NULL
|| !PyString_Check(v
)) {
317 Py_FatalError("non-string found in code slot");
318 PyErr_BadInternalCall();
321 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple
, i
));
327 PyCode_New(int argcount
, int nlocals
, int stacksize
, int flags
,
328 PyObject
*code
, PyObject
*consts
, PyObject
*names
,
329 PyObject
*varnames
, PyObject
*freevars
, PyObject
*cellvars
,
330 PyObject
*filename
, PyObject
*name
, int firstlineno
,
335 /* Check argument types */
336 if (argcount
< 0 || nlocals
< 0 ||
338 consts
== NULL
|| !PyTuple_Check(consts
) ||
339 names
== NULL
|| !PyTuple_Check(names
) ||
340 varnames
== NULL
|| !PyTuple_Check(varnames
) ||
341 freevars
== NULL
|| !PyTuple_Check(freevars
) ||
342 cellvars
== NULL
|| !PyTuple_Check(cellvars
) ||
343 name
== NULL
|| !PyString_Check(name
) ||
344 filename
== NULL
|| !PyString_Check(filename
) ||
345 lnotab
== NULL
|| !PyString_Check(lnotab
) ||
346 !PyObject_CheckReadBuffer(code
)) {
347 PyErr_BadInternalCall();
350 intern_strings(names
);
351 intern_strings(varnames
);
352 intern_strings(freevars
);
353 intern_strings(cellvars
);
354 /* Intern selected string constants */
355 for (i
= PyTuple_Size(consts
); --i
>= 0; ) {
356 PyObject
*v
= PyTuple_GetItem(consts
, i
);
357 if (!PyString_Check(v
))
359 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v
)))
361 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
363 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
365 co
->co_argcount
= argcount
;
366 co
->co_nlocals
= nlocals
;
367 co
->co_stacksize
= stacksize
;
368 co
->co_flags
= flags
;
372 co
->co_consts
= consts
;
374 co
->co_names
= names
;
376 co
->co_varnames
= varnames
;
378 co
->co_freevars
= freevars
;
380 co
->co_cellvars
= cellvars
;
382 co
->co_filename
= filename
;
385 co
->co_firstlineno
= firstlineno
;
387 co
->co_lnotab
= lnotab
;
393 /* Data structure used internally */
395 /* The compiler uses two passes to generate bytecodes. The first pass
396 builds the symbol table. The second pass generates the bytecode.
398 The first pass uses a single symtable struct. The second pass uses
399 a compiling struct for each code block. The compiling structs
400 share a reference to the symtable.
402 The two passes communicate via symtable_load_symbols() and via
403 is_local() and is_global(). The former initializes several slots
404 in the compiling struct: c_varnames, c_locals, c_nlocals,
405 c_argcount, c_globals, and c_flags.
408 /* All about c_lnotab.
410 c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
411 mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
412 to source code line #s (when needed for tracebacks) via c_lnotab instead.
413 The array is conceptually a list of
414 (bytecode offset increment, line number increment)
415 pairs. The details are important and delicate, best illustrated by example:
417 byte code offset source code line number
424 The first trick is that these numbers aren't stored, only the increments
425 from one row to the next (this doesn't really work, but it's a start):
427 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
429 The second trick is that an unsigned byte can't hold negative values, or
430 values larger than 255, so (a) there's a deep assumption that byte code
431 offsets and their corresponding line #s both increase monotonically, and (b)
432 if at least one column jumps by more than 255 from one row to the next, more
433 than one pair is written to the table. In case #b, there's no way to know
434 from looking at the table later how many were written. That's the delicate
435 part. A user of c_lnotab desiring to find the source line number
436 corresponding to a bytecode address A should do something like this
439 for addr_incr, line_incr in c_lnotab:
445 In order for this to work, when the addr field increments by more than 255,
446 the line # increment in each pair generated must be 0 until the remaining addr
447 increment is < 256. So, in the example above, com_set_lineno should not (as
448 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
449 255, 0, 45, 255, 0, 45.
453 PyObject
*c_code
; /* string */
454 PyObject
*c_consts
; /* list of objects */
455 PyObject
*c_const_dict
; /* inverse of c_consts */
456 PyObject
*c_names
; /* list of strings (names) */
457 PyObject
*c_name_dict
; /* inverse of c_names */
458 PyObject
*c_globals
; /* dictionary (value=None) */
459 PyObject
*c_locals
; /* dictionary (value=localID) */
460 PyObject
*c_varnames
; /* list (inverse of c_locals) */
461 PyObject
*c_freevars
; /* dictionary (value=None) */
462 PyObject
*c_cellvars
; /* list */
463 int c_nlocals
; /* index of next local */
464 int c_argcount
; /* number of top-level arguments */
465 int c_flags
; /* same as co_flags */
466 int c_nexti
; /* index into c_code */
467 int c_errors
; /* counts errors occurred */
468 int c_infunction
; /* set when compiling a function */
469 int c_interactive
; /* generating code for interactive command */
470 int c_loops
; /* counts nested loops */
471 int c_begin
; /* begin of current loop, for 'continue' */
472 int c_block
[CO_MAXBLOCKS
]; /* stack of block types */
473 int c_nblocks
; /* current block stack level */
474 char *c_filename
; /* filename of current node */
475 char *c_name
; /* name of object (e.g. function) */
476 int c_lineno
; /* Current line number */
477 int c_stacklevel
; /* Current stack level */
478 int c_maxstacklevel
; /* Maximum stack level */
480 PyObject
*c_lnotab
; /* Table mapping address to line number */
481 int c_last_addr
, c_last_line
, c_lnotab_next
;
482 char *c_private
; /* for private name mangling */
483 int c_tmpname
; /* temporary local name counter */
484 int c_nested
; /* Is block nested funcdef or lamdef? */
485 int c_closure
; /* Is nested w/freevars? */
486 struct symtable
*c_symtable
; /* pointer to module symbol table */
487 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
493 if ((v
& (USE
| DEF_FREE
))
494 && !(v
& (DEF_LOCAL
| DEF_PARAM
| DEF_GLOBAL
)))
496 if (v
& DEF_FREE_CLASS
)
502 com_error(struct compiling
*c
, PyObject
*exc
, char *msg
)
504 PyObject
*t
= NULL
, *v
= NULL
, *w
= NULL
, *line
= NULL
;
507 /* Error occurred via symtable call to
509 PyErr_SetString(exc
, msg
);
513 if (c
->c_lineno
< 1 || c
->c_interactive
) {
514 /* Unknown line number or interactive input */
515 PyErr_SetString(exc
, msg
);
518 v
= PyString_FromString(msg
);
520 return; /* MemoryError, too bad */
522 line
= PyErr_ProgramText(c
->c_filename
, c
->c_lineno
);
527 if (exc
== PyExc_SyntaxError
) {
528 t
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->c_lineno
,
532 w
= Py_BuildValue("(OO)", v
, t
);
535 PyErr_SetObject(exc
, w
);
537 /* Make sure additional exceptions are printed with
538 file and line, also. */
539 PyErr_SetObject(exc
, v
);
540 PyErr_SyntaxLocation(c
->c_filename
, c
->c_lineno
);
549 /* Interface to the block stack */
552 block_push(struct compiling
*c
, int type
)
554 if (c
->c_nblocks
>= CO_MAXBLOCKS
) {
555 com_error(c
, PyExc_SystemError
,
556 "too many statically nested blocks");
559 c
->c_block
[c
->c_nblocks
++] = type
;
564 block_pop(struct compiling
*c
, int type
)
566 if (c
->c_nblocks
> 0)
568 if (c
->c_block
[c
->c_nblocks
] != type
&& c
->c_errors
== 0) {
569 com_error(c
, PyExc_SystemError
, "bad block pop");
573 /* Prototype forward declarations */
575 static int com_init(struct compiling
*, char *);
576 static void com_free(struct compiling
*);
577 static void com_push(struct compiling
*, int);
578 static void com_pop(struct compiling
*, int);
579 static void com_done(struct compiling
*);
580 static void com_node(struct compiling
*, node
*);
581 static void com_factor(struct compiling
*, node
*);
582 static void com_addbyte(struct compiling
*, int);
583 static void com_addint(struct compiling
*, int);
584 static void com_addoparg(struct compiling
*, int, int);
585 static void com_addfwref(struct compiling
*, int, int *);
586 static void com_backpatch(struct compiling
*, int);
587 static int com_add(struct compiling
*, PyObject
*, PyObject
*, PyObject
*);
588 static int com_addconst(struct compiling
*, PyObject
*);
589 static int com_addname(struct compiling
*, PyObject
*);
590 static void com_addopname(struct compiling
*, int, node
*);
591 static void com_list(struct compiling
*, node
*, int);
592 static void com_list_iter(struct compiling
*, node
*, node
*, char *);
593 static int com_argdefs(struct compiling
*, node
*);
594 static void com_assign(struct compiling
*, node
*, int, node
*);
595 static void com_assign_name(struct compiling
*, node
*, int);
596 static PyCodeObject
*icompile(node
*, struct compiling
*);
597 static PyCodeObject
*jcompile(node
*, char *, struct compiling
*,
599 static PyObject
*parsestrplus(struct compiling
*, node
*);
600 static PyObject
*parsestr(struct compiling
*, char *);
601 static node
*get_rawdocstring(node
*);
603 static int get_ref_type(struct compiling
*, char *);
605 /* symtable operations */
606 static int symtable_build(struct compiling
*, node
*);
607 static int symtable_load_symbols(struct compiling
*);
608 static struct symtable
*symtable_init(void);
609 static void symtable_enter_scope(struct symtable
*, char *, int, int);
610 static int symtable_exit_scope(struct symtable
*);
611 static int symtable_add_def(struct symtable
*, char *, int);
612 static int symtable_add_def_o(struct symtable
*, PyObject
*, PyObject
*, int);
614 static void symtable_node(struct symtable
*, node
*);
615 static void symtable_funcdef(struct symtable
*, node
*);
616 static void symtable_default_args(struct symtable
*, node
*);
617 static void symtable_params(struct symtable
*, node
*);
618 static void symtable_params_fplist(struct symtable
*, node
*n
);
619 static void symtable_global(struct symtable
*, node
*);
620 static void symtable_import(struct symtable
*, node
*);
621 static void symtable_assign(struct symtable
*, node
*, int);
622 static void symtable_list_comprehension(struct symtable
*, node
*);
624 static int symtable_update_free_vars(struct symtable
*);
625 static int symtable_undo_free(struct symtable
*, PyObject
*, PyObject
*);
626 static int symtable_check_global(struct symtable
*, PyObject
*, PyObject
*);
633 for (i
= 0; i
< pad
; ++i
)
634 fprintf(stderr
, " ");
638 dump(node
*n
, int pad
, int depth
)
644 fprintf(stderr
, "%d: %s\n", TYPE(n
), STR(n
));
647 for (i
= 0; i
< NCH(n
); ++i
)
648 dump(CHILD(n
, i
), pad
+ 1, depth
);
651 #define DUMP(N) dump(N, 0, -1)
654 com_init(struct compiling
*c
, char *filename
)
656 memset((void *)c
, '\0', sizeof(struct compiling
));
657 if ((c
->c_code
= PyString_FromStringAndSize((char *)NULL
,
660 if ((c
->c_consts
= PyList_New(0)) == NULL
)
662 if ((c
->c_const_dict
= PyDict_New()) == NULL
)
664 if ((c
->c_names
= PyList_New(0)) == NULL
)
666 if ((c
->c_name_dict
= PyDict_New()) == NULL
)
668 if ((c
->c_locals
= PyDict_New()) == NULL
)
670 if ((c
->c_lnotab
= PyString_FromStringAndSize((char *)NULL
,
674 c
->c_varnames
= NULL
;
675 c
->c_freevars
= NULL
;
676 c
->c_cellvars
= NULL
;
683 c
->c_interactive
= 0;
687 c
->c_filename
= filename
;
691 c
->c_maxstacklevel
= 0;
692 c
->c_firstlineno
= 0;
695 c
->c_lnotab_next
= 0;
699 c
->c_symtable
= NULL
;
708 com_free(struct compiling
*c
)
710 Py_XDECREF(c
->c_code
);
711 Py_XDECREF(c
->c_consts
);
712 Py_XDECREF(c
->c_const_dict
);
713 Py_XDECREF(c
->c_names
);
714 Py_XDECREF(c
->c_name_dict
);
715 Py_XDECREF(c
->c_globals
);
716 Py_XDECREF(c
->c_locals
);
717 Py_XDECREF(c
->c_varnames
);
718 Py_XDECREF(c
->c_freevars
);
719 Py_XDECREF(c
->c_cellvars
);
720 Py_XDECREF(c
->c_lnotab
);
722 PyMem_Free((void *)c
->c_future
);
726 com_push(struct compiling
*c
, int n
)
728 c
->c_stacklevel
+= n
;
729 if (c
->c_stacklevel
> c
->c_maxstacklevel
) {
730 c
->c_maxstacklevel
= c
->c_stacklevel
;
732 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
733 c->c_filename, c->c_name, c->c_lineno,
734 c->c_nexti, c->c_stacklevel, n);
740 com_pop(struct compiling
*c
, int n
)
742 if (c
->c_stacklevel
< n
)
745 c
->c_stacklevel
-= n
;
749 com_done(struct compiling
*c
)
751 if (c
->c_code
!= NULL
)
752 _PyString_Resize(&c
->c_code
, c
->c_nexti
);
753 if (c
->c_lnotab
!= NULL
)
754 _PyString_Resize(&c
->c_lnotab
, c
->c_lnotab_next
);
758 com_check_size(PyObject
**s
, int offset
)
760 int len
= PyString_GET_SIZE(*s
);
762 return _PyString_Resize(s
, len
* 2);
767 com_addbyte(struct compiling
*c
, int byte
)
769 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
770 assert(byte
>= 0 && byte
<= 255);
771 assert(c
->c_code
!= 0);
772 if (com_check_size(&c
->c_code
, c
->c_nexti
)) {
776 PyString_AS_STRING(c
->c_code
)[c
->c_nexti
++] = byte
;
780 com_addint(struct compiling
*c
, int x
)
782 com_addbyte(c
, x
& 0xff);
783 com_addbyte(c
, x
>> 8); /* XXX x should be positive */
787 com_add_lnotab(struct compiling
*c
, int addr
, int line
)
790 if (c
->c_lnotab
== NULL
)
792 if (com_check_size(&c
->c_lnotab
, c
->c_lnotab_next
+ 2)) {
796 p
= PyString_AS_STRING(c
->c_lnotab
) + c
->c_lnotab_next
;
799 c
->c_lnotab_next
+= 2;
803 com_set_lineno(struct compiling
*c
, int lineno
)
805 c
->c_lineno
= lineno
;
806 if (c
->c_firstlineno
== 0) {
807 c
->c_firstlineno
= c
->c_last_line
= lineno
;
810 int incr_addr
= c
->c_nexti
- c
->c_last_addr
;
811 int incr_line
= lineno
- c
->c_last_line
;
812 while (incr_addr
> 255) {
813 com_add_lnotab(c
, 255, 0);
816 while (incr_line
> 255) {
817 com_add_lnotab(c
, incr_addr
, 255);
821 if (incr_addr
> 0 || incr_line
> 0)
822 com_add_lnotab(c
, incr_addr
, incr_line
);
823 c
->c_last_addr
= c
->c_nexti
;
824 c
->c_last_line
= lineno
;
829 com_addoparg(struct compiling
*c
, int op
, int arg
)
831 int extended_arg
= arg
>> 16;
832 if (op
== SET_LINENO
) {
833 com_set_lineno(c
, arg
);
838 com_addbyte(c
, EXTENDED_ARG
);
839 com_addint(c
, extended_arg
);
847 com_addfwref(struct compiling
*c
, int op
, int *p_anchor
)
849 /* Compile a forward reference for backpatching */
856 com_addint(c
, anchor
== 0 ? 0 : here
- anchor
);
860 com_backpatch(struct compiling
*c
, int anchor
)
862 unsigned char *code
= (unsigned char *) PyString_AS_STRING(c
->c_code
);
863 int target
= c
->c_nexti
;
867 /* Make the JUMP instruction at anchor point to target */
868 prev
= code
[anchor
] + (code
[anchor
+1] << 8);
869 dist
= target
- (anchor
+2);
870 code
[anchor
] = dist
& 0xff;
872 code
[anchor
+1] = dist
;
875 com_error(c
, PyExc_SystemError
,
876 "com_backpatch: offset too large");
885 /* Handle literals and names uniformly */
888 com_add(struct compiling
*c
, PyObject
*list
, PyObject
*dict
, PyObject
*v
)
890 PyObject
*w
, *t
, *np
=NULL
;
893 t
= Py_BuildValue("(OO)", v
, v
->ob_type
);
896 w
= PyDict_GetItem(dict
, t
);
900 n
= PyList_Size(list
);
901 np
= PyInt_FromLong(n
);
904 if (PyList_Append(list
, v
) != 0)
906 if (PyDict_SetItem(dict
, t
, np
) != 0)
920 com_addconst(struct compiling
*c
, PyObject
*v
)
922 return com_add(c
, c
->c_consts
, c
->c_const_dict
, v
);
926 com_addname(struct compiling
*c
, PyObject
*v
)
928 return com_add(c
, c
->c_names
, c
->c_name_dict
, v
);
932 _Py_Mangle(char *p
, char *name
, char *buffer
, size_t maxlen
)
934 /* Name mangling: __private becomes _classname__private.
935 This is independent from how the name is used. */
937 if (p
== NULL
|| name
== NULL
|| name
[0] != '_' || name
[1] != '_')
940 if (nlen
+2 >= maxlen
)
941 return 0; /* Don't mangle __extremely_long_names */
942 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
943 return 0; /* Don't mangle __whatever__ */
944 /* Strip leading underscores from class name */
948 return 0; /* Don't mangle if class is just underscores */
950 if (plen
+ nlen
>= maxlen
)
951 plen
= maxlen
-nlen
-2; /* Truncate class name if too long */
952 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
954 strncpy(buffer
+1, p
, plen
);
955 strcpy(buffer
+1+plen
, name
);
960 com_addop_name(struct compiling
*c
, int op
, char *name
)
964 char buffer
[MANGLE_LEN
];
966 if (_Py_Mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
968 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
973 i
= com_addname(c
, v
);
976 com_addoparg(c
, op
, i
);
980 #define NAME_GLOBAL 1
981 #define NAME_DEFAULT 2
982 #define NAME_CLOSURE 3
985 com_lookup_arg(PyObject
*dict
, PyObject
*name
)
987 PyObject
*v
= PyDict_GetItem(dict
, name
);
991 return PyInt_AS_LONG(v
);
995 com_addop_varname(struct compiling
*c
, int kind
, char *name
)
999 int scope
= NAME_DEFAULT
;
1001 char buffer
[MANGLE_LEN
];
1003 if (_Py_Mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
1005 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
1011 reftype
= get_ref_type(c
, name
);
1014 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
)
1017 case GLOBAL_EXPLICIT
:
1018 scope
= NAME_GLOBAL
;
1020 case GLOBAL_IMPLICIT
:
1021 if (c
->c_flags
& CO_OPTIMIZED
)
1022 scope
= NAME_GLOBAL
;
1026 scope
= NAME_CLOSURE
;
1030 i
= com_addname(c
, v
);
1031 if (scope
== NAME_LOCAL
)
1032 i
= com_lookup_arg(c
->c_locals
, v
);
1033 else if (reftype
== FREE
)
1034 i
= com_lookup_arg(c
->c_freevars
, v
);
1035 else if (reftype
== CELL
)
1036 i
= com_lookup_arg(c
->c_cellvars
, v
);
1038 c
->c_errors
++; /* XXX no exception set */
1088 case NAME_CLOSURE
: {
1090 PyOS_snprintf(buf
, sizeof(buf
),
1091 DEL_CLOSURE_ERROR
, name
);
1092 com_error(c
, PyExc_SyntaxError
, buf
);
1100 com_addoparg(c
, op
, i
);
1104 com_addopname(struct compiling
*c
, int op
, node
*n
)
1108 /* XXX it is possible to write this code without the 1000
1109 chars on the total length of dotted names, I just can't be
1110 bothered right now */
1111 if (TYPE(n
) == STAR
)
1113 else if (TYPE(n
) == dotted_name
) {
1117 for (i
= 0; i
< NCH(n
); i
+= 2) {
1118 char *s
= STR(CHILD(n
, i
));
1119 if (p
+ strlen(s
) > buffer
+ (sizeof buffer
) - 2) {
1120 com_error(c
, PyExc_MemoryError
,
1121 "dotted_name too long");
1128 p
= strchr(p
, '\0');
1135 com_addop_name(c
, op
, name
);
1139 parsenumber(struct compiling
*co
, char *s
)
1144 #ifndef WITHOUT_COMPLEX
1150 end
= s
+ strlen(s
) - 1;
1151 #ifndef WITHOUT_COMPLEX
1152 imflag
= *end
== 'j' || *end
== 'J';
1154 if (*end
== 'l' || *end
== 'L')
1155 return PyLong_FromString(s
, (char **)0, 0);
1157 x
= (long) PyOS_strtoul(s
, &end
, 0);
1159 x
= PyOS_strtol(s
, &end
, 0);
1162 return PyLong_FromString(s
, (char **)0, 0);
1163 return PyInt_FromLong(x
);
1165 /* XXX Huge floats may silently fail */
1166 #ifndef WITHOUT_COMPLEX
1169 PyFPE_START_PROTECT("atof", return 0)
1171 PyFPE_END_PROTECT(c
)
1172 return PyComplex_FromCComplex(c
);
1177 PyFPE_START_PROTECT("atof", return 0)
1179 PyFPE_END_PROTECT(dx
)
1180 return PyFloat_FromDouble(dx
);
1185 parsestr(struct compiling
*com
, char *s
)
1198 if (isalpha(quote
) || quote
== '_') {
1199 if (quote
== 'u' || quote
== 'U') {
1203 if (quote
== 'r' || quote
== 'R') {
1208 if (quote
!= '\'' && quote
!= '\"') {
1209 PyErr_BadInternalCall();
1214 if (len
> INT_MAX
) {
1215 com_error(com
, PyExc_OverflowError
,
1216 "string to parse is too long");
1219 if (s
[--len
] != quote
) {
1220 PyErr_BadInternalCall();
1223 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
1226 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
1227 PyErr_BadInternalCall();
1231 #ifdef Py_USING_UNICODE
1232 if (unicode
|| Py_UnicodeFlag
) {
1234 v
= PyUnicode_DecodeRawUnicodeEscape(
1237 v
= PyUnicode_DecodeUnicodeEscape(
1240 PyErr_SyntaxLocation(com
->c_filename
, com
->c_lineno
);
1245 if (rawmode
|| strchr(s
, '\\') == NULL
)
1246 return PyString_FromStringAndSize(s
, len
);
1247 v
= PyString_FromStringAndSize((char *)NULL
, len
);
1250 p
= buf
= PyString_AsString(v
);
1259 /* XXX This assumes ASCII! */
1261 case '\\': *p
++ = '\\'; break;
1262 case '\'': *p
++ = '\''; break;
1263 case '\"': *p
++ = '\"'; break;
1264 case 'b': *p
++ = '\b'; break;
1265 case 'f': *p
++ = '\014'; break; /* FF */
1266 case 't': *p
++ = '\t'; break;
1267 case 'n': *p
++ = '\n'; break;
1268 case 'r': *p
++ = '\r'; break;
1269 case 'v': *p
++ = '\013'; break; /* VT */
1270 case 'a': *p
++ = '\007'; break; /* BEL, not classic C */
1271 case '0': case '1': case '2': case '3':
1272 case '4': case '5': case '6': case '7':
1274 if ('0' <= *s
&& *s
<= '7') {
1275 c
= (c
<<3) + *s
++ - '0';
1276 if ('0' <= *s
&& *s
<= '7')
1277 c
= (c
<<3) + *s
++ - '0';
1282 if (isxdigit(Py_CHARMASK(s
[0]))
1283 && isxdigit(Py_CHARMASK(s
[1]))) {
1285 c
= Py_CHARMASK(*s
);
1289 else if (islower(c
))
1294 c
= Py_CHARMASK(*s
);
1298 else if (islower(c
))
1306 com_error(com
, PyExc_ValueError
,
1307 "invalid \\x escape");
1309 #ifndef Py_USING_UNICODE
1315 com_error(com
, PyExc_ValueError
,
1316 "Unicode escapes not legal "
1317 "when Unicode disabled");
1327 _PyString_Resize(&v
, (int)(p
- buf
));
1332 parsestrplus(struct compiling
* c
, node
*n
)
1336 REQ(CHILD(n
, 0), STRING
);
1337 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
1338 /* String literal concatenation */
1339 for (i
= 1; i
< NCH(n
); i
++) {
1341 s
= parsestr(c
, STR(CHILD(n
, i
)));
1344 if (PyString_Check(v
) && PyString_Check(s
)) {
1345 PyString_ConcatAndDel(&v
, s
);
1349 #ifdef Py_USING_UNICODE
1352 temp
= PyUnicode_Concat(v
, s
);
1370 com_list_for(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1373 int save_begin
= c
->c_begin
;
1375 /* list_iter: for v in expr [list_iter] */
1376 com_node(c
, CHILD(n
, 3)); /* expr */
1377 com_addbyte(c
, GET_ITER
);
1378 c
->c_begin
= c
->c_nexti
;
1379 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1380 com_addfwref(c
, FOR_ITER
, &anchor
);
1382 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
1384 com_list_iter(c
, n
, e
, t
);
1386 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
1387 c
->c_begin
= save_begin
;
1388 com_backpatch(c
, anchor
);
1389 com_pop(c
, 1); /* FOR_ITER has popped this */
1393 com_list_if(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1397 /* list_iter: 'if' test [list_iter] */
1398 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1399 com_node(c
, CHILD(n
, 1));
1400 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
1401 com_addbyte(c
, POP_TOP
);
1403 com_list_iter(c
, n
, e
, t
);
1404 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
1405 com_backpatch(c
, a
);
1406 /* We jump here with an extra entry which we now pop */
1407 com_addbyte(c
, POP_TOP
);
1408 com_backpatch(c
, anchor
);
1412 com_list_iter(struct compiling
*c
,
1413 node
*p
, /* parent of list_iter node */
1414 node
*e
, /* element expression node */
1415 char *t
/* name of result list temp local */)
1417 /* list_iter is the last child in a listmaker, list_for, or list_if */
1418 node
*n
= CHILD(p
, NCH(p
)-1);
1419 if (TYPE(n
) == list_iter
) {
1423 com_list_for(c
, n
, e
, t
);
1426 com_list_if(c
, n
, e
, t
);
1429 com_error(c
, PyExc_SystemError
,
1430 "invalid list_iter node type");
1434 com_addop_varname(c
, VAR_LOAD
, t
);
1437 com_addoparg(c
, CALL_FUNCTION
, 1);
1438 com_addbyte(c
, POP_TOP
);
1444 com_list_comprehension(struct compiling
*c
, node
*n
)
1446 /* listmaker: test list_for */
1448 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->c_tmpname
);
1449 com_addoparg(c
, BUILD_LIST
, 0);
1450 com_addbyte(c
, DUP_TOP
); /* leave the result on the stack */
1452 com_addop_name(c
, LOAD_ATTR
, "append");
1453 com_addop_varname(c
, VAR_STORE
, tmpname
);
1455 com_list_for(c
, CHILD(n
, 1), CHILD(n
, 0), tmpname
);
1456 com_addop_varname(c
, VAR_DELETE
, tmpname
);
1461 com_listmaker(struct compiling
*c
, node
*n
)
1463 /* listmaker: test ( list_for | (',' test)* [','] ) */
1464 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
)
1465 com_list_comprehension(c
, n
);
1469 for (i
= 0; i
< NCH(n
); i
+= 2, len
++)
1470 com_node(c
, CHILD(n
, i
));
1471 com_addoparg(c
, BUILD_LIST
, len
);
1477 com_dictmaker(struct compiling
*c
, node
*n
)
1480 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1481 for (i
= 0; i
+2 < NCH(n
); i
+= 4) {
1482 /* We must arrange things just right for STORE_SUBSCR.
1483 It wants the stack to look like (value) (dict) (key) */
1484 com_addbyte(c
, DUP_TOP
);
1486 com_node(c
, CHILD(n
, i
+2)); /* value */
1487 com_addbyte(c
, ROT_TWO
);
1488 com_node(c
, CHILD(n
, i
)); /* key */
1489 com_addbyte(c
, STORE_SUBSCR
);
1495 com_atom(struct compiling
*c
, node
*n
)
1504 if (TYPE(CHILD(n
, 1)) == RPAR
) {
1505 com_addoparg(c
, BUILD_TUPLE
, 0);
1509 com_node(c
, CHILD(n
, 1));
1511 case LSQB
: /* '[' [listmaker] ']' */
1512 if (TYPE(CHILD(n
, 1)) == RSQB
) {
1513 com_addoparg(c
, BUILD_LIST
, 0);
1517 com_listmaker(c
, CHILD(n
, 1));
1519 case LBRACE
: /* '{' [dictmaker] '}' */
1520 com_addoparg(c
, BUILD_MAP
, 0);
1522 if (TYPE(CHILD(n
, 1)) == dictmaker
)
1523 com_dictmaker(c
, CHILD(n
, 1));
1526 com_node(c
, CHILD(n
, 1));
1527 com_addbyte(c
, UNARY_CONVERT
);
1530 if ((v
= parsenumber(c
, STR(ch
))) == NULL
) {
1534 i
= com_addconst(c
, v
);
1537 com_addoparg(c
, LOAD_CONST
, i
);
1541 v
= parsestrplus(c
, n
);
1547 i
= com_addconst(c
, v
);
1550 com_addoparg(c
, LOAD_CONST
, i
);
1554 com_addop_varname(c
, VAR_LOAD
, STR(ch
));
1558 com_error(c
, PyExc_SystemError
,
1559 "com_atom: unexpected node type");
1564 com_slice(struct compiling
*c
, node
*n
, int op
)
1569 else if (NCH(n
) == 2) {
1570 if (TYPE(CHILD(n
, 0)) != COLON
) {
1571 com_node(c
, CHILD(n
, 0));
1572 com_addbyte(c
, op
+1);
1575 com_node(c
, CHILD(n
, 1));
1576 com_addbyte(c
, op
+2);
1581 com_node(c
, CHILD(n
, 0));
1582 com_node(c
, CHILD(n
, 2));
1583 com_addbyte(c
, op
+3);
1589 com_augassign_slice(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
1592 com_addbyte(c
, DUP_TOP
);
1594 com_addbyte(c
, SLICE
);
1596 com_addbyte(c
, opcode
);
1598 com_addbyte(c
, ROT_TWO
);
1599 com_addbyte(c
, STORE_SLICE
);
1601 } else if (NCH(n
) == 2 && TYPE(CHILD(n
, 0)) != COLON
) {
1602 com_node(c
, CHILD(n
, 0));
1603 com_addoparg(c
, DUP_TOPX
, 2);
1605 com_addbyte(c
, SLICE
+1);
1608 com_addbyte(c
, opcode
);
1610 com_addbyte(c
, ROT_THREE
);
1611 com_addbyte(c
, STORE_SLICE
+1);
1613 } else if (NCH(n
) == 2) {
1614 com_node(c
, CHILD(n
, 1));
1615 com_addoparg(c
, DUP_TOPX
, 2);
1617 com_addbyte(c
, SLICE
+2);
1620 com_addbyte(c
, opcode
);
1622 com_addbyte(c
, ROT_THREE
);
1623 com_addbyte(c
, STORE_SLICE
+2);
1626 com_node(c
, CHILD(n
, 0));
1627 com_node(c
, CHILD(n
, 2));
1628 com_addoparg(c
, DUP_TOPX
, 3);
1630 com_addbyte(c
, SLICE
+3);
1633 com_addbyte(c
, opcode
);
1635 com_addbyte(c
, ROT_FOUR
);
1636 com_addbyte(c
, STORE_SLICE
+3);
1642 com_argument(struct compiling
*c
, node
*n
, PyObject
**pkeywords
)
1645 REQ(n
, argument
); /* [test '='] test; really [keyword '='] test */
1647 if (*pkeywords
!= NULL
) {
1648 com_error(c
, PyExc_SyntaxError
,
1649 "non-keyword arg after keyword arg");
1652 com_node(c
, CHILD(n
, 0));
1659 } while (NCH(m
) == 1);
1660 if (TYPE(m
) != NAME
) {
1661 /* f(lambda x: x[0] = 3) ends up getting parsed with
1662 * LHS test = lambda x: x[0], and RHS test = 3.
1663 * SF bug 132313 points out that complaining about a keyword
1664 * then is very confusing.
1666 com_error(c
, PyExc_SyntaxError
,
1667 TYPE(m
) == lambdef
?
1668 "lambda cannot contain assignment" :
1669 "keyword can't be an expression");
1672 PyObject
*v
= PyString_InternFromString(STR(m
));
1673 if (v
!= NULL
&& *pkeywords
== NULL
)
1674 *pkeywords
= PyDict_New();
1677 else if (*pkeywords
== NULL
) {
1681 if (PyDict_GetItem(*pkeywords
, v
) != NULL
)
1682 com_error(c
, PyExc_SyntaxError
,
1683 "duplicate keyword argument");
1685 if (PyDict_SetItem(*pkeywords
, v
, v
) != 0)
1687 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1692 com_node(c
, CHILD(n
, 2));
1696 com_call_function(struct compiling
*c
, node
*n
)
1698 if (TYPE(n
) == RPAR
) {
1699 com_addoparg(c
, CALL_FUNCTION
, 0);
1702 PyObject
*keywords
= NULL
;
1704 int lineno
= n
->n_lineno
;
1706 int starstar_flag
= 0;
1711 for (i
= 0; i
< NCH(n
); i
+= 2) {
1712 node
*ch
= CHILD(n
, i
);
1713 if (TYPE(ch
) == STAR
||
1714 TYPE(ch
) == DOUBLESTAR
)
1716 if (ch
->n_lineno
!= lineno
) {
1717 lineno
= ch
->n_lineno
;
1718 com_addoparg(c
, SET_LINENO
, lineno
);
1720 com_argument(c
, ch
, &keywords
);
1721 if (keywords
== NULL
)
1726 Py_XDECREF(keywords
);
1727 while (i
< NCH(n
)) {
1728 node
*tok
= CHILD(n
, i
);
1729 node
*ch
= CHILD(n
, i
+1);
1731 switch (TYPE(tok
)) {
1732 case STAR
: star_flag
= 1; break;
1733 case DOUBLESTAR
: starstar_flag
= 1; break;
1737 if (na
> 255 || nk
> 255) {
1738 com_error(c
, PyExc_SyntaxError
,
1739 "more than 255 arguments");
1741 if (star_flag
|| starstar_flag
)
1742 opcode
= CALL_FUNCTION_VAR
- 1 +
1743 star_flag
+ (starstar_flag
<< 1);
1745 opcode
= CALL_FUNCTION
;
1746 com_addoparg(c
, opcode
, na
| (nk
<< 8));
1747 com_pop(c
, na
+ 2*nk
+ star_flag
+ starstar_flag
);
1752 com_select_member(struct compiling
*c
, node
*n
)
1754 com_addopname(c
, LOAD_ATTR
, n
);
1758 com_sliceobj(struct compiling
*c
, node
*n
)
1761 int ns
=2; /* number of slice arguments */
1764 /* first argument */
1765 if (TYPE(CHILD(n
,i
)) == COLON
) {
1766 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1771 com_node(c
, CHILD(n
,i
));
1773 REQ(CHILD(n
,i
),COLON
);
1776 /* second argument */
1777 if (i
< NCH(n
) && TYPE(CHILD(n
,i
)) == test
) {
1778 com_node(c
, CHILD(n
,i
));
1782 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1785 /* remaining arguments */
1786 for (; i
< NCH(n
); i
++) {
1791 /* right argument of ':' missing */
1792 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1796 com_node(c
, CHILD(ch
,1));
1798 com_addoparg(c
, BUILD_SLICE
, ns
);
1799 com_pop(c
, 1 + (ns
== 3));
1803 com_subscript(struct compiling
*c
, node
*n
)
1808 /* check for rubber index */
1809 if (TYPE(ch
) == DOT
&& TYPE(CHILD(n
,1)) == DOT
) {
1810 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_Ellipsis
));
1814 /* check for slice */
1815 if ((TYPE(ch
) == COLON
|| NCH(n
) > 1))
1825 com_subscriptlist(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
1828 REQ(n
, subscriptlist
);
1829 /* Check to make backward compatible slice behavior for '[i:j]' */
1831 node
*sub
= CHILD(n
, 0); /* subscript */
1832 /* 'Basic' slice, should have exactly one colon. */
1833 if ((TYPE(CHILD(sub
, 0)) == COLON
1834 || (NCH(sub
) > 1 && TYPE(CHILD(sub
, 1)) == COLON
))
1835 && (TYPE(CHILD(sub
,NCH(sub
)-1)) != sliceop
))
1837 switch (assigning
) {
1848 com_augassign_slice(c
, sub
, assigning
, augn
);
1851 com_slice(c
, sub
, op
);
1852 if (op
== STORE_SLICE
)
1854 else if (op
== DELETE_SLICE
)
1859 /* Else normal subscriptlist. Compile each subscript. */
1860 for (i
= 0; i
< NCH(n
); i
+= 2)
1861 com_subscript(c
, CHILD(n
, i
));
1862 /* Put multiple subscripts into a tuple */
1865 com_addoparg(c
, BUILD_TUPLE
, i
);
1868 switch (assigning
) {
1883 if (assigning
> OP_APPLY
) {
1884 com_addoparg(c
, DUP_TOPX
, 2);
1886 com_addbyte(c
, BINARY_SUBSCR
);
1889 com_addbyte(c
, assigning
);
1891 com_addbyte(c
, ROT_THREE
);
1898 com_apply_trailer(struct compiling
*c
, node
*n
)
1901 switch (TYPE(CHILD(n
, 0))) {
1903 com_call_function(c
, CHILD(n
, 1));
1906 com_select_member(c
, CHILD(n
, 1));
1909 com_subscriptlist(c
, CHILD(n
, 1), OP_APPLY
, NULL
);
1912 com_error(c
, PyExc_SystemError
,
1913 "com_apply_trailer: unknown trailer type");
1918 com_power(struct compiling
*c
, node
*n
)
1922 com_atom(c
, CHILD(n
, 0));
1923 for (i
= 1; i
< NCH(n
); i
++) {
1924 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1925 com_factor(c
, CHILD(n
, i
+1));
1926 com_addbyte(c
, BINARY_POWER
);
1931 com_apply_trailer(c
, CHILD(n
, i
));
1936 com_invert_constant(struct compiling
*c
, node
*n
)
1938 /* Compute the inverse of int and longs and use them directly,
1939 but be prepared to generate code for all other
1940 possibilities (invalid numbers, floats, complex).
1942 PyObject
*num
, *inv
= NULL
;
1946 num
= parsenumber(c
, STR(n
));
1950 inv
= PyNumber_Invert(num
);
1953 i
= com_addconst(c
, num
);
1955 i
= com_addconst(c
, inv
);
1960 com_addoparg(c
, LOAD_CONST
, i
);
1962 if (num
!= NULL
&& inv
== NULL
)
1963 com_addbyte(c
, UNARY_INVERT
);
1967 is_float_zero(const char *p
)
1969 int found_radix_point
= 0;
1971 while ((ch
= Py_CHARMASK(*p
++)) != '\0') {
1974 /* no reason to believe it's not 0 -- continue */
1977 case 'e': case 'E': case 'j': case 'J':
1978 /* If this was a hex constant, we already would have
1979 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1980 must be an exponent marker, and we haven't yet
1981 seen a non-zero digit, and it doesn't matter what
1982 the exponent is then. For 'j' or 'J' similarly,
1983 except that this is an imaginary 0 then. */
1987 found_radix_point
= 1;
1994 return found_radix_point
;
1998 com_factor(struct compiling
*c
, node
*n
)
2000 int childtype
= TYPE(CHILD(n
, 0));
2001 node
*pfactor
, *ppower
, *patom
, *pnum
;
2003 /* If the unary +, -, or ~ operator is applied to a constant,
2004 don't generate a UNARY_xxx opcode. Just store the
2005 approriate value as a constant. If the value is negative,
2006 extend the string containing the constant and insert a
2007 negative in the 0th position -- unless we're doing unary minus
2008 of a floating zero! In that case the sign is significant, but
2009 the const dict can't distinguish +0.0 from -0.0.
2011 if ((childtype
== PLUS
|| childtype
== MINUS
|| childtype
== TILDE
)
2013 && TYPE((pfactor
= CHILD(n
, 1))) == factor
2014 && NCH(pfactor
) == 1
2015 && TYPE((ppower
= CHILD(pfactor
, 0))) == power
2017 && TYPE((patom
= CHILD(ppower
, 0))) == atom
2018 && TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
2019 && !(childtype
== MINUS
&& is_float_zero(STR(pnum
)))) {
2020 if (childtype
== TILDE
) {
2021 com_invert_constant(c
, pnum
);
2024 if (childtype
== MINUS
) {
2025 char *s
= PyMem_Malloc(strlen(STR(pnum
)) + 2);
2027 com_error(c
, PyExc_MemoryError
, "");
2028 com_addbyte(c
, 255);
2032 strcpy(s
+ 1, STR(pnum
));
2033 PyMem_Free(STR(pnum
));
2038 else if (childtype
== PLUS
) {
2039 com_factor(c
, CHILD(n
, 1));
2040 com_addbyte(c
, UNARY_POSITIVE
);
2042 else if (childtype
== MINUS
) {
2043 com_factor(c
, CHILD(n
, 1));
2044 com_addbyte(c
, UNARY_NEGATIVE
);
2046 else if (childtype
== TILDE
) {
2047 com_factor(c
, CHILD(n
, 1));
2048 com_addbyte(c
, UNARY_INVERT
);
2051 com_power(c
, CHILD(n
, 0));
2056 com_term(struct compiling
*c
, node
*n
)
2061 com_factor(c
, CHILD(n
, 0));
2062 for (i
= 2; i
< NCH(n
); i
+= 2) {
2063 com_factor(c
, CHILD(n
, i
));
2064 switch (TYPE(CHILD(n
, i
-1))) {
2066 op
= BINARY_MULTIPLY
;
2069 if (c
->c_flags
& CO_FUTURE_DIVISION
)
2070 op
= BINARY_TRUE_DIVIDE
;
2078 op
= BINARY_FLOOR_DIVIDE
;
2081 com_error(c
, PyExc_SystemError
,
2082 "com_term: operator not *, /, // or %");
2091 com_arith_expr(struct compiling
*c
, node
*n
)
2096 com_term(c
, CHILD(n
, 0));
2097 for (i
= 2; i
< NCH(n
); i
+= 2) {
2098 com_term(c
, CHILD(n
, i
));
2099 switch (TYPE(CHILD(n
, i
-1))) {
2104 op
= BINARY_SUBTRACT
;
2107 com_error(c
, PyExc_SystemError
,
2108 "com_arith_expr: operator not + or -");
2117 com_shift_expr(struct compiling
*c
, node
*n
)
2122 com_arith_expr(c
, CHILD(n
, 0));
2123 for (i
= 2; i
< NCH(n
); i
+= 2) {
2124 com_arith_expr(c
, CHILD(n
, i
));
2125 switch (TYPE(CHILD(n
, i
-1))) {
2133 com_error(c
, PyExc_SystemError
,
2134 "com_shift_expr: operator not << or >>");
2143 com_and_expr(struct compiling
*c
, node
*n
)
2148 com_shift_expr(c
, CHILD(n
, 0));
2149 for (i
= 2; i
< NCH(n
); i
+= 2) {
2150 com_shift_expr(c
, CHILD(n
, i
));
2151 if (TYPE(CHILD(n
, i
-1)) == AMPER
) {
2155 com_error(c
, PyExc_SystemError
,
2156 "com_and_expr: operator not &");
2165 com_xor_expr(struct compiling
*c
, node
*n
)
2170 com_and_expr(c
, CHILD(n
, 0));
2171 for (i
= 2; i
< NCH(n
); i
+= 2) {
2172 com_and_expr(c
, CHILD(n
, i
));
2173 if (TYPE(CHILD(n
, i
-1)) == CIRCUMFLEX
) {
2177 com_error(c
, PyExc_SystemError
,
2178 "com_xor_expr: operator not ^");
2187 com_expr(struct compiling
*c
, node
*n
)
2192 com_xor_expr(c
, CHILD(n
, 0));
2193 for (i
= 2; i
< NCH(n
); i
+= 2) {
2194 com_xor_expr(c
, CHILD(n
, i
));
2195 if (TYPE(CHILD(n
, i
-1)) == VBAR
) {
2199 com_error(c
, PyExc_SystemError
,
2200 "com_expr: expr operator not |");
2212 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2213 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2217 case LESS
: return PyCmp_LT
;
2218 case GREATER
: return PyCmp_GT
;
2219 case EQEQUAL
: /* == */
2220 case EQUAL
: return PyCmp_EQ
;
2221 case LESSEQUAL
: return PyCmp_LE
;
2222 case GREATEREQUAL
: return PyCmp_GE
;
2223 case NOTEQUAL
: return PyCmp_NE
; /* <> or != */
2224 case NAME
: if (strcmp(STR(n
), "in") == 0) return PyCmp_IN
;
2225 if (strcmp(STR(n
), "is") == 0) return PyCmp_IS
;
2228 else if (NCH(n
) == 2) {
2229 switch (TYPE(CHILD(n
, 0))) {
2230 case NAME
: if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
2231 return PyCmp_NOT_IN
;
2232 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
2233 return PyCmp_IS_NOT
;
2240 com_comparison(struct compiling
*c
, node
*n
)
2245 REQ(n
, comparison
); /* comparison: expr (comp_op expr)* */
2246 com_expr(c
, CHILD(n
, 0));
2250 /****************************************************************
2251 The following code is generated for all but the last
2252 comparison in a chain:
2254 label: on stack: opcode: jump to:
2260 b, 0-or-1 JUMP_IF_FALSE L1
2264 We are now ready to repeat this sequence for the next
2265 comparison in the chain.
2267 For the last we generate:
2273 If there were any jumps to L1 (i.e., there was more than one
2274 comparison), we generate:
2276 0-or-1 JUMP_FORWARD L2
2281 ****************************************************************/
2285 for (i
= 2; i
< NCH(n
); i
+= 2) {
2286 com_expr(c
, CHILD(n
, i
));
2288 com_addbyte(c
, DUP_TOP
);
2290 com_addbyte(c
, ROT_THREE
);
2292 op
= cmp_type(CHILD(n
, i
-1));
2293 if (op
== PyCmp_BAD
) {
2294 com_error(c
, PyExc_SystemError
,
2295 "com_comparison: unknown comparison op");
2297 com_addoparg(c
, COMPARE_OP
, op
);
2300 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2301 com_addbyte(c
, POP_TOP
);
2308 com_addfwref(c
, JUMP_FORWARD
, &anchor2
);
2309 com_backpatch(c
, anchor
);
2310 com_addbyte(c
, ROT_TWO
);
2311 com_addbyte(c
, POP_TOP
);
2312 com_backpatch(c
, anchor2
);
2317 com_not_test(struct compiling
*c
, node
*n
)
2319 REQ(n
, not_test
); /* 'not' not_test | comparison */
2321 com_comparison(c
, CHILD(n
, 0));
2324 com_not_test(c
, CHILD(n
, 1));
2325 com_addbyte(c
, UNARY_NOT
);
2330 com_and_test(struct compiling
*c
, node
*n
)
2334 REQ(n
, and_test
); /* not_test ('and' not_test)* */
2338 com_not_test(c
, CHILD(n
, i
));
2339 if ((i
+= 2) >= NCH(n
))
2341 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2342 com_addbyte(c
, POP_TOP
);
2346 com_backpatch(c
, anchor
);
2350 com_make_closure(struct compiling
*c
, PyCodeObject
*co
)
2352 int i
, free
= PyCode_GetNumFree(co
);
2355 for (i
= 0; i
< free
; ++i
) {
2356 /* Bypass com_addop_varname because it will generate
2357 LOAD_DEREF but LOAD_CLOSURE is needed.
2359 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
2362 /* Special case: If a class contains a method with a
2363 free variable that has the same name as a method,
2364 the name will be considered free *and* local in the
2365 class. It should be handled by the closure, as
2366 well as by the normal name loookup logic.
2368 reftype
= get_ref_type(c
, PyString_AS_STRING(name
));
2369 if (reftype
== CELL
)
2370 arg
= com_lookup_arg(c
->c_cellvars
, name
);
2371 else /* (reftype == FREE) */
2372 arg
= com_lookup_arg(c
->c_freevars
, name
);
2374 fprintf(stderr
, "lookup %s in %s %d %d\n"
2375 "freevars of %s: %s\n",
2376 PyObject_REPR(name
),
2379 PyString_AS_STRING(co
->co_name
),
2380 PyObject_REPR(co
->co_freevars
));
2381 Py_FatalError("com_make_closure()");
2383 com_addoparg(c
, LOAD_CLOSURE
, arg
);
2391 com_test(struct compiling
*c
, node
*n
)
2393 REQ(n
, test
); /* and_test ('or' and_test)* | lambdef */
2394 if (NCH(n
) == 1 && TYPE(CHILD(n
, 0)) == lambdef
) {
2397 int ndefs
= com_argdefs(c
, CHILD(n
, 0));
2398 symtable_enter_scope(c
->c_symtable
, "lambda", lambdef
,
2400 co
= icompile(CHILD(n
, 0), c
);
2405 symtable_exit_scope(c
->c_symtable
);
2406 i
= com_addconst(c
, (PyObject
*)co
);
2407 closure
= com_make_closure(c
, co
);
2408 com_addoparg(c
, LOAD_CONST
, i
);
2411 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
2412 com_pop(c
, PyCode_GetNumFree(co
));
2414 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
2422 com_and_test(c
, CHILD(n
, i
));
2423 if ((i
+= 2) >= NCH(n
))
2425 com_addfwref(c
, JUMP_IF_TRUE
, &anchor
);
2426 com_addbyte(c
, POP_TOP
);
2430 com_backpatch(c
, anchor
);
2435 com_list(struct compiling
*c
, node
*n
, int toplevel
)
2437 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2438 if (NCH(n
) == 1 && !toplevel
) {
2439 com_node(c
, CHILD(n
, 0));
2444 len
= (NCH(n
) + 1) / 2;
2445 for (i
= 0; i
< NCH(n
); i
+= 2)
2446 com_node(c
, CHILD(n
, i
));
2447 com_addoparg(c
, BUILD_TUPLE
, len
);
2453 /* Begin of assignment compilation */
2457 com_augassign_attr(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2459 com_addbyte(c
, DUP_TOP
);
2461 com_addopname(c
, LOAD_ATTR
, n
);
2463 com_addbyte(c
, opcode
);
2465 com_addbyte(c
, ROT_TWO
);
2466 com_addopname(c
, STORE_ATTR
, n
);
2471 com_assign_attr(struct compiling
*c
, node
*n
, int assigning
)
2473 com_addopname(c
, assigning
? STORE_ATTR
: DELETE_ATTR
, n
);
2474 com_pop(c
, assigning
? 2 : 1);
2478 com_assign_trailer(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2481 switch (TYPE(CHILD(n
, 0))) {
2482 case LPAR
: /* '(' [exprlist] ')' */
2483 if (assigning
== OP_DELETE
)
2484 com_error(c
, PyExc_SyntaxError
,
2485 "can't delete function call");
2487 com_error(c
, PyExc_SyntaxError
,
2488 "can't assign to function call");
2490 case DOT
: /* '.' NAME */
2491 if (assigning
> OP_APPLY
)
2492 com_augassign_attr(c
, CHILD(n
, 1), assigning
, augn
);
2494 com_assign_attr(c
, CHILD(n
, 1), assigning
);
2496 case LSQB
: /* '[' subscriptlist ']' */
2497 com_subscriptlist(c
, CHILD(n
, 1), assigning
, augn
);
2500 com_error(c
, PyExc_SystemError
, "unknown trailer type");
2505 com_assign_sequence(struct compiling
*c
, node
*n
, int assigning
)
2508 if (TYPE(n
) != testlist
&& TYPE(n
) != listmaker
)
2512 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
2515 for (i
= 0; i
< NCH(n
); i
+= 2)
2516 com_assign(c
, CHILD(n
, i
), assigning
, NULL
);
2520 com_augassign_name(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2523 com_addop_varname(c
, VAR_LOAD
, STR(n
));
2526 com_addbyte(c
, opcode
);
2528 com_assign_name(c
, n
, OP_ASSIGN
);
2532 com_assign_name(struct compiling
*c
, node
*n
, int assigning
)
2535 com_addop_varname(c
, assigning
? VAR_STORE
: VAR_DELETE
, STR(n
));
2541 com_assign(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2543 /* Loop to avoid trivial recursion */
2551 if (assigning
> OP_APPLY
) {
2552 com_error(c
, PyExc_SyntaxError
,
2553 "augmented assign to tuple not possible");
2556 com_assign_sequence(c
, n
, assigning
);
2574 com_error(c
, PyExc_SyntaxError
,
2575 "can't assign to operator");
2581 case power
: /* atom trailer* ('**' power)*
2582 ('+'|'-'|'~') factor | atom trailer* */
2583 if (TYPE(CHILD(n
, 0)) != atom
) {
2584 com_error(c
, PyExc_SyntaxError
,
2585 "can't assign to operator");
2588 if (NCH(n
) > 1) { /* trailer or exponent present */
2590 com_node(c
, CHILD(n
, 0));
2591 for (i
= 1; i
+1 < NCH(n
); i
++) {
2592 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
2593 com_error(c
, PyExc_SyntaxError
,
2594 "can't assign to operator");
2597 com_apply_trailer(c
, CHILD(n
, i
));
2598 } /* NB i is still alive */
2599 com_assign_trailer(c
,
2600 CHILD(n
, i
), assigning
, augn
);
2607 switch (TYPE(CHILD(n
, 0))) {
2610 if (TYPE(n
) == RPAR
) {
2611 /* XXX Should allow () = () ??? */
2612 com_error(c
, PyExc_SyntaxError
,
2613 "can't assign to ()");
2616 if (assigning
> OP_APPLY
) {
2617 com_error(c
, PyExc_SyntaxError
,
2618 "augmented assign to tuple not possible");
2624 if (TYPE(n
) == RSQB
) {
2625 com_error(c
, PyExc_SyntaxError
,
2626 "can't assign to []");
2629 if (assigning
> OP_APPLY
) {
2630 com_error(c
, PyExc_SyntaxError
,
2631 "augmented assign to list not possible");
2635 && TYPE(CHILD(n
, 1)) == list_for
) {
2636 com_error(c
, PyExc_SyntaxError
,
2637 "can't assign to list comprehension");
2640 com_assign_sequence(c
, n
, assigning
);
2643 if (assigning
> OP_APPLY
)
2644 com_augassign_name(c
, CHILD(n
, 0),
2647 com_assign_name(c
, CHILD(n
, 0),
2651 com_error(c
, PyExc_SyntaxError
,
2652 "can't assign to literal");
2658 com_error(c
, PyExc_SyntaxError
,
2659 "can't assign to lambda");
2663 com_error(c
, PyExc_SystemError
,
2664 "com_assign: bad node");
2672 com_augassign(struct compiling
*c
, node
*n
)
2676 switch (STR(CHILD(CHILD(n
, 1), 0))[0]) {
2677 case '+': opcode
= INPLACE_ADD
; break;
2678 case '-': opcode
= INPLACE_SUBTRACT
; break;
2680 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '/')
2681 opcode
= INPLACE_FLOOR_DIVIDE
;
2682 else if (c
->c_flags
& CO_FUTURE_DIVISION
)
2683 opcode
= INPLACE_TRUE_DIVIDE
;
2685 opcode
= INPLACE_DIVIDE
;
2687 case '%': opcode
= INPLACE_MODULO
; break;
2688 case '<': opcode
= INPLACE_LSHIFT
; break;
2689 case '>': opcode
= INPLACE_RSHIFT
; break;
2690 case '&': opcode
= INPLACE_AND
; break;
2691 case '^': opcode
= INPLACE_XOR
; break;
2692 case '|': opcode
= INPLACE_OR
; break;
2694 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '*')
2695 opcode
= INPLACE_POWER
;
2697 opcode
= INPLACE_MULTIPLY
;
2700 com_error(c
, PyExc_SystemError
, "com_augassign: bad operator");
2703 com_assign(c
, CHILD(n
, 0), opcode
, CHILD(n
, 2));
2707 com_expr_stmt(struct compiling
*c
, node
*n
)
2710 /* testlist (('=' testlist)* | augassign testlist) */
2711 /* Forget it if we have just a doc string here */
2712 if (!c
->c_interactive
&& NCH(n
) == 1 && get_rawdocstring(n
) != NULL
)
2715 com_node(c
, CHILD(n
, NCH(n
)-1));
2716 if (c
->c_interactive
)
2717 com_addbyte(c
, PRINT_EXPR
);
2719 com_addbyte(c
, POP_TOP
);
2722 else if (TYPE(CHILD(n
,1)) == augassign
)
2723 com_augassign(c
, n
);
2726 com_node(c
, CHILD(n
, NCH(n
)-1));
2727 for (i
= 0; i
< NCH(n
)-2; i
+=2) {
2728 if (i
+2 < NCH(n
)-2) {
2729 com_addbyte(c
, DUP_TOP
);
2732 com_assign(c
, CHILD(n
, i
), OP_ASSIGN
, NULL
);
2738 com_assert_stmt(struct compiling
*c
, node
*n
)
2742 REQ(n
, assert_stmt
); /* 'assert' test [',' test] */
2743 if (Py_OptimizeFlag
)
2745 /* Generate code like
2748 raise AssertionError [, <message>]
2750 where <message> is the second test, if present.
2752 com_node(c
, CHILD(n
, 1));
2753 com_addfwref(c
, JUMP_IF_TRUE
, &a
);
2754 com_addbyte(c
, POP_TOP
);
2756 /* Raise that exception! */
2757 com_addop_name(c
, LOAD_GLOBAL
, "AssertionError");
2759 i
= NCH(n
)/2; /* Either 2 or 4 */
2761 com_node(c
, CHILD(n
, 3));
2762 com_addoparg(c
, RAISE_VARARGS
, i
);
2764 /* The interpreter does not fall through */
2765 /* Jump ends up here */
2766 com_backpatch(c
, a
);
2767 com_addbyte(c
, POP_TOP
);
2771 com_print_stmt(struct compiling
*c
, node
*n
)
2774 node
* stream
= NULL
;
2776 REQ(n
, print_stmt
); /* 'print' (test ',')* [test] */
2778 /* are we using the extended print form? */
2779 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2780 stream
= CHILD(n
, 2);
2781 com_node(c
, stream
);
2782 /* stack: [...] => [... stream] */
2784 if (NCH(n
) > 3 && TYPE(CHILD(n
, 3)) == COMMA
)
2789 for (; i
< NCH(n
); i
+= 2) {
2790 if (stream
!= NULL
) {
2791 com_addbyte(c
, DUP_TOP
);
2792 /* stack: [stream] => [stream stream] */
2794 com_node(c
, CHILD(n
, i
));
2795 /* stack: [stream stream] => [stream stream obj] */
2796 com_addbyte(c
, ROT_TWO
);
2797 /* stack: [stream stream obj] => [stream obj stream] */
2798 com_addbyte(c
, PRINT_ITEM_TO
);
2799 /* stack: [stream obj stream] => [stream] */
2803 com_node(c
, CHILD(n
, i
));
2804 /* stack: [...] => [... obj] */
2805 com_addbyte(c
, PRINT_ITEM
);
2809 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2810 if (TYPE(CHILD(n
, NCH(n
)-1)) == COMMA
) {
2811 if (stream
!= NULL
) {
2812 /* must pop the extra stream object off the stack */
2813 com_addbyte(c
, POP_TOP
);
2814 /* stack: [... stream] => [...] */
2819 if (stream
!= NULL
) {
2820 /* this consumes the last stream object on stack */
2821 com_addbyte(c
, PRINT_NEWLINE_TO
);
2822 /* stack: [... stream] => [...] */
2826 com_addbyte(c
, PRINT_NEWLINE
);
2831 com_return_stmt(struct compiling
*c
, node
*n
)
2833 REQ(n
, return_stmt
); /* 'return' [testlist] */
2834 if (!c
->c_infunction
) {
2835 com_error(c
, PyExc_SyntaxError
, "'return' outside function");
2837 if (c
->c_flags
& CO_GENERATOR
) {
2839 com_error(c
, PyExc_SyntaxError
,
2840 "'return' with argument inside generator");
2844 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2848 com_node(c
, CHILD(n
, 1));
2849 com_addbyte(c
, RETURN_VALUE
);
2854 com_yield_stmt(struct compiling
*c
, node
*n
)
2857 REQ(n
, yield_stmt
); /* 'yield' testlist */
2858 if (!c
->c_infunction
) {
2859 com_error(c
, PyExc_SyntaxError
, "'yield' outside function");
2862 for (i
= 0; i
< c
->c_nblocks
; ++i
) {
2863 if (c
->c_block
[i
] == SETUP_FINALLY
) {
2864 com_error(c
, PyExc_SyntaxError
,
2865 "'yield' not allowed in a 'try' block "
2866 "with a 'finally' clause");
2870 com_node(c
, CHILD(n
, 1));
2871 com_addbyte(c
, YIELD_VALUE
);
2876 com_raise_stmt(struct compiling
*c
, node
*n
)
2879 REQ(n
, raise_stmt
); /* 'raise' [test [',' test [',' test]]] */
2881 com_node(c
, CHILD(n
, 1));
2883 com_node(c
, CHILD(n
, 3));
2885 com_node(c
, CHILD(n
, 5));
2889 com_addoparg(c
, RAISE_VARARGS
, i
);
2894 com_from_import(struct compiling
*c
, node
*n
)
2896 com_addopname(c
, IMPORT_FROM
, CHILD(n
, 0));
2899 if (strcmp(STR(CHILD(n
, 1)), "as") != 0) {
2900 com_error(c
, PyExc_SyntaxError
, "invalid syntax");
2903 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 2)));
2905 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
2910 com_import_stmt(struct compiling
*c
, node
*n
)
2913 REQ(n
, import_stmt
);
2914 /* 'import' dotted_name (',' dotted_name)* |
2915 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2916 if (STR(CHILD(n
, 0))[0] == 'f') {
2918 /* 'from' dotted_name 'import' ... */
2919 REQ(CHILD(n
, 1), dotted_name
);
2921 if (TYPE(CHILD(n
, 3)) == STAR
) {
2922 tup
= Py_BuildValue("(s)", "*");
2924 tup
= PyTuple_New((NCH(n
) - 2)/2);
2925 for (i
= 3; i
< NCH(n
); i
+= 2) {
2926 PyTuple_SET_ITEM(tup
, (i
-3)/2,
2927 PyString_FromString(STR(
2928 CHILD(CHILD(n
, i
), 0))));
2931 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, tup
));
2934 com_addopname(c
, IMPORT_NAME
, CHILD(n
, 1));
2935 if (TYPE(CHILD(n
, 3)) == STAR
)
2936 com_addbyte(c
, IMPORT_STAR
);
2938 for (i
= 3; i
< NCH(n
); i
+= 2)
2939 com_from_import(c
, CHILD(n
, i
));
2940 com_addbyte(c
, POP_TOP
);
2946 for (i
= 1; i
< NCH(n
); i
+= 2) {
2947 node
*subn
= CHILD(n
, i
);
2948 REQ(subn
, dotted_as_name
);
2949 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2951 com_addopname(c
, IMPORT_NAME
, CHILD(subn
, 0));
2952 if (NCH(subn
) > 1) {
2954 if (strcmp(STR(CHILD(subn
, 1)), "as") != 0) {
2955 com_error(c
, PyExc_SyntaxError
,
2959 for (j
=2 ; j
< NCH(CHILD(subn
, 0)); j
+= 2)
2960 com_addopname(c
, LOAD_ATTR
,
2961 CHILD(CHILD(subn
, 0),
2963 com_addop_varname(c
, VAR_STORE
,
2964 STR(CHILD(subn
, 2)));
2966 com_addop_varname(c
, VAR_STORE
,
2967 STR(CHILD(CHILD(subn
, 0),
2975 com_exec_stmt(struct compiling
*c
, node
*n
)
2978 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2979 com_node(c
, CHILD(n
, 1));
2981 com_node(c
, CHILD(n
, 3));
2983 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2987 com_node(c
, CHILD(n
, 5));
2989 com_addbyte(c
, DUP_TOP
);
2992 com_addbyte(c
, EXEC_STMT
);
2997 is_constant_false(struct compiling
*c
, node
*n
)
3001 /* argument c will be NULL when called from symtable_node() */
3003 /* Label to avoid tail recursion */
3014 for (i
= 0; i
< NCH(n
); i
++) {
3015 node
*ch
= CHILD(n
, i
);
3016 if (TYPE(ch
) == stmt
) {
3052 if (Py_OptimizeFlag
&& strcmp(STR(n
), "__debug__") == 0)
3057 v
= parsenumber(c
, STR(n
));
3062 i
= PyObject_IsTrue(v
);
3067 v
= parsestr(c
, STR(n
));
3072 i
= PyObject_IsTrue(v
);
3081 /* Look under n for a return stmt with an expression.
3082 * This hack is used to find illegal returns under "if 0:" blocks in
3083 * functions already known to be generators (as determined by the symtable
3085 * Return the offending return node if found, else NULL.
3088 look_for_offending_return(node
*n
)
3092 for (i
= 0; i
< NCH(n
); ++i
) {
3093 node
*kid
= CHILD(n
, i
);
3095 switch (TYPE(kid
)) {
3099 /* Stuff in nested functions & classes doesn't
3100 affect the code block we started in. */
3109 node
*bad
= look_for_offending_return(kid
);
3120 com_if_stmt(struct compiling
*c
, node
*n
)
3125 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3126 for (i
= 0; i
+3 < NCH(n
); i
+=4) {
3128 node
*ch
= CHILD(n
, i
+1);
3129 if (is_constant_false(c
, ch
)) {
3130 /* We're going to skip this block. However, if this
3131 is a generator, we have to check the dead code
3132 anyway to make sure there aren't any return stmts
3133 with expressions, in the same scope. */
3134 if (c
->c_flags
& CO_GENERATOR
) {
3135 node
*p
= look_for_offending_return(n
);
3137 int savelineno
= c
->c_lineno
;
3138 c
->c_lineno
= p
->n_lineno
;
3139 com_error(c
, PyExc_SyntaxError
,
3140 "'return' with argument "
3141 "inside generator");
3142 c
->c_lineno
= savelineno
;
3148 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3150 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
3151 com_addbyte(c
, POP_TOP
);
3153 com_node(c
, CHILD(n
, i
+3));
3154 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
3155 com_backpatch(c
, a
);
3156 /* We jump here with an extra entry which we now pop */
3157 com_addbyte(c
, POP_TOP
);
3160 com_node(c
, CHILD(n
, i
+2));
3162 com_backpatch(c
, anchor
);
3166 com_while_stmt(struct compiling
*c
, node
*n
)
3168 int break_anchor
= 0;
3170 int save_begin
= c
->c_begin
;
3171 REQ(n
, while_stmt
); /* 'while' test ':' suite ['else' ':' suite] */
3172 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
3173 block_push(c
, SETUP_LOOP
);
3174 c
->c_begin
= c
->c_nexti
;
3175 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3176 com_node(c
, CHILD(n
, 1));
3177 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
3178 com_addbyte(c
, POP_TOP
);
3181 com_node(c
, CHILD(n
, 3));
3183 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3184 c
->c_begin
= save_begin
;
3185 com_backpatch(c
, anchor
);
3186 /* We jump here with one entry more on the stack */
3187 com_addbyte(c
, POP_TOP
);
3188 com_addbyte(c
, POP_BLOCK
);
3189 block_pop(c
, SETUP_LOOP
);
3191 com_node(c
, CHILD(n
, 6));
3192 com_backpatch(c
, break_anchor
);
3196 com_for_stmt(struct compiling
*c
, node
*n
)
3198 int break_anchor
= 0;
3200 int save_begin
= c
->c_begin
;
3202 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3203 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
3204 block_push(c
, SETUP_LOOP
);
3205 com_node(c
, CHILD(n
, 3));
3206 com_addbyte(c
, GET_ITER
);
3207 c
->c_begin
= c
->c_nexti
;
3208 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3209 com_addfwref(c
, FOR_ITER
, &anchor
);
3211 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
3213 com_node(c
, CHILD(n
, 5));
3215 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3216 c
->c_begin
= save_begin
;
3217 com_backpatch(c
, anchor
);
3218 com_pop(c
, 1); /* FOR_ITER has popped this */
3219 com_addbyte(c
, POP_BLOCK
);
3220 block_pop(c
, SETUP_LOOP
);
3222 com_node(c
, CHILD(n
, 8));
3223 com_backpatch(c
, break_anchor
);
3226 /* Code generated for "try: S finally: Sf" is as follows:
3235 The special instructions use the block stack. Each block
3236 stack entry contains the instruction that created it (here
3237 SETUP_FINALLY), the level of the value stack at the time the
3238 block stack entry was created, and a label (here L).
3241 Pushes the current value stack level and the label
3242 onto the block stack.
3244 Pops en entry from the block stack, and pops the value
3245 stack until its level is the same as indicated on the
3246 block stack. (The label is ignored.)
3248 Pops a variable number of entries from the *value* stack
3249 and re-raises the exception they specify. The number of
3250 entries popped depends on the (pseudo) exception type.
3252 The block stack is unwound when an exception is raised:
3253 when a SETUP_FINALLY entry is found, the exception is pushed
3254 onto the value stack (and the exception condition is cleared),
3255 and the interpreter jumps to the label gotten from the block
3258 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3259 (The contents of the value stack is shown in [], with the top
3260 at the right; 'tb' is trace-back info, 'val' the exception's
3261 associated value, and 'exc' the exception.)
3263 Value stack Label Instruction Argument
3269 [tb, val, exc] L1: DUP )
3270 [tb, val, exc, exc] <evaluate E1> )
3271 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3272 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3273 [tb, val, exc, 1] POP )
3275 [tb, val] <assign to V1> (or POP if no V1)
3280 [tb, val, exc, 0] L2: POP
3282 .............................etc.......................
3284 [tb, val, exc, 0] Ln+1: POP
3285 [tb, val, exc] END_FINALLY # re-raise exception
3287 [] L0: <next statement>
3289 Of course, parts are not generated if Vi or Ei is not present.
3293 com_try_except(struct compiling
*c
, node
*n
)
3295 int except_anchor
= 0;
3297 int else_anchor
= 0;
3301 com_addfwref(c
, SETUP_EXCEPT
, &except_anchor
);
3302 block_push(c
, SETUP_EXCEPT
);
3303 com_node(c
, CHILD(n
, 2));
3304 com_addbyte(c
, POP_BLOCK
);
3305 block_pop(c
, SETUP_EXCEPT
);
3306 com_addfwref(c
, JUMP_FORWARD
, &else_anchor
);
3307 com_backpatch(c
, except_anchor
);
3309 i
< NCH(n
) && TYPE(ch
= CHILD(n
, i
)) == except_clause
;
3311 /* except_clause: 'except' [expr [',' var]] */
3312 if (except_anchor
== 0) {
3313 com_error(c
, PyExc_SyntaxError
,
3314 "default 'except:' must be last");
3318 com_push(c
, 3); /* tb, val, exc pushed by exception */
3319 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3321 com_addbyte(c
, DUP_TOP
);
3323 com_node(c
, CHILD(ch
, 1));
3324 com_addoparg(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
3326 com_addfwref(c
, JUMP_IF_FALSE
, &except_anchor
);
3327 com_addbyte(c
, POP_TOP
);
3330 com_addbyte(c
, POP_TOP
);
3333 com_assign(c
, CHILD(ch
, 3), OP_ASSIGN
, NULL
);
3335 com_addbyte(c
, POP_TOP
);
3338 com_addbyte(c
, POP_TOP
);
3340 com_node(c
, CHILD(n
, i
+2));
3341 com_addfwref(c
, JUMP_FORWARD
, &end_anchor
);
3342 if (except_anchor
) {
3343 com_backpatch(c
, except_anchor
);
3344 /* We come in with [tb, val, exc, 0] on the
3345 stack; one pop and it's the same as
3346 expected at the start of the loop */
3347 com_addbyte(c
, POP_TOP
);
3350 /* We actually come in here with [tb, val, exc] but the
3351 END_FINALLY will zap those and jump around.
3352 The c_stacklevel does not reflect them so we need not pop
3354 com_addbyte(c
, END_FINALLY
);
3355 com_backpatch(c
, else_anchor
);
3357 com_node(c
, CHILD(n
, i
+2));
3358 com_backpatch(c
, end_anchor
);
3362 com_try_finally(struct compiling
*c
, node
*n
)
3364 int finally_anchor
= 0;
3367 com_addfwref(c
, SETUP_FINALLY
, &finally_anchor
);
3368 block_push(c
, SETUP_FINALLY
);
3369 com_node(c
, CHILD(n
, 2));
3370 com_addbyte(c
, POP_BLOCK
);
3371 block_pop(c
, SETUP_FINALLY
);
3372 block_push(c
, END_FINALLY
);
3373 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3374 /* While the generated code pushes only one item,
3375 the try-finally handling can enter here with
3376 up to three items. OK, here are the details:
3377 3 for an exception, 2 for RETURN, 1 for BREAK. */
3379 com_backpatch(c
, finally_anchor
);
3380 ch
= CHILD(n
, NCH(n
)-1);
3381 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3383 com_addbyte(c
, END_FINALLY
);
3384 block_pop(c
, END_FINALLY
);
3385 com_pop(c
, 3); /* Matches the com_push above */
3389 com_try_stmt(struct compiling
*c
, node
*n
)
3392 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3393 | 'try' ':' suite 'finally' ':' suite */
3394 if (TYPE(CHILD(n
, 3)) != except_clause
)
3395 com_try_finally(c
, n
);
3397 com_try_except(c
, n
);
3401 get_rawdocstring(node
*n
)
3405 /* Label to avoid tail recursion */
3416 for (i
= 0; i
< NCH(n
); i
++) {
3417 node
*ch
= CHILD(n
, i
);
3418 if (TYPE(ch
) == stmt
) {
3453 if (TYPE(CHILD(n
, 0)) == STRING
)
3462 get_docstring(struct compiling
*c
, node
*n
)
3464 /* Don't generate doc-strings if run with -OO */
3465 if (Py_OptimizeFlag
> 1)
3467 n
= get_rawdocstring(n
);
3470 return parsestrplus(c
, n
);
3474 com_suite(struct compiling
*c
, node
*n
)
3477 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3479 com_node(c
, CHILD(n
, 0));
3483 for (i
= 0; i
< NCH(n
) && c
->c_errors
== 0; i
++) {
3484 node
*ch
= CHILD(n
, i
);
3485 if (TYPE(ch
) == stmt
)
3493 com_continue_stmt(struct compiling
*c
, node
*n
)
3495 int i
= c
->c_nblocks
;
3496 if (i
-- > 0 && c
->c_block
[i
] == SETUP_LOOP
) {
3497 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3500 /* at the outer level */
3501 com_error(c
, PyExc_SyntaxError
,
3502 "'continue' not properly in loop");
3506 for (j
= i
-1; j
>= 0; --j
) {
3507 if (c
->c_block
[j
] == SETUP_LOOP
)
3511 /* there is a loop, but something interferes */
3512 for (; i
> j
; --i
) {
3513 if (c
->c_block
[i
] == SETUP_EXCEPT
||
3514 c
->c_block
[i
] == SETUP_FINALLY
) {
3515 com_addoparg(c
, CONTINUE_LOOP
,
3519 if (c
->c_block
[i
] == END_FINALLY
) {
3520 com_error(c
, PyExc_SyntaxError
,
3521 "'continue' not supported inside 'finally' clause");
3526 com_error(c
, PyExc_SyntaxError
,
3527 "'continue' not properly in loop");
3529 /* XXX Could allow it inside a 'finally' clause
3530 XXX if we could pop the exception still on the stack */
3534 com_argdefs(struct compiling
*c
, node
*n
)
3536 int i
, nch
, nargs
, ndefs
;
3537 if (TYPE(n
) == lambdef
) {
3538 /* lambdef: 'lambda' [varargslist] ':' test */
3542 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ... */
3544 REQ(n
, parameters
); /* parameters: '(' [varargslist] ')' */
3547 if (TYPE(n
) != varargslist
)
3550 (fpdef ['=' test] ',')* '*' ....... |
3551 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3555 for (i
= 0; i
< nch
; i
++) {
3557 if (TYPE(CHILD(n
, i
)) == STAR
||
3558 TYPE(CHILD(n
, i
)) == DOUBLESTAR
)
3563 t
= RPAR
; /* Anything except EQUAL or COMMA */
3565 t
= TYPE(CHILD(n
, i
));
3569 com_node(c
, CHILD(n
, i
));
3573 t
= TYPE(CHILD(n
, i
));
3576 /* Treat "(a=1, b)" as an error */
3578 com_error(c
, PyExc_SyntaxError
,
3579 "non-default argument follows default argument");
3588 com_funcdef(struct compiling
*c
, node
*n
)
3592 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3593 ndefs
= com_argdefs(c
, n
);
3594 symtable_enter_scope(c
->c_symtable
, STR(CHILD(n
, 1)), TYPE(n
),
3596 co
= (PyObject
*)icompile(n
, c
);
3597 symtable_exit_scope(c
->c_symtable
);
3601 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3602 int i
= com_addconst(c
, co
);
3603 com_addoparg(c
, LOAD_CONST
, i
);
3606 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
3608 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
3610 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3617 com_bases(struct compiling
*c
, node
*n
)
3621 /* testlist: test (',' test)* [','] */
3622 for (i
= 0; i
< NCH(n
); i
+= 2)
3623 com_node(c
, CHILD(n
, i
));
3625 com_addoparg(c
, BUILD_TUPLE
, i
);
3630 com_classdef(struct compiling
*c
, node
*n
)
3638 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3639 if ((v
= PyString_InternFromString(STR(CHILD(n
, 1)))) == NULL
) {
3643 /* Push the class name on the stack */
3644 i
= com_addconst(c
, v
);
3645 com_addoparg(c
, LOAD_CONST
, i
);
3648 /* Push the tuple of base classes on the stack */
3649 if (TYPE(CHILD(n
, 2)) != LPAR
) {
3650 com_addoparg(c
, BUILD_TUPLE
, 0);
3654 com_bases(c
, CHILD(n
, 3));
3655 name
= STR(CHILD(n
, 1));
3656 symtable_enter_scope(c
->c_symtable
, name
, TYPE(n
), n
->n_lineno
);
3657 co
= icompile(n
, c
);
3658 symtable_exit_scope(c
->c_symtable
);
3662 int closure
= com_make_closure(c
, co
);
3663 i
= com_addconst(c
, (PyObject
*)co
);
3664 com_addoparg(c
, LOAD_CONST
, i
);
3667 com_addoparg(c
, MAKE_CLOSURE
, 0);
3668 com_pop(c
, PyCode_GetNumFree(co
));
3670 com_addoparg(c
, MAKE_FUNCTION
, 0);
3671 com_addoparg(c
, CALL_FUNCTION
, 0);
3672 com_addbyte(c
, BUILD_CLASS
);
3674 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3681 com_node(struct compiling
*c
, node
*n
)
3688 /* Definition nodes */
3697 /* Trivial parse tree nodes */
3706 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3707 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3710 for (i
= 0; i
< NCH(n
)-1; i
+= 2)
3711 com_node(c
, CHILD(n
, i
));
3716 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3720 /* Statement nodes */
3723 com_expr_stmt(c
, n
);
3726 com_print_stmt(c
, n
);
3728 case del_stmt
: /* 'del' exprlist */
3729 com_assign(c
, CHILD(n
, 1), OP_DELETE
, NULL
);
3734 if (c
->c_loops
== 0) {
3735 com_error(c
, PyExc_SyntaxError
,
3736 "'break' outside loop");
3738 com_addbyte(c
, BREAK_LOOP
);
3741 com_continue_stmt(c
, n
);
3744 com_return_stmt(c
, n
);
3747 com_yield_stmt(c
, n
);
3750 com_raise_stmt(c
, n
);
3753 com_import_stmt(c
, n
);
3758 com_exec_stmt(c
, n
);
3761 com_assert_stmt(c
, n
);
3767 com_while_stmt(c
, n
);
3779 /* Expression nodes */
3796 com_comparison(c
, n
);
3811 com_shift_expr(c
, n
);
3814 com_arith_expr(c
, n
);
3830 com_error(c
, PyExc_SystemError
,
3831 "com_node: unexpected node type");
3835 static void com_fplist(struct compiling
*, node
*);
3838 com_fpdef(struct compiling
*c
, node
*n
)
3840 REQ(n
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3841 if (TYPE(CHILD(n
, 0)) == LPAR
)
3842 com_fplist(c
, CHILD(n
, 1));
3844 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
3850 com_fplist(struct compiling
*c
, node
*n
)
3852 REQ(n
, fplist
); /* fplist: fpdef (',' fpdef)* [','] */
3854 com_fpdef(c
, CHILD(n
, 0));
3857 int i
= (NCH(n
)+1)/2;
3858 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
3860 for (i
= 0; i
< NCH(n
); i
+= 2)
3861 com_fpdef(c
, CHILD(n
, i
));
3866 com_arglist(struct compiling
*c
, node
*n
)
3871 REQ(n
, varargslist
);
3873 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3875 /* Enter all arguments in table of locals */
3876 for (i
= 0, narg
= 0; i
< nch
; i
++) {
3877 node
*ch
= CHILD(n
, i
);
3879 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3881 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3883 if (TYPE(fp
) != NAME
) {
3884 PyOS_snprintf(nbuf
, sizeof(nbuf
), ".%d", i
);
3888 /* all name updates handled by symtable */
3892 if (TYPE(ch
) == EQUAL
)
3898 /* Generate code for complex arguments only after
3899 having counted the simple arguments */
3901 for (i
= 0; i
< nch
; i
++) {
3902 node
*ch
= CHILD(n
, i
);
3904 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3906 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3908 if (TYPE(fp
) != NAME
) {
3909 com_addoparg(c
, LOAD_FAST
, ilocal
);
3917 if (TYPE(ch
) == EQUAL
)
3926 com_file_input(struct compiling
*c
, node
*n
)
3930 REQ(n
, file_input
); /* (NEWLINE | stmt)* ENDMARKER */
3931 doc
= get_docstring(c
, n
);
3933 int i
= com_addconst(c
, doc
);
3935 com_addoparg(c
, LOAD_CONST
, i
);
3937 com_addop_name(c
, STORE_NAME
, "__doc__");
3940 for (i
= 0; i
< NCH(n
); i
++) {
3941 node
*ch
= CHILD(n
, i
);
3942 if (TYPE(ch
) != ENDMARKER
&& TYPE(ch
) != NEWLINE
)
3947 /* Top-level compile-node interface */
3950 compile_funcdef(struct compiling
*c
, node
*n
)
3954 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3955 c
->c_name
= STR(CHILD(n
, 1));
3956 doc
= get_docstring(c
, CHILD(n
, 4));
3958 (void) com_addconst(c
, doc
);
3962 (void) com_addconst(c
, Py_None
); /* No docstring */
3963 ch
= CHILD(n
, 2); /* parameters: '(' [varargslist] ')' */
3964 ch
= CHILD(ch
, 1); /* ')' | varargslist */
3965 if (TYPE(ch
) == varargslist
)
3967 c
->c_infunction
= 1;
3968 com_node(c
, CHILD(n
, 4));
3969 c
->c_infunction
= 0;
3970 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3972 com_addbyte(c
, RETURN_VALUE
);
3977 compile_lambdef(struct compiling
*c
, node
*n
)
3980 REQ(n
, lambdef
); /* lambdef: 'lambda' [varargslist] ':' test */
3981 c
->c_name
= "<lambda>";
3984 (void) com_addconst(c
, Py_None
); /* No docstring */
3985 if (TYPE(ch
) == varargslist
) {
3992 com_addbyte(c
, RETURN_VALUE
);
3997 compile_classdef(struct compiling
*c
, node
*n
)
4002 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4003 c
->c_name
= STR(CHILD(n
, 1));
4004 c
->c_private
= c
->c_name
;
4005 /* Initialize local __module__ from global __name__ */
4006 com_addop_name(c
, LOAD_GLOBAL
, "__name__");
4007 com_addop_name(c
, STORE_NAME
, "__module__");
4008 ch
= CHILD(n
, NCH(n
)-1); /* The suite */
4009 doc
= get_docstring(c
, ch
);
4011 int i
= com_addconst(c
, doc
);
4013 com_addoparg(c
, LOAD_CONST
, i
);
4015 com_addop_name(c
, STORE_NAME
, "__doc__");
4019 (void) com_addconst(c
, Py_None
);
4021 com_addbyte(c
, LOAD_LOCALS
);
4023 com_addbyte(c
, RETURN_VALUE
);
4028 compile_node(struct compiling
*c
, node
*n
)
4030 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
4034 case single_input
: /* One interactive command */
4035 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
4038 if (TYPE(n
) != NEWLINE
)
4040 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
4042 com_addbyte(c
, RETURN_VALUE
);
4047 case file_input
: /* A whole file, or built-in function exec() */
4048 com_file_input(c
, n
);
4049 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
4051 com_addbyte(c
, RETURN_VALUE
);
4055 case eval_input
: /* Built-in function input() */
4056 com_node(c
, CHILD(n
, 0));
4057 com_addbyte(c
, RETURN_VALUE
);
4061 case lambdef
: /* anonymous function definition */
4062 compile_lambdef(c
, n
);
4065 case funcdef
: /* A function definition */
4066 compile_funcdef(c
, n
);
4069 case classdef
: /* A class definition */
4070 compile_classdef(c
, n
);
4074 com_error(c
, PyExc_SystemError
,
4075 "compile_node: unexpected node type");
4080 dict_keys_inorder(PyObject
*dict
, int offset
)
4082 PyObject
*tuple
, *k
, *v
;
4083 int i
, pos
= 0, size
= PyDict_Size(dict
);
4085 tuple
= PyTuple_New(size
);
4088 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
4089 i
= PyInt_AS_LONG(v
);
4091 assert((i
- offset
) < size
);
4092 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
4098 PyNode_Compile(node
*n
, char *filename
)
4100 return PyNode_CompileFlags(n
, filename
, NULL
);
4104 PyNode_CompileFlags(node
*n
, char *filename
, PyCompilerFlags
*flags
)
4106 return jcompile(n
, filename
, NULL
, flags
);
4110 PyNode_CompileSymtable(node
*n
, char *filename
)
4112 struct symtable
*st
;
4113 PyFutureFeatures
*ff
;
4115 ff
= PyNode_Future(n
, filename
);
4119 st
= symtable_init();
4121 PyMem_Free((void *)ff
);
4125 symtable_enter_scope(st
, TOP
, TYPE(n
), n
->n_lineno
);
4126 if (st
->st_errors
> 0)
4128 symtable_node(st
, n
);
4129 if (st
->st_errors
> 0)
4134 PyMem_Free((void *)ff
);
4135 st
->st_future
= NULL
;
4136 PySymtable_Free(st
);
4140 static PyCodeObject
*
4141 icompile(node
*n
, struct compiling
*base
)
4143 return jcompile(n
, base
->c_filename
, base
, NULL
);
4146 static PyCodeObject
*
4147 jcompile(node
*n
, char *filename
, struct compiling
*base
,
4148 PyCompilerFlags
*flags
)
4150 struct compiling sc
;
4152 if (!com_init(&sc
, filename
))
4155 sc
.c_private
= base
->c_private
;
4156 sc
.c_symtable
= base
->c_symtable
;
4157 /* c_symtable still points to parent's symbols */
4159 || (sc
.c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
))
4161 sc
.c_flags
|= base
->c_flags
& PyCF_MASK
;
4163 sc
.c_private
= NULL
;
4164 sc
.c_future
= PyNode_Future(n
, filename
);
4165 if (sc
.c_future
== NULL
) {
4170 int merged
= sc
.c_future
->ff_features
|
4172 sc
.c_future
->ff_features
= merged
;
4173 flags
->cf_flags
= merged
;
4175 if (symtable_build(&sc
, n
) < 0) {
4181 if (symtable_load_symbols(&sc
) < 0) {
4185 compile_node(&sc
, n
);
4187 if (sc
.c_errors
== 0) {
4188 PyObject
*consts
, *names
, *varnames
, *filename
, *name
,
4189 *freevars
, *cellvars
;
4190 consts
= PyList_AsTuple(sc
.c_consts
);
4191 names
= PyList_AsTuple(sc
.c_names
);
4192 varnames
= PyList_AsTuple(sc
.c_varnames
);
4193 cellvars
= dict_keys_inorder(sc
.c_cellvars
, 0);
4194 freevars
= dict_keys_inorder(sc
.c_freevars
,
4195 PyTuple_GET_SIZE(cellvars
));
4196 filename
= PyString_InternFromString(sc
.c_filename
);
4197 name
= PyString_InternFromString(sc
.c_name
);
4198 if (!PyErr_Occurred())
4199 co
= PyCode_New(sc
.c_argcount
,
4215 Py_XDECREF(varnames
);
4216 Py_XDECREF(freevars
);
4217 Py_XDECREF(cellvars
);
4218 Py_XDECREF(filename
);
4221 else if (!PyErr_Occurred()) {
4222 /* This could happen if someone called PyErr_Clear() after an
4223 error was reported above. That's not supposed to happen,
4224 but I just plugged one case and I'm not sure there can't be
4225 others. In that case, raise SystemError so that at least
4226 it gets reported instead dumping core. */
4227 PyErr_SetString(PyExc_SystemError
, "lost syntax error");
4231 PySymtable_Free(sc
.c_symtable
);
4232 sc
.c_symtable
= NULL
;
4239 PyCode_Addr2Line(PyCodeObject
*co
, int addrq
)
4241 int size
= PyString_Size(co
->co_lnotab
) / 2;
4242 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
4243 int line
= co
->co_firstlineno
;
4245 while (--size
>= 0) {
4254 /* The test for LOCAL must come before the test for FREE in order to
4255 handle classes where name is both local and free. The local var is
4256 a method and the free var is a free var referenced within a method.
4260 get_ref_type(struct compiling
*c
, char *name
)
4265 if (PyDict_GetItemString(c
->c_cellvars
, name
) != NULL
)
4267 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
4269 if (PyDict_GetItemString(c
->c_freevars
, name
) != NULL
)
4271 v
= PyDict_GetItemString(c
->c_globals
, name
);
4274 return GLOBAL_EXPLICIT
;
4276 return GLOBAL_IMPLICIT
;
4279 PyOS_snprintf(buf
, sizeof(buf
),
4280 "unknown scope for %.100s in %.100s(%s) "
4281 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4283 PyObject_REPR(c
->c_symtable
->st_cur
->ste_id
),
4285 PyObject_REPR(c
->c_symtable
->st_cur
->ste_symbols
),
4286 PyObject_REPR(c
->c_locals
),
4287 PyObject_REPR(c
->c_globals
)
4294 /* Helper functions to issue warnings */
4297 issue_warning(char *msg
, char *filename
, int lineno
)
4299 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, filename
,
4300 lineno
, NULL
, NULL
) < 0) {
4301 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
4302 PyErr_SetString(PyExc_SyntaxError
, msg
);
4303 PyErr_SyntaxLocation(filename
, lineno
);
4311 symtable_warn(struct symtable
*st
, char *msg
)
4313 if (issue_warning(msg
, st
->st_filename
, st
->st_cur
->ste_lineno
) < 0) {
4320 /* Helper function for setting lineno and filename */
4323 symtable_build(struct compiling
*c
, node
*n
)
4325 if ((c
->c_symtable
= symtable_init()) == NULL
)
4327 c
->c_symtable
->st_future
= c
->c_future
;
4328 c
->c_symtable
->st_filename
= c
->c_filename
;
4329 symtable_enter_scope(c
->c_symtable
, TOP
, TYPE(n
), n
->n_lineno
);
4330 if (c
->c_symtable
->st_errors
> 0)
4332 symtable_node(c
->c_symtable
, n
);
4333 if (c
->c_symtable
->st_errors
> 0)
4335 /* reset for second pass */
4336 c
->c_symtable
->st_nscopes
= 1;
4337 c
->c_symtable
->st_pass
= 2;
4342 symtable_init_compiling_symbols(struct compiling
*c
)
4346 varnames
= c
->c_symtable
->st_cur
->ste_varnames
;
4347 if (varnames
== NULL
) {
4348 varnames
= PyList_New(0);
4349 if (varnames
== NULL
)
4351 c
->c_symtable
->st_cur
->ste_varnames
= varnames
;
4352 Py_INCREF(varnames
);
4354 Py_INCREF(varnames
);
4355 c
->c_varnames
= varnames
;
4357 c
->c_globals
= PyDict_New();
4358 if (c
->c_globals
== NULL
)
4360 c
->c_freevars
= PyDict_New();
4361 if (c
->c_freevars
== NULL
)
4363 c
->c_cellvars
= PyDict_New();
4364 if (c
->c_cellvars
== NULL
)
4369 struct symbol_info
{
4377 symtable_init_info(struct symbol_info
*si
)
4382 si
->si_nimplicit
= 0;
4386 symtable_resolve_free(struct compiling
*c
, PyObject
*name
, int flags
,
4387 struct symbol_info
*si
)
4391 /* Seperate logic for DEF_FREE. If it occurs in a function,
4392 it indicates a local that we must allocate storage for (a
4393 cell var). If it occurs in a class, then the class has a
4394 method and a free variable with the same name.
4396 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
) {
4397 /* If it isn't declared locally, it can't be a cell. */
4398 if (!(flags
& (DEF_LOCAL
| DEF_PARAM
)))
4400 v
= PyInt_FromLong(si
->si_ncells
++);
4401 dict
= c
->c_cellvars
;
4403 /* If it is free anyway, then there is no need to do
4406 if (is_free(flags
^ DEF_FREE_CLASS
)
4407 || (flags
== DEF_FREE_CLASS
))
4409 v
= PyInt_FromLong(si
->si_nfrees
++);
4410 dict
= c
->c_freevars
;
4414 if (PyDict_SetItem(dict
, name
, v
) < 0) {
4422 /* If a variable is a cell and an argument, make sure that appears in
4423 co_cellvars before any variable to its right in varnames.
4428 symtable_cellvar_offsets(PyObject
**cellvars
, int argcount
,
4429 PyObject
*varnames
, int flags
)
4431 PyObject
*v
, *w
, *d
, *list
= NULL
;
4434 if (flags
& CO_VARARGS
)
4436 if (flags
& CO_VARKEYWORDS
)
4438 for (i
= argcount
; --i
>= 0; ) {
4439 v
= PyList_GET_ITEM(varnames
, i
);
4440 if (PyDict_GetItem(*cellvars
, v
)) {
4442 list
= PyList_New(1);
4445 PyList_SET_ITEM(list
, 0, v
);
4448 PyList_Insert(list
, 0, v
);
4451 if (list
== NULL
|| PyList_GET_SIZE(list
) == 0)
4453 /* There are cellvars that are also arguments. Create a dict
4454 to replace cellvars and put the args at the front.
4457 for (i
= PyList_GET_SIZE(list
); --i
>= 0; ) {
4458 v
= PyInt_FromLong(i
);
4461 if (PyDict_SetItem(d
, PyList_GET_ITEM(list
, i
), v
) < 0)
4463 if (PyDict_DelItem(*cellvars
, PyList_GET_ITEM(list
, i
)) < 0)
4467 i
= PyList_GET_SIZE(list
);
4469 while (PyDict_Next(*cellvars
, &pos
, &v
, &w
)) {
4470 w
= PyInt_FromLong(i
++); /* don't care about the old key */
4471 if (PyDict_SetItem(d
, v
, w
) < 0) {
4477 Py_DECREF(*cellvars
);
4486 symtable_freevar_offsets(PyObject
*freevars
, int offset
)
4491 /* The cell vars are the first elements of the closure,
4492 followed by the free vars. Update the offsets in
4493 c_freevars to account for number of cellvars. */
4495 while (PyDict_Next(freevars
, &pos
, &name
, &v
)) {
4496 int i
= PyInt_AS_LONG(v
) + offset
;
4497 PyObject
*o
= PyInt_FromLong(i
);
4500 if (PyDict_SetItem(freevars
, name
, o
) < 0) {
4510 symtable_check_unoptimized(struct compiling
*c
,
4511 PySymtableEntryObject
*ste
,
4512 struct symbol_info
*si
)
4516 if (!(si
->si_ncells
|| si
->si_nfrees
|| ste
->ste_child_free
4517 || (ste
->ste_nested
&& si
->si_nimplicit
)))
4520 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4522 #define ILLEGAL_IS "is a nested function"
4524 #define ILLEGAL_IMPORT_STAR \
4525 "import * is not allowed in function '%.100s' because it %s"
4527 #define ILLEGAL_BARE_EXEC \
4528 "unqualified exec is not allowed in function '%.100s' it %s"
4530 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4531 "function '%.100s' uses import * and bare exec, which are illegal " \
4534 /* XXX perhaps the linenos for these opt-breaking statements
4535 should be stored so the exception can point to them. */
4537 if (ste
->ste_child_free
) {
4538 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4539 PyOS_snprintf(buf
, sizeof(buf
),
4540 ILLEGAL_IMPORT_STAR
,
4541 PyString_AS_STRING(ste
->ste_name
),
4543 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4544 PyOS_snprintf(buf
, sizeof(buf
),
4546 PyString_AS_STRING(ste
->ste_name
),
4549 PyOS_snprintf(buf
, sizeof(buf
),
4550 ILLEGAL_EXEC_AND_IMPORT_STAR
,
4551 PyString_AS_STRING(ste
->ste_name
),
4555 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4556 PyOS_snprintf(buf
, sizeof(buf
),
4557 ILLEGAL_IMPORT_STAR
,
4558 PyString_AS_STRING(ste
->ste_name
),
4560 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4561 PyOS_snprintf(buf
, sizeof(buf
),
4563 PyString_AS_STRING(ste
->ste_name
),
4566 PyOS_snprintf(buf
, sizeof(buf
),
4567 ILLEGAL_EXEC_AND_IMPORT_STAR
,
4568 PyString_AS_STRING(ste
->ste_name
),
4573 PyErr_SetString(PyExc_SyntaxError
, buf
);
4574 PyErr_SyntaxLocation(c
->c_symtable
->st_filename
,
4575 ste
->ste_opt_lineno
);
4580 symtable_update_flags(struct compiling
*c
, PySymtableEntryObject
*ste
,
4581 struct symbol_info
*si
)
4584 c
->c_flags
|= c
->c_future
->ff_features
;
4585 if (ste
->ste_generator
)
4586 c
->c_flags
|= CO_GENERATOR
;
4587 if (ste
->ste_type
!= TYPE_MODULE
)
4588 c
->c_flags
|= CO_NEWLOCALS
;
4589 if (ste
->ste_type
== TYPE_FUNCTION
) {
4590 c
->c_nlocals
= si
->si_nlocals
;
4591 if (ste
->ste_optimized
== 0)
4592 c
->c_flags
|= CO_OPTIMIZED
;
4593 else if (ste
->ste_optimized
!= OPT_EXEC
)
4594 return symtable_check_unoptimized(c
, ste
, si
);
4600 symtable_load_symbols(struct compiling
*c
)
4602 static PyObject
*implicit
= NULL
;
4603 struct symtable
*st
= c
->c_symtable
;
4604 PySymtableEntryObject
*ste
= st
->st_cur
;
4605 PyObject
*name
, *varnames
, *v
;
4607 struct symbol_info si
;
4609 if (implicit
== NULL
) {
4610 implicit
= PyInt_FromLong(1);
4611 if (implicit
== NULL
)
4616 if (symtable_init_compiling_symbols(c
) < 0)
4618 symtable_init_info(&si
);
4619 varnames
= st
->st_cur
->ste_varnames
;
4620 si
.si_nlocals
= PyList_GET_SIZE(varnames
);
4621 c
->c_argcount
= si
.si_nlocals
;
4623 for (i
= 0; i
< si
.si_nlocals
; ++i
) {
4624 v
= PyInt_FromLong(i
);
4625 if (PyDict_SetItem(c
->c_locals
,
4626 PyList_GET_ITEM(varnames
, i
), v
) < 0)
4631 /* XXX The cases below define the rules for whether a name is
4632 local or global. The logic could probably be clearer. */
4634 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
4635 flags
= PyInt_AS_LONG(v
);
4637 if (flags
& DEF_FREE_GLOBAL
)
4638 /* undo the original DEF_FREE */
4639 flags
&= ~(DEF_FREE
| DEF_FREE_CLASS
);
4641 /* Deal with names that need two actions:
4642 1. Cell variables that are also locals.
4643 2. Free variables in methods that are also class
4644 variables or declared global.
4646 if (flags
& (DEF_FREE
| DEF_FREE_CLASS
))
4647 symtable_resolve_free(c
, name
, flags
, &si
);
4649 if (flags
& DEF_STAR
) {
4651 c
->c_flags
|= CO_VARARGS
;
4652 } else if (flags
& DEF_DOUBLESTAR
) {
4654 c
->c_flags
|= CO_VARKEYWORDS
;
4655 } else if (flags
& DEF_INTUPLE
)
4657 else if (flags
& DEF_GLOBAL
) {
4658 if (flags
& DEF_PARAM
) {
4659 PyErr_Format(PyExc_SyntaxError
, LOCAL_GLOBAL
,
4660 PyString_AS_STRING(name
));
4661 PyErr_SyntaxLocation(st
->st_filename
,
4666 if (PyDict_SetItem(c
->c_globals
, name
, Py_None
) < 0)
4668 } else if (flags
& DEF_FREE_GLOBAL
) {
4670 if (PyDict_SetItem(c
->c_globals
, name
, implicit
) < 0)
4672 } else if ((flags
& DEF_LOCAL
) && !(flags
& DEF_PARAM
)) {
4673 v
= PyInt_FromLong(si
.si_nlocals
++);
4676 if (PyDict_SetItem(c
->c_locals
, name
, v
) < 0)
4679 if (ste
->ste_type
!= TYPE_CLASS
)
4680 if (PyList_Append(c
->c_varnames
, name
) < 0)
4682 } else if (is_free(flags
)) {
4683 if (ste
->ste_nested
) {
4684 v
= PyInt_FromLong(si
.si_nfrees
++);
4687 if (PyDict_SetItem(c
->c_freevars
, name
, v
) < 0)
4692 if (PyDict_SetItem(c
->c_globals
, name
,
4695 if (st
->st_nscopes
!= 1) {
4696 v
= PyInt_FromLong(flags
);
4697 if (PyDict_SetItem(st
->st_global
,
4706 assert(PyDict_Size(c
->c_freevars
) == si
.si_nfrees
);
4708 if (si
.si_ncells
> 1) { /* one cell is always in order */
4709 if (symtable_cellvar_offsets(&c
->c_cellvars
, c
->c_argcount
,
4710 c
->c_varnames
, c
->c_flags
) < 0)
4713 if (symtable_freevar_offsets(c
->c_freevars
, si
.si_ncells
) < 0)
4715 return symtable_update_flags(c
, ste
, &si
);
4717 /* is this always the right thing to do? */
4722 static struct symtable
*
4725 struct symtable
*st
;
4727 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
4732 st
->st_filename
= NULL
;
4733 if ((st
->st_stack
= PyList_New(0)) == NULL
)
4735 if ((st
->st_symbols
= PyDict_New()) == NULL
)
4741 st
->st_private
= NULL
;
4744 PySymtable_Free(st
);
4749 PySymtable_Free(struct symtable
*st
)
4751 Py_XDECREF(st
->st_symbols
);
4752 Py_XDECREF(st
->st_stack
);
4753 Py_XDECREF(st
->st_cur
);
4754 PyMem_Free((void *)st
);
4757 /* When the compiler exits a scope, it must should update the scope's
4758 free variable information with the list of free variables in its
4761 Variables that are free in children and defined in the current
4764 If the scope being exited is defined at the top-level (ste_nested is
4765 false), free variables in children that are not defined here are
4771 symtable_update_free_vars(struct symtable
*st
)
4774 PyObject
*o
, *name
, *list
= NULL
;
4775 PySymtableEntryObject
*child
, *ste
= st
->st_cur
;
4777 if (ste
->ste_type
== TYPE_CLASS
)
4778 def
= DEF_FREE_CLASS
;
4781 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4785 PyList_SetSlice(list
, 0,
4786 ((PyVarObject
*)list
)->ob_size
, 0);
4787 child
= (PySymtableEntryObject
*)
4788 PyList_GET_ITEM(ste
->ste_children
, i
);
4789 while (PyDict_Next(child
->ste_symbols
, &pos
, &name
, &o
)) {
4790 int flags
= PyInt_AS_LONG(o
);
4791 if (!(is_free(flags
)))
4792 continue; /* avoids indentation */
4794 list
= PyList_New(0);
4798 ste
->ste_child_free
= 1;
4799 if (PyList_Append(list
, name
) < 0) {
4804 for (j
= 0; list
&& j
< PyList_GET_SIZE(list
); j
++) {
4806 name
= PyList_GET_ITEM(list
, j
);
4807 v
= PyDict_GetItem(ste
->ste_symbols
, name
);
4808 /* If a name N is declared global in scope A and
4809 referenced in scope B contained (perhaps
4810 indirectly) in A and there are no scopes
4811 with bindings for N between B and A, then N
4812 is global in B. Unless A is a class scope,
4813 because class scopes are not considered for
4816 if (v
&& (ste
->ste_type
!= TYPE_CLASS
)) {
4817 int flags
= PyInt_AS_LONG(v
);
4818 if (flags
& DEF_GLOBAL
) {
4819 symtable_undo_free(st
, child
->ste_id
,
4824 if (ste
->ste_nested
) {
4825 if (symtable_add_def_o(st
, ste
->ste_symbols
,
4831 if (symtable_check_global(st
, child
->ste_id
,
4844 /* If the current scope is a non-nested class or if name is not
4845 defined in the current, non-nested scope, then it is an implicit
4846 global in all nested scopes.
4850 symtable_check_global(struct symtable
*st
, PyObject
*child
, PyObject
*name
)
4854 PySymtableEntryObject
*ste
= st
->st_cur
;
4856 if (ste
->ste_type
== TYPE_CLASS
)
4857 return symtable_undo_free(st
, child
, name
);
4858 o
= PyDict_GetItem(ste
->ste_symbols
, name
);
4860 return symtable_undo_free(st
, child
, name
);
4861 v
= PyInt_AS_LONG(o
);
4863 if (is_free(v
) || (v
& DEF_GLOBAL
))
4864 return symtable_undo_free(st
, child
, name
);
4866 return symtable_add_def_o(st
, ste
->ste_symbols
,
4871 symtable_undo_free(struct symtable
*st
, PyObject
*id
,
4876 PySymtableEntryObject
*ste
;
4878 ste
= (PySymtableEntryObject
*)PyDict_GetItem(st
->st_symbols
, id
);
4882 info
= PyDict_GetItem(ste
->ste_symbols
, name
);
4885 v
= PyInt_AS_LONG(info
);
4887 if (symtable_add_def_o(st
, ste
->ste_symbols
, name
,
4888 DEF_FREE_GLOBAL
) < 0)
4891 /* If the name is defined here or declared global,
4892 then the recursion stops. */
4895 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4896 PySymtableEntryObject
*child
;
4897 child
= (PySymtableEntryObject
*)
4898 PyList_GET_ITEM(ste
->ste_children
, i
);
4899 x
= symtable_undo_free(st
, child
->ste_id
, name
);
4906 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4907 This reference is released when the scope is exited, via the DECREF
4908 in symtable_exit_scope().
4912 symtable_exit_scope(struct symtable
*st
)
4916 if (st
->st_pass
== 1)
4917 symtable_update_free_vars(st
);
4918 Py_DECREF(st
->st_cur
);
4919 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
4920 st
->st_cur
= (PySymtableEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
4922 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
4928 symtable_enter_scope(struct symtable
*st
, char *name
, int type
,
4931 PySymtableEntryObject
*prev
= NULL
;
4935 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
4936 Py_DECREF(st
->st_cur
);
4941 st
->st_cur
= (PySymtableEntryObject
*)
4942 PySymtableEntry_New(st
, name
, type
, lineno
);
4943 if (strcmp(name
, TOP
) == 0)
4944 st
->st_global
= st
->st_cur
->ste_symbols
;
4945 if (prev
&& st
->st_pass
== 1) {
4946 if (PyList_Append(prev
->ste_children
,
4947 (PyObject
*)st
->st_cur
) < 0)
4953 symtable_lookup(struct symtable
*st
, char *name
)
4955 char buffer
[MANGLE_LEN
];
4959 if (_Py_Mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4961 v
= PyDict_GetItemString(st
->st_cur
->ste_symbols
, name
);
4963 if (PyErr_Occurred())
4969 flags
= PyInt_AS_LONG(v
);
4974 symtable_add_def(struct symtable
*st
, char *name
, int flag
)
4977 char buffer
[MANGLE_LEN
];
4980 if (_Py_Mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4982 if ((s
= PyString_InternFromString(name
)) == NULL
)
4984 ret
= symtable_add_def_o(st
, st
->st_cur
->ste_symbols
, s
, flag
);
4989 /* Must only be called with mangled names */
4992 symtable_add_def_o(struct symtable
*st
, PyObject
*dict
,
4993 PyObject
*name
, int flag
)
4998 if ((o
= PyDict_GetItem(dict
, name
))) {
4999 val
= PyInt_AS_LONG(o
);
5000 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
5001 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
5002 PyString_AsString(name
));
5003 PyErr_SyntaxLocation(st
->st_filename
,
5004 st
->st_cur
->ste_lineno
);
5010 o
= PyInt_FromLong(val
);
5011 if (PyDict_SetItem(dict
, name
, o
) < 0) {
5017 if (flag
& DEF_PARAM
) {
5018 if (PyList_Append(st
->st_cur
->ste_varnames
, name
) < 0)
5020 } else if (flag
& DEF_GLOBAL
) {
5021 /* XXX need to update DEF_GLOBAL for other flags too;
5022 perhaps only DEF_FREE_GLOBAL */
5023 if ((o
= PyDict_GetItem(st
->st_global
, name
))) {
5024 val
= PyInt_AS_LONG(o
);
5028 o
= PyInt_FromLong(val
);
5029 if (PyDict_SetItem(st
->st_global
, name
, o
) < 0) {
5038 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
5040 /* Look for a yield stmt under n. Return 1 if found, else 0.
5041 This hack is used to look inside "if 0:" blocks (which are normally
5042 ignored) in case those are the only places a yield occurs (so that this
5043 function is a generator). */
5045 look_for_yield(node
*n
)
5049 for (i
= 0; i
< NCH(n
); ++i
) {
5050 node
*kid
= CHILD(n
, i
);
5052 switch (TYPE(kid
)) {
5057 /* Stuff in nested functions and classes can't make
5058 the parent a generator. */
5065 if (look_for_yield(kid
))
5073 symtable_node(struct symtable
*st
, node
*n
)
5080 char *func_name
= STR(CHILD(n
, 1));
5081 symtable_add_def(st
, func_name
, DEF_LOCAL
);
5082 symtable_default_args(st
, CHILD(n
, 2));
5083 symtable_enter_scope(st
, func_name
, TYPE(n
), n
->n_lineno
);
5084 symtable_funcdef(st
, n
);
5085 symtable_exit_scope(st
);
5090 symtable_default_args(st
, CHILD(n
, 1));
5091 symtable_enter_scope(st
, "lambda", TYPE(n
), n
->n_lineno
);
5092 symtable_funcdef(st
, n
);
5093 symtable_exit_scope(st
);
5096 char *tmp
, *class_name
= STR(CHILD(n
, 1));
5097 symtable_add_def(st
, class_name
, DEF_LOCAL
);
5098 if (TYPE(CHILD(n
, 2)) == LPAR
) {
5099 node
*bases
= CHILD(n
, 3);
5101 for (i
= 0; i
< NCH(bases
); i
+= 2) {
5102 symtable_node(st
, CHILD(bases
, i
));
5105 symtable_enter_scope(st
, class_name
, TYPE(n
), n
->n_lineno
);
5106 tmp
= st
->st_private
;
5107 st
->st_private
= class_name
;
5108 symtable_node(st
, CHILD(n
, NCH(n
) - 1));
5109 st
->st_private
= tmp
;
5110 symtable_exit_scope(st
);
5114 for (i
= 0; i
+ 3 < NCH(n
); i
+= 4) {
5115 if (is_constant_false(NULL
, (CHILD(n
, i
+ 1)))) {
5116 if (st
->st_cur
->ste_generator
== 0)
5117 st
->st_cur
->ste_generator
=
5118 look_for_yield(CHILD(n
, i
+3));
5121 symtable_node(st
, CHILD(n
, i
+ 1));
5122 symtable_node(st
, CHILD(n
, i
+ 3));
5125 symtable_node(st
, CHILD(n
, i
+ 2));
5128 symtable_global(st
, n
);
5131 symtable_import(st
, n
);
5134 st
->st_cur
->ste_optimized
|= OPT_EXEC
;
5135 symtable_node(st
, CHILD(n
, 1));
5137 symtable_node(st
, CHILD(n
, 3));
5139 st
->st_cur
->ste_optimized
|= OPT_BARE_EXEC
;
5140 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
5143 symtable_node(st
, CHILD(n
, 5));
5148 if (Py_OptimizeFlag
)
5154 symtable_node(st
, CHILD(n
, 1));
5160 symtable_assign(st
, CHILD(n
, 3), 0);
5167 symtable_assign(st
, CHILD(n
, 1), 0);
5170 st
->st_cur
->ste_generator
= 1;
5177 if (TYPE(CHILD(n
, 1)) == augassign
) {
5178 symtable_assign(st
, CHILD(n
, 0), 0);
5179 symtable_node(st
, CHILD(n
, 2));
5183 for (i
= 0; i
< NCH(n
) - 2; i
+= 2)
5184 symtable_assign(st
, CHILD(n
, i
), 0);
5185 n
= CHILD(n
, NCH(n
) - 1);
5191 if (TYPE(n
) == list_for
) {
5193 symtable_list_comprehension(st
, n
);
5197 symtable_node(st
, CHILD(n
, 1));
5205 symtable_assign(st
, CHILD(n
, 1), 0);
5206 for (i
= 3; i
< NCH(n
); ++i
)
5207 if (TYPE(CHILD(n
, i
)) >= single_input
)
5208 symtable_node(st
, CHILD(n
, i
));
5210 /* The remaining cases fall through to default except in
5211 special circumstances. This requires the individual cases
5212 to be coded with great care, even though they look like
5213 rather innocuous. Each case must double-check TYPE(n).
5216 if (TYPE(n
) == argument
&& NCH(n
) == 3) {
5222 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
5224 symtable_list_comprehension(st
, CHILD(n
, 1));
5225 symtable_node(st
, CHILD(n
, 0));
5231 if (TYPE(n
) == atom
&& TYPE(CHILD(n
, 0)) == NAME
) {
5232 symtable_add_use(st
, STR(CHILD(n
, 0)));
5237 /* Walk over every non-token child with a special case
5244 for (i
= 0; i
< NCH(n
); ++i
)
5245 if (TYPE(CHILD(n
, i
)) >= single_input
)
5246 symtable_node(st
, CHILD(n
, i
));
5251 symtable_funcdef(struct symtable
*st
, node
*n
)
5255 if (TYPE(n
) == lambdef
) {
5257 symtable_params(st
, CHILD(n
, 1));
5259 symtable_params(st
, CHILD(n
, 2));
5260 body
= CHILD(n
, NCH(n
) - 1);
5261 symtable_node(st
, body
);
5264 /* The next two functions parse the argument tuple.
5265 symtable_default_arg() checks for names in the default arguments,
5266 which are references in the defining scope. symtable_params()
5267 parses the parameter names, which are defined in the function's
5271 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5272 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5276 symtable_default_args(struct symtable
*st
, node
*n
)
5281 if (TYPE(n
) == parameters
) {
5283 if (TYPE(n
) == RPAR
)
5286 REQ(n
, varargslist
);
5287 for (i
= 0; i
< NCH(n
); i
+= 2) {
5289 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
5292 if (i
> 0 && (TYPE(CHILD(n
, i
- 1)) == EQUAL
))
5293 symtable_node(st
, CHILD(n
, i
));
5298 symtable_params(struct symtable
*st
, node
*n
)
5300 int i
, complex = -1, ext
= 0;
5303 if (TYPE(n
) == parameters
) {
5305 if (TYPE(n
) == RPAR
)
5308 REQ(n
, varargslist
);
5309 for (i
= 0; i
< NCH(n
); i
+= 2) {
5311 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
5315 if (TYPE(c
) == test
) {
5318 if (TYPE(CHILD(c
, 0)) == NAME
)
5319 symtable_add_def(st
, STR(CHILD(c
, 0)), DEF_PARAM
);
5322 PyOS_snprintf(nbuf
, sizeof(nbuf
), ".%d", i
);
5323 symtable_add_def(st
, nbuf
, DEF_PARAM
);
5329 if (TYPE(c
) == STAR
) {
5331 symtable_add_def(st
, STR(CHILD(n
, i
)),
5332 DEF_PARAM
| DEF_STAR
);
5339 if (c
&& TYPE(c
) == DOUBLESTAR
) {
5341 symtable_add_def(st
, STR(CHILD(n
, i
)),
5342 DEF_PARAM
| DEF_DOUBLESTAR
);
5347 for (j
= 0; j
<= complex; j
++) {
5349 if (TYPE(c
) == COMMA
)
5351 else if (TYPE(c
) == EQUAL
)
5352 c
= CHILD(n
, j
+= 3);
5353 if (TYPE(CHILD(c
, 0)) == LPAR
)
5354 symtable_params_fplist(st
, CHILD(c
, 1));
5360 symtable_params_fplist(struct symtable
*st
, node
*n
)
5366 for (i
= 0; i
< NCH(n
); i
+= 2) {
5370 symtable_add_def(st
, STR(CHILD(c
, 0)),
5371 DEF_PARAM
| DEF_INTUPLE
);
5373 symtable_params_fplist(st
, CHILD(c
, 1));
5379 symtable_global(struct symtable
*st
, node
*n
)
5383 /* XXX It might be helpful to warn about module-level global
5384 statements, but it's hard to tell the difference between
5385 module-level and a string passed to exec.
5388 for (i
= 1; i
< NCH(n
); i
+= 2) {
5389 char *name
= STR(CHILD(n
, i
));
5392 flags
= symtable_lookup(st
, name
);
5395 if (flags
&& flags
!= DEF_GLOBAL
) {
5397 if (flags
& DEF_PARAM
) {
5398 PyErr_Format(PyExc_SyntaxError
,
5399 "name '%.400s' is local and global",
5401 PyErr_SyntaxLocation(st
->st_filename
,
5402 st
->st_cur
->ste_lineno
);
5407 if (flags
& DEF_LOCAL
)
5408 PyOS_snprintf(buf
, sizeof(buf
),
5409 GLOBAL_AFTER_ASSIGN
,
5412 PyOS_snprintf(buf
, sizeof(buf
),
5413 GLOBAL_AFTER_USE
, name
);
5414 symtable_warn(st
, buf
);
5417 symtable_add_def(st
, name
, DEF_GLOBAL
);
5422 symtable_list_comprehension(struct symtable
*st
, node
*n
)
5426 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", st
->st_tmpname
);
5427 symtable_add_def(st
, tmpname
, DEF_LOCAL
);
5428 symtable_assign(st
, CHILD(n
, 1), 0);
5429 symtable_node(st
, CHILD(n
, 3));
5431 symtable_node(st
, CHILD(n
, 4));
5435 symtable_import(struct symtable
*st
, node
*n
)
5438 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5439 | 'from' dotted_name 'import'
5440 ('*' | import_as_name (',' import_as_name)*)
5441 import_as_name: NAME [NAME NAME]
5443 if (STR(CHILD(n
, 0))[0] == 'f') { /* from */
5444 node
*dotname
= CHILD(n
, 1);
5445 if (strcmp(STR(CHILD(dotname
, 0)), "__future__") == 0) {
5446 /* check for bogus imports */
5447 if (n
->n_lineno
>= st
->st_future
->ff_last_lineno
) {
5448 PyErr_SetString(PyExc_SyntaxError
,
5450 PyErr_SyntaxLocation(st
->st_filename
,
5456 if (TYPE(CHILD(n
, 3)) == STAR
) {
5457 if (st
->st_cur
->ste_type
!= TYPE_MODULE
) {
5458 if (symtable_warn(st
,
5459 "import * only allowed at module level") < 0)
5462 st
->st_cur
->ste_optimized
|= OPT_IMPORT_STAR
;
5463 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
5465 for (i
= 3; i
< NCH(n
); i
+= 2) {
5466 node
*c
= CHILD(n
, i
);
5467 if (NCH(c
) > 1) /* import as */
5468 symtable_assign(st
, CHILD(c
, 2),
5471 symtable_assign(st
, CHILD(c
, 0),
5476 for (i
= 1; i
< NCH(n
); i
+= 2) {
5477 symtable_assign(st
, CHILD(n
, i
), DEF_IMPORT
);
5482 /* The third argument to symatble_assign() is a flag to be passed to
5483 symtable_add_def() if it is eventually called. The flag is useful
5484 to specify the particular type of assignment that should be
5485 recorded, e.g. an assignment caused by import.
5489 symtable_assign(struct symtable
*st
, node
*n
, int def_flag
)
5497 /* invalid assignment, e.g. lambda x:x=2. The next
5498 pass will catch this error. */
5502 for (i
= 2; i
< NCH(n
); ++i
)
5503 if (TYPE(CHILD(n
, i
)) != DOUBLESTAR
)
5504 symtable_node(st
, CHILD(n
, i
));
5507 symtable_node(st
, CHILD(n
, 0));
5508 symtable_node(st
, CHILD(n
, 1));
5515 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
5516 /* XXX This is an error, but the next pass
5520 for (i
= 0; i
< NCH(n
); i
+= 2)
5521 symtable_assign(st
, CHILD(n
, i
), def_flag
);
5533 for (i
= 0; i
< NCH(n
); i
+= 2)
5534 symtable_assign(st
, CHILD(n
, i
), def_flag
);
5539 if (TYPE(tmp
) == LPAR
|| TYPE(tmp
) == LSQB
) {
5542 } else if (TYPE(tmp
) == NAME
) {
5543 if (strcmp(STR(tmp
), "__debug__") == 0) {
5544 PyErr_SetString(PyExc_SyntaxError
,
5546 PyErr_SyntaxLocation(st
->st_filename
,
5550 symtable_add_def(st
, STR(tmp
), DEF_LOCAL
| def_flag
);
5553 case dotted_as_name
:
5555 symtable_add_def(st
, STR(CHILD(n
, 2)),
5556 DEF_LOCAL
| def_flag
);
5558 symtable_add_def(st
,
5561 DEF_LOCAL
| def_flag
);
5564 symtable_add_def(st
, STR(CHILD(n
, 0)), DEF_LOCAL
| def_flag
);
5567 symtable_add_def(st
, STR(n
), DEF_LOCAL
| def_flag
);
5576 /* Should only occur for errors like x + 1 = 1,
5577 which will be caught in the next pass. */
5578 for (i
= 0; i
< NCH(n
); ++i
)
5579 if (TYPE(CHILD(n
, i
)) >= single_input
)
5580 symtable_assign(st
, CHILD(n
, i
), def_flag
);