1 /* Compile an expression node to intermediate code */
4 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
6 XXX Generate simple jump for break/return outside 'try...finally'
7 XXX Allow 'continue' inside finally clause of try-finally
8 XXX New opcode for loading the initial index for a for loop
20 #include "structmember.h"
24 /* Three symbols from graminit.h are also defined in Python.h, with
25 Py_ prefixes to their names. Python.h can't include graminit.h
26 (which defines too many confusing symbols), but we can check here
27 that they haven't changed (which is very unlikely, but possible). */
28 #if Py_single_input != single_input
29 #error "single_input has changed -- update Py_single_input in Python.h"
31 #if Py_file_input != file_input
32 #error "file_input has changed -- update Py_file_input in Python.h"
34 #if Py_eval_input != eval_input
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
38 int Py_OptimizeFlag
= 0;
48 #define DEL_CLOSURE_ERROR \
49 "can not delete variable '%.400s' referenced in nested scope"
51 #define DUPLICATE_ARGUMENT \
52 "duplicate argument '%s' in function definition"
54 #define ILLEGAL_DYNAMIC_SCOPE \
55 "%.100s: exec or 'import *' makes names ambiguous in nested scope"
57 #define GLOBAL_AFTER_ASSIGN \
58 "name '%.400s' is assigned to before global declaration"
60 #define GLOBAL_AFTER_USE \
61 "name '%.400s' is used prior to global declaration"
63 #define LOCAL_GLOBAL \
64 "name '%.400s' is a function parameter and declared global"
67 "from __future__ imports must occur at the beginning of the file"
69 #define ASSIGN_DEBUG \
70 "can not assign to __debug__"
72 #define MANGLE_LEN 256
74 #define OFF(x) offsetof(PyCodeObject, x)
76 static PyMemberDef code_memberlist
[] = {
77 {"co_argcount", T_INT
, OFF(co_argcount
), READONLY
},
78 {"co_nlocals", T_INT
, OFF(co_nlocals
), READONLY
},
79 {"co_stacksize",T_INT
, OFF(co_stacksize
), READONLY
},
80 {"co_flags", T_INT
, OFF(co_flags
), READONLY
},
81 {"co_code", T_OBJECT
, OFF(co_code
), READONLY
},
82 {"co_consts", T_OBJECT
, OFF(co_consts
), READONLY
},
83 {"co_names", T_OBJECT
, OFF(co_names
), READONLY
},
84 {"co_varnames", T_OBJECT
, OFF(co_varnames
), READONLY
},
85 {"co_freevars", T_OBJECT
, OFF(co_freevars
), READONLY
},
86 {"co_cellvars", T_OBJECT
, OFF(co_cellvars
), READONLY
},
87 {"co_filename", T_OBJECT
, OFF(co_filename
), READONLY
},
88 {"co_name", T_OBJECT
, OFF(co_name
), READONLY
},
89 {"co_firstlineno", T_INT
, OFF(co_firstlineno
), READONLY
},
90 {"co_lnotab", T_OBJECT
, OFF(co_lnotab
), READONLY
},
94 PyDoc_STRVAR(code_doc
,
95 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
96 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
98 Create a code object. Not for the faint of heart.");
101 code_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kw
)
111 PyObject
*freevars
= NULL
;
112 PyObject
*cellvars
= NULL
;
118 if (!PyArg_ParseTuple(args
, "iiiiSO!O!O!SSiS|O!O!:code",
119 &argcount
, &nlocals
, &stacksize
, &flags
,
121 &PyTuple_Type
, &consts
,
122 &PyTuple_Type
, &names
,
123 &PyTuple_Type
, &varnames
,
125 &firstlineno
, &lnotab
,
126 &PyTuple_Type
, &freevars
,
127 &PyTuple_Type
, &cellvars
))
130 if (freevars
== NULL
|| cellvars
== NULL
) {
131 PyObject
*empty
= PyTuple_New(0);
134 if (freevars
== NULL
) {
138 if (cellvars
== NULL
) {
145 if (!PyObject_CheckReadBuffer(code
)) {
146 PyErr_SetString(PyExc_TypeError
,
147 "bytecode object must be a single-segment read-only buffer");
151 return (PyObject
*)PyCode_New(argcount
, nlocals
, stacksize
, flags
,
152 code
, consts
, names
, varnames
,
153 freevars
, cellvars
, filename
, name
,
154 firstlineno
, lnotab
);
158 code_dealloc(PyCodeObject
*co
)
160 Py_XDECREF(co
->co_code
);
161 Py_XDECREF(co
->co_consts
);
162 Py_XDECREF(co
->co_names
);
163 Py_XDECREF(co
->co_varnames
);
164 Py_XDECREF(co
->co_freevars
);
165 Py_XDECREF(co
->co_cellvars
);
166 Py_XDECREF(co
->co_filename
);
167 Py_XDECREF(co
->co_name
);
168 Py_XDECREF(co
->co_lnotab
);
173 code_repr(PyCodeObject
*co
)
177 char *filename
= "???";
180 if (co
->co_firstlineno
!= 0)
181 lineno
= co
->co_firstlineno
;
182 if (co
->co_filename
&& PyString_Check(co
->co_filename
))
183 filename
= PyString_AS_STRING(co
->co_filename
);
184 if (co
->co_name
&& PyString_Check(co
->co_name
))
185 name
= PyString_AS_STRING(co
->co_name
);
186 PyOS_snprintf(buf
, sizeof(buf
),
187 "<code object %.100s at %p, file \"%.300s\", line %d>",
188 name
, co
, filename
, lineno
);
189 return PyString_FromString(buf
);
193 code_compare(PyCodeObject
*co
, PyCodeObject
*cp
)
196 cmp
= PyObject_Compare(co
->co_name
, cp
->co_name
);
198 cmp
= co
->co_argcount
- cp
->co_argcount
;
199 if (cmp
) return (cmp
<0)?-1:1;
200 cmp
= co
->co_nlocals
- cp
->co_nlocals
;
201 if (cmp
) return (cmp
<0)?-1:1;
202 cmp
= co
->co_flags
- cp
->co_flags
;
203 if (cmp
) return (cmp
<0)?-1:1;
204 cmp
= PyObject_Compare(co
->co_code
, cp
->co_code
);
206 cmp
= PyObject_Compare(co
->co_consts
, cp
->co_consts
);
208 cmp
= PyObject_Compare(co
->co_names
, cp
->co_names
);
210 cmp
= PyObject_Compare(co
->co_varnames
, cp
->co_varnames
);
212 cmp
= PyObject_Compare(co
->co_freevars
, cp
->co_freevars
);
214 cmp
= PyObject_Compare(co
->co_cellvars
, cp
->co_cellvars
);
219 code_hash(PyCodeObject
*co
)
221 long h
, h0
, h1
, h2
, h3
, h4
, h5
, h6
;
222 h0
= PyObject_Hash(co
->co_name
);
223 if (h0
== -1) return -1;
224 h1
= PyObject_Hash(co
->co_code
);
225 if (h1
== -1) return -1;
226 h2
= PyObject_Hash(co
->co_consts
);
227 if (h2
== -1) return -1;
228 h3
= PyObject_Hash(co
->co_names
);
229 if (h3
== -1) return -1;
230 h4
= PyObject_Hash(co
->co_varnames
);
231 if (h4
== -1) return -1;
232 h5
= PyObject_Hash(co
->co_freevars
);
233 if (h5
== -1) return -1;
234 h6
= PyObject_Hash(co
->co_cellvars
);
235 if (h6
== -1) return -1;
236 h
= h0
^ h1
^ h2
^ h3
^ h4
^ h5
^ h6
^
237 co
->co_argcount
^ co
->co_nlocals
^ co
->co_flags
;
242 /* XXX code objects need to participate in GC? */
244 PyTypeObject PyCode_Type
= {
245 PyObject_HEAD_INIT(&PyType_Type
)
248 sizeof(PyCodeObject
),
250 (destructor
)code_dealloc
, /* tp_dealloc */
254 (cmpfunc
)code_compare
, /* tp_compare */
255 (reprfunc
)code_repr
, /* tp_repr */
256 0, /* tp_as_number */
257 0, /* tp_as_sequence */
258 0, /* tp_as_mapping */
259 (hashfunc
)code_hash
, /* tp_hash */
262 PyObject_GenericGetAttr
, /* tp_getattro */
264 0, /* tp_as_buffer */
265 Py_TPFLAGS_DEFAULT
, /* tp_flags */
266 code_doc
, /* tp_doc */
269 0, /* tp_richcompare */
270 0, /* tp_weaklistoffset */
274 code_memberlist
, /* tp_members */
278 0, /* tp_descr_get */
279 0, /* tp_descr_set */
280 0, /* tp_dictoffset */
283 code_new
, /* tp_new */
287 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
289 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
292 all_name_chars(unsigned char *s
)
294 static char ok_name_char
[256];
295 static unsigned char *name_chars
= (unsigned char *)NAME_CHARS
;
297 if (ok_name_char
[*name_chars
] == 0) {
299 for (p
= name_chars
; *p
; p
++)
300 ok_name_char
[*p
] = 1;
303 if (ok_name_char
[*s
++] == 0)
310 intern_strings(PyObject
*tuple
)
314 for (i
= PyTuple_GET_SIZE(tuple
); --i
>= 0; ) {
315 PyObject
*v
= PyTuple_GET_ITEM(tuple
, i
);
316 if (v
== NULL
|| !PyString_Check(v
)) {
317 Py_FatalError("non-string found in code slot");
318 PyErr_BadInternalCall();
321 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple
, i
));
327 PyCode_New(int argcount
, int nlocals
, int stacksize
, int flags
,
328 PyObject
*code
, PyObject
*consts
, PyObject
*names
,
329 PyObject
*varnames
, PyObject
*freevars
, PyObject
*cellvars
,
330 PyObject
*filename
, PyObject
*name
, int firstlineno
,
335 /* Check argument types */
336 if (argcount
< 0 || nlocals
< 0 ||
338 consts
== NULL
|| !PyTuple_Check(consts
) ||
339 names
== NULL
|| !PyTuple_Check(names
) ||
340 varnames
== NULL
|| !PyTuple_Check(varnames
) ||
341 freevars
== NULL
|| !PyTuple_Check(freevars
) ||
342 cellvars
== NULL
|| !PyTuple_Check(cellvars
) ||
343 name
== NULL
|| !PyString_Check(name
) ||
344 filename
== NULL
|| !PyString_Check(filename
) ||
345 lnotab
== NULL
|| !PyString_Check(lnotab
) ||
346 !PyObject_CheckReadBuffer(code
)) {
347 PyErr_BadInternalCall();
350 intern_strings(names
);
351 intern_strings(varnames
);
352 intern_strings(freevars
);
353 intern_strings(cellvars
);
354 /* Intern selected string constants */
355 for (i
= PyTuple_Size(consts
); --i
>= 0; ) {
356 PyObject
*v
= PyTuple_GetItem(consts
, i
);
357 if (!PyString_Check(v
))
359 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v
)))
361 PyString_InternInPlace(&PyTuple_GET_ITEM(consts
, i
));
363 co
= PyObject_NEW(PyCodeObject
, &PyCode_Type
);
365 co
->co_argcount
= argcount
;
366 co
->co_nlocals
= nlocals
;
367 co
->co_stacksize
= stacksize
;
368 co
->co_flags
= flags
;
372 co
->co_consts
= consts
;
374 co
->co_names
= names
;
376 co
->co_varnames
= varnames
;
378 co
->co_freevars
= freevars
;
380 co
->co_cellvars
= cellvars
;
382 co
->co_filename
= filename
;
385 co
->co_firstlineno
= firstlineno
;
387 co
->co_lnotab
= lnotab
;
393 /* Data structure used internally */
395 /* The compiler uses two passes to generate bytecodes. The first pass
396 builds the symbol table. The second pass generates the bytecode.
398 The first pass uses a single symtable struct. The second pass uses
399 a compiling struct for each code block. The compiling structs
400 share a reference to the symtable.
402 The two passes communicate via symtable_load_symbols() and via
403 is_local() and is_global(). The former initializes several slots
404 in the compiling struct: c_varnames, c_locals, c_nlocals,
405 c_argcount, c_globals, and c_flags.
408 /* All about c_lnotab.
410 c_lnotab is an array of unsigned bytes disguised as a Python string. Since
411 version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are
412 mapped to source code line #s via c_lnotab instead.
414 The array is conceptually a list of
415 (bytecode offset increment, line number increment)
416 pairs. The details are important and delicate, best illustrated by example:
418 byte code offset source code line number
425 The first trick is that these numbers aren't stored, only the increments
426 from one row to the next (this doesn't really work, but it's a start):
428 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
430 The second trick is that an unsigned byte can't hold negative values, or
431 values larger than 255, so (a) there's a deep assumption that byte code
432 offsets and their corresponding line #s both increase monotonically, and (b)
433 if at least one column jumps by more than 255 from one row to the next, more
434 than one pair is written to the table. In case #b, there's no way to know
435 from looking at the table later how many were written. That's the delicate
436 part. A user of c_lnotab desiring to find the source line number
437 corresponding to a bytecode address A should do something like this
440 for addr_incr, line_incr in c_lnotab:
446 In order for this to work, when the addr field increments by more than 255,
447 the line # increment in each pair generated must be 0 until the remaining addr
448 increment is < 256. So, in the example above, com_set_lineno should not (as
449 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
450 255, 0, 45, 255, 0, 45.
454 PyObject
*c_code
; /* string */
455 PyObject
*c_consts
; /* list of objects */
456 PyObject
*c_const_dict
; /* inverse of c_consts */
457 PyObject
*c_names
; /* list of strings (names) */
458 PyObject
*c_name_dict
; /* inverse of c_names */
459 PyObject
*c_globals
; /* dictionary (value=None) */
460 PyObject
*c_locals
; /* dictionary (value=localID) */
461 PyObject
*c_varnames
; /* list (inverse of c_locals) */
462 PyObject
*c_freevars
; /* dictionary (value=None) */
463 PyObject
*c_cellvars
; /* list */
464 int c_nlocals
; /* index of next local */
465 int c_argcount
; /* number of top-level arguments */
466 int c_flags
; /* same as co_flags */
467 int c_nexti
; /* index into c_code */
468 int c_errors
; /* counts errors occurred */
469 int c_infunction
; /* set when compiling a function */
470 int c_interactive
; /* generating code for interactive command */
471 int c_loops
; /* counts nested loops */
472 int c_begin
; /* begin of current loop, for 'continue' */
473 int c_block
[CO_MAXBLOCKS
]; /* stack of block types */
474 int c_nblocks
; /* current block stack level */
475 char *c_filename
; /* filename of current node */
476 char *c_name
; /* name of object (e.g. function) */
477 int c_lineno
; /* Current line number */
478 int c_stacklevel
; /* Current stack level */
479 int c_maxstacklevel
; /* Maximum stack level */
481 PyObject
*c_lnotab
; /* Table mapping address to line number */
482 int c_last_addr
, c_last_line
, c_lnotab_next
;
483 char *c_private
; /* for private name mangling */
484 int c_tmpname
; /* temporary local name counter */
485 int c_nested
; /* Is block nested funcdef or lamdef? */
486 int c_closure
; /* Is nested w/freevars? */
487 struct symtable
*c_symtable
; /* pointer to module symbol table */
488 PyFutureFeatures
*c_future
; /* pointer to module's __future__ */
489 char *c_encoding
; /* source encoding (a borrowed reference) */
495 if ((v
& (USE
| DEF_FREE
))
496 && !(v
& (DEF_LOCAL
| DEF_PARAM
| DEF_GLOBAL
)))
498 if (v
& DEF_FREE_CLASS
)
504 com_error(struct compiling
*c
, PyObject
*exc
, char *msg
)
506 PyObject
*t
= NULL
, *v
= NULL
, *w
= NULL
, *line
= NULL
;
509 /* Error occurred via symtable call to
511 PyErr_SetString(exc
, msg
);
515 if (c
->c_lineno
< 1 || c
->c_interactive
) {
516 /* Unknown line number or interactive input */
517 PyErr_SetString(exc
, msg
);
520 v
= PyString_FromString(msg
);
522 return; /* MemoryError, too bad */
524 line
= PyErr_ProgramText(c
->c_filename
, c
->c_lineno
);
529 if (exc
== PyExc_SyntaxError
) {
530 t
= Py_BuildValue("(ziOO)", c
->c_filename
, c
->c_lineno
,
534 w
= Py_BuildValue("(OO)", v
, t
);
537 PyErr_SetObject(exc
, w
);
539 /* Make sure additional exceptions are printed with
540 file and line, also. */
541 PyErr_SetObject(exc
, v
);
542 PyErr_SyntaxLocation(c
->c_filename
, c
->c_lineno
);
551 /* Interface to the block stack */
554 block_push(struct compiling
*c
, int type
)
556 if (c
->c_nblocks
>= CO_MAXBLOCKS
) {
557 com_error(c
, PyExc_SystemError
,
558 "too many statically nested blocks");
561 c
->c_block
[c
->c_nblocks
++] = type
;
566 block_pop(struct compiling
*c
, int type
)
568 if (c
->c_nblocks
> 0)
570 if (c
->c_block
[c
->c_nblocks
] != type
&& c
->c_errors
== 0) {
571 com_error(c
, PyExc_SystemError
, "bad block pop");
575 /* Prototype forward declarations */
577 static int issue_warning(char *, char *, int);
578 static int com_init(struct compiling
*, char *);
579 static void com_free(struct compiling
*);
580 static void com_push(struct compiling
*, int);
581 static void com_pop(struct compiling
*, int);
582 static void com_done(struct compiling
*);
583 static void com_node(struct compiling
*, node
*);
584 static void com_factor(struct compiling
*, node
*);
585 static void com_addbyte(struct compiling
*, int);
586 static void com_addint(struct compiling
*, int);
587 static void com_addoparg(struct compiling
*, int, int);
588 static void com_addfwref(struct compiling
*, int, int *);
589 static void com_backpatch(struct compiling
*, int);
590 static int com_add(struct compiling
*, PyObject
*, PyObject
*, PyObject
*);
591 static int com_addconst(struct compiling
*, PyObject
*);
592 static int com_addname(struct compiling
*, PyObject
*);
593 static void com_addopname(struct compiling
*, int, node
*);
594 static void com_list(struct compiling
*, node
*, int);
595 static void com_list_iter(struct compiling
*, node
*, node
*, char *);
596 static int com_argdefs(struct compiling
*, node
*);
597 static void com_assign(struct compiling
*, node
*, int, node
*);
598 static void com_assign_name(struct compiling
*, node
*, int);
599 static PyCodeObject
*icompile(node
*, struct compiling
*);
600 static PyCodeObject
*jcompile(node
*, char *, struct compiling
*,
602 static PyObject
*parsestrplus(struct compiling
*, node
*);
603 static PyObject
*parsestr(struct compiling
*, char *);
604 static node
*get_rawdocstring(node
*);
606 static int get_ref_type(struct compiling
*, char *);
608 /* symtable operations */
609 static int symtable_build(struct compiling
*, node
*);
610 static int symtable_load_symbols(struct compiling
*);
611 static struct symtable
*symtable_init(void);
612 static void symtable_enter_scope(struct symtable
*, char *, int, int);
613 static int symtable_exit_scope(struct symtable
*);
614 static int symtable_add_def(struct symtable
*, char *, int);
615 static int symtable_add_def_o(struct symtable
*, PyObject
*, PyObject
*, int);
617 static void symtable_node(struct symtable
*, node
*);
618 static void symtable_funcdef(struct symtable
*, node
*);
619 static void symtable_default_args(struct symtable
*, node
*);
620 static void symtable_params(struct symtable
*, node
*);
621 static void symtable_params_fplist(struct symtable
*, node
*n
);
622 static void symtable_global(struct symtable
*, node
*);
623 static void symtable_import(struct symtable
*, node
*);
624 static void symtable_assign(struct symtable
*, node
*, int);
625 static void symtable_list_comprehension(struct symtable
*, node
*);
627 static int symtable_update_free_vars(struct symtable
*);
628 static int symtable_undo_free(struct symtable
*, PyObject
*, PyObject
*);
629 static int symtable_check_global(struct symtable
*, PyObject
*, PyObject
*);
636 for (i
= 0; i
< pad
; ++i
)
637 fprintf(stderr
, " ");
641 dump(node
*n
, int pad
, int depth
)
647 fprintf(stderr
, "%d: %s\n", TYPE(n
), STR(n
));
650 for (i
= 0; i
< NCH(n
); ++i
)
651 dump(CHILD(n
, i
), pad
+ 1, depth
);
654 #define DUMP(N) dump(N, 0, -1)
657 com_init(struct compiling
*c
, char *filename
)
659 memset((void *)c
, '\0', sizeof(struct compiling
));
660 if ((c
->c_code
= PyString_FromStringAndSize((char *)NULL
,
663 if ((c
->c_consts
= PyList_New(0)) == NULL
)
665 if ((c
->c_const_dict
= PyDict_New()) == NULL
)
667 if ((c
->c_names
= PyList_New(0)) == NULL
)
669 if ((c
->c_name_dict
= PyDict_New()) == NULL
)
671 if ((c
->c_locals
= PyDict_New()) == NULL
)
673 if ((c
->c_lnotab
= PyString_FromStringAndSize((char *)NULL
,
677 c
->c_varnames
= NULL
;
678 c
->c_freevars
= NULL
;
679 c
->c_cellvars
= NULL
;
686 c
->c_interactive
= 0;
690 c
->c_filename
= filename
;
694 c
->c_maxstacklevel
= 0;
695 c
->c_firstlineno
= 0;
698 c
->c_lnotab_next
= 0;
702 c
->c_symtable
= NULL
;
711 com_free(struct compiling
*c
)
713 Py_XDECREF(c
->c_code
);
714 Py_XDECREF(c
->c_consts
);
715 Py_XDECREF(c
->c_const_dict
);
716 Py_XDECREF(c
->c_names
);
717 Py_XDECREF(c
->c_name_dict
);
718 Py_XDECREF(c
->c_globals
);
719 Py_XDECREF(c
->c_locals
);
720 Py_XDECREF(c
->c_varnames
);
721 Py_XDECREF(c
->c_freevars
);
722 Py_XDECREF(c
->c_cellvars
);
723 Py_XDECREF(c
->c_lnotab
);
725 PyObject_FREE((void *)c
->c_future
);
729 com_push(struct compiling
*c
, int n
)
731 c
->c_stacklevel
+= n
;
732 if (c
->c_stacklevel
> c
->c_maxstacklevel
) {
733 c
->c_maxstacklevel
= c
->c_stacklevel
;
735 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
736 c->c_filename, c->c_name, c->c_lineno,
737 c->c_nexti, c->c_stacklevel, n);
743 com_pop(struct compiling
*c
, int n
)
745 if (c
->c_stacklevel
< n
)
748 c
->c_stacklevel
-= n
;
752 com_done(struct compiling
*c
)
754 if (c
->c_code
!= NULL
)
755 _PyString_Resize(&c
->c_code
, c
->c_nexti
);
756 if (c
->c_lnotab
!= NULL
)
757 _PyString_Resize(&c
->c_lnotab
, c
->c_lnotab_next
);
761 com_check_size(PyObject
**s
, int offset
)
763 int len
= PyString_GET_SIZE(*s
);
765 return _PyString_Resize(s
, len
* 2);
770 com_addbyte(struct compiling
*c
, int byte
)
772 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
773 assert(byte
>= 0 && byte
<= 255);
774 assert(c
->c_code
!= 0);
775 if (com_check_size(&c
->c_code
, c
->c_nexti
)) {
779 PyString_AS_STRING(c
->c_code
)[c
->c_nexti
++] = byte
;
783 com_addint(struct compiling
*c
, int x
)
785 com_addbyte(c
, x
& 0xff);
786 com_addbyte(c
, x
>> 8); /* XXX x should be positive */
790 com_add_lnotab(struct compiling
*c
, int addr
, int line
)
793 if (c
->c_lnotab
== NULL
)
795 if (com_check_size(&c
->c_lnotab
, c
->c_lnotab_next
+ 2)) {
799 p
= PyString_AS_STRING(c
->c_lnotab
) + c
->c_lnotab_next
;
802 c
->c_lnotab_next
+= 2;
806 com_set_lineno(struct compiling
*c
, int lineno
)
808 c
->c_lineno
= lineno
;
809 if (c
->c_firstlineno
== 0) {
810 c
->c_firstlineno
= c
->c_last_line
= lineno
;
813 int incr_addr
= c
->c_nexti
- c
->c_last_addr
;
814 int incr_line
= lineno
- c
->c_last_line
;
815 while (incr_addr
> 255) {
816 com_add_lnotab(c
, 255, 0);
819 while (incr_line
> 255) {
820 com_add_lnotab(c
, incr_addr
, 255);
824 if (incr_addr
> 0 || incr_line
> 0)
825 com_add_lnotab(c
, incr_addr
, incr_line
);
826 c
->c_last_addr
= c
->c_nexti
;
827 c
->c_last_line
= lineno
;
832 com_addoparg(struct compiling
*c
, int op
, int arg
)
834 int extended_arg
= arg
>> 16;
836 com_addbyte(c
, EXTENDED_ARG
);
837 com_addint(c
, extended_arg
);
845 com_addfwref(struct compiling
*c
, int op
, int *p_anchor
)
847 /* Compile a forward reference for backpatching */
854 com_addint(c
, anchor
== 0 ? 0 : here
- anchor
);
858 com_backpatch(struct compiling
*c
, int anchor
)
860 unsigned char *code
= (unsigned char *) PyString_AS_STRING(c
->c_code
);
861 int target
= c
->c_nexti
;
865 /* Make the JUMP instruction at anchor point to target */
866 prev
= code
[anchor
] + (code
[anchor
+1] << 8);
867 dist
= target
- (anchor
+2);
868 code
[anchor
] = dist
& 0xff;
870 code
[anchor
+1] = dist
;
873 com_error(c
, PyExc_SystemError
,
874 "com_backpatch: offset too large");
883 /* Handle literals and names uniformly */
886 com_add(struct compiling
*c
, PyObject
*list
, PyObject
*dict
, PyObject
*v
)
888 PyObject
*w
, *t
, *np
=NULL
;
891 t
= Py_BuildValue("(OO)", v
, v
->ob_type
);
894 w
= PyDict_GetItem(dict
, t
);
898 n
= PyList_Size(list
);
899 np
= PyInt_FromLong(n
);
902 if (PyList_Append(list
, v
) != 0)
904 if (PyDict_SetItem(dict
, t
, np
) != 0)
918 com_addconst(struct compiling
*c
, PyObject
*v
)
920 return com_add(c
, c
->c_consts
, c
->c_const_dict
, v
);
924 com_addname(struct compiling
*c
, PyObject
*v
)
926 return com_add(c
, c
->c_names
, c
->c_name_dict
, v
);
930 _Py_Mangle(char *p
, char *name
, char *buffer
, size_t maxlen
)
932 /* Name mangling: __private becomes _classname__private.
933 This is independent from how the name is used. */
935 if (p
== NULL
|| name
== NULL
|| name
[0] != '_' || name
[1] != '_')
938 if (nlen
+2 >= maxlen
)
939 return 0; /* Don't mangle __extremely_long_names */
940 if (name
[nlen
-1] == '_' && name
[nlen
-2] == '_')
941 return 0; /* Don't mangle __whatever__ */
942 /* Strip leading underscores from class name */
946 return 0; /* Don't mangle if class is just underscores */
948 if (plen
+ nlen
>= maxlen
)
949 plen
= maxlen
-nlen
-2; /* Truncate class name if too long */
950 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
952 strncpy(buffer
+1, p
, plen
);
953 strcpy(buffer
+1+plen
, name
);
958 com_addop_name(struct compiling
*c
, int op
, char *name
)
962 char buffer
[MANGLE_LEN
];
964 if (_Py_Mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
966 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
971 i
= com_addname(c
, v
);
974 com_addoparg(c
, op
, i
);
978 #define NAME_GLOBAL 1
979 #define NAME_DEFAULT 2
980 #define NAME_CLOSURE 3
983 com_lookup_arg(PyObject
*dict
, PyObject
*name
)
985 PyObject
*v
= PyDict_GetItem(dict
, name
);
989 return PyInt_AS_LONG(v
);
993 none_assignment_check(struct compiling
*c
, char *name
, int assigning
)
995 if (name
[0] == 'N' && strcmp(name
, "None") == 0) {
998 msg
= "assigment to None";
1000 msg
= "deleting None";
1001 if (issue_warning(msg
, c
->c_filename
, c
->c_lineno
) < 0) {
1010 com_addop_varname(struct compiling
*c
, int kind
, char *name
)
1014 int scope
= NAME_DEFAULT
;
1016 char buffer
[MANGLE_LEN
];
1018 if (kind
!= VAR_LOAD
&&
1019 none_assignment_check(c
, name
, kind
== VAR_STORE
))
1025 if (_Py_Mangle(c
->c_private
, name
, buffer
, sizeof(buffer
)))
1027 if (name
== NULL
|| (v
= PyString_InternFromString(name
)) == NULL
) {
1033 reftype
= get_ref_type(c
, name
);
1036 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
)
1039 case GLOBAL_EXPLICIT
:
1040 scope
= NAME_GLOBAL
;
1042 case GLOBAL_IMPLICIT
:
1043 if (c
->c_flags
& CO_OPTIMIZED
)
1044 scope
= NAME_GLOBAL
;
1048 scope
= NAME_CLOSURE
;
1052 i
= com_addname(c
, v
);
1053 if (scope
== NAME_LOCAL
)
1054 i
= com_lookup_arg(c
->c_locals
, v
);
1055 else if (reftype
== FREE
)
1056 i
= com_lookup_arg(c
->c_freevars
, v
);
1057 else if (reftype
== CELL
)
1058 i
= com_lookup_arg(c
->c_cellvars
, v
);
1060 c
->c_errors
++; /* XXX no exception set */
1110 case NAME_CLOSURE
: {
1112 PyOS_snprintf(buf
, sizeof(buf
),
1113 DEL_CLOSURE_ERROR
, name
);
1114 com_error(c
, PyExc_SyntaxError
, buf
);
1122 com_addoparg(c
, op
, i
);
1126 com_addopname(struct compiling
*c
, int op
, node
*n
)
1130 /* XXX it is possible to write this code without the 1000
1131 chars on the total length of dotted names, I just can't be
1132 bothered right now */
1133 if (TYPE(n
) == STAR
)
1135 else if (TYPE(n
) == dotted_name
) {
1139 for (i
= 0; i
< NCH(n
); i
+= 2) {
1140 char *s
= STR(CHILD(n
, i
));
1141 if (p
+ strlen(s
) > buffer
+ (sizeof buffer
) - 2) {
1142 com_error(c
, PyExc_MemoryError
,
1143 "dotted_name too long");
1150 p
= strchr(p
, '\0');
1157 com_addop_name(c
, op
, name
);
1161 parsenumber(struct compiling
*c
, char *s
)
1166 #ifndef WITHOUT_COMPLEX
1171 end
= s
+ strlen(s
) - 1;
1172 #ifndef WITHOUT_COMPLEX
1173 imflag
= *end
== 'j' || *end
== 'J';
1175 if (*end
== 'l' || *end
== 'L')
1176 return PyLong_FromString(s
, (char **)0, 0);
1178 x
= (long) PyOS_strtoul(s
, &end
, 0);
1179 if (x
< 0 && errno
== 0) {
1180 if (PyErr_WarnExplicit(
1181 PyExc_FutureWarning
,
1182 "hex/oct constants > sys.maxint "
1183 "will return positive values "
1184 "in Python 2.4 and up",
1190 errno
= 0; /* Might be changed by PyErr_Warn() */
1194 x
= PyOS_strtol(s
, &end
, 0);
1197 return PyLong_FromString(s
, (char **)0, 0);
1198 return PyInt_FromLong(x
);
1200 /* XXX Huge floats may silently fail */
1201 #ifndef WITHOUT_COMPLEX
1205 PyFPE_START_PROTECT("atof", return 0)
1207 PyFPE_END_PROTECT(z
)
1208 return PyComplex_FromCComplex(z
);
1213 PyFPE_START_PROTECT("atof", return 0)
1215 PyFPE_END_PROTECT(dx
)
1216 return PyFloat_FromDouble(dx
);
1221 decode_utf8(char **sPtr
, char *end
, char* encoding
)
1223 #ifndef Py_USING_UNICODE
1224 Py_FatalError("decode_utf8 should not be called in this build.");
1230 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
1231 while (s
< end
&& (*s
& 0x80)) s
++;
1233 u
= PyUnicode_DecodeUTF8(t
, s
- t
, NULL
);
1236 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
1243 parsestr(struct compiling
*c
, char *s
)
1252 char* encoding
= ((c
== NULL
) ? NULL
: c
->c_encoding
);
1256 if (isalpha(quote
) || quote
== '_') {
1257 if (quote
== 'u' || quote
== 'U') {
1261 if (quote
== 'r' || quote
== 'R') {
1266 if (quote
!= '\'' && quote
!= '\"') {
1267 PyErr_BadInternalCall();
1272 if (len
> INT_MAX
) {
1273 com_error(c
, PyExc_OverflowError
,
1274 "string to parse is too long");
1277 if (s
[--len
] != quote
) {
1278 PyErr_BadInternalCall();
1281 if (len
>= 4 && s
[0] == quote
&& s
[1] == quote
) {
1284 if (s
[--len
] != quote
|| s
[--len
] != quote
) {
1285 PyErr_BadInternalCall();
1289 #ifdef Py_USING_UNICODE
1290 if (unicode
|| Py_UnicodeFlag
) {
1292 if (encoding
== NULL
) {
1295 } else if (strcmp(encoding
, "iso-8859-1") == 0) {
1299 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
1300 u
= PyString_FromStringAndSize((char *)NULL
, len
* 4);
1303 p
= buf
= PyString_AsString(u
);
1313 if (*s
& 0x80) { /* XXX inefficient */
1316 w
= decode_utf8(&s
, end
, "utf-16-be");
1321 r
= PyString_AsString(w
);
1322 rn
= PyString_Size(w
);
1323 assert(rn
% 2 == 0);
1324 for (i
= 0; i
< rn
; i
+= 2) {
1325 sprintf(p
, "\\u%02x%02x",
1338 v
= PyUnicode_DecodeRawUnicodeEscape(buf
, len
, NULL
);
1340 v
= PyUnicode_DecodeUnicodeEscape(buf
, len
, NULL
);
1343 PyErr_SyntaxLocation(c
->c_filename
, c
->c_lineno
);
1348 need_encoding
= (encoding
!= NULL
&&
1349 strcmp(encoding
, "utf-8") != 0 &&
1350 strcmp(encoding
, "iso-8859-1") != 0);
1351 if (rawmode
|| strchr(s
, '\\') == NULL
) {
1352 if (need_encoding
) {
1353 #ifndef Py_USING_UNICODE
1354 /* This should not happen - we never see any other
1356 Py_FatalError("cannot deal with encodings in this build.");
1358 PyObject
* u
= PyUnicode_DecodeUTF8(s
, len
, NULL
);
1361 v
= PyUnicode_AsEncodedString(u
, encoding
, NULL
);
1366 return PyString_FromStringAndSize(s
, len
);
1370 v
= PyString_DecodeEscape(s
, len
, NULL
, unicode
,
1371 need_encoding
? encoding
: NULL
);
1373 PyErr_SyntaxLocation(c
->c_filename
, c
->c_lineno
);
1378 parsestrplus(struct compiling
* c
, node
*n
)
1382 REQ(CHILD(n
, 0), STRING
);
1383 if ((v
= parsestr(c
, STR(CHILD(n
, 0)))) != NULL
) {
1384 /* String literal concatenation */
1385 for (i
= 1; i
< NCH(n
); i
++) {
1387 s
= parsestr(c
, STR(CHILD(n
, i
)));
1390 if (PyString_Check(v
) && PyString_Check(s
)) {
1391 PyString_ConcatAndDel(&v
, s
);
1395 #ifdef Py_USING_UNICODE
1398 temp
= PyUnicode_Concat(v
, s
);
1416 com_list_for(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1419 int save_begin
= c
->c_begin
;
1421 /* list_iter: for v in expr [list_iter] */
1422 com_node(c
, CHILD(n
, 3)); /* expr */
1423 com_addbyte(c
, GET_ITER
);
1424 c
->c_begin
= c
->c_nexti
;
1425 com_addfwref(c
, FOR_ITER
, &anchor
);
1427 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
1429 com_list_iter(c
, n
, e
, t
);
1431 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
1432 c
->c_begin
= save_begin
;
1433 com_backpatch(c
, anchor
);
1434 com_pop(c
, 1); /* FOR_ITER has popped this */
1438 com_list_if(struct compiling
*c
, node
*n
, node
*e
, char *t
)
1442 /* list_iter: 'if' test [list_iter] */
1443 com_node(c
, CHILD(n
, 1));
1444 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
1445 com_addbyte(c
, POP_TOP
);
1447 com_list_iter(c
, n
, e
, t
);
1448 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
1449 com_backpatch(c
, a
);
1450 /* We jump here with an extra entry which we now pop */
1451 com_addbyte(c
, POP_TOP
);
1452 com_backpatch(c
, anchor
);
1456 com_list_iter(struct compiling
*c
,
1457 node
*p
, /* parent of list_iter node */
1458 node
*e
, /* element expression node */
1459 char *t
/* name of result list temp local */)
1461 /* list_iter is the last child in a listmaker, list_for, or list_if */
1462 node
*n
= CHILD(p
, NCH(p
)-1);
1463 if (TYPE(n
) == list_iter
) {
1467 com_list_for(c
, n
, e
, t
);
1470 com_list_if(c
, n
, e
, t
);
1473 com_error(c
, PyExc_SystemError
,
1474 "invalid list_iter node type");
1478 com_addop_varname(c
, VAR_LOAD
, t
);
1481 com_addoparg(c
, CALL_FUNCTION
, 1);
1482 com_addbyte(c
, POP_TOP
);
1488 com_list_comprehension(struct compiling
*c
, node
*n
)
1490 /* listmaker: test list_for */
1492 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", ++c
->c_tmpname
);
1493 com_addoparg(c
, BUILD_LIST
, 0);
1494 com_addbyte(c
, DUP_TOP
); /* leave the result on the stack */
1496 com_addop_name(c
, LOAD_ATTR
, "append");
1497 com_addop_varname(c
, VAR_STORE
, tmpname
);
1499 com_list_for(c
, CHILD(n
, 1), CHILD(n
, 0), tmpname
);
1500 com_addop_varname(c
, VAR_DELETE
, tmpname
);
1505 com_listmaker(struct compiling
*c
, node
*n
)
1507 /* listmaker: test ( list_for | (',' test)* [','] ) */
1508 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
)
1509 com_list_comprehension(c
, n
);
1513 for (i
= 0; i
< NCH(n
); i
+= 2, len
++)
1514 com_node(c
, CHILD(n
, i
));
1515 com_addoparg(c
, BUILD_LIST
, len
);
1521 com_dictmaker(struct compiling
*c
, node
*n
)
1524 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1525 for (i
= 0; i
+2 < NCH(n
); i
+= 4) {
1526 /* We must arrange things just right for STORE_SUBSCR.
1527 It wants the stack to look like (value) (dict) (key) */
1528 com_addbyte(c
, DUP_TOP
);
1530 com_node(c
, CHILD(n
, i
+2)); /* value */
1531 com_addbyte(c
, ROT_TWO
);
1532 com_node(c
, CHILD(n
, i
)); /* key */
1533 com_addbyte(c
, STORE_SUBSCR
);
1539 com_atom(struct compiling
*c
, node
*n
)
1548 if (TYPE(CHILD(n
, 1)) == RPAR
) {
1549 com_addoparg(c
, BUILD_TUPLE
, 0);
1553 com_node(c
, CHILD(n
, 1));
1555 case LSQB
: /* '[' [listmaker] ']' */
1556 if (TYPE(CHILD(n
, 1)) == RSQB
) {
1557 com_addoparg(c
, BUILD_LIST
, 0);
1561 com_listmaker(c
, CHILD(n
, 1));
1563 case LBRACE
: /* '{' [dictmaker] '}' */
1564 com_addoparg(c
, BUILD_MAP
, 0);
1566 if (TYPE(CHILD(n
, 1)) == dictmaker
)
1567 com_dictmaker(c
, CHILD(n
, 1));
1570 com_node(c
, CHILD(n
, 1));
1571 com_addbyte(c
, UNARY_CONVERT
);
1574 if ((v
= parsenumber(c
, STR(ch
))) == NULL
) {
1578 i
= com_addconst(c
, v
);
1581 com_addoparg(c
, LOAD_CONST
, i
);
1585 v
= parsestrplus(c
, n
);
1591 i
= com_addconst(c
, v
);
1594 com_addoparg(c
, LOAD_CONST
, i
);
1598 com_addop_varname(c
, VAR_LOAD
, STR(ch
));
1602 com_error(c
, PyExc_SystemError
,
1603 "com_atom: unexpected node type");
1608 com_slice(struct compiling
*c
, node
*n
, int op
)
1613 else if (NCH(n
) == 2) {
1614 if (TYPE(CHILD(n
, 0)) != COLON
) {
1615 com_node(c
, CHILD(n
, 0));
1616 com_addbyte(c
, op
+1);
1619 com_node(c
, CHILD(n
, 1));
1620 com_addbyte(c
, op
+2);
1625 com_node(c
, CHILD(n
, 0));
1626 com_node(c
, CHILD(n
, 2));
1627 com_addbyte(c
, op
+3);
1633 com_augassign_slice(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
1636 com_addbyte(c
, DUP_TOP
);
1638 com_addbyte(c
, SLICE
);
1640 com_addbyte(c
, opcode
);
1642 com_addbyte(c
, ROT_TWO
);
1643 com_addbyte(c
, STORE_SLICE
);
1645 } else if (NCH(n
) == 2 && TYPE(CHILD(n
, 0)) != COLON
) {
1646 com_node(c
, CHILD(n
, 0));
1647 com_addoparg(c
, DUP_TOPX
, 2);
1649 com_addbyte(c
, SLICE
+1);
1652 com_addbyte(c
, opcode
);
1654 com_addbyte(c
, ROT_THREE
);
1655 com_addbyte(c
, STORE_SLICE
+1);
1657 } else if (NCH(n
) == 2) {
1658 com_node(c
, CHILD(n
, 1));
1659 com_addoparg(c
, DUP_TOPX
, 2);
1661 com_addbyte(c
, SLICE
+2);
1664 com_addbyte(c
, opcode
);
1666 com_addbyte(c
, ROT_THREE
);
1667 com_addbyte(c
, STORE_SLICE
+2);
1670 com_node(c
, CHILD(n
, 0));
1671 com_node(c
, CHILD(n
, 2));
1672 com_addoparg(c
, DUP_TOPX
, 3);
1674 com_addbyte(c
, SLICE
+3);
1677 com_addbyte(c
, opcode
);
1679 com_addbyte(c
, ROT_FOUR
);
1680 com_addbyte(c
, STORE_SLICE
+3);
1686 com_argument(struct compiling
*c
, node
*n
, PyObject
**pkeywords
)
1689 REQ(n
, argument
); /* [test '='] test; really [keyword '='] test */
1691 if (*pkeywords
!= NULL
) {
1692 com_error(c
, PyExc_SyntaxError
,
1693 "non-keyword arg after keyword arg");
1696 com_node(c
, CHILD(n
, 0));
1703 } while (NCH(m
) == 1);
1704 if (TYPE(m
) != NAME
) {
1705 /* f(lambda x: x[0] = 3) ends up getting parsed with
1706 * LHS test = lambda x: x[0], and RHS test = 3.
1707 * SF bug 132313 points out that complaining about a keyword
1708 * then is very confusing.
1710 com_error(c
, PyExc_SyntaxError
,
1711 TYPE(m
) == lambdef
?
1712 "lambda cannot contain assignment" :
1713 "keyword can't be an expression");
1716 PyObject
*v
= PyString_InternFromString(STR(m
));
1717 (void) none_assignment_check(c
, STR(m
), 1);
1718 if (v
!= NULL
&& *pkeywords
== NULL
)
1719 *pkeywords
= PyDict_New();
1722 else if (*pkeywords
== NULL
) {
1726 if (PyDict_GetItem(*pkeywords
, v
) != NULL
)
1727 com_error(c
, PyExc_SyntaxError
,
1728 "duplicate keyword argument");
1730 if (PyDict_SetItem(*pkeywords
, v
, v
) != 0)
1732 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, v
));
1737 com_node(c
, CHILD(n
, 2));
1741 com_call_function(struct compiling
*c
, node
*n
)
1743 if (TYPE(n
) == RPAR
) {
1744 com_addoparg(c
, CALL_FUNCTION
, 0);
1747 PyObject
*keywords
= NULL
;
1749 int lineno
= n
->n_lineno
;
1751 int starstar_flag
= 0;
1756 for (i
= 0; i
< NCH(n
); i
+= 2) {
1757 node
*ch
= CHILD(n
, i
);
1758 if (TYPE(ch
) == STAR
||
1759 TYPE(ch
) == DOUBLESTAR
)
1761 if (ch
->n_lineno
!= lineno
) {
1762 lineno
= ch
->n_lineno
;
1763 com_set_lineno(c
, lineno
);
1765 com_argument(c
, ch
, &keywords
);
1766 if (keywords
== NULL
)
1771 Py_XDECREF(keywords
);
1772 while (i
< NCH(n
)) {
1773 node
*tok
= CHILD(n
, i
);
1774 node
*ch
= CHILD(n
, i
+1);
1776 switch (TYPE(tok
)) {
1777 case STAR
: star_flag
= 1; break;
1778 case DOUBLESTAR
: starstar_flag
= 1; break;
1782 if (na
> 255 || nk
> 255) {
1783 com_error(c
, PyExc_SyntaxError
,
1784 "more than 255 arguments");
1786 if (star_flag
|| starstar_flag
)
1787 opcode
= CALL_FUNCTION_VAR
- 1 +
1788 star_flag
+ (starstar_flag
<< 1);
1790 opcode
= CALL_FUNCTION
;
1791 com_addoparg(c
, opcode
, na
| (nk
<< 8));
1792 com_pop(c
, na
+ 2*nk
+ star_flag
+ starstar_flag
);
1797 com_select_member(struct compiling
*c
, node
*n
)
1799 com_addopname(c
, LOAD_ATTR
, n
);
1803 com_sliceobj(struct compiling
*c
, node
*n
)
1806 int ns
=2; /* number of slice arguments */
1809 /* first argument */
1810 if (TYPE(CHILD(n
,i
)) == COLON
) {
1811 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1816 com_node(c
, CHILD(n
,i
));
1818 REQ(CHILD(n
,i
),COLON
);
1821 /* second argument */
1822 if (i
< NCH(n
) && TYPE(CHILD(n
,i
)) == test
) {
1823 com_node(c
, CHILD(n
,i
));
1827 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1830 /* remaining arguments */
1831 for (; i
< NCH(n
); i
++) {
1836 /* right argument of ':' missing */
1837 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
1841 com_node(c
, CHILD(ch
,1));
1843 com_addoparg(c
, BUILD_SLICE
, ns
);
1844 com_pop(c
, 1 + (ns
== 3));
1848 com_subscript(struct compiling
*c
, node
*n
)
1853 /* check for rubber index */
1854 if (TYPE(ch
) == DOT
&& TYPE(CHILD(n
,1)) == DOT
) {
1855 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_Ellipsis
));
1859 /* check for slice */
1860 if ((TYPE(ch
) == COLON
|| NCH(n
) > 1))
1870 com_subscriptlist(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
1873 REQ(n
, subscriptlist
);
1874 /* Check to make backward compatible slice behavior for '[i:j]' */
1876 node
*sub
= CHILD(n
, 0); /* subscript */
1877 /* 'Basic' slice, should have exactly one colon. */
1878 if ((TYPE(CHILD(sub
, 0)) == COLON
1879 || (NCH(sub
) > 1 && TYPE(CHILD(sub
, 1)) == COLON
))
1880 && (TYPE(CHILD(sub
,NCH(sub
)-1)) != sliceop
))
1882 switch (assigning
) {
1893 com_augassign_slice(c
, sub
, assigning
, augn
);
1896 com_slice(c
, sub
, op
);
1897 if (op
== STORE_SLICE
)
1899 else if (op
== DELETE_SLICE
)
1904 /* Else normal subscriptlist. Compile each subscript. */
1905 for (i
= 0; i
< NCH(n
); i
+= 2)
1906 com_subscript(c
, CHILD(n
, i
));
1907 /* Put multiple subscripts into a tuple */
1910 com_addoparg(c
, BUILD_TUPLE
, i
);
1913 switch (assigning
) {
1928 if (assigning
> OP_APPLY
) {
1929 com_addoparg(c
, DUP_TOPX
, 2);
1931 com_addbyte(c
, BINARY_SUBSCR
);
1934 com_addbyte(c
, assigning
);
1936 com_addbyte(c
, ROT_THREE
);
1943 com_apply_trailer(struct compiling
*c
, node
*n
)
1946 switch (TYPE(CHILD(n
, 0))) {
1948 com_call_function(c
, CHILD(n
, 1));
1951 com_select_member(c
, CHILD(n
, 1));
1954 com_subscriptlist(c
, CHILD(n
, 1), OP_APPLY
, NULL
);
1957 com_error(c
, PyExc_SystemError
,
1958 "com_apply_trailer: unknown trailer type");
1963 com_power(struct compiling
*c
, node
*n
)
1967 com_atom(c
, CHILD(n
, 0));
1968 for (i
= 1; i
< NCH(n
); i
++) {
1969 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
1970 com_factor(c
, CHILD(n
, i
+1));
1971 com_addbyte(c
, BINARY_POWER
);
1976 com_apply_trailer(c
, CHILD(n
, i
));
1981 com_invert_constant(struct compiling
*c
, node
*n
)
1983 /* Compute the inverse of int and longs and use them directly,
1984 but be prepared to generate code for all other
1985 possibilities (invalid numbers, floats, complex).
1987 PyObject
*num
, *inv
= NULL
;
1991 num
= parsenumber(c
, STR(n
));
1995 inv
= PyNumber_Invert(num
);
1998 i
= com_addconst(c
, num
);
2000 i
= com_addconst(c
, inv
);
2005 com_addoparg(c
, LOAD_CONST
, i
);
2007 if (num
!= NULL
&& inv
== NULL
)
2008 com_addbyte(c
, UNARY_INVERT
);
2012 is_float_zero(const char *p
)
2014 int found_radix_point
= 0;
2016 while ((ch
= Py_CHARMASK(*p
++)) != '\0') {
2019 /* no reason to believe it's not 0 -- continue */
2022 case 'e': case 'E': case 'j': case 'J':
2023 /* If this was a hex constant, we already would have
2024 returned 0 due to the 'x' or 'X', so 'e' or 'E'
2025 must be an exponent marker, and we haven't yet
2026 seen a non-zero digit, and it doesn't matter what
2027 the exponent is then. For 'j' or 'J' similarly,
2028 except that this is an imaginary 0 then. */
2032 found_radix_point
= 1;
2039 return found_radix_point
;
2043 com_factor(struct compiling
*c
, node
*n
)
2045 int childtype
= TYPE(CHILD(n
, 0));
2046 node
*pfactor
, *ppower
, *patom
, *pnum
;
2048 /* If the unary +, -, or ~ operator is applied to a constant,
2049 don't generate a UNARY_xxx opcode. Just store the
2050 approriate value as a constant. If the value is negative,
2051 extend the string containing the constant and insert a
2052 negative in the 0th position -- unless we're doing unary minus
2053 of a floating zero! In that case the sign is significant, but
2054 the const dict can't distinguish +0.0 from -0.0.
2056 if ((childtype
== PLUS
|| childtype
== MINUS
|| childtype
== TILDE
)
2058 && TYPE((pfactor
= CHILD(n
, 1))) == factor
2059 && NCH(pfactor
) == 1
2060 && TYPE((ppower
= CHILD(pfactor
, 0))) == power
2062 && TYPE((patom
= CHILD(ppower
, 0))) == atom
2063 && TYPE((pnum
= CHILD(patom
, 0))) == NUMBER
2064 && !(childtype
== MINUS
&& is_float_zero(STR(pnum
)))) {
2065 if (childtype
== TILDE
) {
2066 com_invert_constant(c
, pnum
);
2069 if (childtype
== MINUS
) {
2070 char *s
= PyObject_MALLOC(strlen(STR(pnum
)) + 2);
2072 com_error(c
, PyExc_MemoryError
, "");
2073 com_addbyte(c
, 255);
2077 strcpy(s
+ 1, STR(pnum
));
2078 PyObject_FREE(STR(pnum
));
2083 else if (childtype
== PLUS
) {
2084 com_factor(c
, CHILD(n
, 1));
2085 com_addbyte(c
, UNARY_POSITIVE
);
2087 else if (childtype
== MINUS
) {
2088 com_factor(c
, CHILD(n
, 1));
2089 com_addbyte(c
, UNARY_NEGATIVE
);
2091 else if (childtype
== TILDE
) {
2092 com_factor(c
, CHILD(n
, 1));
2093 com_addbyte(c
, UNARY_INVERT
);
2096 com_power(c
, CHILD(n
, 0));
2101 com_term(struct compiling
*c
, node
*n
)
2106 com_factor(c
, CHILD(n
, 0));
2107 for (i
= 2; i
< NCH(n
); i
+= 2) {
2108 com_factor(c
, CHILD(n
, i
));
2109 switch (TYPE(CHILD(n
, i
-1))) {
2111 op
= BINARY_MULTIPLY
;
2114 if (c
->c_flags
& CO_FUTURE_DIVISION
)
2115 op
= BINARY_TRUE_DIVIDE
;
2123 op
= BINARY_FLOOR_DIVIDE
;
2126 com_error(c
, PyExc_SystemError
,
2127 "com_term: operator not *, /, // or %");
2136 com_arith_expr(struct compiling
*c
, node
*n
)
2141 com_term(c
, CHILD(n
, 0));
2142 for (i
= 2; i
< NCH(n
); i
+= 2) {
2143 com_term(c
, CHILD(n
, i
));
2144 switch (TYPE(CHILD(n
, i
-1))) {
2149 op
= BINARY_SUBTRACT
;
2152 com_error(c
, PyExc_SystemError
,
2153 "com_arith_expr: operator not + or -");
2162 com_shift_expr(struct compiling
*c
, node
*n
)
2167 com_arith_expr(c
, CHILD(n
, 0));
2168 for (i
= 2; i
< NCH(n
); i
+= 2) {
2169 com_arith_expr(c
, CHILD(n
, i
));
2170 switch (TYPE(CHILD(n
, i
-1))) {
2178 com_error(c
, PyExc_SystemError
,
2179 "com_shift_expr: operator not << or >>");
2188 com_and_expr(struct compiling
*c
, node
*n
)
2193 com_shift_expr(c
, CHILD(n
, 0));
2194 for (i
= 2; i
< NCH(n
); i
+= 2) {
2195 com_shift_expr(c
, CHILD(n
, i
));
2196 if (TYPE(CHILD(n
, i
-1)) == AMPER
) {
2200 com_error(c
, PyExc_SystemError
,
2201 "com_and_expr: operator not &");
2210 com_xor_expr(struct compiling
*c
, node
*n
)
2215 com_and_expr(c
, CHILD(n
, 0));
2216 for (i
= 2; i
< NCH(n
); i
+= 2) {
2217 com_and_expr(c
, CHILD(n
, i
));
2218 if (TYPE(CHILD(n
, i
-1)) == CIRCUMFLEX
) {
2222 com_error(c
, PyExc_SystemError
,
2223 "com_xor_expr: operator not ^");
2232 com_expr(struct compiling
*c
, node
*n
)
2237 com_xor_expr(c
, CHILD(n
, 0));
2238 for (i
= 2; i
< NCH(n
); i
+= 2) {
2239 com_xor_expr(c
, CHILD(n
, i
));
2240 if (TYPE(CHILD(n
, i
-1)) == VBAR
) {
2244 com_error(c
, PyExc_SystemError
,
2245 "com_expr: expr operator not |");
2257 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
2258 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2262 case LESS
: return PyCmp_LT
;
2263 case GREATER
: return PyCmp_GT
;
2264 case EQEQUAL
: /* == */
2265 case EQUAL
: return PyCmp_EQ
;
2266 case LESSEQUAL
: return PyCmp_LE
;
2267 case GREATEREQUAL
: return PyCmp_GE
;
2268 case NOTEQUAL
: return PyCmp_NE
; /* <> or != */
2269 case NAME
: if (strcmp(STR(n
), "in") == 0) return PyCmp_IN
;
2270 if (strcmp(STR(n
), "is") == 0) return PyCmp_IS
;
2273 else if (NCH(n
) == 2) {
2274 switch (TYPE(CHILD(n
, 0))) {
2275 case NAME
: if (strcmp(STR(CHILD(n
, 1)), "in") == 0)
2276 return PyCmp_NOT_IN
;
2277 if (strcmp(STR(CHILD(n
, 0)), "is") == 0)
2278 return PyCmp_IS_NOT
;
2285 com_comparison(struct compiling
*c
, node
*n
)
2290 REQ(n
, comparison
); /* comparison: expr (comp_op expr)* */
2291 com_expr(c
, CHILD(n
, 0));
2295 /****************************************************************
2296 The following code is generated for all but the last
2297 comparison in a chain:
2299 label: on stack: opcode: jump to:
2305 b, 0-or-1 JUMP_IF_FALSE L1
2309 We are now ready to repeat this sequence for the next
2310 comparison in the chain.
2312 For the last we generate:
2318 If there were any jumps to L1 (i.e., there was more than one
2319 comparison), we generate:
2321 0-or-1 JUMP_FORWARD L2
2326 ****************************************************************/
2330 for (i
= 2; i
< NCH(n
); i
+= 2) {
2331 com_expr(c
, CHILD(n
, i
));
2333 com_addbyte(c
, DUP_TOP
);
2335 com_addbyte(c
, ROT_THREE
);
2337 op
= cmp_type(CHILD(n
, i
-1));
2338 if (op
== PyCmp_BAD
) {
2339 com_error(c
, PyExc_SystemError
,
2340 "com_comparison: unknown comparison op");
2342 com_addoparg(c
, COMPARE_OP
, op
);
2345 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2346 com_addbyte(c
, POP_TOP
);
2353 com_addfwref(c
, JUMP_FORWARD
, &anchor2
);
2354 com_backpatch(c
, anchor
);
2355 com_addbyte(c
, ROT_TWO
);
2356 com_addbyte(c
, POP_TOP
);
2357 com_backpatch(c
, anchor2
);
2362 com_not_test(struct compiling
*c
, node
*n
)
2364 REQ(n
, not_test
); /* 'not' not_test | comparison */
2366 com_comparison(c
, CHILD(n
, 0));
2369 com_not_test(c
, CHILD(n
, 1));
2370 com_addbyte(c
, UNARY_NOT
);
2375 com_and_test(struct compiling
*c
, node
*n
)
2379 REQ(n
, and_test
); /* not_test ('and' not_test)* */
2383 com_not_test(c
, CHILD(n
, i
));
2384 if ((i
+= 2) >= NCH(n
))
2386 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
2387 com_addbyte(c
, POP_TOP
);
2391 com_backpatch(c
, anchor
);
2395 com_make_closure(struct compiling
*c
, PyCodeObject
*co
)
2397 int i
, free
= PyCode_GetNumFree(co
);
2400 for (i
= 0; i
< free
; ++i
) {
2401 /* Bypass com_addop_varname because it will generate
2402 LOAD_DEREF but LOAD_CLOSURE is needed.
2404 PyObject
*name
= PyTuple_GET_ITEM(co
->co_freevars
, i
);
2407 /* Special case: If a class contains a method with a
2408 free variable that has the same name as a method,
2409 the name will be considered free *and* local in the
2410 class. It should be handled by the closure, as
2411 well as by the normal name loookup logic.
2413 reftype
= get_ref_type(c
, PyString_AS_STRING(name
));
2414 if (reftype
== CELL
)
2415 arg
= com_lookup_arg(c
->c_cellvars
, name
);
2416 else /* (reftype == FREE) */
2417 arg
= com_lookup_arg(c
->c_freevars
, name
);
2419 fprintf(stderr
, "lookup %s in %s %d %d\n"
2420 "freevars of %s: %s\n",
2421 PyObject_REPR(name
),
2424 PyString_AS_STRING(co
->co_name
),
2425 PyObject_REPR(co
->co_freevars
));
2426 Py_FatalError("com_make_closure()");
2428 com_addoparg(c
, LOAD_CLOSURE
, arg
);
2436 com_test(struct compiling
*c
, node
*n
)
2438 REQ(n
, test
); /* and_test ('or' and_test)* | lambdef */
2439 if (NCH(n
) == 1 && TYPE(CHILD(n
, 0)) == lambdef
) {
2442 int ndefs
= com_argdefs(c
, CHILD(n
, 0));
2443 symtable_enter_scope(c
->c_symtable
, "lambda", lambdef
,
2445 co
= icompile(CHILD(n
, 0), c
);
2450 symtable_exit_scope(c
->c_symtable
);
2451 i
= com_addconst(c
, (PyObject
*)co
);
2452 closure
= com_make_closure(c
, co
);
2453 com_addoparg(c
, LOAD_CONST
, i
);
2456 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
2457 com_pop(c
, PyCode_GetNumFree(co
));
2459 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
2467 com_and_test(c
, CHILD(n
, i
));
2468 if ((i
+= 2) >= NCH(n
))
2470 com_addfwref(c
, JUMP_IF_TRUE
, &anchor
);
2471 com_addbyte(c
, POP_TOP
);
2475 com_backpatch(c
, anchor
);
2480 com_list(struct compiling
*c
, node
*n
, int toplevel
)
2482 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
2483 if (NCH(n
) == 1 && !toplevel
) {
2484 com_node(c
, CHILD(n
, 0));
2489 len
= (NCH(n
) + 1) / 2;
2490 for (i
= 0; i
< NCH(n
); i
+= 2)
2491 com_node(c
, CHILD(n
, i
));
2492 com_addoparg(c
, BUILD_TUPLE
, len
);
2498 /* Begin of assignment compilation */
2502 com_augassign_attr(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2504 com_addbyte(c
, DUP_TOP
);
2506 com_addopname(c
, LOAD_ATTR
, n
);
2508 com_addbyte(c
, opcode
);
2510 com_addbyte(c
, ROT_TWO
);
2511 com_addopname(c
, STORE_ATTR
, n
);
2516 com_assign_attr(struct compiling
*c
, node
*n
, int assigning
)
2518 if (none_assignment_check(c
, STR(n
), assigning
))
2520 com_addopname(c
, assigning
? STORE_ATTR
: DELETE_ATTR
, n
);
2521 com_pop(c
, assigning
? 2 : 1);
2525 com_assign_trailer(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2528 switch (TYPE(CHILD(n
, 0))) {
2529 case LPAR
: /* '(' [exprlist] ')' */
2530 if (assigning
== OP_DELETE
)
2531 com_error(c
, PyExc_SyntaxError
,
2532 "can't delete function call");
2534 com_error(c
, PyExc_SyntaxError
,
2535 "can't assign to function call");
2537 case DOT
: /* '.' NAME */
2538 if (assigning
> OP_APPLY
)
2539 com_augassign_attr(c
, CHILD(n
, 1), assigning
, augn
);
2541 com_assign_attr(c
, CHILD(n
, 1), assigning
);
2543 case LSQB
: /* '[' subscriptlist ']' */
2544 com_subscriptlist(c
, CHILD(n
, 1), assigning
, augn
);
2547 com_error(c
, PyExc_SystemError
, "unknown trailer type");
2552 com_assign_sequence(struct compiling
*c
, node
*n
, int assigning
)
2555 if (TYPE(n
) != testlist
&& TYPE(n
) != listmaker
)
2559 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
2562 for (i
= 0; i
< NCH(n
); i
+= 2)
2563 com_assign(c
, CHILD(n
, i
), assigning
, NULL
);
2567 com_augassign_name(struct compiling
*c
, node
*n
, int opcode
, node
*augn
)
2570 com_addop_varname(c
, VAR_LOAD
, STR(n
));
2573 com_addbyte(c
, opcode
);
2575 com_assign_name(c
, n
, OP_ASSIGN
);
2579 com_assign_name(struct compiling
*c
, node
*n
, int assigning
)
2582 com_addop_varname(c
, assigning
? VAR_STORE
: VAR_DELETE
, STR(n
));
2588 com_assign(struct compiling
*c
, node
*n
, int assigning
, node
*augn
)
2590 /* Loop to avoid trivial recursion */
2598 if (assigning
> OP_APPLY
) {
2599 com_error(c
, PyExc_SyntaxError
,
2600 "augmented assign to tuple not possible");
2603 com_assign_sequence(c
, n
, assigning
);
2621 com_error(c
, PyExc_SyntaxError
,
2622 "can't assign to operator");
2628 case power
: /* atom trailer* ('**' power)*
2629 ('+'|'-'|'~') factor | atom trailer* */
2630 if (TYPE(CHILD(n
, 0)) != atom
) {
2631 com_error(c
, PyExc_SyntaxError
,
2632 "can't assign to operator");
2635 if (NCH(n
) > 1) { /* trailer or exponent present */
2637 com_node(c
, CHILD(n
, 0));
2638 for (i
= 1; i
+1 < NCH(n
); i
++) {
2639 if (TYPE(CHILD(n
, i
)) == DOUBLESTAR
) {
2640 com_error(c
, PyExc_SyntaxError
,
2641 "can't assign to operator");
2644 com_apply_trailer(c
, CHILD(n
, i
));
2645 } /* NB i is still alive */
2646 com_assign_trailer(c
,
2647 CHILD(n
, i
), assigning
, augn
);
2654 switch (TYPE(CHILD(n
, 0))) {
2657 if (TYPE(n
) == RPAR
) {
2658 /* XXX Should allow () = () ??? */
2659 com_error(c
, PyExc_SyntaxError
,
2660 "can't assign to ()");
2663 if (assigning
> OP_APPLY
) {
2664 com_error(c
, PyExc_SyntaxError
,
2665 "augmented assign to tuple not possible");
2671 if (TYPE(n
) == RSQB
) {
2672 com_error(c
, PyExc_SyntaxError
,
2673 "can't assign to []");
2676 if (assigning
> OP_APPLY
) {
2677 com_error(c
, PyExc_SyntaxError
,
2678 "augmented assign to list not possible");
2682 && TYPE(CHILD(n
, 1)) == list_for
) {
2683 com_error(c
, PyExc_SyntaxError
,
2684 "can't assign to list comprehension");
2687 com_assign_sequence(c
, n
, assigning
);
2690 if (assigning
> OP_APPLY
)
2691 com_augassign_name(c
, CHILD(n
, 0),
2694 com_assign_name(c
, CHILD(n
, 0),
2698 com_error(c
, PyExc_SyntaxError
,
2699 "can't assign to literal");
2705 com_error(c
, PyExc_SyntaxError
,
2706 "can't assign to lambda");
2710 com_error(c
, PyExc_SystemError
,
2711 "com_assign: bad node");
2719 com_augassign(struct compiling
*c
, node
*n
)
2723 switch (STR(CHILD(CHILD(n
, 1), 0))[0]) {
2724 case '+': opcode
= INPLACE_ADD
; break;
2725 case '-': opcode
= INPLACE_SUBTRACT
; break;
2727 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '/')
2728 opcode
= INPLACE_FLOOR_DIVIDE
;
2729 else if (c
->c_flags
& CO_FUTURE_DIVISION
)
2730 opcode
= INPLACE_TRUE_DIVIDE
;
2732 opcode
= INPLACE_DIVIDE
;
2734 case '%': opcode
= INPLACE_MODULO
; break;
2735 case '<': opcode
= INPLACE_LSHIFT
; break;
2736 case '>': opcode
= INPLACE_RSHIFT
; break;
2737 case '&': opcode
= INPLACE_AND
; break;
2738 case '^': opcode
= INPLACE_XOR
; break;
2739 case '|': opcode
= INPLACE_OR
; break;
2741 if (STR(CHILD(CHILD(n
, 1), 0))[1] == '*')
2742 opcode
= INPLACE_POWER
;
2744 opcode
= INPLACE_MULTIPLY
;
2747 com_error(c
, PyExc_SystemError
, "com_augassign: bad operator");
2750 com_assign(c
, CHILD(n
, 0), opcode
, CHILD(n
, 2));
2754 com_expr_stmt(struct compiling
*c
, node
*n
)
2757 /* testlist (('=' testlist)* | augassign testlist) */
2758 /* Forget it if we have just a doc string here */
2759 if (!c
->c_interactive
&& NCH(n
) == 1 && get_rawdocstring(n
) != NULL
)
2762 com_node(c
, CHILD(n
, NCH(n
)-1));
2763 if (c
->c_interactive
)
2764 com_addbyte(c
, PRINT_EXPR
);
2766 com_addbyte(c
, POP_TOP
);
2769 else if (TYPE(CHILD(n
,1)) == augassign
)
2770 com_augassign(c
, n
);
2773 com_node(c
, CHILD(n
, NCH(n
)-1));
2774 for (i
= 0; i
< NCH(n
)-2; i
+=2) {
2775 if (i
+2 < NCH(n
)-2) {
2776 com_addbyte(c
, DUP_TOP
);
2779 com_assign(c
, CHILD(n
, i
), OP_ASSIGN
, NULL
);
2785 com_assert_stmt(struct compiling
*c
, node
*n
)
2789 REQ(n
, assert_stmt
); /* 'assert' test [',' test] */
2790 if (Py_OptimizeFlag
)
2792 /* Generate code like
2795 raise AssertionError [, <message>]
2797 where <message> is the second test, if present.
2799 com_node(c
, CHILD(n
, 1));
2800 com_addfwref(c
, JUMP_IF_TRUE
, &a
);
2801 com_addbyte(c
, POP_TOP
);
2803 /* Raise that exception! */
2804 com_addop_name(c
, LOAD_GLOBAL
, "AssertionError");
2806 i
= NCH(n
)/2; /* Either 2 or 4 */
2808 com_node(c
, CHILD(n
, 3));
2809 com_addoparg(c
, RAISE_VARARGS
, i
);
2811 /* The interpreter does not fall through */
2812 /* Jump ends up here */
2813 com_backpatch(c
, a
);
2814 com_addbyte(c
, POP_TOP
);
2818 com_print_stmt(struct compiling
*c
, node
*n
)
2821 node
* stream
= NULL
;
2823 REQ(n
, print_stmt
); /* 'print' (test ',')* [test] */
2825 /* are we using the extended print form? */
2826 if (NCH(n
) >= 2 && TYPE(CHILD(n
, 1)) == RIGHTSHIFT
) {
2827 stream
= CHILD(n
, 2);
2828 com_node(c
, stream
);
2829 /* stack: [...] => [... stream] */
2831 if (NCH(n
) > 3 && TYPE(CHILD(n
, 3)) == COMMA
)
2836 for (; i
< NCH(n
); i
+= 2) {
2837 if (stream
!= NULL
) {
2838 com_addbyte(c
, DUP_TOP
);
2839 /* stack: [stream] => [stream stream] */
2841 com_node(c
, CHILD(n
, i
));
2842 /* stack: [stream stream] => [stream stream obj] */
2843 com_addbyte(c
, ROT_TWO
);
2844 /* stack: [stream stream obj] => [stream obj stream] */
2845 com_addbyte(c
, PRINT_ITEM_TO
);
2846 /* stack: [stream obj stream] => [stream] */
2850 com_node(c
, CHILD(n
, i
));
2851 /* stack: [...] => [... obj] */
2852 com_addbyte(c
, PRINT_ITEM
);
2856 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
2857 if (TYPE(CHILD(n
, NCH(n
)-1)) == COMMA
) {
2858 if (stream
!= NULL
) {
2859 /* must pop the extra stream object off the stack */
2860 com_addbyte(c
, POP_TOP
);
2861 /* stack: [... stream] => [...] */
2866 if (stream
!= NULL
) {
2867 /* this consumes the last stream object on stack */
2868 com_addbyte(c
, PRINT_NEWLINE_TO
);
2869 /* stack: [... stream] => [...] */
2873 com_addbyte(c
, PRINT_NEWLINE
);
2878 com_return_stmt(struct compiling
*c
, node
*n
)
2880 REQ(n
, return_stmt
); /* 'return' [testlist] */
2881 if (!c
->c_infunction
) {
2882 com_error(c
, PyExc_SyntaxError
, "'return' outside function");
2884 if (c
->c_flags
& CO_GENERATOR
) {
2886 com_error(c
, PyExc_SyntaxError
,
2887 "'return' with argument inside generator");
2891 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2895 com_node(c
, CHILD(n
, 1));
2896 com_addbyte(c
, RETURN_VALUE
);
2901 com_yield_stmt(struct compiling
*c
, node
*n
)
2904 REQ(n
, yield_stmt
); /* 'yield' testlist */
2905 if (!c
->c_infunction
) {
2906 com_error(c
, PyExc_SyntaxError
, "'yield' outside function");
2909 for (i
= 0; i
< c
->c_nblocks
; ++i
) {
2910 if (c
->c_block
[i
] == SETUP_FINALLY
) {
2911 com_error(c
, PyExc_SyntaxError
,
2912 "'yield' not allowed in a 'try' block "
2913 "with a 'finally' clause");
2917 com_node(c
, CHILD(n
, 1));
2918 com_addbyte(c
, YIELD_VALUE
);
2923 com_raise_stmt(struct compiling
*c
, node
*n
)
2926 REQ(n
, raise_stmt
); /* 'raise' [test [',' test [',' test]]] */
2928 com_node(c
, CHILD(n
, 1));
2930 com_node(c
, CHILD(n
, 3));
2932 com_node(c
, CHILD(n
, 5));
2936 com_addoparg(c
, RAISE_VARARGS
, i
);
2941 com_from_import(struct compiling
*c
, node
*n
)
2943 com_addopname(c
, IMPORT_FROM
, CHILD(n
, 0));
2946 if (strcmp(STR(CHILD(n
, 1)), "as") != 0) {
2947 com_error(c
, PyExc_SyntaxError
, "invalid syntax");
2950 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 2)));
2952 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
2957 com_import_stmt(struct compiling
*c
, node
*n
)
2960 REQ(n
, import_stmt
);
2961 /* 'import' dotted_name (',' dotted_name)* |
2962 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
2963 if (STR(CHILD(n
, 0))[0] == 'f') {
2965 /* 'from' dotted_name 'import' ... */
2966 REQ(CHILD(n
, 1), dotted_name
);
2968 if (TYPE(CHILD(n
, 3)) == STAR
) {
2969 tup
= Py_BuildValue("(s)", "*");
2971 tup
= PyTuple_New((NCH(n
) - 2)/2);
2972 for (i
= 3; i
< NCH(n
); i
+= 2) {
2973 PyTuple_SET_ITEM(tup
, (i
-3)/2,
2974 PyString_FromString(STR(
2975 CHILD(CHILD(n
, i
), 0))));
2978 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, tup
));
2981 com_addopname(c
, IMPORT_NAME
, CHILD(n
, 1));
2982 if (TYPE(CHILD(n
, 3)) == STAR
)
2983 com_addbyte(c
, IMPORT_STAR
);
2985 for (i
= 3; i
< NCH(n
); i
+= 2)
2986 com_from_import(c
, CHILD(n
, i
));
2987 com_addbyte(c
, POP_TOP
);
2993 for (i
= 1; i
< NCH(n
); i
+= 2) {
2994 node
*subn
= CHILD(n
, i
);
2995 REQ(subn
, dotted_as_name
);
2996 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
2998 com_addopname(c
, IMPORT_NAME
, CHILD(subn
, 0));
2999 if (NCH(subn
) > 1) {
3001 if (strcmp(STR(CHILD(subn
, 1)), "as") != 0) {
3002 com_error(c
, PyExc_SyntaxError
,
3006 for (j
=2 ; j
< NCH(CHILD(subn
, 0)); j
+= 2)
3007 com_addopname(c
, LOAD_ATTR
,
3008 CHILD(CHILD(subn
, 0),
3010 com_addop_varname(c
, VAR_STORE
,
3011 STR(CHILD(subn
, 2)));
3013 com_addop_varname(c
, VAR_STORE
,
3014 STR(CHILD(CHILD(subn
, 0),
3022 com_exec_stmt(struct compiling
*c
, node
*n
)
3025 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
3026 com_node(c
, CHILD(n
, 1));
3028 com_node(c
, CHILD(n
, 3));
3030 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3034 com_node(c
, CHILD(n
, 5));
3036 com_addbyte(c
, DUP_TOP
);
3039 com_addbyte(c
, EXEC_STMT
);
3044 is_constant_false(struct compiling
*c
, node
*n
)
3048 /* argument c will be NULL when called from symtable_node() */
3050 /* Label to avoid tail recursion */
3061 for (i
= 0; i
< NCH(n
); i
++) {
3062 node
*ch
= CHILD(n
, i
);
3063 if (TYPE(ch
) == stmt
) {
3099 if (Py_OptimizeFlag
&& strcmp(STR(n
), "__debug__") == 0)
3104 v
= parsenumber(c
, STR(n
));
3109 i
= PyObject_IsTrue(v
);
3114 v
= parsestr(c
, STR(n
));
3119 i
= PyObject_IsTrue(v
);
3128 /* Look under n for a return stmt with an expression.
3129 * This hack is used to find illegal returns under "if 0:" blocks in
3130 * functions already known to be generators (as determined by the symtable
3132 * Return the offending return node if found, else NULL.
3135 look_for_offending_return(node
*n
)
3139 for (i
= 0; i
< NCH(n
); ++i
) {
3140 node
*kid
= CHILD(n
, i
);
3142 switch (TYPE(kid
)) {
3146 /* Stuff in nested functions & classes doesn't
3147 affect the code block we started in. */
3156 node
*bad
= look_for_offending_return(kid
);
3167 com_if_stmt(struct compiling
*c
, node
*n
)
3172 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3173 for (i
= 0; i
+3 < NCH(n
); i
+=4) {
3175 node
*ch
= CHILD(n
, i
+1);
3176 if (is_constant_false(c
, ch
)) {
3177 /* We're going to skip this block. However, if this
3178 is a generator, we have to check the dead code
3179 anyway to make sure there aren't any return stmts
3180 with expressions, in the same scope. */
3181 if (c
->c_flags
& CO_GENERATOR
) {
3182 node
*p
= look_for_offending_return(n
);
3184 int savelineno
= c
->c_lineno
;
3185 c
->c_lineno
= p
->n_lineno
;
3186 com_error(c
, PyExc_SyntaxError
,
3187 "'return' with argument "
3188 "inside generator");
3189 c
->c_lineno
= savelineno
;
3195 com_set_lineno(c
, ch
->n_lineno
);
3197 com_addfwref(c
, JUMP_IF_FALSE
, &a
);
3198 com_addbyte(c
, POP_TOP
);
3200 com_node(c
, CHILD(n
, i
+3));
3201 com_addfwref(c
, JUMP_FORWARD
, &anchor
);
3202 com_backpatch(c
, a
);
3203 /* We jump here with an extra entry which we now pop */
3204 com_addbyte(c
, POP_TOP
);
3207 com_node(c
, CHILD(n
, i
+2));
3209 com_backpatch(c
, anchor
);
3213 com_while_stmt(struct compiling
*c
, node
*n
)
3215 int break_anchor
= 0;
3217 int save_begin
= c
->c_begin
;
3218 REQ(n
, while_stmt
); /* 'while' test ':' suite ['else' ':' suite] */
3219 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
3220 block_push(c
, SETUP_LOOP
);
3221 c
->c_begin
= c
->c_nexti
;
3222 com_set_lineno(c
, n
->n_lineno
);
3223 com_node(c
, CHILD(n
, 1));
3224 com_addfwref(c
, JUMP_IF_FALSE
, &anchor
);
3225 com_addbyte(c
, POP_TOP
);
3228 com_node(c
, CHILD(n
, 3));
3230 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3231 c
->c_begin
= save_begin
;
3232 com_backpatch(c
, anchor
);
3233 /* We jump here with one entry more on the stack */
3234 com_addbyte(c
, POP_TOP
);
3235 com_addbyte(c
, POP_BLOCK
);
3236 block_pop(c
, SETUP_LOOP
);
3238 com_node(c
, CHILD(n
, 6));
3239 com_backpatch(c
, break_anchor
);
3243 com_for_stmt(struct compiling
*c
, node
*n
)
3245 int break_anchor
= 0;
3247 int save_begin
= c
->c_begin
;
3249 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3250 com_addfwref(c
, SETUP_LOOP
, &break_anchor
);
3251 block_push(c
, SETUP_LOOP
);
3252 com_node(c
, CHILD(n
, 3));
3253 com_addbyte(c
, GET_ITER
);
3254 c
->c_begin
= c
->c_nexti
;
3255 com_set_lineno(c
, n
->n_lineno
);
3256 com_addfwref(c
, FOR_ITER
, &anchor
);
3258 com_assign(c
, CHILD(n
, 1), OP_ASSIGN
, NULL
);
3260 com_node(c
, CHILD(n
, 5));
3262 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3263 c
->c_begin
= save_begin
;
3264 com_backpatch(c
, anchor
);
3265 com_pop(c
, 1); /* FOR_ITER has popped this */
3266 com_addbyte(c
, POP_BLOCK
);
3267 block_pop(c
, SETUP_LOOP
);
3269 com_node(c
, CHILD(n
, 8));
3270 com_backpatch(c
, break_anchor
);
3273 /* Code generated for "try: S finally: Sf" is as follows:
3282 The special instructions use the block stack. Each block
3283 stack entry contains the instruction that created it (here
3284 SETUP_FINALLY), the level of the value stack at the time the
3285 block stack entry was created, and a label (here L).
3288 Pushes the current value stack level and the label
3289 onto the block stack.
3291 Pops en entry from the block stack, and pops the value
3292 stack until its level is the same as indicated on the
3293 block stack. (The label is ignored.)
3295 Pops a variable number of entries from the *value* stack
3296 and re-raises the exception they specify. The number of
3297 entries popped depends on the (pseudo) exception type.
3299 The block stack is unwound when an exception is raised:
3300 when a SETUP_FINALLY entry is found, the exception is pushed
3301 onto the value stack (and the exception condition is cleared),
3302 and the interpreter jumps to the label gotten from the block
3305 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
3306 (The contents of the value stack is shown in [], with the top
3307 at the right; 'tb' is trace-back info, 'val' the exception's
3308 associated value, and 'exc' the exception.)
3310 Value stack Label Instruction Argument
3316 [tb, val, exc] L1: DUP )
3317 [tb, val, exc, exc] <evaluate E1> )
3318 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3319 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3320 [tb, val, exc, 1] POP )
3322 [tb, val] <assign to V1> (or POP if no V1)
3327 [tb, val, exc, 0] L2: POP
3329 .............................etc.......................
3331 [tb, val, exc, 0] Ln+1: POP
3332 [tb, val, exc] END_FINALLY # re-raise exception
3334 [] L0: <next statement>
3336 Of course, parts are not generated if Vi or Ei is not present.
3340 com_try_except(struct compiling
*c
, node
*n
)
3342 int except_anchor
= 0;
3344 int else_anchor
= 0;
3348 com_addfwref(c
, SETUP_EXCEPT
, &except_anchor
);
3349 block_push(c
, SETUP_EXCEPT
);
3350 com_node(c
, CHILD(n
, 2));
3351 com_addbyte(c
, POP_BLOCK
);
3352 block_pop(c
, SETUP_EXCEPT
);
3353 com_addfwref(c
, JUMP_FORWARD
, &else_anchor
);
3354 com_backpatch(c
, except_anchor
);
3356 i
< NCH(n
) && TYPE(ch
= CHILD(n
, i
)) == except_clause
;
3358 /* except_clause: 'except' [expr [',' var]] */
3359 if (except_anchor
== 0) {
3360 com_error(c
, PyExc_SyntaxError
,
3361 "default 'except:' must be last");
3365 com_push(c
, 3); /* tb, val, exc pushed by exception */
3366 com_set_lineno(c
, ch
->n_lineno
);
3368 com_addbyte(c
, DUP_TOP
);
3370 com_node(c
, CHILD(ch
, 1));
3371 com_addoparg(c
, COMPARE_OP
, PyCmp_EXC_MATCH
);
3373 com_addfwref(c
, JUMP_IF_FALSE
, &except_anchor
);
3374 com_addbyte(c
, POP_TOP
);
3377 com_addbyte(c
, POP_TOP
);
3380 com_assign(c
, CHILD(ch
, 3), OP_ASSIGN
, NULL
);
3382 com_addbyte(c
, POP_TOP
);
3385 com_addbyte(c
, POP_TOP
);
3387 com_node(c
, CHILD(n
, i
+2));
3388 com_addfwref(c
, JUMP_FORWARD
, &end_anchor
);
3389 if (except_anchor
) {
3390 com_backpatch(c
, except_anchor
);
3391 /* We come in with [tb, val, exc, 0] on the
3392 stack; one pop and it's the same as
3393 expected at the start of the loop */
3394 com_addbyte(c
, POP_TOP
);
3397 /* We actually come in here with [tb, val, exc] but the
3398 END_FINALLY will zap those and jump around.
3399 The c_stacklevel does not reflect them so we need not pop
3401 com_addbyte(c
, END_FINALLY
);
3402 com_backpatch(c
, else_anchor
);
3404 com_node(c
, CHILD(n
, i
+2));
3405 com_backpatch(c
, end_anchor
);
3409 com_try_finally(struct compiling
*c
, node
*n
)
3411 int finally_anchor
= 0;
3414 com_addfwref(c
, SETUP_FINALLY
, &finally_anchor
);
3415 block_push(c
, SETUP_FINALLY
);
3416 com_node(c
, CHILD(n
, 2));
3417 com_addbyte(c
, POP_BLOCK
);
3418 block_pop(c
, SETUP_FINALLY
);
3419 block_push(c
, END_FINALLY
);
3420 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
3421 /* While the generated code pushes only one item,
3422 the try-finally handling can enter here with
3423 up to three items. OK, here are the details:
3424 3 for an exception, 2 for RETURN, 1 for BREAK. */
3426 com_backpatch(c
, finally_anchor
);
3427 ch
= CHILD(n
, NCH(n
)-1);
3428 com_set_lineno(c
, ch
->n_lineno
);
3430 com_addbyte(c
, END_FINALLY
);
3431 block_pop(c
, END_FINALLY
);
3432 com_pop(c
, 3); /* Matches the com_push above */
3436 com_try_stmt(struct compiling
*c
, node
*n
)
3439 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3440 | 'try' ':' suite 'finally' ':' suite */
3441 if (TYPE(CHILD(n
, 3)) != except_clause
)
3442 com_try_finally(c
, n
);
3444 com_try_except(c
, n
);
3448 get_rawdocstring(node
*n
)
3452 /* Label to avoid tail recursion */
3463 for (i
= 0; i
< NCH(n
); i
++) {
3464 node
*ch
= CHILD(n
, i
);
3465 if (TYPE(ch
) == stmt
) {
3500 if (TYPE(CHILD(n
, 0)) == STRING
)
3509 get_docstring(struct compiling
*c
, node
*n
)
3511 /* Don't generate doc-strings if run with -OO */
3512 if (Py_OptimizeFlag
> 1)
3514 n
= get_rawdocstring(n
);
3517 return parsestrplus(c
, n
);
3521 com_suite(struct compiling
*c
, node
*n
)
3524 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3526 com_node(c
, CHILD(n
, 0));
3530 for (i
= 0; i
< NCH(n
) && c
->c_errors
== 0; i
++) {
3531 node
*ch
= CHILD(n
, i
);
3532 if (TYPE(ch
) == stmt
)
3540 com_continue_stmt(struct compiling
*c
, node
*n
)
3542 int i
= c
->c_nblocks
;
3543 if (i
-- > 0 && c
->c_block
[i
] == SETUP_LOOP
) {
3544 com_addoparg(c
, JUMP_ABSOLUTE
, c
->c_begin
);
3547 /* at the outer level */
3548 com_error(c
, PyExc_SyntaxError
,
3549 "'continue' not properly in loop");
3553 for (j
= i
-1; j
>= 0; --j
) {
3554 if (c
->c_block
[j
] == SETUP_LOOP
)
3558 /* there is a loop, but something interferes */
3559 for (; i
> j
; --i
) {
3560 if (c
->c_block
[i
] == SETUP_EXCEPT
||
3561 c
->c_block
[i
] == SETUP_FINALLY
) {
3562 com_addoparg(c
, CONTINUE_LOOP
,
3566 if (c
->c_block
[i
] == END_FINALLY
) {
3567 com_error(c
, PyExc_SyntaxError
,
3568 "'continue' not supported inside 'finally' clause");
3573 com_error(c
, PyExc_SyntaxError
,
3574 "'continue' not properly in loop");
3576 /* XXX Could allow it inside a 'finally' clause
3577 XXX if we could pop the exception still on the stack */
3581 com_argdefs(struct compiling
*c
, node
*n
)
3583 int i
, nch
, nargs
, ndefs
;
3584 if (TYPE(n
) == lambdef
) {
3585 /* lambdef: 'lambda' [varargslist] ':' test */
3589 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ... */
3591 REQ(n
, parameters
); /* parameters: '(' [varargslist] ')' */
3594 if (TYPE(n
) != varargslist
)
3597 (fpdef ['=' test] ',')* '*' ....... |
3598 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3602 for (i
= 0; i
< nch
; i
++) {
3604 if (TYPE(CHILD(n
, i
)) == STAR
||
3605 TYPE(CHILD(n
, i
)) == DOUBLESTAR
)
3610 t
= RPAR
; /* Anything except EQUAL or COMMA */
3612 t
= TYPE(CHILD(n
, i
));
3616 com_node(c
, CHILD(n
, i
));
3620 t
= TYPE(CHILD(n
, i
));
3623 /* Treat "(a=1, b)" as an error */
3625 com_error(c
, PyExc_SyntaxError
,
3626 "non-default argument follows default argument");
3635 com_funcdef(struct compiling
*c
, node
*n
)
3639 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
3640 ndefs
= com_argdefs(c
, n
);
3641 symtable_enter_scope(c
->c_symtable
, STR(CHILD(n
, 1)), TYPE(n
),
3643 co
= (PyObject
*)icompile(n
, c
);
3644 symtable_exit_scope(c
->c_symtable
);
3648 int closure
= com_make_closure(c
, (PyCodeObject
*)co
);
3649 int i
= com_addconst(c
, co
);
3650 com_addoparg(c
, LOAD_CONST
, i
);
3653 com_addoparg(c
, MAKE_CLOSURE
, ndefs
);
3655 com_addoparg(c
, MAKE_FUNCTION
, ndefs
);
3657 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3664 com_bases(struct compiling
*c
, node
*n
)
3668 /* testlist: test (',' test)* [','] */
3669 for (i
= 0; i
< NCH(n
); i
+= 2)
3670 com_node(c
, CHILD(n
, i
));
3672 com_addoparg(c
, BUILD_TUPLE
, i
);
3677 com_classdef(struct compiling
*c
, node
*n
)
3685 /* classdef: class NAME ['(' testlist ')'] ':' suite */
3686 if ((v
= PyString_InternFromString(STR(CHILD(n
, 1)))) == NULL
) {
3690 /* Push the class name on the stack */
3691 i
= com_addconst(c
, v
);
3692 com_addoparg(c
, LOAD_CONST
, i
);
3695 /* Push the tuple of base classes on the stack */
3696 if (TYPE(CHILD(n
, 2)) != LPAR
) {
3697 com_addoparg(c
, BUILD_TUPLE
, 0);
3701 com_bases(c
, CHILD(n
, 3));
3702 name
= STR(CHILD(n
, 1));
3703 symtable_enter_scope(c
->c_symtable
, name
, TYPE(n
), n
->n_lineno
);
3704 co
= icompile(n
, c
);
3705 symtable_exit_scope(c
->c_symtable
);
3709 int closure
= com_make_closure(c
, co
);
3710 i
= com_addconst(c
, (PyObject
*)co
);
3711 com_addoparg(c
, LOAD_CONST
, i
);
3714 com_addoparg(c
, MAKE_CLOSURE
, 0);
3715 com_pop(c
, PyCode_GetNumFree(co
));
3717 com_addoparg(c
, MAKE_FUNCTION
, 0);
3718 com_addoparg(c
, CALL_FUNCTION
, 0);
3719 com_addbyte(c
, BUILD_CLASS
);
3721 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 1)));
3728 com_node(struct compiling
*c
, node
*n
)
3735 /* Definition nodes */
3744 /* Trivial parse tree nodes */
3753 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3754 com_set_lineno(c
, n
->n_lineno
);
3757 for (i
= 0; i
< NCH(n
)-1; i
+= 2)
3758 com_node(c
, CHILD(n
, i
));
3763 com_set_lineno(c
, n
->n_lineno
);
3767 /* Statement nodes */
3770 com_expr_stmt(c
, n
);
3773 com_print_stmt(c
, n
);
3775 case del_stmt
: /* 'del' exprlist */
3776 com_assign(c
, CHILD(n
, 1), OP_DELETE
, NULL
);
3781 if (c
->c_loops
== 0) {
3782 com_error(c
, PyExc_SyntaxError
,
3783 "'break' outside loop");
3785 com_addbyte(c
, BREAK_LOOP
);
3788 com_continue_stmt(c
, n
);
3791 com_return_stmt(c
, n
);
3794 com_yield_stmt(c
, n
);
3797 com_raise_stmt(c
, n
);
3800 com_import_stmt(c
, n
);
3805 com_exec_stmt(c
, n
);
3808 com_assert_stmt(c
, n
);
3814 com_while_stmt(c
, n
);
3826 /* Expression nodes */
3843 com_comparison(c
, n
);
3858 com_shift_expr(c
, n
);
3861 com_arith_expr(c
, n
);
3877 com_error(c
, PyExc_SystemError
,
3878 "com_node: unexpected node type");
3882 static void com_fplist(struct compiling
*, node
*);
3885 com_fpdef(struct compiling
*c
, node
*n
)
3887 REQ(n
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3888 if (TYPE(CHILD(n
, 0)) == LPAR
)
3889 com_fplist(c
, CHILD(n
, 1));
3891 com_addop_varname(c
, VAR_STORE
, STR(CHILD(n
, 0)));
3897 com_fplist(struct compiling
*c
, node
*n
)
3899 REQ(n
, fplist
); /* fplist: fpdef (',' fpdef)* [','] */
3901 com_fpdef(c
, CHILD(n
, 0));
3904 int i
= (NCH(n
)+1)/2;
3905 com_addoparg(c
, UNPACK_SEQUENCE
, i
);
3907 for (i
= 0; i
< NCH(n
); i
+= 2)
3908 com_fpdef(c
, CHILD(n
, i
));
3913 com_arglist(struct compiling
*c
, node
*n
)
3918 REQ(n
, varargslist
);
3920 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
3922 /* Enter all arguments in table of locals */
3923 for (i
= 0, narg
= 0; i
< nch
; i
++) {
3924 node
*ch
= CHILD(n
, i
);
3926 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3928 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3930 if (TYPE(fp
) != NAME
) {
3931 PyOS_snprintf(nbuf
, sizeof(nbuf
), ".%d", i
);
3935 /* all name updates handled by symtable */
3939 if (TYPE(ch
) == EQUAL
)
3945 /* Generate code for complex arguments only after
3946 having counted the simple arguments */
3948 for (i
= 0; i
< nch
; i
++) {
3949 node
*ch
= CHILD(n
, i
);
3951 if (TYPE(ch
) == STAR
|| TYPE(ch
) == DOUBLESTAR
)
3953 REQ(ch
, fpdef
); /* fpdef: NAME | '(' fplist ')' */
3955 if (TYPE(fp
) != NAME
) {
3956 com_addoparg(c
, LOAD_FAST
, ilocal
);
3964 if (TYPE(ch
) == EQUAL
)
3973 com_file_input(struct compiling
*c
, node
*n
)
3977 REQ(n
, file_input
); /* (NEWLINE | stmt)* ENDMARKER */
3978 doc
= get_docstring(c
, n
);
3980 int i
= com_addconst(c
, doc
);
3982 com_addoparg(c
, LOAD_CONST
, i
);
3984 com_addop_name(c
, STORE_NAME
, "__doc__");
3987 for (i
= 0; i
< NCH(n
); i
++) {
3988 node
*ch
= CHILD(n
, i
);
3989 if (TYPE(ch
) != ENDMARKER
&& TYPE(ch
) != NEWLINE
)
3994 /* Top-level compile-node interface */
3997 compile_funcdef(struct compiling
*c
, node
*n
)
4001 REQ(n
, funcdef
); /* funcdef: 'def' NAME parameters ':' suite */
4002 c
->c_name
= STR(CHILD(n
, 1));
4003 doc
= get_docstring(c
, CHILD(n
, 4));
4005 (void) com_addconst(c
, doc
);
4009 (void) com_addconst(c
, Py_None
); /* No docstring */
4010 ch
= CHILD(n
, 2); /* parameters: '(' [varargslist] ')' */
4011 ch
= CHILD(ch
, 1); /* ')' | varargslist */
4012 if (TYPE(ch
) == varargslist
)
4014 c
->c_infunction
= 1;
4015 com_node(c
, CHILD(n
, 4));
4016 c
->c_infunction
= 0;
4017 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
4019 com_addbyte(c
, RETURN_VALUE
);
4024 compile_lambdef(struct compiling
*c
, node
*n
)
4027 REQ(n
, lambdef
); /* lambdef: 'lambda' [varargslist] ':' test */
4028 c
->c_name
= "<lambda>";
4031 (void) com_addconst(c
, Py_None
); /* No docstring */
4032 if (TYPE(ch
) == varargslist
) {
4039 com_addbyte(c
, RETURN_VALUE
);
4044 compile_classdef(struct compiling
*c
, node
*n
)
4049 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4050 c
->c_name
= STR(CHILD(n
, 1));
4051 c
->c_private
= c
->c_name
;
4052 /* Initialize local __module__ from global __name__ */
4053 com_addop_name(c
, LOAD_GLOBAL
, "__name__");
4054 com_addop_name(c
, STORE_NAME
, "__module__");
4055 ch
= CHILD(n
, NCH(n
)-1); /* The suite */
4056 doc
= get_docstring(c
, ch
);
4058 int i
= com_addconst(c
, doc
);
4060 com_addoparg(c
, LOAD_CONST
, i
);
4062 com_addop_name(c
, STORE_NAME
, "__doc__");
4066 (void) com_addconst(c
, Py_None
);
4068 com_addbyte(c
, LOAD_LOCALS
);
4070 com_addbyte(c
, RETURN_VALUE
);
4075 compile_node(struct compiling
*c
, node
*n
)
4077 com_set_lineno(c
, n
->n_lineno
);
4081 case single_input
: /* One interactive command */
4082 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
4085 if (TYPE(n
) != NEWLINE
)
4087 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
4089 com_addbyte(c
, RETURN_VALUE
);
4094 case file_input
: /* A whole file, or built-in function exec() */
4095 com_file_input(c
, n
);
4096 com_addoparg(c
, LOAD_CONST
, com_addconst(c
, Py_None
));
4098 com_addbyte(c
, RETURN_VALUE
);
4102 case eval_input
: /* Built-in function input() */
4103 com_node(c
, CHILD(n
, 0));
4104 com_addbyte(c
, RETURN_VALUE
);
4108 case lambdef
: /* anonymous function definition */
4109 compile_lambdef(c
, n
);
4112 case funcdef
: /* A function definition */
4113 compile_funcdef(c
, n
);
4116 case classdef
: /* A class definition */
4117 compile_classdef(c
, n
);
4121 com_error(c
, PyExc_SystemError
,
4122 "compile_node: unexpected node type");
4127 dict_keys_inorder(PyObject
*dict
, int offset
)
4129 PyObject
*tuple
, *k
, *v
;
4130 int i
, pos
= 0, size
= PyDict_Size(dict
);
4132 tuple
= PyTuple_New(size
);
4135 while (PyDict_Next(dict
, &pos
, &k
, &v
)) {
4136 i
= PyInt_AS_LONG(v
);
4138 assert((i
- offset
) < size
);
4139 PyTuple_SET_ITEM(tuple
, i
- offset
, k
);
4145 PyNode_Compile(node
*n
, char *filename
)
4147 return PyNode_CompileFlags(n
, filename
, NULL
);
4151 PyNode_CompileFlags(node
*n
, char *filename
, PyCompilerFlags
*flags
)
4153 return jcompile(n
, filename
, NULL
, flags
);
4157 PyNode_CompileSymtable(node
*n
, char *filename
)
4159 struct symtable
*st
;
4160 PyFutureFeatures
*ff
;
4162 ff
= PyNode_Future(n
, filename
);
4166 st
= symtable_init();
4168 PyObject_FREE((void *)ff
);
4172 symtable_enter_scope(st
, TOP
, TYPE(n
), n
->n_lineno
);
4173 if (st
->st_errors
> 0)
4175 symtable_node(st
, n
);
4176 if (st
->st_errors
> 0)
4181 PyObject_FREE((void *)ff
);
4182 st
->st_future
= NULL
;
4183 PySymtable_Free(st
);
4187 static PyCodeObject
*
4188 icompile(node
*n
, struct compiling
*base
)
4190 return jcompile(n
, base
->c_filename
, base
, NULL
);
4193 static PyCodeObject
*
4194 jcompile(node
*n
, char *filename
, struct compiling
*base
,
4195 PyCompilerFlags
*flags
)
4197 struct compiling sc
;
4199 if (!com_init(&sc
, filename
))
4201 if (TYPE(n
) == encoding_decl
) {
4202 sc
.c_encoding
= STR(n
);
4205 sc
.c_encoding
= NULL
;
4208 sc
.c_private
= base
->c_private
;
4209 sc
.c_symtable
= base
->c_symtable
;
4210 /* c_symtable still points to parent's symbols */
4212 || (sc
.c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
))
4214 sc
.c_flags
|= base
->c_flags
& PyCF_MASK
;
4215 if (base
->c_encoding
!= NULL
) {
4216 assert(sc
.c_encoding
== NULL
);
4217 sc
.c_encoding
= base
->c_encoding
;
4220 sc
.c_private
= NULL
;
4221 sc
.c_future
= PyNode_Future(n
, filename
);
4222 if (sc
.c_future
== NULL
) {
4227 int merged
= sc
.c_future
->ff_features
|
4229 sc
.c_future
->ff_features
= merged
;
4230 flags
->cf_flags
= merged
;
4232 if (symtable_build(&sc
, n
) < 0) {
4238 if (symtable_load_symbols(&sc
) < 0) {
4242 compile_node(&sc
, n
);
4244 if (sc
.c_errors
== 0) {
4245 PyObject
*consts
, *names
, *varnames
, *filename
, *name
,
4246 *freevars
, *cellvars
;
4247 consts
= PyList_AsTuple(sc
.c_consts
);
4248 names
= PyList_AsTuple(sc
.c_names
);
4249 varnames
= PyList_AsTuple(sc
.c_varnames
);
4250 cellvars
= dict_keys_inorder(sc
.c_cellvars
, 0);
4251 freevars
= dict_keys_inorder(sc
.c_freevars
,
4252 PyTuple_GET_SIZE(cellvars
));
4253 filename
= PyString_InternFromString(sc
.c_filename
);
4254 name
= PyString_InternFromString(sc
.c_name
);
4255 if (!PyErr_Occurred())
4256 co
= PyCode_New(sc
.c_argcount
,
4272 Py_XDECREF(varnames
);
4273 Py_XDECREF(freevars
);
4274 Py_XDECREF(cellvars
);
4275 Py_XDECREF(filename
);
4278 else if (!PyErr_Occurred()) {
4279 /* This could happen if someone called PyErr_Clear() after an
4280 error was reported above. That's not supposed to happen,
4281 but I just plugged one case and I'm not sure there can't be
4282 others. In that case, raise SystemError so that at least
4283 it gets reported instead dumping core. */
4284 PyErr_SetString(PyExc_SystemError
, "lost syntax error");
4288 PySymtable_Free(sc
.c_symtable
);
4289 sc
.c_symtable
= NULL
;
4296 PyCode_Addr2Line(PyCodeObject
*co
, int addrq
)
4298 int size
= PyString_Size(co
->co_lnotab
) / 2;
4299 unsigned char *p
= (unsigned char*)PyString_AsString(co
->co_lnotab
);
4300 int line
= co
->co_firstlineno
;
4302 while (--size
>= 0) {
4311 /* The test for LOCAL must come before the test for FREE in order to
4312 handle classes where name is both local and free. The local var is
4313 a method and the free var is a free var referenced within a method.
4317 get_ref_type(struct compiling
*c
, char *name
)
4322 if (PyDict_GetItemString(c
->c_cellvars
, name
) != NULL
)
4324 if (PyDict_GetItemString(c
->c_locals
, name
) != NULL
)
4326 if (PyDict_GetItemString(c
->c_freevars
, name
) != NULL
)
4328 v
= PyDict_GetItemString(c
->c_globals
, name
);
4331 return GLOBAL_EXPLICIT
;
4333 return GLOBAL_IMPLICIT
;
4336 PyOS_snprintf(buf
, sizeof(buf
),
4337 "unknown scope for %.100s in %.100s(%s) "
4338 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4340 PyObject_REPR(c
->c_symtable
->st_cur
->ste_id
),
4342 PyObject_REPR(c
->c_symtable
->st_cur
->ste_symbols
),
4343 PyObject_REPR(c
->c_locals
),
4344 PyObject_REPR(c
->c_globals
)
4351 /* Helper functions to issue warnings */
4354 issue_warning(char *msg
, char *filename
, int lineno
)
4356 if (PyErr_WarnExplicit(PyExc_SyntaxWarning
, msg
, filename
,
4357 lineno
, NULL
, NULL
) < 0) {
4358 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning
)) {
4359 PyErr_SetString(PyExc_SyntaxError
, msg
);
4360 PyErr_SyntaxLocation(filename
, lineno
);
4368 symtable_warn(struct symtable
*st
, char *msg
)
4370 if (issue_warning(msg
, st
->st_filename
, st
->st_cur
->ste_lineno
) < 0) {
4377 /* Helper function for setting lineno and filename */
4380 symtable_build(struct compiling
*c
, node
*n
)
4382 if ((c
->c_symtable
= symtable_init()) == NULL
)
4384 c
->c_symtable
->st_future
= c
->c_future
;
4385 c
->c_symtable
->st_filename
= c
->c_filename
;
4386 symtable_enter_scope(c
->c_symtable
, TOP
, TYPE(n
), n
->n_lineno
);
4387 if (c
->c_symtable
->st_errors
> 0)
4389 symtable_node(c
->c_symtable
, n
);
4390 if (c
->c_symtable
->st_errors
> 0)
4392 /* reset for second pass */
4393 c
->c_symtable
->st_nscopes
= 1;
4394 c
->c_symtable
->st_pass
= 2;
4399 symtable_init_compiling_symbols(struct compiling
*c
)
4403 varnames
= c
->c_symtable
->st_cur
->ste_varnames
;
4404 if (varnames
== NULL
) {
4405 varnames
= PyList_New(0);
4406 if (varnames
== NULL
)
4408 c
->c_symtable
->st_cur
->ste_varnames
= varnames
;
4409 Py_INCREF(varnames
);
4411 Py_INCREF(varnames
);
4412 c
->c_varnames
= varnames
;
4414 c
->c_globals
= PyDict_New();
4415 if (c
->c_globals
== NULL
)
4417 c
->c_freevars
= PyDict_New();
4418 if (c
->c_freevars
== NULL
)
4420 c
->c_cellvars
= PyDict_New();
4421 if (c
->c_cellvars
== NULL
)
4426 struct symbol_info
{
4434 symtable_init_info(struct symbol_info
*si
)
4439 si
->si_nimplicit
= 0;
4443 symtable_resolve_free(struct compiling
*c
, PyObject
*name
, int flags
,
4444 struct symbol_info
*si
)
4448 /* Seperate logic for DEF_FREE. If it occurs in a function,
4449 it indicates a local that we must allocate storage for (a
4450 cell var). If it occurs in a class, then the class has a
4451 method and a free variable with the same name.
4453 if (c
->c_symtable
->st_cur
->ste_type
== TYPE_FUNCTION
) {
4454 /* If it isn't declared locally, it can't be a cell. */
4455 if (!(flags
& (DEF_LOCAL
| DEF_PARAM
)))
4457 v
= PyInt_FromLong(si
->si_ncells
++);
4458 dict
= c
->c_cellvars
;
4460 /* If it is free anyway, then there is no need to do
4463 if (is_free(flags
^ DEF_FREE_CLASS
)
4464 || (flags
== DEF_FREE_CLASS
))
4466 v
= PyInt_FromLong(si
->si_nfrees
++);
4467 dict
= c
->c_freevars
;
4471 if (PyDict_SetItem(dict
, name
, v
) < 0) {
4479 /* If a variable is a cell and an argument, make sure that appears in
4480 co_cellvars before any variable to its right in varnames.
4485 symtable_cellvar_offsets(PyObject
**cellvars
, int argcount
,
4486 PyObject
*varnames
, int flags
)
4488 PyObject
*v
, *w
, *d
, *list
= NULL
;
4491 if (flags
& CO_VARARGS
)
4493 if (flags
& CO_VARKEYWORDS
)
4495 for (i
= argcount
; --i
>= 0; ) {
4496 v
= PyList_GET_ITEM(varnames
, i
);
4497 if (PyDict_GetItem(*cellvars
, v
)) {
4499 list
= PyList_New(1);
4502 PyList_SET_ITEM(list
, 0, v
);
4505 PyList_Insert(list
, 0, v
);
4508 if (list
== NULL
|| PyList_GET_SIZE(list
) == 0)
4510 /* There are cellvars that are also arguments. Create a dict
4511 to replace cellvars and put the args at the front.
4514 for (i
= PyList_GET_SIZE(list
); --i
>= 0; ) {
4515 v
= PyInt_FromLong(i
);
4518 if (PyDict_SetItem(d
, PyList_GET_ITEM(list
, i
), v
) < 0)
4520 if (PyDict_DelItem(*cellvars
, PyList_GET_ITEM(list
, i
)) < 0)
4524 i
= PyList_GET_SIZE(list
);
4526 while (PyDict_Next(*cellvars
, &pos
, &v
, &w
)) {
4527 w
= PyInt_FromLong(i
++); /* don't care about the old key */
4528 if (PyDict_SetItem(d
, v
, w
) < 0) {
4534 Py_DECREF(*cellvars
);
4543 symtable_freevar_offsets(PyObject
*freevars
, int offset
)
4548 /* The cell vars are the first elements of the closure,
4549 followed by the free vars. Update the offsets in
4550 c_freevars to account for number of cellvars. */
4552 while (PyDict_Next(freevars
, &pos
, &name
, &v
)) {
4553 int i
= PyInt_AS_LONG(v
) + offset
;
4554 PyObject
*o
= PyInt_FromLong(i
);
4557 if (PyDict_SetItem(freevars
, name
, o
) < 0) {
4567 symtable_check_unoptimized(struct compiling
*c
,
4568 PySymtableEntryObject
*ste
,
4569 struct symbol_info
*si
)
4573 if (!(si
->si_ncells
|| si
->si_nfrees
|| ste
->ste_child_free
4574 || (ste
->ste_nested
&& si
->si_nimplicit
)))
4577 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
4579 #define ILLEGAL_IS "is a nested function"
4581 #define ILLEGAL_IMPORT_STAR \
4582 "import * is not allowed in function '%.100s' because it %s"
4584 #define ILLEGAL_BARE_EXEC \
4585 "unqualified exec is not allowed in function '%.100s' it %s"
4587 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
4588 "function '%.100s' uses import * and bare exec, which are illegal " \
4591 /* XXX perhaps the linenos for these opt-breaking statements
4592 should be stored so the exception can point to them. */
4594 if (ste
->ste_child_free
) {
4595 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4596 PyOS_snprintf(buf
, sizeof(buf
),
4597 ILLEGAL_IMPORT_STAR
,
4598 PyString_AS_STRING(ste
->ste_name
),
4600 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4601 PyOS_snprintf(buf
, sizeof(buf
),
4603 PyString_AS_STRING(ste
->ste_name
),
4606 PyOS_snprintf(buf
, sizeof(buf
),
4607 ILLEGAL_EXEC_AND_IMPORT_STAR
,
4608 PyString_AS_STRING(ste
->ste_name
),
4612 if (ste
->ste_optimized
== OPT_IMPORT_STAR
)
4613 PyOS_snprintf(buf
, sizeof(buf
),
4614 ILLEGAL_IMPORT_STAR
,
4615 PyString_AS_STRING(ste
->ste_name
),
4617 else if (ste
->ste_optimized
== (OPT_BARE_EXEC
| OPT_EXEC
))
4618 PyOS_snprintf(buf
, sizeof(buf
),
4620 PyString_AS_STRING(ste
->ste_name
),
4623 PyOS_snprintf(buf
, sizeof(buf
),
4624 ILLEGAL_EXEC_AND_IMPORT_STAR
,
4625 PyString_AS_STRING(ste
->ste_name
),
4630 PyErr_SetString(PyExc_SyntaxError
, buf
);
4631 PyErr_SyntaxLocation(c
->c_symtable
->st_filename
,
4632 ste
->ste_opt_lineno
);
4637 symtable_update_flags(struct compiling
*c
, PySymtableEntryObject
*ste
,
4638 struct symbol_info
*si
)
4641 c
->c_flags
|= c
->c_future
->ff_features
;
4642 if (ste
->ste_generator
)
4643 c
->c_flags
|= CO_GENERATOR
;
4644 if (ste
->ste_type
!= TYPE_MODULE
)
4645 c
->c_flags
|= CO_NEWLOCALS
;
4646 if (ste
->ste_type
== TYPE_FUNCTION
) {
4647 c
->c_nlocals
= si
->si_nlocals
;
4648 if (ste
->ste_optimized
== 0)
4649 c
->c_flags
|= CO_OPTIMIZED
;
4650 else if (ste
->ste_optimized
!= OPT_EXEC
)
4651 return symtable_check_unoptimized(c
, ste
, si
);
4657 symtable_load_symbols(struct compiling
*c
)
4659 static PyObject
*implicit
= NULL
;
4660 struct symtable
*st
= c
->c_symtable
;
4661 PySymtableEntryObject
*ste
= st
->st_cur
;
4662 PyObject
*name
, *varnames
, *v
;
4664 struct symbol_info si
;
4666 if (implicit
== NULL
) {
4667 implicit
= PyInt_FromLong(1);
4668 if (implicit
== NULL
)
4673 if (symtable_init_compiling_symbols(c
) < 0)
4675 symtable_init_info(&si
);
4676 varnames
= st
->st_cur
->ste_varnames
;
4677 si
.si_nlocals
= PyList_GET_SIZE(varnames
);
4678 c
->c_argcount
= si
.si_nlocals
;
4680 for (i
= 0; i
< si
.si_nlocals
; ++i
) {
4681 v
= PyInt_FromLong(i
);
4682 if (PyDict_SetItem(c
->c_locals
,
4683 PyList_GET_ITEM(varnames
, i
), v
) < 0)
4688 /* XXX The cases below define the rules for whether a name is
4689 local or global. The logic could probably be clearer. */
4691 while (PyDict_Next(ste
->ste_symbols
, &pos
, &name
, &v
)) {
4692 flags
= PyInt_AS_LONG(v
);
4694 if (flags
& DEF_FREE_GLOBAL
)
4695 /* undo the original DEF_FREE */
4696 flags
&= ~(DEF_FREE
| DEF_FREE_CLASS
);
4698 /* Deal with names that need two actions:
4699 1. Cell variables that are also locals.
4700 2. Free variables in methods that are also class
4701 variables or declared global.
4703 if (flags
& (DEF_FREE
| DEF_FREE_CLASS
))
4704 symtable_resolve_free(c
, name
, flags
, &si
);
4706 if (flags
& DEF_STAR
) {
4708 c
->c_flags
|= CO_VARARGS
;
4709 } else if (flags
& DEF_DOUBLESTAR
) {
4711 c
->c_flags
|= CO_VARKEYWORDS
;
4712 } else if (flags
& DEF_INTUPLE
)
4714 else if (flags
& DEF_GLOBAL
) {
4715 if (flags
& DEF_PARAM
) {
4716 PyErr_Format(PyExc_SyntaxError
, LOCAL_GLOBAL
,
4717 PyString_AS_STRING(name
));
4718 PyErr_SyntaxLocation(st
->st_filename
,
4723 if (PyDict_SetItem(c
->c_globals
, name
, Py_None
) < 0)
4725 } else if (flags
& DEF_FREE_GLOBAL
) {
4727 if (PyDict_SetItem(c
->c_globals
, name
, implicit
) < 0)
4729 } else if ((flags
& DEF_LOCAL
) && !(flags
& DEF_PARAM
)) {
4730 v
= PyInt_FromLong(si
.si_nlocals
++);
4733 if (PyDict_SetItem(c
->c_locals
, name
, v
) < 0)
4736 if (ste
->ste_type
!= TYPE_CLASS
)
4737 if (PyList_Append(c
->c_varnames
, name
) < 0)
4739 } else if (is_free(flags
)) {
4740 if (ste
->ste_nested
) {
4741 v
= PyInt_FromLong(si
.si_nfrees
++);
4744 if (PyDict_SetItem(c
->c_freevars
, name
, v
) < 0)
4749 if (PyDict_SetItem(c
->c_globals
, name
,
4752 if (st
->st_nscopes
!= 1) {
4753 v
= PyInt_FromLong(flags
);
4754 if (PyDict_SetItem(st
->st_global
,
4763 assert(PyDict_Size(c
->c_freevars
) == si
.si_nfrees
);
4765 if (si
.si_ncells
> 1) { /* one cell is always in order */
4766 if (symtable_cellvar_offsets(&c
->c_cellvars
, c
->c_argcount
,
4767 c
->c_varnames
, c
->c_flags
) < 0)
4770 if (symtable_freevar_offsets(c
->c_freevars
, si
.si_ncells
) < 0)
4772 return symtable_update_flags(c
, ste
, &si
);
4774 /* is this always the right thing to do? */
4779 static struct symtable
*
4782 struct symtable
*st
;
4784 st
= (struct symtable
*)PyObject_MALLOC(sizeof(struct symtable
));
4789 st
->st_filename
= NULL
;
4790 if ((st
->st_stack
= PyList_New(0)) == NULL
)
4792 if ((st
->st_symbols
= PyDict_New()) == NULL
)
4798 st
->st_private
= NULL
;
4801 PySymtable_Free(st
);
4806 PySymtable_Free(struct symtable
*st
)
4808 Py_XDECREF(st
->st_symbols
);
4809 Py_XDECREF(st
->st_stack
);
4810 Py_XDECREF(st
->st_cur
);
4811 PyObject_FREE((void *)st
);
4814 /* When the compiler exits a scope, it must should update the scope's
4815 free variable information with the list of free variables in its
4818 Variables that are free in children and defined in the current
4821 If the scope being exited is defined at the top-level (ste_nested is
4822 false), free variables in children that are not defined here are
4828 symtable_update_free_vars(struct symtable
*st
)
4831 PyObject
*o
, *name
, *list
= NULL
;
4832 PySymtableEntryObject
*child
, *ste
= st
->st_cur
;
4834 if (ste
->ste_type
== TYPE_CLASS
)
4835 def
= DEF_FREE_CLASS
;
4838 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4842 PyList_SetSlice(list
, 0,
4843 ((PyVarObject
*)list
)->ob_size
, 0);
4844 child
= (PySymtableEntryObject
*)
4845 PyList_GET_ITEM(ste
->ste_children
, i
);
4846 while (PyDict_Next(child
->ste_symbols
, &pos
, &name
, &o
)) {
4847 int flags
= PyInt_AS_LONG(o
);
4848 if (!(is_free(flags
)))
4849 continue; /* avoids indentation */
4851 list
= PyList_New(0);
4855 ste
->ste_child_free
= 1;
4856 if (PyList_Append(list
, name
) < 0) {
4861 for (j
= 0; list
&& j
< PyList_GET_SIZE(list
); j
++) {
4863 name
= PyList_GET_ITEM(list
, j
);
4864 v
= PyDict_GetItem(ste
->ste_symbols
, name
);
4865 /* If a name N is declared global in scope A and
4866 referenced in scope B contained (perhaps
4867 indirectly) in A and there are no scopes
4868 with bindings for N between B and A, then N
4869 is global in B. Unless A is a class scope,
4870 because class scopes are not considered for
4873 if (v
&& (ste
->ste_type
!= TYPE_CLASS
)) {
4874 int flags
= PyInt_AS_LONG(v
);
4875 if (flags
& DEF_GLOBAL
) {
4876 symtable_undo_free(st
, child
->ste_id
,
4881 if (ste
->ste_nested
) {
4882 if (symtable_add_def_o(st
, ste
->ste_symbols
,
4888 if (symtable_check_global(st
, child
->ste_id
,
4901 /* If the current scope is a non-nested class or if name is not
4902 defined in the current, non-nested scope, then it is an implicit
4903 global in all nested scopes.
4907 symtable_check_global(struct symtable
*st
, PyObject
*child
, PyObject
*name
)
4911 PySymtableEntryObject
*ste
= st
->st_cur
;
4913 if (ste
->ste_type
== TYPE_CLASS
)
4914 return symtable_undo_free(st
, child
, name
);
4915 o
= PyDict_GetItem(ste
->ste_symbols
, name
);
4917 return symtable_undo_free(st
, child
, name
);
4918 v
= PyInt_AS_LONG(o
);
4920 if (is_free(v
) || (v
& DEF_GLOBAL
))
4921 return symtable_undo_free(st
, child
, name
);
4923 return symtable_add_def_o(st
, ste
->ste_symbols
,
4928 symtable_undo_free(struct symtable
*st
, PyObject
*id
,
4933 PySymtableEntryObject
*ste
;
4935 ste
= (PySymtableEntryObject
*)PyDict_GetItem(st
->st_symbols
, id
);
4939 info
= PyDict_GetItem(ste
->ste_symbols
, name
);
4942 v
= PyInt_AS_LONG(info
);
4944 if (symtable_add_def_o(st
, ste
->ste_symbols
, name
,
4945 DEF_FREE_GLOBAL
) < 0)
4948 /* If the name is defined here or declared global,
4949 then the recursion stops. */
4952 for (i
= 0; i
< PyList_GET_SIZE(ste
->ste_children
); ++i
) {
4953 PySymtableEntryObject
*child
;
4954 child
= (PySymtableEntryObject
*)
4955 PyList_GET_ITEM(ste
->ste_children
, i
);
4956 x
= symtable_undo_free(st
, child
->ste_id
, name
);
4963 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4964 This reference is released when the scope is exited, via the DECREF
4965 in symtable_exit_scope().
4969 symtable_exit_scope(struct symtable
*st
)
4973 if (st
->st_pass
== 1)
4974 symtable_update_free_vars(st
);
4975 Py_DECREF(st
->st_cur
);
4976 end
= PyList_GET_SIZE(st
->st_stack
) - 1;
4977 st
->st_cur
= (PySymtableEntryObject
*)PyList_GET_ITEM(st
->st_stack
,
4979 if (PySequence_DelItem(st
->st_stack
, end
) < 0)
4985 symtable_enter_scope(struct symtable
*st
, char *name
, int type
,
4988 PySymtableEntryObject
*prev
= NULL
;
4992 if (PyList_Append(st
->st_stack
, (PyObject
*)st
->st_cur
) < 0) {
4993 Py_DECREF(st
->st_cur
);
4998 st
->st_cur
= (PySymtableEntryObject
*)
4999 PySymtableEntry_New(st
, name
, type
, lineno
);
5000 if (strcmp(name
, TOP
) == 0)
5001 st
->st_global
= st
->st_cur
->ste_symbols
;
5002 if (prev
&& st
->st_pass
== 1) {
5003 if (PyList_Append(prev
->ste_children
,
5004 (PyObject
*)st
->st_cur
) < 0)
5010 symtable_lookup(struct symtable
*st
, char *name
)
5012 char buffer
[MANGLE_LEN
];
5016 if (_Py_Mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
5018 v
= PyDict_GetItemString(st
->st_cur
->ste_symbols
, name
);
5020 if (PyErr_Occurred())
5026 flags
= PyInt_AS_LONG(v
);
5031 symtable_add_def(struct symtable
*st
, char *name
, int flag
)
5034 char buffer
[MANGLE_LEN
];
5037 /* Warn about None, except inside a tuple (where the assignment
5038 code already issues a warning). */
5039 if ((flag
& DEF_PARAM
) && !(flag
& DEF_INTUPLE
) &&
5040 *name
== 'N' && strcmp(name
, "None") == 0)
5042 if (symtable_warn(st
, "argument named None"))
5045 if (_Py_Mangle(st
->st_private
, name
, buffer
, sizeof(buffer
)))
5047 if ((s
= PyString_InternFromString(name
)) == NULL
)
5049 ret
= symtable_add_def_o(st
, st
->st_cur
->ste_symbols
, s
, flag
);
5054 /* Must only be called with mangled names */
5057 symtable_add_def_o(struct symtable
*st
, PyObject
*dict
,
5058 PyObject
*name
, int flag
)
5063 if ((o
= PyDict_GetItem(dict
, name
))) {
5064 val
= PyInt_AS_LONG(o
);
5065 if ((flag
& DEF_PARAM
) && (val
& DEF_PARAM
)) {
5066 PyErr_Format(PyExc_SyntaxError
, DUPLICATE_ARGUMENT
,
5067 PyString_AsString(name
));
5068 PyErr_SyntaxLocation(st
->st_filename
,
5069 st
->st_cur
->ste_lineno
);
5075 o
= PyInt_FromLong(val
);
5076 if (PyDict_SetItem(dict
, name
, o
) < 0) {
5082 if (flag
& DEF_PARAM
) {
5083 if (PyList_Append(st
->st_cur
->ste_varnames
, name
) < 0)
5085 } else if (flag
& DEF_GLOBAL
) {
5086 /* XXX need to update DEF_GLOBAL for other flags too;
5087 perhaps only DEF_FREE_GLOBAL */
5088 if ((o
= PyDict_GetItem(st
->st_global
, name
))) {
5089 val
= PyInt_AS_LONG(o
);
5093 o
= PyInt_FromLong(val
);
5094 if (PyDict_SetItem(st
->st_global
, name
, o
) < 0) {
5103 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
5105 /* Look for a yield stmt under n. Return 1 if found, else 0.
5106 This hack is used to look inside "if 0:" blocks (which are normally
5107 ignored) in case those are the only places a yield occurs (so that this
5108 function is a generator). */
5110 look_for_yield(node
*n
)
5114 for (i
= 0; i
< NCH(n
); ++i
) {
5115 node
*kid
= CHILD(n
, i
);
5117 switch (TYPE(kid
)) {
5122 /* Stuff in nested functions and classes can't make
5123 the parent a generator. */
5130 if (look_for_yield(kid
))
5138 symtable_node(struct symtable
*st
, node
*n
)
5145 char *func_name
= STR(CHILD(n
, 1));
5146 symtable_add_def(st
, func_name
, DEF_LOCAL
);
5147 symtable_default_args(st
, CHILD(n
, 2));
5148 symtable_enter_scope(st
, func_name
, TYPE(n
), n
->n_lineno
);
5149 symtable_funcdef(st
, n
);
5150 symtable_exit_scope(st
);
5155 symtable_default_args(st
, CHILD(n
, 1));
5156 symtable_enter_scope(st
, "lambda", TYPE(n
), n
->n_lineno
);
5157 symtable_funcdef(st
, n
);
5158 symtable_exit_scope(st
);
5161 char *tmp
, *class_name
= STR(CHILD(n
, 1));
5162 symtable_add_def(st
, class_name
, DEF_LOCAL
);
5163 if (TYPE(CHILD(n
, 2)) == LPAR
) {
5164 node
*bases
= CHILD(n
, 3);
5166 for (i
= 0; i
< NCH(bases
); i
+= 2) {
5167 symtable_node(st
, CHILD(bases
, i
));
5170 symtable_enter_scope(st
, class_name
, TYPE(n
), n
->n_lineno
);
5171 tmp
= st
->st_private
;
5172 st
->st_private
= class_name
;
5173 symtable_node(st
, CHILD(n
, NCH(n
) - 1));
5174 st
->st_private
= tmp
;
5175 symtable_exit_scope(st
);
5179 for (i
= 0; i
+ 3 < NCH(n
); i
+= 4) {
5180 if (is_constant_false(NULL
, (CHILD(n
, i
+ 1)))) {
5181 if (st
->st_cur
->ste_generator
== 0)
5182 st
->st_cur
->ste_generator
=
5183 look_for_yield(CHILD(n
, i
+3));
5186 symtable_node(st
, CHILD(n
, i
+ 1));
5187 symtable_node(st
, CHILD(n
, i
+ 3));
5190 symtable_node(st
, CHILD(n
, i
+ 2));
5193 symtable_global(st
, n
);
5196 symtable_import(st
, n
);
5199 st
->st_cur
->ste_optimized
|= OPT_EXEC
;
5200 symtable_node(st
, CHILD(n
, 1));
5202 symtable_node(st
, CHILD(n
, 3));
5204 st
->st_cur
->ste_optimized
|= OPT_BARE_EXEC
;
5205 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
5208 symtable_node(st
, CHILD(n
, 5));
5213 if (Py_OptimizeFlag
)
5219 symtable_node(st
, CHILD(n
, 1));
5225 symtable_assign(st
, CHILD(n
, 3), 0);
5232 symtable_assign(st
, CHILD(n
, 1), 0);
5235 st
->st_cur
->ste_generator
= 1;
5242 if (TYPE(CHILD(n
, 1)) == augassign
) {
5243 symtable_assign(st
, CHILD(n
, 0), 0);
5244 symtable_node(st
, CHILD(n
, 2));
5248 for (i
= 0; i
< NCH(n
) - 2; i
+= 2)
5249 symtable_assign(st
, CHILD(n
, i
), 0);
5250 n
= CHILD(n
, NCH(n
) - 1);
5256 if (TYPE(n
) == list_for
) {
5258 symtable_list_comprehension(st
, n
);
5262 symtable_node(st
, CHILD(n
, 1));
5270 symtable_assign(st
, CHILD(n
, 1), 0);
5271 for (i
= 3; i
< NCH(n
); ++i
)
5272 if (TYPE(CHILD(n
, i
)) >= single_input
)
5273 symtable_node(st
, CHILD(n
, i
));
5275 /* The remaining cases fall through to default except in
5276 special circumstances. This requires the individual cases
5277 to be coded with great care, even though they look like
5278 rather innocuous. Each case must double-check TYPE(n).
5281 if (TYPE(n
) == argument
&& NCH(n
) == 3) {
5287 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
5289 symtable_list_comprehension(st
, CHILD(n
, 1));
5290 symtable_node(st
, CHILD(n
, 0));
5296 if (TYPE(n
) == atom
&& TYPE(CHILD(n
, 0)) == NAME
) {
5297 symtable_add_use(st
, STR(CHILD(n
, 0)));
5302 /* Walk over every non-token child with a special case
5309 for (i
= 0; i
< NCH(n
); ++i
)
5310 if (TYPE(CHILD(n
, i
)) >= single_input
)
5311 symtable_node(st
, CHILD(n
, i
));
5316 symtable_funcdef(struct symtable
*st
, node
*n
)
5320 if (TYPE(n
) == lambdef
) {
5322 symtable_params(st
, CHILD(n
, 1));
5324 symtable_params(st
, CHILD(n
, 2));
5325 body
= CHILD(n
, NCH(n
) - 1);
5326 symtable_node(st
, body
);
5329 /* The next two functions parse the argument tuple.
5330 symtable_default_args() checks for names in the default arguments,
5331 which are references in the defining scope. symtable_params()
5332 parses the parameter names, which are defined in the function's
5336 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5337 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5341 symtable_default_args(struct symtable
*st
, node
*n
)
5346 if (TYPE(n
) == parameters
) {
5348 if (TYPE(n
) == RPAR
)
5351 REQ(n
, varargslist
);
5352 for (i
= 0; i
< NCH(n
); i
+= 2) {
5354 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
5357 if (i
> 0 && (TYPE(CHILD(n
, i
- 1)) == EQUAL
))
5358 symtable_node(st
, CHILD(n
, i
));
5363 symtable_params(struct symtable
*st
, node
*n
)
5365 int i
, complex = -1, ext
= 0;
5368 if (TYPE(n
) == parameters
) {
5370 if (TYPE(n
) == RPAR
)
5373 REQ(n
, varargslist
);
5374 for (i
= 0; i
< NCH(n
); i
+= 2) {
5376 if (TYPE(c
) == STAR
|| TYPE(c
) == DOUBLESTAR
) {
5380 if (TYPE(c
) == test
) {
5383 if (TYPE(CHILD(c
, 0)) == NAME
)
5384 symtable_add_def(st
, STR(CHILD(c
, 0)), DEF_PARAM
);
5387 PyOS_snprintf(nbuf
, sizeof(nbuf
), ".%d", i
);
5388 symtable_add_def(st
, nbuf
, DEF_PARAM
);
5394 if (TYPE(c
) == STAR
) {
5396 symtable_add_def(st
, STR(CHILD(n
, i
)),
5397 DEF_PARAM
| DEF_STAR
);
5404 if (c
&& TYPE(c
) == DOUBLESTAR
) {
5406 symtable_add_def(st
, STR(CHILD(n
, i
)),
5407 DEF_PARAM
| DEF_DOUBLESTAR
);
5412 for (j
= 0; j
<= complex; j
++) {
5414 if (TYPE(c
) == COMMA
)
5416 else if (TYPE(c
) == EQUAL
)
5417 c
= CHILD(n
, j
+= 3);
5418 if (TYPE(CHILD(c
, 0)) == LPAR
)
5419 symtable_params_fplist(st
, CHILD(c
, 1));
5425 symtable_params_fplist(struct symtable
*st
, node
*n
)
5431 for (i
= 0; i
< NCH(n
); i
+= 2) {
5435 symtable_add_def(st
, STR(CHILD(c
, 0)),
5436 DEF_PARAM
| DEF_INTUPLE
);
5438 symtable_params_fplist(st
, CHILD(c
, 1));
5444 symtable_global(struct symtable
*st
, node
*n
)
5448 /* XXX It might be helpful to warn about module-level global
5449 statements, but it's hard to tell the difference between
5450 module-level and a string passed to exec.
5453 for (i
= 1; i
< NCH(n
); i
+= 2) {
5454 char *name
= STR(CHILD(n
, i
));
5457 flags
= symtable_lookup(st
, name
);
5460 if (flags
&& flags
!= DEF_GLOBAL
) {
5462 if (flags
& DEF_PARAM
) {
5463 PyErr_Format(PyExc_SyntaxError
,
5464 "name '%.400s' is local and global",
5466 PyErr_SyntaxLocation(st
->st_filename
,
5467 st
->st_cur
->ste_lineno
);
5472 if (flags
& DEF_LOCAL
)
5473 PyOS_snprintf(buf
, sizeof(buf
),
5474 GLOBAL_AFTER_ASSIGN
,
5477 PyOS_snprintf(buf
, sizeof(buf
),
5478 GLOBAL_AFTER_USE
, name
);
5479 symtable_warn(st
, buf
);
5482 symtable_add_def(st
, name
, DEF_GLOBAL
);
5487 symtable_list_comprehension(struct symtable
*st
, node
*n
)
5491 PyOS_snprintf(tmpname
, sizeof(tmpname
), "_[%d]", st
->st_tmpname
);
5492 symtable_add_def(st
, tmpname
, DEF_LOCAL
);
5493 symtable_assign(st
, CHILD(n
, 1), 0);
5494 symtable_node(st
, CHILD(n
, 3));
5496 symtable_node(st
, CHILD(n
, 4));
5500 symtable_import(struct symtable
*st
, node
*n
)
5503 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
5504 | 'from' dotted_name 'import'
5505 ('*' | import_as_name (',' import_as_name)*)
5506 import_as_name: NAME [NAME NAME]
5508 if (STR(CHILD(n
, 0))[0] == 'f') { /* from */
5509 node
*dotname
= CHILD(n
, 1);
5510 if (strcmp(STR(CHILD(dotname
, 0)), "__future__") == 0) {
5511 /* check for bogus imports */
5512 if (n
->n_lineno
>= st
->st_future
->ff_last_lineno
) {
5513 PyErr_SetString(PyExc_SyntaxError
,
5515 PyErr_SyntaxLocation(st
->st_filename
,
5521 if (TYPE(CHILD(n
, 3)) == STAR
) {
5522 if (st
->st_cur
->ste_type
!= TYPE_MODULE
) {
5523 if (symtable_warn(st
,
5524 "import * only allowed at module level") < 0)
5527 st
->st_cur
->ste_optimized
|= OPT_IMPORT_STAR
;
5528 st
->st_cur
->ste_opt_lineno
= n
->n_lineno
;
5530 for (i
= 3; i
< NCH(n
); i
+= 2) {
5531 node
*c
= CHILD(n
, i
);
5532 if (NCH(c
) > 1) /* import as */
5533 symtable_assign(st
, CHILD(c
, 2),
5536 symtable_assign(st
, CHILD(c
, 0),
5541 for (i
= 1; i
< NCH(n
); i
+= 2) {
5542 symtable_assign(st
, CHILD(n
, i
), DEF_IMPORT
);
5547 /* The third argument to symatble_assign() is a flag to be passed to
5548 symtable_add_def() if it is eventually called. The flag is useful
5549 to specify the particular type of assignment that should be
5550 recorded, e.g. an assignment caused by import.
5554 symtable_assign(struct symtable
*st
, node
*n
, int def_flag
)
5562 /* invalid assignment, e.g. lambda x:x=2. The next
5563 pass will catch this error. */
5567 for (i
= 2; i
< NCH(n
); ++i
)
5568 if (TYPE(CHILD(n
, i
)) != DOUBLESTAR
)
5569 symtable_node(st
, CHILD(n
, i
));
5572 symtable_node(st
, CHILD(n
, 0));
5573 symtable_node(st
, CHILD(n
, 1));
5580 if (NCH(n
) > 1 && TYPE(CHILD(n
, 1)) == list_for
) {
5581 /* XXX This is an error, but the next pass
5585 for (i
= 0; i
< NCH(n
); i
+= 2)
5586 symtable_assign(st
, CHILD(n
, i
), def_flag
);
5598 for (i
= 0; i
< NCH(n
); i
+= 2)
5599 symtable_assign(st
, CHILD(n
, i
), def_flag
);
5604 if (TYPE(tmp
) == LPAR
|| TYPE(tmp
) == LSQB
) {
5607 } else if (TYPE(tmp
) == NAME
) {
5608 if (strcmp(STR(tmp
), "__debug__") == 0) {
5609 PyErr_SetString(PyExc_SyntaxError
,
5611 PyErr_SyntaxLocation(st
->st_filename
,
5615 symtable_add_def(st
, STR(tmp
), DEF_LOCAL
| def_flag
);
5618 case dotted_as_name
:
5620 symtable_add_def(st
, STR(CHILD(n
, 2)),
5621 DEF_LOCAL
| def_flag
);
5623 symtable_add_def(st
,
5626 DEF_LOCAL
| def_flag
);
5629 symtable_add_def(st
, STR(CHILD(n
, 0)), DEF_LOCAL
| def_flag
);
5632 symtable_add_def(st
, STR(n
), DEF_LOCAL
| def_flag
);
5641 /* Should only occur for errors like x + 1 = 1,
5642 which will be caught in the next pass. */
5643 for (i
= 0; i
< NCH(n
); ++i
)
5644 if (TYPE(CHILD(n
, i
)) >= single_input
)
5645 symtable_assign(st
, CHILD(n
, i
), def_flag
);