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 paramter 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 struct memberlist code_memberlist
[] = {
77 {"co_argcount", T_INT
, OFF(co_argcount
), READONLY
},
78 {"co_nlocals", T_INT
, OFF(co_nlocals
), READONLY
},
79 {"co_stacksize",T_INT
, OFF(co_stacksize
), READONLY
},
80 {"co_flags", T_INT
, OFF(co_flags
), READONLY
},
81 {"co_code", T_OBJECT
, OFF(co_code
), READONLY
},
82 {"co_consts", T_OBJECT
, OFF(co_consts
), READONLY
},
83 {"co_names", T_OBJECT
, OFF(co_names
), READONLY
},
84 {"co_varnames", T_OBJECT
, OFF(co_varnames
), READONLY
},
85 {"co_freevars", T_OBJECT
, OFF(co_freevars
), READONLY
},
86 {"co_cellvars", T_OBJECT
, OFF(co_cellvars
), READONLY
},
87 {"co_filename", T_OBJECT
, OFF(co_filename
), READONLY
},
88 {"co_name", T_OBJECT
, OFF(co_name
), READONLY
},
89 {"co_firstlineno", T_INT
, OFF(co_firstlineno
), READONLY
},
90 {"co_lnotab", T_OBJECT
, OFF(co_lnotab
), READONLY
},
95 code_getattr(PyCodeObject
*co
, char *name
)
97 return PyMember_Get((char *)co
, code_memberlist
, name
);
101 code_dealloc(PyCodeObject
*co
)
103 Py_XDECREF(co
->co_code
);
104 Py_XDECREF(co
->co_consts
);
105 Py_XDECREF(co
->co_names
);
106 Py_XDECREF(co
->co_varnames
);
107 Py_XDECREF(co
->co_freevars
);
108 Py_XDECREF(co
->co_cellvars
);
109 Py_XDECREF(co
->co_filename
);
110 Py_XDECREF(co
->co_name
);
111 Py_XDECREF(co
->co_lnotab
);
116 code_repr(PyCodeObject
*co
)
120 char *filename
= "???";
123 if (co
->co_firstlineno
!= 0)
124 lineno
= co
->co_firstlineno
;
125 if (co
->co_filename
&& PyString_Check(co
->co_filename
))
126 filename
= PyString_AS_STRING(co
->co_filename
);
127 if (co
->co_name
&& PyString_Check(co
->co_name
))
128 name
= PyString_AS_STRING(co
->co_name
);
129 sprintf(buf
, "<code object %.100s at %p, file \"%.300s\", line %d>",
130 name
, co
, filename
, lineno
);
131 return PyString_FromString(buf
);
135 code_compare(PyCodeObject
*co
, PyCodeObject
*cp
)
138 cmp
= PyObject_Compare(co
->co_name
, cp
->co_name
);
140 cmp
= co
->co_argcount
- cp
->co_argcount
;
142 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
144 cmp
= co
->co_flags
- cp
->co_flags
;
146 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
148 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
150 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
152 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
154 cmp
= PyObject_Compare(co
->co_freevars
, cp
->co_freevars
);
156 cmp
= PyObject_Compare(co
->co_cellvars
, cp
->co_cellvars
);
161 code_hash(PyCodeObject
*co
)
163 long h
, h0
, h1
, h2
, h3
, h4
, h5
, h6
;
164 h0
= PyObject_Hash(co
->co_name
);
165 if (h0
== -1) return -1;
166 h1
= PyObject_Hash(co
->co_code
);
167 if (h1
== -1) return -1;
168 h2
= PyObject_Hash(co
->co_consts
);
169 if (h2
== -1) return -1;
170 h3
= PyObject_Hash(co
->co_names
);
171 if (h3
== -1) return -1;
172 h4
= PyObject_Hash(co
->co_varnames
);
173 if (h4
== -1) return -1;
174 h5
= PyObject_Hash(co
->co_freevars
);
175 if (h5
== -1) return -1;
176 h6
= PyObject_Hash(co
->co_cellvars
);
177 if (h6
== -1) return -1;
178 h
= h0
^ h1
^ h2
^ h3
^ h4
^ h5
^ h6
^
179 co
->co_argcount
^ co
->co_nlocals
^ co
->co_flags
;
184 /* XXX code objects need to participate in GC? */
186 PyTypeObject PyCode_Type
= {
187 PyObject_HEAD_INIT(&PyType_Type
)
190 sizeof(PyCodeObject
),
192 (destructor
)code_dealloc
, /*tp_dealloc*/
194 (getattrfunc
)code_getattr
, /*tp_getattr*/
196 (cmpfunc
)code_compare
, /*tp_compare*/
197 (reprfunc
)code_repr
, /*tp_repr*/
199 0, /*tp_as_sequence*/
201 (hashfunc
)code_hash
, /*tp_hash*/
205 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
207 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
210 all_name_chars(unsigned char *s
)
212 static char ok_name_char
[256];
213 static unsigned char *name_chars
= (unsigned char *)NAME_CHARS
;
215 if (ok_name_char
[*name_chars
] == 0) {
217 for (p
= name_chars
; *p
; p
++)
218 ok_name_char
[*p
] = 1;
221 if (ok_name_char
[*s
++] == 0)
228 intern_strings(PyObject
*tuple
)
232 for (i
= PyTuple_GET_SIZE(tuple
); --i
>= 0; ) {
233 PyObject
*v
= PyTuple_GET_ITEM(tuple
, i
);
234 if (v
== NULL
|| !PyString_Check(v
)) {
235 Py_FatalError("non-string found in code slot");
236 PyErr_BadInternalCall();
239 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple
, i
));
245 PyCode_New(int argcount
, int nlocals
, int stacksize
, int flags
,
246 PyObject
*code
, PyObject
*consts
, PyObject
*names
,
247 PyObject
*varnames
, PyObject
*freevars
, PyObject
*cellvars
,
248 PyObject
*filename
, PyObject
*name
, int firstlineno
,
254 /* Check argument types */
255 if (argcount
< 0 || nlocals
< 0 ||
257 consts
== NULL
|| !PyTuple_Check(consts
) ||
258 names
== NULL
|| !PyTuple_Check(names
) ||
259 varnames
== NULL
|| !PyTuple_Check(varnames
) ||
260 freevars
== NULL
|| !PyTuple_Check(freevars
) ||
261 cellvars
== NULL
|| !PyTuple_Check(cellvars
) ||
262 name
== NULL
|| !PyString_Check(name
) ||
263 filename
== NULL
|| !PyString_Check(filename
) ||
264 lnotab
== NULL
|| !PyString_Check(lnotab
)) {
265 PyErr_BadInternalCall();
268 pb
= code
->ob_type
->tp_as_buffer
;
270 pb
->bf_getreadbuffer
== NULL
||
271 pb
->bf_getsegcount
== NULL
||
272 (*pb
->bf_getsegcount
)(code
, NULL
) != 1)
274 PyErr_BadInternalCall();
277 intern_strings(names
);
278 intern_strings(varnames
);
279 if (freevars
== NULL
)
280 freevars
= PyTuple_New(0);
281 intern_strings(freevars
);
282 if (cellvars
== NULL
)
283 cellvars
= PyTuple_New(0);
284 intern_strings(cellvars
);
285 /* Intern selected string constants */
286 for (i
= PyTuple_Size(consts
); --i
>= 0; ) {
287 PyObject
*v
= PyTuple_GetItem(consts
, i
);
288 if (!PyString_Check(v
))
290 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v
)))
292 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
294 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
296 co
->co_argcount
= argcount
;
297 co
->co_nlocals
= nlocals
;
298 co
->co_stacksize
= stacksize
;
299 co
->co_flags
= flags
;
303 co
->co_consts
= consts
;
305 co
->co_names
= names
;
307 co
->co_varnames
= varnames
;
309 co
->co_freevars
= freevars
;
311 co
->co_cellvars
= cellvars
;
313 co
->co_filename
= filename
;
316 co
->co_firstlineno
= firstlineno
;
318 co
->co_lnotab
= lnotab
;
324 /* Data structure used internally */
326 /* The compiler uses two passes to generate bytecodes. The first pass
327 builds the symbol table. The second pass generates the bytecode.
329 The first pass uses a single symtable struct. The second pass uses
330 a compiling struct for each code block. The compiling structs
331 share a reference to the symtable.
333 The two passes communicate via symtable_load_symbols() and via
334 is_local() and is_global(). The former initializes several slots
335 in the compiling struct: c_varnames, c_locals, c_nlocals,
336 c_argcount, c_globals, and c_flags.
340 PyObject
*c_code
; /* string */
341 PyObject
*c_consts
; /* list of objects */
342 PyObject
*c_const_dict
; /* inverse of c_consts */
343 PyObject
*c_names
; /* list of strings (names) */
344 PyObject
*c_name_dict
; /* inverse of c_names */
345 PyObject
*c_globals
; /* dictionary (value=None) */
346 PyObject
*c_locals
; /* dictionary (value=localID) */
347 PyObject
*c_varnames
; /* list (inverse of c_locals) */
348 PyObject
*c_freevars
; /* dictionary (value=None) */
349 PyObject
*c_cellvars
; /* list */
350 int c_nlocals
; /* index of next local */
351 int c_argcount
; /* number of top-level arguments */
352 int c_flags
; /* same as co_flags */
353 int c_nexti
; /* index into c_code */
354 int c_errors
; /* counts errors occurred */
355 int c_infunction
; /* set when compiling a function */
356 int c_interactive
; /* generating code for interactive command */
357 int c_loops
; /* counts nested loops */
358 int c_begin
; /* begin of current loop, for 'continue' */
359 int c_block
[CO_MAXBLOCKS
]; /* stack of block types */
360 int c_nblocks
; /* current block stack level */
361 char *c_filename
; /* filename of current node */
362 char *c_name
; /* name of object (e.g. function) */
363 int c_lineno
; /* Current line number */
364 int c_stacklevel
; /* Current stack level */
365 int c_maxstacklevel
; /* Maximum stack level */
367 PyObject
*c_lnotab
; /* Table mapping address to line number */
368 int c_last_addr
, c_last_line
, c_lnotab_next
;
369 char *c_private
; /* for private name mangling */
370 int c_tmpname
; /* temporary local name counter */
371 int c_nested
; /* Is block nested funcdef or lamdef? */
372 int c_closure
; /* Is nested w/freevars? */
373 struct symtable
*c_symtable
; /* pointer to module symbol table */
374 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
379 if ((v
& (USE
| DEF_FREE
))
380 && !(v
& (DEF_LOCAL
| DEF_PARAM
| DEF_GLOBAL
)))
382 if (v
& DEF_FREE_CLASS
)
388 com_error(struct compiling
*c
, PyObject
*exc
, char *msg
)
390 PyObject
*t
= NULL
, *v
= NULL
, *w
= NULL
, *line
= NULL
;
393 /* Error occurred via symtable call to
395 PyErr_SetString(exc
, msg
);
399 if (c
->c_lineno
< 1 || c
->c_interactive
) {
400 /* Unknown line number or interactive input */
401 PyErr_SetString(exc
, msg
);
404 v
= PyString_FromString(msg
);
406 return; /* MemoryError, too bad */
408 line
= PyErr_ProgramText(c
->c_filename
, c
->c_lineno
);
413 t
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->c_lineno
,
417 w
= Py_BuildValue("(OO)", v
, t
);
420 PyErr_SetObject(exc
, w
);
428 /* Interface to the block stack */
431 block_push(struct compiling
*c
, int type
)
433 if (c
->c_nblocks
>= CO_MAXBLOCKS
) {
434 com_error(c
, PyExc_SystemError
,
435 "too many statically nested blocks");
438 c
->c_block
[c
->c_nblocks
++] = type
;
443 block_pop(struct compiling
*c
, int type
)
445 if (c
->c_nblocks
> 0)
447 if (c
->c_block
[c
->c_nblocks
] != type
&& c
->c_errors
== 0) {
448 com_error(c
, PyExc_SystemError
, "bad block pop");
452 /* Prototype forward declarations */
454 static int com_init(struct compiling
*, char *);
455 static void com_free(struct compiling
*);
456 static void com_push(struct compiling
*, int);
457 static void com_pop(struct compiling
*, int);
458 static void com_done(struct compiling
*);
459 static void com_node(struct compiling
*, node
*);
460 static void com_factor(struct compiling
*, node
*);
461 static void com_addbyte(struct compiling
*, int);
462 static void com_addint(struct compiling
*, int);
463 static void com_addoparg(struct compiling
*, int, int);
464 static void com_addfwref(struct compiling
*, int, int *);
465 static void com_backpatch(struct compiling
*, int);
466 static int com_add(struct compiling
*, PyObject
*, PyObject
*, PyObject
*);
467 static int com_addconst(struct compiling
*, PyObject
*);
468 static int com_addname(struct compiling
*, PyObject
*);
469 static void com_addopname(struct compiling
*, int, node
*);
470 static void com_list(struct compiling
*, node
*, int);
471 static void com_list_iter(struct compiling
*, node
*, node
*, char *);
472 static int com_argdefs(struct compiling
*, node
*);
473 static void com_assign(struct compiling
*, node
*, int, node
*);
474 static void com_assign_name(struct compiling
*, node
*, int);
475 static PyCodeObject
*icompile(node
*, struct compiling
*);
476 static PyCodeObject
*jcompile(node
*, char *, struct compiling
*,
478 static PyObject
*parsestrplus(node
*);
479 static PyObject
*parsestr(char *);
480 static node
*get_rawdocstring(node
*);
482 static int get_ref_type(struct compiling
*, char *);
484 /* symtable operations */
485 static int symtable_build(struct compiling
*, node
*);
486 static int symtable_load_symbols(struct compiling
*);
487 static struct symtable
*symtable_init(void);
488 static void symtable_enter_scope(struct symtable
*, char *, int, int);
489 static int symtable_exit_scope(struct symtable
*);
490 static int symtable_add_def(struct symtable
*, char *, int);
491 static int symtable_add_def_o(struct symtable
*, PyObject
*, PyObject
*, int);
493 static void symtable_node(struct symtable
*, node
*);
494 static void symtable_funcdef(struct symtable
*, node
*);
495 static void symtable_default_args(struct symtable
*, node
*);
496 static void symtable_params(struct symtable
*, node
*);
497 static void symtable_params_fplist(struct symtable
*, node
*n
);
498 static void symtable_global(struct symtable
*, node
*);
499 static void symtable_import(struct symtable
*, node
*);
500 static void symtable_assign(struct symtable
*, node
*, int);
501 static void symtable_list_comprehension(struct symtable
*, node
*);
503 static int symtable_update_free_vars(struct symtable
*);
504 static int symtable_undo_free(struct symtable
*, PyObject
*, PyObject
*);
505 static int symtable_check_global(struct symtable
*, PyObject
*, PyObject
*);
512 for (i
= 0; i
< pad
; ++i
)
513 fprintf(stderr
, " ");
517 dump(node
*n
, int pad
, int depth
)
523 fprintf(stderr
, "%d: %s\n", TYPE(n
), STR(n
));
526 for (i
= 0; i
< NCH(n
); ++i
)
527 dump(CHILD(n
, i
), pad
+ 1, depth
);
530 #define DUMP(N) dump(N, 0, -1)
533 com_init(struct compiling
*c
, char *filename
)
535 memset((void *)c
, '\0', sizeof(struct compiling
));
536 if ((c
->c_code
= PyString_FromStringAndSize((char *)NULL
,
539 if ((c
->c_consts
= PyList_New(0)) == NULL
)
541 if ((c
->c_const_dict
= PyDict_New()) == NULL
)
543 if ((c
->c_names
= PyList_New(0)) == NULL
)
545 if ((c
->c_name_dict
= PyDict_New()) == NULL
)
547 if ((c
->c_locals
= PyDict_New()) == NULL
)
549 if ((c
->c_lnotab
= PyString_FromStringAndSize((char *)NULL
,
553 c
->c_varnames
= NULL
;
554 c
->c_freevars
= NULL
;
555 c
->c_cellvars
= NULL
;
562 c
->c_interactive
= 0;
566 c
->c_filename
= filename
;
570 c
->c_maxstacklevel
= 0;
571 c
->c_firstlineno
= 0;
574 c
->c_lnotab_next
= 0;
578 c
->c_symtable
= NULL
;
587 com_free(struct compiling
*c
)
589 Py_XDECREF(c
->c_code
);
590 Py_XDECREF(c
->c_consts
);
591 Py_XDECREF(c
->c_const_dict
);
592 Py_XDECREF(c
->c_names
);
593 Py_XDECREF(c
->c_name_dict
);
594 Py_XDECREF(c
->c_globals
);
595 Py_XDECREF(c
->c_locals
);
596 Py_XDECREF(c
->c_varnames
);
597 Py_XDECREF(c
->c_freevars
);
598 Py_XDECREF(c
->c_cellvars
);
599 Py_XDECREF(c
->c_lnotab
);
601 PyMem_Free((void *)c
->c_future
);
605 com_push(struct compiling
*c
, int n
)
607 c
->c_stacklevel
+= n
;
608 if (c
->c_stacklevel
> c
->c_maxstacklevel
)
609 c
->c_maxstacklevel
= c
->c_stacklevel
;
613 com_pop(struct compiling
*c
, int n
)
615 if (c
->c_stacklevel
< n
) {
617 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
618 c->c_filename, c->c_lineno,
619 c->c_nexti, c->c_stacklevel, n); */
623 c
->c_stacklevel
-= n
;
627 com_done(struct compiling
*c
)
629 if (c
->c_code
!= NULL
)
630 _PyString_Resize(&c
->c_code
, c
->c_nexti
);
631 if (c
->c_lnotab
!= NULL
)
632 _PyString_Resize(&c
->c_lnotab
, c
->c_lnotab_next
);
636 com_addbyte(struct compiling
*c
, int byte
)
639 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
640 assert(byte
>= 0 && byte
<= 255);
641 if (byte
< 0 || byte
> 255) {
642 com_error(c
, PyExc_SystemError
,
643 "com_addbyte: byte out of range");
645 if (c
->c_code
== NULL
)
647 len
= PyString_Size(c
->c_code
);
648 if (c
->c_nexti
>= len
) {
649 if (_PyString_Resize(&c
->c_code
, len
+1000) != 0) {
654 PyString_AsString(c
->c_code
)[c
->c_nexti
++] = byte
;
658 com_addint(struct compiling
*c
, int x
)
660 com_addbyte(c
, x
& 0xff);
661 com_addbyte(c
, x
>> 8); /* XXX x should be positive */
665 com_add_lnotab(struct compiling
*c
, int addr
, int line
)
669 if (c
->c_lnotab
== NULL
)
671 size
= PyString_Size(c
->c_lnotab
);
672 if (c
->c_lnotab_next
+2 > size
) {
673 if (_PyString_Resize(&c
->c_lnotab
, size
+ 1000) < 0) {
678 p
= PyString_AsString(c
->c_lnotab
) + c
->c_lnotab_next
;
681 c
->c_lnotab_next
+= 2;
685 com_set_lineno(struct compiling
*c
, int lineno
)
687 c
->c_lineno
= lineno
;
688 if (c
->c_firstlineno
== 0) {
689 c
->c_firstlineno
= c
->c_last_line
= lineno
;
692 int incr_addr
= c
->c_nexti
- c
->c_last_addr
;
693 int incr_line
= lineno
- c
->c_last_line
;
694 while (incr_addr
> 0 || incr_line
> 0) {
695 int trunc_addr
= incr_addr
;
696 int trunc_line
= incr_line
;
697 if (trunc_addr
> 255)
699 if (trunc_line
> 255)
701 com_add_lnotab(c
, trunc_addr
, trunc_line
);
702 incr_addr
-= trunc_addr
;
703 incr_line
-= trunc_line
;
705 c
->c_last_addr
= c
->c_nexti
;
706 c
->c_last_line
= lineno
;
711 com_addoparg(struct compiling
*c
, int op
, int arg
)
713 int extended_arg
= arg
>> 16;
714 if (op
== SET_LINENO
) {
715 com_set_lineno(c
, arg
);
720 com_addbyte(c
, EXTENDED_ARG
);
721 com_addint(c
, extended_arg
);
729 com_addfwref(struct compiling
*c
, int op
, int *p_anchor
)
731 /* Compile a forward reference for backpatching */
738 com_addint(c
, anchor
== 0 ? 0 : here
- anchor
);
742 com_backpatch(struct compiling
*c
, int anchor
)
744 unsigned char *code
= (unsigned char *) PyString_AsString(c
->c_code
);
745 int target
= c
->c_nexti
;
749 /* Make the JUMP instruction at anchor point to target */
750 prev
= code
[anchor
] + (code
[anchor
+1] << 8);
751 dist
= target
- (anchor
+2);
752 code
[anchor
] = dist
& 0xff;
754 code
[anchor
+1] = dist
;
757 com_error(c
, PyExc_SystemError
,
758 "com_backpatch: offset too large");
767 /* Handle literals and names uniformly */
770 com_add(struct compiling
*c
, PyObject
*list
, PyObject
*dict
, PyObject
*v
)
772 PyObject
*w
, *t
, *np
=NULL
;
775 t
= Py_BuildValue("(OO)", v
, v
->ob_type
);
778 w
= PyDict_GetItem(dict
, t
);
782 n
= PyList_Size(list
);
783 np
= PyInt_FromLong(n
);
786 if (PyList_Append(list
, v
) != 0)
788 if (PyDict_SetItem(dict
, t
, np
) != 0)
802 com_addconst(struct compiling
*c
, PyObject
*v
)
804 return com_add(c
, c
->c_consts
, c
->c_const_dict
, v
);
808 com_addname(struct compiling
*c
, PyObject
*v
)
810 return com_add(c
, c
->c_names
, c
->c_name_dict
, v
);
814 mangle(char *p
, char *name
, char *buffer
, size_t maxlen
)
816 /* Name mangling: __private becomes _classname__private.
817 This is independent from how the name is used. */
819 if (p
== NULL
|| name
== NULL
|| name
[0] != '_' || name
[1] != '_')
822 if (nlen
+2 >= maxlen
)
823 return 0; /* Don't mangle __extremely_long_names */
824 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
825 return 0; /* Don't mangle __whatever__ */
826 /* Strip leading underscores from class name */
830 return 0; /* Don't mangle if class is just underscores */
832 if (plen
+ nlen
>= maxlen
)
833 plen
= maxlen
-nlen
-2; /* Truncate class name if too long */
834 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
836 strncpy(buffer
+1, p
, plen
);
837 strcpy(buffer
+1+plen
, name
);
842 com_addop_name(struct compiling
*c
, int op
, char *name
)
846 char buffer
[MANGLE_LEN
];
848 if (mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
850 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
855 i
= com_addname(c
, v
);
858 com_addoparg(c
, op
, i
);
862 #define NAME_GLOBAL 1
863 #define NAME_DEFAULT 2
864 #define NAME_CLOSURE 3
867 com_lookup_arg(PyObject
*dict
, PyObject
*name
)
869 PyObject
*v
= PyDict_GetItem(dict
, name
);
873 return PyInt_AS_LONG(v
);
877 com_addop_varname(struct compiling
*c
, int kind
, char *name
)
881 int scope
= NAME_DEFAULT
;
883 char buffer
[MANGLE_LEN
];
885 if (mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
887 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
893 reftype
= get_ref_type(c
, name
);
896 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
)
899 case GLOBAL_EXPLICIT
:
902 case GLOBAL_IMPLICIT
:
903 if (c
->c_flags
& CO_OPTIMIZED
)
908 scope
= NAME_CLOSURE
;
912 i
= com_addname(c
, v
);
913 if (scope
== NAME_LOCAL
)
914 i
= com_lookup_arg(c
->c_locals
, v
);
915 else if (reftype
== FREE
)
916 i
= com_lookup_arg(c
->c_freevars
, v
);
917 else if (reftype
== CELL
)
918 i
= com_lookup_arg(c
->c_cellvars
, v
);
920 c
->c_errors
++; /* XXX no exception set */
972 sprintf(buf
, DEL_CLOSURE_ERROR
, name
);
973 com_error(c
, PyExc_SyntaxError
, buf
);
981 com_addoparg(c
, op
, i
);
985 com_addopname(struct compiling
*c
, int op
, node
*n
)
989 /* XXX it is possible to write this code without the 1000
990 chars on the total length of dotted names, I just can't be
991 bothered right now */
994 else if (TYPE(n
) == dotted_name
) {
998 for (i
= 0; i
< NCH(n
); i
+= 2) {
999 char *s
= STR(CHILD(n
, i
));
1000 if (p
+ strlen(s
) > buffer
+ (sizeof buffer
) - 2) {
1001 com_error(c
, PyExc_MemoryError
,
1002 "dotted_name too long");
1009 p
= strchr(p
, '\0');
1016 com_addop_name(c
, op
, name
);
1020 parsenumber(struct compiling
*co
, char *s
)
1025 #ifndef WITHOUT_COMPLEX
1031 end
= s
+ strlen(s
) - 1;
1032 #ifndef WITHOUT_COMPLEX
1033 imflag
= *end
== 'j' || *end
== 'J';
1035 if (*end
== 'l' || *end
== 'L')
1036 return PyLong_FromString(s
, (char **)0, 0);
1038 x
= (long) PyOS_strtoul(s
, &end
, 0);
1040 x
= PyOS_strtol(s
, &end
, 0);
1043 com_error(co
, PyExc_OverflowError
,
1044 "integer literal too large");
1047 return PyInt_FromLong(x
);
1049 /* XXX Huge floats may silently fail */
1050 #ifndef WITHOUT_COMPLEX
1053 PyFPE_START_PROTECT("atof", return 0)
1055 PyFPE_END_PROTECT(c
)
1056 return PyComplex_FromCComplex(c
);
1061 PyFPE_START_PROTECT("atof", return 0)
1063 PyFPE_END_PROTECT(dx
)
1064 return PyFloat_FromDouble(dx
);
1081 if (isalpha(quote
) || quote
== '_') {
1082 if (quote
== 'u' || quote
== 'U') {
1086 if (quote
== 'r' || quote
== 'R') {
1091 if (quote
!= '\'' && quote
!= '\"') {
1092 PyErr_BadInternalCall();
1097 if (len
> INT_MAX
) {
1098 PyErr_SetString(PyExc_OverflowError
, "string to parse is too long");
1101 if (s
[--len
] != quote
) {
1102 PyErr_BadInternalCall();
1105 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
1108 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
1109 PyErr_BadInternalCall();
1113 if (unicode
|| Py_UnicodeFlag
) {
1115 return PyUnicode_DecodeRawUnicodeEscape(
1118 return PyUnicode_DecodeUnicodeEscape(
1121 if (rawmode
|| strchr(s
, '\\') == NULL
)
1122 return PyString_FromStringAndSize(s
, len
);
1123 v
= PyString_FromStringAndSize((char *)NULL
, len
);
1126 p
= buf
= PyString_AsString(v
);
1135 /* XXX This assumes ASCII! */
1137 case '\\': *p
++ = '\\'; break;
1138 case '\'': *p
++ = '\''; break;
1139 case '\"': *p
++ = '\"'; break;
1140 case 'b': *p
++ = '\b'; break;
1141 case 'f': *p
++ = '\014'; break; /* FF */
1142 case 't': *p
++ = '\t'; break;
1143 case 'n': *p
++ = '\n'; break;
1144 case 'r': *p
++ = '\r'; break;
1145 case 'v': *p
++ = '\013'; break; /* VT */
1146 case 'a': *p
++ = '\007'; break; /* BEL, not classic C */
1147 case '0': case '1': case '2': case '3':
1148 case '4': case '5': case '6': case '7':
1150 if ('0' <= *s
&& *s
<= '7') {
1151 c
= (c
<<3) + *s
++ - '0';
1152 if ('0' <= *s
&& *s
<= '7')
1153 c
= (c
<<3) + *s
++ - '0';
1158 if (isxdigit(Py_CHARMASK(s
[0]))
1159 && isxdigit(Py_CHARMASK(s
[1]))) {
1161 c
= Py_CHARMASK(*s
);
1165 else if (islower(c
))
1170 c
= Py_CHARMASK(*s
);
1174 else if (islower(c
))
1181 PyErr_SetString(PyExc_ValueError
,
1182 "invalid \\x escape");
1191 _PyString_Resize(&v
, (int)(p
- buf
));
1196 parsestrplus(node
*n
)
1200 REQ(CHILD(n
, 0), STRING
);
1201 if ((v
= parsestr(STR(CHILD(n
, 0)))) != NULL
) {
1202 /* String literal concatenation */
1203 for (i
= 1; i
< NCH(n
); i
++) {
1205 s
= parsestr(STR(CHILD(n
, i
)));
1208 if (PyString_Check(v
) && PyString_Check(s
)) {
1209 PyString_ConcatAndDel(&v
, s
);
1215 temp
= PyUnicode_Concat(v
, s
);
1232 com_list_for(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1236 int save_begin
= c
->c_begin
;
1238 /* list_iter: for v in expr [list_iter] */
1239 com_node(c
, CHILD(n
, 3)); /* expr */
1240 v
= PyInt_FromLong(0L);
1243 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1246 c
->c_begin
= c
->c_nexti
;
1247 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1248 com_addfwref(c
, FOR_LOOP
, &anchor
);
1250 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
1252 com_list_iter(c
, n
, e
, t
);
1254 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
1255 c
->c_begin
= save_begin
;
1256 com_backpatch(c
, anchor
);
1257 com_pop(c
, 2); /* FOR_LOOP has popped these */
1261 com_list_if(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1265 /* list_iter: 'if' test [list_iter] */
1266 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1267 com_node(c
, CHILD(n
, 1));
1268 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
1269 com_addbyte(c
, POP_TOP
);
1271 com_list_iter(c
, n
, e
, t
);
1272 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
1273 com_backpatch(c
, a
);
1274 /* We jump here with an extra entry which we now pop */
1275 com_addbyte(c
, POP_TOP
);
1276 com_backpatch(c
, anchor
);
1280 com_list_iter(struct compiling
*c
,
1281 node
*p
, /* parent of list_iter node */
1282 node
*e
, /* element expression node */
1283 char *t
/* name of result list temp local */)
1285 /* list_iter is the last child in a listmaker, list_for, or list_if */
1286 node
*n
= CHILD(p
, NCH(p
)-1);
1287 if (TYPE(n
) == list_iter
) {
1291 com_list_for(c
, n
, e
, t
);
1294 com_list_if(c
, n
, e
, t
);
1297 com_error(c
, PyExc_SystemError
,
1298 "invalid list_iter node type");
1302 com_addop_varname(c
, VAR_LOAD
, t
);
1305 com_addoparg(c
, CALL_FUNCTION
, 1);
1306 com_addbyte(c
, POP_TOP
);
1312 com_list_comprehension(struct compiling
*c
, node
*n
)
1314 /* listmaker: test list_for */
1316 sprintf(tmpname
, "_[%d]", ++c
->c_tmpname
);
1317 com_addoparg(c
, BUILD_LIST
, 0);
1318 com_addbyte(c
, DUP_TOP
); /* leave the result on the stack */
1320 com_addop_name(c
, LOAD_ATTR
, "append");
1321 com_addop_varname(c
, VAR_STORE
, tmpname
);
1323 com_list_for(c
, CHILD(n
, 1), CHILD(n
, 0), tmpname
);
1324 com_addop_varname(c
, VAR_DELETE
, tmpname
);
1329 com_listmaker(struct compiling
*c
, node
*n
)
1331 /* listmaker: test ( list_for | (',' test)* [','] ) */
1332 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
)
1333 com_list_comprehension(c
, n
);
1337 for (i
= 0; i
< NCH(n
); i
+= 2, len
++)
1338 com_node(c
, CHILD(n
, i
));
1339 com_addoparg(c
, BUILD_LIST
, len
);
1345 com_dictmaker(struct compiling
*c
, node
*n
)
1348 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1349 for (i
= 0; i
+2 < NCH(n
); i
+= 4) {
1350 /* We must arrange things just right for STORE_SUBSCR.
1351 It wants the stack to look like (value) (dict) (key) */
1352 com_addbyte(c
, DUP_TOP
);
1354 com_node(c
, CHILD(n
, i
+2)); /* value */
1355 com_addbyte(c
, ROT_TWO
);
1356 com_node(c
, CHILD(n
, i
)); /* key */
1357 com_addbyte(c
, STORE_SUBSCR
);
1363 com_atom(struct compiling
*c
, node
*n
)
1372 if (TYPE(CHILD(n
, 1)) == RPAR
) {
1373 com_addoparg(c
, BUILD_TUPLE
, 0);
1377 com_node(c
, CHILD(n
, 1));
1379 case LSQB
: /* '[' [listmaker] ']' */
1380 if (TYPE(CHILD(n
, 1)) == RSQB
) {
1381 com_addoparg(c
, BUILD_LIST
, 0);
1385 com_listmaker(c
, CHILD(n
, 1));
1387 case LBRACE
: /* '{' [dictmaker] '}' */
1388 com_addoparg(c
, BUILD_MAP
, 0);
1390 if (TYPE(CHILD(n
, 1)) == dictmaker
)
1391 com_dictmaker(c
, CHILD(n
, 1));
1394 com_node(c
, CHILD(n
, 1));
1395 com_addbyte(c
, UNARY_CONVERT
);
1398 if ((v
= parsenumber(c
, STR(ch
))) == NULL
) {
1402 i
= com_addconst(c
, v
);
1405 com_addoparg(c
, LOAD_CONST
, i
);
1409 v
= parsestrplus(n
);
1415 i
= com_addconst(c
, v
);
1418 com_addoparg(c
, LOAD_CONST
, i
);
1422 com_addop_varname(c
, VAR_LOAD
, STR(ch
));
1426 com_error(c
, PyExc_SystemError
,
1427 "com_atom: unexpected node type");
1432 com_slice(struct compiling
*c
, node
*n
, int op
)
1437 else if (NCH(n
) == 2) {
1438 if (TYPE(CHILD(n
, 0)) != COLON
) {
1439 com_node(c
, CHILD(n
, 0));
1440 com_addbyte(c
, op
+1);
1443 com_node(c
, CHILD(n
, 1));
1444 com_addbyte(c
, op
+2);
1449 com_node(c
, CHILD(n
, 0));
1450 com_node(c
, CHILD(n
, 2));
1451 com_addbyte(c
, op
+3);
1457 com_augassign_slice(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
1460 com_addbyte(c
, DUP_TOP
);
1462 com_addbyte(c
, SLICE
);
1464 com_addbyte(c
, opcode
);
1466 com_addbyte(c
, ROT_TWO
);
1467 com_addbyte(c
, STORE_SLICE
);
1469 } else if (NCH(n
) == 2 && TYPE(CHILD(n
, 0)) != COLON
) {
1470 com_node(c
, CHILD(n
, 0));
1471 com_addoparg(c
, DUP_TOPX
, 2);
1473 com_addbyte(c
, SLICE
+1);
1476 com_addbyte(c
, opcode
);
1478 com_addbyte(c
, ROT_THREE
);
1479 com_addbyte(c
, STORE_SLICE
+1);
1481 } else if (NCH(n
) == 2) {
1482 com_node(c
, CHILD(n
, 1));
1483 com_addoparg(c
, DUP_TOPX
, 2);
1485 com_addbyte(c
, SLICE
+2);
1488 com_addbyte(c
, opcode
);
1490 com_addbyte(c
, ROT_THREE
);
1491 com_addbyte(c
, STORE_SLICE
+2);
1494 com_node(c
, CHILD(n
, 0));
1495 com_node(c
, CHILD(n
, 2));
1496 com_addoparg(c
, DUP_TOPX
, 3);
1498 com_addbyte(c
, SLICE
+3);
1501 com_addbyte(c
, opcode
);
1503 com_addbyte(c
, ROT_FOUR
);
1504 com_addbyte(c
, STORE_SLICE
+3);
1510 com_argument(struct compiling
*c
, node
*n
, PyObject
**pkeywords
)
1513 REQ(n
, argument
); /* [test '='] test; really [keyword '='] test */
1515 if (*pkeywords
!= NULL
) {
1516 com_error(c
, PyExc_SyntaxError
,
1517 "non-keyword arg after keyword arg");
1520 com_node(c
, CHILD(n
, 0));
1527 } while (NCH(m
) == 1);
1528 if (TYPE(m
) != NAME
) {
1529 /* f(lambda x: x[0] = 3) ends up getting parsed with
1530 * LHS test = lambda x: x[0], and RHS test = 3.
1531 * SF bug 132313 points out that complaining about a keyword
1532 * then is very confusing.
1534 com_error(c
, PyExc_SyntaxError
,
1535 TYPE(m
) == lambdef
?
1536 "lambda cannot contain assignment" :
1537 "keyword can't be an expression");
1540 PyObject
*v
= PyString_InternFromString(STR(m
));
1541 if (v
!= NULL
&& *pkeywords
== NULL
)
1542 *pkeywords
= PyDict_New();
1545 else if (*pkeywords
== NULL
) {
1549 if (PyDict_GetItem(*pkeywords
, v
) != NULL
)
1550 com_error(c
, PyExc_SyntaxError
,
1551 "duplicate keyword argument");
1553 if (PyDict_SetItem(*pkeywords
, v
, v
) != 0)
1555 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1560 com_node(c
, CHILD(n
, 2));
1564 com_call_function(struct compiling
*c
, node
*n
)
1566 if (TYPE(n
) == RPAR
) {
1567 com_addoparg(c
, CALL_FUNCTION
, 0);
1570 PyObject
*keywords
= NULL
;
1572 int lineno
= n
->n_lineno
;
1574 int starstar_flag
= 0;
1579 for (i
= 0; i
< NCH(n
); i
+= 2) {
1580 node
*ch
= CHILD(n
, i
);
1581 if (TYPE(ch
) == STAR
||
1582 TYPE(ch
) == DOUBLESTAR
)
1584 if (ch
->n_lineno
!= lineno
) {
1585 lineno
= ch
->n_lineno
;
1586 com_addoparg(c
, SET_LINENO
, lineno
);
1588 com_argument(c
, ch
, &keywords
);
1589 if (keywords
== NULL
)
1594 Py_XDECREF(keywords
);
1595 while (i
< NCH(n
)) {
1596 node
*tok
= CHILD(n
, i
);
1597 node
*ch
= CHILD(n
, i
+1);
1599 switch (TYPE(tok
)) {
1600 case STAR
: star_flag
= 1; break;
1601 case DOUBLESTAR
: starstar_flag
= 1; break;
1605 if (na
> 255 || nk
> 255) {
1606 com_error(c
, PyExc_SyntaxError
,
1607 "more than 255 arguments");
1609 if (star_flag
|| starstar_flag
)
1610 opcode
= CALL_FUNCTION_VAR
- 1 +
1611 star_flag
+ (starstar_flag
<< 1);
1613 opcode
= CALL_FUNCTION
;
1614 com_addoparg(c
, opcode
, na
| (nk
<< 8));
1615 com_pop(c
, na
+ 2*nk
+ star_flag
+ starstar_flag
);
1620 com_select_member(struct compiling
*c
, node
*n
)
1622 com_addopname(c
, LOAD_ATTR
, n
);
1626 com_sliceobj(struct compiling
*c
, node
*n
)
1629 int ns
=2; /* number of slice arguments */
1632 /* first argument */
1633 if (TYPE(CHILD(n
,i
)) == COLON
) {
1634 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1639 com_node(c
, CHILD(n
,i
));
1641 REQ(CHILD(n
,i
),COLON
);
1644 /* second argument */
1645 if (i
< NCH(n
) && TYPE(CHILD(n
,i
)) == test
) {
1646 com_node(c
, CHILD(n
,i
));
1650 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1653 /* remaining arguments */
1654 for (; i
< NCH(n
); i
++) {
1659 /* right argument of ':' missing */
1660 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1664 com_node(c
, CHILD(ch
,1));
1666 com_addoparg(c
, BUILD_SLICE
, ns
);
1667 com_pop(c
, 1 + (ns
== 3));
1671 com_subscript(struct compiling
*c
, node
*n
)
1676 /* check for rubber index */
1677 if (TYPE(ch
) == DOT
&& TYPE(CHILD(n
,1)) == DOT
) {
1678 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_Ellipsis
));
1682 /* check for slice */
1683 if ((TYPE(ch
) == COLON
|| NCH(n
) > 1))
1693 com_subscriptlist(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
1696 REQ(n
, subscriptlist
);
1697 /* Check to make backward compatible slice behavior for '[i:j]' */
1699 node
*sub
= CHILD(n
, 0); /* subscript */
1700 /* 'Basic' slice, should have exactly one colon. */
1701 if ((TYPE(CHILD(sub
, 0)) == COLON
1702 || (NCH(sub
) > 1 && TYPE(CHILD(sub
, 1)) == COLON
))
1703 && (TYPE(CHILD(sub
,NCH(sub
)-1)) != sliceop
))
1705 switch (assigning
) {
1716 com_augassign_slice(c
, sub
, assigning
, augn
);
1719 com_slice(c
, sub
, op
);
1720 if (op
== STORE_SLICE
)
1722 else if (op
== DELETE_SLICE
)
1727 /* Else normal subscriptlist. Compile each subscript. */
1728 for (i
= 0; i
< NCH(n
); i
+= 2)
1729 com_subscript(c
, CHILD(n
, i
));
1730 /* Put multiple subscripts into a tuple */
1733 com_addoparg(c
, BUILD_TUPLE
, i
);
1736 switch (assigning
) {
1751 if (assigning
> OP_APPLY
) {
1752 com_addoparg(c
, DUP_TOPX
, 2);
1754 com_addbyte(c
, BINARY_SUBSCR
);
1757 com_addbyte(c
, assigning
);
1759 com_addbyte(c
, ROT_THREE
);
1766 com_apply_trailer(struct compiling
*c
, node
*n
)
1769 switch (TYPE(CHILD(n
, 0))) {
1771 com_call_function(c
, CHILD(n
, 1));
1774 com_select_member(c
, CHILD(n
, 1));
1777 com_subscriptlist(c
, CHILD(n
, 1), OP_APPLY
, NULL
);
1780 com_error(c
, PyExc_SystemError
,
1781 "com_apply_trailer: unknown trailer type");
1786 com_power(struct compiling
*c
, node
*n
)
1790 com_atom(c
, CHILD(n
, 0));
1791 for (i
= 1; i
< NCH(n
); i
++) {
1792 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1793 com_factor(c
, CHILD(n
, i
+1));
1794 com_addbyte(c
, BINARY_POWER
);
1799 com_apply_trailer(c
, CHILD(n
, i
));
1804 com_factor(struct compiling
*c
, node
*n
)
1807 if (TYPE(CHILD(n
, 0)) == PLUS
) {
1808 com_factor(c
, CHILD(n
, 1));
1809 com_addbyte(c
, UNARY_POSITIVE
);
1811 else if (TYPE(CHILD(n
, 0)) == MINUS
) {
1812 com_factor(c
, CHILD(n
, 1));
1813 com_addbyte(c
, UNARY_NEGATIVE
);
1815 else if (TYPE(CHILD(n
, 0)) == TILDE
) {
1816 com_factor(c
, CHILD(n
, 1));
1817 com_addbyte(c
, UNARY_INVERT
);
1820 com_power(c
, CHILD(n
, 0));
1825 com_term(struct compiling
*c
, node
*n
)
1830 com_factor(c
, CHILD(n
, 0));
1831 for (i
= 2; i
< NCH(n
); i
+= 2) {
1832 com_factor(c
, CHILD(n
, i
));
1833 switch (TYPE(CHILD(n
, i
-1))) {
1835 op
= BINARY_MULTIPLY
;
1844 com_error(c
, PyExc_SystemError
,
1845 "com_term: operator not *, / or %");
1854 com_arith_expr(struct compiling
*c
, node
*n
)
1859 com_term(c
, CHILD(n
, 0));
1860 for (i
= 2; i
< NCH(n
); i
+= 2) {
1861 com_term(c
, CHILD(n
, i
));
1862 switch (TYPE(CHILD(n
, i
-1))) {
1867 op
= BINARY_SUBTRACT
;
1870 com_error(c
, PyExc_SystemError
,
1871 "com_arith_expr: operator not + or -");
1880 com_shift_expr(struct compiling
*c
, node
*n
)
1885 com_arith_expr(c
, CHILD(n
, 0));
1886 for (i
= 2; i
< NCH(n
); i
+= 2) {
1887 com_arith_expr(c
, CHILD(n
, i
));
1888 switch (TYPE(CHILD(n
, i
-1))) {
1896 com_error(c
, PyExc_SystemError
,
1897 "com_shift_expr: operator not << or >>");
1906 com_and_expr(struct compiling
*c
, node
*n
)
1911 com_shift_expr(c
, CHILD(n
, 0));
1912 for (i
= 2; i
< NCH(n
); i
+= 2) {
1913 com_shift_expr(c
, CHILD(n
, i
));
1914 if (TYPE(CHILD(n
, i
-1)) == AMPER
) {
1918 com_error(c
, PyExc_SystemError
,
1919 "com_and_expr: operator not &");
1928 com_xor_expr(struct compiling
*c
, node
*n
)
1933 com_and_expr(c
, CHILD(n
, 0));
1934 for (i
= 2; i
< NCH(n
); i
+= 2) {
1935 com_and_expr(c
, CHILD(n
, i
));
1936 if (TYPE(CHILD(n
, i
-1)) == CIRCUMFLEX
) {
1940 com_error(c
, PyExc_SystemError
,
1941 "com_xor_expr: operator not ^");
1950 com_expr(struct compiling
*c
, node
*n
)
1955 com_xor_expr(c
, CHILD(n
, 0));
1956 for (i
= 2; i
< NCH(n
); i
+= 2) {
1957 com_xor_expr(c
, CHILD(n
, i
));
1958 if (TYPE(CHILD(n
, i
-1)) == VBAR
) {
1962 com_error(c
, PyExc_SystemError
,
1963 "com_expr: expr operator not |");
1975 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1976 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1980 case LESS
: return LT
;
1981 case GREATER
: return GT
;
1982 case EQEQUAL
: /* == */
1983 case EQUAL
: return EQ
;
1984 case LESSEQUAL
: return LE
;
1985 case GREATEREQUAL
: return GE
;
1986 case NOTEQUAL
: return NE
; /* <> or != */
1987 case NAME
: if (strcmp(STR(n
), "in") == 0) return IN
;
1988 if (strcmp(STR(n
), "is") == 0) return IS
;
1991 else if (NCH(n
) == 2) {
1992 switch (TYPE(CHILD(n
, 0))) {
1993 case NAME
: if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
1995 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
2003 com_comparison(struct compiling
*c
, node
*n
)
2008 REQ(n
, comparison
); /* comparison: expr (comp_op expr)* */
2009 com_expr(c
, CHILD(n
, 0));
2013 /****************************************************************
2014 The following code is generated for all but the last
2015 comparison in a chain:
2017 label: on stack: opcode: jump to:
2023 b, 0-or-1 JUMP_IF_FALSE L1
2027 We are now ready to repeat this sequence for the next
2028 comparison in the chain.
2030 For the last we generate:
2036 If there were any jumps to L1 (i.e., there was more than one
2037 comparison), we generate:
2039 0-or-1 JUMP_FORWARD L2
2044 ****************************************************************/
2048 for (i
= 2; i
< NCH(n
); i
+= 2) {
2049 com_expr(c
, CHILD(n
, i
));
2051 com_addbyte(c
, DUP_TOP
);
2053 com_addbyte(c
, ROT_THREE
);
2055 op
= cmp_type(CHILD(n
, i
-1));
2057 com_error(c
, PyExc_SystemError
,
2058 "com_comparison: unknown comparison op");
2060 com_addoparg(c
, COMPARE_OP
, op
);
2063 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2064 com_addbyte(c
, POP_TOP
);
2071 com_addfwref(c
, JUMP_FORWARD
, &anchor2
);
2072 com_backpatch(c
, anchor
);
2073 com_addbyte(c
, ROT_TWO
);
2074 com_addbyte(c
, POP_TOP
);
2075 com_backpatch(c
, anchor2
);
2080 com_not_test(struct compiling
*c
, node
*n
)
2082 REQ(n
, not_test
); /* 'not' not_test | comparison */
2084 com_comparison(c
, CHILD(n
, 0));
2087 com_not_test(c
, CHILD(n
, 1));
2088 com_addbyte(c
, UNARY_NOT
);
2093 com_and_test(struct compiling
*c
, node
*n
)
2097 REQ(n
, and_test
); /* not_test ('and' not_test)* */
2101 com_not_test(c
, CHILD(n
, i
));
2102 if ((i
+= 2) >= NCH(n
))
2104 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2105 com_addbyte(c
, POP_TOP
);
2109 com_backpatch(c
, anchor
);
2113 com_make_closure(struct compiling
*c
, PyCodeObject
*co
)
2115 int i
, free
= PyTuple_GET_SIZE(co
->co_freevars
);
2116 /* If the code is compiled with st->st_nested_scopes == 0,
2117 then no variable will ever be added to co_freevars.
2121 for (i
= 0; i
< free
; ++i
) {
2122 /* Bypass com_addop_varname because it will generate
2123 LOAD_DEREF but LOAD_CLOSURE is needed.
2125 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
2128 /* Special case: If a class contains a method with a
2129 free variable that has the same name as a method,
2130 the name will be considered free *and* local in the
2131 class. It should be handled by the closure, as
2132 well as by the normal name loookup logic.
2134 reftype
= get_ref_type(c
, PyString_AS_STRING(name
));
2135 if (reftype
== CELL
)
2136 arg
= com_lookup_arg(c
->c_cellvars
, name
);
2137 else /* (reftype == FREE) */
2138 arg
= com_lookup_arg(c
->c_freevars
, name
);
2140 fprintf(stderr
, "lookup %s in %s %d %d\n"
2141 "freevars of %s: %s\n",
2142 PyObject_REPR(name
),
2145 PyString_AS_STRING(co
->co_name
),
2146 PyObject_REPR(co
->co_freevars
));
2147 Py_FatalError("com_make_closure()");
2149 com_addoparg(c
, LOAD_CLOSURE
, arg
);
2157 com_test(struct compiling
*c
, node
*n
)
2159 REQ(n
, test
); /* and_test ('or' and_test)* | lambdef */
2160 if (NCH(n
) == 1 && TYPE(CHILD(n
, 0)) == lambdef
) {
2163 int ndefs
= com_argdefs(c
, CHILD(n
, 0));
2164 symtable_enter_scope(c
->c_symtable
, "lambda", lambdef
,
2166 co
= (PyObject
*) icompile(CHILD(n
, 0), c
);
2171 symtable_exit_scope(c
->c_symtable
);
2172 i
= com_addconst(c
, co
);
2173 closure
= com_make_closure(c
, (PyCodeObject
*)co
);
2175 com_addoparg(c
, LOAD_CONST
, i
);
2178 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
2180 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
2187 com_and_test(c
, CHILD(n
, i
));
2188 if ((i
+= 2) >= NCH(n
))
2190 com_addfwref(c
, JUMP_IF_TRUE
, &anchor
);
2191 com_addbyte(c
, POP_TOP
);
2195 com_backpatch(c
, anchor
);
2200 com_list(struct compiling
*c
, node
*n
, int toplevel
)
2202 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2203 if (NCH(n
) == 1 && !toplevel
) {
2204 com_node(c
, CHILD(n
, 0));
2209 len
= (NCH(n
) + 1) / 2;
2210 for (i
= 0; i
< NCH(n
); i
+= 2)
2211 com_node(c
, CHILD(n
, i
));
2212 com_addoparg(c
, BUILD_TUPLE
, len
);
2218 /* Begin of assignment compilation */
2222 com_augassign_attr(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2224 com_addbyte(c
, DUP_TOP
);
2226 com_addopname(c
, LOAD_ATTR
, n
);
2228 com_addbyte(c
, opcode
);
2230 com_addbyte(c
, ROT_TWO
);
2231 com_addopname(c
, STORE_ATTR
, n
);
2236 com_assign_attr(struct compiling
*c
, node
*n
, int assigning
)
2238 com_addopname(c
, assigning
? STORE_ATTR
: DELETE_ATTR
, n
);
2239 com_pop(c
, assigning
? 2 : 1);
2243 com_assign_trailer(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2246 switch (TYPE(CHILD(n
, 0))) {
2247 case LPAR
: /* '(' [exprlist] ')' */
2248 com_error(c
, PyExc_SyntaxError
,
2249 "can't assign to function call");
2251 case DOT
: /* '.' NAME */
2252 if (assigning
> OP_APPLY
)
2253 com_augassign_attr(c
, CHILD(n
, 1), assigning
, augn
);
2255 com_assign_attr(c
, CHILD(n
, 1), assigning
);
2257 case LSQB
: /* '[' subscriptlist ']' */
2258 com_subscriptlist(c
, CHILD(n
, 1), assigning
, augn
);
2261 com_error(c
, PyExc_SystemError
, "unknown trailer type");
2266 com_assign_sequence(struct compiling
*c
, node
*n
, int assigning
)
2269 if (TYPE(n
) != testlist
&& TYPE(n
) != listmaker
)
2273 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
2276 for (i
= 0; i
< NCH(n
); i
+= 2)
2277 com_assign(c
, CHILD(n
, i
), assigning
, NULL
);
2281 com_augassign_name(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2284 com_addop_varname(c
, VAR_LOAD
, STR(n
));
2287 com_addbyte(c
, opcode
);
2289 com_assign_name(c
, n
, OP_ASSIGN
);
2293 com_assign_name(struct compiling
*c
, node
*n
, int assigning
)
2296 com_addop_varname(c
, assigning
? VAR_STORE
: VAR_DELETE
, STR(n
));
2302 com_assign(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2304 /* Loop to avoid trivial recursion */
2311 if (assigning
> OP_APPLY
) {
2312 com_error(c
, PyExc_SyntaxError
,
2313 "augmented assign to tuple not possible");
2316 com_assign_sequence(c
, n
, assigning
);
2334 com_error(c
, PyExc_SyntaxError
,
2335 "can't assign to operator");
2341 case power
: /* atom trailer* ('**' power)*
2342 ('+'|'-'|'~') factor | atom trailer* */
2343 if (TYPE(CHILD(n
, 0)) != atom
) {
2344 com_error(c
, PyExc_SyntaxError
,
2345 "can't assign to operator");
2348 if (NCH(n
) > 1) { /* trailer or exponent present */
2350 com_node(c
, CHILD(n
, 0));
2351 for (i
= 1; i
+1 < NCH(n
); i
++) {
2352 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
2353 com_error(c
, PyExc_SyntaxError
,
2354 "can't assign to operator");
2357 com_apply_trailer(c
, CHILD(n
, i
));
2358 } /* NB i is still alive */
2359 com_assign_trailer(c
,
2360 CHILD(n
, i
), assigning
, augn
);
2367 switch (TYPE(CHILD(n
, 0))) {
2370 if (TYPE(n
) == RPAR
) {
2371 /* XXX Should allow () = () ??? */
2372 com_error(c
, PyExc_SyntaxError
,
2373 "can't assign to ()");
2376 if (assigning
> OP_APPLY
) {
2377 com_error(c
, PyExc_SyntaxError
,
2378 "augmented assign to tuple not possible");
2384 if (TYPE(n
) == RSQB
) {
2385 com_error(c
, PyExc_SyntaxError
,
2386 "can't assign to []");
2389 if (assigning
> OP_APPLY
) {
2390 com_error(c
, PyExc_SyntaxError
,
2391 "augmented assign to list not possible");
2395 && TYPE(CHILD(n
, 1)) == list_for
) {
2396 com_error(c
, PyExc_SyntaxError
,
2397 "can't assign to list comprehension");
2400 com_assign_sequence(c
, n
, assigning
);
2403 if (assigning
> OP_APPLY
)
2404 com_augassign_name(c
, CHILD(n
, 0),
2407 com_assign_name(c
, CHILD(n
, 0),
2411 com_error(c
, PyExc_SyntaxError
,
2412 "can't assign to literal");
2418 com_error(c
, PyExc_SyntaxError
,
2419 "can't assign to lambda");
2423 com_error(c
, PyExc_SystemError
,
2424 "com_assign: bad node");
2432 com_augassign(struct compiling
*c
, node
*n
)
2436 switch (STR(CHILD(CHILD(n
, 1), 0))[0]) {
2437 case '+': opcode
= INPLACE_ADD
; break;
2438 case '-': opcode
= INPLACE_SUBTRACT
; break;
2439 case '/': opcode
= INPLACE_DIVIDE
; break;
2440 case '%': opcode
= INPLACE_MODULO
; break;
2441 case '<': opcode
= INPLACE_LSHIFT
; break;
2442 case '>': opcode
= INPLACE_RSHIFT
; break;
2443 case '&': opcode
= INPLACE_AND
; break;
2444 case '^': opcode
= INPLACE_XOR
; break;
2445 case '|': opcode
= INPLACE_OR
; break;
2447 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '*')
2448 opcode
= INPLACE_POWER
;
2450 opcode
= INPLACE_MULTIPLY
;
2453 com_error(c
, PyExc_SystemError
, "com_augassign: bad operator");
2456 com_assign(c
, CHILD(n
, 0), opcode
, CHILD(n
, 2));
2460 com_expr_stmt(struct compiling
*c
, node
*n
)
2463 /* testlist (('=' testlist)* | augassign testlist) */
2464 /* Forget it if we have just a doc string here */
2465 if (!c
->c_interactive
&& NCH(n
) == 1 && get_rawdocstring(n
) != NULL
)
2468 com_node(c
, CHILD(n
, NCH(n
)-1));
2469 if (c
->c_interactive
)
2470 com_addbyte(c
, PRINT_EXPR
);
2472 com_addbyte(c
, POP_TOP
);
2475 else if (TYPE(CHILD(n
,1)) == augassign
)
2476 com_augassign(c
, n
);
2479 com_node(c
, CHILD(n
, NCH(n
)-1));
2480 for (i
= 0; i
< NCH(n
)-2; i
+=2) {
2481 if (i
+2 < NCH(n
)-2) {
2482 com_addbyte(c
, DUP_TOP
);
2485 com_assign(c
, CHILD(n
, i
), OP_ASSIGN
, NULL
);
2491 com_assert_stmt(struct compiling
*c
, node
*n
)
2495 REQ(n
, assert_stmt
); /* 'assert' test [',' test] */
2496 /* Generate code like for
2500 raise AssertionError [, <message>]
2502 where <message> is the second test, if present.
2505 if (Py_OptimizeFlag
)
2507 com_addop_name(c
, LOAD_GLOBAL
, "__debug__");
2509 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2510 com_addbyte(c
, POP_TOP
);
2512 com_node(c
, CHILD(n
, 1));
2513 com_addfwref(c
, JUMP_IF_TRUE
, &b
);
2514 com_addbyte(c
, POP_TOP
);
2516 /* Raise that exception! */
2517 com_addop_name(c
, LOAD_GLOBAL
, "AssertionError");
2519 i
= NCH(n
)/2; /* Either 2 or 4 */
2521 com_node(c
, CHILD(n
, 3));
2522 com_addoparg(c
, RAISE_VARARGS
, i
);
2524 /* The interpreter does not fall through */
2525 /* All jumps converge here */
2526 com_backpatch(c
, a
);
2527 com_backpatch(c
, b
);
2528 com_addbyte(c
, POP_TOP
);
2532 com_print_stmt(struct compiling
*c
, node
*n
)
2535 node
* stream
= NULL
;
2537 REQ(n
, print_stmt
); /* 'print' (test ',')* [test] */
2539 /* are we using the extended print form? */
2540 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2541 stream
= CHILD(n
, 2);
2542 com_node(c
, stream
);
2543 /* stack: [...] => [... stream] */
2545 if (NCH(n
) > 3 && TYPE(CHILD(n
, 3)) == COMMA
)
2550 for (; i
< NCH(n
); i
+= 2) {
2551 if (stream
!= NULL
) {
2552 com_addbyte(c
, DUP_TOP
);
2553 /* stack: [stream] => [stream stream] */
2555 com_node(c
, CHILD(n
, i
));
2556 /* stack: [stream stream] => [stream stream obj] */
2557 com_addbyte(c
, ROT_TWO
);
2558 /* stack: [stream stream obj] => [stream obj stream] */
2559 com_addbyte(c
, PRINT_ITEM_TO
);
2560 /* stack: [stream obj stream] => [stream] */
2564 com_node(c
, CHILD(n
, i
));
2565 /* stack: [...] => [... obj] */
2566 com_addbyte(c
, PRINT_ITEM
);
2570 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2571 if (TYPE(CHILD(n
, NCH(n
)-1)) == COMMA
) {
2572 if (stream
!= NULL
) {
2573 /* must pop the extra stream object off the stack */
2574 com_addbyte(c
, POP_TOP
);
2575 /* stack: [... stream] => [...] */
2580 if (stream
!= NULL
) {
2581 /* this consumes the last stream object on stack */
2582 com_addbyte(c
, PRINT_NEWLINE_TO
);
2583 /* stack: [... stream] => [...] */
2587 com_addbyte(c
, PRINT_NEWLINE
);
2592 com_return_stmt(struct compiling
*c
, node
*n
)
2594 REQ(n
, return_stmt
); /* 'return' [testlist] */
2595 if (!c
->c_infunction
) {
2596 com_error(c
, PyExc_SyntaxError
, "'return' outside function");
2599 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2603 com_node(c
, CHILD(n
, 1));
2604 com_addbyte(c
, RETURN_VALUE
);
2609 com_raise_stmt(struct compiling
*c
, node
*n
)
2612 REQ(n
, raise_stmt
); /* 'raise' [test [',' test [',' test]]] */
2614 com_node(c
, CHILD(n
, 1));
2616 com_node(c
, CHILD(n
, 3));
2618 com_node(c
, CHILD(n
, 5));
2622 com_addoparg(c
, RAISE_VARARGS
, i
);
2627 com_from_import(struct compiling
*c
, node
*n
)
2629 com_addopname(c
, IMPORT_FROM
, CHILD(n
, 0));
2632 if (strcmp(STR(CHILD(n
, 1)), "as") != 0) {
2633 com_error(c
, PyExc_SyntaxError
, "invalid syntax");
2636 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 2)));
2638 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
2643 com_import_stmt(struct compiling
*c
, node
*n
)
2646 REQ(n
, import_stmt
);
2647 /* 'import' dotted_name (',' dotted_name)* |
2648 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2649 if (STR(CHILD(n
, 0))[0] == 'f') {
2651 /* 'from' dotted_name 'import' ... */
2652 REQ(CHILD(n
, 1), dotted_name
);
2654 if (TYPE(CHILD(n
, 3)) == STAR
) {
2655 tup
= Py_BuildValue("(s)", "*");
2657 tup
= PyTuple_New((NCH(n
) - 2)/2);
2658 for (i
= 3; i
< NCH(n
); i
+= 2) {
2659 PyTuple_SET_ITEM(tup
, (i
-3)/2,
2660 PyString_FromString(STR(
2661 CHILD(CHILD(n
, i
), 0))));
2664 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, tup
));
2667 com_addopname(c
, IMPORT_NAME
, CHILD(n
, 1));
2668 if (TYPE(CHILD(n
, 3)) == STAR
)
2669 com_addbyte(c
, IMPORT_STAR
);
2671 for (i
= 3; i
< NCH(n
); i
+= 2)
2672 com_from_import(c
, CHILD(n
, i
));
2673 com_addbyte(c
, POP_TOP
);
2679 for (i
= 1; i
< NCH(n
); i
+= 2) {
2680 node
*subn
= CHILD(n
, i
);
2681 REQ(subn
, dotted_as_name
);
2682 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2684 com_addopname(c
, IMPORT_NAME
, CHILD(subn
, 0));
2685 if (NCH(subn
) > 1) {
2687 if (strcmp(STR(CHILD(subn
, 1)), "as") != 0) {
2688 com_error(c
, PyExc_SyntaxError
,
2692 for (j
=2 ; j
< NCH(CHILD(subn
, 0)); j
+= 2)
2693 com_addopname(c
, LOAD_ATTR
,
2694 CHILD(CHILD(subn
, 0),
2696 com_addop_varname(c
, VAR_STORE
,
2697 STR(CHILD(subn
, 2)));
2699 com_addop_varname(c
, VAR_STORE
,
2700 STR(CHILD(CHILD(subn
, 0),
2708 com_exec_stmt(struct compiling
*c
, node
*n
)
2711 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2712 com_node(c
, CHILD(n
, 1));
2714 com_node(c
, CHILD(n
, 3));
2716 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2720 com_node(c
, CHILD(n
, 5));
2722 com_addbyte(c
, DUP_TOP
);
2725 com_addbyte(c
, EXEC_STMT
);
2730 is_constant_false(struct compiling
*c
, node
*n
)
2734 /* argument c will be NULL when called from symtable_node() */
2736 /* Label to avoid tail recursion */
2747 for (i
= 0; i
< NCH(n
); i
++) {
2748 node
*ch
= CHILD(n
, i
);
2749 if (TYPE(ch
) == stmt
) {
2784 if (Py_OptimizeFlag
&& strcmp(STR(n
), "__debug__") == 0)
2789 v
= parsenumber(c
, STR(n
));
2794 i
= PyObject_IsTrue(v
);
2799 v
= parsestr(STR(n
));
2804 i
= PyObject_IsTrue(v
);
2813 com_if_stmt(struct compiling
*c
, node
*n
)
2818 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2819 for (i
= 0; i
+3 < NCH(n
); i
+=4) {
2821 node
*ch
= CHILD(n
, i
+1);
2822 if (is_constant_false(c
, ch
))
2825 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
2827 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2828 com_addbyte(c
, POP_TOP
);
2830 com_node(c
, CHILD(n
, i
+3));
2831 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
2832 com_backpatch(c
, a
);
2833 /* We jump here with an extra entry which we now pop */
2834 com_addbyte(c
, POP_TOP
);
2837 com_node(c
, CHILD(n
, i
+2));
2839 com_backpatch(c
, anchor
);
2843 com_while_stmt(struct compiling
*c
, node
*n
)
2845 int break_anchor
= 0;
2847 int save_begin
= c
->c_begin
;
2848 REQ(n
, while_stmt
); /* 'while' test ':' suite ['else' ':' suite] */
2849 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2850 block_push(c
, SETUP_LOOP
);
2851 c
->c_begin
= c
->c_nexti
;
2852 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2853 com_node(c
, CHILD(n
, 1));
2854 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2855 com_addbyte(c
, POP_TOP
);
2858 com_node(c
, CHILD(n
, 3));
2860 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2861 c
->c_begin
= save_begin
;
2862 com_backpatch(c
, anchor
);
2863 /* We jump here with one entry more on the stack */
2864 com_addbyte(c
, POP_TOP
);
2865 com_addbyte(c
, POP_BLOCK
);
2866 block_pop(c
, SETUP_LOOP
);
2868 com_node(c
, CHILD(n
, 6));
2869 com_backpatch(c
, break_anchor
);
2873 com_for_stmt(struct compiling
*c
, node
*n
)
2876 int break_anchor
= 0;
2878 int save_begin
= c
->c_begin
;
2880 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2881 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2882 block_push(c
, SETUP_LOOP
);
2883 com_node(c
, CHILD(n
, 3));
2884 v
= PyInt_FromLong(0L);
2887 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
2890 c
->c_begin
= c
->c_nexti
;
2891 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2892 com_addfwref(c
, FOR_LOOP
, &anchor
);
2894 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
2896 com_node(c
, CHILD(n
, 5));
2898 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2899 c
->c_begin
= save_begin
;
2900 com_backpatch(c
, anchor
);
2901 com_pop(c
, 2); /* FOR_LOOP has popped these */
2902 com_addbyte(c
, POP_BLOCK
);
2903 block_pop(c
, SETUP_LOOP
);
2905 com_node(c
, CHILD(n
, 8));
2906 com_backpatch(c
, break_anchor
);
2909 /* Code generated for "try: S finally: Sf" is as follows:
2918 The special instructions use the block stack. Each block
2919 stack entry contains the instruction that created it (here
2920 SETUP_FINALLY), the level of the value stack at the time the
2921 block stack entry was created, and a label (here L).
2924 Pushes the current value stack level and the label
2925 onto the block stack.
2927 Pops en entry from the block stack, and pops the value
2928 stack until its level is the same as indicated on the
2929 block stack. (The label is ignored.)
2931 Pops a variable number of entries from the *value* stack
2932 and re-raises the exception they specify. The number of
2933 entries popped depends on the (pseudo) exception type.
2935 The block stack is unwound when an exception is raised:
2936 when a SETUP_FINALLY entry is found, the exception is pushed
2937 onto the value stack (and the exception condition is cleared),
2938 and the interpreter jumps to the label gotten from the block
2941 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2942 (The contents of the value stack is shown in [], with the top
2943 at the right; 'tb' is trace-back info, 'val' the exception's
2944 associated value, and 'exc' the exception.)
2946 Value stack Label Instruction Argument
2952 [tb, val, exc] L1: DUP )
2953 [tb, val, exc, exc] <evaluate E1> )
2954 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2955 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2956 [tb, val, exc, 1] POP )
2958 [tb, val] <assign to V1> (or POP if no V1)
2963 [tb, val, exc, 0] L2: POP
2965 .............................etc.......................
2967 [tb, val, exc, 0] Ln+1: POP
2968 [tb, val, exc] END_FINALLY # re-raise exception
2970 [] L0: <next statement>
2972 Of course, parts are not generated if Vi or Ei is not present.
2976 com_try_except(struct compiling
*c
, node
*n
)
2978 int except_anchor
= 0;
2980 int else_anchor
= 0;
2984 com_addfwref(c
, SETUP_EXCEPT
, &except_anchor
);
2985 block_push(c
, SETUP_EXCEPT
);
2986 com_node(c
, CHILD(n
, 2));
2987 com_addbyte(c
, POP_BLOCK
);
2988 block_pop(c
, SETUP_EXCEPT
);
2989 com_addfwref(c
, JUMP_FORWARD
, &else_anchor
);
2990 com_backpatch(c
, except_anchor
);
2992 i
< NCH(n
) && TYPE(ch
= CHILD(n
, i
)) == except_clause
;
2994 /* except_clause: 'except' [expr [',' var]] */
2995 if (except_anchor
== 0) {
2996 com_error(c
, PyExc_SyntaxError
,
2997 "default 'except:' must be last");
3001 com_push(c
, 3); /* tb, val, exc pushed by exception */
3002 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3004 com_addbyte(c
, DUP_TOP
);
3006 com_node(c
, CHILD(ch
, 1));
3007 com_addoparg(c
, COMPARE_OP
, EXC_MATCH
);
3009 com_addfwref(c
, JUMP_IF_FALSE
, &except_anchor
);
3010 com_addbyte(c
, POP_TOP
);
3013 com_addbyte(c
, POP_TOP
);
3016 com_assign(c
, CHILD(ch
, 3), OP_ASSIGN
, NULL
);
3018 com_addbyte(c
, POP_TOP
);
3021 com_addbyte(c
, POP_TOP
);
3023 com_node(c
, CHILD(n
, i
+2));
3024 com_addfwref(c
, JUMP_FORWARD
, &end_anchor
);
3025 if (except_anchor
) {
3026 com_backpatch(c
, except_anchor
);
3027 /* We come in with [tb, val, exc, 0] on the
3028 stack; one pop and it's the same as
3029 expected at the start of the loop */
3030 com_addbyte(c
, POP_TOP
);
3033 /* We actually come in here with [tb, val, exc] but the
3034 END_FINALLY will zap those and jump around.
3035 The c_stacklevel does not reflect them so we need not pop
3037 com_addbyte(c
, END_FINALLY
);
3038 com_backpatch(c
, else_anchor
);
3040 com_node(c
, CHILD(n
, i
+2));
3041 com_backpatch(c
, end_anchor
);
3045 com_try_finally(struct compiling
*c
, node
*n
)
3047 int finally_anchor
= 0;
3050 com_addfwref(c
, SETUP_FINALLY
, &finally_anchor
);
3051 block_push(c
, SETUP_FINALLY
);
3052 com_node(c
, CHILD(n
, 2));
3053 com_addbyte(c
, POP_BLOCK
);
3054 block_pop(c
, SETUP_FINALLY
);
3055 block_push(c
, END_FINALLY
);
3056 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3057 /* While the generated code pushes only one item,
3058 the try-finally handling can enter here with
3059 up to three items. OK, here are the details:
3060 3 for an exception, 2 for RETURN, 1 for BREAK. */
3062 com_backpatch(c
, finally_anchor
);
3063 ch
= CHILD(n
, NCH(n
)-1);
3064 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3066 com_addbyte(c
, END_FINALLY
);
3067 block_pop(c
, END_FINALLY
);
3068 com_pop(c
, 3); /* Matches the com_push above */
3072 com_try_stmt(struct compiling
*c
, node
*n
)
3075 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3076 | 'try' ':' suite 'finally' ':' suite */
3077 if (TYPE(CHILD(n
, 3)) != except_clause
)
3078 com_try_finally(c
, n
);
3080 com_try_except(c
, n
);
3084 get_rawdocstring(node
*n
)
3088 /* Label to avoid tail recursion */
3099 for (i
= 0; i
< NCH(n
); i
++) {
3100 node
*ch
= CHILD(n
, i
);
3101 if (TYPE(ch
) == stmt
) {
3135 if (TYPE(CHILD(n
, 0)) == STRING
)
3144 get_docstring(node
*n
)
3146 /* Don't generate doc-strings if run with -OO */
3147 if (Py_OptimizeFlag
> 1)
3149 n
= get_rawdocstring(n
);
3152 return parsestrplus(n
);
3156 com_suite(struct compiling
*c
, node
*n
)
3159 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3161 com_node(c
, CHILD(n
, 0));
3165 for (i
= 0; i
< NCH(n
) && c
->c_errors
== 0; i
++) {
3166 node
*ch
= CHILD(n
, i
);
3167 if (TYPE(ch
) == stmt
)
3175 com_continue_stmt(struct compiling
*c
, node
*n
)
3177 int i
= c
->c_nblocks
;
3178 if (i
-- > 0 && c
->c_block
[i
] == SETUP_LOOP
) {
3179 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3182 /* at the outer level */
3183 com_error(c
, PyExc_SyntaxError
,
3184 "'continue' not properly in loop");
3188 for (j
= i
-1; j
>= 0; --j
) {
3189 if (c
->c_block
[j
] == SETUP_LOOP
)
3193 /* there is a loop, but something interferes */
3194 for (; i
> j
; --i
) {
3195 if (c
->c_block
[i
] == SETUP_EXCEPT
||
3196 c
->c_block
[i
] == SETUP_FINALLY
) {
3197 com_addoparg(c
, CONTINUE_LOOP
,
3201 if (c
->c_block
[i
] == END_FINALLY
) {
3202 com_error(c
, PyExc_SyntaxError
,
3203 "'continue' not supported inside 'finally' clause");
3208 com_error(c
, PyExc_SyntaxError
,
3209 "'continue' not properly in loop");
3211 /* XXX Could allow it inside a 'finally' clause
3212 XXX if we could pop the exception still on the stack */
3216 com_argdefs(struct compiling
*c
, node
*n
)
3218 int i
, nch
, nargs
, ndefs
;
3219 if (TYPE(n
) == lambdef
) {
3220 /* lambdef: 'lambda' [varargslist] ':' test */
3224 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ... */
3226 REQ(n
, parameters
); /* parameters: '(' [varargslist] ')' */
3229 if (TYPE(n
) != varargslist
)
3232 (fpdef ['=' test] ',')* '*' ....... |
3233 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3237 for (i
= 0; i
< nch
; i
++) {
3239 if (TYPE(CHILD(n
, i
)) == STAR
||
3240 TYPE(CHILD(n
, i
)) == DOUBLESTAR
)
3245 t
= RPAR
; /* Anything except EQUAL or COMMA */
3247 t
= TYPE(CHILD(n
, i
));
3251 com_node(c
, CHILD(n
, i
));
3255 t
= TYPE(CHILD(n
, i
));
3258 /* Treat "(a=1, b)" as an error */
3260 com_error(c
, PyExc_SyntaxError
,
3261 "non-default argument follows default argument");
3270 com_funcdef(struct compiling
*c
, node
*n
)
3274 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3275 ndefs
= com_argdefs(c
, n
);
3276 symtable_enter_scope(c
->c_symtable
, STR(CHILD(n
, 1)), TYPE(n
),
3278 co
= (PyObject
*)icompile(n
, c
);
3279 symtable_exit_scope(c
->c_symtable
);
3283 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3284 int i
= com_addconst(c
, co
);
3285 com_addoparg(c
, LOAD_CONST
, i
);
3288 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
3290 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
3292 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3299 com_bases(struct compiling
*c
, node
*n
)
3303 /* testlist: test (',' test)* [','] */
3304 for (i
= 0; i
< NCH(n
); i
+= 2)
3305 com_node(c
, CHILD(n
, i
));
3307 com_addoparg(c
, BUILD_TUPLE
, i
);
3312 com_classdef(struct compiling
*c
, node
*n
)
3319 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3320 if ((v
= PyString_InternFromString(STR(CHILD(n
, 1)))) == NULL
) {
3324 /* Push the class name on the stack */
3325 i
= com_addconst(c
, v
);
3326 com_addoparg(c
, LOAD_CONST
, i
);
3329 /* Push the tuple of base classes on the stack */
3330 if (TYPE(CHILD(n
, 2)) != LPAR
) {
3331 com_addoparg(c
, BUILD_TUPLE
, 0);
3335 com_bases(c
, CHILD(n
, 3));
3336 name
= STR(CHILD(n
, 1));
3337 symtable_enter_scope(c
->c_symtable
, name
, TYPE(n
), n
->n_lineno
);
3338 co
= (PyObject
*)icompile(n
, c
);
3339 symtable_exit_scope(c
->c_symtable
);
3343 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3344 i
= com_addconst(c
, co
);
3345 com_addoparg(c
, LOAD_CONST
, i
);
3348 com_addoparg(c
, MAKE_CLOSURE
, 0);
3350 com_addoparg(c
, MAKE_FUNCTION
, 0);
3351 com_addoparg(c
, CALL_FUNCTION
, 0);
3352 com_addbyte(c
, BUILD_CLASS
);
3354 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3360 com_node(struct compiling
*c
, node
*n
)
3367 /* Definition nodes */
3376 /* Trivial parse tree nodes */
3385 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3386 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3389 for (i
= 0; i
< NCH(n
)-1; i
+= 2)
3390 com_node(c
, CHILD(n
, i
));
3395 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3399 /* Statement nodes */
3402 com_expr_stmt(c
, n
);
3405 com_print_stmt(c
, n
);
3407 case del_stmt
: /* 'del' exprlist */
3408 com_assign(c
, CHILD(n
, 1), OP_DELETE
, NULL
);
3413 if (c
->c_loops
== 0) {
3414 com_error(c
, PyExc_SyntaxError
,
3415 "'break' outside loop");
3417 com_addbyte(c
, BREAK_LOOP
);
3420 com_continue_stmt(c
, n
);
3423 com_return_stmt(c
, n
);
3426 com_raise_stmt(c
, n
);
3429 com_import_stmt(c
, n
);
3434 com_exec_stmt(c
, n
);
3437 com_assert_stmt(c
, n
);
3443 com_while_stmt(c
, n
);
3455 /* Expression nodes */
3470 com_comparison(c
, n
);
3485 com_shift_expr(c
, n
);
3488 com_arith_expr(c
, n
);
3504 com_error(c
, PyExc_SystemError
,
3505 "com_node: unexpected node type");
3509 static void com_fplist(struct compiling
*, node
*);
3512 com_fpdef(struct compiling
*c
, node
*n
)
3514 REQ(n
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3515 if (TYPE(CHILD(n
, 0)) == LPAR
)
3516 com_fplist(c
, CHILD(n
, 1));
3518 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
3524 com_fplist(struct compiling
*c
, node
*n
)
3526 REQ(n
, fplist
); /* fplist: fpdef (',' fpdef)* [','] */
3528 com_fpdef(c
, CHILD(n
, 0));
3531 int i
= (NCH(n
)+1)/2;
3532 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
3534 for (i
= 0; i
< NCH(n
); i
+= 2)
3535 com_fpdef(c
, CHILD(n
, i
));
3540 com_arglist(struct compiling
*c
, node
*n
)
3545 REQ(n
, varargslist
);
3547 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3549 /* Enter all arguments in table of locals */
3550 for (i
= 0, narg
= 0; i
< nch
; i
++) {
3551 node
*ch
= CHILD(n
, i
);
3554 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3556 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3558 if (TYPE(fp
) != NAME
) {
3560 sprintf(nbuf
, ".%d", i
);
3564 /* all name updates handled by symtable */
3568 if (TYPE(ch
) == EQUAL
)
3574 /* Generate code for complex arguments only after
3575 having counted the simple arguments */
3577 for (i
= 0; i
< nch
; i
++) {
3578 node
*ch
= CHILD(n
, i
);
3580 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3582 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3584 if (TYPE(fp
) != NAME
) {
3585 com_addoparg(c
, LOAD_FAST
, ilocal
);
3593 if (TYPE(ch
) == EQUAL
)
3602 com_file_input(struct compiling
*c
, node
*n
)
3606 REQ(n
, file_input
); /* (NEWLINE | stmt)* ENDMARKER */
3607 doc
= get_docstring(n
);
3609 int i
= com_addconst(c
, doc
);
3611 com_addoparg(c
, LOAD_CONST
, i
);
3613 com_addop_name(c
, STORE_NAME
, "__doc__");
3616 for (i
= 0; i
< NCH(n
); i
++) {
3617 node
*ch
= CHILD(n
, i
);
3618 if (TYPE(ch
) != ENDMARKER
&& TYPE(ch
) != NEWLINE
)
3623 /* Top-level compile-node interface */
3626 compile_funcdef(struct compiling
*c
, node
*n
)
3630 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3631 c
->c_name
= STR(CHILD(n
, 1));
3632 doc
= get_docstring(CHILD(n
, 4));
3634 (void) com_addconst(c
, doc
);
3638 (void) com_addconst(c
, Py_None
); /* No docstring */
3639 ch
= CHILD(n
, 2); /* parameters: '(' [varargslist] ')' */
3640 ch
= CHILD(ch
, 1); /* ')' | varargslist */
3641 if (TYPE(ch
) == varargslist
)
3643 c
->c_infunction
= 1;
3644 com_node(c
, CHILD(n
, 4));
3645 c
->c_infunction
= 0;
3646 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3648 com_addbyte(c
, RETURN_VALUE
);
3653 compile_lambdef(struct compiling
*c
, node
*n
)
3656 REQ(n
, lambdef
); /* lambdef: 'lambda' [varargslist] ':' test */
3657 c
->c_name
= "<lambda>";
3660 (void) com_addconst(c
, Py_None
); /* No docstring */
3661 if (TYPE(ch
) == varargslist
) {
3668 com_addbyte(c
, RETURN_VALUE
);
3673 compile_classdef(struct compiling
*c
, node
*n
)
3678 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3679 c
->c_name
= STR(CHILD(n
, 1));
3680 c
->c_private
= c
->c_name
;
3681 ch
= CHILD(n
, NCH(n
)-1); /* The suite */
3682 doc
= get_docstring(ch
);
3684 int i
= com_addconst(c
, doc
);
3686 com_addoparg(c
, LOAD_CONST
, i
);
3688 com_addop_name(c
, STORE_NAME
, "__doc__");
3692 (void) com_addconst(c
, Py_None
);
3694 com_addbyte(c
, LOAD_LOCALS
);
3696 com_addbyte(c
, RETURN_VALUE
);
3701 compile_node(struct compiling
*c
, node
*n
)
3703 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3707 case single_input
: /* One interactive command */
3708 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3711 if (TYPE(n
) != NEWLINE
)
3713 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3715 com_addbyte(c
, RETURN_VALUE
);
3720 case file_input
: /* A whole file, or built-in function exec() */
3721 com_file_input(c
, n
);
3722 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3724 com_addbyte(c
, RETURN_VALUE
);
3728 case eval_input
: /* Built-in function input() */
3729 com_node(c
, CHILD(n
, 0));
3730 com_addbyte(c
, RETURN_VALUE
);
3734 case lambdef
: /* anonymous function definition */
3735 compile_lambdef(c
, n
);
3738 case funcdef
: /* A function definition */
3739 compile_funcdef(c
, n
);
3742 case classdef
: /* A class definition */
3743 compile_classdef(c
, n
);
3747 com_error(c
, PyExc_SystemError
,
3748 "compile_node: unexpected node type");
3753 dict_keys_inorder(PyObject
*dict
, int offset
)
3755 PyObject
*tuple
, *k
, *v
;
3756 int i
, pos
= 0, size
= PyDict_Size(dict
);
3758 tuple
= PyTuple_New(size
);
3761 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3762 i
= PyInt_AS_LONG(v
);
3764 assert((i
- offset
) < size
);
3765 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3771 PyNode_Compile(node
*n
, char *filename
)
3773 return PyNode_CompileFlags(n
, filename
, NULL
);
3777 PyNode_CompileFlags(node
*n
, char *filename
, PyCompilerFlags
*flags
)
3779 return jcompile(n
, filename
, NULL
, flags
);
3783 PyNode_CompileSymtable(node
*n
, char *filename
)
3785 struct symtable
*st
;
3786 PyFutureFeatures
*ff
;
3788 ff
= PyNode_Future(n
, filename
);
3791 st
= symtable_init();
3795 symtable_enter_scope(st
, TOP
, TYPE(n
), n
->n_lineno
);
3796 if (st
->st_errors
> 0)
3798 symtable_node(st
, n
);
3799 if (st
->st_errors
> 0)
3804 PyMem_Free((void *)ff
);
3805 st
->st_future
= NULL
;
3806 PySymtable_Free(st
);
3810 static PyCodeObject
*
3811 icompile(node
*n
, struct compiling
*base
)
3813 return jcompile(n
, base
->c_filename
, base
, NULL
);
3816 static PyCodeObject
*
3817 jcompile(node
*n
, char *filename
, struct compiling
*base
,
3818 PyCompilerFlags
*flags
)
3820 struct compiling sc
;
3822 if (!com_init(&sc
, filename
))
3825 sc
.c_private
= base
->c_private
;
3826 sc
.c_symtable
= base
->c_symtable
;
3827 /* c_symtable still points to parent's symbols */
3829 || (sc
.c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
))
3832 sc
.c_private
= NULL
;
3833 sc
.c_future
= PyNode_Future(n
, filename
);
3834 if (sc
.c_future
== NULL
) {
3839 if (flags
->cf_nested_scopes
)
3840 sc
.c_future
->ff_nested_scopes
= 1;
3841 else if (sc
.c_future
->ff_nested_scopes
)
3842 flags
->cf_nested_scopes
= 1;
3844 if (symtable_build(&sc
, n
) < 0) {
3850 if (symtable_load_symbols(&sc
) < 0) {
3854 compile_node(&sc
, n
);
3856 if (sc
.c_errors
== 0) {
3857 PyObject
*consts
, *names
, *varnames
, *filename
, *name
,
3858 *freevars
, *cellvars
;
3859 consts
= PyList_AsTuple(sc
.c_consts
);
3860 names
= PyList_AsTuple(sc
.c_names
);
3861 varnames
= PyList_AsTuple(sc
.c_varnames
);
3862 cellvars
= dict_keys_inorder(sc
.c_cellvars
, 0);
3863 freevars
= dict_keys_inorder(sc
.c_freevars
,
3864 PyTuple_GET_SIZE(cellvars
));
3865 filename
= PyString_InternFromString(sc
.c_filename
);
3866 name
= PyString_InternFromString(sc
.c_name
);
3867 if (!PyErr_Occurred())
3868 co
= PyCode_New(sc
.c_argcount
,
3884 Py_XDECREF(varnames
);
3885 Py_XDECREF(freevars
);
3886 Py_XDECREF(cellvars
);
3887 Py_XDECREF(filename
);
3890 else if (!PyErr_Occurred()) {
3891 /* This could happen if someone called PyErr_Clear() after an
3892 error was reported above. That's not supposed to happen,
3893 but I just plugged one case and I'm not sure there can't be
3894 others. In that case, raise SystemError so that at least
3895 it gets reported instead dumping core. */
3896 PyErr_SetString(PyExc_SystemError
, "lost syntax error");
3900 PySymtable_Free(sc
.c_symtable
);
3901 sc
.c_symtable
= NULL
;
3908 PyCode_Addr2Line(PyCodeObject
*co
, int addrq
)
3910 int size
= PyString_Size(co
->co_lnotab
) / 2;
3911 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
3912 int line
= co
->co_firstlineno
;
3914 while (--size
>= 0) {
3923 /* The test for LOCAL must come before the test for FREE in order to
3924 handle classes where name is both local and free. The local var is
3925 a method and the free var is a free var referenced within a method.
3929 get_ref_type(struct compiling
*c
, char *name
)
3932 if (c
->c_symtable
->st_nested_scopes
) {
3933 if (PyDict_GetItemString(c
->c_cellvars
, name
) != NULL
)
3935 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
3937 if (PyDict_GetItemString(c
->c_freevars
, name
) != NULL
)
3939 v
= PyDict_GetItemString(c
->c_globals
, name
);
3942 return GLOBAL_EXPLICIT
;
3944 return GLOBAL_IMPLICIT
;
3948 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
3950 v
= PyDict_GetItemString(c
->c_globals
, name
);
3953 return GLOBAL_EXPLICIT
;
3955 return GLOBAL_IMPLICIT
;
3962 "unknown scope for %.100s in %.100s(%s) "
3963 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
3965 PyObject_REPR(c
->c_symtable
->st_cur
->ste_id
),
3967 PyObject_REPR(c
->c_symtable
->st_cur
->ste_symbols
),
3968 PyObject_REPR(c
->c_locals
),
3969 PyObject_REPR(c
->c_globals
)
3974 return -1; /* can't get here */
3977 /* Helper functions to issue warnings */
3980 issue_warning(char *msg
, char *filename
, int lineno
)
3982 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, filename
,
3983 lineno
, NULL
, NULL
) < 0) {
3984 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
3985 PyErr_SetString(PyExc_SyntaxError
, msg
);
3986 PyErr_SyntaxLocation(filename
, lineno
);
3994 symtable_warn(struct symtable
*st
, char *msg
)
3996 if (issue_warning(msg
, st
->st_filename
, st
->st_cur
->ste_lineno
) < 0) {
4003 /* Helper function for setting lineno and filename */
4006 symtable_build(struct compiling
*c
, node
*n
)
4008 if ((c
->c_symtable
= symtable_init()) == NULL
)
4010 c
->c_symtable
->st_future
= c
->c_future
;
4011 if (c
->c_future
->ff_nested_scopes
)
4012 c
->c_symtable
->st_nested_scopes
= 1;
4013 c
->c_symtable
->st_filename
= c
->c_filename
;
4014 symtable_enter_scope(c
->c_symtable
, TOP
, TYPE(n
), n
->n_lineno
);
4015 if (c
->c_symtable
->st_errors
> 0)
4017 symtable_node(c
->c_symtable
, n
);
4018 if (c
->c_symtable
->st_errors
> 0)
4020 /* reset for second pass */
4021 c
->c_symtable
->st_nscopes
= 1;
4022 c
->c_symtable
->st_pass
= 2;
4027 symtable_init_compiling_symbols(struct compiling
*c
)
4031 varnames
= c
->c_symtable
->st_cur
->ste_varnames
;
4032 if (varnames
== NULL
) {
4033 varnames
= PyList_New(0);
4034 if (varnames
== NULL
)
4036 c
->c_symtable
->st_cur
->ste_varnames
= varnames
;
4037 Py_INCREF(varnames
);
4039 Py_INCREF(varnames
);
4040 c
->c_varnames
= varnames
;
4042 c
->c_globals
= PyDict_New();
4043 if (c
->c_globals
== NULL
)
4045 c
->c_freevars
= PyDict_New();
4046 if (c
->c_freevars
== NULL
)
4048 c
->c_cellvars
= PyDict_New();
4049 if (c
->c_cellvars
== NULL
)
4054 struct symbol_info
{
4062 symtable_init_info(struct symbol_info
*si
)
4067 si
->si_nimplicit
= 0;
4071 symtable_resolve_free(struct compiling
*c
, PyObject
*name
,
4072 struct symbol_info
*si
)
4076 /* Seperate logic for DEF_FREE. If it occurs in a function,
4077 it indicates a local that we must allocate storage for (a
4078 cell var). If it occurs in a class, then the class has a
4079 method and a free variable with the same name.
4082 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
) {
4083 v
= PyInt_FromLong(si
->si_ncells
++);
4084 dict
= c
->c_cellvars
;
4086 v
= PyInt_FromLong(si
->si_nfrees
++);
4087 dict
= c
->c_freevars
;
4091 if (PyDict_SetItem(dict
, name
, v
) < 0) {
4099 /* If a variable is a cell and an argument, make sure that appears in
4100 co_cellvars before any variable to its right in varnames.
4105 symtable_cellvar_offsets(PyObject
**cellvars
, int argcount
,
4106 PyObject
*varnames
, int flags
)
4108 PyObject
*v
, *w
, *d
, *list
= NULL
;
4111 if (flags
& CO_VARARGS
)
4113 if (flags
& CO_VARKEYWORDS
)
4115 for (i
= argcount
; --i
>= 0; ) {
4116 v
= PyList_GET_ITEM(varnames
, i
);
4117 if (PyDict_GetItem(*cellvars
, v
)) {
4119 list
= PyList_New(1);
4122 PyList_SET_ITEM(list
, 0, v
);
4125 PyList_Insert(list
, 0, v
);
4128 if (list
== NULL
|| PyList_GET_SIZE(list
) == 0)
4130 /* There are cellvars that are also arguments. Create a dict
4131 to replace cellvars and put the args at the front.
4134 for (i
= PyList_GET_SIZE(list
); --i
>= 0; ) {
4135 v
= PyInt_FromLong(i
);
4138 if (PyDict_SetItem(d
, PyList_GET_ITEM(list
, i
), v
) < 0)
4140 if (PyDict_DelItem(*cellvars
, PyList_GET_ITEM(list
, i
)) < 0)
4144 i
= PyList_GET_SIZE(list
);
4146 while (PyDict_Next(*cellvars
, &pos
, &v
, &w
)) {
4147 w
= PyInt_FromLong(i
++); /* don't care about the old key */
4148 if (PyDict_SetItem(d
, v
, w
) < 0) {
4154 Py_DECREF(*cellvars
);
4163 symtable_freevar_offsets(PyObject
*freevars
, int offset
)
4168 /* The cell vars are the first elements of the closure,
4169 followed by the free vars. Update the offsets in
4170 c_freevars to account for number of cellvars. */
4172 while (PyDict_Next(freevars
, &pos
, &name
, &v
)) {
4173 int i
= PyInt_AS_LONG(v
) + offset
;
4174 PyObject
*o
= PyInt_FromLong(i
);
4177 if (PyDict_SetItem(freevars
, name
, o
) < 0) {
4187 symtable_check_unoptimized(struct compiling
*c
,
4188 PySymtableEntryObject
*ste
,
4189 struct symbol_info
*si
)
4193 if (!(si
->si_ncells
|| si
->si_nfrees
|| ste
->ste_child_free
4194 || (ste
->ste_nested
&& si
->si_nimplicit
)))
4197 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4199 #define ILLEGAL_IS "is a nested function"
4201 #define ILLEGAL_IMPORT_STAR \
4202 "import * is not allowed in function '%.100s' because it %s"
4204 #define ILLEGAL_BARE_EXEC \
4205 "unqualified exec is not allowed in function '%.100s' it %s"
4207 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4208 "function '%.100s' uses import * and bare exec, which are illegal" \
4211 /* XXX perhaps the linenos for these opt-breaking statements
4212 should be stored so the exception can point to them. */
4214 if (ste
->ste_child_free
) {
4215 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4216 sprintf(buf
, ILLEGAL_IMPORT_STAR
,
4217 PyString_AS_STRING(ste
->ste_name
),
4219 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4220 sprintf(buf
, ILLEGAL_BARE_EXEC
,
4221 PyString_AS_STRING(ste
->ste_name
),
4224 sprintf(buf
, ILLEGAL_EXEC_AND_IMPORT_STAR
,
4225 PyString_AS_STRING(ste
->ste_name
),
4229 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4230 sprintf(buf
, ILLEGAL_IMPORT_STAR
,
4231 PyString_AS_STRING(ste
->ste_name
),
4233 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4234 sprintf(buf
, ILLEGAL_BARE_EXEC
,
4235 PyString_AS_STRING(ste
->ste_name
),
4238 sprintf(buf
, ILLEGAL_EXEC_AND_IMPORT_STAR
,
4239 PyString_AS_STRING(ste
->ste_name
),
4244 if (c
->c_symtable
->st_nested_scopes
) {
4245 PyErr_SetString(PyExc_SyntaxError
, buf
);
4246 PyErr_SyntaxLocation(c
->c_symtable
->st_filename
,
4247 ste
->ste_opt_lineno
);
4251 return issue_warning(buf
, c
->c_filename
, ste
->ste_lineno
);
4257 symtable_check_shadow(struct symtable
*st
, PyObject
*name
, int flags
)
4260 PyObject
*children
, *v
;
4261 PySymtableEntryObject
*child
= NULL
;
4264 if (!(flags
& DEF_BOUND
))
4266 /* The semantics of this code will change with nested scopes.
4267 It is defined in the current scope and referenced in a
4268 child scope. Under the old rules, the child will see a
4269 global. Under the new rules, the child will see the
4270 binding in the current scope.
4273 /* Find name of child function that has free variable */
4274 children
= st
->st_cur
->ste_children
;
4275 for (i
= 0; i
< PyList_GET_SIZE(children
); i
++) {
4277 child
= (PySymtableEntryObject
*)PyList_GET_ITEM(children
, i
);
4278 v
= PyDict_GetItem(child
->ste_symbols
, name
);
4281 cflags
= PyInt_AS_LONG(v
);
4282 if (!(cflags
& DEF_BOUND
))
4286 assert(child
!= NULL
);
4288 sprintf(buf
, "local name '%.100s' in '%.100s' shadows "
4289 "use of '%.100s' as global in nested scope '%.100s'",
4290 PyString_AS_STRING(name
),
4291 PyString_AS_STRING(st
->st_cur
->ste_name
),
4292 PyString_AS_STRING(name
),
4293 PyString_AS_STRING(child
->ste_name
)
4296 return symtable_warn(st
, buf
);
4300 symtable_update_flags(struct compiling
*c
, PySymtableEntryObject
*ste
,
4301 struct symbol_info
*si
)
4303 if (c
->c_future
&& c
->c_future
->ff_nested_scopes
)
4304 c
->c_flags
|= CO_NESTED
;
4305 if (ste
->ste_type
!= TYPE_MODULE
)
4306 c
->c_flags
|= CO_NEWLOCALS
;
4307 if (ste
->ste_type
== TYPE_FUNCTION
) {
4308 c
->c_nlocals
= si
->si_nlocals
;
4309 if (ste
->ste_optimized
== 0)
4310 c
->c_flags
|= CO_OPTIMIZED
;
4311 else if (ste
->ste_optimized
!= OPT_EXEC
)
4312 return symtable_check_unoptimized(c
, ste
, si
);
4318 symtable_load_symbols(struct compiling
*c
)
4320 static PyObject
*implicit
= NULL
;
4321 struct symtable
*st
= c
->c_symtable
;
4322 PySymtableEntryObject
*ste
= st
->st_cur
;
4323 PyObject
*name
, *varnames
, *v
;
4325 struct symbol_info si
;
4327 if (implicit
== NULL
) {
4328 implicit
= PyInt_FromLong(1);
4329 if (implicit
== NULL
)
4334 if (symtable_init_compiling_symbols(c
) < 0)
4336 symtable_init_info(&si
);
4337 varnames
= st
->st_cur
->ste_varnames
;
4338 si
.si_nlocals
= PyList_GET_SIZE(varnames
);
4339 c
->c_argcount
= si
.si_nlocals
;
4341 for (i
= 0; i
< si
.si_nlocals
; ++i
) {
4342 v
= PyInt_FromLong(i
);
4343 if (PyDict_SetItem(c
->c_locals
,
4344 PyList_GET_ITEM(varnames
, i
), v
) < 0)
4349 /* XXX The cases below define the rules for whether a name is
4350 local or global. The logic could probably be clearer. */
4352 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
4353 flags
= PyInt_AS_LONG(v
);
4355 if (st
->st_nested_scopes
== 0
4356 && (flags
& (DEF_FREE
| DEF_FREE_CLASS
))) {
4357 if (symtable_check_shadow(st
, name
, flags
) < 0)
4361 if (flags
& DEF_FREE_GLOBAL
)
4362 /* undo the original DEF_FREE */
4363 flags
&= ~(DEF_FREE
| DEF_FREE_CLASS
);
4365 /* Deal with names that need two actions:
4366 1. Cell variables, which are also locals.
4367 2. Free variables in methods that are also class
4368 variables or declared global.
4370 if (flags
& (DEF_FREE
| DEF_FREE_CLASS
)) {
4371 if ((ste
->ste_type
== TYPE_CLASS
4372 && flags
!= DEF_FREE_CLASS
)
4373 || (flags
& (DEF_LOCAL
| DEF_PARAM
)))
4374 symtable_resolve_free(c
, name
, &si
);
4377 if (flags
& DEF_STAR
) {
4379 c
->c_flags
|= CO_VARARGS
;
4380 } else if (flags
& DEF_DOUBLESTAR
) {
4382 c
->c_flags
|= CO_VARKEYWORDS
;
4383 } else if (flags
& DEF_INTUPLE
)
4385 else if (flags
& DEF_GLOBAL
) {
4386 if (flags
& DEF_PARAM
) {
4387 PyErr_Format(PyExc_SyntaxError
, LOCAL_GLOBAL
,
4388 PyString_AS_STRING(name
));
4389 PyErr_SyntaxLocation(st
->st_filename
,
4394 if (PyDict_SetItem(c
->c_globals
, name
, Py_None
) < 0)
4396 } else if (flags
& DEF_FREE_GLOBAL
) {
4398 if (PyDict_SetItem(c
->c_globals
, name
, implicit
) < 0)
4400 } else if ((flags
& DEF_LOCAL
) && !(flags
& DEF_PARAM
)) {
4401 v
= PyInt_FromLong(si
.si_nlocals
++);
4404 if (PyDict_SetItem(c
->c_locals
, name
, v
) < 0)
4407 if (ste
->ste_type
!= TYPE_CLASS
)
4408 if (PyList_Append(c
->c_varnames
, name
) < 0)
4410 } else if (is_free(flags
)) {
4411 if (ste
->ste_nested
&& st
->st_nested_scopes
) {
4412 v
= PyInt_FromLong(si
.si_nfrees
++);
4415 if (PyDict_SetItem(c
->c_freevars
, name
, v
) < 0)
4420 if (PyDict_SetItem(c
->c_globals
, name
,
4423 if (st
->st_nscopes
!= 1) {
4424 v
= PyInt_FromLong(flags
);
4425 if (PyDict_SetItem(st
->st_global
,
4434 if (si
.si_ncells
> 1) { /* one cell is always in order */
4435 if (symtable_cellvar_offsets(&c
->c_cellvars
, c
->c_argcount
,
4436 c
->c_varnames
, c
->c_flags
) < 0)
4439 if (symtable_freevar_offsets(c
->c_freevars
, si
.si_ncells
) < 0)
4441 return symtable_update_flags(c
, ste
, &si
);
4443 /* is this always the right thing to do? */
4448 static struct symtable
*
4451 struct symtable
*st
;
4453 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
4457 st
->st_nested_scopes
= NESTED_SCOPES_DEFAULT
;
4458 st
->st_filename
= NULL
;
4459 if ((st
->st_stack
= PyList_New(0)) == NULL
)
4461 if ((st
->st_symbols
= PyDict_New()) == NULL
)
4467 st
->st_private
= NULL
;
4470 PySymtable_Free(st
);
4475 PySymtable_Free(struct symtable
*st
)
4477 Py_XDECREF(st
->st_symbols
);
4478 Py_XDECREF(st
->st_stack
);
4479 Py_XDECREF(st
->st_cur
);
4480 PyMem_Free((void *)st
);
4483 /* When the compiler exits a scope, it must should update the scope's
4484 free variable information with the list of free variables in its
4487 Variables that are free in children and defined in the current
4490 If the scope being exited is defined at the top-level (ste_nested is
4491 false), free variables in children that are not defined here are
4497 symtable_update_free_vars(struct symtable
*st
)
4500 PyObject
*o
, *name
, *list
= NULL
;
4501 PySymtableEntryObject
*child
, *ste
= st
->st_cur
;
4503 if (ste
->ste_type
== TYPE_CLASS
)
4504 def
= DEF_FREE_CLASS
;
4507 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4511 PyList_SetSlice(list
, 0,
4512 ((PyVarObject
*)list
)->ob_size
, 0);
4513 child
= (PySymtableEntryObject
*)
4514 PyList_GET_ITEM(ste
->ste_children
, i
);
4515 while (PyDict_Next(child
->ste_symbols
, &pos
, &name
, &o
)) {
4516 int flags
= PyInt_AS_LONG(o
);
4517 if (!(is_free(flags
)))
4518 continue; /* avoids indentation */
4520 list
= PyList_New(0);
4524 ste
->ste_child_free
= 1;
4525 if (PyList_Append(list
, name
) < 0) {
4530 for (j
= 0; list
&& j
< PyList_GET_SIZE(list
); j
++) {
4532 name
= PyList_GET_ITEM(list
, j
);
4533 v
= PyDict_GetItem(ste
->ste_symbols
, name
);
4534 /* If a name N is declared global in scope A and
4535 referenced in scope B contained (perhaps
4536 indirectly) in A and there are no scopes
4537 with bindings for N between B and A, then N
4540 if (v
&& (ste
->ste_type
!= TYPE_CLASS
)) {
4541 int flags
= PyInt_AS_LONG(v
);
4542 if (flags
& DEF_GLOBAL
) {
4543 symtable_undo_free(st
, child
->ste_id
,
4548 if (ste
->ste_nested
) {
4549 if (symtable_add_def_o(st
, ste
->ste_symbols
,
4555 if (symtable_check_global(st
, child
->ste_id
,
4568 /* If the current scope is a non-nested class or if name is not
4569 defined in the current, non-nested scope, then it is an implicit
4570 global in all nested scopes.
4574 symtable_check_global(struct symtable
*st
, PyObject
*child
, PyObject
*name
)
4578 PySymtableEntryObject
*ste
= st
->st_cur
;
4580 if (ste
->ste_type
== TYPE_CLASS
)
4581 return symtable_undo_free(st
, child
, name
);
4582 o
= PyDict_GetItem(ste
->ste_symbols
, name
);
4584 return symtable_undo_free(st
, child
, name
);
4585 v
= PyInt_AS_LONG(o
);
4587 if (is_free(v
) || (v
& DEF_GLOBAL
))
4588 return symtable_undo_free(st
, child
, name
);
4590 return symtable_add_def_o(st
, ste
->ste_symbols
,
4595 symtable_undo_free(struct symtable
*st
, PyObject
*id
,
4600 PySymtableEntryObject
*ste
;
4602 ste
= (PySymtableEntryObject
*)PyDict_GetItem(st
->st_symbols
, id
);
4606 info
= PyDict_GetItem(ste
->ste_symbols
, name
);
4609 v
= PyInt_AS_LONG(info
);
4611 if (symtable_add_def_o(st
, ste
->ste_symbols
, name
,
4612 DEF_FREE_GLOBAL
) < 0)
4615 /* If the name is defined here or declared global,
4616 then the recursion stops. */
4619 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4620 PySymtableEntryObject
*child
;
4621 child
= (PySymtableEntryObject
*)
4622 PyList_GET_ITEM(ste
->ste_children
, i
);
4623 x
= symtable_undo_free(st
, child
->ste_id
, name
);
4630 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4631 This reference is released when the scope is exited, via the DECREF
4632 in symtable_exit_scope().
4636 symtable_exit_scope(struct symtable
*st
)
4640 if (st
->st_pass
== 1)
4641 symtable_update_free_vars(st
);
4642 Py_DECREF(st
->st_cur
);
4643 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
4644 st
->st_cur
= (PySymtableEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
4646 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
4652 symtable_enter_scope(struct symtable
*st
, char *name
, int type
,
4655 PySymtableEntryObject
*prev
= NULL
;
4659 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
4660 Py_DECREF(st
->st_cur
);
4665 st
->st_cur
= (PySymtableEntryObject
*)
4666 PySymtableEntry_New(st
, name
, type
, lineno
);
4667 if (strcmp(name
, TOP
) == 0)
4668 st
->st_global
= st
->st_cur
->ste_symbols
;
4669 if (prev
&& st
->st_pass
== 1) {
4670 if (PyList_Append(prev
->ste_children
,
4671 (PyObject
*)st
->st_cur
) < 0)
4677 symtable_lookup(struct symtable
*st
, char *name
)
4679 char buffer
[MANGLE_LEN
];
4683 if (mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4685 v
= PyDict_GetItemString(st
->st_cur
->ste_symbols
, name
);
4687 if (PyErr_Occurred())
4693 flags
= PyInt_AS_LONG(v
);
4698 symtable_add_def(struct symtable
*st
, char *name
, int flag
)
4701 char buffer
[MANGLE_LEN
];
4704 if (mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4706 if ((s
= PyString_InternFromString(name
)) == NULL
)
4708 ret
= symtable_add_def_o(st
, st
->st_cur
->ste_symbols
, s
, flag
);
4713 /* Must only be called with mangled names */
4716 symtable_add_def_o(struct symtable
*st
, PyObject
*dict
,
4717 PyObject
*name
, int flag
)
4722 if ((o
= PyDict_GetItem(dict
, name
))) {
4723 val
= PyInt_AS_LONG(o
);
4724 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
4725 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
4726 PyString_AsString(name
));
4727 PyErr_SyntaxLocation(st
->st_filename
,
4728 st
->st_cur
->ste_lineno
);
4734 o
= PyInt_FromLong(val
);
4735 if (PyDict_SetItem(dict
, name
, o
) < 0) {
4741 if (flag
& DEF_PARAM
) {
4742 if (PyList_Append(st
->st_cur
->ste_varnames
, name
) < 0)
4744 } else if (flag
& DEF_GLOBAL
) {
4745 /* XXX need to update DEF_GLOBAL for other flags too;
4746 perhaps only DEF_FREE_GLOBAL */
4747 if ((o
= PyDict_GetItem(st
->st_global
, name
))) {
4748 val
= PyInt_AS_LONG(o
);
4752 o
= PyInt_FromLong(val
);
4753 if (PyDict_SetItem(st
->st_global
, name
, o
) < 0) {
4762 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4765 symtable_node(struct symtable
*st
, node
*n
)
4772 char *func_name
= STR(CHILD(n
, 1));
4773 symtable_add_def(st
, func_name
, DEF_LOCAL
);
4774 symtable_default_args(st
, CHILD(n
, 2));
4775 symtable_enter_scope(st
, func_name
, TYPE(n
), n
->n_lineno
);
4776 symtable_funcdef(st
, n
);
4777 symtable_exit_scope(st
);
4782 symtable_default_args(st
, CHILD(n
, 1));
4783 symtable_enter_scope(st
, "lambda", TYPE(n
), n
->n_lineno
);
4784 symtable_funcdef(st
, n
);
4785 symtable_exit_scope(st
);
4788 char *tmp
, *class_name
= STR(CHILD(n
, 1));
4789 symtable_add_def(st
, class_name
, DEF_LOCAL
);
4790 if (TYPE(CHILD(n
, 2)) == LPAR
) {
4791 node
*bases
= CHILD(n
, 3);
4793 for (i
= 0; i
< NCH(bases
); i
+= 2) {
4794 symtable_node(st
, CHILD(bases
, i
));
4797 symtable_enter_scope(st
, class_name
, TYPE(n
), n
->n_lineno
);
4798 tmp
= st
->st_private
;
4799 st
->st_private
= class_name
;
4800 symtable_node(st
, CHILD(n
, NCH(n
) - 1));
4801 st
->st_private
= tmp
;
4802 symtable_exit_scope(st
);
4806 for (i
= 0; i
+ 3 < NCH(n
); i
+= 4) {
4807 if (is_constant_false(NULL
, (CHILD(n
, i
+ 1))))
4809 symtable_node(st
, CHILD(n
, i
+ 1));
4810 symtable_node(st
, CHILD(n
, i
+ 3));
4813 symtable_node(st
, CHILD(n
, i
+ 2));
4816 symtable_global(st
, n
);
4819 symtable_import(st
, n
);
4822 st
->st_cur
->ste_optimized
|= OPT_EXEC
;
4823 symtable_node(st
, CHILD(n
, 1));
4825 symtable_node(st
, CHILD(n
, 3));
4827 st
->st_cur
->ste_optimized
|= OPT_BARE_EXEC
;
4828 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
4831 symtable_node(st
, CHILD(n
, 5));
4836 if (Py_OptimizeFlag
)
4842 symtable_node(st
, CHILD(n
, 1));
4848 symtable_assign(st
, CHILD(n
, 3), 0);
4855 symtable_assign(st
, CHILD(n
, 1), 0);
4861 if (TYPE(CHILD(n
, 1)) == augassign
) {
4862 symtable_assign(st
, CHILD(n
, 0), 0);
4863 symtable_node(st
, CHILD(n
, 2));
4867 for (i
= 0; i
< NCH(n
) - 2; i
+= 2)
4868 symtable_assign(st
, CHILD(n
, i
), 0);
4869 n
= CHILD(n
, NCH(n
) - 1);
4873 /* watchout for fall-through logic below */
4880 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
4882 symtable_list_comprehension(st
, CHILD(n
, 1));
4883 symtable_node(st
, CHILD(n
, 0));
4888 if (TYPE(n
) == atom
&& TYPE(CHILD(n
, 0)) == NAME
) {
4889 symtable_add_use(st
, STR(CHILD(n
, 0)));
4893 if (TYPE(n
) == for_stmt
) {
4894 symtable_assign(st
, CHILD(n
, 1), 0);
4902 for (i
= start
; i
< NCH(n
); ++i
)
4903 if (TYPE(CHILD(n
, i
)) >= single_input
)
4904 symtable_node(st
, CHILD(n
, i
));
4909 symtable_funcdef(struct symtable
*st
, node
*n
)
4913 if (TYPE(n
) == lambdef
) {
4915 symtable_params(st
, CHILD(n
, 1));
4917 symtable_params(st
, CHILD(n
, 2));
4918 body
= CHILD(n
, NCH(n
) - 1);
4919 symtable_node(st
, body
);
4922 /* The next two functions parse the argument tuple.
4923 symtable_default_arg() checks for names in the default arguments,
4924 which are references in the defining scope. symtable_params()
4925 parses the parameter names, which are defined in the function's
4929 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
4930 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4934 symtable_default_args(struct symtable
*st
, node
*n
)
4939 if (TYPE(n
) == parameters
) {
4941 if (TYPE(n
) == RPAR
)
4944 REQ(n
, varargslist
);
4945 for (i
= 0; i
< NCH(n
); i
+= 2) {
4947 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
4950 if (i
> 0 && (TYPE(CHILD(n
, i
- 1)) == EQUAL
))
4951 symtable_node(st
, CHILD(n
, i
));
4956 symtable_params(struct symtable
*st
, node
*n
)
4958 int i
, complex = -1, ext
= 0;
4961 if (TYPE(n
) == parameters
) {
4963 if (TYPE(n
) == RPAR
)
4966 REQ(n
, varargslist
);
4967 for (i
= 0; i
< NCH(n
); i
+= 2) {
4969 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
4973 if (TYPE(c
) == test
) {
4976 if (TYPE(CHILD(c
, 0)) == NAME
)
4977 symtable_add_def(st
, STR(CHILD(c
, 0)), DEF_PARAM
);
4980 sprintf(nbuf
, ".%d", i
);
4981 symtable_add_def(st
, nbuf
, DEF_PARAM
);
4987 if (TYPE(c
) == STAR
) {
4989 symtable_add_def(st
, STR(CHILD(n
, i
)),
4990 DEF_PARAM
| DEF_STAR
);
4997 if (c
&& TYPE(c
) == DOUBLESTAR
) {
4999 symtable_add_def(st
, STR(CHILD(n
, i
)),
5000 DEF_PARAM
| DEF_DOUBLESTAR
);
5005 for (j
= 0; j
<= complex; j
++) {
5007 if (TYPE(c
) == COMMA
)
5009 else if (TYPE(c
) == EQUAL
)
5010 c
= CHILD(n
, j
+= 3);
5011 if (TYPE(CHILD(c
, 0)) == LPAR
)
5012 symtable_params_fplist(st
, CHILD(c
, 1));
5018 symtable_params_fplist(struct symtable
*st
, node
*n
)
5024 for (i
= 0; i
< NCH(n
); i
+= 2) {
5028 symtable_add_def(st
, STR(CHILD(c
, 0)),
5029 DEF_PARAM
| DEF_INTUPLE
);
5031 symtable_params_fplist(st
, CHILD(c
, 1));
5037 symtable_global(struct symtable
*st
, node
*n
)
5041 /* XXX It might be helpful to warn about module-level global
5042 statements, but it's hard to tell the difference between
5043 module-level and a string passed to exec.
5046 for (i
= 1; i
< NCH(n
); i
+= 2) {
5047 char *name
= STR(CHILD(n
, i
));
5050 flags
= symtable_lookup(st
, name
);
5053 if (flags
&& flags
!= DEF_GLOBAL
) {
5055 if (flags
& DEF_PARAM
) {
5056 PyErr_Format(PyExc_SyntaxError
,
5057 "name '%.400s' is local and global",
5059 PyErr_SyntaxLocation(st
->st_filename
,
5060 st
->st_cur
->ste_lineno
);
5065 if (flags
& DEF_LOCAL
)
5066 sprintf(buf
, GLOBAL_AFTER_ASSIGN
,
5069 sprintf(buf
, GLOBAL_AFTER_USE
, name
);
5070 symtable_warn(st
, buf
);
5073 symtable_add_def(st
, name
, DEF_GLOBAL
);
5078 symtable_list_comprehension(struct symtable
*st
, node
*n
)
5082 sprintf(tmpname
, "_[%d]", st
->st_tmpname
);
5083 symtable_add_def(st
, tmpname
, DEF_LOCAL
);
5084 symtable_assign(st
, CHILD(n
, 1), 0);
5085 symtable_node(st
, CHILD(n
, 3));
5087 symtable_node(st
, CHILD(n
, 4));
5091 symtable_import(struct symtable
*st
, node
*n
)
5094 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5095 | 'from' dotted_name 'import'
5096 ('*' | import_as_name (',' import_as_name)*)
5097 import_as_name: NAME [NAME NAME]
5099 if (STR(CHILD(n
, 0))[0] == 'f') { /* from */
5100 node
*dotname
= CHILD(n
, 1);
5101 if (strcmp(STR(CHILD(dotname
, 0)), "__future__") == 0) {
5102 /* check for bogus imports */
5103 if (n
->n_lineno
>= st
->st_future
->ff_last_lineno
) {
5104 PyErr_SetString(PyExc_SyntaxError
,
5106 PyErr_SyntaxLocation(st
->st_filename
,
5112 if (TYPE(CHILD(n
, 3)) == STAR
) {
5113 st
->st_cur
->ste_optimized
|= OPT_IMPORT_STAR
;
5114 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
5116 for (i
= 3; i
< NCH(n
); i
+= 2) {
5117 node
*c
= CHILD(n
, i
);
5118 if (NCH(c
) > 1) /* import as */
5119 symtable_assign(st
, CHILD(c
, 2),
5122 symtable_assign(st
, CHILD(c
, 0),
5127 for (i
= 1; i
< NCH(n
); i
+= 2) {
5128 symtable_assign(st
, CHILD(n
, i
), DEF_IMPORT
);
5134 symtable_assign(struct symtable
*st
, node
*n
, int flag
)
5142 /* invalid assignment, e.g. lambda x:x=2. The next
5143 pass will catch this error. */
5147 for (i
= 2; i
< NCH(n
); ++i
)
5148 if (TYPE(CHILD(n
, i
)) != DOUBLESTAR
)
5149 symtable_node(st
, CHILD(n
, i
));
5152 symtable_node(st
, CHILD(n
, 0));
5153 symtable_node(st
, CHILD(n
, 1));
5160 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
5161 /* XXX This is an error, but the next pass
5165 for (i
= 0; i
< NCH(n
); i
+= 2)
5166 symtable_assign(st
, CHILD(n
, i
), flag
);
5177 for (i
= 0; i
< NCH(n
); i
+= 2)
5178 symtable_assign(st
, CHILD(n
, i
), flag
);
5184 if (TYPE(tmp
) == LPAR
|| TYPE(tmp
) == LSQB
) {
5187 } else if (TYPE(tmp
) == NAME
) {
5188 if (strcmp(STR(tmp
), "__debug__") == 0) {
5189 PyErr_SetString(PyExc_SyntaxError
,
5191 PyErr_SyntaxLocation(st
->st_filename
,
5195 symtable_add_def(st
, STR(tmp
), DEF_LOCAL
| flag
);
5198 case dotted_as_name
:
5200 symtable_add_def(st
, STR(CHILD(n
, 2)),
5203 symtable_add_def(st
,
5209 symtable_add_def(st
, STR(CHILD(n
, 0)), DEF_LOCAL
| flag
);
5212 symtable_add_def(st
, STR(n
), DEF_LOCAL
| flag
);
5221 /* Should only occur for errors like x + 1 = 1,
5222 which will be caught in the next pass. */
5223 for (i
= 0; i
< NCH(n
); ++i
)
5224 if (TYPE(CHILD(n
, i
)) >= single_input
)
5225 symtable_assign(st
, CHILD(n
, i
), flag
);