blob: 9a2d50b0b5cd1e854a80d32f4be47e82d7271832 [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 \
64"name '%.400s' is a function paramter and declared global"
65
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
76static struct memberlist 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 Rossum79f25d91997-04-29 20:08:16 +000094static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000095code_getattr(PyCodeObject *co, char *name)
Guido van Rossum3f5da241990-12-20 15:06:42 +000096{
Guido van Rossum79f25d91997-04-29 20:08:16 +000097 return PyMember_Get((char *)co, code_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +000098}
Guido van Rossum10dc2e81990-11-18 17:27:39 +000099
100static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000101code_dealloc(PyCodeObject *co)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 Py_XDECREF(co->co_code);
104 Py_XDECREF(co->co_consts);
105 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +0000106 Py_XDECREF(co->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000107 Py_XDECREF(co->co_freevars);
108 Py_XDECREF(co->co_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109 Py_XDECREF(co->co_filename);
110 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +0000111 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000112 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000113}
114
Guido van Rossum79f25d91997-04-29 20:08:16 +0000115static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000116code_repr(PyCodeObject *co)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000117{
118 char buf[500];
119 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000120 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000121 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +0000122
Guido van Rossuma396a882000-04-07 01:21:36 +0000123 if (co->co_firstlineno != 0)
124 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000125 if (co->co_filename && PyString_Check(co->co_filename))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000126 filename = PyString_AS_STRING(co->co_filename);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000127 if (co->co_name && PyString_Check(co->co_name))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000128 name = PyString_AS_STRING(co->co_name);
Fred Drake615ae552000-06-30 16:20:13 +0000129 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
130 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000131 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000132}
133
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000134static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000135code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000136{
137 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000138 cmp = PyObject_Compare(co->co_name, cp->co_name);
139 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000140 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000141 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000142 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000143 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000144 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000145 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000146 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000147 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000148 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000149 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000150 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000151 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000152 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000153 if (cmp) return cmp;
154 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
155 if (cmp) return cmp;
156 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000157 return cmp;
158}
159
160static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000161code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000162{
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000163 long h, h0, h1, h2, h3, h4, h5, h6;
Guido van Rossum44679592000-04-10 16:20:31 +0000164 h0 = PyObject_Hash(co->co_name);
165 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000166 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000167 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000168 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000169 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000170 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000171 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000172 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000173 if (h4 == -1) return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000174 h5 = PyObject_Hash(co->co_freevars);
175 if (h5 == -1) return -1;
176 h6 = PyObject_Hash(co->co_cellvars);
177 if (h6 == -1) return -1;
178 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000179 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000180 if (h == -1) h = -2;
181 return h;
182}
183
Jeremy Hylton78891072001-03-01 06:09:34 +0000184/* XXX code objects need to participate in GC? */
185
Guido van Rossum79f25d91997-04-29 20:08:16 +0000186PyTypeObject PyCode_Type = {
187 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000188 0,
189 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000190 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000191 0,
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000192 (destructor)code_dealloc, /*tp_dealloc*/
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000193 0, /*tp_print*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000194 (getattrfunc)code_getattr, /*tp_getattr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000195 0, /*tp_setattr*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000196 (cmpfunc)code_compare, /*tp_compare*/
197 (reprfunc)code_repr, /*tp_repr*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000198 0, /*tp_as_number*/
199 0, /*tp_as_sequence*/
200 0, /*tp_as_mapping*/
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000201 (hashfunc)code_hash, /*tp_hash*/
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000202};
203
Guido van Rossum644a12b1997-04-09 19:24:53 +0000204#define NAME_CHARS \
205 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
206
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000207/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
208
209static int
210all_name_chars(unsigned char *s)
211{
Guido van Rossumcd90c202001-02-09 15:06:42 +0000212 static char ok_name_char[256];
213 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000214
Guido van Rossumcd90c202001-02-09 15:06:42 +0000215 if (ok_name_char[*name_chars] == 0) {
216 unsigned char *p;
217 for (p = name_chars; *p; p++)
218 ok_name_char[*p] = 1;
219 }
220 while (*s) {
221 if (ok_name_char[*s++] == 0)
222 return 0;
223 }
224 return 1;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000225}
226
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000227static int
228intern_strings(PyObject *tuple)
229{
230 int i;
231
232 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
233 PyObject *v = PyTuple_GET_ITEM(tuple, i);
234 if (v == NULL || !PyString_Check(v)) {
235 Py_FatalError("non-string found in code slot");
236 PyErr_BadInternalCall();
237 return -1;
238 }
239 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
240 }
241 return 0;
242}
243
Guido van Rossum79f25d91997-04-29 20:08:16 +0000244PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000245PyCode_New(int argcount, int nlocals, int stacksize, int flags,
246 PyObject *code, PyObject *consts, PyObject *names,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000247 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
248 PyObject *filename, PyObject *name, int firstlineno,
249 PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000250{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000251 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000252 int i;
Guido van Rossumd076c731998-10-07 19:42:25 +0000253 PyBufferProcs *pb;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000254 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000255 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000256 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 consts == NULL || !PyTuple_Check(consts) ||
258 names == NULL || !PyTuple_Check(names) ||
259 varnames == NULL || !PyTuple_Check(varnames) ||
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000260 freevars == NULL || !PyTuple_Check(freevars) ||
261 cellvars == NULL || !PyTuple_Check(cellvars) ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262 name == NULL || !PyString_Check(name) ||
263 filename == NULL || !PyString_Check(filename) ||
264 lnotab == NULL || !PyString_Check(lnotab)) {
265 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000266 return NULL;
267 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000268 pb = code->ob_type->tp_as_buffer;
269 if (pb == NULL ||
270 pb->bf_getreadbuffer == NULL ||
271 pb->bf_getsegcount == NULL ||
272 (*pb->bf_getsegcount)(code, NULL) != 1)
273 {
274 PyErr_BadInternalCall();
275 return NULL;
276 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000277 intern_strings(names);
278 intern_strings(varnames);
279 if (freevars == NULL)
280 freevars = PyTuple_New(0);
281 intern_strings(freevars);
282 if (cellvars == NULL)
283 cellvars = PyTuple_New(0);
284 intern_strings(cellvars);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000285 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 for (i = PyTuple_Size(consts); --i >= 0; ) {
287 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000288 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000289 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000290 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000291 continue;
292 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000293 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000294 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000295 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000296 co->co_argcount = argcount;
297 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000298 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000299 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000300 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000301 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000302 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000303 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000305 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000307 co->co_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000308 Py_INCREF(freevars);
309 co->co_freevars = freevars;
310 Py_INCREF(cellvars);
311 co->co_cellvars = cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000313 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000315 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000316 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000318 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319 }
320 return co;
321}
322
323
324/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000325
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000326/* The compiler uses two passes to generate bytecodes. The first pass
327 builds the symbol table. The second pass generates the bytecode.
328
329 The first pass uses a single symtable struct. The second pass uses
330 a compiling struct for each code block. The compiling structs
331 share a reference to the symtable.
332
333 The two passes communicate via symtable_load_symbols() and via
334 is_local() and is_global(). The former initializes several slots
335 in the compiling struct: c_varnames, c_locals, c_nlocals,
336 c_argcount, c_globals, and c_flags.
337*/
338
Tim Peters2a7f3842001-06-09 09:26:21 +0000339/* All about c_lnotab.
340
341c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
342mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
343to source code line #s (when needed for tracebacks) via c_lnotab instead.
344The array is conceptually a list of
345 (bytecode offset increment, line number increment)
346pairs. The details are important and delicate, best illustrated by example:
347
348 byte code offset source code line number
349 0 1
350 6 2
351 50 7
352 350 307
353 361 308
354
355The first trick is that these numbers aren't stored, only the increments
356from one row to the next (this doesn't really work, but it's a start):
357
358 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
359
360The second trick is that an unsigned byte can't hold negative values, or
361values larger than 255, so (a) there's a deep assumption that byte code
362offsets and their corresponding line #s both increase monotonically, and (b)
363if at least one column jumps by more than 255 from one row to the next, more
364than one pair is written to the table. In case #b, there's no way to know
365from looking at the table later how many were written. That's the delicate
366part. A user of c_lnotab desiring to find the source line number
367corresponding to a bytecode address A should do something like this
368
369 lineno = addr = 0
370 for addr_incr, line_incr in c_lnotab:
371 addr += addr_incr
372 if addr > A:
373 return lineno
374 lineno += line_incr
375
376In order for this to work, when the addr field increments by more than 255,
377the line # increment in each pair generated must be 0 until the remaining addr
378increment is < 256. So, in the example above, com_set_lineno should not (as
379was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
380255, 0, 45, 255, 0, 45.
381*/
382
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000383struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000384 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000385 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000386 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000387 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000388 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000389 PyObject *c_globals; /* dictionary (value=None) */
390 PyObject *c_locals; /* dictionary (value=localID) */
391 PyObject *c_varnames; /* list (inverse of c_locals) */
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000392 PyObject *c_freevars; /* dictionary (value=None) */
393 PyObject *c_cellvars; /* list */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000394 int c_nlocals; /* index of next local */
395 int c_argcount; /* number of top-level arguments */
396 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000397 int c_nexti; /* index into c_code */
398 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000399 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000400 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000401 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000402 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000403 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000404 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000405 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000406 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000407 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000408 int c_stacklevel; /* Current stack level */
409 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000410 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000411 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000412 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000413 char *c_private; /* for private name mangling */
Skip Montanaro803d6e52000-08-12 18:09:51 +0000414 int c_tmpname; /* temporary local name counter */
Guido van Rossumcd90c202001-02-09 15:06:42 +0000415 int c_nested; /* Is block nested funcdef or lamdef? */
416 int c_closure; /* Is nested w/freevars? */
417 struct symtable *c_symtable; /* pointer to module symbol table */
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000418 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000419};
420
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000421static int
422is_free(int v)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000423{
424 if ((v & (USE | DEF_FREE))
425 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
426 return 1;
427 if (v & DEF_FREE_CLASS)
428 return 1;
429 return 0;
430}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000431
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000432static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000433com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000434{
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000435 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
436
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000437 if (c == NULL) {
438 /* Error occurred via symtable call to
439 is_constant_false */
440 PyErr_SetString(exc, msg);
441 return;
442 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000443 c->c_errors++;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000444 if (c->c_lineno < 1 || c->c_interactive) {
445 /* Unknown line number or interactive input */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000446 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000447 return;
448 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000449 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000450 if (v == NULL)
451 return; /* MemoryError, too bad */
Fred Drakedcf08e02000-08-15 15:49:44 +0000452
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000453 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000454 if (line == NULL) {
455 Py_INCREF(Py_None);
456 line = Py_None;
457 }
458 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
459 Py_None, line);
460 if (t == NULL)
461 goto exit;
462 w = Py_BuildValue("(OO)", v, t);
463 if (w == NULL)
464 goto exit;
465 PyErr_SetObject(exc, w);
466 exit:
467 Py_XDECREF(t);
468 Py_XDECREF(v);
469 Py_XDECREF(w);
470 Py_XDECREF(line);
Guido van Rossum452a9831996-09-17 14:32:04 +0000471}
472
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000473/* Interface to the block stack */
474
475static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000476block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000477{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000478 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000479 com_error(c, PyExc_SystemError,
480 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000481 }
482 else {
483 c->c_block[c->c_nblocks++] = type;
484 }
485}
486
487static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000488block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000489{
490 if (c->c_nblocks > 0)
491 c->c_nblocks--;
492 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000493 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000494 }
495}
496
Guido van Rossum681d79a1995-07-18 14:51:37 +0000497/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000498
Tim Petersdbd9ba62000-07-09 03:09:57 +0000499static int com_init(struct compiling *, char *);
500static void com_free(struct compiling *);
501static void com_push(struct compiling *, int);
502static void com_pop(struct compiling *, int);
503static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000504static void com_node(struct compiling *, node *);
505static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000506static void com_addbyte(struct compiling *, int);
507static void com_addint(struct compiling *, int);
508static void com_addoparg(struct compiling *, int, int);
509static void com_addfwref(struct compiling *, int, int *);
510static void com_backpatch(struct compiling *, int);
511static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
512static int com_addconst(struct compiling *, PyObject *);
513static int com_addname(struct compiling *, PyObject *);
514static void com_addopname(struct compiling *, int, node *);
515static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000516static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000517static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000518static void com_assign(struct compiling *, node *, int, node *);
519static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000520static PyCodeObject *icompile(node *, struct compiling *);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000521static PyCodeObject *jcompile(node *, char *, struct compiling *,
522 PyCompilerFlags *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000523static PyObject *parsestrplus(node *);
524static PyObject *parsestr(char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000525static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000526
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000527static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000528
529/* symtable operations */
530static int symtable_build(struct compiling *, node *);
531static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000532static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000533static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000534static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000535static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000536static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000537
538static void symtable_node(struct symtable *, node *);
539static void symtable_funcdef(struct symtable *, node *);
540static void symtable_default_args(struct symtable *, node *);
541static void symtable_params(struct symtable *, node *);
542static void symtable_params_fplist(struct symtable *, node *n);
543static void symtable_global(struct symtable *, node *);
544static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000545static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000546static void symtable_list_comprehension(struct symtable *, node *);
547
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000548static int symtable_update_free_vars(struct symtable *);
549static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
550static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
551
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000552/* helper */
553static void
554do_pad(int pad)
555{
556 int i;
557 for (i = 0; i < pad; ++i)
558 fprintf(stderr, " ");
559}
560
561static void
562dump(node *n, int pad, int depth)
563{
564 int i;
565 if (depth == 0)
566 return;
567 do_pad(pad);
568 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
569 if (depth > 0)
570 depth--;
571 for (i = 0; i < NCH(n); ++i)
572 dump(CHILD(n, i), pad + 1, depth);
573}
574
575#define DUMP(N) dump(N, 0, -1)
576
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000577static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000578com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000579{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000580 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000581 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
582 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000583 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000584 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000585 goto fail;
586 if ((c->c_const_dict = PyDict_New()) == NULL)
587 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000588 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000589 goto fail;
590 if ((c->c_name_dict = PyDict_New()) == NULL)
591 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000592 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000593 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
595 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000596 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000597 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000598 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000599 c->c_freevars = NULL;
600 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000601 c->c_nlocals = 0;
602 c->c_argcount = 0;
603 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000604 c->c_nexti = 0;
605 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000606 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000607 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000608 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000609 c->c_begin = 0;
610 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000611 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000612 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000613 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000614 c->c_stacklevel = 0;
615 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000616 c->c_firstlineno = 0;
617 c->c_last_addr = 0;
618 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000619 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000620 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000621 c->c_nested = 0;
622 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000623 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000624 return 1;
625
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000626 fail:
627 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000628 return 0;
629}
630
631static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000632com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000633{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000634 Py_XDECREF(c->c_code);
635 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000636 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000637 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000638 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000639 Py_XDECREF(c->c_globals);
640 Py_XDECREF(c->c_locals);
641 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000642 Py_XDECREF(c->c_freevars);
643 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 Py_XDECREF(c->c_lnotab);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000645 if (c->c_future)
646 PyMem_Free((void *)c->c_future);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000647}
648
649static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000651{
652 c->c_stacklevel += n;
653 if (c->c_stacklevel > c->c_maxstacklevel)
654 c->c_maxstacklevel = c->c_stacklevel;
655}
656
657static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000658com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000659{
660 if (c->c_stacklevel < n) {
Guido van Rossum90f827c1998-08-25 18:22:17 +0000661 /* fprintf(stderr,
Guido van Rossum8b993a91997-01-17 21:04:03 +0000662 "%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
663 c->c_filename, c->c_lineno,
Guido van Rossum90f827c1998-08-25 18:22:17 +0000664 c->c_nexti, c->c_stacklevel, n); */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000665 c->c_stacklevel = 0;
666 }
667 else
668 c->c_stacklevel -= n;
669}
670
671static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000672com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000673{
674 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000675 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000676 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000677 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000678}
679
680static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000681com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000682{
683 int len;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000684 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000685 assert(byte >= 0 && byte <= 255);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000686 if (byte < 0 || byte > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000687 com_error(c, PyExc_SystemError,
688 "com_addbyte: byte out of range");
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000689 }
690 if (c->c_code == NULL)
691 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000692 len = PyString_Size(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000693 if (c->c_nexti >= len) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 if (_PyString_Resize(&c->c_code, len+1000) != 0) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000695 c->c_errors++;
696 return;
697 }
698 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000699 PyString_AsString(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000700}
701
702static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000703com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000704{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000705 com_addbyte(c, x & 0xff);
706 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000707}
708
709static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000710com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000711{
712 int size;
713 char *p;
714 if (c->c_lnotab == NULL)
715 return;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000716 size = PyString_Size(c->c_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000717 if (c->c_lnotab_next+2 > size) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000718 if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000719 c->c_errors++;
720 return;
721 }
722 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000723 p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000724 *p++ = addr;
725 *p++ = line;
726 c->c_lnotab_next += 2;
727}
728
729static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000730com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000731{
732 c->c_lineno = lineno;
733 if (c->c_firstlineno == 0) {
734 c->c_firstlineno = c->c_last_line = lineno;
735 }
736 else {
737 int incr_addr = c->c_nexti - c->c_last_addr;
738 int incr_line = lineno - c->c_last_line;
Tim Peters2a7f3842001-06-09 09:26:21 +0000739 while (incr_addr > 255) {
740 com_add_lnotab(c, 255, 0);
741 incr_addr -= 255;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000742 }
Tim Peters2a7f3842001-06-09 09:26:21 +0000743 while (incr_line > 255) {
744 com_add_lnotab(c, incr_addr, 255);
745 incr_line -=255;
746 incr_addr = 0;
747 }
748 if (incr_addr > 0 || incr_line > 0)
749 com_add_lnotab(c, incr_addr, incr_line);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000750 c->c_last_addr = c->c_nexti;
751 c->c_last_line = lineno;
752 }
753}
754
755static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000756com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000757{
Fred Drakeef8ace32000-08-24 00:32:09 +0000758 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000759 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000760 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000761 if (Py_OptimizeFlag)
762 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000763 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000764 if (extended_arg){
765 com_addbyte(c, EXTENDED_ARG);
766 com_addint(c, extended_arg);
767 arg &= 0xffff;
768 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000769 com_addbyte(c, op);
770 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000771}
772
773static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000774com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000775{
776 /* Compile a forward reference for backpatching */
777 int here;
778 int anchor;
779 com_addbyte(c, op);
780 here = c->c_nexti;
781 anchor = *p_anchor;
782 *p_anchor = here;
783 com_addint(c, anchor == 0 ? 0 : here - anchor);
784}
785
786static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000787com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000788{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000789 unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000790 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000791 int dist;
792 int prev;
793 for (;;) {
794 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000795 prev = code[anchor] + (code[anchor+1] << 8);
796 dist = target - (anchor+2);
797 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000798 dist >>= 8;
799 code[anchor+1] = dist;
800 dist >>= 8;
801 if (dist) {
802 com_error(c, PyExc_SystemError,
803 "com_backpatch: offset too large");
804 break;
805 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000806 if (!prev)
807 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000808 anchor -= prev;
809 }
810}
811
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000812/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000813
814static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000815com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000816{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000817 PyObject *w, *t, *np=NULL;
818 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000819
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000820 t = Py_BuildValue("(OO)", v, v->ob_type);
821 if (t == NULL)
822 goto fail;
823 w = PyDict_GetItem(dict, t);
824 if (w != NULL) {
825 n = PyInt_AsLong(w);
826 } else {
827 n = PyList_Size(list);
828 np = PyInt_FromLong(n);
829 if (np == NULL)
830 goto fail;
831 if (PyList_Append(list, v) != 0)
832 goto fail;
833 if (PyDict_SetItem(dict, t, np) != 0)
834 goto fail;
835 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000836 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000837 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000838 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000839 fail:
840 Py_XDECREF(np);
841 Py_XDECREF(t);
842 c->c_errors++;
843 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000844}
845
846static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000847com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000848{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000849 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000850}
851
852static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000853com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000854{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000855 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000856}
857
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000858static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000859mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000860{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000861 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000862 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000863 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000864 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
865 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000866 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000867 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000868 return 0; /* Don't mangle __extremely_long_names */
869 if (name[nlen-1] == '_' && name[nlen-2] == '_')
870 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000871 /* Strip leading underscores from class name */
872 while (*p == '_')
873 p++;
874 if (*p == '\0')
875 return 0; /* Don't mangle if class is just underscores */
876 plen = strlen(p);
877 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000878 plen = maxlen-nlen-2; /* Truncate class name if too long */
879 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000880 buffer[0] = '_';
881 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000882 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000883 return 1;
884}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000885
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000886static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000887com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000888{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000889 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000890 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000891 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000892
893 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000894 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000895 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000896 c->c_errors++;
897 i = 255;
898 }
899 else {
900 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000901 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000902 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000903 com_addoparg(c, op, i);
904}
905
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000906#define NAME_LOCAL 0
907#define NAME_GLOBAL 1
908#define NAME_DEFAULT 2
909#define NAME_CLOSURE 3
910
911static int
912com_lookup_arg(PyObject *dict, PyObject *name)
913{
914 PyObject *v = PyDict_GetItem(dict, name);
915 if (v == NULL)
916 return -1;
917 else
918 return PyInt_AS_LONG(v);
919}
920
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000921static void
922com_addop_varname(struct compiling *c, int kind, char *name)
923{
924 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000925 int i, reftype;
926 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000927 int op = STOP_CODE;
928 char buffer[MANGLE_LEN];
929
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000930 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000931 name = buffer;
932 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
933 c->c_errors++;
934 i = 255;
935 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000936 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000937
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000938 reftype = get_ref_type(c, name);
939 switch (reftype) {
940 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000941 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000942 scope = NAME_LOCAL;
943 break;
944 case GLOBAL_EXPLICIT:
945 scope = NAME_GLOBAL;
946 break;
947 case GLOBAL_IMPLICIT:
948 if (c->c_flags & CO_OPTIMIZED)
949 scope = NAME_GLOBAL;
950 break;
951 case FREE:
952 case CELL:
953 scope = NAME_CLOSURE;
954 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000955 }
956
957 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000958 if (scope == NAME_LOCAL)
959 i = com_lookup_arg(c->c_locals, v);
960 else if (reftype == FREE)
961 i = com_lookup_arg(c->c_freevars, v);
962 else if (reftype == CELL)
963 i = com_lookup_arg(c->c_cellvars, v);
964 if (i == -1) {
965 c->c_errors++; /* XXX no exception set */
966 i = 255;
967 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000968 }
969 Py_DECREF(v);
970
971 switch (kind) {
972 case VAR_LOAD:
973 switch (scope) {
974 case NAME_LOCAL:
975 op = LOAD_FAST;
976 break;
977 case NAME_GLOBAL:
978 op = LOAD_GLOBAL;
979 break;
980 case NAME_DEFAULT:
981 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000982 break;
983 case NAME_CLOSURE:
984 op = LOAD_DEREF;
985 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000986 }
987 break;
988 case VAR_STORE:
989 switch (scope) {
990 case NAME_LOCAL:
991 op = STORE_FAST;
992 break;
993 case NAME_GLOBAL:
994 op = STORE_GLOBAL;
995 break;
996 case NAME_DEFAULT:
997 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000998 break;
999 case NAME_CLOSURE:
1000 op = STORE_DEREF;
1001 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001002 }
1003 break;
1004 case VAR_DELETE:
1005 switch (scope) {
1006 case NAME_LOCAL:
1007 op = DELETE_FAST;
1008 break;
1009 case NAME_GLOBAL:
1010 op = DELETE_GLOBAL;
1011 break;
1012 case NAME_DEFAULT:
1013 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001014 break;
1015 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00001016 char buf[500];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001017 sprintf(buf, DEL_CLOSURE_ERROR, name);
1018 com_error(c, PyExc_SyntaxError, buf);
1019 i = 255;
1020 break;
1021 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001022 }
1023 break;
1024 }
1025done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001026 com_addoparg(c, op, i);
1027}
1028
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001029static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001030com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001031{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001032 char *name;
1033 char buffer[1000];
1034 /* XXX it is possible to write this code without the 1000
1035 chars on the total length of dotted names, I just can't be
1036 bothered right now */
1037 if (TYPE(n) == STAR)
1038 name = "*";
1039 else if (TYPE(n) == dotted_name) {
1040 char *p = buffer;
1041 int i;
1042 name = buffer;
1043 for (i = 0; i < NCH(n); i += 2) {
1044 char *s = STR(CHILD(n, i));
1045 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001046 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001047 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001048 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001049 break;
1050 }
1051 if (p != buffer)
1052 *p++ = '.';
1053 strcpy(p, s);
1054 p = strchr(p, '\0');
1055 }
1056 }
1057 else {
1058 REQ(n, NAME);
1059 name = STR(n);
1060 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001061 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001062}
1063
Guido van Rossum79f25d91997-04-29 20:08:16 +00001064static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001065parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001066{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001067 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001068 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001069 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001070#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001071 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001072 int imflag;
1073#endif
1074
Guido van Rossum282914b1991-04-04 10:42:56 +00001075 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001076 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001077#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001078 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001079#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001080 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001081 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001082 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001083 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001084 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001085 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001086 if (*end == '\0') {
1087 if (errno != 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001088 com_error(co, PyExc_OverflowError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001089 "integer literal too large");
Guido van Rossum282914b1991-04-04 10:42:56 +00001090 return NULL;
1091 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001092 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001093 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001094 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001095#ifndef WITHOUT_COMPLEX
1096 if (imflag) {
1097 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001098 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001099 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001100 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001101 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001102 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001103 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001104#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001105 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001106 PyFPE_START_PROTECT("atof", return 0)
1107 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001108 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001109 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001110 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001111}
1112
Guido van Rossum79f25d91997-04-29 20:08:16 +00001113static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001114parsestr(char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001115{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001116 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001117 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001118 char *buf;
1119 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001120 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001121 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001122 int first = *s;
1123 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001124 int rawmode = 0;
1125 int unicode = 0;
1126 if (isalpha(quote) || quote == '_') {
1127 if (quote == 'u' || quote == 'U') {
1128 quote = *++s;
1129 unicode = 1;
1130 }
1131 if (quote == 'r' || quote == 'R') {
1132 quote = *++s;
1133 rawmode = 1;
1134 }
1135 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001136 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001137 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001138 return NULL;
1139 }
1140 s++;
1141 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001142 if (len > INT_MAX) {
1143 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1144 return NULL;
1145 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001146 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001147 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001148 return NULL;
1149 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001150 if (len >= 4 && s[0] == quote && s[1] == quote) {
1151 s += 2;
1152 len -= 2;
1153 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001154 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001155 return NULL;
1156 }
1157 }
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001158 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001159 if (rawmode)
1160 return PyUnicode_DecodeRawUnicodeEscape(
1161 s, len, NULL);
1162 else
1163 return PyUnicode_DecodeUnicodeEscape(
1164 s, len, NULL);
1165 }
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001166 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 return PyString_FromStringAndSize(s, len);
1168 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001169 if (v == NULL)
1170 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001171 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001172 end = s + len;
1173 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001174 if (*s != '\\') {
1175 *p++ = *s++;
1176 continue;
1177 }
1178 s++;
1179 switch (*s++) {
1180 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001181 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001182 case '\\': *p++ = '\\'; break;
1183 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001184 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001185 case 'b': *p++ = '\b'; break;
1186 case 'f': *p++ = '\014'; break; /* FF */
1187 case 't': *p++ = '\t'; break;
1188 case 'n': *p++ = '\n'; break;
1189 case 'r': *p++ = '\r'; break;
1190 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001191 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1192 case '0': case '1': case '2': case '3':
1193 case '4': case '5': case '6': case '7':
1194 c = s[-1] - '0';
1195 if ('0' <= *s && *s <= '7') {
1196 c = (c<<3) + *s++ - '0';
1197 if ('0' <= *s && *s <= '7')
1198 c = (c<<3) + *s++ - '0';
1199 }
1200 *p++ = c;
1201 break;
1202 case 'x':
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001203 if (isxdigit(Py_CHARMASK(s[0]))
1204 && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001205 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001206 c = Py_CHARMASK(*s);
1207 s++;
1208 if (isdigit(c))
1209 x = c - '0';
1210 else if (islower(c))
1211 x = 10 + c - 'a';
1212 else
1213 x = 10 + c - 'A';
1214 x = x << 4;
1215 c = Py_CHARMASK(*s);
1216 s++;
1217 if (isdigit(c))
1218 x += c - '0';
1219 else if (islower(c))
1220 x += 10 + c - 'a';
1221 else
1222 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001223 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001224 break;
1225 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001226 PyErr_SetString(PyExc_ValueError,
1227 "invalid \\x escape");
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001228 Py_DECREF(v);
1229 return NULL;
1230 default:
1231 *p++ = '\\';
1232 *p++ = s[-1];
1233 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001234 }
1235 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001236 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001237 return v;
1238}
1239
Guido van Rossum79f25d91997-04-29 20:08:16 +00001240static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001241parsestrplus(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001242{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001243 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001244 int i;
1245 REQ(CHILD(n, 0), STRING);
1246 if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
1247 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001248 for (i = 1; i < NCH(n); i++) {
1249 PyObject *s;
1250 s = parsestr(STR(CHILD(n, i)));
1251 if (s == NULL)
1252 goto onError;
1253 if (PyString_Check(v) && PyString_Check(s)) {
1254 PyString_ConcatAndDel(&v, s);
1255 if (v == NULL)
1256 goto onError;
1257 }
1258 else {
1259 PyObject *temp;
1260 temp = PyUnicode_Concat(v, s);
1261 Py_DECREF(s);
1262 if (temp == NULL)
1263 goto onError;
1264 Py_DECREF(v);
1265 v = temp;
1266 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001267 }
1268 }
1269 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001270
1271 onError:
1272 Py_XDECREF(v);
1273 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001274}
1275
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001276static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001277com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001278{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001279 int anchor = 0;
1280 int save_begin = c->c_begin;
1281
1282 /* list_iter: for v in expr [list_iter] */
1283 com_node(c, CHILD(n, 3)); /* expr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001284 com_addbyte(c, GET_ITER);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001285 c->c_begin = c->c_nexti;
1286 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001287 com_addfwref(c, FOR_ITER, &anchor);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001288 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001289 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001290 c->c_loops++;
1291 com_list_iter(c, n, e, t);
1292 c->c_loops--;
1293 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1294 c->c_begin = save_begin;
1295 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001296 com_pop(c, 1); /* FOR_ITER has popped this */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001297}
1298
1299static void
1300com_list_if(struct compiling *c, node *n, node *e, char *t)
1301{
1302 int anchor = 0;
1303 int a = 0;
1304 /* list_iter: 'if' test [list_iter] */
1305 com_addoparg(c, SET_LINENO, n->n_lineno);
1306 com_node(c, CHILD(n, 1));
1307 com_addfwref(c, JUMP_IF_FALSE, &a);
1308 com_addbyte(c, POP_TOP);
1309 com_pop(c, 1);
1310 com_list_iter(c, n, e, t);
1311 com_addfwref(c, JUMP_FORWARD, &anchor);
1312 com_backpatch(c, a);
1313 /* We jump here with an extra entry which we now pop */
1314 com_addbyte(c, POP_TOP);
1315 com_backpatch(c, anchor);
1316}
1317
1318static void
1319com_list_iter(struct compiling *c,
1320 node *p, /* parent of list_iter node */
1321 node *e, /* element expression node */
1322 char *t /* name of result list temp local */)
1323{
1324 /* list_iter is the last child in a listmaker, list_for, or list_if */
1325 node *n = CHILD(p, NCH(p)-1);
1326 if (TYPE(n) == list_iter) {
1327 n = CHILD(n, 0);
1328 switch (TYPE(n)) {
1329 case list_for:
1330 com_list_for(c, n, e, t);
1331 break;
1332 case list_if:
1333 com_list_if(c, n, e, t);
1334 break;
1335 default:
1336 com_error(c, PyExc_SystemError,
1337 "invalid list_iter node type");
1338 }
1339 }
1340 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001341 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001342 com_push(c, 1);
1343 com_node(c, e);
1344 com_addoparg(c, CALL_FUNCTION, 1);
1345 com_addbyte(c, POP_TOP);
1346 com_pop(c, 2);
1347 }
1348}
1349
1350static void
1351com_list_comprehension(struct compiling *c, node *n)
1352{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001353 /* listmaker: test list_for */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001354 char tmpname[12];
Tim Peters30814212001-02-17 05:30:26 +00001355 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001356 com_addoparg(c, BUILD_LIST, 0);
1357 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1358 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001359 com_addop_name(c, LOAD_ATTR, "append");
1360 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001361 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001362 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001363 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001364 --c->c_tmpname;
1365}
1366
1367static void
1368com_listmaker(struct compiling *c, node *n)
1369{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001370 /* listmaker: test ( list_for | (',' test)* [','] ) */
1371 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001372 com_list_comprehension(c, n);
1373 else {
1374 int len = 0;
1375 int i;
1376 for (i = 0; i < NCH(n); i += 2, len++)
1377 com_node(c, CHILD(n, i));
1378 com_addoparg(c, BUILD_LIST, len);
1379 com_pop(c, len-1);
1380 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001381}
1382
1383static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001384com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001385{
1386 int i;
1387 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1388 for (i = 0; i+2 < NCH(n); i += 4) {
1389 /* We must arrange things just right for STORE_SUBSCR.
1390 It wants the stack to look like (value) (dict) (key) */
1391 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001392 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001393 com_node(c, CHILD(n, i+2)); /* value */
1394 com_addbyte(c, ROT_TWO);
1395 com_node(c, CHILD(n, i)); /* key */
1396 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001397 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001398 }
1399}
1400
1401static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001402com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001403{
1404 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001405 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001406 int i;
1407 REQ(n, atom);
1408 ch = CHILD(n, 0);
1409 switch (TYPE(ch)) {
1410 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001411 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001412 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001413 com_push(c, 1);
1414 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001415 else
1416 com_node(c, CHILD(n, 1));
1417 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001418 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001419 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001420 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001421 com_push(c, 1);
1422 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001423 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001424 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001425 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001426 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001427 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001428 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001429 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001430 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001431 break;
1432 case BACKQUOTE:
1433 com_node(c, CHILD(n, 1));
1434 com_addbyte(c, UNARY_CONVERT);
1435 break;
1436 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001437 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001438 i = 255;
1439 }
1440 else {
1441 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001442 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001443 }
1444 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001445 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001446 break;
1447 case STRING:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001448 v = parsestrplus(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001449 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001450 c->c_errors++;
1451 i = 255;
1452 }
1453 else {
1454 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001455 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001456 }
1457 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001458 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001459 break;
1460 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001461 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001462 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001463 break;
1464 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001465 com_error(c, PyExc_SystemError,
1466 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001467 }
1468}
1469
1470static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001471com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001472{
1473 if (NCH(n) == 1) {
1474 com_addbyte(c, op);
1475 }
1476 else if (NCH(n) == 2) {
1477 if (TYPE(CHILD(n, 0)) != COLON) {
1478 com_node(c, CHILD(n, 0));
1479 com_addbyte(c, op+1);
1480 }
1481 else {
1482 com_node(c, CHILD(n, 1));
1483 com_addbyte(c, op+2);
1484 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001485 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001486 }
1487 else {
1488 com_node(c, CHILD(n, 0));
1489 com_node(c, CHILD(n, 2));
1490 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001491 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001492 }
1493}
1494
Guido van Rossum635abd21997-01-06 22:56:52 +00001495static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001496com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1497{
1498 if (NCH(n) == 1) {
1499 com_addbyte(c, DUP_TOP);
1500 com_push(c, 1);
1501 com_addbyte(c, SLICE);
1502 com_node(c, augn);
1503 com_addbyte(c, opcode);
1504 com_pop(c, 1);
1505 com_addbyte(c, ROT_TWO);
1506 com_addbyte(c, STORE_SLICE);
1507 com_pop(c, 2);
1508 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1509 com_node(c, CHILD(n, 0));
1510 com_addoparg(c, DUP_TOPX, 2);
1511 com_push(c, 2);
1512 com_addbyte(c, SLICE+1);
1513 com_pop(c, 1);
1514 com_node(c, augn);
1515 com_addbyte(c, opcode);
1516 com_pop(c, 1);
1517 com_addbyte(c, ROT_THREE);
1518 com_addbyte(c, STORE_SLICE+1);
1519 com_pop(c, 3);
1520 } else if (NCH(n) == 2) {
1521 com_node(c, CHILD(n, 1));
1522 com_addoparg(c, DUP_TOPX, 2);
1523 com_push(c, 2);
1524 com_addbyte(c, SLICE+2);
1525 com_pop(c, 1);
1526 com_node(c, augn);
1527 com_addbyte(c, opcode);
1528 com_pop(c, 1);
1529 com_addbyte(c, ROT_THREE);
1530 com_addbyte(c, STORE_SLICE+2);
1531 com_pop(c, 3);
1532 } else {
1533 com_node(c, CHILD(n, 0));
1534 com_node(c, CHILD(n, 2));
1535 com_addoparg(c, DUP_TOPX, 3);
1536 com_push(c, 3);
1537 com_addbyte(c, SLICE+3);
1538 com_pop(c, 2);
1539 com_node(c, augn);
1540 com_addbyte(c, opcode);
1541 com_pop(c, 1);
1542 com_addbyte(c, ROT_FOUR);
1543 com_addbyte(c, STORE_SLICE+3);
1544 com_pop(c, 4);
1545 }
1546}
1547
1548static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001549com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001550{
1551 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001552 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001553 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001554 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001555 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001556 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001557 }
1558 else {
1559 com_node(c, CHILD(n, 0));
1560 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001561 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001562 }
1563 m = n;
1564 do {
1565 m = CHILD(m, 0);
1566 } while (NCH(m) == 1);
1567 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001568 /* f(lambda x: x[0] = 3) ends up getting parsed with
1569 * LHS test = lambda x: x[0], and RHS test = 3.
1570 * SF bug 132313 points out that complaining about a keyword
1571 * then is very confusing.
1572 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001573 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001574 TYPE(m) == lambdef ?
1575 "lambda cannot contain assignment" :
1576 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001577 }
1578 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001579 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001580 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001581 *pkeywords = PyDict_New();
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001582 if (v == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001583 c->c_errors++;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001584 else if (*pkeywords == NULL) {
1585 c->c_errors++;
1586 Py_DECREF(v);
1587 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001588 if (PyDict_GetItem(*pkeywords, v) != NULL)
1589 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001590 "duplicate keyword argument");
1591 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001592 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001593 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001594 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001595 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001597 }
1598 }
1599 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001600}
1601
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001602static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001603com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001604{
1605 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001606 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001607 }
1608 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001609 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001610 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001611 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001612 int star_flag = 0;
1613 int starstar_flag = 0;
1614 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001615 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001616 na = 0;
1617 nk = 0;
1618 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001619 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001620 if (TYPE(ch) == STAR ||
1621 TYPE(ch) == DOUBLESTAR)
1622 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001623 if (ch->n_lineno != lineno) {
1624 lineno = ch->n_lineno;
1625 com_addoparg(c, SET_LINENO, lineno);
1626 }
1627 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001628 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001629 na++;
1630 else
1631 nk++;
1632 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001633 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001634 while (i < NCH(n)) {
1635 node *tok = CHILD(n, i);
1636 node *ch = CHILD(n, i+1);
1637 i += 3;
1638 switch (TYPE(tok)) {
1639 case STAR: star_flag = 1; break;
1640 case DOUBLESTAR: starstar_flag = 1; break;
1641 }
1642 com_node(c, ch);
1643 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001644 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001645 com_error(c, PyExc_SyntaxError,
1646 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001647 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001648 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001649 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001650 star_flag + (starstar_flag << 1);
1651 else
1652 opcode = CALL_FUNCTION;
1653 com_addoparg(c, opcode, na | (nk << 8));
1654 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001655 }
1656}
1657
1658static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001659com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001660{
1661 com_addopname(c, LOAD_ATTR, n);
1662}
1663
1664static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001665com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001666{
1667 int i=0;
1668 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001669 node *ch;
1670
1671 /* first argument */
1672 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001673 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001674 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001675 i++;
1676 }
1677 else {
1678 com_node(c, CHILD(n,i));
1679 i++;
1680 REQ(CHILD(n,i),COLON);
1681 i++;
1682 }
1683 /* second argument */
1684 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1685 com_node(c, CHILD(n,i));
1686 i++;
1687 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001688 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001689 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001690 com_push(c, 1);
1691 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001692 /* remaining arguments */
1693 for (; i < NCH(n); i++) {
1694 ns++;
1695 ch=CHILD(n,i);
1696 REQ(ch, sliceop);
1697 if (NCH(ch) == 1) {
1698 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001699 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001700 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001701 }
1702 else
1703 com_node(c, CHILD(ch,1));
1704 }
1705 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001706 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001707}
1708
1709static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001710com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001711{
1712 node *ch;
1713 REQ(n, subscript);
1714 ch = CHILD(n,0);
1715 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001716 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001717 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001718 com_push(c, 1);
1719 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001720 else {
1721 /* check for slice */
1722 if ((TYPE(ch) == COLON || NCH(n) > 1))
1723 com_sliceobj(c, n);
1724 else {
1725 REQ(ch, test);
1726 com_node(c, ch);
1727 }
1728 }
1729}
1730
1731static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001732com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001733{
1734 int i, op;
1735 REQ(n, subscriptlist);
1736 /* Check to make backward compatible slice behavior for '[i:j]' */
1737 if (NCH(n) == 1) {
1738 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001739 /* 'Basic' slice, should have exactly one colon. */
1740 if ((TYPE(CHILD(sub, 0)) == COLON
1741 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1742 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1743 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001744 switch (assigning) {
1745 case OP_DELETE:
1746 op = DELETE_SLICE;
1747 break;
1748 case OP_ASSIGN:
1749 op = STORE_SLICE;
1750 break;
1751 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001752 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001753 break;
1754 default:
1755 com_augassign_slice(c, sub, assigning, augn);
1756 return;
1757 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001758 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001759 if (op == STORE_SLICE)
1760 com_pop(c, 2);
1761 else if (op == DELETE_SLICE)
1762 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001763 return;
1764 }
1765 }
1766 /* Else normal subscriptlist. Compile each subscript. */
1767 for (i = 0; i < NCH(n); i += 2)
1768 com_subscript(c, CHILD(n, i));
1769 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001770 if (NCH(n) > 1) {
1771 i = (NCH(n)+1) / 2;
1772 com_addoparg(c, BUILD_TUPLE, i);
1773 com_pop(c, i-1);
1774 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001775 switch (assigning) {
1776 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001777 op = DELETE_SUBSCR;
1778 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001779 break;
1780 default:
1781 case OP_ASSIGN:
1782 op = STORE_SUBSCR;
1783 i = 3;
1784 break;
1785 case OP_APPLY:
1786 op = BINARY_SUBSCR;
1787 i = 1;
1788 break;
1789 }
1790 if (assigning > OP_APPLY) {
1791 com_addoparg(c, DUP_TOPX, 2);
1792 com_push(c, 2);
1793 com_addbyte(c, BINARY_SUBSCR);
1794 com_pop(c, 1);
1795 com_node(c, augn);
1796 com_addbyte(c, assigning);
1797 com_pop(c, 1);
1798 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001799 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001800 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001801 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001802}
1803
1804static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001805com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001806{
1807 REQ(n, trailer);
1808 switch (TYPE(CHILD(n, 0))) {
1809 case LPAR:
1810 com_call_function(c, CHILD(n, 1));
1811 break;
1812 case DOT:
1813 com_select_member(c, CHILD(n, 1));
1814 break;
1815 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001816 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001817 break;
1818 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001819 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001820 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001821 }
1822}
1823
1824static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001825com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001826{
1827 int i;
1828 REQ(n, power);
1829 com_atom(c, CHILD(n, 0));
1830 for (i = 1; i < NCH(n); i++) {
1831 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1832 com_factor(c, CHILD(n, i+1));
1833 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001834 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001835 break;
1836 }
1837 else
1838 com_apply_trailer(c, CHILD(n, i));
1839 }
1840}
1841
1842static void
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001843com_invert_constant(struct compiling *c, node *n)
1844{
1845 /* Compute the inverse of int and longs and use them directly,
1846 but be prepared to generate code for all other
1847 possibilities (invalid numbers, floats, complex).
1848 */
1849 PyObject *num, *inv = NULL;
1850 int i;
1851
1852 REQ(n, NUMBER);
1853 num = parsenumber(c, STR(n));
1854 if (num == NULL)
1855 i = 255;
1856 else {
1857 inv = PyNumber_Invert(num);
1858 if (inv == NULL) {
1859 PyErr_Clear();
1860 i = com_addconst(c, num);
1861 } else {
1862 i = com_addconst(c, inv);
1863 Py_DECREF(inv);
1864 }
1865 Py_DECREF(num);
1866 }
1867 com_addoparg(c, LOAD_CONST, i);
1868 com_push(c, 1);
1869 if (num != NULL && inv == NULL)
1870 com_addbyte(c, UNARY_INVERT);
1871}
1872
1873static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001874com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001875{
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001876 int childtype = TYPE(CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001877 REQ(n, factor);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001878 /* If the unary +, -, or ~ operator is applied to a constant,
1879 don't generate a UNARY_xxx opcode. Just store the
1880 approriate value as a constant. If the value is negative,
1881 extend the string containing the constant and insert a
1882 negative in the 0th position.
1883 */
1884 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
1885 && TYPE(CHILD(n, 1)) == factor
1886 && TYPE(CHILD(CHILD(n, 1), 0)) == power
1887 && TYPE(CHILD(CHILD(CHILD(n, 1), 0), 0)) == atom
1888 && TYPE(CHILD(CHILD(CHILD(CHILD(n, 1), 0), 0), 0)) == NUMBER) {
1889 node *constant = CHILD(CHILD(CHILD(n, 1), 0), 0);
1890 if (childtype == TILDE) {
1891 com_invert_constant(c, CHILD(constant, 0));
1892 return;
1893 }
1894 if (childtype == MINUS) {
1895 node *numnode = CHILD(constant, 0);
1896 char *s = malloc(strlen(STR(numnode)) + 2);
1897 if (s == NULL) {
1898 com_error(c, PyExc_MemoryError, "");
1899 com_addbyte(c, 255);
1900 return;
1901 }
1902 s[0] = '-';
1903 strcpy(s + 1, STR(numnode));
1904 free(STR(numnode));
1905 STR(numnode) = s;
1906 }
1907 com_atom(c, constant);
1908 }
1909 else if (childtype == PLUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001910 com_factor(c, CHILD(n, 1));
1911 com_addbyte(c, UNARY_POSITIVE);
1912 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001913 else if (childtype == MINUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001914 com_factor(c, CHILD(n, 1));
1915 com_addbyte(c, UNARY_NEGATIVE);
1916 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001917 else if (childtype == TILDE) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001918 com_factor(c, CHILD(n, 1));
1919 com_addbyte(c, UNARY_INVERT);
1920 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001921 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001922 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001923 }
1924}
1925
1926static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001927com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001928{
1929 int i;
1930 int op;
1931 REQ(n, term);
1932 com_factor(c, CHILD(n, 0));
1933 for (i = 2; i < NCH(n); i += 2) {
1934 com_factor(c, CHILD(n, i));
1935 switch (TYPE(CHILD(n, i-1))) {
1936 case STAR:
1937 op = BINARY_MULTIPLY;
1938 break;
1939 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00001940 if (c->c_flags & CO_FUTURE_DIVISION)
1941 op = BINARY_TRUE_DIVIDE;
1942 else
1943 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001944 break;
1945 case PERCENT:
1946 op = BINARY_MODULO;
1947 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001948 case DOUBLESLASH:
1949 op = BINARY_FLOOR_DIVIDE;
1950 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001951 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001952 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00001953 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001954 op = 255;
1955 }
1956 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001957 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001958 }
1959}
1960
1961static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001962com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001963{
1964 int i;
1965 int op;
1966 REQ(n, arith_expr);
1967 com_term(c, CHILD(n, 0));
1968 for (i = 2; i < NCH(n); i += 2) {
1969 com_term(c, CHILD(n, i));
1970 switch (TYPE(CHILD(n, i-1))) {
1971 case PLUS:
1972 op = BINARY_ADD;
1973 break;
1974 case MINUS:
1975 op = BINARY_SUBTRACT;
1976 break;
1977 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001978 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001979 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001980 op = 255;
1981 }
1982 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001983 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001984 }
1985}
1986
1987static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001988com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001989{
1990 int i;
1991 int op;
1992 REQ(n, shift_expr);
1993 com_arith_expr(c, CHILD(n, 0));
1994 for (i = 2; i < NCH(n); i += 2) {
1995 com_arith_expr(c, CHILD(n, i));
1996 switch (TYPE(CHILD(n, i-1))) {
1997 case LEFTSHIFT:
1998 op = BINARY_LSHIFT;
1999 break;
2000 case RIGHTSHIFT:
2001 op = BINARY_RSHIFT;
2002 break;
2003 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002004 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002005 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002006 op = 255;
2007 }
2008 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002009 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002010 }
2011}
2012
2013static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002014com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002015{
2016 int i;
2017 int op;
2018 REQ(n, and_expr);
2019 com_shift_expr(c, CHILD(n, 0));
2020 for (i = 2; i < NCH(n); i += 2) {
2021 com_shift_expr(c, CHILD(n, i));
2022 if (TYPE(CHILD(n, i-1)) == AMPER) {
2023 op = BINARY_AND;
2024 }
2025 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002026 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002027 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002028 op = 255;
2029 }
2030 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002031 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002032 }
2033}
2034
2035static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002036com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002037{
2038 int i;
2039 int op;
2040 REQ(n, xor_expr);
2041 com_and_expr(c, CHILD(n, 0));
2042 for (i = 2; i < NCH(n); i += 2) {
2043 com_and_expr(c, CHILD(n, i));
2044 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2045 op = BINARY_XOR;
2046 }
2047 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002048 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002049 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002050 op = 255;
2051 }
2052 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002053 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002054 }
2055}
2056
2057static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002058com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002059{
2060 int i;
2061 int op;
2062 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002063 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002064 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002065 com_xor_expr(c, CHILD(n, i));
2066 if (TYPE(CHILD(n, i-1)) == VBAR) {
2067 op = BINARY_OR;
2068 }
2069 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002070 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002071 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002072 op = 255;
2073 }
2074 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002075 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002076 }
2077}
2078
2079static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002080cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002081{
2082 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002083 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002084 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2085 if (NCH(n) == 1) {
2086 n = CHILD(n, 0);
2087 switch (TYPE(n)) {
2088 case LESS: return LT;
2089 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002090 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002091 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002092 case LESSEQUAL: return LE;
2093 case GREATEREQUAL: return GE;
2094 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002095 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2096 if (strcmp(STR(n), "is") == 0) return IS;
2097 }
2098 }
2099 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002100 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002101 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2102 return NOT_IN;
2103 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2104 return IS_NOT;
2105 }
2106 }
2107 return BAD;
2108}
2109
2110static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002111com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002112{
2113 int i;
2114 enum cmp_op op;
2115 int anchor;
2116 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2117 com_expr(c, CHILD(n, 0));
2118 if (NCH(n) == 1)
2119 return;
2120
2121 /****************************************************************
2122 The following code is generated for all but the last
2123 comparison in a chain:
2124
2125 label: on stack: opcode: jump to:
2126
2127 a <code to load b>
2128 a, b DUP_TOP
2129 a, b, b ROT_THREE
2130 b, a, b COMPARE_OP
2131 b, 0-or-1 JUMP_IF_FALSE L1
2132 b, 1 POP_TOP
2133 b
2134
2135 We are now ready to repeat this sequence for the next
2136 comparison in the chain.
2137
2138 For the last we generate:
2139
2140 b <code to load c>
2141 b, c COMPARE_OP
2142 0-or-1
2143
2144 If there were any jumps to L1 (i.e., there was more than one
2145 comparison), we generate:
2146
2147 0-or-1 JUMP_FORWARD L2
2148 L1: b, 0 ROT_TWO
2149 0, b POP_TOP
2150 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002151 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002152 ****************************************************************/
2153
2154 anchor = 0;
2155
2156 for (i = 2; i < NCH(n); i += 2) {
2157 com_expr(c, CHILD(n, i));
2158 if (i+2 < NCH(n)) {
2159 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002160 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002161 com_addbyte(c, ROT_THREE);
2162 }
2163 op = cmp_type(CHILD(n, i-1));
2164 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002165 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002166 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002167 }
2168 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002169 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002170 if (i+2 < NCH(n)) {
2171 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2172 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002173 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002174 }
2175 }
2176
2177 if (anchor) {
2178 int anchor2 = 0;
2179 com_addfwref(c, JUMP_FORWARD, &anchor2);
2180 com_backpatch(c, anchor);
2181 com_addbyte(c, ROT_TWO);
2182 com_addbyte(c, POP_TOP);
2183 com_backpatch(c, anchor2);
2184 }
2185}
2186
2187static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002188com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002189{
2190 REQ(n, not_test); /* 'not' not_test | comparison */
2191 if (NCH(n) == 1) {
2192 com_comparison(c, CHILD(n, 0));
2193 }
2194 else {
2195 com_not_test(c, CHILD(n, 1));
2196 com_addbyte(c, UNARY_NOT);
2197 }
2198}
2199
2200static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002201com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002202{
2203 int i;
2204 int anchor;
2205 REQ(n, and_test); /* not_test ('and' not_test)* */
2206 anchor = 0;
2207 i = 0;
2208 for (;;) {
2209 com_not_test(c, CHILD(n, i));
2210 if ((i += 2) >= NCH(n))
2211 break;
2212 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2213 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002214 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002215 }
2216 if (anchor)
2217 com_backpatch(c, anchor);
2218}
2219
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002220static int
2221com_make_closure(struct compiling *c, PyCodeObject *co)
2222{
2223 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2224 if (free == 0)
2225 return 0;
2226 for (i = 0; i < free; ++i) {
2227 /* Bypass com_addop_varname because it will generate
2228 LOAD_DEREF but LOAD_CLOSURE is needed.
2229 */
2230 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2231 int arg, reftype;
2232
2233 /* Special case: If a class contains a method with a
2234 free variable that has the same name as a method,
2235 the name will be considered free *and* local in the
2236 class. It should be handled by the closure, as
2237 well as by the normal name loookup logic.
2238 */
2239 reftype = get_ref_type(c, PyString_AS_STRING(name));
2240 if (reftype == CELL)
2241 arg = com_lookup_arg(c->c_cellvars, name);
2242 else /* (reftype == FREE) */
2243 arg = com_lookup_arg(c->c_freevars, name);
2244 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002245 fprintf(stderr, "lookup %s in %s %d %d\n"
2246 "freevars of %s: %s\n",
2247 PyObject_REPR(name),
2248 c->c_name,
2249 reftype, arg,
2250 PyString_AS_STRING(co->co_name),
2251 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002252 Py_FatalError("com_make_closure()");
2253 }
2254 com_addoparg(c, LOAD_CLOSURE, arg);
2255
2256 }
2257 com_push(c, free);
2258 return 1;
2259}
2260
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002261static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002262com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002263{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002264 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002265 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002266 PyObject *co;
2267 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002268 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002269 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2270 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002271 co = (PyObject *) icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002272 if (co == NULL) {
2273 c->c_errors++;
2274 return;
2275 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002276 symtable_exit_scope(c->c_symtable);
Tim Peters1e542112001-02-23 22:23:53 +00002277 i = com_addconst(c, co);
2278 closure = com_make_closure(c, (PyCodeObject *)co);
2279 Py_DECREF(co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002280 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002281 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002282 if (closure)
2283 com_addoparg(c, MAKE_CLOSURE, ndefs);
2284 else
2285 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002286 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002287 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002288 else {
2289 int anchor = 0;
2290 int i = 0;
2291 for (;;) {
2292 com_and_test(c, CHILD(n, i));
2293 if ((i += 2) >= NCH(n))
2294 break;
2295 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2296 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002297 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002298 }
2299 if (anchor)
2300 com_backpatch(c, anchor);
2301 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002302}
2303
2304static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002305com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002306{
2307 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002308 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002309 com_node(c, CHILD(n, 0));
2310 }
2311 else {
2312 int i;
2313 int len;
2314 len = (NCH(n) + 1) / 2;
2315 for (i = 0; i < NCH(n); i += 2)
2316 com_node(c, CHILD(n, i));
2317 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002318 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002319 }
2320}
2321
2322
2323/* Begin of assignment compilation */
2324
Thomas Wouters434d0822000-08-24 20:11:32 +00002325
2326static void
2327com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2328{
2329 com_addbyte(c, DUP_TOP);
2330 com_push(c, 1);
2331 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002332 com_node(c, augn);
2333 com_addbyte(c, opcode);
2334 com_pop(c, 1);
2335 com_addbyte(c, ROT_TWO);
2336 com_addopname(c, STORE_ATTR, n);
2337 com_pop(c, 2);
2338}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002339
2340static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002341com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002342{
2343 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002344 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002345}
2346
2347static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002348com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002349{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002350 REQ(n, trailer);
2351 switch (TYPE(CHILD(n, 0))) {
2352 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002353 com_error(c, PyExc_SyntaxError,
2354 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002355 break;
2356 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002357 if (assigning > OP_APPLY)
2358 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2359 else
2360 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002361 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002362 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002363 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002364 break;
2365 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002366 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002367 }
2368}
2369
2370static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002371com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002372{
2373 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002374 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002375 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002376 if (assigning) {
2377 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002378 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002379 com_push(c, i-1);
2380 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002381 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002382 com_assign(c, CHILD(n, i), assigning, NULL);
2383}
2384
2385static void
2386com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2387{
2388 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002389 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002390 com_push(c, 1);
2391 com_node(c, augn);
2392 com_addbyte(c, opcode);
2393 com_pop(c, 1);
2394 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002395}
2396
2397static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002398com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002399{
2400 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002401 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002402 if (assigning)
2403 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002404}
2405
2406static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002407com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002408{
2409 /* Loop to avoid trivial recursion */
2410 for (;;) {
2411 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002412
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002413 case exprlist:
2414 case testlist:
2415 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002416 if (assigning > OP_APPLY) {
2417 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002418 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002419 return;
2420 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002421 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002422 return;
2423 }
2424 n = CHILD(n, 0);
2425 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002426
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002427 case test:
2428 case and_test:
2429 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002430 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002431 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002432 case xor_expr:
2433 case and_expr:
2434 case shift_expr:
2435 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002436 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002437 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002438 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002439 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002440 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002441 return;
2442 }
2443 n = CHILD(n, 0);
2444 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002445
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002446 case power: /* atom trailer* ('**' power)*
2447 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002448 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002449 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002450 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002451 return;
2452 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002453 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002454 int i;
2455 com_node(c, CHILD(n, 0));
2456 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002457 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002458 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002459 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002460 return;
2461 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002462 com_apply_trailer(c, CHILD(n, i));
2463 } /* NB i is still alive */
2464 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002465 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002466 return;
2467 }
2468 n = CHILD(n, 0);
2469 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002470
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002471 case atom:
2472 switch (TYPE(CHILD(n, 0))) {
2473 case LPAR:
2474 n = CHILD(n, 1);
2475 if (TYPE(n) == RPAR) {
2476 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002477 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002478 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002479 return;
2480 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002481 if (assigning > OP_APPLY) {
2482 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002483 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002484 return;
2485 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002486 break;
2487 case LSQB:
2488 n = CHILD(n, 1);
2489 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002490 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002491 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002492 return;
2493 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002494 if (assigning > OP_APPLY) {
2495 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002496 "augmented assign to list not possible");
2497 return;
2498 }
2499 if (NCH(n) > 1
2500 && TYPE(CHILD(n, 1)) == list_for) {
2501 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002502 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002503 return;
2504 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002505 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002506 return;
2507 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002508 if (assigning > OP_APPLY)
2509 com_augassign_name(c, CHILD(n, 0),
2510 assigning, augn);
2511 else
2512 com_assign_name(c, CHILD(n, 0),
2513 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002514 return;
2515 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002516 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002517 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002518 return;
2519 }
2520 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002521
2522 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002523 com_error(c, PyExc_SyntaxError,
2524 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002525 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002526
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002527 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002528 com_error(c, PyExc_SystemError,
2529 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002530 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002531
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002532 }
2533 }
2534}
Guido van Rossum7c531111997-03-11 18:42:21 +00002535
Thomas Wouters434d0822000-08-24 20:11:32 +00002536static void
2537com_augassign(struct compiling *c, node *n)
2538{
2539 int opcode;
2540
2541 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2542 case '+': opcode = INPLACE_ADD; break;
2543 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002544 case '/':
2545 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2546 opcode = INPLACE_FLOOR_DIVIDE;
2547 else if (c->c_flags & CO_FUTURE_DIVISION)
2548 opcode = INPLACE_TRUE_DIVIDE;
2549 else
2550 opcode = INPLACE_DIVIDE;
2551 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002552 case '%': opcode = INPLACE_MODULO; break;
2553 case '<': opcode = INPLACE_LSHIFT; break;
2554 case '>': opcode = INPLACE_RSHIFT; break;
2555 case '&': opcode = INPLACE_AND; break;
2556 case '^': opcode = INPLACE_XOR; break;
2557 case '|': opcode = INPLACE_OR; break;
2558 case '*':
2559 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2560 opcode = INPLACE_POWER;
2561 else
2562 opcode = INPLACE_MULTIPLY;
2563 break;
2564 default:
2565 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2566 return;
2567 }
2568 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2569}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002570
2571static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002572com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002573{
Thomas Wouters434d0822000-08-24 20:11:32 +00002574 REQ(n, expr_stmt);
2575 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002576 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002577 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002578 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002579 if (NCH(n) == 1) {
2580 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002581 if (c->c_interactive)
2582 com_addbyte(c, PRINT_EXPR);
2583 else
2584 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002585 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002586 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002587 else if (TYPE(CHILD(n,1)) == augassign)
2588 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002589 else {
2590 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002591 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002592 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002593 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002594 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002595 com_push(c, 1);
2596 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002597 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002598 }
2599 }
2600}
2601
2602static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002603com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002604{
2605 int a = 0, b = 0;
2606 int i;
2607 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2608 /* Generate code like for
2609
2610 if __debug__:
2611 if not <test>:
2612 raise AssertionError [, <message>]
2613
2614 where <message> is the second test, if present.
2615 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002616
Guido van Rossum228d7f31997-04-02 05:24:36 +00002617 if (Py_OptimizeFlag)
2618 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002619 com_addop_name(c, LOAD_GLOBAL, "__debug__");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002620 com_push(c, 1);
2621 com_addfwref(c, JUMP_IF_FALSE, &a);
2622 com_addbyte(c, POP_TOP);
2623 com_pop(c, 1);
2624 com_node(c, CHILD(n, 1));
2625 com_addfwref(c, JUMP_IF_TRUE, &b);
2626 com_addbyte(c, POP_TOP);
2627 com_pop(c, 1);
2628 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002629 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002630 com_push(c, 1);
2631 i = NCH(n)/2; /* Either 2 or 4 */
2632 if (i > 1)
2633 com_node(c, CHILD(n, 3));
2634 com_addoparg(c, RAISE_VARARGS, i);
2635 com_pop(c, i);
2636 /* The interpreter does not fall through */
2637 /* All jumps converge here */
2638 com_backpatch(c, a);
2639 com_backpatch(c, b);
2640 com_addbyte(c, POP_TOP);
2641}
2642
2643static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002644com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002645{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002646 int i = 1;
2647 node* stream = NULL;
2648
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002649 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002650
2651 /* are we using the extended print form? */
2652 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2653 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002654 com_node(c, stream);
2655 /* stack: [...] => [... stream] */
2656 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002657 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2658 i = 4;
2659 else
2660 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002661 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002662 for (; i < NCH(n); i += 2) {
2663 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002664 com_addbyte(c, DUP_TOP);
2665 /* stack: [stream] => [stream stream] */
2666 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002667 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002668 /* stack: [stream stream] => [stream stream obj] */
2669 com_addbyte(c, ROT_TWO);
2670 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002671 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002672 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002673 com_pop(c, 2);
2674 }
2675 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002676 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002677 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002678 com_addbyte(c, PRINT_ITEM);
2679 com_pop(c, 1);
2680 }
2681 }
2682 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002683 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002684 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002685 /* must pop the extra stream object off the stack */
2686 com_addbyte(c, POP_TOP);
2687 /* stack: [... stream] => [...] */
2688 com_pop(c, 1);
2689 }
2690 }
2691 else {
2692 if (stream != NULL) {
2693 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002694 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002695 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002696 com_pop(c, 1);
2697 }
2698 else
2699 com_addbyte(c, PRINT_NEWLINE);
2700 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002701}
2702
2703static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002704com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002705{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002706 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002707 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002708 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002709 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002710 if (c->c_flags & CO_GENERATOR) {
2711 if (NCH(n) > 1) {
2712 com_error(c, PyExc_SyntaxError,
2713 "'return' with argument inside generator");
2714 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002715 }
2716 if (NCH(n) < 2) {
2717 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002718 com_push(c, 1);
2719 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002720 else
2721 com_node(c, CHILD(n, 1));
2722 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002723 com_pop(c, 1);
2724}
2725
2726static void
2727com_yield_stmt(struct compiling *c, node *n)
2728{
Tim Peters95c80f82001-06-23 02:07:08 +00002729 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002730 REQ(n, yield_stmt); /* 'yield' testlist */
2731 if (!c->c_infunction) {
2732 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2733 }
Tim Peters95c80f82001-06-23 02:07:08 +00002734
2735 for (i = 0; i < c->c_nblocks; ++i) {
2736 if (c->c_block[i] == SETUP_FINALLY) {
2737 com_error(c, PyExc_SyntaxError,
2738 "'yield' not allowed in a 'try' block "
2739 "with a 'finally' clause");
2740 return;
2741 }
2742 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002743 com_node(c, CHILD(n, 1));
2744 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002745 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002746}
2747
2748static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002749com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002750{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002751 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002752 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2753 if (NCH(n) > 1) {
2754 com_node(c, CHILD(n, 1));
2755 if (NCH(n) > 3) {
2756 com_node(c, CHILD(n, 3));
2757 if (NCH(n) > 5)
2758 com_node(c, CHILD(n, 5));
2759 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002760 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002761 i = NCH(n)/2;
2762 com_addoparg(c, RAISE_VARARGS, i);
2763 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002764}
2765
2766static void
Thomas Wouters52152252000-08-17 22:55:00 +00002767com_from_import(struct compiling *c, node *n)
2768{
2769 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2770 com_push(c, 1);
2771 if (NCH(n) > 1) {
2772 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2773 com_error(c, PyExc_SyntaxError, "invalid syntax");
2774 return;
2775 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002776 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002777 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002778 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002779 com_pop(c, 1);
2780}
2781
2782static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002783com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002784{
2785 int i;
2786 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002787 /* 'import' dotted_name (',' dotted_name)* |
2788 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002789 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002790 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002791 /* 'from' dotted_name 'import' ... */
2792 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002793
2794 if (TYPE(CHILD(n, 3)) == STAR) {
2795 tup = Py_BuildValue("(s)", "*");
2796 } else {
2797 tup = PyTuple_New((NCH(n) - 2)/2);
2798 for (i = 3; i < NCH(n); i += 2) {
2799 PyTuple_SET_ITEM(tup, (i-3)/2,
2800 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00002801 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002802 }
2803 }
2804 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002805 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002806 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002807 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002808 if (TYPE(CHILD(n, 3)) == STAR)
2809 com_addbyte(c, IMPORT_STAR);
2810 else {
2811 for (i = 3; i < NCH(n); i += 2)
2812 com_from_import(c, CHILD(n, i));
2813 com_addbyte(c, POP_TOP);
2814 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002815 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002816 }
2817 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002818 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002819 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002820 node *subn = CHILD(n, i);
2821 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002822 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002823 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002824 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002825 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002826 int j;
2827 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002828 com_error(c, PyExc_SyntaxError,
2829 "invalid syntax");
2830 return;
2831 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002832 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2833 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002834 CHILD(CHILD(subn, 0),
2835 j));
2836 com_addop_varname(c, VAR_STORE,
2837 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002838 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002839 com_addop_varname(c, VAR_STORE,
2840 STR(CHILD(CHILD(subn, 0),
2841 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002842 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002843 }
2844 }
2845}
2846
2847static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002848com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002849{
2850 REQ(n, exec_stmt);
2851 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2852 com_node(c, CHILD(n, 1));
2853 if (NCH(n) >= 4)
2854 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002855 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002856 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002857 com_push(c, 1);
2858 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002859 if (NCH(n) >= 6)
2860 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002861 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002862 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002863 com_push(c, 1);
2864 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002865 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002866 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002867}
2868
Guido van Rossum7c531111997-03-11 18:42:21 +00002869static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002870is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002871{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002872 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002873 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002874 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00002875
2876 /* Label to avoid tail recursion */
2877 next:
2878 switch (TYPE(n)) {
2879
2880 case suite:
2881 if (NCH(n) == 1) {
2882 n = CHILD(n, 0);
2883 goto next;
2884 }
2885 /* Fall through */
2886 case file_input:
2887 for (i = 0; i < NCH(n); i++) {
2888 node *ch = CHILD(n, i);
2889 if (TYPE(ch) == stmt) {
2890 n = ch;
2891 goto next;
2892 }
2893 }
2894 break;
2895
2896 case stmt:
2897 case simple_stmt:
2898 case small_stmt:
2899 n = CHILD(n, 0);
2900 goto next;
2901
2902 case expr_stmt:
2903 case testlist:
2904 case test:
2905 case and_test:
2906 case not_test:
2907 case comparison:
2908 case expr:
2909 case xor_expr:
2910 case and_expr:
2911 case shift_expr:
2912 case arith_expr:
2913 case term:
2914 case factor:
2915 case power:
2916 case atom:
2917 if (NCH(n) == 1) {
2918 n = CHILD(n, 0);
2919 goto next;
2920 }
2921 break;
2922
2923 case NAME:
2924 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2925 return 1;
2926 break;
2927
2928 case NUMBER:
2929 v = parsenumber(c, STR(n));
2930 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002931 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002932 break;
2933 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002934 i = PyObject_IsTrue(v);
2935 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002936 return i == 0;
2937
2938 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002939 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002940 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002941 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002942 break;
2943 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002944 i = PyObject_IsTrue(v);
2945 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002946 return i == 0;
2947
2948 }
2949 return 0;
2950}
2951
Tim Peters08a898f2001-06-28 01:52:22 +00002952
2953/* Look under n for a return stmt with an expression.
2954 * This hack is used to find illegal returns under "if 0:" blocks in
2955 * functions already known to be generators (as determined by the symtable
2956 * pass).
2957 * Return the offending return node if found, else NULL.
2958 */
2959static node *
2960look_for_offending_return(node *n)
2961{
2962 int i;
2963
2964 for (i = 0; i < NCH(n); ++i) {
2965 node *kid = CHILD(n, i);
2966
2967 switch (TYPE(kid)) {
2968 case classdef:
2969 case funcdef:
2970 case lambdef:
2971 /* Stuff in nested functions & classes doesn't
2972 affect the code block we started in. */
2973 return NULL;
2974
2975 case return_stmt:
2976 if (NCH(kid) > 1)
2977 return kid;
2978 break;
2979
2980 default: {
2981 node *bad = look_for_offending_return(kid);
2982 if (bad != NULL)
2983 return bad;
2984 }
2985 }
2986 }
2987
2988 return NULL;
2989}
2990
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002991static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002992com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002993{
2994 int i;
2995 int anchor = 0;
2996 REQ(n, if_stmt);
2997 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2998 for (i = 0; i+3 < NCH(n); i+=4) {
2999 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003000 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00003001 if (is_constant_false(c, ch)) {
3002 /* We're going to skip this block. However, if this
3003 is a generator, we have to check the dead code
3004 anyway to make sure there aren't any return stmts
3005 with expressions, in the same scope. */
3006 if (c->c_flags & CO_GENERATOR) {
3007 node *p = look_for_offending_return(n);
3008 if (p != NULL) {
3009 int savelineno = c->c_lineno;
3010 c->c_lineno = p->n_lineno;
3011 com_error(c, PyExc_SyntaxError,
3012 "'return' with argument "
3013 "inside generator");
3014 c->c_lineno = savelineno;
3015 }
3016 }
Guido van Rossum7c531111997-03-11 18:42:21 +00003017 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00003018 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00003019 if (i > 0)
3020 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00003021 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003022 com_addfwref(c, JUMP_IF_FALSE, &a);
3023 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003024 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003025 com_node(c, CHILD(n, i+3));
3026 com_addfwref(c, JUMP_FORWARD, &anchor);
3027 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003028 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003029 com_addbyte(c, POP_TOP);
3030 }
3031 if (i+2 < NCH(n))
3032 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00003033 if (anchor)
3034 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003035}
3036
3037static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003038com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003039{
3040 int break_anchor = 0;
3041 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003042 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003043 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3044 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003045 block_push(c, SETUP_LOOP);
3046 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003047 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003048 com_node(c, CHILD(n, 1));
3049 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3050 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003051 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003052 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003053 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003054 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003055 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3056 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003057 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003058 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003059 com_addbyte(c, POP_TOP);
3060 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003061 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003062 if (NCH(n) > 4)
3063 com_node(c, CHILD(n, 6));
3064 com_backpatch(c, break_anchor);
3065}
3066
3067static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003068com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003069{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003070 int break_anchor = 0;
3071 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003072 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003073 REQ(n, for_stmt);
3074 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3075 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003076 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003077 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003078 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003079 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003080 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003081 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003082 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003083 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003084 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003085 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003086 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003087 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3088 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003089 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003090 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003091 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003092 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003093 if (NCH(n) > 8)
3094 com_node(c, CHILD(n, 8));
3095 com_backpatch(c, break_anchor);
3096}
3097
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003098/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003099
3100 SETUP_FINALLY L
3101 <code for S>
3102 POP_BLOCK
3103 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003104 L: <code for Sf>
3105 END_FINALLY
3106
3107 The special instructions use the block stack. Each block
3108 stack entry contains the instruction that created it (here
3109 SETUP_FINALLY), the level of the value stack at the time the
3110 block stack entry was created, and a label (here L).
3111
3112 SETUP_FINALLY:
3113 Pushes the current value stack level and the label
3114 onto the block stack.
3115 POP_BLOCK:
3116 Pops en entry from the block stack, and pops the value
3117 stack until its level is the same as indicated on the
3118 block stack. (The label is ignored.)
3119 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003120 Pops a variable number of entries from the *value* stack
3121 and re-raises the exception they specify. The number of
3122 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003123
3124 The block stack is unwound when an exception is raised:
3125 when a SETUP_FINALLY entry is found, the exception is pushed
3126 onto the value stack (and the exception condition is cleared),
3127 and the interpreter jumps to the label gotten from the block
3128 stack.
3129
3130 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003131 (The contents of the value stack is shown in [], with the top
3132 at the right; 'tb' is trace-back info, 'val' the exception's
3133 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003134
3135 Value stack Label Instruction Argument
3136 [] SETUP_EXCEPT L1
3137 [] <code for S>
3138 [] POP_BLOCK
3139 [] JUMP_FORWARD L0
3140
Guido van Rossum3f5da241990-12-20 15:06:42 +00003141 [tb, val, exc] L1: DUP )
3142 [tb, val, exc, exc] <evaluate E1> )
3143 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3144 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3145 [tb, val, exc, 1] POP )
3146 [tb, val, exc] POP
3147 [tb, val] <assign to V1> (or POP if no V1)
3148 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003149 [] <code for S1>
3150 JUMP_FORWARD L0
3151
Guido van Rossum3f5da241990-12-20 15:06:42 +00003152 [tb, val, exc, 0] L2: POP
3153 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003154 .............................etc.......................
3155
Guido van Rossum3f5da241990-12-20 15:06:42 +00003156 [tb, val, exc, 0] Ln+1: POP
3157 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003158
3159 [] L0: <next statement>
3160
3161 Of course, parts are not generated if Vi or Ei is not present.
3162*/
3163
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003164static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003165com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003166{
3167 int except_anchor = 0;
3168 int end_anchor = 0;
3169 int else_anchor = 0;
3170 int i;
3171 node *ch;
3172
3173 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3174 block_push(c, SETUP_EXCEPT);
3175 com_node(c, CHILD(n, 2));
3176 com_addbyte(c, POP_BLOCK);
3177 block_pop(c, SETUP_EXCEPT);
3178 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3179 com_backpatch(c, except_anchor);
3180 for (i = 3;
3181 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3182 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003183 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003184 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003185 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003186 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003187 break;
3188 }
3189 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003190 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003191 com_addoparg(c, SET_LINENO, ch->n_lineno);
3192 if (NCH(ch) > 1) {
3193 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003194 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003195 com_node(c, CHILD(ch, 1));
3196 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003197 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003198 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3199 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003200 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003201 }
3202 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003203 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003204 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003205 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003206 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003207 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003208 com_pop(c, 1);
3209 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003210 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003211 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003212 com_node(c, CHILD(n, i+2));
3213 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3214 if (except_anchor) {
3215 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003216 /* We come in with [tb, val, exc, 0] on the
3217 stack; one pop and it's the same as
3218 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003219 com_addbyte(c, POP_TOP);
3220 }
3221 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003222 /* We actually come in here with [tb, val, exc] but the
3223 END_FINALLY will zap those and jump around.
3224 The c_stacklevel does not reflect them so we need not pop
3225 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003226 com_addbyte(c, END_FINALLY);
3227 com_backpatch(c, else_anchor);
3228 if (i < NCH(n))
3229 com_node(c, CHILD(n, i+2));
3230 com_backpatch(c, end_anchor);
3231}
3232
3233static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003234com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003235{
3236 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003237 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003238
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003239 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3240 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003241 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003242 com_addbyte(c, POP_BLOCK);
3243 block_pop(c, SETUP_FINALLY);
3244 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003245 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003246 /* While the generated code pushes only one item,
3247 the try-finally handling can enter here with
3248 up to three items. OK, here are the details:
3249 3 for an exception, 2 for RETURN, 1 for BREAK. */
3250 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003251 com_backpatch(c, finally_anchor);
3252 ch = CHILD(n, NCH(n)-1);
3253 com_addoparg(c, SET_LINENO, ch->n_lineno);
3254 com_node(c, ch);
3255 com_addbyte(c, END_FINALLY);
3256 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003257 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003258}
3259
3260static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003261com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003262{
3263 REQ(n, try_stmt);
3264 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3265 | 'try' ':' suite 'finally' ':' suite */
3266 if (TYPE(CHILD(n, 3)) != except_clause)
3267 com_try_finally(c, n);
3268 else
3269 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003270}
3271
Guido van Rossum8b993a91997-01-17 21:04:03 +00003272static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003273get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003274{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003275 int i;
3276
Guido van Rossum8b993a91997-01-17 21:04:03 +00003277 /* Label to avoid tail recursion */
3278 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003279 switch (TYPE(n)) {
3280
3281 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003282 if (NCH(n) == 1) {
3283 n = CHILD(n, 0);
3284 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003285 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003286 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003287 case file_input:
3288 for (i = 0; i < NCH(n); i++) {
3289 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003290 if (TYPE(ch) == stmt) {
3291 n = ch;
3292 goto next;
3293 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003294 }
3295 break;
3296
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003297 case stmt:
3298 case simple_stmt:
3299 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003300 n = CHILD(n, 0);
3301 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003302
3303 case expr_stmt:
3304 case testlist:
3305 case test:
3306 case and_test:
3307 case not_test:
3308 case comparison:
3309 case expr:
3310 case xor_expr:
3311 case and_expr:
3312 case shift_expr:
3313 case arith_expr:
3314 case term:
3315 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003316 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003317 if (NCH(n) == 1) {
3318 n = CHILD(n, 0);
3319 goto next;
3320 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003321 break;
3322
3323 case atom:
3324 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003325 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003326 break;
3327
3328 }
3329 return NULL;
3330}
3331
Guido van Rossum79f25d91997-04-29 20:08:16 +00003332static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003333get_docstring(node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003334{
Guido van Rossum541563e1999-01-28 15:08:09 +00003335 /* Don't generate doc-strings if run with -OO */
3336 if (Py_OptimizeFlag > 1)
3337 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003338 n = get_rawdocstring(n);
3339 if (n == NULL)
3340 return NULL;
3341 return parsestrplus(n);
3342}
3343
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003344static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003345com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003346{
3347 REQ(n, suite);
3348 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3349 if (NCH(n) == 1) {
3350 com_node(c, CHILD(n, 0));
3351 }
3352 else {
3353 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003354 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003355 node *ch = CHILD(n, i);
3356 if (TYPE(ch) == stmt)
3357 com_node(c, ch);
3358 }
3359 }
3360}
3361
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003362/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003363static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003364com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003365{
3366 int i = c->c_nblocks;
3367 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3368 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3369 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003370 else if (i <= 0) {
3371 /* at the outer level */
3372 com_error(c, PyExc_SyntaxError,
3373 "'continue' not properly in loop");
3374 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003375 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003376 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003377 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003378 if (c->c_block[j] == SETUP_LOOP)
3379 break;
3380 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003381 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003382 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003383 for (; i > j; --i) {
3384 if (c->c_block[i] == SETUP_EXCEPT ||
3385 c->c_block[i] == SETUP_FINALLY) {
3386 com_addoparg(c, CONTINUE_LOOP,
3387 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003388 return;
3389 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003390 if (c->c_block[i] == END_FINALLY) {
3391 com_error(c, PyExc_SyntaxError,
3392 "'continue' not supported inside 'finally' clause");
3393 return;
3394 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003395 }
3396 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003397 com_error(c, PyExc_SyntaxError,
3398 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003399 }
3400 /* XXX Could allow it inside a 'finally' clause
3401 XXX if we could pop the exception still on the stack */
3402}
3403
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003404static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003405com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003406{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003407 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003408 if (TYPE(n) == lambdef) {
3409 /* lambdef: 'lambda' [varargslist] ':' test */
3410 n = CHILD(n, 1);
3411 }
3412 else {
3413 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3414 n = CHILD(n, 2);
3415 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3416 n = CHILD(n, 1);
3417 }
3418 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003419 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003420 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003421 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003422 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3423 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003424 nargs = 0;
3425 ndefs = 0;
3426 for (i = 0; i < nch; i++) {
3427 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003428 if (TYPE(CHILD(n, i)) == STAR ||
3429 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003430 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003431 nargs++;
3432 i++;
3433 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003434 t = RPAR; /* Anything except EQUAL or COMMA */
3435 else
3436 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003437 if (t == EQUAL) {
3438 i++;
3439 ndefs++;
3440 com_node(c, CHILD(n, i));
3441 i++;
3442 if (i >= nch)
3443 break;
3444 t = TYPE(CHILD(n, i));
3445 }
3446 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003447 /* Treat "(a=1, b)" as an error */
3448 if (ndefs)
3449 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003450 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003451 }
3452 if (t != COMMA)
3453 break;
3454 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003455 return ndefs;
3456}
3457
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003458static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003459com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003460{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003461 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003462 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003463 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003464 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003465 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3466 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003467 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003468 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003469 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003470 c->c_errors++;
3471 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003472 int closure = com_make_closure(c, (PyCodeObject *)co);
3473 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003474 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003475 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003476 if (closure)
3477 com_addoparg(c, MAKE_CLOSURE, ndefs);
3478 else
3479 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003480 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003481 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003482 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003483 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003484 }
3485}
3486
3487static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003488com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003489{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003490 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003491 REQ(n, testlist);
3492 /* testlist: test (',' test)* [','] */
3493 for (i = 0; i < NCH(n); i += 2)
3494 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003495 i = (NCH(n)+1) / 2;
3496 com_addoparg(c, BUILD_TUPLE, i);
3497 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003498}
3499
3500static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003501com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003502{
Guido van Rossum25831651993-05-19 14:50:45 +00003503 int i;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003504 PyObject *co, *v;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003505 char *name;
3506
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003507 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003508 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003509 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003510 c->c_errors++;
3511 return;
3512 }
3513 /* Push the class name on the stack */
3514 i = com_addconst(c, v);
3515 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003516 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003517 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003518 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003519 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003520 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003521 com_push(c, 1);
3522 }
Guido van Rossum25831651993-05-19 14:50:45 +00003523 else
3524 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003525 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003526 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003527 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003528 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003529 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003530 c->c_errors++;
3531 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003532 int closure = com_make_closure(c, (PyCodeObject *)co);
3533 i = com_addconst(c, co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003534 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003535 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003536 if (closure)
3537 com_addoparg(c, MAKE_CLOSURE, 0);
3538 else
3539 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003540 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003541 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003542 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003543 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003544 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003545 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003546}
3547
3548static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003549com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003550{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003551 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003552 if (c->c_errors)
3553 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003554 switch (TYPE(n)) {
3555
3556 /* Definition nodes */
3557
3558 case funcdef:
3559 com_funcdef(c, n);
3560 break;
3561 case classdef:
3562 com_classdef(c, n);
3563 break;
3564
3565 /* Trivial parse tree nodes */
3566
3567 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003568 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003569 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003570 n = CHILD(n, 0);
3571 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003572
3573 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003574 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3575 com_addoparg(c, SET_LINENO, n->n_lineno);
3576 {
3577 int i;
3578 for (i = 0; i < NCH(n)-1; i += 2)
3579 com_node(c, CHILD(n, i));
3580 }
3581 break;
3582
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003583 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003584 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003585 n = CHILD(n, 0);
3586 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003587
3588 /* Statement nodes */
3589
3590 case expr_stmt:
3591 com_expr_stmt(c, n);
3592 break;
3593 case print_stmt:
3594 com_print_stmt(c, n);
3595 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003596 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003597 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003598 break;
3599 case pass_stmt:
3600 break;
3601 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003602 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003603 com_error(c, PyExc_SyntaxError,
3604 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003605 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003606 com_addbyte(c, BREAK_LOOP);
3607 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003608 case continue_stmt:
3609 com_continue_stmt(c, n);
3610 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003611 case return_stmt:
3612 com_return_stmt(c, n);
3613 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003614 case yield_stmt:
3615 com_yield_stmt(c, n);
3616 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003617 case raise_stmt:
3618 com_raise_stmt(c, n);
3619 break;
3620 case import_stmt:
3621 com_import_stmt(c, n);
3622 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003623 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003624 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003625 case exec_stmt:
3626 com_exec_stmt(c, n);
3627 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003628 case assert_stmt:
3629 com_assert_stmt(c, n);
3630 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003631 case if_stmt:
3632 com_if_stmt(c, n);
3633 break;
3634 case while_stmt:
3635 com_while_stmt(c, n);
3636 break;
3637 case for_stmt:
3638 com_for_stmt(c, n);
3639 break;
3640 case try_stmt:
3641 com_try_stmt(c, n);
3642 break;
3643 case suite:
3644 com_suite(c, n);
3645 break;
3646
3647 /* Expression nodes */
3648
3649 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003650 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003651 break;
3652 case test:
3653 com_test(c, n);
3654 break;
3655 case and_test:
3656 com_and_test(c, n);
3657 break;
3658 case not_test:
3659 com_not_test(c, n);
3660 break;
3661 case comparison:
3662 com_comparison(c, n);
3663 break;
3664 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003665 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003666 break;
3667 case expr:
3668 com_expr(c, n);
3669 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003670 case xor_expr:
3671 com_xor_expr(c, n);
3672 break;
3673 case and_expr:
3674 com_and_expr(c, n);
3675 break;
3676 case shift_expr:
3677 com_shift_expr(c, n);
3678 break;
3679 case arith_expr:
3680 com_arith_expr(c, n);
3681 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003682 case term:
3683 com_term(c, n);
3684 break;
3685 case factor:
3686 com_factor(c, n);
3687 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003688 case power:
3689 com_power(c, n);
3690 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003691 case atom:
3692 com_atom(c, n);
3693 break;
3694
3695 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003696 com_error(c, PyExc_SystemError,
3697 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003698 }
3699}
3700
Tim Petersdbd9ba62000-07-09 03:09:57 +00003701static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003702
3703static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003704com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003705{
3706 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3707 if (TYPE(CHILD(n, 0)) == LPAR)
3708 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003709 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003710 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003711 com_pop(c, 1);
3712 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003713}
3714
3715static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003716com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003717{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003718 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003719 if (NCH(n) == 1) {
3720 com_fpdef(c, CHILD(n, 0));
3721 }
3722 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003723 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003724 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003725 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003726 for (i = 0; i < NCH(n); i += 2)
3727 com_fpdef(c, CHILD(n, i));
3728 }
3729}
3730
3731static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003732com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003733{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003734 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003735 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003736 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003737 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003738 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003739 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003740 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003741 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003742 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003743 node *ch = CHILD(n, i);
3744 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003745 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003746 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003747 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3748 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003749 if (TYPE(fp) != NAME) {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003750 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003751 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003752 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003753 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003754 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003755 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003756 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003757 ch = CHILD(n, i);
3758 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003759 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003760 else
3761 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003762 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003763 if (complex) {
3764 /* Generate code for complex arguments only after
3765 having counted the simple arguments */
3766 int ilocal = 0;
3767 for (i = 0; i < nch; i++) {
3768 node *ch = CHILD(n, i);
3769 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003770 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003771 break;
3772 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3773 fp = CHILD(ch, 0);
3774 if (TYPE(fp) != NAME) {
3775 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003776 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003777 com_fpdef(c, ch);
3778 }
3779 ilocal++;
3780 if (++i >= nch)
3781 break;
3782 ch = CHILD(n, i);
3783 if (TYPE(ch) == EQUAL)
3784 i += 2;
3785 else
3786 REQ(ch, COMMA);
3787 }
3788 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003789}
3790
3791static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003792com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003793{
3794 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003795 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003797 doc = get_docstring(n);
3798 if (doc != NULL) {
3799 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003800 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003801 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003802 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003803 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003804 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003805 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003806 for (i = 0; i < NCH(n); i++) {
3807 node *ch = CHILD(n, i);
3808 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3809 com_node(c, ch);
3810 }
3811}
3812
3813/* Top-level compile-node interface */
3814
3815static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003816compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003817{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003818 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003819 node *ch;
3820 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003821 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003822 doc = get_docstring(CHILD(n, 4));
3823 if (doc != NULL) {
3824 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003825 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003826 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003827 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003828 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003829 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3830 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003831 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003832 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003833 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003834 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003835 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00003836 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3837 com_push(c, 1);
3838 com_addbyte(c, RETURN_VALUE);
3839 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003840}
3841
3842static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003843compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003844{
Guido van Rossum590baa41993-11-30 13:40:46 +00003845 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003846 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003847 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003848
3849 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003850 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003851 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003852 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003853 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003854 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003855 else
3856 ch = CHILD(n, 2);
3857 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003858 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003859 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003860}
3861
3862static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003863compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003864{
3865 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003866 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003867 REQ(n, classdef);
3868 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3869 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003870 c->c_private = c->c_name;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003871 ch = CHILD(n, NCH(n)-1); /* The suite */
3872 doc = get_docstring(ch);
3873 if (doc != NULL) {
3874 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003875 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003876 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003877 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003878 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003879 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003880 }
3881 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003882 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003883 com_node(c, ch);
3884 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003885 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003886 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003887 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003888}
3889
3890static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003891compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003893 com_addoparg(c, SET_LINENO, n->n_lineno);
3894
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003895 switch (TYPE(n)) {
3896
Guido van Rossum4c417781991-01-21 16:09:22 +00003897 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003899 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900 n = CHILD(n, 0);
3901 if (TYPE(n) != NEWLINE)
3902 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003903 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003904 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003905 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003906 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003907 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908 break;
3909
Guido van Rossum4c417781991-01-21 16:09:22 +00003910 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003911 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003912 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003913 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003914 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003915 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003916 break;
3917
Guido van Rossum590baa41993-11-30 13:40:46 +00003918 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003919 com_node(c, CHILD(n, 0));
3920 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003921 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003922 break;
3923
Guido van Rossum590baa41993-11-30 13:40:46 +00003924 case lambdef: /* anonymous function definition */
3925 compile_lambdef(c, n);
3926 break;
3927
Guido van Rossum4c417781991-01-21 16:09:22 +00003928 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003929 compile_funcdef(c, n);
3930 break;
3931
Guido van Rossum4c417781991-01-21 16:09:22 +00003932 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003933 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003934 break;
3935
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003936 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003937 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003938 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003939 }
3940}
3941
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003942static PyObject *
3943dict_keys_inorder(PyObject *dict, int offset)
3944{
3945 PyObject *tuple, *k, *v;
3946 int i, pos = 0, size = PyDict_Size(dict);
3947
3948 tuple = PyTuple_New(size);
3949 if (tuple == NULL)
3950 return NULL;
3951 while (PyDict_Next(dict, &pos, &k, &v)) {
3952 i = PyInt_AS_LONG(v);
3953 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003954 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003955 PyTuple_SET_ITEM(tuple, i - offset, k);
3956 }
3957 return tuple;
3958}
3959
Guido van Rossum79f25d91997-04-29 20:08:16 +00003960PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003961PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003962{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003963 return PyNode_CompileFlags(n, filename, NULL);
3964}
3965
3966PyCodeObject *
3967PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
3968{
3969 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003970}
3971
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003972struct symtable *
3973PyNode_CompileSymtable(node *n, char *filename)
3974{
3975 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003976 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003977
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003978 ff = PyNode_Future(n, filename);
3979 if (ff == NULL)
3980 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003981 st = symtable_init();
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003982 if (st == NULL)
3983 return NULL;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003984 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003985 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003986 if (st->st_errors > 0)
3987 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003988 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003989 if (st->st_errors > 0)
3990 goto fail;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00003991
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003992 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003993 fail:
3994 PyMem_Free((void *)ff);
3995 st->st_future = NULL;
3996 PySymtable_Free(st);
3997 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003998}
3999
Guido van Rossum79f25d91997-04-29 20:08:16 +00004000static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004001icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004002{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004003 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004004}
4005
Guido van Rossum79f25d91997-04-29 20:08:16 +00004006static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004007jcompile(node *n, char *filename, struct compiling *base,
4008 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004009{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004010 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004011 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00004012 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004013 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004014 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004015 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004016 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004017 /* c_symtable still points to parent's symbols */
4018 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004019 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004020 sc.c_nested = 1;
Guido van Rossum4668b002001-08-08 05:00:18 +00004021 sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED |
4022 CO_FUTURE_DIVISION);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004023 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004024 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004025 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004026 if (sc.c_future == NULL) {
4027 com_free(&sc);
4028 return NULL;
4029 }
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004030 if (flags)
4031 sc.c_future->ff_features |= flags->cf_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004032 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004033 com_free(&sc);
4034 return NULL;
4035 }
4036 }
4037 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004038 if (symtable_load_symbols(&sc) < 0) {
4039 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004040 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004041 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004042 compile_node(&sc, n);
4043 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00004044 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004045 PyObject *consts, *names, *varnames, *filename, *name,
4046 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004047 consts = PyList_AsTuple(sc.c_consts);
4048 names = PyList_AsTuple(sc.c_names);
4049 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004050 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4051 freevars = dict_keys_inorder(sc.c_freevars,
4052 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00004053 filename = PyString_InternFromString(sc.c_filename);
4054 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004055 if (!PyErr_Occurred())
4056 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004057 sc.c_nlocals,
4058 sc.c_maxstacklevel,
4059 sc.c_flags,
4060 sc.c_code,
4061 consts,
4062 names,
4063 varnames,
4064 freevars,
4065 cellvars,
4066 filename,
4067 name,
4068 sc.c_firstlineno,
4069 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004070 Py_XDECREF(consts);
4071 Py_XDECREF(names);
4072 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004073 Py_XDECREF(freevars);
4074 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004075 Py_XDECREF(filename);
4076 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004077 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004078 else if (!PyErr_Occurred()) {
4079 /* This could happen if someone called PyErr_Clear() after an
4080 error was reported above. That's not supposed to happen,
4081 but I just plugged one case and I'm not sure there can't be
4082 others. In that case, raise SystemError so that at least
4083 it gets reported instead dumping core. */
4084 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4085 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004086 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004087 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004088 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004089 sc.c_symtable = NULL;
4090 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004091 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004092 return co;
4093}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004094
4095int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004096PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004097{
4098 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004099 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004100 int line = co->co_firstlineno;
4101 int addr = 0;
4102 while (--size >= 0) {
4103 addr += *p++;
4104 if (addr > addrq)
4105 break;
4106 line += *p++;
4107 }
4108 return line;
4109}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004110
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004111/* The test for LOCAL must come before the test for FREE in order to
4112 handle classes where name is both local and free. The local var is
4113 a method and the free var is a free var referenced within a method.
4114*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004115
4116static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004117get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004118{
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004119 char buf[350];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004120 PyObject *v;
Jeremy Hylton51257732001-03-01 00:42:55 +00004121
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004122 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4123 return CELL;
4124 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4125 return LOCAL;
4126 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4127 return FREE;
4128 v = PyDict_GetItemString(c->c_globals, name);
4129 if (v) {
4130 if (v == Py_None)
4131 return GLOBAL_EXPLICIT;
4132 else {
4133 return GLOBAL_IMPLICIT;
4134 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004135 }
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004136 sprintf(buf,
4137 "unknown scope for %.100s in %.100s(%s) "
4138 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4139 name, c->c_name,
4140 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4141 c->c_filename,
4142 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4143 PyObject_REPR(c->c_locals),
4144 PyObject_REPR(c->c_globals)
4145 );
4146
4147 Py_FatalError(buf);
4148 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004149}
4150
Guido van Rossum207fda62001-03-02 03:30:41 +00004151/* Helper functions to issue warnings */
4152
4153static int
4154issue_warning(char *msg, char *filename, int lineno)
4155{
4156 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4157 lineno, NULL, NULL) < 0) {
4158 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4159 PyErr_SetString(PyExc_SyntaxError, msg);
4160 PyErr_SyntaxLocation(filename, lineno);
4161 }
4162 return -1;
4163 }
4164 return 0;
4165}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004166
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004167static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004168symtable_warn(struct symtable *st, char *msg)
4169{
Guido van Rossum207fda62001-03-02 03:30:41 +00004170 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004171 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004172 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004173 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004174 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004175}
4176
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004177/* Helper function for setting lineno and filename */
4178
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004179static int
4180symtable_build(struct compiling *c, node *n)
4181{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004182 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004183 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004184 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004185 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004186 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4187 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004188 return -1;
4189 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004190 if (c->c_symtable->st_errors > 0)
4191 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004192 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004193 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004194 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004195 return 0;
4196}
4197
4198static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004199symtable_init_compiling_symbols(struct compiling *c)
4200{
4201 PyObject *varnames;
4202
4203 varnames = c->c_symtable->st_cur->ste_varnames;
4204 if (varnames == NULL) {
4205 varnames = PyList_New(0);
4206 if (varnames == NULL)
4207 return -1;
4208 c->c_symtable->st_cur->ste_varnames = varnames;
4209 Py_INCREF(varnames);
4210 } else
4211 Py_INCREF(varnames);
4212 c->c_varnames = varnames;
4213
4214 c->c_globals = PyDict_New();
4215 if (c->c_globals == NULL)
4216 return -1;
4217 c->c_freevars = PyDict_New();
4218 if (c->c_freevars == NULL)
4219 return -1;
4220 c->c_cellvars = PyDict_New();
4221 if (c->c_cellvars == NULL)
4222 return -1;
4223 return 0;
4224}
4225
4226struct symbol_info {
4227 int si_nlocals;
4228 int si_ncells;
4229 int si_nfrees;
4230 int si_nimplicit;
4231};
4232
4233static void
4234symtable_init_info(struct symbol_info *si)
4235{
4236 si->si_nlocals = 0;
4237 si->si_ncells = 0;
4238 si->si_nfrees = 0;
4239 si->si_nimplicit = 0;
4240}
4241
4242static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004243symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004244 struct symbol_info *si)
4245{
4246 PyObject *dict, *v;
4247
4248 /* Seperate logic for DEF_FREE. If it occurs in a function,
4249 it indicates a local that we must allocate storage for (a
4250 cell var). If it occurs in a class, then the class has a
4251 method and a free variable with the same name.
4252 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004253 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004254 /* If it isn't declared locally, it can't be a cell. */
4255 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4256 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004257 v = PyInt_FromLong(si->si_ncells++);
4258 dict = c->c_cellvars;
4259 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004260 /* If it is free anyway, then there is no need to do
4261 anything here.
4262 */
4263 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004264 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004265 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004266 v = PyInt_FromLong(si->si_nfrees++);
4267 dict = c->c_freevars;
4268 }
4269 if (v == NULL)
4270 return -1;
4271 if (PyDict_SetItem(dict, name, v) < 0) {
4272 Py_DECREF(v);
4273 return -1;
4274 }
4275 Py_DECREF(v);
4276 return 0;
4277}
4278
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004279/* If a variable is a cell and an argument, make sure that appears in
4280 co_cellvars before any variable to its right in varnames.
4281*/
4282
4283
4284static int
4285symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4286 PyObject *varnames, int flags)
4287{
4288 PyObject *v, *w, *d, *list = NULL;
4289 int i, pos;
4290
4291 if (flags & CO_VARARGS)
4292 argcount++;
4293 if (flags & CO_VARKEYWORDS)
4294 argcount++;
4295 for (i = argcount; --i >= 0; ) {
4296 v = PyList_GET_ITEM(varnames, i);
4297 if (PyDict_GetItem(*cellvars, v)) {
4298 if (list == NULL) {
4299 list = PyList_New(1);
4300 if (list == NULL)
4301 return -1;
4302 PyList_SET_ITEM(list, 0, v);
4303 Py_INCREF(v);
4304 } else
4305 PyList_Insert(list, 0, v);
4306 }
4307 }
4308 if (list == NULL || PyList_GET_SIZE(list) == 0)
4309 return 0;
4310 /* There are cellvars that are also arguments. Create a dict
4311 to replace cellvars and put the args at the front.
4312 */
4313 d = PyDict_New();
4314 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4315 v = PyInt_FromLong(i);
4316 if (v == NULL)
4317 goto fail;
4318 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4319 goto fail;
4320 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4321 goto fail;
4322 }
4323 pos = 0;
4324 i = PyList_GET_SIZE(list);
4325 Py_DECREF(list);
4326 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4327 w = PyInt_FromLong(i++); /* don't care about the old key */
4328 if (PyDict_SetItem(d, v, w) < 0) {
4329 Py_DECREF(w);
4330 goto fail;
4331 }
4332 Py_DECREF(w);
4333 }
4334 Py_DECREF(*cellvars);
4335 *cellvars = d;
4336 return 1;
4337 fail:
4338 Py_DECREF(d);
4339 return -1;
4340}
4341
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004342static int
4343symtable_freevar_offsets(PyObject *freevars, int offset)
4344{
4345 PyObject *name, *v;
4346 int pos;
4347
4348 /* The cell vars are the first elements of the closure,
4349 followed by the free vars. Update the offsets in
4350 c_freevars to account for number of cellvars. */
4351 pos = 0;
4352 while (PyDict_Next(freevars, &pos, &name, &v)) {
4353 int i = PyInt_AS_LONG(v) + offset;
4354 PyObject *o = PyInt_FromLong(i);
4355 if (o == NULL)
4356 return -1;
4357 if (PyDict_SetItem(freevars, name, o) < 0) {
4358 Py_DECREF(o);
4359 return -1;
4360 }
4361 Py_DECREF(o);
4362 }
4363 return 0;
4364}
4365
4366static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004367symtable_check_unoptimized(struct compiling *c,
4368 PySymtableEntryObject *ste,
4369 struct symbol_info *si)
4370{
4371 char buf[300];
4372
4373 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4374 || (ste->ste_nested && si->si_nimplicit)))
4375 return 0;
4376
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004377#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4378
4379#define ILLEGAL_IS "is a nested function"
4380
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004381#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004382"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004383
4384#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004385"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004386
4387#define ILLEGAL_EXEC_AND_IMPORT_STAR \
4388"function '%.100s' uses import * and bare exec, which are illegal" \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004389"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004390
4391 /* XXX perhaps the linenos for these opt-breaking statements
4392 should be stored so the exception can point to them. */
4393
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004394 if (ste->ste_child_free) {
4395 if (ste->ste_optimized == OPT_IMPORT_STAR)
4396 sprintf(buf, ILLEGAL_IMPORT_STAR,
4397 PyString_AS_STRING(ste->ste_name),
4398 ILLEGAL_CONTAINS);
4399 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4400 sprintf(buf, ILLEGAL_BARE_EXEC,
4401 PyString_AS_STRING(ste->ste_name),
4402 ILLEGAL_CONTAINS);
4403 else {
4404 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4405 PyString_AS_STRING(ste->ste_name),
4406 ILLEGAL_CONTAINS);
4407 }
4408 } else {
4409 if (ste->ste_optimized == OPT_IMPORT_STAR)
4410 sprintf(buf, ILLEGAL_IMPORT_STAR,
4411 PyString_AS_STRING(ste->ste_name),
4412 ILLEGAL_IS);
4413 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4414 sprintf(buf, ILLEGAL_BARE_EXEC,
4415 PyString_AS_STRING(ste->ste_name),
4416 ILLEGAL_IS);
4417 else {
4418 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4419 PyString_AS_STRING(ste->ste_name),
4420 ILLEGAL_IS);
4421 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004422 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004423
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004424 PyErr_SetString(PyExc_SyntaxError, buf);
4425 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4426 ste->ste_opt_lineno);
4427 return -1;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004428}
4429
4430static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004431symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4432 struct symbol_info *si)
4433{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004434 if (c->c_future)
4435 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004436 if (ste->ste_generator)
4437 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004438 if (ste->ste_type != TYPE_MODULE)
4439 c->c_flags |= CO_NEWLOCALS;
4440 if (ste->ste_type == TYPE_FUNCTION) {
4441 c->c_nlocals = si->si_nlocals;
4442 if (ste->ste_optimized == 0)
4443 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004444 else if (ste->ste_optimized != OPT_EXEC)
4445 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004446 }
4447 return 0;
4448}
4449
4450static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004451symtable_load_symbols(struct compiling *c)
4452{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004453 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004454 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004455 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004456 PyObject *name, *varnames, *v;
4457 int i, flags, pos;
4458 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004459
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004460 if (implicit == NULL) {
4461 implicit = PyInt_FromLong(1);
4462 if (implicit == NULL)
4463 return -1;
4464 }
4465 v = NULL;
4466
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004467 if (symtable_init_compiling_symbols(c) < 0)
4468 goto fail;
4469 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004470 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004471 si.si_nlocals = PyList_GET_SIZE(varnames);
4472 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004473
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004474 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004475 v = PyInt_FromLong(i);
4476 if (PyDict_SetItem(c->c_locals,
4477 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004478 goto fail;
4479 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004480 }
4481
4482 /* XXX The cases below define the rules for whether a name is
4483 local or global. The logic could probably be clearer. */
4484 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004485 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4486 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004487
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004488 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004489 /* undo the original DEF_FREE */
4490 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004491
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004492 /* Deal with names that need two actions:
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004493 1. Cell variables that are also locals.
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004494 2. Free variables in methods that are also class
4495 variables or declared global.
4496 */
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004497 if (flags & (DEF_FREE | DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004498 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004499
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004500 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004501 c->c_argcount--;
4502 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004503 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004504 c->c_argcount--;
4505 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004506 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004507 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004508 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004509 if (flags & DEF_PARAM) {
4510 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004511 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004512 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004513 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004514 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004515 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004516 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004517 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4518 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004519 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004520 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004521 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4522 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004523 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004524 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004525 if (v == NULL)
4526 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004527 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004528 goto fail;
4529 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004530 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004531 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004532 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004533 } else if (is_free(flags)) {
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004534 if (ste->ste_nested) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004535 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004536 if (v == NULL)
4537 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004538 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004539 goto fail;
4540 Py_DECREF(v);
4541 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004542 si.si_nimplicit++;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004543 if (PyDict_SetItem(c->c_globals, name,
4544 implicit) < 0)
4545 goto fail;
4546 if (st->st_nscopes != 1) {
4547 v = PyInt_FromLong(flags);
4548 if (PyDict_SetItem(st->st_global,
4549 name, v))
4550 goto fail;
4551 Py_DECREF(v);
4552 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004553 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004554 }
4555 }
4556
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004557 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4558
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004559 if (si.si_ncells > 1) { /* one cell is always in order */
4560 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4561 c->c_varnames, c->c_flags) < 0)
4562 return -1;
4563 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004564 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4565 return -1;
4566 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004567 fail:
4568 /* is this always the right thing to do? */
4569 Py_XDECREF(v);
4570 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004571}
4572
4573static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004574symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004575{
4576 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004577
4578 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4579 if (st == NULL)
4580 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004581 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004582
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004583 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004584 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004585 goto fail;
4586 if ((st->st_symbols = PyDict_New()) == NULL)
4587 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004588 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004589 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004590 st->st_errors = 0;
4591 st->st_tmpname = 0;
4592 st->st_private = NULL;
4593 return st;
4594 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004595 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004596 return NULL;
4597}
4598
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004599void
4600PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004601{
4602 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004603 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004604 Py_XDECREF(st->st_cur);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004605 PyMem_Free((void *)st);
4606}
4607
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004608/* When the compiler exits a scope, it must should update the scope's
4609 free variable information with the list of free variables in its
4610 children.
4611
4612 Variables that are free in children and defined in the current
4613 scope are cellvars.
4614
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004615 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004616 false), free variables in children that are not defined here are
4617 implicit globals.
4618
4619*/
4620
4621static int
4622symtable_update_free_vars(struct symtable *st)
4623{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004624 int i, j, def;
4625 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004626 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004627
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004628 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004629 def = DEF_FREE_CLASS;
4630 else
4631 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004632 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004633 int pos = 0;
4634
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004635 if (list)
4636 PyList_SetSlice(list, 0,
4637 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004638 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004639 PyList_GET_ITEM(ste->ste_children, i);
4640 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004641 int flags = PyInt_AS_LONG(o);
4642 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004643 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004644 if (list == NULL) {
4645 list = PyList_New(0);
4646 if (list == NULL)
4647 return -1;
4648 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004649 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004650 if (PyList_Append(list, name) < 0) {
4651 Py_DECREF(list);
4652 return -1;
4653 }
4654 }
4655 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004656 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004657 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004658 v = PyDict_GetItem(ste->ste_symbols, name);
4659 /* If a name N is declared global in scope A and
4660 referenced in scope B contained (perhaps
4661 indirectly) in A and there are no scopes
4662 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004663 is global in B. Unless A is a class scope,
4664 because class scopes are not considered for
4665 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004666 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004667 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004668 int flags = PyInt_AS_LONG(v);
4669 if (flags & DEF_GLOBAL) {
4670 symtable_undo_free(st, child->ste_id,
4671 name);
4672 continue;
4673 }
4674 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004675 if (ste->ste_nested) {
4676 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004677 name, def) < 0) {
4678 Py_DECREF(list);
4679 return -1;
4680 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004681 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004682 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004683 name) < 0) {
4684 Py_DECREF(list);
4685 return -1;
4686 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004687 }
4688 }
4689 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004690
4691 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004692 return 0;
4693}
4694
4695/* If the current scope is a non-nested class or if name is not
4696 defined in the current, non-nested scope, then it is an implicit
4697 global in all nested scopes.
4698*/
4699
4700static int
4701symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4702{
4703 PyObject *o;
4704 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004705 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004706
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004707 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004708 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004709 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004710 if (o == NULL)
4711 return symtable_undo_free(st, child, name);
4712 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004713
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004714 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004715 return symtable_undo_free(st, child, name);
4716 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004717 return symtable_add_def_o(st, ste->ste_symbols,
4718 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004719}
4720
4721static int
4722symtable_undo_free(struct symtable *st, PyObject *id,
4723 PyObject *name)
4724{
4725 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004726 PyObject *info;
4727 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004728
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004729 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4730 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004731 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004732
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004733 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004734 if (info == NULL)
4735 return 0;
4736 v = PyInt_AS_LONG(info);
4737 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004738 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004739 DEF_FREE_GLOBAL) < 0)
4740 return -1;
4741 } else
4742 /* If the name is defined here or declared global,
4743 then the recursion stops. */
4744 return 0;
4745
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004746 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4747 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00004748 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004749 PyList_GET_ITEM(ste->ste_children, i);
4750 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004751 if (x < 0)
4752 return x;
4753 }
4754 return 0;
4755}
4756
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004757/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4758 This reference is released when the scope is exited, via the DECREF
4759 in symtable_exit_scope().
4760*/
4761
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004762static int
4763symtable_exit_scope(struct symtable *st)
4764{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004765 int end;
4766
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004767 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004768 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004769 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004770 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004771 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4772 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004773 if (PySequence_DelItem(st->st_stack, end) < 0)
4774 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004775 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004776}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004777
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004778static void
4779symtable_enter_scope(struct symtable *st, char *name, int type,
4780 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004781{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004782 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004783
4784 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004785 prev = st->st_cur;
4786 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4787 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004788 st->st_errors++;
4789 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004790 }
4791 }
Barry Warsaw0372af72001-02-23 18:22:59 +00004792 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004793 PySymtableEntry_New(st, name, type, lineno);
4794 if (strcmp(name, TOP) == 0)
4795 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004796 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004797 if (PyList_Append(prev->ste_children,
4798 (PyObject *)st->st_cur) < 0)
4799 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004800 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004801}
4802
4803static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004804symtable_lookup(struct symtable *st, char *name)
4805{
4806 char buffer[MANGLE_LEN];
4807 PyObject *v;
4808 int flags;
4809
4810 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4811 name = buffer;
4812 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4813 if (v == NULL) {
4814 if (PyErr_Occurred())
4815 return -1;
4816 else
4817 return 0;
4818 }
4819
4820 flags = PyInt_AS_LONG(v);
4821 return flags;
4822}
4823
4824static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004825symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004826{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004827 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004828 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004829 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004830
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004831 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004832 name = buffer;
4833 if ((s = PyString_InternFromString(name)) == NULL)
4834 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004835 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4836 Py_DECREF(s);
4837 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004838}
4839
4840/* Must only be called with mangled names */
4841
4842static int
4843symtable_add_def_o(struct symtable *st, PyObject *dict,
4844 PyObject *name, int flag)
4845{
4846 PyObject *o;
4847 int val;
4848
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004849 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004850 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004851 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004852 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00004853 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004854 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004855 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004856 return -1;
4857 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004858 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004859 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004860 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004861 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004862 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004863 Py_DECREF(o);
4864 return -1;
4865 }
4866 Py_DECREF(o);
4867
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004868 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004869 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004870 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004871 } else if (flag & DEF_GLOBAL) {
4872 /* XXX need to update DEF_GLOBAL for other flags too;
4873 perhaps only DEF_FREE_GLOBAL */
4874 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004875 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004876 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004877 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004878 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004879 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004880 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004881 Py_DECREF(o);
4882 return -1;
4883 }
4884 Py_DECREF(o);
4885 }
4886 return 0;
4887}
4888
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004889#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004890
Tim Peters08a898f2001-06-28 01:52:22 +00004891/* Look for a yield stmt under n. Return 1 if found, else 0.
4892 This hack is used to look inside "if 0:" blocks (which are normally
4893 ignored) in case those are the only places a yield occurs (so that this
4894 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00004895static int
4896look_for_yield(node *n)
4897{
4898 int i;
4899
4900 for (i = 0; i < NCH(n); ++i) {
4901 node *kid = CHILD(n, i);
4902
4903 switch (TYPE(kid)) {
4904
4905 case classdef:
4906 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00004907 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00004908 /* Stuff in nested functions and classes can't make
4909 the parent a generator. */
4910 return 0;
4911
4912 case yield_stmt:
4913 return 1;
4914
4915 default:
4916 if (look_for_yield(kid))
4917 return 1;
4918 }
4919 }
4920 return 0;
4921}
4922
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004923static void
4924symtable_node(struct symtable *st, node *n)
4925{
4926 int i, start = 0;
4927
4928 loop:
4929 switch (TYPE(n)) {
4930 case funcdef: {
4931 char *func_name = STR(CHILD(n, 1));
4932 symtable_add_def(st, func_name, DEF_LOCAL);
4933 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004934 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004935 symtable_funcdef(st, n);
4936 symtable_exit_scope(st);
4937 break;
4938 }
4939 case lambdef:
4940 if (NCH(n) == 4)
4941 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004942 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004943 symtable_funcdef(st, n);
4944 symtable_exit_scope(st);
4945 break;
4946 case classdef: {
4947 char *tmp, *class_name = STR(CHILD(n, 1));
4948 symtable_add_def(st, class_name, DEF_LOCAL);
4949 if (TYPE(CHILD(n, 2)) == LPAR) {
4950 node *bases = CHILD(n, 3);
4951 int i;
4952 for (i = 0; i < NCH(bases); i += 2) {
4953 symtable_node(st, CHILD(bases, i));
4954 }
4955 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004956 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004957 tmp = st->st_private;
4958 st->st_private = class_name;
4959 symtable_node(st, CHILD(n, NCH(n) - 1));
4960 st->st_private = tmp;
4961 symtable_exit_scope(st);
4962 break;
4963 }
4964 case if_stmt:
4965 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00004966 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
4967 if (st->st_cur->ste_generator == 0)
4968 st->st_cur->ste_generator =
4969 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004970 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004971 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004972 symtable_node(st, CHILD(n, i + 1));
4973 symtable_node(st, CHILD(n, i + 3));
4974 }
4975 if (i + 2 < NCH(n))
4976 symtable_node(st, CHILD(n, i + 2));
4977 break;
4978 case global_stmt:
4979 symtable_global(st, n);
4980 break;
4981 case import_stmt:
4982 symtable_import(st, n);
4983 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00004984 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004985 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004986 symtable_node(st, CHILD(n, 1));
4987 if (NCH(n) > 2)
4988 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00004989 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004990 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00004991 st->st_cur->ste_opt_lineno = n->n_lineno;
4992 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004993 if (NCH(n) > 4)
4994 symtable_node(st, CHILD(n, 5));
4995 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00004996
4997 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00004998 case assert_stmt:
4999 if (Py_OptimizeFlag)
5000 return;
5001 if (NCH(n) == 2) {
5002 n = CHILD(n, 1);
5003 goto loop;
5004 } else {
5005 symtable_node(st, CHILD(n, 1));
5006 n = CHILD(n, 3);
5007 goto loop;
5008 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005009 case except_clause:
5010 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005011 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005012 if (NCH(n) > 1) {
5013 n = CHILD(n, 1);
5014 goto loop;
5015 }
5016 break;
5017 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005018 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005019 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005020 case yield_stmt:
5021 st->st_cur->ste_generator = 1;
5022 n = CHILD(n, 1);
5023 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005024 case expr_stmt:
5025 if (NCH(n) == 1)
5026 n = CHILD(n, 0);
5027 else {
5028 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005029 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005030 symtable_node(st, CHILD(n, 2));
5031 break;
5032 } else {
5033 int i;
5034 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005035 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005036 n = CHILD(n, NCH(n) - 1);
5037 }
5038 }
5039 goto loop;
5040 /* watchout for fall-through logic below */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005041 case argument:
5042 if (NCH(n) == 3) {
5043 n = CHILD(n, 2);
5044 goto loop;
5045 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005046 case listmaker:
5047 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005048 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005049 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005050 symtable_node(st, CHILD(n, 0));
5051 st->st_tmpname--;
5052 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005053 }
5054 case atom:
5055 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5056 symtable_add_use(st, STR(CHILD(n, 0)));
5057 break;
5058 }
5059 case for_stmt:
5060 if (TYPE(n) == for_stmt) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005061 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005062 start = 3;
5063 }
5064 default:
5065 if (NCH(n) == 1) {
5066 n = CHILD(n, 0);
5067 goto loop;
5068 }
5069 for (i = start; i < NCH(n); ++i)
5070 if (TYPE(CHILD(n, i)) >= single_input)
5071 symtable_node(st, CHILD(n, i));
5072 }
5073}
5074
5075static void
5076symtable_funcdef(struct symtable *st, node *n)
5077{
5078 node *body;
5079
5080 if (TYPE(n) == lambdef) {
5081 if (NCH(n) == 4)
5082 symtable_params(st, CHILD(n, 1));
5083 } else
5084 symtable_params(st, CHILD(n, 2));
5085 body = CHILD(n, NCH(n) - 1);
5086 symtable_node(st, body);
5087}
5088
5089/* The next two functions parse the argument tuple.
5090 symtable_default_arg() checks for names in the default arguments,
5091 which are references in the defining scope. symtable_params()
5092 parses the parameter names, which are defined in the function's
5093 body.
5094
5095 varargslist:
5096 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5097 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5098*/
5099
5100static void
5101symtable_default_args(struct symtable *st, node *n)
5102{
5103 node *c;
5104 int i;
5105
5106 if (TYPE(n) == parameters) {
5107 n = CHILD(n, 1);
5108 if (TYPE(n) == RPAR)
5109 return;
5110 }
5111 REQ(n, varargslist);
5112 for (i = 0; i < NCH(n); i += 2) {
5113 c = CHILD(n, i);
5114 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5115 break;
5116 }
5117 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5118 symtable_node(st, CHILD(n, i));
5119 }
5120}
5121
5122static void
5123symtable_params(struct symtable *st, node *n)
5124{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005125 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005126 node *c = NULL;
5127
5128 if (TYPE(n) == parameters) {
5129 n = CHILD(n, 1);
5130 if (TYPE(n) == RPAR)
5131 return;
5132 }
5133 REQ(n, varargslist);
5134 for (i = 0; i < NCH(n); i += 2) {
5135 c = CHILD(n, i);
5136 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5137 ext = 1;
5138 break;
5139 }
5140 if (TYPE(c) == test) {
5141 continue;
5142 }
5143 if (TYPE(CHILD(c, 0)) == NAME)
5144 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5145 else {
5146 char nbuf[10];
5147 sprintf(nbuf, ".%d", i);
5148 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005149 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005150 }
5151 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005152 if (ext) {
5153 c = CHILD(n, i);
5154 if (TYPE(c) == STAR) {
5155 i++;
5156 symtable_add_def(st, STR(CHILD(n, i)),
5157 DEF_PARAM | DEF_STAR);
5158 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005159 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005160 c = NULL;
5161 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005162 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005163 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005164 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005165 i++;
5166 symtable_add_def(st, STR(CHILD(n, i)),
5167 DEF_PARAM | DEF_DOUBLESTAR);
5168 }
5169 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005170 if (complex >= 0) {
5171 int j;
5172 for (j = 0; j <= complex; j++) {
5173 c = CHILD(n, j);
5174 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005175 c = CHILD(n, ++j);
5176 else if (TYPE(c) == EQUAL)
5177 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005178 if (TYPE(CHILD(c, 0)) == LPAR)
5179 symtable_params_fplist(st, CHILD(c, 1));
5180 }
5181 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005182}
5183
5184static void
5185symtable_params_fplist(struct symtable *st, node *n)
5186{
5187 int i;
5188 node *c;
5189
5190 REQ(n, fplist);
5191 for (i = 0; i < NCH(n); i += 2) {
5192 c = CHILD(n, i);
5193 REQ(c, fpdef);
5194 if (NCH(c) == 1)
5195 symtable_add_def(st, STR(CHILD(c, 0)),
5196 DEF_PARAM | DEF_INTUPLE);
5197 else
5198 symtable_params_fplist(st, CHILD(c, 1));
5199 }
5200
5201}
5202
5203static void
5204symtable_global(struct symtable *st, node *n)
5205{
5206 int i;
5207
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005208 /* XXX It might be helpful to warn about module-level global
5209 statements, but it's hard to tell the difference between
5210 module-level and a string passed to exec.
5211 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005212
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005213 for (i = 1; i < NCH(n); i += 2) {
5214 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005215 int flags;
5216
5217 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005218 if (flags < 0)
5219 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005220 if (flags && flags != DEF_GLOBAL) {
5221 char buf[500];
5222 if (flags & DEF_PARAM) {
5223 PyErr_Format(PyExc_SyntaxError,
5224 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005225 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005226 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005227 st->st_cur->ste_lineno);
5228 st->st_errors++;
5229 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005230 }
5231 else {
5232 if (flags & DEF_LOCAL)
5233 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5234 name);
5235 else
5236 sprintf(buf, GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005237 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005238 }
5239 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005240 symtable_add_def(st, name, DEF_GLOBAL);
5241 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005242}
5243
5244static void
5245symtable_list_comprehension(struct symtable *st, node *n)
5246{
5247 char tmpname[12];
5248
Jeremy Hylton23b42272001-03-19 20:38:06 +00005249 sprintf(tmpname, "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005250 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005251 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005252 symtable_node(st, CHILD(n, 3));
5253 if (NCH(n) == 5)
5254 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005255}
5256
5257static void
5258symtable_import(struct symtable *st, node *n)
5259{
5260 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005261 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005262 | 'from' dotted_name 'import'
5263 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005264 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005265 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005266 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005267 node *dotname = CHILD(n, 1);
5268 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5269 /* check for bogus imports */
5270 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5271 PyErr_SetString(PyExc_SyntaxError,
5272 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005273 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005274 n->n_lineno);
5275 st->st_errors++;
5276 return;
5277 }
5278 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005279 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005280 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005281 if (symtable_warn(st,
5282 "import * only allowed at module level") < 0)
5283 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005284 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005285 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005286 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005287 } else {
5288 for (i = 3; i < NCH(n); i += 2) {
5289 node *c = CHILD(n, i);
5290 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005291 symtable_assign(st, CHILD(c, 2),
5292 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005293 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005294 symtable_assign(st, CHILD(c, 0),
5295 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005296 }
5297 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005298 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005299 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005300 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005301 }
5302 }
5303}
5304
5305static void
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005306symtable_assign(struct symtable *st, node *n, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005307{
5308 node *tmp;
5309 int i;
5310
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005311 loop:
5312 switch (TYPE(n)) {
5313 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005314 /* invalid assignment, e.g. lambda x:x=2. The next
5315 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005316 return;
5317 case power:
5318 if (NCH(n) > 2) {
5319 for (i = 2; i < NCH(n); ++i)
5320 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5321 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005322 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005323 if (NCH(n) > 1) {
5324 symtable_node(st, CHILD(n, 0));
5325 symtable_node(st, CHILD(n, 1));
5326 } else {
5327 n = CHILD(n, 0);
5328 goto loop;
5329 }
5330 return;
5331 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005332 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5333 /* XXX This is an error, but the next pass
5334 will catch it. */
5335 return;
5336 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005337 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005338 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005339 }
5340 return;
5341 case exprlist:
5342 case testlist:
5343 if (NCH(n) == 1) {
5344 n = CHILD(n, 0);
5345 goto loop;
5346 }
5347 else {
5348 int i;
5349 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005350 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005351 return;
5352 }
5353 goto loop;
5354 case atom:
5355 tmp = CHILD(n, 0);
5356 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5357 n = CHILD(n, 1);
5358 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005359 } else if (TYPE(tmp) == NAME) {
Jeremy Hyltona830b382001-04-09 16:07:59 +00005360 if (strcmp(STR(tmp), "__debug__") == 0)
5361 symtable_warn(st, ASSIGN_DEBUG);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005362 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005363 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005364 return;
5365 case dotted_as_name:
5366 if (NCH(n) == 3)
5367 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005368 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005369 else
5370 symtable_add_def(st,
5371 STR(CHILD(CHILD(n,
5372 0), 0)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005373 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005374 return;
5375 case dotted_name:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005376 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005377 return;
5378 case NAME:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005379 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005380 return;
5381 default:
5382 if (NCH(n) == 0)
5383 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005384 if (NCH(n) == 1) {
5385 n = CHILD(n, 0);
5386 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005387 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005388 /* Should only occur for errors like x + 1 = 1,
5389 which will be caught in the next pass. */
5390 for (i = 0; i < NCH(n); ++i)
5391 if (TYPE(CHILD(n, i)) >= single_input)
5392 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005393 }
5394}