blob: 79756eab0ec0881285c8c737ddf1fdfc95344f92 [file] [log] [blame]
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001/* Compile an expression node to intermediate code */
2
Guido van Rossum3f5da241990-12-20 15:06:42 +00003/* XXX TO DO:
Guido van Rossum8b993a91997-01-17 21:04:03 +00004 XXX add __doc__ attribute == co_doc to code object attributes?
5 XXX (it's currently the first item of the co_const tuple)
Guido van Rossum3f5da241990-12-20 15:06:42 +00006 XXX Generate simple jump for break/return outside 'try...finally'
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00007 XXX Allow 'continue' inside finally clause of try-finally
Guido van Rossum8b993a91997-01-17 21:04:03 +00008 XXX New opcode for loading the initial index for a for loop
Guido van Rossum681d79a1995-07-18 14:51:37 +00009 XXX other JAR tricks?
Guido van Rossum3f5da241990-12-20 15:06:42 +000010*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +000011
Guido van Rossum79f25d91997-04-29 20:08:16 +000012#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014#include "node.h"
15#include "token.h"
16#include "graminit.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000018#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019#include "opcode.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000020#include "structmember.h"
21
22#include <ctype.h>
Guido van Rossum282914b1991-04-04 10:42:56 +000023
Guido van Rossumb05a5c71997-05-07 17:46:13 +000024/* 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
Guido van Rossum2f75b291997-05-20 22:18:48 +000029 #error "single_input has changed -- update Py_single_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000030#endif
31#if Py_file_input != file_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000032 #error "file_input has changed -- update Py_file_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000033#endif
34#if Py_eval_input != eval_input
Guido van Rossum2f75b291997-05-20 22:18:48 +000035 #error "eval_input has changed -- update Py_eval_input in Python.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000036#endif
37
Guido van Rossum8e793d91997-03-03 19:13:14 +000038int Py_OptimizeFlag = 0;
39
Guido van Rossum8861b741996-07-30 16:49:37 +000040#define OP_DELETE 0
41#define OP_ASSIGN 1
42#define OP_APPLY 2
43
Jeremy Hyltone36f7782001-01-19 03:21:30 +000044#define VAR_LOAD 0
45#define VAR_STORE 1
46#define VAR_DELETE 2
47
Jeremy Hylton64949cb2001-01-25 20:06:59 +000048#define DEL_CLOSURE_ERROR \
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000049"can not delete variable '%.400s' referenced in nested scope"
50
51#define DUPLICATE_ARGUMENT \
52"duplicate argument '%s' in function definition"
53
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054#define ILLEGAL_DYNAMIC_SCOPE \
55"%.100s: exec or 'import *' makes names ambiguous in nested scope"
Jeremy Hyltoneab156f2001-01-30 01:24:43 +000056
Jeremy Hylton29906ee2001-02-27 04:23:34 +000057#define GLOBAL_AFTER_ASSIGN \
58"name '%.400s' is assigned to before global declaration"
59
60#define GLOBAL_AFTER_USE \
61"name '%.400s' is used prior to global declaration"
62
63#define LOCAL_GLOBAL \
Neal Norwitz2a47c0f2002-01-29 00:53:41 +000064"name '%.400s' is a function parameter and declared global"
Jeremy Hylton29906ee2001-02-27 04:23:34 +000065
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +000066#define LATE_FUTURE \
67"from __future__ imports must occur at the beginning of the file"
68
Jeremy Hylton897b8212001-03-23 14:08:38 +000069#define ASSIGN_DEBUG \
70"can not assign to __debug__"
71
Jeremy Hyltone36f7782001-01-19 03:21:30 +000072#define MANGLE_LEN 256
73
Guido van Rossum79f25d91997-04-29 20:08:16 +000074#define OFF(x) offsetof(PyCodeObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossum6f799372001-09-20 20:46:19 +000076static PyMemberDef code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000077 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000079 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000080 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000081 {"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},
Guido van Rossum681d79a1995-07-18 14:51:37 +000084 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Jeremy Hylton64949cb2001-01-25 20:06:59 +000085 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000087 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000088 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000089 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 {NULL} /* Sentinel */
92};
93
Guido van Rossumbea18cc2002-06-14 20:41:17 +000094PyDoc_STRVAR(code_doc,
95"code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
96 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
97\n\
98Create a code object. Not for the faint of heart.");
99
100static PyObject *
101code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
102{
103 int argcount;
104 int nlocals;
105 int stacksize;
106 int flags;
107 PyObject *code;
108 PyObject *consts;
109 PyObject *names;
110 PyObject *varnames;
111 PyObject *freevars = NULL;
112 PyObject *cellvars = NULL;
113 PyObject *filename;
114 PyObject *name;
115 int firstlineno;
116 PyObject *lnotab;
117
118 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
119 &argcount, &nlocals, &stacksize, &flags,
120 &code,
121 &PyTuple_Type, &consts,
122 &PyTuple_Type, &names,
123 &PyTuple_Type, &varnames,
124 &filename, &name,
125 &firstlineno, &lnotab,
126 &PyTuple_Type, &freevars,
127 &PyTuple_Type, &cellvars))
128 return NULL;
129
130 if (freevars == NULL || cellvars == NULL) {
131 PyObject *empty = PyTuple_New(0);
132 if (empty == NULL)
133 return NULL;
134 if (freevars == NULL) {
135 freevars = empty;
136 Py_INCREF(freevars);
137 }
138 if (cellvars == NULL) {
139 cellvars = empty;
140 Py_INCREF(cellvars);
141 }
142 Py_DECREF(empty);
143 }
144
145 if (!PyObject_CheckReadBuffer(code)) {
146 PyErr_SetString(PyExc_TypeError,
147 "bytecode object must be a single-segment read-only buffer");
148 return NULL;
149 }
150
151 return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
152 code, consts, names, varnames,
153 freevars, cellvars, filename, name,
154 firstlineno, lnotab);
155}
156
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000157static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000158code_dealloc(PyCodeObject *co)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000159{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000160 Py_XDECREF(co->co_code);
161 Py_XDECREF(co->co_consts);
162 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +0000163 Py_XDECREF(co->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000164 Py_XDECREF(co->co_freevars);
165 Py_XDECREF(co->co_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000166 Py_XDECREF(co->co_filename);
167 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +0000168 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000169 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000170}
171
Guido van Rossum79f25d91997-04-29 20:08:16 +0000172static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000173code_repr(PyCodeObject *co)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000174{
175 char buf[500];
176 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000177 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000178 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +0000179
Guido van Rossuma396a882000-04-07 01:21:36 +0000180 if (co->co_firstlineno != 0)
181 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000182 if (co->co_filename && PyString_Check(co->co_filename))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000183 filename = PyString_AS_STRING(co->co_filename);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 if (co->co_name && PyString_Check(co->co_name))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000185 name = PyString_AS_STRING(co->co_name);
Barry Warsaw8f6d8682001-11-28 21:10:39 +0000186 PyOS_snprintf(buf, sizeof(buf),
187 "<code object %.100s at %p, file \"%.300s\", line %d>",
188 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000189 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000190}
191
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000192static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000193code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000194{
195 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000196 cmp = PyObject_Compare(co->co_name, cp->co_name);
197 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000198 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000199 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000200 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000201 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000202 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000203 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000204 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000205 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000206 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000207 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000208 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000209 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000210 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000211 if (cmp) return cmp;
212 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
213 if (cmp) return cmp;
214 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000215 return cmp;
216}
217
218static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000219code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000220{
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000221 long h, h0, h1, h2, h3, h4, h5, h6;
Guido van Rossum44679592000-04-10 16:20:31 +0000222 h0 = PyObject_Hash(co->co_name);
223 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000224 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000225 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000226 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000227 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000228 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000229 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000230 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000231 if (h4 == -1) return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000232 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 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000237 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000238 if (h == -1) h = -2;
239 return h;
240}
241
Jeremy Hylton78891072001-03-01 06:09:34 +0000242/* XXX code objects need to participate in GC? */
243
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244PyTypeObject PyCode_Type = {
245 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000246 0,
247 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000248 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000249 0,
Jeremy Hyltonc785f482001-09-14 20:08:07 +0000250 (destructor)code_dealloc, /* tp_dealloc */
251 0, /* tp_print */
252 0, /* tp_getattr */
253 0, /* tp_setattr */
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 */
260 0, /* tp_call */
261 0, /* tp_str */
262 PyObject_GenericGetAttr, /* tp_getattro */
263 0, /* tp_setattro */
264 0, /* tp_as_buffer */
265 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000266 code_doc, /* tp_doc */
Jeremy Hyltonc785f482001-09-14 20:08:07 +0000267 0, /* tp_traverse */
268 0, /* tp_clear */
269 0, /* tp_richcompare */
270 0, /* tp_weaklistoffset */
271 0, /* tp_iter */
272 0, /* tp_iternext */
273 0, /* tp_methods */
274 code_memberlist, /* tp_members */
275 0, /* tp_getset */
276 0, /* tp_base */
277 0, /* tp_dict */
278 0, /* tp_descr_get */
279 0, /* tp_descr_set */
280 0, /* tp_dictoffset */
281 0, /* tp_init */
282 0, /* tp_alloc */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000283 code_new, /* tp_new */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000284};
285
Guido van Rossum644a12b1997-04-09 19:24:53 +0000286#define NAME_CHARS \
287 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
288
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000289/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
290
291static int
292all_name_chars(unsigned char *s)
293{
Guido van Rossumcd90c202001-02-09 15:06:42 +0000294 static char ok_name_char[256];
295 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000296
Guido van Rossumcd90c202001-02-09 15:06:42 +0000297 if (ok_name_char[*name_chars] == 0) {
298 unsigned char *p;
299 for (p = name_chars; *p; p++)
300 ok_name_char[*p] = 1;
301 }
302 while (*s) {
303 if (ok_name_char[*s++] == 0)
304 return 0;
305 }
306 return 1;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000307}
308
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000309static int
310intern_strings(PyObject *tuple)
311{
312 int i;
313
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();
319 return -1;
320 }
321 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
322 }
323 return 0;
324}
325
Guido van Rossum79f25d91997-04-29 20:08:16 +0000326PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyCode_New(int argcount, int nlocals, int stacksize, int flags,
328 PyObject *code, PyObject *consts, PyObject *names,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000329 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
330 PyObject *filename, PyObject *name, int firstlineno,
331 PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000332{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000333 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000334 int i;
335 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000336 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000337 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 consts == NULL || !PyTuple_Check(consts) ||
339 names == NULL || !PyTuple_Check(names) ||
340 varnames == NULL || !PyTuple_Check(varnames) ||
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 freevars == NULL || !PyTuple_Check(freevars) ||
342 cellvars == NULL || !PyTuple_Check(cellvars) ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000343 name == NULL || !PyString_Check(name) ||
344 filename == NULL || !PyString_Check(filename) ||
Jeremy Hylton9f64caa2001-11-09 22:02:48 +0000345 lnotab == NULL || !PyString_Check(lnotab) ||
346 !PyObject_CheckReadBuffer(code)) {
Guido van Rossumd076c731998-10-07 19:42:25 +0000347 PyErr_BadInternalCall();
348 return NULL;
349 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000350 intern_strings(names);
351 intern_strings(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000352 intern_strings(freevars);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000353 intern_strings(cellvars);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000354 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000355 for (i = PyTuple_Size(consts); --i >= 0; ) {
356 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000358 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000359 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000360 continue;
361 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000362 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000363 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000364 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000365 co->co_argcount = argcount;
366 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000367 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000368 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000369 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000370 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000372 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000373 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000374 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000375 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000376 co->co_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000377 Py_INCREF(freevars);
378 co->co_freevars = freevars;
379 Py_INCREF(cellvars);
380 co->co_cellvars = cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000381 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000382 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000384 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000385 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000386 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000387 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000388 }
389 return co;
390}
391
392
393/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000394
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000395/* The compiler uses two passes to generate bytecodes. The first pass
396 builds the symbol table. The second pass generates the bytecode.
397
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.
401
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.
406*/
407
Tim Peters2a7f3842001-06-09 09:26:21 +0000408/* All about c_lnotab.
409
410c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
411mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
412to source code line #s (when needed for tracebacks) via c_lnotab instead.
413The array is conceptually a list of
414 (bytecode offset increment, line number increment)
415pairs. The details are important and delicate, best illustrated by example:
416
417 byte code offset source code line number
418 0 1
419 6 2
420 50 7
421 350 307
422 361 308
423
424The first trick is that these numbers aren't stored, only the increments
425from one row to the next (this doesn't really work, but it's a start):
426
427 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
428
429The second trick is that an unsigned byte can't hold negative values, or
430values larger than 255, so (a) there's a deep assumption that byte code
431offsets and their corresponding line #s both increase monotonically, and (b)
432if at least one column jumps by more than 255 from one row to the next, more
433than one pair is written to the table. In case #b, there's no way to know
434from looking at the table later how many were written. That's the delicate
435part. A user of c_lnotab desiring to find the source line number
436corresponding to a bytecode address A should do something like this
437
438 lineno = addr = 0
439 for addr_incr, line_incr in c_lnotab:
440 addr += addr_incr
441 if addr > A:
442 return lineno
443 lineno += line_incr
444
445In order for this to work, when the addr field increments by more than 255,
446the line # increment in each pair generated must be 0 until the remaining addr
447increment is < 256. So, in the example above, com_set_lineno should not (as
448was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
449255, 0, 45, 255, 0, 45.
450*/
451
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000452struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000453 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000454 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000455 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000457 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000458 PyObject *c_globals; /* dictionary (value=None) */
459 PyObject *c_locals; /* dictionary (value=localID) */
460 PyObject *c_varnames; /* list (inverse of c_locals) */
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000461 PyObject *c_freevars; /* dictionary (value=None) */
462 PyObject *c_cellvars; /* list */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000463 int c_nlocals; /* index of next local */
464 int c_argcount; /* number of top-level arguments */
465 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000466 int c_nexti; /* index into c_code */
467 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000468 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000469 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000470 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000471 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000472 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000473 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000474 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000475 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000476 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000477 int c_stacklevel; /* Current stack level */
478 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000479 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000480 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000481 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000482 char *c_private; /* for private name mangling */
Skip Montanaro803d6e52000-08-12 18:09:51 +0000483 int c_tmpname; /* temporary local name counter */
Guido van Rossumcd90c202001-02-09 15:06:42 +0000484 int c_nested; /* Is block nested funcdef or lamdef? */
485 int c_closure; /* Is nested w/freevars? */
486 struct symtable *c_symtable; /* pointer to module symbol table */
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000487 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000488 char *c_encoding; /* source encoding (a borrowed reference) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000489};
490
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000491static int
492is_free(int v)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000493{
494 if ((v & (USE | DEF_FREE))
495 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
496 return 1;
497 if (v & DEF_FREE_CLASS)
498 return 1;
499 return 0;
500}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000501
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000502static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000503com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000504{
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000505 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
506
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000507 if (c == NULL) {
508 /* Error occurred via symtable call to
509 is_constant_false */
510 PyErr_SetString(exc, msg);
511 return;
512 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000513 c->c_errors++;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000514 if (c->c_lineno < 1 || c->c_interactive) {
515 /* Unknown line number or interactive input */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000516 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000517 return;
518 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000519 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000520 if (v == NULL)
521 return; /* MemoryError, too bad */
Fred Drakedcf08e02000-08-15 15:49:44 +0000522
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000523 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000524 if (line == NULL) {
525 Py_INCREF(Py_None);
526 line = Py_None;
527 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000528 if (exc == PyExc_SyntaxError) {
529 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
530 Py_None, line);
531 if (t == NULL)
532 goto exit;
533 w = Py_BuildValue("(OO)", v, t);
534 if (w == NULL)
535 goto exit;
536 PyErr_SetObject(exc, w);
537 } else {
538 /* Make sure additional exceptions are printed with
539 file and line, also. */
540 PyErr_SetObject(exc, v);
541 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
542 }
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000543 exit:
544 Py_XDECREF(t);
545 Py_XDECREF(v);
546 Py_XDECREF(w);
547 Py_XDECREF(line);
Guido van Rossum452a9831996-09-17 14:32:04 +0000548}
549
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000550/* Interface to the block stack */
551
552static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000553block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000554{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000555 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000556 com_error(c, PyExc_SystemError,
557 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000558 }
559 else {
560 c->c_block[c->c_nblocks++] = type;
561 }
562}
563
564static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000566{
567 if (c->c_nblocks > 0)
568 c->c_nblocks--;
569 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000570 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000571 }
572}
573
Guido van Rossum681d79a1995-07-18 14:51:37 +0000574/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000575
Tim Petersdbd9ba62000-07-09 03:09:57 +0000576static int com_init(struct compiling *, char *);
577static void com_free(struct compiling *);
578static void com_push(struct compiling *, int);
579static void com_pop(struct compiling *, int);
580static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000581static void com_node(struct compiling *, node *);
582static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000583static void com_addbyte(struct compiling *, int);
584static void com_addint(struct compiling *, int);
585static void com_addoparg(struct compiling *, int, int);
586static void com_addfwref(struct compiling *, int, int *);
587static void com_backpatch(struct compiling *, int);
588static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
589static int com_addconst(struct compiling *, PyObject *);
590static int com_addname(struct compiling *, PyObject *);
591static void com_addopname(struct compiling *, int, node *);
592static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000593static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000594static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000595static void com_assign(struct compiling *, node *, int, node *);
596static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000597static PyCodeObject *icompile(node *, struct compiling *);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000598static PyCodeObject *jcompile(node *, char *, struct compiling *,
599 PyCompilerFlags *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000600static PyObject *parsestrplus(struct compiling*, node *);
601static PyObject *parsestr(struct compiling *, char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000602static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000603
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000604static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000605
606/* symtable operations */
607static int symtable_build(struct compiling *, node *);
608static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000609static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000610static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000611static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000612static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000613static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000614
615static void symtable_node(struct symtable *, node *);
616static void symtable_funcdef(struct symtable *, node *);
617static void symtable_default_args(struct symtable *, node *);
618static void symtable_params(struct symtable *, node *);
619static void symtable_params_fplist(struct symtable *, node *n);
620static void symtable_global(struct symtable *, node *);
621static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000622static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000623static void symtable_list_comprehension(struct symtable *, node *);
624
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000625static int symtable_update_free_vars(struct symtable *);
626static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
627static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
628
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000629/* helper */
630static void
631do_pad(int pad)
632{
633 int i;
634 for (i = 0; i < pad; ++i)
635 fprintf(stderr, " ");
636}
637
638static void
639dump(node *n, int pad, int depth)
640{
641 int i;
642 if (depth == 0)
643 return;
644 do_pad(pad);
645 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
646 if (depth > 0)
647 depth--;
648 for (i = 0; i < NCH(n); ++i)
649 dump(CHILD(n, i), pad + 1, depth);
650}
651
652#define DUMP(N) dump(N, 0, -1)
653
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000654static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000655com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000656{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000657 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000658 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
659 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000660 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000661 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000662 goto fail;
663 if ((c->c_const_dict = PyDict_New()) == NULL)
664 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000665 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000666 goto fail;
667 if ((c->c_name_dict = PyDict_New()) == NULL)
668 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000669 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000670 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000671 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
672 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000673 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000674 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000675 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000676 c->c_freevars = NULL;
677 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000678 c->c_nlocals = 0;
679 c->c_argcount = 0;
680 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000681 c->c_nexti = 0;
682 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000683 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000684 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000685 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000686 c->c_begin = 0;
687 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000688 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000689 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000690 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000691 c->c_stacklevel = 0;
692 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000693 c->c_firstlineno = 0;
694 c->c_last_addr = 0;
695 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000696 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000697 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000698 c->c_nested = 0;
699 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000700 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000701 return 1;
702
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000703 fail:
704 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000705 return 0;
706}
707
708static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000709com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000710{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000711 Py_XDECREF(c->c_code);
712 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000713 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000714 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000715 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 Py_XDECREF(c->c_globals);
717 Py_XDECREF(c->c_locals);
718 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000719 Py_XDECREF(c->c_freevars);
720 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000721 Py_XDECREF(c->c_lnotab);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000722 if (c->c_future)
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +0000723 PyObject_FREE((void *)c->c_future);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000724}
725
726static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000727com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000728{
729 c->c_stacklevel += n;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000730 if (c->c_stacklevel > c->c_maxstacklevel) {
Guido van Rossum8b993a91997-01-17 21:04:03 +0000731 c->c_maxstacklevel = c->c_stacklevel;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000732 /*
733 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
734 c->c_filename, c->c_name, c->c_lineno,
735 c->c_nexti, c->c_stacklevel, n);
736 */
737 }
Guido van Rossum8b993a91997-01-17 21:04:03 +0000738}
739
740static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000741com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000742{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000743 if (c->c_stacklevel < n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000744 c->c_stacklevel = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000745 else
746 c->c_stacklevel -= n;
747}
748
749static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000750com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000751{
752 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000753 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000754 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000755 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000756}
757
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000758static int
759com_check_size(PyObject **s, int offset)
760{
761 int len = PyString_GET_SIZE(*s);
762 if (offset >= len)
763 return _PyString_Resize(s, len * 2);
764 return 0;
765}
766
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000767static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000768com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000769{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000771 assert(byte >= 0 && byte <= 255);
Martin v. Löwis7198a522002-01-01 19:59:11 +0000772 assert(c->c_code != 0);
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000773 if (com_check_size(&c->c_code, c->c_nexti)) {
774 c->c_errors++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000775 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000776 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000777 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000778}
779
780static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000781com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000782{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000783 com_addbyte(c, x & 0xff);
784 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000785}
786
787static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000788com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000789{
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000790 char *p;
791 if (c->c_lnotab == NULL)
792 return;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000793 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
794 c->c_errors++;
795 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000796 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000797 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000798 *p++ = addr;
799 *p++ = line;
800 c->c_lnotab_next += 2;
801}
802
803static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000804com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000805{
806 c->c_lineno = lineno;
807 if (c->c_firstlineno == 0) {
808 c->c_firstlineno = c->c_last_line = lineno;
809 }
810 else {
811 int incr_addr = c->c_nexti - c->c_last_addr;
812 int incr_line = lineno - c->c_last_line;
Tim Peters2a7f3842001-06-09 09:26:21 +0000813 while (incr_addr > 255) {
814 com_add_lnotab(c, 255, 0);
815 incr_addr -= 255;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000816 }
Tim Peters2a7f3842001-06-09 09:26:21 +0000817 while (incr_line > 255) {
818 com_add_lnotab(c, incr_addr, 255);
819 incr_line -=255;
820 incr_addr = 0;
821 }
822 if (incr_addr > 0 || incr_line > 0)
823 com_add_lnotab(c, incr_addr, incr_line);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000824 c->c_last_addr = c->c_nexti;
825 c->c_last_line = lineno;
826 }
827}
828
829static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000830com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000831{
Fred Drakeef8ace32000-08-24 00:32:09 +0000832 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000833 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000834 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000835 if (Py_OptimizeFlag)
836 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000837 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000838 if (extended_arg){
839 com_addbyte(c, EXTENDED_ARG);
840 com_addint(c, extended_arg);
841 arg &= 0xffff;
842 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000843 com_addbyte(c, op);
844 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000845}
846
847static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000848com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000849{
850 /* Compile a forward reference for backpatching */
851 int here;
852 int anchor;
853 com_addbyte(c, op);
854 here = c->c_nexti;
855 anchor = *p_anchor;
856 *p_anchor = here;
857 com_addint(c, anchor == 0 ? 0 : here - anchor);
858}
859
860static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000861com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000862{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000863 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000864 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000865 int dist;
866 int prev;
867 for (;;) {
868 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000869 prev = code[anchor] + (code[anchor+1] << 8);
870 dist = target - (anchor+2);
871 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000872 dist >>= 8;
873 code[anchor+1] = dist;
874 dist >>= 8;
875 if (dist) {
876 com_error(c, PyExc_SystemError,
877 "com_backpatch: offset too large");
878 break;
879 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000880 if (!prev)
881 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000882 anchor -= prev;
883 }
884}
885
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000886/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000887
888static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000889com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000890{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000891 PyObject *w, *t, *np=NULL;
892 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000893
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000894 t = Py_BuildValue("(OO)", v, v->ob_type);
895 if (t == NULL)
896 goto fail;
897 w = PyDict_GetItem(dict, t);
898 if (w != NULL) {
899 n = PyInt_AsLong(w);
900 } else {
901 n = PyList_Size(list);
902 np = PyInt_FromLong(n);
903 if (np == NULL)
904 goto fail;
905 if (PyList_Append(list, v) != 0)
906 goto fail;
907 if (PyDict_SetItem(dict, t, np) != 0)
908 goto fail;
909 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000910 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000911 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000912 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000913 fail:
914 Py_XDECREF(np);
915 Py_XDECREF(t);
916 c->c_errors++;
917 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000918}
919
920static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000921com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000922{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000923 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000924}
925
926static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000927com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000928{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000929 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000930}
931
Raymond Hettinger0ae0c072002-06-20 22:23:15 +0000932int
933_Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000934{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000935 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000936 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000937 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000938 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
939 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000940 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000941 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000942 return 0; /* Don't mangle __extremely_long_names */
943 if (name[nlen-1] == '_' && name[nlen-2] == '_')
944 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000945 /* Strip leading underscores from class name */
946 while (*p == '_')
947 p++;
948 if (*p == '\0')
949 return 0; /* Don't mangle if class is just underscores */
950 plen = strlen(p);
951 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000952 plen = maxlen-nlen-2; /* Truncate class name if too long */
953 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000954 buffer[0] = '_';
955 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000956 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000957 return 1;
958}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000959
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000960static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000961com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000962{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000963 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000964 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000965 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000966
Raymond Hettinger0ae0c072002-06-20 22:23:15 +0000967 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000968 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000969 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000970 c->c_errors++;
971 i = 255;
972 }
973 else {
974 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000975 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000976 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000977 com_addoparg(c, op, i);
978}
979
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000980#define NAME_LOCAL 0
981#define NAME_GLOBAL 1
982#define NAME_DEFAULT 2
983#define NAME_CLOSURE 3
984
985static int
986com_lookup_arg(PyObject *dict, PyObject *name)
987{
988 PyObject *v = PyDict_GetItem(dict, name);
989 if (v == NULL)
990 return -1;
991 else
992 return PyInt_AS_LONG(v);
993}
994
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000995static void
996com_addop_varname(struct compiling *c, int kind, char *name)
997{
998 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000999 int i, reftype;
1000 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001001 int op = STOP_CODE;
1002 char buffer[MANGLE_LEN];
1003
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001004 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001005 name = buffer;
1006 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
1007 c->c_errors++;
1008 i = 255;
1009 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001010 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001011
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001012 reftype = get_ref_type(c, name);
1013 switch (reftype) {
1014 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001015 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001016 scope = NAME_LOCAL;
1017 break;
1018 case GLOBAL_EXPLICIT:
1019 scope = NAME_GLOBAL;
1020 break;
1021 case GLOBAL_IMPLICIT:
1022 if (c->c_flags & CO_OPTIMIZED)
1023 scope = NAME_GLOBAL;
1024 break;
1025 case FREE:
1026 case CELL:
1027 scope = NAME_CLOSURE;
1028 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001029 }
1030
1031 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001032 if (scope == NAME_LOCAL)
1033 i = com_lookup_arg(c->c_locals, v);
1034 else if (reftype == FREE)
1035 i = com_lookup_arg(c->c_freevars, v);
1036 else if (reftype == CELL)
1037 i = com_lookup_arg(c->c_cellvars, v);
1038 if (i == -1) {
1039 c->c_errors++; /* XXX no exception set */
1040 i = 255;
1041 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001042 }
1043 Py_DECREF(v);
1044
1045 switch (kind) {
1046 case VAR_LOAD:
1047 switch (scope) {
1048 case NAME_LOCAL:
1049 op = LOAD_FAST;
1050 break;
1051 case NAME_GLOBAL:
1052 op = LOAD_GLOBAL;
1053 break;
1054 case NAME_DEFAULT:
1055 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001056 break;
1057 case NAME_CLOSURE:
1058 op = LOAD_DEREF;
1059 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001060 }
1061 break;
1062 case VAR_STORE:
1063 switch (scope) {
1064 case NAME_LOCAL:
1065 op = STORE_FAST;
1066 break;
1067 case NAME_GLOBAL:
1068 op = STORE_GLOBAL;
1069 break;
1070 case NAME_DEFAULT:
1071 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001072 break;
1073 case NAME_CLOSURE:
1074 op = STORE_DEREF;
1075 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001076 }
1077 break;
1078 case VAR_DELETE:
1079 switch (scope) {
1080 case NAME_LOCAL:
1081 op = DELETE_FAST;
1082 break;
1083 case NAME_GLOBAL:
1084 op = DELETE_GLOBAL;
1085 break;
1086 case NAME_DEFAULT:
1087 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001088 break;
1089 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00001090 char buf[500];
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001091 PyOS_snprintf(buf, sizeof(buf),
1092 DEL_CLOSURE_ERROR, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001093 com_error(c, PyExc_SyntaxError, buf);
1094 i = 255;
1095 break;
1096 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001097 }
1098 break;
1099 }
1100done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001101 com_addoparg(c, op, i);
1102}
1103
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001104static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001105com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001106{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001107 char *name;
1108 char buffer[1000];
1109 /* XXX it is possible to write this code without the 1000
1110 chars on the total length of dotted names, I just can't be
1111 bothered right now */
1112 if (TYPE(n) == STAR)
1113 name = "*";
1114 else if (TYPE(n) == dotted_name) {
1115 char *p = buffer;
1116 int i;
1117 name = buffer;
1118 for (i = 0; i < NCH(n); i += 2) {
1119 char *s = STR(CHILD(n, i));
1120 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001122 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001123 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001124 break;
1125 }
1126 if (p != buffer)
1127 *p++ = '.';
1128 strcpy(p, s);
1129 p = strchr(p, '\0');
1130 }
1131 }
1132 else {
1133 REQ(n, NAME);
1134 name = STR(n);
1135 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001136 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001137}
1138
Guido van Rossum79f25d91997-04-29 20:08:16 +00001139static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001140parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001141{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001142 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001143 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001144 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001145#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001146 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001147 int imflag;
1148#endif
1149
Guido van Rossum282914b1991-04-04 10:42:56 +00001150 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001151 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001152#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001153 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001154#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001155 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001156 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001157 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001158 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001159 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001161 if (*end == '\0') {
Jeremy Hylton71b6af92001-08-27 19:45:25 +00001162 if (errno != 0)
1163 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001164 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001165 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001166 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001167#ifndef WITHOUT_COMPLEX
1168 if (imflag) {
1169 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001170 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001171 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001172 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001173 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001174 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001175 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001176#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001177 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001178 PyFPE_START_PROTECT("atof", return 0)
1179 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001180 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001181 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001182 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001183}
1184
Guido van Rossum79f25d91997-04-29 20:08:16 +00001185static PyObject *
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001186decode_utf8(char **sPtr, char *end, char* encoding)
1187{
Martin v. Löwis019934b2002-08-07 12:33:18 +00001188#ifndef Py_USING_UNICODE
Martin v. Löwis2863c102002-08-07 15:18:57 +00001189 Py_FatalError("decode_utf8 should not be called in this build.");
1190 return NULL;
Martin v. Löwis019934b2002-08-07 12:33:18 +00001191#else
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001192 PyObject *u, *v;
1193 char *s, *t;
1194 t = s = *sPtr;
1195 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
1196 while (s < end && (*s & 0x80)) s++;
1197 *sPtr = s;
1198 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
1199 if (u == NULL)
1200 return NULL;
1201 v = PyUnicode_AsEncodedString(u, encoding, NULL);
1202 Py_DECREF(u);
1203 return v;
Martin v. Löwis019934b2002-08-07 12:33:18 +00001204#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001205}
1206
1207static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001208parsestr(struct compiling *com, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001209{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001210 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001211 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001212 char *buf;
1213 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001214 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001215 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001216 int first = *s;
1217 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001218 int rawmode = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001219 char* encoding = ((com == NULL) ? NULL : com->c_encoding);
1220 int need_encoding;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001221 int unicode = 0;
Guido van Rossum05459c52002-05-28 18:47:29 +00001222
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001223 if (isalpha(quote) || quote == '_') {
1224 if (quote == 'u' || quote == 'U') {
1225 quote = *++s;
1226 unicode = 1;
1227 }
1228 if (quote == 'r' || quote == 'R') {
1229 quote = *++s;
1230 rawmode = 1;
1231 }
1232 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001233 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001234 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001235 return NULL;
1236 }
1237 s++;
1238 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001239 if (len > INT_MAX) {
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001240 com_error(com, PyExc_OverflowError,
1241 "string to parse is too long");
Guido van Rossum582acec2000-06-28 22:07:35 +00001242 return NULL;
1243 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001244 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001245 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001246 return NULL;
1247 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001248 if (len >= 4 && s[0] == quote && s[1] == quote) {
1249 s += 2;
1250 len -= 2;
1251 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001252 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001253 return NULL;
1254 }
1255 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001256#ifdef Py_USING_UNICODE
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001257 if (unicode || Py_UnicodeFlag) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001258 PyObject *u, *w;
1259 if (encoding == NULL) {
1260 buf = s;
1261 u = NULL;
1262 } else if (strcmp(encoding, "iso-8859-1") == 0) {
1263 buf = s;
1264 u = NULL;
1265 } else {
1266 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
1267 u = PyString_FromStringAndSize((char *)NULL, len * 4);
1268 if (u == NULL)
1269 return NULL;
1270 p = buf = PyString_AsString(u);
1271 end = s + len;
1272 while (s < end) {
1273 if (*s == '\\') {
1274 *p++ = *s++;
1275 if (*s & 0x80) {
1276 strcpy(p, "u005c");
1277 p += 5;
1278 }
1279 }
1280 if (*s & 0x80) { /* XXX inefficient */
1281 char *r;
1282 int rn, i;
1283 w = decode_utf8(&s, end, "utf-16-be");
1284 if (w == NULL) {
1285 Py_DECREF(u);
1286 return NULL;
1287 }
1288 r = PyString_AsString(w);
1289 rn = PyString_Size(w);
1290 assert(rn % 2 == 0);
1291 for (i = 0; i < rn; i += 2) {
1292 sprintf(p, "\\u%02x%02x",
1293 r[i + 0] & 0xFF,
1294 r[i + 1] & 0xFF);
1295 p += 6;
1296 }
1297 Py_DECREF(w);
1298 } else {
1299 *p++ = *s++;
1300 }
1301 }
1302 len = p - buf;
1303 }
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001304 if (rawmode)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001305 v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL);
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001306 else
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001307 v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL);
1308 Py_XDECREF(u);
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001309 if (v == NULL)
1310 PyErr_SyntaxLocation(com->c_filename, com->c_lineno);
1311 return v;
1312
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001313 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001314#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001315 need_encoding = (encoding != NULL &&
1316 strcmp(encoding, "utf-8") != 0 &&
1317 strcmp(encoding, "iso-8859-1") != 0);
1318 if (rawmode || strchr(s, '\\') == NULL) {
1319 if (need_encoding) {
Martin v. Löwis019934b2002-08-07 12:33:18 +00001320#ifndef Py_USING_UNICODE
1321 /* This should not happen - we never see any other
1322 encoding. */
Martin v. Löwis2863c102002-08-07 15:18:57 +00001323 Py_FatalError("cannot deal with encodings in this build.");
Martin v. Löwis019934b2002-08-07 12:33:18 +00001324#else
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001325 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
1326 if (u == NULL)
1327 return NULL;
1328 v = PyUnicode_AsEncodedString(u, encoding, NULL);
1329 Py_DECREF(u);
1330 return v;
Martin v. Löwis019934b2002-08-07 12:33:18 +00001331#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001332 } else {
1333 return PyString_FromStringAndSize(s, len);
1334 }
1335 }
1336 v = PyString_FromStringAndSize((char *)NULL, /* XXX 4 is enough? */
1337 need_encoding ? len * 4 : len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001338 if (v == NULL)
1339 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001340 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001341 end = s + len;
1342 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001343 if (*s != '\\') {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001344 ORDINAL:
1345 if (need_encoding && (*s & 0x80)) {
1346 char *r;
1347 int rn;
1348 PyObject* w = decode_utf8(&s, end, encoding);
1349 if (w == NULL)
1350 return NULL;
1351 r = PyString_AsString(w);
1352 rn = PyString_Size(w);
1353 memcpy(p, r, rn);
1354 p += rn;
1355 Py_DECREF(w);
1356 } else {
1357 *p++ = *s++;
1358 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001359 continue;
1360 }
1361 s++;
1362 switch (*s++) {
1363 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001364 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001365 case '\\': *p++ = '\\'; break;
1366 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001367 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001368 case 'b': *p++ = '\b'; break;
1369 case 'f': *p++ = '\014'; break; /* FF */
1370 case 't': *p++ = '\t'; break;
1371 case 'n': *p++ = '\n'; break;
1372 case 'r': *p++ = '\r'; break;
1373 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001374 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1375 case '0': case '1': case '2': case '3':
1376 case '4': case '5': case '6': case '7':
1377 c = s[-1] - '0';
1378 if ('0' <= *s && *s <= '7') {
1379 c = (c<<3) + *s++ - '0';
1380 if ('0' <= *s && *s <= '7')
1381 c = (c<<3) + *s++ - '0';
1382 }
1383 *p++ = c;
1384 break;
1385 case 'x':
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001386 if (isxdigit(Py_CHARMASK(s[0]))
1387 && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001388 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001389 c = Py_CHARMASK(*s);
1390 s++;
1391 if (isdigit(c))
1392 x = c - '0';
1393 else if (islower(c))
1394 x = 10 + c - 'a';
1395 else
1396 x = 10 + c - 'A';
1397 x = x << 4;
1398 c = Py_CHARMASK(*s);
1399 s++;
1400 if (isdigit(c))
1401 x += c - '0';
1402 else if (islower(c))
1403 x += 10 + c - 'a';
1404 else
1405 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001406 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001407 break;
1408 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001409 Py_DECREF(v);
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001410 com_error(com, PyExc_ValueError,
1411 "invalid \\x escape");
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001412 return NULL;
Guido van Rossum05459c52002-05-28 18:47:29 +00001413#ifndef Py_USING_UNICODE
1414 case 'u':
1415 case 'U':
1416 case 'N':
1417 if (unicode) {
1418 Py_DECREF(v);
1419 com_error(com, PyExc_ValueError,
1420 "Unicode escapes not legal "
1421 "when Unicode disabled");
1422 return NULL;
1423 }
1424#endif
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001425 default:
1426 *p++ = '\\';
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001427 s--;
1428 goto ORDINAL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001429 }
1430 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001431 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001432 return v;
1433}
1434
Guido van Rossum79f25d91997-04-29 20:08:16 +00001435static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001436parsestrplus(struct compiling* c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001437{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001438 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001439 int i;
1440 REQ(CHILD(n, 0), STRING);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001441 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001442 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001443 for (i = 1; i < NCH(n); i++) {
1444 PyObject *s;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001445 s = parsestr(c, STR(CHILD(n, i)));
Fred Drake4e998bc2000-04-13 14:10:44 +00001446 if (s == NULL)
1447 goto onError;
1448 if (PyString_Check(v) && PyString_Check(s)) {
1449 PyString_ConcatAndDel(&v, s);
1450 if (v == NULL)
1451 goto onError;
1452 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001453#ifdef Py_USING_UNICODE
Fred Drake4e998bc2000-04-13 14:10:44 +00001454 else {
1455 PyObject *temp;
1456 temp = PyUnicode_Concat(v, s);
1457 Py_DECREF(s);
1458 if (temp == NULL)
1459 goto onError;
1460 Py_DECREF(v);
1461 v = temp;
1462 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001463#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001464 }
1465 }
1466 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001467
1468 onError:
1469 Py_XDECREF(v);
1470 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001471}
1472
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001473static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001474com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001475{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001476 int anchor = 0;
1477 int save_begin = c->c_begin;
1478
1479 /* list_iter: for v in expr [list_iter] */
1480 com_node(c, CHILD(n, 3)); /* expr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001481 com_addbyte(c, GET_ITER);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001482 c->c_begin = c->c_nexti;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001483 com_addfwref(c, FOR_ITER, &anchor);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001484 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001485 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001486 c->c_loops++;
1487 com_list_iter(c, n, e, t);
1488 c->c_loops--;
1489 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1490 c->c_begin = save_begin;
1491 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001492 com_pop(c, 1); /* FOR_ITER has popped this */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001493}
1494
1495static void
1496com_list_if(struct compiling *c, node *n, node *e, char *t)
1497{
1498 int anchor = 0;
1499 int a = 0;
1500 /* list_iter: 'if' test [list_iter] */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001501 com_node(c, CHILD(n, 1));
1502 com_addfwref(c, JUMP_IF_FALSE, &a);
1503 com_addbyte(c, POP_TOP);
1504 com_pop(c, 1);
1505 com_list_iter(c, n, e, t);
1506 com_addfwref(c, JUMP_FORWARD, &anchor);
1507 com_backpatch(c, a);
1508 /* We jump here with an extra entry which we now pop */
1509 com_addbyte(c, POP_TOP);
1510 com_backpatch(c, anchor);
1511}
1512
1513static void
1514com_list_iter(struct compiling *c,
1515 node *p, /* parent of list_iter node */
1516 node *e, /* element expression node */
1517 char *t /* name of result list temp local */)
1518{
1519 /* list_iter is the last child in a listmaker, list_for, or list_if */
1520 node *n = CHILD(p, NCH(p)-1);
1521 if (TYPE(n) == list_iter) {
1522 n = CHILD(n, 0);
1523 switch (TYPE(n)) {
1524 case list_for:
1525 com_list_for(c, n, e, t);
1526 break;
1527 case list_if:
1528 com_list_if(c, n, e, t);
1529 break;
1530 default:
1531 com_error(c, PyExc_SystemError,
1532 "invalid list_iter node type");
1533 }
1534 }
1535 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001536 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001537 com_push(c, 1);
1538 com_node(c, e);
1539 com_addoparg(c, CALL_FUNCTION, 1);
1540 com_addbyte(c, POP_TOP);
1541 com_pop(c, 2);
1542 }
1543}
1544
1545static void
1546com_list_comprehension(struct compiling *c, node *n)
1547{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001548 /* listmaker: test list_for */
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001549 char tmpname[30];
1550 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001551 com_addoparg(c, BUILD_LIST, 0);
1552 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1553 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001554 com_addop_name(c, LOAD_ATTR, "append");
1555 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001556 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001557 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001558 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001559 --c->c_tmpname;
1560}
1561
1562static void
1563com_listmaker(struct compiling *c, node *n)
1564{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001565 /* listmaker: test ( list_for | (',' test)* [','] ) */
1566 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001567 com_list_comprehension(c, n);
1568 else {
1569 int len = 0;
1570 int i;
1571 for (i = 0; i < NCH(n); i += 2, len++)
1572 com_node(c, CHILD(n, i));
1573 com_addoparg(c, BUILD_LIST, len);
1574 com_pop(c, len-1);
1575 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001576}
1577
1578static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001579com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001580{
1581 int i;
1582 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1583 for (i = 0; i+2 < NCH(n); i += 4) {
1584 /* We must arrange things just right for STORE_SUBSCR.
1585 It wants the stack to look like (value) (dict) (key) */
1586 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001587 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001588 com_node(c, CHILD(n, i+2)); /* value */
1589 com_addbyte(c, ROT_TWO);
1590 com_node(c, CHILD(n, i)); /* key */
1591 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001592 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001593 }
1594}
1595
1596static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001597com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001598{
1599 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001600 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001601 int i;
1602 REQ(n, atom);
1603 ch = CHILD(n, 0);
1604 switch (TYPE(ch)) {
1605 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001606 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001608 com_push(c, 1);
1609 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001610 else
1611 com_node(c, CHILD(n, 1));
1612 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001613 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001614 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001615 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001616 com_push(c, 1);
1617 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001618 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001619 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001620 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001621 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001622 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001623 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001624 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001625 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001626 break;
1627 case BACKQUOTE:
1628 com_node(c, CHILD(n, 1));
1629 com_addbyte(c, UNARY_CONVERT);
1630 break;
1631 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001632 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001633 i = 255;
1634 }
1635 else {
1636 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001637 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001638 }
1639 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001640 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001641 break;
1642 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001643 v = parsestrplus(c, n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001644 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001645 c->c_errors++;
1646 i = 255;
1647 }
1648 else {
1649 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001651 }
1652 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001653 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001654 break;
1655 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001656 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001657 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001658 break;
1659 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001660 com_error(c, PyExc_SystemError,
1661 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001662 }
1663}
1664
1665static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001666com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001667{
1668 if (NCH(n) == 1) {
1669 com_addbyte(c, op);
1670 }
1671 else if (NCH(n) == 2) {
1672 if (TYPE(CHILD(n, 0)) != COLON) {
1673 com_node(c, CHILD(n, 0));
1674 com_addbyte(c, op+1);
1675 }
1676 else {
1677 com_node(c, CHILD(n, 1));
1678 com_addbyte(c, op+2);
1679 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001680 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001681 }
1682 else {
1683 com_node(c, CHILD(n, 0));
1684 com_node(c, CHILD(n, 2));
1685 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001686 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001687 }
1688}
1689
Guido van Rossum635abd21997-01-06 22:56:52 +00001690static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001691com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1692{
1693 if (NCH(n) == 1) {
1694 com_addbyte(c, DUP_TOP);
1695 com_push(c, 1);
1696 com_addbyte(c, SLICE);
1697 com_node(c, augn);
1698 com_addbyte(c, opcode);
1699 com_pop(c, 1);
1700 com_addbyte(c, ROT_TWO);
1701 com_addbyte(c, STORE_SLICE);
1702 com_pop(c, 2);
1703 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1704 com_node(c, CHILD(n, 0));
1705 com_addoparg(c, DUP_TOPX, 2);
1706 com_push(c, 2);
1707 com_addbyte(c, SLICE+1);
1708 com_pop(c, 1);
1709 com_node(c, augn);
1710 com_addbyte(c, opcode);
1711 com_pop(c, 1);
1712 com_addbyte(c, ROT_THREE);
1713 com_addbyte(c, STORE_SLICE+1);
1714 com_pop(c, 3);
1715 } else if (NCH(n) == 2) {
1716 com_node(c, CHILD(n, 1));
1717 com_addoparg(c, DUP_TOPX, 2);
1718 com_push(c, 2);
1719 com_addbyte(c, SLICE+2);
1720 com_pop(c, 1);
1721 com_node(c, augn);
1722 com_addbyte(c, opcode);
1723 com_pop(c, 1);
1724 com_addbyte(c, ROT_THREE);
1725 com_addbyte(c, STORE_SLICE+2);
1726 com_pop(c, 3);
1727 } else {
1728 com_node(c, CHILD(n, 0));
1729 com_node(c, CHILD(n, 2));
1730 com_addoparg(c, DUP_TOPX, 3);
1731 com_push(c, 3);
1732 com_addbyte(c, SLICE+3);
1733 com_pop(c, 2);
1734 com_node(c, augn);
1735 com_addbyte(c, opcode);
1736 com_pop(c, 1);
1737 com_addbyte(c, ROT_FOUR);
1738 com_addbyte(c, STORE_SLICE+3);
1739 com_pop(c, 4);
1740 }
1741}
1742
1743static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001744com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001745{
1746 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001747 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001748 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001749 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001750 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001751 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001752 }
1753 else {
1754 com_node(c, CHILD(n, 0));
1755 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001756 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001757 }
1758 m = n;
1759 do {
1760 m = CHILD(m, 0);
1761 } while (NCH(m) == 1);
1762 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001763 /* f(lambda x: x[0] = 3) ends up getting parsed with
1764 * LHS test = lambda x: x[0], and RHS test = 3.
1765 * SF bug 132313 points out that complaining about a keyword
1766 * then is very confusing.
1767 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001768 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001769 TYPE(m) == lambdef ?
1770 "lambda cannot contain assignment" :
1771 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001772 }
1773 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001774 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001775 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 *pkeywords = PyDict_New();
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001777 if (v == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001778 c->c_errors++;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001779 else if (*pkeywords == NULL) {
1780 c->c_errors++;
1781 Py_DECREF(v);
1782 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001783 if (PyDict_GetItem(*pkeywords, v) != NULL)
1784 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001785 "duplicate keyword argument");
1786 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001788 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001789 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001790 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001791 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001792 }
1793 }
1794 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001795}
1796
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001797static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001798com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001799{
1800 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001801 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001802 }
1803 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001804 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001805 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001806 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001807 int star_flag = 0;
1808 int starstar_flag = 0;
1809 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001810 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001811 na = 0;
1812 nk = 0;
1813 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001814 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001815 if (TYPE(ch) == STAR ||
1816 TYPE(ch) == DOUBLESTAR)
1817 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001818 if (ch->n_lineno != lineno) {
1819 lineno = ch->n_lineno;
1820 com_addoparg(c, SET_LINENO, lineno);
1821 }
1822 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001823 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001824 na++;
1825 else
1826 nk++;
1827 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001828 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001829 while (i < NCH(n)) {
1830 node *tok = CHILD(n, i);
1831 node *ch = CHILD(n, i+1);
1832 i += 3;
1833 switch (TYPE(tok)) {
1834 case STAR: star_flag = 1; break;
1835 case DOUBLESTAR: starstar_flag = 1; break;
1836 }
1837 com_node(c, ch);
1838 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001839 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001840 com_error(c, PyExc_SyntaxError,
1841 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001842 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001843 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001844 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001845 star_flag + (starstar_flag << 1);
1846 else
1847 opcode = CALL_FUNCTION;
1848 com_addoparg(c, opcode, na | (nk << 8));
1849 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001850 }
1851}
1852
1853static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001854com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001855{
1856 com_addopname(c, LOAD_ATTR, n);
1857}
1858
1859static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001860com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001861{
1862 int i=0;
1863 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001864 node *ch;
1865
1866 /* first argument */
1867 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001868 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001869 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001870 i++;
1871 }
1872 else {
1873 com_node(c, CHILD(n,i));
1874 i++;
1875 REQ(CHILD(n,i),COLON);
1876 i++;
1877 }
1878 /* second argument */
1879 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1880 com_node(c, CHILD(n,i));
1881 i++;
1882 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001883 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001884 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001885 com_push(c, 1);
1886 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001887 /* remaining arguments */
1888 for (; i < NCH(n); i++) {
1889 ns++;
1890 ch=CHILD(n,i);
1891 REQ(ch, sliceop);
1892 if (NCH(ch) == 1) {
1893 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001894 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001895 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001896 }
1897 else
1898 com_node(c, CHILD(ch,1));
1899 }
1900 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001901 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001902}
1903
1904static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001905com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001906{
1907 node *ch;
1908 REQ(n, subscript);
1909 ch = CHILD(n,0);
1910 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001911 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001912 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001913 com_push(c, 1);
1914 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001915 else {
1916 /* check for slice */
1917 if ((TYPE(ch) == COLON || NCH(n) > 1))
1918 com_sliceobj(c, n);
1919 else {
1920 REQ(ch, test);
1921 com_node(c, ch);
1922 }
1923 }
1924}
1925
1926static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001927com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001928{
1929 int i, op;
1930 REQ(n, subscriptlist);
1931 /* Check to make backward compatible slice behavior for '[i:j]' */
1932 if (NCH(n) == 1) {
1933 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001934 /* 'Basic' slice, should have exactly one colon. */
1935 if ((TYPE(CHILD(sub, 0)) == COLON
1936 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1937 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1938 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001939 switch (assigning) {
1940 case OP_DELETE:
1941 op = DELETE_SLICE;
1942 break;
1943 case OP_ASSIGN:
1944 op = STORE_SLICE;
1945 break;
1946 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001947 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001948 break;
1949 default:
1950 com_augassign_slice(c, sub, assigning, augn);
1951 return;
1952 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001953 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001954 if (op == STORE_SLICE)
1955 com_pop(c, 2);
1956 else if (op == DELETE_SLICE)
1957 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001958 return;
1959 }
1960 }
1961 /* Else normal subscriptlist. Compile each subscript. */
1962 for (i = 0; i < NCH(n); i += 2)
1963 com_subscript(c, CHILD(n, i));
1964 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001965 if (NCH(n) > 1) {
1966 i = (NCH(n)+1) / 2;
1967 com_addoparg(c, BUILD_TUPLE, i);
1968 com_pop(c, i-1);
1969 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001970 switch (assigning) {
1971 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001972 op = DELETE_SUBSCR;
1973 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001974 break;
1975 default:
1976 case OP_ASSIGN:
1977 op = STORE_SUBSCR;
1978 i = 3;
1979 break;
1980 case OP_APPLY:
1981 op = BINARY_SUBSCR;
1982 i = 1;
1983 break;
1984 }
1985 if (assigning > OP_APPLY) {
1986 com_addoparg(c, DUP_TOPX, 2);
1987 com_push(c, 2);
1988 com_addbyte(c, BINARY_SUBSCR);
1989 com_pop(c, 1);
1990 com_node(c, augn);
1991 com_addbyte(c, assigning);
1992 com_pop(c, 1);
1993 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001994 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001995 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001996 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001997}
1998
1999static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002000com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002001{
2002 REQ(n, trailer);
2003 switch (TYPE(CHILD(n, 0))) {
2004 case LPAR:
2005 com_call_function(c, CHILD(n, 1));
2006 break;
2007 case DOT:
2008 com_select_member(c, CHILD(n, 1));
2009 break;
2010 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00002011 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002012 break;
2013 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002014 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002015 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002016 }
2017}
2018
2019static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002020com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00002021{
2022 int i;
2023 REQ(n, power);
2024 com_atom(c, CHILD(n, 0));
2025 for (i = 1; i < NCH(n); i++) {
2026 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
2027 com_factor(c, CHILD(n, i+1));
2028 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002029 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00002030 break;
2031 }
2032 else
2033 com_apply_trailer(c, CHILD(n, i));
2034 }
2035}
2036
2037static void
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002038com_invert_constant(struct compiling *c, node *n)
2039{
2040 /* Compute the inverse of int and longs and use them directly,
2041 but be prepared to generate code for all other
2042 possibilities (invalid numbers, floats, complex).
2043 */
2044 PyObject *num, *inv = NULL;
2045 int i;
2046
2047 REQ(n, NUMBER);
2048 num = parsenumber(c, STR(n));
2049 if (num == NULL)
2050 i = 255;
2051 else {
2052 inv = PyNumber_Invert(num);
2053 if (inv == NULL) {
2054 PyErr_Clear();
2055 i = com_addconst(c, num);
2056 } else {
2057 i = com_addconst(c, inv);
2058 Py_DECREF(inv);
2059 }
2060 Py_DECREF(num);
2061 }
2062 com_addoparg(c, LOAD_CONST, i);
2063 com_push(c, 1);
2064 if (num != NULL && inv == NULL)
2065 com_addbyte(c, UNARY_INVERT);
2066}
2067
Tim Peters51e26512001-09-07 08:45:55 +00002068static int
2069is_float_zero(const char *p)
2070{
2071 int found_radix_point = 0;
2072 int ch;
2073 while ((ch = Py_CHARMASK(*p++)) != '\0') {
2074 switch (ch) {
2075 case '0':
2076 /* no reason to believe it's not 0 -- continue */
2077 break;
2078
2079 case 'e': case 'E': case 'j': case 'J':
2080 /* If this was a hex constant, we already would have
2081 returned 0 due to the 'x' or 'X', so 'e' or 'E'
2082 must be an exponent marker, and we haven't yet
2083 seen a non-zero digit, and it doesn't matter what
2084 the exponent is then. For 'j' or 'J' similarly,
2085 except that this is an imaginary 0 then. */
2086 return 1;
2087
2088 case '.':
2089 found_radix_point = 1;
2090 break;
2091
2092 default:
2093 return 0;
2094 }
2095 }
2096 return found_radix_point;
2097}
2098
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002099static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002100com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002101{
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002102 int childtype = TYPE(CHILD(n, 0));
Tim Peters51e26512001-09-07 08:45:55 +00002103 node *pfactor, *ppower, *patom, *pnum;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002104 REQ(n, factor);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002105 /* If the unary +, -, or ~ operator is applied to a constant,
Tim Peters51e26512001-09-07 08:45:55 +00002106 don't generate a UNARY_xxx opcode. Just store the
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002107 approriate value as a constant. If the value is negative,
2108 extend the string containing the constant and insert a
Tim Peters51e26512001-09-07 08:45:55 +00002109 negative in the 0th position -- unless we're doing unary minus
2110 of a floating zero! In that case the sign is significant, but
2111 the const dict can't distinguish +0.0 from -0.0.
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002112 */
2113 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
Fred Drake14ef2442001-08-30 18:53:25 +00002114 && NCH(n) == 2
Tim Peters51e26512001-09-07 08:45:55 +00002115 && TYPE((pfactor = CHILD(n, 1))) == factor
2116 && NCH(pfactor) == 1
2117 && TYPE((ppower = CHILD(pfactor, 0))) == power
2118 && NCH(ppower) == 1
2119 && TYPE((patom = CHILD(ppower, 0))) == atom
2120 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
2121 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002122 if (childtype == TILDE) {
Tim Peters51e26512001-09-07 08:45:55 +00002123 com_invert_constant(c, pnum);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002124 return;
2125 }
2126 if (childtype == MINUS) {
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00002127 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002128 if (s == NULL) {
2129 com_error(c, PyExc_MemoryError, "");
2130 com_addbyte(c, 255);
2131 return;
2132 }
2133 s[0] = '-';
Tim Peters51e26512001-09-07 08:45:55 +00002134 strcpy(s + 1, STR(pnum));
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00002135 PyObject_FREE(STR(pnum));
Tim Peters51e26512001-09-07 08:45:55 +00002136 STR(pnum) = s;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002137 }
Tim Peters51e26512001-09-07 08:45:55 +00002138 com_atom(c, patom);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002139 }
2140 else if (childtype == PLUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002141 com_factor(c, CHILD(n, 1));
2142 com_addbyte(c, UNARY_POSITIVE);
2143 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002144 else if (childtype == MINUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002145 com_factor(c, CHILD(n, 1));
2146 com_addbyte(c, UNARY_NEGATIVE);
2147 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002148 else if (childtype == TILDE) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002149 com_factor(c, CHILD(n, 1));
2150 com_addbyte(c, UNARY_INVERT);
2151 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002152 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00002153 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002154 }
2155}
2156
2157static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002158com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002159{
2160 int i;
2161 int op;
2162 REQ(n, term);
2163 com_factor(c, CHILD(n, 0));
2164 for (i = 2; i < NCH(n); i += 2) {
2165 com_factor(c, CHILD(n, i));
2166 switch (TYPE(CHILD(n, i-1))) {
2167 case STAR:
2168 op = BINARY_MULTIPLY;
2169 break;
2170 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00002171 if (c->c_flags & CO_FUTURE_DIVISION)
2172 op = BINARY_TRUE_DIVIDE;
2173 else
2174 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002175 break;
2176 case PERCENT:
2177 op = BINARY_MODULO;
2178 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002179 case DOUBLESLASH:
2180 op = BINARY_FLOOR_DIVIDE;
2181 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002182 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002183 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00002184 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002185 op = 255;
2186 }
2187 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002188 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002189 }
2190}
2191
2192static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002193com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002194{
2195 int i;
2196 int op;
2197 REQ(n, arith_expr);
2198 com_term(c, CHILD(n, 0));
2199 for (i = 2; i < NCH(n); i += 2) {
2200 com_term(c, CHILD(n, i));
2201 switch (TYPE(CHILD(n, i-1))) {
2202 case PLUS:
2203 op = BINARY_ADD;
2204 break;
2205 case MINUS:
2206 op = BINARY_SUBTRACT;
2207 break;
2208 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002209 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002210 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002211 op = 255;
2212 }
2213 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002214 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002215 }
2216}
2217
2218static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002219com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002220{
2221 int i;
2222 int op;
2223 REQ(n, shift_expr);
2224 com_arith_expr(c, CHILD(n, 0));
2225 for (i = 2; i < NCH(n); i += 2) {
2226 com_arith_expr(c, CHILD(n, i));
2227 switch (TYPE(CHILD(n, i-1))) {
2228 case LEFTSHIFT:
2229 op = BINARY_LSHIFT;
2230 break;
2231 case RIGHTSHIFT:
2232 op = BINARY_RSHIFT;
2233 break;
2234 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002235 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002236 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002237 op = 255;
2238 }
2239 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002240 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002241 }
2242}
2243
2244static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002245com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002246{
2247 int i;
2248 int op;
2249 REQ(n, and_expr);
2250 com_shift_expr(c, CHILD(n, 0));
2251 for (i = 2; i < NCH(n); i += 2) {
2252 com_shift_expr(c, CHILD(n, i));
2253 if (TYPE(CHILD(n, i-1)) == AMPER) {
2254 op = BINARY_AND;
2255 }
2256 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002257 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002258 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002259 op = 255;
2260 }
2261 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002262 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002263 }
2264}
2265
2266static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002267com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002268{
2269 int i;
2270 int op;
2271 REQ(n, xor_expr);
2272 com_and_expr(c, CHILD(n, 0));
2273 for (i = 2; i < NCH(n); i += 2) {
2274 com_and_expr(c, CHILD(n, i));
2275 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2276 op = BINARY_XOR;
2277 }
2278 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002279 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002280 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002281 op = 255;
2282 }
2283 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002284 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002285 }
2286}
2287
2288static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002289com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002290{
2291 int i;
2292 int op;
2293 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002294 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002295 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002296 com_xor_expr(c, CHILD(n, i));
2297 if (TYPE(CHILD(n, i-1)) == VBAR) {
2298 op = BINARY_OR;
2299 }
2300 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002301 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002302 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002303 op = 255;
2304 }
2305 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002306 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002307 }
2308}
2309
2310static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002311cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002312{
2313 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002314 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002315 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2316 if (NCH(n) == 1) {
2317 n = CHILD(n, 0);
2318 switch (TYPE(n)) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002319 case LESS: return PyCmp_LT;
2320 case GREATER: return PyCmp_GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002321 case EQEQUAL: /* == */
Martin v. Löwis7198a522002-01-01 19:59:11 +00002322 case EQUAL: return PyCmp_EQ;
2323 case LESSEQUAL: return PyCmp_LE;
2324 case GREATEREQUAL: return PyCmp_GE;
2325 case NOTEQUAL: return PyCmp_NE; /* <> or != */
2326 case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
2327 if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002328 }
2329 }
2330 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002332 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
Martin v. Löwis7198a522002-01-01 19:59:11 +00002333 return PyCmp_NOT_IN;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002334 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
Martin v. Löwis7198a522002-01-01 19:59:11 +00002335 return PyCmp_IS_NOT;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002336 }
2337 }
Martin v. Löwis7198a522002-01-01 19:59:11 +00002338 return PyCmp_BAD;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002339}
2340
2341static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002342com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002343{
2344 int i;
2345 enum cmp_op op;
2346 int anchor;
2347 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2348 com_expr(c, CHILD(n, 0));
2349 if (NCH(n) == 1)
2350 return;
2351
2352 /****************************************************************
2353 The following code is generated for all but the last
2354 comparison in a chain:
2355
2356 label: on stack: opcode: jump to:
2357
2358 a <code to load b>
2359 a, b DUP_TOP
2360 a, b, b ROT_THREE
2361 b, a, b COMPARE_OP
2362 b, 0-or-1 JUMP_IF_FALSE L1
2363 b, 1 POP_TOP
2364 b
2365
2366 We are now ready to repeat this sequence for the next
2367 comparison in the chain.
2368
2369 For the last we generate:
2370
2371 b <code to load c>
2372 b, c COMPARE_OP
2373 0-or-1
2374
2375 If there were any jumps to L1 (i.e., there was more than one
2376 comparison), we generate:
2377
2378 0-or-1 JUMP_FORWARD L2
2379 L1: b, 0 ROT_TWO
2380 0, b POP_TOP
2381 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002382 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002383 ****************************************************************/
2384
2385 anchor = 0;
2386
2387 for (i = 2; i < NCH(n); i += 2) {
2388 com_expr(c, CHILD(n, i));
2389 if (i+2 < NCH(n)) {
2390 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002391 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002392 com_addbyte(c, ROT_THREE);
2393 }
2394 op = cmp_type(CHILD(n, i-1));
Martin v. Löwis7198a522002-01-01 19:59:11 +00002395 if (op == PyCmp_BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002396 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002397 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002398 }
2399 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002400 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002401 if (i+2 < NCH(n)) {
2402 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2403 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002404 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002405 }
2406 }
2407
2408 if (anchor) {
2409 int anchor2 = 0;
2410 com_addfwref(c, JUMP_FORWARD, &anchor2);
2411 com_backpatch(c, anchor);
2412 com_addbyte(c, ROT_TWO);
2413 com_addbyte(c, POP_TOP);
2414 com_backpatch(c, anchor2);
2415 }
2416}
2417
2418static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002419com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002420{
2421 REQ(n, not_test); /* 'not' not_test | comparison */
2422 if (NCH(n) == 1) {
2423 com_comparison(c, CHILD(n, 0));
2424 }
2425 else {
2426 com_not_test(c, CHILD(n, 1));
2427 com_addbyte(c, UNARY_NOT);
2428 }
2429}
2430
2431static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002432com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002433{
2434 int i;
2435 int anchor;
2436 REQ(n, and_test); /* not_test ('and' not_test)* */
2437 anchor = 0;
2438 i = 0;
2439 for (;;) {
2440 com_not_test(c, CHILD(n, i));
2441 if ((i += 2) >= NCH(n))
2442 break;
2443 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2444 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002445 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002446 }
2447 if (anchor)
2448 com_backpatch(c, anchor);
2449}
2450
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002451static int
2452com_make_closure(struct compiling *c, PyCodeObject *co)
2453{
Jeremy Hylton733c8932001-12-13 19:51:56 +00002454 int i, free = PyCode_GetNumFree(co);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002455 if (free == 0)
2456 return 0;
2457 for (i = 0; i < free; ++i) {
2458 /* Bypass com_addop_varname because it will generate
2459 LOAD_DEREF but LOAD_CLOSURE is needed.
2460 */
2461 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2462 int arg, reftype;
2463
2464 /* Special case: If a class contains a method with a
2465 free variable that has the same name as a method,
2466 the name will be considered free *and* local in the
2467 class. It should be handled by the closure, as
2468 well as by the normal name loookup logic.
2469 */
2470 reftype = get_ref_type(c, PyString_AS_STRING(name));
2471 if (reftype == CELL)
2472 arg = com_lookup_arg(c->c_cellvars, name);
2473 else /* (reftype == FREE) */
2474 arg = com_lookup_arg(c->c_freevars, name);
2475 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002476 fprintf(stderr, "lookup %s in %s %d %d\n"
2477 "freevars of %s: %s\n",
2478 PyObject_REPR(name),
2479 c->c_name,
2480 reftype, arg,
2481 PyString_AS_STRING(co->co_name),
2482 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002483 Py_FatalError("com_make_closure()");
2484 }
2485 com_addoparg(c, LOAD_CLOSURE, arg);
2486
2487 }
2488 com_push(c, free);
2489 return 1;
2490}
2491
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002492static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002493com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002494{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002495 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002496 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002497 PyCodeObject *co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002498 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002499 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002500 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2501 n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002502 co = icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002503 if (co == NULL) {
2504 c->c_errors++;
2505 return;
2506 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002507 symtable_exit_scope(c->c_symtable);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002508 i = com_addconst(c, (PyObject *)co);
2509 closure = com_make_closure(c, co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002510 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002511 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002512 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002513 com_addoparg(c, MAKE_CLOSURE, ndefs);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002514 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002515 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002516 com_addoparg(c, MAKE_FUNCTION, ndefs);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002517 Py_DECREF(co);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002518 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002519 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002520 else {
2521 int anchor = 0;
2522 int i = 0;
2523 for (;;) {
2524 com_and_test(c, CHILD(n, i));
2525 if ((i += 2) >= NCH(n))
2526 break;
2527 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2528 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002529 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002530 }
2531 if (anchor)
2532 com_backpatch(c, anchor);
2533 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002534}
2535
2536static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002537com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002538{
2539 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002540 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002541 com_node(c, CHILD(n, 0));
2542 }
2543 else {
2544 int i;
2545 int len;
2546 len = (NCH(n) + 1) / 2;
2547 for (i = 0; i < NCH(n); i += 2)
2548 com_node(c, CHILD(n, i));
2549 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002550 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002551 }
2552}
2553
2554
2555/* Begin of assignment compilation */
2556
Thomas Wouters434d0822000-08-24 20:11:32 +00002557
2558static void
2559com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2560{
2561 com_addbyte(c, DUP_TOP);
2562 com_push(c, 1);
2563 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002564 com_node(c, augn);
2565 com_addbyte(c, opcode);
2566 com_pop(c, 1);
2567 com_addbyte(c, ROT_TWO);
2568 com_addopname(c, STORE_ATTR, n);
2569 com_pop(c, 2);
2570}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002571
2572static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002573com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002574{
2575 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002576 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002577}
2578
2579static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002580com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002581{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002582 REQ(n, trailer);
2583 switch (TYPE(CHILD(n, 0))) {
2584 case LPAR: /* '(' [exprlist] ')' */
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00002585 if (assigning == OP_DELETE)
2586 com_error(c, PyExc_SyntaxError,
2587 "can't delete function call");
2588 else
2589 com_error(c, PyExc_SyntaxError,
2590 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002591 break;
2592 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002593 if (assigning > OP_APPLY)
2594 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2595 else
2596 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002597 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002598 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002599 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002600 break;
2601 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002602 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002603 }
2604}
2605
2606static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002607com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002608{
2609 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002610 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002611 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002612 if (assigning) {
2613 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002614 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002615 com_push(c, i-1);
2616 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002617 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002618 com_assign(c, CHILD(n, i), assigning, NULL);
2619}
2620
2621static void
2622com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2623{
2624 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002625 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002626 com_push(c, 1);
2627 com_node(c, augn);
2628 com_addbyte(c, opcode);
2629 com_pop(c, 1);
2630 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002631}
2632
2633static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002634com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002635{
2636 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002637 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002638 if (assigning)
2639 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002640}
2641
2642static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002643com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002644{
2645 /* Loop to avoid trivial recursion */
2646 for (;;) {
2647 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002648
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002649 case exprlist:
2650 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002651 case testlist1:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002652 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002653 if (assigning > OP_APPLY) {
2654 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002655 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002656 return;
2657 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002658 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002659 return;
2660 }
2661 n = CHILD(n, 0);
2662 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002663
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002664 case test:
2665 case and_test:
2666 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002667 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002668 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002669 case xor_expr:
2670 case and_expr:
2671 case shift_expr:
2672 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002673 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002674 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002675 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002676 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002677 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002678 return;
2679 }
2680 n = CHILD(n, 0);
2681 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002682
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002683 case power: /* atom trailer* ('**' power)*
2684 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002685 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002686 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002687 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002688 return;
2689 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002690 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002691 int i;
2692 com_node(c, CHILD(n, 0));
2693 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002694 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002695 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002696 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002697 return;
2698 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002699 com_apply_trailer(c, CHILD(n, i));
2700 } /* NB i is still alive */
2701 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002702 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002703 return;
2704 }
2705 n = CHILD(n, 0);
2706 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002707
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002708 case atom:
2709 switch (TYPE(CHILD(n, 0))) {
2710 case LPAR:
2711 n = CHILD(n, 1);
2712 if (TYPE(n) == RPAR) {
2713 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002714 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002715 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002716 return;
2717 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002718 if (assigning > OP_APPLY) {
2719 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002720 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002721 return;
2722 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002723 break;
2724 case LSQB:
2725 n = CHILD(n, 1);
2726 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002727 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002728 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002729 return;
2730 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002731 if (assigning > OP_APPLY) {
2732 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002733 "augmented assign to list not possible");
2734 return;
2735 }
2736 if (NCH(n) > 1
2737 && TYPE(CHILD(n, 1)) == list_for) {
2738 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002739 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002740 return;
2741 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002742 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002743 return;
2744 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002745 if (assigning > OP_APPLY)
2746 com_augassign_name(c, CHILD(n, 0),
2747 assigning, augn);
2748 else
2749 com_assign_name(c, CHILD(n, 0),
2750 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002751 return;
2752 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002753 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002754 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002755 return;
2756 }
2757 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002758
2759 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002760 com_error(c, PyExc_SyntaxError,
2761 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002762 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002763
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002764 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002765 com_error(c, PyExc_SystemError,
2766 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002767 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002768
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002769 }
2770 }
2771}
Guido van Rossum7c531111997-03-11 18:42:21 +00002772
Thomas Wouters434d0822000-08-24 20:11:32 +00002773static void
2774com_augassign(struct compiling *c, node *n)
2775{
2776 int opcode;
2777
2778 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2779 case '+': opcode = INPLACE_ADD; break;
2780 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002781 case '/':
2782 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2783 opcode = INPLACE_FLOOR_DIVIDE;
2784 else if (c->c_flags & CO_FUTURE_DIVISION)
2785 opcode = INPLACE_TRUE_DIVIDE;
2786 else
2787 opcode = INPLACE_DIVIDE;
2788 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002789 case '%': opcode = INPLACE_MODULO; break;
2790 case '<': opcode = INPLACE_LSHIFT; break;
2791 case '>': opcode = INPLACE_RSHIFT; break;
2792 case '&': opcode = INPLACE_AND; break;
2793 case '^': opcode = INPLACE_XOR; break;
2794 case '|': opcode = INPLACE_OR; break;
2795 case '*':
2796 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2797 opcode = INPLACE_POWER;
2798 else
2799 opcode = INPLACE_MULTIPLY;
2800 break;
2801 default:
2802 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2803 return;
2804 }
2805 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2806}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002807
2808static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002809com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002810{
Thomas Wouters434d0822000-08-24 20:11:32 +00002811 REQ(n, expr_stmt);
2812 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002813 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002814 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002815 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002816 if (NCH(n) == 1) {
2817 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002818 if (c->c_interactive)
2819 com_addbyte(c, PRINT_EXPR);
2820 else
2821 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002822 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002823 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002824 else if (TYPE(CHILD(n,1)) == augassign)
2825 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002826 else {
2827 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002828 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002829 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002830 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002831 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002832 com_push(c, 1);
2833 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002834 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002835 }
2836 }
2837}
2838
2839static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002840com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002841{
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002842 int a = 0;
Guido van Rossum228d7f31997-04-02 05:24:36 +00002843 int i;
2844 REQ(n, assert_stmt); /* 'assert' test [',' test] */
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002845 if (Py_OptimizeFlag)
2846 return;
2847 /* Generate code like
Guido van Rossum228d7f31997-04-02 05:24:36 +00002848
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002849 if not <test>:
Guido van Rossum228d7f31997-04-02 05:24:36 +00002850 raise AssertionError [, <message>]
2851
2852 where <message> is the second test, if present.
2853 */
Guido van Rossum228d7f31997-04-02 05:24:36 +00002854 com_node(c, CHILD(n, 1));
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002855 com_addfwref(c, JUMP_IF_TRUE, &a);
Guido van Rossum228d7f31997-04-02 05:24:36 +00002856 com_addbyte(c, POP_TOP);
2857 com_pop(c, 1);
2858 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002859 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002860 com_push(c, 1);
2861 i = NCH(n)/2; /* Either 2 or 4 */
2862 if (i > 1)
2863 com_node(c, CHILD(n, 3));
2864 com_addoparg(c, RAISE_VARARGS, i);
2865 com_pop(c, i);
2866 /* The interpreter does not fall through */
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002867 /* Jump ends up here */
Guido van Rossum228d7f31997-04-02 05:24:36 +00002868 com_backpatch(c, a);
Guido van Rossum228d7f31997-04-02 05:24:36 +00002869 com_addbyte(c, POP_TOP);
2870}
2871
2872static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002873com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002874{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002875 int i = 1;
2876 node* stream = NULL;
2877
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002878 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002879
2880 /* are we using the extended print form? */
2881 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2882 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002883 com_node(c, stream);
2884 /* stack: [...] => [... stream] */
2885 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002886 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2887 i = 4;
2888 else
2889 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002890 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002891 for (; i < NCH(n); i += 2) {
2892 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002893 com_addbyte(c, DUP_TOP);
2894 /* stack: [stream] => [stream stream] */
2895 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002896 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002897 /* stack: [stream stream] => [stream stream obj] */
2898 com_addbyte(c, ROT_TWO);
2899 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002900 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002901 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002902 com_pop(c, 2);
2903 }
2904 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002905 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002906 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002907 com_addbyte(c, PRINT_ITEM);
2908 com_pop(c, 1);
2909 }
2910 }
2911 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002912 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002913 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002914 /* must pop the extra stream object off the stack */
2915 com_addbyte(c, POP_TOP);
2916 /* stack: [... stream] => [...] */
2917 com_pop(c, 1);
2918 }
2919 }
2920 else {
2921 if (stream != NULL) {
2922 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002923 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002924 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002925 com_pop(c, 1);
2926 }
2927 else
2928 com_addbyte(c, PRINT_NEWLINE);
2929 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002930}
2931
2932static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002933com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002934{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002935 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002936 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002937 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002938 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002939 if (c->c_flags & CO_GENERATOR) {
2940 if (NCH(n) > 1) {
2941 com_error(c, PyExc_SyntaxError,
2942 "'return' with argument inside generator");
2943 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002944 }
2945 if (NCH(n) < 2) {
2946 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002947 com_push(c, 1);
2948 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002949 else
2950 com_node(c, CHILD(n, 1));
2951 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002952 com_pop(c, 1);
2953}
2954
2955static void
2956com_yield_stmt(struct compiling *c, node *n)
2957{
Tim Peters95c80f82001-06-23 02:07:08 +00002958 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002959 REQ(n, yield_stmt); /* 'yield' testlist */
2960 if (!c->c_infunction) {
2961 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2962 }
Tim Peters95c80f82001-06-23 02:07:08 +00002963
2964 for (i = 0; i < c->c_nblocks; ++i) {
2965 if (c->c_block[i] == SETUP_FINALLY) {
2966 com_error(c, PyExc_SyntaxError,
2967 "'yield' not allowed in a 'try' block "
2968 "with a 'finally' clause");
2969 return;
2970 }
2971 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002972 com_node(c, CHILD(n, 1));
2973 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002974 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002975}
2976
2977static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002978com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002979{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002980 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002981 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2982 if (NCH(n) > 1) {
2983 com_node(c, CHILD(n, 1));
2984 if (NCH(n) > 3) {
2985 com_node(c, CHILD(n, 3));
2986 if (NCH(n) > 5)
2987 com_node(c, CHILD(n, 5));
2988 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002989 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002990 i = NCH(n)/2;
2991 com_addoparg(c, RAISE_VARARGS, i);
2992 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002993}
2994
2995static void
Thomas Wouters52152252000-08-17 22:55:00 +00002996com_from_import(struct compiling *c, node *n)
2997{
2998 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2999 com_push(c, 1);
3000 if (NCH(n) > 1) {
3001 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
3002 com_error(c, PyExc_SyntaxError, "invalid syntax");
3003 return;
3004 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003005 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00003006 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003007 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00003008 com_pop(c, 1);
3009}
3010
3011static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003012com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003013{
3014 int i;
3015 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003016 /* 'import' dotted_name (',' dotted_name)* |
3017 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003018 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00003019 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003020 /* 'from' dotted_name 'import' ... */
3021 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00003022
3023 if (TYPE(CHILD(n, 3)) == STAR) {
3024 tup = Py_BuildValue("(s)", "*");
3025 } else {
3026 tup = PyTuple_New((NCH(n) - 2)/2);
3027 for (i = 3; i < NCH(n); i += 2) {
3028 PyTuple_SET_ITEM(tup, (i-3)/2,
3029 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00003030 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00003031 }
3032 }
3033 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00003034 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003035 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00003036 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00003037 if (TYPE(CHILD(n, 3)) == STAR)
3038 com_addbyte(c, IMPORT_STAR);
3039 else {
3040 for (i = 3; i < NCH(n); i += 2)
3041 com_from_import(c, CHILD(n, i));
3042 com_addbyte(c, POP_TOP);
3043 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003044 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003045 }
3046 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003047 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003048 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00003049 node *subn = CHILD(n, i);
3050 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00003051 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003052 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00003053 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00003054 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00003055 int j;
3056 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00003057 com_error(c, PyExc_SyntaxError,
3058 "invalid syntax");
3059 return;
3060 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00003061 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
3062 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003063 CHILD(CHILD(subn, 0),
3064 j));
3065 com_addop_varname(c, VAR_STORE,
3066 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00003067 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003068 com_addop_varname(c, VAR_STORE,
3069 STR(CHILD(CHILD(subn, 0),
3070 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003071 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003072 }
3073 }
3074}
3075
3076static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003077com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003078{
3079 REQ(n, exec_stmt);
3080 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
3081 com_node(c, CHILD(n, 1));
3082 if (NCH(n) >= 4)
3083 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003084 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003085 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003086 com_push(c, 1);
3087 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003088 if (NCH(n) >= 6)
3089 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003090 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003091 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003092 com_push(c, 1);
3093 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003094 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003095 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003096}
3097
Guido van Rossum7c531111997-03-11 18:42:21 +00003098static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003099is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00003100{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003101 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00003102 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003103 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00003104
3105 /* Label to avoid tail recursion */
3106 next:
3107 switch (TYPE(n)) {
3108
3109 case suite:
3110 if (NCH(n) == 1) {
3111 n = CHILD(n, 0);
3112 goto next;
3113 }
3114 /* Fall through */
3115 case file_input:
3116 for (i = 0; i < NCH(n); i++) {
3117 node *ch = CHILD(n, i);
3118 if (TYPE(ch) == stmt) {
3119 n = ch;
3120 goto next;
3121 }
3122 }
3123 break;
3124
3125 case stmt:
3126 case simple_stmt:
3127 case small_stmt:
3128 n = CHILD(n, 0);
3129 goto next;
3130
3131 case expr_stmt:
3132 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003133 case testlist1:
Guido van Rossum7c531111997-03-11 18:42:21 +00003134 case test:
3135 case and_test:
3136 case not_test:
3137 case comparison:
3138 case expr:
3139 case xor_expr:
3140 case and_expr:
3141 case shift_expr:
3142 case arith_expr:
3143 case term:
3144 case factor:
3145 case power:
3146 case atom:
3147 if (NCH(n) == 1) {
3148 n = CHILD(n, 0);
3149 goto next;
3150 }
3151 break;
3152
3153 case NAME:
3154 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
3155 return 1;
3156 break;
3157
3158 case NUMBER:
3159 v = parsenumber(c, STR(n));
3160 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003161 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00003162 break;
3163 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003164 i = PyObject_IsTrue(v);
3165 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003166 return i == 0;
3167
3168 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003169 v = parsestr(c, STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00003170 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003171 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00003172 break;
3173 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003174 i = PyObject_IsTrue(v);
3175 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003176 return i == 0;
3177
3178 }
3179 return 0;
3180}
3181
Tim Peters08a898f2001-06-28 01:52:22 +00003182
3183/* Look under n for a return stmt with an expression.
3184 * This hack is used to find illegal returns under "if 0:" blocks in
3185 * functions already known to be generators (as determined by the symtable
3186 * pass).
3187 * Return the offending return node if found, else NULL.
3188 */
3189static node *
3190look_for_offending_return(node *n)
3191{
3192 int i;
3193
3194 for (i = 0; i < NCH(n); ++i) {
3195 node *kid = CHILD(n, i);
3196
3197 switch (TYPE(kid)) {
3198 case classdef:
3199 case funcdef:
3200 case lambdef:
3201 /* Stuff in nested functions & classes doesn't
3202 affect the code block we started in. */
3203 return NULL;
3204
3205 case return_stmt:
3206 if (NCH(kid) > 1)
3207 return kid;
3208 break;
3209
3210 default: {
3211 node *bad = look_for_offending_return(kid);
3212 if (bad != NULL)
3213 return bad;
3214 }
3215 }
3216 }
3217
3218 return NULL;
3219}
3220
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003221static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003222com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003223{
3224 int i;
3225 int anchor = 0;
3226 REQ(n, if_stmt);
3227 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3228 for (i = 0; i+3 < NCH(n); i+=4) {
3229 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003230 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00003231 if (is_constant_false(c, ch)) {
3232 /* We're going to skip this block. However, if this
3233 is a generator, we have to check the dead code
3234 anyway to make sure there aren't any return stmts
3235 with expressions, in the same scope. */
3236 if (c->c_flags & CO_GENERATOR) {
3237 node *p = look_for_offending_return(n);
3238 if (p != NULL) {
3239 int savelineno = c->c_lineno;
3240 c->c_lineno = p->n_lineno;
3241 com_error(c, PyExc_SyntaxError,
3242 "'return' with argument "
3243 "inside generator");
3244 c->c_lineno = savelineno;
3245 }
3246 }
Guido van Rossum7c531111997-03-11 18:42:21 +00003247 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00003248 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00003249 if (i > 0)
3250 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00003251 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003252 com_addfwref(c, JUMP_IF_FALSE, &a);
3253 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003254 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003255 com_node(c, CHILD(n, i+3));
3256 com_addfwref(c, JUMP_FORWARD, &anchor);
3257 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003258 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003259 com_addbyte(c, POP_TOP);
3260 }
3261 if (i+2 < NCH(n))
3262 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00003263 if (anchor)
3264 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003265}
3266
3267static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003268com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003269{
3270 int break_anchor = 0;
3271 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003272 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003273 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3274 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003275 block_push(c, SETUP_LOOP);
3276 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003277 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003278 com_node(c, CHILD(n, 1));
3279 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3280 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003281 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003282 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003283 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003284 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003285 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3286 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003287 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003288 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003289 com_addbyte(c, POP_TOP);
3290 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003291 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003292 if (NCH(n) > 4)
3293 com_node(c, CHILD(n, 6));
3294 com_backpatch(c, break_anchor);
3295}
3296
3297static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003298com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003299{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003300 int break_anchor = 0;
3301 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003302 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003303 REQ(n, for_stmt);
3304 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3305 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003306 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003307 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003308 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003309 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003310 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003311 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003312 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003313 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003314 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003315 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003316 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003317 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3318 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003319 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003320 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003321 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003322 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003323 if (NCH(n) > 8)
3324 com_node(c, CHILD(n, 8));
3325 com_backpatch(c, break_anchor);
3326}
3327
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003328/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003329
3330 SETUP_FINALLY L
3331 <code for S>
3332 POP_BLOCK
3333 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003334 L: <code for Sf>
3335 END_FINALLY
3336
3337 The special instructions use the block stack. Each block
3338 stack entry contains the instruction that created it (here
3339 SETUP_FINALLY), the level of the value stack at the time the
3340 block stack entry was created, and a label (here L).
3341
3342 SETUP_FINALLY:
3343 Pushes the current value stack level and the label
3344 onto the block stack.
3345 POP_BLOCK:
3346 Pops en entry from the block stack, and pops the value
3347 stack until its level is the same as indicated on the
3348 block stack. (The label is ignored.)
3349 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003350 Pops a variable number of entries from the *value* stack
3351 and re-raises the exception they specify. The number of
3352 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003353
3354 The block stack is unwound when an exception is raised:
3355 when a SETUP_FINALLY entry is found, the exception is pushed
3356 onto the value stack (and the exception condition is cleared),
3357 and the interpreter jumps to the label gotten from the block
3358 stack.
3359
3360 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003361 (The contents of the value stack is shown in [], with the top
3362 at the right; 'tb' is trace-back info, 'val' the exception's
3363 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003364
3365 Value stack Label Instruction Argument
3366 [] SETUP_EXCEPT L1
3367 [] <code for S>
3368 [] POP_BLOCK
3369 [] JUMP_FORWARD L0
3370
Guido van Rossum3f5da241990-12-20 15:06:42 +00003371 [tb, val, exc] L1: DUP )
3372 [tb, val, exc, exc] <evaluate E1> )
3373 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3374 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3375 [tb, val, exc, 1] POP )
3376 [tb, val, exc] POP
3377 [tb, val] <assign to V1> (or POP if no V1)
3378 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003379 [] <code for S1>
3380 JUMP_FORWARD L0
3381
Guido van Rossum3f5da241990-12-20 15:06:42 +00003382 [tb, val, exc, 0] L2: POP
3383 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003384 .............................etc.......................
3385
Guido van Rossum3f5da241990-12-20 15:06:42 +00003386 [tb, val, exc, 0] Ln+1: POP
3387 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003388
3389 [] L0: <next statement>
3390
3391 Of course, parts are not generated if Vi or Ei is not present.
3392*/
3393
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003394static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003395com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003396{
3397 int except_anchor = 0;
3398 int end_anchor = 0;
3399 int else_anchor = 0;
3400 int i;
3401 node *ch;
3402
3403 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3404 block_push(c, SETUP_EXCEPT);
3405 com_node(c, CHILD(n, 2));
3406 com_addbyte(c, POP_BLOCK);
3407 block_pop(c, SETUP_EXCEPT);
3408 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3409 com_backpatch(c, except_anchor);
3410 for (i = 3;
3411 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3412 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003413 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003414 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003415 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003416 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003417 break;
3418 }
3419 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003420 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003421 com_addoparg(c, SET_LINENO, ch->n_lineno);
3422 if (NCH(ch) > 1) {
3423 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003424 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003425 com_node(c, CHILD(ch, 1));
Martin v. Löwis7198a522002-01-01 19:59:11 +00003426 com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003427 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003428 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3429 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003430 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003431 }
3432 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003433 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003434 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003435 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003436 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003437 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003438 com_pop(c, 1);
3439 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003440 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003441 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003442 com_node(c, CHILD(n, i+2));
3443 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3444 if (except_anchor) {
3445 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003446 /* We come in with [tb, val, exc, 0] on the
3447 stack; one pop and it's the same as
3448 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003449 com_addbyte(c, POP_TOP);
3450 }
3451 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003452 /* We actually come in here with [tb, val, exc] but the
3453 END_FINALLY will zap those and jump around.
3454 The c_stacklevel does not reflect them so we need not pop
3455 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003456 com_addbyte(c, END_FINALLY);
3457 com_backpatch(c, else_anchor);
3458 if (i < NCH(n))
3459 com_node(c, CHILD(n, i+2));
3460 com_backpatch(c, end_anchor);
3461}
3462
3463static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003464com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003465{
3466 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003467 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003468
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003469 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3470 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003471 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003472 com_addbyte(c, POP_BLOCK);
3473 block_pop(c, SETUP_FINALLY);
3474 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003475 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003476 /* While the generated code pushes only one item,
3477 the try-finally handling can enter here with
3478 up to three items. OK, here are the details:
3479 3 for an exception, 2 for RETURN, 1 for BREAK. */
3480 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003481 com_backpatch(c, finally_anchor);
3482 ch = CHILD(n, NCH(n)-1);
3483 com_addoparg(c, SET_LINENO, ch->n_lineno);
3484 com_node(c, ch);
3485 com_addbyte(c, END_FINALLY);
3486 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003487 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003488}
3489
3490static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003491com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003492{
3493 REQ(n, try_stmt);
3494 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3495 | 'try' ':' suite 'finally' ':' suite */
3496 if (TYPE(CHILD(n, 3)) != except_clause)
3497 com_try_finally(c, n);
3498 else
3499 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003500}
3501
Guido van Rossum8b993a91997-01-17 21:04:03 +00003502static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003503get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003504{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003505 int i;
3506
Guido van Rossum8b993a91997-01-17 21:04:03 +00003507 /* Label to avoid tail recursion */
3508 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003509 switch (TYPE(n)) {
3510
3511 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003512 if (NCH(n) == 1) {
3513 n = CHILD(n, 0);
3514 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003515 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003516 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003517 case file_input:
3518 for (i = 0; i < NCH(n); i++) {
3519 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003520 if (TYPE(ch) == stmt) {
3521 n = ch;
3522 goto next;
3523 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003524 }
3525 break;
3526
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003527 case stmt:
3528 case simple_stmt:
3529 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003530 n = CHILD(n, 0);
3531 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003532
3533 case expr_stmt:
3534 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003535 case testlist1:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003536 case test:
3537 case and_test:
3538 case not_test:
3539 case comparison:
3540 case expr:
3541 case xor_expr:
3542 case and_expr:
3543 case shift_expr:
3544 case arith_expr:
3545 case term:
3546 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003547 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003548 if (NCH(n) == 1) {
3549 n = CHILD(n, 0);
3550 goto next;
3551 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003552 break;
3553
3554 case atom:
3555 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003556 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003557 break;
3558
3559 }
3560 return NULL;
3561}
3562
Guido van Rossum79f25d91997-04-29 20:08:16 +00003563static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003564get_docstring(struct compiling *c, node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003565{
Guido van Rossum541563e1999-01-28 15:08:09 +00003566 /* Don't generate doc-strings if run with -OO */
3567 if (Py_OptimizeFlag > 1)
3568 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003569 n = get_rawdocstring(n);
3570 if (n == NULL)
3571 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003572 return parsestrplus(c, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003573}
3574
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003575static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003576com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003577{
3578 REQ(n, suite);
3579 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3580 if (NCH(n) == 1) {
3581 com_node(c, CHILD(n, 0));
3582 }
3583 else {
3584 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003585 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003586 node *ch = CHILD(n, i);
3587 if (TYPE(ch) == stmt)
3588 com_node(c, ch);
3589 }
3590 }
3591}
3592
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003593/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003594static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003595com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003596{
3597 int i = c->c_nblocks;
3598 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3599 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3600 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003601 else if (i <= 0) {
3602 /* at the outer level */
3603 com_error(c, PyExc_SyntaxError,
3604 "'continue' not properly in loop");
3605 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003606 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003607 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003608 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003609 if (c->c_block[j] == SETUP_LOOP)
3610 break;
3611 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003612 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003613 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003614 for (; i > j; --i) {
3615 if (c->c_block[i] == SETUP_EXCEPT ||
3616 c->c_block[i] == SETUP_FINALLY) {
3617 com_addoparg(c, CONTINUE_LOOP,
3618 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003619 return;
3620 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003621 if (c->c_block[i] == END_FINALLY) {
3622 com_error(c, PyExc_SyntaxError,
3623 "'continue' not supported inside 'finally' clause");
3624 return;
3625 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003626 }
3627 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003628 com_error(c, PyExc_SyntaxError,
3629 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003630 }
3631 /* XXX Could allow it inside a 'finally' clause
3632 XXX if we could pop the exception still on the stack */
3633}
3634
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003635static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003636com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003637{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003638 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003639 if (TYPE(n) == lambdef) {
3640 /* lambdef: 'lambda' [varargslist] ':' test */
3641 n = CHILD(n, 1);
3642 }
3643 else {
3644 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3645 n = CHILD(n, 2);
3646 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3647 n = CHILD(n, 1);
3648 }
3649 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003650 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003651 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003652 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003653 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3654 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003655 nargs = 0;
3656 ndefs = 0;
3657 for (i = 0; i < nch; i++) {
3658 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003659 if (TYPE(CHILD(n, i)) == STAR ||
3660 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003661 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003662 nargs++;
3663 i++;
3664 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003665 t = RPAR; /* Anything except EQUAL or COMMA */
3666 else
3667 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003668 if (t == EQUAL) {
3669 i++;
3670 ndefs++;
3671 com_node(c, CHILD(n, i));
3672 i++;
3673 if (i >= nch)
3674 break;
3675 t = TYPE(CHILD(n, i));
3676 }
3677 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003678 /* Treat "(a=1, b)" as an error */
3679 if (ndefs)
3680 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003681 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003682 }
3683 if (t != COMMA)
3684 break;
3685 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003686 return ndefs;
3687}
3688
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003689static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003690com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003691{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003692 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003693 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003694 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003695 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003696 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3697 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003698 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003699 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003700 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003701 c->c_errors++;
3702 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003703 int closure = com_make_closure(c, (PyCodeObject *)co);
3704 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003705 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003706 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003707 if (closure)
3708 com_addoparg(c, MAKE_CLOSURE, ndefs);
3709 else
3710 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003711 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003712 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003713 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003714 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003715 }
3716}
3717
3718static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003719com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003720{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003721 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003722 REQ(n, testlist);
3723 /* testlist: test (',' test)* [','] */
3724 for (i = 0; i < NCH(n); i += 2)
3725 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003726 i = (NCH(n)+1) / 2;
3727 com_addoparg(c, BUILD_TUPLE, i);
3728 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003729}
3730
3731static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003732com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003733{
Guido van Rossum25831651993-05-19 14:50:45 +00003734 int i;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003735 PyObject *v;
3736 PyCodeObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003737 char *name;
3738
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003739 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003740 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003741 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003742 c->c_errors++;
3743 return;
3744 }
3745 /* Push the class name on the stack */
3746 i = com_addconst(c, v);
3747 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003748 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003749 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003750 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003751 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003752 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003753 com_push(c, 1);
3754 }
Guido van Rossum25831651993-05-19 14:50:45 +00003755 else
3756 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003757 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003758 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003759 co = icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003760 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003761 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003762 c->c_errors++;
3763 else {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003764 int closure = com_make_closure(c, co);
3765 i = com_addconst(c, (PyObject *)co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003766 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003767 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003768 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003769 com_addoparg(c, MAKE_CLOSURE, 0);
Jeremy Hylton733c8932001-12-13 19:51:56 +00003770 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003771 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003772 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003773 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003774 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003775 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003776 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003777 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003778 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003779 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003780}
3781
3782static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003783com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003784{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003785 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003786 if (c->c_errors)
3787 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003788 switch (TYPE(n)) {
3789
3790 /* Definition nodes */
3791
3792 case funcdef:
3793 com_funcdef(c, n);
3794 break;
3795 case classdef:
3796 com_classdef(c, n);
3797 break;
3798
3799 /* Trivial parse tree nodes */
3800
3801 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003802 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003803 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003804 n = CHILD(n, 0);
3805 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003806
3807 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003808 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3809 com_addoparg(c, SET_LINENO, n->n_lineno);
3810 {
3811 int i;
3812 for (i = 0; i < NCH(n)-1; i += 2)
3813 com_node(c, CHILD(n, i));
3814 }
3815 break;
3816
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003817 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003818 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003819 n = CHILD(n, 0);
3820 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003821
3822 /* Statement nodes */
3823
3824 case expr_stmt:
3825 com_expr_stmt(c, n);
3826 break;
3827 case print_stmt:
3828 com_print_stmt(c, n);
3829 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003830 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003831 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003832 break;
3833 case pass_stmt:
3834 break;
3835 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003836 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003837 com_error(c, PyExc_SyntaxError,
3838 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003839 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003840 com_addbyte(c, BREAK_LOOP);
3841 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003842 case continue_stmt:
3843 com_continue_stmt(c, n);
3844 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003845 case return_stmt:
3846 com_return_stmt(c, n);
3847 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003848 case yield_stmt:
3849 com_yield_stmt(c, n);
3850 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851 case raise_stmt:
3852 com_raise_stmt(c, n);
3853 break;
3854 case import_stmt:
3855 com_import_stmt(c, n);
3856 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003857 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003858 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003859 case exec_stmt:
3860 com_exec_stmt(c, n);
3861 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003862 case assert_stmt:
3863 com_assert_stmt(c, n);
3864 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003865 case if_stmt:
3866 com_if_stmt(c, n);
3867 break;
3868 case while_stmt:
3869 com_while_stmt(c, n);
3870 break;
3871 case for_stmt:
3872 com_for_stmt(c, n);
3873 break;
3874 case try_stmt:
3875 com_try_stmt(c, n);
3876 break;
3877 case suite:
3878 com_suite(c, n);
3879 break;
3880
3881 /* Expression nodes */
3882
3883 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003884 case testlist1:
Guido van Rossum1c917072001-10-15 15:44:05 +00003885 case testlist_safe:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003886 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003887 break;
3888 case test:
3889 com_test(c, n);
3890 break;
3891 case and_test:
3892 com_and_test(c, n);
3893 break;
3894 case not_test:
3895 com_not_test(c, n);
3896 break;
3897 case comparison:
3898 com_comparison(c, n);
3899 break;
3900 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003901 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003902 break;
3903 case expr:
3904 com_expr(c, n);
3905 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003906 case xor_expr:
3907 com_xor_expr(c, n);
3908 break;
3909 case and_expr:
3910 com_and_expr(c, n);
3911 break;
3912 case shift_expr:
3913 com_shift_expr(c, n);
3914 break;
3915 case arith_expr:
3916 com_arith_expr(c, n);
3917 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003918 case term:
3919 com_term(c, n);
3920 break;
3921 case factor:
3922 com_factor(c, n);
3923 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003924 case power:
3925 com_power(c, n);
3926 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003927 case atom:
3928 com_atom(c, n);
3929 break;
3930
3931 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003932 com_error(c, PyExc_SystemError,
3933 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003934 }
3935}
3936
Tim Petersdbd9ba62000-07-09 03:09:57 +00003937static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003938
3939static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003940com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003941{
3942 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3943 if (TYPE(CHILD(n, 0)) == LPAR)
3944 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003945 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003946 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003947 com_pop(c, 1);
3948 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003949}
3950
3951static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003952com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003954 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955 if (NCH(n) == 1) {
3956 com_fpdef(c, CHILD(n, 0));
3957 }
3958 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003959 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003960 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003961 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003962 for (i = 0; i < NCH(n); i += 2)
3963 com_fpdef(c, CHILD(n, i));
3964 }
3965}
3966
3967static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003968com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003969{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003970 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003971 int complex = 0;
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003972 char nbuf[30];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003973 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003974 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003975 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003976 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003977 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003978 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003979 node *ch = CHILD(n, i);
3980 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003981 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003982 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003983 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3984 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003985 if (TYPE(fp) != NAME) {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003986 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003987 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003988 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003989 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003990 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003991 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003992 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003993 ch = CHILD(n, i);
3994 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003995 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003996 else
3997 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003998 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003999 if (complex) {
4000 /* Generate code for complex arguments only after
4001 having counted the simple arguments */
4002 int ilocal = 0;
4003 for (i = 0; i < nch; i++) {
4004 node *ch = CHILD(n, i);
4005 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00004006 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004007 break;
4008 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
4009 fp = CHILD(ch, 0);
4010 if (TYPE(fp) != NAME) {
4011 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004012 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004013 com_fpdef(c, ch);
4014 }
4015 ilocal++;
4016 if (++i >= nch)
4017 break;
4018 ch = CHILD(n, i);
4019 if (TYPE(ch) == EQUAL)
4020 i += 2;
4021 else
4022 REQ(ch, COMMA);
4023 }
4024 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00004025}
4026
4027static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004028com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004029{
4030 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004031 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004032 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004033 doc = get_docstring(c, n);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004034 if (doc != NULL) {
4035 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004036 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004037 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004038 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004039 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00004040 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004041 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004042 for (i = 0; i < NCH(n); i++) {
4043 node *ch = CHILD(n, i);
4044 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
4045 com_node(c, ch);
4046 }
4047}
4048
4049/* Top-level compile-node interface */
4050
4051static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004052compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004053{
Guido van Rossum79f25d91997-04-29 20:08:16 +00004054 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004055 node *ch;
4056 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00004057 c->c_name = STR(CHILD(n, 1));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004058 doc = get_docstring(c, CHILD(n, 4));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004059 if (doc != NULL) {
4060 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004061 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004062 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004063 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00004064 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00004065 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
4066 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00004067 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00004068 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004069 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004070 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00004071 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00004072 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4073 com_push(c, 1);
4074 com_addbyte(c, RETURN_VALUE);
4075 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004076}
4077
4078static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004079compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00004080{
Guido van Rossum590baa41993-11-30 13:40:46 +00004081 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004082 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00004083 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00004084
4085 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004086 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00004087 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00004088 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004089 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00004090 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004091 else
4092 ch = CHILD(n, 2);
4093 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00004094 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004095 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00004096}
4097
4098static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004099compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004100{
4101 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004102 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004103 REQ(n, classdef);
4104 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4105 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004106 c->c_private = c->c_name;
Guido van Rossum340cbe72002-01-15 21:06:07 +00004107 /* Initialize local __module__ from global __name__ */
4108 com_addop_name(c, LOAD_GLOBAL, "__name__");
4109 com_addop_name(c, STORE_NAME, "__module__");
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004110 ch = CHILD(n, NCH(n)-1); /* The suite */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004111 doc = get_docstring(c, ch);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004112 if (doc != NULL) {
4113 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004114 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004115 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004116 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004117 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00004118 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004119 }
4120 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00004121 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004122 com_node(c, ch);
4123 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004124 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004125 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004126 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004127}
4128
4129static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004130compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004131{
Guido van Rossum3f5da241990-12-20 15:06:42 +00004132 com_addoparg(c, SET_LINENO, n->n_lineno);
4133
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004134 switch (TYPE(n)) {
4135
Guido van Rossum4c417781991-01-21 16:09:22 +00004136 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004137 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004138 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004139 n = CHILD(n, 0);
4140 if (TYPE(n) != NEWLINE)
4141 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004142 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00004143 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004144 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004145 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004146 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004147 break;
4148
Guido van Rossum4c417781991-01-21 16:09:22 +00004149 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004150 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004151 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00004152 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004153 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004154 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004155 break;
4156
Guido van Rossum590baa41993-11-30 13:40:46 +00004157 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00004158 com_node(c, CHILD(n, 0));
4159 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004160 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00004161 break;
4162
Guido van Rossum590baa41993-11-30 13:40:46 +00004163 case lambdef: /* anonymous function definition */
4164 compile_lambdef(c, n);
4165 break;
4166
Guido van Rossum4c417781991-01-21 16:09:22 +00004167 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004168 compile_funcdef(c, n);
4169 break;
4170
Guido van Rossum4c417781991-01-21 16:09:22 +00004171 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004172 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00004173 break;
4174
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004175 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00004176 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00004177 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004178 }
4179}
4180
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004181static PyObject *
4182dict_keys_inorder(PyObject *dict, int offset)
4183{
4184 PyObject *tuple, *k, *v;
4185 int i, pos = 0, size = PyDict_Size(dict);
4186
4187 tuple = PyTuple_New(size);
4188 if (tuple == NULL)
4189 return NULL;
4190 while (PyDict_Next(dict, &pos, &k, &v)) {
4191 i = PyInt_AS_LONG(v);
4192 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004193 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004194 PyTuple_SET_ITEM(tuple, i - offset, k);
4195 }
4196 return tuple;
4197}
4198
Guido van Rossum79f25d91997-04-29 20:08:16 +00004199PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004200PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004201{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004202 return PyNode_CompileFlags(n, filename, NULL);
4203}
4204
4205PyCodeObject *
4206PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4207{
4208 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004209}
4210
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004211struct symtable *
4212PyNode_CompileSymtable(node *n, char *filename)
4213{
4214 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004215 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004216
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004217 ff = PyNode_Future(n, filename);
4218 if (ff == NULL)
4219 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004220
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004221 st = symtable_init();
Tim Peters8c5e4152001-11-04 19:26:58 +00004222 if (st == NULL) {
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00004223 PyObject_FREE((void *)ff);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004224 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004225 }
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004226 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004227 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004228 if (st->st_errors > 0)
4229 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004230 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004231 if (st->st_errors > 0)
4232 goto fail;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004233
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004234 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004235 fail:
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00004236 PyObject_FREE((void *)ff);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004237 st->st_future = NULL;
4238 PySymtable_Free(st);
4239 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004240}
4241
Guido van Rossum79f25d91997-04-29 20:08:16 +00004242static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004243icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004244{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004245 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004246}
4247
Guido van Rossum79f25d91997-04-29 20:08:16 +00004248static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004249jcompile(node *n, char *filename, struct compiling *base,
4250 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004251{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004252 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004253 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00004254 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004255 return NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00004256 if (TYPE(n) == encoding_decl) {
4257 sc.c_encoding = STR(n);
4258 n = CHILD(n, 0);
4259 } else {
4260 sc.c_encoding = NULL;
4261 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004262 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004263 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004264 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004265 /* c_symtable still points to parent's symbols */
4266 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004267 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004268 sc.c_nested = 1;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00004269 sc.c_flags |= base->c_flags & PyCF_MASK;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00004270 if (base->c_encoding != NULL) {
4271 assert(sc.c_encoding == NULL);
4272 sc.c_encoding = base->c_encoding;
4273 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004274 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004275 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004276 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004277 if (sc.c_future == NULL) {
4278 com_free(&sc);
4279 return NULL;
4280 }
Jeremy Hylton481081e2001-08-14 20:01:59 +00004281 if (flags) {
4282 int merged = sc.c_future->ff_features |
4283 flags->cf_flags;
4284 sc.c_future->ff_features = merged;
4285 flags->cf_flags = merged;
4286 }
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004287 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004288 com_free(&sc);
4289 return NULL;
4290 }
4291 }
4292 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004293 if (symtable_load_symbols(&sc) < 0) {
4294 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004295 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004296 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004297 compile_node(&sc, n);
4298 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00004299 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004300 PyObject *consts, *names, *varnames, *filename, *name,
4301 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004302 consts = PyList_AsTuple(sc.c_consts);
4303 names = PyList_AsTuple(sc.c_names);
4304 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004305 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4306 freevars = dict_keys_inorder(sc.c_freevars,
4307 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00004308 filename = PyString_InternFromString(sc.c_filename);
4309 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004310 if (!PyErr_Occurred())
4311 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004312 sc.c_nlocals,
4313 sc.c_maxstacklevel,
4314 sc.c_flags,
4315 sc.c_code,
4316 consts,
4317 names,
4318 varnames,
4319 freevars,
4320 cellvars,
4321 filename,
4322 name,
4323 sc.c_firstlineno,
4324 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004325 Py_XDECREF(consts);
4326 Py_XDECREF(names);
4327 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004328 Py_XDECREF(freevars);
4329 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004330 Py_XDECREF(filename);
4331 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004332 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004333 else if (!PyErr_Occurred()) {
4334 /* This could happen if someone called PyErr_Clear() after an
4335 error was reported above. That's not supposed to happen,
4336 but I just plugged one case and I'm not sure there can't be
4337 others. In that case, raise SystemError so that at least
4338 it gets reported instead dumping core. */
4339 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4340 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004341 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004342 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004343 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004344 sc.c_symtable = NULL;
4345 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004346 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004347 return co;
4348}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004349
4350int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004351PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004352{
4353 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004354 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004355 int line = co->co_firstlineno;
4356 int addr = 0;
4357 while (--size >= 0) {
4358 addr += *p++;
4359 if (addr > addrq)
4360 break;
4361 line += *p++;
4362 }
4363 return line;
4364}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004365
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004366/* The test for LOCAL must come before the test for FREE in order to
4367 handle classes where name is both local and free. The local var is
4368 a method and the free var is a free var referenced within a method.
4369*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004370
4371static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004372get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004373{
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004374 char buf[350];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004375 PyObject *v;
Jeremy Hylton51257732001-03-01 00:42:55 +00004376
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004377 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4378 return CELL;
4379 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4380 return LOCAL;
4381 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4382 return FREE;
4383 v = PyDict_GetItemString(c->c_globals, name);
4384 if (v) {
4385 if (v == Py_None)
4386 return GLOBAL_EXPLICIT;
4387 else {
4388 return GLOBAL_IMPLICIT;
4389 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004390 }
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00004391 PyOS_snprintf(buf, sizeof(buf),
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004392 "unknown scope for %.100s in %.100s(%s) "
4393 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4394 name, c->c_name,
4395 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4396 c->c_filename,
4397 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4398 PyObject_REPR(c->c_locals),
4399 PyObject_REPR(c->c_globals)
4400 );
4401
4402 Py_FatalError(buf);
4403 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004404}
4405
Guido van Rossum207fda62001-03-02 03:30:41 +00004406/* Helper functions to issue warnings */
4407
4408static int
4409issue_warning(char *msg, char *filename, int lineno)
4410{
4411 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4412 lineno, NULL, NULL) < 0) {
4413 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4414 PyErr_SetString(PyExc_SyntaxError, msg);
4415 PyErr_SyntaxLocation(filename, lineno);
4416 }
4417 return -1;
4418 }
4419 return 0;
4420}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004421
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004422static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004423symtable_warn(struct symtable *st, char *msg)
4424{
Guido van Rossum207fda62001-03-02 03:30:41 +00004425 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004426 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004427 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004428 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004429 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004430}
4431
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004432/* Helper function for setting lineno and filename */
4433
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004434static int
4435symtable_build(struct compiling *c, node *n)
4436{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004437 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004438 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004439 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004440 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004441 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4442 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004443 return -1;
4444 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004445 if (c->c_symtable->st_errors > 0)
4446 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004447 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004448 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004449 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004450 return 0;
4451}
4452
4453static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004454symtable_init_compiling_symbols(struct compiling *c)
4455{
4456 PyObject *varnames;
4457
4458 varnames = c->c_symtable->st_cur->ste_varnames;
4459 if (varnames == NULL) {
4460 varnames = PyList_New(0);
4461 if (varnames == NULL)
4462 return -1;
4463 c->c_symtable->st_cur->ste_varnames = varnames;
4464 Py_INCREF(varnames);
4465 } else
4466 Py_INCREF(varnames);
4467 c->c_varnames = varnames;
4468
4469 c->c_globals = PyDict_New();
4470 if (c->c_globals == NULL)
4471 return -1;
4472 c->c_freevars = PyDict_New();
4473 if (c->c_freevars == NULL)
4474 return -1;
4475 c->c_cellvars = PyDict_New();
4476 if (c->c_cellvars == NULL)
4477 return -1;
4478 return 0;
4479}
4480
4481struct symbol_info {
4482 int si_nlocals;
4483 int si_ncells;
4484 int si_nfrees;
4485 int si_nimplicit;
4486};
4487
4488static void
4489symtable_init_info(struct symbol_info *si)
4490{
4491 si->si_nlocals = 0;
4492 si->si_ncells = 0;
4493 si->si_nfrees = 0;
4494 si->si_nimplicit = 0;
4495}
4496
4497static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004498symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004499 struct symbol_info *si)
4500{
4501 PyObject *dict, *v;
4502
4503 /* Seperate logic for DEF_FREE. If it occurs in a function,
4504 it indicates a local that we must allocate storage for (a
4505 cell var). If it occurs in a class, then the class has a
4506 method and a free variable with the same name.
4507 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004508 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004509 /* If it isn't declared locally, it can't be a cell. */
4510 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4511 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004512 v = PyInt_FromLong(si->si_ncells++);
4513 dict = c->c_cellvars;
4514 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004515 /* If it is free anyway, then there is no need to do
4516 anything here.
4517 */
4518 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004519 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004520 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004521 v = PyInt_FromLong(si->si_nfrees++);
4522 dict = c->c_freevars;
4523 }
4524 if (v == NULL)
4525 return -1;
4526 if (PyDict_SetItem(dict, name, v) < 0) {
4527 Py_DECREF(v);
4528 return -1;
4529 }
4530 Py_DECREF(v);
4531 return 0;
4532}
4533
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004534/* If a variable is a cell and an argument, make sure that appears in
4535 co_cellvars before any variable to its right in varnames.
4536*/
4537
4538
4539static int
4540symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4541 PyObject *varnames, int flags)
4542{
4543 PyObject *v, *w, *d, *list = NULL;
4544 int i, pos;
4545
4546 if (flags & CO_VARARGS)
4547 argcount++;
4548 if (flags & CO_VARKEYWORDS)
4549 argcount++;
4550 for (i = argcount; --i >= 0; ) {
4551 v = PyList_GET_ITEM(varnames, i);
4552 if (PyDict_GetItem(*cellvars, v)) {
4553 if (list == NULL) {
4554 list = PyList_New(1);
4555 if (list == NULL)
4556 return -1;
4557 PyList_SET_ITEM(list, 0, v);
4558 Py_INCREF(v);
4559 } else
4560 PyList_Insert(list, 0, v);
4561 }
4562 }
4563 if (list == NULL || PyList_GET_SIZE(list) == 0)
4564 return 0;
4565 /* There are cellvars that are also arguments. Create a dict
4566 to replace cellvars and put the args at the front.
4567 */
4568 d = PyDict_New();
4569 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4570 v = PyInt_FromLong(i);
4571 if (v == NULL)
4572 goto fail;
4573 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4574 goto fail;
4575 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4576 goto fail;
4577 }
4578 pos = 0;
4579 i = PyList_GET_SIZE(list);
4580 Py_DECREF(list);
4581 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4582 w = PyInt_FromLong(i++); /* don't care about the old key */
4583 if (PyDict_SetItem(d, v, w) < 0) {
4584 Py_DECREF(w);
4585 goto fail;
4586 }
4587 Py_DECREF(w);
4588 }
4589 Py_DECREF(*cellvars);
4590 *cellvars = d;
4591 return 1;
4592 fail:
4593 Py_DECREF(d);
4594 return -1;
4595}
4596
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004597static int
4598symtable_freevar_offsets(PyObject *freevars, int offset)
4599{
4600 PyObject *name, *v;
4601 int pos;
4602
4603 /* The cell vars are the first elements of the closure,
4604 followed by the free vars. Update the offsets in
4605 c_freevars to account for number of cellvars. */
4606 pos = 0;
4607 while (PyDict_Next(freevars, &pos, &name, &v)) {
4608 int i = PyInt_AS_LONG(v) + offset;
4609 PyObject *o = PyInt_FromLong(i);
4610 if (o == NULL)
4611 return -1;
4612 if (PyDict_SetItem(freevars, name, o) < 0) {
4613 Py_DECREF(o);
4614 return -1;
4615 }
4616 Py_DECREF(o);
4617 }
4618 return 0;
4619}
4620
4621static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004622symtable_check_unoptimized(struct compiling *c,
4623 PySymtableEntryObject *ste,
4624 struct symbol_info *si)
4625{
4626 char buf[300];
4627
4628 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4629 || (ste->ste_nested && si->si_nimplicit)))
4630 return 0;
4631
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004632#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4633
4634#define ILLEGAL_IS "is a nested function"
4635
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004636#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004637"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004638
4639#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004640"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004641
4642#define ILLEGAL_EXEC_AND_IMPORT_STAR \
Neal Norwitz150d09d2002-01-29 00:56:37 +00004643"function '%.100s' uses import * and bare exec, which are illegal " \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004644"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004645
4646 /* XXX perhaps the linenos for these opt-breaking statements
4647 should be stored so the exception can point to them. */
4648
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004649 if (ste->ste_child_free) {
4650 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004651 PyOS_snprintf(buf, sizeof(buf),
4652 ILLEGAL_IMPORT_STAR,
4653 PyString_AS_STRING(ste->ste_name),
4654 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004655 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004656 PyOS_snprintf(buf, sizeof(buf),
4657 ILLEGAL_BARE_EXEC,
4658 PyString_AS_STRING(ste->ste_name),
4659 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004660 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004661 PyOS_snprintf(buf, sizeof(buf),
4662 ILLEGAL_EXEC_AND_IMPORT_STAR,
4663 PyString_AS_STRING(ste->ste_name),
4664 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004665 }
4666 } else {
4667 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004668 PyOS_snprintf(buf, sizeof(buf),
4669 ILLEGAL_IMPORT_STAR,
4670 PyString_AS_STRING(ste->ste_name),
4671 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004672 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004673 PyOS_snprintf(buf, sizeof(buf),
4674 ILLEGAL_BARE_EXEC,
4675 PyString_AS_STRING(ste->ste_name),
4676 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004677 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004678 PyOS_snprintf(buf, sizeof(buf),
4679 ILLEGAL_EXEC_AND_IMPORT_STAR,
4680 PyString_AS_STRING(ste->ste_name),
4681 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004682 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004683 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004684
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004685 PyErr_SetString(PyExc_SyntaxError, buf);
4686 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4687 ste->ste_opt_lineno);
4688 return -1;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004689}
4690
4691static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004692symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4693 struct symbol_info *si)
4694{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004695 if (c->c_future)
4696 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004697 if (ste->ste_generator)
4698 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004699 if (ste->ste_type != TYPE_MODULE)
4700 c->c_flags |= CO_NEWLOCALS;
4701 if (ste->ste_type == TYPE_FUNCTION) {
4702 c->c_nlocals = si->si_nlocals;
4703 if (ste->ste_optimized == 0)
4704 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004705 else if (ste->ste_optimized != OPT_EXEC)
4706 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004707 }
4708 return 0;
4709}
4710
4711static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004712symtable_load_symbols(struct compiling *c)
4713{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004714 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004715 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004716 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004717 PyObject *name, *varnames, *v;
4718 int i, flags, pos;
4719 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004720
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004721 if (implicit == NULL) {
4722 implicit = PyInt_FromLong(1);
4723 if (implicit == NULL)
4724 return -1;
4725 }
4726 v = NULL;
4727
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004728 if (symtable_init_compiling_symbols(c) < 0)
4729 goto fail;
4730 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004731 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004732 si.si_nlocals = PyList_GET_SIZE(varnames);
4733 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004734
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004735 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004736 v = PyInt_FromLong(i);
4737 if (PyDict_SetItem(c->c_locals,
4738 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004739 goto fail;
4740 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004741 }
4742
4743 /* XXX The cases below define the rules for whether a name is
4744 local or global. The logic could probably be clearer. */
4745 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004746 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4747 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004748
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004749 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004750 /* undo the original DEF_FREE */
4751 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004752
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004753 /* Deal with names that need two actions:
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004754 1. Cell variables that are also locals.
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004755 2. Free variables in methods that are also class
4756 variables or declared global.
4757 */
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004758 if (flags & (DEF_FREE | DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004759 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004760
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004761 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004762 c->c_argcount--;
4763 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004764 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004765 c->c_argcount--;
4766 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004767 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004768 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004769 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004770 if (flags & DEF_PARAM) {
4771 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004772 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004773 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004774 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004775 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004776 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004777 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004778 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4779 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004780 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004781 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004782 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4783 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004784 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004785 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004786 if (v == NULL)
4787 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004788 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004789 goto fail;
4790 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004791 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004792 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004793 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004794 } else if (is_free(flags)) {
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004795 if (ste->ste_nested) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004796 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004797 if (v == NULL)
4798 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004799 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004800 goto fail;
4801 Py_DECREF(v);
4802 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004803 si.si_nimplicit++;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004804 if (PyDict_SetItem(c->c_globals, name,
4805 implicit) < 0)
4806 goto fail;
4807 if (st->st_nscopes != 1) {
4808 v = PyInt_FromLong(flags);
4809 if (PyDict_SetItem(st->st_global,
4810 name, v))
4811 goto fail;
4812 Py_DECREF(v);
4813 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004814 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004815 }
4816 }
4817
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004818 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4819
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004820 if (si.si_ncells > 1) { /* one cell is always in order */
4821 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4822 c->c_varnames, c->c_flags) < 0)
4823 return -1;
4824 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004825 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4826 return -1;
4827 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004828 fail:
4829 /* is this always the right thing to do? */
4830 Py_XDECREF(v);
4831 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004832}
4833
4834static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004835symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004836{
4837 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004838
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00004839 st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004840 if (st == NULL)
4841 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004842 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004843
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004844 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004845 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004846 goto fail;
4847 if ((st->st_symbols = PyDict_New()) == NULL)
4848 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004849 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004850 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004851 st->st_errors = 0;
4852 st->st_tmpname = 0;
4853 st->st_private = NULL;
4854 return st;
4855 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004856 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004857 return NULL;
4858}
4859
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004860void
4861PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004862{
4863 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004864 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004865 Py_XDECREF(st->st_cur);
Andrew MacIntyre80d4e2a2002-08-04 06:28:21 +00004866 PyObject_FREE((void *)st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004867}
4868
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004869/* When the compiler exits a scope, it must should update the scope's
4870 free variable information with the list of free variables in its
4871 children.
4872
4873 Variables that are free in children and defined in the current
4874 scope are cellvars.
4875
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004876 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004877 false), free variables in children that are not defined here are
4878 implicit globals.
4879
4880*/
4881
4882static int
4883symtable_update_free_vars(struct symtable *st)
4884{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004885 int i, j, def;
4886 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004887 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004888
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004889 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004890 def = DEF_FREE_CLASS;
4891 else
4892 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004893 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004894 int pos = 0;
4895
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004896 if (list)
4897 PyList_SetSlice(list, 0,
4898 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004899 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004900 PyList_GET_ITEM(ste->ste_children, i);
4901 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004902 int flags = PyInt_AS_LONG(o);
4903 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004904 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004905 if (list == NULL) {
4906 list = PyList_New(0);
4907 if (list == NULL)
4908 return -1;
4909 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004910 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004911 if (PyList_Append(list, name) < 0) {
4912 Py_DECREF(list);
4913 return -1;
4914 }
4915 }
4916 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004917 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004918 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004919 v = PyDict_GetItem(ste->ste_symbols, name);
4920 /* If a name N is declared global in scope A and
4921 referenced in scope B contained (perhaps
4922 indirectly) in A and there are no scopes
4923 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004924 is global in B. Unless A is a class scope,
4925 because class scopes are not considered for
4926 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004927 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004928 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004929 int flags = PyInt_AS_LONG(v);
4930 if (flags & DEF_GLOBAL) {
4931 symtable_undo_free(st, child->ste_id,
4932 name);
4933 continue;
4934 }
4935 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004936 if (ste->ste_nested) {
4937 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004938 name, def) < 0) {
4939 Py_DECREF(list);
4940 return -1;
4941 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004942 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004943 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004944 name) < 0) {
4945 Py_DECREF(list);
4946 return -1;
4947 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004948 }
4949 }
4950 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004951
4952 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004953 return 0;
4954}
4955
4956/* If the current scope is a non-nested class or if name is not
4957 defined in the current, non-nested scope, then it is an implicit
4958 global in all nested scopes.
4959*/
4960
4961static int
4962symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4963{
4964 PyObject *o;
4965 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004966 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004967
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004968 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004969 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004970 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004971 if (o == NULL)
4972 return symtable_undo_free(st, child, name);
4973 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004974
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004975 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004976 return symtable_undo_free(st, child, name);
4977 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004978 return symtable_add_def_o(st, ste->ste_symbols,
4979 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004980}
4981
4982static int
4983symtable_undo_free(struct symtable *st, PyObject *id,
4984 PyObject *name)
4985{
4986 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004987 PyObject *info;
4988 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004989
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004990 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4991 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004992 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004993
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004994 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004995 if (info == NULL)
4996 return 0;
4997 v = PyInt_AS_LONG(info);
4998 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004999 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005000 DEF_FREE_GLOBAL) < 0)
5001 return -1;
5002 } else
5003 /* If the name is defined here or declared global,
5004 then the recursion stops. */
5005 return 0;
5006
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005007 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
5008 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00005009 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005010 PyList_GET_ITEM(ste->ste_children, i);
5011 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005012 if (x < 0)
5013 return x;
5014 }
5015 return 0;
5016}
5017
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005018/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
5019 This reference is released when the scope is exited, via the DECREF
5020 in symtable_exit_scope().
5021*/
5022
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005023static int
5024symtable_exit_scope(struct symtable *st)
5025{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005026 int end;
5027
Jeremy Hylton4419ac12001-02-28 22:54:51 +00005028 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005029 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005030 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005031 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005032 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
5033 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005034 if (PySequence_DelItem(st->st_stack, end) < 0)
5035 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005036 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005037}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005038
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005039static void
5040symtable_enter_scope(struct symtable *st, char *name, int type,
5041 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005042{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005043 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005044
5045 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005046 prev = st->st_cur;
5047 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
5048 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005049 st->st_errors++;
5050 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005051 }
5052 }
Barry Warsaw0372af72001-02-23 18:22:59 +00005053 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005054 PySymtableEntry_New(st, name, type, lineno);
5055 if (strcmp(name, TOP) == 0)
5056 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00005057 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005058 if (PyList_Append(prev->ste_children,
5059 (PyObject *)st->st_cur) < 0)
5060 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00005061 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005062}
5063
5064static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005065symtable_lookup(struct symtable *st, char *name)
5066{
5067 char buffer[MANGLE_LEN];
5068 PyObject *v;
5069 int flags;
5070
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00005071 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005072 name = buffer;
5073 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
5074 if (v == NULL) {
5075 if (PyErr_Occurred())
5076 return -1;
5077 else
5078 return 0;
5079 }
5080
5081 flags = PyInt_AS_LONG(v);
5082 return flags;
5083}
5084
5085static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005086symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005087{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005088 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005089 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005090 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005091
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00005092 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005093 name = buffer;
5094 if ((s = PyString_InternFromString(name)) == NULL)
5095 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005096 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
5097 Py_DECREF(s);
5098 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005099}
5100
5101/* Must only be called with mangled names */
5102
5103static int
5104symtable_add_def_o(struct symtable *st, PyObject *dict,
5105 PyObject *name, int flag)
5106{
5107 PyObject *o;
5108 int val;
5109
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005110 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005111 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005112 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005113 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00005114 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005115 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005116 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005117 return -1;
5118 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005119 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005120 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005121 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005122 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005123 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005124 Py_DECREF(o);
5125 return -1;
5126 }
5127 Py_DECREF(o);
5128
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005129 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005130 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005131 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005132 } else if (flag & DEF_GLOBAL) {
5133 /* XXX need to update DEF_GLOBAL for other flags too;
5134 perhaps only DEF_FREE_GLOBAL */
5135 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005136 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005137 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005138 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005139 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005140 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005141 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005142 Py_DECREF(o);
5143 return -1;
5144 }
5145 Py_DECREF(o);
5146 }
5147 return 0;
5148}
5149
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005150#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005151
Tim Peters08a898f2001-06-28 01:52:22 +00005152/* Look for a yield stmt under n. Return 1 if found, else 0.
5153 This hack is used to look inside "if 0:" blocks (which are normally
5154 ignored) in case those are the only places a yield occurs (so that this
5155 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00005156static int
5157look_for_yield(node *n)
5158{
5159 int i;
5160
5161 for (i = 0; i < NCH(n); ++i) {
5162 node *kid = CHILD(n, i);
5163
5164 switch (TYPE(kid)) {
5165
5166 case classdef:
5167 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00005168 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00005169 /* Stuff in nested functions and classes can't make
5170 the parent a generator. */
5171 return 0;
5172
5173 case yield_stmt:
5174 return 1;
5175
5176 default:
5177 if (look_for_yield(kid))
5178 return 1;
5179 }
5180 }
5181 return 0;
5182}
5183
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005184static void
5185symtable_node(struct symtable *st, node *n)
5186{
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005187 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005188
5189 loop:
5190 switch (TYPE(n)) {
5191 case funcdef: {
5192 char *func_name = STR(CHILD(n, 1));
5193 symtable_add_def(st, func_name, DEF_LOCAL);
5194 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005195 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005196 symtable_funcdef(st, n);
5197 symtable_exit_scope(st);
5198 break;
5199 }
5200 case lambdef:
5201 if (NCH(n) == 4)
5202 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005203 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005204 symtable_funcdef(st, n);
5205 symtable_exit_scope(st);
5206 break;
5207 case classdef: {
5208 char *tmp, *class_name = STR(CHILD(n, 1));
5209 symtable_add_def(st, class_name, DEF_LOCAL);
5210 if (TYPE(CHILD(n, 2)) == LPAR) {
5211 node *bases = CHILD(n, 3);
5212 int i;
5213 for (i = 0; i < NCH(bases); i += 2) {
5214 symtable_node(st, CHILD(bases, i));
5215 }
5216 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005217 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005218 tmp = st->st_private;
5219 st->st_private = class_name;
5220 symtable_node(st, CHILD(n, NCH(n) - 1));
5221 st->st_private = tmp;
5222 symtable_exit_scope(st);
5223 break;
5224 }
5225 case if_stmt:
5226 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00005227 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5228 if (st->st_cur->ste_generator == 0)
5229 st->st_cur->ste_generator =
5230 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005231 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00005232 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005233 symtable_node(st, CHILD(n, i + 1));
5234 symtable_node(st, CHILD(n, i + 3));
5235 }
5236 if (i + 2 < NCH(n))
5237 symtable_node(st, CHILD(n, i + 2));
5238 break;
5239 case global_stmt:
5240 symtable_global(st, n);
5241 break;
5242 case import_stmt:
5243 symtable_import(st, n);
5244 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005245 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005246 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005247 symtable_node(st, CHILD(n, 1));
5248 if (NCH(n) > 2)
5249 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005250 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005251 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005252 st->st_cur->ste_opt_lineno = n->n_lineno;
5253 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005254 if (NCH(n) > 4)
5255 symtable_node(st, CHILD(n, 5));
5256 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005257
5258 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00005259 case assert_stmt:
5260 if (Py_OptimizeFlag)
5261 return;
5262 if (NCH(n) == 2) {
5263 n = CHILD(n, 1);
5264 goto loop;
5265 } else {
5266 symtable_node(st, CHILD(n, 1));
5267 n = CHILD(n, 3);
5268 goto loop;
5269 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005270 case except_clause:
5271 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005272 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005273 if (NCH(n) > 1) {
5274 n = CHILD(n, 1);
5275 goto loop;
5276 }
5277 break;
5278 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005279 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005280 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005281 case yield_stmt:
5282 st->st_cur->ste_generator = 1;
5283 n = CHILD(n, 1);
5284 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005285 case expr_stmt:
5286 if (NCH(n) == 1)
5287 n = CHILD(n, 0);
5288 else {
5289 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005290 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005291 symtable_node(st, CHILD(n, 2));
5292 break;
5293 } else {
5294 int i;
5295 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005296 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005297 n = CHILD(n, NCH(n) - 1);
5298 }
5299 }
5300 goto loop;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005301 case list_iter:
5302 n = CHILD(n, 0);
5303 if (TYPE(n) == list_for) {
5304 st->st_tmpname++;
5305 symtable_list_comprehension(st, n);
5306 st->st_tmpname--;
5307 } else {
5308 REQ(n, list_if);
5309 symtable_node(st, CHILD(n, 1));
5310 if (NCH(n) == 3) {
5311 n = CHILD(n, 2);
5312 goto loop;
5313 }
5314 }
5315 break;
5316 case for_stmt:
5317 symtable_assign(st, CHILD(n, 1), 0);
5318 for (i = 3; i < NCH(n); ++i)
5319 if (TYPE(CHILD(n, i)) >= single_input)
5320 symtable_node(st, CHILD(n, i));
5321 break;
5322 /* The remaining cases fall through to default except in
5323 special circumstances. This requires the individual cases
5324 to be coded with great care, even though they look like
5325 rather innocuous. Each case must double-check TYPE(n).
5326 */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005327 case argument:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005328 if (TYPE(n) == argument && NCH(n) == 3) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005329 n = CHILD(n, 2);
5330 goto loop;
5331 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005332 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005333 case listmaker:
5334 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005335 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005336 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005337 symtable_node(st, CHILD(n, 0));
5338 st->st_tmpname--;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005339 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005340 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005341 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005342 case atom:
5343 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5344 symtable_add_use(st, STR(CHILD(n, 0)));
5345 break;
5346 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005347 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005348 default:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005349 /* Walk over every non-token child with a special case
5350 for one child.
5351 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005352 if (NCH(n) == 1) {
5353 n = CHILD(n, 0);
5354 goto loop;
5355 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005356 for (i = 0; i < NCH(n); ++i)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005357 if (TYPE(CHILD(n, i)) >= single_input)
5358 symtable_node(st, CHILD(n, i));
5359 }
5360}
5361
5362static void
5363symtable_funcdef(struct symtable *st, node *n)
5364{
5365 node *body;
5366
5367 if (TYPE(n) == lambdef) {
5368 if (NCH(n) == 4)
5369 symtable_params(st, CHILD(n, 1));
5370 } else
5371 symtable_params(st, CHILD(n, 2));
5372 body = CHILD(n, NCH(n) - 1);
5373 symtable_node(st, body);
5374}
5375
5376/* The next two functions parse the argument tuple.
5377 symtable_default_arg() checks for names in the default arguments,
5378 which are references in the defining scope. symtable_params()
5379 parses the parameter names, which are defined in the function's
5380 body.
5381
5382 varargslist:
5383 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5384 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5385*/
5386
5387static void
5388symtable_default_args(struct symtable *st, node *n)
5389{
5390 node *c;
5391 int i;
5392
5393 if (TYPE(n) == parameters) {
5394 n = CHILD(n, 1);
5395 if (TYPE(n) == RPAR)
5396 return;
5397 }
5398 REQ(n, varargslist);
5399 for (i = 0; i < NCH(n); i += 2) {
5400 c = CHILD(n, i);
5401 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5402 break;
5403 }
5404 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5405 symtable_node(st, CHILD(n, i));
5406 }
5407}
5408
5409static void
5410symtable_params(struct symtable *st, node *n)
5411{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005412 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005413 node *c = NULL;
5414
5415 if (TYPE(n) == parameters) {
5416 n = CHILD(n, 1);
5417 if (TYPE(n) == RPAR)
5418 return;
5419 }
5420 REQ(n, varargslist);
5421 for (i = 0; i < NCH(n); i += 2) {
5422 c = CHILD(n, i);
5423 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5424 ext = 1;
5425 break;
5426 }
5427 if (TYPE(c) == test) {
5428 continue;
5429 }
5430 if (TYPE(CHILD(c, 0)) == NAME)
5431 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5432 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005433 char nbuf[30];
5434 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005435 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005436 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005437 }
5438 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005439 if (ext) {
5440 c = CHILD(n, i);
5441 if (TYPE(c) == STAR) {
5442 i++;
5443 symtable_add_def(st, STR(CHILD(n, i)),
5444 DEF_PARAM | DEF_STAR);
5445 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005446 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005447 c = NULL;
5448 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005449 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005450 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005451 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005452 i++;
5453 symtable_add_def(st, STR(CHILD(n, i)),
5454 DEF_PARAM | DEF_DOUBLESTAR);
5455 }
5456 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005457 if (complex >= 0) {
5458 int j;
5459 for (j = 0; j <= complex; j++) {
5460 c = CHILD(n, j);
5461 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005462 c = CHILD(n, ++j);
5463 else if (TYPE(c) == EQUAL)
5464 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005465 if (TYPE(CHILD(c, 0)) == LPAR)
5466 symtable_params_fplist(st, CHILD(c, 1));
5467 }
5468 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005469}
5470
5471static void
5472symtable_params_fplist(struct symtable *st, node *n)
5473{
5474 int i;
5475 node *c;
5476
5477 REQ(n, fplist);
5478 for (i = 0; i < NCH(n); i += 2) {
5479 c = CHILD(n, i);
5480 REQ(c, fpdef);
5481 if (NCH(c) == 1)
5482 symtable_add_def(st, STR(CHILD(c, 0)),
5483 DEF_PARAM | DEF_INTUPLE);
5484 else
5485 symtable_params_fplist(st, CHILD(c, 1));
5486 }
5487
5488}
5489
5490static void
5491symtable_global(struct symtable *st, node *n)
5492{
5493 int i;
5494
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005495 /* XXX It might be helpful to warn about module-level global
5496 statements, but it's hard to tell the difference between
5497 module-level and a string passed to exec.
5498 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005499
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005500 for (i = 1; i < NCH(n); i += 2) {
5501 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005502 int flags;
5503
5504 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005505 if (flags < 0)
5506 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005507 if (flags && flags != DEF_GLOBAL) {
5508 char buf[500];
5509 if (flags & DEF_PARAM) {
5510 PyErr_Format(PyExc_SyntaxError,
5511 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005512 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005513 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005514 st->st_cur->ste_lineno);
5515 st->st_errors++;
5516 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005517 }
5518 else {
5519 if (flags & DEF_LOCAL)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005520 PyOS_snprintf(buf, sizeof(buf),
5521 GLOBAL_AFTER_ASSIGN,
5522 name);
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005523 else
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005524 PyOS_snprintf(buf, sizeof(buf),
5525 GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005526 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005527 }
5528 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005529 symtable_add_def(st, name, DEF_GLOBAL);
5530 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005531}
5532
5533static void
5534symtable_list_comprehension(struct symtable *st, node *n)
5535{
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005536 char tmpname[30];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005537
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005538 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005539 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005540 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005541 symtable_node(st, CHILD(n, 3));
5542 if (NCH(n) == 5)
5543 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005544}
5545
5546static void
5547symtable_import(struct symtable *st, node *n)
5548{
5549 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005550 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005551 | 'from' dotted_name 'import'
5552 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005553 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005554 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005555 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005556 node *dotname = CHILD(n, 1);
5557 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5558 /* check for bogus imports */
5559 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5560 PyErr_SetString(PyExc_SyntaxError,
5561 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005562 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005563 n->n_lineno);
5564 st->st_errors++;
5565 return;
5566 }
5567 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005568 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005569 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005570 if (symtable_warn(st,
5571 "import * only allowed at module level") < 0)
5572 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005573 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005574 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005575 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005576 } else {
5577 for (i = 3; i < NCH(n); i += 2) {
5578 node *c = CHILD(n, i);
5579 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005580 symtable_assign(st, CHILD(c, 2),
5581 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005582 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005583 symtable_assign(st, CHILD(c, 0),
5584 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005585 }
5586 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005587 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005588 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005589 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005590 }
5591 }
5592}
5593
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005594/* The third argument to symatble_assign() is a flag to be passed to
5595 symtable_add_def() if it is eventually called. The flag is useful
5596 to specify the particular type of assignment that should be
5597 recorded, e.g. an assignment caused by import.
5598 */
5599
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005600static void
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005601symtable_assign(struct symtable *st, node *n, int def_flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005602{
5603 node *tmp;
5604 int i;
5605
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005606 loop:
5607 switch (TYPE(n)) {
5608 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005609 /* invalid assignment, e.g. lambda x:x=2. The next
5610 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005611 return;
5612 case power:
5613 if (NCH(n) > 2) {
5614 for (i = 2; i < NCH(n); ++i)
5615 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5616 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005617 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005618 if (NCH(n) > 1) {
5619 symtable_node(st, CHILD(n, 0));
5620 symtable_node(st, CHILD(n, 1));
5621 } else {
5622 n = CHILD(n, 0);
5623 goto loop;
5624 }
5625 return;
5626 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005627 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5628 /* XXX This is an error, but the next pass
5629 will catch it. */
5630 return;
5631 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005632 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005633 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005634 }
5635 return;
5636 case exprlist:
5637 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00005638 case testlist1:
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005639 if (NCH(n) == 1) {
5640 n = CHILD(n, 0);
5641 goto loop;
5642 }
5643 else {
5644 int i;
5645 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005646 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005647 return;
5648 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005649 case atom:
5650 tmp = CHILD(n, 0);
5651 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5652 n = CHILD(n, 1);
5653 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005654 } else if (TYPE(tmp) == NAME) {
Jeremy Hylton778e2652001-11-09 19:50:08 +00005655 if (strcmp(STR(tmp), "__debug__") == 0) {
5656 PyErr_SetString(PyExc_SyntaxError,
5657 ASSIGN_DEBUG);
5658 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton86424e32001-12-04 02:41:46 +00005659 n->n_lineno);
Jeremy Hylton778e2652001-11-09 19:50:08 +00005660 st->st_errors++;
5661 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005662 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005663 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005664 return;
5665 case dotted_as_name:
5666 if (NCH(n) == 3)
5667 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005668 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005669 else
5670 symtable_add_def(st,
5671 STR(CHILD(CHILD(n,
5672 0), 0)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005673 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005674 return;
5675 case dotted_name:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005676 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005677 return;
5678 case NAME:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005679 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005680 return;
5681 default:
5682 if (NCH(n) == 0)
5683 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005684 if (NCH(n) == 1) {
5685 n = CHILD(n, 0);
5686 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005687 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005688 /* Should only occur for errors like x + 1 = 1,
5689 which will be caught in the next pass. */
5690 for (i = 0; i < NCH(n); ++i)
5691 if (TYPE(CHILD(n, i)) >= single_input)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005692 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005693 }
5694}