2 /* Compile an expression node to intermediate code */
5 XXX add __doc__ attribute == co_doc to code object attributes?
6 XXX (it's currently the first item of the co_const tuple)
7 XXX Generate simple jump for break/return outside 'try...finally'
8 XXX Allow 'continue' inside finally clause of try-finally
9 XXX New opcode for loading the initial index for a for loop
21 #include "structmember.h"
25 /* Three symbols from graminit.h are also defined in Python.h, with
26 Py_ prefixes to their names. Python.h can't include graminit.h
27 (which defines too many confusing symbols), but we can check here
28 that they haven't changed (which is very unlikely, but possible). */
29 #if Py_single_input != single_input
30 #error "single_input has changed -- update Py_single_input in Python.h"
32 #if Py_file_input != file_input
33 #error "file_input has changed -- update Py_file_input in Python.h"
35 #if Py_eval_input != eval_input
36 #error "eval_input has changed -- update Py_eval_input in Python.h"
39 int Py_OptimizeFlag
= 0;
49 #define DEL_CLOSURE_ERROR \
50 "can not delete variable '%.400s' referenced in nested scope"
52 #define DUPLICATE_ARGUMENT \
53 "duplicate argument '%s' in function definition"
55 #define ILLEGAL_DYNAMIC_SCOPE \
56 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
58 #define GLOBAL_AFTER_ASSIGN \
59 "name '%.400s' is assigned to before global declaration"
61 #define GLOBAL_AFTER_USE \
62 "name '%.400s' is used prior to global declaration"
64 #define LOCAL_GLOBAL \
65 "name '%.400s' is a function paramter and declared global"
68 "from __future__ imports must occur at the beginning of the file"
70 #define MANGLE_LEN 256
72 #define OFF(x) offsetof(PyCodeObject, x)
74 static struct memberlist code_memberlist
[] = {
75 {"co_argcount", T_INT
, OFF(co_argcount
), READONLY
},
76 {"co_nlocals", T_INT
, OFF(co_nlocals
), READONLY
},
77 {"co_stacksize",T_INT
, OFF(co_stacksize
), READONLY
},
78 {"co_flags", T_INT
, OFF(co_flags
), READONLY
},
79 {"co_code", T_OBJECT
, OFF(co_code
), READONLY
},
80 {"co_consts", T_OBJECT
, OFF(co_consts
), READONLY
},
81 {"co_names", T_OBJECT
, OFF(co_names
), READONLY
},
82 {"co_varnames", T_OBJECT
, OFF(co_varnames
), READONLY
},
83 {"co_freevars", T_OBJECT
, OFF(co_freevars
), READONLY
},
84 {"co_cellvars", T_OBJECT
, OFF(co_cellvars
), READONLY
},
85 {"co_filename", T_OBJECT
, OFF(co_filename
), READONLY
},
86 {"co_name", T_OBJECT
, OFF(co_name
), READONLY
},
87 {"co_firstlineno", T_INT
, OFF(co_firstlineno
), READONLY
},
88 {"co_lnotab", T_OBJECT
, OFF(co_lnotab
), READONLY
},
93 code_getattr(PyCodeObject
*co
, char *name
)
95 return PyMember_Get((char *)co
, code_memberlist
, name
);
99 code_dealloc(PyCodeObject
*co
)
101 Py_XDECREF(co
->co_code
);
102 Py_XDECREF(co
->co_consts
);
103 Py_XDECREF(co
->co_names
);
104 Py_XDECREF(co
->co_varnames
);
105 Py_XDECREF(co
->co_freevars
);
106 Py_XDECREF(co
->co_cellvars
);
107 Py_XDECREF(co
->co_filename
);
108 Py_XDECREF(co
->co_name
);
109 Py_XDECREF(co
->co_lnotab
);
114 code_repr(PyCodeObject
*co
)
118 char *filename
= "???";
121 if (co
->co_firstlineno
!= 0)
122 lineno
= co
->co_firstlineno
;
123 if (co
->co_filename
&& PyString_Check(co
->co_filename
))
124 filename
= PyString_AS_STRING(co
->co_filename
);
125 if (co
->co_name
&& PyString_Check(co
->co_name
))
126 name
= PyString_AS_STRING(co
->co_name
);
127 sprintf(buf
, "<code object %.100s at %p, file \"%.300s\", line %d>",
128 name
, co
, filename
, lineno
);
129 return PyString_FromString(buf
);
133 code_compare(PyCodeObject
*co
, PyCodeObject
*cp
)
136 cmp
= PyObject_Compare(co
->co_name
, cp
->co_name
);
138 cmp
= co
->co_argcount
- cp
->co_argcount
;
140 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
142 cmp
= co
->co_flags
- cp
->co_flags
;
144 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
146 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
148 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
150 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
152 cmp
= PyObject_Compare(co
->co_freevars
, cp
->co_freevars
);
154 cmp
= PyObject_Compare(co
->co_cellvars
, cp
->co_cellvars
);
159 code_hash(PyCodeObject
*co
)
161 long h
, h0
, h1
, h2
, h3
, h4
, h5
, h6
;
162 h0
= PyObject_Hash(co
->co_name
);
163 if (h0
== -1) return -1;
164 h1
= PyObject_Hash(co
->co_code
);
165 if (h1
== -1) return -1;
166 h2
= PyObject_Hash(co
->co_consts
);
167 if (h2
== -1) return -1;
168 h3
= PyObject_Hash(co
->co_names
);
169 if (h3
== -1) return -1;
170 h4
= PyObject_Hash(co
->co_varnames
);
171 if (h4
== -1) return -1;
172 h5
= PyObject_Hash(co
->co_freevars
);
173 if (h5
== -1) return -1;
174 h6
= PyObject_Hash(co
->co_cellvars
);
175 if (h6
== -1) return -1;
176 h
= h0
^ h1
^ h2
^ h3
^ h4
^ h5
^ h6
^
177 co
->co_argcount
^ co
->co_nlocals
^ co
->co_flags
;
182 /* XXX code objects need to participate in GC? */
184 PyTypeObject PyCode_Type
= {
185 PyObject_HEAD_INIT(&PyType_Type
)
188 sizeof(PyCodeObject
),
190 (destructor
)code_dealloc
, /*tp_dealloc*/
192 (getattrfunc
)code_getattr
, /*tp_getattr*/
194 (cmpfunc
)code_compare
, /*tp_compare*/
195 (reprfunc
)code_repr
, /*tp_repr*/
197 0, /*tp_as_sequence*/
199 (hashfunc
)code_hash
, /*tp_hash*/
203 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
205 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
208 all_name_chars(unsigned char *s
)
210 static char ok_name_char
[256];
211 static unsigned char *name_chars
= (unsigned char *)NAME_CHARS
;
213 if (ok_name_char
[*name_chars
] == 0) {
215 for (p
= name_chars
; *p
; p
++)
216 ok_name_char
[*p
] = 1;
219 if (ok_name_char
[*s
++] == 0)
226 intern_strings(PyObject
*tuple
)
230 for (i
= PyTuple_GET_SIZE(tuple
); --i
>= 0; ) {
231 PyObject
*v
= PyTuple_GET_ITEM(tuple
, i
);
232 if (v
== NULL
|| !PyString_Check(v
)) {
233 Py_FatalError("non-string found in code slot");
234 PyErr_BadInternalCall();
237 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple
, i
));
243 PyCode_New(int argcount
, int nlocals
, int stacksize
, int flags
,
244 PyObject
*code
, PyObject
*consts
, PyObject
*names
,
245 PyObject
*varnames
, PyObject
*freevars
, PyObject
*cellvars
,
246 PyObject
*filename
, PyObject
*name
, int firstlineno
,
252 /* Check argument types */
253 if (argcount
< 0 || nlocals
< 0 ||
255 consts
== NULL
|| !PyTuple_Check(consts
) ||
256 names
== NULL
|| !PyTuple_Check(names
) ||
257 varnames
== NULL
|| !PyTuple_Check(varnames
) ||
258 freevars
== NULL
|| !PyTuple_Check(freevars
) ||
259 cellvars
== NULL
|| !PyTuple_Check(cellvars
) ||
260 name
== NULL
|| !PyString_Check(name
) ||
261 filename
== NULL
|| !PyString_Check(filename
) ||
262 lnotab
== NULL
|| !PyString_Check(lnotab
)) {
263 PyErr_BadInternalCall();
266 pb
= code
->ob_type
->tp_as_buffer
;
268 pb
->bf_getreadbuffer
== NULL
||
269 pb
->bf_getsegcount
== NULL
||
270 (*pb
->bf_getsegcount
)(code
, NULL
) != 1)
272 PyErr_BadInternalCall();
275 intern_strings(names
);
276 intern_strings(varnames
);
277 if (freevars
== NULL
)
278 freevars
= PyTuple_New(0);
279 intern_strings(freevars
);
280 if (cellvars
== NULL
)
281 cellvars
= PyTuple_New(0);
282 intern_strings(cellvars
);
283 /* Intern selected string constants */
284 for (i
= PyTuple_Size(consts
); --i
>= 0; ) {
285 PyObject
*v
= PyTuple_GetItem(consts
, i
);
286 if (!PyString_Check(v
))
288 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v
)))
290 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
292 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
294 co
->co_argcount
= argcount
;
295 co
->co_nlocals
= nlocals
;
296 co
->co_stacksize
= stacksize
;
297 co
->co_flags
= flags
;
301 co
->co_consts
= consts
;
303 co
->co_names
= names
;
305 co
->co_varnames
= varnames
;
307 co
->co_freevars
= freevars
;
309 co
->co_cellvars
= cellvars
;
311 co
->co_filename
= filename
;
314 co
->co_firstlineno
= firstlineno
;
316 co
->co_lnotab
= lnotab
;
322 /* Data structure used internally */
324 /* The compiler uses two passes to generate bytecodes. The first pass
325 builds the symbol table. The second pass generates the bytecode.
327 The first pass uses a single symtable struct. The second pass uses
328 a compiling struct for each code block. The compiling structs
329 share a reference to the symtable.
331 The two passes communicate via symtable_load_symbols() and via
332 is_local() and is_global(). The former initializes several slots
333 in the compiling struct: c_varnames, c_locals, c_nlocals,
334 c_argcount, c_globals, and c_flags.
338 PyObject
*c_code
; /* string */
339 PyObject
*c_consts
; /* list of objects */
340 PyObject
*c_const_dict
; /* inverse of c_consts */
341 PyObject
*c_names
; /* list of strings (names) */
342 PyObject
*c_name_dict
; /* inverse of c_names */
343 PyObject
*c_globals
; /* dictionary (value=None) */
344 PyObject
*c_locals
; /* dictionary (value=localID) */
345 PyObject
*c_varnames
; /* list (inverse of c_locals) */
346 PyObject
*c_freevars
; /* dictionary (value=None) */
347 PyObject
*c_cellvars
; /* list */
348 int c_nlocals
; /* index of next local */
349 int c_argcount
; /* number of top-level arguments */
350 int c_flags
; /* same as co_flags */
351 int c_nexti
; /* index into c_code */
352 int c_errors
; /* counts errors occurred */
353 int c_infunction
; /* set when compiling a function */
354 int c_interactive
; /* generating code for interactive command */
355 int c_loops
; /* counts nested loops */
356 int c_begin
; /* begin of current loop, for 'continue' */
357 int c_block
[CO_MAXBLOCKS
]; /* stack of block types */
358 int c_nblocks
; /* current block stack level */
359 char *c_filename
; /* filename of current node */
360 char *c_name
; /* name of object (e.g. function) */
361 int c_lineno
; /* Current line number */
362 int c_stacklevel
; /* Current stack level */
363 int c_maxstacklevel
; /* Maximum stack level */
365 PyObject
*c_lnotab
; /* Table mapping address to line number */
366 int c_last_addr
, c_last_line
, c_lnotab_next
;
367 char *c_private
; /* for private name mangling */
368 int c_tmpname
; /* temporary local name counter */
369 int c_nested
; /* Is block nested funcdef or lamdef? */
370 int c_closure
; /* Is nested w/freevars? */
371 struct symtable
*c_symtable
; /* pointer to module symbol table */
372 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
377 if ((v
& (USE
| DEF_FREE
))
378 && !(v
& (DEF_LOCAL
| DEF_PARAM
| DEF_GLOBAL
)))
380 if (v
& DEF_FREE_CLASS
)
386 com_error(struct compiling
*c
, PyObject
*exc
, char *msg
)
388 PyObject
*t
= NULL
, *v
= NULL
, *w
= NULL
, *line
= NULL
;
391 /* Error occurred via symtable call to
393 PyErr_SetString(exc
, msg
);
397 if (c
->c_lineno
< 1 || c
->c_interactive
) {
398 /* Unknown line number or interactive input */
399 PyErr_SetString(exc
, msg
);
402 v
= PyString_FromString(msg
);
404 return; /* MemoryError, too bad */
406 line
= PyErr_ProgramText(c
->c_filename
, c
->c_lineno
);
411 t
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->c_lineno
,
415 w
= Py_BuildValue("(OO)", v
, t
);
418 PyErr_SetObject(exc
, w
);
426 /* Interface to the block stack */
429 block_push(struct compiling
*c
, int type
)
431 if (c
->c_nblocks
>= CO_MAXBLOCKS
) {
432 com_error(c
, PyExc_SystemError
,
433 "too many statically nested blocks");
436 c
->c_block
[c
->c_nblocks
++] = type
;
441 block_pop(struct compiling
*c
, int type
)
443 if (c
->c_nblocks
> 0)
445 if (c
->c_block
[c
->c_nblocks
] != type
&& c
->c_errors
== 0) {
446 com_error(c
, PyExc_SystemError
, "bad block pop");
450 /* Prototype forward declarations */
452 static int com_init(struct compiling
*, char *);
453 static void com_free(struct compiling
*);
454 static void com_push(struct compiling
*, int);
455 static void com_pop(struct compiling
*, int);
456 static void com_done(struct compiling
*);
457 static void com_node(struct compiling
*, node
*);
458 static void com_factor(struct compiling
*, node
*);
459 static void com_addbyte(struct compiling
*, int);
460 static void com_addint(struct compiling
*, int);
461 static void com_addoparg(struct compiling
*, int, int);
462 static void com_addfwref(struct compiling
*, int, int *);
463 static void com_backpatch(struct compiling
*, int);
464 static int com_add(struct compiling
*, PyObject
*, PyObject
*, PyObject
*);
465 static int com_addconst(struct compiling
*, PyObject
*);
466 static int com_addname(struct compiling
*, PyObject
*);
467 static void com_addopname(struct compiling
*, int, node
*);
468 static void com_list(struct compiling
*, node
*, int);
469 static void com_list_iter(struct compiling
*, node
*, node
*, char *);
470 static int com_argdefs(struct compiling
*, node
*);
471 static void com_assign(struct compiling
*, node
*, int, node
*);
472 static void com_assign_name(struct compiling
*, node
*, int);
473 static PyCodeObject
*icompile(node
*, struct compiling
*);
474 static PyCodeObject
*jcompile(node
*, char *, struct compiling
*,
476 static PyObject
*parsestrplus(node
*);
477 static PyObject
*parsestr(char *);
478 static node
*get_rawdocstring(node
*);
480 static int get_ref_type(struct compiling
*, char *);
482 /* symtable operations */
483 static int symtable_build(struct compiling
*, node
*);
484 static int symtable_load_symbols(struct compiling
*);
485 static struct symtable
*symtable_init(void);
486 static void symtable_enter_scope(struct symtable
*, char *, int, int);
487 static int symtable_exit_scope(struct symtable
*);
488 static int symtable_add_def(struct symtable
*, char *, int);
489 static int symtable_add_def_o(struct symtable
*, PyObject
*, PyObject
*, int);
491 static void symtable_node(struct symtable
*, node
*);
492 static void symtable_funcdef(struct symtable
*, node
*);
493 static void symtable_default_args(struct symtable
*, node
*);
494 static void symtable_params(struct symtable
*, node
*);
495 static void symtable_params_fplist(struct symtable
*, node
*n
);
496 static void symtable_global(struct symtable
*, node
*);
497 static void symtable_import(struct symtable
*, node
*);
498 static void symtable_assign(struct symtable
*, node
*, int);
499 static void symtable_list_comprehension(struct symtable
*, node
*);
501 static int symtable_update_free_vars(struct symtable
*);
502 static int symtable_undo_free(struct symtable
*, PyObject
*, PyObject
*);
503 static int symtable_check_global(struct symtable
*, PyObject
*, PyObject
*);
510 for (i
= 0; i
< pad
; ++i
)
511 fprintf(stderr
, " ");
515 dump(node
*n
, int pad
, int depth
)
521 fprintf(stderr
, "%d: %s\n", TYPE(n
), STR(n
));
524 for (i
= 0; i
< NCH(n
); ++i
)
525 dump(CHILD(n
, i
), pad
+ 1, depth
);
528 #define DUMP(N) dump(N, 0, -1)
531 com_init(struct compiling
*c
, char *filename
)
533 memset((void *)c
, '\0', sizeof(struct compiling
));
534 if ((c
->c_code
= PyString_FromStringAndSize((char *)NULL
,
537 if ((c
->c_consts
= PyList_New(0)) == NULL
)
539 if ((c
->c_const_dict
= PyDict_New()) == NULL
)
541 if ((c
->c_names
= PyList_New(0)) == NULL
)
543 if ((c
->c_name_dict
= PyDict_New()) == NULL
)
545 if ((c
->c_locals
= PyDict_New()) == NULL
)
547 if ((c
->c_lnotab
= PyString_FromStringAndSize((char *)NULL
,
551 c
->c_varnames
= NULL
;
552 c
->c_freevars
= NULL
;
553 c
->c_cellvars
= NULL
;
560 c
->c_interactive
= 0;
564 c
->c_filename
= filename
;
568 c
->c_maxstacklevel
= 0;
569 c
->c_firstlineno
= 0;
572 c
->c_lnotab_next
= 0;
576 c
->c_symtable
= NULL
;
585 com_free(struct compiling
*c
)
587 Py_XDECREF(c
->c_code
);
588 Py_XDECREF(c
->c_consts
);
589 Py_XDECREF(c
->c_const_dict
);
590 Py_XDECREF(c
->c_names
);
591 Py_XDECREF(c
->c_name_dict
);
592 Py_XDECREF(c
->c_globals
);
593 Py_XDECREF(c
->c_locals
);
594 Py_XDECREF(c
->c_varnames
);
595 Py_XDECREF(c
->c_freevars
);
596 Py_XDECREF(c
->c_cellvars
);
597 Py_XDECREF(c
->c_lnotab
);
599 PyMem_Free((void *)c
->c_future
);
603 com_push(struct compiling
*c
, int n
)
605 c
->c_stacklevel
+= n
;
606 if (c
->c_stacklevel
> c
->c_maxstacklevel
)
607 c
->c_maxstacklevel
= c
->c_stacklevel
;
611 com_pop(struct compiling
*c
, int n
)
613 if (c
->c_stacklevel
< n
) {
615 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
616 c->c_filename, c->c_lineno,
617 c->c_nexti, c->c_stacklevel, n); */
621 c
->c_stacklevel
-= n
;
625 com_done(struct compiling
*c
)
627 if (c
->c_code
!= NULL
)
628 _PyString_Resize(&c
->c_code
, c
->c_nexti
);
629 if (c
->c_lnotab
!= NULL
)
630 _PyString_Resize(&c
->c_lnotab
, c
->c_lnotab_next
);
634 com_addbyte(struct compiling
*c
, int byte
)
637 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
638 assert(byte
>= 0 && byte
<= 255);
639 if (byte
< 0 || byte
> 255) {
640 com_error(c
, PyExc_SystemError
,
641 "com_addbyte: byte out of range");
643 if (c
->c_code
== NULL
)
645 len
= PyString_Size(c
->c_code
);
646 if (c
->c_nexti
>= len
) {
647 if (_PyString_Resize(&c
->c_code
, len
+1000) != 0) {
652 PyString_AsString(c
->c_code
)[c
->c_nexti
++] = byte
;
656 com_addint(struct compiling
*c
, int x
)
658 com_addbyte(c
, x
& 0xff);
659 com_addbyte(c
, x
>> 8); /* XXX x should be positive */
663 com_add_lnotab(struct compiling
*c
, int addr
, int line
)
667 if (c
->c_lnotab
== NULL
)
669 size
= PyString_Size(c
->c_lnotab
);
670 if (c
->c_lnotab_next
+2 > size
) {
671 if (_PyString_Resize(&c
->c_lnotab
, size
+ 1000) < 0) {
676 p
= PyString_AsString(c
->c_lnotab
) + c
->c_lnotab_next
;
679 c
->c_lnotab_next
+= 2;
683 com_set_lineno(struct compiling
*c
, int lineno
)
685 c
->c_lineno
= lineno
;
686 if (c
->c_firstlineno
== 0) {
687 c
->c_firstlineno
= c
->c_last_line
= lineno
;
690 int incr_addr
= c
->c_nexti
- c
->c_last_addr
;
691 int incr_line
= lineno
- c
->c_last_line
;
692 while (incr_addr
> 0 || incr_line
> 0) {
693 int trunc_addr
= incr_addr
;
694 int trunc_line
= incr_line
;
695 if (trunc_addr
> 255)
697 if (trunc_line
> 255)
699 com_add_lnotab(c
, trunc_addr
, trunc_line
);
700 incr_addr
-= trunc_addr
;
701 incr_line
-= trunc_line
;
703 c
->c_last_addr
= c
->c_nexti
;
704 c
->c_last_line
= lineno
;
709 com_addoparg(struct compiling
*c
, int op
, int arg
)
711 int extended_arg
= arg
>> 16;
712 if (op
== SET_LINENO
) {
713 com_set_lineno(c
, arg
);
718 com_addbyte(c
, EXTENDED_ARG
);
719 com_addint(c
, extended_arg
);
727 com_addfwref(struct compiling
*c
, int op
, int *p_anchor
)
729 /* Compile a forward reference for backpatching */
736 com_addint(c
, anchor
== 0 ? 0 : here
- anchor
);
740 com_backpatch(struct compiling
*c
, int anchor
)
742 unsigned char *code
= (unsigned char *) PyString_AsString(c
->c_code
);
743 int target
= c
->c_nexti
;
747 /* Make the JUMP instruction at anchor point to target */
748 prev
= code
[anchor
] + (code
[anchor
+1] << 8);
749 dist
= target
- (anchor
+2);
750 code
[anchor
] = dist
& 0xff;
752 code
[anchor
+1] = dist
;
755 com_error(c
, PyExc_SystemError
,
756 "com_backpatch: offset too large");
765 /* Handle literals and names uniformly */
768 com_add(struct compiling
*c
, PyObject
*list
, PyObject
*dict
, PyObject
*v
)
770 PyObject
*w
, *t
, *np
=NULL
;
773 t
= Py_BuildValue("(OO)", v
, v
->ob_type
);
776 w
= PyDict_GetItem(dict
, t
);
780 n
= PyList_Size(list
);
781 np
= PyInt_FromLong(n
);
784 if (PyList_Append(list
, v
) != 0)
786 if (PyDict_SetItem(dict
, t
, np
) != 0)
800 com_addconst(struct compiling
*c
, PyObject
*v
)
802 return com_add(c
, c
->c_consts
, c
->c_const_dict
, v
);
806 com_addname(struct compiling
*c
, PyObject
*v
)
808 return com_add(c
, c
->c_names
, c
->c_name_dict
, v
);
812 mangle(char *p
, char *name
, char *buffer
, size_t maxlen
)
814 /* Name mangling: __private becomes _classname__private.
815 This is independent from how the name is used. */
817 if (p
== NULL
|| name
== NULL
|| name
[0] != '_' || name
[1] != '_')
820 if (nlen
+2 >= maxlen
)
821 return 0; /* Don't mangle __extremely_long_names */
822 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
823 return 0; /* Don't mangle __whatever__ */
824 /* Strip leading underscores from class name */
828 return 0; /* Don't mangle if class is just underscores */
830 if (plen
+ nlen
>= maxlen
)
831 plen
= maxlen
-nlen
-2; /* Truncate class name if too long */
832 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
834 strncpy(buffer
+1, p
, plen
);
835 strcpy(buffer
+1+plen
, name
);
840 com_addop_name(struct compiling
*c
, int op
, char *name
)
844 char buffer
[MANGLE_LEN
];
846 if (mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
848 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
853 i
= com_addname(c
, v
);
856 com_addoparg(c
, op
, i
);
860 #define NAME_GLOBAL 1
861 #define NAME_DEFAULT 2
862 #define NAME_CLOSURE 3
865 com_lookup_arg(PyObject
*dict
, PyObject
*name
)
867 PyObject
*v
= PyDict_GetItem(dict
, name
);
871 return PyInt_AS_LONG(v
);
875 com_addop_varname(struct compiling
*c
, int kind
, char *name
)
879 int scope
= NAME_DEFAULT
;
881 char buffer
[MANGLE_LEN
];
883 if (mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
885 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
891 reftype
= get_ref_type(c
, name
);
894 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
)
897 case GLOBAL_EXPLICIT
:
900 case GLOBAL_IMPLICIT
:
901 if (c
->c_flags
& CO_OPTIMIZED
)
906 scope
= NAME_CLOSURE
;
910 i
= com_addname(c
, v
);
911 if (scope
== NAME_LOCAL
)
912 i
= com_lookup_arg(c
->c_locals
, v
);
913 else if (reftype
== FREE
)
914 i
= com_lookup_arg(c
->c_freevars
, v
);
915 else if (reftype
== CELL
)
916 i
= com_lookup_arg(c
->c_cellvars
, v
);
918 c
->c_errors
++; /* XXX no exception set */
970 sprintf(buf
, DEL_CLOSURE_ERROR
, name
);
971 com_error(c
, PyExc_SyntaxError
, buf
);
979 com_addoparg(c
, op
, i
);
983 com_addopname(struct compiling
*c
, int op
, node
*n
)
987 /* XXX it is possible to write this code without the 1000
988 chars on the total length of dotted names, I just can't be
989 bothered right now */
992 else if (TYPE(n
) == dotted_name
) {
996 for (i
= 0; i
< NCH(n
); i
+= 2) {
997 char *s
= STR(CHILD(n
, i
));
998 if (p
+ strlen(s
) > buffer
+ (sizeof buffer
) - 2) {
999 com_error(c
, PyExc_MemoryError
,
1000 "dotted_name too long");
1007 p
= strchr(p
, '\0');
1014 com_addop_name(c
, op
, name
);
1018 parsenumber(struct compiling
*co
, char *s
)
1023 #ifndef WITHOUT_COMPLEX
1029 end
= s
+ strlen(s
) - 1;
1030 #ifndef WITHOUT_COMPLEX
1031 imflag
= *end
== 'j' || *end
== 'J';
1033 if (*end
== 'l' || *end
== 'L')
1034 return PyLong_FromString(s
, (char **)0, 0);
1036 x
= (long) PyOS_strtoul(s
, &end
, 0);
1038 x
= PyOS_strtol(s
, &end
, 0);
1041 com_error(co
, PyExc_OverflowError
,
1042 "integer literal too large");
1045 return PyInt_FromLong(x
);
1047 /* XXX Huge floats may silently fail */
1048 #ifndef WITHOUT_COMPLEX
1051 PyFPE_START_PROTECT("atof", return 0)
1053 PyFPE_END_PROTECT(c
)
1054 return PyComplex_FromCComplex(c
);
1059 PyFPE_START_PROTECT("atof", return 0)
1061 PyFPE_END_PROTECT(dx
)
1062 return PyFloat_FromDouble(dx
);
1079 if (isalpha(quote
) || quote
== '_') {
1080 if (quote
== 'u' || quote
== 'U') {
1084 if (quote
== 'r' || quote
== 'R') {
1089 if (quote
!= '\'' && quote
!= '\"') {
1090 PyErr_BadInternalCall();
1095 if (len
> INT_MAX
) {
1096 PyErr_SetString(PyExc_OverflowError
, "string to parse is too long");
1099 if (s
[--len
] != quote
) {
1100 PyErr_BadInternalCall();
1103 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
1106 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
1107 PyErr_BadInternalCall();
1111 if (unicode
|| Py_UnicodeFlag
) {
1113 return PyUnicode_DecodeRawUnicodeEscape(
1116 return PyUnicode_DecodeUnicodeEscape(
1119 if (rawmode
|| strchr(s
, '\\') == NULL
)
1120 return PyString_FromStringAndSize(s
, len
);
1121 v
= PyString_FromStringAndSize((char *)NULL
, len
);
1124 p
= buf
= PyString_AsString(v
);
1133 /* XXX This assumes ASCII! */
1135 case '\\': *p
++ = '\\'; break;
1136 case '\'': *p
++ = '\''; break;
1137 case '\"': *p
++ = '\"'; break;
1138 case 'b': *p
++ = '\b'; break;
1139 case 'f': *p
++ = '\014'; break; /* FF */
1140 case 't': *p
++ = '\t'; break;
1141 case 'n': *p
++ = '\n'; break;
1142 case 'r': *p
++ = '\r'; break;
1143 case 'v': *p
++ = '\013'; break; /* VT */
1144 case 'a': *p
++ = '\007'; break; /* BEL, not classic C */
1145 case '0': case '1': case '2': case '3':
1146 case '4': case '5': case '6': case '7':
1148 if ('0' <= *s
&& *s
<= '7') {
1149 c
= (c
<<3) + *s
++ - '0';
1150 if ('0' <= *s
&& *s
<= '7')
1151 c
= (c
<<3) + *s
++ - '0';
1156 if (isxdigit(Py_CHARMASK(s
[0]))
1157 && isxdigit(Py_CHARMASK(s
[1]))) {
1159 c
= Py_CHARMASK(*s
);
1163 else if (islower(c
))
1168 c
= Py_CHARMASK(*s
);
1172 else if (islower(c
))
1179 PyErr_SetString(PyExc_ValueError
,
1180 "invalid \\x escape");
1189 _PyString_Resize(&v
, (int)(p
- buf
));
1194 parsestrplus(node
*n
)
1198 REQ(CHILD(n
, 0), STRING
);
1199 if ((v
= parsestr(STR(CHILD(n
, 0)))) != NULL
) {
1200 /* String literal concatenation */
1201 for (i
= 1; i
< NCH(n
); i
++) {
1203 s
= parsestr(STR(CHILD(n
, i
)));
1206 if (PyString_Check(v
) && PyString_Check(s
)) {
1207 PyString_ConcatAndDel(&v
, s
);
1213 temp
= PyUnicode_Concat(v
, s
);
1230 com_list_for(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1234 int save_begin
= c
->c_begin
;
1236 /* list_iter: for v in expr [list_iter] */
1237 com_node(c
, CHILD(n
, 3)); /* expr */
1238 v
= PyInt_FromLong(0L);
1241 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1244 c
->c_begin
= c
->c_nexti
;
1245 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1246 com_addfwref(c
, FOR_LOOP
, &anchor
);
1248 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
1250 com_list_iter(c
, n
, e
, t
);
1252 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
1253 c
->c_begin
= save_begin
;
1254 com_backpatch(c
, anchor
);
1255 com_pop(c
, 2); /* FOR_LOOP has popped these */
1259 com_list_if(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1263 /* list_iter: 'if' test [list_iter] */
1264 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
1265 com_node(c
, CHILD(n
, 1));
1266 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
1267 com_addbyte(c
, POP_TOP
);
1269 com_list_iter(c
, n
, e
, t
);
1270 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
1271 com_backpatch(c
, a
);
1272 /* We jump here with an extra entry which we now pop */
1273 com_addbyte(c
, POP_TOP
);
1274 com_backpatch(c
, anchor
);
1278 com_list_iter(struct compiling
*c
,
1279 node
*p
, /* parent of list_iter node */
1280 node
*e
, /* element expression node */
1281 char *t
/* name of result list temp local */)
1283 /* list_iter is the last child in a listmaker, list_for, or list_if */
1284 node
*n
= CHILD(p
, NCH(p
)-1);
1285 if (TYPE(n
) == list_iter
) {
1289 com_list_for(c
, n
, e
, t
);
1292 com_list_if(c
, n
, e
, t
);
1295 com_error(c
, PyExc_SystemError
,
1296 "invalid list_iter node type");
1300 com_addop_varname(c
, VAR_LOAD
, t
);
1303 com_addoparg(c
, CALL_FUNCTION
, 1);
1304 com_addbyte(c
, POP_TOP
);
1310 com_list_comprehension(struct compiling
*c
, node
*n
)
1312 /* listmaker: test list_for */
1314 sprintf(tmpname
, "_[%d]", ++c
->c_tmpname
);
1315 com_addoparg(c
, BUILD_LIST
, 0);
1316 com_addbyte(c
, DUP_TOP
); /* leave the result on the stack */
1318 com_addop_name(c
, LOAD_ATTR
, "append");
1319 com_addop_varname(c
, VAR_STORE
, tmpname
);
1321 com_list_for(c
, CHILD(n
, 1), CHILD(n
, 0), tmpname
);
1322 com_addop_varname(c
, VAR_DELETE
, tmpname
);
1327 com_listmaker(struct compiling
*c
, node
*n
)
1329 /* listmaker: test ( list_for | (',' test)* [','] ) */
1330 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
)
1331 com_list_comprehension(c
, n
);
1335 for (i
= 0; i
< NCH(n
); i
+= 2, len
++)
1336 com_node(c
, CHILD(n
, i
));
1337 com_addoparg(c
, BUILD_LIST
, len
);
1343 com_dictmaker(struct compiling
*c
, node
*n
)
1346 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1347 for (i
= 0; i
+2 < NCH(n
); i
+= 4) {
1348 /* We must arrange things just right for STORE_SUBSCR.
1349 It wants the stack to look like (value) (dict) (key) */
1350 com_addbyte(c
, DUP_TOP
);
1352 com_node(c
, CHILD(n
, i
+2)); /* value */
1353 com_addbyte(c
, ROT_TWO
);
1354 com_node(c
, CHILD(n
, i
)); /* key */
1355 com_addbyte(c
, STORE_SUBSCR
);
1361 com_atom(struct compiling
*c
, node
*n
)
1370 if (TYPE(CHILD(n
, 1)) == RPAR
) {
1371 com_addoparg(c
, BUILD_TUPLE
, 0);
1375 com_node(c
, CHILD(n
, 1));
1377 case LSQB
: /* '[' [listmaker] ']' */
1378 if (TYPE(CHILD(n
, 1)) == RSQB
) {
1379 com_addoparg(c
, BUILD_LIST
, 0);
1383 com_listmaker(c
, CHILD(n
, 1));
1385 case LBRACE
: /* '{' [dictmaker] '}' */
1386 com_addoparg(c
, BUILD_MAP
, 0);
1388 if (TYPE(CHILD(n
, 1)) == dictmaker
)
1389 com_dictmaker(c
, CHILD(n
, 1));
1392 com_node(c
, CHILD(n
, 1));
1393 com_addbyte(c
, UNARY_CONVERT
);
1396 if ((v
= parsenumber(c
, STR(ch
))) == NULL
) {
1400 i
= com_addconst(c
, v
);
1403 com_addoparg(c
, LOAD_CONST
, i
);
1407 v
= parsestrplus(n
);
1413 i
= com_addconst(c
, v
);
1416 com_addoparg(c
, LOAD_CONST
, i
);
1420 com_addop_varname(c
, VAR_LOAD
, STR(ch
));
1424 com_error(c
, PyExc_SystemError
,
1425 "com_atom: unexpected node type");
1430 com_slice(struct compiling
*c
, node
*n
, int op
)
1435 else if (NCH(n
) == 2) {
1436 if (TYPE(CHILD(n
, 0)) != COLON
) {
1437 com_node(c
, CHILD(n
, 0));
1438 com_addbyte(c
, op
+1);
1441 com_node(c
, CHILD(n
, 1));
1442 com_addbyte(c
, op
+2);
1447 com_node(c
, CHILD(n
, 0));
1448 com_node(c
, CHILD(n
, 2));
1449 com_addbyte(c
, op
+3);
1455 com_augassign_slice(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
1458 com_addbyte(c
, DUP_TOP
);
1460 com_addbyte(c
, SLICE
);
1462 com_addbyte(c
, opcode
);
1464 com_addbyte(c
, ROT_TWO
);
1465 com_addbyte(c
, STORE_SLICE
);
1467 } else if (NCH(n
) == 2 && TYPE(CHILD(n
, 0)) != COLON
) {
1468 com_node(c
, CHILD(n
, 0));
1469 com_addoparg(c
, DUP_TOPX
, 2);
1471 com_addbyte(c
, SLICE
+1);
1474 com_addbyte(c
, opcode
);
1476 com_addbyte(c
, ROT_THREE
);
1477 com_addbyte(c
, STORE_SLICE
+1);
1479 } else if (NCH(n
) == 2) {
1480 com_node(c
, CHILD(n
, 1));
1481 com_addoparg(c
, DUP_TOPX
, 2);
1483 com_addbyte(c
, SLICE
+2);
1486 com_addbyte(c
, opcode
);
1488 com_addbyte(c
, ROT_THREE
);
1489 com_addbyte(c
, STORE_SLICE
+2);
1492 com_node(c
, CHILD(n
, 0));
1493 com_node(c
, CHILD(n
, 2));
1494 com_addoparg(c
, DUP_TOPX
, 3);
1496 com_addbyte(c
, SLICE
+3);
1499 com_addbyte(c
, opcode
);
1501 com_addbyte(c
, ROT_FOUR
);
1502 com_addbyte(c
, STORE_SLICE
+3);
1508 com_argument(struct compiling
*c
, node
*n
, PyObject
**pkeywords
)
1511 REQ(n
, argument
); /* [test '='] test; really [keyword '='] test */
1513 if (*pkeywords
!= NULL
) {
1514 com_error(c
, PyExc_SyntaxError
,
1515 "non-keyword arg after keyword arg");
1518 com_node(c
, CHILD(n
, 0));
1525 } while (NCH(m
) == 1);
1526 if (TYPE(m
) != NAME
) {
1527 /* f(lambda x: x[0] = 3) ends up getting parsed with
1528 * LHS test = lambda x: x[0], and RHS test = 3.
1529 * SF bug 132313 points out that complaining about a keyword
1530 * then is very confusing.
1532 com_error(c
, PyExc_SyntaxError
,
1533 TYPE(m
) == lambdef
?
1534 "lambda cannot contain assignment" :
1535 "keyword can't be an expression");
1538 PyObject
*v
= PyString_InternFromString(STR(m
));
1539 if (v
!= NULL
&& *pkeywords
== NULL
)
1540 *pkeywords
= PyDict_New();
1543 else if (*pkeywords
== NULL
) {
1547 if (PyDict_GetItem(*pkeywords
, v
) != NULL
)
1548 com_error(c
, PyExc_SyntaxError
,
1549 "duplicate keyword argument");
1551 if (PyDict_SetItem(*pkeywords
, v
, v
) != 0)
1553 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1558 com_node(c
, CHILD(n
, 2));
1562 com_call_function(struct compiling
*c
, node
*n
)
1564 if (TYPE(n
) == RPAR
) {
1565 com_addoparg(c
, CALL_FUNCTION
, 0);
1568 PyObject
*keywords
= NULL
;
1570 int lineno
= n
->n_lineno
;
1572 int starstar_flag
= 0;
1577 for (i
= 0; i
< NCH(n
); i
+= 2) {
1578 node
*ch
= CHILD(n
, i
);
1579 if (TYPE(ch
) == STAR
||
1580 TYPE(ch
) == DOUBLESTAR
)
1582 if (ch
->n_lineno
!= lineno
) {
1583 lineno
= ch
->n_lineno
;
1584 com_addoparg(c
, SET_LINENO
, lineno
);
1586 com_argument(c
, ch
, &keywords
);
1587 if (keywords
== NULL
)
1592 Py_XDECREF(keywords
);
1593 while (i
< NCH(n
)) {
1594 node
*tok
= CHILD(n
, i
);
1595 node
*ch
= CHILD(n
, i
+1);
1597 switch (TYPE(tok
)) {
1598 case STAR
: star_flag
= 1; break;
1599 case DOUBLESTAR
: starstar_flag
= 1; break;
1603 if (na
> 255 || nk
> 255) {
1604 com_error(c
, PyExc_SyntaxError
,
1605 "more than 255 arguments");
1607 if (star_flag
|| starstar_flag
)
1608 opcode
= CALL_FUNCTION_VAR
- 1 +
1609 star_flag
+ (starstar_flag
<< 1);
1611 opcode
= CALL_FUNCTION
;
1612 com_addoparg(c
, opcode
, na
| (nk
<< 8));
1613 com_pop(c
, na
+ 2*nk
+ star_flag
+ starstar_flag
);
1618 com_select_member(struct compiling
*c
, node
*n
)
1620 com_addopname(c
, LOAD_ATTR
, n
);
1624 com_sliceobj(struct compiling
*c
, node
*n
)
1627 int ns
=2; /* number of slice arguments */
1630 /* first argument */
1631 if (TYPE(CHILD(n
,i
)) == COLON
) {
1632 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1637 com_node(c
, CHILD(n
,i
));
1639 REQ(CHILD(n
,i
),COLON
);
1642 /* second argument */
1643 if (i
< NCH(n
) && TYPE(CHILD(n
,i
)) == test
) {
1644 com_node(c
, CHILD(n
,i
));
1648 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1651 /* remaining arguments */
1652 for (; i
< NCH(n
); i
++) {
1657 /* right argument of ':' missing */
1658 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1662 com_node(c
, CHILD(ch
,1));
1664 com_addoparg(c
, BUILD_SLICE
, ns
);
1665 com_pop(c
, 1 + (ns
== 3));
1669 com_subscript(struct compiling
*c
, node
*n
)
1674 /* check for rubber index */
1675 if (TYPE(ch
) == DOT
&& TYPE(CHILD(n
,1)) == DOT
) {
1676 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_Ellipsis
));
1680 /* check for slice */
1681 if ((TYPE(ch
) == COLON
|| NCH(n
) > 1))
1691 com_subscriptlist(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
1694 REQ(n
, subscriptlist
);
1695 /* Check to make backward compatible slice behavior for '[i:j]' */
1697 node
*sub
= CHILD(n
, 0); /* subscript */
1698 /* 'Basic' slice, should have exactly one colon. */
1699 if ((TYPE(CHILD(sub
, 0)) == COLON
1700 || (NCH(sub
) > 1 && TYPE(CHILD(sub
, 1)) == COLON
))
1701 && (TYPE(CHILD(sub
,NCH(sub
)-1)) != sliceop
))
1703 switch (assigning
) {
1714 com_augassign_slice(c
, sub
, assigning
, augn
);
1717 com_slice(c
, sub
, op
);
1718 if (op
== STORE_SLICE
)
1720 else if (op
== DELETE_SLICE
)
1725 /* Else normal subscriptlist. Compile each subscript. */
1726 for (i
= 0; i
< NCH(n
); i
+= 2)
1727 com_subscript(c
, CHILD(n
, i
));
1728 /* Put multiple subscripts into a tuple */
1731 com_addoparg(c
, BUILD_TUPLE
, i
);
1734 switch (assigning
) {
1749 if (assigning
> OP_APPLY
) {
1750 com_addoparg(c
, DUP_TOPX
, 2);
1752 com_addbyte(c
, BINARY_SUBSCR
);
1755 com_addbyte(c
, assigning
);
1757 com_addbyte(c
, ROT_THREE
);
1764 com_apply_trailer(struct compiling
*c
, node
*n
)
1767 switch (TYPE(CHILD(n
, 0))) {
1769 com_call_function(c
, CHILD(n
, 1));
1772 com_select_member(c
, CHILD(n
, 1));
1775 com_subscriptlist(c
, CHILD(n
, 1), OP_APPLY
, NULL
);
1778 com_error(c
, PyExc_SystemError
,
1779 "com_apply_trailer: unknown trailer type");
1784 com_power(struct compiling
*c
, node
*n
)
1788 com_atom(c
, CHILD(n
, 0));
1789 for (i
= 1; i
< NCH(n
); i
++) {
1790 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1791 com_factor(c
, CHILD(n
, i
+1));
1792 com_addbyte(c
, BINARY_POWER
);
1797 com_apply_trailer(c
, CHILD(n
, i
));
1802 com_factor(struct compiling
*c
, node
*n
)
1805 if (TYPE(CHILD(n
, 0)) == PLUS
) {
1806 com_factor(c
, CHILD(n
, 1));
1807 com_addbyte(c
, UNARY_POSITIVE
);
1809 else if (TYPE(CHILD(n
, 0)) == MINUS
) {
1810 com_factor(c
, CHILD(n
, 1));
1811 com_addbyte(c
, UNARY_NEGATIVE
);
1813 else if (TYPE(CHILD(n
, 0)) == TILDE
) {
1814 com_factor(c
, CHILD(n
, 1));
1815 com_addbyte(c
, UNARY_INVERT
);
1818 com_power(c
, CHILD(n
, 0));
1823 com_term(struct compiling
*c
, node
*n
)
1828 com_factor(c
, CHILD(n
, 0));
1829 for (i
= 2; i
< NCH(n
); i
+= 2) {
1830 com_factor(c
, CHILD(n
, i
));
1831 switch (TYPE(CHILD(n
, i
-1))) {
1833 op
= BINARY_MULTIPLY
;
1842 com_error(c
, PyExc_SystemError
,
1843 "com_term: operator not *, / or %");
1852 com_arith_expr(struct compiling
*c
, node
*n
)
1857 com_term(c
, CHILD(n
, 0));
1858 for (i
= 2; i
< NCH(n
); i
+= 2) {
1859 com_term(c
, CHILD(n
, i
));
1860 switch (TYPE(CHILD(n
, i
-1))) {
1865 op
= BINARY_SUBTRACT
;
1868 com_error(c
, PyExc_SystemError
,
1869 "com_arith_expr: operator not + or -");
1878 com_shift_expr(struct compiling
*c
, node
*n
)
1883 com_arith_expr(c
, CHILD(n
, 0));
1884 for (i
= 2; i
< NCH(n
); i
+= 2) {
1885 com_arith_expr(c
, CHILD(n
, i
));
1886 switch (TYPE(CHILD(n
, i
-1))) {
1894 com_error(c
, PyExc_SystemError
,
1895 "com_shift_expr: operator not << or >>");
1904 com_and_expr(struct compiling
*c
, node
*n
)
1909 com_shift_expr(c
, CHILD(n
, 0));
1910 for (i
= 2; i
< NCH(n
); i
+= 2) {
1911 com_shift_expr(c
, CHILD(n
, i
));
1912 if (TYPE(CHILD(n
, i
-1)) == AMPER
) {
1916 com_error(c
, PyExc_SystemError
,
1917 "com_and_expr: operator not &");
1926 com_xor_expr(struct compiling
*c
, node
*n
)
1931 com_and_expr(c
, CHILD(n
, 0));
1932 for (i
= 2; i
< NCH(n
); i
+= 2) {
1933 com_and_expr(c
, CHILD(n
, i
));
1934 if (TYPE(CHILD(n
, i
-1)) == CIRCUMFLEX
) {
1938 com_error(c
, PyExc_SystemError
,
1939 "com_xor_expr: operator not ^");
1948 com_expr(struct compiling
*c
, node
*n
)
1953 com_xor_expr(c
, CHILD(n
, 0));
1954 for (i
= 2; i
< NCH(n
); i
+= 2) {
1955 com_xor_expr(c
, CHILD(n
, i
));
1956 if (TYPE(CHILD(n
, i
-1)) == VBAR
) {
1960 com_error(c
, PyExc_SystemError
,
1961 "com_expr: expr operator not |");
1973 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1974 | 'in' | 'not' 'in' | 'is' | 'is' not' */
1978 case LESS
: return LT
;
1979 case GREATER
: return GT
;
1980 case EQEQUAL
: /* == */
1981 case EQUAL
: return EQ
;
1982 case LESSEQUAL
: return LE
;
1983 case GREATEREQUAL
: return GE
;
1984 case NOTEQUAL
: return NE
; /* <> or != */
1985 case NAME
: if (strcmp(STR(n
), "in") == 0) return IN
;
1986 if (strcmp(STR(n
), "is") == 0) return IS
;
1989 else if (NCH(n
) == 2) {
1990 switch (TYPE(CHILD(n
, 0))) {
1991 case NAME
: if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
1993 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
2001 com_comparison(struct compiling
*c
, node
*n
)
2006 REQ(n
, comparison
); /* comparison: expr (comp_op expr)* */
2007 com_expr(c
, CHILD(n
, 0));
2011 /****************************************************************
2012 The following code is generated for all but the last
2013 comparison in a chain:
2015 label: on stack: opcode: jump to:
2021 b, 0-or-1 JUMP_IF_FALSE L1
2025 We are now ready to repeat this sequence for the next
2026 comparison in the chain.
2028 For the last we generate:
2034 If there were any jumps to L1 (i.e., there was more than one
2035 comparison), we generate:
2037 0-or-1 JUMP_FORWARD L2
2042 ****************************************************************/
2046 for (i
= 2; i
< NCH(n
); i
+= 2) {
2047 com_expr(c
, CHILD(n
, i
));
2049 com_addbyte(c
, DUP_TOP
);
2051 com_addbyte(c
, ROT_THREE
);
2053 op
= cmp_type(CHILD(n
, i
-1));
2055 com_error(c
, PyExc_SystemError
,
2056 "com_comparison: unknown comparison op");
2058 com_addoparg(c
, COMPARE_OP
, op
);
2061 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2062 com_addbyte(c
, POP_TOP
);
2069 com_addfwref(c
, JUMP_FORWARD
, &anchor2
);
2070 com_backpatch(c
, anchor
);
2071 com_addbyte(c
, ROT_TWO
);
2072 com_addbyte(c
, POP_TOP
);
2073 com_backpatch(c
, anchor2
);
2078 com_not_test(struct compiling
*c
, node
*n
)
2080 REQ(n
, not_test
); /* 'not' not_test | comparison */
2082 com_comparison(c
, CHILD(n
, 0));
2085 com_not_test(c
, CHILD(n
, 1));
2086 com_addbyte(c
, UNARY_NOT
);
2091 com_and_test(struct compiling
*c
, node
*n
)
2095 REQ(n
, and_test
); /* not_test ('and' not_test)* */
2099 com_not_test(c
, CHILD(n
, i
));
2100 if ((i
+= 2) >= NCH(n
))
2102 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2103 com_addbyte(c
, POP_TOP
);
2107 com_backpatch(c
, anchor
);
2111 com_make_closure(struct compiling
*c
, PyCodeObject
*co
)
2113 int i
, free
= PyTuple_GET_SIZE(co
->co_freevars
);
2114 /* If the code is compiled with st->st_nested_scopes == 0,
2115 then no variable will ever be added to co_freevars.
2119 for (i
= 0; i
< free
; ++i
) {
2120 /* Bypass com_addop_varname because it will generate
2121 LOAD_DEREF but LOAD_CLOSURE is needed.
2123 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
2126 /* Special case: If a class contains a method with a
2127 free variable that has the same name as a method,
2128 the name will be considered free *and* local in the
2129 class. It should be handled by the closure, as
2130 well as by the normal name loookup logic.
2132 reftype
= get_ref_type(c
, PyString_AS_STRING(name
));
2133 if (reftype
== CELL
)
2134 arg
= com_lookup_arg(c
->c_cellvars
, name
);
2135 else /* (reftype == FREE) */
2136 arg
= com_lookup_arg(c
->c_freevars
, name
);
2138 fprintf(stderr
, "lookup %s in %s %d %d\n"
2139 "freevars of %s: %s\n",
2140 PyObject_REPR(name
),
2143 PyString_AS_STRING(co
->co_name
),
2144 PyObject_REPR(co
->co_freevars
));
2145 Py_FatalError("com_make_closure()");
2147 com_addoparg(c
, LOAD_CLOSURE
, arg
);
2155 com_test(struct compiling
*c
, node
*n
)
2157 REQ(n
, test
); /* and_test ('or' and_test)* | lambdef */
2158 if (NCH(n
) == 1 && TYPE(CHILD(n
, 0)) == lambdef
) {
2161 int ndefs
= com_argdefs(c
, CHILD(n
, 0));
2162 symtable_enter_scope(c
->c_symtable
, "lambda", lambdef
,
2164 co
= (PyObject
*) icompile(CHILD(n
, 0), c
);
2169 symtable_exit_scope(c
->c_symtable
);
2170 i
= com_addconst(c
, co
);
2171 closure
= com_make_closure(c
, (PyCodeObject
*)co
);
2173 com_addoparg(c
, LOAD_CONST
, i
);
2176 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
2178 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
2185 com_and_test(c
, CHILD(n
, i
));
2186 if ((i
+= 2) >= NCH(n
))
2188 com_addfwref(c
, JUMP_IF_TRUE
, &anchor
);
2189 com_addbyte(c
, POP_TOP
);
2193 com_backpatch(c
, anchor
);
2198 com_list(struct compiling
*c
, node
*n
, int toplevel
)
2200 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2201 if (NCH(n
) == 1 && !toplevel
) {
2202 com_node(c
, CHILD(n
, 0));
2207 len
= (NCH(n
) + 1) / 2;
2208 for (i
= 0; i
< NCH(n
); i
+= 2)
2209 com_node(c
, CHILD(n
, i
));
2210 com_addoparg(c
, BUILD_TUPLE
, len
);
2216 /* Begin of assignment compilation */
2220 com_augassign_attr(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2222 com_addbyte(c
, DUP_TOP
);
2224 com_addopname(c
, LOAD_ATTR
, n
);
2226 com_addbyte(c
, opcode
);
2228 com_addbyte(c
, ROT_TWO
);
2229 com_addopname(c
, STORE_ATTR
, n
);
2234 com_assign_attr(struct compiling
*c
, node
*n
, int assigning
)
2236 com_addopname(c
, assigning
? STORE_ATTR
: DELETE_ATTR
, n
);
2237 com_pop(c
, assigning
? 2 : 1);
2241 com_assign_trailer(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2244 switch (TYPE(CHILD(n
, 0))) {
2245 case LPAR
: /* '(' [exprlist] ')' */
2246 com_error(c
, PyExc_SyntaxError
,
2247 "can't assign to function call");
2249 case DOT
: /* '.' NAME */
2250 if (assigning
> OP_APPLY
)
2251 com_augassign_attr(c
, CHILD(n
, 1), assigning
, augn
);
2253 com_assign_attr(c
, CHILD(n
, 1), assigning
);
2255 case LSQB
: /* '[' subscriptlist ']' */
2256 com_subscriptlist(c
, CHILD(n
, 1), assigning
, augn
);
2259 com_error(c
, PyExc_SystemError
, "unknown trailer type");
2264 com_assign_sequence(struct compiling
*c
, node
*n
, int assigning
)
2267 if (TYPE(n
) != testlist
&& TYPE(n
) != listmaker
)
2271 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
2274 for (i
= 0; i
< NCH(n
); i
+= 2)
2275 com_assign(c
, CHILD(n
, i
), assigning
, NULL
);
2279 com_augassign_name(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2282 com_addop_varname(c
, VAR_LOAD
, STR(n
));
2285 com_addbyte(c
, opcode
);
2287 com_assign_name(c
, n
, OP_ASSIGN
);
2291 com_assign_name(struct compiling
*c
, node
*n
, int assigning
)
2294 com_addop_varname(c
, assigning
? VAR_STORE
: VAR_DELETE
, STR(n
));
2300 com_assign(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2302 /* Loop to avoid trivial recursion */
2309 if (assigning
> OP_APPLY
) {
2310 com_error(c
, PyExc_SyntaxError
,
2311 "augmented assign to tuple not possible");
2314 com_assign_sequence(c
, n
, assigning
);
2332 com_error(c
, PyExc_SyntaxError
,
2333 "can't assign to operator");
2339 case power
: /* atom trailer* ('**' power)*
2340 ('+'|'-'|'~') factor | atom trailer* */
2341 if (TYPE(CHILD(n
, 0)) != atom
) {
2342 com_error(c
, PyExc_SyntaxError
,
2343 "can't assign to operator");
2346 if (NCH(n
) > 1) { /* trailer or exponent present */
2348 com_node(c
, CHILD(n
, 0));
2349 for (i
= 1; i
+1 < NCH(n
); i
++) {
2350 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
2351 com_error(c
, PyExc_SyntaxError
,
2352 "can't assign to operator");
2355 com_apply_trailer(c
, CHILD(n
, i
));
2356 } /* NB i is still alive */
2357 com_assign_trailer(c
,
2358 CHILD(n
, i
), assigning
, augn
);
2365 switch (TYPE(CHILD(n
, 0))) {
2368 if (TYPE(n
) == RPAR
) {
2369 /* XXX Should allow () = () ??? */
2370 com_error(c
, PyExc_SyntaxError
,
2371 "can't assign to ()");
2374 if (assigning
> OP_APPLY
) {
2375 com_error(c
, PyExc_SyntaxError
,
2376 "augmented assign to tuple not possible");
2382 if (TYPE(n
) == RSQB
) {
2383 com_error(c
, PyExc_SyntaxError
,
2384 "can't assign to []");
2387 if (assigning
> OP_APPLY
) {
2388 com_error(c
, PyExc_SyntaxError
,
2389 "augmented assign to list not possible");
2393 && TYPE(CHILD(n
, 1)) == list_for
) {
2394 com_error(c
, PyExc_SyntaxError
,
2395 "can't assign to list comprehension");
2398 com_assign_sequence(c
, n
, assigning
);
2401 if (assigning
> OP_APPLY
)
2402 com_augassign_name(c
, CHILD(n
, 0),
2405 com_assign_name(c
, CHILD(n
, 0),
2409 com_error(c
, PyExc_SyntaxError
,
2410 "can't assign to literal");
2416 com_error(c
, PyExc_SyntaxError
,
2417 "can't assign to lambda");
2421 com_error(c
, PyExc_SystemError
,
2422 "com_assign: bad node");
2430 com_augassign(struct compiling
*c
, node
*n
)
2434 switch (STR(CHILD(CHILD(n
, 1), 0))[0]) {
2435 case '+': opcode
= INPLACE_ADD
; break;
2436 case '-': opcode
= INPLACE_SUBTRACT
; break;
2437 case '/': opcode
= INPLACE_DIVIDE
; break;
2438 case '%': opcode
= INPLACE_MODULO
; break;
2439 case '<': opcode
= INPLACE_LSHIFT
; break;
2440 case '>': opcode
= INPLACE_RSHIFT
; break;
2441 case '&': opcode
= INPLACE_AND
; break;
2442 case '^': opcode
= INPLACE_XOR
; break;
2443 case '|': opcode
= INPLACE_OR
; break;
2445 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '*')
2446 opcode
= INPLACE_POWER
;
2448 opcode
= INPLACE_MULTIPLY
;
2451 com_error(c
, PyExc_SystemError
, "com_augassign: bad operator");
2454 com_assign(c
, CHILD(n
, 0), opcode
, CHILD(n
, 2));
2458 com_expr_stmt(struct compiling
*c
, node
*n
)
2461 /* testlist (('=' testlist)* | augassign testlist) */
2462 /* Forget it if we have just a doc string here */
2463 if (!c
->c_interactive
&& NCH(n
) == 1 && get_rawdocstring(n
) != NULL
)
2466 com_node(c
, CHILD(n
, NCH(n
)-1));
2467 if (c
->c_interactive
)
2468 com_addbyte(c
, PRINT_EXPR
);
2470 com_addbyte(c
, POP_TOP
);
2473 else if (TYPE(CHILD(n
,1)) == augassign
)
2474 com_augassign(c
, n
);
2477 com_node(c
, CHILD(n
, NCH(n
)-1));
2478 for (i
= 0; i
< NCH(n
)-2; i
+=2) {
2479 if (i
+2 < NCH(n
)-2) {
2480 com_addbyte(c
, DUP_TOP
);
2483 com_assign(c
, CHILD(n
, i
), OP_ASSIGN
, NULL
);
2489 com_assert_stmt(struct compiling
*c
, node
*n
)
2493 REQ(n
, assert_stmt
); /* 'assert' test [',' test] */
2494 /* Generate code like for
2498 raise AssertionError [, <message>]
2500 where <message> is the second test, if present.
2503 if (Py_OptimizeFlag
)
2505 com_addop_name(c
, LOAD_GLOBAL
, "__debug__");
2507 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2508 com_addbyte(c
, POP_TOP
);
2510 com_node(c
, CHILD(n
, 1));
2511 com_addfwref(c
, JUMP_IF_TRUE
, &b
);
2512 com_addbyte(c
, POP_TOP
);
2514 /* Raise that exception! */
2515 com_addop_name(c
, LOAD_GLOBAL
, "AssertionError");
2517 i
= NCH(n
)/2; /* Either 2 or 4 */
2519 com_node(c
, CHILD(n
, 3));
2520 com_addoparg(c
, RAISE_VARARGS
, i
);
2522 /* The interpreter does not fall through */
2523 /* All jumps converge here */
2524 com_backpatch(c
, a
);
2525 com_backpatch(c
, b
);
2526 com_addbyte(c
, POP_TOP
);
2530 com_print_stmt(struct compiling
*c
, node
*n
)
2533 node
* stream
= NULL
;
2535 REQ(n
, print_stmt
); /* 'print' (test ',')* [test] */
2537 /* are we using the extended print form? */
2538 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2539 stream
= CHILD(n
, 2);
2540 com_node(c
, stream
);
2541 /* stack: [...] => [... stream] */
2543 if (NCH(n
) > 3 && TYPE(CHILD(n
, 3)) == COMMA
)
2548 for (; i
< NCH(n
); i
+= 2) {
2549 if (stream
!= NULL
) {
2550 com_addbyte(c
, DUP_TOP
);
2551 /* stack: [stream] => [stream stream] */
2553 com_node(c
, CHILD(n
, i
));
2554 /* stack: [stream stream] => [stream stream obj] */
2555 com_addbyte(c
, ROT_TWO
);
2556 /* stack: [stream stream obj] => [stream obj stream] */
2557 com_addbyte(c
, PRINT_ITEM_TO
);
2558 /* stack: [stream obj stream] => [stream] */
2562 com_node(c
, CHILD(n
, i
));
2563 /* stack: [...] => [... obj] */
2564 com_addbyte(c
, PRINT_ITEM
);
2568 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2569 if (TYPE(CHILD(n
, NCH(n
)-1)) == COMMA
) {
2570 if (stream
!= NULL
) {
2571 /* must pop the extra stream object off the stack */
2572 com_addbyte(c
, POP_TOP
);
2573 /* stack: [... stream] => [...] */
2578 if (stream
!= NULL
) {
2579 /* this consumes the last stream object on stack */
2580 com_addbyte(c
, PRINT_NEWLINE_TO
);
2581 /* stack: [... stream] => [...] */
2585 com_addbyte(c
, PRINT_NEWLINE
);
2590 com_return_stmt(struct compiling
*c
, node
*n
)
2592 REQ(n
, return_stmt
); /* 'return' [testlist] */
2593 if (!c
->c_infunction
) {
2594 com_error(c
, PyExc_SyntaxError
, "'return' outside function");
2597 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2601 com_node(c
, CHILD(n
, 1));
2602 com_addbyte(c
, RETURN_VALUE
);
2607 com_raise_stmt(struct compiling
*c
, node
*n
)
2610 REQ(n
, raise_stmt
); /* 'raise' [test [',' test [',' test]]] */
2612 com_node(c
, CHILD(n
, 1));
2614 com_node(c
, CHILD(n
, 3));
2616 com_node(c
, CHILD(n
, 5));
2620 com_addoparg(c
, RAISE_VARARGS
, i
);
2625 com_from_import(struct compiling
*c
, node
*n
)
2627 com_addopname(c
, IMPORT_FROM
, CHILD(n
, 0));
2630 if (strcmp(STR(CHILD(n
, 1)), "as") != 0) {
2631 com_error(c
, PyExc_SyntaxError
, "invalid syntax");
2634 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 2)));
2636 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
2641 com_import_stmt(struct compiling
*c
, node
*n
)
2644 REQ(n
, import_stmt
);
2645 /* 'import' dotted_name (',' dotted_name)* |
2646 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2647 if (STR(CHILD(n
, 0))[0] == 'f') {
2649 /* 'from' dotted_name 'import' ... */
2650 REQ(CHILD(n
, 1), dotted_name
);
2652 if (TYPE(CHILD(n
, 3)) == STAR
) {
2653 tup
= Py_BuildValue("(s)", "*");
2655 tup
= PyTuple_New((NCH(n
) - 2)/2);
2656 for (i
= 3; i
< NCH(n
); i
+= 2) {
2657 PyTuple_SET_ITEM(tup
, (i
-3)/2,
2658 PyString_FromString(STR(
2659 CHILD(CHILD(n
, i
), 0))));
2662 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, tup
));
2665 com_addopname(c
, IMPORT_NAME
, CHILD(n
, 1));
2666 if (TYPE(CHILD(n
, 3)) == STAR
)
2667 com_addbyte(c
, IMPORT_STAR
);
2669 for (i
= 3; i
< NCH(n
); i
+= 2)
2670 com_from_import(c
, CHILD(n
, i
));
2671 com_addbyte(c
, POP_TOP
);
2677 for (i
= 1; i
< NCH(n
); i
+= 2) {
2678 node
*subn
= CHILD(n
, i
);
2679 REQ(subn
, dotted_as_name
);
2680 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2682 com_addopname(c
, IMPORT_NAME
, CHILD(subn
, 0));
2683 if (NCH(subn
) > 1) {
2685 if (strcmp(STR(CHILD(subn
, 1)), "as") != 0) {
2686 com_error(c
, PyExc_SyntaxError
,
2690 for (j
=2 ; j
< NCH(CHILD(subn
, 0)); j
+= 2)
2691 com_addopname(c
, LOAD_ATTR
,
2692 CHILD(CHILD(subn
, 0),
2694 com_addop_varname(c
, VAR_STORE
,
2695 STR(CHILD(subn
, 2)));
2697 com_addop_varname(c
, VAR_STORE
,
2698 STR(CHILD(CHILD(subn
, 0),
2706 com_exec_stmt(struct compiling
*c
, node
*n
)
2709 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2710 com_node(c
, CHILD(n
, 1));
2712 com_node(c
, CHILD(n
, 3));
2714 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2718 com_node(c
, CHILD(n
, 5));
2720 com_addbyte(c
, DUP_TOP
);
2723 com_addbyte(c
, EXEC_STMT
);
2728 is_constant_false(struct compiling
*c
, node
*n
)
2732 /* argument c will be NULL when called from symtable_node() */
2734 /* Label to avoid tail recursion */
2745 for (i
= 0; i
< NCH(n
); i
++) {
2746 node
*ch
= CHILD(n
, i
);
2747 if (TYPE(ch
) == stmt
) {
2782 if (Py_OptimizeFlag
&& strcmp(STR(n
), "__debug__") == 0)
2787 v
= parsenumber(c
, STR(n
));
2792 i
= PyObject_IsTrue(v
);
2797 v
= parsestr(STR(n
));
2802 i
= PyObject_IsTrue(v
);
2811 com_if_stmt(struct compiling
*c
, node
*n
)
2816 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2817 for (i
= 0; i
+3 < NCH(n
); i
+=4) {
2819 node
*ch
= CHILD(n
, i
+1);
2820 if (is_constant_false(c
, ch
))
2823 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
2825 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
2826 com_addbyte(c
, POP_TOP
);
2828 com_node(c
, CHILD(n
, i
+3));
2829 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
2830 com_backpatch(c
, a
);
2831 /* We jump here with an extra entry which we now pop */
2832 com_addbyte(c
, POP_TOP
);
2835 com_node(c
, CHILD(n
, i
+2));
2837 com_backpatch(c
, anchor
);
2841 com_while_stmt(struct compiling
*c
, node
*n
)
2843 int break_anchor
= 0;
2845 int save_begin
= c
->c_begin
;
2846 REQ(n
, while_stmt
); /* 'while' test ':' suite ['else' ':' suite] */
2847 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2848 block_push(c
, SETUP_LOOP
);
2849 c
->c_begin
= c
->c_nexti
;
2850 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2851 com_node(c
, CHILD(n
, 1));
2852 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2853 com_addbyte(c
, POP_TOP
);
2856 com_node(c
, CHILD(n
, 3));
2858 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2859 c
->c_begin
= save_begin
;
2860 com_backpatch(c
, anchor
);
2861 /* We jump here with one entry more on the stack */
2862 com_addbyte(c
, POP_TOP
);
2863 com_addbyte(c
, POP_BLOCK
);
2864 block_pop(c
, SETUP_LOOP
);
2866 com_node(c
, CHILD(n
, 6));
2867 com_backpatch(c
, break_anchor
);
2871 com_for_stmt(struct compiling
*c
, node
*n
)
2874 int break_anchor
= 0;
2876 int save_begin
= c
->c_begin
;
2878 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
2879 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
2880 block_push(c
, SETUP_LOOP
);
2881 com_node(c
, CHILD(n
, 3));
2882 v
= PyInt_FromLong(0L);
2885 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
2888 c
->c_begin
= c
->c_nexti
;
2889 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
2890 com_addfwref(c
, FOR_LOOP
, &anchor
);
2892 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
2894 com_node(c
, CHILD(n
, 5));
2896 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
2897 c
->c_begin
= save_begin
;
2898 com_backpatch(c
, anchor
);
2899 com_pop(c
, 2); /* FOR_LOOP has popped these */
2900 com_addbyte(c
, POP_BLOCK
);
2901 block_pop(c
, SETUP_LOOP
);
2903 com_node(c
, CHILD(n
, 8));
2904 com_backpatch(c
, break_anchor
);
2907 /* Code generated for "try: S finally: Sf" is as follows:
2916 The special instructions use the block stack. Each block
2917 stack entry contains the instruction that created it (here
2918 SETUP_FINALLY), the level of the value stack at the time the
2919 block stack entry was created, and a label (here L).
2922 Pushes the current value stack level and the label
2923 onto the block stack.
2925 Pops en entry from the block stack, and pops the value
2926 stack until its level is the same as indicated on the
2927 block stack. (The label is ignored.)
2929 Pops a variable number of entries from the *value* stack
2930 and re-raises the exception they specify. The number of
2931 entries popped depends on the (pseudo) exception type.
2933 The block stack is unwound when an exception is raised:
2934 when a SETUP_FINALLY entry is found, the exception is pushed
2935 onto the value stack (and the exception condition is cleared),
2936 and the interpreter jumps to the label gotten from the block
2939 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
2940 (The contents of the value stack is shown in [], with the top
2941 at the right; 'tb' is trace-back info, 'val' the exception's
2942 associated value, and 'exc' the exception.)
2944 Value stack Label Instruction Argument
2950 [tb, val, exc] L1: DUP )
2951 [tb, val, exc, exc] <evaluate E1> )
2952 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2953 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
2954 [tb, val, exc, 1] POP )
2956 [tb, val] <assign to V1> (or POP if no V1)
2961 [tb, val, exc, 0] L2: POP
2963 .............................etc.......................
2965 [tb, val, exc, 0] Ln+1: POP
2966 [tb, val, exc] END_FINALLY # re-raise exception
2968 [] L0: <next statement>
2970 Of course, parts are not generated if Vi or Ei is not present.
2974 com_try_except(struct compiling
*c
, node
*n
)
2976 int except_anchor
= 0;
2978 int else_anchor
= 0;
2982 com_addfwref(c
, SETUP_EXCEPT
, &except_anchor
);
2983 block_push(c
, SETUP_EXCEPT
);
2984 com_node(c
, CHILD(n
, 2));
2985 com_addbyte(c
, POP_BLOCK
);
2986 block_pop(c
, SETUP_EXCEPT
);
2987 com_addfwref(c
, JUMP_FORWARD
, &else_anchor
);
2988 com_backpatch(c
, except_anchor
);
2990 i
< NCH(n
) && TYPE(ch
= CHILD(n
, i
)) == except_clause
;
2992 /* except_clause: 'except' [expr [',' var]] */
2993 if (except_anchor
== 0) {
2994 com_error(c
, PyExc_SyntaxError
,
2995 "default 'except:' must be last");
2999 com_push(c
, 3); /* tb, val, exc pushed by exception */
3000 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3002 com_addbyte(c
, DUP_TOP
);
3004 com_node(c
, CHILD(ch
, 1));
3005 com_addoparg(c
, COMPARE_OP
, EXC_MATCH
);
3007 com_addfwref(c
, JUMP_IF_FALSE
, &except_anchor
);
3008 com_addbyte(c
, POP_TOP
);
3011 com_addbyte(c
, POP_TOP
);
3014 com_assign(c
, CHILD(ch
, 3), OP_ASSIGN
, NULL
);
3016 com_addbyte(c
, POP_TOP
);
3019 com_addbyte(c
, POP_TOP
);
3021 com_node(c
, CHILD(n
, i
+2));
3022 com_addfwref(c
, JUMP_FORWARD
, &end_anchor
);
3023 if (except_anchor
) {
3024 com_backpatch(c
, except_anchor
);
3025 /* We come in with [tb, val, exc, 0] on the
3026 stack; one pop and it's the same as
3027 expected at the start of the loop */
3028 com_addbyte(c
, POP_TOP
);
3031 /* We actually come in here with [tb, val, exc] but the
3032 END_FINALLY will zap those and jump around.
3033 The c_stacklevel does not reflect them so we need not pop
3035 com_addbyte(c
, END_FINALLY
);
3036 com_backpatch(c
, else_anchor
);
3038 com_node(c
, CHILD(n
, i
+2));
3039 com_backpatch(c
, end_anchor
);
3043 com_try_finally(struct compiling
*c
, node
*n
)
3045 int finally_anchor
= 0;
3048 com_addfwref(c
, SETUP_FINALLY
, &finally_anchor
);
3049 block_push(c
, SETUP_FINALLY
);
3050 com_node(c
, CHILD(n
, 2));
3051 com_addbyte(c
, POP_BLOCK
);
3052 block_pop(c
, SETUP_FINALLY
);
3053 block_push(c
, END_FINALLY
);
3054 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3055 /* While the generated code pushes only one item,
3056 the try-finally handling can enter here with
3057 up to three items. OK, here are the details:
3058 3 for an exception, 2 for RETURN, 1 for BREAK. */
3060 com_backpatch(c
, finally_anchor
);
3061 ch
= CHILD(n
, NCH(n
)-1);
3062 com_addoparg(c
, SET_LINENO
, ch
->n_lineno
);
3064 com_addbyte(c
, END_FINALLY
);
3065 block_pop(c
, END_FINALLY
);
3066 com_pop(c
, 3); /* Matches the com_push above */
3070 com_try_stmt(struct compiling
*c
, node
*n
)
3073 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3074 | 'try' ':' suite 'finally' ':' suite */
3075 if (TYPE(CHILD(n
, 3)) != except_clause
)
3076 com_try_finally(c
, n
);
3078 com_try_except(c
, n
);
3082 get_rawdocstring(node
*n
)
3086 /* Label to avoid tail recursion */
3097 for (i
= 0; i
< NCH(n
); i
++) {
3098 node
*ch
= CHILD(n
, i
);
3099 if (TYPE(ch
) == stmt
) {
3133 if (TYPE(CHILD(n
, 0)) == STRING
)
3142 get_docstring(node
*n
)
3144 /* Don't generate doc-strings if run with -OO */
3145 if (Py_OptimizeFlag
> 1)
3147 n
= get_rawdocstring(n
);
3150 return parsestrplus(n
);
3154 com_suite(struct compiling
*c
, node
*n
)
3157 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3159 com_node(c
, CHILD(n
, 0));
3163 for (i
= 0; i
< NCH(n
) && c
->c_errors
== 0; i
++) {
3164 node
*ch
= CHILD(n
, i
);
3165 if (TYPE(ch
) == stmt
)
3173 com_continue_stmt(struct compiling
*c
, node
*n
)
3175 int i
= c
->c_nblocks
;
3176 if (i
-- > 0 && c
->c_block
[i
] == SETUP_LOOP
) {
3177 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3180 /* at the outer level */
3181 com_error(c
, PyExc_SyntaxError
,
3182 "'continue' not properly in loop");
3186 for (j
= i
-1; j
>= 0; --j
) {
3187 if (c
->c_block
[j
] == SETUP_LOOP
)
3191 /* there is a loop, but something interferes */
3192 for (; i
> j
; --i
) {
3193 if (c
->c_block
[i
] == SETUP_EXCEPT
||
3194 c
->c_block
[i
] == SETUP_FINALLY
) {
3195 com_addoparg(c
, CONTINUE_LOOP
,
3199 if (c
->c_block
[i
] == END_FINALLY
) {
3200 com_error(c
, PyExc_SyntaxError
,
3201 "'continue' not supported inside 'finally' clause");
3206 com_error(c
, PyExc_SyntaxError
,
3207 "'continue' not properly in loop");
3209 /* XXX Could allow it inside a 'finally' clause
3210 XXX if we could pop the exception still on the stack */
3214 com_argdefs(struct compiling
*c
, node
*n
)
3216 int i
, nch
, nargs
, ndefs
;
3217 if (TYPE(n
) == lambdef
) {
3218 /* lambdef: 'lambda' [varargslist] ':' test */
3222 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ... */
3224 REQ(n
, parameters
); /* parameters: '(' [varargslist] ')' */
3227 if (TYPE(n
) != varargslist
)
3230 (fpdef ['=' test] ',')* '*' ....... |
3231 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3235 for (i
= 0; i
< nch
; i
++) {
3237 if (TYPE(CHILD(n
, i
)) == STAR
||
3238 TYPE(CHILD(n
, i
)) == DOUBLESTAR
)
3243 t
= RPAR
; /* Anything except EQUAL or COMMA */
3245 t
= TYPE(CHILD(n
, i
));
3249 com_node(c
, CHILD(n
, i
));
3253 t
= TYPE(CHILD(n
, i
));
3256 /* Treat "(a=1, b)" as an error */
3258 com_error(c
, PyExc_SyntaxError
,
3259 "non-default argument follows default argument");
3268 com_funcdef(struct compiling
*c
, node
*n
)
3272 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3273 ndefs
= com_argdefs(c
, n
);
3274 symtable_enter_scope(c
->c_symtable
, STR(CHILD(n
, 1)), TYPE(n
),
3276 co
= (PyObject
*)icompile(n
, c
);
3277 symtable_exit_scope(c
->c_symtable
);
3281 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3282 int i
= com_addconst(c
, co
);
3283 com_addoparg(c
, LOAD_CONST
, i
);
3286 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
3288 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
3290 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3297 com_bases(struct compiling
*c
, node
*n
)
3301 /* testlist: test (',' test)* [','] */
3302 for (i
= 0; i
< NCH(n
); i
+= 2)
3303 com_node(c
, CHILD(n
, i
));
3305 com_addoparg(c
, BUILD_TUPLE
, i
);
3310 com_classdef(struct compiling
*c
, node
*n
)
3317 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3318 if ((v
= PyString_InternFromString(STR(CHILD(n
, 1)))) == NULL
) {
3322 /* Push the class name on the stack */
3323 i
= com_addconst(c
, v
);
3324 com_addoparg(c
, LOAD_CONST
, i
);
3327 /* Push the tuple of base classes on the stack */
3328 if (TYPE(CHILD(n
, 2)) != LPAR
) {
3329 com_addoparg(c
, BUILD_TUPLE
, 0);
3333 com_bases(c
, CHILD(n
, 3));
3334 name
= STR(CHILD(n
, 1));
3335 symtable_enter_scope(c
->c_symtable
, name
, TYPE(n
), n
->n_lineno
);
3336 co
= (PyObject
*)icompile(n
, c
);
3337 symtable_exit_scope(c
->c_symtable
);
3341 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3342 i
= com_addconst(c
, co
);
3343 com_addoparg(c
, LOAD_CONST
, i
);
3346 com_addoparg(c
, MAKE_CLOSURE
, 0);
3348 com_addoparg(c
, MAKE_FUNCTION
, 0);
3349 com_addoparg(c
, CALL_FUNCTION
, 0);
3350 com_addbyte(c
, BUILD_CLASS
);
3352 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3358 com_node(struct compiling
*c
, node
*n
)
3365 /* Definition nodes */
3374 /* Trivial parse tree nodes */
3383 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3384 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3387 for (i
= 0; i
< NCH(n
)-1; i
+= 2)
3388 com_node(c
, CHILD(n
, i
));
3393 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3397 /* Statement nodes */
3400 com_expr_stmt(c
, n
);
3403 com_print_stmt(c
, n
);
3405 case del_stmt
: /* 'del' exprlist */
3406 com_assign(c
, CHILD(n
, 1), OP_DELETE
, NULL
);
3411 if (c
->c_loops
== 0) {
3412 com_error(c
, PyExc_SyntaxError
,
3413 "'break' outside loop");
3415 com_addbyte(c
, BREAK_LOOP
);
3418 com_continue_stmt(c
, n
);
3421 com_return_stmt(c
, n
);
3424 com_raise_stmt(c
, n
);
3427 com_import_stmt(c
, n
);
3432 com_exec_stmt(c
, n
);
3435 com_assert_stmt(c
, n
);
3441 com_while_stmt(c
, n
);
3453 /* Expression nodes */
3468 com_comparison(c
, n
);
3483 com_shift_expr(c
, n
);
3486 com_arith_expr(c
, n
);
3502 com_error(c
, PyExc_SystemError
,
3503 "com_node: unexpected node type");
3507 static void com_fplist(struct compiling
*, node
*);
3510 com_fpdef(struct compiling
*c
, node
*n
)
3512 REQ(n
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3513 if (TYPE(CHILD(n
, 0)) == LPAR
)
3514 com_fplist(c
, CHILD(n
, 1));
3516 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
3522 com_fplist(struct compiling
*c
, node
*n
)
3524 REQ(n
, fplist
); /* fplist: fpdef (',' fpdef)* [','] */
3526 com_fpdef(c
, CHILD(n
, 0));
3529 int i
= (NCH(n
)+1)/2;
3530 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
3532 for (i
= 0; i
< NCH(n
); i
+= 2)
3533 com_fpdef(c
, CHILD(n
, i
));
3538 com_arglist(struct compiling
*c
, node
*n
)
3543 REQ(n
, varargslist
);
3545 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3547 /* Enter all arguments in table of locals */
3548 for (i
= 0, narg
= 0; i
< nch
; i
++) {
3549 node
*ch
= CHILD(n
, i
);
3552 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3554 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3556 if (TYPE(fp
) == NAME
) {
3559 v
= PyDict_GetItemString(c
->c_cellvars
, name
);
3561 com_addoparg(c
, LOAD_FAST
, narg
);
3562 com_addoparg(c
, STORE_DEREF
,
3567 sprintf(nbuf
, ".%d", i
);
3571 /* all name updates handled by symtable */
3575 if (TYPE(ch
) == EQUAL
)
3580 /* Handle *arguments */
3584 if (TYPE(ch
) != DOUBLESTAR
) {
3587 if (TYPE(ch
) == NAME
) {
3590 v
= PyDict_GetItemString(c
->c_cellvars
,
3593 com_addoparg(c
, LOAD_FAST
, narg
);
3594 com_addoparg(c
, STORE_DEREF
,
3601 /* Handle **keywords */
3606 if (TYPE(ch
) != DOUBLESTAR
) {
3615 v
= PyDict_GetItemString(c
->c_cellvars
, STR(ch
));
3617 com_addoparg(c
, LOAD_FAST
, narg
);
3618 com_addoparg(c
, STORE_DEREF
, PyInt_AS_LONG(v
));
3622 /* Generate code for complex arguments only after
3623 having counted the simple arguments */
3625 for (i
= 0; i
< nch
; i
++) {
3626 node
*ch
= CHILD(n
, i
);
3628 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3630 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3632 if (TYPE(fp
) != NAME
) {
3633 com_addoparg(c
, LOAD_FAST
, ilocal
);
3641 if (TYPE(ch
) == EQUAL
)
3650 com_file_input(struct compiling
*c
, node
*n
)
3654 REQ(n
, file_input
); /* (NEWLINE | stmt)* ENDMARKER */
3655 doc
= get_docstring(n
);
3657 int i
= com_addconst(c
, doc
);
3659 com_addoparg(c
, LOAD_CONST
, i
);
3661 com_addop_name(c
, STORE_NAME
, "__doc__");
3664 for (i
= 0; i
< NCH(n
); i
++) {
3665 node
*ch
= CHILD(n
, i
);
3666 if (TYPE(ch
) != ENDMARKER
&& TYPE(ch
) != NEWLINE
)
3671 /* Top-level compile-node interface */
3674 compile_funcdef(struct compiling
*c
, node
*n
)
3678 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3679 c
->c_name
= STR(CHILD(n
, 1));
3680 doc
= get_docstring(CHILD(n
, 4));
3682 (void) com_addconst(c
, doc
);
3686 (void) com_addconst(c
, Py_None
); /* No docstring */
3687 ch
= CHILD(n
, 2); /* parameters: '(' [varargslist] ')' */
3688 ch
= CHILD(ch
, 1); /* ')' | varargslist */
3689 if (TYPE(ch
) == varargslist
)
3691 c
->c_infunction
= 1;
3692 com_node(c
, CHILD(n
, 4));
3693 c
->c_infunction
= 0;
3694 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3696 com_addbyte(c
, RETURN_VALUE
);
3701 compile_lambdef(struct compiling
*c
, node
*n
)
3704 REQ(n
, lambdef
); /* lambdef: 'lambda' [varargslist] ':' test */
3705 c
->c_name
= "<lambda>";
3708 (void) com_addconst(c
, Py_None
); /* No docstring */
3709 if (TYPE(ch
) == varargslist
) {
3716 com_addbyte(c
, RETURN_VALUE
);
3721 compile_classdef(struct compiling
*c
, node
*n
)
3726 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3727 c
->c_name
= STR(CHILD(n
, 1));
3728 c
->c_private
= c
->c_name
;
3729 ch
= CHILD(n
, NCH(n
)-1); /* The suite */
3730 doc
= get_docstring(ch
);
3732 int i
= com_addconst(c
, doc
);
3734 com_addoparg(c
, LOAD_CONST
, i
);
3736 com_addop_name(c
, STORE_NAME
, "__doc__");
3740 (void) com_addconst(c
, Py_None
);
3742 com_addbyte(c
, LOAD_LOCALS
);
3744 com_addbyte(c
, RETURN_VALUE
);
3749 compile_node(struct compiling
*c
, node
*n
)
3751 com_addoparg(c
, SET_LINENO
, n
->n_lineno
);
3755 case single_input
: /* One interactive command */
3756 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
3759 if (TYPE(n
) != NEWLINE
)
3761 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3763 com_addbyte(c
, RETURN_VALUE
);
3768 case file_input
: /* A whole file, or built-in function exec() */
3769 com_file_input(c
, n
);
3770 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3772 com_addbyte(c
, RETURN_VALUE
);
3776 case eval_input
: /* Built-in function input() */
3777 com_node(c
, CHILD(n
, 0));
3778 com_addbyte(c
, RETURN_VALUE
);
3782 case lambdef
: /* anonymous function definition */
3783 compile_lambdef(c
, n
);
3786 case funcdef
: /* A function definition */
3787 compile_funcdef(c
, n
);
3790 case classdef
: /* A class definition */
3791 compile_classdef(c
, n
);
3795 com_error(c
, PyExc_SystemError
,
3796 "compile_node: unexpected node type");
3801 dict_keys_inorder(PyObject
*dict
, int offset
)
3803 PyObject
*tuple
, *k
, *v
;
3804 int i
, pos
= 0, size
= PyDict_Size(dict
);
3806 tuple
= PyTuple_New(size
);
3809 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
3810 i
= PyInt_AS_LONG(v
);
3812 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
3818 PyNode_Compile(node
*n
, char *filename
)
3820 return PyNode_CompileFlags(n
, filename
, NULL
);
3824 PyNode_CompileFlags(node
*n
, char *filename
, PyCompilerFlags
*flags
)
3826 return jcompile(n
, filename
, NULL
, flags
);
3830 PyNode_CompileSymtable(node
*n
, char *filename
)
3832 struct symtable
*st
;
3834 st
= symtable_init();
3837 assert(st
->st_symbols
!= NULL
);
3838 symtable_enter_scope(st
, TOP
, TYPE(n
), n
->n_lineno
);
3839 if (st
->st_errors
> 0) {
3840 PySymtable_Free(st
);
3843 symtable_node(st
, n
);
3844 if (st
->st_errors
> 0) {
3845 PySymtable_Free(st
);
3851 static PyCodeObject
*
3852 icompile(node
*n
, struct compiling
*base
)
3854 return jcompile(n
, base
->c_filename
, base
, NULL
);
3857 static PyCodeObject
*
3858 jcompile(node
*n
, char *filename
, struct compiling
*base
,
3859 PyCompilerFlags
*flags
)
3861 struct compiling sc
;
3863 if (!com_init(&sc
, filename
))
3866 sc
.c_private
= base
->c_private
;
3867 sc
.c_symtable
= base
->c_symtable
;
3868 /* c_symtable still points to parent's symbols */
3870 || (sc
.c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
))
3873 sc
.c_private
= NULL
;
3874 sc
.c_future
= PyNode_Future(n
, filename
);
3875 if (sc
.c_future
== NULL
) {
3880 if (flags
->cf_nested_scopes
)
3881 sc
.c_future
->ff_nested_scopes
= 1;
3882 else if (sc
.c_future
->ff_nested_scopes
)
3883 flags
->cf_nested_scopes
= 1;
3885 if (symtable_build(&sc
, n
) < 0) {
3891 if (symtable_load_symbols(&sc
) < 0) {
3895 compile_node(&sc
, n
);
3897 if (sc
.c_errors
== 0) {
3898 PyObject
*consts
, *names
, *varnames
, *filename
, *name
,
3899 *freevars
, *cellvars
;
3900 consts
= PyList_AsTuple(sc
.c_consts
);
3901 names
= PyList_AsTuple(sc
.c_names
);
3902 varnames
= PyList_AsTuple(sc
.c_varnames
);
3903 cellvars
= dict_keys_inorder(sc
.c_cellvars
, 0);
3904 freevars
= dict_keys_inorder(sc
.c_freevars
,
3905 PyTuple_GET_SIZE(cellvars
));
3906 filename
= PyString_InternFromString(sc
.c_filename
);
3907 name
= PyString_InternFromString(sc
.c_name
);
3908 if (!PyErr_Occurred())
3909 co
= PyCode_New(sc
.c_argcount
,
3925 Py_XDECREF(varnames
);
3926 Py_XDECREF(freevars
);
3927 Py_XDECREF(cellvars
);
3928 Py_XDECREF(filename
);
3931 else if (!PyErr_Occurred()) {
3932 /* This could happen if someone called PyErr_Clear() after an
3933 error was reported above. That's not supposed to happen,
3934 but I just plugged one case and I'm not sure there can't be
3935 others. In that case, raise SystemError so that at least
3936 it gets reported instead dumping core. */
3937 PyErr_SetString(PyExc_SystemError
, "lost syntax error");
3941 PySymtable_Free(sc
.c_symtable
);
3942 sc
.c_symtable
= NULL
;
3949 PyCode_Addr2Line(PyCodeObject
*co
, int addrq
)
3951 int size
= PyString_Size(co
->co_lnotab
) / 2;
3952 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
3953 int line
= co
->co_firstlineno
;
3955 while (--size
>= 0) {
3964 /* The test for LOCAL must come before the test for FREE in order to
3965 handle classes where name is both local and free. The local var is
3966 a method and the free var is a free var referenced within a method.
3970 get_ref_type(struct compiling
*c
, char *name
)
3973 if (c
->c_symtable
->st_nested_scopes
) {
3974 if (PyDict_GetItemString(c
->c_cellvars
, name
) != NULL
)
3976 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
3978 if (PyDict_GetItemString(c
->c_freevars
, name
) != NULL
)
3980 v
= PyDict_GetItemString(c
->c_globals
, name
);
3983 return GLOBAL_EXPLICIT
;
3985 return GLOBAL_IMPLICIT
;
3989 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
3991 v
= PyDict_GetItemString(c
->c_globals
, name
);
3994 return GLOBAL_EXPLICIT
;
3996 return GLOBAL_IMPLICIT
;
4003 "unknown scope for %.100s in %.100s(%s) "
4004 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4006 PyObject_REPR(c
->c_symtable
->st_cur
->ste_id
),
4008 PyObject_REPR(c
->c_symtable
->st_cur
->ste_symbols
),
4009 PyObject_REPR(c
->c_locals
),
4010 PyObject_REPR(c
->c_globals
)
4015 return -1; /* can't get here */
4018 /* Helper functions to issue warnings */
4021 issue_warning(char *msg
, char *filename
, int lineno
)
4023 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, filename
,
4024 lineno
, NULL
, NULL
) < 0) {
4025 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
4026 PyErr_SetString(PyExc_SyntaxError
, msg
);
4027 PyErr_SyntaxLocation(filename
, lineno
);
4035 symtable_warn(struct symtable
*st
, char *msg
)
4037 if (issue_warning(msg
, st
->st_filename
, st
->st_cur
->ste_lineno
) < 0) {
4044 /* Helper function for setting lineno and filename */
4047 symtable_build(struct compiling
*c
, node
*n
)
4049 if ((c
->c_symtable
= symtable_init()) == NULL
)
4051 c
->c_symtable
->st_future
= c
->c_future
;
4052 if (c
->c_future
->ff_nested_scopes
)
4053 c
->c_symtable
->st_nested_scopes
= 1;
4054 c
->c_symtable
->st_filename
= c
->c_filename
;
4055 symtable_enter_scope(c
->c_symtable
, TOP
, TYPE(n
), n
->n_lineno
);
4056 if (c
->c_symtable
->st_errors
> 0)
4058 symtable_node(c
->c_symtable
, n
);
4059 if (c
->c_symtable
->st_errors
> 0)
4061 /* reset for second pass */
4062 c
->c_symtable
->st_nscopes
= 1;
4063 c
->c_symtable
->st_pass
= 2;
4068 symtable_init_compiling_symbols(struct compiling
*c
)
4072 varnames
= c
->c_symtable
->st_cur
->ste_varnames
;
4073 if (varnames
== NULL
) {
4074 varnames
= PyList_New(0);
4075 if (varnames
== NULL
)
4077 c
->c_symtable
->st_cur
->ste_varnames
= varnames
;
4078 Py_INCREF(varnames
);
4080 Py_INCREF(varnames
);
4081 c
->c_varnames
= varnames
;
4083 c
->c_globals
= PyDict_New();
4084 if (c
->c_globals
== NULL
)
4086 c
->c_freevars
= PyDict_New();
4087 if (c
->c_freevars
== NULL
)
4089 c
->c_cellvars
= PyDict_New();
4090 if (c
->c_cellvars
== NULL
)
4095 struct symbol_info
{
4103 symtable_init_info(struct symbol_info
*si
)
4108 si
->si_nimplicit
= 0;
4112 symtable_resolve_free(struct compiling
*c
, PyObject
*name
,
4113 struct symbol_info
*si
)
4117 /* Seperate logic for DEF_FREE. If it occurs in a function,
4118 it indicates a local that we must allocate storage for (a
4119 cell var). If it occurs in a class, then the class has a
4120 method and a free variable with the same name.
4123 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
) {
4124 v
= PyInt_FromLong(si
->si_ncells
++);
4125 dict
= c
->c_cellvars
;
4127 v
= PyInt_FromLong(si
->si_nfrees
++);
4128 dict
= c
->c_freevars
;
4132 if (PyDict_SetItem(dict
, name
, v
) < 0) {
4141 symtable_freevar_offsets(PyObject
*freevars
, int offset
)
4146 /* The cell vars are the first elements of the closure,
4147 followed by the free vars. Update the offsets in
4148 c_freevars to account for number of cellvars. */
4150 while (PyDict_Next(freevars
, &pos
, &name
, &v
)) {
4151 int i
= PyInt_AS_LONG(v
) + offset
;
4152 PyObject
*o
= PyInt_FromLong(i
);
4155 if (PyDict_SetItem(freevars
, name
, o
) < 0) {
4165 symtable_check_unoptimized(struct compiling
*c
,
4166 PySymtableEntryObject
*ste
,
4167 struct symbol_info
*si
)
4171 if (!(si
->si_ncells
|| si
->si_nfrees
|| ste
->ste_child_free
4172 || (ste
->ste_nested
&& si
->si_nimplicit
)))
4175 #define ILLEGAL_IMPORT_STAR \
4176 "import * is not allowed in function '%.100s' " \
4177 "because it contains a nested function with free variables"
4179 #define ILLEGAL_BARE_EXEC \
4180 "unqualified exec is not allowed in function '%.100s' " \
4181 "because it contains a nested function with free variables"
4183 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4184 "function '%.100s' uses import * and bare exec, which are illegal" \
4185 "because it contains a nested function with free variables"
4187 /* XXX perhaps the linenos for these opt-breaking statements
4188 should be stored so the exception can point to them. */
4190 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4191 sprintf(buf
, ILLEGAL_IMPORT_STAR
,
4192 PyString_AS_STRING(ste
->ste_name
));
4193 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4194 sprintf(buf
, ILLEGAL_BARE_EXEC
,
4195 PyString_AS_STRING(ste
->ste_name
));
4197 sprintf(buf
, ILLEGAL_EXEC_AND_IMPORT_STAR
,
4198 PyString_AS_STRING(ste
->ste_name
));
4201 if (c
->c_symtable
->st_nested_scopes
) {
4202 PyErr_SetString(PyExc_SyntaxError
, buf
);
4203 PyErr_SyntaxLocation(c
->c_symtable
->st_filename
,
4208 return issue_warning(buf
, c
->c_filename
, ste
->ste_lineno
);
4214 symtable_check_shadow(struct symtable
*st
, PyObject
*name
, int flags
)
4217 PyObject
*children
, *v
;
4218 PySymtableEntryObject
*child
= NULL
;
4221 if (!(flags
& DEF_BOUND
))
4223 /* The semantics of this code will change with nested scopes.
4224 It is defined in the current scope and referenced in a
4225 child scope. Under the old rules, the child will see a
4226 global. Under the new rules, the child will see the
4227 binding in the current scope.
4230 /* Find name of child function that has free variable */
4231 children
= st
->st_cur
->ste_children
;
4232 for (i
= 0; i
< PyList_GET_SIZE(children
); i
++) {
4234 child
= (PySymtableEntryObject
*)PyList_GET_ITEM(children
, i
);
4235 v
= PyDict_GetItem(child
->ste_symbols
, name
);
4238 cflags
= PyInt_AS_LONG(v
);
4239 if (!(cflags
& DEF_BOUND
))
4243 assert(child
!= NULL
);
4245 sprintf(buf
, "local name '%.100s' in '%.100s' shadows "
4246 "use of '%.100s' as global in nested scope '%.100s'",
4247 PyString_AS_STRING(name
),
4248 PyString_AS_STRING(st
->st_cur
->ste_name
),
4249 PyString_AS_STRING(name
),
4250 PyString_AS_STRING(child
->ste_name
)
4253 return symtable_warn(st
, buf
);
4257 symtable_update_flags(struct compiling
*c
, PySymtableEntryObject
*ste
,
4258 struct symbol_info
*si
)
4260 if (ste
->ste_type
!= TYPE_MODULE
)
4261 c
->c_flags
|= CO_NEWLOCALS
;
4262 if (ste
->ste_type
== TYPE_FUNCTION
) {
4263 c
->c_nlocals
= si
->si_nlocals
;
4264 if (ste
->ste_optimized
== 0)
4265 c
->c_flags
|= CO_OPTIMIZED
;
4266 else if (ste
->ste_optimized
!= OPT_EXEC
)
4267 return symtable_check_unoptimized(c
, ste
, si
);
4273 symtable_load_symbols(struct compiling
*c
)
4275 static PyObject
*implicit
= NULL
;
4276 struct symtable
*st
= c
->c_symtable
;
4277 PySymtableEntryObject
*ste
= st
->st_cur
;
4278 PyObject
*name
, *varnames
, *v
;
4280 struct symbol_info si
;
4282 if (implicit
== NULL
) {
4283 implicit
= PyInt_FromLong(1);
4284 if (implicit
== NULL
)
4289 if (symtable_init_compiling_symbols(c
) < 0)
4291 symtable_init_info(&si
);
4292 varnames
= st
->st_cur
->ste_varnames
;
4293 si
.si_nlocals
= PyList_GET_SIZE(varnames
);
4294 c
->c_argcount
= si
.si_nlocals
;
4296 for (i
= 0; i
< si
.si_nlocals
; ++i
) {
4297 v
= PyInt_FromLong(i
);
4298 if (PyDict_SetItem(c
->c_locals
,
4299 PyList_GET_ITEM(varnames
, i
), v
) < 0)
4304 /* XXX The cases below define the rules for whether a name is
4305 local or global. The logic could probably be clearer. */
4307 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
4308 flags
= PyInt_AS_LONG(v
);
4310 if (st
->st_nested_scopes
== 0
4311 && (flags
& (DEF_FREE
| DEF_FREE_CLASS
))) {
4312 if (symtable_check_shadow(st
, name
, flags
) < 0)
4316 if (flags
& DEF_FREE_GLOBAL
)
4317 /* undo the original DEF_FREE */
4318 flags
&= ~(DEF_FREE
| DEF_FREE_CLASS
);
4320 if ((flags
& (DEF_FREE
| DEF_FREE_CLASS
))
4321 && (flags
& (DEF_LOCAL
| DEF_PARAM
)))
4322 symtable_resolve_free(c
, name
, &si
);
4324 if (flags
& DEF_STAR
) {
4326 c
->c_flags
|= CO_VARARGS
;
4327 } else if (flags
& DEF_DOUBLESTAR
) {
4329 c
->c_flags
|= CO_VARKEYWORDS
;
4330 } else if (flags
& DEF_INTUPLE
)
4332 else if (flags
& DEF_GLOBAL
) {
4333 if (flags
& DEF_PARAM
) {
4334 PyErr_Format(PyExc_SyntaxError
, LOCAL_GLOBAL
,
4335 PyString_AS_STRING(name
));
4336 PyErr_SyntaxLocation(st
->st_filename
,
4341 if (PyDict_SetItem(c
->c_globals
, name
, Py_None
) < 0)
4343 } else if (flags
& DEF_FREE_GLOBAL
) {
4345 if (PyDict_SetItem(c
->c_globals
, name
, implicit
) < 0)
4347 } else if ((flags
& DEF_LOCAL
) && !(flags
& DEF_PARAM
)) {
4348 v
= PyInt_FromLong(si
.si_nlocals
++);
4351 if (PyDict_SetItem(c
->c_locals
, name
, v
) < 0)
4354 if (ste
->ste_type
!= TYPE_CLASS
)
4355 if (PyList_Append(c
->c_varnames
, name
) < 0)
4357 } else if (is_free(flags
)) {
4358 if (ste
->ste_nested
&& st
->st_nested_scopes
) {
4359 v
= PyInt_FromLong(si
.si_nfrees
++);
4362 if (PyDict_SetItem(c
->c_freevars
, name
, v
) < 0)
4367 if (PyDict_SetItem(c
->c_globals
, name
,
4370 if (st
->st_nscopes
!= 1) {
4371 v
= PyInt_FromLong(flags
);
4372 if (PyDict_SetItem(st
->st_global
,
4381 if (symtable_freevar_offsets(c
->c_freevars
, si
.si_ncells
) < 0)
4383 return symtable_update_flags(c
, ste
, &si
);
4385 /* is this always the right thing to do? */
4390 static struct symtable
*
4393 struct symtable
*st
;
4395 st
= (struct symtable
*)PyMem_Malloc(sizeof(struct symtable
));
4399 st
->st_nested_scopes
= NESTED_SCOPES_DEFAULT
;
4400 st
->st_filename
= NULL
;
4401 if ((st
->st_stack
= PyList_New(0)) == NULL
)
4403 if ((st
->st_symbols
= PyDict_New()) == NULL
)
4409 st
->st_private
= NULL
;
4412 PySymtable_Free(st
);
4417 PySymtable_Free(struct symtable
*st
)
4419 Py_XDECREF(st
->st_symbols
);
4420 Py_XDECREF(st
->st_stack
);
4421 Py_XDECREF(st
->st_cur
);
4422 PyMem_Free((void *)st
);
4425 /* When the compiler exits a scope, it must should update the scope's
4426 free variable information with the list of free variables in its
4429 Variables that are free in children and defined in the current
4432 If the scope being exited is defined at the top-level (ste_nested is
4433 false), free variables in children that are not defined here are
4439 symtable_update_free_vars(struct symtable
*st
)
4442 PyObject
*o
, *name
, *list
= NULL
;
4443 PySymtableEntryObject
*child
, *ste
= st
->st_cur
;
4445 if (ste
->ste_type
== TYPE_CLASS
)
4446 def
= DEF_FREE_CLASS
;
4449 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4453 PyList_SetSlice(list
, 0,
4454 ((PyVarObject
*)list
)->ob_size
, 0);
4455 child
= (PySymtableEntryObject
*)
4456 PyList_GET_ITEM(ste
->ste_children
, i
);
4457 while (PyDict_Next(child
->ste_symbols
, &pos
, &name
, &o
)) {
4458 int flags
= PyInt_AS_LONG(o
);
4459 if (!(is_free(flags
)))
4460 continue; /* avoids indentation */
4462 list
= PyList_New(0);
4466 ste
->ste_child_free
= 1;
4467 if (PyList_Append(list
, name
) < 0) {
4472 for (j
= 0; list
&& j
< PyList_GET_SIZE(list
); j
++) {
4474 name
= PyList_GET_ITEM(list
, j
);
4475 v
= PyDict_GetItem(ste
->ste_symbols
, name
);
4476 /* If a name N is declared global in scope A and
4477 referenced in scope B contained (perhaps
4478 indirectly) in A and there are no scopes
4479 with bindings for N between B and A, then N
4483 int flags
= PyInt_AS_LONG(v
);
4484 if (flags
& DEF_GLOBAL
) {
4485 symtable_undo_free(st
, child
->ste_id
,
4490 if (ste
->ste_nested
) {
4491 if (symtable_add_def_o(st
, ste
->ste_symbols
,
4497 if (symtable_check_global(st
, child
->ste_id
,
4510 /* If the current scope is a non-nested class or if name is not
4511 defined in the current, non-nested scope, then it is an implicit
4512 global in all nested scopes.
4516 symtable_check_global(struct symtable
*st
, PyObject
*child
, PyObject
*name
)
4520 PySymtableEntryObject
*ste
= st
->st_cur
;
4522 if (ste
->ste_type
== TYPE_CLASS
)
4523 return symtable_undo_free(st
, child
, name
);
4524 o
= PyDict_GetItem(ste
->ste_symbols
, name
);
4526 return symtable_undo_free(st
, child
, name
);
4527 v
= PyInt_AS_LONG(o
);
4529 if (is_free(v
) || (v
& DEF_GLOBAL
))
4530 return symtable_undo_free(st
, child
, name
);
4532 return symtable_add_def_o(st
, ste
->ste_symbols
,
4537 symtable_undo_free(struct symtable
*st
, PyObject
*id
,
4542 PySymtableEntryObject
*ste
;
4544 ste
= (PySymtableEntryObject
*)PyDict_GetItem(st
->st_symbols
, id
);
4548 info
= PyDict_GetItem(ste
->ste_symbols
, name
);
4551 v
= PyInt_AS_LONG(info
);
4553 if (symtable_add_def_o(st
, ste
->ste_symbols
, name
,
4554 DEF_FREE_GLOBAL
) < 0)
4557 /* If the name is defined here or declared global,
4558 then the recursion stops. */
4561 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4562 PySymtableEntryObject
*child
;
4563 child
= (PySymtableEntryObject
*)
4564 PyList_GET_ITEM(ste
->ste_children
, i
);
4565 x
= symtable_undo_free(st
, child
->ste_id
, name
);
4572 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4573 This reference is released when the scope is exited, via the DECREF
4574 in symtable_exit_scope().
4578 symtable_exit_scope(struct symtable
*st
)
4582 if (st
->st_pass
== 1)
4583 symtable_update_free_vars(st
);
4584 Py_DECREF(st
->st_cur
);
4585 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
4586 st
->st_cur
= (PySymtableEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
4588 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
4594 symtable_enter_scope(struct symtable
*st
, char *name
, int type
,
4597 PySymtableEntryObject
*prev
= NULL
;
4601 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
4602 Py_DECREF(st
->st_cur
);
4607 st
->st_cur
= (PySymtableEntryObject
*)
4608 PySymtableEntry_New(st
, name
, type
, lineno
);
4609 if (strcmp(name
, TOP
) == 0)
4610 st
->st_global
= st
->st_cur
->ste_symbols
;
4611 if (prev
&& st
->st_pass
== 1) {
4612 if (PyList_Append(prev
->ste_children
,
4613 (PyObject
*)st
->st_cur
) < 0)
4619 symtable_lookup(struct symtable
*st
, char *name
)
4621 char buffer
[MANGLE_LEN
];
4625 if (mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4627 v
= PyDict_GetItemString(st
->st_cur
->ste_symbols
, name
);
4629 if (PyErr_Occurred())
4635 flags
= PyInt_AS_LONG(v
);
4640 symtable_add_def(struct symtable
*st
, char *name
, int flag
)
4643 char buffer
[MANGLE_LEN
];
4646 if (mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
4648 if ((s
= PyString_InternFromString(name
)) == NULL
)
4650 ret
= symtable_add_def_o(st
, st
->st_cur
->ste_symbols
, s
, flag
);
4655 /* Must only be called with mangled names */
4658 symtable_add_def_o(struct symtable
*st
, PyObject
*dict
,
4659 PyObject
*name
, int flag
)
4664 if ((o
= PyDict_GetItem(dict
, name
))) {
4665 val
= PyInt_AS_LONG(o
);
4666 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
4667 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
4668 PyString_AsString(name
));
4669 PyErr_SyntaxLocation(st
->st_filename
,
4670 st
->st_cur
->ste_lineno
);
4676 o
= PyInt_FromLong(val
);
4677 if (PyDict_SetItem(dict
, name
, o
) < 0) {
4683 if (flag
& DEF_PARAM
) {
4684 if (PyList_Append(st
->st_cur
->ste_varnames
, name
) < 0)
4686 } else if (flag
& DEF_GLOBAL
) {
4687 /* XXX need to update DEF_GLOBAL for other flags too;
4688 perhaps only DEF_FREE_GLOBAL */
4689 if ((o
= PyDict_GetItem(st
->st_global
, name
))) {
4690 val
= PyInt_AS_LONG(o
);
4694 o
= PyInt_FromLong(val
);
4695 if (PyDict_SetItem(st
->st_global
, name
, o
) < 0) {
4704 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
4707 symtable_node(struct symtable
*st
, node
*n
)
4714 char *func_name
= STR(CHILD(n
, 1));
4715 symtable_add_def(st
, func_name
, DEF_LOCAL
);
4716 symtable_default_args(st
, CHILD(n
, 2));
4717 symtable_enter_scope(st
, func_name
, TYPE(n
), n
->n_lineno
);
4718 symtable_funcdef(st
, n
);
4719 symtable_exit_scope(st
);
4724 symtable_default_args(st
, CHILD(n
, 1));
4725 symtable_enter_scope(st
, "lambda", TYPE(n
), n
->n_lineno
);
4726 symtable_funcdef(st
, n
);
4727 symtable_exit_scope(st
);
4730 char *tmp
, *class_name
= STR(CHILD(n
, 1));
4731 symtable_add_def(st
, class_name
, DEF_LOCAL
);
4732 if (TYPE(CHILD(n
, 2)) == LPAR
) {
4733 node
*bases
= CHILD(n
, 3);
4735 for (i
= 0; i
< NCH(bases
); i
+= 2) {
4736 symtable_node(st
, CHILD(bases
, i
));
4739 symtable_enter_scope(st
, class_name
, TYPE(n
), n
->n_lineno
);
4740 tmp
= st
->st_private
;
4741 st
->st_private
= class_name
;
4742 symtable_node(st
, CHILD(n
, NCH(n
) - 1));
4743 st
->st_private
= tmp
;
4744 symtable_exit_scope(st
);
4748 for (i
= 0; i
+ 3 < NCH(n
); i
+= 4) {
4749 if (is_constant_false(NULL
, (CHILD(n
, i
+ 1))))
4751 symtable_node(st
, CHILD(n
, i
+ 1));
4752 symtable_node(st
, CHILD(n
, i
+ 3));
4755 symtable_node(st
, CHILD(n
, i
+ 2));
4758 symtable_global(st
, n
);
4761 symtable_import(st
, n
);
4764 st
->st_cur
->ste_optimized
|= OPT_EXEC
;
4765 symtable_node(st
, CHILD(n
, 1));
4767 symtable_node(st
, CHILD(n
, 3));
4769 st
->st_cur
->ste_optimized
|= OPT_BARE_EXEC
;
4771 symtable_node(st
, CHILD(n
, 5));
4776 if (Py_OptimizeFlag
)
4782 symtable_node(st
, CHILD(n
, 1));
4788 symtable_assign(st
, CHILD(n
, 3), 0);
4795 symtable_assign(st
, CHILD(n
, 1), 0);
4801 if (TYPE(CHILD(n
, 1)) == augassign
) {
4802 symtable_assign(st
, CHILD(n
, 0), 0);
4803 symtable_node(st
, CHILD(n
, 2));
4807 for (i
= 0; i
< NCH(n
) - 2; i
+= 2)
4808 symtable_assign(st
, CHILD(n
, i
), 0);
4809 n
= CHILD(n
, NCH(n
) - 1);
4813 /* watchout for fall-through logic below */
4820 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
4821 symtable_list_comprehension(st
, CHILD(n
, 1));
4826 if (TYPE(n
) == atom
&& TYPE(CHILD(n
, 0)) == NAME
) {
4827 symtable_add_use(st
, STR(CHILD(n
, 0)));
4831 if (TYPE(n
) == for_stmt
) {
4832 symtable_assign(st
, CHILD(n
, 1), 0);
4840 for (i
= start
; i
< NCH(n
); ++i
)
4841 if (TYPE(CHILD(n
, i
)) >= single_input
)
4842 symtable_node(st
, CHILD(n
, i
));
4847 symtable_funcdef(struct symtable
*st
, node
*n
)
4851 if (TYPE(n
) == lambdef
) {
4853 symtable_params(st
, CHILD(n
, 1));
4855 symtable_params(st
, CHILD(n
, 2));
4856 body
= CHILD(n
, NCH(n
) - 1);
4857 symtable_node(st
, body
);
4860 /* The next two functions parse the argument tuple.
4861 symtable_default_arg() checks for names in the default arguments,
4862 which are references in the defining scope. symtable_params()
4863 parses the parameter names, which are defined in the function's
4867 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
4868 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
4872 symtable_default_args(struct symtable
*st
, node
*n
)
4877 if (TYPE(n
) == parameters
) {
4879 if (TYPE(n
) == RPAR
)
4882 REQ(n
, varargslist
);
4883 for (i
= 0; i
< NCH(n
); i
+= 2) {
4885 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
4888 if (i
> 0 && (TYPE(CHILD(n
, i
- 1)) == EQUAL
))
4889 symtable_node(st
, CHILD(n
, i
));
4894 symtable_params(struct symtable
*st
, node
*n
)
4896 int i
, complex = -1, ext
= 0;
4899 if (TYPE(n
) == parameters
) {
4901 if (TYPE(n
) == RPAR
)
4904 REQ(n
, varargslist
);
4905 for (i
= 0; i
< NCH(n
); i
+= 2) {
4907 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
4911 if (TYPE(c
) == test
) {
4914 if (TYPE(CHILD(c
, 0)) == NAME
)
4915 symtable_add_def(st
, STR(CHILD(c
, 0)), DEF_PARAM
);
4918 sprintf(nbuf
, ".%d", i
);
4919 symtable_add_def(st
, nbuf
, DEF_PARAM
);
4925 if (TYPE(c
) == STAR
) {
4927 symtable_add_def(st
, STR(CHILD(n
, i
)),
4928 DEF_PARAM
| DEF_STAR
);
4935 if (c
&& TYPE(c
) == DOUBLESTAR
) {
4937 symtable_add_def(st
, STR(CHILD(n
, i
)),
4938 DEF_PARAM
| DEF_DOUBLESTAR
);
4943 for (j
= 0; j
<= complex; j
++) {
4945 if (TYPE(c
) == COMMA
)
4947 else if (TYPE(c
) == EQUAL
)
4948 c
= CHILD(n
, j
+= 3);
4949 if (TYPE(CHILD(c
, 0)) == LPAR
)
4950 symtable_params_fplist(st
, CHILD(c
, 1));
4956 symtable_params_fplist(struct symtable
*st
, node
*n
)
4962 for (i
= 0; i
< NCH(n
); i
+= 2) {
4966 symtable_add_def(st
, STR(CHILD(c
, 0)),
4967 DEF_PARAM
| DEF_INTUPLE
);
4969 symtable_params_fplist(st
, CHILD(c
, 1));
4975 symtable_global(struct symtable
*st
, node
*n
)
4979 /* XXX It might be helpful to warn about module-level global
4980 statements, but it's hard to tell the difference between
4981 module-level and a string passed to exec.
4984 for (i
= 1; i
< NCH(n
); i
+= 2) {
4985 char *name
= STR(CHILD(n
, i
));
4988 flags
= symtable_lookup(st
, name
);
4991 if (flags
&& flags
!= DEF_GLOBAL
) {
4993 if (flags
& DEF_PARAM
) {
4994 PyErr_Format(PyExc_SyntaxError
,
4995 "name '%.400s' is local and global",
4997 PyErr_SyntaxLocation(st
->st_filename
,
4998 st
->st_cur
->ste_lineno
);
5003 if (flags
& DEF_LOCAL
)
5004 sprintf(buf
, GLOBAL_AFTER_ASSIGN
,
5007 sprintf(buf
, GLOBAL_AFTER_USE
, name
);
5008 symtable_warn(st
, buf
);
5011 symtable_add_def(st
, name
, DEF_GLOBAL
);
5016 symtable_list_comprehension(struct symtable
*st
, node
*n
)
5020 sprintf(tmpname
, "_[%d]", ++st
->st_tmpname
);
5021 symtable_add_def(st
, tmpname
, DEF_LOCAL
);
5022 symtable_assign(st
, CHILD(n
, 1), 0);
5023 symtable_node(st
, CHILD(n
, 3));
5025 symtable_node(st
, CHILD(n
, 4));
5030 symtable_import(struct symtable
*st
, node
*n
)
5033 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5034 | 'from' dotted_name 'import'
5035 ('*' | import_as_name (',' import_as_name)*)
5036 import_as_name: NAME [NAME NAME]
5038 if (STR(CHILD(n
, 0))[0] == 'f') { /* from */
5039 node
*dotname
= CHILD(n
, 1);
5040 if (strcmp(STR(CHILD(dotname
, 0)), "__future__") == 0) {
5041 /* check for bogus imports */
5042 if (n
->n_lineno
>= st
->st_future
->ff_last_lineno
) {
5043 PyErr_SetString(PyExc_SyntaxError
,
5045 PyErr_SyntaxLocation(st
->st_filename
,
5051 if (TYPE(CHILD(n
, 3)) == STAR
) {
5052 st
->st_cur
->ste_optimized
|= OPT_IMPORT_STAR
;
5054 for (i
= 3; i
< NCH(n
); i
+= 2) {
5055 node
*c
= CHILD(n
, i
);
5056 if (NCH(c
) > 1) /* import as */
5057 symtable_assign(st
, CHILD(c
, 2),
5060 symtable_assign(st
, CHILD(c
, 0),
5065 for (i
= 1; i
< NCH(n
); i
+= 2) {
5066 symtable_assign(st
, CHILD(n
, i
), DEF_IMPORT
);
5072 symtable_assign(struct symtable
*st
, node
*n
, int flag
)
5080 /* invalid assignment, e.g. lambda x:x=2. The next
5081 pass will catch this error. */
5085 for (i
= 2; i
< NCH(n
); ++i
)
5086 if (TYPE(CHILD(n
, i
)) != DOUBLESTAR
)
5087 symtable_node(st
, CHILD(n
, i
));
5090 symtable_node(st
, CHILD(n
, 0));
5091 symtable_node(st
, CHILD(n
, 1));
5098 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
)
5099 symtable_list_comprehension(st
, CHILD(n
, 1));
5101 for (i
= 0; i
< NCH(n
); i
+= 2)
5102 symtable_assign(st
, CHILD(n
, i
), flag
);
5113 for (i
= 0; i
< NCH(n
); i
+= 2)
5114 symtable_assign(st
, CHILD(n
, i
), flag
);
5120 if (TYPE(tmp
) == LPAR
|| TYPE(tmp
) == LSQB
) {
5123 } else if (TYPE(tmp
) == NAME
)
5124 symtable_add_def(st
, STR(tmp
), DEF_LOCAL
| flag
);
5126 case dotted_as_name
:
5128 symtable_add_def(st
, STR(CHILD(n
, 2)),
5131 symtable_add_def(st
,
5137 symtable_add_def(st
, STR(CHILD(n
, 0)), DEF_LOCAL
| flag
);
5140 symtable_add_def(st
, STR(n
), DEF_LOCAL
| flag
);
5149 /* Should only occur for errors like x + 1 = 1,
5150 which will be caught in the next pass. */
5151 for (i
= 0; i
< NCH(n
); ++i
)
5152 if (TYPE(CHILD(n
, i
)) >= single_input
)
5153 symtable_assign(st
, CHILD(n
, i
), flag
);