Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1 | /* Compile an expression node to intermediate code */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | /* XXX TO DO: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4 | 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | XXX Generate simple jump for break/return outside 'try...finally' |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 7 | XXX Allow 'continue' inside finally clause of try-finally |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 8 | XXX New opcode for loading the initial index for a for loop |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 9 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 14 | #include "node.h" |
| 15 | #include "token.h" |
| 16 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 18 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
| 21 | |
| 22 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 24 | /* 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 Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 29 | #error "single_input has changed -- update Py_single_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 32 | #error "file_input has changed -- update Py_file_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | #endif |
| 34 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 35 | #error "eval_input has changed -- update Py_eval_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 38 | int Py_OptimizeFlag = 0; |
| 39 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 40 | #define OP_DELETE 0 |
| 41 | #define OP_ASSIGN 1 |
| 42 | #define OP_APPLY 2 |
| 43 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 44 | #define VAR_LOAD 0 |
| 45 | #define VAR_STORE 1 |
| 46 | #define VAR_DELETE 2 |
| 47 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 48 | #define DEL_CLOSURE_ERROR \ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 49 | "can not delete variable '%.400s' referenced in nested scope" |
| 50 | |
| 51 | #define DUPLICATE_ARGUMENT \ |
| 52 | "duplicate argument '%s' in function definition" |
| 53 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 | #define ILLEGAL_DYNAMIC_SCOPE \ |
| 55 | "%.100s: exec or 'import *' makes names ambiguous in nested scope" |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 57 | #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 | |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 63 | #define PARAM_GLOBAL \ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 64 | "name '%.400s' is a function parameter and declared global" |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 66 | #define LATE_FUTURE \ |
| 67 | "from __future__ imports must occur at the beginning of the file" |
| 68 | |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 69 | #define ASSIGN_DEBUG \ |
| 70 | "can not assign to __debug__" |
| 71 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 72 | #define MANGLE_LEN 256 |
| 73 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 76 | static PyMemberDef code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 77 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 78 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 79 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 81 | {"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 Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 84 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 85 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 86 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 87 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 89 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 90 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | {NULL} /* Sentinel */ |
| 92 | }; |
| 93 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(code_doc, |
| 95 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 96 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 97 | \n\ |
| 98 | Create a code object. Not for the faint of heart."); |
| 99 | |
| 100 | static PyObject * |
| 101 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 102 | { |
| 103 | int argcount; |
| 104 | int nlocals; |
| 105 | int stacksize; |
| 106 | int flags; |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 107 | PyObject *co; |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 108 | PyObject *empty = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 109 | PyObject *code; |
| 110 | PyObject *consts; |
| 111 | PyObject *names; |
| 112 | PyObject *varnames; |
| 113 | PyObject *freevars = NULL; |
| 114 | PyObject *cellvars = NULL; |
| 115 | PyObject *filename; |
| 116 | PyObject *name; |
| 117 | int firstlineno; |
| 118 | PyObject *lnotab; |
| 119 | |
| 120 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 121 | &argcount, &nlocals, &stacksize, &flags, |
| 122 | &code, |
| 123 | &PyTuple_Type, &consts, |
| 124 | &PyTuple_Type, &names, |
| 125 | &PyTuple_Type, &varnames, |
| 126 | &filename, &name, |
| 127 | &firstlineno, &lnotab, |
| 128 | &PyTuple_Type, &freevars, |
| 129 | &PyTuple_Type, &cellvars)) |
| 130 | return NULL; |
| 131 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 132 | if (!PyObject_CheckReadBuffer(code)) { |
| 133 | PyErr_SetString(PyExc_TypeError, |
| 134 | "bytecode object must be a single-segment read-only buffer"); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 138 | if (freevars == NULL || cellvars == NULL) { |
| 139 | empty = PyTuple_New(0); |
| 140 | if (empty == NULL) |
| 141 | return NULL; |
| 142 | if (freevars == NULL) |
| 143 | freevars = empty; |
| 144 | if (cellvars == NULL) |
| 145 | cellvars = empty; |
| 146 | } |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 147 | |
| 148 | co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 149 | code, consts, names, varnames, |
| 150 | freevars, cellvars, filename, name, |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 151 | firstlineno, lnotab); |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 152 | Py_XDECREF(empty); |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 153 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 156 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 159 | Py_XDECREF(co->co_code); |
| 160 | Py_XDECREF(co->co_consts); |
| 161 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 162 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 163 | Py_XDECREF(co->co_freevars); |
| 164 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 165 | Py_XDECREF(co->co_filename); |
| 166 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 167 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 168 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 171 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 172 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 173 | { |
| 174 | char buf[500]; |
| 175 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 176 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 177 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 179 | if (co->co_firstlineno != 0) |
| 180 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 182 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 183 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 184 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 185 | PyOS_snprintf(buf, sizeof(buf), |
| 186 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 187 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 188 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 191 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 192 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 193 | { |
| 194 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 195 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 196 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 197 | cmp = co->co_argcount - cp->co_argcount; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 198 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 199 | cmp = co->co_nlocals - cp->co_nlocals; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 200 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 201 | cmp = co->co_flags - cp->co_flags; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 202 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 203 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 204 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 206 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 207 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 208 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 209 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 210 | if (cmp) return cmp; |
| 211 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 212 | if (cmp) return cmp; |
| 213 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 214 | return cmp; |
| 215 | } |
| 216 | |
| 217 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 218 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 219 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 220 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 221 | h0 = PyObject_Hash(co->co_name); |
| 222 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 223 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 224 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 226 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 227 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 228 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 229 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 230 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 231 | h5 = PyObject_Hash(co->co_freevars); |
| 232 | if (h5 == -1) return -1; |
| 233 | h6 = PyObject_Hash(co->co_cellvars); |
| 234 | if (h6 == -1) return -1; |
| 235 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 236 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 237 | if (h == -1) h = -2; |
| 238 | return h; |
| 239 | } |
| 240 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 241 | /* XXX code objects need to participate in GC? */ |
| 242 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 243 | PyTypeObject PyCode_Type = { |
| 244 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 245 | 0, |
| 246 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 248 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 249 | (destructor)code_dealloc, /* tp_dealloc */ |
| 250 | 0, /* tp_print */ |
| 251 | 0, /* tp_getattr */ |
| 252 | 0, /* tp_setattr */ |
| 253 | (cmpfunc)code_compare, /* tp_compare */ |
| 254 | (reprfunc)code_repr, /* tp_repr */ |
| 255 | 0, /* tp_as_number */ |
| 256 | 0, /* tp_as_sequence */ |
| 257 | 0, /* tp_as_mapping */ |
| 258 | (hashfunc)code_hash, /* tp_hash */ |
| 259 | 0, /* tp_call */ |
| 260 | 0, /* tp_str */ |
| 261 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 262 | 0, /* tp_setattro */ |
| 263 | 0, /* tp_as_buffer */ |
| 264 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 265 | code_doc, /* tp_doc */ |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 266 | 0, /* tp_traverse */ |
| 267 | 0, /* tp_clear */ |
| 268 | 0, /* tp_richcompare */ |
| 269 | 0, /* tp_weaklistoffset */ |
| 270 | 0, /* tp_iter */ |
| 271 | 0, /* tp_iternext */ |
| 272 | 0, /* tp_methods */ |
| 273 | code_memberlist, /* tp_members */ |
| 274 | 0, /* tp_getset */ |
| 275 | 0, /* tp_base */ |
| 276 | 0, /* tp_dict */ |
| 277 | 0, /* tp_descr_get */ |
| 278 | 0, /* tp_descr_set */ |
| 279 | 0, /* tp_dictoffset */ |
| 280 | 0, /* tp_init */ |
| 281 | 0, /* tp_alloc */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 282 | code_new, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 285 | #define NAME_CHARS \ |
| 286 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 287 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 288 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 289 | |
| 290 | static int |
| 291 | all_name_chars(unsigned char *s) |
| 292 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 293 | static char ok_name_char[256]; |
| 294 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 296 | if (ok_name_char[*name_chars] == 0) { |
| 297 | unsigned char *p; |
| 298 | for (p = name_chars; *p; p++) |
| 299 | ok_name_char[*p] = 1; |
| 300 | } |
| 301 | while (*s) { |
| 302 | if (ok_name_char[*s++] == 0) |
| 303 | return 0; |
| 304 | } |
| 305 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 308 | static int |
| 309 | intern_strings(PyObject *tuple) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 314 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 315 | if (v == NULL || !PyString_Check(v)) { |
| 316 | Py_FatalError("non-string found in code slot"); |
| 317 | PyErr_BadInternalCall(); |
| 318 | return -1; |
| 319 | } |
| 320 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 325 | #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) |
| 326 | #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 327 | #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) |
| 328 | #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 329 | #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 |
| 330 | |
| 331 | static PyObject * |
| 332 | optimize_code(PyObject *code, PyObject* consts) |
| 333 | { |
| 334 | int i, j, codelen; |
| 335 | int tgt, tgttgt, opcode; |
| 336 | unsigned char *codestr; |
| 337 | |
| 338 | /* Make a modifiable copy of the code string */ |
| 339 | if (!PyString_Check(code)) |
| 340 | goto exitUnchanged; |
| 341 | codelen = PyString_Size(code); |
| 342 | codestr = PyMem_Malloc(codelen); |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 343 | if (codestr == NULL) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 344 | goto exitUnchanged; |
| 345 | codestr = memcpy(codestr, PyString_AS_STRING(code), codelen); |
| 346 | assert(PyTuple_Check(consts)); |
| 347 | |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 348 | for (i=0 ; i<codelen-7 ; i += HAS_ARG(codestr[i]) ? 3 : 1) { |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 349 | opcode = codestr[i]; |
| 350 | switch (opcode) { |
| 351 | |
| 352 | /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP. |
| 353 | Note, only the first opcode is changed, the others still |
| 354 | perform normally if they happen to be jump targets. */ |
| 355 | case LOAD_CONST: |
| 356 | j = GETARG(codestr, i); |
| 357 | if (codestr[i+3] != JUMP_IF_FALSE || |
| 358 | codestr[i+6] != POP_TOP || |
| 359 | !PyObject_IsTrue(PyTuple_GET_ITEM(consts, j))) |
| 360 | continue; |
| 361 | codestr[i] = JUMP_FORWARD; |
| 362 | SETARG(codestr, i, 4); |
| 363 | break; |
| 364 | |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 365 | /* Replace jumps to unconditional jumps */ |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 366 | case FOR_ITER: |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 367 | case JUMP_FORWARD: |
| 368 | case JUMP_IF_FALSE: |
| 369 | case JUMP_IF_TRUE: |
| 370 | case JUMP_ABSOLUTE: |
| 371 | case CONTINUE_LOOP: |
| 372 | case SETUP_LOOP: |
| 373 | case SETUP_EXCEPT: |
| 374 | case SETUP_FINALLY: |
| 375 | tgt = GETJUMPTGT(codestr, i); |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 376 | if (!UNCONDITIONAL_JUMP(codestr[tgt])) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 377 | continue; |
| 378 | tgttgt = GETJUMPTGT(codestr, tgt); |
| 379 | if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ |
| 380 | opcode = JUMP_ABSOLUTE; |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 381 | if (!ABSOLUTE_JUMP(opcode)) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 382 | tgttgt -= i + 3; /* Calc relative jump addr */ |
| 383 | if (tgttgt < 0) /* No backward relative jumps */ |
| 384 | continue; |
| 385 | codestr[i] = opcode; |
| 386 | SETARG(codestr, i, tgttgt); |
| 387 | break; |
| 388 | |
| 389 | case EXTENDED_ARG: |
| 390 | PyMem_Free(codestr); |
| 391 | goto exitUnchanged; |
| 392 | } |
| 393 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 394 | code = PyString_FromStringAndSize((char *)codestr, codelen); |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 395 | PyMem_Free(codestr); |
| 396 | return code; |
| 397 | |
| 398 | exitUnchanged: |
| 399 | Py_INCREF(code); |
| 400 | return code; |
| 401 | } |
| 402 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 403 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 404 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 405 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 406 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 407 | PyObject *filename, PyObject *name, int firstlineno, |
| 408 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 409 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 410 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 411 | int i; |
| 412 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 413 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 414 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 415 | consts == NULL || !PyTuple_Check(consts) || |
| 416 | names == NULL || !PyTuple_Check(names) || |
| 417 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 418 | freevars == NULL || !PyTuple_Check(freevars) || |
| 419 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 420 | name == NULL || !PyString_Check(name) || |
| 421 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 422 | lnotab == NULL || !PyString_Check(lnotab) || |
| 423 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 424 | PyErr_BadInternalCall(); |
| 425 | return NULL; |
| 426 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 427 | intern_strings(names); |
| 428 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 429 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 430 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 431 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 433 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 435 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 436 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 437 | continue; |
| 438 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 439 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 440 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 441 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 442 | co->co_argcount = argcount; |
| 443 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 444 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 445 | co->co_flags = flags; |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 446 | co->co_code = optimize_code(code, consts); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 447 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 448 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 449 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 450 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 451 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 452 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 453 | Py_INCREF(freevars); |
| 454 | co->co_freevars = freevars; |
| 455 | Py_INCREF(cellvars); |
| 456 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 457 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 458 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 459 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 460 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 461 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 462 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 463 | co->co_lnotab = lnotab; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 464 | if (PyTuple_GET_SIZE(freevars) == 0 && |
| 465 | PyTuple_GET_SIZE(cellvars) == 0) |
| 466 | co->co_flags |= CO_NOFREE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 467 | } |
| 468 | return co; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 473 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 474 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 475 | builds the symbol table. The second pass generates the bytecode. |
| 476 | |
| 477 | The first pass uses a single symtable struct. The second pass uses |
| 478 | a compiling struct for each code block. The compiling structs |
| 479 | share a reference to the symtable. |
| 480 | |
| 481 | The two passes communicate via symtable_load_symbols() and via |
| 482 | is_local() and is_global(). The former initializes several slots |
| 483 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 484 | c_argcount, c_globals, and c_flags. |
| 485 | */ |
| 486 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 487 | /* All about c_lnotab. |
| 488 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 489 | c_lnotab is an array of unsigned bytes disguised as a Python string. Since |
| 490 | version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are |
| 491 | mapped to source code line #s via c_lnotab instead. |
| 492 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 493 | The array is conceptually a list of |
| 494 | (bytecode offset increment, line number increment) |
| 495 | pairs. The details are important and delicate, best illustrated by example: |
| 496 | |
| 497 | byte code offset source code line number |
| 498 | 0 1 |
| 499 | 6 2 |
| 500 | 50 7 |
| 501 | 350 307 |
| 502 | 361 308 |
| 503 | |
| 504 | The first trick is that these numbers aren't stored, only the increments |
| 505 | from one row to the next (this doesn't really work, but it's a start): |
| 506 | |
| 507 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 508 | |
| 509 | The second trick is that an unsigned byte can't hold negative values, or |
| 510 | values larger than 255, so (a) there's a deep assumption that byte code |
| 511 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 512 | if at least one column jumps by more than 255 from one row to the next, more |
| 513 | than one pair is written to the table. In case #b, there's no way to know |
| 514 | from looking at the table later how many were written. That's the delicate |
| 515 | part. A user of c_lnotab desiring to find the source line number |
| 516 | corresponding to a bytecode address A should do something like this |
| 517 | |
| 518 | lineno = addr = 0 |
| 519 | for addr_incr, line_incr in c_lnotab: |
| 520 | addr += addr_incr |
| 521 | if addr > A: |
| 522 | return lineno |
| 523 | lineno += line_incr |
| 524 | |
| 525 | In order for this to work, when the addr field increments by more than 255, |
| 526 | the line # increment in each pair generated must be 0 until the remaining addr |
| 527 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 528 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 529 | 255, 0, 45, 255, 0, 45. |
| 530 | */ |
| 531 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 532 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 533 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 534 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 535 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 536 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 537 | PyObject *c_name_dict; /* inverse of c_names */ |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 538 | PyObject *c_globals; /* dictionary (value=None or True) */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 539 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 540 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 541 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 542 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 543 | int c_nlocals; /* index of next local */ |
| 544 | int c_argcount; /* number of top-level arguments */ |
| 545 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 546 | int c_nexti; /* index into c_code */ |
| 547 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 548 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 549 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 550 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 551 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 552 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 553 | int c_nblocks; /* current block stack level */ |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 554 | const char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 555 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 556 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 557 | int c_stacklevel; /* Current stack level */ |
| 558 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 559 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 560 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 561 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 562 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 563 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 564 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 565 | int c_closure; /* Is nested w/freevars? */ |
| 566 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 567 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 568 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 569 | }; |
| 570 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 571 | static int |
| 572 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 573 | { |
| 574 | if ((v & (USE | DEF_FREE)) |
| 575 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 576 | return 1; |
| 577 | if (v & DEF_FREE_CLASS) |
| 578 | return 1; |
| 579 | return 0; |
| 580 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 581 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 582 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 583 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 584 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 585 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 586 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 587 | if (c == NULL) { |
| 588 | /* Error occurred via symtable call to |
| 589 | is_constant_false */ |
| 590 | PyErr_SetString(exc, msg); |
| 591 | return; |
| 592 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 593 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 594 | if (c->c_lineno < 1 || c->c_interactive) { |
| 595 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 596 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 599 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 600 | if (v == NULL) |
| 601 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 602 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 603 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 604 | if (line == NULL) { |
| 605 | Py_INCREF(Py_None); |
| 606 | line = Py_None; |
| 607 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 608 | if (exc == PyExc_SyntaxError) { |
| 609 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 610 | Py_None, line); |
| 611 | if (t == NULL) |
| 612 | goto exit; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 613 | w = PyTuple_Pack(2, v, t); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 614 | if (w == NULL) |
| 615 | goto exit; |
| 616 | PyErr_SetObject(exc, w); |
| 617 | } else { |
| 618 | /* Make sure additional exceptions are printed with |
| 619 | file and line, also. */ |
| 620 | PyErr_SetObject(exc, v); |
| 621 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 622 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 623 | exit: |
| 624 | Py_XDECREF(t); |
| 625 | Py_XDECREF(v); |
| 626 | Py_XDECREF(w); |
| 627 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 630 | /* Interface to the block stack */ |
| 631 | |
| 632 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 633 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 634 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 635 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 636 | com_error(c, PyExc_SystemError, |
| 637 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 638 | } |
| 639 | else { |
| 640 | c->c_block[c->c_nblocks++] = type; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 645 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 646 | { |
| 647 | if (c->c_nblocks > 0) |
| 648 | c->c_nblocks--; |
| 649 | if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 650 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 654 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 655 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 656 | static int issue_warning(const char *, const char *, int); |
| 657 | static int com_init(struct compiling *, const char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 658 | static void com_free(struct compiling *); |
| 659 | static void com_push(struct compiling *, int); |
| 660 | static void com_pop(struct compiling *, int); |
| 661 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 662 | static void com_node(struct compiling *, node *); |
| 663 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 664 | static void com_addbyte(struct compiling *, int); |
| 665 | static void com_addint(struct compiling *, int); |
| 666 | static void com_addoparg(struct compiling *, int, int); |
| 667 | static void com_addfwref(struct compiling *, int, int *); |
| 668 | static void com_backpatch(struct compiling *, int); |
| 669 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 670 | static int com_addconst(struct compiling *, PyObject *); |
| 671 | static int com_addname(struct compiling *, PyObject *); |
| 672 | static void com_addopname(struct compiling *, int, node *); |
| 673 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 674 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 675 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 676 | static void com_assign(struct compiling *, node *, int, node *); |
| 677 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 678 | static PyCodeObject *icompile(node *, struct compiling *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 679 | static PyCodeObject *jcompile(node *, const char *, struct compiling *, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 680 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 681 | static PyObject *parsestrplus(struct compiling*, node *); |
| 682 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 683 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 684 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 685 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 686 | |
| 687 | /* symtable operations */ |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 688 | static struct symtable *symtable_build(node *, PyFutureFeatures *, |
| 689 | const char *filename); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 690 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 691 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 692 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 693 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 694 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 695 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 696 | |
| 697 | static void symtable_node(struct symtable *, node *); |
| 698 | static void symtable_funcdef(struct symtable *, node *); |
| 699 | static void symtable_default_args(struct symtable *, node *); |
| 700 | static void symtable_params(struct symtable *, node *); |
| 701 | static void symtable_params_fplist(struct symtable *, node *n); |
| 702 | static void symtable_global(struct symtable *, node *); |
| 703 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 704 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 705 | static void symtable_list_comprehension(struct symtable *, node *); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 706 | static void symtable_list_for(struct symtable *, node *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 707 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 708 | static int symtable_update_free_vars(struct symtable *); |
| 709 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 710 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 711 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 712 | /* helper */ |
| 713 | static void |
| 714 | do_pad(int pad) |
| 715 | { |
| 716 | int i; |
| 717 | for (i = 0; i < pad; ++i) |
| 718 | fprintf(stderr, " "); |
| 719 | } |
| 720 | |
| 721 | static void |
| 722 | dump(node *n, int pad, int depth) |
| 723 | { |
| 724 | int i; |
| 725 | if (depth == 0) |
| 726 | return; |
| 727 | do_pad(pad); |
| 728 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 729 | if (depth > 0) |
| 730 | depth--; |
| 731 | for (i = 0; i < NCH(n); ++i) |
| 732 | dump(CHILD(n, i), pad + 1, depth); |
| 733 | } |
| 734 | |
| 735 | #define DUMP(N) dump(N, 0, -1) |
| 736 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 737 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 738 | com_init(struct compiling *c, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 739 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 740 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 741 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 742 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 743 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 744 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 745 | goto fail; |
| 746 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 747 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 748 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 749 | goto fail; |
| 750 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 751 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 752 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 753 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 754 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 755 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 756 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 757 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 758 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 759 | c->c_freevars = NULL; |
| 760 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 761 | c->c_nlocals = 0; |
| 762 | c->c_argcount = 0; |
| 763 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 764 | c->c_nexti = 0; |
| 765 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 766 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 767 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 768 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 769 | c->c_begin = 0; |
| 770 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 771 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 772 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 773 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 774 | c->c_stacklevel = 0; |
| 775 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 776 | c->c_firstlineno = 0; |
| 777 | c->c_last_addr = 0; |
| 778 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 779 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 780 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 781 | c->c_nested = 0; |
| 782 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 783 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 784 | return 1; |
| 785 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 786 | fail: |
| 787 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 792 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 793 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 794 | Py_XDECREF(c->c_code); |
| 795 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 796 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 797 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 798 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 799 | Py_XDECREF(c->c_globals); |
| 800 | Py_XDECREF(c->c_locals); |
| 801 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 802 | Py_XDECREF(c->c_freevars); |
| 803 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 804 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 805 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 806 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 810 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 811 | { |
| 812 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 813 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 814 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 815 | /* |
| 816 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 817 | c->c_filename, c->c_name, c->c_lineno, |
| 818 | c->c_nexti, c->c_stacklevel, n); |
| 819 | */ |
| 820 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 824 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 825 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 826 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 827 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 828 | else |
| 829 | c->c_stacklevel -= n; |
| 830 | } |
| 831 | |
| 832 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 833 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 834 | { |
| 835 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 836 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 837 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 838 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 841 | static int |
| 842 | com_check_size(PyObject **s, int offset) |
| 843 | { |
| 844 | int len = PyString_GET_SIZE(*s); |
| 845 | if (offset >= len) |
| 846 | return _PyString_Resize(s, len * 2); |
| 847 | return 0; |
| 848 | } |
| 849 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 850 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 851 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 852 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 853 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 854 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 855 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 856 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 857 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 858 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 859 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 860 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 864 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 865 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 866 | com_addbyte(c, x & 0xff); |
| 867 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 871 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 872 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 873 | char *p; |
| 874 | if (c->c_lnotab == NULL) |
| 875 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 876 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 877 | c->c_errors++; |
| 878 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 879 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 880 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 881 | *p++ = addr; |
| 882 | *p++ = line; |
| 883 | c->c_lnotab_next += 2; |
| 884 | } |
| 885 | |
| 886 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 887 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 888 | { |
| 889 | c->c_lineno = lineno; |
| 890 | if (c->c_firstlineno == 0) { |
| 891 | c->c_firstlineno = c->c_last_line = lineno; |
| 892 | } |
| 893 | else { |
| 894 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 895 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 896 | while (incr_addr > 255) { |
| 897 | com_add_lnotab(c, 255, 0); |
| 898 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 899 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 900 | while (incr_line > 255) { |
| 901 | com_add_lnotab(c, incr_addr, 255); |
| 902 | incr_line -=255; |
| 903 | incr_addr = 0; |
| 904 | } |
| 905 | if (incr_addr > 0 || incr_line > 0) |
| 906 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 907 | c->c_last_addr = c->c_nexti; |
| 908 | c->c_last_line = lineno; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 913 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 914 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 915 | int extended_arg = arg >> 16; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 916 | if (extended_arg){ |
| 917 | com_addbyte(c, EXTENDED_ARG); |
| 918 | com_addint(c, extended_arg); |
| 919 | arg &= 0xffff; |
| 920 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 921 | com_addbyte(c, op); |
| 922 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 926 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 927 | { |
| 928 | /* Compile a forward reference for backpatching */ |
| 929 | int here; |
| 930 | int anchor; |
| 931 | com_addbyte(c, op); |
| 932 | here = c->c_nexti; |
| 933 | anchor = *p_anchor; |
| 934 | *p_anchor = here; |
| 935 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 936 | } |
| 937 | |
| 938 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 939 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 940 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 941 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 942 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 943 | int dist; |
| 944 | int prev; |
| 945 | for (;;) { |
| 946 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 947 | prev = code[anchor] + (code[anchor+1] << 8); |
| 948 | dist = target - (anchor+2); |
| 949 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 950 | dist >>= 8; |
| 951 | code[anchor+1] = dist; |
| 952 | dist >>= 8; |
| 953 | if (dist) { |
| 954 | com_error(c, PyExc_SystemError, |
| 955 | "com_backpatch: offset too large"); |
| 956 | break; |
| 957 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 958 | if (!prev) |
| 959 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 960 | anchor -= prev; |
| 961 | } |
| 962 | } |
| 963 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 964 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 965 | |
| 966 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 967 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 968 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 969 | PyObject *w, *t, *np=NULL; |
| 970 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 971 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 972 | t = PyTuple_Pack(2, v, v->ob_type); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 973 | if (t == NULL) |
| 974 | goto fail; |
| 975 | w = PyDict_GetItem(dict, t); |
| 976 | if (w != NULL) { |
| 977 | n = PyInt_AsLong(w); |
| 978 | } else { |
| 979 | n = PyList_Size(list); |
| 980 | np = PyInt_FromLong(n); |
| 981 | if (np == NULL) |
| 982 | goto fail; |
| 983 | if (PyList_Append(list, v) != 0) |
| 984 | goto fail; |
| 985 | if (PyDict_SetItem(dict, t, np) != 0) |
| 986 | goto fail; |
| 987 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 988 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 989 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 990 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 991 | fail: |
| 992 | Py_XDECREF(np); |
| 993 | Py_XDECREF(t); |
| 994 | c->c_errors++; |
| 995 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 999 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1000 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1001 | return com_add(c, c->c_consts, c->c_const_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1005 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1006 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1007 | return com_add(c, c->c_names, c->c_name_dict, v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1010 | int |
| 1011 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1012 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1013 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1014 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1015 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1016 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 1017 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1018 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1019 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1020 | return 0; /* Don't mangle __extremely_long_names */ |
| 1021 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 1022 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1023 | /* Strip leading underscores from class name */ |
| 1024 | while (*p == '_') |
| 1025 | p++; |
| 1026 | if (*p == '\0') |
| 1027 | return 0; /* Don't mangle if class is just underscores */ |
| 1028 | plen = strlen(p); |
| 1029 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1030 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 1031 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1032 | buffer[0] = '_'; |
| 1033 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1034 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1035 | return 1; |
| 1036 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1037 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1038 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1039 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1040 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1041 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1042 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1043 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1044 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1045 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1046 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 1047 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1048 | c->c_errors++; |
| 1049 | i = 255; |
| 1050 | } |
| 1051 | else { |
| 1052 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1053 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1054 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1055 | com_addoparg(c, op, i); |
| 1056 | } |
| 1057 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1058 | #define NAME_LOCAL 0 |
| 1059 | #define NAME_GLOBAL 1 |
| 1060 | #define NAME_DEFAULT 2 |
| 1061 | #define NAME_CLOSURE 3 |
| 1062 | |
| 1063 | static int |
| 1064 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 1065 | { |
| 1066 | PyObject *v = PyDict_GetItem(dict, name); |
| 1067 | if (v == NULL) |
| 1068 | return -1; |
| 1069 | else |
| 1070 | return PyInt_AS_LONG(v); |
| 1071 | } |
| 1072 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1073 | static int |
| 1074 | none_assignment_check(struct compiling *c, char *name, int assigning) |
| 1075 | { |
| 1076 | if (name[0] == 'N' && strcmp(name, "None") == 0) { |
| 1077 | char *msg; |
| 1078 | if (assigning) |
Michael W. Hudson | 976249b | 2003-01-16 15:39:07 +0000 | [diff] [blame] | 1079 | msg = "assignment to None"; |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1080 | else |
| 1081 | msg = "deleting None"; |
| 1082 | if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) { |
| 1083 | c->c_errors++; |
| 1084 | return -1; |
| 1085 | } |
| 1086 | } |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1090 | static void |
| 1091 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 1092 | { |
| 1093 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1094 | int i, reftype; |
| 1095 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1096 | int op = STOP_CODE; |
| 1097 | char buffer[MANGLE_LEN]; |
| 1098 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1099 | if (kind != VAR_LOAD && |
| 1100 | none_assignment_check(c, name, kind == VAR_STORE)) |
| 1101 | { |
| 1102 | c->c_errors++; |
| 1103 | i = 255; |
| 1104 | goto done; |
| 1105 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1106 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1107 | name = buffer; |
| 1108 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1109 | c->c_errors++; |
| 1110 | i = 255; |
| 1111 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1112 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1113 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1114 | reftype = get_ref_type(c, name); |
| 1115 | switch (reftype) { |
| 1116 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1117 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1118 | scope = NAME_LOCAL; |
| 1119 | break; |
| 1120 | case GLOBAL_EXPLICIT: |
| 1121 | scope = NAME_GLOBAL; |
| 1122 | break; |
| 1123 | case GLOBAL_IMPLICIT: |
| 1124 | if (c->c_flags & CO_OPTIMIZED) |
| 1125 | scope = NAME_GLOBAL; |
| 1126 | break; |
| 1127 | case FREE: |
| 1128 | case CELL: |
| 1129 | scope = NAME_CLOSURE; |
| 1130 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1134 | if (scope == NAME_LOCAL) |
| 1135 | i = com_lookup_arg(c->c_locals, v); |
| 1136 | else if (reftype == FREE) |
| 1137 | i = com_lookup_arg(c->c_freevars, v); |
| 1138 | else if (reftype == CELL) |
| 1139 | i = com_lookup_arg(c->c_cellvars, v); |
| 1140 | if (i == -1) { |
| 1141 | c->c_errors++; /* XXX no exception set */ |
| 1142 | i = 255; |
| 1143 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1144 | } |
| 1145 | Py_DECREF(v); |
| 1146 | |
| 1147 | switch (kind) { |
| 1148 | case VAR_LOAD: |
| 1149 | switch (scope) { |
| 1150 | case NAME_LOCAL: |
| 1151 | op = LOAD_FAST; |
| 1152 | break; |
| 1153 | case NAME_GLOBAL: |
| 1154 | op = LOAD_GLOBAL; |
| 1155 | break; |
| 1156 | case NAME_DEFAULT: |
| 1157 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1158 | break; |
| 1159 | case NAME_CLOSURE: |
| 1160 | op = LOAD_DEREF; |
| 1161 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1162 | } |
| 1163 | break; |
| 1164 | case VAR_STORE: |
| 1165 | switch (scope) { |
| 1166 | case NAME_LOCAL: |
| 1167 | op = STORE_FAST; |
| 1168 | break; |
| 1169 | case NAME_GLOBAL: |
| 1170 | op = STORE_GLOBAL; |
| 1171 | break; |
| 1172 | case NAME_DEFAULT: |
| 1173 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | case NAME_CLOSURE: |
| 1176 | op = STORE_DEREF; |
| 1177 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1178 | } |
| 1179 | break; |
| 1180 | case VAR_DELETE: |
| 1181 | switch (scope) { |
| 1182 | case NAME_LOCAL: |
| 1183 | op = DELETE_FAST; |
| 1184 | break; |
| 1185 | case NAME_GLOBAL: |
| 1186 | op = DELETE_GLOBAL; |
| 1187 | break; |
| 1188 | case NAME_DEFAULT: |
| 1189 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1190 | break; |
| 1191 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1192 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1193 | PyOS_snprintf(buf, sizeof(buf), |
| 1194 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1195 | com_error(c, PyExc_SyntaxError, buf); |
| 1196 | i = 255; |
| 1197 | break; |
| 1198 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1199 | } |
| 1200 | break; |
| 1201 | } |
| 1202 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1203 | com_addoparg(c, op, i); |
| 1204 | } |
| 1205 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1206 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1207 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1208 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1209 | char *name; |
| 1210 | char buffer[1000]; |
| 1211 | /* XXX it is possible to write this code without the 1000 |
| 1212 | chars on the total length of dotted names, I just can't be |
| 1213 | bothered right now */ |
| 1214 | if (TYPE(n) == STAR) |
| 1215 | name = "*"; |
| 1216 | else if (TYPE(n) == dotted_name) { |
| 1217 | char *p = buffer; |
| 1218 | int i; |
| 1219 | name = buffer; |
| 1220 | for (i = 0; i < NCH(n); i += 2) { |
| 1221 | char *s = STR(CHILD(n, i)); |
| 1222 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1223 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1224 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1225 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | if (p != buffer) |
| 1229 | *p++ = '.'; |
| 1230 | strcpy(p, s); |
| 1231 | p = strchr(p, '\0'); |
| 1232 | } |
| 1233 | } |
| 1234 | else { |
| 1235 | REQ(n, NAME); |
| 1236 | name = STR(n); |
| 1237 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1238 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1241 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1242 | parsenumber(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1243 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1244 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1245 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1246 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1247 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1248 | int imflag; |
| 1249 | #endif |
| 1250 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1251 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1252 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1253 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1254 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1255 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1256 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1257 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1258 | if (s[0] == '0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1259 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1260 | if (x < 0 && errno == 0) { |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1261 | if (PyErr_WarnExplicit( |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1262 | PyExc_FutureWarning, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1263 | "hex/oct constants > sys.maxint " |
| 1264 | "will return positive values " |
| 1265 | "in Python 2.4 and up", |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1266 | /* XXX: Give WarnExplicit |
| 1267 | a const char* argument. */ |
| 1268 | (char*)c->c_filename, |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1269 | c->c_lineno, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1270 | NULL, |
| 1271 | NULL) < 0) |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1272 | return NULL; |
Guido van Rossum | 3cb8e54 | 2002-08-11 14:06:15 +0000 | [diff] [blame] | 1273 | errno = 0; /* Might be changed by PyErr_Warn() */ |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1276 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1277 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1278 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1279 | if (errno != 0) |
| 1280 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1281 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1282 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1283 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1284 | #ifndef WITHOUT_COMPLEX |
| 1285 | if (imflag) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1286 | Py_complex z; |
| 1287 | z.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1288 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1289 | z.imag = atof(s); |
| 1290 | PyFPE_END_PROTECT(z) |
| 1291 | return PyComplex_FromCComplex(z); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1292 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1293 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1294 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1295 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1296 | PyFPE_START_PROTECT("atof", return 0) |
| 1297 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1298 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1299 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1300 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1303 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1304 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1305 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1306 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1307 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1308 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1309 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1310 | PyObject *u, *v; |
| 1311 | char *s, *t; |
| 1312 | t = s = *sPtr; |
| 1313 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1314 | while (s < end && (*s & 0x80)) s++; |
| 1315 | *sPtr = s; |
| 1316 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1317 | if (u == NULL) |
| 1318 | return NULL; |
| 1319 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1320 | Py_DECREF(u); |
| 1321 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1322 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 1325 | /* compiler.transformer.Transformer.decode_literal depends on what |
| 1326 | might seem like minor details of this function -- changes here |
| 1327 | must be reflected there. */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1328 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1329 | parsestr(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1330 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1331 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1332 | size_t len; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1333 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1334 | int rawmode = 0; |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1335 | char* encoding = ((c == NULL) ? NULL : c->c_encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1336 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1337 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1338 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1339 | if (isalpha(quote) || quote == '_') { |
| 1340 | if (quote == 'u' || quote == 'U') { |
| 1341 | quote = *++s; |
| 1342 | unicode = 1; |
| 1343 | } |
| 1344 | if (quote == 'r' || quote == 'R') { |
| 1345 | quote = *++s; |
| 1346 | rawmode = 1; |
| 1347 | } |
| 1348 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1349 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1350 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1351 | return NULL; |
| 1352 | } |
| 1353 | s++; |
| 1354 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1355 | if (len > INT_MAX) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1356 | com_error(c, PyExc_OverflowError, |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1357 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1358 | return NULL; |
| 1359 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1360 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1361 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1362 | return NULL; |
| 1363 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1364 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1365 | s += 2; |
| 1366 | len -= 2; |
| 1367 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1368 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1369 | return NULL; |
| 1370 | } |
| 1371 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1372 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1373 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1374 | PyObject *u, *w; |
Walter Dörwald | 4c6c765 | 2002-11-21 20:13:40 +0000 | [diff] [blame] | 1375 | char *buf; |
| 1376 | char *p; |
| 1377 | char *end; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1378 | if (encoding == NULL) { |
| 1379 | buf = s; |
| 1380 | u = NULL; |
| 1381 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1382 | buf = s; |
| 1383 | u = NULL; |
| 1384 | } else { |
| 1385 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1386 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1387 | if (u == NULL) |
| 1388 | return NULL; |
| 1389 | p = buf = PyString_AsString(u); |
| 1390 | end = s + len; |
| 1391 | while (s < end) { |
| 1392 | if (*s == '\\') { |
| 1393 | *p++ = *s++; |
| 1394 | if (*s & 0x80) { |
| 1395 | strcpy(p, "u005c"); |
| 1396 | p += 5; |
| 1397 | } |
| 1398 | } |
| 1399 | if (*s & 0x80) { /* XXX inefficient */ |
| 1400 | char *r; |
| 1401 | int rn, i; |
| 1402 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1403 | if (w == NULL) { |
| 1404 | Py_DECREF(u); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | r = PyString_AsString(w); |
| 1408 | rn = PyString_Size(w); |
| 1409 | assert(rn % 2 == 0); |
| 1410 | for (i = 0; i < rn; i += 2) { |
| 1411 | sprintf(p, "\\u%02x%02x", |
| 1412 | r[i + 0] & 0xFF, |
| 1413 | r[i + 1] & 0xFF); |
| 1414 | p += 6; |
| 1415 | } |
| 1416 | Py_DECREF(w); |
| 1417 | } else { |
| 1418 | *p++ = *s++; |
| 1419 | } |
| 1420 | } |
| 1421 | len = p - buf; |
| 1422 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1423 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1424 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1425 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1426 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1427 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1428 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1429 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1430 | return v; |
| 1431 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1432 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1433 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1434 | need_encoding = (encoding != NULL && |
| 1435 | strcmp(encoding, "utf-8") != 0 && |
| 1436 | strcmp(encoding, "iso-8859-1") != 0); |
| 1437 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1438 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1439 | #ifndef Py_USING_UNICODE |
| 1440 | /* This should not happen - we never see any other |
| 1441 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1442 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1443 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1444 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1445 | if (u == NULL) |
| 1446 | return NULL; |
| 1447 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1448 | Py_DECREF(u); |
| 1449 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1450 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1451 | } else { |
| 1452 | return PyString_FromStringAndSize(s, len); |
| 1453 | } |
| 1454 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1455 | |
| 1456 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1457 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1458 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1459 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1460 | return v; |
| 1461 | } |
| 1462 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1463 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1464 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1465 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1466 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1467 | int i; |
| 1468 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1469 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1470 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1471 | for (i = 1; i < NCH(n); i++) { |
| 1472 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1473 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1474 | if (s == NULL) |
| 1475 | goto onError; |
| 1476 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1477 | PyString_ConcatAndDel(&v, s); |
| 1478 | if (v == NULL) |
| 1479 | goto onError; |
| 1480 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1481 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1482 | else { |
| 1483 | PyObject *temp; |
| 1484 | temp = PyUnicode_Concat(v, s); |
| 1485 | Py_DECREF(s); |
| 1486 | if (temp == NULL) |
| 1487 | goto onError; |
| 1488 | Py_DECREF(v); |
| 1489 | v = temp; |
| 1490 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1491 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1495 | |
| 1496 | onError: |
| 1497 | Py_XDECREF(v); |
| 1498 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1501 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1502 | com_list_for(struct compiling *c, node *n, node *e, char *t) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1503 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1504 | int anchor = 0; |
| 1505 | int save_begin = c->c_begin; |
| 1506 | |
| 1507 | /* list_iter: for v in expr [list_iter] */ |
| 1508 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1509 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1510 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1511 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1512 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1513 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1514 | c->c_loops++; |
| 1515 | com_list_iter(c, n, e, t); |
| 1516 | c->c_loops--; |
| 1517 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1518 | c->c_begin = save_begin; |
| 1519 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1520 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | static void |
| 1524 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1525 | { |
| 1526 | int anchor = 0; |
| 1527 | int a = 0; |
| 1528 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1529 | com_node(c, CHILD(n, 1)); |
| 1530 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1531 | com_addbyte(c, POP_TOP); |
| 1532 | com_pop(c, 1); |
| 1533 | com_list_iter(c, n, e, t); |
| 1534 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1535 | com_backpatch(c, a); |
| 1536 | /* We jump here with an extra entry which we now pop */ |
| 1537 | com_addbyte(c, POP_TOP); |
| 1538 | com_backpatch(c, anchor); |
| 1539 | } |
| 1540 | |
| 1541 | static void |
| 1542 | com_list_iter(struct compiling *c, |
| 1543 | node *p, /* parent of list_iter node */ |
| 1544 | node *e, /* element expression node */ |
| 1545 | char *t /* name of result list temp local */) |
| 1546 | { |
| 1547 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1548 | node *n = CHILD(p, NCH(p)-1); |
| 1549 | if (TYPE(n) == list_iter) { |
| 1550 | n = CHILD(n, 0); |
| 1551 | switch (TYPE(n)) { |
| 1552 | case list_for: |
| 1553 | com_list_for(c, n, e, t); |
| 1554 | break; |
| 1555 | case list_if: |
| 1556 | com_list_if(c, n, e, t); |
| 1557 | break; |
| 1558 | default: |
| 1559 | com_error(c, PyExc_SystemError, |
| 1560 | "invalid list_iter node type"); |
| 1561 | } |
| 1562 | } |
| 1563 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1564 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1565 | com_push(c, 1); |
| 1566 | com_node(c, e); |
| 1567 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1568 | com_addbyte(c, POP_TOP); |
| 1569 | com_pop(c, 2); |
| 1570 | } |
| 1571 | } |
| 1572 | |
| 1573 | static void |
| 1574 | com_list_comprehension(struct compiling *c, node *n) |
| 1575 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1576 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1577 | char tmpname[30]; |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 1578 | |
| 1579 | REQ(n, listmaker); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1580 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1581 | com_addoparg(c, BUILD_LIST, 0); |
| 1582 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1583 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1584 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1585 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1586 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1587 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1588 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1589 | --c->c_tmpname; |
| 1590 | } |
| 1591 | |
| 1592 | static void |
| 1593 | com_listmaker(struct compiling *c, node *n) |
| 1594 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1595 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1596 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1597 | com_list_comprehension(c, n); |
| 1598 | else { |
| 1599 | int len = 0; |
| 1600 | int i; |
| 1601 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1602 | com_node(c, CHILD(n, i)); |
| 1603 | com_addoparg(c, BUILD_LIST, len); |
| 1604 | com_pop(c, len-1); |
| 1605 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1609 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1610 | { |
| 1611 | int i; |
| 1612 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1613 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1614 | /* We must arrange things just right for STORE_SUBSCR. |
| 1615 | It wants the stack to look like (value) (dict) (key) */ |
| 1616 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1617 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1618 | com_node(c, CHILD(n, i)); /* key */ |
Gustavo Niemeyer | 78429a6 | 2002-12-16 13:54:02 +0000 | [diff] [blame] | 1619 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1620 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1621 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1622 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1627 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1628 | { |
| 1629 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1630 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1631 | int i; |
| 1632 | REQ(n, atom); |
| 1633 | ch = CHILD(n, 0); |
| 1634 | switch (TYPE(ch)) { |
| 1635 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1636 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1637 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1638 | com_push(c, 1); |
| 1639 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1640 | else |
| 1641 | com_node(c, CHILD(n, 1)); |
| 1642 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1643 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1644 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1645 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1646 | com_push(c, 1); |
| 1647 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1648 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1649 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1650 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1651 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1652 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1653 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1654 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1655 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1656 | break; |
| 1657 | case BACKQUOTE: |
| 1658 | com_node(c, CHILD(n, 1)); |
| 1659 | com_addbyte(c, UNARY_CONVERT); |
| 1660 | break; |
| 1661 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1662 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1663 | i = 255; |
| 1664 | } |
| 1665 | else { |
| 1666 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1667 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1668 | } |
| 1669 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1670 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1673 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1674 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1675 | c->c_errors++; |
| 1676 | i = 255; |
| 1677 | } |
| 1678 | else { |
| 1679 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1680 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1681 | } |
| 1682 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1683 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1684 | break; |
| 1685 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1686 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1687 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1690 | com_error(c, PyExc_SystemError, |
| 1691 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1696 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1697 | { |
| 1698 | if (NCH(n) == 1) { |
| 1699 | com_addbyte(c, op); |
| 1700 | } |
| 1701 | else if (NCH(n) == 2) { |
| 1702 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1703 | com_node(c, CHILD(n, 0)); |
| 1704 | com_addbyte(c, op+1); |
| 1705 | } |
| 1706 | else { |
| 1707 | com_node(c, CHILD(n, 1)); |
| 1708 | com_addbyte(c, op+2); |
| 1709 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1710 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1711 | } |
| 1712 | else { |
| 1713 | com_node(c, CHILD(n, 0)); |
| 1714 | com_node(c, CHILD(n, 2)); |
| 1715 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1716 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1720 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1721 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1722 | { |
| 1723 | if (NCH(n) == 1) { |
| 1724 | com_addbyte(c, DUP_TOP); |
| 1725 | com_push(c, 1); |
| 1726 | com_addbyte(c, SLICE); |
| 1727 | com_node(c, augn); |
| 1728 | com_addbyte(c, opcode); |
| 1729 | com_pop(c, 1); |
| 1730 | com_addbyte(c, ROT_TWO); |
| 1731 | com_addbyte(c, STORE_SLICE); |
| 1732 | com_pop(c, 2); |
| 1733 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1734 | com_node(c, CHILD(n, 0)); |
| 1735 | com_addoparg(c, DUP_TOPX, 2); |
| 1736 | com_push(c, 2); |
| 1737 | com_addbyte(c, SLICE+1); |
| 1738 | com_pop(c, 1); |
| 1739 | com_node(c, augn); |
| 1740 | com_addbyte(c, opcode); |
| 1741 | com_pop(c, 1); |
| 1742 | com_addbyte(c, ROT_THREE); |
| 1743 | com_addbyte(c, STORE_SLICE+1); |
| 1744 | com_pop(c, 3); |
| 1745 | } else if (NCH(n) == 2) { |
| 1746 | com_node(c, CHILD(n, 1)); |
| 1747 | com_addoparg(c, DUP_TOPX, 2); |
| 1748 | com_push(c, 2); |
| 1749 | com_addbyte(c, SLICE+2); |
| 1750 | com_pop(c, 1); |
| 1751 | com_node(c, augn); |
| 1752 | com_addbyte(c, opcode); |
| 1753 | com_pop(c, 1); |
| 1754 | com_addbyte(c, ROT_THREE); |
| 1755 | com_addbyte(c, STORE_SLICE+2); |
| 1756 | com_pop(c, 3); |
| 1757 | } else { |
| 1758 | com_node(c, CHILD(n, 0)); |
| 1759 | com_node(c, CHILD(n, 2)); |
| 1760 | com_addoparg(c, DUP_TOPX, 3); |
| 1761 | com_push(c, 3); |
| 1762 | com_addbyte(c, SLICE+3); |
| 1763 | com_pop(c, 2); |
| 1764 | com_node(c, augn); |
| 1765 | com_addbyte(c, opcode); |
| 1766 | com_pop(c, 1); |
| 1767 | com_addbyte(c, ROT_FOUR); |
| 1768 | com_addbyte(c, STORE_SLICE+3); |
| 1769 | com_pop(c, 4); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1774 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1775 | { |
| 1776 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1777 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1778 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1779 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1780 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1781 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1782 | } |
| 1783 | else { |
| 1784 | com_node(c, CHILD(n, 0)); |
| 1785 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1786 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1787 | } |
| 1788 | m = n; |
| 1789 | do { |
| 1790 | m = CHILD(m, 0); |
| 1791 | } while (NCH(m) == 1); |
| 1792 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1793 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1794 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1795 | * SF bug 132313 points out that complaining about a keyword |
| 1796 | * then is very confusing. |
| 1797 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1798 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1799 | TYPE(m) == lambdef ? |
| 1800 | "lambda cannot contain assignment" : |
| 1801 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1802 | } |
| 1803 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1804 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 63dd79a | 2002-08-16 02:24:56 +0000 | [diff] [blame] | 1805 | (void) none_assignment_check(c, STR(m), 1); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1806 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1807 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1808 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1809 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1810 | else if (*pkeywords == NULL) { |
| 1811 | c->c_errors++; |
| 1812 | Py_DECREF(v); |
| 1813 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1814 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1815 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1816 | "duplicate keyword argument"); |
| 1817 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1818 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1819 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1820 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1821 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1822 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1828 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1829 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1830 | { |
| 1831 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1832 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1833 | } |
| 1834 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1835 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1836 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1837 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1838 | int star_flag = 0; |
| 1839 | int starstar_flag = 0; |
| 1840 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1841 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1842 | na = 0; |
| 1843 | nk = 0; |
| 1844 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1845 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1846 | if (TYPE(ch) == STAR || |
| 1847 | TYPE(ch) == DOUBLESTAR) |
| 1848 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1849 | if (ch->n_lineno != lineno) { |
| 1850 | lineno = ch->n_lineno; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1851 | com_set_lineno(c, lineno); |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1852 | } |
| 1853 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1854 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1855 | na++; |
| 1856 | else |
| 1857 | nk++; |
| 1858 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1859 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1860 | while (i < NCH(n)) { |
| 1861 | node *tok = CHILD(n, i); |
| 1862 | node *ch = CHILD(n, i+1); |
| 1863 | i += 3; |
| 1864 | switch (TYPE(tok)) { |
| 1865 | case STAR: star_flag = 1; break; |
| 1866 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1867 | } |
| 1868 | com_node(c, ch); |
| 1869 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1870 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1871 | com_error(c, PyExc_SyntaxError, |
| 1872 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1873 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1874 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1875 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1876 | star_flag + (starstar_flag << 1); |
| 1877 | else |
| 1878 | opcode = CALL_FUNCTION; |
| 1879 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1880 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1885 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1886 | { |
| 1887 | com_addopname(c, LOAD_ATTR, n); |
| 1888 | } |
| 1889 | |
| 1890 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1891 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1892 | { |
| 1893 | int i=0; |
| 1894 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1895 | node *ch; |
| 1896 | |
| 1897 | /* first argument */ |
| 1898 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1899 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1900 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1901 | i++; |
| 1902 | } |
| 1903 | else { |
| 1904 | com_node(c, CHILD(n,i)); |
| 1905 | i++; |
| 1906 | REQ(CHILD(n,i),COLON); |
| 1907 | i++; |
| 1908 | } |
| 1909 | /* second argument */ |
| 1910 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1911 | com_node(c, CHILD(n,i)); |
| 1912 | i++; |
| 1913 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1914 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1915 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1916 | com_push(c, 1); |
| 1917 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1918 | /* remaining arguments */ |
| 1919 | for (; i < NCH(n); i++) { |
| 1920 | ns++; |
| 1921 | ch=CHILD(n,i); |
| 1922 | REQ(ch, sliceop); |
| 1923 | if (NCH(ch) == 1) { |
| 1924 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1925 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1926 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1927 | } |
| 1928 | else |
| 1929 | com_node(c, CHILD(ch,1)); |
| 1930 | } |
| 1931 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1932 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1936 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1937 | { |
| 1938 | node *ch; |
| 1939 | REQ(n, subscript); |
| 1940 | ch = CHILD(n,0); |
| 1941 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1942 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1943 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1944 | com_push(c, 1); |
| 1945 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1946 | else { |
| 1947 | /* check for slice */ |
| 1948 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1949 | com_sliceobj(c, n); |
| 1950 | else { |
| 1951 | REQ(ch, test); |
| 1952 | com_node(c, ch); |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1958 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1959 | { |
| 1960 | int i, op; |
| 1961 | REQ(n, subscriptlist); |
| 1962 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1963 | if (NCH(n) == 1) { |
| 1964 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1965 | /* 'Basic' slice, should have exactly one colon. */ |
| 1966 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1967 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1968 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1969 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1970 | switch (assigning) { |
| 1971 | case OP_DELETE: |
| 1972 | op = DELETE_SLICE; |
| 1973 | break; |
| 1974 | case OP_ASSIGN: |
| 1975 | op = STORE_SLICE; |
| 1976 | break; |
| 1977 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1978 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1979 | break; |
| 1980 | default: |
| 1981 | com_augassign_slice(c, sub, assigning, augn); |
| 1982 | return; |
| 1983 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1984 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1985 | if (op == STORE_SLICE) |
| 1986 | com_pop(c, 2); |
| 1987 | else if (op == DELETE_SLICE) |
| 1988 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1989 | return; |
| 1990 | } |
| 1991 | } |
| 1992 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1993 | for (i = 0; i < NCH(n); i += 2) |
| 1994 | com_subscript(c, CHILD(n, i)); |
| 1995 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1996 | if (NCH(n) > 1) { |
| 1997 | i = (NCH(n)+1) / 2; |
| 1998 | com_addoparg(c, BUILD_TUPLE, i); |
| 1999 | com_pop(c, i-1); |
| 2000 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2001 | switch (assigning) { |
| 2002 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2003 | op = DELETE_SUBSCR; |
| 2004 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2005 | break; |
| 2006 | default: |
| 2007 | case OP_ASSIGN: |
| 2008 | op = STORE_SUBSCR; |
| 2009 | i = 3; |
| 2010 | break; |
| 2011 | case OP_APPLY: |
| 2012 | op = BINARY_SUBSCR; |
| 2013 | i = 1; |
| 2014 | break; |
| 2015 | } |
| 2016 | if (assigning > OP_APPLY) { |
| 2017 | com_addoparg(c, DUP_TOPX, 2); |
| 2018 | com_push(c, 2); |
| 2019 | com_addbyte(c, BINARY_SUBSCR); |
| 2020 | com_pop(c, 1); |
| 2021 | com_node(c, augn); |
| 2022 | com_addbyte(c, assigning); |
| 2023 | com_pop(c, 1); |
| 2024 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2025 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2026 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2027 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2031 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2032 | { |
| 2033 | REQ(n, trailer); |
| 2034 | switch (TYPE(CHILD(n, 0))) { |
| 2035 | case LPAR: |
| 2036 | com_call_function(c, CHILD(n, 1)); |
| 2037 | break; |
| 2038 | case DOT: |
| 2039 | com_select_member(c, CHILD(n, 1)); |
| 2040 | break; |
| 2041 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2042 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2043 | break; |
| 2044 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2045 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2046 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2051 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2052 | { |
| 2053 | int i; |
| 2054 | REQ(n, power); |
| 2055 | com_atom(c, CHILD(n, 0)); |
| 2056 | for (i = 1; i < NCH(n); i++) { |
| 2057 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 2058 | com_factor(c, CHILD(n, i+1)); |
| 2059 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2060 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2061 | break; |
| 2062 | } |
| 2063 | else |
| 2064 | com_apply_trailer(c, CHILD(n, i)); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2069 | com_invert_constant(struct compiling *c, node *n) |
| 2070 | { |
| 2071 | /* Compute the inverse of int and longs and use them directly, |
| 2072 | but be prepared to generate code for all other |
| 2073 | possibilities (invalid numbers, floats, complex). |
| 2074 | */ |
| 2075 | PyObject *num, *inv = NULL; |
| 2076 | int i; |
| 2077 | |
| 2078 | REQ(n, NUMBER); |
| 2079 | num = parsenumber(c, STR(n)); |
| 2080 | if (num == NULL) |
| 2081 | i = 255; |
| 2082 | else { |
| 2083 | inv = PyNumber_Invert(num); |
| 2084 | if (inv == NULL) { |
| 2085 | PyErr_Clear(); |
| 2086 | i = com_addconst(c, num); |
| 2087 | } else { |
| 2088 | i = com_addconst(c, inv); |
| 2089 | Py_DECREF(inv); |
| 2090 | } |
| 2091 | Py_DECREF(num); |
| 2092 | } |
| 2093 | com_addoparg(c, LOAD_CONST, i); |
| 2094 | com_push(c, 1); |
| 2095 | if (num != NULL && inv == NULL) |
| 2096 | com_addbyte(c, UNARY_INVERT); |
| 2097 | } |
| 2098 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2099 | static int |
| 2100 | is_float_zero(const char *p) |
| 2101 | { |
| 2102 | int found_radix_point = 0; |
| 2103 | int ch; |
| 2104 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2105 | switch (ch) { |
| 2106 | case '0': |
| 2107 | /* no reason to believe it's not 0 -- continue */ |
| 2108 | break; |
| 2109 | |
| 2110 | case 'e': case 'E': case 'j': case 'J': |
| 2111 | /* If this was a hex constant, we already would have |
| 2112 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2113 | must be an exponent marker, and we haven't yet |
| 2114 | seen a non-zero digit, and it doesn't matter what |
| 2115 | the exponent is then. For 'j' or 'J' similarly, |
| 2116 | except that this is an imaginary 0 then. */ |
| 2117 | return 1; |
| 2118 | |
| 2119 | case '.': |
| 2120 | found_radix_point = 1; |
| 2121 | break; |
| 2122 | |
| 2123 | default: |
| 2124 | return 0; |
| 2125 | } |
| 2126 | } |
| 2127 | return found_radix_point; |
| 2128 | } |
| 2129 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2130 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2131 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2132 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2133 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2134 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2135 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2136 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2137 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2138 | approriate value as a constant. If the value is negative, |
| 2139 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2140 | negative in the 0th position -- unless we're doing unary minus |
| 2141 | of a floating zero! In that case the sign is significant, but |
| 2142 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2143 | */ |
| 2144 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2145 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2146 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2147 | && NCH(pfactor) == 1 |
| 2148 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2149 | && NCH(ppower) == 1 |
| 2150 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2151 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
Guido van Rossum | 66b1259 | 2003-02-12 16:57:47 +0000 | [diff] [blame] | 2152 | && !(childtype == MINUS && |
| 2153 | (STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2154 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2155 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
| 2158 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2159 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2160 | if (s == NULL) { |
| 2161 | com_error(c, PyExc_MemoryError, ""); |
| 2162 | com_addbyte(c, 255); |
| 2163 | return; |
| 2164 | } |
| 2165 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2166 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2167 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2168 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2169 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2170 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2171 | } |
| 2172 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2173 | com_factor(c, CHILD(n, 1)); |
| 2174 | com_addbyte(c, UNARY_POSITIVE); |
| 2175 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2176 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2177 | com_factor(c, CHILD(n, 1)); |
| 2178 | com_addbyte(c, UNARY_NEGATIVE); |
| 2179 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2180 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2181 | com_factor(c, CHILD(n, 1)); |
| 2182 | com_addbyte(c, UNARY_INVERT); |
| 2183 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2184 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2185 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2190 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2191 | { |
| 2192 | int i; |
| 2193 | int op; |
| 2194 | REQ(n, term); |
| 2195 | com_factor(c, CHILD(n, 0)); |
| 2196 | for (i = 2; i < NCH(n); i += 2) { |
| 2197 | com_factor(c, CHILD(n, i)); |
| 2198 | switch (TYPE(CHILD(n, i-1))) { |
| 2199 | case STAR: |
| 2200 | op = BINARY_MULTIPLY; |
| 2201 | break; |
| 2202 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2203 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2204 | op = BINARY_TRUE_DIVIDE; |
| 2205 | else |
| 2206 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2207 | break; |
| 2208 | case PERCENT: |
| 2209 | op = BINARY_MODULO; |
| 2210 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2211 | case DOUBLESLASH: |
| 2212 | op = BINARY_FLOOR_DIVIDE; |
| 2213 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2214 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2215 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2216 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2217 | op = 255; |
| 2218 | } |
| 2219 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2220 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2225 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2226 | { |
| 2227 | int i; |
| 2228 | int op; |
| 2229 | REQ(n, arith_expr); |
| 2230 | com_term(c, CHILD(n, 0)); |
| 2231 | for (i = 2; i < NCH(n); i += 2) { |
| 2232 | com_term(c, CHILD(n, i)); |
| 2233 | switch (TYPE(CHILD(n, i-1))) { |
| 2234 | case PLUS: |
| 2235 | op = BINARY_ADD; |
| 2236 | break; |
| 2237 | case MINUS: |
| 2238 | op = BINARY_SUBTRACT; |
| 2239 | break; |
| 2240 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2241 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2242 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2243 | op = 255; |
| 2244 | } |
| 2245 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2246 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2251 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2252 | { |
| 2253 | int i; |
| 2254 | int op; |
| 2255 | REQ(n, shift_expr); |
| 2256 | com_arith_expr(c, CHILD(n, 0)); |
| 2257 | for (i = 2; i < NCH(n); i += 2) { |
| 2258 | com_arith_expr(c, CHILD(n, i)); |
| 2259 | switch (TYPE(CHILD(n, i-1))) { |
| 2260 | case LEFTSHIFT: |
| 2261 | op = BINARY_LSHIFT; |
| 2262 | break; |
| 2263 | case RIGHTSHIFT: |
| 2264 | op = BINARY_RSHIFT; |
| 2265 | break; |
| 2266 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2267 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2268 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2269 | op = 255; |
| 2270 | } |
| 2271 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2272 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
| 2276 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2277 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2278 | { |
| 2279 | int i; |
| 2280 | int op; |
| 2281 | REQ(n, and_expr); |
| 2282 | com_shift_expr(c, CHILD(n, 0)); |
| 2283 | for (i = 2; i < NCH(n); i += 2) { |
| 2284 | com_shift_expr(c, CHILD(n, i)); |
| 2285 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2286 | op = BINARY_AND; |
| 2287 | } |
| 2288 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2289 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2290 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2291 | op = 255; |
| 2292 | } |
| 2293 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2294 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2299 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2300 | { |
| 2301 | int i; |
| 2302 | int op; |
| 2303 | REQ(n, xor_expr); |
| 2304 | com_and_expr(c, CHILD(n, 0)); |
| 2305 | for (i = 2; i < NCH(n); i += 2) { |
| 2306 | com_and_expr(c, CHILD(n, i)); |
| 2307 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2308 | op = BINARY_XOR; |
| 2309 | } |
| 2310 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2311 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2312 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2313 | op = 255; |
| 2314 | } |
| 2315 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2316 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2321 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2322 | { |
| 2323 | int i; |
| 2324 | int op; |
| 2325 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2326 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2327 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2328 | com_xor_expr(c, CHILD(n, i)); |
| 2329 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2330 | op = BINARY_OR; |
| 2331 | } |
| 2332 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2333 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2334 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2335 | op = 255; |
| 2336 | } |
| 2337 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2338 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2343 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2344 | { |
| 2345 | REQ(n, comp_op); |
Tim Peters | 12d55a7 | 2003-05-12 19:16:52 +0000 | [diff] [blame] | 2346 | /* comp_op: '<' | '>' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2347 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2348 | if (NCH(n) == 1) { |
| 2349 | n = CHILD(n, 0); |
| 2350 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2351 | case LESS: return PyCmp_LT; |
| 2352 | case GREATER: return PyCmp_GT; |
Tim Peters | 12d55a7 | 2003-05-12 19:16:52 +0000 | [diff] [blame] | 2353 | case EQEQUAL: return PyCmp_EQ; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2354 | case LESSEQUAL: return PyCmp_LE; |
| 2355 | case GREATEREQUAL: return PyCmp_GE; |
| 2356 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2357 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2358 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2359 | } |
| 2360 | } |
| 2361 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2362 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2363 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2364 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2365 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2366 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2367 | } |
| 2368 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2369 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2373 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2374 | { |
| 2375 | int i; |
| 2376 | enum cmp_op op; |
| 2377 | int anchor; |
| 2378 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2379 | com_expr(c, CHILD(n, 0)); |
| 2380 | if (NCH(n) == 1) |
| 2381 | return; |
| 2382 | |
| 2383 | /**************************************************************** |
| 2384 | The following code is generated for all but the last |
| 2385 | comparison in a chain: |
| 2386 | |
| 2387 | label: on stack: opcode: jump to: |
| 2388 | |
| 2389 | a <code to load b> |
| 2390 | a, b DUP_TOP |
| 2391 | a, b, b ROT_THREE |
| 2392 | b, a, b COMPARE_OP |
| 2393 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2394 | b, 1 POP_TOP |
| 2395 | b |
| 2396 | |
| 2397 | We are now ready to repeat this sequence for the next |
| 2398 | comparison in the chain. |
| 2399 | |
| 2400 | For the last we generate: |
| 2401 | |
| 2402 | b <code to load c> |
| 2403 | b, c COMPARE_OP |
| 2404 | 0-or-1 |
| 2405 | |
| 2406 | If there were any jumps to L1 (i.e., there was more than one |
| 2407 | comparison), we generate: |
| 2408 | |
| 2409 | 0-or-1 JUMP_FORWARD L2 |
| 2410 | L1: b, 0 ROT_TWO |
| 2411 | 0, b POP_TOP |
| 2412 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2413 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2414 | ****************************************************************/ |
| 2415 | |
| 2416 | anchor = 0; |
| 2417 | |
| 2418 | for (i = 2; i < NCH(n); i += 2) { |
| 2419 | com_expr(c, CHILD(n, i)); |
| 2420 | if (i+2 < NCH(n)) { |
| 2421 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2422 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2423 | com_addbyte(c, ROT_THREE); |
| 2424 | } |
| 2425 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2426 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2427 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2428 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2429 | } |
| 2430 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2431 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2432 | if (i+2 < NCH(n)) { |
| 2433 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2434 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2435 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | if (anchor) { |
| 2440 | int anchor2 = 0; |
| 2441 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2442 | com_backpatch(c, anchor); |
| 2443 | com_addbyte(c, ROT_TWO); |
| 2444 | com_addbyte(c, POP_TOP); |
| 2445 | com_backpatch(c, anchor2); |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2450 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2451 | { |
| 2452 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2453 | if (NCH(n) == 1) { |
| 2454 | com_comparison(c, CHILD(n, 0)); |
| 2455 | } |
| 2456 | else { |
| 2457 | com_not_test(c, CHILD(n, 1)); |
| 2458 | com_addbyte(c, UNARY_NOT); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2463 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2464 | { |
| 2465 | int i; |
| 2466 | int anchor; |
| 2467 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2468 | anchor = 0; |
| 2469 | i = 0; |
| 2470 | for (;;) { |
| 2471 | com_not_test(c, CHILD(n, i)); |
| 2472 | if ((i += 2) >= NCH(n)) |
| 2473 | break; |
| 2474 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2475 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2476 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2477 | } |
| 2478 | if (anchor) |
| 2479 | com_backpatch(c, anchor); |
| 2480 | } |
| 2481 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2482 | static int |
| 2483 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2484 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2485 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2486 | if (free == 0) |
| 2487 | return 0; |
| 2488 | for (i = 0; i < free; ++i) { |
| 2489 | /* Bypass com_addop_varname because it will generate |
| 2490 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2491 | */ |
| 2492 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2493 | int arg, reftype; |
| 2494 | |
| 2495 | /* Special case: If a class contains a method with a |
| 2496 | free variable that has the same name as a method, |
| 2497 | the name will be considered free *and* local in the |
| 2498 | class. It should be handled by the closure, as |
| 2499 | well as by the normal name loookup logic. |
| 2500 | */ |
| 2501 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2502 | if (reftype == CELL) |
| 2503 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2504 | else /* (reftype == FREE) */ |
| 2505 | arg = com_lookup_arg(c->c_freevars, name); |
| 2506 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2507 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2508 | "freevars of %s: %s\n", |
| 2509 | PyObject_REPR(name), |
| 2510 | c->c_name, |
| 2511 | reftype, arg, |
| 2512 | PyString_AS_STRING(co->co_name), |
| 2513 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2514 | Py_FatalError("com_make_closure()"); |
| 2515 | } |
| 2516 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2517 | |
| 2518 | } |
| 2519 | com_push(c, free); |
| 2520 | return 1; |
| 2521 | } |
| 2522 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2523 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2524 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2525 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2526 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2527 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2528 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2529 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2530 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2531 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2532 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2533 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2534 | if (co == NULL) { |
| 2535 | c->c_errors++; |
| 2536 | return; |
| 2537 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2538 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2539 | i = com_addconst(c, (PyObject *)co); |
| 2540 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2541 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2542 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2543 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2544 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2545 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2546 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2547 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2548 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2549 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2550 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2551 | else { |
| 2552 | int anchor = 0; |
| 2553 | int i = 0; |
| 2554 | for (;;) { |
| 2555 | com_and_test(c, CHILD(n, i)); |
| 2556 | if ((i += 2) >= NCH(n)) |
| 2557 | break; |
| 2558 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2559 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2560 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2561 | } |
| 2562 | if (anchor) |
| 2563 | com_backpatch(c, anchor); |
| 2564 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
| 2567 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2568 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2569 | { |
| 2570 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2571 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2572 | com_node(c, CHILD(n, 0)); |
| 2573 | } |
| 2574 | else { |
| 2575 | int i; |
| 2576 | int len; |
| 2577 | len = (NCH(n) + 1) / 2; |
| 2578 | for (i = 0; i < NCH(n); i += 2) |
| 2579 | com_node(c, CHILD(n, i)); |
| 2580 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2581 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | |
| 2586 | /* Begin of assignment compilation */ |
| 2587 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2588 | |
| 2589 | static void |
| 2590 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2591 | { |
| 2592 | com_addbyte(c, DUP_TOP); |
| 2593 | com_push(c, 1); |
| 2594 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2595 | com_node(c, augn); |
| 2596 | com_addbyte(c, opcode); |
| 2597 | com_pop(c, 1); |
| 2598 | com_addbyte(c, ROT_TWO); |
| 2599 | com_addopname(c, STORE_ATTR, n); |
| 2600 | com_pop(c, 2); |
| 2601 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2602 | |
| 2603 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2604 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2605 | { |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 2606 | if (none_assignment_check(c, STR(n), assigning)) |
| 2607 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2608 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2609 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
| 2612 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2613 | com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2614 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2615 | REQ(n, trailer); |
| 2616 | switch (TYPE(CHILD(n, 0))) { |
| 2617 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2618 | if (assigning == OP_DELETE) |
| 2619 | com_error(c, PyExc_SyntaxError, |
| 2620 | "can't delete function call"); |
| 2621 | else |
| 2622 | com_error(c, PyExc_SyntaxError, |
| 2623 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2624 | break; |
| 2625 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2626 | if (assigning > OP_APPLY) |
| 2627 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2628 | else |
| 2629 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2630 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2631 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2632 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2633 | break; |
| 2634 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2635 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2640 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | { |
| 2642 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2643 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2644 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2645 | if (assigning) { |
| 2646 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2647 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2648 | com_push(c, i-1); |
| 2649 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2650 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2651 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2652 | } |
| 2653 | |
| 2654 | static void |
| 2655 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2656 | { |
| 2657 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2658 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2659 | com_push(c, 1); |
| 2660 | com_node(c, augn); |
| 2661 | com_addbyte(c, opcode); |
| 2662 | com_pop(c, 1); |
| 2663 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2667 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2668 | { |
| 2669 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2670 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2671 | if (assigning) |
| 2672 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2676 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2677 | { |
| 2678 | /* Loop to avoid trivial recursion */ |
| 2679 | for (;;) { |
| 2680 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2681 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2682 | case exprlist: |
| 2683 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2684 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2685 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2686 | if (assigning > OP_APPLY) { |
| 2687 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2688 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2689 | return; |
| 2690 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2691 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2692 | return; |
| 2693 | } |
| 2694 | n = CHILD(n, 0); |
| 2695 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2696 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2697 | case test: |
| 2698 | case and_test: |
| 2699 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2700 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2701 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2702 | case xor_expr: |
| 2703 | case and_expr: |
| 2704 | case shift_expr: |
| 2705 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2706 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2707 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2708 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2709 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2710 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2711 | return; |
| 2712 | } |
| 2713 | n = CHILD(n, 0); |
| 2714 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2715 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2716 | case power: /* atom trailer* ('**' power)* |
| 2717 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2718 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2719 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2720 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2723 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2724 | int i; |
| 2725 | com_node(c, CHILD(n, 0)); |
| 2726 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2727 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2728 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2729 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2730 | return; |
| 2731 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2732 | com_apply_trailer(c, CHILD(n, i)); |
| 2733 | } /* NB i is still alive */ |
| 2734 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2735 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2736 | return; |
| 2737 | } |
| 2738 | n = CHILD(n, 0); |
| 2739 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2740 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2741 | case atom: |
| 2742 | switch (TYPE(CHILD(n, 0))) { |
| 2743 | case LPAR: |
| 2744 | n = CHILD(n, 1); |
| 2745 | if (TYPE(n) == RPAR) { |
| 2746 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2747 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2748 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2749 | return; |
| 2750 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2751 | if (assigning > OP_APPLY) { |
| 2752 | com_error(c, PyExc_SyntaxError, |
Neal Norwitz | a1d654e | 2003-05-22 22:00:04 +0000 | [diff] [blame] | 2753 | "augmented assign to tuple literal not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2754 | return; |
| 2755 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | case LSQB: |
| 2758 | n = CHILD(n, 1); |
| 2759 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2760 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2761 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2764 | if (assigning > OP_APPLY) { |
| 2765 | com_error(c, PyExc_SyntaxError, |
Neal Norwitz | a1d654e | 2003-05-22 22:00:04 +0000 | [diff] [blame] | 2766 | "augmented assign to list literal not possible"); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2767 | return; |
| 2768 | } |
| 2769 | if (NCH(n) > 1 |
| 2770 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2771 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2772 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2773 | return; |
| 2774 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2775 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2776 | return; |
| 2777 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2778 | if (assigning > OP_APPLY) |
| 2779 | com_augassign_name(c, CHILD(n, 0), |
| 2780 | assigning, augn); |
| 2781 | else |
| 2782 | com_assign_name(c, CHILD(n, 0), |
| 2783 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2784 | return; |
| 2785 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2786 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2787 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | } |
| 2790 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2791 | |
| 2792 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2793 | com_error(c, PyExc_SyntaxError, |
| 2794 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2795 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2796 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2797 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2798 | com_error(c, PyExc_SystemError, |
| 2799 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2800 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2801 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2805 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2806 | static void |
| 2807 | com_augassign(struct compiling *c, node *n) |
| 2808 | { |
| 2809 | int opcode; |
| 2810 | |
| 2811 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2812 | case '+': opcode = INPLACE_ADD; break; |
| 2813 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2814 | case '/': |
| 2815 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2816 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2817 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2818 | opcode = INPLACE_TRUE_DIVIDE; |
| 2819 | else |
| 2820 | opcode = INPLACE_DIVIDE; |
| 2821 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2822 | case '%': opcode = INPLACE_MODULO; break; |
| 2823 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2824 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2825 | case '&': opcode = INPLACE_AND; break; |
| 2826 | case '^': opcode = INPLACE_XOR; break; |
| 2827 | case '|': opcode = INPLACE_OR; break; |
| 2828 | case '*': |
| 2829 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2830 | opcode = INPLACE_POWER; |
| 2831 | else |
| 2832 | opcode = INPLACE_MULTIPLY; |
| 2833 | break; |
| 2834 | default: |
| 2835 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2836 | return; |
| 2837 | } |
| 2838 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2839 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2840 | |
| 2841 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2842 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2843 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2844 | REQ(n, expr_stmt); |
| 2845 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2846 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2847 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2848 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2849 | if (NCH(n) == 1) { |
| 2850 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2851 | if (c->c_interactive) |
| 2852 | com_addbyte(c, PRINT_EXPR); |
| 2853 | else |
| 2854 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2855 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2856 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2857 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2858 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2859 | else { |
| 2860 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2861 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2862 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2863 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2864 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2865 | com_push(c, 1); |
| 2866 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2867 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | } |
| 2871 | |
| 2872 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2873 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2874 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2875 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2876 | int i; |
| 2877 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2878 | if (Py_OptimizeFlag) |
| 2879 | return; |
| 2880 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2881 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2882 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2883 | raise AssertionError [, <message>] |
| 2884 | |
| 2885 | where <message> is the second test, if present. |
| 2886 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2887 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2888 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2889 | com_addbyte(c, POP_TOP); |
| 2890 | com_pop(c, 1); |
| 2891 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2892 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2893 | com_push(c, 1); |
| 2894 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2895 | if (i > 1) |
| 2896 | com_node(c, CHILD(n, 3)); |
| 2897 | com_addoparg(c, RAISE_VARARGS, i); |
| 2898 | com_pop(c, i); |
| 2899 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2900 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2901 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2902 | com_addbyte(c, POP_TOP); |
| 2903 | } |
| 2904 | |
| 2905 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2906 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2907 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2908 | int i = 1; |
| 2909 | node* stream = NULL; |
| 2910 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2911 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2912 | |
| 2913 | /* are we using the extended print form? */ |
| 2914 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2915 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2916 | com_node(c, stream); |
| 2917 | /* stack: [...] => [... stream] */ |
| 2918 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2919 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2920 | i = 4; |
| 2921 | else |
| 2922 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2923 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2924 | for (; i < NCH(n); i += 2) { |
| 2925 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2926 | com_addbyte(c, DUP_TOP); |
| 2927 | /* stack: [stream] => [stream stream] */ |
| 2928 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2929 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2930 | /* stack: [stream stream] => [stream stream obj] */ |
| 2931 | com_addbyte(c, ROT_TWO); |
| 2932 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2933 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2934 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2935 | com_pop(c, 2); |
| 2936 | } |
| 2937 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2938 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2939 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2940 | com_addbyte(c, PRINT_ITEM); |
| 2941 | com_pop(c, 1); |
| 2942 | } |
| 2943 | } |
| 2944 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2945 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2946 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2947 | /* must pop the extra stream object off the stack */ |
| 2948 | com_addbyte(c, POP_TOP); |
| 2949 | /* stack: [... stream] => [...] */ |
| 2950 | com_pop(c, 1); |
| 2951 | } |
| 2952 | } |
| 2953 | else { |
| 2954 | if (stream != NULL) { |
| 2955 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2956 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2957 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2958 | com_pop(c, 1); |
| 2959 | } |
| 2960 | else |
| 2961 | com_addbyte(c, PRINT_NEWLINE); |
| 2962 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
| 2965 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2966 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2967 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2968 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2969 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2970 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2971 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2972 | if (c->c_flags & CO_GENERATOR) { |
| 2973 | if (NCH(n) > 1) { |
| 2974 | com_error(c, PyExc_SyntaxError, |
| 2975 | "'return' with argument inside generator"); |
| 2976 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2977 | } |
| 2978 | if (NCH(n) < 2) { |
| 2979 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2980 | com_push(c, 1); |
| 2981 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2982 | else |
| 2983 | com_node(c, CHILD(n, 1)); |
| 2984 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2985 | com_pop(c, 1); |
| 2986 | } |
| 2987 | |
| 2988 | static void |
| 2989 | com_yield_stmt(struct compiling *c, node *n) |
| 2990 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2991 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2992 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2993 | if (!c->c_infunction) { |
| 2994 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2995 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2996 | |
| 2997 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2998 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2999 | com_error(c, PyExc_SyntaxError, |
| 3000 | "'yield' not allowed in a 'try' block " |
| 3001 | "with a 'finally' clause"); |
| 3002 | return; |
| 3003 | } |
| 3004 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3005 | com_node(c, CHILD(n, 1)); |
| 3006 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3007 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3011 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3012 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3013 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3014 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 3015 | if (NCH(n) > 1) { |
| 3016 | com_node(c, CHILD(n, 1)); |
| 3017 | if (NCH(n) > 3) { |
| 3018 | com_node(c, CHILD(n, 3)); |
| 3019 | if (NCH(n) > 5) |
| 3020 | com_node(c, CHILD(n, 5)); |
| 3021 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3022 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3023 | i = NCH(n)/2; |
| 3024 | com_addoparg(c, RAISE_VARARGS, i); |
| 3025 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3029 | com_from_import(struct compiling *c, node *n) |
| 3030 | { |
| 3031 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 3032 | com_push(c, 1); |
| 3033 | if (NCH(n) > 1) { |
| 3034 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 3035 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 3036 | return; |
| 3037 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3038 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3039 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3040 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3041 | com_pop(c, 1); |
| 3042 | } |
| 3043 | |
| 3044 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3045 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3046 | { |
| 3047 | int i; |
| 3048 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3049 | /* 'import' dotted_name (',' dotted_name)* | |
| 3050 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3051 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3052 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3053 | /* 'from' dotted_name 'import' ... */ |
| 3054 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3055 | |
| 3056 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 3057 | tup = Py_BuildValue("(s)", "*"); |
| 3058 | } else { |
| 3059 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 3060 | for (i = 3; i < NCH(n); i += 2) { |
| 3061 | PyTuple_SET_ITEM(tup, (i-3)/2, |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3062 | PyString_FromString(STR( |
| 3063 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3067 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3068 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3069 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3070 | if (TYPE(CHILD(n, 3)) == STAR) |
| 3071 | com_addbyte(c, IMPORT_STAR); |
| 3072 | else { |
| 3073 | for (i = 3; i < NCH(n); i += 2) |
| 3074 | com_from_import(c, CHILD(n, i)); |
| 3075 | com_addbyte(c, POP_TOP); |
| 3076 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3077 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3078 | } |
| 3079 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3080 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3081 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3082 | node *subn = CHILD(n, i); |
| 3083 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3084 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3085 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3086 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3087 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3088 | int j; |
| 3089 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3090 | com_error(c, PyExc_SyntaxError, |
| 3091 | "invalid syntax"); |
| 3092 | return; |
| 3093 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3094 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3095 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3096 | CHILD(CHILD(subn, 0), |
| 3097 | j)); |
| 3098 | com_addop_varname(c, VAR_STORE, |
| 3099 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3100 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3101 | com_addop_varname(c, VAR_STORE, |
| 3102 | STR(CHILD(CHILD(subn, 0), |
| 3103 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3104 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3105 | } |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3110 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3111 | { |
| 3112 | REQ(n, exec_stmt); |
| 3113 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3114 | com_node(c, CHILD(n, 1)); |
| 3115 | if (NCH(n) >= 4) |
| 3116 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3117 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3118 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3119 | com_push(c, 1); |
| 3120 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3121 | if (NCH(n) >= 6) |
| 3122 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3123 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3124 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3125 | com_push(c, 1); |
| 3126 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3127 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3128 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3131 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3132 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3133 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3134 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3135 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3136 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3137 | |
| 3138 | /* Label to avoid tail recursion */ |
| 3139 | next: |
| 3140 | switch (TYPE(n)) { |
| 3141 | |
| 3142 | case suite: |
| 3143 | if (NCH(n) == 1) { |
| 3144 | n = CHILD(n, 0); |
| 3145 | goto next; |
| 3146 | } |
| 3147 | /* Fall through */ |
| 3148 | case file_input: |
| 3149 | for (i = 0; i < NCH(n); i++) { |
| 3150 | node *ch = CHILD(n, i); |
| 3151 | if (TYPE(ch) == stmt) { |
| 3152 | n = ch; |
| 3153 | goto next; |
| 3154 | } |
| 3155 | } |
| 3156 | break; |
| 3157 | |
| 3158 | case stmt: |
| 3159 | case simple_stmt: |
| 3160 | case small_stmt: |
| 3161 | n = CHILD(n, 0); |
| 3162 | goto next; |
| 3163 | |
| 3164 | case expr_stmt: |
| 3165 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3166 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3167 | case test: |
| 3168 | case and_test: |
| 3169 | case not_test: |
| 3170 | case comparison: |
| 3171 | case expr: |
| 3172 | case xor_expr: |
| 3173 | case and_expr: |
| 3174 | case shift_expr: |
| 3175 | case arith_expr: |
| 3176 | case term: |
| 3177 | case factor: |
| 3178 | case power: |
| 3179 | case atom: |
| 3180 | if (NCH(n) == 1) { |
| 3181 | n = CHILD(n, 0); |
| 3182 | goto next; |
| 3183 | } |
| 3184 | break; |
| 3185 | |
| 3186 | case NAME: |
| 3187 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3188 | return 1; |
| 3189 | break; |
| 3190 | |
| 3191 | case NUMBER: |
| 3192 | v = parsenumber(c, STR(n)); |
| 3193 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3194 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3195 | break; |
| 3196 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3197 | i = PyObject_IsTrue(v); |
| 3198 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3199 | return i == 0; |
| 3200 | |
| 3201 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3202 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3203 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3204 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3207 | i = PyObject_IsTrue(v); |
| 3208 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3209 | return i == 0; |
| 3210 | |
| 3211 | } |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3215 | |
| 3216 | /* Look under n for a return stmt with an expression. |
| 3217 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3218 | * functions already known to be generators (as determined by the symtable |
| 3219 | * pass). |
| 3220 | * Return the offending return node if found, else NULL. |
| 3221 | */ |
| 3222 | static node * |
| 3223 | look_for_offending_return(node *n) |
| 3224 | { |
| 3225 | int i; |
| 3226 | |
| 3227 | for (i = 0; i < NCH(n); ++i) { |
| 3228 | node *kid = CHILD(n, i); |
| 3229 | |
| 3230 | switch (TYPE(kid)) { |
| 3231 | case classdef: |
| 3232 | case funcdef: |
| 3233 | case lambdef: |
| 3234 | /* Stuff in nested functions & classes doesn't |
| 3235 | affect the code block we started in. */ |
| 3236 | return NULL; |
| 3237 | |
| 3238 | case return_stmt: |
| 3239 | if (NCH(kid) > 1) |
| 3240 | return kid; |
| 3241 | break; |
| 3242 | |
| 3243 | default: { |
| 3244 | node *bad = look_for_offending_return(kid); |
| 3245 | if (bad != NULL) |
| 3246 | return bad; |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | return NULL; |
| 3252 | } |
| 3253 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3254 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3255 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3256 | { |
| 3257 | int i; |
| 3258 | int anchor = 0; |
| 3259 | REQ(n, if_stmt); |
| 3260 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3261 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3262 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3263 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3264 | if (is_constant_false(c, ch)) { |
| 3265 | /* We're going to skip this block. However, if this |
| 3266 | is a generator, we have to check the dead code |
| 3267 | anyway to make sure there aren't any return stmts |
| 3268 | with expressions, in the same scope. */ |
| 3269 | if (c->c_flags & CO_GENERATOR) { |
| 3270 | node *p = look_for_offending_return(n); |
| 3271 | if (p != NULL) { |
| 3272 | int savelineno = c->c_lineno; |
| 3273 | c->c_lineno = p->n_lineno; |
| 3274 | com_error(c, PyExc_SyntaxError, |
| 3275 | "'return' with argument " |
| 3276 | "inside generator"); |
| 3277 | c->c_lineno = savelineno; |
| 3278 | } |
| 3279 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3280 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3281 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3282 | if (i > 0) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3283 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3284 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3285 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3286 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3287 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3288 | com_node(c, CHILD(n, i+3)); |
| 3289 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3290 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3291 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3292 | com_addbyte(c, POP_TOP); |
| 3293 | } |
| 3294 | if (i+2 < NCH(n)) |
| 3295 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3296 | if (anchor) |
| 3297 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3301 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3302 | { |
| 3303 | int break_anchor = 0; |
| 3304 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3305 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3306 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3307 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3308 | block_push(c, SETUP_LOOP); |
| 3309 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3310 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3311 | com_node(c, CHILD(n, 1)); |
| 3312 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3313 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3314 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3315 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3316 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3317 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3318 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3319 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3320 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3321 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3322 | com_addbyte(c, POP_TOP); |
| 3323 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3324 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3325 | if (NCH(n) > 4) |
| 3326 | com_node(c, CHILD(n, 6)); |
| 3327 | com_backpatch(c, break_anchor); |
| 3328 | } |
| 3329 | |
| 3330 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3331 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3332 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3333 | int break_anchor = 0; |
| 3334 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3335 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3336 | REQ(n, for_stmt); |
| 3337 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3338 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3339 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3340 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3341 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3342 | c->c_begin = c->c_nexti; |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 3343 | com_set_lineno(c, c->c_last_line); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3344 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3345 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3346 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3347 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3348 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3349 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3350 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3351 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3352 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3353 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3354 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3355 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3356 | if (NCH(n) > 8) |
| 3357 | com_node(c, CHILD(n, 8)); |
| 3358 | com_backpatch(c, break_anchor); |
| 3359 | } |
| 3360 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3361 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3362 | |
| 3363 | SETUP_FINALLY L |
| 3364 | <code for S> |
| 3365 | POP_BLOCK |
| 3366 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3367 | L: <code for Sf> |
| 3368 | END_FINALLY |
| 3369 | |
| 3370 | The special instructions use the block stack. Each block |
| 3371 | stack entry contains the instruction that created it (here |
| 3372 | SETUP_FINALLY), the level of the value stack at the time the |
| 3373 | block stack entry was created, and a label (here L). |
| 3374 | |
| 3375 | SETUP_FINALLY: |
| 3376 | Pushes the current value stack level and the label |
| 3377 | onto the block stack. |
| 3378 | POP_BLOCK: |
| 3379 | Pops en entry from the block stack, and pops the value |
| 3380 | stack until its level is the same as indicated on the |
| 3381 | block stack. (The label is ignored.) |
| 3382 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3383 | Pops a variable number of entries from the *value* stack |
| 3384 | and re-raises the exception they specify. The number of |
| 3385 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3386 | |
| 3387 | The block stack is unwound when an exception is raised: |
| 3388 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3389 | onto the value stack (and the exception condition is cleared), |
| 3390 | and the interpreter jumps to the label gotten from the block |
| 3391 | stack. |
| 3392 | |
| 3393 | Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...": |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3394 | (The contents of the value stack is shown in [], with the top |
| 3395 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3396 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3397 | |
| 3398 | Value stack Label Instruction Argument |
| 3399 | [] SETUP_EXCEPT L1 |
| 3400 | [] <code for S> |
| 3401 | [] POP_BLOCK |
| 3402 | [] JUMP_FORWARD L0 |
| 3403 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3404 | [tb, val, exc] L1: DUP ) |
| 3405 | [tb, val, exc, exc] <evaluate E1> ) |
| 3406 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3407 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3408 | [tb, val, exc, 1] POP ) |
| 3409 | [tb, val, exc] POP |
| 3410 | [tb, val] <assign to V1> (or POP if no V1) |
| 3411 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3412 | [] <code for S1> |
| 3413 | JUMP_FORWARD L0 |
| 3414 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3415 | [tb, val, exc, 0] L2: POP |
| 3416 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3417 | .............................etc....................... |
| 3418 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3419 | [tb, val, exc, 0] Ln+1: POP |
| 3420 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3421 | |
| 3422 | [] L0: <next statement> |
| 3423 | |
| 3424 | Of course, parts are not generated if Vi or Ei is not present. |
| 3425 | */ |
| 3426 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3427 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3428 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3429 | { |
| 3430 | int except_anchor = 0; |
| 3431 | int end_anchor = 0; |
| 3432 | int else_anchor = 0; |
| 3433 | int i; |
| 3434 | node *ch; |
| 3435 | |
| 3436 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3437 | block_push(c, SETUP_EXCEPT); |
| 3438 | com_node(c, CHILD(n, 2)); |
| 3439 | com_addbyte(c, POP_BLOCK); |
| 3440 | block_pop(c, SETUP_EXCEPT); |
| 3441 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3442 | com_backpatch(c, except_anchor); |
| 3443 | for (i = 3; |
| 3444 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3445 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3446 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3447 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3448 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3449 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | } |
| 3452 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3453 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3454 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3455 | if (NCH(ch) > 1) { |
| 3456 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3457 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3458 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3459 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3460 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3461 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3462 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3463 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3464 | } |
| 3465 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3466 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3467 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3468 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3469 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3470 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3471 | com_pop(c, 1); |
| 3472 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3473 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3474 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3475 | com_node(c, CHILD(n, i+2)); |
| 3476 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3477 | if (except_anchor) { |
| 3478 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3479 | /* We come in with [tb, val, exc, 0] on the |
| 3480 | stack; one pop and it's the same as |
| 3481 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3482 | com_addbyte(c, POP_TOP); |
| 3483 | } |
| 3484 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3485 | /* We actually come in here with [tb, val, exc] but the |
| 3486 | END_FINALLY will zap those and jump around. |
| 3487 | The c_stacklevel does not reflect them so we need not pop |
| 3488 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3489 | com_addbyte(c, END_FINALLY); |
| 3490 | com_backpatch(c, else_anchor); |
| 3491 | if (i < NCH(n)) |
| 3492 | com_node(c, CHILD(n, i+2)); |
| 3493 | com_backpatch(c, end_anchor); |
| 3494 | } |
| 3495 | |
| 3496 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3497 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3498 | { |
| 3499 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3500 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3501 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3502 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3503 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3504 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3505 | com_addbyte(c, POP_BLOCK); |
| 3506 | block_pop(c, SETUP_FINALLY); |
| 3507 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3508 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3509 | /* While the generated code pushes only one item, |
| 3510 | the try-finally handling can enter here with |
| 3511 | up to three items. OK, here are the details: |
| 3512 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3513 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3514 | com_backpatch(c, finally_anchor); |
| 3515 | ch = CHILD(n, NCH(n)-1); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3516 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3517 | com_node(c, ch); |
| 3518 | com_addbyte(c, END_FINALLY); |
| 3519 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3520 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3524 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3525 | { |
| 3526 | REQ(n, try_stmt); |
| 3527 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3528 | | 'try' ':' suite 'finally' ':' suite */ |
| 3529 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3530 | com_try_finally(c, n); |
| 3531 | else |
| 3532 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3535 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3536 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3537 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3538 | int i; |
| 3539 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3540 | /* Label to avoid tail recursion */ |
| 3541 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3542 | switch (TYPE(n)) { |
| 3543 | |
| 3544 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3545 | if (NCH(n) == 1) { |
| 3546 | n = CHILD(n, 0); |
| 3547 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3548 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3549 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3550 | case file_input: |
| 3551 | for (i = 0; i < NCH(n); i++) { |
| 3552 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3553 | if (TYPE(ch) == stmt) { |
| 3554 | n = ch; |
| 3555 | goto next; |
| 3556 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3557 | } |
| 3558 | break; |
| 3559 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3560 | case stmt: |
| 3561 | case simple_stmt: |
| 3562 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3563 | n = CHILD(n, 0); |
| 3564 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3565 | |
| 3566 | case expr_stmt: |
| 3567 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3568 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3569 | case test: |
| 3570 | case and_test: |
| 3571 | case not_test: |
| 3572 | case comparison: |
| 3573 | case expr: |
| 3574 | case xor_expr: |
| 3575 | case and_expr: |
| 3576 | case shift_expr: |
| 3577 | case arith_expr: |
| 3578 | case term: |
| 3579 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3580 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3581 | if (NCH(n) == 1) { |
| 3582 | n = CHILD(n, 0); |
| 3583 | goto next; |
| 3584 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3585 | break; |
| 3586 | |
| 3587 | case atom: |
| 3588 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3589 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3590 | break; |
| 3591 | |
| 3592 | } |
| 3593 | return NULL; |
| 3594 | } |
| 3595 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3596 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3597 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3598 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3599 | /* Don't generate doc-strings if run with -OO */ |
| 3600 | if (Py_OptimizeFlag > 1) |
| 3601 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3602 | n = get_rawdocstring(n); |
| 3603 | if (n == NULL) |
| 3604 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3605 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3608 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3609 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3610 | { |
| 3611 | REQ(n, suite); |
| 3612 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3613 | if (NCH(n) == 1) { |
| 3614 | com_node(c, CHILD(n, 0)); |
| 3615 | } |
| 3616 | else { |
| 3617 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3618 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3619 | node *ch = CHILD(n, i); |
| 3620 | if (TYPE(ch) == stmt) |
| 3621 | com_node(c, ch); |
| 3622 | } |
| 3623 | } |
| 3624 | } |
| 3625 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3626 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3627 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3628 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3629 | { |
| 3630 | int i = c->c_nblocks; |
| 3631 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3632 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3633 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3634 | else if (i <= 0) { |
| 3635 | /* at the outer level */ |
| 3636 | com_error(c, PyExc_SyntaxError, |
| 3637 | "'continue' not properly in loop"); |
| 3638 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3639 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3640 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3641 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3642 | if (c->c_block[j] == SETUP_LOOP) |
| 3643 | break; |
| 3644 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3645 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3646 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3647 | for (; i > j; --i) { |
| 3648 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3649 | c->c_block[i] == SETUP_FINALLY) { |
| 3650 | com_addoparg(c, CONTINUE_LOOP, |
| 3651 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3652 | return; |
| 3653 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3654 | if (c->c_block[i] == END_FINALLY) { |
| 3655 | com_error(c, PyExc_SyntaxError, |
| 3656 | "'continue' not supported inside 'finally' clause"); |
| 3657 | return; |
| 3658 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3659 | } |
| 3660 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3661 | com_error(c, PyExc_SyntaxError, |
| 3662 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3663 | } |
| 3664 | /* XXX Could allow it inside a 'finally' clause |
| 3665 | XXX if we could pop the exception still on the stack */ |
| 3666 | } |
| 3667 | |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3668 | /* Return the number of default values in the argument list. |
| 3669 | |
| 3670 | If a non-default argument follows a default argument, set an |
| 3671 | exception and return -1. |
| 3672 | */ |
| 3673 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3674 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3675 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3676 | { |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3677 | int i, nch, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3678 | if (TYPE(n) == lambdef) { |
| 3679 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3680 | n = CHILD(n, 1); |
| 3681 | } |
| 3682 | else { |
| 3683 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3684 | n = CHILD(n, 2); |
| 3685 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3686 | n = CHILD(n, 1); |
| 3687 | } |
| 3688 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3689 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3690 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3691 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3692 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3693 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3694 | ndefs = 0; |
| 3695 | for (i = 0; i < nch; i++) { |
| 3696 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3697 | if (TYPE(CHILD(n, i)) == STAR || |
| 3698 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3699 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3700 | i++; |
| 3701 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3702 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3703 | else |
| 3704 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3705 | if (t == EQUAL) { |
| 3706 | i++; |
| 3707 | ndefs++; |
| 3708 | com_node(c, CHILD(n, i)); |
| 3709 | i++; |
| 3710 | if (i >= nch) |
| 3711 | break; |
| 3712 | t = TYPE(CHILD(n, i)); |
| 3713 | } |
| 3714 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3715 | /* Treat "(a=1, b)" as an error */ |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3716 | if (ndefs) { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3717 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3718 | "non-default argument follows default argument"); |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3719 | return -1; |
| 3720 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3721 | } |
| 3722 | if (t != COMMA) |
| 3723 | break; |
| 3724 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3725 | return ndefs; |
| 3726 | } |
| 3727 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3728 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3729 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3730 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3731 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3732 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3733 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3734 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3735 | if (ndefs < 0) |
| 3736 | return; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3737 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3738 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3739 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3740 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3741 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3742 | c->c_errors++; |
| 3743 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3744 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3745 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3746 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3747 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3748 | if (closure) |
| 3749 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3750 | else |
| 3751 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3752 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3753 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3754 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3755 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3760 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3761 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3762 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3763 | REQ(n, testlist); |
| 3764 | /* testlist: test (',' test)* [','] */ |
| 3765 | for (i = 0; i < NCH(n); i += 2) |
| 3766 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3767 | i = (NCH(n)+1) / 2; |
| 3768 | com_addoparg(c, BUILD_TUPLE, i); |
| 3769 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3773 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3774 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3775 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3776 | PyObject *v; |
| 3777 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3778 | char *name; |
| 3779 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3780 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3781 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3782 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3783 | c->c_errors++; |
| 3784 | return; |
| 3785 | } |
| 3786 | /* Push the class name on the stack */ |
| 3787 | i = com_addconst(c, v); |
| 3788 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3789 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3790 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3791 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3792 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3793 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3794 | com_push(c, 1); |
| 3795 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3796 | else |
| 3797 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3798 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3799 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3800 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3801 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3802 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3803 | c->c_errors++; |
| 3804 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3805 | int closure = com_make_closure(c, co); |
| 3806 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3807 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3808 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3809 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3810 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3811 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3812 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3813 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3814 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3815 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3816 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3817 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3818 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3819 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3820 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3824 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3825 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3826 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3827 | if (c->c_errors) |
| 3828 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3829 | switch (TYPE(n)) { |
| 3830 | |
| 3831 | /* Definition nodes */ |
| 3832 | |
| 3833 | case funcdef: |
| 3834 | com_funcdef(c, n); |
| 3835 | break; |
| 3836 | case classdef: |
| 3837 | com_classdef(c, n); |
| 3838 | break; |
| 3839 | |
| 3840 | /* Trivial parse tree nodes */ |
| 3841 | |
| 3842 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3843 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3844 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3845 | n = CHILD(n, 0); |
| 3846 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3847 | |
| 3848 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3849 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3850 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3851 | { |
| 3852 | int i; |
| 3853 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3854 | com_node(c, CHILD(n, i)); |
| 3855 | } |
| 3856 | break; |
| 3857 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3858 | case compound_stmt: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3859 | com_set_lineno(c, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3860 | n = CHILD(n, 0); |
| 3861 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3862 | |
| 3863 | /* Statement nodes */ |
| 3864 | |
| 3865 | case expr_stmt: |
| 3866 | com_expr_stmt(c, n); |
| 3867 | break; |
| 3868 | case print_stmt: |
| 3869 | com_print_stmt(c, n); |
| 3870 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3871 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3872 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3873 | break; |
| 3874 | case pass_stmt: |
| 3875 | break; |
| 3876 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3877 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3878 | com_error(c, PyExc_SyntaxError, |
| 3879 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3880 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3881 | com_addbyte(c, BREAK_LOOP); |
| 3882 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3883 | case continue_stmt: |
| 3884 | com_continue_stmt(c, n); |
| 3885 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3886 | case return_stmt: |
| 3887 | com_return_stmt(c, n); |
| 3888 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3889 | case yield_stmt: |
| 3890 | com_yield_stmt(c, n); |
| 3891 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3892 | case raise_stmt: |
| 3893 | com_raise_stmt(c, n); |
| 3894 | break; |
| 3895 | case import_stmt: |
| 3896 | com_import_stmt(c, n); |
| 3897 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3898 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3899 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3900 | case exec_stmt: |
| 3901 | com_exec_stmt(c, n); |
| 3902 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3903 | case assert_stmt: |
| 3904 | com_assert_stmt(c, n); |
| 3905 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3906 | case if_stmt: |
| 3907 | com_if_stmt(c, n); |
| 3908 | break; |
| 3909 | case while_stmt: |
| 3910 | com_while_stmt(c, n); |
| 3911 | break; |
| 3912 | case for_stmt: |
| 3913 | com_for_stmt(c, n); |
| 3914 | break; |
| 3915 | case try_stmt: |
| 3916 | com_try_stmt(c, n); |
| 3917 | break; |
| 3918 | case suite: |
| 3919 | com_suite(c, n); |
| 3920 | break; |
| 3921 | |
| 3922 | /* Expression nodes */ |
| 3923 | |
| 3924 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3925 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3926 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3927 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3928 | break; |
| 3929 | case test: |
| 3930 | com_test(c, n); |
| 3931 | break; |
| 3932 | case and_test: |
| 3933 | com_and_test(c, n); |
| 3934 | break; |
| 3935 | case not_test: |
| 3936 | com_not_test(c, n); |
| 3937 | break; |
| 3938 | case comparison: |
| 3939 | com_comparison(c, n); |
| 3940 | break; |
| 3941 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3942 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3943 | break; |
| 3944 | case expr: |
| 3945 | com_expr(c, n); |
| 3946 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3947 | case xor_expr: |
| 3948 | com_xor_expr(c, n); |
| 3949 | break; |
| 3950 | case and_expr: |
| 3951 | com_and_expr(c, n); |
| 3952 | break; |
| 3953 | case shift_expr: |
| 3954 | com_shift_expr(c, n); |
| 3955 | break; |
| 3956 | case arith_expr: |
| 3957 | com_arith_expr(c, n); |
| 3958 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3959 | case term: |
| 3960 | com_term(c, n); |
| 3961 | break; |
| 3962 | case factor: |
| 3963 | com_factor(c, n); |
| 3964 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3965 | case power: |
| 3966 | com_power(c, n); |
| 3967 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3968 | case atom: |
| 3969 | com_atom(c, n); |
| 3970 | break; |
| 3971 | |
| 3972 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3973 | com_error(c, PyExc_SystemError, |
| 3974 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3975 | } |
| 3976 | } |
| 3977 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3978 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3979 | |
| 3980 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3981 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3982 | { |
| 3983 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3984 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3985 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3986 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3987 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3988 | com_pop(c, 1); |
| 3989 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3993 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3994 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3995 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3996 | if (NCH(n) == 1) { |
| 3997 | com_fpdef(c, CHILD(n, 0)); |
| 3998 | } |
| 3999 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4000 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 4001 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4002 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4003 | for (i = 0; i < NCH(n); i += 2) |
| 4004 | com_fpdef(c, CHILD(n, i)); |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4009 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4010 | { |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4011 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4012 | int complex = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4013 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4014 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 4015 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4016 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4017 | nch = NCH(n); |
| 4018 | /* Enter all arguments in table of locals */ |
| 4019 | for (i = 0, narg = 0; i < nch; i++) { |
| 4020 | node *ch = CHILD(n, i); |
| 4021 | node *fp; |
| 4022 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4023 | break; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4024 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4025 | fp = CHILD(ch, 0); |
| 4026 | if (TYPE(fp) != NAME) { |
| 4027 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
| 4028 | complex = 1; |
Guido van Rossum | 9c8a086 | 2002-12-23 16:35:23 +0000 | [diff] [blame] | 4029 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4030 | narg++; |
| 4031 | /* all name updates handled by symtable */ |
| 4032 | if (++i >= nch) |
| 4033 | break; |
| 4034 | ch = CHILD(n, i); |
| 4035 | if (TYPE(ch) == EQUAL) |
| 4036 | i += 2; |
| 4037 | else |
| 4038 | REQ(ch, COMMA); |
| 4039 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4040 | if (complex) { |
| 4041 | /* Generate code for complex arguments only after |
| 4042 | having counted the simple arguments */ |
| 4043 | int ilocal = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4044 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4045 | node *ch = CHILD(n, i); |
| 4046 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 4047 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4048 | break; |
| 4049 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4050 | fp = CHILD(ch, 0); |
| 4051 | if (TYPE(fp) != NAME) { |
| 4052 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4053 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4054 | com_fpdef(c, ch); |
| 4055 | } |
| 4056 | ilocal++; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4057 | if (++i >= nch) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4058 | break; |
| 4059 | ch = CHILD(n, i); |
| 4060 | if (TYPE(ch) == EQUAL) |
| 4061 | i += 2; |
| 4062 | else |
| 4063 | REQ(ch, COMMA); |
| 4064 | } |
| 4065 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4069 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4070 | { |
| 4071 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4072 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4073 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4074 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4075 | if (doc != NULL) { |
| 4076 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4077 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4078 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4079 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4080 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4081 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4082 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4083 | for (i = 0; i < NCH(n); i++) { |
| 4084 | node *ch = CHILD(n, i); |
| 4085 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 4086 | com_node(c, ch); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | /* Top-level compile-node interface */ |
| 4091 | |
| 4092 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4093 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4094 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4095 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4096 | node *ch; |
| 4097 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4098 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4099 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4100 | if (doc != NULL) { |
| 4101 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4102 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4103 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4104 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4105 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4106 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4107 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4108 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4109 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4110 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4111 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4112 | c->c_infunction = 0; |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4113 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4114 | com_push(c, 1); |
| 4115 | com_addbyte(c, RETURN_VALUE); |
| 4116 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4120 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4121 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4122 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4123 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4124 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4125 | |
| 4126 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4127 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4128 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4129 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4130 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4131 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4132 | else |
| 4133 | ch = CHILD(n, 2); |
| 4134 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4135 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4136 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4140 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4141 | { |
| 4142 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4143 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4144 | REQ(n, classdef); |
| 4145 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4146 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4147 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4148 | /* Initialize local __module__ from global __name__ */ |
| 4149 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4150 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4151 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4152 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4153 | if (doc != NULL) { |
| 4154 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4155 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4156 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4157 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4158 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4159 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4160 | } |
| 4161 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4162 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4163 | com_node(c, ch); |
| 4164 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4165 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4166 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4167 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4171 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4172 | { |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4173 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4174 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4175 | switch (TYPE(n)) { |
| 4176 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4177 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4178 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4179 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4180 | n = CHILD(n, 0); |
| 4181 | if (TYPE(n) != NEWLINE) |
| 4182 | com_node(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4183 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4184 | com_push(c, 1); |
| 4185 | com_addbyte(c, RETURN_VALUE); |
| 4186 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4187 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4188 | break; |
| 4189 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4190 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4191 | com_file_input(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4192 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4193 | com_push(c, 1); |
| 4194 | com_addbyte(c, RETURN_VALUE); |
| 4195 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4196 | break; |
| 4197 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4198 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4199 | com_node(c, CHILD(n, 0)); |
| 4200 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4201 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4202 | break; |
| 4203 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4204 | case lambdef: /* anonymous function definition */ |
| 4205 | compile_lambdef(c, n); |
| 4206 | break; |
| 4207 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4208 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4209 | compile_funcdef(c, n); |
| 4210 | break; |
| 4211 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4212 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4213 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4214 | break; |
| 4215 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4216 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4217 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4218 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4222 | static PyObject * |
| 4223 | dict_keys_inorder(PyObject *dict, int offset) |
| 4224 | { |
| 4225 | PyObject *tuple, *k, *v; |
| 4226 | int i, pos = 0, size = PyDict_Size(dict); |
| 4227 | |
| 4228 | tuple = PyTuple_New(size); |
| 4229 | if (tuple == NULL) |
| 4230 | return NULL; |
| 4231 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4232 | i = PyInt_AS_LONG(v); |
| 4233 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4234 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4235 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4236 | } |
| 4237 | return tuple; |
| 4238 | } |
| 4239 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4240 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4241 | PyNode_Compile(node *n, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4242 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4243 | return PyNode_CompileFlags(n, filename, NULL); |
| 4244 | } |
| 4245 | |
| 4246 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4247 | PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4248 | { |
| 4249 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4252 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4253 | PyNode_CompileSymtable(node *n, const char *filename) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4254 | { |
| 4255 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4256 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4257 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4258 | ff = PyNode_Future(n, filename); |
| 4259 | if (ff == NULL) |
| 4260 | return NULL; |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4261 | st = symtable_build(n, ff, filename); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4262 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4263 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4264 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4265 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4266 | return st; |
| 4267 | } |
| 4268 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4269 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4270 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4271 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4272 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4273 | } |
| 4274 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4275 | static PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4276 | jcompile(node *n, const char *filename, struct compiling *base, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4277 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4278 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4279 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4280 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4281 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4282 | return NULL; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4283 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { |
| 4284 | sc.c_encoding = "utf-8"; |
| 4285 | } else if (TYPE(n) == encoding_decl) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4286 | sc.c_encoding = STR(n); |
| 4287 | n = CHILD(n, 0); |
| 4288 | } else { |
| 4289 | sc.c_encoding = NULL; |
| 4290 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4291 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4292 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4293 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4294 | /* c_symtable still points to parent's symbols */ |
| 4295 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4296 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4297 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4298 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4299 | if (base->c_encoding != NULL) { |
| 4300 | assert(sc.c_encoding == NULL); |
| 4301 | sc.c_encoding = base->c_encoding; |
| 4302 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4303 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4304 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4305 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4306 | if (sc.c_future == NULL) { |
| 4307 | com_free(&sc); |
| 4308 | return NULL; |
| 4309 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4310 | if (flags) { |
| 4311 | int merged = sc.c_future->ff_features | |
| 4312 | flags->cf_flags; |
| 4313 | sc.c_future->ff_features = merged; |
| 4314 | flags->cf_flags = merged; |
| 4315 | } |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4316 | sc.c_symtable = symtable_build(n, sc.c_future, sc.c_filename); |
| 4317 | if (sc.c_symtable == NULL) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4318 | com_free(&sc); |
| 4319 | return NULL; |
| 4320 | } |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4321 | /* reset symbol table for second pass */ |
| 4322 | sc.c_symtable->st_nscopes = 1; |
| 4323 | sc.c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4324 | } |
| 4325 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4326 | if (symtable_load_symbols(&sc) < 0) { |
| 4327 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4328 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4329 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4330 | compile_node(&sc, n); |
| 4331 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4332 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4333 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4334 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4335 | consts = PyList_AsTuple(sc.c_consts); |
| 4336 | names = PyList_AsTuple(sc.c_names); |
| 4337 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4338 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4339 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4340 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4341 | filename = PyString_InternFromString(sc.c_filename); |
| 4342 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4343 | if (!PyErr_Occurred()) |
| 4344 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4345 | sc.c_nlocals, |
| 4346 | sc.c_maxstacklevel, |
| 4347 | sc.c_flags, |
| 4348 | sc.c_code, |
| 4349 | consts, |
| 4350 | names, |
| 4351 | varnames, |
| 4352 | freevars, |
| 4353 | cellvars, |
| 4354 | filename, |
| 4355 | name, |
| 4356 | sc.c_firstlineno, |
| 4357 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4358 | Py_XDECREF(consts); |
| 4359 | Py_XDECREF(names); |
| 4360 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4361 | Py_XDECREF(freevars); |
| 4362 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4363 | Py_XDECREF(filename); |
| 4364 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4365 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4366 | else if (!PyErr_Occurred()) { |
| 4367 | /* This could happen if someone called PyErr_Clear() after an |
| 4368 | error was reported above. That's not supposed to happen, |
| 4369 | but I just plugged one case and I'm not sure there can't be |
| 4370 | others. In that case, raise SystemError so that at least |
| 4371 | it gets reported instead dumping core. */ |
| 4372 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4373 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4374 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4375 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4376 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4377 | sc.c_symtable = NULL; |
| 4378 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4379 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4380 | return co; |
| 4381 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4382 | |
| 4383 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4384 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4385 | { |
| 4386 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4387 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4388 | int line = co->co_firstlineno; |
| 4389 | int addr = 0; |
| 4390 | while (--size >= 0) { |
| 4391 | addr += *p++; |
| 4392 | if (addr > addrq) |
| 4393 | break; |
| 4394 | line += *p++; |
| 4395 | } |
| 4396 | return line; |
| 4397 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4398 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4399 | /* The test for LOCAL must come before the test for FREE in order to |
| 4400 | handle classes where name is both local and free. The local var is |
| 4401 | a method and the free var is a free var referenced within a method. |
| 4402 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4403 | |
| 4404 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4405 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4406 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4407 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4408 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4409 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4410 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4411 | return CELL; |
| 4412 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4413 | return LOCAL; |
| 4414 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4415 | return FREE; |
| 4416 | v = PyDict_GetItemString(c->c_globals, name); |
| 4417 | if (v) { |
| 4418 | if (v == Py_None) |
| 4419 | return GLOBAL_EXPLICIT; |
| 4420 | else { |
| 4421 | return GLOBAL_IMPLICIT; |
| 4422 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4423 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4424 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4425 | "unknown scope for %.100s in %.100s(%s) " |
| 4426 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4427 | name, c->c_name, |
| 4428 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4429 | c->c_filename, |
| 4430 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4431 | PyObject_REPR(c->c_locals), |
| 4432 | PyObject_REPR(c->c_globals) |
| 4433 | ); |
| 4434 | |
| 4435 | Py_FatalError(buf); |
| 4436 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4439 | /* Helper functions to issue warnings */ |
| 4440 | |
| 4441 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4442 | issue_warning(const char *msg, const char *filename, int lineno) |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4443 | { |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4444 | if (PyErr_Occurred()) { |
| 4445 | /* This can happen because symtable_node continues |
| 4446 | processing even after raising a SyntaxError. |
| 4447 | Calling PyErr_WarnExplicit now would clobber the |
| 4448 | pending exception; instead we fail and let that |
| 4449 | exception propagate. |
| 4450 | */ |
| 4451 | return -1; |
| 4452 | } |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4453 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4454 | lineno, NULL, NULL) < 0) { |
| 4455 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4456 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4457 | PyErr_SyntaxLocation(filename, lineno); |
| 4458 | } |
| 4459 | return -1; |
| 4460 | } |
| 4461 | return 0; |
| 4462 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4463 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4464 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4465 | symtable_warn(struct symtable *st, char *msg) |
| 4466 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4467 | if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) { |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4468 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4469 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4470 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4471 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4474 | /* Helper function for setting lineno and filename */ |
| 4475 | |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4476 | static struct symtable * |
| 4477 | symtable_build(node *n, PyFutureFeatures *ff, const char *filename) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4478 | { |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4479 | struct symtable *st; |
| 4480 | |
| 4481 | st = symtable_init(); |
| 4482 | if (st == NULL) |
| 4483 | return NULL; |
| 4484 | st->st_future = ff; |
| 4485 | st->st_filename = filename; |
| 4486 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
| 4487 | if (st->st_errors > 0) |
| 4488 | goto fail; |
| 4489 | symtable_node(st, n); |
| 4490 | if (st->st_errors > 0) |
| 4491 | goto fail; |
| 4492 | return st; |
| 4493 | fail: |
| 4494 | if (!PyErr_Occurred()) { |
| 4495 | /* This could happen because after a syntax error is |
| 4496 | detected, the symbol-table-building continues for |
| 4497 | a while, and PyErr_Clear() might erroneously be |
| 4498 | called during that process. One such case has been |
| 4499 | fixed, but there might be more (now or later). |
| 4500 | */ |
| 4501 | PyErr_SetString(PyExc_SystemError, "lost exception"); |
| 4502 | } |
| 4503 | st->st_future = NULL; |
| 4504 | st->st_filename = NULL; |
| 4505 | PySymtable_Free(st); |
| 4506 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4510 | symtable_init_compiling_symbols(struct compiling *c) |
| 4511 | { |
| 4512 | PyObject *varnames; |
| 4513 | |
| 4514 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4515 | if (varnames == NULL) { |
| 4516 | varnames = PyList_New(0); |
| 4517 | if (varnames == NULL) |
| 4518 | return -1; |
| 4519 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4520 | Py_INCREF(varnames); |
| 4521 | } else |
| 4522 | Py_INCREF(varnames); |
| 4523 | c->c_varnames = varnames; |
| 4524 | |
| 4525 | c->c_globals = PyDict_New(); |
| 4526 | if (c->c_globals == NULL) |
| 4527 | return -1; |
| 4528 | c->c_freevars = PyDict_New(); |
| 4529 | if (c->c_freevars == NULL) |
| 4530 | return -1; |
| 4531 | c->c_cellvars = PyDict_New(); |
| 4532 | if (c->c_cellvars == NULL) |
| 4533 | return -1; |
| 4534 | return 0; |
| 4535 | } |
| 4536 | |
| 4537 | struct symbol_info { |
| 4538 | int si_nlocals; |
| 4539 | int si_ncells; |
| 4540 | int si_nfrees; |
| 4541 | int si_nimplicit; |
| 4542 | }; |
| 4543 | |
| 4544 | static void |
| 4545 | symtable_init_info(struct symbol_info *si) |
| 4546 | { |
| 4547 | si->si_nlocals = 0; |
| 4548 | si->si_ncells = 0; |
| 4549 | si->si_nfrees = 0; |
| 4550 | si->si_nimplicit = 0; |
| 4551 | } |
| 4552 | |
| 4553 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4554 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4555 | struct symbol_info *si) |
| 4556 | { |
| 4557 | PyObject *dict, *v; |
| 4558 | |
| 4559 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4560 | it indicates a local that we must allocate storage for (a |
| 4561 | cell var). If it occurs in a class, then the class has a |
| 4562 | method and a free variable with the same name. |
| 4563 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4564 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4565 | /* If it isn't declared locally, it can't be a cell. */ |
| 4566 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4567 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4568 | v = PyInt_FromLong(si->si_ncells++); |
| 4569 | dict = c->c_cellvars; |
| 4570 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4571 | /* If it is free anyway, then there is no need to do |
| 4572 | anything here. |
| 4573 | */ |
| 4574 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4575 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4576 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4577 | v = PyInt_FromLong(si->si_nfrees++); |
| 4578 | dict = c->c_freevars; |
| 4579 | } |
| 4580 | if (v == NULL) |
| 4581 | return -1; |
| 4582 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4583 | Py_DECREF(v); |
| 4584 | return -1; |
| 4585 | } |
| 4586 | Py_DECREF(v); |
| 4587 | return 0; |
| 4588 | } |
| 4589 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4590 | /* If a variable is a cell and an argument, make sure that appears in |
| 4591 | co_cellvars before any variable to its right in varnames. |
| 4592 | */ |
| 4593 | |
| 4594 | |
| 4595 | static int |
| 4596 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4597 | PyObject *varnames, int flags) |
| 4598 | { |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4599 | PyObject *v = NULL; |
| 4600 | PyObject *w, *d, *list = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4601 | int i, pos; |
| 4602 | |
| 4603 | if (flags & CO_VARARGS) |
| 4604 | argcount++; |
| 4605 | if (flags & CO_VARKEYWORDS) |
| 4606 | argcount++; |
| 4607 | for (i = argcount; --i >= 0; ) { |
| 4608 | v = PyList_GET_ITEM(varnames, i); |
| 4609 | if (PyDict_GetItem(*cellvars, v)) { |
| 4610 | if (list == NULL) { |
| 4611 | list = PyList_New(1); |
| 4612 | if (list == NULL) |
| 4613 | return -1; |
| 4614 | PyList_SET_ITEM(list, 0, v); |
| 4615 | Py_INCREF(v); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4616 | } else { |
| 4617 | if (PyList_Insert(list, 0, v) < 0) { |
| 4618 | Py_DECREF(list); |
| 4619 | return -1; |
| 4620 | } |
| 4621 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4622 | } |
| 4623 | } |
Jeremy Hylton | 521482d | 2003-05-22 15:47:02 +0000 | [diff] [blame] | 4624 | if (list == NULL) |
| 4625 | return 0; |
| 4626 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4627 | /* There are cellvars that are also arguments. Create a dict |
| 4628 | to replace cellvars and put the args at the front. |
| 4629 | */ |
| 4630 | d = PyDict_New(); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4631 | if (d == NULL) |
| 4632 | return -1; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4633 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4634 | v = PyInt_FromLong(i); |
| 4635 | if (v == NULL) |
| 4636 | goto fail; |
| 4637 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4638 | goto fail; |
| 4639 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4640 | goto fail; |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4641 | Py_DECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4642 | } |
| 4643 | pos = 0; |
| 4644 | i = PyList_GET_SIZE(list); |
| 4645 | Py_DECREF(list); |
| 4646 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4647 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4648 | if (w == NULL) |
| 4649 | goto fail; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4650 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4651 | Py_DECREF(w); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4652 | v = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4653 | goto fail; |
| 4654 | } |
| 4655 | Py_DECREF(w); |
| 4656 | } |
| 4657 | Py_DECREF(*cellvars); |
| 4658 | *cellvars = d; |
| 4659 | return 1; |
| 4660 | fail: |
| 4661 | Py_DECREF(d); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4662 | Py_XDECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4663 | return -1; |
| 4664 | } |
| 4665 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4666 | static int |
| 4667 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4668 | { |
| 4669 | PyObject *name, *v; |
| 4670 | int pos; |
| 4671 | |
| 4672 | /* The cell vars are the first elements of the closure, |
| 4673 | followed by the free vars. Update the offsets in |
| 4674 | c_freevars to account for number of cellvars. */ |
| 4675 | pos = 0; |
| 4676 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4677 | int i = PyInt_AS_LONG(v) + offset; |
| 4678 | PyObject *o = PyInt_FromLong(i); |
| 4679 | if (o == NULL) |
| 4680 | return -1; |
| 4681 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4682 | Py_DECREF(o); |
| 4683 | return -1; |
| 4684 | } |
| 4685 | Py_DECREF(o); |
| 4686 | } |
| 4687 | return 0; |
| 4688 | } |
| 4689 | |
| 4690 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4691 | symtable_check_unoptimized(struct compiling *c, |
| 4692 | PySymtableEntryObject *ste, |
| 4693 | struct symbol_info *si) |
| 4694 | { |
| 4695 | char buf[300]; |
| 4696 | |
| 4697 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4698 | || (ste->ste_nested && si->si_nimplicit))) |
| 4699 | return 0; |
| 4700 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4701 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4702 | |
| 4703 | #define ILLEGAL_IS "is a nested function" |
| 4704 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4705 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4706 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4707 | |
| 4708 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4709 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4710 | |
| 4711 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4712 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4713 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4714 | |
| 4715 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4716 | should be stored so the exception can point to them. */ |
| 4717 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4718 | if (ste->ste_child_free) { |
| 4719 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4720 | PyOS_snprintf(buf, sizeof(buf), |
| 4721 | ILLEGAL_IMPORT_STAR, |
| 4722 | PyString_AS_STRING(ste->ste_name), |
| 4723 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4724 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4725 | PyOS_snprintf(buf, sizeof(buf), |
| 4726 | ILLEGAL_BARE_EXEC, |
| 4727 | PyString_AS_STRING(ste->ste_name), |
| 4728 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4729 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4730 | PyOS_snprintf(buf, sizeof(buf), |
| 4731 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4732 | PyString_AS_STRING(ste->ste_name), |
| 4733 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4734 | } |
| 4735 | } else { |
| 4736 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4737 | PyOS_snprintf(buf, sizeof(buf), |
| 4738 | ILLEGAL_IMPORT_STAR, |
| 4739 | PyString_AS_STRING(ste->ste_name), |
| 4740 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4741 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4742 | PyOS_snprintf(buf, sizeof(buf), |
| 4743 | ILLEGAL_BARE_EXEC, |
| 4744 | PyString_AS_STRING(ste->ste_name), |
| 4745 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4746 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4747 | PyOS_snprintf(buf, sizeof(buf), |
| 4748 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4749 | PyString_AS_STRING(ste->ste_name), |
| 4750 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4751 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4752 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4753 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4754 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4755 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4756 | ste->ste_opt_lineno); |
| 4757 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4761 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4762 | struct symbol_info *si) |
| 4763 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4764 | if (c->c_future) |
| 4765 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4766 | if (ste->ste_generator) |
| 4767 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4768 | if (ste->ste_type != TYPE_MODULE) |
| 4769 | c->c_flags |= CO_NEWLOCALS; |
| 4770 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4771 | c->c_nlocals = si->si_nlocals; |
| 4772 | if (ste->ste_optimized == 0) |
| 4773 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4774 | else if (ste->ste_optimized != OPT_EXEC) |
| 4775 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4776 | } |
| 4777 | return 0; |
| 4778 | } |
| 4779 | |
| 4780 | static int |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 4781 | symtable_error(struct symtable *st, int lineno) |
| 4782 | { |
| 4783 | if (lineno == 0) |
| 4784 | lineno = st->st_cur->ste_lineno; |
| 4785 | PyErr_SyntaxLocation(st->st_filename, lineno); |
| 4786 | st->st_errors++; |
| 4787 | return -1; |
| 4788 | } |
| 4789 | |
| 4790 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4791 | symtable_load_symbols(struct compiling *c) |
| 4792 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4793 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4794 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4795 | PyObject *name, *varnames, *v; |
| 4796 | int i, flags, pos; |
| 4797 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4798 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4799 | v = NULL; |
| 4800 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4801 | if (symtable_init_compiling_symbols(c) < 0) |
| 4802 | goto fail; |
| 4803 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4804 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4805 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4806 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4807 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4808 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4809 | v = PyInt_FromLong(i); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4810 | if (v == NULL) |
| 4811 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4812 | if (PyDict_SetItem(c->c_locals, |
| 4813 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4814 | goto fail; |
| 4815 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | /* XXX The cases below define the rules for whether a name is |
| 4819 | local or global. The logic could probably be clearer. */ |
| 4820 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4821 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4822 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4823 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4824 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4825 | /* undo the original DEF_FREE */ |
| 4826 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4827 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4828 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4829 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4830 | 2. Free variables in methods that are also class |
| 4831 | variables or declared global. |
| 4832 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4833 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4834 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4835 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4836 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4837 | c->c_argcount--; |
| 4838 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4839 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4840 | c->c_argcount--; |
| 4841 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4842 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4843 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4844 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4845 | if (flags & DEF_PARAM) { |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 4846 | PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4847 | PyString_AS_STRING(name)); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 4848 | symtable_error(st, 0); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4849 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4850 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4851 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4852 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4853 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4854 | si.si_nimplicit++; |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4855 | if (PyDict_SetItem(c->c_globals, name, Py_True) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4856 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4857 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4858 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4859 | if (v == NULL) |
| 4860 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4861 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4862 | goto fail; |
| 4863 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4864 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4865 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4866 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4867 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4868 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4869 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4870 | if (v == NULL) |
| 4871 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4872 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4873 | goto fail; |
| 4874 | Py_DECREF(v); |
| 4875 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4876 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4877 | if (PyDict_SetItem(c->c_globals, name, |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4878 | Py_True) < 0) |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4879 | goto fail; |
| 4880 | if (st->st_nscopes != 1) { |
| 4881 | v = PyInt_FromLong(flags); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4882 | if (v == NULL) |
| 4883 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4884 | if (PyDict_SetItem(st->st_global, |
| 4885 | name, v)) |
| 4886 | goto fail; |
| 4887 | Py_DECREF(v); |
| 4888 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4889 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4890 | } |
| 4891 | } |
| 4892 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4893 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4894 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4895 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4896 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4897 | c->c_varnames, c->c_flags) < 0) |
| 4898 | return -1; |
| 4899 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4900 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4901 | return -1; |
| 4902 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4903 | fail: |
| 4904 | /* is this always the right thing to do? */ |
| 4905 | Py_XDECREF(v); |
| 4906 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4910 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4911 | { |
| 4912 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4913 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4914 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4915 | if (st == NULL) |
| 4916 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4917 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4918 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4919 | st->st_filename = NULL; |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4920 | st->st_symbols = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4921 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4922 | goto fail; |
| 4923 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4924 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4925 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4926 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4927 | st->st_errors = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4928 | st->st_private = NULL; |
| 4929 | return st; |
| 4930 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4931 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4932 | return NULL; |
| 4933 | } |
| 4934 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4935 | void |
| 4936 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4937 | { |
| 4938 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4939 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4940 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4941 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4944 | /* When the compiler exits a scope, it must should update the scope's |
| 4945 | free variable information with the list of free variables in its |
| 4946 | children. |
| 4947 | |
| 4948 | Variables that are free in children and defined in the current |
| 4949 | scope are cellvars. |
| 4950 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4951 | If the scope being exited is defined at the top-level (ste_nested is |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4952 | false), free variables in children that are not defined here are |
| 4953 | implicit globals. |
| 4954 | |
| 4955 | */ |
| 4956 | |
| 4957 | static int |
| 4958 | symtable_update_free_vars(struct symtable *st) |
| 4959 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4960 | int i, j, def; |
| 4961 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4962 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4963 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4964 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4965 | def = DEF_FREE_CLASS; |
| 4966 | else |
| 4967 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4968 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4969 | int pos = 0; |
| 4970 | |
Jeremy Hylton | f9415e6 | 2003-05-22 16:22:33 +0000 | [diff] [blame] | 4971 | if (list && PyList_SetSlice(list, 0, |
| 4972 | PyList_GET_SIZE(list), 0) < 0) |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4973 | return -1; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4974 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4975 | PyList_GET_ITEM(ste->ste_children, i); |
| 4976 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4977 | int flags = PyInt_AS_LONG(o); |
| 4978 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4979 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4980 | if (list == NULL) { |
| 4981 | list = PyList_New(0); |
| 4982 | if (list == NULL) |
| 4983 | return -1; |
| 4984 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4985 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4986 | if (PyList_Append(list, name) < 0) { |
| 4987 | Py_DECREF(list); |
| 4988 | return -1; |
| 4989 | } |
| 4990 | } |
| 4991 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4992 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4993 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4994 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4995 | /* If a name N is declared global in scope A and |
| 4996 | referenced in scope B contained (perhaps |
| 4997 | indirectly) in A and there are no scopes |
| 4998 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4999 | is global in B. Unless A is a class scope, |
| 5000 | because class scopes are not considered for |
| 5001 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5002 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 5003 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5004 | int flags = PyInt_AS_LONG(v); |
| 5005 | if (flags & DEF_GLOBAL) { |
| 5006 | symtable_undo_free(st, child->ste_id, |
| 5007 | name); |
| 5008 | continue; |
| 5009 | } |
| 5010 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5011 | if (ste->ste_nested) { |
| 5012 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5013 | name, def) < 0) { |
| 5014 | Py_DECREF(list); |
| 5015 | return -1; |
| 5016 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5017 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5018 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5019 | name) < 0) { |
| 5020 | Py_DECREF(list); |
| 5021 | return -1; |
| 5022 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5023 | } |
| 5024 | } |
| 5025 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5026 | |
| 5027 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5028 | return 0; |
| 5029 | } |
| 5030 | |
| 5031 | /* If the current scope is a non-nested class or if name is not |
| 5032 | defined in the current, non-nested scope, then it is an implicit |
| 5033 | global in all nested scopes. |
| 5034 | */ |
| 5035 | |
| 5036 | static int |
| 5037 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 5038 | { |
| 5039 | PyObject *o; |
| 5040 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5041 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5042 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5043 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5044 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5045 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5046 | if (o == NULL) |
| 5047 | return symtable_undo_free(st, child, name); |
| 5048 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5049 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5050 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5051 | return symtable_undo_free(st, child, name); |
| 5052 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5053 | return symtable_add_def_o(st, ste->ste_symbols, |
| 5054 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | static int |
| 5058 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 5059 | PyObject *name) |
| 5060 | { |
| 5061 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5062 | PyObject *info; |
| 5063 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5064 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5065 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 5066 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5067 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5068 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5069 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5070 | if (info == NULL) |
| 5071 | return 0; |
| 5072 | v = PyInt_AS_LONG(info); |
| 5073 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5074 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5075 | DEF_FREE_GLOBAL) < 0) |
| 5076 | return -1; |
| 5077 | } else |
| 5078 | /* If the name is defined here or declared global, |
| 5079 | then the recursion stops. */ |
| 5080 | return 0; |
| 5081 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5082 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 5083 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5084 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5085 | PyList_GET_ITEM(ste->ste_children, i); |
| 5086 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5087 | if (x < 0) |
| 5088 | return x; |
| 5089 | } |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5093 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 5094 | This reference is released when the scope is exited, via the DECREF |
| 5095 | in symtable_exit_scope(). |
| 5096 | */ |
| 5097 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5098 | static int |
| 5099 | symtable_exit_scope(struct symtable *st) |
| 5100 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5101 | int end; |
| 5102 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5103 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5104 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5105 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5106 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5107 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 5108 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5109 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 5110 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5111 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5112 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5113 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5114 | static void |
| 5115 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 5116 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5117 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5118 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5119 | |
| 5120 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5121 | prev = st->st_cur; |
| 5122 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5123 | st->st_errors++; |
| 5124 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5125 | } |
| 5126 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5127 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5128 | PySymtableEntry_New(st, name, type, lineno); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5129 | if (st->st_cur == NULL) { |
| 5130 | st->st_errors++; |
| 5131 | return; |
| 5132 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5133 | if (strcmp(name, TOP) == 0) |
| 5134 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5135 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5136 | if (PyList_Append(prev->ste_children, |
| 5137 | (PyObject *)st->st_cur) < 0) |
| 5138 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5139 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5143 | symtable_lookup(struct symtable *st, char *name) |
| 5144 | { |
| 5145 | char buffer[MANGLE_LEN]; |
| 5146 | PyObject *v; |
| 5147 | int flags; |
| 5148 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5149 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5150 | name = buffer; |
| 5151 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5152 | if (v == NULL) { |
| 5153 | if (PyErr_Occurred()) |
| 5154 | return -1; |
| 5155 | else |
| 5156 | return 0; |
| 5157 | } |
| 5158 | |
| 5159 | flags = PyInt_AS_LONG(v); |
| 5160 | return flags; |
| 5161 | } |
| 5162 | |
| 5163 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5164 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5165 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5166 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5167 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5168 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5169 | |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5170 | /* Warn about None, except inside a tuple (where the assignment |
| 5171 | code already issues a warning). */ |
| 5172 | if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) && |
| 5173 | *name == 'N' && strcmp(name, "None") == 0) |
| 5174 | { |
| 5175 | if (symtable_warn(st, "argument named None")) |
| 5176 | return -1; |
| 5177 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5178 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5179 | name = buffer; |
| 5180 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5181 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5182 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5183 | Py_DECREF(s); |
| 5184 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | /* Must only be called with mangled names */ |
| 5188 | |
| 5189 | static int |
| 5190 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5191 | PyObject *name, int flag) |
| 5192 | { |
| 5193 | PyObject *o; |
| 5194 | int val; |
| 5195 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5196 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5197 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5198 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5199 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5200 | PyString_AsString(name)); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5201 | return symtable_error(st, 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5202 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5203 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5204 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5205 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5206 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5207 | if (o == NULL) |
| 5208 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5209 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5210 | Py_DECREF(o); |
| 5211 | return -1; |
| 5212 | } |
| 5213 | Py_DECREF(o); |
| 5214 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5215 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5216 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5217 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5218 | } else if (flag & DEF_GLOBAL) { |
| 5219 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5220 | perhaps only DEF_FREE_GLOBAL */ |
| 5221 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5222 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5223 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5224 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5225 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5226 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5227 | if (o == NULL) |
| 5228 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5229 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5230 | Py_DECREF(o); |
| 5231 | return -1; |
| 5232 | } |
| 5233 | Py_DECREF(o); |
| 5234 | } |
| 5235 | return 0; |
| 5236 | } |
| 5237 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5238 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5239 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5240 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5241 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5242 | ignored) in case those are the only places a yield occurs (so that this |
| 5243 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5244 | static int |
| 5245 | look_for_yield(node *n) |
| 5246 | { |
| 5247 | int i; |
| 5248 | |
| 5249 | for (i = 0; i < NCH(n); ++i) { |
| 5250 | node *kid = CHILD(n, i); |
| 5251 | |
| 5252 | switch (TYPE(kid)) { |
| 5253 | |
| 5254 | case classdef: |
| 5255 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5256 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5257 | /* Stuff in nested functions and classes can't make |
| 5258 | the parent a generator. */ |
| 5259 | return 0; |
| 5260 | |
| 5261 | case yield_stmt: |
| 5262 | return 1; |
| 5263 | |
| 5264 | default: |
| 5265 | if (look_for_yield(kid)) |
| 5266 | return 1; |
| 5267 | } |
| 5268 | } |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5272 | static void |
| 5273 | symtable_node(struct symtable *st, node *n) |
| 5274 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5275 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5276 | |
| 5277 | loop: |
| 5278 | switch (TYPE(n)) { |
| 5279 | case funcdef: { |
| 5280 | char *func_name = STR(CHILD(n, 1)); |
| 5281 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5282 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5283 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5284 | symtable_funcdef(st, n); |
| 5285 | symtable_exit_scope(st); |
| 5286 | break; |
| 5287 | } |
| 5288 | case lambdef: |
| 5289 | if (NCH(n) == 4) |
| 5290 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5291 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5292 | symtable_funcdef(st, n); |
| 5293 | symtable_exit_scope(st); |
| 5294 | break; |
| 5295 | case classdef: { |
| 5296 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5297 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5298 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5299 | node *bases = CHILD(n, 3); |
| 5300 | int i; |
| 5301 | for (i = 0; i < NCH(bases); i += 2) { |
| 5302 | symtable_node(st, CHILD(bases, i)); |
| 5303 | } |
| 5304 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5305 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5306 | tmp = st->st_private; |
| 5307 | st->st_private = class_name; |
| 5308 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5309 | st->st_private = tmp; |
| 5310 | symtable_exit_scope(st); |
| 5311 | break; |
| 5312 | } |
| 5313 | case if_stmt: |
| 5314 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5315 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5316 | if (st->st_cur->ste_generator == 0) |
| 5317 | st->st_cur->ste_generator = |
| 5318 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5319 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5320 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5321 | symtable_node(st, CHILD(n, i + 1)); |
| 5322 | symtable_node(st, CHILD(n, i + 3)); |
| 5323 | } |
| 5324 | if (i + 2 < NCH(n)) |
| 5325 | symtable_node(st, CHILD(n, i + 2)); |
| 5326 | break; |
| 5327 | case global_stmt: |
| 5328 | symtable_global(st, n); |
| 5329 | break; |
| 5330 | case import_stmt: |
| 5331 | symtable_import(st, n); |
| 5332 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5333 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5334 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5335 | symtable_node(st, CHILD(n, 1)); |
| 5336 | if (NCH(n) > 2) |
| 5337 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5338 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5339 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5340 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5341 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5342 | if (NCH(n) > 4) |
| 5343 | symtable_node(st, CHILD(n, 5)); |
| 5344 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5345 | |
| 5346 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5347 | case assert_stmt: |
| 5348 | if (Py_OptimizeFlag) |
| 5349 | return; |
| 5350 | if (NCH(n) == 2) { |
| 5351 | n = CHILD(n, 1); |
| 5352 | goto loop; |
| 5353 | } else { |
| 5354 | symtable_node(st, CHILD(n, 1)); |
| 5355 | n = CHILD(n, 3); |
| 5356 | goto loop; |
| 5357 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5358 | case except_clause: |
| 5359 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5360 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5361 | if (NCH(n) > 1) { |
| 5362 | n = CHILD(n, 1); |
| 5363 | goto loop; |
| 5364 | } |
| 5365 | break; |
| 5366 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5367 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5368 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5369 | case yield_stmt: |
| 5370 | st->st_cur->ste_generator = 1; |
| 5371 | n = CHILD(n, 1); |
| 5372 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5373 | case expr_stmt: |
| 5374 | if (NCH(n) == 1) |
| 5375 | n = CHILD(n, 0); |
| 5376 | else { |
| 5377 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5378 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5379 | symtable_node(st, CHILD(n, 2)); |
| 5380 | break; |
| 5381 | } else { |
| 5382 | int i; |
| 5383 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5384 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5385 | n = CHILD(n, NCH(n) - 1); |
| 5386 | } |
| 5387 | } |
| 5388 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5389 | case list_iter: |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5390 | /* only occurs when there are multiple for loops |
| 5391 | in a list comprehension */ |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5392 | n = CHILD(n, 0); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5393 | if (TYPE(n) == list_for) |
| 5394 | symtable_list_for(st, n); |
| 5395 | else { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5396 | REQ(n, list_if); |
| 5397 | symtable_node(st, CHILD(n, 1)); |
| 5398 | if (NCH(n) == 3) { |
| 5399 | n = CHILD(n, 2); |
| 5400 | goto loop; |
| 5401 | } |
| 5402 | } |
| 5403 | break; |
| 5404 | case for_stmt: |
| 5405 | symtable_assign(st, CHILD(n, 1), 0); |
| 5406 | for (i = 3; i < NCH(n); ++i) |
| 5407 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5408 | symtable_node(st, CHILD(n, i)); |
| 5409 | break; |
| 5410 | /* The remaining cases fall through to default except in |
| 5411 | special circumstances. This requires the individual cases |
| 5412 | to be coded with great care, even though they look like |
| 5413 | rather innocuous. Each case must double-check TYPE(n). |
| 5414 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5415 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5416 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5417 | n = CHILD(n, 2); |
| 5418 | goto loop; |
| 5419 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5420 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5421 | case listmaker: |
| 5422 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5423 | symtable_list_comprehension(st, n); |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5424 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5425 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5426 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5427 | case atom: |
| 5428 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5429 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5430 | break; |
| 5431 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5432 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5433 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5434 | /* Walk over every non-token child with a special case |
| 5435 | for one child. |
| 5436 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5437 | if (NCH(n) == 1) { |
| 5438 | n = CHILD(n, 0); |
| 5439 | goto loop; |
| 5440 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5441 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5442 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5443 | symtable_node(st, CHILD(n, i)); |
| 5444 | } |
| 5445 | } |
| 5446 | |
| 5447 | static void |
| 5448 | symtable_funcdef(struct symtable *st, node *n) |
| 5449 | { |
| 5450 | node *body; |
| 5451 | |
| 5452 | if (TYPE(n) == lambdef) { |
| 5453 | if (NCH(n) == 4) |
| 5454 | symtable_params(st, CHILD(n, 1)); |
| 5455 | } else |
| 5456 | symtable_params(st, CHILD(n, 2)); |
| 5457 | body = CHILD(n, NCH(n) - 1); |
| 5458 | symtable_node(st, body); |
| 5459 | } |
| 5460 | |
| 5461 | /* The next two functions parse the argument tuple. |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5462 | symtable_default_args() checks for names in the default arguments, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5463 | which are references in the defining scope. symtable_params() |
| 5464 | parses the parameter names, which are defined in the function's |
| 5465 | body. |
| 5466 | |
| 5467 | varargslist: |
| 5468 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5469 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5470 | */ |
| 5471 | |
| 5472 | static void |
| 5473 | symtable_default_args(struct symtable *st, node *n) |
| 5474 | { |
| 5475 | node *c; |
| 5476 | int i; |
| 5477 | |
| 5478 | if (TYPE(n) == parameters) { |
| 5479 | n = CHILD(n, 1); |
| 5480 | if (TYPE(n) == RPAR) |
| 5481 | return; |
| 5482 | } |
| 5483 | REQ(n, varargslist); |
| 5484 | for (i = 0; i < NCH(n); i += 2) { |
| 5485 | c = CHILD(n, i); |
| 5486 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5487 | break; |
| 5488 | } |
| 5489 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5490 | symtable_node(st, CHILD(n, i)); |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | static void |
| 5495 | symtable_params(struct symtable *st, node *n) |
| 5496 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5497 | int i, complex = -1, ext = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5498 | node *c = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5499 | |
| 5500 | if (TYPE(n) == parameters) { |
| 5501 | n = CHILD(n, 1); |
| 5502 | if (TYPE(n) == RPAR) |
| 5503 | return; |
| 5504 | } |
| 5505 | REQ(n, varargslist); |
| 5506 | for (i = 0; i < NCH(n); i += 2) { |
| 5507 | c = CHILD(n, i); |
| 5508 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5509 | ext = 1; |
| 5510 | break; |
| 5511 | } |
| 5512 | if (TYPE(c) == test) { |
| 5513 | continue; |
| 5514 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5515 | if (TYPE(CHILD(c, 0)) == NAME) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5516 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5517 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5518 | char nbuf[30]; |
| 5519 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5520 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5521 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5522 | } |
| 5523 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5524 | if (ext) { |
| 5525 | c = CHILD(n, i); |
| 5526 | if (TYPE(c) == STAR) { |
| 5527 | i++; |
| 5528 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5529 | DEF_PARAM | DEF_STAR); |
| 5530 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5531 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5532 | c = NULL; |
| 5533 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5534 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5535 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5536 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5537 | i++; |
| 5538 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5539 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5540 | } |
| 5541 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5542 | if (complex >= 0) { |
| 5543 | int j; |
| 5544 | for (j = 0; j <= complex; j++) { |
| 5545 | c = CHILD(n, j); |
| 5546 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5547 | c = CHILD(n, ++j); |
| 5548 | else if (TYPE(c) == EQUAL) |
| 5549 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5550 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5551 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5552 | } |
| 5553 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | static void |
| 5557 | symtable_params_fplist(struct symtable *st, node *n) |
| 5558 | { |
| 5559 | int i; |
| 5560 | node *c; |
| 5561 | |
| 5562 | REQ(n, fplist); |
| 5563 | for (i = 0; i < NCH(n); i += 2) { |
| 5564 | c = CHILD(n, i); |
| 5565 | REQ(c, fpdef); |
| 5566 | if (NCH(c) == 1) |
| 5567 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5568 | DEF_PARAM | DEF_INTUPLE); |
| 5569 | else |
| 5570 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5571 | } |
| 5572 | |
| 5573 | } |
| 5574 | |
| 5575 | static void |
| 5576 | symtable_global(struct symtable *st, node *n) |
| 5577 | { |
| 5578 | int i; |
| 5579 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5580 | /* XXX It might be helpful to warn about module-level global |
| 5581 | statements, but it's hard to tell the difference between |
| 5582 | module-level and a string passed to exec. |
| 5583 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5584 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5585 | for (i = 1; i < NCH(n); i += 2) { |
| 5586 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5587 | int flags; |
| 5588 | |
| 5589 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5590 | if (flags < 0) |
| 5591 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5592 | if (flags && flags != DEF_GLOBAL) { |
| 5593 | char buf[500]; |
| 5594 | if (flags & DEF_PARAM) { |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 5595 | PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5596 | name); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5597 | symtable_error(st, 0); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5598 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5599 | } |
| 5600 | else { |
| 5601 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5602 | PyOS_snprintf(buf, sizeof(buf), |
| 5603 | GLOBAL_AFTER_ASSIGN, |
| 5604 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5605 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5606 | PyOS_snprintf(buf, sizeof(buf), |
| 5607 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5608 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5609 | } |
| 5610 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5611 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5612 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | static void |
| 5616 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5617 | { |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5618 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5619 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5620 | |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5621 | REQ(n, listmaker); |
| 5622 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", |
| 5623 | ++st->st_cur->ste_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5624 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5625 | symtable_list_for(st, CHILD(n, 1)); |
| 5626 | symtable_node(st, CHILD(n, 0)); |
| 5627 | --st->st_cur->ste_tmpname; |
| 5628 | } |
| 5629 | |
| 5630 | static void |
| 5631 | symtable_list_for(struct symtable *st, node *n) |
| 5632 | { |
| 5633 | REQ(n, list_for); |
| 5634 | /* list_for: for v in expr [list_iter] */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5635 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5636 | symtable_node(st, CHILD(n, 3)); |
| 5637 | if (NCH(n) == 5) |
| 5638 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | static void |
| 5642 | symtable_import(struct symtable *st, node *n) |
| 5643 | { |
| 5644 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5645 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5646 | | 'from' dotted_name 'import' |
| 5647 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5648 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5649 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5650 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5651 | node *dotname = CHILD(n, 1); |
| 5652 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5653 | /* check for bogus imports */ |
| 5654 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5655 | PyErr_SetString(PyExc_SyntaxError, |
| 5656 | LATE_FUTURE); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5657 | symtable_error(st, n->n_lineno); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5658 | return; |
| 5659 | } |
| 5660 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5661 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5662 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5663 | if (symtable_warn(st, |
| 5664 | "import * only allowed at module level") < 0) |
| 5665 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5666 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5667 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5668 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5669 | } else { |
| 5670 | for (i = 3; i < NCH(n); i += 2) { |
| 5671 | node *c = CHILD(n, i); |
| 5672 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5673 | symtable_assign(st, CHILD(c, 2), |
| 5674 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5675 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5676 | symtable_assign(st, CHILD(c, 0), |
| 5677 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5678 | } |
| 5679 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5680 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5681 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5682 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5683 | } |
| 5684 | } |
| 5685 | } |
| 5686 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5687 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5688 | symtable_add_def() if it is eventually called. The flag is useful |
| 5689 | to specify the particular type of assignment that should be |
| 5690 | recorded, e.g. an assignment caused by import. |
| 5691 | */ |
| 5692 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5693 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5694 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5695 | { |
| 5696 | node *tmp; |
| 5697 | int i; |
| 5698 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5699 | loop: |
| 5700 | switch (TYPE(n)) { |
| 5701 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5702 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5703 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5704 | return; |
| 5705 | case power: |
| 5706 | if (NCH(n) > 2) { |
| 5707 | for (i = 2; i < NCH(n); ++i) |
| 5708 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5709 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5710 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5711 | if (NCH(n) > 1) { |
| 5712 | symtable_node(st, CHILD(n, 0)); |
| 5713 | symtable_node(st, CHILD(n, 1)); |
| 5714 | } else { |
| 5715 | n = CHILD(n, 0); |
| 5716 | goto loop; |
| 5717 | } |
| 5718 | return; |
| 5719 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5720 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5721 | /* XXX This is an error, but the next pass |
| 5722 | will catch it. */ |
| 5723 | return; |
| 5724 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5725 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5726 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5727 | } |
| 5728 | return; |
| 5729 | case exprlist: |
| 5730 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5731 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5732 | if (NCH(n) == 1) { |
| 5733 | n = CHILD(n, 0); |
| 5734 | goto loop; |
| 5735 | } |
| 5736 | else { |
| 5737 | int i; |
| 5738 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5739 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5740 | return; |
| 5741 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5742 | case atom: |
| 5743 | tmp = CHILD(n, 0); |
| 5744 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5745 | n = CHILD(n, 1); |
| 5746 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5747 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5748 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5749 | PyErr_SetString(PyExc_SyntaxError, |
| 5750 | ASSIGN_DEBUG); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5751 | symtable_error(st, n->n_lineno); |
| 5752 | return; |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5753 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5754 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5755 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5756 | return; |
| 5757 | case dotted_as_name: |
| 5758 | if (NCH(n) == 3) |
| 5759 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5760 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5761 | else |
| 5762 | symtable_add_def(st, |
| 5763 | STR(CHILD(CHILD(n, |
| 5764 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5765 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5766 | return; |
| 5767 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5768 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5769 | return; |
| 5770 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5771 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5772 | return; |
| 5773 | default: |
| 5774 | if (NCH(n) == 0) |
| 5775 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5776 | if (NCH(n) == 1) { |
| 5777 | n = CHILD(n, 0); |
| 5778 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5779 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5780 | /* Should only occur for errors like x + 1 = 1, |
| 5781 | which will be caught in the next pass. */ |
| 5782 | for (i = 0; i < NCH(n); ++i) |
| 5783 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5784 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5785 | } |
| 5786 | } |