blob: 5e150c917df9cd5dbdc4e90f4b1e85c3b07b6033 [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
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001843com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001844{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001845 REQ(n, factor);
1846 if (TYPE(CHILD(n, 0)) == PLUS) {
1847 com_factor(c, CHILD(n, 1));
1848 com_addbyte(c, UNARY_POSITIVE);
1849 }
1850 else if (TYPE(CHILD(n, 0)) == MINUS) {
1851 com_factor(c, CHILD(n, 1));
1852 com_addbyte(c, UNARY_NEGATIVE);
1853 }
Guido van Rossum7928cd71991-10-24 14:59:31 +00001854 else if (TYPE(CHILD(n, 0)) == TILDE) {
1855 com_factor(c, CHILD(n, 1));
1856 com_addbyte(c, UNARY_INVERT);
1857 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001858 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001859 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860 }
1861}
1862
1863static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001864com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001865{
1866 int i;
1867 int op;
1868 REQ(n, term);
1869 com_factor(c, CHILD(n, 0));
1870 for (i = 2; i < NCH(n); i += 2) {
1871 com_factor(c, CHILD(n, i));
1872 switch (TYPE(CHILD(n, i-1))) {
1873 case STAR:
1874 op = BINARY_MULTIPLY;
1875 break;
1876 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00001877 if (c->c_flags & CO_FUTURE_DIVISION)
1878 op = BINARY_TRUE_DIVIDE;
1879 else
1880 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001881 break;
1882 case PERCENT:
1883 op = BINARY_MODULO;
1884 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001885 case DOUBLESLASH:
1886 op = BINARY_FLOOR_DIVIDE;
1887 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001888 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001889 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00001890 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001891 op = 255;
1892 }
1893 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001894 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001895 }
1896}
1897
1898static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001899com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001900{
1901 int i;
1902 int op;
1903 REQ(n, arith_expr);
1904 com_term(c, CHILD(n, 0));
1905 for (i = 2; i < NCH(n); i += 2) {
1906 com_term(c, CHILD(n, i));
1907 switch (TYPE(CHILD(n, i-1))) {
1908 case PLUS:
1909 op = BINARY_ADD;
1910 break;
1911 case MINUS:
1912 op = BINARY_SUBTRACT;
1913 break;
1914 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001915 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001916 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001917 op = 255;
1918 }
1919 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001920 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001921 }
1922}
1923
1924static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001925com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001926{
1927 int i;
1928 int op;
1929 REQ(n, shift_expr);
1930 com_arith_expr(c, CHILD(n, 0));
1931 for (i = 2; i < NCH(n); i += 2) {
1932 com_arith_expr(c, CHILD(n, i));
1933 switch (TYPE(CHILD(n, i-1))) {
1934 case LEFTSHIFT:
1935 op = BINARY_LSHIFT;
1936 break;
1937 case RIGHTSHIFT:
1938 op = BINARY_RSHIFT;
1939 break;
1940 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001941 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001942 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001943 op = 255;
1944 }
1945 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001946 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001947 }
1948}
1949
1950static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001951com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001952{
1953 int i;
1954 int op;
1955 REQ(n, and_expr);
1956 com_shift_expr(c, CHILD(n, 0));
1957 for (i = 2; i < NCH(n); i += 2) {
1958 com_shift_expr(c, CHILD(n, i));
1959 if (TYPE(CHILD(n, i-1)) == AMPER) {
1960 op = BINARY_AND;
1961 }
1962 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001963 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001964 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00001965 op = 255;
1966 }
1967 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001968 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00001969 }
1970}
1971
1972static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001973com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001974{
1975 int i;
1976 int op;
1977 REQ(n, xor_expr);
1978 com_and_expr(c, CHILD(n, 0));
1979 for (i = 2; i < NCH(n); i += 2) {
1980 com_and_expr(c, CHILD(n, i));
1981 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
1982 op = BINARY_XOR;
1983 }
1984 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001985 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001986 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001987 op = 255;
1988 }
1989 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001990 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001991 }
1992}
1993
1994static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001995com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001996{
1997 int i;
1998 int op;
1999 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002000 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002001 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002002 com_xor_expr(c, CHILD(n, i));
2003 if (TYPE(CHILD(n, i-1)) == VBAR) {
2004 op = BINARY_OR;
2005 }
2006 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002007 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002008 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002009 op = 255;
2010 }
2011 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002012 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002013 }
2014}
2015
2016static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002017cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002018{
2019 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002020 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002021 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2022 if (NCH(n) == 1) {
2023 n = CHILD(n, 0);
2024 switch (TYPE(n)) {
2025 case LESS: return LT;
2026 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002027 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002028 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002029 case LESSEQUAL: return LE;
2030 case GREATEREQUAL: return GE;
2031 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002032 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2033 if (strcmp(STR(n), "is") == 0) return IS;
2034 }
2035 }
2036 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002037 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002038 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2039 return NOT_IN;
2040 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2041 return IS_NOT;
2042 }
2043 }
2044 return BAD;
2045}
2046
2047static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002048com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002049{
2050 int i;
2051 enum cmp_op op;
2052 int anchor;
2053 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2054 com_expr(c, CHILD(n, 0));
2055 if (NCH(n) == 1)
2056 return;
2057
2058 /****************************************************************
2059 The following code is generated for all but the last
2060 comparison in a chain:
2061
2062 label: on stack: opcode: jump to:
2063
2064 a <code to load b>
2065 a, b DUP_TOP
2066 a, b, b ROT_THREE
2067 b, a, b COMPARE_OP
2068 b, 0-or-1 JUMP_IF_FALSE L1
2069 b, 1 POP_TOP
2070 b
2071
2072 We are now ready to repeat this sequence for the next
2073 comparison in the chain.
2074
2075 For the last we generate:
2076
2077 b <code to load c>
2078 b, c COMPARE_OP
2079 0-or-1
2080
2081 If there were any jumps to L1 (i.e., there was more than one
2082 comparison), we generate:
2083
2084 0-or-1 JUMP_FORWARD L2
2085 L1: b, 0 ROT_TWO
2086 0, b POP_TOP
2087 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002088 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002089 ****************************************************************/
2090
2091 anchor = 0;
2092
2093 for (i = 2; i < NCH(n); i += 2) {
2094 com_expr(c, CHILD(n, i));
2095 if (i+2 < NCH(n)) {
2096 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002097 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002098 com_addbyte(c, ROT_THREE);
2099 }
2100 op = cmp_type(CHILD(n, i-1));
2101 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002102 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002103 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002104 }
2105 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002106 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002107 if (i+2 < NCH(n)) {
2108 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2109 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002110 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002111 }
2112 }
2113
2114 if (anchor) {
2115 int anchor2 = 0;
2116 com_addfwref(c, JUMP_FORWARD, &anchor2);
2117 com_backpatch(c, anchor);
2118 com_addbyte(c, ROT_TWO);
2119 com_addbyte(c, POP_TOP);
2120 com_backpatch(c, anchor2);
2121 }
2122}
2123
2124static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002125com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002126{
2127 REQ(n, not_test); /* 'not' not_test | comparison */
2128 if (NCH(n) == 1) {
2129 com_comparison(c, CHILD(n, 0));
2130 }
2131 else {
2132 com_not_test(c, CHILD(n, 1));
2133 com_addbyte(c, UNARY_NOT);
2134 }
2135}
2136
2137static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002138com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002139{
2140 int i;
2141 int anchor;
2142 REQ(n, and_test); /* not_test ('and' not_test)* */
2143 anchor = 0;
2144 i = 0;
2145 for (;;) {
2146 com_not_test(c, CHILD(n, i));
2147 if ((i += 2) >= NCH(n))
2148 break;
2149 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2150 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002151 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002152 }
2153 if (anchor)
2154 com_backpatch(c, anchor);
2155}
2156
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002157static int
2158com_make_closure(struct compiling *c, PyCodeObject *co)
2159{
2160 int i, free = PyTuple_GET_SIZE(co->co_freevars);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002161 /* If the code is compiled with st->st_nested_scopes == 0,
2162 then no variable will ever be added to co_freevars.
2163 */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002164 if (free == 0)
2165 return 0;
2166 for (i = 0; i < free; ++i) {
2167 /* Bypass com_addop_varname because it will generate
2168 LOAD_DEREF but LOAD_CLOSURE is needed.
2169 */
2170 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2171 int arg, reftype;
2172
2173 /* Special case: If a class contains a method with a
2174 free variable that has the same name as a method,
2175 the name will be considered free *and* local in the
2176 class. It should be handled by the closure, as
2177 well as by the normal name loookup logic.
2178 */
2179 reftype = get_ref_type(c, PyString_AS_STRING(name));
2180 if (reftype == CELL)
2181 arg = com_lookup_arg(c->c_cellvars, name);
2182 else /* (reftype == FREE) */
2183 arg = com_lookup_arg(c->c_freevars, name);
2184 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002185 fprintf(stderr, "lookup %s in %s %d %d\n"
2186 "freevars of %s: %s\n",
2187 PyObject_REPR(name),
2188 c->c_name,
2189 reftype, arg,
2190 PyString_AS_STRING(co->co_name),
2191 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002192 Py_FatalError("com_make_closure()");
2193 }
2194 com_addoparg(c, LOAD_CLOSURE, arg);
2195
2196 }
2197 com_push(c, free);
2198 return 1;
2199}
2200
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002201static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002202com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002203{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002204 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002205 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002206 PyObject *co;
2207 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002208 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002209 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2210 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002211 co = (PyObject *) icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002212 if (co == NULL) {
2213 c->c_errors++;
2214 return;
2215 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002216 symtable_exit_scope(c->c_symtable);
Tim Peters1e542112001-02-23 22:23:53 +00002217 i = com_addconst(c, co);
2218 closure = com_make_closure(c, (PyCodeObject *)co);
2219 Py_DECREF(co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002220 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002221 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002222 if (closure)
2223 com_addoparg(c, MAKE_CLOSURE, ndefs);
2224 else
2225 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002226 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002227 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002228 else {
2229 int anchor = 0;
2230 int i = 0;
2231 for (;;) {
2232 com_and_test(c, CHILD(n, i));
2233 if ((i += 2) >= NCH(n))
2234 break;
2235 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2236 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002237 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002238 }
2239 if (anchor)
2240 com_backpatch(c, anchor);
2241 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002242}
2243
2244static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002245com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002246{
2247 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002248 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002249 com_node(c, CHILD(n, 0));
2250 }
2251 else {
2252 int i;
2253 int len;
2254 len = (NCH(n) + 1) / 2;
2255 for (i = 0; i < NCH(n); i += 2)
2256 com_node(c, CHILD(n, i));
2257 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002258 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002259 }
2260}
2261
2262
2263/* Begin of assignment compilation */
2264
Thomas Wouters434d0822000-08-24 20:11:32 +00002265
2266static void
2267com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2268{
2269 com_addbyte(c, DUP_TOP);
2270 com_push(c, 1);
2271 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002272 com_node(c, augn);
2273 com_addbyte(c, opcode);
2274 com_pop(c, 1);
2275 com_addbyte(c, ROT_TWO);
2276 com_addopname(c, STORE_ATTR, n);
2277 com_pop(c, 2);
2278}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002279
2280static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002281com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002282{
2283 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002284 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002285}
2286
2287static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002288com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002289{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002290 REQ(n, trailer);
2291 switch (TYPE(CHILD(n, 0))) {
2292 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002293 com_error(c, PyExc_SyntaxError,
2294 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002295 break;
2296 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002297 if (assigning > OP_APPLY)
2298 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2299 else
2300 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002301 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002302 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002303 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002304 break;
2305 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002306 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002307 }
2308}
2309
2310static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002311com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002312{
2313 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002314 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002315 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002316 if (assigning) {
2317 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002318 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002319 com_push(c, i-1);
2320 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002321 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002322 com_assign(c, CHILD(n, i), assigning, NULL);
2323}
2324
2325static void
2326com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2327{
2328 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002329 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002330 com_push(c, 1);
2331 com_node(c, augn);
2332 com_addbyte(c, opcode);
2333 com_pop(c, 1);
2334 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002335}
2336
2337static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002338com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002339{
2340 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002341 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002342 if (assigning)
2343 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002344}
2345
2346static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002347com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002348{
2349 /* Loop to avoid trivial recursion */
2350 for (;;) {
2351 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002352
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002353 case exprlist:
2354 case testlist:
2355 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002356 if (assigning > OP_APPLY) {
2357 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002358 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002359 return;
2360 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002361 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002362 return;
2363 }
2364 n = CHILD(n, 0);
2365 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002366
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002367 case test:
2368 case and_test:
2369 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002370 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002371 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002372 case xor_expr:
2373 case and_expr:
2374 case shift_expr:
2375 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002376 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002377 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002378 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002379 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002380 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002381 return;
2382 }
2383 n = CHILD(n, 0);
2384 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002385
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002386 case power: /* atom trailer* ('**' power)*
2387 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002388 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002389 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002390 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002391 return;
2392 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002393 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002394 int i;
2395 com_node(c, CHILD(n, 0));
2396 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002397 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002398 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002399 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002400 return;
2401 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002402 com_apply_trailer(c, CHILD(n, i));
2403 } /* NB i is still alive */
2404 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002405 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002406 return;
2407 }
2408 n = CHILD(n, 0);
2409 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002410
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002411 case atom:
2412 switch (TYPE(CHILD(n, 0))) {
2413 case LPAR:
2414 n = CHILD(n, 1);
2415 if (TYPE(n) == RPAR) {
2416 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002417 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002418 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002419 return;
2420 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002421 if (assigning > OP_APPLY) {
2422 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002423 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002424 return;
2425 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002426 break;
2427 case LSQB:
2428 n = CHILD(n, 1);
2429 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002430 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002431 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002432 return;
2433 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002434 if (assigning > OP_APPLY) {
2435 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002436 "augmented assign to list not possible");
2437 return;
2438 }
2439 if (NCH(n) > 1
2440 && TYPE(CHILD(n, 1)) == list_for) {
2441 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002442 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002443 return;
2444 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002445 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002446 return;
2447 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002448 if (assigning > OP_APPLY)
2449 com_augassign_name(c, CHILD(n, 0),
2450 assigning, augn);
2451 else
2452 com_assign_name(c, CHILD(n, 0),
2453 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002454 return;
2455 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002456 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002457 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002458 return;
2459 }
2460 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002461
2462 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002463 com_error(c, PyExc_SyntaxError,
2464 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002465 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002466
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002467 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002468 com_error(c, PyExc_SystemError,
2469 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002470 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002471
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002472 }
2473 }
2474}
Guido van Rossum7c531111997-03-11 18:42:21 +00002475
Thomas Wouters434d0822000-08-24 20:11:32 +00002476static void
2477com_augassign(struct compiling *c, node *n)
2478{
2479 int opcode;
2480
2481 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2482 case '+': opcode = INPLACE_ADD; break;
2483 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002484 case '/':
2485 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2486 opcode = INPLACE_FLOOR_DIVIDE;
2487 else if (c->c_flags & CO_FUTURE_DIVISION)
2488 opcode = INPLACE_TRUE_DIVIDE;
2489 else
2490 opcode = INPLACE_DIVIDE;
2491 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002492 case '%': opcode = INPLACE_MODULO; break;
2493 case '<': opcode = INPLACE_LSHIFT; break;
2494 case '>': opcode = INPLACE_RSHIFT; break;
2495 case '&': opcode = INPLACE_AND; break;
2496 case '^': opcode = INPLACE_XOR; break;
2497 case '|': opcode = INPLACE_OR; break;
2498 case '*':
2499 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2500 opcode = INPLACE_POWER;
2501 else
2502 opcode = INPLACE_MULTIPLY;
2503 break;
2504 default:
2505 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2506 return;
2507 }
2508 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2509}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002510
2511static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002512com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002513{
Thomas Wouters434d0822000-08-24 20:11:32 +00002514 REQ(n, expr_stmt);
2515 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002516 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002517 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002518 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002519 if (NCH(n) == 1) {
2520 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002521 if (c->c_interactive)
2522 com_addbyte(c, PRINT_EXPR);
2523 else
2524 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002525 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002526 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002527 else if (TYPE(CHILD(n,1)) == augassign)
2528 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002529 else {
2530 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002531 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002532 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002533 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002534 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002535 com_push(c, 1);
2536 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002537 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002538 }
2539 }
2540}
2541
2542static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002543com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002544{
2545 int a = 0, b = 0;
2546 int i;
2547 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2548 /* Generate code like for
2549
2550 if __debug__:
2551 if not <test>:
2552 raise AssertionError [, <message>]
2553
2554 where <message> is the second test, if present.
2555 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002556
Guido van Rossum228d7f31997-04-02 05:24:36 +00002557 if (Py_OptimizeFlag)
2558 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002559 com_addop_name(c, LOAD_GLOBAL, "__debug__");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002560 com_push(c, 1);
2561 com_addfwref(c, JUMP_IF_FALSE, &a);
2562 com_addbyte(c, POP_TOP);
2563 com_pop(c, 1);
2564 com_node(c, CHILD(n, 1));
2565 com_addfwref(c, JUMP_IF_TRUE, &b);
2566 com_addbyte(c, POP_TOP);
2567 com_pop(c, 1);
2568 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002569 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002570 com_push(c, 1);
2571 i = NCH(n)/2; /* Either 2 or 4 */
2572 if (i > 1)
2573 com_node(c, CHILD(n, 3));
2574 com_addoparg(c, RAISE_VARARGS, i);
2575 com_pop(c, i);
2576 /* The interpreter does not fall through */
2577 /* All jumps converge here */
2578 com_backpatch(c, a);
2579 com_backpatch(c, b);
2580 com_addbyte(c, POP_TOP);
2581}
2582
2583static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002584com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002585{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002586 int i = 1;
2587 node* stream = NULL;
2588
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002589 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002590
2591 /* are we using the extended print form? */
2592 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2593 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002594 com_node(c, stream);
2595 /* stack: [...] => [... stream] */
2596 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002597 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2598 i = 4;
2599 else
2600 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002601 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002602 for (; i < NCH(n); i += 2) {
2603 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002604 com_addbyte(c, DUP_TOP);
2605 /* stack: [stream] => [stream stream] */
2606 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002607 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002608 /* stack: [stream stream] => [stream stream obj] */
2609 com_addbyte(c, ROT_TWO);
2610 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002611 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002612 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002613 com_pop(c, 2);
2614 }
2615 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002616 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002617 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002618 com_addbyte(c, PRINT_ITEM);
2619 com_pop(c, 1);
2620 }
2621 }
2622 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002623 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002624 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002625 /* must pop the extra stream object off the stack */
2626 com_addbyte(c, POP_TOP);
2627 /* stack: [... stream] => [...] */
2628 com_pop(c, 1);
2629 }
2630 }
2631 else {
2632 if (stream != NULL) {
2633 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002634 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002635 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002636 com_pop(c, 1);
2637 }
2638 else
2639 com_addbyte(c, PRINT_NEWLINE);
2640 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002641}
2642
2643static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002644com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002645{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002646 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002647 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002648 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002649 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002650 if (c->c_flags & CO_GENERATOR) {
2651 if (NCH(n) > 1) {
2652 com_error(c, PyExc_SyntaxError,
2653 "'return' with argument inside generator");
2654 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002655 }
2656 if (NCH(n) < 2) {
2657 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002658 com_push(c, 1);
2659 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002660 else
2661 com_node(c, CHILD(n, 1));
2662 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002663 com_pop(c, 1);
2664}
2665
2666static void
2667com_yield_stmt(struct compiling *c, node *n)
2668{
Tim Peters95c80f82001-06-23 02:07:08 +00002669 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002670 REQ(n, yield_stmt); /* 'yield' testlist */
2671 if (!c->c_infunction) {
2672 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2673 }
Tim Peters95c80f82001-06-23 02:07:08 +00002674
2675 for (i = 0; i < c->c_nblocks; ++i) {
2676 if (c->c_block[i] == SETUP_FINALLY) {
2677 com_error(c, PyExc_SyntaxError,
2678 "'yield' not allowed in a 'try' block "
2679 "with a 'finally' clause");
2680 return;
2681 }
2682 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002683 com_node(c, CHILD(n, 1));
2684 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002685 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002686}
2687
2688static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002689com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002690{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002691 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002692 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2693 if (NCH(n) > 1) {
2694 com_node(c, CHILD(n, 1));
2695 if (NCH(n) > 3) {
2696 com_node(c, CHILD(n, 3));
2697 if (NCH(n) > 5)
2698 com_node(c, CHILD(n, 5));
2699 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002700 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002701 i = NCH(n)/2;
2702 com_addoparg(c, RAISE_VARARGS, i);
2703 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002704}
2705
2706static void
Thomas Wouters52152252000-08-17 22:55:00 +00002707com_from_import(struct compiling *c, node *n)
2708{
2709 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2710 com_push(c, 1);
2711 if (NCH(n) > 1) {
2712 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2713 com_error(c, PyExc_SyntaxError, "invalid syntax");
2714 return;
2715 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002716 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002717 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002718 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002719 com_pop(c, 1);
2720}
2721
2722static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002723com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002724{
2725 int i;
2726 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002727 /* 'import' dotted_name (',' dotted_name)* |
2728 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002729 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002730 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002731 /* 'from' dotted_name 'import' ... */
2732 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002733
2734 if (TYPE(CHILD(n, 3)) == STAR) {
2735 tup = Py_BuildValue("(s)", "*");
2736 } else {
2737 tup = PyTuple_New((NCH(n) - 2)/2);
2738 for (i = 3; i < NCH(n); i += 2) {
2739 PyTuple_SET_ITEM(tup, (i-3)/2,
2740 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00002741 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002742 }
2743 }
2744 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002745 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002746 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002747 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002748 if (TYPE(CHILD(n, 3)) == STAR)
2749 com_addbyte(c, IMPORT_STAR);
2750 else {
2751 for (i = 3; i < NCH(n); i += 2)
2752 com_from_import(c, CHILD(n, i));
2753 com_addbyte(c, POP_TOP);
2754 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002755 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002756 }
2757 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002758 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002759 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002760 node *subn = CHILD(n, i);
2761 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002762 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002763 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002764 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002765 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002766 int j;
2767 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002768 com_error(c, PyExc_SyntaxError,
2769 "invalid syntax");
2770 return;
2771 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002772 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2773 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002774 CHILD(CHILD(subn, 0),
2775 j));
2776 com_addop_varname(c, VAR_STORE,
2777 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002778 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002779 com_addop_varname(c, VAR_STORE,
2780 STR(CHILD(CHILD(subn, 0),
2781 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002782 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002783 }
2784 }
2785}
2786
2787static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002788com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002789{
2790 REQ(n, exec_stmt);
2791 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2792 com_node(c, CHILD(n, 1));
2793 if (NCH(n) >= 4)
2794 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002795 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002796 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002797 com_push(c, 1);
2798 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002799 if (NCH(n) >= 6)
2800 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002801 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002802 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002803 com_push(c, 1);
2804 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002805 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002806 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002807}
2808
Guido van Rossum7c531111997-03-11 18:42:21 +00002809static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002810is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002811{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002812 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002813 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002814 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00002815
2816 /* Label to avoid tail recursion */
2817 next:
2818 switch (TYPE(n)) {
2819
2820 case suite:
2821 if (NCH(n) == 1) {
2822 n = CHILD(n, 0);
2823 goto next;
2824 }
2825 /* Fall through */
2826 case file_input:
2827 for (i = 0; i < NCH(n); i++) {
2828 node *ch = CHILD(n, i);
2829 if (TYPE(ch) == stmt) {
2830 n = ch;
2831 goto next;
2832 }
2833 }
2834 break;
2835
2836 case stmt:
2837 case simple_stmt:
2838 case small_stmt:
2839 n = CHILD(n, 0);
2840 goto next;
2841
2842 case expr_stmt:
2843 case testlist:
2844 case test:
2845 case and_test:
2846 case not_test:
2847 case comparison:
2848 case expr:
2849 case xor_expr:
2850 case and_expr:
2851 case shift_expr:
2852 case arith_expr:
2853 case term:
2854 case factor:
2855 case power:
2856 case atom:
2857 if (NCH(n) == 1) {
2858 n = CHILD(n, 0);
2859 goto next;
2860 }
2861 break;
2862
2863 case NAME:
2864 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2865 return 1;
2866 break;
2867
2868 case NUMBER:
2869 v = parsenumber(c, STR(n));
2870 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002871 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002872 break;
2873 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002874 i = PyObject_IsTrue(v);
2875 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002876 return i == 0;
2877
2878 case STRING:
Guido van Rossum6af0c001997-03-11 21:25:55 +00002879 v = parsestr(STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002880 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002881 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002882 break;
2883 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002884 i = PyObject_IsTrue(v);
2885 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002886 return i == 0;
2887
2888 }
2889 return 0;
2890}
2891
Tim Peters08a898f2001-06-28 01:52:22 +00002892
2893/* Look under n for a return stmt with an expression.
2894 * This hack is used to find illegal returns under "if 0:" blocks in
2895 * functions already known to be generators (as determined by the symtable
2896 * pass).
2897 * Return the offending return node if found, else NULL.
2898 */
2899static node *
2900look_for_offending_return(node *n)
2901{
2902 int i;
2903
2904 for (i = 0; i < NCH(n); ++i) {
2905 node *kid = CHILD(n, i);
2906
2907 switch (TYPE(kid)) {
2908 case classdef:
2909 case funcdef:
2910 case lambdef:
2911 /* Stuff in nested functions & classes doesn't
2912 affect the code block we started in. */
2913 return NULL;
2914
2915 case return_stmt:
2916 if (NCH(kid) > 1)
2917 return kid;
2918 break;
2919
2920 default: {
2921 node *bad = look_for_offending_return(kid);
2922 if (bad != NULL)
2923 return bad;
2924 }
2925 }
2926 }
2927
2928 return NULL;
2929}
2930
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002931static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002932com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002933{
2934 int i;
2935 int anchor = 0;
2936 REQ(n, if_stmt);
2937 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
2938 for (i = 0; i+3 < NCH(n); i+=4) {
2939 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002940 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00002941 if (is_constant_false(c, ch)) {
2942 /* We're going to skip this block. However, if this
2943 is a generator, we have to check the dead code
2944 anyway to make sure there aren't any return stmts
2945 with expressions, in the same scope. */
2946 if (c->c_flags & CO_GENERATOR) {
2947 node *p = look_for_offending_return(n);
2948 if (p != NULL) {
2949 int savelineno = c->c_lineno;
2950 c->c_lineno = p->n_lineno;
2951 com_error(c, PyExc_SyntaxError,
2952 "'return' with argument "
2953 "inside generator");
2954 c->c_lineno = savelineno;
2955 }
2956 }
Guido van Rossum7c531111997-03-11 18:42:21 +00002957 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00002958 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00002959 if (i > 0)
2960 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00002961 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002962 com_addfwref(c, JUMP_IF_FALSE, &a);
2963 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002964 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002965 com_node(c, CHILD(n, i+3));
2966 com_addfwref(c, JUMP_FORWARD, &anchor);
2967 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002968 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002969 com_addbyte(c, POP_TOP);
2970 }
2971 if (i+2 < NCH(n))
2972 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00002973 if (anchor)
2974 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002975}
2976
2977static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002978com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002979{
2980 int break_anchor = 0;
2981 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002982 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002983 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
2984 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002985 block_push(c, SETUP_LOOP);
2986 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002987 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002988 com_node(c, CHILD(n, 1));
2989 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2990 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002991 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002992 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002993 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00002994 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002995 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
2996 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002997 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002998 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002999 com_addbyte(c, POP_TOP);
3000 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003001 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003002 if (NCH(n) > 4)
3003 com_node(c, CHILD(n, 6));
3004 com_backpatch(c, break_anchor);
3005}
3006
3007static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003008com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003009{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003010 int break_anchor = 0;
3011 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003012 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003013 REQ(n, for_stmt);
3014 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3015 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003016 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003017 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003018 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003019 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003020 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003021 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003022 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003023 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003024 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003025 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003026 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003027 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3028 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003029 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003030 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003031 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003032 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003033 if (NCH(n) > 8)
3034 com_node(c, CHILD(n, 8));
3035 com_backpatch(c, break_anchor);
3036}
3037
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003038/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003039
3040 SETUP_FINALLY L
3041 <code for S>
3042 POP_BLOCK
3043 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003044 L: <code for Sf>
3045 END_FINALLY
3046
3047 The special instructions use the block stack. Each block
3048 stack entry contains the instruction that created it (here
3049 SETUP_FINALLY), the level of the value stack at the time the
3050 block stack entry was created, and a label (here L).
3051
3052 SETUP_FINALLY:
3053 Pushes the current value stack level and the label
3054 onto the block stack.
3055 POP_BLOCK:
3056 Pops en entry from the block stack, and pops the value
3057 stack until its level is the same as indicated on the
3058 block stack. (The label is ignored.)
3059 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003060 Pops a variable number of entries from the *value* stack
3061 and re-raises the exception they specify. The number of
3062 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003063
3064 The block stack is unwound when an exception is raised:
3065 when a SETUP_FINALLY entry is found, the exception is pushed
3066 onto the value stack (and the exception condition is cleared),
3067 and the interpreter jumps to the label gotten from the block
3068 stack.
3069
3070 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003071 (The contents of the value stack is shown in [], with the top
3072 at the right; 'tb' is trace-back info, 'val' the exception's
3073 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003074
3075 Value stack Label Instruction Argument
3076 [] SETUP_EXCEPT L1
3077 [] <code for S>
3078 [] POP_BLOCK
3079 [] JUMP_FORWARD L0
3080
Guido van Rossum3f5da241990-12-20 15:06:42 +00003081 [tb, val, exc] L1: DUP )
3082 [tb, val, exc, exc] <evaluate E1> )
3083 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3084 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3085 [tb, val, exc, 1] POP )
3086 [tb, val, exc] POP
3087 [tb, val] <assign to V1> (or POP if no V1)
3088 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003089 [] <code for S1>
3090 JUMP_FORWARD L0
3091
Guido van Rossum3f5da241990-12-20 15:06:42 +00003092 [tb, val, exc, 0] L2: POP
3093 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003094 .............................etc.......................
3095
Guido van Rossum3f5da241990-12-20 15:06:42 +00003096 [tb, val, exc, 0] Ln+1: POP
3097 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003098
3099 [] L0: <next statement>
3100
3101 Of course, parts are not generated if Vi or Ei is not present.
3102*/
3103
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003104static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003105com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003106{
3107 int except_anchor = 0;
3108 int end_anchor = 0;
3109 int else_anchor = 0;
3110 int i;
3111 node *ch;
3112
3113 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3114 block_push(c, SETUP_EXCEPT);
3115 com_node(c, CHILD(n, 2));
3116 com_addbyte(c, POP_BLOCK);
3117 block_pop(c, SETUP_EXCEPT);
3118 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3119 com_backpatch(c, except_anchor);
3120 for (i = 3;
3121 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3122 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003123 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003124 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003125 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003126 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003127 break;
3128 }
3129 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003130 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003131 com_addoparg(c, SET_LINENO, ch->n_lineno);
3132 if (NCH(ch) > 1) {
3133 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003134 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003135 com_node(c, CHILD(ch, 1));
3136 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003137 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003138 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3139 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003140 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003141 }
3142 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003143 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003144 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003145 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003146 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003147 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003148 com_pop(c, 1);
3149 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003150 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003151 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003152 com_node(c, CHILD(n, i+2));
3153 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3154 if (except_anchor) {
3155 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003156 /* We come in with [tb, val, exc, 0] on the
3157 stack; one pop and it's the same as
3158 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003159 com_addbyte(c, POP_TOP);
3160 }
3161 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003162 /* We actually come in here with [tb, val, exc] but the
3163 END_FINALLY will zap those and jump around.
3164 The c_stacklevel does not reflect them so we need not pop
3165 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003166 com_addbyte(c, END_FINALLY);
3167 com_backpatch(c, else_anchor);
3168 if (i < NCH(n))
3169 com_node(c, CHILD(n, i+2));
3170 com_backpatch(c, end_anchor);
3171}
3172
3173static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003174com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003175{
3176 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003177 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003178
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003179 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3180 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003181 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003182 com_addbyte(c, POP_BLOCK);
3183 block_pop(c, SETUP_FINALLY);
3184 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003185 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003186 /* While the generated code pushes only one item,
3187 the try-finally handling can enter here with
3188 up to three items. OK, here are the details:
3189 3 for an exception, 2 for RETURN, 1 for BREAK. */
3190 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003191 com_backpatch(c, finally_anchor);
3192 ch = CHILD(n, NCH(n)-1);
3193 com_addoparg(c, SET_LINENO, ch->n_lineno);
3194 com_node(c, ch);
3195 com_addbyte(c, END_FINALLY);
3196 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003197 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003198}
3199
3200static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003201com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003202{
3203 REQ(n, try_stmt);
3204 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3205 | 'try' ':' suite 'finally' ':' suite */
3206 if (TYPE(CHILD(n, 3)) != except_clause)
3207 com_try_finally(c, n);
3208 else
3209 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003210}
3211
Guido van Rossum8b993a91997-01-17 21:04:03 +00003212static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003213get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003214{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003215 int i;
3216
Guido van Rossum8b993a91997-01-17 21:04:03 +00003217 /* Label to avoid tail recursion */
3218 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003219 switch (TYPE(n)) {
3220
3221 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003222 if (NCH(n) == 1) {
3223 n = CHILD(n, 0);
3224 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003225 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003226 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003227 case file_input:
3228 for (i = 0; i < NCH(n); i++) {
3229 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003230 if (TYPE(ch) == stmt) {
3231 n = ch;
3232 goto next;
3233 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003234 }
3235 break;
3236
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003237 case stmt:
3238 case simple_stmt:
3239 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003240 n = CHILD(n, 0);
3241 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003242
3243 case expr_stmt:
3244 case testlist:
3245 case test:
3246 case and_test:
3247 case not_test:
3248 case comparison:
3249 case expr:
3250 case xor_expr:
3251 case and_expr:
3252 case shift_expr:
3253 case arith_expr:
3254 case term:
3255 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003256 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003257 if (NCH(n) == 1) {
3258 n = CHILD(n, 0);
3259 goto next;
3260 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003261 break;
3262
3263 case atom:
3264 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003265 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003266 break;
3267
3268 }
3269 return NULL;
3270}
3271
Guido van Rossum79f25d91997-04-29 20:08:16 +00003272static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003273get_docstring(node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003274{
Guido van Rossum541563e1999-01-28 15:08:09 +00003275 /* Don't generate doc-strings if run with -OO */
3276 if (Py_OptimizeFlag > 1)
3277 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003278 n = get_rawdocstring(n);
3279 if (n == NULL)
3280 return NULL;
3281 return parsestrplus(n);
3282}
3283
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003284static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003285com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003286{
3287 REQ(n, suite);
3288 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3289 if (NCH(n) == 1) {
3290 com_node(c, CHILD(n, 0));
3291 }
3292 else {
3293 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003294 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003295 node *ch = CHILD(n, i);
3296 if (TYPE(ch) == stmt)
3297 com_node(c, ch);
3298 }
3299 }
3300}
3301
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003302/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003303static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003304com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003305{
3306 int i = c->c_nblocks;
3307 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3308 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3309 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003310 else if (i <= 0) {
3311 /* at the outer level */
3312 com_error(c, PyExc_SyntaxError,
3313 "'continue' not properly in loop");
3314 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003315 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003316 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003317 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003318 if (c->c_block[j] == SETUP_LOOP)
3319 break;
3320 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003321 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003322 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003323 for (; i > j; --i) {
3324 if (c->c_block[i] == SETUP_EXCEPT ||
3325 c->c_block[i] == SETUP_FINALLY) {
3326 com_addoparg(c, CONTINUE_LOOP,
3327 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003328 return;
3329 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003330 if (c->c_block[i] == END_FINALLY) {
3331 com_error(c, PyExc_SyntaxError,
3332 "'continue' not supported inside 'finally' clause");
3333 return;
3334 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003335 }
3336 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003337 com_error(c, PyExc_SyntaxError,
3338 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003339 }
3340 /* XXX Could allow it inside a 'finally' clause
3341 XXX if we could pop the exception still on the stack */
3342}
3343
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003344static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003345com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003346{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003347 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003348 if (TYPE(n) == lambdef) {
3349 /* lambdef: 'lambda' [varargslist] ':' test */
3350 n = CHILD(n, 1);
3351 }
3352 else {
3353 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3354 n = CHILD(n, 2);
3355 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3356 n = CHILD(n, 1);
3357 }
3358 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003359 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003360 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003361 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003362 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3363 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003364 nargs = 0;
3365 ndefs = 0;
3366 for (i = 0; i < nch; i++) {
3367 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003368 if (TYPE(CHILD(n, i)) == STAR ||
3369 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003370 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003371 nargs++;
3372 i++;
3373 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003374 t = RPAR; /* Anything except EQUAL or COMMA */
3375 else
3376 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003377 if (t == EQUAL) {
3378 i++;
3379 ndefs++;
3380 com_node(c, CHILD(n, i));
3381 i++;
3382 if (i >= nch)
3383 break;
3384 t = TYPE(CHILD(n, i));
3385 }
3386 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003387 /* Treat "(a=1, b)" as an error */
3388 if (ndefs)
3389 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003390 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003391 }
3392 if (t != COMMA)
3393 break;
3394 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003395 return ndefs;
3396}
3397
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003398static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003399com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003400{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003401 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003402 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003403 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003404 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003405 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3406 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003407 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003408 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003409 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003410 c->c_errors++;
3411 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003412 int closure = com_make_closure(c, (PyCodeObject *)co);
3413 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003414 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003415 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003416 if (closure)
3417 com_addoparg(c, MAKE_CLOSURE, ndefs);
3418 else
3419 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003420 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003421 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003422 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003423 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003424 }
3425}
3426
3427static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003428com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003429{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003430 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003431 REQ(n, testlist);
3432 /* testlist: test (',' test)* [','] */
3433 for (i = 0; i < NCH(n); i += 2)
3434 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003435 i = (NCH(n)+1) / 2;
3436 com_addoparg(c, BUILD_TUPLE, i);
3437 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003438}
3439
3440static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003441com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003442{
Guido van Rossum25831651993-05-19 14:50:45 +00003443 int i;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003444 PyObject *co, *v;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003445 char *name;
3446
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003447 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003448 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003449 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003450 c->c_errors++;
3451 return;
3452 }
3453 /* Push the class name on the stack */
3454 i = com_addconst(c, v);
3455 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003456 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003457 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003458 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003459 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003460 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003461 com_push(c, 1);
3462 }
Guido van Rossum25831651993-05-19 14:50:45 +00003463 else
3464 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003465 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003466 symtable_enter_scope(c->c_symtable, name, TYPE(n), 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 Rossumd6f3bc21990-11-18 17:35:03 +00003470 c->c_errors++;
3471 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003472 int closure = com_make_closure(c, (PyCodeObject *)co);
3473 i = com_addconst(c, co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +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, 0);
3478 else
3479 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003480 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003481 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003482 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003483 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003484 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003485 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003486}
3487
3488static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003489com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003490{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003491 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003492 if (c->c_errors)
3493 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003494 switch (TYPE(n)) {
3495
3496 /* Definition nodes */
3497
3498 case funcdef:
3499 com_funcdef(c, n);
3500 break;
3501 case classdef:
3502 com_classdef(c, n);
3503 break;
3504
3505 /* Trivial parse tree nodes */
3506
3507 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003508 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003509 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003510 n = CHILD(n, 0);
3511 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003512
3513 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003514 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3515 com_addoparg(c, SET_LINENO, n->n_lineno);
3516 {
3517 int i;
3518 for (i = 0; i < NCH(n)-1; i += 2)
3519 com_node(c, CHILD(n, i));
3520 }
3521 break;
3522
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003523 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003524 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003525 n = CHILD(n, 0);
3526 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003527
3528 /* Statement nodes */
3529
3530 case expr_stmt:
3531 com_expr_stmt(c, n);
3532 break;
3533 case print_stmt:
3534 com_print_stmt(c, n);
3535 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003536 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003537 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003538 break;
3539 case pass_stmt:
3540 break;
3541 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003542 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003543 com_error(c, PyExc_SyntaxError,
3544 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003545 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003546 com_addbyte(c, BREAK_LOOP);
3547 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003548 case continue_stmt:
3549 com_continue_stmt(c, n);
3550 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003551 case return_stmt:
3552 com_return_stmt(c, n);
3553 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003554 case yield_stmt:
3555 com_yield_stmt(c, n);
3556 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003557 case raise_stmt:
3558 com_raise_stmt(c, n);
3559 break;
3560 case import_stmt:
3561 com_import_stmt(c, n);
3562 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003563 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003564 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003565 case exec_stmt:
3566 com_exec_stmt(c, n);
3567 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003568 case assert_stmt:
3569 com_assert_stmt(c, n);
3570 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003571 case if_stmt:
3572 com_if_stmt(c, n);
3573 break;
3574 case while_stmt:
3575 com_while_stmt(c, n);
3576 break;
3577 case for_stmt:
3578 com_for_stmt(c, n);
3579 break;
3580 case try_stmt:
3581 com_try_stmt(c, n);
3582 break;
3583 case suite:
3584 com_suite(c, n);
3585 break;
3586
3587 /* Expression nodes */
3588
3589 case testlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003590 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003591 break;
3592 case test:
3593 com_test(c, n);
3594 break;
3595 case and_test:
3596 com_and_test(c, n);
3597 break;
3598 case not_test:
3599 com_not_test(c, n);
3600 break;
3601 case comparison:
3602 com_comparison(c, n);
3603 break;
3604 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003605 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003606 break;
3607 case expr:
3608 com_expr(c, n);
3609 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003610 case xor_expr:
3611 com_xor_expr(c, n);
3612 break;
3613 case and_expr:
3614 com_and_expr(c, n);
3615 break;
3616 case shift_expr:
3617 com_shift_expr(c, n);
3618 break;
3619 case arith_expr:
3620 com_arith_expr(c, n);
3621 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003622 case term:
3623 com_term(c, n);
3624 break;
3625 case factor:
3626 com_factor(c, n);
3627 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003628 case power:
3629 com_power(c, n);
3630 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003631 case atom:
3632 com_atom(c, n);
3633 break;
3634
3635 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003636 com_error(c, PyExc_SystemError,
3637 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003638 }
3639}
3640
Tim Petersdbd9ba62000-07-09 03:09:57 +00003641static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003642
3643static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003644com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003645{
3646 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3647 if (TYPE(CHILD(n, 0)) == LPAR)
3648 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003649 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003650 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003651 com_pop(c, 1);
3652 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003653}
3654
3655static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003656com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003657{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003658 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003659 if (NCH(n) == 1) {
3660 com_fpdef(c, CHILD(n, 0));
3661 }
3662 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003663 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003664 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003665 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003666 for (i = 0; i < NCH(n); i += 2)
3667 com_fpdef(c, CHILD(n, i));
3668 }
3669}
3670
3671static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003673{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003674 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003675 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003676 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003677 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003678 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003679 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003680 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003681 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003682 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003683 node *ch = CHILD(n, i);
3684 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003685 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003686 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003687 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3688 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003689 if (TYPE(fp) != NAME) {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003690 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003691 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003692 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003693 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003694 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003695 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003696 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003697 ch = CHILD(n, i);
3698 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003699 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003700 else
3701 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003702 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003703 if (complex) {
3704 /* Generate code for complex arguments only after
3705 having counted the simple arguments */
3706 int ilocal = 0;
3707 for (i = 0; i < nch; i++) {
3708 node *ch = CHILD(n, i);
3709 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003710 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003711 break;
3712 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3713 fp = CHILD(ch, 0);
3714 if (TYPE(fp) != NAME) {
3715 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003716 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003717 com_fpdef(c, ch);
3718 }
3719 ilocal++;
3720 if (++i >= nch)
3721 break;
3722 ch = CHILD(n, i);
3723 if (TYPE(ch) == EQUAL)
3724 i += 2;
3725 else
3726 REQ(ch, COMMA);
3727 }
3728 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003729}
3730
3731static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003732com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003733{
3734 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003735 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003736 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003737 doc = get_docstring(n);
3738 if (doc != NULL) {
3739 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003740 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003741 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003742 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003743 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003744 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003745 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003746 for (i = 0; i < NCH(n); i++) {
3747 node *ch = CHILD(n, i);
3748 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3749 com_node(c, ch);
3750 }
3751}
3752
3753/* Top-level compile-node interface */
3754
3755static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003756compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003757{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003758 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003759 node *ch;
3760 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003761 c->c_name = STR(CHILD(n, 1));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003762 doc = get_docstring(CHILD(n, 4));
3763 if (doc != NULL) {
3764 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003765 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003766 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003767 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003768 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003769 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3770 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003771 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003772 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003773 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003774 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003775 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00003776 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3777 com_push(c, 1);
3778 com_addbyte(c, RETURN_VALUE);
3779 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003780}
3781
3782static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003783compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003784{
Guido van Rossum590baa41993-11-30 13:40:46 +00003785 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003786 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003787 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003788
3789 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003790 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003791 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003792 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003793 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003794 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003795 else
3796 ch = CHILD(n, 2);
3797 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003798 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003799 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003800}
3801
3802static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003803compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003804{
3805 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003806 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003807 REQ(n, classdef);
3808 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3809 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003810 c->c_private = c->c_name;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003811 ch = CHILD(n, NCH(n)-1); /* The suite */
3812 doc = get_docstring(ch);
3813 if (doc != NULL) {
3814 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003815 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003816 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003817 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003818 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003819 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003820 }
3821 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003822 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003823 com_node(c, ch);
3824 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003825 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003826 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003827 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003828}
3829
3830static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003831compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003832{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003833 com_addoparg(c, SET_LINENO, n->n_lineno);
3834
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003835 switch (TYPE(n)) {
3836
Guido van Rossum4c417781991-01-21 16:09:22 +00003837 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003838 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003839 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003840 n = CHILD(n, 0);
3841 if (TYPE(n) != NEWLINE)
3842 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003843 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003844 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003845 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003846 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003847 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003848 break;
3849
Guido van Rossum4c417781991-01-21 16:09:22 +00003850 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003852 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003853 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003854 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003855 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856 break;
3857
Guido van Rossum590baa41993-11-30 13:40:46 +00003858 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003859 com_node(c, CHILD(n, 0));
3860 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003861 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003862 break;
3863
Guido van Rossum590baa41993-11-30 13:40:46 +00003864 case lambdef: /* anonymous function definition */
3865 compile_lambdef(c, n);
3866 break;
3867
Guido van Rossum4c417781991-01-21 16:09:22 +00003868 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003869 compile_funcdef(c, n);
3870 break;
3871
Guido van Rossum4c417781991-01-21 16:09:22 +00003872 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003873 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003874 break;
3875
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003877 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003878 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003879 }
3880}
3881
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003882static PyObject *
3883dict_keys_inorder(PyObject *dict, int offset)
3884{
3885 PyObject *tuple, *k, *v;
3886 int i, pos = 0, size = PyDict_Size(dict);
3887
3888 tuple = PyTuple_New(size);
3889 if (tuple == NULL)
3890 return NULL;
3891 while (PyDict_Next(dict, &pos, &k, &v)) {
3892 i = PyInt_AS_LONG(v);
3893 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003894 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003895 PyTuple_SET_ITEM(tuple, i - offset, k);
3896 }
3897 return tuple;
3898}
3899
Guido van Rossum79f25d91997-04-29 20:08:16 +00003900PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003901PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003902{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003903 return PyNode_CompileFlags(n, filename, NULL);
3904}
3905
3906PyCodeObject *
3907PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
3908{
3909 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003910}
3911
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003912struct symtable *
3913PyNode_CompileSymtable(node *n, char *filename)
3914{
3915 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003916 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003917
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003918 ff = PyNode_Future(n, filename);
3919 if (ff == NULL)
3920 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003921 st = symtable_init();
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003922 if (st == NULL)
3923 return NULL;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003924 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003925 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003926 if (st->st_errors > 0)
3927 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003928 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003929 if (st->st_errors > 0)
3930 goto fail;
3931
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003932 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00003933 fail:
3934 PyMem_Free((void *)ff);
3935 st->st_future = NULL;
3936 PySymtable_Free(st);
3937 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003938}
3939
Guido van Rossum79f25d91997-04-29 20:08:16 +00003940static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003941icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003942{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003943 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003944}
3945
Guido van Rossum79f25d91997-04-29 20:08:16 +00003946static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003947jcompile(node *n, char *filename, struct compiling *base,
3948 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003949{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003950 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003951 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00003952 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003954 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003955 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003956 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003957 /* c_symtable still points to parent's symbols */
3958 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003959 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003960 sc.c_nested = 1;
Guido van Rossum4668b002001-08-08 05:00:18 +00003961 sc.c_flags |= base->c_flags & (CO_GENERATOR_ALLOWED |
3962 CO_FUTURE_DIVISION);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003963 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003964 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00003965 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003966 if (sc.c_future == NULL) {
3967 com_free(&sc);
3968 return NULL;
3969 }
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00003970 if (flags)
3971 sc.c_future->ff_features |= flags->cf_flags;
Jeremy Hylton9f324e92001-03-01 22:59:14 +00003972 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003973 com_free(&sc);
3974 return NULL;
3975 }
3976 }
3977 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003978 if (symtable_load_symbols(&sc) < 0) {
3979 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003980 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003981 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003982 compile_node(&sc, n);
3983 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00003984 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003985 PyObject *consts, *names, *varnames, *filename, *name,
3986 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003987 consts = PyList_AsTuple(sc.c_consts);
3988 names = PyList_AsTuple(sc.c_names);
3989 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003990 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
3991 freevars = dict_keys_inorder(sc.c_freevars,
3992 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003993 filename = PyString_InternFromString(sc.c_filename);
3994 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003995 if (!PyErr_Occurred())
3996 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003997 sc.c_nlocals,
3998 sc.c_maxstacklevel,
3999 sc.c_flags,
4000 sc.c_code,
4001 consts,
4002 names,
4003 varnames,
4004 freevars,
4005 cellvars,
4006 filename,
4007 name,
4008 sc.c_firstlineno,
4009 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004010 Py_XDECREF(consts);
4011 Py_XDECREF(names);
4012 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004013 Py_XDECREF(freevars);
4014 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004015 Py_XDECREF(filename);
4016 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004017 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004018 else if (!PyErr_Occurred()) {
4019 /* This could happen if someone called PyErr_Clear() after an
4020 error was reported above. That's not supposed to happen,
4021 but I just plugged one case and I'm not sure there can't be
4022 others. In that case, raise SystemError so that at least
4023 it gets reported instead dumping core. */
4024 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4025 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004026 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004027 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004028 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004029 sc.c_symtable = NULL;
4030 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004031 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004032 return co;
4033}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004034
4035int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004036PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004037{
4038 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004039 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004040 int line = co->co_firstlineno;
4041 int addr = 0;
4042 while (--size >= 0) {
4043 addr += *p++;
4044 if (addr > addrq)
4045 break;
4046 line += *p++;
4047 }
4048 return line;
4049}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004050
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004051/* The test for LOCAL must come before the test for FREE in order to
4052 handle classes where name is both local and free. The local var is
4053 a method and the free var is a free var referenced within a method.
4054*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055
4056static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004057get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004058{
4059 PyObject *v;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004060 if (c->c_symtable->st_nested_scopes) {
4061 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4062 return CELL;
4063 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4064 return LOCAL;
4065 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4066 return FREE;
4067 v = PyDict_GetItemString(c->c_globals, name);
4068 if (v) {
4069 if (v == Py_None)
4070 return GLOBAL_EXPLICIT;
4071 else {
4072 return GLOBAL_IMPLICIT;
4073 }
4074 }
4075 } else {
4076 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4077 return LOCAL;
4078 v = PyDict_GetItemString(c->c_globals, name);
4079 if (v) {
4080 if (v == Py_None)
4081 return GLOBAL_EXPLICIT;
4082 else {
4083 return GLOBAL_IMPLICIT;
4084 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085 }
4086 }
4087 {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004088 char buf[350];
4089 sprintf(buf,
Jeremy Hylton51257732001-03-01 00:42:55 +00004090 "unknown scope for %.100s in %.100s(%s) "
4091 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
Jeremy Hylton483638c2001-02-01 20:20:45 +00004092 name, c->c_name,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004093 PyObject_REPR(c->c_symtable->st_cur->ste_id),
Jeremy Hylton51257732001-03-01 00:42:55 +00004094 c->c_filename,
4095 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4096 PyObject_REPR(c->c_locals),
4097 PyObject_REPR(c->c_globals)
4098 );
4099
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004100 Py_FatalError(buf);
4101 }
4102 return -1; /* can't get here */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004103}
4104
Guido van Rossum207fda62001-03-02 03:30:41 +00004105/* Helper functions to issue warnings */
4106
4107static int
4108issue_warning(char *msg, char *filename, int lineno)
4109{
4110 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4111 lineno, NULL, NULL) < 0) {
4112 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4113 PyErr_SetString(PyExc_SyntaxError, msg);
4114 PyErr_SyntaxLocation(filename, lineno);
4115 }
4116 return -1;
4117 }
4118 return 0;
4119}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004120
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004121static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004122symtable_warn(struct symtable *st, char *msg)
4123{
Guido van Rossum207fda62001-03-02 03:30:41 +00004124 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004125 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004126 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004127 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004128 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004129}
4130
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004131/* Helper function for setting lineno and filename */
4132
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004133static int
4134symtable_build(struct compiling *c, node *n)
4135{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004136 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004137 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004138 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004139 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004140 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4141 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004142 return -1;
4143 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004144 if (c->c_symtable->st_errors > 0)
4145 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004146 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004147 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004148 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004149 return 0;
4150}
4151
4152static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004153symtable_init_compiling_symbols(struct compiling *c)
4154{
4155 PyObject *varnames;
4156
4157 varnames = c->c_symtable->st_cur->ste_varnames;
4158 if (varnames == NULL) {
4159 varnames = PyList_New(0);
4160 if (varnames == NULL)
4161 return -1;
4162 c->c_symtable->st_cur->ste_varnames = varnames;
4163 Py_INCREF(varnames);
4164 } else
4165 Py_INCREF(varnames);
4166 c->c_varnames = varnames;
4167
4168 c->c_globals = PyDict_New();
4169 if (c->c_globals == NULL)
4170 return -1;
4171 c->c_freevars = PyDict_New();
4172 if (c->c_freevars == NULL)
4173 return -1;
4174 c->c_cellvars = PyDict_New();
4175 if (c->c_cellvars == NULL)
4176 return -1;
4177 return 0;
4178}
4179
4180struct symbol_info {
4181 int si_nlocals;
4182 int si_ncells;
4183 int si_nfrees;
4184 int si_nimplicit;
4185};
4186
4187static void
4188symtable_init_info(struct symbol_info *si)
4189{
4190 si->si_nlocals = 0;
4191 si->si_ncells = 0;
4192 si->si_nfrees = 0;
4193 si->si_nimplicit = 0;
4194}
4195
4196static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004197symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004198 struct symbol_info *si)
4199{
4200 PyObject *dict, *v;
4201
4202 /* Seperate logic for DEF_FREE. If it occurs in a function,
4203 it indicates a local that we must allocate storage for (a
4204 cell var). If it occurs in a class, then the class has a
4205 method and a free variable with the same name.
4206 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004207 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004208 /* If it isn't declared locally, it can't be a cell. */
4209 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4210 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004211 v = PyInt_FromLong(si->si_ncells++);
4212 dict = c->c_cellvars;
4213 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004214 /* If it is free anyway, then there is no need to do
4215 anything here.
4216 */
4217 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004218 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004219 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004220 v = PyInt_FromLong(si->si_nfrees++);
4221 dict = c->c_freevars;
4222 }
4223 if (v == NULL)
4224 return -1;
4225 if (PyDict_SetItem(dict, name, v) < 0) {
4226 Py_DECREF(v);
4227 return -1;
4228 }
4229 Py_DECREF(v);
4230 return 0;
4231}
4232
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004233/* If a variable is a cell and an argument, make sure that appears in
4234 co_cellvars before any variable to its right in varnames.
4235*/
4236
4237
4238static int
4239symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4240 PyObject *varnames, int flags)
4241{
4242 PyObject *v, *w, *d, *list = NULL;
4243 int i, pos;
4244
4245 if (flags & CO_VARARGS)
4246 argcount++;
4247 if (flags & CO_VARKEYWORDS)
4248 argcount++;
4249 for (i = argcount; --i >= 0; ) {
4250 v = PyList_GET_ITEM(varnames, i);
4251 if (PyDict_GetItem(*cellvars, v)) {
4252 if (list == NULL) {
4253 list = PyList_New(1);
4254 if (list == NULL)
4255 return -1;
4256 PyList_SET_ITEM(list, 0, v);
4257 Py_INCREF(v);
4258 } else
4259 PyList_Insert(list, 0, v);
4260 }
4261 }
4262 if (list == NULL || PyList_GET_SIZE(list) == 0)
4263 return 0;
4264 /* There are cellvars that are also arguments. Create a dict
4265 to replace cellvars and put the args at the front.
4266 */
4267 d = PyDict_New();
4268 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4269 v = PyInt_FromLong(i);
4270 if (v == NULL)
4271 goto fail;
4272 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4273 goto fail;
4274 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4275 goto fail;
4276 }
4277 pos = 0;
4278 i = PyList_GET_SIZE(list);
4279 Py_DECREF(list);
4280 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4281 w = PyInt_FromLong(i++); /* don't care about the old key */
4282 if (PyDict_SetItem(d, v, w) < 0) {
4283 Py_DECREF(w);
4284 goto fail;
4285 }
4286 Py_DECREF(w);
4287 }
4288 Py_DECREF(*cellvars);
4289 *cellvars = d;
4290 return 1;
4291 fail:
4292 Py_DECREF(d);
4293 return -1;
4294}
4295
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004296static int
4297symtable_freevar_offsets(PyObject *freevars, int offset)
4298{
4299 PyObject *name, *v;
4300 int pos;
4301
4302 /* The cell vars are the first elements of the closure,
4303 followed by the free vars. Update the offsets in
4304 c_freevars to account for number of cellvars. */
4305 pos = 0;
4306 while (PyDict_Next(freevars, &pos, &name, &v)) {
4307 int i = PyInt_AS_LONG(v) + offset;
4308 PyObject *o = PyInt_FromLong(i);
4309 if (o == NULL)
4310 return -1;
4311 if (PyDict_SetItem(freevars, name, o) < 0) {
4312 Py_DECREF(o);
4313 return -1;
4314 }
4315 Py_DECREF(o);
4316 }
4317 return 0;
4318}
4319
4320static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004321symtable_check_unoptimized(struct compiling *c,
4322 PySymtableEntryObject *ste,
4323 struct symbol_info *si)
4324{
4325 char buf[300];
4326
4327 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4328 || (ste->ste_nested && si->si_nimplicit)))
4329 return 0;
4330
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004331#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4332
4333#define ILLEGAL_IS "is a nested function"
4334
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004335#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004336"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004337
4338#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004339"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004340
4341#define ILLEGAL_EXEC_AND_IMPORT_STAR \
4342"function '%.100s' uses import * and bare exec, which are illegal" \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004343"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004344
4345 /* XXX perhaps the linenos for these opt-breaking statements
4346 should be stored so the exception can point to them. */
4347
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004348 if (ste->ste_child_free) {
4349 if (ste->ste_optimized == OPT_IMPORT_STAR)
4350 sprintf(buf, ILLEGAL_IMPORT_STAR,
4351 PyString_AS_STRING(ste->ste_name),
4352 ILLEGAL_CONTAINS);
4353 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4354 sprintf(buf, ILLEGAL_BARE_EXEC,
4355 PyString_AS_STRING(ste->ste_name),
4356 ILLEGAL_CONTAINS);
4357 else {
4358 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4359 PyString_AS_STRING(ste->ste_name),
4360 ILLEGAL_CONTAINS);
4361 }
4362 } else {
4363 if (ste->ste_optimized == OPT_IMPORT_STAR)
4364 sprintf(buf, ILLEGAL_IMPORT_STAR,
4365 PyString_AS_STRING(ste->ste_name),
4366 ILLEGAL_IS);
4367 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4368 sprintf(buf, ILLEGAL_BARE_EXEC,
4369 PyString_AS_STRING(ste->ste_name),
4370 ILLEGAL_IS);
4371 else {
4372 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4373 PyString_AS_STRING(ste->ste_name),
4374 ILLEGAL_IS);
4375 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004376 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004377
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004378 if (c->c_symtable->st_nested_scopes) {
4379 PyErr_SetString(PyExc_SyntaxError, buf);
4380 PyErr_SyntaxLocation(c->c_symtable->st_filename,
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00004381 ste->ste_opt_lineno);
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004382 return -1;
Guido van Rossum207fda62001-03-02 03:30:41 +00004383 }
4384 else {
4385 return issue_warning(buf, c->c_filename, ste->ste_lineno);
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004386 }
4387 return 0;
4388}
4389
4390static int
4391symtable_check_shadow(struct symtable *st, PyObject *name, int flags)
4392{
4393 char buf[500];
4394 PyObject *children, *v;
Jeremy Hyltonc1761322001-02-28 23:44:45 +00004395 PySymtableEntryObject *child = NULL;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004396 int i;
4397
4398 if (!(flags & DEF_BOUND))
4399 return 0;
Jeremy Hylton9c901052001-05-08 04:12:34 +00004400
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004401 /* The semantics of this code will change with nested scopes.
4402 It is defined in the current scope and referenced in a
4403 child scope. Under the old rules, the child will see a
4404 global. Under the new rules, the child will see the
4405 binding in the current scope.
4406 */
4407
4408 /* Find name of child function that has free variable */
4409 children = st->st_cur->ste_children;
4410 for (i = 0; i < PyList_GET_SIZE(children); i++) {
4411 int cflags;
4412 child = (PySymtableEntryObject *)PyList_GET_ITEM(children, i);
4413 v = PyDict_GetItem(child->ste_symbols, name);
4414 if (v == NULL)
4415 continue;
4416 cflags = PyInt_AS_LONG(v);
4417 if (!(cflags & DEF_BOUND))
4418 break;
4419 }
Jeremy Hyltonc1761322001-02-28 23:44:45 +00004420
4421 assert(child != NULL);
4422
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004423 sprintf(buf, "local name '%.100s' in '%.100s' shadows "
4424 "use of '%.100s' as global in nested scope '%.100s'",
4425 PyString_AS_STRING(name),
4426 PyString_AS_STRING(st->st_cur->ste_name),
4427 PyString_AS_STRING(name),
4428 PyString_AS_STRING(child->ste_name)
4429 );
4430
4431 return symtable_warn(st, buf);
4432}
4433
4434static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004435symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4436 struct symbol_info *si)
4437{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004438 if (c->c_future)
4439 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004440 if (ste->ste_generator)
4441 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004442 if (ste->ste_type != TYPE_MODULE)
4443 c->c_flags |= CO_NEWLOCALS;
4444 if (ste->ste_type == TYPE_FUNCTION) {
4445 c->c_nlocals = si->si_nlocals;
4446 if (ste->ste_optimized == 0)
4447 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004448 else if (ste->ste_optimized != OPT_EXEC)
4449 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004450 }
4451 return 0;
4452}
4453
4454static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004455symtable_load_symbols(struct compiling *c)
4456{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004457 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004458 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004459 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004460 PyObject *name, *varnames, *v;
4461 int i, flags, pos;
4462 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004463
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004464 if (implicit == NULL) {
4465 implicit = PyInt_FromLong(1);
4466 if (implicit == NULL)
4467 return -1;
4468 }
4469 v = NULL;
4470
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004471 if (symtable_init_compiling_symbols(c) < 0)
4472 goto fail;
4473 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004474 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004475 si.si_nlocals = PyList_GET_SIZE(varnames);
4476 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004477
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004478 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004479 v = PyInt_FromLong(i);
4480 if (PyDict_SetItem(c->c_locals,
4481 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004482 goto fail;
4483 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004484 }
4485
4486 /* XXX The cases below define the rules for whether a name is
4487 local or global. The logic could probably be clearer. */
4488 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004489 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4490 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004491
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004492 if (st->st_nested_scopes == 0
4493 && (flags & (DEF_FREE | DEF_FREE_CLASS))) {
4494 if (symtable_check_shadow(st, name, flags) < 0)
4495 goto fail;
4496 }
4497
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004498 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004499 /* undo the original DEF_FREE */
4500 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004501
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004502 /* Deal with names that need two actions:
4503 1. Cell variables, which are also locals.
4504 2. Free variables in methods that are also class
4505 variables or declared global.
4506 */
Jeremy Hylton9c901052001-05-08 04:12:34 +00004507 if (st->st_nested_scopes) {
4508 if (flags & (DEF_FREE | DEF_FREE_CLASS)) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004509 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton9c901052001-05-08 04:12:34 +00004510 }
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004511 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004512
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004513 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004514 c->c_argcount--;
4515 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004516 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004517 c->c_argcount--;
4518 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004519 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004520 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004521 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004522 if (flags & DEF_PARAM) {
4523 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004524 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004525 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004526 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004527 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004528 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004529 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004530 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4531 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004532 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004533 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004534 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4535 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004536 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004537 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004538 if (v == NULL)
4539 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004540 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004541 goto fail;
4542 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004543 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004544 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004545 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004546 } else if (is_free(flags)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004547 if (ste->ste_nested && st->st_nested_scopes) {
4548 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004549 if (v == NULL)
4550 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004551 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004552 goto fail;
4553 Py_DECREF(v);
4554 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004555 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004556 if (PyDict_SetItem(c->c_globals, name,
4557 implicit) < 0)
4558 goto fail;
Jeremy Hylton51257732001-03-01 00:42:55 +00004559 if (st->st_nscopes != 1) {
4560 v = PyInt_FromLong(flags);
4561 if (PyDict_SetItem(st->st_global,
4562 name, v))
4563 goto fail;
4564 Py_DECREF(v);
4565 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004566 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004567 }
4568 }
4569
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004570 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4571
Jeremy Hylton9c901052001-05-08 04:12:34 +00004572 if (st->st_nested_scopes == 0)
4573 assert(si.si_nfrees == 0);
4574
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004575 if (si.si_ncells > 1) { /* one cell is always in order */
4576 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4577 c->c_varnames, c->c_flags) < 0)
4578 return -1;
4579 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004580 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4581 return -1;
4582 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004583 fail:
4584 /* is this always the right thing to do? */
4585 Py_XDECREF(v);
4586 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004587}
4588
4589static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004590symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004591{
4592 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004593
4594 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4595 if (st == NULL)
4596 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004597 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004598
4599 /* XXX Tim: Jeremy deleted the next line and everything went to hell.
4600 XXX It should probably get fixed by getting rid of st_nested_scopes
4601 XXX entirely. */
4602 st->st_nested_scopes = 1;
4603
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004604 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004605 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004606 goto fail;
4607 if ((st->st_symbols = PyDict_New()) == NULL)
4608 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004609 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004610 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004611 st->st_errors = 0;
4612 st->st_tmpname = 0;
4613 st->st_private = NULL;
4614 return st;
4615 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004616 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004617 return NULL;
4618}
4619
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004620void
4621PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004622{
4623 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004624 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004625 Py_XDECREF(st->st_cur);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004626 PyMem_Free((void *)st);
4627}
4628
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004629/* When the compiler exits a scope, it must should update the scope's
4630 free variable information with the list of free variables in its
4631 children.
4632
4633 Variables that are free in children and defined in the current
4634 scope are cellvars.
4635
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004636 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004637 false), free variables in children that are not defined here are
4638 implicit globals.
4639
4640*/
4641
4642static int
4643symtable_update_free_vars(struct symtable *st)
4644{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004645 int i, j, def;
4646 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004647 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004648
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004649 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004650 def = DEF_FREE_CLASS;
4651 else
4652 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004653 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004654 int pos = 0;
4655
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004656 if (list)
4657 PyList_SetSlice(list, 0,
4658 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004659 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004660 PyList_GET_ITEM(ste->ste_children, i);
4661 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004662 int flags = PyInt_AS_LONG(o);
4663 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004664 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004665 if (list == NULL) {
4666 list = PyList_New(0);
4667 if (list == NULL)
4668 return -1;
4669 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004670 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004671 if (PyList_Append(list, name) < 0) {
4672 Py_DECREF(list);
4673 return -1;
4674 }
4675 }
4676 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004677 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004678 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004679 v = PyDict_GetItem(ste->ste_symbols, name);
4680 /* If a name N is declared global in scope A and
4681 referenced in scope B contained (perhaps
4682 indirectly) in A and there are no scopes
4683 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004684 is global in B. Unless A is a class scope,
4685 because class scopes are not considered for
4686 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004687 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004688 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004689 int flags = PyInt_AS_LONG(v);
4690 if (flags & DEF_GLOBAL) {
4691 symtable_undo_free(st, child->ste_id,
4692 name);
4693 continue;
4694 }
4695 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004696 if (ste->ste_nested) {
4697 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004698 name, def) < 0) {
4699 Py_DECREF(list);
4700 return -1;
4701 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004702 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004703 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004704 name) < 0) {
4705 Py_DECREF(list);
4706 return -1;
4707 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004708 }
4709 }
4710 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004711
4712 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004713 return 0;
4714}
4715
4716/* If the current scope is a non-nested class or if name is not
4717 defined in the current, non-nested scope, then it is an implicit
4718 global in all nested scopes.
4719*/
4720
4721static int
4722symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4723{
4724 PyObject *o;
4725 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004726 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004727
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004728 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004729 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004730 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004731 if (o == NULL)
4732 return symtable_undo_free(st, child, name);
4733 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004734
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004735 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004736 return symtable_undo_free(st, child, name);
4737 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004738 return symtable_add_def_o(st, ste->ste_symbols,
4739 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004740}
4741
4742static int
4743symtable_undo_free(struct symtable *st, PyObject *id,
4744 PyObject *name)
4745{
4746 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004747 PyObject *info;
4748 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004749
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004750 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4751 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004752 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004753
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004754 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004755 if (info == NULL)
4756 return 0;
4757 v = PyInt_AS_LONG(info);
4758 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004759 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004760 DEF_FREE_GLOBAL) < 0)
4761 return -1;
4762 } else
4763 /* If the name is defined here or declared global,
4764 then the recursion stops. */
4765 return 0;
4766
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004767 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4768 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00004769 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004770 PyList_GET_ITEM(ste->ste_children, i);
4771 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004772 if (x < 0)
4773 return x;
4774 }
4775 return 0;
4776}
4777
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004778/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4779 This reference is released when the scope is exited, via the DECREF
4780 in symtable_exit_scope().
4781*/
4782
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004783static int
4784symtable_exit_scope(struct symtable *st)
4785{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004786 int end;
4787
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004788 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004789 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004790 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004791 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004792 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4793 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004794 if (PySequence_DelItem(st->st_stack, end) < 0)
4795 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004796 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004797}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004798
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004799static void
4800symtable_enter_scope(struct symtable *st, char *name, int type,
4801 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004802{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004803 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004804
4805 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004806 prev = st->st_cur;
4807 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4808 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004809 st->st_errors++;
4810 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004811 }
4812 }
Barry Warsaw0372af72001-02-23 18:22:59 +00004813 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004814 PySymtableEntry_New(st, name, type, lineno);
4815 if (strcmp(name, TOP) == 0)
4816 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004817 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004818 if (PyList_Append(prev->ste_children,
4819 (PyObject *)st->st_cur) < 0)
4820 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004821 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004822}
4823
4824static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004825symtable_lookup(struct symtable *st, char *name)
4826{
4827 char buffer[MANGLE_LEN];
4828 PyObject *v;
4829 int flags;
4830
4831 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4832 name = buffer;
4833 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4834 if (v == NULL) {
4835 if (PyErr_Occurred())
4836 return -1;
4837 else
4838 return 0;
4839 }
4840
4841 flags = PyInt_AS_LONG(v);
4842 return flags;
4843}
4844
4845static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004846symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004847{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004848 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004849 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004850 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004851
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004852 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004853 name = buffer;
4854 if ((s = PyString_InternFromString(name)) == NULL)
4855 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004856 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4857 Py_DECREF(s);
4858 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004859}
4860
4861/* Must only be called with mangled names */
4862
4863static int
4864symtable_add_def_o(struct symtable *st, PyObject *dict,
4865 PyObject *name, int flag)
4866{
4867 PyObject *o;
4868 int val;
4869
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004870 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004871 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004872 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004873 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00004874 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004875 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004876 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004877 return -1;
4878 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004879 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004880 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004881 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004882 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004883 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004884 Py_DECREF(o);
4885 return -1;
4886 }
4887 Py_DECREF(o);
4888
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004889 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004890 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004891 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004892 } else if (flag & DEF_GLOBAL) {
4893 /* XXX need to update DEF_GLOBAL for other flags too;
4894 perhaps only DEF_FREE_GLOBAL */
4895 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004896 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004897 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004898 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004899 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004900 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004901 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004902 Py_DECREF(o);
4903 return -1;
4904 }
4905 Py_DECREF(o);
4906 }
4907 return 0;
4908}
4909
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004910#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004911
Tim Peters08a898f2001-06-28 01:52:22 +00004912/* Look for a yield stmt under n. Return 1 if found, else 0.
4913 This hack is used to look inside "if 0:" blocks (which are normally
4914 ignored) in case those are the only places a yield occurs (so that this
4915 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00004916static int
4917look_for_yield(node *n)
4918{
4919 int i;
4920
4921 for (i = 0; i < NCH(n); ++i) {
4922 node *kid = CHILD(n, i);
4923
4924 switch (TYPE(kid)) {
4925
4926 case classdef:
4927 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00004928 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00004929 /* Stuff in nested functions and classes can't make
4930 the parent a generator. */
4931 return 0;
4932
4933 case yield_stmt:
4934 return 1;
4935
4936 default:
4937 if (look_for_yield(kid))
4938 return 1;
4939 }
4940 }
4941 return 0;
4942}
4943
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004944static void
4945symtable_node(struct symtable *st, node *n)
4946{
4947 int i, start = 0;
4948
4949 loop:
4950 switch (TYPE(n)) {
4951 case funcdef: {
4952 char *func_name = STR(CHILD(n, 1));
4953 symtable_add_def(st, func_name, DEF_LOCAL);
4954 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004955 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004956 symtable_funcdef(st, n);
4957 symtable_exit_scope(st);
4958 break;
4959 }
4960 case lambdef:
4961 if (NCH(n) == 4)
4962 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004963 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004964 symtable_funcdef(st, n);
4965 symtable_exit_scope(st);
4966 break;
4967 case classdef: {
4968 char *tmp, *class_name = STR(CHILD(n, 1));
4969 symtable_add_def(st, class_name, DEF_LOCAL);
4970 if (TYPE(CHILD(n, 2)) == LPAR) {
4971 node *bases = CHILD(n, 3);
4972 int i;
4973 for (i = 0; i < NCH(bases); i += 2) {
4974 symtable_node(st, CHILD(bases, i));
4975 }
4976 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004977 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004978 tmp = st->st_private;
4979 st->st_private = class_name;
4980 symtable_node(st, CHILD(n, NCH(n) - 1));
4981 st->st_private = tmp;
4982 symtable_exit_scope(st);
4983 break;
4984 }
4985 case if_stmt:
4986 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00004987 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
4988 if (st->st_cur->ste_generator == 0)
4989 st->st_cur->ste_generator =
4990 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004991 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004992 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004993 symtable_node(st, CHILD(n, i + 1));
4994 symtable_node(st, CHILD(n, i + 3));
4995 }
4996 if (i + 2 < NCH(n))
4997 symtable_node(st, CHILD(n, i + 2));
4998 break;
4999 case global_stmt:
5000 symtable_global(st, n);
5001 break;
5002 case import_stmt:
5003 symtable_import(st, n);
5004 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005005 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005006 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005007 symtable_node(st, CHILD(n, 1));
5008 if (NCH(n) > 2)
5009 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005010 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005011 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005012 st->st_cur->ste_opt_lineno = n->n_lineno;
5013 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005014 if (NCH(n) > 4)
5015 symtable_node(st, CHILD(n, 5));
5016 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005017
5018 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00005019 case assert_stmt:
5020 if (Py_OptimizeFlag)
5021 return;
5022 if (NCH(n) == 2) {
5023 n = CHILD(n, 1);
5024 goto loop;
5025 } else {
5026 symtable_node(st, CHILD(n, 1));
5027 n = CHILD(n, 3);
5028 goto loop;
5029 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005030 case except_clause:
5031 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005032 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005033 if (NCH(n) > 1) {
5034 n = CHILD(n, 1);
5035 goto loop;
5036 }
5037 break;
5038 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005039 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005040 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005041 case yield_stmt:
5042 st->st_cur->ste_generator = 1;
5043 n = CHILD(n, 1);
5044 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005045 case expr_stmt:
5046 if (NCH(n) == 1)
5047 n = CHILD(n, 0);
5048 else {
5049 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005050 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005051 symtable_node(st, CHILD(n, 2));
5052 break;
5053 } else {
5054 int i;
5055 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005056 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005057 n = CHILD(n, NCH(n) - 1);
5058 }
5059 }
5060 goto loop;
5061 /* watchout for fall-through logic below */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005062 case argument:
5063 if (NCH(n) == 3) {
5064 n = CHILD(n, 2);
5065 goto loop;
5066 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005067 case listmaker:
5068 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005069 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005070 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005071 symtable_node(st, CHILD(n, 0));
5072 st->st_tmpname--;
5073 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005074 }
5075 case atom:
5076 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5077 symtable_add_use(st, STR(CHILD(n, 0)));
5078 break;
5079 }
5080 case for_stmt:
5081 if (TYPE(n) == for_stmt) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005082 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005083 start = 3;
5084 }
5085 default:
5086 if (NCH(n) == 1) {
5087 n = CHILD(n, 0);
5088 goto loop;
5089 }
5090 for (i = start; i < NCH(n); ++i)
5091 if (TYPE(CHILD(n, i)) >= single_input)
5092 symtable_node(st, CHILD(n, i));
5093 }
5094}
5095
5096static void
5097symtable_funcdef(struct symtable *st, node *n)
5098{
5099 node *body;
5100
5101 if (TYPE(n) == lambdef) {
5102 if (NCH(n) == 4)
5103 symtable_params(st, CHILD(n, 1));
5104 } else
5105 symtable_params(st, CHILD(n, 2));
5106 body = CHILD(n, NCH(n) - 1);
5107 symtable_node(st, body);
5108}
5109
5110/* The next two functions parse the argument tuple.
5111 symtable_default_arg() checks for names in the default arguments,
5112 which are references in the defining scope. symtable_params()
5113 parses the parameter names, which are defined in the function's
5114 body.
5115
5116 varargslist:
5117 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5118 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5119*/
5120
5121static void
5122symtable_default_args(struct symtable *st, node *n)
5123{
5124 node *c;
5125 int i;
5126
5127 if (TYPE(n) == parameters) {
5128 n = CHILD(n, 1);
5129 if (TYPE(n) == RPAR)
5130 return;
5131 }
5132 REQ(n, varargslist);
5133 for (i = 0; i < NCH(n); i += 2) {
5134 c = CHILD(n, i);
5135 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5136 break;
5137 }
5138 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5139 symtable_node(st, CHILD(n, i));
5140 }
5141}
5142
5143static void
5144symtable_params(struct symtable *st, node *n)
5145{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005146 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005147 node *c = NULL;
5148
5149 if (TYPE(n) == parameters) {
5150 n = CHILD(n, 1);
5151 if (TYPE(n) == RPAR)
5152 return;
5153 }
5154 REQ(n, varargslist);
5155 for (i = 0; i < NCH(n); i += 2) {
5156 c = CHILD(n, i);
5157 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5158 ext = 1;
5159 break;
5160 }
5161 if (TYPE(c) == test) {
5162 continue;
5163 }
5164 if (TYPE(CHILD(c, 0)) == NAME)
5165 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5166 else {
5167 char nbuf[10];
5168 sprintf(nbuf, ".%d", i);
5169 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005170 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005171 }
5172 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005173 if (ext) {
5174 c = CHILD(n, i);
5175 if (TYPE(c) == STAR) {
5176 i++;
5177 symtable_add_def(st, STR(CHILD(n, i)),
5178 DEF_PARAM | DEF_STAR);
5179 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005180 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005181 c = NULL;
5182 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005183 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005184 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005185 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005186 i++;
5187 symtable_add_def(st, STR(CHILD(n, i)),
5188 DEF_PARAM | DEF_DOUBLESTAR);
5189 }
5190 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005191 if (complex >= 0) {
5192 int j;
5193 for (j = 0; j <= complex; j++) {
5194 c = CHILD(n, j);
5195 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005196 c = CHILD(n, ++j);
5197 else if (TYPE(c) == EQUAL)
5198 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005199 if (TYPE(CHILD(c, 0)) == LPAR)
5200 symtable_params_fplist(st, CHILD(c, 1));
5201 }
5202 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005203}
5204
5205static void
5206symtable_params_fplist(struct symtable *st, node *n)
5207{
5208 int i;
5209 node *c;
5210
5211 REQ(n, fplist);
5212 for (i = 0; i < NCH(n); i += 2) {
5213 c = CHILD(n, i);
5214 REQ(c, fpdef);
5215 if (NCH(c) == 1)
5216 symtable_add_def(st, STR(CHILD(c, 0)),
5217 DEF_PARAM | DEF_INTUPLE);
5218 else
5219 symtable_params_fplist(st, CHILD(c, 1));
5220 }
5221
5222}
5223
5224static void
5225symtable_global(struct symtable *st, node *n)
5226{
5227 int i;
5228
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005229 /* XXX It might be helpful to warn about module-level global
5230 statements, but it's hard to tell the difference between
5231 module-level and a string passed to exec.
5232 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005233
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005234 for (i = 1; i < NCH(n); i += 2) {
5235 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005236 int flags;
5237
5238 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005239 if (flags < 0)
5240 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005241 if (flags && flags != DEF_GLOBAL) {
5242 char buf[500];
5243 if (flags & DEF_PARAM) {
5244 PyErr_Format(PyExc_SyntaxError,
5245 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005246 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005247 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005248 st->st_cur->ste_lineno);
5249 st->st_errors++;
5250 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005251 }
5252 else {
5253 if (flags & DEF_LOCAL)
5254 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5255 name);
5256 else
5257 sprintf(buf, GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005258 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005259 }
5260 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005261 symtable_add_def(st, name, DEF_GLOBAL);
5262 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005263}
5264
5265static void
5266symtable_list_comprehension(struct symtable *st, node *n)
5267{
5268 char tmpname[12];
5269
Jeremy Hylton23b42272001-03-19 20:38:06 +00005270 sprintf(tmpname, "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005271 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005272 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005273 symtable_node(st, CHILD(n, 3));
5274 if (NCH(n) == 5)
5275 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005276}
5277
5278static void
5279symtable_import(struct symtable *st, node *n)
5280{
5281 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005282 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005283 | 'from' dotted_name 'import'
5284 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005285 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005286 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005287 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005288 node *dotname = CHILD(n, 1);
5289 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5290 /* check for bogus imports */
5291 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5292 PyErr_SetString(PyExc_SyntaxError,
5293 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005294 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005295 n->n_lineno);
5296 st->st_errors++;
5297 return;
5298 }
5299 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005300 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005301 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005302 if (symtable_warn(st,
5303 "import * only allowed at module level") < 0)
5304 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005305 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005306 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005307 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005308 } else {
5309 for (i = 3; i < NCH(n); i += 2) {
5310 node *c = CHILD(n, i);
5311 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005312 symtable_assign(st, CHILD(c, 2),
5313 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005314 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005315 symtable_assign(st, CHILD(c, 0),
5316 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005317 }
5318 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005319 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005320 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005321 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005322 }
5323 }
5324}
5325
5326static void
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005327symtable_assign(struct symtable *st, node *n, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005328{
5329 node *tmp;
5330 int i;
5331
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005332 loop:
5333 switch (TYPE(n)) {
5334 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005335 /* invalid assignment, e.g. lambda x:x=2. The next
5336 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005337 return;
5338 case power:
5339 if (NCH(n) > 2) {
5340 for (i = 2; i < NCH(n); ++i)
5341 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5342 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005343 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005344 if (NCH(n) > 1) {
5345 symtable_node(st, CHILD(n, 0));
5346 symtable_node(st, CHILD(n, 1));
5347 } else {
5348 n = CHILD(n, 0);
5349 goto loop;
5350 }
5351 return;
5352 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005353 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5354 /* XXX This is an error, but the next pass
5355 will catch it. */
5356 return;
5357 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005358 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005359 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005360 }
5361 return;
5362 case exprlist:
5363 case testlist:
5364 if (NCH(n) == 1) {
5365 n = CHILD(n, 0);
5366 goto loop;
5367 }
5368 else {
5369 int i;
5370 for (i = 0; i < NCH(n); i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005371 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005372 return;
5373 }
5374 goto loop;
5375 case atom:
5376 tmp = CHILD(n, 0);
5377 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5378 n = CHILD(n, 1);
5379 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005380 } else if (TYPE(tmp) == NAME) {
Jeremy Hyltona830b382001-04-09 16:07:59 +00005381 if (strcmp(STR(tmp), "__debug__") == 0)
5382 symtable_warn(st, ASSIGN_DEBUG);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005383 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005384 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005385 return;
5386 case dotted_as_name:
5387 if (NCH(n) == 3)
5388 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005389 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005390 else
5391 symtable_add_def(st,
5392 STR(CHILD(CHILD(n,
5393 0), 0)),
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005394 DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005395 return;
5396 case dotted_name:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005397 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005398 return;
5399 case NAME:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005400 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005401 return;
5402 default:
5403 if (NCH(n) == 0)
5404 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005405 if (NCH(n) == 1) {
5406 n = CHILD(n, 0);
5407 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005408 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005409 /* Should only occur for errors like x + 1 = 1,
5410 which will be caught in the next pass. */
5411 for (i = 0; i < NCH(n); ++i)
5412 if (TYPE(CHILD(n, i)) >= single_input)
5413 symtable_assign(st, CHILD(n, i), flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005414 }
5415}