blob: fa53b1ea85a326649c0d8b099a2fe0eae7b2d5b5 [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__ */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000488};
489
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000490static int
491is_free(int v)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000492{
493 if ((v & (USE | DEF_FREE))
494 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
495 return 1;
496 if (v & DEF_FREE_CLASS)
497 return 1;
498 return 0;
499}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000500
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000501static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000502com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000503{
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000504 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
505
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000506 if (c == NULL) {
507 /* Error occurred via symtable call to
508 is_constant_false */
509 PyErr_SetString(exc, msg);
510 return;
511 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000512 c->c_errors++;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000513 if (c->c_lineno < 1 || c->c_interactive) {
514 /* Unknown line number or interactive input */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000515 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000516 return;
517 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000518 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000519 if (v == NULL)
520 return; /* MemoryError, too bad */
Fred Drakedcf08e02000-08-15 15:49:44 +0000521
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000522 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000523 if (line == NULL) {
524 Py_INCREF(Py_None);
525 line = Py_None;
526 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000527 if (exc == PyExc_SyntaxError) {
528 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
529 Py_None, line);
530 if (t == NULL)
531 goto exit;
532 w = Py_BuildValue("(OO)", v, t);
533 if (w == NULL)
534 goto exit;
535 PyErr_SetObject(exc, w);
536 } else {
537 /* Make sure additional exceptions are printed with
538 file and line, also. */
539 PyErr_SetObject(exc, v);
540 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
541 }
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000542 exit:
543 Py_XDECREF(t);
544 Py_XDECREF(v);
545 Py_XDECREF(w);
546 Py_XDECREF(line);
Guido van Rossum452a9831996-09-17 14:32:04 +0000547}
548
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000549/* Interface to the block stack */
550
551static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000552block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000553{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000554 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000555 com_error(c, PyExc_SystemError,
556 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000557 }
558 else {
559 c->c_block[c->c_nblocks++] = type;
560 }
561}
562
563static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000564block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000565{
566 if (c->c_nblocks > 0)
567 c->c_nblocks--;
568 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000569 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000570 }
571}
572
Guido van Rossum681d79a1995-07-18 14:51:37 +0000573/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000574
Tim Petersdbd9ba62000-07-09 03:09:57 +0000575static int com_init(struct compiling *, char *);
576static void com_free(struct compiling *);
577static void com_push(struct compiling *, int);
578static void com_pop(struct compiling *, int);
579static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000580static void com_node(struct compiling *, node *);
581static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000582static void com_addbyte(struct compiling *, int);
583static void com_addint(struct compiling *, int);
584static void com_addoparg(struct compiling *, int, int);
585static void com_addfwref(struct compiling *, int, int *);
586static void com_backpatch(struct compiling *, int);
587static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
588static int com_addconst(struct compiling *, PyObject *);
589static int com_addname(struct compiling *, PyObject *);
590static void com_addopname(struct compiling *, int, node *);
591static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000592static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000593static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000594static void com_assign(struct compiling *, node *, int, node *);
595static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000596static PyCodeObject *icompile(node *, struct compiling *);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000597static PyCodeObject *jcompile(node *, char *, struct compiling *,
598 PyCompilerFlags *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000599static PyObject *parsestrplus(struct compiling*, node *);
600static PyObject *parsestr(struct compiling *, char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000601static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000602
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000603static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000604
605/* symtable operations */
606static int symtable_build(struct compiling *, node *);
607static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000608static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000609static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000610static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000611static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000612static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000613
614static void symtable_node(struct symtable *, node *);
615static void symtable_funcdef(struct symtable *, node *);
616static void symtable_default_args(struct symtable *, node *);
617static void symtable_params(struct symtable *, node *);
618static void symtable_params_fplist(struct symtable *, node *n);
619static void symtable_global(struct symtable *, node *);
620static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000621static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000622static void symtable_list_comprehension(struct symtable *, node *);
623
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000624static int symtable_update_free_vars(struct symtable *);
625static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
626static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
627
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000628/* helper */
629static void
630do_pad(int pad)
631{
632 int i;
633 for (i = 0; i < pad; ++i)
634 fprintf(stderr, " ");
635}
636
637static void
638dump(node *n, int pad, int depth)
639{
640 int i;
641 if (depth == 0)
642 return;
643 do_pad(pad);
644 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
645 if (depth > 0)
646 depth--;
647 for (i = 0; i < NCH(n); ++i)
648 dump(CHILD(n, i), pad + 1, depth);
649}
650
651#define DUMP(N) dump(N, 0, -1)
652
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000653static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000655{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000656 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
658 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000659 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000660 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000661 goto fail;
662 if ((c->c_const_dict = PyDict_New()) == NULL)
663 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000664 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000665 goto fail;
666 if ((c->c_name_dict = PyDict_New()) == NULL)
667 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000668 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000669 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000670 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
671 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000672 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000673 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000674 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000675 c->c_freevars = NULL;
676 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000677 c->c_nlocals = 0;
678 c->c_argcount = 0;
679 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000680 c->c_nexti = 0;
681 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000682 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000683 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000684 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000685 c->c_begin = 0;
686 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000687 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000688 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000689 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000690 c->c_stacklevel = 0;
691 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000692 c->c_firstlineno = 0;
693 c->c_last_addr = 0;
694 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000695 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000696 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000697 c->c_nested = 0;
698 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000699 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000700 return 1;
701
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000702 fail:
703 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000704 return 0;
705}
706
707static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000708com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000709{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000710 Py_XDECREF(c->c_code);
711 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000712 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000713 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000714 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000715 Py_XDECREF(c->c_globals);
716 Py_XDECREF(c->c_locals);
717 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000718 Py_XDECREF(c->c_freevars);
719 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000720 Py_XDECREF(c->c_lnotab);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000721 if (c->c_future)
722 PyMem_Free((void *)c->c_future);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000723}
724
725static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000726com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000727{
728 c->c_stacklevel += n;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000729 if (c->c_stacklevel > c->c_maxstacklevel) {
Guido van Rossum8b993a91997-01-17 21:04:03 +0000730 c->c_maxstacklevel = c->c_stacklevel;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000731 /*
732 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
733 c->c_filename, c->c_name, c->c_lineno,
734 c->c_nexti, c->c_stacklevel, n);
735 */
736 }
Guido van Rossum8b993a91997-01-17 21:04:03 +0000737}
738
739static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000740com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000741{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000742 if (c->c_stacklevel < n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000743 c->c_stacklevel = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000744 else
745 c->c_stacklevel -= n;
746}
747
748static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000749com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000750{
751 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000752 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000753 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000754 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000755}
756
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000757static int
758com_check_size(PyObject **s, int offset)
759{
760 int len = PyString_GET_SIZE(*s);
761 if (offset >= len)
762 return _PyString_Resize(s, len * 2);
763 return 0;
764}
765
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000766static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000767com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000768{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000769 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000770 assert(byte >= 0 && byte <= 255);
Martin v. Löwis7198a522002-01-01 19:59:11 +0000771 assert(c->c_code != 0);
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000772 if (com_check_size(&c->c_code, c->c_nexti)) {
773 c->c_errors++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000774 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000775 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000776 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000777}
778
779static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000780com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000781{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000782 com_addbyte(c, x & 0xff);
783 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000784}
785
786static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000787com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000788{
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000789 char *p;
790 if (c->c_lnotab == NULL)
791 return;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000792 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
793 c->c_errors++;
794 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000795 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000796 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000797 *p++ = addr;
798 *p++ = line;
799 c->c_lnotab_next += 2;
800}
801
802static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000803com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000804{
805 c->c_lineno = lineno;
806 if (c->c_firstlineno == 0) {
807 c->c_firstlineno = c->c_last_line = lineno;
808 }
809 else {
810 int incr_addr = c->c_nexti - c->c_last_addr;
811 int incr_line = lineno - c->c_last_line;
Tim Peters2a7f3842001-06-09 09:26:21 +0000812 while (incr_addr > 255) {
813 com_add_lnotab(c, 255, 0);
814 incr_addr -= 255;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000815 }
Tim Peters2a7f3842001-06-09 09:26:21 +0000816 while (incr_line > 255) {
817 com_add_lnotab(c, incr_addr, 255);
818 incr_line -=255;
819 incr_addr = 0;
820 }
821 if (incr_addr > 0 || incr_line > 0)
822 com_add_lnotab(c, incr_addr, incr_line);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000823 c->c_last_addr = c->c_nexti;
824 c->c_last_line = lineno;
825 }
826}
827
828static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000829com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000830{
Fred Drakeef8ace32000-08-24 00:32:09 +0000831 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000832 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000833 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000834 if (Py_OptimizeFlag)
835 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000836 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000837 if (extended_arg){
838 com_addbyte(c, EXTENDED_ARG);
839 com_addint(c, extended_arg);
840 arg &= 0xffff;
841 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000842 com_addbyte(c, op);
843 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000844}
845
846static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000847com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000848{
849 /* Compile a forward reference for backpatching */
850 int here;
851 int anchor;
852 com_addbyte(c, op);
853 here = c->c_nexti;
854 anchor = *p_anchor;
855 *p_anchor = here;
856 com_addint(c, anchor == 0 ? 0 : here - anchor);
857}
858
859static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000860com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000861{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000862 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000863 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000864 int dist;
865 int prev;
866 for (;;) {
867 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000868 prev = code[anchor] + (code[anchor+1] << 8);
869 dist = target - (anchor+2);
870 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000871 dist >>= 8;
872 code[anchor+1] = dist;
873 dist >>= 8;
874 if (dist) {
875 com_error(c, PyExc_SystemError,
876 "com_backpatch: offset too large");
877 break;
878 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000879 if (!prev)
880 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000881 anchor -= prev;
882 }
883}
884
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000885/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000886
887static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000888com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000889{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000890 PyObject *w, *t, *np=NULL;
891 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000892
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000893 t = Py_BuildValue("(OO)", v, v->ob_type);
894 if (t == NULL)
895 goto fail;
896 w = PyDict_GetItem(dict, t);
897 if (w != NULL) {
898 n = PyInt_AsLong(w);
899 } else {
900 n = PyList_Size(list);
901 np = PyInt_FromLong(n);
902 if (np == NULL)
903 goto fail;
904 if (PyList_Append(list, v) != 0)
905 goto fail;
906 if (PyDict_SetItem(dict, t, np) != 0)
907 goto fail;
908 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000909 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000910 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000911 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000912 fail:
913 Py_XDECREF(np);
914 Py_XDECREF(t);
915 c->c_errors++;
916 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000917}
918
919static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000920com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000921{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000922 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000923}
924
925static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000926com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000927{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000928 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000929}
930
Raymond Hettinger0ae0c072002-06-20 22:23:15 +0000931int
932_Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000933{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000934 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000935 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000936 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000937 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
938 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000939 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000940 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000941 return 0; /* Don't mangle __extremely_long_names */
942 if (name[nlen-1] == '_' && name[nlen-2] == '_')
943 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000944 /* Strip leading underscores from class name */
945 while (*p == '_')
946 p++;
947 if (*p == '\0')
948 return 0; /* Don't mangle if class is just underscores */
949 plen = strlen(p);
950 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000951 plen = maxlen-nlen-2; /* Truncate class name if too long */
952 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000953 buffer[0] = '_';
954 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000955 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000956 return 1;
957}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000958
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000959static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000960com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000961{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000962 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000963 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000964 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000965
Raymond Hettinger0ae0c072002-06-20 22:23:15 +0000966 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000967 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000968 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000969 c->c_errors++;
970 i = 255;
971 }
972 else {
973 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000974 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000975 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000976 com_addoparg(c, op, i);
977}
978
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000979#define NAME_LOCAL 0
980#define NAME_GLOBAL 1
981#define NAME_DEFAULT 2
982#define NAME_CLOSURE 3
983
984static int
985com_lookup_arg(PyObject *dict, PyObject *name)
986{
987 PyObject *v = PyDict_GetItem(dict, name);
988 if (v == NULL)
989 return -1;
990 else
991 return PyInt_AS_LONG(v);
992}
993
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000994static void
995com_addop_varname(struct compiling *c, int kind, char *name)
996{
997 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000998 int i, reftype;
999 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001000 int op = STOP_CODE;
1001 char buffer[MANGLE_LEN];
1002
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001003 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001004 name = buffer;
1005 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
1006 c->c_errors++;
1007 i = 255;
1008 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001009 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001010
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001011 reftype = get_ref_type(c, name);
1012 switch (reftype) {
1013 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001014 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001015 scope = NAME_LOCAL;
1016 break;
1017 case GLOBAL_EXPLICIT:
1018 scope = NAME_GLOBAL;
1019 break;
1020 case GLOBAL_IMPLICIT:
1021 if (c->c_flags & CO_OPTIMIZED)
1022 scope = NAME_GLOBAL;
1023 break;
1024 case FREE:
1025 case CELL:
1026 scope = NAME_CLOSURE;
1027 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001028 }
1029
1030 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001031 if (scope == NAME_LOCAL)
1032 i = com_lookup_arg(c->c_locals, v);
1033 else if (reftype == FREE)
1034 i = com_lookup_arg(c->c_freevars, v);
1035 else if (reftype == CELL)
1036 i = com_lookup_arg(c->c_cellvars, v);
1037 if (i == -1) {
1038 c->c_errors++; /* XXX no exception set */
1039 i = 255;
1040 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001041 }
1042 Py_DECREF(v);
1043
1044 switch (kind) {
1045 case VAR_LOAD:
1046 switch (scope) {
1047 case NAME_LOCAL:
1048 op = LOAD_FAST;
1049 break;
1050 case NAME_GLOBAL:
1051 op = LOAD_GLOBAL;
1052 break;
1053 case NAME_DEFAULT:
1054 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001055 break;
1056 case NAME_CLOSURE:
1057 op = LOAD_DEREF;
1058 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001059 }
1060 break;
1061 case VAR_STORE:
1062 switch (scope) {
1063 case NAME_LOCAL:
1064 op = STORE_FAST;
1065 break;
1066 case NAME_GLOBAL:
1067 op = STORE_GLOBAL;
1068 break;
1069 case NAME_DEFAULT:
1070 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001071 break;
1072 case NAME_CLOSURE:
1073 op = STORE_DEREF;
1074 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001075 }
1076 break;
1077 case VAR_DELETE:
1078 switch (scope) {
1079 case NAME_LOCAL:
1080 op = DELETE_FAST;
1081 break;
1082 case NAME_GLOBAL:
1083 op = DELETE_GLOBAL;
1084 break;
1085 case NAME_DEFAULT:
1086 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001087 break;
1088 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00001089 char buf[500];
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001090 PyOS_snprintf(buf, sizeof(buf),
1091 DEL_CLOSURE_ERROR, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001092 com_error(c, PyExc_SyntaxError, buf);
1093 i = 255;
1094 break;
1095 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001096 }
1097 break;
1098 }
1099done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001100 com_addoparg(c, op, i);
1101}
1102
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001103static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001104com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001105{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001106 char *name;
1107 char buffer[1000];
1108 /* XXX it is possible to write this code without the 1000
1109 chars on the total length of dotted names, I just can't be
1110 bothered right now */
1111 if (TYPE(n) == STAR)
1112 name = "*";
1113 else if (TYPE(n) == dotted_name) {
1114 char *p = buffer;
1115 int i;
1116 name = buffer;
1117 for (i = 0; i < NCH(n); i += 2) {
1118 char *s = STR(CHILD(n, i));
1119 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001120 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001121 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001122 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001123 break;
1124 }
1125 if (p != buffer)
1126 *p++ = '.';
1127 strcpy(p, s);
1128 p = strchr(p, '\0');
1129 }
1130 }
1131 else {
1132 REQ(n, NAME);
1133 name = STR(n);
1134 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001135 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001136}
1137
Guido van Rossum79f25d91997-04-29 20:08:16 +00001138static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001139parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001140{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001141 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001142 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001143 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001144#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001145 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001146 int imflag;
1147#endif
1148
Guido van Rossum282914b1991-04-04 10:42:56 +00001149 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001150 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001151#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001152 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001153#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001154 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001155 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001156 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001158 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001159 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001160 if (*end == '\0') {
Jeremy Hylton71b6af92001-08-27 19:45:25 +00001161 if (errno != 0)
1162 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001163 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001164 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001165 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001166#ifndef WITHOUT_COMPLEX
1167 if (imflag) {
1168 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001169 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001170 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001171 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001172 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001173 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001174 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001175#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001176 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001177 PyFPE_START_PROTECT("atof", return 0)
1178 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001179 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001181 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001182}
1183
Guido van Rossum79f25d91997-04-29 20:08:16 +00001184static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001185parsestr(struct compiling *com, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001186{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001187 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001188 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001189 char *buf;
1190 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001191 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001192 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001193 int first = *s;
1194 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001195 int rawmode = 0;
1196 int unicode = 0;
Guido van Rossum05459c52002-05-28 18:47:29 +00001197
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001198 if (isalpha(quote) || quote == '_') {
1199 if (quote == 'u' || quote == 'U') {
1200 quote = *++s;
1201 unicode = 1;
1202 }
1203 if (quote == 'r' || quote == 'R') {
1204 quote = *++s;
1205 rawmode = 1;
1206 }
1207 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001208 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001209 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001210 return NULL;
1211 }
1212 s++;
1213 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001214 if (len > INT_MAX) {
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001215 com_error(com, PyExc_OverflowError,
1216 "string to parse is too long");
Guido van Rossum582acec2000-06-28 22:07:35 +00001217 return NULL;
1218 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001219 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001220 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001221 return NULL;
1222 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001223 if (len >= 4 && s[0] == quote && s[1] == quote) {
1224 s += 2;
1225 len -= 2;
1226 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001227 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001228 return NULL;
1229 }
1230 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001231#ifdef Py_USING_UNICODE
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001232 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001233 if (rawmode)
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001234 v = PyUnicode_DecodeRawUnicodeEscape(
1235 s, len, NULL);
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001236 else
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001237 v = PyUnicode_DecodeUnicodeEscape(
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001238 s, len, NULL);
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001239 if (v == NULL)
1240 PyErr_SyntaxLocation(com->c_filename, com->c_lineno);
1241 return v;
1242
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001243 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001244#endif
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001245 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001246 return PyString_FromStringAndSize(s, len);
1247 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001248 if (v == NULL)
1249 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001250 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001251 end = s + len;
1252 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001253 if (*s != '\\') {
1254 *p++ = *s++;
1255 continue;
1256 }
1257 s++;
1258 switch (*s++) {
1259 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001260 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001261 case '\\': *p++ = '\\'; break;
1262 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001263 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001264 case 'b': *p++ = '\b'; break;
1265 case 'f': *p++ = '\014'; break; /* FF */
1266 case 't': *p++ = '\t'; break;
1267 case 'n': *p++ = '\n'; break;
1268 case 'r': *p++ = '\r'; break;
1269 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001270 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1271 case '0': case '1': case '2': case '3':
1272 case '4': case '5': case '6': case '7':
1273 c = s[-1] - '0';
1274 if ('0' <= *s && *s <= '7') {
1275 c = (c<<3) + *s++ - '0';
1276 if ('0' <= *s && *s <= '7')
1277 c = (c<<3) + *s++ - '0';
1278 }
1279 *p++ = c;
1280 break;
1281 case 'x':
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001282 if (isxdigit(Py_CHARMASK(s[0]))
1283 && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001284 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001285 c = Py_CHARMASK(*s);
1286 s++;
1287 if (isdigit(c))
1288 x = c - '0';
1289 else if (islower(c))
1290 x = 10 + c - 'a';
1291 else
1292 x = 10 + c - 'A';
1293 x = x << 4;
1294 c = Py_CHARMASK(*s);
1295 s++;
1296 if (isdigit(c))
1297 x += c - '0';
1298 else if (islower(c))
1299 x += 10 + c - 'a';
1300 else
1301 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001302 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001303 break;
1304 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001305 Py_DECREF(v);
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +00001306 com_error(com, PyExc_ValueError,
1307 "invalid \\x escape");
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001308 return NULL;
Guido van Rossum05459c52002-05-28 18:47:29 +00001309#ifndef Py_USING_UNICODE
1310 case 'u':
1311 case 'U':
1312 case 'N':
1313 if (unicode) {
1314 Py_DECREF(v);
1315 com_error(com, PyExc_ValueError,
1316 "Unicode escapes not legal "
1317 "when Unicode disabled");
1318 return NULL;
1319 }
1320#endif
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001321 default:
1322 *p++ = '\\';
1323 *p++ = s[-1];
1324 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001325 }
1326 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001327 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001328 return v;
1329}
1330
Guido van Rossum79f25d91997-04-29 20:08:16 +00001331static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001332parsestrplus(struct compiling* c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001333{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001334 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001335 int i;
1336 REQ(CHILD(n, 0), STRING);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001337 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001338 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001339 for (i = 1; i < NCH(n); i++) {
1340 PyObject *s;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001341 s = parsestr(c, STR(CHILD(n, i)));
Fred Drake4e998bc2000-04-13 14:10:44 +00001342 if (s == NULL)
1343 goto onError;
1344 if (PyString_Check(v) && PyString_Check(s)) {
1345 PyString_ConcatAndDel(&v, s);
1346 if (v == NULL)
1347 goto onError;
1348 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001349#ifdef Py_USING_UNICODE
Fred Drake4e998bc2000-04-13 14:10:44 +00001350 else {
1351 PyObject *temp;
1352 temp = PyUnicode_Concat(v, s);
1353 Py_DECREF(s);
1354 if (temp == NULL)
1355 goto onError;
1356 Py_DECREF(v);
1357 v = temp;
1358 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001359#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001360 }
1361 }
1362 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001363
1364 onError:
1365 Py_XDECREF(v);
1366 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001367}
1368
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001369static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001370com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001371{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001372 int anchor = 0;
1373 int save_begin = c->c_begin;
1374
1375 /* list_iter: for v in expr [list_iter] */
1376 com_node(c, CHILD(n, 3)); /* expr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001377 com_addbyte(c, GET_ITER);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001378 c->c_begin = c->c_nexti;
1379 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001380 com_addfwref(c, FOR_ITER, &anchor);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001381 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001382 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001383 c->c_loops++;
1384 com_list_iter(c, n, e, t);
1385 c->c_loops--;
1386 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1387 c->c_begin = save_begin;
1388 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001389 com_pop(c, 1); /* FOR_ITER has popped this */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001390}
1391
1392static void
1393com_list_if(struct compiling *c, node *n, node *e, char *t)
1394{
1395 int anchor = 0;
1396 int a = 0;
1397 /* list_iter: 'if' test [list_iter] */
1398 com_addoparg(c, SET_LINENO, n->n_lineno);
1399 com_node(c, CHILD(n, 1));
1400 com_addfwref(c, JUMP_IF_FALSE, &a);
1401 com_addbyte(c, POP_TOP);
1402 com_pop(c, 1);
1403 com_list_iter(c, n, e, t);
1404 com_addfwref(c, JUMP_FORWARD, &anchor);
1405 com_backpatch(c, a);
1406 /* We jump here with an extra entry which we now pop */
1407 com_addbyte(c, POP_TOP);
1408 com_backpatch(c, anchor);
1409}
1410
1411static void
1412com_list_iter(struct compiling *c,
1413 node *p, /* parent of list_iter node */
1414 node *e, /* element expression node */
1415 char *t /* name of result list temp local */)
1416{
1417 /* list_iter is the last child in a listmaker, list_for, or list_if */
1418 node *n = CHILD(p, NCH(p)-1);
1419 if (TYPE(n) == list_iter) {
1420 n = CHILD(n, 0);
1421 switch (TYPE(n)) {
1422 case list_for:
1423 com_list_for(c, n, e, t);
1424 break;
1425 case list_if:
1426 com_list_if(c, n, e, t);
1427 break;
1428 default:
1429 com_error(c, PyExc_SystemError,
1430 "invalid list_iter node type");
1431 }
1432 }
1433 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001434 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001435 com_push(c, 1);
1436 com_node(c, e);
1437 com_addoparg(c, CALL_FUNCTION, 1);
1438 com_addbyte(c, POP_TOP);
1439 com_pop(c, 2);
1440 }
1441}
1442
1443static void
1444com_list_comprehension(struct compiling *c, node *n)
1445{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001446 /* listmaker: test list_for */
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001447 char tmpname[30];
1448 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001449 com_addoparg(c, BUILD_LIST, 0);
1450 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1451 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001452 com_addop_name(c, LOAD_ATTR, "append");
1453 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001454 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001455 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001456 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001457 --c->c_tmpname;
1458}
1459
1460static void
1461com_listmaker(struct compiling *c, node *n)
1462{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001463 /* listmaker: test ( list_for | (',' test)* [','] ) */
1464 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001465 com_list_comprehension(c, n);
1466 else {
1467 int len = 0;
1468 int i;
1469 for (i = 0; i < NCH(n); i += 2, len++)
1470 com_node(c, CHILD(n, i));
1471 com_addoparg(c, BUILD_LIST, len);
1472 com_pop(c, len-1);
1473 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001474}
1475
1476static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001477com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001478{
1479 int i;
1480 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1481 for (i = 0; i+2 < NCH(n); i += 4) {
1482 /* We must arrange things just right for STORE_SUBSCR.
1483 It wants the stack to look like (value) (dict) (key) */
1484 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001485 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001486 com_node(c, CHILD(n, i+2)); /* value */
1487 com_addbyte(c, ROT_TWO);
1488 com_node(c, CHILD(n, i)); /* key */
1489 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001490 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001491 }
1492}
1493
1494static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001495com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001496{
1497 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001498 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001499 int i;
1500 REQ(n, atom);
1501 ch = CHILD(n, 0);
1502 switch (TYPE(ch)) {
1503 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001504 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001505 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001506 com_push(c, 1);
1507 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001508 else
1509 com_node(c, CHILD(n, 1));
1510 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001511 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001512 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001513 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001514 com_push(c, 1);
1515 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001516 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001517 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001518 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001519 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001520 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001521 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001522 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001523 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001524 break;
1525 case BACKQUOTE:
1526 com_node(c, CHILD(n, 1));
1527 com_addbyte(c, UNARY_CONVERT);
1528 break;
1529 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001530 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001531 i = 255;
1532 }
1533 else {
1534 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001535 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001536 }
1537 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001538 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001539 break;
1540 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001541 v = parsestrplus(c, n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001542 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001543 c->c_errors++;
1544 i = 255;
1545 }
1546 else {
1547 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001548 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001549 }
1550 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001551 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001552 break;
1553 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001554 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001555 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001556 break;
1557 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001558 com_error(c, PyExc_SystemError,
1559 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001560 }
1561}
1562
1563static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001564com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001565{
1566 if (NCH(n) == 1) {
1567 com_addbyte(c, op);
1568 }
1569 else if (NCH(n) == 2) {
1570 if (TYPE(CHILD(n, 0)) != COLON) {
1571 com_node(c, CHILD(n, 0));
1572 com_addbyte(c, op+1);
1573 }
1574 else {
1575 com_node(c, CHILD(n, 1));
1576 com_addbyte(c, op+2);
1577 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001578 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001579 }
1580 else {
1581 com_node(c, CHILD(n, 0));
1582 com_node(c, CHILD(n, 2));
1583 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001584 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001585 }
1586}
1587
Guido van Rossum635abd21997-01-06 22:56:52 +00001588static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001589com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1590{
1591 if (NCH(n) == 1) {
1592 com_addbyte(c, DUP_TOP);
1593 com_push(c, 1);
1594 com_addbyte(c, SLICE);
1595 com_node(c, augn);
1596 com_addbyte(c, opcode);
1597 com_pop(c, 1);
1598 com_addbyte(c, ROT_TWO);
1599 com_addbyte(c, STORE_SLICE);
1600 com_pop(c, 2);
1601 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1602 com_node(c, CHILD(n, 0));
1603 com_addoparg(c, DUP_TOPX, 2);
1604 com_push(c, 2);
1605 com_addbyte(c, SLICE+1);
1606 com_pop(c, 1);
1607 com_node(c, augn);
1608 com_addbyte(c, opcode);
1609 com_pop(c, 1);
1610 com_addbyte(c, ROT_THREE);
1611 com_addbyte(c, STORE_SLICE+1);
1612 com_pop(c, 3);
1613 } else if (NCH(n) == 2) {
1614 com_node(c, CHILD(n, 1));
1615 com_addoparg(c, DUP_TOPX, 2);
1616 com_push(c, 2);
1617 com_addbyte(c, SLICE+2);
1618 com_pop(c, 1);
1619 com_node(c, augn);
1620 com_addbyte(c, opcode);
1621 com_pop(c, 1);
1622 com_addbyte(c, ROT_THREE);
1623 com_addbyte(c, STORE_SLICE+2);
1624 com_pop(c, 3);
1625 } else {
1626 com_node(c, CHILD(n, 0));
1627 com_node(c, CHILD(n, 2));
1628 com_addoparg(c, DUP_TOPX, 3);
1629 com_push(c, 3);
1630 com_addbyte(c, SLICE+3);
1631 com_pop(c, 2);
1632 com_node(c, augn);
1633 com_addbyte(c, opcode);
1634 com_pop(c, 1);
1635 com_addbyte(c, ROT_FOUR);
1636 com_addbyte(c, STORE_SLICE+3);
1637 com_pop(c, 4);
1638 }
1639}
1640
1641static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001642com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001643{
1644 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001645 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001646 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001647 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001648 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001649 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001650 }
1651 else {
1652 com_node(c, CHILD(n, 0));
1653 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001654 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001655 }
1656 m = n;
1657 do {
1658 m = CHILD(m, 0);
1659 } while (NCH(m) == 1);
1660 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001661 /* f(lambda x: x[0] = 3) ends up getting parsed with
1662 * LHS test = lambda x: x[0], and RHS test = 3.
1663 * SF bug 132313 points out that complaining about a keyword
1664 * then is very confusing.
1665 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001666 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001667 TYPE(m) == lambdef ?
1668 "lambda cannot contain assignment" :
1669 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001670 }
1671 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001672 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001673 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001674 *pkeywords = PyDict_New();
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001675 if (v == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001676 c->c_errors++;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001677 else if (*pkeywords == NULL) {
1678 c->c_errors++;
1679 Py_DECREF(v);
1680 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001681 if (PyDict_GetItem(*pkeywords, v) != NULL)
1682 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001683 "duplicate keyword argument");
1684 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001685 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001686 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001687 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001688 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001690 }
1691 }
1692 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001693}
1694
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001695static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001696com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001697{
1698 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001699 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001700 }
1701 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001703 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001704 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001705 int star_flag = 0;
1706 int starstar_flag = 0;
1707 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001708 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001709 na = 0;
1710 nk = 0;
1711 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001712 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001713 if (TYPE(ch) == STAR ||
1714 TYPE(ch) == DOUBLESTAR)
1715 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001716 if (ch->n_lineno != lineno) {
1717 lineno = ch->n_lineno;
1718 com_addoparg(c, SET_LINENO, lineno);
1719 }
1720 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001721 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001722 na++;
1723 else
1724 nk++;
1725 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001727 while (i < NCH(n)) {
1728 node *tok = CHILD(n, i);
1729 node *ch = CHILD(n, i+1);
1730 i += 3;
1731 switch (TYPE(tok)) {
1732 case STAR: star_flag = 1; break;
1733 case DOUBLESTAR: starstar_flag = 1; break;
1734 }
1735 com_node(c, ch);
1736 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001737 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001738 com_error(c, PyExc_SyntaxError,
1739 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001740 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001741 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001742 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001743 star_flag + (starstar_flag << 1);
1744 else
1745 opcode = CALL_FUNCTION;
1746 com_addoparg(c, opcode, na | (nk << 8));
1747 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001748 }
1749}
1750
1751static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001752com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001753{
1754 com_addopname(c, LOAD_ATTR, n);
1755}
1756
1757static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001758com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001759{
1760 int i=0;
1761 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001762 node *ch;
1763
1764 /* first argument */
1765 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001766 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001767 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001768 i++;
1769 }
1770 else {
1771 com_node(c, CHILD(n,i));
1772 i++;
1773 REQ(CHILD(n,i),COLON);
1774 i++;
1775 }
1776 /* second argument */
1777 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1778 com_node(c, CHILD(n,i));
1779 i++;
1780 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001781 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001782 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001783 com_push(c, 1);
1784 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001785 /* remaining arguments */
1786 for (; i < NCH(n); i++) {
1787 ns++;
1788 ch=CHILD(n,i);
1789 REQ(ch, sliceop);
1790 if (NCH(ch) == 1) {
1791 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001792 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001793 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001794 }
1795 else
1796 com_node(c, CHILD(ch,1));
1797 }
1798 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001799 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001800}
1801
1802static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001803com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001804{
1805 node *ch;
1806 REQ(n, subscript);
1807 ch = CHILD(n,0);
1808 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001809 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001810 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001811 com_push(c, 1);
1812 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001813 else {
1814 /* check for slice */
1815 if ((TYPE(ch) == COLON || NCH(n) > 1))
1816 com_sliceobj(c, n);
1817 else {
1818 REQ(ch, test);
1819 com_node(c, ch);
1820 }
1821 }
1822}
1823
1824static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001825com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001826{
1827 int i, op;
1828 REQ(n, subscriptlist);
1829 /* Check to make backward compatible slice behavior for '[i:j]' */
1830 if (NCH(n) == 1) {
1831 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001832 /* 'Basic' slice, should have exactly one colon. */
1833 if ((TYPE(CHILD(sub, 0)) == COLON
1834 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1835 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1836 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001837 switch (assigning) {
1838 case OP_DELETE:
1839 op = DELETE_SLICE;
1840 break;
1841 case OP_ASSIGN:
1842 op = STORE_SLICE;
1843 break;
1844 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001845 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001846 break;
1847 default:
1848 com_augassign_slice(c, sub, assigning, augn);
1849 return;
1850 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001851 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001852 if (op == STORE_SLICE)
1853 com_pop(c, 2);
1854 else if (op == DELETE_SLICE)
1855 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001856 return;
1857 }
1858 }
1859 /* Else normal subscriptlist. Compile each subscript. */
1860 for (i = 0; i < NCH(n); i += 2)
1861 com_subscript(c, CHILD(n, i));
1862 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001863 if (NCH(n) > 1) {
1864 i = (NCH(n)+1) / 2;
1865 com_addoparg(c, BUILD_TUPLE, i);
1866 com_pop(c, i-1);
1867 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001868 switch (assigning) {
1869 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001870 op = DELETE_SUBSCR;
1871 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001872 break;
1873 default:
1874 case OP_ASSIGN:
1875 op = STORE_SUBSCR;
1876 i = 3;
1877 break;
1878 case OP_APPLY:
1879 op = BINARY_SUBSCR;
1880 i = 1;
1881 break;
1882 }
1883 if (assigning > OP_APPLY) {
1884 com_addoparg(c, DUP_TOPX, 2);
1885 com_push(c, 2);
1886 com_addbyte(c, BINARY_SUBSCR);
1887 com_pop(c, 1);
1888 com_node(c, augn);
1889 com_addbyte(c, assigning);
1890 com_pop(c, 1);
1891 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001892 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001893 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001894 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001895}
1896
1897static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001898com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001899{
1900 REQ(n, trailer);
1901 switch (TYPE(CHILD(n, 0))) {
1902 case LPAR:
1903 com_call_function(c, CHILD(n, 1));
1904 break;
1905 case DOT:
1906 com_select_member(c, CHILD(n, 1));
1907 break;
1908 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001909 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001910 break;
1911 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001912 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001913 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001914 }
1915}
1916
1917static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001918com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001919{
1920 int i;
1921 REQ(n, power);
1922 com_atom(c, CHILD(n, 0));
1923 for (i = 1; i < NCH(n); i++) {
1924 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1925 com_factor(c, CHILD(n, i+1));
1926 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001927 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001928 break;
1929 }
1930 else
1931 com_apply_trailer(c, CHILD(n, i));
1932 }
1933}
1934
1935static void
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001936com_invert_constant(struct compiling *c, node *n)
1937{
1938 /* Compute the inverse of int and longs and use them directly,
1939 but be prepared to generate code for all other
1940 possibilities (invalid numbers, floats, complex).
1941 */
1942 PyObject *num, *inv = NULL;
1943 int i;
1944
1945 REQ(n, NUMBER);
1946 num = parsenumber(c, STR(n));
1947 if (num == NULL)
1948 i = 255;
1949 else {
1950 inv = PyNumber_Invert(num);
1951 if (inv == NULL) {
1952 PyErr_Clear();
1953 i = com_addconst(c, num);
1954 } else {
1955 i = com_addconst(c, inv);
1956 Py_DECREF(inv);
1957 }
1958 Py_DECREF(num);
1959 }
1960 com_addoparg(c, LOAD_CONST, i);
1961 com_push(c, 1);
1962 if (num != NULL && inv == NULL)
1963 com_addbyte(c, UNARY_INVERT);
1964}
1965
Tim Peters51e26512001-09-07 08:45:55 +00001966static int
1967is_float_zero(const char *p)
1968{
1969 int found_radix_point = 0;
1970 int ch;
1971 while ((ch = Py_CHARMASK(*p++)) != '\0') {
1972 switch (ch) {
1973 case '0':
1974 /* no reason to believe it's not 0 -- continue */
1975 break;
1976
1977 case 'e': case 'E': case 'j': case 'J':
1978 /* If this was a hex constant, we already would have
1979 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1980 must be an exponent marker, and we haven't yet
1981 seen a non-zero digit, and it doesn't matter what
1982 the exponent is then. For 'j' or 'J' similarly,
1983 except that this is an imaginary 0 then. */
1984 return 1;
1985
1986 case '.':
1987 found_radix_point = 1;
1988 break;
1989
1990 default:
1991 return 0;
1992 }
1993 }
1994 return found_radix_point;
1995}
1996
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001997static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001998com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001999{
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002000 int childtype = TYPE(CHILD(n, 0));
Tim Peters51e26512001-09-07 08:45:55 +00002001 node *pfactor, *ppower, *patom, *pnum;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002002 REQ(n, factor);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002003 /* If the unary +, -, or ~ operator is applied to a constant,
Tim Peters51e26512001-09-07 08:45:55 +00002004 don't generate a UNARY_xxx opcode. Just store the
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002005 approriate value as a constant. If the value is negative,
2006 extend the string containing the constant and insert a
Tim Peters51e26512001-09-07 08:45:55 +00002007 negative in the 0th position -- unless we're doing unary minus
2008 of a floating zero! In that case the sign is significant, but
2009 the const dict can't distinguish +0.0 from -0.0.
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002010 */
2011 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
Fred Drake14ef2442001-08-30 18:53:25 +00002012 && NCH(n) == 2
Tim Peters51e26512001-09-07 08:45:55 +00002013 && TYPE((pfactor = CHILD(n, 1))) == factor
2014 && NCH(pfactor) == 1
2015 && TYPE((ppower = CHILD(pfactor, 0))) == power
2016 && NCH(ppower) == 1
2017 && TYPE((patom = CHILD(ppower, 0))) == atom
2018 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
2019 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002020 if (childtype == TILDE) {
Tim Peters51e26512001-09-07 08:45:55 +00002021 com_invert_constant(c, pnum);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002022 return;
2023 }
2024 if (childtype == MINUS) {
Tim Peters51e7f5c2002-04-22 02:33:27 +00002025 char *s = PyMem_Malloc(strlen(STR(pnum)) + 2);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002026 if (s == NULL) {
2027 com_error(c, PyExc_MemoryError, "");
2028 com_addbyte(c, 255);
2029 return;
2030 }
2031 s[0] = '-';
Tim Peters51e26512001-09-07 08:45:55 +00002032 strcpy(s + 1, STR(pnum));
Tim Peters51e7f5c2002-04-22 02:33:27 +00002033 PyMem_Free(STR(pnum));
Tim Peters51e26512001-09-07 08:45:55 +00002034 STR(pnum) = s;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002035 }
Tim Peters51e26512001-09-07 08:45:55 +00002036 com_atom(c, patom);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002037 }
2038 else if (childtype == PLUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002039 com_factor(c, CHILD(n, 1));
2040 com_addbyte(c, UNARY_POSITIVE);
2041 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002042 else if (childtype == MINUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002043 com_factor(c, CHILD(n, 1));
2044 com_addbyte(c, UNARY_NEGATIVE);
2045 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00002046 else if (childtype == TILDE) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002047 com_factor(c, CHILD(n, 1));
2048 com_addbyte(c, UNARY_INVERT);
2049 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002050 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00002051 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002052 }
2053}
2054
2055static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002056com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002057{
2058 int i;
2059 int op;
2060 REQ(n, term);
2061 com_factor(c, CHILD(n, 0));
2062 for (i = 2; i < NCH(n); i += 2) {
2063 com_factor(c, CHILD(n, i));
2064 switch (TYPE(CHILD(n, i-1))) {
2065 case STAR:
2066 op = BINARY_MULTIPLY;
2067 break;
2068 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00002069 if (c->c_flags & CO_FUTURE_DIVISION)
2070 op = BINARY_TRUE_DIVIDE;
2071 else
2072 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002073 break;
2074 case PERCENT:
2075 op = BINARY_MODULO;
2076 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002077 case DOUBLESLASH:
2078 op = BINARY_FLOOR_DIVIDE;
2079 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002080 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002081 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00002082 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002083 op = 255;
2084 }
2085 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002086 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002087 }
2088}
2089
2090static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002091com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002092{
2093 int i;
2094 int op;
2095 REQ(n, arith_expr);
2096 com_term(c, CHILD(n, 0));
2097 for (i = 2; i < NCH(n); i += 2) {
2098 com_term(c, CHILD(n, i));
2099 switch (TYPE(CHILD(n, i-1))) {
2100 case PLUS:
2101 op = BINARY_ADD;
2102 break;
2103 case MINUS:
2104 op = BINARY_SUBTRACT;
2105 break;
2106 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002107 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002108 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002109 op = 255;
2110 }
2111 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002112 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002113 }
2114}
2115
2116static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002117com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002118{
2119 int i;
2120 int op;
2121 REQ(n, shift_expr);
2122 com_arith_expr(c, CHILD(n, 0));
2123 for (i = 2; i < NCH(n); i += 2) {
2124 com_arith_expr(c, CHILD(n, i));
2125 switch (TYPE(CHILD(n, i-1))) {
2126 case LEFTSHIFT:
2127 op = BINARY_LSHIFT;
2128 break;
2129 case RIGHTSHIFT:
2130 op = BINARY_RSHIFT;
2131 break;
2132 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002133 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002134 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002135 op = 255;
2136 }
2137 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002138 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002139 }
2140}
2141
2142static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002143com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002144{
2145 int i;
2146 int op;
2147 REQ(n, and_expr);
2148 com_shift_expr(c, CHILD(n, 0));
2149 for (i = 2; i < NCH(n); i += 2) {
2150 com_shift_expr(c, CHILD(n, i));
2151 if (TYPE(CHILD(n, i-1)) == AMPER) {
2152 op = BINARY_AND;
2153 }
2154 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002155 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002156 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002157 op = 255;
2158 }
2159 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002160 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002161 }
2162}
2163
2164static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002165com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002166{
2167 int i;
2168 int op;
2169 REQ(n, xor_expr);
2170 com_and_expr(c, CHILD(n, 0));
2171 for (i = 2; i < NCH(n); i += 2) {
2172 com_and_expr(c, CHILD(n, i));
2173 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2174 op = BINARY_XOR;
2175 }
2176 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002177 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002178 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002179 op = 255;
2180 }
2181 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002182 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002183 }
2184}
2185
2186static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002187com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002188{
2189 int i;
2190 int op;
2191 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002192 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002193 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002194 com_xor_expr(c, CHILD(n, i));
2195 if (TYPE(CHILD(n, i-1)) == VBAR) {
2196 op = BINARY_OR;
2197 }
2198 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002199 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002200 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002201 op = 255;
2202 }
2203 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002204 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002205 }
2206}
2207
2208static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002209cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002210{
2211 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002212 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002213 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2214 if (NCH(n) == 1) {
2215 n = CHILD(n, 0);
2216 switch (TYPE(n)) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00002217 case LESS: return PyCmp_LT;
2218 case GREATER: return PyCmp_GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002219 case EQEQUAL: /* == */
Martin v. Löwis7198a522002-01-01 19:59:11 +00002220 case EQUAL: return PyCmp_EQ;
2221 case LESSEQUAL: return PyCmp_LE;
2222 case GREATEREQUAL: return PyCmp_GE;
2223 case NOTEQUAL: return PyCmp_NE; /* <> or != */
2224 case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
2225 if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002226 }
2227 }
2228 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002229 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002230 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
Martin v. Löwis7198a522002-01-01 19:59:11 +00002231 return PyCmp_NOT_IN;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002232 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
Martin v. Löwis7198a522002-01-01 19:59:11 +00002233 return PyCmp_IS_NOT;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002234 }
2235 }
Martin v. Löwis7198a522002-01-01 19:59:11 +00002236 return PyCmp_BAD;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002237}
2238
2239static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002240com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002241{
2242 int i;
2243 enum cmp_op op;
2244 int anchor;
2245 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2246 com_expr(c, CHILD(n, 0));
2247 if (NCH(n) == 1)
2248 return;
2249
2250 /****************************************************************
2251 The following code is generated for all but the last
2252 comparison in a chain:
2253
2254 label: on stack: opcode: jump to:
2255
2256 a <code to load b>
2257 a, b DUP_TOP
2258 a, b, b ROT_THREE
2259 b, a, b COMPARE_OP
2260 b, 0-or-1 JUMP_IF_FALSE L1
2261 b, 1 POP_TOP
2262 b
2263
2264 We are now ready to repeat this sequence for the next
2265 comparison in the chain.
2266
2267 For the last we generate:
2268
2269 b <code to load c>
2270 b, c COMPARE_OP
2271 0-or-1
2272
2273 If there were any jumps to L1 (i.e., there was more than one
2274 comparison), we generate:
2275
2276 0-or-1 JUMP_FORWARD L2
2277 L1: b, 0 ROT_TWO
2278 0, b POP_TOP
2279 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002280 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002281 ****************************************************************/
2282
2283 anchor = 0;
2284
2285 for (i = 2; i < NCH(n); i += 2) {
2286 com_expr(c, CHILD(n, i));
2287 if (i+2 < NCH(n)) {
2288 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002289 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002290 com_addbyte(c, ROT_THREE);
2291 }
2292 op = cmp_type(CHILD(n, i-1));
Martin v. Löwis7198a522002-01-01 19:59:11 +00002293 if (op == PyCmp_BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002294 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002295 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002296 }
2297 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002298 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002299 if (i+2 < NCH(n)) {
2300 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2301 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002302 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002303 }
2304 }
2305
2306 if (anchor) {
2307 int anchor2 = 0;
2308 com_addfwref(c, JUMP_FORWARD, &anchor2);
2309 com_backpatch(c, anchor);
2310 com_addbyte(c, ROT_TWO);
2311 com_addbyte(c, POP_TOP);
2312 com_backpatch(c, anchor2);
2313 }
2314}
2315
2316static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002317com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002318{
2319 REQ(n, not_test); /* 'not' not_test | comparison */
2320 if (NCH(n) == 1) {
2321 com_comparison(c, CHILD(n, 0));
2322 }
2323 else {
2324 com_not_test(c, CHILD(n, 1));
2325 com_addbyte(c, UNARY_NOT);
2326 }
2327}
2328
2329static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002330com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002331{
2332 int i;
2333 int anchor;
2334 REQ(n, and_test); /* not_test ('and' not_test)* */
2335 anchor = 0;
2336 i = 0;
2337 for (;;) {
2338 com_not_test(c, CHILD(n, i));
2339 if ((i += 2) >= NCH(n))
2340 break;
2341 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2342 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002343 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002344 }
2345 if (anchor)
2346 com_backpatch(c, anchor);
2347}
2348
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002349static int
2350com_make_closure(struct compiling *c, PyCodeObject *co)
2351{
Jeremy Hylton733c8932001-12-13 19:51:56 +00002352 int i, free = PyCode_GetNumFree(co);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002353 if (free == 0)
2354 return 0;
2355 for (i = 0; i < free; ++i) {
2356 /* Bypass com_addop_varname because it will generate
2357 LOAD_DEREF but LOAD_CLOSURE is needed.
2358 */
2359 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2360 int arg, reftype;
2361
2362 /* Special case: If a class contains a method with a
2363 free variable that has the same name as a method,
2364 the name will be considered free *and* local in the
2365 class. It should be handled by the closure, as
2366 well as by the normal name loookup logic.
2367 */
2368 reftype = get_ref_type(c, PyString_AS_STRING(name));
2369 if (reftype == CELL)
2370 arg = com_lookup_arg(c->c_cellvars, name);
2371 else /* (reftype == FREE) */
2372 arg = com_lookup_arg(c->c_freevars, name);
2373 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002374 fprintf(stderr, "lookup %s in %s %d %d\n"
2375 "freevars of %s: %s\n",
2376 PyObject_REPR(name),
2377 c->c_name,
2378 reftype, arg,
2379 PyString_AS_STRING(co->co_name),
2380 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002381 Py_FatalError("com_make_closure()");
2382 }
2383 com_addoparg(c, LOAD_CLOSURE, arg);
2384
2385 }
2386 com_push(c, free);
2387 return 1;
2388}
2389
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002390static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002391com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002392{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002393 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002394 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002395 PyCodeObject *co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002396 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002397 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002398 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2399 n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002400 co = icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002401 if (co == NULL) {
2402 c->c_errors++;
2403 return;
2404 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002405 symtable_exit_scope(c->c_symtable);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002406 i = com_addconst(c, (PyObject *)co);
2407 closure = com_make_closure(c, co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002408 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002409 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002410 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002411 com_addoparg(c, MAKE_CLOSURE, ndefs);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002412 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002413 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002414 com_addoparg(c, MAKE_FUNCTION, ndefs);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002415 Py_DECREF(co);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002416 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002417 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002418 else {
2419 int anchor = 0;
2420 int i = 0;
2421 for (;;) {
2422 com_and_test(c, CHILD(n, i));
2423 if ((i += 2) >= NCH(n))
2424 break;
2425 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2426 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002427 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002428 }
2429 if (anchor)
2430 com_backpatch(c, anchor);
2431 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002432}
2433
2434static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002435com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002436{
2437 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002438 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002439 com_node(c, CHILD(n, 0));
2440 }
2441 else {
2442 int i;
2443 int len;
2444 len = (NCH(n) + 1) / 2;
2445 for (i = 0; i < NCH(n); i += 2)
2446 com_node(c, CHILD(n, i));
2447 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002448 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002449 }
2450}
2451
2452
2453/* Begin of assignment compilation */
2454
Thomas Wouters434d0822000-08-24 20:11:32 +00002455
2456static void
2457com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2458{
2459 com_addbyte(c, DUP_TOP);
2460 com_push(c, 1);
2461 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002462 com_node(c, augn);
2463 com_addbyte(c, opcode);
2464 com_pop(c, 1);
2465 com_addbyte(c, ROT_TWO);
2466 com_addopname(c, STORE_ATTR, n);
2467 com_pop(c, 2);
2468}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002469
2470static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002471com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002472{
2473 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002474 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002475}
2476
2477static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002478com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002479{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002480 REQ(n, trailer);
2481 switch (TYPE(CHILD(n, 0))) {
2482 case LPAR: /* '(' [exprlist] ')' */
Jeremy Hylton05ab2e62002-05-31 14:08:29 +00002483 if (assigning == OP_DELETE)
2484 com_error(c, PyExc_SyntaxError,
2485 "can't delete function call");
2486 else
2487 com_error(c, PyExc_SyntaxError,
2488 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002489 break;
2490 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002491 if (assigning > OP_APPLY)
2492 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2493 else
2494 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002495 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002496 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002497 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002498 break;
2499 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002501 }
2502}
2503
2504static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002505com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002506{
2507 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002508 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002509 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002510 if (assigning) {
2511 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002512 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002513 com_push(c, i-1);
2514 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002515 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002516 com_assign(c, CHILD(n, i), assigning, NULL);
2517}
2518
2519static void
2520com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2521{
2522 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002523 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002524 com_push(c, 1);
2525 com_node(c, augn);
2526 com_addbyte(c, opcode);
2527 com_pop(c, 1);
2528 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002529}
2530
2531static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002532com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002533{
2534 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002535 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002536 if (assigning)
2537 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002538}
2539
2540static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002541com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002542{
2543 /* Loop to avoid trivial recursion */
2544 for (;;) {
2545 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002546
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002547 case exprlist:
2548 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00002549 case testlist1:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002550 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002551 if (assigning > OP_APPLY) {
2552 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002553 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002554 return;
2555 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002556 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002557 return;
2558 }
2559 n = CHILD(n, 0);
2560 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002561
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002562 case test:
2563 case and_test:
2564 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002565 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002566 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002567 case xor_expr:
2568 case and_expr:
2569 case shift_expr:
2570 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002571 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002572 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002573 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002574 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002575 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002576 return;
2577 }
2578 n = CHILD(n, 0);
2579 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002580
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002581 case power: /* atom trailer* ('**' power)*
2582 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002583 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002585 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002586 return;
2587 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002588 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002589 int i;
2590 com_node(c, CHILD(n, 0));
2591 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002592 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002593 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002594 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002595 return;
2596 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002597 com_apply_trailer(c, CHILD(n, i));
2598 } /* NB i is still alive */
2599 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002600 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002601 return;
2602 }
2603 n = CHILD(n, 0);
2604 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002605
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002606 case atom:
2607 switch (TYPE(CHILD(n, 0))) {
2608 case LPAR:
2609 n = CHILD(n, 1);
2610 if (TYPE(n) == RPAR) {
2611 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002612 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002613 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002614 return;
2615 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002616 if (assigning > OP_APPLY) {
2617 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002618 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002619 return;
2620 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002621 break;
2622 case LSQB:
2623 n = CHILD(n, 1);
2624 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002625 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002626 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002627 return;
2628 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002629 if (assigning > OP_APPLY) {
2630 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002631 "augmented assign to list not possible");
2632 return;
2633 }
2634 if (NCH(n) > 1
2635 && TYPE(CHILD(n, 1)) == list_for) {
2636 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002637 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002638 return;
2639 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002640 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002641 return;
2642 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002643 if (assigning > OP_APPLY)
2644 com_augassign_name(c, CHILD(n, 0),
2645 assigning, augn);
2646 else
2647 com_assign_name(c, CHILD(n, 0),
2648 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002649 return;
2650 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002651 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002652 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002653 return;
2654 }
2655 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002656
2657 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002658 com_error(c, PyExc_SyntaxError,
2659 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002660 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002661
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002662 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002663 com_error(c, PyExc_SystemError,
2664 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002665 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002666
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002667 }
2668 }
2669}
Guido van Rossum7c531111997-03-11 18:42:21 +00002670
Thomas Wouters434d0822000-08-24 20:11:32 +00002671static void
2672com_augassign(struct compiling *c, node *n)
2673{
2674 int opcode;
2675
2676 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2677 case '+': opcode = INPLACE_ADD; break;
2678 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002679 case '/':
2680 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2681 opcode = INPLACE_FLOOR_DIVIDE;
2682 else if (c->c_flags & CO_FUTURE_DIVISION)
2683 opcode = INPLACE_TRUE_DIVIDE;
2684 else
2685 opcode = INPLACE_DIVIDE;
2686 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002687 case '%': opcode = INPLACE_MODULO; break;
2688 case '<': opcode = INPLACE_LSHIFT; break;
2689 case '>': opcode = INPLACE_RSHIFT; break;
2690 case '&': opcode = INPLACE_AND; break;
2691 case '^': opcode = INPLACE_XOR; break;
2692 case '|': opcode = INPLACE_OR; break;
2693 case '*':
2694 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2695 opcode = INPLACE_POWER;
2696 else
2697 opcode = INPLACE_MULTIPLY;
2698 break;
2699 default:
2700 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2701 return;
2702 }
2703 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2704}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002705
2706static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002707com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002708{
Thomas Wouters434d0822000-08-24 20:11:32 +00002709 REQ(n, expr_stmt);
2710 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002711 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002712 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002713 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002714 if (NCH(n) == 1) {
2715 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002716 if (c->c_interactive)
2717 com_addbyte(c, PRINT_EXPR);
2718 else
2719 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002720 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002721 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002722 else if (TYPE(CHILD(n,1)) == augassign)
2723 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002724 else {
2725 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002726 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002727 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002728 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002729 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002730 com_push(c, 1);
2731 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002732 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002733 }
2734 }
2735}
2736
2737static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002738com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002739{
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002740 int a = 0;
Guido van Rossum228d7f31997-04-02 05:24:36 +00002741 int i;
2742 REQ(n, assert_stmt); /* 'assert' test [',' test] */
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002743 if (Py_OptimizeFlag)
2744 return;
2745 /* Generate code like
Guido van Rossum228d7f31997-04-02 05:24:36 +00002746
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002747 if not <test>:
Guido van Rossum228d7f31997-04-02 05:24:36 +00002748 raise AssertionError [, <message>]
2749
2750 where <message> is the second test, if present.
2751 */
Guido van Rossum228d7f31997-04-02 05:24:36 +00002752 com_node(c, CHILD(n, 1));
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002753 com_addfwref(c, JUMP_IF_TRUE, &a);
Guido van Rossum228d7f31997-04-02 05:24:36 +00002754 com_addbyte(c, POP_TOP);
2755 com_pop(c, 1);
2756 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002757 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002758 com_push(c, 1);
2759 i = NCH(n)/2; /* Either 2 or 4 */
2760 if (i > 1)
2761 com_node(c, CHILD(n, 3));
2762 com_addoparg(c, RAISE_VARARGS, i);
2763 com_pop(c, i);
2764 /* The interpreter does not fall through */
Neil Schemenauer89e3ee02002-04-26 01:58:53 +00002765 /* Jump ends up here */
Guido van Rossum228d7f31997-04-02 05:24:36 +00002766 com_backpatch(c, a);
Guido van Rossum228d7f31997-04-02 05:24:36 +00002767 com_addbyte(c, POP_TOP);
2768}
2769
2770static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002771com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002772{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002773 int i = 1;
2774 node* stream = NULL;
2775
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002776 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002777
2778 /* are we using the extended print form? */
2779 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2780 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002781 com_node(c, stream);
2782 /* stack: [...] => [... stream] */
2783 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002784 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2785 i = 4;
2786 else
2787 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002788 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002789 for (; i < NCH(n); i += 2) {
2790 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002791 com_addbyte(c, DUP_TOP);
2792 /* stack: [stream] => [stream stream] */
2793 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002794 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002795 /* stack: [stream stream] => [stream stream obj] */
2796 com_addbyte(c, ROT_TWO);
2797 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002798 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002799 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002800 com_pop(c, 2);
2801 }
2802 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002803 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002804 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002805 com_addbyte(c, PRINT_ITEM);
2806 com_pop(c, 1);
2807 }
2808 }
2809 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002810 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002811 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002812 /* must pop the extra stream object off the stack */
2813 com_addbyte(c, POP_TOP);
2814 /* stack: [... stream] => [...] */
2815 com_pop(c, 1);
2816 }
2817 }
2818 else {
2819 if (stream != NULL) {
2820 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002821 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002822 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002823 com_pop(c, 1);
2824 }
2825 else
2826 com_addbyte(c, PRINT_NEWLINE);
2827 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002828}
2829
2830static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002831com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002832{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002833 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002834 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002835 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002836 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002837 if (c->c_flags & CO_GENERATOR) {
2838 if (NCH(n) > 1) {
2839 com_error(c, PyExc_SyntaxError,
2840 "'return' with argument inside generator");
2841 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002842 }
2843 if (NCH(n) < 2) {
2844 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002845 com_push(c, 1);
2846 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002847 else
2848 com_node(c, CHILD(n, 1));
2849 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002850 com_pop(c, 1);
2851}
2852
2853static void
2854com_yield_stmt(struct compiling *c, node *n)
2855{
Tim Peters95c80f82001-06-23 02:07:08 +00002856 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002857 REQ(n, yield_stmt); /* 'yield' testlist */
2858 if (!c->c_infunction) {
2859 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2860 }
Tim Peters95c80f82001-06-23 02:07:08 +00002861
2862 for (i = 0; i < c->c_nblocks; ++i) {
2863 if (c->c_block[i] == SETUP_FINALLY) {
2864 com_error(c, PyExc_SyntaxError,
2865 "'yield' not allowed in a 'try' block "
2866 "with a 'finally' clause");
2867 return;
2868 }
2869 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002870 com_node(c, CHILD(n, 1));
2871 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002872 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002873}
2874
2875static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002876com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002877{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002878 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002879 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2880 if (NCH(n) > 1) {
2881 com_node(c, CHILD(n, 1));
2882 if (NCH(n) > 3) {
2883 com_node(c, CHILD(n, 3));
2884 if (NCH(n) > 5)
2885 com_node(c, CHILD(n, 5));
2886 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002887 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002888 i = NCH(n)/2;
2889 com_addoparg(c, RAISE_VARARGS, i);
2890 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002891}
2892
2893static void
Thomas Wouters52152252000-08-17 22:55:00 +00002894com_from_import(struct compiling *c, node *n)
2895{
2896 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2897 com_push(c, 1);
2898 if (NCH(n) > 1) {
2899 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2900 com_error(c, PyExc_SyntaxError, "invalid syntax");
2901 return;
2902 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002903 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002904 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002905 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002906 com_pop(c, 1);
2907}
2908
2909static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002910com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002911{
2912 int i;
2913 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002914 /* 'import' dotted_name (',' dotted_name)* |
2915 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002916 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002917 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002918 /* 'from' dotted_name 'import' ... */
2919 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002920
2921 if (TYPE(CHILD(n, 3)) == STAR) {
2922 tup = Py_BuildValue("(s)", "*");
2923 } else {
2924 tup = PyTuple_New((NCH(n) - 2)/2);
2925 for (i = 3; i < NCH(n); i += 2) {
2926 PyTuple_SET_ITEM(tup, (i-3)/2,
2927 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00002928 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002929 }
2930 }
2931 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002932 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002933 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002934 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002935 if (TYPE(CHILD(n, 3)) == STAR)
2936 com_addbyte(c, IMPORT_STAR);
2937 else {
2938 for (i = 3; i < NCH(n); i += 2)
2939 com_from_import(c, CHILD(n, i));
2940 com_addbyte(c, POP_TOP);
2941 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002942 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002943 }
2944 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002945 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002946 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002947 node *subn = CHILD(n, i);
2948 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002949 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002950 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002951 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002952 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002953 int j;
2954 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002955 com_error(c, PyExc_SyntaxError,
2956 "invalid syntax");
2957 return;
2958 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002959 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2960 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002961 CHILD(CHILD(subn, 0),
2962 j));
2963 com_addop_varname(c, VAR_STORE,
2964 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002965 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002966 com_addop_varname(c, VAR_STORE,
2967 STR(CHILD(CHILD(subn, 0),
2968 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002969 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002970 }
2971 }
2972}
2973
2974static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002975com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002976{
2977 REQ(n, exec_stmt);
2978 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2979 com_node(c, CHILD(n, 1));
2980 if (NCH(n) >= 4)
2981 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002982 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002983 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002984 com_push(c, 1);
2985 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002986 if (NCH(n) >= 6)
2987 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002988 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002989 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002990 com_push(c, 1);
2991 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002992 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002993 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002994}
2995
Guido van Rossum7c531111997-03-11 18:42:21 +00002996static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002997is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002998{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002999 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00003000 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003001 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00003002
3003 /* Label to avoid tail recursion */
3004 next:
3005 switch (TYPE(n)) {
3006
3007 case suite:
3008 if (NCH(n) == 1) {
3009 n = CHILD(n, 0);
3010 goto next;
3011 }
3012 /* Fall through */
3013 case file_input:
3014 for (i = 0; i < NCH(n); i++) {
3015 node *ch = CHILD(n, i);
3016 if (TYPE(ch) == stmt) {
3017 n = ch;
3018 goto next;
3019 }
3020 }
3021 break;
3022
3023 case stmt:
3024 case simple_stmt:
3025 case small_stmt:
3026 n = CHILD(n, 0);
3027 goto next;
3028
3029 case expr_stmt:
3030 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003031 case testlist1:
Guido van Rossum7c531111997-03-11 18:42:21 +00003032 case test:
3033 case and_test:
3034 case not_test:
3035 case comparison:
3036 case expr:
3037 case xor_expr:
3038 case and_expr:
3039 case shift_expr:
3040 case arith_expr:
3041 case term:
3042 case factor:
3043 case power:
3044 case atom:
3045 if (NCH(n) == 1) {
3046 n = CHILD(n, 0);
3047 goto next;
3048 }
3049 break;
3050
3051 case NAME:
3052 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
3053 return 1;
3054 break;
3055
3056 case NUMBER:
3057 v = parsenumber(c, STR(n));
3058 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003059 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00003060 break;
3061 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003062 i = PyObject_IsTrue(v);
3063 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003064 return i == 0;
3065
3066 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003067 v = parsestr(c, STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00003068 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003069 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00003070 break;
3071 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003072 i = PyObject_IsTrue(v);
3073 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003074 return i == 0;
3075
3076 }
3077 return 0;
3078}
3079
Tim Peters08a898f2001-06-28 01:52:22 +00003080
3081/* Look under n for a return stmt with an expression.
3082 * This hack is used to find illegal returns under "if 0:" blocks in
3083 * functions already known to be generators (as determined by the symtable
3084 * pass).
3085 * Return the offending return node if found, else NULL.
3086 */
3087static node *
3088look_for_offending_return(node *n)
3089{
3090 int i;
3091
3092 for (i = 0; i < NCH(n); ++i) {
3093 node *kid = CHILD(n, i);
3094
3095 switch (TYPE(kid)) {
3096 case classdef:
3097 case funcdef:
3098 case lambdef:
3099 /* Stuff in nested functions & classes doesn't
3100 affect the code block we started in. */
3101 return NULL;
3102
3103 case return_stmt:
3104 if (NCH(kid) > 1)
3105 return kid;
3106 break;
3107
3108 default: {
3109 node *bad = look_for_offending_return(kid);
3110 if (bad != NULL)
3111 return bad;
3112 }
3113 }
3114 }
3115
3116 return NULL;
3117}
3118
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003119static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003120com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003121{
3122 int i;
3123 int anchor = 0;
3124 REQ(n, if_stmt);
3125 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3126 for (i = 0; i+3 < NCH(n); i+=4) {
3127 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003128 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00003129 if (is_constant_false(c, ch)) {
3130 /* We're going to skip this block. However, if this
3131 is a generator, we have to check the dead code
3132 anyway to make sure there aren't any return stmts
3133 with expressions, in the same scope. */
3134 if (c->c_flags & CO_GENERATOR) {
3135 node *p = look_for_offending_return(n);
3136 if (p != NULL) {
3137 int savelineno = c->c_lineno;
3138 c->c_lineno = p->n_lineno;
3139 com_error(c, PyExc_SyntaxError,
3140 "'return' with argument "
3141 "inside generator");
3142 c->c_lineno = savelineno;
3143 }
3144 }
Guido van Rossum7c531111997-03-11 18:42:21 +00003145 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00003146 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00003147 if (i > 0)
3148 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00003149 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003150 com_addfwref(c, JUMP_IF_FALSE, &a);
3151 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003152 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003153 com_node(c, CHILD(n, i+3));
3154 com_addfwref(c, JUMP_FORWARD, &anchor);
3155 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003156 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003157 com_addbyte(c, POP_TOP);
3158 }
3159 if (i+2 < NCH(n))
3160 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00003161 if (anchor)
3162 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003163}
3164
3165static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003166com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003167{
3168 int break_anchor = 0;
3169 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003170 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003171 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3172 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003173 block_push(c, SETUP_LOOP);
3174 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003175 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003176 com_node(c, CHILD(n, 1));
3177 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3178 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003179 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003180 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003181 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003182 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003183 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3184 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003185 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003186 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003187 com_addbyte(c, POP_TOP);
3188 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003189 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003190 if (NCH(n) > 4)
3191 com_node(c, CHILD(n, 6));
3192 com_backpatch(c, break_anchor);
3193}
3194
3195static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003196com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003197{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003198 int break_anchor = 0;
3199 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003200 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003201 REQ(n, for_stmt);
3202 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3203 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003204 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003205 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003206 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003207 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003208 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003209 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003210 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003211 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003212 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003213 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003214 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003215 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3216 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003217 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003218 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003219 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003220 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003221 if (NCH(n) > 8)
3222 com_node(c, CHILD(n, 8));
3223 com_backpatch(c, break_anchor);
3224}
3225
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003226/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003227
3228 SETUP_FINALLY L
3229 <code for S>
3230 POP_BLOCK
3231 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003232 L: <code for Sf>
3233 END_FINALLY
3234
3235 The special instructions use the block stack. Each block
3236 stack entry contains the instruction that created it (here
3237 SETUP_FINALLY), the level of the value stack at the time the
3238 block stack entry was created, and a label (here L).
3239
3240 SETUP_FINALLY:
3241 Pushes the current value stack level and the label
3242 onto the block stack.
3243 POP_BLOCK:
3244 Pops en entry from the block stack, and pops the value
3245 stack until its level is the same as indicated on the
3246 block stack. (The label is ignored.)
3247 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003248 Pops a variable number of entries from the *value* stack
3249 and re-raises the exception they specify. The number of
3250 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003251
3252 The block stack is unwound when an exception is raised:
3253 when a SETUP_FINALLY entry is found, the exception is pushed
3254 onto the value stack (and the exception condition is cleared),
3255 and the interpreter jumps to the label gotten from the block
3256 stack.
3257
3258 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003259 (The contents of the value stack is shown in [], with the top
3260 at the right; 'tb' is trace-back info, 'val' the exception's
3261 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003262
3263 Value stack Label Instruction Argument
3264 [] SETUP_EXCEPT L1
3265 [] <code for S>
3266 [] POP_BLOCK
3267 [] JUMP_FORWARD L0
3268
Guido van Rossum3f5da241990-12-20 15:06:42 +00003269 [tb, val, exc] L1: DUP )
3270 [tb, val, exc, exc] <evaluate E1> )
3271 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3272 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3273 [tb, val, exc, 1] POP )
3274 [tb, val, exc] POP
3275 [tb, val] <assign to V1> (or POP if no V1)
3276 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003277 [] <code for S1>
3278 JUMP_FORWARD L0
3279
Guido van Rossum3f5da241990-12-20 15:06:42 +00003280 [tb, val, exc, 0] L2: POP
3281 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003282 .............................etc.......................
3283
Guido van Rossum3f5da241990-12-20 15:06:42 +00003284 [tb, val, exc, 0] Ln+1: POP
3285 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003286
3287 [] L0: <next statement>
3288
3289 Of course, parts are not generated if Vi or Ei is not present.
3290*/
3291
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003292static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003293com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003294{
3295 int except_anchor = 0;
3296 int end_anchor = 0;
3297 int else_anchor = 0;
3298 int i;
3299 node *ch;
3300
3301 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3302 block_push(c, SETUP_EXCEPT);
3303 com_node(c, CHILD(n, 2));
3304 com_addbyte(c, POP_BLOCK);
3305 block_pop(c, SETUP_EXCEPT);
3306 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3307 com_backpatch(c, except_anchor);
3308 for (i = 3;
3309 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3310 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003311 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003312 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003313 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003314 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003315 break;
3316 }
3317 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003318 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003319 com_addoparg(c, SET_LINENO, ch->n_lineno);
3320 if (NCH(ch) > 1) {
3321 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003322 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003323 com_node(c, CHILD(ch, 1));
Martin v. Löwis7198a522002-01-01 19:59:11 +00003324 com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003325 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003326 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3327 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003328 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003329 }
3330 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003331 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003332 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003333 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003334 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003335 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003336 com_pop(c, 1);
3337 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003338 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003339 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003340 com_node(c, CHILD(n, i+2));
3341 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3342 if (except_anchor) {
3343 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003344 /* We come in with [tb, val, exc, 0] on the
3345 stack; one pop and it's the same as
3346 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003347 com_addbyte(c, POP_TOP);
3348 }
3349 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003350 /* We actually come in here with [tb, val, exc] but the
3351 END_FINALLY will zap those and jump around.
3352 The c_stacklevel does not reflect them so we need not pop
3353 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003354 com_addbyte(c, END_FINALLY);
3355 com_backpatch(c, else_anchor);
3356 if (i < NCH(n))
3357 com_node(c, CHILD(n, i+2));
3358 com_backpatch(c, end_anchor);
3359}
3360
3361static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003362com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003363{
3364 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003365 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003366
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003367 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3368 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003369 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003370 com_addbyte(c, POP_BLOCK);
3371 block_pop(c, SETUP_FINALLY);
3372 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003373 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003374 /* While the generated code pushes only one item,
3375 the try-finally handling can enter here with
3376 up to three items. OK, here are the details:
3377 3 for an exception, 2 for RETURN, 1 for BREAK. */
3378 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003379 com_backpatch(c, finally_anchor);
3380 ch = CHILD(n, NCH(n)-1);
3381 com_addoparg(c, SET_LINENO, ch->n_lineno);
3382 com_node(c, ch);
3383 com_addbyte(c, END_FINALLY);
3384 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003385 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003386}
3387
3388static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003389com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003390{
3391 REQ(n, try_stmt);
3392 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3393 | 'try' ':' suite 'finally' ':' suite */
3394 if (TYPE(CHILD(n, 3)) != except_clause)
3395 com_try_finally(c, n);
3396 else
3397 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003398}
3399
Guido van Rossum8b993a91997-01-17 21:04:03 +00003400static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003401get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003402{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003403 int i;
3404
Guido van Rossum8b993a91997-01-17 21:04:03 +00003405 /* Label to avoid tail recursion */
3406 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003407 switch (TYPE(n)) {
3408
3409 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003410 if (NCH(n) == 1) {
3411 n = CHILD(n, 0);
3412 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003413 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003414 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003415 case file_input:
3416 for (i = 0; i < NCH(n); i++) {
3417 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003418 if (TYPE(ch) == stmt) {
3419 n = ch;
3420 goto next;
3421 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003422 }
3423 break;
3424
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003425 case stmt:
3426 case simple_stmt:
3427 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003428 n = CHILD(n, 0);
3429 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003430
3431 case expr_stmt:
3432 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003433 case testlist1:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003434 case test:
3435 case and_test:
3436 case not_test:
3437 case comparison:
3438 case expr:
3439 case xor_expr:
3440 case and_expr:
3441 case shift_expr:
3442 case arith_expr:
3443 case term:
3444 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003445 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003446 if (NCH(n) == 1) {
3447 n = CHILD(n, 0);
3448 goto next;
3449 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003450 break;
3451
3452 case atom:
3453 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003454 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003455 break;
3456
3457 }
3458 return NULL;
3459}
3460
Guido van Rossum79f25d91997-04-29 20:08:16 +00003461static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003462get_docstring(struct compiling *c, node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003463{
Guido van Rossum541563e1999-01-28 15:08:09 +00003464 /* Don't generate doc-strings if run with -OO */
3465 if (Py_OptimizeFlag > 1)
3466 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003467 n = get_rawdocstring(n);
3468 if (n == NULL)
3469 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003470 return parsestrplus(c, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003471}
3472
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003473static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003474com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003475{
3476 REQ(n, suite);
3477 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3478 if (NCH(n) == 1) {
3479 com_node(c, CHILD(n, 0));
3480 }
3481 else {
3482 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003483 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003484 node *ch = CHILD(n, i);
3485 if (TYPE(ch) == stmt)
3486 com_node(c, ch);
3487 }
3488 }
3489}
3490
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003491/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003492static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003493com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003494{
3495 int i = c->c_nblocks;
3496 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3497 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3498 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003499 else if (i <= 0) {
3500 /* at the outer level */
3501 com_error(c, PyExc_SyntaxError,
3502 "'continue' not properly in loop");
3503 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003504 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003505 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003506 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003507 if (c->c_block[j] == SETUP_LOOP)
3508 break;
3509 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003510 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003511 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003512 for (; i > j; --i) {
3513 if (c->c_block[i] == SETUP_EXCEPT ||
3514 c->c_block[i] == SETUP_FINALLY) {
3515 com_addoparg(c, CONTINUE_LOOP,
3516 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003517 return;
3518 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003519 if (c->c_block[i] == END_FINALLY) {
3520 com_error(c, PyExc_SyntaxError,
3521 "'continue' not supported inside 'finally' clause");
3522 return;
3523 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003524 }
3525 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003526 com_error(c, PyExc_SyntaxError,
3527 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003528 }
3529 /* XXX Could allow it inside a 'finally' clause
3530 XXX if we could pop the exception still on the stack */
3531}
3532
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003533static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003534com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003535{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003536 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003537 if (TYPE(n) == lambdef) {
3538 /* lambdef: 'lambda' [varargslist] ':' test */
3539 n = CHILD(n, 1);
3540 }
3541 else {
3542 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3543 n = CHILD(n, 2);
3544 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3545 n = CHILD(n, 1);
3546 }
3547 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003548 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003549 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003550 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003551 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3552 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003553 nargs = 0;
3554 ndefs = 0;
3555 for (i = 0; i < nch; i++) {
3556 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003557 if (TYPE(CHILD(n, i)) == STAR ||
3558 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003559 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003560 nargs++;
3561 i++;
3562 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003563 t = RPAR; /* Anything except EQUAL or COMMA */
3564 else
3565 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003566 if (t == EQUAL) {
3567 i++;
3568 ndefs++;
3569 com_node(c, CHILD(n, i));
3570 i++;
3571 if (i >= nch)
3572 break;
3573 t = TYPE(CHILD(n, i));
3574 }
3575 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003576 /* Treat "(a=1, b)" as an error */
3577 if (ndefs)
3578 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003579 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003580 }
3581 if (t != COMMA)
3582 break;
3583 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003584 return ndefs;
3585}
3586
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003587static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003588com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003589{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003590 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003591 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003592 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003593 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003594 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3595 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003596 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003597 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003598 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003599 c->c_errors++;
3600 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003601 int closure = com_make_closure(c, (PyCodeObject *)co);
3602 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003603 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003604 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003605 if (closure)
3606 com_addoparg(c, MAKE_CLOSURE, ndefs);
3607 else
3608 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003609 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003610 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003611 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003612 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003613 }
3614}
3615
3616static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003617com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003618{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003619 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003620 REQ(n, testlist);
3621 /* testlist: test (',' test)* [','] */
3622 for (i = 0; i < NCH(n); i += 2)
3623 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003624 i = (NCH(n)+1) / 2;
3625 com_addoparg(c, BUILD_TUPLE, i);
3626 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003627}
3628
3629static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003630com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003631{
Guido van Rossum25831651993-05-19 14:50:45 +00003632 int i;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003633 PyObject *v;
3634 PyCodeObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003635 char *name;
3636
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003637 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003638 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003639 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003640 c->c_errors++;
3641 return;
3642 }
3643 /* Push the class name on the stack */
3644 i = com_addconst(c, v);
3645 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003646 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003647 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003648 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003649 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003650 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003651 com_push(c, 1);
3652 }
Guido van Rossum25831651993-05-19 14:50:45 +00003653 else
3654 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003655 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003656 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003657 co = icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003658 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003659 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003660 c->c_errors++;
3661 else {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003662 int closure = com_make_closure(c, co);
3663 i = com_addconst(c, (PyObject *)co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003664 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003665 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003666 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003667 com_addoparg(c, MAKE_CLOSURE, 0);
Jeremy Hylton733c8932001-12-13 19:51:56 +00003668 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003669 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003670 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003671 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003672 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003673 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003674 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003675 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003676 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003677 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003678}
3679
3680static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003681com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003682{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003683 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003684 if (c->c_errors)
3685 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003686 switch (TYPE(n)) {
3687
3688 /* Definition nodes */
3689
3690 case funcdef:
3691 com_funcdef(c, n);
3692 break;
3693 case classdef:
3694 com_classdef(c, n);
3695 break;
3696
3697 /* Trivial parse tree nodes */
3698
3699 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003700 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003701 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003702 n = CHILD(n, 0);
3703 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003704
3705 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003706 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3707 com_addoparg(c, SET_LINENO, n->n_lineno);
3708 {
3709 int i;
3710 for (i = 0; i < NCH(n)-1; i += 2)
3711 com_node(c, CHILD(n, i));
3712 }
3713 break;
3714
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003715 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003716 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003717 n = CHILD(n, 0);
3718 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003719
3720 /* Statement nodes */
3721
3722 case expr_stmt:
3723 com_expr_stmt(c, n);
3724 break;
3725 case print_stmt:
3726 com_print_stmt(c, n);
3727 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003728 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003729 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003730 break;
3731 case pass_stmt:
3732 break;
3733 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003734 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003735 com_error(c, PyExc_SyntaxError,
3736 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003737 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003738 com_addbyte(c, BREAK_LOOP);
3739 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003740 case continue_stmt:
3741 com_continue_stmt(c, n);
3742 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003743 case return_stmt:
3744 com_return_stmt(c, n);
3745 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003746 case yield_stmt:
3747 com_yield_stmt(c, n);
3748 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003749 case raise_stmt:
3750 com_raise_stmt(c, n);
3751 break;
3752 case import_stmt:
3753 com_import_stmt(c, n);
3754 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003755 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003756 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003757 case exec_stmt:
3758 com_exec_stmt(c, n);
3759 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003760 case assert_stmt:
3761 com_assert_stmt(c, n);
3762 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003763 case if_stmt:
3764 com_if_stmt(c, n);
3765 break;
3766 case while_stmt:
3767 com_while_stmt(c, n);
3768 break;
3769 case for_stmt:
3770 com_for_stmt(c, n);
3771 break;
3772 case try_stmt:
3773 com_try_stmt(c, n);
3774 break;
3775 case suite:
3776 com_suite(c, n);
3777 break;
3778
3779 /* Expression nodes */
3780
3781 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00003782 case testlist1:
Guido van Rossum1c917072001-10-15 15:44:05 +00003783 case testlist_safe:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003784 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003785 break;
3786 case test:
3787 com_test(c, n);
3788 break;
3789 case and_test:
3790 com_and_test(c, n);
3791 break;
3792 case not_test:
3793 com_not_test(c, n);
3794 break;
3795 case comparison:
3796 com_comparison(c, n);
3797 break;
3798 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003799 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003800 break;
3801 case expr:
3802 com_expr(c, n);
3803 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003804 case xor_expr:
3805 com_xor_expr(c, n);
3806 break;
3807 case and_expr:
3808 com_and_expr(c, n);
3809 break;
3810 case shift_expr:
3811 com_shift_expr(c, n);
3812 break;
3813 case arith_expr:
3814 com_arith_expr(c, n);
3815 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003816 case term:
3817 com_term(c, n);
3818 break;
3819 case factor:
3820 com_factor(c, n);
3821 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003822 case power:
3823 com_power(c, n);
3824 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003825 case atom:
3826 com_atom(c, n);
3827 break;
3828
3829 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003830 com_error(c, PyExc_SystemError,
3831 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003832 }
3833}
3834
Tim Petersdbd9ba62000-07-09 03:09:57 +00003835static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003836
3837static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003838com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003839{
3840 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3841 if (TYPE(CHILD(n, 0)) == LPAR)
3842 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003843 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003844 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003845 com_pop(c, 1);
3846 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003847}
3848
3849static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003852 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003853 if (NCH(n) == 1) {
3854 com_fpdef(c, CHILD(n, 0));
3855 }
3856 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003857 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003858 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003859 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003860 for (i = 0; i < NCH(n); i += 2)
3861 com_fpdef(c, CHILD(n, i));
3862 }
3863}
3864
3865static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003866com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003867{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003868 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003869 int complex = 0;
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003870 char nbuf[30];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003871 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003872 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003873 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003874 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003875 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003876 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003877 node *ch = CHILD(n, i);
3878 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003879 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003880 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003881 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3882 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003883 if (TYPE(fp) != NAME) {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003884 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003885 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003886 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003887 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003888 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003889 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003890 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003891 ch = CHILD(n, i);
3892 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003893 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003894 else
3895 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003896 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003897 if (complex) {
3898 /* Generate code for complex arguments only after
3899 having counted the simple arguments */
3900 int ilocal = 0;
3901 for (i = 0; i < nch; i++) {
3902 node *ch = CHILD(n, i);
3903 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003904 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003905 break;
3906 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3907 fp = CHILD(ch, 0);
3908 if (TYPE(fp) != NAME) {
3909 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003910 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003911 com_fpdef(c, ch);
3912 }
3913 ilocal++;
3914 if (++i >= nch)
3915 break;
3916 ch = CHILD(n, i);
3917 if (TYPE(ch) == EQUAL)
3918 i += 2;
3919 else
3920 REQ(ch, COMMA);
3921 }
3922 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003923}
3924
3925static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003926com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003927{
3928 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003929 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003930 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003931 doc = get_docstring(c, n);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003932 if (doc != NULL) {
3933 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003934 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003935 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003936 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003937 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003938 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003939 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003940 for (i = 0; i < NCH(n); i++) {
3941 node *ch = CHILD(n, i);
3942 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3943 com_node(c, ch);
3944 }
3945}
3946
3947/* Top-level compile-node interface */
3948
3949static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003950compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003951{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003952 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 node *ch;
3954 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003955 c->c_name = STR(CHILD(n, 1));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003956 doc = get_docstring(c, CHILD(n, 4));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003957 if (doc != NULL) {
3958 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003959 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003960 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003961 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003962 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003963 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3964 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003965 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003966 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003967 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003968 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003969 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00003970 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3971 com_push(c, 1);
3972 com_addbyte(c, RETURN_VALUE);
3973 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003974}
3975
3976static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003977compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003978{
Guido van Rossum590baa41993-11-30 13:40:46 +00003979 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003980 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003981 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003982
3983 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003984 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003985 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003986 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003987 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003988 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003989 else
3990 ch = CHILD(n, 2);
3991 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003992 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003993 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003994}
3995
3996static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003997compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003998{
3999 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004000 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004001 REQ(n, classdef);
4002 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
4003 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004004 c->c_private = c->c_name;
Guido van Rossum340cbe72002-01-15 21:06:07 +00004005 /* Initialize local __module__ from global __name__ */
4006 com_addop_name(c, LOAD_GLOBAL, "__name__");
4007 com_addop_name(c, STORE_NAME, "__module__");
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004008 ch = CHILD(n, NCH(n)-1); /* The suite */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00004009 doc = get_docstring(c, ch);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004010 if (doc != NULL) {
4011 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004012 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004013 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004014 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004015 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00004016 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004017 }
4018 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00004019 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004020 com_node(c, ch);
4021 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004022 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004023 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004024 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004025}
4026
4027static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004028compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004029{
Guido van Rossum3f5da241990-12-20 15:06:42 +00004030 com_addoparg(c, SET_LINENO, n->n_lineno);
4031
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004032 switch (TYPE(n)) {
4033
Guido van Rossum4c417781991-01-21 16:09:22 +00004034 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004035 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004036 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004037 n = CHILD(n, 0);
4038 if (TYPE(n) != NEWLINE)
4039 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004040 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00004041 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004042 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004043 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004044 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004045 break;
4046
Guido van Rossum4c417781991-01-21 16:09:22 +00004047 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004048 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004049 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00004050 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004051 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004052 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004053 break;
4054
Guido van Rossum590baa41993-11-30 13:40:46 +00004055 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00004056 com_node(c, CHILD(n, 0));
4057 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00004058 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00004059 break;
4060
Guido van Rossum590baa41993-11-30 13:40:46 +00004061 case lambdef: /* anonymous function definition */
4062 compile_lambdef(c, n);
4063 break;
4064
Guido van Rossum4c417781991-01-21 16:09:22 +00004065 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004066 compile_funcdef(c, n);
4067 break;
4068
Guido van Rossum4c417781991-01-21 16:09:22 +00004069 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00004070 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00004071 break;
4072
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004073 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00004074 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00004075 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004076 }
4077}
4078
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004079static PyObject *
4080dict_keys_inorder(PyObject *dict, int offset)
4081{
4082 PyObject *tuple, *k, *v;
4083 int i, pos = 0, size = PyDict_Size(dict);
4084
4085 tuple = PyTuple_New(size);
4086 if (tuple == NULL)
4087 return NULL;
4088 while (PyDict_Next(dict, &pos, &k, &v)) {
4089 i = PyInt_AS_LONG(v);
4090 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004091 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004092 PyTuple_SET_ITEM(tuple, i - offset, k);
4093 }
4094 return tuple;
4095}
4096
Guido van Rossum79f25d91997-04-29 20:08:16 +00004097PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004098PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004099{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004100 return PyNode_CompileFlags(n, filename, NULL);
4101}
4102
4103PyCodeObject *
4104PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4105{
4106 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004107}
4108
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004109struct symtable *
4110PyNode_CompileSymtable(node *n, char *filename)
4111{
4112 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004113 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004114
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004115 ff = PyNode_Future(n, filename);
4116 if (ff == NULL)
4117 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004118
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004119 st = symtable_init();
Tim Peters8c5e4152001-11-04 19:26:58 +00004120 if (st == NULL) {
4121 PyMem_Free((void *)ff);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004122 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004123 }
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004124 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004125 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004126 if (st->st_errors > 0)
4127 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004128 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004129 if (st->st_errors > 0)
4130 goto fail;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004131
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004132 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004133 fail:
4134 PyMem_Free((void *)ff);
4135 st->st_future = NULL;
4136 PySymtable_Free(st);
4137 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004138}
4139
Guido van Rossum79f25d91997-04-29 20:08:16 +00004140static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004141icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004142{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004143 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004144}
4145
Guido van Rossum79f25d91997-04-29 20:08:16 +00004146static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004147jcompile(node *n, char *filename, struct compiling *base,
4148 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004149{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004150 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004151 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00004152 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004153 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004154 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004155 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004156 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004157 /* c_symtable still points to parent's symbols */
4158 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004159 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004160 sc.c_nested = 1;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00004161 sc.c_flags |= base->c_flags & PyCF_MASK;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004162 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004163 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004164 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004165 if (sc.c_future == NULL) {
4166 com_free(&sc);
4167 return NULL;
4168 }
Jeremy Hylton481081e2001-08-14 20:01:59 +00004169 if (flags) {
4170 int merged = sc.c_future->ff_features |
4171 flags->cf_flags;
4172 sc.c_future->ff_features = merged;
4173 flags->cf_flags = merged;
4174 }
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004175 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004176 com_free(&sc);
4177 return NULL;
4178 }
4179 }
4180 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004181 if (symtable_load_symbols(&sc) < 0) {
4182 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004183 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004184 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004185 compile_node(&sc, n);
4186 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00004187 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004188 PyObject *consts, *names, *varnames, *filename, *name,
4189 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004190 consts = PyList_AsTuple(sc.c_consts);
4191 names = PyList_AsTuple(sc.c_names);
4192 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004193 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4194 freevars = dict_keys_inorder(sc.c_freevars,
4195 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00004196 filename = PyString_InternFromString(sc.c_filename);
4197 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004198 if (!PyErr_Occurred())
4199 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004200 sc.c_nlocals,
4201 sc.c_maxstacklevel,
4202 sc.c_flags,
4203 sc.c_code,
4204 consts,
4205 names,
4206 varnames,
4207 freevars,
4208 cellvars,
4209 filename,
4210 name,
4211 sc.c_firstlineno,
4212 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004213 Py_XDECREF(consts);
4214 Py_XDECREF(names);
4215 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004216 Py_XDECREF(freevars);
4217 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004218 Py_XDECREF(filename);
4219 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004220 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004221 else if (!PyErr_Occurred()) {
4222 /* This could happen if someone called PyErr_Clear() after an
4223 error was reported above. That's not supposed to happen,
4224 but I just plugged one case and I'm not sure there can't be
4225 others. In that case, raise SystemError so that at least
4226 it gets reported instead dumping core. */
4227 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4228 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004229 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004230 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004231 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004232 sc.c_symtable = NULL;
4233 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004234 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004235 return co;
4236}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004237
4238int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004239PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004240{
4241 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004242 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004243 int line = co->co_firstlineno;
4244 int addr = 0;
4245 while (--size >= 0) {
4246 addr += *p++;
4247 if (addr > addrq)
4248 break;
4249 line += *p++;
4250 }
4251 return line;
4252}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004253
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004254/* The test for LOCAL must come before the test for FREE in order to
4255 handle classes where name is both local and free. The local var is
4256 a method and the free var is a free var referenced within a method.
4257*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004258
4259static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004260get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004261{
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004262 char buf[350];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004263 PyObject *v;
Jeremy Hylton51257732001-03-01 00:42:55 +00004264
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004265 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4266 return CELL;
4267 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4268 return LOCAL;
4269 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4270 return FREE;
4271 v = PyDict_GetItemString(c->c_globals, name);
4272 if (v) {
4273 if (v == Py_None)
4274 return GLOBAL_EXPLICIT;
4275 else {
4276 return GLOBAL_IMPLICIT;
4277 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004278 }
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00004279 PyOS_snprintf(buf, sizeof(buf),
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004280 "unknown scope for %.100s in %.100s(%s) "
4281 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4282 name, c->c_name,
4283 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4284 c->c_filename,
4285 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4286 PyObject_REPR(c->c_locals),
4287 PyObject_REPR(c->c_globals)
4288 );
4289
4290 Py_FatalError(buf);
4291 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004292}
4293
Guido van Rossum207fda62001-03-02 03:30:41 +00004294/* Helper functions to issue warnings */
4295
4296static int
4297issue_warning(char *msg, char *filename, int lineno)
4298{
4299 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4300 lineno, NULL, NULL) < 0) {
4301 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4302 PyErr_SetString(PyExc_SyntaxError, msg);
4303 PyErr_SyntaxLocation(filename, lineno);
4304 }
4305 return -1;
4306 }
4307 return 0;
4308}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004309
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004310static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004311symtable_warn(struct symtable *st, char *msg)
4312{
Guido van Rossum207fda62001-03-02 03:30:41 +00004313 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004314 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004315 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004316 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004317 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004318}
4319
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004320/* Helper function for setting lineno and filename */
4321
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004322static int
4323symtable_build(struct compiling *c, node *n)
4324{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004325 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004326 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004327 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004328 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004329 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4330 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004331 return -1;
4332 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004333 if (c->c_symtable->st_errors > 0)
4334 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004335 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004336 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004337 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004338 return 0;
4339}
4340
4341static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004342symtable_init_compiling_symbols(struct compiling *c)
4343{
4344 PyObject *varnames;
4345
4346 varnames = c->c_symtable->st_cur->ste_varnames;
4347 if (varnames == NULL) {
4348 varnames = PyList_New(0);
4349 if (varnames == NULL)
4350 return -1;
4351 c->c_symtable->st_cur->ste_varnames = varnames;
4352 Py_INCREF(varnames);
4353 } else
4354 Py_INCREF(varnames);
4355 c->c_varnames = varnames;
4356
4357 c->c_globals = PyDict_New();
4358 if (c->c_globals == NULL)
4359 return -1;
4360 c->c_freevars = PyDict_New();
4361 if (c->c_freevars == NULL)
4362 return -1;
4363 c->c_cellvars = PyDict_New();
4364 if (c->c_cellvars == NULL)
4365 return -1;
4366 return 0;
4367}
4368
4369struct symbol_info {
4370 int si_nlocals;
4371 int si_ncells;
4372 int si_nfrees;
4373 int si_nimplicit;
4374};
4375
4376static void
4377symtable_init_info(struct symbol_info *si)
4378{
4379 si->si_nlocals = 0;
4380 si->si_ncells = 0;
4381 si->si_nfrees = 0;
4382 si->si_nimplicit = 0;
4383}
4384
4385static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004386symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004387 struct symbol_info *si)
4388{
4389 PyObject *dict, *v;
4390
4391 /* Seperate logic for DEF_FREE. If it occurs in a function,
4392 it indicates a local that we must allocate storage for (a
4393 cell var). If it occurs in a class, then the class has a
4394 method and a free variable with the same name.
4395 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004396 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004397 /* If it isn't declared locally, it can't be a cell. */
4398 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4399 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004400 v = PyInt_FromLong(si->si_ncells++);
4401 dict = c->c_cellvars;
4402 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004403 /* If it is free anyway, then there is no need to do
4404 anything here.
4405 */
4406 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004407 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004408 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004409 v = PyInt_FromLong(si->si_nfrees++);
4410 dict = c->c_freevars;
4411 }
4412 if (v == NULL)
4413 return -1;
4414 if (PyDict_SetItem(dict, name, v) < 0) {
4415 Py_DECREF(v);
4416 return -1;
4417 }
4418 Py_DECREF(v);
4419 return 0;
4420}
4421
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004422/* If a variable is a cell and an argument, make sure that appears in
4423 co_cellvars before any variable to its right in varnames.
4424*/
4425
4426
4427static int
4428symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4429 PyObject *varnames, int flags)
4430{
4431 PyObject *v, *w, *d, *list = NULL;
4432 int i, pos;
4433
4434 if (flags & CO_VARARGS)
4435 argcount++;
4436 if (flags & CO_VARKEYWORDS)
4437 argcount++;
4438 for (i = argcount; --i >= 0; ) {
4439 v = PyList_GET_ITEM(varnames, i);
4440 if (PyDict_GetItem(*cellvars, v)) {
4441 if (list == NULL) {
4442 list = PyList_New(1);
4443 if (list == NULL)
4444 return -1;
4445 PyList_SET_ITEM(list, 0, v);
4446 Py_INCREF(v);
4447 } else
4448 PyList_Insert(list, 0, v);
4449 }
4450 }
4451 if (list == NULL || PyList_GET_SIZE(list) == 0)
4452 return 0;
4453 /* There are cellvars that are also arguments. Create a dict
4454 to replace cellvars and put the args at the front.
4455 */
4456 d = PyDict_New();
4457 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4458 v = PyInt_FromLong(i);
4459 if (v == NULL)
4460 goto fail;
4461 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4462 goto fail;
4463 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4464 goto fail;
4465 }
4466 pos = 0;
4467 i = PyList_GET_SIZE(list);
4468 Py_DECREF(list);
4469 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4470 w = PyInt_FromLong(i++); /* don't care about the old key */
4471 if (PyDict_SetItem(d, v, w) < 0) {
4472 Py_DECREF(w);
4473 goto fail;
4474 }
4475 Py_DECREF(w);
4476 }
4477 Py_DECREF(*cellvars);
4478 *cellvars = d;
4479 return 1;
4480 fail:
4481 Py_DECREF(d);
4482 return -1;
4483}
4484
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004485static int
4486symtable_freevar_offsets(PyObject *freevars, int offset)
4487{
4488 PyObject *name, *v;
4489 int pos;
4490
4491 /* The cell vars are the first elements of the closure,
4492 followed by the free vars. Update the offsets in
4493 c_freevars to account for number of cellvars. */
4494 pos = 0;
4495 while (PyDict_Next(freevars, &pos, &name, &v)) {
4496 int i = PyInt_AS_LONG(v) + offset;
4497 PyObject *o = PyInt_FromLong(i);
4498 if (o == NULL)
4499 return -1;
4500 if (PyDict_SetItem(freevars, name, o) < 0) {
4501 Py_DECREF(o);
4502 return -1;
4503 }
4504 Py_DECREF(o);
4505 }
4506 return 0;
4507}
4508
4509static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004510symtable_check_unoptimized(struct compiling *c,
4511 PySymtableEntryObject *ste,
4512 struct symbol_info *si)
4513{
4514 char buf[300];
4515
4516 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4517 || (ste->ste_nested && si->si_nimplicit)))
4518 return 0;
4519
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004520#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4521
4522#define ILLEGAL_IS "is a nested function"
4523
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004524#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004525"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004526
4527#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004528"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004529
4530#define ILLEGAL_EXEC_AND_IMPORT_STAR \
Neal Norwitz150d09d2002-01-29 00:56:37 +00004531"function '%.100s' uses import * and bare exec, which are illegal " \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004532"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004533
4534 /* XXX perhaps the linenos for these opt-breaking statements
4535 should be stored so the exception can point to them. */
4536
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004537 if (ste->ste_child_free) {
4538 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004539 PyOS_snprintf(buf, sizeof(buf),
4540 ILLEGAL_IMPORT_STAR,
4541 PyString_AS_STRING(ste->ste_name),
4542 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004543 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004544 PyOS_snprintf(buf, sizeof(buf),
4545 ILLEGAL_BARE_EXEC,
4546 PyString_AS_STRING(ste->ste_name),
4547 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004548 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004549 PyOS_snprintf(buf, sizeof(buf),
4550 ILLEGAL_EXEC_AND_IMPORT_STAR,
4551 PyString_AS_STRING(ste->ste_name),
4552 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004553 }
4554 } else {
4555 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004556 PyOS_snprintf(buf, sizeof(buf),
4557 ILLEGAL_IMPORT_STAR,
4558 PyString_AS_STRING(ste->ste_name),
4559 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004560 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004561 PyOS_snprintf(buf, sizeof(buf),
4562 ILLEGAL_BARE_EXEC,
4563 PyString_AS_STRING(ste->ste_name),
4564 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004565 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004566 PyOS_snprintf(buf, sizeof(buf),
4567 ILLEGAL_EXEC_AND_IMPORT_STAR,
4568 PyString_AS_STRING(ste->ste_name),
4569 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004570 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004571 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004572
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004573 PyErr_SetString(PyExc_SyntaxError, buf);
4574 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4575 ste->ste_opt_lineno);
4576 return -1;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004577}
4578
4579static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004580symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4581 struct symbol_info *si)
4582{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004583 if (c->c_future)
4584 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004585 if (ste->ste_generator)
4586 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004587 if (ste->ste_type != TYPE_MODULE)
4588 c->c_flags |= CO_NEWLOCALS;
4589 if (ste->ste_type == TYPE_FUNCTION) {
4590 c->c_nlocals = si->si_nlocals;
4591 if (ste->ste_optimized == 0)
4592 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004593 else if (ste->ste_optimized != OPT_EXEC)
4594 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004595 }
4596 return 0;
4597}
4598
4599static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004600symtable_load_symbols(struct compiling *c)
4601{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004602 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004603 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004604 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004605 PyObject *name, *varnames, *v;
4606 int i, flags, pos;
4607 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004608
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004609 if (implicit == NULL) {
4610 implicit = PyInt_FromLong(1);
4611 if (implicit == NULL)
4612 return -1;
4613 }
4614 v = NULL;
4615
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004616 if (symtable_init_compiling_symbols(c) < 0)
4617 goto fail;
4618 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004619 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004620 si.si_nlocals = PyList_GET_SIZE(varnames);
4621 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004622
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004623 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004624 v = PyInt_FromLong(i);
4625 if (PyDict_SetItem(c->c_locals,
4626 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004627 goto fail;
4628 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004629 }
4630
4631 /* XXX The cases below define the rules for whether a name is
4632 local or global. The logic could probably be clearer. */
4633 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004634 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4635 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004636
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004637 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004638 /* undo the original DEF_FREE */
4639 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004640
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004641 /* Deal with names that need two actions:
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004642 1. Cell variables that are also locals.
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004643 2. Free variables in methods that are also class
4644 variables or declared global.
4645 */
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004646 if (flags & (DEF_FREE | DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004647 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004648
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004649 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004650 c->c_argcount--;
4651 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004652 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004653 c->c_argcount--;
4654 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004655 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004656 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004657 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004658 if (flags & DEF_PARAM) {
4659 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004660 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004661 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004662 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004663 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004664 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004665 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004666 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4667 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004668 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004669 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004670 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4671 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004672 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004673 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004674 if (v == NULL)
4675 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004676 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004677 goto fail;
4678 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004679 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004680 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004681 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004682 } else if (is_free(flags)) {
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004683 if (ste->ste_nested) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004684 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004685 if (v == NULL)
4686 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004687 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004688 goto fail;
4689 Py_DECREF(v);
4690 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004691 si.si_nimplicit++;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004692 if (PyDict_SetItem(c->c_globals, name,
4693 implicit) < 0)
4694 goto fail;
4695 if (st->st_nscopes != 1) {
4696 v = PyInt_FromLong(flags);
4697 if (PyDict_SetItem(st->st_global,
4698 name, v))
4699 goto fail;
4700 Py_DECREF(v);
4701 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004702 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004703 }
4704 }
4705
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004706 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4707
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004708 if (si.si_ncells > 1) { /* one cell is always in order */
4709 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4710 c->c_varnames, c->c_flags) < 0)
4711 return -1;
4712 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004713 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4714 return -1;
4715 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004716 fail:
4717 /* is this always the right thing to do? */
4718 Py_XDECREF(v);
4719 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004720}
4721
4722static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004723symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004724{
4725 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004726
4727 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4728 if (st == NULL)
4729 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004730 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004731
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004732 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004733 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004734 goto fail;
4735 if ((st->st_symbols = PyDict_New()) == NULL)
4736 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004737 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004738 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004739 st->st_errors = 0;
4740 st->st_tmpname = 0;
4741 st->st_private = NULL;
4742 return st;
4743 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004744 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004745 return NULL;
4746}
4747
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004748void
4749PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004750{
4751 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004752 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004753 Py_XDECREF(st->st_cur);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004754 PyMem_Free((void *)st);
4755}
4756
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004757/* When the compiler exits a scope, it must should update the scope's
4758 free variable information with the list of free variables in its
4759 children.
4760
4761 Variables that are free in children and defined in the current
4762 scope are cellvars.
4763
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004764 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004765 false), free variables in children that are not defined here are
4766 implicit globals.
4767
4768*/
4769
4770static int
4771symtable_update_free_vars(struct symtable *st)
4772{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004773 int i, j, def;
4774 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004775 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004776
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004777 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004778 def = DEF_FREE_CLASS;
4779 else
4780 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004781 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004782 int pos = 0;
4783
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004784 if (list)
4785 PyList_SetSlice(list, 0,
4786 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004787 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004788 PyList_GET_ITEM(ste->ste_children, i);
4789 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004790 int flags = PyInt_AS_LONG(o);
4791 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004792 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004793 if (list == NULL) {
4794 list = PyList_New(0);
4795 if (list == NULL)
4796 return -1;
4797 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004798 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004799 if (PyList_Append(list, name) < 0) {
4800 Py_DECREF(list);
4801 return -1;
4802 }
4803 }
4804 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004805 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004806 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004807 v = PyDict_GetItem(ste->ste_symbols, name);
4808 /* If a name N is declared global in scope A and
4809 referenced in scope B contained (perhaps
4810 indirectly) in A and there are no scopes
4811 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004812 is global in B. Unless A is a class scope,
4813 because class scopes are not considered for
4814 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004815 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004816 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004817 int flags = PyInt_AS_LONG(v);
4818 if (flags & DEF_GLOBAL) {
4819 symtable_undo_free(st, child->ste_id,
4820 name);
4821 continue;
4822 }
4823 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004824 if (ste->ste_nested) {
4825 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004826 name, def) < 0) {
4827 Py_DECREF(list);
4828 return -1;
4829 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004830 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004831 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004832 name) < 0) {
4833 Py_DECREF(list);
4834 return -1;
4835 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004836 }
4837 }
4838 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004839
4840 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004841 return 0;
4842}
4843
4844/* If the current scope is a non-nested class or if name is not
4845 defined in the current, non-nested scope, then it is an implicit
4846 global in all nested scopes.
4847*/
4848
4849static int
4850symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4851{
4852 PyObject *o;
4853 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004854 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004855
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004856 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004857 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004858 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004859 if (o == NULL)
4860 return symtable_undo_free(st, child, name);
4861 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004862
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004863 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004864 return symtable_undo_free(st, child, name);
4865 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004866 return symtable_add_def_o(st, ste->ste_symbols,
4867 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004868}
4869
4870static int
4871symtable_undo_free(struct symtable *st, PyObject *id,
4872 PyObject *name)
4873{
4874 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004875 PyObject *info;
4876 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004877
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004878 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4879 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004880 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004881
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004882 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004883 if (info == NULL)
4884 return 0;
4885 v = PyInt_AS_LONG(info);
4886 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004887 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004888 DEF_FREE_GLOBAL) < 0)
4889 return -1;
4890 } else
4891 /* If the name is defined here or declared global,
4892 then the recursion stops. */
4893 return 0;
4894
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004895 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4896 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00004897 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004898 PyList_GET_ITEM(ste->ste_children, i);
4899 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004900 if (x < 0)
4901 return x;
4902 }
4903 return 0;
4904}
4905
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004906/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4907 This reference is released when the scope is exited, via the DECREF
4908 in symtable_exit_scope().
4909*/
4910
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004911static int
4912symtable_exit_scope(struct symtable *st)
4913{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004914 int end;
4915
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004916 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004917 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004918 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004919 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004920 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4921 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004922 if (PySequence_DelItem(st->st_stack, end) < 0)
4923 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004924 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004925}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004926
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004927static void
4928symtable_enter_scope(struct symtable *st, char *name, int type,
4929 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004930{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004931 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004932
4933 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004934 prev = st->st_cur;
4935 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4936 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004937 st->st_errors++;
4938 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004939 }
4940 }
Barry Warsaw0372af72001-02-23 18:22:59 +00004941 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004942 PySymtableEntry_New(st, name, type, lineno);
4943 if (strcmp(name, TOP) == 0)
4944 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004945 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004946 if (PyList_Append(prev->ste_children,
4947 (PyObject *)st->st_cur) < 0)
4948 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004949 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004950}
4951
4952static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004953symtable_lookup(struct symtable *st, char *name)
4954{
4955 char buffer[MANGLE_LEN];
4956 PyObject *v;
4957 int flags;
4958
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00004959 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004960 name = buffer;
4961 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4962 if (v == NULL) {
4963 if (PyErr_Occurred())
4964 return -1;
4965 else
4966 return 0;
4967 }
4968
4969 flags = PyInt_AS_LONG(v);
4970 return flags;
4971}
4972
4973static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004974symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004975{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004976 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004977 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004978 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004979
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00004980 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004981 name = buffer;
4982 if ((s = PyString_InternFromString(name)) == NULL)
4983 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004984 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4985 Py_DECREF(s);
4986 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004987}
4988
4989/* Must only be called with mangled names */
4990
4991static int
4992symtable_add_def_o(struct symtable *st, PyObject *dict,
4993 PyObject *name, int flag)
4994{
4995 PyObject *o;
4996 int val;
4997
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004998 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004999 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005000 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005001 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00005002 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005003 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005004 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005005 return -1;
5006 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005007 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005008 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005009 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005010 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005011 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005012 Py_DECREF(o);
5013 return -1;
5014 }
5015 Py_DECREF(o);
5016
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005017 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005018 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005019 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005020 } else if (flag & DEF_GLOBAL) {
5021 /* XXX need to update DEF_GLOBAL for other flags too;
5022 perhaps only DEF_FREE_GLOBAL */
5023 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005024 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005025 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005026 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005027 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005028 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005029 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005030 Py_DECREF(o);
5031 return -1;
5032 }
5033 Py_DECREF(o);
5034 }
5035 return 0;
5036}
5037
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005038#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005039
Tim Peters08a898f2001-06-28 01:52:22 +00005040/* Look for a yield stmt under n. Return 1 if found, else 0.
5041 This hack is used to look inside "if 0:" blocks (which are normally
5042 ignored) in case those are the only places a yield occurs (so that this
5043 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00005044static int
5045look_for_yield(node *n)
5046{
5047 int i;
5048
5049 for (i = 0; i < NCH(n); ++i) {
5050 node *kid = CHILD(n, i);
5051
5052 switch (TYPE(kid)) {
5053
5054 case classdef:
5055 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00005056 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00005057 /* Stuff in nested functions and classes can't make
5058 the parent a generator. */
5059 return 0;
5060
5061 case yield_stmt:
5062 return 1;
5063
5064 default:
5065 if (look_for_yield(kid))
5066 return 1;
5067 }
5068 }
5069 return 0;
5070}
5071
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005072static void
5073symtable_node(struct symtable *st, node *n)
5074{
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005075 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005076
5077 loop:
5078 switch (TYPE(n)) {
5079 case funcdef: {
5080 char *func_name = STR(CHILD(n, 1));
5081 symtable_add_def(st, func_name, DEF_LOCAL);
5082 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005083 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005084 symtable_funcdef(st, n);
5085 symtable_exit_scope(st);
5086 break;
5087 }
5088 case lambdef:
5089 if (NCH(n) == 4)
5090 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005091 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005092 symtable_funcdef(st, n);
5093 symtable_exit_scope(st);
5094 break;
5095 case classdef: {
5096 char *tmp, *class_name = STR(CHILD(n, 1));
5097 symtable_add_def(st, class_name, DEF_LOCAL);
5098 if (TYPE(CHILD(n, 2)) == LPAR) {
5099 node *bases = CHILD(n, 3);
5100 int i;
5101 for (i = 0; i < NCH(bases); i += 2) {
5102 symtable_node(st, CHILD(bases, i));
5103 }
5104 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005105 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005106 tmp = st->st_private;
5107 st->st_private = class_name;
5108 symtable_node(st, CHILD(n, NCH(n) - 1));
5109 st->st_private = tmp;
5110 symtable_exit_scope(st);
5111 break;
5112 }
5113 case if_stmt:
5114 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00005115 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5116 if (st->st_cur->ste_generator == 0)
5117 st->st_cur->ste_generator =
5118 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005119 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00005120 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005121 symtable_node(st, CHILD(n, i + 1));
5122 symtable_node(st, CHILD(n, i + 3));
5123 }
5124 if (i + 2 < NCH(n))
5125 symtable_node(st, CHILD(n, i + 2));
5126 break;
5127 case global_stmt:
5128 symtable_global(st, n);
5129 break;
5130 case import_stmt:
5131 symtable_import(st, n);
5132 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005133 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005134 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005135 symtable_node(st, CHILD(n, 1));
5136 if (NCH(n) > 2)
5137 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005138 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005139 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005140 st->st_cur->ste_opt_lineno = n->n_lineno;
5141 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005142 if (NCH(n) > 4)
5143 symtable_node(st, CHILD(n, 5));
5144 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005145
5146 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00005147 case assert_stmt:
5148 if (Py_OptimizeFlag)
5149 return;
5150 if (NCH(n) == 2) {
5151 n = CHILD(n, 1);
5152 goto loop;
5153 } else {
5154 symtable_node(st, CHILD(n, 1));
5155 n = CHILD(n, 3);
5156 goto loop;
5157 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005158 case except_clause:
5159 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005160 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005161 if (NCH(n) > 1) {
5162 n = CHILD(n, 1);
5163 goto loop;
5164 }
5165 break;
5166 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005167 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005168 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005169 case yield_stmt:
5170 st->st_cur->ste_generator = 1;
5171 n = CHILD(n, 1);
5172 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005173 case expr_stmt:
5174 if (NCH(n) == 1)
5175 n = CHILD(n, 0);
5176 else {
5177 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005178 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005179 symtable_node(st, CHILD(n, 2));
5180 break;
5181 } else {
5182 int i;
5183 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005184 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005185 n = CHILD(n, NCH(n) - 1);
5186 }
5187 }
5188 goto loop;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005189 case list_iter:
5190 n = CHILD(n, 0);
5191 if (TYPE(n) == list_for) {
5192 st->st_tmpname++;
5193 symtable_list_comprehension(st, n);
5194 st->st_tmpname--;
5195 } else {
5196 REQ(n, list_if);
5197 symtable_node(st, CHILD(n, 1));
5198 if (NCH(n) == 3) {
5199 n = CHILD(n, 2);
5200 goto loop;
5201 }
5202 }
5203 break;
5204 case for_stmt:
5205 symtable_assign(st, CHILD(n, 1), 0);
5206 for (i = 3; i < NCH(n); ++i)
5207 if (TYPE(CHILD(n, i)) >= single_input)
5208 symtable_node(st, CHILD(n, i));
5209 break;
5210 /* The remaining cases fall through to default except in
5211 special circumstances. This requires the individual cases
5212 to be coded with great care, even though they look like
5213 rather innocuous. Each case must double-check TYPE(n).
5214 */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005215 case argument:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005216 if (TYPE(n) == argument && NCH(n) == 3) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005217 n = CHILD(n, 2);
5218 goto loop;
5219 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005220 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005221 case listmaker:
5222 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005223 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005224 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005225 symtable_node(st, CHILD(n, 0));
5226 st->st_tmpname--;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005227 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005228 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005229 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005230 case atom:
5231 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5232 symtable_add_use(st, STR(CHILD(n, 0)));
5233 break;
5234 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005235 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005236 default:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005237 /* Walk over every non-token child with a special case
5238 for one child.
5239 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005240 if (NCH(n) == 1) {
5241 n = CHILD(n, 0);
5242 goto loop;
5243 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005244 for (i = 0; i < NCH(n); ++i)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005245 if (TYPE(CHILD(n, i)) >= single_input)
5246 symtable_node(st, CHILD(n, i));
5247 }
5248}
5249
5250static void
5251symtable_funcdef(struct symtable *st, node *n)
5252{
5253 node *body;
5254
5255 if (TYPE(n) == lambdef) {
5256 if (NCH(n) == 4)
5257 symtable_params(st, CHILD(n, 1));
5258 } else
5259 symtable_params(st, CHILD(n, 2));
5260 body = CHILD(n, NCH(n) - 1);
5261 symtable_node(st, body);
5262}
5263
5264/* The next two functions parse the argument tuple.
5265 symtable_default_arg() checks for names in the default arguments,
5266 which are references in the defining scope. symtable_params()
5267 parses the parameter names, which are defined in the function's
5268 body.
5269
5270 varargslist:
5271 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5272 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5273*/
5274
5275static void
5276symtable_default_args(struct symtable *st, node *n)
5277{
5278 node *c;
5279 int i;
5280
5281 if (TYPE(n) == parameters) {
5282 n = CHILD(n, 1);
5283 if (TYPE(n) == RPAR)
5284 return;
5285 }
5286 REQ(n, varargslist);
5287 for (i = 0; i < NCH(n); i += 2) {
5288 c = CHILD(n, i);
5289 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5290 break;
5291 }
5292 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5293 symtable_node(st, CHILD(n, i));
5294 }
5295}
5296
5297static void
5298symtable_params(struct symtable *st, node *n)
5299{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005300 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005301 node *c = NULL;
5302
5303 if (TYPE(n) == parameters) {
5304 n = CHILD(n, 1);
5305 if (TYPE(n) == RPAR)
5306 return;
5307 }
5308 REQ(n, varargslist);
5309 for (i = 0; i < NCH(n); i += 2) {
5310 c = CHILD(n, i);
5311 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5312 ext = 1;
5313 break;
5314 }
5315 if (TYPE(c) == test) {
5316 continue;
5317 }
5318 if (TYPE(CHILD(c, 0)) == NAME)
5319 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5320 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005321 char nbuf[30];
5322 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005323 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005324 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005325 }
5326 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005327 if (ext) {
5328 c = CHILD(n, i);
5329 if (TYPE(c) == STAR) {
5330 i++;
5331 symtable_add_def(st, STR(CHILD(n, i)),
5332 DEF_PARAM | DEF_STAR);
5333 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005334 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005335 c = NULL;
5336 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005337 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005338 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005339 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005340 i++;
5341 symtable_add_def(st, STR(CHILD(n, i)),
5342 DEF_PARAM | DEF_DOUBLESTAR);
5343 }
5344 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005345 if (complex >= 0) {
5346 int j;
5347 for (j = 0; j <= complex; j++) {
5348 c = CHILD(n, j);
5349 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005350 c = CHILD(n, ++j);
5351 else if (TYPE(c) == EQUAL)
5352 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005353 if (TYPE(CHILD(c, 0)) == LPAR)
5354 symtable_params_fplist(st, CHILD(c, 1));
5355 }
5356 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005357}
5358
5359static void
5360symtable_params_fplist(struct symtable *st, node *n)
5361{
5362 int i;
5363 node *c;
5364
5365 REQ(n, fplist);
5366 for (i = 0; i < NCH(n); i += 2) {
5367 c = CHILD(n, i);
5368 REQ(c, fpdef);
5369 if (NCH(c) == 1)
5370 symtable_add_def(st, STR(CHILD(c, 0)),
5371 DEF_PARAM | DEF_INTUPLE);
5372 else
5373 symtable_params_fplist(st, CHILD(c, 1));
5374 }
5375
5376}
5377
5378static void
5379symtable_global(struct symtable *st, node *n)
5380{
5381 int i;
5382
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005383 /* XXX It might be helpful to warn about module-level global
5384 statements, but it's hard to tell the difference between
5385 module-level and a string passed to exec.
5386 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005387
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005388 for (i = 1; i < NCH(n); i += 2) {
5389 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005390 int flags;
5391
5392 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005393 if (flags < 0)
5394 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005395 if (flags && flags != DEF_GLOBAL) {
5396 char buf[500];
5397 if (flags & DEF_PARAM) {
5398 PyErr_Format(PyExc_SyntaxError,
5399 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005400 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005401 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005402 st->st_cur->ste_lineno);
5403 st->st_errors++;
5404 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005405 }
5406 else {
5407 if (flags & DEF_LOCAL)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005408 PyOS_snprintf(buf, sizeof(buf),
5409 GLOBAL_AFTER_ASSIGN,
5410 name);
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005411 else
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005412 PyOS_snprintf(buf, sizeof(buf),
5413 GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005414 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005415 }
5416 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005417 symtable_add_def(st, name, DEF_GLOBAL);
5418 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005419}
5420
5421static void
5422symtable_list_comprehension(struct symtable *st, node *n)
5423{
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005424 char tmpname[30];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005425
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005426 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005427 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005428 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005429 symtable_node(st, CHILD(n, 3));
5430 if (NCH(n) == 5)
5431 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005432}
5433
5434static void
5435symtable_import(struct symtable *st, node *n)
5436{
5437 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005438 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005439 | 'from' dotted_name 'import'
5440 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005441 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005442 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005443 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005444 node *dotname = CHILD(n, 1);
5445 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5446 /* check for bogus imports */
5447 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5448 PyErr_SetString(PyExc_SyntaxError,
5449 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005450 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005451 n->n_lineno);
5452 st->st_errors++;
5453 return;
5454 }
5455 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005456 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005457 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005458 if (symtable_warn(st,
5459 "import * only allowed at module level") < 0)
5460 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005461 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005462 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005463 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005464 } else {
5465 for (i = 3; i < NCH(n); i += 2) {
5466 node *c = CHILD(n, i);
5467 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005468 symtable_assign(st, CHILD(c, 2),
5469 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005470 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005471 symtable_assign(st, CHILD(c, 0),
5472 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005473 }
5474 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005475 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005476 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005477 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005478 }
5479 }
5480}
5481
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005482/* The third argument to symatble_assign() is a flag to be passed to
5483 symtable_add_def() if it is eventually called. The flag is useful
5484 to specify the particular type of assignment that should be
5485 recorded, e.g. an assignment caused by import.
5486 */
5487
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005488static void
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005489symtable_assign(struct symtable *st, node *n, int def_flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005490{
5491 node *tmp;
5492 int i;
5493
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005494 loop:
5495 switch (TYPE(n)) {
5496 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005497 /* invalid assignment, e.g. lambda x:x=2. The next
5498 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005499 return;
5500 case power:
5501 if (NCH(n) > 2) {
5502 for (i = 2; i < NCH(n); ++i)
5503 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5504 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005505 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005506 if (NCH(n) > 1) {
5507 symtable_node(st, CHILD(n, 0));
5508 symtable_node(st, CHILD(n, 1));
5509 } else {
5510 n = CHILD(n, 0);
5511 goto loop;
5512 }
5513 return;
5514 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005515 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5516 /* XXX This is an error, but the next pass
5517 will catch it. */
5518 return;
5519 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005520 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005521 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005522 }
5523 return;
5524 case exprlist:
5525 case testlist:
Guido van Rossum2d3b9862002-05-24 15:47:06 +00005526 case testlist1:
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005527 if (NCH(n) == 1) {
5528 n = CHILD(n, 0);
5529 goto loop;
5530 }
5531 else {
5532 int i;
5533 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005534 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005535 return;
5536 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005537 case atom:
5538 tmp = CHILD(n, 0);
5539 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5540 n = CHILD(n, 1);
5541 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005542 } else if (TYPE(tmp) == NAME) {
Jeremy Hylton778e2652001-11-09 19:50:08 +00005543 if (strcmp(STR(tmp), "__debug__") == 0) {
5544 PyErr_SetString(PyExc_SyntaxError,
5545 ASSIGN_DEBUG);
5546 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton86424e32001-12-04 02:41:46 +00005547 n->n_lineno);
Jeremy Hylton778e2652001-11-09 19:50:08 +00005548 st->st_errors++;
5549 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005550 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005551 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005552 return;
5553 case dotted_as_name:
5554 if (NCH(n) == 3)
5555 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005556 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005557 else
5558 symtable_add_def(st,
5559 STR(CHILD(CHILD(n,
5560 0), 0)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005561 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005562 return;
5563 case dotted_name:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005564 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005565 return;
5566 case NAME:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005567 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005568 return;
5569 default:
5570 if (NCH(n) == 0)
5571 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005572 if (NCH(n) == 1) {
5573 n = CHILD(n, 0);
5574 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005575 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005576 /* Should only occur for errors like x + 1 = 1,
5577 which will be caught in the next pass. */
5578 for (i = 0; i < NCH(n); ++i)
5579 if (TYPE(CHILD(n, i)) >= single_input)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005580 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005581 }
5582}