blob: f2cd59effb4e14896da1ed485acac74ce5b4d8fd [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
Guido van Rossum6f799372001-09-20 20:46:19 +000076static PyMemberDef code_memberlist[] = {
Guido van Rossum681d79a1995-07-18 14:51:37 +000077 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
78 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
Guido van Rossum8b993a91997-01-17 21:04:03 +000079 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000080 {"co_flags", T_INT, OFF(co_flags), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000081 {"co_code", T_OBJECT, OFF(co_code), READONLY},
82 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
83 {"co_names", T_OBJECT, OFF(co_names), READONLY},
Guido van Rossum681d79a1995-07-18 14:51:37 +000084 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
Jeremy Hylton64949cb2001-01-25 20:06:59 +000085 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
86 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
Guido van Rossum39d942d1992-01-12 02:30:05 +000087 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
Guido van Rossum9bfef441993-03-29 10:43:31 +000088 {"co_name", T_OBJECT, OFF(co_name), READONLY},
Guido van Rossumda4eb5c1997-01-24 03:43:35 +000089 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
90 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 {NULL} /* Sentinel */
92};
93
Guido van Rossum10dc2e81990-11-18 17:27:39 +000094static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000095code_dealloc(PyCodeObject *co)
Guido van Rossum10dc2e81990-11-18 17:27:39 +000096{
Guido van Rossum79f25d91997-04-29 20:08:16 +000097 Py_XDECREF(co->co_code);
98 Py_XDECREF(co->co_consts);
99 Py_XDECREF(co->co_names);
Guido van Rossum275558c1997-07-25 20:13:49 +0000100 Py_XDECREF(co->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000101 Py_XDECREF(co->co_freevars);
102 Py_XDECREF(co->co_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000103 Py_XDECREF(co->co_filename);
104 Py_XDECREF(co->co_name);
Guido van Rossum275558c1997-07-25 20:13:49 +0000105 Py_XDECREF(co->co_lnotab);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 PyObject_DEL(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000107}
108
Guido van Rossum79f25d91997-04-29 20:08:16 +0000109static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000110code_repr(PyCodeObject *co)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000111{
112 char buf[500];
113 int lineno = -1;
Guido van Rossum2dff9911992-09-03 20:50:59 +0000114 char *filename = "???";
Guido van Rossum9bfef441993-03-29 10:43:31 +0000115 char *name = "???";
Guido van Rossumd076c731998-10-07 19:42:25 +0000116
Guido van Rossuma396a882000-04-07 01:21:36 +0000117 if (co->co_firstlineno != 0)
118 lineno = co->co_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000119 if (co->co_filename && PyString_Check(co->co_filename))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000120 filename = PyString_AS_STRING(co->co_filename);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000121 if (co->co_name && PyString_Check(co->co_name))
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000122 name = PyString_AS_STRING(co->co_name);
Fred Drake615ae552000-06-30 16:20:13 +0000123 sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
124 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000125 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000126}
127
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000128static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000129code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000130{
131 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000132 cmp = PyObject_Compare(co->co_name, cp->co_name);
133 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000134 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000135 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000136 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000137 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000138 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000139 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000140 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000141 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000142 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000143 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000144 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000145 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000146 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000147 if (cmp) return cmp;
148 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
149 if (cmp) return cmp;
150 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000151 return cmp;
152}
153
154static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000155code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000156{
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000157 long h, h0, h1, h2, h3, h4, h5, h6;
Guido van Rossum44679592000-04-10 16:20:31 +0000158 h0 = PyObject_Hash(co->co_name);
159 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000160 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000161 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000162 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000163 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000164 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000165 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000166 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000167 if (h4 == -1) return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000168 h5 = PyObject_Hash(co->co_freevars);
169 if (h5 == -1) return -1;
170 h6 = PyObject_Hash(co->co_cellvars);
171 if (h6 == -1) return -1;
172 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000173 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000174 if (h == -1) h = -2;
175 return h;
176}
177
Jeremy Hylton78891072001-03-01 06:09:34 +0000178/* XXX code objects need to participate in GC? */
179
Guido van Rossum79f25d91997-04-29 20:08:16 +0000180PyTypeObject PyCode_Type = {
181 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000182 0,
183 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000184 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000185 0,
Jeremy Hyltonc785f482001-09-14 20:08:07 +0000186 (destructor)code_dealloc, /* tp_dealloc */
187 0, /* tp_print */
188 0, /* tp_getattr */
189 0, /* tp_setattr */
190 (cmpfunc)code_compare, /* tp_compare */
191 (reprfunc)code_repr, /* tp_repr */
192 0, /* tp_as_number */
193 0, /* tp_as_sequence */
194 0, /* tp_as_mapping */
195 (hashfunc)code_hash, /* tp_hash */
196 0, /* tp_call */
197 0, /* tp_str */
198 PyObject_GenericGetAttr, /* tp_getattro */
199 0, /* tp_setattro */
200 0, /* tp_as_buffer */
201 Py_TPFLAGS_DEFAULT, /* tp_flags */
202 0, /* tp_doc */
203 0, /* tp_traverse */
204 0, /* tp_clear */
205 0, /* tp_richcompare */
206 0, /* tp_weaklistoffset */
207 0, /* tp_iter */
208 0, /* tp_iternext */
209 0, /* tp_methods */
210 code_memberlist, /* tp_members */
211 0, /* tp_getset */
212 0, /* tp_base */
213 0, /* tp_dict */
214 0, /* tp_descr_get */
215 0, /* tp_descr_set */
216 0, /* tp_dictoffset */
217 0, /* tp_init */
218 0, /* tp_alloc */
219 0, /* tp_new */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000220};
221
Guido van Rossum644a12b1997-04-09 19:24:53 +0000222#define NAME_CHARS \
223 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
224
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000225/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
226
227static int
228all_name_chars(unsigned char *s)
229{
Guido van Rossumcd90c202001-02-09 15:06:42 +0000230 static char ok_name_char[256];
231 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000232
Guido van Rossumcd90c202001-02-09 15:06:42 +0000233 if (ok_name_char[*name_chars] == 0) {
234 unsigned char *p;
235 for (p = name_chars; *p; p++)
236 ok_name_char[*p] = 1;
237 }
238 while (*s) {
239 if (ok_name_char[*s++] == 0)
240 return 0;
241 }
242 return 1;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000243}
244
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000245static int
246intern_strings(PyObject *tuple)
247{
248 int i;
249
250 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
251 PyObject *v = PyTuple_GET_ITEM(tuple, i);
252 if (v == NULL || !PyString_Check(v)) {
253 Py_FatalError("non-string found in code slot");
254 PyErr_BadInternalCall();
255 return -1;
256 }
257 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
258 }
259 return 0;
260}
261
Guido van Rossum79f25d91997-04-29 20:08:16 +0000262PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyCode_New(int argcount, int nlocals, int stacksize, int flags,
264 PyObject *code, PyObject *consts, PyObject *names,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000265 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
266 PyObject *filename, PyObject *name, int firstlineno,
267 PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000268{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000269 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000270 int i;
Guido van Rossumd076c731998-10-07 19:42:25 +0000271 PyBufferProcs *pb;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000272 /* Check argument types */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000273 if (argcount < 0 || nlocals < 0 ||
Guido van Rossumd076c731998-10-07 19:42:25 +0000274 code == NULL ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 consts == NULL || !PyTuple_Check(consts) ||
276 names == NULL || !PyTuple_Check(names) ||
277 varnames == NULL || !PyTuple_Check(varnames) ||
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000278 freevars == NULL || !PyTuple_Check(freevars) ||
279 cellvars == NULL || !PyTuple_Check(cellvars) ||
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280 name == NULL || !PyString_Check(name) ||
281 filename == NULL || !PyString_Check(filename) ||
282 lnotab == NULL || !PyString_Check(lnotab)) {
283 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000284 return NULL;
285 }
Guido van Rossumd076c731998-10-07 19:42:25 +0000286 pb = code->ob_type->tp_as_buffer;
287 if (pb == NULL ||
288 pb->bf_getreadbuffer == NULL ||
289 pb->bf_getsegcount == NULL ||
290 (*pb->bf_getsegcount)(code, NULL) != 1)
291 {
292 PyErr_BadInternalCall();
293 return NULL;
294 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000295 intern_strings(names);
296 intern_strings(varnames);
297 if (freevars == NULL)
298 freevars = PyTuple_New(0);
299 intern_strings(freevars);
300 if (cellvars == NULL)
301 cellvars = PyTuple_New(0);
302 intern_strings(cellvars);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000303 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 for (i = PyTuple_Size(consts); --i >= 0; ) {
305 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000306 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000307 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000308 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000309 continue;
310 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000311 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000313 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000314 co->co_argcount = argcount;
315 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000316 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000317 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000318 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000319 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000321 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000323 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000325 co->co_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000326 Py_INCREF(freevars);
327 co->co_freevars = freevars;
328 Py_INCREF(cellvars);
329 co->co_cellvars = cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000330 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000331 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000332 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000333 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000334 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000335 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000336 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000337 }
338 return co;
339}
340
341
342/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000343
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000344/* The compiler uses two passes to generate bytecodes. The first pass
345 builds the symbol table. The second pass generates the bytecode.
346
347 The first pass uses a single symtable struct. The second pass uses
348 a compiling struct for each code block. The compiling structs
349 share a reference to the symtable.
350
351 The two passes communicate via symtable_load_symbols() and via
352 is_local() and is_global(). The former initializes several slots
353 in the compiling struct: c_varnames, c_locals, c_nlocals,
354 c_argcount, c_globals, and c_flags.
355*/
356
Tim Peters2a7f3842001-06-09 09:26:21 +0000357/* All about c_lnotab.
358
359c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
360mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
361to source code line #s (when needed for tracebacks) via c_lnotab instead.
362The array is conceptually a list of
363 (bytecode offset increment, line number increment)
364pairs. The details are important and delicate, best illustrated by example:
365
366 byte code offset source code line number
367 0 1
368 6 2
369 50 7
370 350 307
371 361 308
372
373The first trick is that these numbers aren't stored, only the increments
374from one row to the next (this doesn't really work, but it's a start):
375
376 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
377
378The second trick is that an unsigned byte can't hold negative values, or
379values larger than 255, so (a) there's a deep assumption that byte code
380offsets and their corresponding line #s both increase monotonically, and (b)
381if at least one column jumps by more than 255 from one row to the next, more
382than one pair is written to the table. In case #b, there's no way to know
383from looking at the table later how many were written. That's the delicate
384part. A user of c_lnotab desiring to find the source line number
385corresponding to a bytecode address A should do something like this
386
387 lineno = addr = 0
388 for addr_incr, line_incr in c_lnotab:
389 addr += addr_incr
390 if addr > A:
391 return lineno
392 lineno += line_incr
393
394In order for this to work, when the addr field increments by more than 255,
395the line # increment in each pair generated must be 0 until the remaining addr
396increment is < 256. So, in the example above, com_set_lineno should not (as
397was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
398255, 0, 45, 255, 0, 45.
399*/
400
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000401struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000402 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000403 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000404 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000405 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000406 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000407 PyObject *c_globals; /* dictionary (value=None) */
408 PyObject *c_locals; /* dictionary (value=localID) */
409 PyObject *c_varnames; /* list (inverse of c_locals) */
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000410 PyObject *c_freevars; /* dictionary (value=None) */
411 PyObject *c_cellvars; /* list */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000412 int c_nlocals; /* index of next local */
413 int c_argcount; /* number of top-level arguments */
414 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000415 int c_nexti; /* index into c_code */
416 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000417 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000418 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000419 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000420 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000421 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000422 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000423 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000424 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000425 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000426 int c_stacklevel; /* Current stack level */
427 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000428 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000430 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000431 char *c_private; /* for private name mangling */
Skip Montanaro803d6e52000-08-12 18:09:51 +0000432 int c_tmpname; /* temporary local name counter */
Guido van Rossumcd90c202001-02-09 15:06:42 +0000433 int c_nested; /* Is block nested funcdef or lamdef? */
434 int c_closure; /* Is nested w/freevars? */
435 struct symtable *c_symtable; /* pointer to module symbol table */
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000436 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000437};
438
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000439static int
440is_free(int v)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000441{
442 if ((v & (USE | DEF_FREE))
443 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
444 return 1;
445 if (v & DEF_FREE_CLASS)
446 return 1;
447 return 0;
448}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000449
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000450static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000451com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000452{
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000453 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
454
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000455 if (c == NULL) {
456 /* Error occurred via symtable call to
457 is_constant_false */
458 PyErr_SetString(exc, msg);
459 return;
460 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000461 c->c_errors++;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000462 if (c->c_lineno < 1 || c->c_interactive) {
463 /* Unknown line number or interactive input */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000464 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000465 return;
466 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000467 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000468 if (v == NULL)
469 return; /* MemoryError, too bad */
Fred Drakedcf08e02000-08-15 15:49:44 +0000470
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000471 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000472 if (line == NULL) {
473 Py_INCREF(Py_None);
474 line = Py_None;
475 }
476 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
477 Py_None, line);
478 if (t == NULL)
479 goto exit;
480 w = Py_BuildValue("(OO)", v, t);
481 if (w == NULL)
482 goto exit;
483 PyErr_SetObject(exc, w);
484 exit:
485 Py_XDECREF(t);
486 Py_XDECREF(v);
487 Py_XDECREF(w);
488 Py_XDECREF(line);
Guido van Rossum452a9831996-09-17 14:32:04 +0000489}
490
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000491/* Interface to the block stack */
492
493static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000494block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000495{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000496 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000497 com_error(c, PyExc_SystemError,
498 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000499 }
500 else {
501 c->c_block[c->c_nblocks++] = type;
502 }
503}
504
505static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000506block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000507{
508 if (c->c_nblocks > 0)
509 c->c_nblocks--;
510 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000511 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000512 }
513}
514
Guido van Rossum681d79a1995-07-18 14:51:37 +0000515/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000516
Tim Petersdbd9ba62000-07-09 03:09:57 +0000517static int com_init(struct compiling *, char *);
518static void com_free(struct compiling *);
519static void com_push(struct compiling *, int);
520static void com_pop(struct compiling *, int);
521static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000522static void com_node(struct compiling *, node *);
523static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000524static void com_addbyte(struct compiling *, int);
525static void com_addint(struct compiling *, int);
526static void com_addoparg(struct compiling *, int, int);
527static void com_addfwref(struct compiling *, int, int *);
528static void com_backpatch(struct compiling *, int);
529static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
530static int com_addconst(struct compiling *, PyObject *);
531static int com_addname(struct compiling *, PyObject *);
532static void com_addopname(struct compiling *, int, node *);
533static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000534static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000535static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000536static void com_assign(struct compiling *, node *, int, node *);
537static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000538static PyCodeObject *icompile(node *, struct compiling *);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000539static PyCodeObject *jcompile(node *, char *, struct compiling *,
540 PyCompilerFlags *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000541static PyObject *parsestrplus(struct compiling*, node *);
542static PyObject *parsestr(struct compiling *, char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000543static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000544
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000545static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000546
547/* symtable operations */
548static int symtable_build(struct compiling *, node *);
549static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000550static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000551static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000552static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000553static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000554static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000555
556static void symtable_node(struct symtable *, node *);
557static void symtable_funcdef(struct symtable *, node *);
558static void symtable_default_args(struct symtable *, node *);
559static void symtable_params(struct symtable *, node *);
560static void symtable_params_fplist(struct symtable *, node *n);
561static void symtable_global(struct symtable *, node *);
562static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000563static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000564static void symtable_list_comprehension(struct symtable *, node *);
565
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000566static int symtable_update_free_vars(struct symtable *);
567static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
568static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
569
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000570/* helper */
571static void
572do_pad(int pad)
573{
574 int i;
575 for (i = 0; i < pad; ++i)
576 fprintf(stderr, " ");
577}
578
579static void
580dump(node *n, int pad, int depth)
581{
582 int i;
583 if (depth == 0)
584 return;
585 do_pad(pad);
586 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
587 if (depth > 0)
588 depth--;
589 for (i = 0; i < NCH(n); ++i)
590 dump(CHILD(n, i), pad + 1, depth);
591}
592
593#define DUMP(N) dump(N, 0, -1)
594
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000595static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000596com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000597{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000598 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000599 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
600 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000601 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000603 goto fail;
604 if ((c->c_const_dict = PyDict_New()) == NULL)
605 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000606 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000607 goto fail;
608 if ((c->c_name_dict = PyDict_New()) == NULL)
609 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000610 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000611 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000612 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
613 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000614 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000615 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000616 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000617 c->c_freevars = NULL;
618 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000619 c->c_nlocals = 0;
620 c->c_argcount = 0;
621 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000622 c->c_nexti = 0;
623 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000624 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000625 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000626 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000627 c->c_begin = 0;
628 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000629 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000630 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000631 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000632 c->c_stacklevel = 0;
633 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000634 c->c_firstlineno = 0;
635 c->c_last_addr = 0;
636 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000637 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000638 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000639 c->c_nested = 0;
640 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000641 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000642 return 1;
643
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000644 fail:
645 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000646 return 0;
647}
648
649static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000651{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000652 Py_XDECREF(c->c_code);
653 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000654 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000655 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000656 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000657 Py_XDECREF(c->c_globals);
658 Py_XDECREF(c->c_locals);
659 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000660 Py_XDECREF(c->c_freevars);
661 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000662 Py_XDECREF(c->c_lnotab);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000663 if (c->c_future)
664 PyMem_Free((void *)c->c_future);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000665}
666
667static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000668com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000669{
670 c->c_stacklevel += n;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000671 if (c->c_stacklevel > c->c_maxstacklevel) {
Guido van Rossum8b993a91997-01-17 21:04:03 +0000672 c->c_maxstacklevel = c->c_stacklevel;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000673 /*
674 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
675 c->c_filename, c->c_name, c->c_lineno,
676 c->c_nexti, c->c_stacklevel, n);
677 */
678 }
Guido van Rossum8b993a91997-01-17 21:04:03 +0000679}
680
681static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000682com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000683{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000684 if (c->c_stacklevel < n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000685 c->c_stacklevel = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000686 else
687 c->c_stacklevel -= n;
688}
689
690static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000691com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000692{
693 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000694 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000695 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000696 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000697}
698
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000699static int
700com_check_size(PyObject **s, int offset)
701{
702 int len = PyString_GET_SIZE(*s);
703 if (offset >= len)
704 return _PyString_Resize(s, len * 2);
705 return 0;
706}
707
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000708static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000709com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000710{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000711 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000712 assert(byte >= 0 && byte <= 255);
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000713 assert(c->c_code);
714 if (com_check_size(&c->c_code, c->c_nexti)) {
715 c->c_errors++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000716 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000717 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000718 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000719}
720
721static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000722com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000723{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000724 com_addbyte(c, x & 0xff);
725 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000726}
727
728static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000729com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000730{
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000731 char *p;
732 if (c->c_lnotab == NULL)
733 return;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000734 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
735 c->c_errors++;
736 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000737 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000738 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000739 *p++ = addr;
740 *p++ = line;
741 c->c_lnotab_next += 2;
742}
743
744static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000745com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000746{
747 c->c_lineno = lineno;
748 if (c->c_firstlineno == 0) {
749 c->c_firstlineno = c->c_last_line = lineno;
750 }
751 else {
752 int incr_addr = c->c_nexti - c->c_last_addr;
753 int incr_line = lineno - c->c_last_line;
Tim Peters2a7f3842001-06-09 09:26:21 +0000754 while (incr_addr > 255) {
755 com_add_lnotab(c, 255, 0);
756 incr_addr -= 255;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000757 }
Tim Peters2a7f3842001-06-09 09:26:21 +0000758 while (incr_line > 255) {
759 com_add_lnotab(c, incr_addr, 255);
760 incr_line -=255;
761 incr_addr = 0;
762 }
763 if (incr_addr > 0 || incr_line > 0)
764 com_add_lnotab(c, incr_addr, incr_line);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000765 c->c_last_addr = c->c_nexti;
766 c->c_last_line = lineno;
767 }
768}
769
770static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000771com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000772{
Fred Drakeef8ace32000-08-24 00:32:09 +0000773 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000774 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000775 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000776 if (Py_OptimizeFlag)
777 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000778 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000779 if (extended_arg){
780 com_addbyte(c, EXTENDED_ARG);
781 com_addint(c, extended_arg);
782 arg &= 0xffff;
783 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000784 com_addbyte(c, op);
785 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000786}
787
788static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000789com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000790{
791 /* Compile a forward reference for backpatching */
792 int here;
793 int anchor;
794 com_addbyte(c, op);
795 here = c->c_nexti;
796 anchor = *p_anchor;
797 *p_anchor = here;
798 com_addint(c, anchor == 0 ? 0 : here - anchor);
799}
800
801static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000802com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000803{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000804 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000805 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000806 int dist;
807 int prev;
808 for (;;) {
809 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000810 prev = code[anchor] + (code[anchor+1] << 8);
811 dist = target - (anchor+2);
812 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000813 dist >>= 8;
814 code[anchor+1] = dist;
815 dist >>= 8;
816 if (dist) {
817 com_error(c, PyExc_SystemError,
818 "com_backpatch: offset too large");
819 break;
820 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000821 if (!prev)
822 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000823 anchor -= prev;
824 }
825}
826
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000827/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000828
829static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000830com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000831{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000832 PyObject *w, *t, *np=NULL;
833 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000834
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000835 t = Py_BuildValue("(OO)", v, v->ob_type);
836 if (t == NULL)
837 goto fail;
838 w = PyDict_GetItem(dict, t);
839 if (w != NULL) {
840 n = PyInt_AsLong(w);
841 } else {
842 n = PyList_Size(list);
843 np = PyInt_FromLong(n);
844 if (np == NULL)
845 goto fail;
846 if (PyList_Append(list, v) != 0)
847 goto fail;
848 if (PyDict_SetItem(dict, t, np) != 0)
849 goto fail;
850 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000851 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000852 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000853 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000854 fail:
855 Py_XDECREF(np);
856 Py_XDECREF(t);
857 c->c_errors++;
858 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000859}
860
861static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000862com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000863{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000864 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000865}
866
867static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000868com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000869{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000870 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000871}
872
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000873static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000874mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000875{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000876 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000877 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000878 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000879 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
880 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000881 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000882 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000883 return 0; /* Don't mangle __extremely_long_names */
884 if (name[nlen-1] == '_' && name[nlen-2] == '_')
885 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000886 /* Strip leading underscores from class name */
887 while (*p == '_')
888 p++;
889 if (*p == '\0')
890 return 0; /* Don't mangle if class is just underscores */
891 plen = strlen(p);
892 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000893 plen = maxlen-nlen-2; /* Truncate class name if too long */
894 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000895 buffer[0] = '_';
896 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000897 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000898 return 1;
899}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000900
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000901static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000902com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000903{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000904 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000905 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000906 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000907
908 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000909 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000910 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000911 c->c_errors++;
912 i = 255;
913 }
914 else {
915 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000916 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000917 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000918 com_addoparg(c, op, i);
919}
920
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000921#define NAME_LOCAL 0
922#define NAME_GLOBAL 1
923#define NAME_DEFAULT 2
924#define NAME_CLOSURE 3
925
926static int
927com_lookup_arg(PyObject *dict, PyObject *name)
928{
929 PyObject *v = PyDict_GetItem(dict, name);
930 if (v == NULL)
931 return -1;
932 else
933 return PyInt_AS_LONG(v);
934}
935
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000936static void
937com_addop_varname(struct compiling *c, int kind, char *name)
938{
939 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000940 int i, reftype;
941 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000942 int op = STOP_CODE;
943 char buffer[MANGLE_LEN];
944
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000945 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000946 name = buffer;
947 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
948 c->c_errors++;
949 i = 255;
950 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000951 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000952
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000953 reftype = get_ref_type(c, name);
954 switch (reftype) {
955 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000956 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000957 scope = NAME_LOCAL;
958 break;
959 case GLOBAL_EXPLICIT:
960 scope = NAME_GLOBAL;
961 break;
962 case GLOBAL_IMPLICIT:
963 if (c->c_flags & CO_OPTIMIZED)
964 scope = NAME_GLOBAL;
965 break;
966 case FREE:
967 case CELL:
968 scope = NAME_CLOSURE;
969 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000970 }
971
972 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000973 if (scope == NAME_LOCAL)
974 i = com_lookup_arg(c->c_locals, v);
975 else if (reftype == FREE)
976 i = com_lookup_arg(c->c_freevars, v);
977 else if (reftype == CELL)
978 i = com_lookup_arg(c->c_cellvars, v);
979 if (i == -1) {
980 c->c_errors++; /* XXX no exception set */
981 i = 255;
982 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000983 }
984 Py_DECREF(v);
985
986 switch (kind) {
987 case VAR_LOAD:
988 switch (scope) {
989 case NAME_LOCAL:
990 op = LOAD_FAST;
991 break;
992 case NAME_GLOBAL:
993 op = LOAD_GLOBAL;
994 break;
995 case NAME_DEFAULT:
996 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000997 break;
998 case NAME_CLOSURE:
999 op = LOAD_DEREF;
1000 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001001 }
1002 break;
1003 case VAR_STORE:
1004 switch (scope) {
1005 case NAME_LOCAL:
1006 op = STORE_FAST;
1007 break;
1008 case NAME_GLOBAL:
1009 op = STORE_GLOBAL;
1010 break;
1011 case NAME_DEFAULT:
1012 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001013 break;
1014 case NAME_CLOSURE:
1015 op = STORE_DEREF;
1016 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001017 }
1018 break;
1019 case VAR_DELETE:
1020 switch (scope) {
1021 case NAME_LOCAL:
1022 op = DELETE_FAST;
1023 break;
1024 case NAME_GLOBAL:
1025 op = DELETE_GLOBAL;
1026 break;
1027 case NAME_DEFAULT:
1028 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001029 break;
1030 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00001031 char buf[500];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001032 sprintf(buf, DEL_CLOSURE_ERROR, name);
1033 com_error(c, PyExc_SyntaxError, buf);
1034 i = 255;
1035 break;
1036 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001037 }
1038 break;
1039 }
1040done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001041 com_addoparg(c, op, i);
1042}
1043
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001044static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001045com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001046{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001047 char *name;
1048 char buffer[1000];
1049 /* XXX it is possible to write this code without the 1000
1050 chars on the total length of dotted names, I just can't be
1051 bothered right now */
1052 if (TYPE(n) == STAR)
1053 name = "*";
1054 else if (TYPE(n) == dotted_name) {
1055 char *p = buffer;
1056 int i;
1057 name = buffer;
1058 for (i = 0; i < NCH(n); i += 2) {
1059 char *s = STR(CHILD(n, i));
1060 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001061 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001062 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001063 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001064 break;
1065 }
1066 if (p != buffer)
1067 *p++ = '.';
1068 strcpy(p, s);
1069 p = strchr(p, '\0');
1070 }
1071 }
1072 else {
1073 REQ(n, NAME);
1074 name = STR(n);
1075 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001076 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001077}
1078
Guido van Rossum79f25d91997-04-29 20:08:16 +00001079static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001080parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001081{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001082 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001083 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001084 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001085#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001086 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001087 int imflag;
1088#endif
1089
Guido van Rossum282914b1991-04-04 10:42:56 +00001090 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001091 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001092#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001093 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001094#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001095 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001096 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001097 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001098 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001099 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001100 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001101 if (*end == '\0') {
Jeremy Hylton71b6af92001-08-27 19:45:25 +00001102 if (errno != 0)
1103 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001104 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001105 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001106 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001107#ifndef WITHOUT_COMPLEX
1108 if (imflag) {
1109 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001110 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001111 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001112 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001113 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001114 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001115 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001116#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001117 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001118 PyFPE_START_PROTECT("atof", return 0)
1119 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001120 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001122 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001123}
1124
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001126parsestr(struct compiling *com, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001127{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001128 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001129 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001130 char *buf;
1131 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001132 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001133 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001134 int first = *s;
1135 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001136 int rawmode = 0;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001137#ifdef Py_USING_UNICODE
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001138 int unicode = 0;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001139#endif
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001140 if (isalpha(quote) || quote == '_') {
1141 if (quote == 'u' || quote == 'U') {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001142#ifdef Py_USING_UNICODE
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001143 quote = *++s;
1144 unicode = 1;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001145#else
1146 com_error(com, PyExc_SyntaxError,
1147 "Unicode literals not supported in this Python");
1148 return NULL;
1149#endif
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001150 }
1151 if (quote == 'r' || quote == 'R') {
1152 quote = *++s;
1153 rawmode = 1;
1154 }
1155 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001156 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001157 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001158 return NULL;
1159 }
1160 s++;
1161 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001162 if (len > INT_MAX) {
1163 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1164 return NULL;
1165 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001166 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001168 return NULL;
1169 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001170 if (len >= 4 && s[0] == quote && s[1] == quote) {
1171 s += 2;
1172 len -= 2;
1173 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001174 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001175 return NULL;
1176 }
1177 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001178#ifdef Py_USING_UNICODE
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001179 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001180 if (rawmode)
1181 return PyUnicode_DecodeRawUnicodeEscape(
1182 s, len, NULL);
1183 else
1184 return PyUnicode_DecodeUnicodeEscape(
1185 s, len, NULL);
1186 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001187#endif
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001188 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001189 return PyString_FromStringAndSize(s, len);
1190 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001191 if (v == NULL)
1192 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001194 end = s + len;
1195 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001196 if (*s != '\\') {
1197 *p++ = *s++;
1198 continue;
1199 }
1200 s++;
1201 switch (*s++) {
1202 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001203 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001204 case '\\': *p++ = '\\'; break;
1205 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001206 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001207 case 'b': *p++ = '\b'; break;
1208 case 'f': *p++ = '\014'; break; /* FF */
1209 case 't': *p++ = '\t'; break;
1210 case 'n': *p++ = '\n'; break;
1211 case 'r': *p++ = '\r'; break;
1212 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001213 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1214 case '0': case '1': case '2': case '3':
1215 case '4': case '5': case '6': case '7':
1216 c = s[-1] - '0';
1217 if ('0' <= *s && *s <= '7') {
1218 c = (c<<3) + *s++ - '0';
1219 if ('0' <= *s && *s <= '7')
1220 c = (c<<3) + *s++ - '0';
1221 }
1222 *p++ = c;
1223 break;
1224 case 'x':
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001225 if (isxdigit(Py_CHARMASK(s[0]))
1226 && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001227 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001228 c = Py_CHARMASK(*s);
1229 s++;
1230 if (isdigit(c))
1231 x = c - '0';
1232 else if (islower(c))
1233 x = 10 + c - 'a';
1234 else
1235 x = 10 + c - 'A';
1236 x = x << 4;
1237 c = Py_CHARMASK(*s);
1238 s++;
1239 if (isdigit(c))
1240 x += c - '0';
1241 else if (islower(c))
1242 x += 10 + c - 'a';
1243 else
1244 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001245 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001246 break;
1247 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001248 PyErr_SetString(PyExc_ValueError,
1249 "invalid \\x escape");
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001250 Py_DECREF(v);
1251 return NULL;
1252 default:
1253 *p++ = '\\';
1254 *p++ = s[-1];
1255 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001256 }
1257 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001259 return v;
1260}
1261
Guido van Rossum79f25d91997-04-29 20:08:16 +00001262static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001263parsestrplus(struct compiling* c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001264{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001265 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001266 int i;
1267 REQ(CHILD(n, 0), STRING);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001268 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001269 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001270 for (i = 1; i < NCH(n); i++) {
1271 PyObject *s;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001272 s = parsestr(c, STR(CHILD(n, i)));
Fred Drake4e998bc2000-04-13 14:10:44 +00001273 if (s == NULL)
1274 goto onError;
1275 if (PyString_Check(v) && PyString_Check(s)) {
1276 PyString_ConcatAndDel(&v, s);
1277 if (v == NULL)
1278 goto onError;
1279 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001280#ifdef Py_USING_UNICODE
Fred Drake4e998bc2000-04-13 14:10:44 +00001281 else {
1282 PyObject *temp;
1283 temp = PyUnicode_Concat(v, s);
1284 Py_DECREF(s);
1285 if (temp == NULL)
1286 goto onError;
1287 Py_DECREF(v);
1288 v = temp;
1289 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001290#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001291 }
1292 }
1293 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001294
1295 onError:
1296 Py_XDECREF(v);
1297 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001298}
1299
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001300static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001301com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001302{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001303 int anchor = 0;
1304 int save_begin = c->c_begin;
1305
1306 /* list_iter: for v in expr [list_iter] */
1307 com_node(c, CHILD(n, 3)); /* expr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001308 com_addbyte(c, GET_ITER);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001309 c->c_begin = c->c_nexti;
1310 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001311 com_addfwref(c, FOR_ITER, &anchor);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001312 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001313 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001314 c->c_loops++;
1315 com_list_iter(c, n, e, t);
1316 c->c_loops--;
1317 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1318 c->c_begin = save_begin;
1319 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001320 com_pop(c, 1); /* FOR_ITER has popped this */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001321}
1322
1323static void
1324com_list_if(struct compiling *c, node *n, node *e, char *t)
1325{
1326 int anchor = 0;
1327 int a = 0;
1328 /* list_iter: 'if' test [list_iter] */
1329 com_addoparg(c, SET_LINENO, n->n_lineno);
1330 com_node(c, CHILD(n, 1));
1331 com_addfwref(c, JUMP_IF_FALSE, &a);
1332 com_addbyte(c, POP_TOP);
1333 com_pop(c, 1);
1334 com_list_iter(c, n, e, t);
1335 com_addfwref(c, JUMP_FORWARD, &anchor);
1336 com_backpatch(c, a);
1337 /* We jump here with an extra entry which we now pop */
1338 com_addbyte(c, POP_TOP);
1339 com_backpatch(c, anchor);
1340}
1341
1342static void
1343com_list_iter(struct compiling *c,
1344 node *p, /* parent of list_iter node */
1345 node *e, /* element expression node */
1346 char *t /* name of result list temp local */)
1347{
1348 /* list_iter is the last child in a listmaker, list_for, or list_if */
1349 node *n = CHILD(p, NCH(p)-1);
1350 if (TYPE(n) == list_iter) {
1351 n = CHILD(n, 0);
1352 switch (TYPE(n)) {
1353 case list_for:
1354 com_list_for(c, n, e, t);
1355 break;
1356 case list_if:
1357 com_list_if(c, n, e, t);
1358 break;
1359 default:
1360 com_error(c, PyExc_SystemError,
1361 "invalid list_iter node type");
1362 }
1363 }
1364 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001365 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001366 com_push(c, 1);
1367 com_node(c, e);
1368 com_addoparg(c, CALL_FUNCTION, 1);
1369 com_addbyte(c, POP_TOP);
1370 com_pop(c, 2);
1371 }
1372}
1373
1374static void
1375com_list_comprehension(struct compiling *c, node *n)
1376{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001377 /* listmaker: test list_for */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001378 char tmpname[12];
Tim Peters30814212001-02-17 05:30:26 +00001379 sprintf(tmpname, "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001380 com_addoparg(c, BUILD_LIST, 0);
1381 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1382 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001383 com_addop_name(c, LOAD_ATTR, "append");
1384 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001385 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001386 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001387 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001388 --c->c_tmpname;
1389}
1390
1391static void
1392com_listmaker(struct compiling *c, node *n)
1393{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001394 /* listmaker: test ( list_for | (',' test)* [','] ) */
1395 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001396 com_list_comprehension(c, n);
1397 else {
1398 int len = 0;
1399 int i;
1400 for (i = 0; i < NCH(n); i += 2, len++)
1401 com_node(c, CHILD(n, i));
1402 com_addoparg(c, BUILD_LIST, len);
1403 com_pop(c, len-1);
1404 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001405}
1406
1407static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001408com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001409{
1410 int i;
1411 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1412 for (i = 0; i+2 < NCH(n); i += 4) {
1413 /* We must arrange things just right for STORE_SUBSCR.
1414 It wants the stack to look like (value) (dict) (key) */
1415 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001416 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001417 com_node(c, CHILD(n, i+2)); /* value */
1418 com_addbyte(c, ROT_TWO);
1419 com_node(c, CHILD(n, i)); /* key */
1420 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001421 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001422 }
1423}
1424
1425static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001426com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001427{
1428 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001429 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001430 int i;
1431 REQ(n, atom);
1432 ch = CHILD(n, 0);
1433 switch (TYPE(ch)) {
1434 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001435 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001436 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001437 com_push(c, 1);
1438 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001439 else
1440 com_node(c, CHILD(n, 1));
1441 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001442 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001443 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001444 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001445 com_push(c, 1);
1446 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001447 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001448 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001449 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001450 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001451 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001452 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001453 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001454 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001455 break;
1456 case BACKQUOTE:
1457 com_node(c, CHILD(n, 1));
1458 com_addbyte(c, UNARY_CONVERT);
1459 break;
1460 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001461 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001462 i = 255;
1463 }
1464 else {
1465 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001466 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001467 }
1468 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001469 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001470 break;
1471 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001472 v = parsestrplus(c, n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001473 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001474 c->c_errors++;
1475 i = 255;
1476 }
1477 else {
1478 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001479 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001480 }
1481 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001482 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001483 break;
1484 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001485 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001486 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001487 break;
1488 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001489 com_error(c, PyExc_SystemError,
1490 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001491 }
1492}
1493
1494static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001495com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001496{
1497 if (NCH(n) == 1) {
1498 com_addbyte(c, op);
1499 }
1500 else if (NCH(n) == 2) {
1501 if (TYPE(CHILD(n, 0)) != COLON) {
1502 com_node(c, CHILD(n, 0));
1503 com_addbyte(c, op+1);
1504 }
1505 else {
1506 com_node(c, CHILD(n, 1));
1507 com_addbyte(c, op+2);
1508 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001509 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001510 }
1511 else {
1512 com_node(c, CHILD(n, 0));
1513 com_node(c, CHILD(n, 2));
1514 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001515 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001516 }
1517}
1518
Guido van Rossum635abd21997-01-06 22:56:52 +00001519static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001520com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1521{
1522 if (NCH(n) == 1) {
1523 com_addbyte(c, DUP_TOP);
1524 com_push(c, 1);
1525 com_addbyte(c, SLICE);
1526 com_node(c, augn);
1527 com_addbyte(c, opcode);
1528 com_pop(c, 1);
1529 com_addbyte(c, ROT_TWO);
1530 com_addbyte(c, STORE_SLICE);
1531 com_pop(c, 2);
1532 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1533 com_node(c, CHILD(n, 0));
1534 com_addoparg(c, DUP_TOPX, 2);
1535 com_push(c, 2);
1536 com_addbyte(c, SLICE+1);
1537 com_pop(c, 1);
1538 com_node(c, augn);
1539 com_addbyte(c, opcode);
1540 com_pop(c, 1);
1541 com_addbyte(c, ROT_THREE);
1542 com_addbyte(c, STORE_SLICE+1);
1543 com_pop(c, 3);
1544 } else if (NCH(n) == 2) {
1545 com_node(c, CHILD(n, 1));
1546 com_addoparg(c, DUP_TOPX, 2);
1547 com_push(c, 2);
1548 com_addbyte(c, SLICE+2);
1549 com_pop(c, 1);
1550 com_node(c, augn);
1551 com_addbyte(c, opcode);
1552 com_pop(c, 1);
1553 com_addbyte(c, ROT_THREE);
1554 com_addbyte(c, STORE_SLICE+2);
1555 com_pop(c, 3);
1556 } else {
1557 com_node(c, CHILD(n, 0));
1558 com_node(c, CHILD(n, 2));
1559 com_addoparg(c, DUP_TOPX, 3);
1560 com_push(c, 3);
1561 com_addbyte(c, SLICE+3);
1562 com_pop(c, 2);
1563 com_node(c, augn);
1564 com_addbyte(c, opcode);
1565 com_pop(c, 1);
1566 com_addbyte(c, ROT_FOUR);
1567 com_addbyte(c, STORE_SLICE+3);
1568 com_pop(c, 4);
1569 }
1570}
1571
1572static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001573com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001574{
1575 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001576 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001577 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001578 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001579 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001580 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001581 }
1582 else {
1583 com_node(c, CHILD(n, 0));
1584 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001585 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001586 }
1587 m = n;
1588 do {
1589 m = CHILD(m, 0);
1590 } while (NCH(m) == 1);
1591 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001592 /* f(lambda x: x[0] = 3) ends up getting parsed with
1593 * LHS test = lambda x: x[0], and RHS test = 3.
1594 * SF bug 132313 points out that complaining about a keyword
1595 * then is very confusing.
1596 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001597 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001598 TYPE(m) == lambdef ?
1599 "lambda cannot contain assignment" :
1600 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001601 }
1602 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001603 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001604 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 *pkeywords = PyDict_New();
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001606 if (v == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001607 c->c_errors++;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001608 else if (*pkeywords == NULL) {
1609 c->c_errors++;
1610 Py_DECREF(v);
1611 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001612 if (PyDict_GetItem(*pkeywords, v) != NULL)
1613 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001614 "duplicate keyword argument");
1615 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001616 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001617 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001618 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001619 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001620 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001621 }
1622 }
1623 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001624}
1625
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001626static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001627com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001628{
1629 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001630 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001631 }
1632 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001633 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001634 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001635 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001636 int star_flag = 0;
1637 int starstar_flag = 0;
1638 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001639 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001640 na = 0;
1641 nk = 0;
1642 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001643 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001644 if (TYPE(ch) == STAR ||
1645 TYPE(ch) == DOUBLESTAR)
1646 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001647 if (ch->n_lineno != lineno) {
1648 lineno = ch->n_lineno;
1649 com_addoparg(c, SET_LINENO, lineno);
1650 }
1651 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001652 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001653 na++;
1654 else
1655 nk++;
1656 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001657 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001658 while (i < NCH(n)) {
1659 node *tok = CHILD(n, i);
1660 node *ch = CHILD(n, i+1);
1661 i += 3;
1662 switch (TYPE(tok)) {
1663 case STAR: star_flag = 1; break;
1664 case DOUBLESTAR: starstar_flag = 1; break;
1665 }
1666 com_node(c, ch);
1667 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001668 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001669 com_error(c, PyExc_SyntaxError,
1670 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001671 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001672 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001673 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001674 star_flag + (starstar_flag << 1);
1675 else
1676 opcode = CALL_FUNCTION;
1677 com_addoparg(c, opcode, na | (nk << 8));
1678 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001679 }
1680}
1681
1682static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001683com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001684{
1685 com_addopname(c, LOAD_ATTR, n);
1686}
1687
1688static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001689com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001690{
1691 int i=0;
1692 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001693 node *ch;
1694
1695 /* first argument */
1696 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001697 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001698 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001699 i++;
1700 }
1701 else {
1702 com_node(c, CHILD(n,i));
1703 i++;
1704 REQ(CHILD(n,i),COLON);
1705 i++;
1706 }
1707 /* second argument */
1708 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1709 com_node(c, CHILD(n,i));
1710 i++;
1711 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001712 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001713 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001714 com_push(c, 1);
1715 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001716 /* remaining arguments */
1717 for (; i < NCH(n); i++) {
1718 ns++;
1719 ch=CHILD(n,i);
1720 REQ(ch, sliceop);
1721 if (NCH(ch) == 1) {
1722 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001723 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001724 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001725 }
1726 else
1727 com_node(c, CHILD(ch,1));
1728 }
1729 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001730 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001731}
1732
1733static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001734com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001735{
1736 node *ch;
1737 REQ(n, subscript);
1738 ch = CHILD(n,0);
1739 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001740 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001741 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001742 com_push(c, 1);
1743 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001744 else {
1745 /* check for slice */
1746 if ((TYPE(ch) == COLON || NCH(n) > 1))
1747 com_sliceobj(c, n);
1748 else {
1749 REQ(ch, test);
1750 com_node(c, ch);
1751 }
1752 }
1753}
1754
1755static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001756com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001757{
1758 int i, op;
1759 REQ(n, subscriptlist);
1760 /* Check to make backward compatible slice behavior for '[i:j]' */
1761 if (NCH(n) == 1) {
1762 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001763 /* 'Basic' slice, should have exactly one colon. */
1764 if ((TYPE(CHILD(sub, 0)) == COLON
1765 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1766 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1767 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001768 switch (assigning) {
1769 case OP_DELETE:
1770 op = DELETE_SLICE;
1771 break;
1772 case OP_ASSIGN:
1773 op = STORE_SLICE;
1774 break;
1775 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001776 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001777 break;
1778 default:
1779 com_augassign_slice(c, sub, assigning, augn);
1780 return;
1781 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001782 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001783 if (op == STORE_SLICE)
1784 com_pop(c, 2);
1785 else if (op == DELETE_SLICE)
1786 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001787 return;
1788 }
1789 }
1790 /* Else normal subscriptlist. Compile each subscript. */
1791 for (i = 0; i < NCH(n); i += 2)
1792 com_subscript(c, CHILD(n, i));
1793 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001794 if (NCH(n) > 1) {
1795 i = (NCH(n)+1) / 2;
1796 com_addoparg(c, BUILD_TUPLE, i);
1797 com_pop(c, i-1);
1798 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001799 switch (assigning) {
1800 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001801 op = DELETE_SUBSCR;
1802 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001803 break;
1804 default:
1805 case OP_ASSIGN:
1806 op = STORE_SUBSCR;
1807 i = 3;
1808 break;
1809 case OP_APPLY:
1810 op = BINARY_SUBSCR;
1811 i = 1;
1812 break;
1813 }
1814 if (assigning > OP_APPLY) {
1815 com_addoparg(c, DUP_TOPX, 2);
1816 com_push(c, 2);
1817 com_addbyte(c, BINARY_SUBSCR);
1818 com_pop(c, 1);
1819 com_node(c, augn);
1820 com_addbyte(c, assigning);
1821 com_pop(c, 1);
1822 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001823 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001824 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001825 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001826}
1827
1828static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001829com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001830{
1831 REQ(n, trailer);
1832 switch (TYPE(CHILD(n, 0))) {
1833 case LPAR:
1834 com_call_function(c, CHILD(n, 1));
1835 break;
1836 case DOT:
1837 com_select_member(c, CHILD(n, 1));
1838 break;
1839 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001840 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001841 break;
1842 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001843 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001844 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001845 }
1846}
1847
1848static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001849com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001850{
1851 int i;
1852 REQ(n, power);
1853 com_atom(c, CHILD(n, 0));
1854 for (i = 1; i < NCH(n); i++) {
1855 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1856 com_factor(c, CHILD(n, i+1));
1857 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001858 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001859 break;
1860 }
1861 else
1862 com_apply_trailer(c, CHILD(n, i));
1863 }
1864}
1865
1866static void
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001867com_invert_constant(struct compiling *c, node *n)
1868{
1869 /* Compute the inverse of int and longs and use them directly,
1870 but be prepared to generate code for all other
1871 possibilities (invalid numbers, floats, complex).
1872 */
1873 PyObject *num, *inv = NULL;
1874 int i;
1875
1876 REQ(n, NUMBER);
1877 num = parsenumber(c, STR(n));
1878 if (num == NULL)
1879 i = 255;
1880 else {
1881 inv = PyNumber_Invert(num);
1882 if (inv == NULL) {
1883 PyErr_Clear();
1884 i = com_addconst(c, num);
1885 } else {
1886 i = com_addconst(c, inv);
1887 Py_DECREF(inv);
1888 }
1889 Py_DECREF(num);
1890 }
1891 com_addoparg(c, LOAD_CONST, i);
1892 com_push(c, 1);
1893 if (num != NULL && inv == NULL)
1894 com_addbyte(c, UNARY_INVERT);
1895}
1896
Tim Peters51e26512001-09-07 08:45:55 +00001897static int
1898is_float_zero(const char *p)
1899{
1900 int found_radix_point = 0;
1901 int ch;
1902 while ((ch = Py_CHARMASK(*p++)) != '\0') {
1903 switch (ch) {
1904 case '0':
1905 /* no reason to believe it's not 0 -- continue */
1906 break;
1907
1908 case 'e': case 'E': case 'j': case 'J':
1909 /* If this was a hex constant, we already would have
1910 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1911 must be an exponent marker, and we haven't yet
1912 seen a non-zero digit, and it doesn't matter what
1913 the exponent is then. For 'j' or 'J' similarly,
1914 except that this is an imaginary 0 then. */
1915 return 1;
1916
1917 case '.':
1918 found_radix_point = 1;
1919 break;
1920
1921 default:
1922 return 0;
1923 }
1924 }
1925 return found_radix_point;
1926}
1927
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001928static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001929com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001930{
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001931 int childtype = TYPE(CHILD(n, 0));
Tim Peters51e26512001-09-07 08:45:55 +00001932 node *pfactor, *ppower, *patom, *pnum;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001933 REQ(n, factor);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001934 /* If the unary +, -, or ~ operator is applied to a constant,
Tim Peters51e26512001-09-07 08:45:55 +00001935 don't generate a UNARY_xxx opcode. Just store the
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001936 approriate value as a constant. If the value is negative,
1937 extend the string containing the constant and insert a
Tim Peters51e26512001-09-07 08:45:55 +00001938 negative in the 0th position -- unless we're doing unary minus
1939 of a floating zero! In that case the sign is significant, but
1940 the const dict can't distinguish +0.0 from -0.0.
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001941 */
1942 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
Fred Drake14ef2442001-08-30 18:53:25 +00001943 && NCH(n) == 2
Tim Peters51e26512001-09-07 08:45:55 +00001944 && TYPE((pfactor = CHILD(n, 1))) == factor
1945 && NCH(pfactor) == 1
1946 && TYPE((ppower = CHILD(pfactor, 0))) == power
1947 && NCH(ppower) == 1
1948 && TYPE((patom = CHILD(ppower, 0))) == atom
1949 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
1950 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001951 if (childtype == TILDE) {
Tim Peters51e26512001-09-07 08:45:55 +00001952 com_invert_constant(c, pnum);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001953 return;
1954 }
1955 if (childtype == MINUS) {
Tim Peters51e26512001-09-07 08:45:55 +00001956 char *s = malloc(strlen(STR(pnum)) + 2);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001957 if (s == NULL) {
1958 com_error(c, PyExc_MemoryError, "");
1959 com_addbyte(c, 255);
1960 return;
1961 }
1962 s[0] = '-';
Tim Peters51e26512001-09-07 08:45:55 +00001963 strcpy(s + 1, STR(pnum));
1964 free(STR(pnum));
1965 STR(pnum) = s;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001966 }
Tim Peters51e26512001-09-07 08:45:55 +00001967 com_atom(c, patom);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001968 }
1969 else if (childtype == PLUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001970 com_factor(c, CHILD(n, 1));
1971 com_addbyte(c, UNARY_POSITIVE);
1972 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001973 else if (childtype == MINUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001974 com_factor(c, CHILD(n, 1));
1975 com_addbyte(c, UNARY_NEGATIVE);
1976 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001977 else if (childtype == TILDE) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001978 com_factor(c, CHILD(n, 1));
1979 com_addbyte(c, UNARY_INVERT);
1980 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001981 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001982 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001983 }
1984}
1985
1986static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001987com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001988{
1989 int i;
1990 int op;
1991 REQ(n, term);
1992 com_factor(c, CHILD(n, 0));
1993 for (i = 2; i < NCH(n); i += 2) {
1994 com_factor(c, CHILD(n, i));
1995 switch (TYPE(CHILD(n, i-1))) {
1996 case STAR:
1997 op = BINARY_MULTIPLY;
1998 break;
1999 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00002000 if (c->c_flags & CO_FUTURE_DIVISION)
2001 op = BINARY_TRUE_DIVIDE;
2002 else
2003 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002004 break;
2005 case PERCENT:
2006 op = BINARY_MODULO;
2007 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002008 case DOUBLESLASH:
2009 op = BINARY_FLOOR_DIVIDE;
2010 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002011 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002012 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00002013 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002014 op = 255;
2015 }
2016 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002017 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002018 }
2019}
2020
2021static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002022com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002023{
2024 int i;
2025 int op;
2026 REQ(n, arith_expr);
2027 com_term(c, CHILD(n, 0));
2028 for (i = 2; i < NCH(n); i += 2) {
2029 com_term(c, CHILD(n, i));
2030 switch (TYPE(CHILD(n, i-1))) {
2031 case PLUS:
2032 op = BINARY_ADD;
2033 break;
2034 case MINUS:
2035 op = BINARY_SUBTRACT;
2036 break;
2037 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002038 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002039 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002040 op = 255;
2041 }
2042 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002043 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002044 }
2045}
2046
2047static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002048com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002049{
2050 int i;
2051 int op;
2052 REQ(n, shift_expr);
2053 com_arith_expr(c, CHILD(n, 0));
2054 for (i = 2; i < NCH(n); i += 2) {
2055 com_arith_expr(c, CHILD(n, i));
2056 switch (TYPE(CHILD(n, i-1))) {
2057 case LEFTSHIFT:
2058 op = BINARY_LSHIFT;
2059 break;
2060 case RIGHTSHIFT:
2061 op = BINARY_RSHIFT;
2062 break;
2063 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002064 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002065 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002066 op = 255;
2067 }
2068 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002069 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002070 }
2071}
2072
2073static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002074com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002075{
2076 int i;
2077 int op;
2078 REQ(n, and_expr);
2079 com_shift_expr(c, CHILD(n, 0));
2080 for (i = 2; i < NCH(n); i += 2) {
2081 com_shift_expr(c, CHILD(n, i));
2082 if (TYPE(CHILD(n, i-1)) == AMPER) {
2083 op = BINARY_AND;
2084 }
2085 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002086 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002087 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002088 op = 255;
2089 }
2090 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002091 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002092 }
2093}
2094
2095static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002096com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002097{
2098 int i;
2099 int op;
2100 REQ(n, xor_expr);
2101 com_and_expr(c, CHILD(n, 0));
2102 for (i = 2; i < NCH(n); i += 2) {
2103 com_and_expr(c, CHILD(n, i));
2104 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2105 op = BINARY_XOR;
2106 }
2107 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002108 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002109 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002110 op = 255;
2111 }
2112 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002113 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002114 }
2115}
2116
2117static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002118com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002119{
2120 int i;
2121 int op;
2122 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002123 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002124 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002125 com_xor_expr(c, CHILD(n, i));
2126 if (TYPE(CHILD(n, i-1)) == VBAR) {
2127 op = BINARY_OR;
2128 }
2129 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002130 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002131 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002132 op = 255;
2133 }
2134 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002135 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002136 }
2137}
2138
2139static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002140cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002141{
2142 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002143 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002144 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2145 if (NCH(n) == 1) {
2146 n = CHILD(n, 0);
2147 switch (TYPE(n)) {
2148 case LESS: return LT;
2149 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002150 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002151 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002152 case LESSEQUAL: return LE;
2153 case GREATEREQUAL: return GE;
2154 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002155 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2156 if (strcmp(STR(n), "is") == 0) return IS;
2157 }
2158 }
2159 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002160 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002161 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2162 return NOT_IN;
2163 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2164 return IS_NOT;
2165 }
2166 }
2167 return BAD;
2168}
2169
2170static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002171com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002172{
2173 int i;
2174 enum cmp_op op;
2175 int anchor;
2176 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2177 com_expr(c, CHILD(n, 0));
2178 if (NCH(n) == 1)
2179 return;
2180
2181 /****************************************************************
2182 The following code is generated for all but the last
2183 comparison in a chain:
2184
2185 label: on stack: opcode: jump to:
2186
2187 a <code to load b>
2188 a, b DUP_TOP
2189 a, b, b ROT_THREE
2190 b, a, b COMPARE_OP
2191 b, 0-or-1 JUMP_IF_FALSE L1
2192 b, 1 POP_TOP
2193 b
2194
2195 We are now ready to repeat this sequence for the next
2196 comparison in the chain.
2197
2198 For the last we generate:
2199
2200 b <code to load c>
2201 b, c COMPARE_OP
2202 0-or-1
2203
2204 If there were any jumps to L1 (i.e., there was more than one
2205 comparison), we generate:
2206
2207 0-or-1 JUMP_FORWARD L2
2208 L1: b, 0 ROT_TWO
2209 0, b POP_TOP
2210 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002211 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002212 ****************************************************************/
2213
2214 anchor = 0;
2215
2216 for (i = 2; i < NCH(n); i += 2) {
2217 com_expr(c, CHILD(n, i));
2218 if (i+2 < NCH(n)) {
2219 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002220 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002221 com_addbyte(c, ROT_THREE);
2222 }
2223 op = cmp_type(CHILD(n, i-1));
2224 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002225 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002226 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002227 }
2228 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002229 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002230 if (i+2 < NCH(n)) {
2231 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2232 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002233 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002234 }
2235 }
2236
2237 if (anchor) {
2238 int anchor2 = 0;
2239 com_addfwref(c, JUMP_FORWARD, &anchor2);
2240 com_backpatch(c, anchor);
2241 com_addbyte(c, ROT_TWO);
2242 com_addbyte(c, POP_TOP);
2243 com_backpatch(c, anchor2);
2244 }
2245}
2246
2247static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002248com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002249{
2250 REQ(n, not_test); /* 'not' not_test | comparison */
2251 if (NCH(n) == 1) {
2252 com_comparison(c, CHILD(n, 0));
2253 }
2254 else {
2255 com_not_test(c, CHILD(n, 1));
2256 com_addbyte(c, UNARY_NOT);
2257 }
2258}
2259
2260static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002261com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002262{
2263 int i;
2264 int anchor;
2265 REQ(n, and_test); /* not_test ('and' not_test)* */
2266 anchor = 0;
2267 i = 0;
2268 for (;;) {
2269 com_not_test(c, CHILD(n, i));
2270 if ((i += 2) >= NCH(n))
2271 break;
2272 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2273 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002274 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002275 }
2276 if (anchor)
2277 com_backpatch(c, anchor);
2278}
2279
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002280static int
2281com_make_closure(struct compiling *c, PyCodeObject *co)
2282{
2283 int i, free = PyTuple_GET_SIZE(co->co_freevars);
2284 if (free == 0)
2285 return 0;
2286 for (i = 0; i < free; ++i) {
2287 /* Bypass com_addop_varname because it will generate
2288 LOAD_DEREF but LOAD_CLOSURE is needed.
2289 */
2290 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2291 int arg, reftype;
2292
2293 /* Special case: If a class contains a method with a
2294 free variable that has the same name as a method,
2295 the name will be considered free *and* local in the
2296 class. It should be handled by the closure, as
2297 well as by the normal name loookup logic.
2298 */
2299 reftype = get_ref_type(c, PyString_AS_STRING(name));
2300 if (reftype == CELL)
2301 arg = com_lookup_arg(c->c_cellvars, name);
2302 else /* (reftype == FREE) */
2303 arg = com_lookup_arg(c->c_freevars, name);
2304 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002305 fprintf(stderr, "lookup %s in %s %d %d\n"
2306 "freevars of %s: %s\n",
2307 PyObject_REPR(name),
2308 c->c_name,
2309 reftype, arg,
2310 PyString_AS_STRING(co->co_name),
2311 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002312 Py_FatalError("com_make_closure()");
2313 }
2314 com_addoparg(c, LOAD_CLOSURE, arg);
2315
2316 }
2317 com_push(c, free);
2318 return 1;
2319}
2320
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002321static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002322com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002323{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002324 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002325 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002326 PyCodeObject *co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002327 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002328 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002329 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2330 n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002331 co = icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002332 if (co == NULL) {
2333 c->c_errors++;
2334 return;
2335 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002336 symtable_exit_scope(c->c_symtable);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002337 i = com_addconst(c, (PyObject *)co);
2338 closure = com_make_closure(c, co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002339 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002340 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002341 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002342 com_addoparg(c, MAKE_CLOSURE, ndefs);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002343 com_pop(c, PyTuple_GET_SIZE(co->co_freevars));
2344 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002345 com_addoparg(c, MAKE_FUNCTION, ndefs);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002346 Py_DECREF(co);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002347 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002348 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002349 else {
2350 int anchor = 0;
2351 int i = 0;
2352 for (;;) {
2353 com_and_test(c, CHILD(n, i));
2354 if ((i += 2) >= NCH(n))
2355 break;
2356 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2357 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002358 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002359 }
2360 if (anchor)
2361 com_backpatch(c, anchor);
2362 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002363}
2364
2365static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002366com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002367{
2368 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002369 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002370 com_node(c, CHILD(n, 0));
2371 }
2372 else {
2373 int i;
2374 int len;
2375 len = (NCH(n) + 1) / 2;
2376 for (i = 0; i < NCH(n); i += 2)
2377 com_node(c, CHILD(n, i));
2378 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002379 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002380 }
2381}
2382
2383
2384/* Begin of assignment compilation */
2385
Thomas Wouters434d0822000-08-24 20:11:32 +00002386
2387static void
2388com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2389{
2390 com_addbyte(c, DUP_TOP);
2391 com_push(c, 1);
2392 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002393 com_node(c, augn);
2394 com_addbyte(c, opcode);
2395 com_pop(c, 1);
2396 com_addbyte(c, ROT_TWO);
2397 com_addopname(c, STORE_ATTR, n);
2398 com_pop(c, 2);
2399}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002400
2401static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002402com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002403{
2404 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002405 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002406}
2407
2408static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002409com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002410{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002411 REQ(n, trailer);
2412 switch (TYPE(CHILD(n, 0))) {
2413 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002414 com_error(c, PyExc_SyntaxError,
2415 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002416 break;
2417 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002418 if (assigning > OP_APPLY)
2419 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2420 else
2421 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002422 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002423 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002424 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002425 break;
2426 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002427 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002428 }
2429}
2430
2431static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002432com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002433{
2434 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002435 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002436 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002437 if (assigning) {
2438 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002439 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002440 com_push(c, i-1);
2441 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002442 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002443 com_assign(c, CHILD(n, i), assigning, NULL);
2444}
2445
2446static void
2447com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2448{
2449 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002450 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002451 com_push(c, 1);
2452 com_node(c, augn);
2453 com_addbyte(c, opcode);
2454 com_pop(c, 1);
2455 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002456}
2457
2458static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002459com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002460{
2461 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002462 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002463 if (assigning)
2464 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002465}
2466
2467static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002468com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002469{
2470 /* Loop to avoid trivial recursion */
2471 for (;;) {
2472 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002473
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002474 case exprlist:
2475 case testlist:
2476 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002477 if (assigning > OP_APPLY) {
2478 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002479 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002480 return;
2481 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002482 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002483 return;
2484 }
2485 n = CHILD(n, 0);
2486 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002487
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002488 case test:
2489 case and_test:
2490 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002491 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002492 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002493 case xor_expr:
2494 case and_expr:
2495 case shift_expr:
2496 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002497 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002498 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002499 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002500 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002501 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002502 return;
2503 }
2504 n = CHILD(n, 0);
2505 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002506
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002507 case power: /* atom trailer* ('**' power)*
2508 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002509 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002510 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002511 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002512 return;
2513 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002514 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002515 int i;
2516 com_node(c, CHILD(n, 0));
2517 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002518 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002519 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002520 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002521 return;
2522 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002523 com_apply_trailer(c, CHILD(n, i));
2524 } /* NB i is still alive */
2525 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002526 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002527 return;
2528 }
2529 n = CHILD(n, 0);
2530 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002531
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002532 case atom:
2533 switch (TYPE(CHILD(n, 0))) {
2534 case LPAR:
2535 n = CHILD(n, 1);
2536 if (TYPE(n) == RPAR) {
2537 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002538 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002539 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002540 return;
2541 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002542 if (assigning > OP_APPLY) {
2543 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002544 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002545 return;
2546 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002547 break;
2548 case LSQB:
2549 n = CHILD(n, 1);
2550 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002551 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002552 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002553 return;
2554 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002555 if (assigning > OP_APPLY) {
2556 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002557 "augmented assign to list not possible");
2558 return;
2559 }
2560 if (NCH(n) > 1
2561 && TYPE(CHILD(n, 1)) == list_for) {
2562 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002563 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002564 return;
2565 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002566 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002567 return;
2568 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002569 if (assigning > OP_APPLY)
2570 com_augassign_name(c, CHILD(n, 0),
2571 assigning, augn);
2572 else
2573 com_assign_name(c, CHILD(n, 0),
2574 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002575 return;
2576 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002577 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002578 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002579 return;
2580 }
2581 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002582
2583 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002584 com_error(c, PyExc_SyntaxError,
2585 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002586 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002587
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002588 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002589 com_error(c, PyExc_SystemError,
2590 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002591 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002592
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002593 }
2594 }
2595}
Guido van Rossum7c531111997-03-11 18:42:21 +00002596
Thomas Wouters434d0822000-08-24 20:11:32 +00002597static void
2598com_augassign(struct compiling *c, node *n)
2599{
2600 int opcode;
2601
2602 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2603 case '+': opcode = INPLACE_ADD; break;
2604 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002605 case '/':
2606 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2607 opcode = INPLACE_FLOOR_DIVIDE;
2608 else if (c->c_flags & CO_FUTURE_DIVISION)
2609 opcode = INPLACE_TRUE_DIVIDE;
2610 else
2611 opcode = INPLACE_DIVIDE;
2612 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002613 case '%': opcode = INPLACE_MODULO; break;
2614 case '<': opcode = INPLACE_LSHIFT; break;
2615 case '>': opcode = INPLACE_RSHIFT; break;
2616 case '&': opcode = INPLACE_AND; break;
2617 case '^': opcode = INPLACE_XOR; break;
2618 case '|': opcode = INPLACE_OR; break;
2619 case '*':
2620 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2621 opcode = INPLACE_POWER;
2622 else
2623 opcode = INPLACE_MULTIPLY;
2624 break;
2625 default:
2626 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2627 return;
2628 }
2629 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2630}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002631
2632static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002633com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002634{
Thomas Wouters434d0822000-08-24 20:11:32 +00002635 REQ(n, expr_stmt);
2636 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002637 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002638 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002639 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002640 if (NCH(n) == 1) {
2641 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002642 if (c->c_interactive)
2643 com_addbyte(c, PRINT_EXPR);
2644 else
2645 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002646 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002647 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002648 else if (TYPE(CHILD(n,1)) == augassign)
2649 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002650 else {
2651 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002652 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002653 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002654 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002655 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002656 com_push(c, 1);
2657 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002658 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002659 }
2660 }
2661}
2662
2663static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002664com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002665{
2666 int a = 0, b = 0;
2667 int i;
2668 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2669 /* Generate code like for
2670
2671 if __debug__:
2672 if not <test>:
2673 raise AssertionError [, <message>]
2674
2675 where <message> is the second test, if present.
2676 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002677
Guido van Rossum228d7f31997-04-02 05:24:36 +00002678 if (Py_OptimizeFlag)
2679 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002680 com_addop_name(c, LOAD_GLOBAL, "__debug__");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002681 com_push(c, 1);
2682 com_addfwref(c, JUMP_IF_FALSE, &a);
2683 com_addbyte(c, POP_TOP);
2684 com_pop(c, 1);
2685 com_node(c, CHILD(n, 1));
2686 com_addfwref(c, JUMP_IF_TRUE, &b);
2687 com_addbyte(c, POP_TOP);
2688 com_pop(c, 1);
2689 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002690 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002691 com_push(c, 1);
2692 i = NCH(n)/2; /* Either 2 or 4 */
2693 if (i > 1)
2694 com_node(c, CHILD(n, 3));
2695 com_addoparg(c, RAISE_VARARGS, i);
2696 com_pop(c, i);
2697 /* The interpreter does not fall through */
2698 /* All jumps converge here */
2699 com_backpatch(c, a);
2700 com_backpatch(c, b);
2701 com_addbyte(c, POP_TOP);
2702}
2703
2704static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002705com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002706{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002707 int i = 1;
2708 node* stream = NULL;
2709
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002710 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002711
2712 /* are we using the extended print form? */
2713 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2714 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002715 com_node(c, stream);
2716 /* stack: [...] => [... stream] */
2717 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002718 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2719 i = 4;
2720 else
2721 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002722 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002723 for (; i < NCH(n); i += 2) {
2724 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002725 com_addbyte(c, DUP_TOP);
2726 /* stack: [stream] => [stream stream] */
2727 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002728 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002729 /* stack: [stream stream] => [stream stream obj] */
2730 com_addbyte(c, ROT_TWO);
2731 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002732 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002733 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002734 com_pop(c, 2);
2735 }
2736 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002737 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002738 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002739 com_addbyte(c, PRINT_ITEM);
2740 com_pop(c, 1);
2741 }
2742 }
2743 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002744 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002745 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002746 /* must pop the extra stream object off the stack */
2747 com_addbyte(c, POP_TOP);
2748 /* stack: [... stream] => [...] */
2749 com_pop(c, 1);
2750 }
2751 }
2752 else {
2753 if (stream != NULL) {
2754 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002755 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002756 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002757 com_pop(c, 1);
2758 }
2759 else
2760 com_addbyte(c, PRINT_NEWLINE);
2761 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002762}
2763
2764static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002765com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002766{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002767 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002768 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002769 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002770 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002771 if (c->c_flags & CO_GENERATOR) {
2772 if (NCH(n) > 1) {
2773 com_error(c, PyExc_SyntaxError,
2774 "'return' with argument inside generator");
2775 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002776 }
2777 if (NCH(n) < 2) {
2778 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002779 com_push(c, 1);
2780 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002781 else
2782 com_node(c, CHILD(n, 1));
2783 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002784 com_pop(c, 1);
2785}
2786
2787static void
2788com_yield_stmt(struct compiling *c, node *n)
2789{
Tim Peters95c80f82001-06-23 02:07:08 +00002790 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002791 REQ(n, yield_stmt); /* 'yield' testlist */
2792 if (!c->c_infunction) {
2793 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2794 }
Tim Peters95c80f82001-06-23 02:07:08 +00002795
2796 for (i = 0; i < c->c_nblocks; ++i) {
2797 if (c->c_block[i] == SETUP_FINALLY) {
2798 com_error(c, PyExc_SyntaxError,
2799 "'yield' not allowed in a 'try' block "
2800 "with a 'finally' clause");
2801 return;
2802 }
2803 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002804 com_node(c, CHILD(n, 1));
2805 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002806 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002807}
2808
2809static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002810com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002811{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002812 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002813 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2814 if (NCH(n) > 1) {
2815 com_node(c, CHILD(n, 1));
2816 if (NCH(n) > 3) {
2817 com_node(c, CHILD(n, 3));
2818 if (NCH(n) > 5)
2819 com_node(c, CHILD(n, 5));
2820 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002821 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002822 i = NCH(n)/2;
2823 com_addoparg(c, RAISE_VARARGS, i);
2824 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002825}
2826
2827static void
Thomas Wouters52152252000-08-17 22:55:00 +00002828com_from_import(struct compiling *c, node *n)
2829{
2830 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2831 com_push(c, 1);
2832 if (NCH(n) > 1) {
2833 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2834 com_error(c, PyExc_SyntaxError, "invalid syntax");
2835 return;
2836 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002837 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002838 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002839 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002840 com_pop(c, 1);
2841}
2842
2843static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002844com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002845{
2846 int i;
2847 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002848 /* 'import' dotted_name (',' dotted_name)* |
2849 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002850 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002851 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002852 /* 'from' dotted_name 'import' ... */
2853 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002854
2855 if (TYPE(CHILD(n, 3)) == STAR) {
2856 tup = Py_BuildValue("(s)", "*");
2857 } else {
2858 tup = PyTuple_New((NCH(n) - 2)/2);
2859 for (i = 3; i < NCH(n); i += 2) {
2860 PyTuple_SET_ITEM(tup, (i-3)/2,
2861 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00002862 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002863 }
2864 }
2865 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002866 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002867 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002868 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002869 if (TYPE(CHILD(n, 3)) == STAR)
2870 com_addbyte(c, IMPORT_STAR);
2871 else {
2872 for (i = 3; i < NCH(n); i += 2)
2873 com_from_import(c, CHILD(n, i));
2874 com_addbyte(c, POP_TOP);
2875 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002876 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002877 }
2878 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002879 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002880 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002881 node *subn = CHILD(n, i);
2882 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002883 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002884 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002885 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002886 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002887 int j;
2888 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002889 com_error(c, PyExc_SyntaxError,
2890 "invalid syntax");
2891 return;
2892 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002893 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2894 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002895 CHILD(CHILD(subn, 0),
2896 j));
2897 com_addop_varname(c, VAR_STORE,
2898 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002899 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002900 com_addop_varname(c, VAR_STORE,
2901 STR(CHILD(CHILD(subn, 0),
2902 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002903 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002904 }
2905 }
2906}
2907
2908static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002909com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002910{
2911 REQ(n, exec_stmt);
2912 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2913 com_node(c, CHILD(n, 1));
2914 if (NCH(n) >= 4)
2915 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002916 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002917 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002918 com_push(c, 1);
2919 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002920 if (NCH(n) >= 6)
2921 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002922 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002923 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002924 com_push(c, 1);
2925 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002926 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002927 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002928}
2929
Guido van Rossum7c531111997-03-11 18:42:21 +00002930static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002931is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002932{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002933 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002934 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002935 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00002936
2937 /* Label to avoid tail recursion */
2938 next:
2939 switch (TYPE(n)) {
2940
2941 case suite:
2942 if (NCH(n) == 1) {
2943 n = CHILD(n, 0);
2944 goto next;
2945 }
2946 /* Fall through */
2947 case file_input:
2948 for (i = 0; i < NCH(n); i++) {
2949 node *ch = CHILD(n, i);
2950 if (TYPE(ch) == stmt) {
2951 n = ch;
2952 goto next;
2953 }
2954 }
2955 break;
2956
2957 case stmt:
2958 case simple_stmt:
2959 case small_stmt:
2960 n = CHILD(n, 0);
2961 goto next;
2962
2963 case expr_stmt:
2964 case testlist:
2965 case test:
2966 case and_test:
2967 case not_test:
2968 case comparison:
2969 case expr:
2970 case xor_expr:
2971 case and_expr:
2972 case shift_expr:
2973 case arith_expr:
2974 case term:
2975 case factor:
2976 case power:
2977 case atom:
2978 if (NCH(n) == 1) {
2979 n = CHILD(n, 0);
2980 goto next;
2981 }
2982 break;
2983
2984 case NAME:
2985 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2986 return 1;
2987 break;
2988
2989 case NUMBER:
2990 v = parsenumber(c, STR(n));
2991 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002992 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002993 break;
2994 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002995 i = PyObject_IsTrue(v);
2996 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002997 return i == 0;
2998
2999 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003000 v = parsestr(c, STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00003001 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003002 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00003003 break;
3004 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003005 i = PyObject_IsTrue(v);
3006 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003007 return i == 0;
3008
3009 }
3010 return 0;
3011}
3012
Tim Peters08a898f2001-06-28 01:52:22 +00003013
3014/* Look under n for a return stmt with an expression.
3015 * This hack is used to find illegal returns under "if 0:" blocks in
3016 * functions already known to be generators (as determined by the symtable
3017 * pass).
3018 * Return the offending return node if found, else NULL.
3019 */
3020static node *
3021look_for_offending_return(node *n)
3022{
3023 int i;
3024
3025 for (i = 0; i < NCH(n); ++i) {
3026 node *kid = CHILD(n, i);
3027
3028 switch (TYPE(kid)) {
3029 case classdef:
3030 case funcdef:
3031 case lambdef:
3032 /* Stuff in nested functions & classes doesn't
3033 affect the code block we started in. */
3034 return NULL;
3035
3036 case return_stmt:
3037 if (NCH(kid) > 1)
3038 return kid;
3039 break;
3040
3041 default: {
3042 node *bad = look_for_offending_return(kid);
3043 if (bad != NULL)
3044 return bad;
3045 }
3046 }
3047 }
3048
3049 return NULL;
3050}
3051
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003052static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003053com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003054{
3055 int i;
3056 int anchor = 0;
3057 REQ(n, if_stmt);
3058 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3059 for (i = 0; i+3 < NCH(n); i+=4) {
3060 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003061 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00003062 if (is_constant_false(c, ch)) {
3063 /* We're going to skip this block. However, if this
3064 is a generator, we have to check the dead code
3065 anyway to make sure there aren't any return stmts
3066 with expressions, in the same scope. */
3067 if (c->c_flags & CO_GENERATOR) {
3068 node *p = look_for_offending_return(n);
3069 if (p != NULL) {
3070 int savelineno = c->c_lineno;
3071 c->c_lineno = p->n_lineno;
3072 com_error(c, PyExc_SyntaxError,
3073 "'return' with argument "
3074 "inside generator");
3075 c->c_lineno = savelineno;
3076 }
3077 }
Guido van Rossum7c531111997-03-11 18:42:21 +00003078 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00003079 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00003080 if (i > 0)
3081 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00003082 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003083 com_addfwref(c, JUMP_IF_FALSE, &a);
3084 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003085 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003086 com_node(c, CHILD(n, i+3));
3087 com_addfwref(c, JUMP_FORWARD, &anchor);
3088 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003089 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003090 com_addbyte(c, POP_TOP);
3091 }
3092 if (i+2 < NCH(n))
3093 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00003094 if (anchor)
3095 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003096}
3097
3098static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003099com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003100{
3101 int break_anchor = 0;
3102 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003103 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003104 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3105 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003106 block_push(c, SETUP_LOOP);
3107 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003108 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003109 com_node(c, CHILD(n, 1));
3110 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3111 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003112 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003113 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003114 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003115 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003116 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3117 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003118 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003119 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003120 com_addbyte(c, POP_TOP);
3121 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003122 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003123 if (NCH(n) > 4)
3124 com_node(c, CHILD(n, 6));
3125 com_backpatch(c, break_anchor);
3126}
3127
3128static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003129com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003130{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003131 int break_anchor = 0;
3132 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003133 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003134 REQ(n, for_stmt);
3135 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3136 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003137 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003138 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003139 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003140 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003141 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003142 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003143 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003144 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003145 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003146 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003147 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003148 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3149 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003150 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003151 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003152 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003153 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003154 if (NCH(n) > 8)
3155 com_node(c, CHILD(n, 8));
3156 com_backpatch(c, break_anchor);
3157}
3158
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003159/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003160
3161 SETUP_FINALLY L
3162 <code for S>
3163 POP_BLOCK
3164 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003165 L: <code for Sf>
3166 END_FINALLY
3167
3168 The special instructions use the block stack. Each block
3169 stack entry contains the instruction that created it (here
3170 SETUP_FINALLY), the level of the value stack at the time the
3171 block stack entry was created, and a label (here L).
3172
3173 SETUP_FINALLY:
3174 Pushes the current value stack level and the label
3175 onto the block stack.
3176 POP_BLOCK:
3177 Pops en entry from the block stack, and pops the value
3178 stack until its level is the same as indicated on the
3179 block stack. (The label is ignored.)
3180 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003181 Pops a variable number of entries from the *value* stack
3182 and re-raises the exception they specify. The number of
3183 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003184
3185 The block stack is unwound when an exception is raised:
3186 when a SETUP_FINALLY entry is found, the exception is pushed
3187 onto the value stack (and the exception condition is cleared),
3188 and the interpreter jumps to the label gotten from the block
3189 stack.
3190
3191 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003192 (The contents of the value stack is shown in [], with the top
3193 at the right; 'tb' is trace-back info, 'val' the exception's
3194 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003195
3196 Value stack Label Instruction Argument
3197 [] SETUP_EXCEPT L1
3198 [] <code for S>
3199 [] POP_BLOCK
3200 [] JUMP_FORWARD L0
3201
Guido van Rossum3f5da241990-12-20 15:06:42 +00003202 [tb, val, exc] L1: DUP )
3203 [tb, val, exc, exc] <evaluate E1> )
3204 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3205 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3206 [tb, val, exc, 1] POP )
3207 [tb, val, exc] POP
3208 [tb, val] <assign to V1> (or POP if no V1)
3209 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003210 [] <code for S1>
3211 JUMP_FORWARD L0
3212
Guido van Rossum3f5da241990-12-20 15:06:42 +00003213 [tb, val, exc, 0] L2: POP
3214 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003215 .............................etc.......................
3216
Guido van Rossum3f5da241990-12-20 15:06:42 +00003217 [tb, val, exc, 0] Ln+1: POP
3218 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003219
3220 [] L0: <next statement>
3221
3222 Of course, parts are not generated if Vi or Ei is not present.
3223*/
3224
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003225static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003226com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003227{
3228 int except_anchor = 0;
3229 int end_anchor = 0;
3230 int else_anchor = 0;
3231 int i;
3232 node *ch;
3233
3234 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3235 block_push(c, SETUP_EXCEPT);
3236 com_node(c, CHILD(n, 2));
3237 com_addbyte(c, POP_BLOCK);
3238 block_pop(c, SETUP_EXCEPT);
3239 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3240 com_backpatch(c, except_anchor);
3241 for (i = 3;
3242 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3243 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003244 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003245 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003246 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003247 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003248 break;
3249 }
3250 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003251 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003252 com_addoparg(c, SET_LINENO, ch->n_lineno);
3253 if (NCH(ch) > 1) {
3254 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003255 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003256 com_node(c, CHILD(ch, 1));
3257 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003258 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003259 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3260 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003261 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003262 }
3263 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003264 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003265 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003266 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003267 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003268 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003269 com_pop(c, 1);
3270 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003271 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003272 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003273 com_node(c, CHILD(n, i+2));
3274 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3275 if (except_anchor) {
3276 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003277 /* We come in with [tb, val, exc, 0] on the
3278 stack; one pop and it's the same as
3279 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003280 com_addbyte(c, POP_TOP);
3281 }
3282 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003283 /* We actually come in here with [tb, val, exc] but the
3284 END_FINALLY will zap those and jump around.
3285 The c_stacklevel does not reflect them so we need not pop
3286 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003287 com_addbyte(c, END_FINALLY);
3288 com_backpatch(c, else_anchor);
3289 if (i < NCH(n))
3290 com_node(c, CHILD(n, i+2));
3291 com_backpatch(c, end_anchor);
3292}
3293
3294static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003295com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003296{
3297 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003298 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003299
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003300 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3301 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003302 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003303 com_addbyte(c, POP_BLOCK);
3304 block_pop(c, SETUP_FINALLY);
3305 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003306 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003307 /* While the generated code pushes only one item,
3308 the try-finally handling can enter here with
3309 up to three items. OK, here are the details:
3310 3 for an exception, 2 for RETURN, 1 for BREAK. */
3311 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003312 com_backpatch(c, finally_anchor);
3313 ch = CHILD(n, NCH(n)-1);
3314 com_addoparg(c, SET_LINENO, ch->n_lineno);
3315 com_node(c, ch);
3316 com_addbyte(c, END_FINALLY);
3317 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003318 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003319}
3320
3321static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003322com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003323{
3324 REQ(n, try_stmt);
3325 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3326 | 'try' ':' suite 'finally' ':' suite */
3327 if (TYPE(CHILD(n, 3)) != except_clause)
3328 com_try_finally(c, n);
3329 else
3330 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003331}
3332
Guido van Rossum8b993a91997-01-17 21:04:03 +00003333static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003334get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003335{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003336 int i;
3337
Guido van Rossum8b993a91997-01-17 21:04:03 +00003338 /* Label to avoid tail recursion */
3339 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003340 switch (TYPE(n)) {
3341
3342 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003343 if (NCH(n) == 1) {
3344 n = CHILD(n, 0);
3345 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003346 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003347 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003348 case file_input:
3349 for (i = 0; i < NCH(n); i++) {
3350 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003351 if (TYPE(ch) == stmt) {
3352 n = ch;
3353 goto next;
3354 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003355 }
3356 break;
3357
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003358 case stmt:
3359 case simple_stmt:
3360 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003361 n = CHILD(n, 0);
3362 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003363
3364 case expr_stmt:
3365 case testlist:
3366 case test:
3367 case and_test:
3368 case not_test:
3369 case comparison:
3370 case expr:
3371 case xor_expr:
3372 case and_expr:
3373 case shift_expr:
3374 case arith_expr:
3375 case term:
3376 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003377 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003378 if (NCH(n) == 1) {
3379 n = CHILD(n, 0);
3380 goto next;
3381 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003382 break;
3383
3384 case atom:
3385 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003386 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003387 break;
3388
3389 }
3390 return NULL;
3391}
3392
Guido van Rossum79f25d91997-04-29 20:08:16 +00003393static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003394get_docstring(struct compiling *c, node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003395{
Guido van Rossum541563e1999-01-28 15:08:09 +00003396 /* Don't generate doc-strings if run with -OO */
3397 if (Py_OptimizeFlag > 1)
3398 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003399 n = get_rawdocstring(n);
3400 if (n == NULL)
3401 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003402 return parsestrplus(c, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003403}
3404
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003405static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003406com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003407{
3408 REQ(n, suite);
3409 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3410 if (NCH(n) == 1) {
3411 com_node(c, CHILD(n, 0));
3412 }
3413 else {
3414 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003415 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003416 node *ch = CHILD(n, i);
3417 if (TYPE(ch) == stmt)
3418 com_node(c, ch);
3419 }
3420 }
3421}
3422
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003423/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003424static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003425com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003426{
3427 int i = c->c_nblocks;
3428 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3429 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3430 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003431 else if (i <= 0) {
3432 /* at the outer level */
3433 com_error(c, PyExc_SyntaxError,
3434 "'continue' not properly in loop");
3435 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003436 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003437 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003438 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003439 if (c->c_block[j] == SETUP_LOOP)
3440 break;
3441 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003442 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003443 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003444 for (; i > j; --i) {
3445 if (c->c_block[i] == SETUP_EXCEPT ||
3446 c->c_block[i] == SETUP_FINALLY) {
3447 com_addoparg(c, CONTINUE_LOOP,
3448 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003449 return;
3450 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003451 if (c->c_block[i] == END_FINALLY) {
3452 com_error(c, PyExc_SyntaxError,
3453 "'continue' not supported inside 'finally' clause");
3454 return;
3455 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003456 }
3457 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003458 com_error(c, PyExc_SyntaxError,
3459 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003460 }
3461 /* XXX Could allow it inside a 'finally' clause
3462 XXX if we could pop the exception still on the stack */
3463}
3464
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003465static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003466com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003467{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003468 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003469 if (TYPE(n) == lambdef) {
3470 /* lambdef: 'lambda' [varargslist] ':' test */
3471 n = CHILD(n, 1);
3472 }
3473 else {
3474 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3475 n = CHILD(n, 2);
3476 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3477 n = CHILD(n, 1);
3478 }
3479 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003480 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003481 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003482 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003483 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3484 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003485 nargs = 0;
3486 ndefs = 0;
3487 for (i = 0; i < nch; i++) {
3488 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003489 if (TYPE(CHILD(n, i)) == STAR ||
3490 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003491 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003492 nargs++;
3493 i++;
3494 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003495 t = RPAR; /* Anything except EQUAL or COMMA */
3496 else
3497 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003498 if (t == EQUAL) {
3499 i++;
3500 ndefs++;
3501 com_node(c, CHILD(n, i));
3502 i++;
3503 if (i >= nch)
3504 break;
3505 t = TYPE(CHILD(n, i));
3506 }
3507 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003508 /* Treat "(a=1, b)" as an error */
3509 if (ndefs)
3510 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003511 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003512 }
3513 if (t != COMMA)
3514 break;
3515 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003516 return ndefs;
3517}
3518
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003519static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003520com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003521{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003522 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003523 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003525 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003526 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3527 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003528 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003529 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003530 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003531 c->c_errors++;
3532 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003533 int closure = com_make_closure(c, (PyCodeObject *)co);
3534 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003535 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003536 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003537 if (closure)
3538 com_addoparg(c, MAKE_CLOSURE, ndefs);
3539 else
3540 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003541 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003542 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003543 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003544 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003545 }
3546}
3547
3548static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003549com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003550{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003551 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003552 REQ(n, testlist);
3553 /* testlist: test (',' test)* [','] */
3554 for (i = 0; i < NCH(n); i += 2)
3555 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003556 i = (NCH(n)+1) / 2;
3557 com_addoparg(c, BUILD_TUPLE, i);
3558 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003559}
3560
3561static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003562com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003563{
Guido van Rossum25831651993-05-19 14:50:45 +00003564 int i;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003565 PyObject *v;
3566 PyCodeObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003567 char *name;
3568
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003569 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003570 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003571 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003572 c->c_errors++;
3573 return;
3574 }
3575 /* Push the class name on the stack */
3576 i = com_addconst(c, v);
3577 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003578 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003579 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003580 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003581 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003582 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003583 com_push(c, 1);
3584 }
Guido van Rossum25831651993-05-19 14:50:45 +00003585 else
3586 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003587 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003588 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003589 co = icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003590 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003591 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003592 c->c_errors++;
3593 else {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003594 int closure = com_make_closure(c, co);
3595 i = com_addconst(c, (PyObject *)co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003596 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003597 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003598 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003599 com_addoparg(c, MAKE_CLOSURE, 0);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003600 com_pop(c, PyTuple_GET_SIZE(co->co_freevars));
3601 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003602 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003603 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003604 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003605 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003606 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003607 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003608 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003609 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003610}
3611
3612static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003613com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003614{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003615 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003616 if (c->c_errors)
3617 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003618 switch (TYPE(n)) {
3619
3620 /* Definition nodes */
3621
3622 case funcdef:
3623 com_funcdef(c, n);
3624 break;
3625 case classdef:
3626 com_classdef(c, n);
3627 break;
3628
3629 /* Trivial parse tree nodes */
3630
3631 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003632 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003633 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003634 n = CHILD(n, 0);
3635 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003636
3637 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003638 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3639 com_addoparg(c, SET_LINENO, n->n_lineno);
3640 {
3641 int i;
3642 for (i = 0; i < NCH(n)-1; i += 2)
3643 com_node(c, CHILD(n, i));
3644 }
3645 break;
3646
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003647 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003648 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003649 n = CHILD(n, 0);
3650 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003651
3652 /* Statement nodes */
3653
3654 case expr_stmt:
3655 com_expr_stmt(c, n);
3656 break;
3657 case print_stmt:
3658 com_print_stmt(c, n);
3659 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003660 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003661 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003662 break;
3663 case pass_stmt:
3664 break;
3665 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003666 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003667 com_error(c, PyExc_SyntaxError,
3668 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003669 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003670 com_addbyte(c, BREAK_LOOP);
3671 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003672 case continue_stmt:
3673 com_continue_stmt(c, n);
3674 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003675 case return_stmt:
3676 com_return_stmt(c, n);
3677 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003678 case yield_stmt:
3679 com_yield_stmt(c, n);
3680 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003681 case raise_stmt:
3682 com_raise_stmt(c, n);
3683 break;
3684 case import_stmt:
3685 com_import_stmt(c, n);
3686 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003687 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003688 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003689 case exec_stmt:
3690 com_exec_stmt(c, n);
3691 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003692 case assert_stmt:
3693 com_assert_stmt(c, n);
3694 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003695 case if_stmt:
3696 com_if_stmt(c, n);
3697 break;
3698 case while_stmt:
3699 com_while_stmt(c, n);
3700 break;
3701 case for_stmt:
3702 com_for_stmt(c, n);
3703 break;
3704 case try_stmt:
3705 com_try_stmt(c, n);
3706 break;
3707 case suite:
3708 com_suite(c, n);
3709 break;
3710
3711 /* Expression nodes */
3712
3713 case testlist:
Guido van Rossum1c917072001-10-15 15:44:05 +00003714 case testlist_safe:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003715 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003716 break;
3717 case test:
3718 com_test(c, n);
3719 break;
3720 case and_test:
3721 com_and_test(c, n);
3722 break;
3723 case not_test:
3724 com_not_test(c, n);
3725 break;
3726 case comparison:
3727 com_comparison(c, n);
3728 break;
3729 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003730 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003731 break;
3732 case expr:
3733 com_expr(c, n);
3734 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003735 case xor_expr:
3736 com_xor_expr(c, n);
3737 break;
3738 case and_expr:
3739 com_and_expr(c, n);
3740 break;
3741 case shift_expr:
3742 com_shift_expr(c, n);
3743 break;
3744 case arith_expr:
3745 com_arith_expr(c, n);
3746 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003747 case term:
3748 com_term(c, n);
3749 break;
3750 case factor:
3751 com_factor(c, n);
3752 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003753 case power:
3754 com_power(c, n);
3755 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003756 case atom:
3757 com_atom(c, n);
3758 break;
3759
3760 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003761 com_error(c, PyExc_SystemError,
3762 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003763 }
3764}
3765
Tim Petersdbd9ba62000-07-09 03:09:57 +00003766static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003767
3768static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003769com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003770{
3771 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3772 if (TYPE(CHILD(n, 0)) == LPAR)
3773 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003774 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003775 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003776 com_pop(c, 1);
3777 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003778}
3779
3780static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003781com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003783 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003784 if (NCH(n) == 1) {
3785 com_fpdef(c, CHILD(n, 0));
3786 }
3787 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003788 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003789 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003790 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003791 for (i = 0; i < NCH(n); i += 2)
3792 com_fpdef(c, CHILD(n, i));
3793 }
3794}
3795
3796static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003797com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003798{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003799 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003800 int complex = 0;
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003801 char nbuf[10];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003802 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003803 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003804 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003805 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003806 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003807 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003808 node *ch = CHILD(n, i);
3809 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003810 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003811 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003812 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3813 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003814 if (TYPE(fp) != NAME) {
Guido van Rossumdb9e20f1997-07-10 01:06:53 +00003815 sprintf(nbuf, ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003816 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003817 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003818 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003819 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003820 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003821 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003822 ch = CHILD(n, i);
3823 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003824 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003825 else
3826 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003827 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003828 if (complex) {
3829 /* Generate code for complex arguments only after
3830 having counted the simple arguments */
3831 int ilocal = 0;
3832 for (i = 0; i < nch; i++) {
3833 node *ch = CHILD(n, i);
3834 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003835 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003836 break;
3837 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3838 fp = CHILD(ch, 0);
3839 if (TYPE(fp) != NAME) {
3840 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003841 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003842 com_fpdef(c, ch);
3843 }
3844 ilocal++;
3845 if (++i >= nch)
3846 break;
3847 ch = CHILD(n, i);
3848 if (TYPE(ch) == EQUAL)
3849 i += 2;
3850 else
3851 REQ(ch, COMMA);
3852 }
3853 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003854}
3855
3856static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003857com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858{
3859 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003860 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003861 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003862 doc = get_docstring(c, n);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003863 if (doc != NULL) {
3864 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003865 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003866 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003867 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003868 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003869 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003870 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003871 for (i = 0; i < NCH(n); i++) {
3872 node *ch = CHILD(n, i);
3873 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3874 com_node(c, ch);
3875 }
3876}
3877
3878/* Top-level compile-node interface */
3879
3880static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003881compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003882{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003883 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003884 node *ch;
3885 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003886 c->c_name = STR(CHILD(n, 1));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003887 doc = get_docstring(c, CHILD(n, 4));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003888 if (doc != NULL) {
3889 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003890 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003891 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003892 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003893 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003894 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3895 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003896 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003897 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003898 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003899 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003900 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00003901 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3902 com_push(c, 1);
3903 com_addbyte(c, RETURN_VALUE);
3904 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003905}
3906
3907static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003908compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003909{
Guido van Rossum590baa41993-11-30 13:40:46 +00003910 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003911 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003912 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003913
3914 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003915 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003916 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003917 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003918 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003919 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003920 else
3921 ch = CHILD(n, 2);
3922 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003923 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003924 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003925}
3926
3927static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003928compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003929{
3930 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003931 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003932 REQ(n, classdef);
3933 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3934 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003935 c->c_private = c->c_name;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003936 ch = CHILD(n, NCH(n)-1); /* The suite */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003937 doc = get_docstring(c, ch);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003938 if (doc != NULL) {
3939 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003940 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003941 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003942 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003943 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003944 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003945 }
3946 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003947 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003948 com_node(c, ch);
3949 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003950 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003951 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003952 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003953}
3954
3955static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003956compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003957{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003958 com_addoparg(c, SET_LINENO, n->n_lineno);
3959
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003960 switch (TYPE(n)) {
3961
Guido van Rossum4c417781991-01-21 16:09:22 +00003962 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003963 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003964 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965 n = CHILD(n, 0);
3966 if (TYPE(n) != NEWLINE)
3967 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003968 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003969 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003970 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003971 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003972 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003973 break;
3974
Guido van Rossum4c417781991-01-21 16:09:22 +00003975 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003976 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003977 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003978 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003979 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003980 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003981 break;
3982
Guido van Rossum590baa41993-11-30 13:40:46 +00003983 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003984 com_node(c, CHILD(n, 0));
3985 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003986 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003987 break;
3988
Guido van Rossum590baa41993-11-30 13:40:46 +00003989 case lambdef: /* anonymous function definition */
3990 compile_lambdef(c, n);
3991 break;
3992
Guido van Rossum4c417781991-01-21 16:09:22 +00003993 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003994 compile_funcdef(c, n);
3995 break;
3996
Guido van Rossum4c417781991-01-21 16:09:22 +00003997 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003998 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003999 break;
4000
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004001 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00004002 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00004003 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004004 }
4005}
4006
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004007static PyObject *
4008dict_keys_inorder(PyObject *dict, int offset)
4009{
4010 PyObject *tuple, *k, *v;
4011 int i, pos = 0, size = PyDict_Size(dict);
4012
4013 tuple = PyTuple_New(size);
4014 if (tuple == NULL)
4015 return NULL;
4016 while (PyDict_Next(dict, &pos, &k, &v)) {
4017 i = PyInt_AS_LONG(v);
4018 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004019 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004020 PyTuple_SET_ITEM(tuple, i - offset, k);
4021 }
4022 return tuple;
4023}
4024
Guido van Rossum79f25d91997-04-29 20:08:16 +00004025PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004026PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004027{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004028 return PyNode_CompileFlags(n, filename, NULL);
4029}
4030
4031PyCodeObject *
4032PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4033{
4034 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004035}
4036
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004037struct symtable *
4038PyNode_CompileSymtable(node *n, char *filename)
4039{
4040 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004041 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004042
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004043 ff = PyNode_Future(n, filename);
4044 if (ff == NULL)
4045 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004046 st = symtable_init();
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004047 if (st == NULL)
4048 return NULL;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004049 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004050 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004051 if (st->st_errors > 0)
4052 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004053 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004054 if (st->st_errors > 0)
4055 goto fail;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004056
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004057 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004058 fail:
4059 PyMem_Free((void *)ff);
4060 st->st_future = NULL;
4061 PySymtable_Free(st);
4062 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004063}
4064
Guido van Rossum79f25d91997-04-29 20:08:16 +00004065static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004066icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004067{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004068 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004069}
4070
Guido van Rossum79f25d91997-04-29 20:08:16 +00004071static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004072jcompile(node *n, char *filename, struct compiling *base,
4073 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004074{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004075 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004076 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00004077 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004078 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004079 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004080 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004081 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004082 /* c_symtable still points to parent's symbols */
4083 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004084 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085 sc.c_nested = 1;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00004086 sc.c_flags |= base->c_flags & PyCF_MASK;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004087 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004088 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004089 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004090 if (sc.c_future == NULL) {
4091 com_free(&sc);
4092 return NULL;
4093 }
Jeremy Hylton481081e2001-08-14 20:01:59 +00004094 if (flags) {
4095 int merged = sc.c_future->ff_features |
4096 flags->cf_flags;
4097 sc.c_future->ff_features = merged;
4098 flags->cf_flags = merged;
4099 }
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004100 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004101 com_free(&sc);
4102 return NULL;
4103 }
4104 }
4105 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004106 if (symtable_load_symbols(&sc) < 0) {
4107 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004108 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004109 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004110 compile_node(&sc, n);
4111 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00004112 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004113 PyObject *consts, *names, *varnames, *filename, *name,
4114 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004115 consts = PyList_AsTuple(sc.c_consts);
4116 names = PyList_AsTuple(sc.c_names);
4117 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004118 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4119 freevars = dict_keys_inorder(sc.c_freevars,
4120 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00004121 filename = PyString_InternFromString(sc.c_filename);
4122 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004123 if (!PyErr_Occurred())
4124 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004125 sc.c_nlocals,
4126 sc.c_maxstacklevel,
4127 sc.c_flags,
4128 sc.c_code,
4129 consts,
4130 names,
4131 varnames,
4132 freevars,
4133 cellvars,
4134 filename,
4135 name,
4136 sc.c_firstlineno,
4137 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004138 Py_XDECREF(consts);
4139 Py_XDECREF(names);
4140 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004141 Py_XDECREF(freevars);
4142 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004143 Py_XDECREF(filename);
4144 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004145 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004146 else if (!PyErr_Occurred()) {
4147 /* This could happen if someone called PyErr_Clear() after an
4148 error was reported above. That's not supposed to happen,
4149 but I just plugged one case and I'm not sure there can't be
4150 others. In that case, raise SystemError so that at least
4151 it gets reported instead dumping core. */
4152 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4153 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004154 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004155 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004156 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004157 sc.c_symtable = NULL;
4158 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004159 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004160 return co;
4161}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004162
4163int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004164PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004165{
4166 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004167 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004168 int line = co->co_firstlineno;
4169 int addr = 0;
4170 while (--size >= 0) {
4171 addr += *p++;
4172 if (addr > addrq)
4173 break;
4174 line += *p++;
4175 }
4176 return line;
4177}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004178
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004179/* The test for LOCAL must come before the test for FREE in order to
4180 handle classes where name is both local and free. The local var is
4181 a method and the free var is a free var referenced within a method.
4182*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004183
4184static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004185get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004186{
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004187 char buf[350];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004188 PyObject *v;
Jeremy Hylton51257732001-03-01 00:42:55 +00004189
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004190 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4191 return CELL;
4192 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4193 return LOCAL;
4194 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4195 return FREE;
4196 v = PyDict_GetItemString(c->c_globals, name);
4197 if (v) {
4198 if (v == Py_None)
4199 return GLOBAL_EXPLICIT;
4200 else {
4201 return GLOBAL_IMPLICIT;
4202 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004203 }
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004204 sprintf(buf,
4205 "unknown scope for %.100s in %.100s(%s) "
4206 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4207 name, c->c_name,
4208 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4209 c->c_filename,
4210 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4211 PyObject_REPR(c->c_locals),
4212 PyObject_REPR(c->c_globals)
4213 );
4214
4215 Py_FatalError(buf);
4216 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004217}
4218
Guido van Rossum207fda62001-03-02 03:30:41 +00004219/* Helper functions to issue warnings */
4220
4221static int
4222issue_warning(char *msg, char *filename, int lineno)
4223{
4224 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4225 lineno, NULL, NULL) < 0) {
4226 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4227 PyErr_SetString(PyExc_SyntaxError, msg);
4228 PyErr_SyntaxLocation(filename, lineno);
4229 }
4230 return -1;
4231 }
4232 return 0;
4233}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004234
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004235static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004236symtable_warn(struct symtable *st, char *msg)
4237{
Guido van Rossum207fda62001-03-02 03:30:41 +00004238 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004239 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004240 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004241 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004242 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004243}
4244
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004245/* Helper function for setting lineno and filename */
4246
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004247static int
4248symtable_build(struct compiling *c, node *n)
4249{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004250 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004251 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004252 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004253 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004254 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4255 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004256 return -1;
4257 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004258 if (c->c_symtable->st_errors > 0)
4259 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004260 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004261 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004262 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004263 return 0;
4264}
4265
4266static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004267symtable_init_compiling_symbols(struct compiling *c)
4268{
4269 PyObject *varnames;
4270
4271 varnames = c->c_symtable->st_cur->ste_varnames;
4272 if (varnames == NULL) {
4273 varnames = PyList_New(0);
4274 if (varnames == NULL)
4275 return -1;
4276 c->c_symtable->st_cur->ste_varnames = varnames;
4277 Py_INCREF(varnames);
4278 } else
4279 Py_INCREF(varnames);
4280 c->c_varnames = varnames;
4281
4282 c->c_globals = PyDict_New();
4283 if (c->c_globals == NULL)
4284 return -1;
4285 c->c_freevars = PyDict_New();
4286 if (c->c_freevars == NULL)
4287 return -1;
4288 c->c_cellvars = PyDict_New();
4289 if (c->c_cellvars == NULL)
4290 return -1;
4291 return 0;
4292}
4293
4294struct symbol_info {
4295 int si_nlocals;
4296 int si_ncells;
4297 int si_nfrees;
4298 int si_nimplicit;
4299};
4300
4301static void
4302symtable_init_info(struct symbol_info *si)
4303{
4304 si->si_nlocals = 0;
4305 si->si_ncells = 0;
4306 si->si_nfrees = 0;
4307 si->si_nimplicit = 0;
4308}
4309
4310static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004311symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004312 struct symbol_info *si)
4313{
4314 PyObject *dict, *v;
4315
4316 /* Seperate logic for DEF_FREE. If it occurs in a function,
4317 it indicates a local that we must allocate storage for (a
4318 cell var). If it occurs in a class, then the class has a
4319 method and a free variable with the same name.
4320 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004321 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004322 /* If it isn't declared locally, it can't be a cell. */
4323 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4324 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004325 v = PyInt_FromLong(si->si_ncells++);
4326 dict = c->c_cellvars;
4327 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004328 /* If it is free anyway, then there is no need to do
4329 anything here.
4330 */
4331 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004332 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004333 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004334 v = PyInt_FromLong(si->si_nfrees++);
4335 dict = c->c_freevars;
4336 }
4337 if (v == NULL)
4338 return -1;
4339 if (PyDict_SetItem(dict, name, v) < 0) {
4340 Py_DECREF(v);
4341 return -1;
4342 }
4343 Py_DECREF(v);
4344 return 0;
4345}
4346
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004347/* If a variable is a cell and an argument, make sure that appears in
4348 co_cellvars before any variable to its right in varnames.
4349*/
4350
4351
4352static int
4353symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4354 PyObject *varnames, int flags)
4355{
4356 PyObject *v, *w, *d, *list = NULL;
4357 int i, pos;
4358
4359 if (flags & CO_VARARGS)
4360 argcount++;
4361 if (flags & CO_VARKEYWORDS)
4362 argcount++;
4363 for (i = argcount; --i >= 0; ) {
4364 v = PyList_GET_ITEM(varnames, i);
4365 if (PyDict_GetItem(*cellvars, v)) {
4366 if (list == NULL) {
4367 list = PyList_New(1);
4368 if (list == NULL)
4369 return -1;
4370 PyList_SET_ITEM(list, 0, v);
4371 Py_INCREF(v);
4372 } else
4373 PyList_Insert(list, 0, v);
4374 }
4375 }
4376 if (list == NULL || PyList_GET_SIZE(list) == 0)
4377 return 0;
4378 /* There are cellvars that are also arguments. Create a dict
4379 to replace cellvars and put the args at the front.
4380 */
4381 d = PyDict_New();
4382 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4383 v = PyInt_FromLong(i);
4384 if (v == NULL)
4385 goto fail;
4386 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4387 goto fail;
4388 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4389 goto fail;
4390 }
4391 pos = 0;
4392 i = PyList_GET_SIZE(list);
4393 Py_DECREF(list);
4394 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4395 w = PyInt_FromLong(i++); /* don't care about the old key */
4396 if (PyDict_SetItem(d, v, w) < 0) {
4397 Py_DECREF(w);
4398 goto fail;
4399 }
4400 Py_DECREF(w);
4401 }
4402 Py_DECREF(*cellvars);
4403 *cellvars = d;
4404 return 1;
4405 fail:
4406 Py_DECREF(d);
4407 return -1;
4408}
4409
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004410static int
4411symtable_freevar_offsets(PyObject *freevars, int offset)
4412{
4413 PyObject *name, *v;
4414 int pos;
4415
4416 /* The cell vars are the first elements of the closure,
4417 followed by the free vars. Update the offsets in
4418 c_freevars to account for number of cellvars. */
4419 pos = 0;
4420 while (PyDict_Next(freevars, &pos, &name, &v)) {
4421 int i = PyInt_AS_LONG(v) + offset;
4422 PyObject *o = PyInt_FromLong(i);
4423 if (o == NULL)
4424 return -1;
4425 if (PyDict_SetItem(freevars, name, o) < 0) {
4426 Py_DECREF(o);
4427 return -1;
4428 }
4429 Py_DECREF(o);
4430 }
4431 return 0;
4432}
4433
4434static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004435symtable_check_unoptimized(struct compiling *c,
4436 PySymtableEntryObject *ste,
4437 struct symbol_info *si)
4438{
4439 char buf[300];
4440
4441 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4442 || (ste->ste_nested && si->si_nimplicit)))
4443 return 0;
4444
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004445#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4446
4447#define ILLEGAL_IS "is a nested function"
4448
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004449#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004450"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004451
4452#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004453"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004454
4455#define ILLEGAL_EXEC_AND_IMPORT_STAR \
4456"function '%.100s' uses import * and bare exec, which are illegal" \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004457"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004458
4459 /* XXX perhaps the linenos for these opt-breaking statements
4460 should be stored so the exception can point to them. */
4461
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004462 if (ste->ste_child_free) {
4463 if (ste->ste_optimized == OPT_IMPORT_STAR)
4464 sprintf(buf, ILLEGAL_IMPORT_STAR,
4465 PyString_AS_STRING(ste->ste_name),
4466 ILLEGAL_CONTAINS);
4467 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4468 sprintf(buf, ILLEGAL_BARE_EXEC,
4469 PyString_AS_STRING(ste->ste_name),
4470 ILLEGAL_CONTAINS);
4471 else {
4472 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4473 PyString_AS_STRING(ste->ste_name),
4474 ILLEGAL_CONTAINS);
4475 }
4476 } else {
4477 if (ste->ste_optimized == OPT_IMPORT_STAR)
4478 sprintf(buf, ILLEGAL_IMPORT_STAR,
4479 PyString_AS_STRING(ste->ste_name),
4480 ILLEGAL_IS);
4481 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
4482 sprintf(buf, ILLEGAL_BARE_EXEC,
4483 PyString_AS_STRING(ste->ste_name),
4484 ILLEGAL_IS);
4485 else {
4486 sprintf(buf, ILLEGAL_EXEC_AND_IMPORT_STAR,
4487 PyString_AS_STRING(ste->ste_name),
4488 ILLEGAL_IS);
4489 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004490 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004491
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004492 PyErr_SetString(PyExc_SyntaxError, buf);
4493 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4494 ste->ste_opt_lineno);
4495 return -1;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004496}
4497
4498static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004499symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4500 struct symbol_info *si)
4501{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004502 if (c->c_future)
4503 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004504 if (ste->ste_generator)
4505 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004506 if (ste->ste_type != TYPE_MODULE)
4507 c->c_flags |= CO_NEWLOCALS;
4508 if (ste->ste_type == TYPE_FUNCTION) {
4509 c->c_nlocals = si->si_nlocals;
4510 if (ste->ste_optimized == 0)
4511 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004512 else if (ste->ste_optimized != OPT_EXEC)
4513 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004514 }
4515 return 0;
4516}
4517
4518static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004519symtable_load_symbols(struct compiling *c)
4520{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004521 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004522 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004523 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004524 PyObject *name, *varnames, *v;
4525 int i, flags, pos;
4526 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004527
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004528 if (implicit == NULL) {
4529 implicit = PyInt_FromLong(1);
4530 if (implicit == NULL)
4531 return -1;
4532 }
4533 v = NULL;
4534
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004535 if (symtable_init_compiling_symbols(c) < 0)
4536 goto fail;
4537 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004538 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004539 si.si_nlocals = PyList_GET_SIZE(varnames);
4540 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004541
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004542 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004543 v = PyInt_FromLong(i);
4544 if (PyDict_SetItem(c->c_locals,
4545 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004546 goto fail;
4547 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004548 }
4549
4550 /* XXX The cases below define the rules for whether a name is
4551 local or global. The logic could probably be clearer. */
4552 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004553 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4554 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004555
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004556 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004557 /* undo the original DEF_FREE */
4558 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004559
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004560 /* Deal with names that need two actions:
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004561 1. Cell variables that are also locals.
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004562 2. Free variables in methods that are also class
4563 variables or declared global.
4564 */
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004565 if (flags & (DEF_FREE | DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004566 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004567
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004568 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004569 c->c_argcount--;
4570 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004571 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004572 c->c_argcount--;
4573 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004574 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004575 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004576 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004577 if (flags & DEF_PARAM) {
4578 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004579 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004580 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004581 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004582 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004583 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004584 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004585 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4586 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004587 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004588 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004589 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4590 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004591 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004592 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004593 if (v == NULL)
4594 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004595 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004596 goto fail;
4597 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004598 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004599 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004600 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004601 } else if (is_free(flags)) {
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004602 if (ste->ste_nested) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004603 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004604 if (v == NULL)
4605 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004606 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004607 goto fail;
4608 Py_DECREF(v);
4609 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004610 si.si_nimplicit++;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004611 if (PyDict_SetItem(c->c_globals, name,
4612 implicit) < 0)
4613 goto fail;
4614 if (st->st_nscopes != 1) {
4615 v = PyInt_FromLong(flags);
4616 if (PyDict_SetItem(st->st_global,
4617 name, v))
4618 goto fail;
4619 Py_DECREF(v);
4620 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004621 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004622 }
4623 }
4624
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004625 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4626
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004627 if (si.si_ncells > 1) { /* one cell is always in order */
4628 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4629 c->c_varnames, c->c_flags) < 0)
4630 return -1;
4631 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004632 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4633 return -1;
4634 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004635 fail:
4636 /* is this always the right thing to do? */
4637 Py_XDECREF(v);
4638 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004639}
4640
4641static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004642symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004643{
4644 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004645
4646 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4647 if (st == NULL)
4648 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004649 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004650
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004651 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004652 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004653 goto fail;
4654 if ((st->st_symbols = PyDict_New()) == NULL)
4655 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004656 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004657 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004658 st->st_errors = 0;
4659 st->st_tmpname = 0;
4660 st->st_private = NULL;
4661 return st;
4662 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004663 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004664 return NULL;
4665}
4666
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004667void
4668PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004669{
4670 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004671 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004672 Py_XDECREF(st->st_cur);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004673 PyMem_Free((void *)st);
4674}
4675
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004676/* When the compiler exits a scope, it must should update the scope's
4677 free variable information with the list of free variables in its
4678 children.
4679
4680 Variables that are free in children and defined in the current
4681 scope are cellvars.
4682
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004683 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004684 false), free variables in children that are not defined here are
4685 implicit globals.
4686
4687*/
4688
4689static int
4690symtable_update_free_vars(struct symtable *st)
4691{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004692 int i, j, def;
4693 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004694 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004695
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004696 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004697 def = DEF_FREE_CLASS;
4698 else
4699 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004700 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004701 int pos = 0;
4702
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004703 if (list)
4704 PyList_SetSlice(list, 0,
4705 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004706 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004707 PyList_GET_ITEM(ste->ste_children, i);
4708 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004709 int flags = PyInt_AS_LONG(o);
4710 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004711 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004712 if (list == NULL) {
4713 list = PyList_New(0);
4714 if (list == NULL)
4715 return -1;
4716 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004717 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004718 if (PyList_Append(list, name) < 0) {
4719 Py_DECREF(list);
4720 return -1;
4721 }
4722 }
4723 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004724 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004725 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004726 v = PyDict_GetItem(ste->ste_symbols, name);
4727 /* If a name N is declared global in scope A and
4728 referenced in scope B contained (perhaps
4729 indirectly) in A and there are no scopes
4730 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004731 is global in B. Unless A is a class scope,
4732 because class scopes are not considered for
4733 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004734 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004735 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004736 int flags = PyInt_AS_LONG(v);
4737 if (flags & DEF_GLOBAL) {
4738 symtable_undo_free(st, child->ste_id,
4739 name);
4740 continue;
4741 }
4742 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004743 if (ste->ste_nested) {
4744 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004745 name, def) < 0) {
4746 Py_DECREF(list);
4747 return -1;
4748 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004749 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004750 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004751 name) < 0) {
4752 Py_DECREF(list);
4753 return -1;
4754 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004755 }
4756 }
4757 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004758
4759 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004760 return 0;
4761}
4762
4763/* If the current scope is a non-nested class or if name is not
4764 defined in the current, non-nested scope, then it is an implicit
4765 global in all nested scopes.
4766*/
4767
4768static int
4769symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4770{
4771 PyObject *o;
4772 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004773 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004774
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004775 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004776 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004777 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004778 if (o == NULL)
4779 return symtable_undo_free(st, child, name);
4780 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004781
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004782 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004783 return symtable_undo_free(st, child, name);
4784 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004785 return symtable_add_def_o(st, ste->ste_symbols,
4786 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004787}
4788
4789static int
4790symtable_undo_free(struct symtable *st, PyObject *id,
4791 PyObject *name)
4792{
4793 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004794 PyObject *info;
4795 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004796
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004797 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4798 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004799 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004800
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004801 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004802 if (info == NULL)
4803 return 0;
4804 v = PyInt_AS_LONG(info);
4805 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004806 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004807 DEF_FREE_GLOBAL) < 0)
4808 return -1;
4809 } else
4810 /* If the name is defined here or declared global,
4811 then the recursion stops. */
4812 return 0;
4813
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004814 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4815 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00004816 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004817 PyList_GET_ITEM(ste->ste_children, i);
4818 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004819 if (x < 0)
4820 return x;
4821 }
4822 return 0;
4823}
4824
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004825/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4826 This reference is released when the scope is exited, via the DECREF
4827 in symtable_exit_scope().
4828*/
4829
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004830static int
4831symtable_exit_scope(struct symtable *st)
4832{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004833 int end;
4834
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004835 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004836 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004837 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004838 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004839 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4840 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004841 if (PySequence_DelItem(st->st_stack, end) < 0)
4842 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004843 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004844}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004845
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004846static void
4847symtable_enter_scope(struct symtable *st, char *name, int type,
4848 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004849{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004850 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004851
4852 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004853 prev = st->st_cur;
4854 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4855 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004856 st->st_errors++;
4857 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004858 }
4859 }
Barry Warsaw0372af72001-02-23 18:22:59 +00004860 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004861 PySymtableEntry_New(st, name, type, lineno);
4862 if (strcmp(name, TOP) == 0)
4863 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004864 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004865 if (PyList_Append(prev->ste_children,
4866 (PyObject *)st->st_cur) < 0)
4867 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004868 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004869}
4870
4871static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004872symtable_lookup(struct symtable *st, char *name)
4873{
4874 char buffer[MANGLE_LEN];
4875 PyObject *v;
4876 int flags;
4877
4878 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4879 name = buffer;
4880 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4881 if (v == NULL) {
4882 if (PyErr_Occurred())
4883 return -1;
4884 else
4885 return 0;
4886 }
4887
4888 flags = PyInt_AS_LONG(v);
4889 return flags;
4890}
4891
4892static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004893symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004894{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004895 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004896 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004897 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004898
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004899 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004900 name = buffer;
4901 if ((s = PyString_InternFromString(name)) == NULL)
4902 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004903 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4904 Py_DECREF(s);
4905 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004906}
4907
4908/* Must only be called with mangled names */
4909
4910static int
4911symtable_add_def_o(struct symtable *st, PyObject *dict,
4912 PyObject *name, int flag)
4913{
4914 PyObject *o;
4915 int val;
4916
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004917 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004918 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004919 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004920 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00004921 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004922 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004923 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004924 return -1;
4925 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004926 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004927 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004928 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004929 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004930 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004931 Py_DECREF(o);
4932 return -1;
4933 }
4934 Py_DECREF(o);
4935
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004936 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004937 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004938 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004939 } else if (flag & DEF_GLOBAL) {
4940 /* XXX need to update DEF_GLOBAL for other flags too;
4941 perhaps only DEF_FREE_GLOBAL */
4942 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004943 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004944 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004945 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004946 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004947 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004948 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004949 Py_DECREF(o);
4950 return -1;
4951 }
4952 Py_DECREF(o);
4953 }
4954 return 0;
4955}
4956
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004957#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004958
Tim Peters08a898f2001-06-28 01:52:22 +00004959/* Look for a yield stmt under n. Return 1 if found, else 0.
4960 This hack is used to look inside "if 0:" blocks (which are normally
4961 ignored) in case those are the only places a yield occurs (so that this
4962 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00004963static int
4964look_for_yield(node *n)
4965{
4966 int i;
4967
4968 for (i = 0; i < NCH(n); ++i) {
4969 node *kid = CHILD(n, i);
4970
4971 switch (TYPE(kid)) {
4972
4973 case classdef:
4974 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00004975 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00004976 /* Stuff in nested functions and classes can't make
4977 the parent a generator. */
4978 return 0;
4979
4980 case yield_stmt:
4981 return 1;
4982
4983 default:
4984 if (look_for_yield(kid))
4985 return 1;
4986 }
4987 }
4988 return 0;
4989}
4990
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004991static void
4992symtable_node(struct symtable *st, node *n)
4993{
Jeremy Hylton961dfe02001-10-18 16:15:10 +00004994 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004995
4996 loop:
4997 switch (TYPE(n)) {
4998 case funcdef: {
4999 char *func_name = STR(CHILD(n, 1));
5000 symtable_add_def(st, func_name, DEF_LOCAL);
5001 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005002 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005003 symtable_funcdef(st, n);
5004 symtable_exit_scope(st);
5005 break;
5006 }
5007 case lambdef:
5008 if (NCH(n) == 4)
5009 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005010 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005011 symtable_funcdef(st, n);
5012 symtable_exit_scope(st);
5013 break;
5014 case classdef: {
5015 char *tmp, *class_name = STR(CHILD(n, 1));
5016 symtable_add_def(st, class_name, DEF_LOCAL);
5017 if (TYPE(CHILD(n, 2)) == LPAR) {
5018 node *bases = CHILD(n, 3);
5019 int i;
5020 for (i = 0; i < NCH(bases); i += 2) {
5021 symtable_node(st, CHILD(bases, i));
5022 }
5023 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005024 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005025 tmp = st->st_private;
5026 st->st_private = class_name;
5027 symtable_node(st, CHILD(n, NCH(n) - 1));
5028 st->st_private = tmp;
5029 symtable_exit_scope(st);
5030 break;
5031 }
5032 case if_stmt:
5033 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00005034 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5035 if (st->st_cur->ste_generator == 0)
5036 st->st_cur->ste_generator =
5037 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005038 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00005039 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005040 symtable_node(st, CHILD(n, i + 1));
5041 symtable_node(st, CHILD(n, i + 3));
5042 }
5043 if (i + 2 < NCH(n))
5044 symtable_node(st, CHILD(n, i + 2));
5045 break;
5046 case global_stmt:
5047 symtable_global(st, n);
5048 break;
5049 case import_stmt:
5050 symtable_import(st, n);
5051 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005052 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005053 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005054 symtable_node(st, CHILD(n, 1));
5055 if (NCH(n) > 2)
5056 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005057 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005058 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005059 st->st_cur->ste_opt_lineno = n->n_lineno;
5060 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005061 if (NCH(n) > 4)
5062 symtable_node(st, CHILD(n, 5));
5063 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005064
5065 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00005066 case assert_stmt:
5067 if (Py_OptimizeFlag)
5068 return;
5069 if (NCH(n) == 2) {
5070 n = CHILD(n, 1);
5071 goto loop;
5072 } else {
5073 symtable_node(st, CHILD(n, 1));
5074 n = CHILD(n, 3);
5075 goto loop;
5076 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005077 case except_clause:
5078 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005079 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005080 if (NCH(n) > 1) {
5081 n = CHILD(n, 1);
5082 goto loop;
5083 }
5084 break;
5085 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005086 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005087 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005088 case yield_stmt:
5089 st->st_cur->ste_generator = 1;
5090 n = CHILD(n, 1);
5091 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005092 case expr_stmt:
5093 if (NCH(n) == 1)
5094 n = CHILD(n, 0);
5095 else {
5096 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005097 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005098 symtable_node(st, CHILD(n, 2));
5099 break;
5100 } else {
5101 int i;
5102 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005103 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005104 n = CHILD(n, NCH(n) - 1);
5105 }
5106 }
5107 goto loop;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005108 case list_iter:
5109 n = CHILD(n, 0);
5110 if (TYPE(n) == list_for) {
5111 st->st_tmpname++;
5112 symtable_list_comprehension(st, n);
5113 st->st_tmpname--;
5114 } else {
5115 REQ(n, list_if);
5116 symtable_node(st, CHILD(n, 1));
5117 if (NCH(n) == 3) {
5118 n = CHILD(n, 2);
5119 goto loop;
5120 }
5121 }
5122 break;
5123 case for_stmt:
5124 symtable_assign(st, CHILD(n, 1), 0);
5125 for (i = 3; i < NCH(n); ++i)
5126 if (TYPE(CHILD(n, i)) >= single_input)
5127 symtable_node(st, CHILD(n, i));
5128 break;
5129 /* The remaining cases fall through to default except in
5130 special circumstances. This requires the individual cases
5131 to be coded with great care, even though they look like
5132 rather innocuous. Each case must double-check TYPE(n).
5133 */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005134 case argument:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005135 if (TYPE(n) == argument && NCH(n) == 3) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005136 n = CHILD(n, 2);
5137 goto loop;
5138 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005139 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005140 case listmaker:
5141 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005142 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005143 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005144 symtable_node(st, CHILD(n, 0));
5145 st->st_tmpname--;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005146 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005147 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005148 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005149 case atom:
5150 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5151 symtable_add_use(st, STR(CHILD(n, 0)));
5152 break;
5153 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005154 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005155 default:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005156 /* Walk over every non-token child with a special case
5157 for one child.
5158 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005159 if (NCH(n) == 1) {
5160 n = CHILD(n, 0);
5161 goto loop;
5162 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005163 for (i = 0; i < NCH(n); ++i)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005164 if (TYPE(CHILD(n, i)) >= single_input)
5165 symtable_node(st, CHILD(n, i));
5166 }
5167}
5168
5169static void
5170symtable_funcdef(struct symtable *st, node *n)
5171{
5172 node *body;
5173
5174 if (TYPE(n) == lambdef) {
5175 if (NCH(n) == 4)
5176 symtable_params(st, CHILD(n, 1));
5177 } else
5178 symtable_params(st, CHILD(n, 2));
5179 body = CHILD(n, NCH(n) - 1);
5180 symtable_node(st, body);
5181}
5182
5183/* The next two functions parse the argument tuple.
5184 symtable_default_arg() checks for names in the default arguments,
5185 which are references in the defining scope. symtable_params()
5186 parses the parameter names, which are defined in the function's
5187 body.
5188
5189 varargslist:
5190 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5191 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5192*/
5193
5194static void
5195symtable_default_args(struct symtable *st, node *n)
5196{
5197 node *c;
5198 int i;
5199
5200 if (TYPE(n) == parameters) {
5201 n = CHILD(n, 1);
5202 if (TYPE(n) == RPAR)
5203 return;
5204 }
5205 REQ(n, varargslist);
5206 for (i = 0; i < NCH(n); i += 2) {
5207 c = CHILD(n, i);
5208 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5209 break;
5210 }
5211 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5212 symtable_node(st, CHILD(n, i));
5213 }
5214}
5215
5216static void
5217symtable_params(struct symtable *st, node *n)
5218{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005219 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005220 node *c = NULL;
5221
5222 if (TYPE(n) == parameters) {
5223 n = CHILD(n, 1);
5224 if (TYPE(n) == RPAR)
5225 return;
5226 }
5227 REQ(n, varargslist);
5228 for (i = 0; i < NCH(n); i += 2) {
5229 c = CHILD(n, i);
5230 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5231 ext = 1;
5232 break;
5233 }
5234 if (TYPE(c) == test) {
5235 continue;
5236 }
5237 if (TYPE(CHILD(c, 0)) == NAME)
5238 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5239 else {
5240 char nbuf[10];
5241 sprintf(nbuf, ".%d", i);
5242 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005243 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005244 }
5245 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005246 if (ext) {
5247 c = CHILD(n, i);
5248 if (TYPE(c) == STAR) {
5249 i++;
5250 symtable_add_def(st, STR(CHILD(n, i)),
5251 DEF_PARAM | DEF_STAR);
5252 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005253 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005254 c = NULL;
5255 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005256 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005257 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005258 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005259 i++;
5260 symtable_add_def(st, STR(CHILD(n, i)),
5261 DEF_PARAM | DEF_DOUBLESTAR);
5262 }
5263 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005264 if (complex >= 0) {
5265 int j;
5266 for (j = 0; j <= complex; j++) {
5267 c = CHILD(n, j);
5268 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005269 c = CHILD(n, ++j);
5270 else if (TYPE(c) == EQUAL)
5271 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005272 if (TYPE(CHILD(c, 0)) == LPAR)
5273 symtable_params_fplist(st, CHILD(c, 1));
5274 }
5275 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005276}
5277
5278static void
5279symtable_params_fplist(struct symtable *st, node *n)
5280{
5281 int i;
5282 node *c;
5283
5284 REQ(n, fplist);
5285 for (i = 0; i < NCH(n); i += 2) {
5286 c = CHILD(n, i);
5287 REQ(c, fpdef);
5288 if (NCH(c) == 1)
5289 symtable_add_def(st, STR(CHILD(c, 0)),
5290 DEF_PARAM | DEF_INTUPLE);
5291 else
5292 symtable_params_fplist(st, CHILD(c, 1));
5293 }
5294
5295}
5296
5297static void
5298symtable_global(struct symtable *st, node *n)
5299{
5300 int i;
5301
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005302 /* XXX It might be helpful to warn about module-level global
5303 statements, but it's hard to tell the difference between
5304 module-level and a string passed to exec.
5305 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005306
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005307 for (i = 1; i < NCH(n); i += 2) {
5308 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005309 int flags;
5310
5311 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005312 if (flags < 0)
5313 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005314 if (flags && flags != DEF_GLOBAL) {
5315 char buf[500];
5316 if (flags & DEF_PARAM) {
5317 PyErr_Format(PyExc_SyntaxError,
5318 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005319 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005320 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005321 st->st_cur->ste_lineno);
5322 st->st_errors++;
5323 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005324 }
5325 else {
5326 if (flags & DEF_LOCAL)
5327 sprintf(buf, GLOBAL_AFTER_ASSIGN,
5328 name);
5329 else
5330 sprintf(buf, GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005331 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005332 }
5333 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005334 symtable_add_def(st, name, DEF_GLOBAL);
5335 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005336}
5337
5338static void
5339symtable_list_comprehension(struct symtable *st, node *n)
5340{
5341 char tmpname[12];
5342
Jeremy Hylton23b42272001-03-19 20:38:06 +00005343 sprintf(tmpname, "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005344 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005345 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005346 symtable_node(st, CHILD(n, 3));
5347 if (NCH(n) == 5)
5348 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005349}
5350
5351static void
5352symtable_import(struct symtable *st, node *n)
5353{
5354 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005355 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005356 | 'from' dotted_name 'import'
5357 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005358 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005359 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005360 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005361 node *dotname = CHILD(n, 1);
5362 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5363 /* check for bogus imports */
5364 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5365 PyErr_SetString(PyExc_SyntaxError,
5366 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005367 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005368 n->n_lineno);
5369 st->st_errors++;
5370 return;
5371 }
5372 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005373 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005374 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005375 if (symtable_warn(st,
5376 "import * only allowed at module level") < 0)
5377 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005378 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005379 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005380 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005381 } else {
5382 for (i = 3; i < NCH(n); i += 2) {
5383 node *c = CHILD(n, i);
5384 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005385 symtable_assign(st, CHILD(c, 2),
5386 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005387 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005388 symtable_assign(st, CHILD(c, 0),
5389 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005390 }
5391 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005392 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005393 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005394 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005395 }
5396 }
5397}
5398
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005399/* The third argument to symatble_assign() is a flag to be passed to
5400 symtable_add_def() if it is eventually called. The flag is useful
5401 to specify the particular type of assignment that should be
5402 recorded, e.g. an assignment caused by import.
5403 */
5404
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005405static void
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005406symtable_assign(struct symtable *st, node *n, int def_flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005407{
5408 node *tmp;
5409 int i;
5410
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005411 loop:
5412 switch (TYPE(n)) {
5413 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005414 /* invalid assignment, e.g. lambda x:x=2. The next
5415 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005416 return;
5417 case power:
5418 if (NCH(n) > 2) {
5419 for (i = 2; i < NCH(n); ++i)
5420 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5421 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005422 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005423 if (NCH(n) > 1) {
5424 symtable_node(st, CHILD(n, 0));
5425 symtable_node(st, CHILD(n, 1));
5426 } else {
5427 n = CHILD(n, 0);
5428 goto loop;
5429 }
5430 return;
5431 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005432 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5433 /* XXX This is an error, but the next pass
5434 will catch it. */
5435 return;
5436 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005437 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005438 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005439 }
5440 return;
5441 case exprlist:
5442 case testlist:
5443 if (NCH(n) == 1) {
5444 n = CHILD(n, 0);
5445 goto loop;
5446 }
5447 else {
5448 int i;
5449 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005450 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005451 return;
5452 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005453 case atom:
5454 tmp = CHILD(n, 0);
5455 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5456 n = CHILD(n, 1);
5457 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005458 } else if (TYPE(tmp) == NAME) {
Jeremy Hyltona830b382001-04-09 16:07:59 +00005459 if (strcmp(STR(tmp), "__debug__") == 0)
5460 symtable_warn(st, ASSIGN_DEBUG);
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005461 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005462 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005463 return;
5464 case dotted_as_name:
5465 if (NCH(n) == 3)
5466 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005467 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005468 else
5469 symtable_add_def(st,
5470 STR(CHILD(CHILD(n,
5471 0), 0)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005472 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005473 return;
5474 case dotted_name:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005475 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005476 return;
5477 case NAME:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005478 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005479 return;
5480 default:
5481 if (NCH(n) == 0)
5482 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005483 if (NCH(n) == 1) {
5484 n = CHILD(n, 0);
5485 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005486 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005487 /* Should only occur for errors like x + 1 = 1,
5488 which will be caught in the next pass. */
5489 for (i = 0; i < NCH(n); ++i)
5490 if (TYPE(CHILD(n, i)) >= single_input)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005491 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005492 }
5493}