blob: 442128495d2dbbcfbb7a465a380d511b1e224d6d [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);
Barry Warsaw8f6d8682001-11-28 21:10:39 +0000123 PyOS_snprintf(buf, sizeof(buf),
124 "<code object %.100s at %p, file \"%.300s\", line %d>",
125 name, co, filename, lineno);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000126 return PyString_FromString(buf);
Guido van Rossum2dff9911992-09-03 20:50:59 +0000127}
128
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000129static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000130code_compare(PyCodeObject *co, PyCodeObject *cp)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000131{
132 int cmp;
Guido van Rossum44679592000-04-10 16:20:31 +0000133 cmp = PyObject_Compare(co->co_name, cp->co_name);
134 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000135 cmp = co->co_argcount - cp->co_argcount;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000136 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000137 cmp = co->co_nlocals - cp->co_nlocals;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000138 if (cmp) return cmp;
Guido van Rossumbeef8aa1997-08-29 17:12:43 +0000139 cmp = co->co_flags - cp->co_flags;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000140 if (cmp) return cmp;
Guido van Rossumd076c731998-10-07 19:42:25 +0000141 cmp = PyObject_Compare(co->co_code, cp->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000142 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000143 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000144 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000145 cmp = PyObject_Compare(co->co_names, cp->co_names);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000146 if (cmp) return cmp;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000147 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000148 if (cmp) return cmp;
149 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
150 if (cmp) return cmp;
151 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000152 return cmp;
153}
154
155static long
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000156code_hash(PyCodeObject *co)
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000157{
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000158 long h, h0, h1, h2, h3, h4, h5, h6;
Guido van Rossum44679592000-04-10 16:20:31 +0000159 h0 = PyObject_Hash(co->co_name);
160 if (h0 == -1) return -1;
Guido van Rossumd076c731998-10-07 19:42:25 +0000161 h1 = PyObject_Hash(co->co_code);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000162 if (h1 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000163 h2 = PyObject_Hash(co->co_consts);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000164 if (h2 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000165 h3 = PyObject_Hash(co->co_names);
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000166 if (h3 == -1) return -1;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000167 h4 = PyObject_Hash(co->co_varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000168 if (h4 == -1) return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000169 h5 = PyObject_Hash(co->co_freevars);
170 if (h5 == -1) return -1;
171 h6 = PyObject_Hash(co->co_cellvars);
172 if (h6 == -1) return -1;
173 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
Guido van Rossum681d79a1995-07-18 14:51:37 +0000174 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
Guido van Rossum2e8f8a31993-11-05 10:20:10 +0000175 if (h == -1) h = -2;
176 return h;
177}
178
Jeremy Hylton78891072001-03-01 06:09:34 +0000179/* XXX code objects need to participate in GC? */
180
Guido van Rossum79f25d91997-04-29 20:08:16 +0000181PyTypeObject PyCode_Type = {
182 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000183 0,
184 "code",
Guido van Rossum79f25d91997-04-29 20:08:16 +0000185 sizeof(PyCodeObject),
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000186 0,
Jeremy Hyltonc785f482001-09-14 20:08:07 +0000187 (destructor)code_dealloc, /* tp_dealloc */
188 0, /* tp_print */
189 0, /* tp_getattr */
190 0, /* tp_setattr */
191 (cmpfunc)code_compare, /* tp_compare */
192 (reprfunc)code_repr, /* tp_repr */
193 0, /* tp_as_number */
194 0, /* tp_as_sequence */
195 0, /* tp_as_mapping */
196 (hashfunc)code_hash, /* tp_hash */
197 0, /* tp_call */
198 0, /* tp_str */
199 PyObject_GenericGetAttr, /* tp_getattro */
200 0, /* tp_setattro */
201 0, /* tp_as_buffer */
202 Py_TPFLAGS_DEFAULT, /* tp_flags */
203 0, /* tp_doc */
204 0, /* tp_traverse */
205 0, /* tp_clear */
206 0, /* tp_richcompare */
207 0, /* tp_weaklistoffset */
208 0, /* tp_iter */
209 0, /* tp_iternext */
210 0, /* tp_methods */
211 code_memberlist, /* tp_members */
212 0, /* tp_getset */
213 0, /* tp_base */
214 0, /* tp_dict */
215 0, /* tp_descr_get */
216 0, /* tp_descr_set */
217 0, /* tp_dictoffset */
218 0, /* tp_init */
219 0, /* tp_alloc */
220 0, /* tp_new */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000221};
222
Guido van Rossum644a12b1997-04-09 19:24:53 +0000223#define NAME_CHARS \
224 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
225
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000226/* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
227
228static int
229all_name_chars(unsigned char *s)
230{
Guido van Rossumcd90c202001-02-09 15:06:42 +0000231 static char ok_name_char[256];
232 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000233
Guido van Rossumcd90c202001-02-09 15:06:42 +0000234 if (ok_name_char[*name_chars] == 0) {
235 unsigned char *p;
236 for (p = name_chars; *p; p++)
237 ok_name_char[*p] = 1;
238 }
239 while (*s) {
240 if (ok_name_char[*s++] == 0)
241 return 0;
242 }
243 return 1;
Guido van Rossumfc5ce612001-01-19 00:24:06 +0000244}
245
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000246static int
247intern_strings(PyObject *tuple)
248{
249 int i;
250
251 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
252 PyObject *v = PyTuple_GET_ITEM(tuple, i);
253 if (v == NULL || !PyString_Check(v)) {
254 Py_FatalError("non-string found in code slot");
255 PyErr_BadInternalCall();
256 return -1;
257 }
258 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
259 }
260 return 0;
261}
262
Guido van Rossum79f25d91997-04-29 20:08:16 +0000263PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000264PyCode_New(int argcount, int nlocals, int stacksize, int flags,
265 PyObject *code, PyObject *consts, PyObject *names,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000266 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
267 PyObject *filename, PyObject *name, int firstlineno,
268 PyObject *lnotab)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000269{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 PyCodeObject *co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000271 int i;
272 /* 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) ||
Jeremy Hylton9f64caa2001-11-09 22:02:48 +0000282 lnotab == NULL || !PyString_Check(lnotab) ||
283 !PyObject_CheckReadBuffer(code)) {
Guido van Rossumd076c731998-10-07 19:42:25 +0000284 PyErr_BadInternalCall();
285 return NULL;
286 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000287 intern_strings(names);
288 intern_strings(varnames);
289 if (freevars == NULL)
290 freevars = PyTuple_New(0);
291 intern_strings(freevars);
292 if (cellvars == NULL)
293 cellvars = PyTuple_New(0);
294 intern_strings(cellvars);
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000295 /* Intern selected string constants */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000296 for (i = PyTuple_Size(consts); --i >= 0; ) {
297 PyObject *v = PyTuple_GetItem(consts, i);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (!PyString_Check(v))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000299 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000300 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000301 continue;
302 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000303 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000304 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000305 if (co != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +0000306 co->co_argcount = argcount;
307 co->co_nlocals = nlocals;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000308 co->co_stacksize = stacksize;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000309 co->co_flags = flags;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 Py_INCREF(code);
Guido van Rossumd076c731998-10-07 19:42:25 +0000311 co->co_code = code;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000312 Py_INCREF(consts);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000313 co->co_consts = consts;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000314 Py_INCREF(names);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000315 co->co_names = names;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 Py_INCREF(varnames);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000317 co->co_varnames = varnames;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000318 Py_INCREF(freevars);
319 co->co_freevars = freevars;
320 Py_INCREF(cellvars);
321 co->co_cellvars = cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322 Py_INCREF(filename);
Guido van Rossuma082ce41991-06-04 19:41:56 +0000323 co->co_filename = filename;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000324 Py_INCREF(name);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000325 co->co_name = name;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000326 co->co_firstlineno = firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327 Py_INCREF(lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000328 co->co_lnotab = lnotab;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000329 }
330 return co;
331}
332
333
334/* Data structure used internally */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000335
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000336/* The compiler uses two passes to generate bytecodes. The first pass
337 builds the symbol table. The second pass generates the bytecode.
338
339 The first pass uses a single symtable struct. The second pass uses
340 a compiling struct for each code block. The compiling structs
341 share a reference to the symtable.
342
343 The two passes communicate via symtable_load_symbols() and via
344 is_local() and is_global(). The former initializes several slots
345 in the compiling struct: c_varnames, c_locals, c_nlocals,
346 c_argcount, c_globals, and c_flags.
347*/
348
Tim Peters2a7f3842001-06-09 09:26:21 +0000349/* All about c_lnotab.
350
351c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
352mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
353to source code line #s (when needed for tracebacks) via c_lnotab instead.
354The array is conceptually a list of
355 (bytecode offset increment, line number increment)
356pairs. The details are important and delicate, best illustrated by example:
357
358 byte code offset source code line number
359 0 1
360 6 2
361 50 7
362 350 307
363 361 308
364
365The first trick is that these numbers aren't stored, only the increments
366from one row to the next (this doesn't really work, but it's a start):
367
368 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
369
370The second trick is that an unsigned byte can't hold negative values, or
371values larger than 255, so (a) there's a deep assumption that byte code
372offsets and their corresponding line #s both increase monotonically, and (b)
373if at least one column jumps by more than 255 from one row to the next, more
374than one pair is written to the table. In case #b, there's no way to know
375from looking at the table later how many were written. That's the delicate
376part. A user of c_lnotab desiring to find the source line number
377corresponding to a bytecode address A should do something like this
378
379 lineno = addr = 0
380 for addr_incr, line_incr in c_lnotab:
381 addr += addr_incr
382 if addr > A:
383 return lineno
384 lineno += line_incr
385
386In order for this to work, when the addr field increments by more than 255,
387the line # increment in each pair generated must be 0 until the remaining addr
388increment is < 256. So, in the example above, com_set_lineno should not (as
389was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
390255, 0, 45, 255, 0, 45.
391*/
392
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000393struct compiling {
Fred Drakefd1f1be2000-09-08 16:31:24 +0000394 PyObject *c_code; /* string */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000395 PyObject *c_consts; /* list of objects */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000396 PyObject *c_const_dict; /* inverse of c_consts */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000397 PyObject *c_names; /* list of strings (names) */
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000398 PyObject *c_name_dict; /* inverse of c_names */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000399 PyObject *c_globals; /* dictionary (value=None) */
400 PyObject *c_locals; /* dictionary (value=localID) */
401 PyObject *c_varnames; /* list (inverse of c_locals) */
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000402 PyObject *c_freevars; /* dictionary (value=None) */
403 PyObject *c_cellvars; /* list */
Guido van Rossum681d79a1995-07-18 14:51:37 +0000404 int c_nlocals; /* index of next local */
405 int c_argcount; /* number of top-level arguments */
406 int c_flags; /* same as co_flags */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000407 int c_nexti; /* index into c_code */
408 int c_errors; /* counts errors occurred */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000409 int c_infunction; /* set when compiling a function */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000410 int c_interactive; /* generating code for interactive command */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000411 int c_loops; /* counts nested loops */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000412 int c_begin; /* begin of current loop, for 'continue' */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000413 int c_block[CO_MAXBLOCKS]; /* stack of block types */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000414 int c_nblocks; /* current block stack level */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000415 char *c_filename; /* filename of current node */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000416 char *c_name; /* name of object (e.g. function) */
Guido van Rossum452a9831996-09-17 14:32:04 +0000417 int c_lineno; /* Current line number */
Guido van Rossum8b993a91997-01-17 21:04:03 +0000418 int c_stacklevel; /* Current stack level */
419 int c_maxstacklevel; /* Maximum stack level */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000420 int c_firstlineno;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000421 PyObject *c_lnotab; /* Table mapping address to line number */
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000422 int c_last_addr, c_last_line, c_lnotab_next;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000423 char *c_private; /* for private name mangling */
Skip Montanaro803d6e52000-08-12 18:09:51 +0000424 int c_tmpname; /* temporary local name counter */
Guido van Rossumcd90c202001-02-09 15:06:42 +0000425 int c_nested; /* Is block nested funcdef or lamdef? */
426 int c_closure; /* Is nested w/freevars? */
427 struct symtable *c_symtable; /* pointer to module symbol table */
Jeremy Hylton4db62b12001-02-27 19:07:02 +0000428 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000429};
430
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000431static int
432is_free(int v)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000433{
434 if ((v & (USE | DEF_FREE))
435 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
436 return 1;
437 if (v & DEF_FREE_CLASS)
438 return 1;
439 return 0;
440}
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000441
Jeremy Hylton5acc0c02001-02-02 20:01:10 +0000442static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443com_error(struct compiling *c, PyObject *exc, char *msg)
Guido van Rossum452a9831996-09-17 14:32:04 +0000444{
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000445 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
446
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000447 if (c == NULL) {
448 /* Error occurred via symtable call to
449 is_constant_false */
450 PyErr_SetString(exc, msg);
451 return;
452 }
Guido van Rossum635abd21997-01-06 22:56:52 +0000453 c->c_errors++;
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000454 if (c->c_lineno < 1 || c->c_interactive) {
455 /* Unknown line number or interactive input */
Guido van Rossum79f25d91997-04-29 20:08:16 +0000456 PyErr_SetString(exc, msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000457 return;
458 }
Fred Drakedcf08e02000-08-15 15:49:44 +0000459 v = PyString_FromString(msg);
Guido van Rossum452a9831996-09-17 14:32:04 +0000460 if (v == NULL)
461 return; /* MemoryError, too bad */
Fred Drakedcf08e02000-08-15 15:49:44 +0000462
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000463 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
Jeremy Hylton9f1b9932001-02-28 07:07:43 +0000464 if (line == NULL) {
465 Py_INCREF(Py_None);
466 line = Py_None;
467 }
468 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
469 Py_None, line);
470 if (t == NULL)
471 goto exit;
472 w = Py_BuildValue("(OO)", v, t);
473 if (w == NULL)
474 goto exit;
475 PyErr_SetObject(exc, w);
476 exit:
477 Py_XDECREF(t);
478 Py_XDECREF(v);
479 Py_XDECREF(w);
480 Py_XDECREF(line);
Guido van Rossum452a9831996-09-17 14:32:04 +0000481}
482
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000483/* Interface to the block stack */
484
485static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000486block_push(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000487{
Guido van Rossum8b993a91997-01-17 21:04:03 +0000488 if (c->c_nblocks >= CO_MAXBLOCKS) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000489 com_error(c, PyExc_SystemError,
490 "too many statically nested blocks");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000491 }
492 else {
493 c->c_block[c->c_nblocks++] = type;
494 }
495}
496
497static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000498block_pop(struct compiling *c, int type)
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000499{
500 if (c->c_nblocks > 0)
501 c->c_nblocks--;
502 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000503 com_error(c, PyExc_SystemError, "bad block pop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000504 }
505}
506
Guido van Rossum681d79a1995-07-18 14:51:37 +0000507/* Prototype forward declarations */
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000508
Tim Petersdbd9ba62000-07-09 03:09:57 +0000509static int com_init(struct compiling *, char *);
510static void com_free(struct compiling *);
511static void com_push(struct compiling *, int);
512static void com_pop(struct compiling *, int);
513static void com_done(struct compiling *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000514static void com_node(struct compiling *, node *);
515static void com_factor(struct compiling *, node *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000516static void com_addbyte(struct compiling *, int);
517static void com_addint(struct compiling *, int);
518static void com_addoparg(struct compiling *, int, int);
519static void com_addfwref(struct compiling *, int, int *);
520static void com_backpatch(struct compiling *, int);
521static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
522static int com_addconst(struct compiling *, PyObject *);
523static int com_addname(struct compiling *, PyObject *);
524static void com_addopname(struct compiling *, int, node *);
525static void com_list(struct compiling *, node *, int);
Skip Montanaro803d6e52000-08-12 18:09:51 +0000526static void com_list_iter(struct compiling *, node *, node *, char *);
Tim Petersdbd9ba62000-07-09 03:09:57 +0000527static int com_argdefs(struct compiling *, node *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000528static void com_assign(struct compiling *, node *, int, node *);
529static void com_assign_name(struct compiling *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000530static PyCodeObject *icompile(node *, struct compiling *);
Jeremy Hylton9f324e92001-03-01 22:59:14 +0000531static PyCodeObject *jcompile(node *, char *, struct compiling *,
532 PyCompilerFlags *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000533static PyObject *parsestrplus(struct compiling*, node *);
534static PyObject *parsestr(struct compiling *, char *);
Thomas Wouters434d0822000-08-24 20:11:32 +0000535static node *get_rawdocstring(node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000536
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000537static int get_ref_type(struct compiling *, char *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000538
539/* symtable operations */
540static int symtable_build(struct compiling *, node *);
541static int symtable_load_symbols(struct compiling *);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000542static struct symtable *symtable_init(void);
Jeremy Hylton4b38da62001-02-02 18:19:15 +0000543static void symtable_enter_scope(struct symtable *, char *, int, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000544static int symtable_exit_scope(struct symtable *);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000545static int symtable_add_def(struct symtable *, char *, int);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000546static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000547
548static void symtable_node(struct symtable *, node *);
549static void symtable_funcdef(struct symtable *, node *);
550static void symtable_default_args(struct symtable *, node *);
551static void symtable_params(struct symtable *, node *);
552static void symtable_params_fplist(struct symtable *, node *n);
553static void symtable_global(struct symtable *, node *);
554static void symtable_import(struct symtable *, node *);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000555static void symtable_assign(struct symtable *, node *, int);
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000556static void symtable_list_comprehension(struct symtable *, node *);
557
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000558static int symtable_update_free_vars(struct symtable *);
559static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
560static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
561
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000562/* helper */
563static void
564do_pad(int pad)
565{
566 int i;
567 for (i = 0; i < pad; ++i)
568 fprintf(stderr, " ");
569}
570
571static void
572dump(node *n, int pad, int depth)
573{
574 int i;
575 if (depth == 0)
576 return;
577 do_pad(pad);
578 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
579 if (depth > 0)
580 depth--;
581 for (i = 0; i < NCH(n); ++i)
582 dump(CHILD(n, i), pad + 1, depth);
583}
584
585#define DUMP(N) dump(N, 0, -1)
586
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000587static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000588com_init(struct compiling *c, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000589{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000590 memset((void *)c, '\0', sizeof(struct compiling));
Guido van Rossum79f25d91997-04-29 20:08:16 +0000591 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
592 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000593 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000594 if ((c->c_consts = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000595 goto fail;
596 if ((c->c_const_dict = PyDict_New()) == NULL)
597 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000598 if ((c->c_names = PyList_New(0)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000599 goto fail;
600 if ((c->c_name_dict = PyDict_New()) == NULL)
601 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000602 if ((c->c_locals = PyDict_New()) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000603 goto fail;
Guido van Rossum79f25d91997-04-29 20:08:16 +0000604 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
605 1000)) == NULL)
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000606 goto fail;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000607 c->c_globals = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000608 c->c_varnames = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000609 c->c_freevars = NULL;
610 c->c_cellvars = NULL;
Guido van Rossum681d79a1995-07-18 14:51:37 +0000611 c->c_nlocals = 0;
612 c->c_argcount = 0;
613 c->c_flags = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000614 c->c_nexti = 0;
615 c->c_errors = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000616 c->c_infunction = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +0000617 c->c_interactive = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000618 c->c_loops = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +0000619 c->c_begin = 0;
620 c->c_nblocks = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000621 c->c_filename = filename;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000622 c->c_name = "?";
Guido van Rossum452a9831996-09-17 14:32:04 +0000623 c->c_lineno = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000624 c->c_stacklevel = 0;
625 c->c_maxstacklevel = 0;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000626 c->c_firstlineno = 0;
627 c->c_last_addr = 0;
628 c->c_last_line = 0;
Barry Warsaw174e8012001-01-22 04:35:57 +0000629 c->c_lnotab_next = 0;
Skip Montanaro803d6e52000-08-12 18:09:51 +0000630 c->c_tmpname = 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000631 c->c_nested = 0;
632 c->c_closure = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000633 c->c_symtable = NULL;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000634 return 1;
635
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000636 fail:
637 com_free(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000638 return 0;
639}
640
641static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000642com_free(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000643{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000644 Py_XDECREF(c->c_code);
645 Py_XDECREF(c->c_consts);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000646 Py_XDECREF(c->c_const_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000647 Py_XDECREF(c->c_names);
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000648 Py_XDECREF(c->c_name_dict);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000649 Py_XDECREF(c->c_globals);
650 Py_XDECREF(c->c_locals);
651 Py_XDECREF(c->c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000652 Py_XDECREF(c->c_freevars);
653 Py_XDECREF(c->c_cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000654 Py_XDECREF(c->c_lnotab);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +0000655 if (c->c_future)
656 PyMem_Free((void *)c->c_future);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000657}
658
659static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660com_push(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000661{
662 c->c_stacklevel += n;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000663 if (c->c_stacklevel > c->c_maxstacklevel) {
Guido van Rossum8b993a91997-01-17 21:04:03 +0000664 c->c_maxstacklevel = c->c_stacklevel;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000665 /*
666 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
667 c->c_filename, c->c_name, c->c_lineno,
668 c->c_nexti, c->c_stacklevel, n);
669 */
670 }
Guido van Rossum8b993a91997-01-17 21:04:03 +0000671}
672
673static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000674com_pop(struct compiling *c, int n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000675{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000676 if (c->c_stacklevel < n)
Guido van Rossum8b993a91997-01-17 21:04:03 +0000677 c->c_stacklevel = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +0000678 else
679 c->c_stacklevel -= n;
680}
681
682static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000683com_done(struct compiling *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000684{
685 if (c->c_code != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000686 _PyString_Resize(&c->c_code, c->c_nexti);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000687 if (c->c_lnotab != NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000688 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000689}
690
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000691static int
692com_check_size(PyObject **s, int offset)
693{
694 int len = PyString_GET_SIZE(*s);
695 if (offset >= len)
696 return _PyString_Resize(s, len * 2);
697 return 0;
698}
699
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000700static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000701com_addbyte(struct compiling *c, int byte)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000702{
Guido van Rossum681d79a1995-07-18 14:51:37 +0000703 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000704 assert(byte >= 0 && byte <= 255);
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000705 assert(c->c_code);
706 if (com_check_size(&c->c_code, c->c_nexti)) {
707 c->c_errors++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000708 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000709 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000710 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000711}
712
713static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000714com_addint(struct compiling *c, int x)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000715{
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000716 com_addbyte(c, x & 0xff);
717 com_addbyte(c, x >> 8); /* XXX x should be positive */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000718}
719
720static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000721com_add_lnotab(struct compiling *c, int addr, int line)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000722{
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000723 char *p;
724 if (c->c_lnotab == NULL)
725 return;
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000726 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
727 c->c_errors++;
728 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000729 }
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000730 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000731 *p++ = addr;
732 *p++ = line;
733 c->c_lnotab_next += 2;
734}
735
736static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000737com_set_lineno(struct compiling *c, int lineno)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000738{
739 c->c_lineno = lineno;
740 if (c->c_firstlineno == 0) {
741 c->c_firstlineno = c->c_last_line = lineno;
742 }
743 else {
744 int incr_addr = c->c_nexti - c->c_last_addr;
745 int incr_line = lineno - c->c_last_line;
Tim Peters2a7f3842001-06-09 09:26:21 +0000746 while (incr_addr > 255) {
747 com_add_lnotab(c, 255, 0);
748 incr_addr -= 255;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000749 }
Tim Peters2a7f3842001-06-09 09:26:21 +0000750 while (incr_line > 255) {
751 com_add_lnotab(c, incr_addr, 255);
752 incr_line -=255;
753 incr_addr = 0;
754 }
755 if (incr_addr > 0 || incr_line > 0)
756 com_add_lnotab(c, incr_addr, incr_line);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000757 c->c_last_addr = c->c_nexti;
758 c->c_last_line = lineno;
759 }
760}
761
762static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000763com_addoparg(struct compiling *c, int op, int arg)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000764{
Fred Drakeef8ace32000-08-24 00:32:09 +0000765 int extended_arg = arg >> 16;
Guido van Rossum8e793d91997-03-03 19:13:14 +0000766 if (op == SET_LINENO) {
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000767 com_set_lineno(c, arg);
Guido van Rossum8e793d91997-03-03 19:13:14 +0000768 if (Py_OptimizeFlag)
769 return;
Guido van Rossumda4eb5c1997-01-24 03:43:35 +0000770 }
Fred Drakeef8ace32000-08-24 00:32:09 +0000771 if (extended_arg){
772 com_addbyte(c, EXTENDED_ARG);
773 com_addint(c, extended_arg);
774 arg &= 0xffff;
775 }
Guido van Rossum8e793d91997-03-03 19:13:14 +0000776 com_addbyte(c, op);
777 com_addint(c, arg);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000778}
779
780static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000781com_addfwref(struct compiling *c, int op, int *p_anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000782{
783 /* Compile a forward reference for backpatching */
784 int here;
785 int anchor;
786 com_addbyte(c, op);
787 here = c->c_nexti;
788 anchor = *p_anchor;
789 *p_anchor = here;
790 com_addint(c, anchor == 0 ? 0 : here - anchor);
791}
792
793static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000794com_backpatch(struct compiling *c, int anchor)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000795{
Jeremy Hylton93a569d2001-10-17 13:22:22 +0000796 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000797 int target = c->c_nexti;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000798 int dist;
799 int prev;
800 for (;;) {
801 /* Make the JUMP instruction at anchor point to target */
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000802 prev = code[anchor] + (code[anchor+1] << 8);
803 dist = target - (anchor+2);
804 code[anchor] = dist & 0xff;
Fred Drakeef8ace32000-08-24 00:32:09 +0000805 dist >>= 8;
806 code[anchor+1] = dist;
807 dist >>= 8;
808 if (dist) {
809 com_error(c, PyExc_SystemError,
810 "com_backpatch: offset too large");
811 break;
812 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000813 if (!prev)
814 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000815 anchor -= prev;
816 }
817}
818
Guido van Rossuma9df32a1991-12-31 13:13:35 +0000819/* Handle literals and names uniformly */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000820
821static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000822com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000823{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000824 PyObject *w, *t, *np=NULL;
825 long n;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000826
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000827 t = Py_BuildValue("(OO)", v, v->ob_type);
828 if (t == NULL)
829 goto fail;
830 w = PyDict_GetItem(dict, t);
831 if (w != NULL) {
832 n = PyInt_AsLong(w);
833 } else {
834 n = PyList_Size(list);
835 np = PyInt_FromLong(n);
836 if (np == NULL)
837 goto fail;
838 if (PyList_Append(list, v) != 0)
839 goto fail;
840 if (PyDict_SetItem(dict, t, np) != 0)
841 goto fail;
842 Py_DECREF(np);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000843 }
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000844 Py_DECREF(t);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +0000845 return n;
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000846 fail:
847 Py_XDECREF(np);
848 Py_XDECREF(t);
849 c->c_errors++;
850 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000851}
852
853static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000854com_addconst(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000855{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000856 return com_add(c, c->c_consts, c->c_const_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000857}
858
859static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000860com_addname(struct compiling *c, PyObject *v)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000861{
Guido van Rossuma2ace6a2000-04-28 16:42:25 +0000862 return com_add(c, c->c_names, c->c_name_dict, v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000863}
864
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000865static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000866mangle(char *p, char *name, char *buffer, size_t maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000867{
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000868 /* Name mangling: __private becomes _classname__private.
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000869 This is independent from how the name is used. */
Guido van Rossum582acec2000-06-28 22:07:35 +0000870 size_t nlen, plen;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000871 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
872 return 0;
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000873 nlen = strlen(name);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000874 if (nlen+2 >= maxlen)
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000875 return 0; /* Don't mangle __extremely_long_names */
876 if (name[nlen-1] == '_' && name[nlen-2] == '_')
877 return 0; /* Don't mangle __whatever__ */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000878 /* Strip leading underscores from class name */
879 while (*p == '_')
880 p++;
881 if (*p == '\0')
882 return 0; /* Don't mangle if class is just underscores */
883 plen = strlen(p);
884 if (plen + nlen >= maxlen)
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000885 plen = maxlen-nlen-2; /* Truncate class name if too long */
886 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000887 buffer[0] = '_';
888 strncpy(buffer+1, p, plen);
Guido van Rossumd8a6d1c1996-08-24 07:54:27 +0000889 strcpy(buffer+1+plen, name);
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000890 return 1;
891}
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000892
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000893static void
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000894com_addop_name(struct compiling *c, int op, char *name)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000895{
Guido van Rossum79f25d91997-04-29 20:08:16 +0000896 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000897 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000898 char buffer[MANGLE_LEN];
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000899
900 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Guido van Rossum8ff077b1996-08-24 06:21:31 +0000901 name = buffer;
Guido van Rossum7b89b6a1997-01-18 08:02:57 +0000902 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000903 c->c_errors++;
904 i = 255;
905 }
906 else {
907 i = com_addname(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000908 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000909 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000910 com_addoparg(c, op, i);
911}
912
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000913#define NAME_LOCAL 0
914#define NAME_GLOBAL 1
915#define NAME_DEFAULT 2
916#define NAME_CLOSURE 3
917
918static int
919com_lookup_arg(PyObject *dict, PyObject *name)
920{
921 PyObject *v = PyDict_GetItem(dict, name);
922 if (v == NULL)
923 return -1;
924 else
925 return PyInt_AS_LONG(v);
926}
927
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000928static void
929com_addop_varname(struct compiling *c, int kind, char *name)
930{
931 PyObject *v;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000932 int i, reftype;
933 int scope = NAME_DEFAULT;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000934 int op = STOP_CODE;
935 char buffer[MANGLE_LEN];
936
Jeremy Hyltoneab156f2001-01-30 01:24:43 +0000937 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000938 name = buffer;
939 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
940 c->c_errors++;
941 i = 255;
942 goto done;
Guido van Rossumc5e96291991-12-10 13:53:51 +0000943 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000944
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000945 reftype = get_ref_type(c, name);
946 switch (reftype) {
947 case LOCAL:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000948 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000949 scope = NAME_LOCAL;
950 break;
951 case GLOBAL_EXPLICIT:
952 scope = NAME_GLOBAL;
953 break;
954 case GLOBAL_IMPLICIT:
955 if (c->c_flags & CO_OPTIMIZED)
956 scope = NAME_GLOBAL;
957 break;
958 case FREE:
959 case CELL:
960 scope = NAME_CLOSURE;
961 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000962 }
963
964 i = com_addname(c, v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000965 if (scope == NAME_LOCAL)
966 i = com_lookup_arg(c->c_locals, v);
967 else if (reftype == FREE)
968 i = com_lookup_arg(c->c_freevars, v);
969 else if (reftype == CELL)
970 i = com_lookup_arg(c->c_cellvars, v);
971 if (i == -1) {
972 c->c_errors++; /* XXX no exception set */
973 i = 255;
974 goto done;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000975 }
976 Py_DECREF(v);
977
978 switch (kind) {
979 case VAR_LOAD:
980 switch (scope) {
981 case NAME_LOCAL:
982 op = LOAD_FAST;
983 break;
984 case NAME_GLOBAL:
985 op = LOAD_GLOBAL;
986 break;
987 case NAME_DEFAULT:
988 op = LOAD_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000989 break;
990 case NAME_CLOSURE:
991 op = LOAD_DEREF;
992 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +0000993 }
994 break;
995 case VAR_STORE:
996 switch (scope) {
997 case NAME_LOCAL:
998 op = STORE_FAST;
999 break;
1000 case NAME_GLOBAL:
1001 op = STORE_GLOBAL;
1002 break;
1003 case NAME_DEFAULT:
1004 op = STORE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001005 break;
1006 case NAME_CLOSURE:
1007 op = STORE_DEREF;
1008 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001009 }
1010 break;
1011 case VAR_DELETE:
1012 switch (scope) {
1013 case NAME_LOCAL:
1014 op = DELETE_FAST;
1015 break;
1016 case NAME_GLOBAL:
1017 op = DELETE_GLOBAL;
1018 break;
1019 case NAME_DEFAULT:
1020 op = DELETE_NAME;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001021 break;
1022 case NAME_CLOSURE: {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00001023 char buf[500];
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001024 PyOS_snprintf(buf, sizeof(buf),
1025 DEL_CLOSURE_ERROR, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00001026 com_error(c, PyExc_SyntaxError, buf);
1027 i = 255;
1028 break;
1029 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001030 }
1031 break;
1032 }
1033done:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001034 com_addoparg(c, op, i);
1035}
1036
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001037static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001038com_addopname(struct compiling *c, int op, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001039{
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001040 char *name;
1041 char buffer[1000];
1042 /* XXX it is possible to write this code without the 1000
1043 chars on the total length of dotted names, I just can't be
1044 bothered right now */
1045 if (TYPE(n) == STAR)
1046 name = "*";
1047 else if (TYPE(n) == dotted_name) {
1048 char *p = buffer;
1049 int i;
1050 name = buffer;
1051 for (i = 0; i < NCH(n); i += 2) {
1052 char *s = STR(CHILD(n, i));
1053 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001054 com_error(c, PyExc_MemoryError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001055 "dotted_name too long");
Guido van Rossumd9dfaf51995-02-17 15:04:57 +00001056 name = NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001057 break;
1058 }
1059 if (p != buffer)
1060 *p++ = '.';
1061 strcpy(p, s);
1062 p = strchr(p, '\0');
1063 }
1064 }
1065 else {
1066 REQ(n, NAME);
1067 name = STR(n);
1068 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001069 com_addop_name(c, op, name);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001070}
1071
Guido van Rossum79f25d91997-04-29 20:08:16 +00001072static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001073parsenumber(struct compiling *co, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001074{
Guido van Rossumc5e96291991-12-10 13:53:51 +00001075 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001076 long x;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001077 double dx;
Guido van Rossum50564e81996-01-12 01:13:16 +00001078#ifndef WITHOUT_COMPLEX
Guido van Rossum530956d1996-07-21 02:27:43 +00001079 Py_complex c;
Guido van Rossum50564e81996-01-12 01:13:16 +00001080 int imflag;
1081#endif
1082
Guido van Rossum282914b1991-04-04 10:42:56 +00001083 errno = 0;
Guido van Rossumc5e96291991-12-10 13:53:51 +00001084 end = s + strlen(s) - 1;
Guido van Rossum50564e81996-01-12 01:13:16 +00001085#ifndef WITHOUT_COMPLEX
Guido van Rossum15ad9a61996-01-26 20:53:56 +00001086 imflag = *end == 'j' || *end == 'J';
Guido van Rossum50564e81996-01-12 01:13:16 +00001087#endif
Guido van Rossumf1aeab71992-03-27 17:28:26 +00001088 if (*end == 'l' || *end == 'L')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001089 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001090 if (s[0] == '0')
Guido van Rossum79f25d91997-04-29 20:08:16 +00001091 x = (long) PyOS_strtoul(s, &end, 0);
Guido van Rossumacbefef1992-01-19 16:33:51 +00001092 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001093 x = PyOS_strtol(s, &end, 0);
Guido van Rossum282914b1991-04-04 10:42:56 +00001094 if (*end == '\0') {
Jeremy Hylton71b6af92001-08-27 19:45:25 +00001095 if (errno != 0)
1096 return PyLong_FromString(s, (char **)0, 0);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001097 return PyInt_FromLong(x);
Guido van Rossum282914b1991-04-04 10:42:56 +00001098 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001099 /* XXX Huge floats may silently fail */
Guido van Rossum50564e81996-01-12 01:13:16 +00001100#ifndef WITHOUT_COMPLEX
1101 if (imflag) {
1102 c.real = 0.;
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001103 PyFPE_START_PROTECT("atof", return 0)
Guido van Rossum50564e81996-01-12 01:13:16 +00001104 c.imag = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001105 PyFPE_END_PROTECT(c)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001106 return PyComplex_FromCComplex(c);
Guido van Rossum50564e81996-01-12 01:13:16 +00001107 }
Guido van Rossumac1fc951997-10-08 15:23:55 +00001108 else
Guido van Rossum50564e81996-01-12 01:13:16 +00001109#endif
Guido van Rossumac1fc951997-10-08 15:23:55 +00001110 {
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001111 PyFPE_START_PROTECT("atof", return 0)
1112 dx = atof(s);
Guido van Rossum45b83911997-03-14 04:32:50 +00001113 PyFPE_END_PROTECT(dx)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001114 return PyFloat_FromDouble(dx);
Guido van Rossum0ae748d1997-02-14 22:58:07 +00001115 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001116}
1117
Guido van Rossum79f25d91997-04-29 20:08:16 +00001118static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001119parsestr(struct compiling *com, char *s)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001120{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001121 PyObject *v;
Guido van Rossum582acec2000-06-28 22:07:35 +00001122 size_t len;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001123 char *buf;
1124 char *p;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001125 char *end;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001126 int c;
Guido van Rossum5f5e8171997-04-06 03:41:40 +00001127 int first = *s;
1128 int quote = first;
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001129 int rawmode = 0;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001130#ifdef Py_USING_UNICODE
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001131 int unicode = 0;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001132#endif
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001133 if (isalpha(quote) || quote == '_') {
1134 if (quote == 'u' || quote == 'U') {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001135#ifdef Py_USING_UNICODE
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001136 quote = *++s;
1137 unicode = 1;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001138#else
1139 com_error(com, PyExc_SyntaxError,
1140 "Unicode literals not supported in this Python");
1141 return NULL;
1142#endif
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001143 }
1144 if (quote == 'r' || quote == 'R') {
1145 quote = *++s;
1146 rawmode = 1;
1147 }
1148 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001149 if (quote != '\'' && quote != '\"') {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001150 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001151 return NULL;
1152 }
1153 s++;
1154 len = strlen(s);
Guido van Rossum582acec2000-06-28 22:07:35 +00001155 if (len > INT_MAX) {
1156 PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
1157 return NULL;
1158 }
Guido van Rossum8054fad1993-10-26 15:19:44 +00001159 if (s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001160 PyErr_BadInternalCall();
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001161 return NULL;
1162 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001163 if (len >= 4 && s[0] == quote && s[1] == quote) {
1164 s += 2;
1165 len -= 2;
1166 if (s[--len] != quote || s[--len] != quote) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001167 PyErr_BadInternalCall();
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001168 return NULL;
1169 }
1170 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001171#ifdef Py_USING_UNICODE
Guido van Rossumfdc8bdb2000-05-01 17:54:56 +00001172 if (unicode || Py_UnicodeFlag) {
Guido van Rossum5aa88f02000-03-10 23:01:36 +00001173 if (rawmode)
1174 return PyUnicode_DecodeRawUnicodeEscape(
1175 s, len, NULL);
1176 else
1177 return PyUnicode_DecodeUnicodeEscape(
1178 s, len, NULL);
1179 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001180#endif
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001181 if (rawmode || strchr(s, '\\') == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001182 return PyString_FromStringAndSize(s, len);
1183 v = PyString_FromStringAndSize((char *)NULL, len);
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001184 if (v == NULL)
1185 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001186 p = buf = PyString_AsString(v);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001187 end = s + len;
1188 while (s < end) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001189 if (*s != '\\') {
1190 *p++ = *s++;
1191 continue;
1192 }
1193 s++;
1194 switch (*s++) {
1195 /* XXX This assumes ASCII! */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001196 case '\n': break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001197 case '\\': *p++ = '\\'; break;
1198 case '\'': *p++ = '\''; break;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001199 case '\"': *p++ = '\"'; break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001200 case 'b': *p++ = '\b'; break;
1201 case 'f': *p++ = '\014'; break; /* FF */
1202 case 't': *p++ = '\t'; break;
1203 case 'n': *p++ = '\n'; break;
1204 case 'r': *p++ = '\r'; break;
1205 case 'v': *p++ = '\013'; break; /* VT */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001206 case 'a': *p++ = '\007'; break; /* BEL, not classic C */
1207 case '0': case '1': case '2': case '3':
1208 case '4': case '5': case '6': case '7':
1209 c = s[-1] - '0';
1210 if ('0' <= *s && *s <= '7') {
1211 c = (c<<3) + *s++ - '0';
1212 if ('0' <= *s && *s <= '7')
1213 c = (c<<3) + *s++ - '0';
1214 }
1215 *p++ = c;
1216 break;
1217 case 'x':
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001218 if (isxdigit(Py_CHARMASK(s[0]))
1219 && isxdigit(Py_CHARMASK(s[1]))) {
Guido van Rossumed1100f1997-10-20 23:24:07 +00001220 unsigned int x = 0;
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001221 c = Py_CHARMASK(*s);
1222 s++;
1223 if (isdigit(c))
1224 x = c - '0';
1225 else if (islower(c))
1226 x = 10 + c - 'a';
1227 else
1228 x = 10 + c - 'A';
1229 x = x << 4;
1230 c = Py_CHARMASK(*s);
1231 s++;
1232 if (isdigit(c))
1233 x += c - '0';
1234 else if (islower(c))
1235 x += 10 + c - 'a';
1236 else
1237 x += 10 + c - 'A';
Guido van Rossumed1100f1997-10-20 23:24:07 +00001238 *p++ = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001239 break;
1240 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00001241 PyErr_SetString(PyExc_ValueError,
1242 "invalid \\x escape");
Fredrik Lundh1fa0b892000-09-02 20:11:27 +00001243 Py_DECREF(v);
1244 return NULL;
1245 default:
1246 *p++ = '\\';
1247 *p++ = s[-1];
1248 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001249 }
1250 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001251 _PyString_Resize(&v, (int)(p - buf));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001252 return v;
1253}
1254
Guido van Rossum79f25d91997-04-29 20:08:16 +00001255static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001256parsestrplus(struct compiling* c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001257{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001258 PyObject *v;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001259 int i;
1260 REQ(CHILD(n, 0), STRING);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001261 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001262 /* String literal concatenation */
Fred Drake4e998bc2000-04-13 14:10:44 +00001263 for (i = 1; i < NCH(n); i++) {
1264 PyObject *s;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001265 s = parsestr(c, STR(CHILD(n, i)));
Fred Drake4e998bc2000-04-13 14:10:44 +00001266 if (s == NULL)
1267 goto onError;
1268 if (PyString_Check(v) && PyString_Check(s)) {
1269 PyString_ConcatAndDel(&v, s);
1270 if (v == NULL)
1271 goto onError;
1272 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001273#ifdef Py_USING_UNICODE
Fred Drake4e998bc2000-04-13 14:10:44 +00001274 else {
1275 PyObject *temp;
1276 temp = PyUnicode_Concat(v, s);
1277 Py_DECREF(s);
1278 if (temp == NULL)
1279 goto onError;
1280 Py_DECREF(v);
1281 v = temp;
1282 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001283#endif
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001284 }
1285 }
1286 return v;
Fred Drake4e998bc2000-04-13 14:10:44 +00001287
1288 onError:
1289 Py_XDECREF(v);
1290 return NULL;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00001291}
1292
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001293static void
Skip Montanaro803d6e52000-08-12 18:09:51 +00001294com_list_for(struct compiling *c, node *n, node *e, char *t)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001295{
Skip Montanaro803d6e52000-08-12 18:09:51 +00001296 int anchor = 0;
1297 int save_begin = c->c_begin;
1298
1299 /* list_iter: for v in expr [list_iter] */
1300 com_node(c, CHILD(n, 3)); /* expr */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001301 com_addbyte(c, GET_ITER);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001302 c->c_begin = c->c_nexti;
1303 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001304 com_addfwref(c, FOR_ITER, &anchor);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001305 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00001306 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001307 c->c_loops++;
1308 com_list_iter(c, n, e, t);
1309 c->c_loops--;
1310 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
1311 c->c_begin = save_begin;
1312 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001313 com_pop(c, 1); /* FOR_ITER has popped this */
Skip Montanaro803d6e52000-08-12 18:09:51 +00001314}
1315
1316static void
1317com_list_if(struct compiling *c, node *n, node *e, char *t)
1318{
1319 int anchor = 0;
1320 int a = 0;
1321 /* list_iter: 'if' test [list_iter] */
1322 com_addoparg(c, SET_LINENO, n->n_lineno);
1323 com_node(c, CHILD(n, 1));
1324 com_addfwref(c, JUMP_IF_FALSE, &a);
1325 com_addbyte(c, POP_TOP);
1326 com_pop(c, 1);
1327 com_list_iter(c, n, e, t);
1328 com_addfwref(c, JUMP_FORWARD, &anchor);
1329 com_backpatch(c, a);
1330 /* We jump here with an extra entry which we now pop */
1331 com_addbyte(c, POP_TOP);
1332 com_backpatch(c, anchor);
1333}
1334
1335static void
1336com_list_iter(struct compiling *c,
1337 node *p, /* parent of list_iter node */
1338 node *e, /* element expression node */
1339 char *t /* name of result list temp local */)
1340{
1341 /* list_iter is the last child in a listmaker, list_for, or list_if */
1342 node *n = CHILD(p, NCH(p)-1);
1343 if (TYPE(n) == list_iter) {
1344 n = CHILD(n, 0);
1345 switch (TYPE(n)) {
1346 case list_for:
1347 com_list_for(c, n, e, t);
1348 break;
1349 case list_if:
1350 com_list_if(c, n, e, t);
1351 break;
1352 default:
1353 com_error(c, PyExc_SystemError,
1354 "invalid list_iter node type");
1355 }
1356 }
1357 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001358 com_addop_varname(c, VAR_LOAD, t);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001359 com_push(c, 1);
1360 com_node(c, e);
1361 com_addoparg(c, CALL_FUNCTION, 1);
1362 com_addbyte(c, POP_TOP);
1363 com_pop(c, 2);
1364 }
1365}
1366
1367static void
1368com_list_comprehension(struct compiling *c, node *n)
1369{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001370 /* listmaker: test list_for */
Barry Warsaw8f6d8682001-11-28 21:10:39 +00001371 char tmpname[30];
1372 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001373 com_addoparg(c, BUILD_LIST, 0);
1374 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
1375 com_push(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001376 com_addop_name(c, LOAD_ATTR, "append");
1377 com_addop_varname(c, VAR_STORE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001378 com_pop(c, 1);
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001379 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001380 com_addop_varname(c, VAR_DELETE, tmpname);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001381 --c->c_tmpname;
1382}
1383
1384static void
1385com_listmaker(struct compiling *c, node *n)
1386{
Skip Montanaro46dfa5f2000-08-22 02:43:07 +00001387 /* listmaker: test ( list_for | (',' test)* [','] ) */
1388 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
Skip Montanaro803d6e52000-08-12 18:09:51 +00001389 com_list_comprehension(c, n);
1390 else {
1391 int len = 0;
1392 int i;
1393 for (i = 0; i < NCH(n); i += 2, len++)
1394 com_node(c, CHILD(n, i));
1395 com_addoparg(c, BUILD_LIST, len);
1396 com_pop(c, len-1);
1397 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001398}
1399
1400static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001401com_dictmaker(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001402{
1403 int i;
1404 /* dictmaker: test ':' test (',' test ':' value)* [','] */
1405 for (i = 0; i+2 < NCH(n); i += 4) {
1406 /* We must arrange things just right for STORE_SUBSCR.
1407 It wants the stack to look like (value) (dict) (key) */
1408 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001409 com_push(c, 1);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001410 com_node(c, CHILD(n, i+2)); /* value */
1411 com_addbyte(c, ROT_TWO);
1412 com_node(c, CHILD(n, i)); /* key */
1413 com_addbyte(c, STORE_SUBSCR);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001414 com_pop(c, 3);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001415 }
1416}
1417
1418static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001419com_atom(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001420{
1421 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001422 PyObject *v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001423 int i;
1424 REQ(n, atom);
1425 ch = CHILD(n, 0);
1426 switch (TYPE(ch)) {
1427 case LPAR:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001428 if (TYPE(CHILD(n, 1)) == RPAR) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001429 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001430 com_push(c, 1);
1431 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001432 else
1433 com_node(c, CHILD(n, 1));
1434 break;
Skip Montanaro803d6e52000-08-12 18:09:51 +00001435 case LSQB: /* '[' [listmaker] ']' */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001436 if (TYPE(CHILD(n, 1)) == RSQB) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001437 com_addoparg(c, BUILD_LIST, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001438 com_push(c, 1);
1439 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001440 else
Skip Montanaro803d6e52000-08-12 18:09:51 +00001441 com_listmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001442 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001443 case LBRACE: /* '{' [dictmaker] '}' */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001444 com_addoparg(c, BUILD_MAP, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001445 com_push(c, 1);
Skip Montanaro803d6e52000-08-12 18:09:51 +00001446 if (TYPE(CHILD(n, 1)) == dictmaker)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00001447 com_dictmaker(c, CHILD(n, 1));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001448 break;
1449 case BACKQUOTE:
1450 com_node(c, CHILD(n, 1));
1451 com_addbyte(c, UNARY_CONVERT);
1452 break;
1453 case NUMBER:
Guido van Rossum452a9831996-09-17 14:32:04 +00001454 if ((v = parsenumber(c, STR(ch))) == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001455 i = 255;
1456 }
1457 else {
1458 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001459 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001460 }
1461 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001462 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001463 break;
1464 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001465 v = parsestrplus(c, n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00001466 if (v == NULL) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001467 c->c_errors++;
1468 i = 255;
1469 }
1470 else {
1471 i = com_addconst(c, v);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001472 Py_DECREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001473 }
1474 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001475 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001476 break;
1477 case NAME:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001478 com_addop_varname(c, VAR_LOAD, STR(ch));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001479 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001480 break;
1481 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001482 com_error(c, PyExc_SystemError,
1483 "com_atom: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001484 }
1485}
1486
1487static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001488com_slice(struct compiling *c, node *n, int op)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001489{
1490 if (NCH(n) == 1) {
1491 com_addbyte(c, op);
1492 }
1493 else if (NCH(n) == 2) {
1494 if (TYPE(CHILD(n, 0)) != COLON) {
1495 com_node(c, CHILD(n, 0));
1496 com_addbyte(c, op+1);
1497 }
1498 else {
1499 com_node(c, CHILD(n, 1));
1500 com_addbyte(c, op+2);
1501 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001502 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001503 }
1504 else {
1505 com_node(c, CHILD(n, 0));
1506 com_node(c, CHILD(n, 2));
1507 com_addbyte(c, op+3);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001508 com_pop(c, 2);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001509 }
1510}
1511
Guido van Rossum635abd21997-01-06 22:56:52 +00001512static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001513com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1514{
1515 if (NCH(n) == 1) {
1516 com_addbyte(c, DUP_TOP);
1517 com_push(c, 1);
1518 com_addbyte(c, SLICE);
1519 com_node(c, augn);
1520 com_addbyte(c, opcode);
1521 com_pop(c, 1);
1522 com_addbyte(c, ROT_TWO);
1523 com_addbyte(c, STORE_SLICE);
1524 com_pop(c, 2);
1525 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1526 com_node(c, CHILD(n, 0));
1527 com_addoparg(c, DUP_TOPX, 2);
1528 com_push(c, 2);
1529 com_addbyte(c, SLICE+1);
1530 com_pop(c, 1);
1531 com_node(c, augn);
1532 com_addbyte(c, opcode);
1533 com_pop(c, 1);
1534 com_addbyte(c, ROT_THREE);
1535 com_addbyte(c, STORE_SLICE+1);
1536 com_pop(c, 3);
1537 } else if (NCH(n) == 2) {
1538 com_node(c, CHILD(n, 1));
1539 com_addoparg(c, DUP_TOPX, 2);
1540 com_push(c, 2);
1541 com_addbyte(c, SLICE+2);
1542 com_pop(c, 1);
1543 com_node(c, augn);
1544 com_addbyte(c, opcode);
1545 com_pop(c, 1);
1546 com_addbyte(c, ROT_THREE);
1547 com_addbyte(c, STORE_SLICE+2);
1548 com_pop(c, 3);
1549 } else {
1550 com_node(c, CHILD(n, 0));
1551 com_node(c, CHILD(n, 2));
1552 com_addoparg(c, DUP_TOPX, 3);
1553 com_push(c, 3);
1554 com_addbyte(c, SLICE+3);
1555 com_pop(c, 2);
1556 com_node(c, augn);
1557 com_addbyte(c, opcode);
1558 com_pop(c, 1);
1559 com_addbyte(c, ROT_FOUR);
1560 com_addbyte(c, STORE_SLICE+3);
1561 com_pop(c, 4);
1562 }
1563}
1564
1565static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001566com_argument(struct compiling *c, node *n, PyObject **pkeywords)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001567{
1568 node *m;
Guido van Rossum8b993a91997-01-17 21:04:03 +00001569 REQ(n, argument); /* [test '='] test; really [keyword '='] test */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001570 if (NCH(n) == 1) {
Guido van Rossum635abd21997-01-06 22:56:52 +00001571 if (*pkeywords != NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001572 com_error(c, PyExc_SyntaxError,
Fred Drakedcf08e02000-08-15 15:49:44 +00001573 "non-keyword arg after keyword arg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001574 }
1575 else {
1576 com_node(c, CHILD(n, 0));
1577 }
Guido van Rossum635abd21997-01-06 22:56:52 +00001578 return;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001579 }
1580 m = n;
1581 do {
1582 m = CHILD(m, 0);
1583 } while (NCH(m) == 1);
1584 if (TYPE(m) != NAME) {
Tim Peters4e303782001-02-18 04:45:10 +00001585 /* f(lambda x: x[0] = 3) ends up getting parsed with
1586 * LHS test = lambda x: x[0], and RHS test = 3.
1587 * SF bug 132313 points out that complaining about a keyword
1588 * then is very confusing.
1589 */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001590 com_error(c, PyExc_SyntaxError,
Tim Peters4e303782001-02-18 04:45:10 +00001591 TYPE(m) == lambdef ?
1592 "lambda cannot contain assignment" :
1593 "keyword can't be an expression");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001594 }
1595 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001596 PyObject *v = PyString_InternFromString(STR(m));
Guido van Rossum635abd21997-01-06 22:56:52 +00001597 if (v != NULL && *pkeywords == NULL)
Guido van Rossum79f25d91997-04-29 20:08:16 +00001598 *pkeywords = PyDict_New();
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001599 if (v == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001600 c->c_errors++;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00001601 else if (*pkeywords == NULL) {
1602 c->c_errors++;
1603 Py_DECREF(v);
1604 } else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001605 if (PyDict_GetItem(*pkeywords, v) != NULL)
1606 com_error(c, PyExc_SyntaxError,
Guido van Rossum635abd21997-01-06 22:56:52 +00001607 "duplicate keyword argument");
1608 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00001609 if (PyDict_SetItem(*pkeywords, v, v) != 0)
Guido van Rossum635abd21997-01-06 22:56:52 +00001610 c->c_errors++;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001611 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001612 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00001613 Py_DECREF(v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001614 }
1615 }
1616 com_node(c, CHILD(n, 2));
Guido van Rossumf10570b1995-07-07 22:53:21 +00001617}
1618
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001619static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001620com_call_function(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001621{
1622 if (TYPE(n) == RPAR) {
Guido van Rossumf10570b1995-07-07 22:53:21 +00001623 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001624 }
1625 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001626 PyObject *keywords = NULL;
Guido van Rossum635abd21997-01-06 22:56:52 +00001627 int i, na, nk;
Guido van Rossumca906051998-12-10 16:56:22 +00001628 int lineno = n->n_lineno;
Jeremy Hylton76901512000-03-28 23:49:17 +00001629 int star_flag = 0;
1630 int starstar_flag = 0;
1631 int opcode;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001632 REQ(n, arglist);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001633 na = 0;
1634 nk = 0;
1635 for (i = 0; i < NCH(n); i += 2) {
Guido van Rossumca906051998-12-10 16:56:22 +00001636 node *ch = CHILD(n, i);
Jeremy Hylton76901512000-03-28 23:49:17 +00001637 if (TYPE(ch) == STAR ||
1638 TYPE(ch) == DOUBLESTAR)
1639 break;
Guido van Rossumca906051998-12-10 16:56:22 +00001640 if (ch->n_lineno != lineno) {
1641 lineno = ch->n_lineno;
1642 com_addoparg(c, SET_LINENO, lineno);
1643 }
1644 com_argument(c, ch, &keywords);
Guido van Rossum635abd21997-01-06 22:56:52 +00001645 if (keywords == NULL)
Guido van Rossumf10570b1995-07-07 22:53:21 +00001646 na++;
1647 else
1648 nk++;
1649 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00001650 Py_XDECREF(keywords);
Jeremy Hylton76901512000-03-28 23:49:17 +00001651 while (i < NCH(n)) {
1652 node *tok = CHILD(n, i);
1653 node *ch = CHILD(n, i+1);
1654 i += 3;
1655 switch (TYPE(tok)) {
1656 case STAR: star_flag = 1; break;
1657 case DOUBLESTAR: starstar_flag = 1; break;
1658 }
1659 com_node(c, ch);
1660 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00001661 if (na > 255 || nk > 255) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001662 com_error(c, PyExc_SyntaxError,
1663 "more than 255 arguments");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001664 }
Jeremy Hylton76901512000-03-28 23:49:17 +00001665 if (star_flag || starstar_flag)
Jeremy Hyltone4fb9582000-03-29 00:10:44 +00001666 opcode = CALL_FUNCTION_VAR - 1 +
Jeremy Hylton76901512000-03-28 23:49:17 +00001667 star_flag + (starstar_flag << 1);
1668 else
1669 opcode = CALL_FUNCTION;
1670 com_addoparg(c, opcode, na | (nk << 8));
1671 com_pop(c, na + 2*nk + star_flag + starstar_flag);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001672 }
1673}
1674
1675static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001676com_select_member(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001677{
1678 com_addopname(c, LOAD_ATTR, n);
1679}
1680
1681static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001682com_sliceobj(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001683{
1684 int i=0;
1685 int ns=2; /* number of slice arguments */
Guido van Rossum8861b741996-07-30 16:49:37 +00001686 node *ch;
1687
1688 /* first argument */
1689 if (TYPE(CHILD(n,i)) == COLON) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001690 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001691 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001692 i++;
1693 }
1694 else {
1695 com_node(c, CHILD(n,i));
1696 i++;
1697 REQ(CHILD(n,i),COLON);
1698 i++;
1699 }
1700 /* second argument */
1701 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
1702 com_node(c, CHILD(n,i));
1703 i++;
1704 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00001705 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001706 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001707 com_push(c, 1);
1708 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001709 /* remaining arguments */
1710 for (; i < NCH(n); i++) {
1711 ns++;
1712 ch=CHILD(n,i);
1713 REQ(ch, sliceop);
1714 if (NCH(ch) == 1) {
1715 /* right argument of ':' missing */
Guido van Rossum79f25d91997-04-29 20:08:16 +00001716 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001717 com_push(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001718 }
1719 else
1720 com_node(c, CHILD(ch,1));
1721 }
1722 com_addoparg(c, BUILD_SLICE, ns);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001723 com_pop(c, 1 + (ns == 3));
Guido van Rossum8861b741996-07-30 16:49:37 +00001724}
1725
1726static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001727com_subscript(struct compiling *c, node *n)
Guido van Rossum8861b741996-07-30 16:49:37 +00001728{
1729 node *ch;
1730 REQ(n, subscript);
1731 ch = CHILD(n,0);
1732 /* check for rubber index */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001733 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
Guido van Rossume449af71996-10-11 16:25:41 +00001734 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
Guido van Rossum8b993a91997-01-17 21:04:03 +00001735 com_push(c, 1);
1736 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001737 else {
1738 /* check for slice */
1739 if ((TYPE(ch) == COLON || NCH(n) > 1))
1740 com_sliceobj(c, n);
1741 else {
1742 REQ(ch, test);
1743 com_node(c, ch);
1744 }
1745 }
1746}
1747
1748static void
Thomas Wouters434d0822000-08-24 20:11:32 +00001749com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum8861b741996-07-30 16:49:37 +00001750{
1751 int i, op;
1752 REQ(n, subscriptlist);
1753 /* Check to make backward compatible slice behavior for '[i:j]' */
1754 if (NCH(n) == 1) {
1755 node *sub = CHILD(n, 0); /* subscript */
Thomas Wouterse8643c42000-08-05 21:37:50 +00001756 /* 'Basic' slice, should have exactly one colon. */
1757 if ((TYPE(CHILD(sub, 0)) == COLON
1758 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
1759 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
1760 {
Thomas Wouters434d0822000-08-24 20:11:32 +00001761 switch (assigning) {
1762 case OP_DELETE:
1763 op = DELETE_SLICE;
1764 break;
1765 case OP_ASSIGN:
1766 op = STORE_SLICE;
1767 break;
1768 case OP_APPLY:
Guido van Rossum8861b741996-07-30 16:49:37 +00001769 op = SLICE;
Thomas Wouters434d0822000-08-24 20:11:32 +00001770 break;
1771 default:
1772 com_augassign_slice(c, sub, assigning, augn);
1773 return;
1774 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001775 com_slice(c, sub, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001776 if (op == STORE_SLICE)
1777 com_pop(c, 2);
1778 else if (op == DELETE_SLICE)
1779 com_pop(c, 1);
Guido van Rossum8861b741996-07-30 16:49:37 +00001780 return;
1781 }
1782 }
1783 /* Else normal subscriptlist. Compile each subscript. */
1784 for (i = 0; i < NCH(n); i += 2)
1785 com_subscript(c, CHILD(n, i));
1786 /* Put multiple subscripts into a tuple */
Guido van Rossum8b993a91997-01-17 21:04:03 +00001787 if (NCH(n) > 1) {
1788 i = (NCH(n)+1) / 2;
1789 com_addoparg(c, BUILD_TUPLE, i);
1790 com_pop(c, i-1);
1791 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001792 switch (assigning) {
1793 case OP_DELETE:
Guido van Rossum8b993a91997-01-17 21:04:03 +00001794 op = DELETE_SUBSCR;
1795 i = 2;
Thomas Wouters434d0822000-08-24 20:11:32 +00001796 break;
1797 default:
1798 case OP_ASSIGN:
1799 op = STORE_SUBSCR;
1800 i = 3;
1801 break;
1802 case OP_APPLY:
1803 op = BINARY_SUBSCR;
1804 i = 1;
1805 break;
1806 }
1807 if (assigning > OP_APPLY) {
1808 com_addoparg(c, DUP_TOPX, 2);
1809 com_push(c, 2);
1810 com_addbyte(c, BINARY_SUBSCR);
1811 com_pop(c, 1);
1812 com_node(c, augn);
1813 com_addbyte(c, assigning);
1814 com_pop(c, 1);
1815 com_addbyte(c, ROT_THREE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001816 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001817 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001818 com_pop(c, i);
Guido van Rossum8861b741996-07-30 16:49:37 +00001819}
1820
1821static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001822com_apply_trailer(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001823{
1824 REQ(n, trailer);
1825 switch (TYPE(CHILD(n, 0))) {
1826 case LPAR:
1827 com_call_function(c, CHILD(n, 1));
1828 break;
1829 case DOT:
1830 com_select_member(c, CHILD(n, 1));
1831 break;
1832 case LSQB:
Thomas Wouters434d0822000-08-24 20:11:32 +00001833 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001834 break;
1835 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00001836 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00001837 "com_apply_trailer: unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001838 }
1839}
1840
1841static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001842com_power(struct compiling *c, node *n)
Guido van Rossum50564e81996-01-12 01:13:16 +00001843{
1844 int i;
1845 REQ(n, power);
1846 com_atom(c, CHILD(n, 0));
1847 for (i = 1; i < NCH(n); i++) {
1848 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
1849 com_factor(c, CHILD(n, i+1));
1850 com_addbyte(c, BINARY_POWER);
Guido van Rossum8b993a91997-01-17 21:04:03 +00001851 com_pop(c, 1);
Guido van Rossum50564e81996-01-12 01:13:16 +00001852 break;
1853 }
1854 else
1855 com_apply_trailer(c, CHILD(n, i));
1856 }
1857}
1858
1859static void
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001860com_invert_constant(struct compiling *c, node *n)
1861{
1862 /* Compute the inverse of int and longs and use them directly,
1863 but be prepared to generate code for all other
1864 possibilities (invalid numbers, floats, complex).
1865 */
1866 PyObject *num, *inv = NULL;
1867 int i;
1868
1869 REQ(n, NUMBER);
1870 num = parsenumber(c, STR(n));
1871 if (num == NULL)
1872 i = 255;
1873 else {
1874 inv = PyNumber_Invert(num);
1875 if (inv == NULL) {
1876 PyErr_Clear();
1877 i = com_addconst(c, num);
1878 } else {
1879 i = com_addconst(c, inv);
1880 Py_DECREF(inv);
1881 }
1882 Py_DECREF(num);
1883 }
1884 com_addoparg(c, LOAD_CONST, i);
1885 com_push(c, 1);
1886 if (num != NULL && inv == NULL)
1887 com_addbyte(c, UNARY_INVERT);
1888}
1889
Tim Peters51e26512001-09-07 08:45:55 +00001890static int
1891is_float_zero(const char *p)
1892{
1893 int found_radix_point = 0;
1894 int ch;
1895 while ((ch = Py_CHARMASK(*p++)) != '\0') {
1896 switch (ch) {
1897 case '0':
1898 /* no reason to believe it's not 0 -- continue */
1899 break;
1900
1901 case 'e': case 'E': case 'j': case 'J':
1902 /* If this was a hex constant, we already would have
1903 returned 0 due to the 'x' or 'X', so 'e' or 'E'
1904 must be an exponent marker, and we haven't yet
1905 seen a non-zero digit, and it doesn't matter what
1906 the exponent is then. For 'j' or 'J' similarly,
1907 except that this is an imaginary 0 then. */
1908 return 1;
1909
1910 case '.':
1911 found_radix_point = 1;
1912 break;
1913
1914 default:
1915 return 0;
1916 }
1917 }
1918 return found_radix_point;
1919}
1920
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001921static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001922com_factor(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001923{
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001924 int childtype = TYPE(CHILD(n, 0));
Tim Peters51e26512001-09-07 08:45:55 +00001925 node *pfactor, *ppower, *patom, *pnum;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001926 REQ(n, factor);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001927 /* If the unary +, -, or ~ operator is applied to a constant,
Tim Peters51e26512001-09-07 08:45:55 +00001928 don't generate a UNARY_xxx opcode. Just store the
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001929 approriate value as a constant. If the value is negative,
1930 extend the string containing the constant and insert a
Tim Peters51e26512001-09-07 08:45:55 +00001931 negative in the 0th position -- unless we're doing unary minus
1932 of a floating zero! In that case the sign is significant, but
1933 the const dict can't distinguish +0.0 from -0.0.
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001934 */
1935 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
Fred Drake14ef2442001-08-30 18:53:25 +00001936 && NCH(n) == 2
Tim Peters51e26512001-09-07 08:45:55 +00001937 && TYPE((pfactor = CHILD(n, 1))) == factor
1938 && NCH(pfactor) == 1
1939 && TYPE((ppower = CHILD(pfactor, 0))) == power
1940 && NCH(ppower) == 1
1941 && TYPE((patom = CHILD(ppower, 0))) == atom
1942 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
1943 && !(childtype == MINUS && is_float_zero(STR(pnum)))) {
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001944 if (childtype == TILDE) {
Tim Peters51e26512001-09-07 08:45:55 +00001945 com_invert_constant(c, pnum);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001946 return;
1947 }
1948 if (childtype == MINUS) {
Tim Peters51e26512001-09-07 08:45:55 +00001949 char *s = malloc(strlen(STR(pnum)) + 2);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001950 if (s == NULL) {
1951 com_error(c, PyExc_MemoryError, "");
1952 com_addbyte(c, 255);
1953 return;
1954 }
1955 s[0] = '-';
Tim Peters51e26512001-09-07 08:45:55 +00001956 strcpy(s + 1, STR(pnum));
1957 free(STR(pnum));
1958 STR(pnum) = s;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001959 }
Tim Peters51e26512001-09-07 08:45:55 +00001960 com_atom(c, patom);
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001961 }
1962 else if (childtype == PLUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001963 com_factor(c, CHILD(n, 1));
1964 com_addbyte(c, UNARY_POSITIVE);
1965 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001966 else if (childtype == MINUS) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001967 com_factor(c, CHILD(n, 1));
1968 com_addbyte(c, UNARY_NEGATIVE);
1969 }
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00001970 else if (childtype == TILDE) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00001971 com_factor(c, CHILD(n, 1));
1972 com_addbyte(c, UNARY_INVERT);
1973 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001974 else {
Guido van Rossum50564e81996-01-12 01:13:16 +00001975 com_power(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001976 }
1977}
1978
1979static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00001980com_term(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001981{
1982 int i;
1983 int op;
1984 REQ(n, term);
1985 com_factor(c, CHILD(n, 0));
1986 for (i = 2; i < NCH(n); i += 2) {
1987 com_factor(c, CHILD(n, i));
1988 switch (TYPE(CHILD(n, i-1))) {
1989 case STAR:
1990 op = BINARY_MULTIPLY;
1991 break;
1992 case SLASH:
Guido van Rossum4668b002001-08-08 05:00:18 +00001993 if (c->c_flags & CO_FUTURE_DIVISION)
1994 op = BINARY_TRUE_DIVIDE;
1995 else
1996 op = BINARY_DIVIDE;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001997 break;
1998 case PERCENT:
1999 op = BINARY_MODULO;
2000 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002001 case DOUBLESLASH:
2002 op = BINARY_FLOOR_DIVIDE;
2003 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002004 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002005 com_error(c, PyExc_SystemError,
Guido van Rossum4668b002001-08-08 05:00:18 +00002006 "com_term: operator not *, /, // or %");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002007 op = 255;
2008 }
2009 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002010 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002011 }
2012}
2013
2014static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002015com_arith_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002016{
2017 int i;
2018 int op;
2019 REQ(n, arith_expr);
2020 com_term(c, CHILD(n, 0));
2021 for (i = 2; i < NCH(n); i += 2) {
2022 com_term(c, CHILD(n, i));
2023 switch (TYPE(CHILD(n, i-1))) {
2024 case PLUS:
2025 op = BINARY_ADD;
2026 break;
2027 case MINUS:
2028 op = BINARY_SUBTRACT;
2029 break;
2030 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002031 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002032 "com_arith_expr: operator not + or -");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002033 op = 255;
2034 }
2035 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002036 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002037 }
2038}
2039
2040static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002041com_shift_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002042{
2043 int i;
2044 int op;
2045 REQ(n, shift_expr);
2046 com_arith_expr(c, CHILD(n, 0));
2047 for (i = 2; i < NCH(n); i += 2) {
2048 com_arith_expr(c, CHILD(n, i));
2049 switch (TYPE(CHILD(n, i-1))) {
2050 case LEFTSHIFT:
2051 op = BINARY_LSHIFT;
2052 break;
2053 case RIGHTSHIFT:
2054 op = BINARY_RSHIFT;
2055 break;
2056 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002057 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002058 "com_shift_expr: operator not << or >>");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002059 op = 255;
2060 }
2061 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002062 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002063 }
2064}
2065
2066static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002067com_and_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002068{
2069 int i;
2070 int op;
2071 REQ(n, and_expr);
2072 com_shift_expr(c, CHILD(n, 0));
2073 for (i = 2; i < NCH(n); i += 2) {
2074 com_shift_expr(c, CHILD(n, i));
2075 if (TYPE(CHILD(n, i-1)) == AMPER) {
2076 op = BINARY_AND;
2077 }
2078 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002079 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002080 "com_and_expr: operator not &");
Guido van Rossum7928cd71991-10-24 14:59:31 +00002081 op = 255;
2082 }
2083 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002084 com_pop(c, 1);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002085 }
2086}
2087
2088static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002089com_xor_expr(struct compiling *c, node *n)
Guido van Rossum7928cd71991-10-24 14:59:31 +00002090{
2091 int i;
2092 int op;
2093 REQ(n, xor_expr);
2094 com_and_expr(c, CHILD(n, 0));
2095 for (i = 2; i < NCH(n); i += 2) {
2096 com_and_expr(c, CHILD(n, i));
2097 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
2098 op = BINARY_XOR;
2099 }
2100 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002101 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002102 "com_xor_expr: operator not ^");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002103 op = 255;
2104 }
2105 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002106 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002107 }
2108}
2109
2110static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002111com_expr(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002112{
2113 int i;
2114 int op;
2115 REQ(n, expr);
Guido van Rossum7928cd71991-10-24 14:59:31 +00002116 com_xor_expr(c, CHILD(n, 0));
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002117 for (i = 2; i < NCH(n); i += 2) {
Guido van Rossum7928cd71991-10-24 14:59:31 +00002118 com_xor_expr(c, CHILD(n, i));
2119 if (TYPE(CHILD(n, i-1)) == VBAR) {
2120 op = BINARY_OR;
2121 }
2122 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002123 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002124 "com_expr: expr operator not |");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002125 op = 255;
2126 }
2127 com_addbyte(c, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002128 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002129 }
2130}
2131
2132static enum cmp_op
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002133cmp_type(node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002134{
2135 REQ(n, comp_op);
Guido van Rossum01cfd441991-10-20 20:12:38 +00002136 /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002137 | 'in' | 'not' 'in' | 'is' | 'is' not' */
2138 if (NCH(n) == 1) {
2139 n = CHILD(n, 0);
2140 switch (TYPE(n)) {
2141 case LESS: return LT;
2142 case GREATER: return GT;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002143 case EQEQUAL: /* == */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002144 case EQUAL: return EQ;
Guido van Rossum01cfd441991-10-20 20:12:38 +00002145 case LESSEQUAL: return LE;
2146 case GREATEREQUAL: return GE;
2147 case NOTEQUAL: return NE; /* <> or != */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002148 case NAME: if (strcmp(STR(n), "in") == 0) return IN;
2149 if (strcmp(STR(n), "is") == 0) return IS;
2150 }
2151 }
2152 else if (NCH(n) == 2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002153 switch (TYPE(CHILD(n, 0))) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002154 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
2155 return NOT_IN;
2156 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
2157 return IS_NOT;
2158 }
2159 }
2160 return BAD;
2161}
2162
2163static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002164com_comparison(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002165{
2166 int i;
2167 enum cmp_op op;
2168 int anchor;
2169 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
2170 com_expr(c, CHILD(n, 0));
2171 if (NCH(n) == 1)
2172 return;
2173
2174 /****************************************************************
2175 The following code is generated for all but the last
2176 comparison in a chain:
2177
2178 label: on stack: opcode: jump to:
2179
2180 a <code to load b>
2181 a, b DUP_TOP
2182 a, b, b ROT_THREE
2183 b, a, b COMPARE_OP
2184 b, 0-or-1 JUMP_IF_FALSE L1
2185 b, 1 POP_TOP
2186 b
2187
2188 We are now ready to repeat this sequence for the next
2189 comparison in the chain.
2190
2191 For the last we generate:
2192
2193 b <code to load c>
2194 b, c COMPARE_OP
2195 0-or-1
2196
2197 If there were any jumps to L1 (i.e., there was more than one
2198 comparison), we generate:
2199
2200 0-or-1 JUMP_FORWARD L2
2201 L1: b, 0 ROT_TWO
2202 0, b POP_TOP
2203 0
Guido van Rossum8b993a91997-01-17 21:04:03 +00002204 L2: 0-or-1
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002205 ****************************************************************/
2206
2207 anchor = 0;
2208
2209 for (i = 2; i < NCH(n); i += 2) {
2210 com_expr(c, CHILD(n, i));
2211 if (i+2 < NCH(n)) {
2212 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002213 com_push(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002214 com_addbyte(c, ROT_THREE);
2215 }
2216 op = cmp_type(CHILD(n, i-1));
2217 if (op == BAD) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002218 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002219 "com_comparison: unknown comparison op");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002220 }
2221 com_addoparg(c, COMPARE_OP, op);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002222 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002223 if (i+2 < NCH(n)) {
2224 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2225 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002226 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002227 }
2228 }
2229
2230 if (anchor) {
2231 int anchor2 = 0;
2232 com_addfwref(c, JUMP_FORWARD, &anchor2);
2233 com_backpatch(c, anchor);
2234 com_addbyte(c, ROT_TWO);
2235 com_addbyte(c, POP_TOP);
2236 com_backpatch(c, anchor2);
2237 }
2238}
2239
2240static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002241com_not_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002242{
2243 REQ(n, not_test); /* 'not' not_test | comparison */
2244 if (NCH(n) == 1) {
2245 com_comparison(c, CHILD(n, 0));
2246 }
2247 else {
2248 com_not_test(c, CHILD(n, 1));
2249 com_addbyte(c, UNARY_NOT);
2250 }
2251}
2252
2253static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002254com_and_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002255{
2256 int i;
2257 int anchor;
2258 REQ(n, and_test); /* not_test ('and' not_test)* */
2259 anchor = 0;
2260 i = 0;
2261 for (;;) {
2262 com_not_test(c, CHILD(n, i));
2263 if ((i += 2) >= NCH(n))
2264 break;
2265 com_addfwref(c, JUMP_IF_FALSE, &anchor);
2266 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002267 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002268 }
2269 if (anchor)
2270 com_backpatch(c, anchor);
2271}
2272
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002273static int
2274com_make_closure(struct compiling *c, PyCodeObject *co)
2275{
Jeremy Hylton733c8932001-12-13 19:51:56 +00002276 int i, free = PyCode_GetNumFree(co);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002277 if (free == 0)
2278 return 0;
2279 for (i = 0; i < free; ++i) {
2280 /* Bypass com_addop_varname because it will generate
2281 LOAD_DEREF but LOAD_CLOSURE is needed.
2282 */
2283 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
2284 int arg, reftype;
2285
2286 /* Special case: If a class contains a method with a
2287 free variable that has the same name as a method,
2288 the name will be considered free *and* local in the
2289 class. It should be handled by the closure, as
2290 well as by the normal name loookup logic.
2291 */
2292 reftype = get_ref_type(c, PyString_AS_STRING(name));
2293 if (reftype == CELL)
2294 arg = com_lookup_arg(c->c_cellvars, name);
2295 else /* (reftype == FREE) */
2296 arg = com_lookup_arg(c->c_freevars, name);
2297 if (arg == -1) {
Jeremy Hylton78891072001-03-01 06:09:34 +00002298 fprintf(stderr, "lookup %s in %s %d %d\n"
2299 "freevars of %s: %s\n",
2300 PyObject_REPR(name),
2301 c->c_name,
2302 reftype, arg,
2303 PyString_AS_STRING(co->co_name),
2304 PyObject_REPR(co->co_freevars));
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002305 Py_FatalError("com_make_closure()");
2306 }
2307 com_addoparg(c, LOAD_CLOSURE, arg);
2308
2309 }
2310 com_push(c, free);
2311 return 1;
2312}
2313
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002314static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002315com_test(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002316{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002317 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
Guido van Rossum57531fe1993-11-30 14:57:42 +00002318 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002319 PyCodeObject *co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002320 int i, closure;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002321 int ndefs = com_argdefs(c, CHILD(n, 0));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00002322 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
2323 n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002324 co = icompile(CHILD(n, 0), c);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002325 if (co == NULL) {
2326 c->c_errors++;
2327 return;
2328 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002329 symtable_exit_scope(c->c_symtable);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002330 i = com_addconst(c, (PyObject *)co);
2331 closure = com_make_closure(c, co);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002332 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002333 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002334 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002335 com_addoparg(c, MAKE_CLOSURE, ndefs);
Jeremy Hylton733c8932001-12-13 19:51:56 +00002336 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002337 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002338 com_addoparg(c, MAKE_FUNCTION, ndefs);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00002339 Py_DECREF(co);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002340 com_pop(c, ndefs);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002341 }
Guido van Rossum57531fe1993-11-30 14:57:42 +00002342 else {
2343 int anchor = 0;
2344 int i = 0;
2345 for (;;) {
2346 com_and_test(c, CHILD(n, i));
2347 if ((i += 2) >= NCH(n))
2348 break;
2349 com_addfwref(c, JUMP_IF_TRUE, &anchor);
2350 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002351 com_pop(c, 1);
Guido van Rossum57531fe1993-11-30 14:57:42 +00002352 }
2353 if (anchor)
2354 com_backpatch(c, anchor);
2355 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002356}
2357
2358static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002359com_list(struct compiling *c, node *n, int toplevel)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002360{
2361 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
Guido van Rossum288a60f1991-12-16 13:05:10 +00002362 if (NCH(n) == 1 && !toplevel) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002363 com_node(c, CHILD(n, 0));
2364 }
2365 else {
2366 int i;
2367 int len;
2368 len = (NCH(n) + 1) / 2;
2369 for (i = 0; i < NCH(n); i += 2)
2370 com_node(c, CHILD(n, i));
2371 com_addoparg(c, BUILD_TUPLE, len);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002372 com_pop(c, len-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002373 }
2374}
2375
2376
2377/* Begin of assignment compilation */
2378
Thomas Wouters434d0822000-08-24 20:11:32 +00002379
2380static void
2381com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
2382{
2383 com_addbyte(c, DUP_TOP);
2384 com_push(c, 1);
2385 com_addopname(c, LOAD_ATTR, n);
Thomas Wouters434d0822000-08-24 20:11:32 +00002386 com_node(c, augn);
2387 com_addbyte(c, opcode);
2388 com_pop(c, 1);
2389 com_addbyte(c, ROT_TWO);
2390 com_addopname(c, STORE_ATTR, n);
2391 com_pop(c, 2);
2392}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002393
2394static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002395com_assign_attr(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002396{
2397 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002398 com_pop(c, assigning ? 2 : 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002399}
2400
2401static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002402com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002403{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002404 REQ(n, trailer);
2405 switch (TYPE(CHILD(n, 0))) {
2406 case LPAR: /* '(' [exprlist] ')' */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002407 com_error(c, PyExc_SyntaxError,
2408 "can't assign to function call");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002409 break;
2410 case DOT: /* '.' NAME */
Thomas Wouters434d0822000-08-24 20:11:32 +00002411 if (assigning > OP_APPLY)
2412 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
2413 else
2414 com_assign_attr(c, CHILD(n, 1), assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002415 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002416 case LSQB: /* '[' subscriptlist ']' */
Thomas Wouters434d0822000-08-24 20:11:32 +00002417 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002418 break;
2419 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002420 com_error(c, PyExc_SystemError, "unknown trailer type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002421 }
2422}
2423
2424static void
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002425com_assign_sequence(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002426{
2427 int i;
Thomas Woutersb59290f2000-08-25 05:41:11 +00002428 if (TYPE(n) != testlist && TYPE(n) != listmaker)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002429 REQ(n, exprlist);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002430 if (assigning) {
2431 i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002432 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002433 com_push(c, i-1);
2434 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002435 for (i = 0; i < NCH(n); i += 2)
Thomas Wouters434d0822000-08-24 20:11:32 +00002436 com_assign(c, CHILD(n, i), assigning, NULL);
2437}
2438
2439static void
2440com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
2441{
2442 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002443 com_addop_varname(c, VAR_LOAD, STR(n));
Thomas Wouters434d0822000-08-24 20:11:32 +00002444 com_push(c, 1);
2445 com_node(c, augn);
2446 com_addbyte(c, opcode);
2447 com_pop(c, 1);
2448 com_assign_name(c, n, OP_ASSIGN);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002449}
2450
2451static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002452com_assign_name(struct compiling *c, node *n, int assigning)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002453{
2454 REQ(n, NAME);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002455 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002456 if (assigning)
2457 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002458}
2459
2460static void
Thomas Wouters434d0822000-08-24 20:11:32 +00002461com_assign(struct compiling *c, node *n, int assigning, node *augn)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002462{
2463 /* Loop to avoid trivial recursion */
2464 for (;;) {
2465 switch (TYPE(n)) {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002466
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002467 case exprlist:
2468 case testlist:
2469 if (NCH(n) > 1) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002470 if (assigning > OP_APPLY) {
2471 com_error(c, PyExc_SyntaxError,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00002472 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002473 return;
2474 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002475 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002476 return;
2477 }
2478 n = CHILD(n, 0);
2479 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002480
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002481 case test:
2482 case and_test:
2483 case not_test:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002484 case comparison:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002485 case expr:
Guido van Rossum7928cd71991-10-24 14:59:31 +00002486 case xor_expr:
2487 case and_expr:
2488 case shift_expr:
2489 case arith_expr:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002490 case term:
Guido van Rossum50564e81996-01-12 01:13:16 +00002491 case factor:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002492 if (NCH(n) > 1) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002493 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002494 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002495 return;
2496 }
2497 n = CHILD(n, 0);
2498 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002499
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00002500 case power: /* atom trailer* ('**' power)*
2501 ('+'|'-'|'~') factor | atom trailer* */
Guido van Rossum50564e81996-01-12 01:13:16 +00002502 if (TYPE(CHILD(n, 0)) != atom) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002503 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002504 "can't assign to operator");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002505 return;
2506 }
Guido van Rossum50564e81996-01-12 01:13:16 +00002507 if (NCH(n) > 1) { /* trailer or exponent present */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002508 int i;
2509 com_node(c, CHILD(n, 0));
2510 for (i = 1; i+1 < NCH(n); i++) {
Guido van Rossum50564e81996-01-12 01:13:16 +00002511 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002512 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002513 "can't assign to operator");
Guido van Rossum50564e81996-01-12 01:13:16 +00002514 return;
2515 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002516 com_apply_trailer(c, CHILD(n, i));
2517 } /* NB i is still alive */
2518 com_assign_trailer(c,
Thomas Wouters434d0822000-08-24 20:11:32 +00002519 CHILD(n, i), assigning, augn);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002520 return;
2521 }
2522 n = CHILD(n, 0);
2523 break;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002524
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002525 case atom:
2526 switch (TYPE(CHILD(n, 0))) {
2527 case LPAR:
2528 n = CHILD(n, 1);
2529 if (TYPE(n) == RPAR) {
2530 /* XXX Should allow () = () ??? */
Guido van Rossum79f25d91997-04-29 20:08:16 +00002531 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002532 "can't assign to ()");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002533 return;
2534 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002535 if (assigning > OP_APPLY) {
2536 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002537 "augmented assign to tuple not possible");
Thomas Wouters434d0822000-08-24 20:11:32 +00002538 return;
2539 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002540 break;
2541 case LSQB:
2542 n = CHILD(n, 1);
2543 if (TYPE(n) == RSQB) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002544 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002545 "can't assign to []");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002546 return;
2547 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002548 if (assigning > OP_APPLY) {
2549 com_error(c, PyExc_SyntaxError,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002550 "augmented assign to list not possible");
2551 return;
2552 }
2553 if (NCH(n) > 1
2554 && TYPE(CHILD(n, 1)) == list_for) {
2555 com_error(c, PyExc_SyntaxError,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002556 "can't assign to list comprehension");
Thomas Wouters434d0822000-08-24 20:11:32 +00002557 return;
2558 }
Thomas Wouters0be5aab2000-08-11 22:15:52 +00002559 com_assign_sequence(c, n, assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002560 return;
2561 case NAME:
Thomas Wouters434d0822000-08-24 20:11:32 +00002562 if (assigning > OP_APPLY)
2563 com_augassign_name(c, CHILD(n, 0),
2564 assigning, augn);
2565 else
2566 com_assign_name(c, CHILD(n, 0),
2567 assigning);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002568 return;
2569 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002570 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00002571 "can't assign to literal");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002572 return;
2573 }
2574 break;
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002575
2576 case lambdef:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002577 com_error(c, PyExc_SyntaxError,
2578 "can't assign to lambda");
Guido van Rossum15cc9a01996-08-08 18:51:04 +00002579 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002580
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002581 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00002582 com_error(c, PyExc_SystemError,
2583 "com_assign: bad node");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002584 return;
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002585
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002586 }
2587 }
2588}
Guido van Rossum7c531111997-03-11 18:42:21 +00002589
Thomas Wouters434d0822000-08-24 20:11:32 +00002590static void
2591com_augassign(struct compiling *c, node *n)
2592{
2593 int opcode;
2594
2595 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
2596 case '+': opcode = INPLACE_ADD; break;
2597 case '-': opcode = INPLACE_SUBTRACT; break;
Guido van Rossum4668b002001-08-08 05:00:18 +00002598 case '/':
2599 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
2600 opcode = INPLACE_FLOOR_DIVIDE;
2601 else if (c->c_flags & CO_FUTURE_DIVISION)
2602 opcode = INPLACE_TRUE_DIVIDE;
2603 else
2604 opcode = INPLACE_DIVIDE;
2605 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00002606 case '%': opcode = INPLACE_MODULO; break;
2607 case '<': opcode = INPLACE_LSHIFT; break;
2608 case '>': opcode = INPLACE_RSHIFT; break;
2609 case '&': opcode = INPLACE_AND; break;
2610 case '^': opcode = INPLACE_XOR; break;
2611 case '|': opcode = INPLACE_OR; break;
2612 case '*':
2613 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
2614 opcode = INPLACE_POWER;
2615 else
2616 opcode = INPLACE_MULTIPLY;
2617 break;
2618 default:
2619 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
2620 return;
2621 }
2622 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
2623}
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002624
2625static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002626com_expr_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002627{
Thomas Wouters434d0822000-08-24 20:11:32 +00002628 REQ(n, expr_stmt);
2629 /* testlist (('=' testlist)* | augassign testlist) */
Guido van Rossum8b993a91997-01-17 21:04:03 +00002630 /* Forget it if we have just a doc string here */
Guido van Rossum5f5e8171997-04-06 03:41:40 +00002631 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
Guido van Rossum8b993a91997-01-17 21:04:03 +00002632 return;
Thomas Wouters434d0822000-08-24 20:11:32 +00002633 if (NCH(n) == 1) {
2634 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00002635 if (c->c_interactive)
2636 com_addbyte(c, PRINT_EXPR);
2637 else
2638 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002639 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002640 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002641 else if (TYPE(CHILD(n,1)) == augassign)
2642 com_augassign(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002643 else {
2644 int i;
Thomas Wouters434d0822000-08-24 20:11:32 +00002645 com_node(c, CHILD(n, NCH(n)-1));
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002646 for (i = 0; i < NCH(n)-2; i+=2) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00002647 if (i+2 < NCH(n)-2) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002648 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002649 com_push(c, 1);
2650 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002651 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002652 }
2653 }
2654}
2655
2656static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002657com_assert_stmt(struct compiling *c, node *n)
Guido van Rossum228d7f31997-04-02 05:24:36 +00002658{
2659 int a = 0, b = 0;
2660 int i;
2661 REQ(n, assert_stmt); /* 'assert' test [',' test] */
2662 /* Generate code like for
2663
2664 if __debug__:
2665 if not <test>:
2666 raise AssertionError [, <message>]
2667
2668 where <message> is the second test, if present.
2669 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002670
Guido van Rossum228d7f31997-04-02 05:24:36 +00002671 if (Py_OptimizeFlag)
2672 return;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002673 com_addop_name(c, LOAD_GLOBAL, "__debug__");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002674 com_push(c, 1);
2675 com_addfwref(c, JUMP_IF_FALSE, &a);
2676 com_addbyte(c, POP_TOP);
2677 com_pop(c, 1);
2678 com_node(c, CHILD(n, 1));
2679 com_addfwref(c, JUMP_IF_TRUE, &b);
2680 com_addbyte(c, POP_TOP);
2681 com_pop(c, 1);
2682 /* Raise that exception! */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002683 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
Guido van Rossum228d7f31997-04-02 05:24:36 +00002684 com_push(c, 1);
2685 i = NCH(n)/2; /* Either 2 or 4 */
2686 if (i > 1)
2687 com_node(c, CHILD(n, 3));
2688 com_addoparg(c, RAISE_VARARGS, i);
2689 com_pop(c, i);
2690 /* The interpreter does not fall through */
2691 /* All jumps converge here */
2692 com_backpatch(c, a);
2693 com_backpatch(c, b);
2694 com_addbyte(c, POP_TOP);
2695}
2696
2697static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002698com_print_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002699{
Barry Warsaw29c574e2000-08-21 15:38:56 +00002700 int i = 1;
2701 node* stream = NULL;
2702
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002703 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002704
2705 /* are we using the extended print form? */
2706 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
2707 stream = CHILD(n, 2);
Barry Warsaw24703a02000-08-21 17:07:20 +00002708 com_node(c, stream);
2709 /* stack: [...] => [... stream] */
2710 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002711 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
2712 i = 4;
2713 else
2714 i = 3;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002715 }
Barry Warsaw29c574e2000-08-21 15:38:56 +00002716 for (; i < NCH(n); i += 2) {
2717 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002718 com_addbyte(c, DUP_TOP);
2719 /* stack: [stream] => [stream stream] */
2720 com_push(c, 1);
Barry Warsaw29c574e2000-08-21 15:38:56 +00002721 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002722 /* stack: [stream stream] => [stream stream obj] */
2723 com_addbyte(c, ROT_TWO);
2724 /* stack: [stream stream obj] => [stream obj stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002725 com_addbyte(c, PRINT_ITEM_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002726 /* stack: [stream obj stream] => [stream] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002727 com_pop(c, 2);
2728 }
2729 else {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002730 com_node(c, CHILD(n, i));
Barry Warsaw24703a02000-08-21 17:07:20 +00002731 /* stack: [...] => [... obj] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002732 com_addbyte(c, PRINT_ITEM);
2733 com_pop(c, 1);
2734 }
2735 }
2736 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
Barry Warsaw24703a02000-08-21 17:07:20 +00002737 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
Barry Warsaw29c574e2000-08-21 15:38:56 +00002738 if (stream != NULL) {
Barry Warsaw24703a02000-08-21 17:07:20 +00002739 /* must pop the extra stream object off the stack */
2740 com_addbyte(c, POP_TOP);
2741 /* stack: [... stream] => [...] */
2742 com_pop(c, 1);
2743 }
2744 }
2745 else {
2746 if (stream != NULL) {
2747 /* this consumes the last stream object on stack */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002748 com_addbyte(c, PRINT_NEWLINE_TO);
Barry Warsaw24703a02000-08-21 17:07:20 +00002749 /* stack: [... stream] => [...] */
Barry Warsaw29c574e2000-08-21 15:38:56 +00002750 com_pop(c, 1);
2751 }
2752 else
2753 com_addbyte(c, PRINT_NEWLINE);
2754 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002755}
2756
2757static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002758com_return_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002759{
Guido van Rossum4bad92c1991-07-27 21:34:52 +00002760 REQ(n, return_stmt); /* 'return' [testlist] */
Guido van Rossum3f5da241990-12-20 15:06:42 +00002761 if (!c->c_infunction) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002762 com_error(c, PyExc_SyntaxError, "'return' outside function");
Guido van Rossum3f5da241990-12-20 15:06:42 +00002763 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002764 if (c->c_flags & CO_GENERATOR) {
2765 if (NCH(n) > 1) {
2766 com_error(c, PyExc_SyntaxError,
2767 "'return' with argument inside generator");
2768 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002769 }
2770 if (NCH(n) < 2) {
2771 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002772 com_push(c, 1);
2773 }
Tim Petersad1a18b2001-06-23 06:19:16 +00002774 else
2775 com_node(c, CHILD(n, 1));
2776 com_addbyte(c, RETURN_VALUE);
Tim Peters5ca576e2001-06-18 22:08:13 +00002777 com_pop(c, 1);
2778}
2779
2780static void
2781com_yield_stmt(struct compiling *c, node *n)
2782{
Tim Peters95c80f82001-06-23 02:07:08 +00002783 int i;
Tim Peters5ca576e2001-06-18 22:08:13 +00002784 REQ(n, yield_stmt); /* 'yield' testlist */
2785 if (!c->c_infunction) {
2786 com_error(c, PyExc_SyntaxError, "'yield' outside function");
2787 }
Tim Peters95c80f82001-06-23 02:07:08 +00002788
2789 for (i = 0; i < c->c_nblocks; ++i) {
2790 if (c->c_block[i] == SETUP_FINALLY) {
2791 com_error(c, PyExc_SyntaxError,
2792 "'yield' not allowed in a 'try' block "
2793 "with a 'finally' clause");
2794 return;
2795 }
2796 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002797 com_node(c, CHILD(n, 1));
2798 com_addbyte(c, YIELD_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002799 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002800}
2801
2802static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002803com_raise_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002804{
Guido van Rossum8b993a91997-01-17 21:04:03 +00002805 int i;
Guido van Rossumd295f121998-04-09 21:39:57 +00002806 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
2807 if (NCH(n) > 1) {
2808 com_node(c, CHILD(n, 1));
2809 if (NCH(n) > 3) {
2810 com_node(c, CHILD(n, 3));
2811 if (NCH(n) > 5)
2812 com_node(c, CHILD(n, 5));
2813 }
Guido van Rossumf10570b1995-07-07 22:53:21 +00002814 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002815 i = NCH(n)/2;
2816 com_addoparg(c, RAISE_VARARGS, i);
2817 com_pop(c, i);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002818}
2819
2820static void
Thomas Wouters52152252000-08-17 22:55:00 +00002821com_from_import(struct compiling *c, node *n)
2822{
2823 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
2824 com_push(c, 1);
2825 if (NCH(n) > 1) {
2826 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
2827 com_error(c, PyExc_SyntaxError, "invalid syntax");
2828 return;
2829 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002830 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002831 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002832 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Thomas Wouters52152252000-08-17 22:55:00 +00002833 com_pop(c, 1);
2834}
2835
2836static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002837com_import_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002838{
2839 int i;
2840 REQ(n, import_stmt);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002841 /* 'import' dotted_name (',' dotted_name)* |
2842 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002843 if (STR(CHILD(n, 0))[0] == 'f') {
Guido van Rossum83fb0732000-11-27 22:22:36 +00002844 PyObject *tup;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00002845 /* 'from' dotted_name 'import' ... */
2846 REQ(CHILD(n, 1), dotted_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002847
2848 if (TYPE(CHILD(n, 3)) == STAR) {
2849 tup = Py_BuildValue("(s)", "*");
2850 } else {
2851 tup = PyTuple_New((NCH(n) - 2)/2);
2852 for (i = 3; i < NCH(n); i += 2) {
2853 PyTuple_SET_ITEM(tup, (i-3)/2,
2854 PyString_FromString(STR(
Jeremy Hylton4419ac12001-02-28 22:54:51 +00002855 CHILD(CHILD(n, i), 0))));
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002856 }
2857 }
2858 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
Guido van Rossum83fb0732000-11-27 22:22:36 +00002859 Py_DECREF(tup);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002860 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002861 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
Thomas Wouters52152252000-08-17 22:55:00 +00002862 if (TYPE(CHILD(n, 3)) == STAR)
2863 com_addbyte(c, IMPORT_STAR);
2864 else {
2865 for (i = 3; i < NCH(n); i += 2)
2866 com_from_import(c, CHILD(n, i));
2867 com_addbyte(c, POP_TOP);
2868 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00002869 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002870 }
2871 else {
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00002872 /* 'import' ... */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002873 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters52152252000-08-17 22:55:00 +00002874 node *subn = CHILD(n, i);
2875 REQ(subn, dotted_as_name);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002876 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002877 com_push(c, 1);
Thomas Woutersdd13e4f2000-08-27 20:31:27 +00002878 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
Thomas Wouters52152252000-08-17 22:55:00 +00002879 if (NCH(subn) > 1) {
Thomas Wouterse753ef82000-08-27 20:16:32 +00002880 int j;
2881 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
Thomas Wouters52152252000-08-17 22:55:00 +00002882 com_error(c, PyExc_SyntaxError,
2883 "invalid syntax");
2884 return;
2885 }
Thomas Wouterse753ef82000-08-27 20:16:32 +00002886 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
2887 com_addopname(c, LOAD_ATTR,
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002888 CHILD(CHILD(subn, 0),
2889 j));
2890 com_addop_varname(c, VAR_STORE,
2891 STR(CHILD(subn, 2)));
Thomas Wouters52152252000-08-17 22:55:00 +00002892 } else
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002893 com_addop_varname(c, VAR_STORE,
2894 STR(CHILD(CHILD(subn, 0),
2895 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002896 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00002897 }
2898 }
2899}
2900
2901static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002902com_exec_stmt(struct compiling *c, node *n)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002903{
2904 REQ(n, exec_stmt);
2905 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
2906 com_node(c, CHILD(n, 1));
2907 if (NCH(n) >= 4)
2908 com_node(c, CHILD(n, 3));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002909 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002910 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002911 com_push(c, 1);
2912 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002913 if (NCH(n) >= 6)
2914 com_node(c, CHILD(n, 5));
Guido van Rossum8b993a91997-01-17 21:04:03 +00002915 else {
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002916 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002917 com_push(c, 1);
2918 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002919 com_addbyte(c, EXEC_STMT);
Guido van Rossum8b993a91997-01-17 21:04:03 +00002920 com_pop(c, 3);
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002921}
2922
Guido van Rossum7c531111997-03-11 18:42:21 +00002923static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00002924is_constant_false(struct compiling *c, node *n)
Guido van Rossum7c531111997-03-11 18:42:21 +00002925{
Guido van Rossum79f25d91997-04-29 20:08:16 +00002926 PyObject *v;
Guido van Rossum7c531111997-03-11 18:42:21 +00002927 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00002928 /* argument c will be NULL when called from symtable_node() */
Guido van Rossum7c531111997-03-11 18:42:21 +00002929
2930 /* Label to avoid tail recursion */
2931 next:
2932 switch (TYPE(n)) {
2933
2934 case suite:
2935 if (NCH(n) == 1) {
2936 n = CHILD(n, 0);
2937 goto next;
2938 }
2939 /* Fall through */
2940 case file_input:
2941 for (i = 0; i < NCH(n); i++) {
2942 node *ch = CHILD(n, i);
2943 if (TYPE(ch) == stmt) {
2944 n = ch;
2945 goto next;
2946 }
2947 }
2948 break;
2949
2950 case stmt:
2951 case simple_stmt:
2952 case small_stmt:
2953 n = CHILD(n, 0);
2954 goto next;
2955
2956 case expr_stmt:
2957 case testlist:
2958 case test:
2959 case and_test:
2960 case not_test:
2961 case comparison:
2962 case expr:
2963 case xor_expr:
2964 case and_expr:
2965 case shift_expr:
2966 case arith_expr:
2967 case term:
2968 case factor:
2969 case power:
2970 case atom:
2971 if (NCH(n) == 1) {
2972 n = CHILD(n, 0);
2973 goto next;
2974 }
2975 break;
2976
2977 case NAME:
2978 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
2979 return 1;
2980 break;
2981
2982 case NUMBER:
2983 v = parsenumber(c, STR(n));
2984 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002985 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002986 break;
2987 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002988 i = PyObject_IsTrue(v);
2989 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00002990 return i == 0;
2991
2992 case STRING:
Martin v. Löwis339d0f72001-08-17 18:39:25 +00002993 v = parsestr(c, STR(n));
Guido van Rossum7c531111997-03-11 18:42:21 +00002994 if (v == NULL) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00002995 PyErr_Clear();
Guido van Rossum7c531111997-03-11 18:42:21 +00002996 break;
2997 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00002998 i = PyObject_IsTrue(v);
2999 Py_DECREF(v);
Guido van Rossum7c531111997-03-11 18:42:21 +00003000 return i == 0;
3001
3002 }
3003 return 0;
3004}
3005
Tim Peters08a898f2001-06-28 01:52:22 +00003006
3007/* Look under n for a return stmt with an expression.
3008 * This hack is used to find illegal returns under "if 0:" blocks in
3009 * functions already known to be generators (as determined by the symtable
3010 * pass).
3011 * Return the offending return node if found, else NULL.
3012 */
3013static node *
3014look_for_offending_return(node *n)
3015{
3016 int i;
3017
3018 for (i = 0; i < NCH(n); ++i) {
3019 node *kid = CHILD(n, i);
3020
3021 switch (TYPE(kid)) {
3022 case classdef:
3023 case funcdef:
3024 case lambdef:
3025 /* Stuff in nested functions & classes doesn't
3026 affect the code block we started in. */
3027 return NULL;
3028
3029 case return_stmt:
3030 if (NCH(kid) > 1)
3031 return kid;
3032 break;
3033
3034 default: {
3035 node *bad = look_for_offending_return(kid);
3036 if (bad != NULL)
3037 return bad;
3038 }
3039 }
3040 }
3041
3042 return NULL;
3043}
3044
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003045static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003046com_if_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003047{
3048 int i;
3049 int anchor = 0;
3050 REQ(n, if_stmt);
3051 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
3052 for (i = 0; i+3 < NCH(n); i+=4) {
3053 int a = 0;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003054 node *ch = CHILD(n, i+1);
Tim Peters08a898f2001-06-28 01:52:22 +00003055 if (is_constant_false(c, ch)) {
3056 /* We're going to skip this block. However, if this
3057 is a generator, we have to check the dead code
3058 anyway to make sure there aren't any return stmts
3059 with expressions, in the same scope. */
3060 if (c->c_flags & CO_GENERATOR) {
3061 node *p = look_for_offending_return(n);
3062 if (p != NULL) {
3063 int savelineno = c->c_lineno;
3064 c->c_lineno = p->n_lineno;
3065 com_error(c, PyExc_SyntaxError,
3066 "'return' with argument "
3067 "inside generator");
3068 c->c_lineno = savelineno;
3069 }
3070 }
Guido van Rossum7c531111997-03-11 18:42:21 +00003071 continue;
Tim Peters08a898f2001-06-28 01:52:22 +00003072 }
Guido van Rossum3f5da241990-12-20 15:06:42 +00003073 if (i > 0)
3074 com_addoparg(c, SET_LINENO, ch->n_lineno);
Guido van Rossum7c531111997-03-11 18:42:21 +00003075 com_node(c, ch);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003076 com_addfwref(c, JUMP_IF_FALSE, &a);
3077 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003078 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003079 com_node(c, CHILD(n, i+3));
3080 com_addfwref(c, JUMP_FORWARD, &anchor);
3081 com_backpatch(c, a);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003082 /* We jump here with an extra entry which we now pop */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003083 com_addbyte(c, POP_TOP);
3084 }
3085 if (i+2 < NCH(n))
3086 com_node(c, CHILD(n, i+2));
Guido van Rossum7c531111997-03-11 18:42:21 +00003087 if (anchor)
3088 com_backpatch(c, anchor);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003089}
3090
3091static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003092com_while_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003093{
3094 int break_anchor = 0;
3095 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003096 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003097 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
3098 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003099 block_push(c, SETUP_LOOP);
3100 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003101 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003102 com_node(c, CHILD(n, 1));
3103 com_addfwref(c, JUMP_IF_FALSE, &anchor);
3104 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003105 com_pop(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003106 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003107 com_node(c, CHILD(n, 3));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003108 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003109 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3110 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003111 com_backpatch(c, anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003112 /* We jump here with one entry more on the stack */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003113 com_addbyte(c, POP_TOP);
3114 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003115 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003116 if (NCH(n) > 4)
3117 com_node(c, CHILD(n, 6));
3118 com_backpatch(c, break_anchor);
3119}
3120
3121static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003122com_for_stmt(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003123{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003124 int break_anchor = 0;
3125 int anchor = 0;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003126 int save_begin = c->c_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003127 REQ(n, for_stmt);
3128 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
3129 com_addfwref(c, SETUP_LOOP, &break_anchor);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003130 block_push(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003131 com_node(c, CHILD(n, 3));
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003132 com_addbyte(c, GET_ITER);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003133 c->c_begin = c->c_nexti;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003134 com_addoparg(c, SET_LINENO, n->n_lineno);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003135 com_addfwref(c, FOR_ITER, &anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003136 com_push(c, 1);
Thomas Wouters434d0822000-08-24 20:11:32 +00003137 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003138 c->c_loops++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003139 com_node(c, CHILD(n, 5));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003140 c->c_loops--;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003141 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3142 c->c_begin = save_begin;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003143 com_backpatch(c, anchor);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003144 com_pop(c, 1); /* FOR_ITER has popped this */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003145 com_addbyte(c, POP_BLOCK);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003146 block_pop(c, SETUP_LOOP);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003147 if (NCH(n) > 8)
3148 com_node(c, CHILD(n, 8));
3149 com_backpatch(c, break_anchor);
3150}
3151
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003152/* Code generated for "try: S finally: Sf" is as follows:
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003153
3154 SETUP_FINALLY L
3155 <code for S>
3156 POP_BLOCK
3157 LOAD_CONST <nil>
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003158 L: <code for Sf>
3159 END_FINALLY
3160
3161 The special instructions use the block stack. Each block
3162 stack entry contains the instruction that created it (here
3163 SETUP_FINALLY), the level of the value stack at the time the
3164 block stack entry was created, and a label (here L).
3165
3166 SETUP_FINALLY:
3167 Pushes the current value stack level and the label
3168 onto the block stack.
3169 POP_BLOCK:
3170 Pops en entry from the block stack, and pops the value
3171 stack until its level is the same as indicated on the
3172 block stack. (The label is ignored.)
3173 END_FINALLY:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003174 Pops a variable number of entries from the *value* stack
3175 and re-raises the exception they specify. The number of
3176 entries popped depends on the (pseudo) exception type.
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003177
3178 The block stack is unwound when an exception is raised:
3179 when a SETUP_FINALLY entry is found, the exception is pushed
3180 onto the value stack (and the exception condition is cleared),
3181 and the interpreter jumps to the label gotten from the block
3182 stack.
3183
3184 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
Guido van Rossum3f5da241990-12-20 15:06:42 +00003185 (The contents of the value stack is shown in [], with the top
3186 at the right; 'tb' is trace-back info, 'val' the exception's
3187 associated value, and 'exc' the exception.)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003188
3189 Value stack Label Instruction Argument
3190 [] SETUP_EXCEPT L1
3191 [] <code for S>
3192 [] POP_BLOCK
3193 [] JUMP_FORWARD L0
3194
Guido van Rossum3f5da241990-12-20 15:06:42 +00003195 [tb, val, exc] L1: DUP )
3196 [tb, val, exc, exc] <evaluate E1> )
3197 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
3198 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
3199 [tb, val, exc, 1] POP )
3200 [tb, val, exc] POP
3201 [tb, val] <assign to V1> (or POP if no V1)
3202 [tb] POP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003203 [] <code for S1>
3204 JUMP_FORWARD L0
3205
Guido van Rossum3f5da241990-12-20 15:06:42 +00003206 [tb, val, exc, 0] L2: POP
3207 [tb, val, exc] DUP
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003208 .............................etc.......................
3209
Guido van Rossum3f5da241990-12-20 15:06:42 +00003210 [tb, val, exc, 0] Ln+1: POP
3211 [tb, val, exc] END_FINALLY # re-raise exception
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003212
3213 [] L0: <next statement>
3214
3215 Of course, parts are not generated if Vi or Ei is not present.
3216*/
3217
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003218static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003219com_try_except(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003220{
3221 int except_anchor = 0;
3222 int end_anchor = 0;
3223 int else_anchor = 0;
3224 int i;
3225 node *ch;
3226
3227 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
3228 block_push(c, SETUP_EXCEPT);
3229 com_node(c, CHILD(n, 2));
3230 com_addbyte(c, POP_BLOCK);
3231 block_pop(c, SETUP_EXCEPT);
3232 com_addfwref(c, JUMP_FORWARD, &else_anchor);
3233 com_backpatch(c, except_anchor);
3234 for (i = 3;
3235 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
3236 i += 3) {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003237 /* except_clause: 'except' [expr [',' var]] */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003238 if (except_anchor == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003239 com_error(c, PyExc_SyntaxError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003240 "default 'except:' must be last");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003241 break;
3242 }
3243 except_anchor = 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003244 com_push(c, 3); /* tb, val, exc pushed by exception */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003245 com_addoparg(c, SET_LINENO, ch->n_lineno);
3246 if (NCH(ch) > 1) {
3247 com_addbyte(c, DUP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003248 com_push(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003249 com_node(c, CHILD(ch, 1));
3250 com_addoparg(c, COMPARE_OP, EXC_MATCH);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003251 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003252 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
3253 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003254 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003255 }
3256 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003257 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003258 if (NCH(ch) > 3)
Thomas Wouters434d0822000-08-24 20:11:32 +00003259 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003260 else {
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003261 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003262 com_pop(c, 1);
3263 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003264 com_addbyte(c, POP_TOP);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003265 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003266 com_node(c, CHILD(n, i+2));
3267 com_addfwref(c, JUMP_FORWARD, &end_anchor);
3268 if (except_anchor) {
3269 com_backpatch(c, except_anchor);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003270 /* We come in with [tb, val, exc, 0] on the
3271 stack; one pop and it's the same as
3272 expected at the start of the loop */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003273 com_addbyte(c, POP_TOP);
3274 }
3275 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003276 /* We actually come in here with [tb, val, exc] but the
3277 END_FINALLY will zap those and jump around.
3278 The c_stacklevel does not reflect them so we need not pop
3279 anything. */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003280 com_addbyte(c, END_FINALLY);
3281 com_backpatch(c, else_anchor);
3282 if (i < NCH(n))
3283 com_node(c, CHILD(n, i+2));
3284 com_backpatch(c, end_anchor);
3285}
3286
3287static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003288com_try_finally(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003289{
3290 int finally_anchor = 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003291 node *ch;
Guido van Rossum94fb82e1992-04-05 14:24:50 +00003292
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003293 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
3294 block_push(c, SETUP_FINALLY);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003295 com_node(c, CHILD(n, 2));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003296 com_addbyte(c, POP_BLOCK);
3297 block_pop(c, SETUP_FINALLY);
3298 block_push(c, END_FINALLY);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003299 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003300 /* While the generated code pushes only one item,
3301 the try-finally handling can enter here with
3302 up to three items. OK, here are the details:
3303 3 for an exception, 2 for RETURN, 1 for BREAK. */
3304 com_push(c, 3);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003305 com_backpatch(c, finally_anchor);
3306 ch = CHILD(n, NCH(n)-1);
3307 com_addoparg(c, SET_LINENO, ch->n_lineno);
3308 com_node(c, ch);
3309 com_addbyte(c, END_FINALLY);
3310 block_pop(c, END_FINALLY);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003311 com_pop(c, 3); /* Matches the com_push above */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003312}
3313
3314static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003315com_try_stmt(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003316{
3317 REQ(n, try_stmt);
3318 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
3319 | 'try' ':' suite 'finally' ':' suite */
3320 if (TYPE(CHILD(n, 3)) != except_clause)
3321 com_try_finally(c, n);
3322 else
3323 com_try_except(c, n);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003324}
3325
Guido van Rossum8b993a91997-01-17 21:04:03 +00003326static node *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003327get_rawdocstring(node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003328{
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003329 int i;
3330
Guido van Rossum8b993a91997-01-17 21:04:03 +00003331 /* Label to avoid tail recursion */
3332 next:
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003333 switch (TYPE(n)) {
3334
3335 case suite:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003336 if (NCH(n) == 1) {
3337 n = CHILD(n, 0);
3338 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003339 }
Guido van Rossum8b993a91997-01-17 21:04:03 +00003340 /* Fall through */
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003341 case file_input:
3342 for (i = 0; i < NCH(n); i++) {
3343 node *ch = CHILD(n, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003344 if (TYPE(ch) == stmt) {
3345 n = ch;
3346 goto next;
3347 }
Guido van Rossum164d4ff1995-01-26 00:40:09 +00003348 }
3349 break;
3350
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003351 case stmt:
3352 case simple_stmt:
3353 case small_stmt:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003354 n = CHILD(n, 0);
3355 goto next;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003356
3357 case expr_stmt:
3358 case testlist:
3359 case test:
3360 case and_test:
3361 case not_test:
3362 case comparison:
3363 case expr:
3364 case xor_expr:
3365 case and_expr:
3366 case shift_expr:
3367 case arith_expr:
3368 case term:
3369 case factor:
Guido van Rossum50564e81996-01-12 01:13:16 +00003370 case power:
Guido van Rossum8b993a91997-01-17 21:04:03 +00003371 if (NCH(n) == 1) {
3372 n = CHILD(n, 0);
3373 goto next;
3374 }
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003375 break;
3376
3377 case atom:
3378 if (TYPE(CHILD(n, 0)) == STRING)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003379 return n;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003380 break;
3381
3382 }
3383 return NULL;
3384}
3385
Guido van Rossum79f25d91997-04-29 20:08:16 +00003386static PyObject *
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003387get_docstring(struct compiling *c, node *n)
Guido van Rossum8b993a91997-01-17 21:04:03 +00003388{
Guido van Rossum541563e1999-01-28 15:08:09 +00003389 /* Don't generate doc-strings if run with -OO */
3390 if (Py_OptimizeFlag > 1)
3391 return NULL;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003392 n = get_rawdocstring(n);
3393 if (n == NULL)
3394 return NULL;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003395 return parsestrplus(c, n);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003396}
3397
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003398static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003399com_suite(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003400{
3401 REQ(n, suite);
3402 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
3403 if (NCH(n) == 1) {
3404 com_node(c, CHILD(n, 0));
3405 }
3406 else {
3407 int i;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003408 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003409 node *ch = CHILD(n, i);
3410 if (TYPE(ch) == stmt)
3411 com_node(c, ch);
3412 }
3413 }
3414}
3415
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003416/* ARGSUSED */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003417static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003418com_continue_stmt(struct compiling *c, node *n)
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003419{
3420 int i = c->c_nblocks;
3421 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
3422 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
3423 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003424 else if (i <= 0) {
3425 /* at the outer level */
3426 com_error(c, PyExc_SyntaxError,
3427 "'continue' not properly in loop");
3428 }
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003429 else {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003430 int j;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003431 for (j = i-1; j >= 0; --j) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003432 if (c->c_block[j] == SETUP_LOOP)
3433 break;
3434 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003435 if (j >= 0) {
Fred Drakefd1f1be2000-09-08 16:31:24 +00003436 /* there is a loop, but something interferes */
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003437 for (; i > j; --i) {
3438 if (c->c_block[i] == SETUP_EXCEPT ||
3439 c->c_block[i] == SETUP_FINALLY) {
3440 com_addoparg(c, CONTINUE_LOOP,
3441 c->c_begin);
Fred Drakefd1f1be2000-09-08 16:31:24 +00003442 return;
3443 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003444 if (c->c_block[i] == END_FINALLY) {
3445 com_error(c, PyExc_SyntaxError,
3446 "'continue' not supported inside 'finally' clause");
3447 return;
3448 }
Fred Drakefd1f1be2000-09-08 16:31:24 +00003449 }
3450 }
Guido van Rossum79f25d91997-04-29 20:08:16 +00003451 com_error(c, PyExc_SyntaxError,
3452 "'continue' not properly in loop");
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003453 }
3454 /* XXX Could allow it inside a 'finally' clause
3455 XXX if we could pop the exception still on the stack */
3456}
3457
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003458static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003459com_argdefs(struct compiling *c, node *n)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003460{
Guido van Rossum681d79a1995-07-18 14:51:37 +00003461 int i, nch, nargs, ndefs;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003462 if (TYPE(n) == lambdef) {
3463 /* lambdef: 'lambda' [varargslist] ':' test */
3464 n = CHILD(n, 1);
3465 }
3466 else {
3467 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */
3468 n = CHILD(n, 2);
3469 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
3470 n = CHILD(n, 1);
3471 }
3472 if (TYPE(n) != varargslist)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003473 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003474 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003475 (fpdef ['=' test] ',')* '*' ....... |
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003476 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
3477 nch = NCH(n);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003478 nargs = 0;
3479 ndefs = 0;
3480 for (i = 0; i < nch; i++) {
3481 int t;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003482 if (TYPE(CHILD(n, i)) == STAR ||
3483 TYPE(CHILD(n, i)) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003484 break;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003485 nargs++;
3486 i++;
3487 if (i >= nch)
Guido van Rossuma1e7e621995-09-18 21:44:04 +00003488 t = RPAR; /* Anything except EQUAL or COMMA */
3489 else
3490 t = TYPE(CHILD(n, i));
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003491 if (t == EQUAL) {
3492 i++;
3493 ndefs++;
3494 com_node(c, CHILD(n, i));
3495 i++;
3496 if (i >= nch)
3497 break;
3498 t = TYPE(CHILD(n, i));
3499 }
3500 else {
Guido van Rossum29d38cd1998-10-02 13:41:54 +00003501 /* Treat "(a=1, b)" as an error */
3502 if (ndefs)
3503 com_error(c, PyExc_SyntaxError,
Guido van Rossumdfede311998-10-02 14:06:56 +00003504 "non-default argument follows default argument");
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003505 }
3506 if (t != COMMA)
3507 break;
3508 }
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003509 return ndefs;
3510}
3511
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003512static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003513com_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003514{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003515 PyObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003516 int ndefs;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003517 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003518 ndefs = com_argdefs(c, n);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003519 symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n),
3520 n->n_lineno);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003521 co = (PyObject *)icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003522 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003523 if (co == NULL)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524 c->c_errors++;
3525 else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003526 int closure = com_make_closure(c, (PyCodeObject *)co);
3527 int i = com_addconst(c, co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003528 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003529 com_push(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003530 if (closure)
3531 com_addoparg(c, MAKE_CLOSURE, ndefs);
3532 else
3533 com_addoparg(c, MAKE_FUNCTION, ndefs);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003534 com_pop(c, ndefs);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003535 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003536 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003537 Py_DECREF(co);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003538 }
3539}
3540
3541static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003542com_bases(struct compiling *c, node *n)
Guido van Rossumc5e96291991-12-10 13:53:51 +00003543{
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003544 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003545 REQ(n, testlist);
3546 /* testlist: test (',' test)* [','] */
3547 for (i = 0; i < NCH(n); i += 2)
3548 com_node(c, CHILD(n, i));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003549 i = (NCH(n)+1) / 2;
3550 com_addoparg(c, BUILD_TUPLE, i);
3551 com_pop(c, i-1);
Guido van Rossumc5e96291991-12-10 13:53:51 +00003552}
3553
3554static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003555com_classdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003556{
Guido van Rossum25831651993-05-19 14:50:45 +00003557 int i;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003558 PyObject *v;
3559 PyCodeObject *co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003560 char *name;
3561
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003562 REQ(n, classdef);
Guido van Rossum25831651993-05-19 14:50:45 +00003563 /* classdef: class NAME ['(' testlist ')'] ':' suite */
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00003564 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +00003565 c->c_errors++;
3566 return;
3567 }
3568 /* Push the class name on the stack */
3569 i = com_addconst(c, v);
3570 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003571 com_push(c, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003572 Py_DECREF(v);
Guido van Rossum25831651993-05-19 14:50:45 +00003573 /* Push the tuple of base classes on the stack */
Guido van Rossum8b993a91997-01-17 21:04:03 +00003574 if (TYPE(CHILD(n, 2)) != LPAR) {
Guido van Rossumc5e96291991-12-10 13:53:51 +00003575 com_addoparg(c, BUILD_TUPLE, 0);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003576 com_push(c, 1);
3577 }
Guido van Rossum25831651993-05-19 14:50:45 +00003578 else
3579 com_bases(c, CHILD(n, 3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003580 name = STR(CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00003581 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003582 co = icompile(n, c);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003583 symtable_exit_scope(c->c_symtable);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003584 if (co == NULL)
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003585 c->c_errors++;
3586 else {
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003587 int closure = com_make_closure(c, co);
3588 i = com_addconst(c, (PyObject *)co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003589 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003590 com_push(c, 1);
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003591 if (closure) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003592 com_addoparg(c, MAKE_CLOSURE, 0);
Jeremy Hylton733c8932001-12-13 19:51:56 +00003593 com_pop(c, PyCode_GetNumFree(co));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003594 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003595 com_addoparg(c, MAKE_FUNCTION, 0);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003596 com_addoparg(c, CALL_FUNCTION, 0);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003597 com_addbyte(c, BUILD_CLASS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003598 com_pop(c, 2);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003599 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
Jeremy Hylton93a569d2001-10-17 13:22:22 +00003600 com_pop(c, 1);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003601 Py_DECREF(co);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003602 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003603}
3604
3605static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003606com_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003607{
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003608 loop:
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003609 if (c->c_errors)
3610 return;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003611 switch (TYPE(n)) {
3612
3613 /* Definition nodes */
3614
3615 case funcdef:
3616 com_funcdef(c, n);
3617 break;
3618 case classdef:
3619 com_classdef(c, n);
3620 break;
3621
3622 /* Trivial parse tree nodes */
3623
3624 case stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003625 case small_stmt:
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003626 case flow_stmt:
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003627 n = CHILD(n, 0);
3628 goto loop;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003629
3630 case simple_stmt:
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003631 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
3632 com_addoparg(c, SET_LINENO, n->n_lineno);
3633 {
3634 int i;
3635 for (i = 0; i < NCH(n)-1; i += 2)
3636 com_node(c, CHILD(n, i));
3637 }
3638 break;
3639
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003640 case compound_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003641 com_addoparg(c, SET_LINENO, n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003642 n = CHILD(n, 0);
3643 goto loop;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003644
3645 /* Statement nodes */
3646
3647 case expr_stmt:
3648 com_expr_stmt(c, n);
3649 break;
3650 case print_stmt:
3651 com_print_stmt(c, n);
3652 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003653 case del_stmt: /* 'del' exprlist */
Thomas Wouters434d0822000-08-24 20:11:32 +00003654 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003655 break;
3656 case pass_stmt:
3657 break;
3658 case break_stmt:
Guido van Rossum3f5da241990-12-20 15:06:42 +00003659 if (c->c_loops == 0) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00003660 com_error(c, PyExc_SyntaxError,
3661 "'break' outside loop");
Guido van Rossum3f5da241990-12-20 15:06:42 +00003662 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003663 com_addbyte(c, BREAK_LOOP);
3664 break;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003665 case continue_stmt:
3666 com_continue_stmt(c, n);
3667 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003668 case return_stmt:
3669 com_return_stmt(c, n);
3670 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00003671 case yield_stmt:
3672 com_yield_stmt(c, n);
3673 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003674 case raise_stmt:
3675 com_raise_stmt(c, n);
3676 break;
3677 case import_stmt:
3678 com_import_stmt(c, n);
3679 break;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003680 case global_stmt:
Guido van Rossumc5e96291991-12-10 13:53:51 +00003681 break;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00003682 case exec_stmt:
3683 com_exec_stmt(c, n);
3684 break;
Guido van Rossum228d7f31997-04-02 05:24:36 +00003685 case assert_stmt:
3686 com_assert_stmt(c, n);
3687 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003688 case if_stmt:
3689 com_if_stmt(c, n);
3690 break;
3691 case while_stmt:
3692 com_while_stmt(c, n);
3693 break;
3694 case for_stmt:
3695 com_for_stmt(c, n);
3696 break;
3697 case try_stmt:
3698 com_try_stmt(c, n);
3699 break;
3700 case suite:
3701 com_suite(c, n);
3702 break;
3703
3704 /* Expression nodes */
3705
3706 case testlist:
Guido van Rossum1c917072001-10-15 15:44:05 +00003707 case testlist_safe:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003708 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003709 break;
3710 case test:
3711 com_test(c, n);
3712 break;
3713 case and_test:
3714 com_and_test(c, n);
3715 break;
3716 case not_test:
3717 com_not_test(c, n);
3718 break;
3719 case comparison:
3720 com_comparison(c, n);
3721 break;
3722 case exprlist:
Guido van Rossum288a60f1991-12-16 13:05:10 +00003723 com_list(c, n, 0);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003724 break;
3725 case expr:
3726 com_expr(c, n);
3727 break;
Guido van Rossum7928cd71991-10-24 14:59:31 +00003728 case xor_expr:
3729 com_xor_expr(c, n);
3730 break;
3731 case and_expr:
3732 com_and_expr(c, n);
3733 break;
3734 case shift_expr:
3735 com_shift_expr(c, n);
3736 break;
3737 case arith_expr:
3738 com_arith_expr(c, n);
3739 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003740 case term:
3741 com_term(c, n);
3742 break;
3743 case factor:
3744 com_factor(c, n);
3745 break;
Guido van Rossum50564e81996-01-12 01:13:16 +00003746 case power:
3747 com_power(c, n);
3748 break;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003749 case atom:
3750 com_atom(c, n);
3751 break;
3752
3753 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003754 com_error(c, PyExc_SystemError,
3755 "com_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003756 }
3757}
3758
Tim Petersdbd9ba62000-07-09 03:09:57 +00003759static void com_fplist(struct compiling *, node *);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003760
3761static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003762com_fpdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003763{
3764 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
3765 if (TYPE(CHILD(n, 0)) == LPAR)
3766 com_fplist(c, CHILD(n, 1));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003767 else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003768 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003769 com_pop(c, 1);
3770 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003771}
3772
3773static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003774com_fplist(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003775{
Guido van Rossuma9df32a1991-12-31 13:13:35 +00003776 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003777 if (NCH(n) == 1) {
3778 com_fpdef(c, CHILD(n, 0));
3779 }
3780 else {
Guido van Rossum8b993a91997-01-17 21:04:03 +00003781 int i = (NCH(n)+1)/2;
Thomas Wouters0be5aab2000-08-11 22:15:52 +00003782 com_addoparg(c, UNPACK_SEQUENCE, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003783 com_push(c, i-1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003784 for (i = 0; i < NCH(n); i += 2)
3785 com_fpdef(c, CHILD(n, i));
3786 }
3787}
3788
3789static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003790com_arglist(struct compiling *c, node *n)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003791{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003792 int nch, i, narg;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003793 int complex = 0;
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003794 char nbuf[30];
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003795 REQ(n, varargslist);
Guido van Rossumacbefef1992-01-19 16:33:51 +00003796 /* varargslist:
Guido van Rossum681d79a1995-07-18 14:51:37 +00003797 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003798 nch = NCH(n);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003799 /* Enter all arguments in table of locals */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003800 for (i = 0, narg = 0; i < nch; i++) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003801 node *ch = CHILD(n, i);
3802 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003803 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossumf10570b1995-07-07 22:53:21 +00003804 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003805 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3806 fp = CHILD(ch, 0);
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00003807 if (TYPE(fp) != NAME) {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00003808 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Guido van Rossum530956d1996-07-21 02:27:43 +00003809 complex = 1;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003810 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003811 narg++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003812 /* all name updates handled by symtable */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003813 if (++i >= nch)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003814 break;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003815 ch = CHILD(n, i);
3816 if (TYPE(ch) == EQUAL)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003817 i += 2;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003818 else
3819 REQ(ch, COMMA);
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003820 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003821 if (complex) {
3822 /* Generate code for complex arguments only after
3823 having counted the simple arguments */
3824 int ilocal = 0;
3825 for (i = 0; i < nch; i++) {
3826 node *ch = CHILD(n, i);
3827 node *fp;
Guido van Rossum50564e81996-01-12 01:13:16 +00003828 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003829 break;
3830 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
3831 fp = CHILD(ch, 0);
3832 if (TYPE(fp) != NAME) {
3833 com_addoparg(c, LOAD_FAST, ilocal);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003834 com_push(c, 1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003835 com_fpdef(c, ch);
3836 }
3837 ilocal++;
3838 if (++i >= nch)
3839 break;
3840 ch = CHILD(n, i);
3841 if (TYPE(ch) == EQUAL)
3842 i += 2;
3843 else
3844 REQ(ch, COMMA);
3845 }
3846 }
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003847}
3848
3849static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850com_file_input(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851{
3852 int i;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003853 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003854 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003855 doc = get_docstring(c, n);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003856 if (doc != NULL) {
3857 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003858 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003859 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003860 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003861 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003862 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003863 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864 for (i = 0; i < NCH(n); i++) {
3865 node *ch = CHILD(n, i);
3866 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
3867 com_node(c, ch);
3868 }
3869}
3870
3871/* Top-level compile-node interface */
3872
3873static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003874compile_funcdef(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003875{
Guido van Rossum79f25d91997-04-29 20:08:16 +00003876 PyObject *doc;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003877 node *ch;
3878 REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */
Guido van Rossum9bfef441993-03-29 10:43:31 +00003879 c->c_name = STR(CHILD(n, 1));
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003880 doc = get_docstring(c, CHILD(n, 4));
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003881 if (doc != NULL) {
3882 (void) com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003883 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003884 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003885 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003886 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003887 ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */
3888 ch = CHILD(ch, 1); /* ')' | varargslist */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003889 if (TYPE(ch) == varargslist)
Guido van Rossum49d6dc41992-01-14 18:30:26 +00003890 com_arglist(c, ch);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003891 c->c_infunction = 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003892 com_node(c, CHILD(n, 4));
Guido van Rossum3f5da241990-12-20 15:06:42 +00003893 c->c_infunction = 0;
Tim Petersad1a18b2001-06-23 06:19:16 +00003894 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
3895 com_push(c, 1);
3896 com_addbyte(c, RETURN_VALUE);
3897 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898}
3899
3900static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003901compile_lambdef(struct compiling *c, node *n)
Guido van Rossum12d12c51993-10-26 17:58:25 +00003902{
Guido van Rossum590baa41993-11-30 13:40:46 +00003903 node *ch;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003904 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
Guido van Rossum363ac7d1994-11-10 22:40:34 +00003905 c->c_name = "<lambda>";
Guido van Rossum590baa41993-11-30 13:40:46 +00003906
3907 ch = CHILD(n, 1);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003908 (void) com_addconst(c, Py_None); /* No docstring */
Guido van Rossum681d79a1995-07-18 14:51:37 +00003909 if (TYPE(ch) == varargslist) {
Guido van Rossum590baa41993-11-30 13:40:46 +00003910 com_arglist(c, ch);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003911 ch = CHILD(n, 3);
Guido van Rossum590baa41993-11-30 13:40:46 +00003912 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003913 else
3914 ch = CHILD(n, 2);
3915 com_node(c, ch);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003916 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003917 com_pop(c, 1);
Guido van Rossum12d12c51993-10-26 17:58:25 +00003918}
3919
3920static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003921compile_classdef(struct compiling *c, node *n)
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003922{
3923 node *ch;
Guido van Rossum79f25d91997-04-29 20:08:16 +00003924 PyObject *doc;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003925 REQ(n, classdef);
3926 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
3927 c->c_name = STR(CHILD(n, 1));
Guido van Rossum8ff077b1996-08-24 06:21:31 +00003928 c->c_private = c->c_name;
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003929 ch = CHILD(n, NCH(n)-1); /* The suite */
Martin v. Löwis339d0f72001-08-17 18:39:25 +00003930 doc = get_docstring(c, ch);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003931 if (doc != NULL) {
3932 int i = com_addconst(c, doc);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003933 Py_DECREF(doc);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003934 com_addoparg(c, LOAD_CONST, i);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003935 com_push(c, 1);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003936 com_addop_name(c, STORE_NAME, "__doc__");
Guido van Rossum8b993a91997-01-17 21:04:03 +00003937 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003938 }
3939 else
Guido van Rossum79f25d91997-04-29 20:08:16 +00003940 (void) com_addconst(c, Py_None);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003941 com_node(c, ch);
3942 com_addbyte(c, LOAD_LOCALS);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003943 com_push(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003944 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003945 com_pop(c, 1);
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003946}
3947
3948static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003949compile_node(struct compiling *c, node *n)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003950{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003951 com_addoparg(c, SET_LINENO, n->n_lineno);
3952
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003953 switch (TYPE(n)) {
3954
Guido van Rossum4c417781991-01-21 16:09:22 +00003955 case single_input: /* One interactive command */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003956 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003957 c->c_interactive++;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003958 n = CHILD(n, 0);
3959 if (TYPE(n) != NEWLINE)
3960 com_node(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003961 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003962 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003963 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003964 com_pop(c, 1);
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003965 c->c_interactive--;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003966 break;
3967
Guido van Rossum4c417781991-01-21 16:09:22 +00003968 case file_input: /* A whole file, or built-in function exec() */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003969 com_file_input(c, n);
Guido van Rossum79f25d91997-04-29 20:08:16 +00003970 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
Guido van Rossum8b993a91997-01-17 21:04:03 +00003971 com_push(c, 1);
Guido van Rossum3f5da241990-12-20 15:06:42 +00003972 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003973 com_pop(c, 1);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003974 break;
3975
Guido van Rossum590baa41993-11-30 13:40:46 +00003976 case eval_input: /* Built-in function input() */
Guido van Rossum4c417781991-01-21 16:09:22 +00003977 com_node(c, CHILD(n, 0));
3978 com_addbyte(c, RETURN_VALUE);
Guido van Rossum8b993a91997-01-17 21:04:03 +00003979 com_pop(c, 1);
Guido van Rossum4c417781991-01-21 16:09:22 +00003980 break;
3981
Guido van Rossum590baa41993-11-30 13:40:46 +00003982 case lambdef: /* anonymous function definition */
3983 compile_lambdef(c, n);
3984 break;
3985
Guido van Rossum4c417781991-01-21 16:09:22 +00003986 case funcdef: /* A function definition */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003987 compile_funcdef(c, n);
3988 break;
3989
Guido van Rossum4c417781991-01-21 16:09:22 +00003990 case classdef: /* A class definition */
Guido van Rossum1f4fa501995-01-07 12:41:23 +00003991 compile_classdef(c, n);
Guido van Rossumd6f3bc21990-11-18 17:35:03 +00003992 break;
3993
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003994 default:
Guido van Rossum79f25d91997-04-29 20:08:16 +00003995 com_error(c, PyExc_SystemError,
Guido van Rossum452a9831996-09-17 14:32:04 +00003996 "compile_node: unexpected node type");
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003997 }
3998}
3999
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004000static PyObject *
4001dict_keys_inorder(PyObject *dict, int offset)
4002{
4003 PyObject *tuple, *k, *v;
4004 int i, pos = 0, size = PyDict_Size(dict);
4005
4006 tuple = PyTuple_New(size);
4007 if (tuple == NULL)
4008 return NULL;
4009 while (PyDict_Next(dict, &pos, &k, &v)) {
4010 i = PyInt_AS_LONG(v);
4011 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004012 assert((i - offset) < size);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004013 PyTuple_SET_ITEM(tuple, i - offset, k);
4014 }
4015 return tuple;
4016}
4017
Guido van Rossum79f25d91997-04-29 20:08:16 +00004018PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004019PyNode_Compile(node *n, char *filename)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004020{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004021 return PyNode_CompileFlags(n, filename, NULL);
4022}
4023
4024PyCodeObject *
4025PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags)
4026{
4027 return jcompile(n, filename, NULL, flags);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004028}
4029
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004030struct symtable *
4031PyNode_CompileSymtable(node *n, char *filename)
4032{
4033 struct symtable *st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004034 PyFutureFeatures *ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004035
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004036 ff = PyNode_Future(n, filename);
4037 if (ff == NULL)
4038 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004039
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004040 st = symtable_init();
Tim Peters8c5e4152001-11-04 19:26:58 +00004041 if (st == NULL) {
4042 PyMem_Free((void *)ff);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004043 return NULL;
Tim Peters8c5e4152001-11-04 19:26:58 +00004044 }
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004045 st->st_future = ff;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004046 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004047 if (st->st_errors > 0)
4048 goto fail;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004049 symtable_node(st, n);
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004050 if (st->st_errors > 0)
4051 goto fail;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004052
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004053 return st;
Jeremy Hyltonded4bd72001-03-21 19:01:33 +00004054 fail:
4055 PyMem_Free((void *)ff);
4056 st->st_future = NULL;
4057 PySymtable_Free(st);
4058 return NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004059}
4060
Guido van Rossum79f25d91997-04-29 20:08:16 +00004061static PyCodeObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004062icompile(node *n, struct compiling *base)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004063{
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004064 return jcompile(n, base->c_filename, base, NULL);
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004065}
4066
Guido van Rossum79f25d91997-04-29 20:08:16 +00004067static PyCodeObject *
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004068jcompile(node *n, char *filename, struct compiling *base,
4069 PyCompilerFlags *flags)
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004070{
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004071 struct compiling sc;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004072 PyCodeObject *co;
Guido van Rossum590baa41993-11-30 13:40:46 +00004073 if (!com_init(&sc, filename))
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004074 return NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004075 if (base) {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004076 sc.c_private = base->c_private;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004077 sc.c_symtable = base->c_symtable;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004078 /* c_symtable still points to parent's symbols */
4079 if (base->c_nested
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004080 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004081 sc.c_nested = 1;
Jeremy Hylton93a569d2001-10-17 13:22:22 +00004082 sc.c_flags |= base->c_flags & PyCF_MASK;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004083 } else {
Guido van Rossum8ff077b1996-08-24 06:21:31 +00004084 sc.c_private = NULL;
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004085 sc.c_future = PyNode_Future(n, filename);
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004086 if (sc.c_future == NULL) {
4087 com_free(&sc);
4088 return NULL;
4089 }
Jeremy Hylton481081e2001-08-14 20:01:59 +00004090 if (flags) {
4091 int merged = sc.c_future->ff_features |
4092 flags->cf_flags;
4093 sc.c_future->ff_features = merged;
4094 flags->cf_flags = merged;
4095 }
Jeremy Hylton9f324e92001-03-01 22:59:14 +00004096 if (symtable_build(&sc, n) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004097 com_free(&sc);
4098 return NULL;
4099 }
4100 }
4101 co = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004102 if (symtable_load_symbols(&sc) < 0) {
4103 sc.c_errors++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004104 goto exit;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004105 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004106 compile_node(&sc, n);
4107 com_done(&sc);
Guido van Rossum9bfef441993-03-29 10:43:31 +00004108 if (sc.c_errors == 0) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004109 PyObject *consts, *names, *varnames, *filename, *name,
4110 *freevars, *cellvars;
Guido van Rossum79f25d91997-04-29 20:08:16 +00004111 consts = PyList_AsTuple(sc.c_consts);
4112 names = PyList_AsTuple(sc.c_names);
4113 varnames = PyList_AsTuple(sc.c_varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004114 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
4115 freevars = dict_keys_inorder(sc.c_freevars,
4116 PyTuple_GET_SIZE(cellvars));
Guido van Rossum7b89b6a1997-01-18 08:02:57 +00004117 filename = PyString_InternFromString(sc.c_filename);
4118 name = PyString_InternFromString(sc.c_name);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004119 if (!PyErr_Occurred())
4120 co = PyCode_New(sc.c_argcount,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004121 sc.c_nlocals,
4122 sc.c_maxstacklevel,
4123 sc.c_flags,
4124 sc.c_code,
4125 consts,
4126 names,
4127 varnames,
4128 freevars,
4129 cellvars,
4130 filename,
4131 name,
4132 sc.c_firstlineno,
4133 sc.c_lnotab);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004134 Py_XDECREF(consts);
4135 Py_XDECREF(names);
4136 Py_XDECREF(varnames);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004137 Py_XDECREF(freevars);
4138 Py_XDECREF(cellvars);
Guido van Rossum79f25d91997-04-29 20:08:16 +00004139 Py_XDECREF(filename);
4140 Py_XDECREF(name);
Guido van Rossuma082ce41991-06-04 19:41:56 +00004141 }
Guido van Rossum6c2f0c72000-08-07 19:22:43 +00004142 else if (!PyErr_Occurred()) {
4143 /* This could happen if someone called PyErr_Clear() after an
4144 error was reported above. That's not supposed to happen,
4145 but I just plugged one case and I'm not sure there can't be
4146 others. In that case, raise SystemError so that at least
4147 it gets reported instead dumping core. */
4148 PyErr_SetString(PyExc_SystemError, "lost syntax error");
4149 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004150 exit:
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004151 if (base == NULL) {
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004152 PySymtable_Free(sc.c_symtable);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004153 sc.c_symtable = NULL;
4154 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004155 com_free(&sc);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004156 return co;
4157}
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004158
4159int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004160PyCode_Addr2Line(PyCodeObject *co, int addrq)
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004161{
4162 int size = PyString_Size(co->co_lnotab) / 2;
Guido van Rossum2174dcb1999-09-15 22:48:09 +00004163 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
Guido van Rossumda4eb5c1997-01-24 03:43:35 +00004164 int line = co->co_firstlineno;
4165 int addr = 0;
4166 while (--size >= 0) {
4167 addr += *p++;
4168 if (addr > addrq)
4169 break;
4170 line += *p++;
4171 }
4172 return line;
4173}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004174
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004175/* The test for LOCAL must come before the test for FREE in order to
4176 handle classes where name is both local and free. The local var is
4177 a method and the free var is a free var referenced within a method.
4178*/
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004179
4180static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004181get_ref_type(struct compiling *c, char *name)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004182{
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004183 char buf[350];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004184 PyObject *v;
Jeremy Hylton51257732001-03-01 00:42:55 +00004185
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004186 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
4187 return CELL;
4188 if (PyDict_GetItemString(c->c_locals, name) != NULL)
4189 return LOCAL;
4190 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
4191 return FREE;
4192 v = PyDict_GetItemString(c->c_globals, name);
4193 if (v) {
4194 if (v == Py_None)
4195 return GLOBAL_EXPLICIT;
4196 else {
4197 return GLOBAL_IMPLICIT;
4198 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004199 }
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +00004200 PyOS_snprintf(buf, sizeof(buf),
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004201 "unknown scope for %.100s in %.100s(%s) "
4202 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
4203 name, c->c_name,
4204 PyObject_REPR(c->c_symtable->st_cur->ste_id),
4205 c->c_filename,
4206 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
4207 PyObject_REPR(c->c_locals),
4208 PyObject_REPR(c->c_globals)
4209 );
4210
4211 Py_FatalError(buf);
4212 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004213}
4214
Guido van Rossum207fda62001-03-02 03:30:41 +00004215/* Helper functions to issue warnings */
4216
4217static int
4218issue_warning(char *msg, char *filename, int lineno)
4219{
4220 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
4221 lineno, NULL, NULL) < 0) {
4222 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
4223 PyErr_SetString(PyExc_SyntaxError, msg);
4224 PyErr_SyntaxLocation(filename, lineno);
4225 }
4226 return -1;
4227 }
4228 return 0;
4229}
Guido van Rossumee34ac12001-02-28 22:08:12 +00004230
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004231static int
Guido van Rossumee34ac12001-02-28 22:08:12 +00004232symtable_warn(struct symtable *st, char *msg)
4233{
Guido van Rossum207fda62001-03-02 03:30:41 +00004234 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
Guido van Rossumee34ac12001-02-28 22:08:12 +00004235 st->st_errors++;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004236 return -1;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004237 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004238 return 0;
Guido van Rossumee34ac12001-02-28 22:08:12 +00004239}
4240
Jeremy Hylton9f1b9932001-02-28 07:07:43 +00004241/* Helper function for setting lineno and filename */
4242
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004243static int
4244symtable_build(struct compiling *c, node *n)
4245{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004246 if ((c->c_symtable = symtable_init()) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004247 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004248 c->c_symtable->st_future = c->c_future;
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004249 c->c_symtable->st_filename = c->c_filename;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004250 symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno);
4251 if (c->c_symtable->st_errors > 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004252 return -1;
4253 symtable_node(c->c_symtable, n);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004254 if (c->c_symtable->st_errors > 0)
4255 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004256 /* reset for second pass */
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004257 c->c_symtable->st_nscopes = 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004258 c->c_symtable->st_pass = 2;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004259 return 0;
4260}
4261
4262static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004263symtable_init_compiling_symbols(struct compiling *c)
4264{
4265 PyObject *varnames;
4266
4267 varnames = c->c_symtable->st_cur->ste_varnames;
4268 if (varnames == NULL) {
4269 varnames = PyList_New(0);
4270 if (varnames == NULL)
4271 return -1;
4272 c->c_symtable->st_cur->ste_varnames = varnames;
4273 Py_INCREF(varnames);
4274 } else
4275 Py_INCREF(varnames);
4276 c->c_varnames = varnames;
4277
4278 c->c_globals = PyDict_New();
4279 if (c->c_globals == NULL)
4280 return -1;
4281 c->c_freevars = PyDict_New();
4282 if (c->c_freevars == NULL)
4283 return -1;
4284 c->c_cellvars = PyDict_New();
4285 if (c->c_cellvars == NULL)
4286 return -1;
4287 return 0;
4288}
4289
4290struct symbol_info {
4291 int si_nlocals;
4292 int si_ncells;
4293 int si_nfrees;
4294 int si_nimplicit;
4295};
4296
4297static void
4298symtable_init_info(struct symbol_info *si)
4299{
4300 si->si_nlocals = 0;
4301 si->si_ncells = 0;
4302 si->si_nfrees = 0;
4303 si->si_nimplicit = 0;
4304}
4305
4306static int
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004307symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004308 struct symbol_info *si)
4309{
4310 PyObject *dict, *v;
4311
4312 /* Seperate logic for DEF_FREE. If it occurs in a function,
4313 it indicates a local that we must allocate storage for (a
4314 cell var). If it occurs in a class, then the class has a
4315 method and a free variable with the same name.
4316 */
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004317 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004318 /* If it isn't declared locally, it can't be a cell. */
4319 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
4320 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004321 v = PyInt_FromLong(si->si_ncells++);
4322 dict = c->c_cellvars;
4323 } else {
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004324 /* If it is free anyway, then there is no need to do
4325 anything here.
4326 */
4327 if (is_free(flags ^ DEF_FREE_CLASS)
Jeremy Hylton9c901052001-05-08 04:12:34 +00004328 || (flags == DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004329 return 0;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004330 v = PyInt_FromLong(si->si_nfrees++);
4331 dict = c->c_freevars;
4332 }
4333 if (v == NULL)
4334 return -1;
4335 if (PyDict_SetItem(dict, name, v) < 0) {
4336 Py_DECREF(v);
4337 return -1;
4338 }
4339 Py_DECREF(v);
4340 return 0;
4341}
4342
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004343/* If a variable is a cell and an argument, make sure that appears in
4344 co_cellvars before any variable to its right in varnames.
4345*/
4346
4347
4348static int
4349symtable_cellvar_offsets(PyObject **cellvars, int argcount,
4350 PyObject *varnames, int flags)
4351{
4352 PyObject *v, *w, *d, *list = NULL;
4353 int i, pos;
4354
4355 if (flags & CO_VARARGS)
4356 argcount++;
4357 if (flags & CO_VARKEYWORDS)
4358 argcount++;
4359 for (i = argcount; --i >= 0; ) {
4360 v = PyList_GET_ITEM(varnames, i);
4361 if (PyDict_GetItem(*cellvars, v)) {
4362 if (list == NULL) {
4363 list = PyList_New(1);
4364 if (list == NULL)
4365 return -1;
4366 PyList_SET_ITEM(list, 0, v);
4367 Py_INCREF(v);
4368 } else
4369 PyList_Insert(list, 0, v);
4370 }
4371 }
4372 if (list == NULL || PyList_GET_SIZE(list) == 0)
4373 return 0;
4374 /* There are cellvars that are also arguments. Create a dict
4375 to replace cellvars and put the args at the front.
4376 */
4377 d = PyDict_New();
4378 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
4379 v = PyInt_FromLong(i);
4380 if (v == NULL)
4381 goto fail;
4382 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
4383 goto fail;
4384 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
4385 goto fail;
4386 }
4387 pos = 0;
4388 i = PyList_GET_SIZE(list);
4389 Py_DECREF(list);
4390 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
4391 w = PyInt_FromLong(i++); /* don't care about the old key */
4392 if (PyDict_SetItem(d, v, w) < 0) {
4393 Py_DECREF(w);
4394 goto fail;
4395 }
4396 Py_DECREF(w);
4397 }
4398 Py_DECREF(*cellvars);
4399 *cellvars = d;
4400 return 1;
4401 fail:
4402 Py_DECREF(d);
4403 return -1;
4404}
4405
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004406static int
4407symtable_freevar_offsets(PyObject *freevars, int offset)
4408{
4409 PyObject *name, *v;
4410 int pos;
4411
4412 /* The cell vars are the first elements of the closure,
4413 followed by the free vars. Update the offsets in
4414 c_freevars to account for number of cellvars. */
4415 pos = 0;
4416 while (PyDict_Next(freevars, &pos, &name, &v)) {
4417 int i = PyInt_AS_LONG(v) + offset;
4418 PyObject *o = PyInt_FromLong(i);
4419 if (o == NULL)
4420 return -1;
4421 if (PyDict_SetItem(freevars, name, o) < 0) {
4422 Py_DECREF(o);
4423 return -1;
4424 }
4425 Py_DECREF(o);
4426 }
4427 return 0;
4428}
4429
4430static int
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004431symtable_check_unoptimized(struct compiling *c,
4432 PySymtableEntryObject *ste,
4433 struct symbol_info *si)
4434{
4435 char buf[300];
4436
4437 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
4438 || (ste->ste_nested && si->si_nimplicit)))
4439 return 0;
4440
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004441#define ILLEGAL_CONTAINS "contains a nested function with free variables"
4442
4443#define ILLEGAL_IS "is a nested function"
4444
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004445#define ILLEGAL_IMPORT_STAR \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004446"import * is not allowed in function '%.100s' because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004447
4448#define ILLEGAL_BARE_EXEC \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004449"unqualified exec is not allowed in function '%.100s' it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004450
4451#define ILLEGAL_EXEC_AND_IMPORT_STAR \
4452"function '%.100s' uses import * and bare exec, which are illegal" \
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004453"because it %s"
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004454
4455 /* XXX perhaps the linenos for these opt-breaking statements
4456 should be stored so the exception can point to them. */
4457
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004458 if (ste->ste_child_free) {
4459 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004460 PyOS_snprintf(buf, sizeof(buf),
4461 ILLEGAL_IMPORT_STAR,
4462 PyString_AS_STRING(ste->ste_name),
4463 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004464 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004465 PyOS_snprintf(buf, sizeof(buf),
4466 ILLEGAL_BARE_EXEC,
4467 PyString_AS_STRING(ste->ste_name),
4468 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004469 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004470 PyOS_snprintf(buf, sizeof(buf),
4471 ILLEGAL_EXEC_AND_IMPORT_STAR,
4472 PyString_AS_STRING(ste->ste_name),
4473 ILLEGAL_CONTAINS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004474 }
4475 } else {
4476 if (ste->ste_optimized == OPT_IMPORT_STAR)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004477 PyOS_snprintf(buf, sizeof(buf),
4478 ILLEGAL_IMPORT_STAR,
4479 PyString_AS_STRING(ste->ste_name),
4480 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004481 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004482 PyOS_snprintf(buf, sizeof(buf),
4483 ILLEGAL_BARE_EXEC,
4484 PyString_AS_STRING(ste->ste_name),
4485 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004486 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00004487 PyOS_snprintf(buf, sizeof(buf),
4488 ILLEGAL_EXEC_AND_IMPORT_STAR,
4489 PyString_AS_STRING(ste->ste_name),
4490 ILLEGAL_IS);
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004491 }
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004492 }
Jeremy Hylton280e6bd2001-03-22 03:51:05 +00004493
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004494 PyErr_SetString(PyExc_SyntaxError, buf);
4495 PyErr_SyntaxLocation(c->c_symtable->st_filename,
4496 ste->ste_opt_lineno);
4497 return -1;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004498}
4499
4500static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004501symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
4502 struct symbol_info *si)
4503{
Jeremy Hyltonb857ba22001-08-10 21:41:33 +00004504 if (c->c_future)
4505 c->c_flags |= c->c_future->ff_features;
Tim Peters5ca576e2001-06-18 22:08:13 +00004506 if (ste->ste_generator)
4507 c->c_flags |= CO_GENERATOR;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004508 if (ste->ste_type != TYPE_MODULE)
4509 c->c_flags |= CO_NEWLOCALS;
4510 if (ste->ste_type == TYPE_FUNCTION) {
4511 c->c_nlocals = si->si_nlocals;
4512 if (ste->ste_optimized == 0)
4513 c->c_flags |= CO_OPTIMIZED;
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004514 else if (ste->ste_optimized != OPT_EXEC)
4515 return symtable_check_unoptimized(c, ste, si);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004516 }
4517 return 0;
4518}
4519
4520static int
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004521symtable_load_symbols(struct compiling *c)
4522{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004523 static PyObject *implicit = NULL;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004524 struct symtable *st = c->c_symtable;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004525 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004526 PyObject *name, *varnames, *v;
4527 int i, flags, pos;
4528 struct symbol_info si;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004529
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004530 if (implicit == NULL) {
4531 implicit = PyInt_FromLong(1);
4532 if (implicit == NULL)
4533 return -1;
4534 }
4535 v = NULL;
4536
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004537 if (symtable_init_compiling_symbols(c) < 0)
4538 goto fail;
4539 symtable_init_info(&si);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004540 varnames = st->st_cur->ste_varnames;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004541 si.si_nlocals = PyList_GET_SIZE(varnames);
4542 c->c_argcount = si.si_nlocals;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004543
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004544 for (i = 0; i < si.si_nlocals; ++i) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004545 v = PyInt_FromLong(i);
4546 if (PyDict_SetItem(c->c_locals,
4547 PyList_GET_ITEM(varnames, i), v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004548 goto fail;
4549 Py_DECREF(v);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004550 }
4551
4552 /* XXX The cases below define the rules for whether a name is
4553 local or global. The logic could probably be clearer. */
4554 pos = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004555 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
4556 flags = PyInt_AS_LONG(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004557
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004558 if (flags & DEF_FREE_GLOBAL)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004559 /* undo the original DEF_FREE */
4560 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004561
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004562 /* Deal with names that need two actions:
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004563 1. Cell variables that are also locals.
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004564 2. Free variables in methods that are also class
4565 variables or declared global.
4566 */
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004567 if (flags & (DEF_FREE | DEF_FREE_CLASS))
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004568 symtable_resolve_free(c, name, flags, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004569
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004570 if (flags & DEF_STAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004571 c->c_argcount--;
4572 c->c_flags |= CO_VARARGS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004573 } else if (flags & DEF_DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004574 c->c_argcount--;
4575 c->c_flags |= CO_VARKEYWORDS;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004576 } else if (flags & DEF_INTUPLE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004577 c->c_argcount--;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004578 else if (flags & DEF_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004579 if (flags & DEF_PARAM) {
4580 PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL,
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004581 PyString_AS_STRING(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004582 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004583 ste->ste_lineno);
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004584 st->st_errors++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004585 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004586 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004587 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4588 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004589 } else if (flags & DEF_FREE_GLOBAL) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004590 si.si_nimplicit++;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004591 if (PyDict_SetItem(c->c_globals, name, implicit) < 0)
4592 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004593 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004594 v = PyInt_FromLong(si.si_nlocals++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004595 if (v == NULL)
4596 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004597 if (PyDict_SetItem(c->c_locals, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004598 goto fail;
4599 Py_DECREF(v);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004600 if (ste->ste_type != TYPE_CLASS)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004601 if (PyList_Append(c->c_varnames, name) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004602 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004603 } else if (is_free(flags)) {
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004604 if (ste->ste_nested) {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004605 v = PyInt_FromLong(si.si_nfrees++);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004606 if (v == NULL)
4607 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004608 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004609 goto fail;
4610 Py_DECREF(v);
4611 } else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004612 si.si_nimplicit++;
Jeremy Hylton1abf6102001-08-11 21:51:24 +00004613 if (PyDict_SetItem(c->c_globals, name,
4614 implicit) < 0)
4615 goto fail;
4616 if (st->st_nscopes != 1) {
4617 v = PyInt_FromLong(flags);
4618 if (PyDict_SetItem(st->st_global,
4619 name, v))
4620 goto fail;
4621 Py_DECREF(v);
4622 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004623 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004624 }
4625 }
4626
Jeremy Hyltonddc4fd02001-04-27 02:29:40 +00004627 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
4628
Jeremy Hylton220ae7c2001-03-21 16:43:47 +00004629 if (si.si_ncells > 1) { /* one cell is always in order */
4630 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
4631 c->c_varnames, c->c_flags) < 0)
4632 return -1;
4633 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004634 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
4635 return -1;
4636 return symtable_update_flags(c, ste, &si);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004637 fail:
4638 /* is this always the right thing to do? */
4639 Py_XDECREF(v);
4640 return -1;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004641}
4642
4643static struct symtable *
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004644symtable_init()
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004645{
4646 struct symtable *st;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004647
4648 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
4649 if (st == NULL)
4650 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004651 st->st_pass = 1;
Tim Petersff1f8522001-08-11 01:06:35 +00004652
Jeremy Hylton5acc0c02001-02-02 20:01:10 +00004653 st->st_filename = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004654 if ((st->st_stack = PyList_New(0)) == NULL)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004655 goto fail;
4656 if ((st->st_symbols = PyDict_New()) == NULL)
4657 goto fail;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004658 st->st_cur = NULL;
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004659 st->st_nscopes = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004660 st->st_errors = 0;
4661 st->st_tmpname = 0;
4662 st->st_private = NULL;
4663 return st;
4664 fail:
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004665 PySymtable_Free(st);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004666 return NULL;
4667}
4668
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004669void
4670PySymtable_Free(struct symtable *st)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004671{
4672 Py_XDECREF(st->st_symbols);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004673 Py_XDECREF(st->st_stack);
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004674 Py_XDECREF(st->st_cur);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004675 PyMem_Free((void *)st);
4676}
4677
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004678/* When the compiler exits a scope, it must should update the scope's
4679 free variable information with the list of free variables in its
4680 children.
4681
4682 Variables that are free in children and defined in the current
4683 scope are cellvars.
4684
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004685 If the scope being exited is defined at the top-level (ste_nested is
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004686 false), free variables in children that are not defined here are
4687 implicit globals.
4688
4689*/
4690
4691static int
4692symtable_update_free_vars(struct symtable *st)
4693{
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004694 int i, j, def;
4695 PyObject *o, *name, *list = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004696 PySymtableEntryObject *child, *ste = st->st_cur;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004697
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004698 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004699 def = DEF_FREE_CLASS;
4700 else
4701 def = DEF_FREE;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004702 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004703 int pos = 0;
4704
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004705 if (list)
4706 PyList_SetSlice(list, 0,
4707 ((PyVarObject*)list)->ob_size, 0);
Barry Warsaw0372af72001-02-23 18:22:59 +00004708 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004709 PyList_GET_ITEM(ste->ste_children, i);
4710 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004711 int flags = PyInt_AS_LONG(o);
4712 if (!(is_free(flags)))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004713 continue; /* avoids indentation */
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004714 if (list == NULL) {
4715 list = PyList_New(0);
4716 if (list == NULL)
4717 return -1;
4718 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004719 ste->ste_child_free = 1;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004720 if (PyList_Append(list, name) < 0) {
4721 Py_DECREF(list);
4722 return -1;
4723 }
4724 }
4725 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004726 PyObject *v;
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004727 name = PyList_GET_ITEM(list, j);
Jeremy Hylton78891072001-03-01 06:09:34 +00004728 v = PyDict_GetItem(ste->ste_symbols, name);
4729 /* If a name N is declared global in scope A and
4730 referenced in scope B contained (perhaps
4731 indirectly) in A and there are no scopes
4732 with bindings for N between B and A, then N
Jeremy Hylton9c901052001-05-08 04:12:34 +00004733 is global in B. Unless A is a class scope,
4734 because class scopes are not considered for
4735 nested scopes.
Jeremy Hylton78891072001-03-01 06:09:34 +00004736 */
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00004737 if (v && (ste->ste_type != TYPE_CLASS)) {
Jeremy Hylton78891072001-03-01 06:09:34 +00004738 int flags = PyInt_AS_LONG(v);
4739 if (flags & DEF_GLOBAL) {
4740 symtable_undo_free(st, child->ste_id,
4741 name);
4742 continue;
4743 }
4744 }
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004745 if (ste->ste_nested) {
4746 if (symtable_add_def_o(st, ste->ste_symbols,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004747 name, def) < 0) {
4748 Py_DECREF(list);
4749 return -1;
4750 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004751 } else {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004752 if (symtable_check_global(st, child->ste_id,
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004753 name) < 0) {
4754 Py_DECREF(list);
4755 return -1;
4756 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004757 }
4758 }
4759 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004760
4761 Py_XDECREF(list);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004762 return 0;
4763}
4764
4765/* If the current scope is a non-nested class or if name is not
4766 defined in the current, non-nested scope, then it is an implicit
4767 global in all nested scopes.
4768*/
4769
4770static int
4771symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
4772{
4773 PyObject *o;
4774 int v;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004775 PySymtableEntryObject *ste = st->st_cur;
Jeremy Hylton78891072001-03-01 06:09:34 +00004776
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004777 if (ste->ste_type == TYPE_CLASS)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004778 return symtable_undo_free(st, child, name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004779 o = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004780 if (o == NULL)
4781 return symtable_undo_free(st, child, name);
4782 v = PyInt_AS_LONG(o);
Jeremy Hylton78891072001-03-01 06:09:34 +00004783
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004784 if (is_free(v) || (v & DEF_GLOBAL))
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004785 return symtable_undo_free(st, child, name);
4786 else
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004787 return symtable_add_def_o(st, ste->ste_symbols,
4788 name, DEF_FREE);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004789}
4790
4791static int
4792symtable_undo_free(struct symtable *st, PyObject *id,
4793 PyObject *name)
4794{
4795 int i, v, x;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004796 PyObject *info;
4797 PySymtableEntryObject *ste;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004798
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004799 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
4800 if (ste == NULL)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004801 return -1;
Jeremy Hylton78891072001-03-01 06:09:34 +00004802
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004803 info = PyDict_GetItem(ste->ste_symbols, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004804 if (info == NULL)
4805 return 0;
4806 v = PyInt_AS_LONG(info);
4807 if (is_free(v)) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004808 if (symtable_add_def_o(st, ste->ste_symbols, name,
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004809 DEF_FREE_GLOBAL) < 0)
4810 return -1;
4811 } else
4812 /* If the name is defined here or declared global,
4813 then the recursion stops. */
4814 return 0;
4815
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004816 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
4817 PySymtableEntryObject *child;
Barry Warsaw0372af72001-02-23 18:22:59 +00004818 child = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004819 PyList_GET_ITEM(ste->ste_children, i);
4820 x = symtable_undo_free(st, child->ste_id, name);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004821 if (x < 0)
4822 return x;
4823 }
4824 return 0;
4825}
4826
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004827/* symtable_enter_scope() gets a reference via PySymtableEntry_New().
4828 This reference is released when the scope is exited, via the DECREF
4829 in symtable_exit_scope().
4830*/
4831
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004832static int
4833symtable_exit_scope(struct symtable *st)
4834{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004835 int end;
4836
Jeremy Hylton4419ac12001-02-28 22:54:51 +00004837 if (st->st_pass == 1)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004838 symtable_update_free_vars(st);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004839 Py_DECREF(st->st_cur);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004840 end = PyList_GET_SIZE(st->st_stack) - 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004841 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
4842 end);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004843 if (PySequence_DelItem(st->st_stack, end) < 0)
4844 return -1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004845 return 0;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004846}
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004847
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004848static void
4849symtable_enter_scope(struct symtable *st, char *name, int type,
4850 int lineno)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004851{
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004852 PySymtableEntryObject *prev = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004853
4854 if (st->st_cur) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004855 prev = st->st_cur;
4856 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
4857 Py_DECREF(st->st_cur);
Jeremy Hylton4b38da62001-02-02 18:19:15 +00004858 st->st_errors++;
4859 return;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004860 }
4861 }
Barry Warsaw0372af72001-02-23 18:22:59 +00004862 st->st_cur = (PySymtableEntryObject *)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004863 PySymtableEntry_New(st, name, type, lineno);
4864 if (strcmp(name, TOP) == 0)
4865 st->st_global = st->st_cur->ste_symbols;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004866 if (prev && st->st_pass == 1) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004867 if (PyList_Append(prev->ste_children,
4868 (PyObject *)st->st_cur) < 0)
4869 st->st_errors++;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +00004870 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004871}
4872
4873static int
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004874symtable_lookup(struct symtable *st, char *name)
4875{
4876 char buffer[MANGLE_LEN];
4877 PyObject *v;
4878 int flags;
4879
4880 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4881 name = buffer;
4882 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
4883 if (v == NULL) {
4884 if (PyErr_Occurred())
4885 return -1;
4886 else
4887 return 0;
4888 }
4889
4890 flags = PyInt_AS_LONG(v);
4891 return flags;
4892}
4893
4894static int
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004895symtable_add_def(struct symtable *st, char *name, int flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004896{
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004897 PyObject *s;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004898 char buffer[MANGLE_LEN];
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004899 int ret;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004900
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004901 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004902 name = buffer;
4903 if ((s = PyString_InternFromString(name)) == NULL)
4904 return -1;
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00004905 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
4906 Py_DECREF(s);
4907 return ret;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004908}
4909
4910/* Must only be called with mangled names */
4911
4912static int
4913symtable_add_def_o(struct symtable *st, PyObject *dict,
4914 PyObject *name, int flag)
4915{
4916 PyObject *o;
4917 int val;
4918
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004919 if ((o = PyDict_GetItem(dict, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004920 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004921 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00004922 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Jeremy Hylton483638c2001-02-01 20:20:45 +00004923 PyString_AsString(name));
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00004924 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004925 st->st_cur->ste_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004926 return -1;
4927 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004928 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004929 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004930 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004931 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004932 if (PyDict_SetItem(dict, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004933 Py_DECREF(o);
4934 return -1;
4935 }
4936 Py_DECREF(o);
4937
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004938 if (flag & DEF_PARAM) {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004939 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004940 return -1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004941 } else if (flag & DEF_GLOBAL) {
4942 /* XXX need to update DEF_GLOBAL for other flags too;
4943 perhaps only DEF_FREE_GLOBAL */
4944 if ((o = PyDict_GetItem(st->st_global, name))) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004945 val = PyInt_AS_LONG(o);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004946 val |= flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004947 } else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004948 val = flag;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004949 o = PyInt_FromLong(val);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004950 if (PyDict_SetItem(st->st_global, name, o) < 0) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004951 Py_DECREF(o);
4952 return -1;
4953 }
4954 Py_DECREF(o);
4955 }
4956 return 0;
4957}
4958
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004959#define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004960
Tim Peters08a898f2001-06-28 01:52:22 +00004961/* Look for a yield stmt under n. Return 1 if found, else 0.
4962 This hack is used to look inside "if 0:" blocks (which are normally
4963 ignored) in case those are the only places a yield occurs (so that this
4964 function is a generator). */
Tim Petersb6c3cea2001-06-26 03:36:28 +00004965static int
4966look_for_yield(node *n)
4967{
4968 int i;
4969
4970 for (i = 0; i < NCH(n); ++i) {
4971 node *kid = CHILD(n, i);
4972
4973 switch (TYPE(kid)) {
4974
4975 case classdef:
4976 case funcdef:
Tim Peters08a898f2001-06-28 01:52:22 +00004977 case lambdef:
Tim Petersb6c3cea2001-06-26 03:36:28 +00004978 /* Stuff in nested functions and classes can't make
4979 the parent a generator. */
4980 return 0;
4981
4982 case yield_stmt:
4983 return 1;
4984
4985 default:
4986 if (look_for_yield(kid))
4987 return 1;
4988 }
4989 }
4990 return 0;
4991}
4992
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004993static void
4994symtable_node(struct symtable *st, node *n)
4995{
Jeremy Hylton961dfe02001-10-18 16:15:10 +00004996 int i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004997
4998 loop:
4999 switch (TYPE(n)) {
5000 case funcdef: {
5001 char *func_name = STR(CHILD(n, 1));
5002 symtable_add_def(st, func_name, DEF_LOCAL);
5003 symtable_default_args(st, CHILD(n, 2));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005004 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005005 symtable_funcdef(st, n);
5006 symtable_exit_scope(st);
5007 break;
5008 }
5009 case lambdef:
5010 if (NCH(n) == 4)
5011 symtable_default_args(st, CHILD(n, 1));
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005012 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005013 symtable_funcdef(st, n);
5014 symtable_exit_scope(st);
5015 break;
5016 case classdef: {
5017 char *tmp, *class_name = STR(CHILD(n, 1));
5018 symtable_add_def(st, class_name, DEF_LOCAL);
5019 if (TYPE(CHILD(n, 2)) == LPAR) {
5020 node *bases = CHILD(n, 3);
5021 int i;
5022 for (i = 0; i < NCH(bases); i += 2) {
5023 symtable_node(st, CHILD(bases, i));
5024 }
5025 }
Jeremy Hylton4b38da62001-02-02 18:19:15 +00005026 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005027 tmp = st->st_private;
5028 st->st_private = class_name;
5029 symtable_node(st, CHILD(n, NCH(n) - 1));
5030 st->st_private = tmp;
5031 symtable_exit_scope(st);
5032 break;
5033 }
5034 case if_stmt:
5035 for (i = 0; i + 3 < NCH(n); i += 4) {
Tim Petersb6c3cea2001-06-26 03:36:28 +00005036 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
5037 if (st->st_cur->ste_generator == 0)
5038 st->st_cur->ste_generator =
5039 look_for_yield(CHILD(n, i+3));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005040 continue;
Tim Petersb6c3cea2001-06-26 03:36:28 +00005041 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005042 symtable_node(st, CHILD(n, i + 1));
5043 symtable_node(st, CHILD(n, i + 3));
5044 }
5045 if (i + 2 < NCH(n))
5046 symtable_node(st, CHILD(n, i + 2));
5047 break;
5048 case global_stmt:
5049 symtable_global(st, n);
5050 break;
5051 case import_stmt:
5052 symtable_import(st, n);
5053 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005054 case exec_stmt: {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005055 st->st_cur->ste_optimized |= OPT_EXEC;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005056 symtable_node(st, CHILD(n, 1));
5057 if (NCH(n) > 2)
5058 symtable_node(st, CHILD(n, 3));
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005059 else {
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005060 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005061 st->st_cur->ste_opt_lineno = n->n_lineno;
5062 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005063 if (NCH(n) > 4)
5064 symtable_node(st, CHILD(n, 5));
5065 break;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005066
5067 }
Jeremy Hylton384639f2001-02-19 15:50:51 +00005068 case assert_stmt:
5069 if (Py_OptimizeFlag)
5070 return;
5071 if (NCH(n) == 2) {
5072 n = CHILD(n, 1);
5073 goto loop;
5074 } else {
5075 symtable_node(st, CHILD(n, 1));
5076 n = CHILD(n, 3);
5077 goto loop;
5078 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005079 case except_clause:
5080 if (NCH(n) == 4)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005081 symtable_assign(st, CHILD(n, 3), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005082 if (NCH(n) > 1) {
5083 n = CHILD(n, 1);
5084 goto loop;
5085 }
5086 break;
5087 case del_stmt:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005088 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005089 break;
Tim Peters5ca576e2001-06-18 22:08:13 +00005090 case yield_stmt:
5091 st->st_cur->ste_generator = 1;
5092 n = CHILD(n, 1);
5093 goto loop;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005094 case expr_stmt:
5095 if (NCH(n) == 1)
5096 n = CHILD(n, 0);
5097 else {
5098 if (TYPE(CHILD(n, 1)) == augassign) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005099 symtable_assign(st, CHILD(n, 0), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005100 symtable_node(st, CHILD(n, 2));
5101 break;
5102 } else {
5103 int i;
5104 for (i = 0; i < NCH(n) - 2; i += 2)
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005105 symtable_assign(st, CHILD(n, i), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005106 n = CHILD(n, NCH(n) - 1);
5107 }
5108 }
5109 goto loop;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005110 case list_iter:
5111 n = CHILD(n, 0);
5112 if (TYPE(n) == list_for) {
5113 st->st_tmpname++;
5114 symtable_list_comprehension(st, n);
5115 st->st_tmpname--;
5116 } else {
5117 REQ(n, list_if);
5118 symtable_node(st, CHILD(n, 1));
5119 if (NCH(n) == 3) {
5120 n = CHILD(n, 2);
5121 goto loop;
5122 }
5123 }
5124 break;
5125 case for_stmt:
5126 symtable_assign(st, CHILD(n, 1), 0);
5127 for (i = 3; i < NCH(n); ++i)
5128 if (TYPE(CHILD(n, i)) >= single_input)
5129 symtable_node(st, CHILD(n, i));
5130 break;
5131 /* The remaining cases fall through to default except in
5132 special circumstances. This requires the individual cases
5133 to be coded with great care, even though they look like
5134 rather innocuous. Each case must double-check TYPE(n).
5135 */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005136 case argument:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005137 if (TYPE(n) == argument && NCH(n) == 3) {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005138 n = CHILD(n, 2);
5139 goto loop;
5140 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005141 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005142 case listmaker:
5143 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
Jeremy Hylton23b42272001-03-19 20:38:06 +00005144 st->st_tmpname++;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005145 symtable_list_comprehension(st, CHILD(n, 1));
Jeremy Hylton23b42272001-03-19 20:38:06 +00005146 symtable_node(st, CHILD(n, 0));
5147 st->st_tmpname--;
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005148 break;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005149 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005150 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005151 case atom:
5152 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
5153 symtable_add_use(st, STR(CHILD(n, 0)));
5154 break;
5155 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005156 /* fall through */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005157 default:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005158 /* Walk over every non-token child with a special case
5159 for one child.
5160 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005161 if (NCH(n) == 1) {
5162 n = CHILD(n, 0);
5163 goto loop;
5164 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005165 for (i = 0; i < NCH(n); ++i)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005166 if (TYPE(CHILD(n, i)) >= single_input)
5167 symtable_node(st, CHILD(n, i));
5168 }
5169}
5170
5171static void
5172symtable_funcdef(struct symtable *st, node *n)
5173{
5174 node *body;
5175
5176 if (TYPE(n) == lambdef) {
5177 if (NCH(n) == 4)
5178 symtable_params(st, CHILD(n, 1));
5179 } else
5180 symtable_params(st, CHILD(n, 2));
5181 body = CHILD(n, NCH(n) - 1);
5182 symtable_node(st, body);
5183}
5184
5185/* The next two functions parse the argument tuple.
5186 symtable_default_arg() checks for names in the default arguments,
5187 which are references in the defining scope. symtable_params()
5188 parses the parameter names, which are defined in the function's
5189 body.
5190
5191 varargslist:
5192 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
5193 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
5194*/
5195
5196static void
5197symtable_default_args(struct symtable *st, node *n)
5198{
5199 node *c;
5200 int i;
5201
5202 if (TYPE(n) == parameters) {
5203 n = CHILD(n, 1);
5204 if (TYPE(n) == RPAR)
5205 return;
5206 }
5207 REQ(n, varargslist);
5208 for (i = 0; i < NCH(n); i += 2) {
5209 c = CHILD(n, i);
5210 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5211 break;
5212 }
5213 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
5214 symtable_node(st, CHILD(n, i));
5215 }
5216}
5217
5218static void
5219symtable_params(struct symtable *st, node *n)
5220{
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005221 int i, complex = -1, ext = 0;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005222 node *c = NULL;
5223
5224 if (TYPE(n) == parameters) {
5225 n = CHILD(n, 1);
5226 if (TYPE(n) == RPAR)
5227 return;
5228 }
5229 REQ(n, varargslist);
5230 for (i = 0; i < NCH(n); i += 2) {
5231 c = CHILD(n, i);
5232 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
5233 ext = 1;
5234 break;
5235 }
5236 if (TYPE(c) == test) {
5237 continue;
5238 }
5239 if (TYPE(CHILD(c, 0)) == NAME)
5240 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
5241 else {
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005242 char nbuf[30];
5243 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005244 symtable_add_def(st, nbuf, DEF_PARAM);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005245 complex = i;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005246 }
5247 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005248 if (ext) {
5249 c = CHILD(n, i);
5250 if (TYPE(c) == STAR) {
5251 i++;
5252 symtable_add_def(st, STR(CHILD(n, i)),
5253 DEF_PARAM | DEF_STAR);
5254 i += 2;
Jeremy Hylton1113cfc2001-01-23 00:50:52 +00005255 if (i >= NCH(n))
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005256 c = NULL;
5257 else
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005258 c = CHILD(n, i);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005259 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005260 if (c && TYPE(c) == DOUBLESTAR) {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005261 i++;
5262 symtable_add_def(st, STR(CHILD(n, i)),
5263 DEF_PARAM | DEF_DOUBLESTAR);
5264 }
5265 }
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005266 if (complex >= 0) {
5267 int j;
5268 for (j = 0; j <= complex; j++) {
5269 c = CHILD(n, j);
5270 if (TYPE(c) == COMMA)
Jeremy Hylton2b3f0ca2001-02-19 23:52:49 +00005271 c = CHILD(n, ++j);
5272 else if (TYPE(c) == EQUAL)
5273 c = CHILD(n, j += 3);
Jeremy Hyltona6ebc482001-01-25 17:01:49 +00005274 if (TYPE(CHILD(c, 0)) == LPAR)
5275 symtable_params_fplist(st, CHILD(c, 1));
5276 }
5277 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005278}
5279
5280static void
5281symtable_params_fplist(struct symtable *st, node *n)
5282{
5283 int i;
5284 node *c;
5285
5286 REQ(n, fplist);
5287 for (i = 0; i < NCH(n); i += 2) {
5288 c = CHILD(n, i);
5289 REQ(c, fpdef);
5290 if (NCH(c) == 1)
5291 symtable_add_def(st, STR(CHILD(c, 0)),
5292 DEF_PARAM | DEF_INTUPLE);
5293 else
5294 symtable_params_fplist(st, CHILD(c, 1));
5295 }
5296
5297}
5298
5299static void
5300symtable_global(struct symtable *st, node *n)
5301{
5302 int i;
5303
Jeremy Hylton9f324e92001-03-01 22:59:14 +00005304 /* XXX It might be helpful to warn about module-level global
5305 statements, but it's hard to tell the difference between
5306 module-level and a string passed to exec.
5307 */
Jeremy Hyltonc1761322001-02-28 23:44:45 +00005308
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005309 for (i = 1; i < NCH(n); i += 2) {
5310 char *name = STR(CHILD(n, i));
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005311 int flags;
5312
5313 flags = symtable_lookup(st, name);
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005314 if (flags < 0)
5315 continue;
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005316 if (flags && flags != DEF_GLOBAL) {
5317 char buf[500];
5318 if (flags & DEF_PARAM) {
5319 PyErr_Format(PyExc_SyntaxError,
5320 "name '%.400s' is local and global",
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005321 name);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005322 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005323 st->st_cur->ste_lineno);
5324 st->st_errors++;
5325 return;
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005326 }
5327 else {
5328 if (flags & DEF_LOCAL)
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005329 PyOS_snprintf(buf, sizeof(buf),
5330 GLOBAL_AFTER_ASSIGN,
5331 name);
Guido van Rossum0bba7f82001-02-28 21:55:38 +00005332 else
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005333 PyOS_snprintf(buf, sizeof(buf),
5334 GLOBAL_AFTER_USE, name);
Guido van Rossumee34ac12001-02-28 22:08:12 +00005335 symtable_warn(st, buf);
Jeremy Hyltonbe77cf72001-02-27 05:15:57 +00005336 }
5337 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005338 symtable_add_def(st, name, DEF_GLOBAL);
5339 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005340}
5341
5342static void
5343symtable_list_comprehension(struct symtable *st, node *n)
5344{
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005345 char tmpname[30];
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005346
Barry Warsaw8f6d8682001-11-28 21:10:39 +00005347 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005348 symtable_add_def(st, tmpname, DEF_LOCAL);
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005349 symtable_assign(st, CHILD(n, 1), 0);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005350 symtable_node(st, CHILD(n, 3));
5351 if (NCH(n) == 5)
5352 symtable_node(st, CHILD(n, 4));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005353}
5354
5355static void
5356symtable_import(struct symtable *st, node *n)
5357{
5358 int i;
Jeremy Hylton483638c2001-02-01 20:20:45 +00005359 /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)*
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005360 | 'from' dotted_name 'import'
5361 ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton483638c2001-02-01 20:20:45 +00005362 import_as_name: NAME [NAME NAME]
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005363 */
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005364 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005365 node *dotname = CHILD(n, 1);
5366 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
5367 /* check for bogus imports */
5368 if (n->n_lineno >= st->st_future->ff_last_lineno) {
5369 PyErr_SetString(PyExc_SyntaxError,
5370 LATE_FUTURE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +00005371 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton39e2f3f2001-02-28 01:58:08 +00005372 n->n_lineno);
5373 st->st_errors++;
5374 return;
5375 }
5376 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005377 if (TYPE(CHILD(n, 3)) == STAR) {
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005378 if (st->st_cur->ste_type != TYPE_MODULE) {
Jeremy Hylton6a53bd82001-08-06 20:34:25 +00005379 if (symtable_warn(st,
5380 "import * only allowed at module level") < 0)
5381 return;
Jeremy Hylton8a6f2952001-08-06 19:45:40 +00005382 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005383 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
Jeremy Hylton2e2cded2001-03-22 03:57:58 +00005384 st->st_cur->ste_opt_lineno = n->n_lineno;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005385 } else {
5386 for (i = 3; i < NCH(n); i += 2) {
5387 node *c = CHILD(n, i);
5388 if (NCH(c) > 1) /* import as */
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005389 symtable_assign(st, CHILD(c, 2),
5390 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005391 else
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005392 symtable_assign(st, CHILD(c, 0),
5393 DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005394 }
5395 }
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005396 } else {
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005397 for (i = 1; i < NCH(n); i += 2) {
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005398 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005399 }
5400 }
5401}
5402
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005403/* The third argument to symatble_assign() is a flag to be passed to
5404 symtable_add_def() if it is eventually called. The flag is useful
5405 to specify the particular type of assignment that should be
5406 recorded, e.g. an assignment caused by import.
5407 */
5408
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005409static void
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005410symtable_assign(struct symtable *st, node *n, int def_flag)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005411{
5412 node *tmp;
5413 int i;
5414
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005415 loop:
5416 switch (TYPE(n)) {
5417 case lambdef:
Jeremy Hyltoneab156f2001-01-30 01:24:43 +00005418 /* invalid assignment, e.g. lambda x:x=2. The next
5419 pass will catch this error. */
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005420 return;
5421 case power:
5422 if (NCH(n) > 2) {
5423 for (i = 2; i < NCH(n); ++i)
5424 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
5425 symtable_node(st, CHILD(n, i));
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005426 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005427 if (NCH(n) > 1) {
5428 symtable_node(st, CHILD(n, 0));
5429 symtable_node(st, CHILD(n, 1));
5430 } else {
5431 n = CHILD(n, 0);
5432 goto loop;
5433 }
5434 return;
5435 case listmaker:
Jeremy Hylton23b42272001-03-19 20:38:06 +00005436 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
5437 /* XXX This is an error, but the next pass
5438 will catch it. */
5439 return;
5440 } else {
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005441 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005442 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005443 }
5444 return;
5445 case exprlist:
5446 case testlist:
5447 if (NCH(n) == 1) {
5448 n = CHILD(n, 0);
5449 goto loop;
5450 }
5451 else {
5452 int i;
5453 for (i = 0; i < NCH(n); i += 2)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005454 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005455 return;
5456 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005457 case atom:
5458 tmp = CHILD(n, 0);
5459 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
5460 n = CHILD(n, 1);
5461 goto loop;
Jeremy Hylton897b8212001-03-23 14:08:38 +00005462 } else if (TYPE(tmp) == NAME) {
Jeremy Hylton778e2652001-11-09 19:50:08 +00005463 if (strcmp(STR(tmp), "__debug__") == 0) {
5464 PyErr_SetString(PyExc_SyntaxError,
5465 ASSIGN_DEBUG);
5466 PyErr_SyntaxLocation(st->st_filename,
Jeremy Hylton86424e32001-12-04 02:41:46 +00005467 n->n_lineno);
Jeremy Hylton778e2652001-11-09 19:50:08 +00005468 st->st_errors++;
5469 }
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005470 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
Jeremy Hylton897b8212001-03-23 14:08:38 +00005471 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005472 return;
5473 case dotted_as_name:
5474 if (NCH(n) == 3)
5475 symtable_add_def(st, STR(CHILD(n, 2)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005476 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005477 else
5478 symtable_add_def(st,
5479 STR(CHILD(CHILD(n,
5480 0), 0)),
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005481 DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005482 return;
5483 case dotted_name:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005484 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005485 return;
5486 case NAME:
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005487 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005488 return;
5489 default:
5490 if (NCH(n) == 0)
5491 return;
Jeremy Hylton17820c42001-02-19 15:33:10 +00005492 if (NCH(n) == 1) {
5493 n = CHILD(n, 0);
5494 goto loop;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005495 }
Jeremy Hylton17820c42001-02-19 15:33:10 +00005496 /* Should only occur for errors like x + 1 = 1,
5497 which will be caught in the next pass. */
5498 for (i = 0; i < NCH(n); ++i)
5499 if (TYPE(CHILD(n, i)) >= single_input)
Jeremy Hylton961dfe02001-10-18 16:15:10 +00005500 symtable_assign(st, CHILD(n, i), def_flag);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005501 }
5502}