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 | |
| 63 | #define LOCAL_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; |
| 107 | PyObject *code; |
| 108 | PyObject *consts; |
| 109 | PyObject *names; |
| 110 | PyObject *varnames; |
| 111 | PyObject *freevars = NULL; |
| 112 | PyObject *cellvars = NULL; |
| 113 | PyObject *filename; |
| 114 | PyObject *name; |
| 115 | int firstlineno; |
| 116 | PyObject *lnotab; |
| 117 | |
| 118 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 119 | &argcount, &nlocals, &stacksize, &flags, |
| 120 | &code, |
| 121 | &PyTuple_Type, &consts, |
| 122 | &PyTuple_Type, &names, |
| 123 | &PyTuple_Type, &varnames, |
| 124 | &filename, &name, |
| 125 | &firstlineno, &lnotab, |
| 126 | &PyTuple_Type, &freevars, |
| 127 | &PyTuple_Type, &cellvars)) |
| 128 | return NULL; |
| 129 | |
| 130 | if (freevars == NULL || cellvars == NULL) { |
| 131 | PyObject *empty = PyTuple_New(0); |
| 132 | if (empty == NULL) |
| 133 | return NULL; |
| 134 | if (freevars == NULL) { |
| 135 | freevars = empty; |
| 136 | Py_INCREF(freevars); |
| 137 | } |
| 138 | if (cellvars == NULL) { |
| 139 | cellvars = empty; |
| 140 | Py_INCREF(cellvars); |
| 141 | } |
| 142 | Py_DECREF(empty); |
| 143 | } |
| 144 | |
| 145 | if (!PyObject_CheckReadBuffer(code)) { |
| 146 | PyErr_SetString(PyExc_TypeError, |
| 147 | "bytecode object must be a single-segment read-only buffer"); |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | return (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags, |
| 152 | code, consts, names, varnames, |
| 153 | freevars, cellvars, filename, name, |
| 154 | firstlineno, lnotab); |
| 155 | } |
| 156 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 157 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 158 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 160 | Py_XDECREF(co->co_code); |
| 161 | Py_XDECREF(co->co_consts); |
| 162 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 163 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 164 | Py_XDECREF(co->co_freevars); |
| 165 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 166 | Py_XDECREF(co->co_filename); |
| 167 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 168 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 169 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 172 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 173 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 174 | { |
| 175 | char buf[500]; |
| 176 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 177 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 178 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 180 | if (co->co_firstlineno != 0) |
| 181 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 182 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 183 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 185 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 186 | PyOS_snprintf(buf, sizeof(buf), |
| 187 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 188 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 189 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 192 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 193 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 194 | { |
| 195 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 196 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 197 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 198 | cmp = co->co_argcount - cp->co_argcount; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 199 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 200 | cmp = co->co_nlocals - cp->co_nlocals; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 201 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 202 | cmp = co->co_flags - cp->co_flags; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 203 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 204 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 205 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 206 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 207 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 208 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 209 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 210 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 211 | if (cmp) return cmp; |
| 212 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 213 | if (cmp) return cmp; |
| 214 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 215 | return cmp; |
| 216 | } |
| 217 | |
| 218 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 219 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 220 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 221 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 222 | h0 = PyObject_Hash(co->co_name); |
| 223 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 224 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 225 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 226 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 227 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 228 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 229 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 230 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 231 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 232 | h5 = PyObject_Hash(co->co_freevars); |
| 233 | if (h5 == -1) return -1; |
| 234 | h6 = PyObject_Hash(co->co_cellvars); |
| 235 | if (h6 == -1) return -1; |
| 236 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 237 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 238 | if (h == -1) h = -2; |
| 239 | return h; |
| 240 | } |
| 241 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 242 | /* XXX code objects need to participate in GC? */ |
| 243 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 244 | PyTypeObject PyCode_Type = { |
| 245 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 246 | 0, |
| 247 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 248 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 249 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 250 | (destructor)code_dealloc, /* tp_dealloc */ |
| 251 | 0, /* tp_print */ |
| 252 | 0, /* tp_getattr */ |
| 253 | 0, /* tp_setattr */ |
| 254 | (cmpfunc)code_compare, /* tp_compare */ |
| 255 | (reprfunc)code_repr, /* tp_repr */ |
| 256 | 0, /* tp_as_number */ |
| 257 | 0, /* tp_as_sequence */ |
| 258 | 0, /* tp_as_mapping */ |
| 259 | (hashfunc)code_hash, /* tp_hash */ |
| 260 | 0, /* tp_call */ |
| 261 | 0, /* tp_str */ |
| 262 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 263 | 0, /* tp_setattro */ |
| 264 | 0, /* tp_as_buffer */ |
| 265 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 266 | code_doc, /* tp_doc */ |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 267 | 0, /* tp_traverse */ |
| 268 | 0, /* tp_clear */ |
| 269 | 0, /* tp_richcompare */ |
| 270 | 0, /* tp_weaklistoffset */ |
| 271 | 0, /* tp_iter */ |
| 272 | 0, /* tp_iternext */ |
| 273 | 0, /* tp_methods */ |
| 274 | code_memberlist, /* tp_members */ |
| 275 | 0, /* tp_getset */ |
| 276 | 0, /* tp_base */ |
| 277 | 0, /* tp_dict */ |
| 278 | 0, /* tp_descr_get */ |
| 279 | 0, /* tp_descr_set */ |
| 280 | 0, /* tp_dictoffset */ |
| 281 | 0, /* tp_init */ |
| 282 | 0, /* tp_alloc */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 283 | code_new, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 286 | #define NAME_CHARS \ |
| 287 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 288 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 289 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 290 | |
| 291 | static int |
| 292 | all_name_chars(unsigned char *s) |
| 293 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 294 | static char ok_name_char[256]; |
| 295 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 297 | if (ok_name_char[*name_chars] == 0) { |
| 298 | unsigned char *p; |
| 299 | for (p = name_chars; *p; p++) |
| 300 | ok_name_char[*p] = 1; |
| 301 | } |
| 302 | while (*s) { |
| 303 | if (ok_name_char[*s++] == 0) |
| 304 | return 0; |
| 305 | } |
| 306 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 309 | static int |
| 310 | intern_strings(PyObject *tuple) |
| 311 | { |
| 312 | int i; |
| 313 | |
| 314 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 315 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 316 | if (v == NULL || !PyString_Check(v)) { |
| 317 | Py_FatalError("non-string found in code slot"); |
| 318 | PyErr_BadInternalCall(); |
| 319 | return -1; |
| 320 | } |
| 321 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 322 | } |
| 323 | return 0; |
| 324 | } |
| 325 | |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 326 | #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) |
| 327 | #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 328 | #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) |
| 329 | #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] | 330 | #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 |
| 331 | |
| 332 | static PyObject * |
| 333 | optimize_code(PyObject *code, PyObject* consts) |
| 334 | { |
| 335 | int i, j, codelen; |
| 336 | int tgt, tgttgt, opcode; |
| 337 | unsigned char *codestr; |
| 338 | |
| 339 | /* Make a modifiable copy of the code string */ |
| 340 | if (!PyString_Check(code)) |
| 341 | goto exitUnchanged; |
| 342 | codelen = PyString_Size(code); |
| 343 | codestr = PyMem_Malloc(codelen); |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 344 | if (codestr == NULL) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 345 | goto exitUnchanged; |
| 346 | codestr = memcpy(codestr, PyString_AS_STRING(code), codelen); |
| 347 | assert(PyTuple_Check(consts)); |
| 348 | |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 349 | 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] | 350 | opcode = codestr[i]; |
| 351 | switch (opcode) { |
| 352 | |
| 353 | /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP. |
| 354 | Note, only the first opcode is changed, the others still |
| 355 | perform normally if they happen to be jump targets. */ |
| 356 | case LOAD_CONST: |
| 357 | j = GETARG(codestr, i); |
| 358 | if (codestr[i+3] != JUMP_IF_FALSE || |
| 359 | codestr[i+6] != POP_TOP || |
| 360 | !PyObject_IsTrue(PyTuple_GET_ITEM(consts, j))) |
| 361 | continue; |
| 362 | codestr[i] = JUMP_FORWARD; |
| 363 | SETARG(codestr, i, 4); |
| 364 | break; |
| 365 | |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 366 | /* Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2 JMP+2. |
| 367 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2 JMP+1. |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 368 | Note, these opcodes occur together only in assignment |
| 369 | statements. Accordingly, the unpack opcode is never |
| 370 | a jump target. */ |
| 371 | case BUILD_TUPLE: |
| 372 | case BUILD_LIST: |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 373 | if (codestr[i+3] != UNPACK_SEQUENCE) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 374 | continue; |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 375 | if (GETARG(codestr, i) == 2 && \ |
| 376 | GETARG(codestr, i+3) == 2) { |
| 377 | codestr[i] = ROT_TWO; |
| 378 | codestr[i+1] = JUMP_FORWARD; |
| 379 | SETARG(codestr, i+1, 2); |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 380 | codestr[i+4] = DUP_TOP; /* Filler codes used as NOPs */ |
| 381 | codestr[i+5] = POP_TOP; |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 382 | continue; |
| 383 | } |
| 384 | if (GETARG(codestr, i) == 3 && \ |
| 385 | GETARG(codestr, i+3) == 3) { |
| 386 | codestr[i] = ROT_THREE; |
| 387 | codestr[i+1] = ROT_TWO; |
| 388 | codestr[i+2] = JUMP_FORWARD; |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 389 | SETARG(codestr, i+2, 1); |
| 390 | codestr[i+5] = DUP_TOP; |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 391 | } |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | /* Replace jumps to unconditional jumps */ |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 395 | case FOR_ITER: |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 396 | case JUMP_FORWARD: |
| 397 | case JUMP_IF_FALSE: |
| 398 | case JUMP_IF_TRUE: |
| 399 | case JUMP_ABSOLUTE: |
| 400 | case CONTINUE_LOOP: |
| 401 | case SETUP_LOOP: |
| 402 | case SETUP_EXCEPT: |
| 403 | case SETUP_FINALLY: |
| 404 | tgt = GETJUMPTGT(codestr, i); |
Raymond Hettinger | f4cf76d | 2003-04-24 05:45:23 +0000 | [diff] [blame] | 405 | if (!UNCONDITIONAL_JUMP(codestr[tgt])) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 406 | continue; |
| 407 | tgttgt = GETJUMPTGT(codestr, tgt); |
| 408 | if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ |
| 409 | opcode = JUMP_ABSOLUTE; |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 410 | if (!ABSOLUTE_JUMP(opcode)) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 411 | tgttgt -= i + 3; /* Calc relative jump addr */ |
| 412 | if (tgttgt < 0) /* No backward relative jumps */ |
| 413 | continue; |
| 414 | codestr[i] = opcode; |
| 415 | SETARG(codestr, i, tgttgt); |
| 416 | break; |
| 417 | |
| 418 | case EXTENDED_ARG: |
| 419 | PyMem_Free(codestr); |
| 420 | goto exitUnchanged; |
| 421 | } |
| 422 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame^] | 423 | code = PyString_FromStringAndSize((char *)codestr, codelen); |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 424 | PyMem_Free(codestr); |
| 425 | return code; |
| 426 | |
| 427 | exitUnchanged: |
| 428 | Py_INCREF(code); |
| 429 | return code; |
| 430 | } |
| 431 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 432 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 433 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 434 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 435 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 436 | PyObject *filename, PyObject *name, int firstlineno, |
| 437 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 438 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 439 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 440 | int i; |
| 441 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 442 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 443 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 444 | consts == NULL || !PyTuple_Check(consts) || |
| 445 | names == NULL || !PyTuple_Check(names) || |
| 446 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 447 | freevars == NULL || !PyTuple_Check(freevars) || |
| 448 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 449 | name == NULL || !PyString_Check(name) || |
| 450 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 451 | lnotab == NULL || !PyString_Check(lnotab) || |
| 452 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 453 | PyErr_BadInternalCall(); |
| 454 | return NULL; |
| 455 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 456 | intern_strings(names); |
| 457 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 458 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 459 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 460 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 461 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 462 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 463 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 464 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 465 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 466 | continue; |
| 467 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 468 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 469 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 470 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 471 | co->co_argcount = argcount; |
| 472 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 473 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 474 | co->co_flags = flags; |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 475 | co->co_code = optimize_code(code, consts); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 476 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 477 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 478 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 479 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 480 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 481 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 482 | Py_INCREF(freevars); |
| 483 | co->co_freevars = freevars; |
| 484 | Py_INCREF(cellvars); |
| 485 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 486 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 487 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 488 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 489 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 490 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 491 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 492 | co->co_lnotab = lnotab; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 493 | if (PyTuple_GET_SIZE(freevars) == 0 && |
| 494 | PyTuple_GET_SIZE(cellvars) == 0) |
| 495 | co->co_flags |= CO_NOFREE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 496 | } |
| 497 | return co; |
| 498 | } |
| 499 | |
| 500 | |
| 501 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 502 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 503 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 504 | builds the symbol table. The second pass generates the bytecode. |
| 505 | |
| 506 | The first pass uses a single symtable struct. The second pass uses |
| 507 | a compiling struct for each code block. The compiling structs |
| 508 | share a reference to the symtable. |
| 509 | |
| 510 | The two passes communicate via symtable_load_symbols() and via |
| 511 | is_local() and is_global(). The former initializes several slots |
| 512 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 513 | c_argcount, c_globals, and c_flags. |
| 514 | */ |
| 515 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 516 | /* All about c_lnotab. |
| 517 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 518 | c_lnotab is an array of unsigned bytes disguised as a Python string. Since |
| 519 | version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are |
| 520 | mapped to source code line #s via c_lnotab instead. |
| 521 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 522 | The array is conceptually a list of |
| 523 | (bytecode offset increment, line number increment) |
| 524 | pairs. The details are important and delicate, best illustrated by example: |
| 525 | |
| 526 | byte code offset source code line number |
| 527 | 0 1 |
| 528 | 6 2 |
| 529 | 50 7 |
| 530 | 350 307 |
| 531 | 361 308 |
| 532 | |
| 533 | The first trick is that these numbers aren't stored, only the increments |
| 534 | from one row to the next (this doesn't really work, but it's a start): |
| 535 | |
| 536 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 537 | |
| 538 | The second trick is that an unsigned byte can't hold negative values, or |
| 539 | values larger than 255, so (a) there's a deep assumption that byte code |
| 540 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 541 | if at least one column jumps by more than 255 from one row to the next, more |
| 542 | than one pair is written to the table. In case #b, there's no way to know |
| 543 | from looking at the table later how many were written. That's the delicate |
| 544 | part. A user of c_lnotab desiring to find the source line number |
| 545 | corresponding to a bytecode address A should do something like this |
| 546 | |
| 547 | lineno = addr = 0 |
| 548 | for addr_incr, line_incr in c_lnotab: |
| 549 | addr += addr_incr |
| 550 | if addr > A: |
| 551 | return lineno |
| 552 | lineno += line_incr |
| 553 | |
| 554 | In order for this to work, when the addr field increments by more than 255, |
| 555 | the line # increment in each pair generated must be 0 until the remaining addr |
| 556 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 557 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 558 | 255, 0, 45, 255, 0, 45. |
| 559 | */ |
| 560 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 561 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 562 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 563 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 564 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 565 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 566 | PyObject *c_name_dict; /* inverse of c_names */ |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 567 | PyObject *c_globals; /* dictionary (value=None or True) */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 568 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 569 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 570 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 571 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 572 | int c_nlocals; /* index of next local */ |
| 573 | int c_argcount; /* number of top-level arguments */ |
| 574 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 575 | int c_nexti; /* index into c_code */ |
| 576 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 577 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 578 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 579 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 580 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 581 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 582 | int c_nblocks; /* current block stack level */ |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 583 | const char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 584 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 585 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 586 | int c_stacklevel; /* Current stack level */ |
| 587 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 588 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 589 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 590 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 591 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 592 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 593 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 594 | int c_closure; /* Is nested w/freevars? */ |
| 595 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 596 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 597 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 598 | }; |
| 599 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 600 | static int |
| 601 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 602 | { |
| 603 | if ((v & (USE | DEF_FREE)) |
| 604 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 605 | return 1; |
| 606 | if (v & DEF_FREE_CLASS) |
| 607 | return 1; |
| 608 | return 0; |
| 609 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 610 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 611 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 612 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 613 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 614 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 615 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 616 | if (c == NULL) { |
| 617 | /* Error occurred via symtable call to |
| 618 | is_constant_false */ |
| 619 | PyErr_SetString(exc, msg); |
| 620 | return; |
| 621 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 622 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 623 | if (c->c_lineno < 1 || c->c_interactive) { |
| 624 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 625 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 626 | return; |
| 627 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 628 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 629 | if (v == NULL) |
| 630 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 631 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 632 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 633 | if (line == NULL) { |
| 634 | Py_INCREF(Py_None); |
| 635 | line = Py_None; |
| 636 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 637 | if (exc == PyExc_SyntaxError) { |
| 638 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 639 | Py_None, line); |
| 640 | if (t == NULL) |
| 641 | goto exit; |
| 642 | w = Py_BuildValue("(OO)", v, t); |
| 643 | if (w == NULL) |
| 644 | goto exit; |
| 645 | PyErr_SetObject(exc, w); |
| 646 | } else { |
| 647 | /* Make sure additional exceptions are printed with |
| 648 | file and line, also. */ |
| 649 | PyErr_SetObject(exc, v); |
| 650 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 651 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 652 | exit: |
| 653 | Py_XDECREF(t); |
| 654 | Py_XDECREF(v); |
| 655 | Py_XDECREF(w); |
| 656 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 659 | /* Interface to the block stack */ |
| 660 | |
| 661 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 662 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 663 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 664 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 665 | com_error(c, PyExc_SystemError, |
| 666 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 667 | } |
| 668 | else { |
| 669 | c->c_block[c->c_nblocks++] = type; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 674 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 675 | { |
| 676 | if (c->c_nblocks > 0) |
| 677 | c->c_nblocks--; |
| 678 | 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] | 679 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 683 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 684 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 685 | static int issue_warning(const char *, const char *, int); |
| 686 | static int com_init(struct compiling *, const char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 687 | static void com_free(struct compiling *); |
| 688 | static void com_push(struct compiling *, int); |
| 689 | static void com_pop(struct compiling *, int); |
| 690 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 691 | static void com_node(struct compiling *, node *); |
| 692 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 693 | static void com_addbyte(struct compiling *, int); |
| 694 | static void com_addint(struct compiling *, int); |
| 695 | static void com_addoparg(struct compiling *, int, int); |
| 696 | static void com_addfwref(struct compiling *, int, int *); |
| 697 | static void com_backpatch(struct compiling *, int); |
| 698 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 699 | static int com_addconst(struct compiling *, PyObject *); |
| 700 | static int com_addname(struct compiling *, PyObject *); |
| 701 | static void com_addopname(struct compiling *, int, node *); |
| 702 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 703 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 704 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 705 | static void com_assign(struct compiling *, node *, int, node *); |
| 706 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 707 | static PyCodeObject *icompile(node *, struct compiling *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 708 | static PyCodeObject *jcompile(node *, const char *, struct compiling *, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 709 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 710 | static PyObject *parsestrplus(struct compiling*, node *); |
| 711 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 712 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 713 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 714 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 715 | |
| 716 | /* symtable operations */ |
| 717 | static int symtable_build(struct compiling *, node *); |
| 718 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 719 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 720 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 721 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 722 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 723 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 724 | |
| 725 | static void symtable_node(struct symtable *, node *); |
| 726 | static void symtable_funcdef(struct symtable *, node *); |
| 727 | static void symtable_default_args(struct symtable *, node *); |
| 728 | static void symtable_params(struct symtable *, node *); |
| 729 | static void symtable_params_fplist(struct symtable *, node *n); |
| 730 | static void symtable_global(struct symtable *, node *); |
| 731 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 732 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 733 | static void symtable_list_comprehension(struct symtable *, node *); |
| 734 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 735 | static int symtable_update_free_vars(struct symtable *); |
| 736 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 737 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 738 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 739 | /* helper */ |
| 740 | static void |
| 741 | do_pad(int pad) |
| 742 | { |
| 743 | int i; |
| 744 | for (i = 0; i < pad; ++i) |
| 745 | fprintf(stderr, " "); |
| 746 | } |
| 747 | |
| 748 | static void |
| 749 | dump(node *n, int pad, int depth) |
| 750 | { |
| 751 | int i; |
| 752 | if (depth == 0) |
| 753 | return; |
| 754 | do_pad(pad); |
| 755 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 756 | if (depth > 0) |
| 757 | depth--; |
| 758 | for (i = 0; i < NCH(n); ++i) |
| 759 | dump(CHILD(n, i), pad + 1, depth); |
| 760 | } |
| 761 | |
| 762 | #define DUMP(N) dump(N, 0, -1) |
| 763 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 764 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 765 | com_init(struct compiling *c, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 766 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 767 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 768 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 769 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 770 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 771 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 772 | goto fail; |
| 773 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 774 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 775 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 776 | goto fail; |
| 777 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 778 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 779 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 780 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 781 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 782 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 783 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 784 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 785 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 786 | c->c_freevars = NULL; |
| 787 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 788 | c->c_nlocals = 0; |
| 789 | c->c_argcount = 0; |
| 790 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 791 | c->c_nexti = 0; |
| 792 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 793 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 794 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 795 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 796 | c->c_begin = 0; |
| 797 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 798 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 799 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 800 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 801 | c->c_stacklevel = 0; |
| 802 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 803 | c->c_firstlineno = 0; |
| 804 | c->c_last_addr = 0; |
| 805 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 806 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 807 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 808 | c->c_nested = 0; |
| 809 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 810 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 811 | return 1; |
| 812 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 813 | fail: |
| 814 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 819 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 820 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 821 | Py_XDECREF(c->c_code); |
| 822 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 823 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 824 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 825 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 826 | Py_XDECREF(c->c_globals); |
| 827 | Py_XDECREF(c->c_locals); |
| 828 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 829 | Py_XDECREF(c->c_freevars); |
| 830 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 831 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 832 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 833 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 837 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 838 | { |
| 839 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 840 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 841 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 842 | /* |
| 843 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 844 | c->c_filename, c->c_name, c->c_lineno, |
| 845 | c->c_nexti, c->c_stacklevel, n); |
| 846 | */ |
| 847 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 851 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 852 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 853 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 854 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 855 | else |
| 856 | c->c_stacklevel -= n; |
| 857 | } |
| 858 | |
| 859 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 860 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 861 | { |
| 862 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 863 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 864 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 865 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 868 | static int |
| 869 | com_check_size(PyObject **s, int offset) |
| 870 | { |
| 871 | int len = PyString_GET_SIZE(*s); |
| 872 | if (offset >= len) |
| 873 | return _PyString_Resize(s, len * 2); |
| 874 | return 0; |
| 875 | } |
| 876 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 877 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 878 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 879 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 880 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 881 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 882 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 883 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 884 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 885 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 886 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 887 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 891 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 892 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 893 | com_addbyte(c, x & 0xff); |
| 894 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 898 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 899 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 900 | char *p; |
| 901 | if (c->c_lnotab == NULL) |
| 902 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 903 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 904 | c->c_errors++; |
| 905 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 906 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 907 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 908 | *p++ = addr; |
| 909 | *p++ = line; |
| 910 | c->c_lnotab_next += 2; |
| 911 | } |
| 912 | |
| 913 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 914 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 915 | { |
| 916 | c->c_lineno = lineno; |
| 917 | if (c->c_firstlineno == 0) { |
| 918 | c->c_firstlineno = c->c_last_line = lineno; |
| 919 | } |
| 920 | else { |
| 921 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 922 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 923 | while (incr_addr > 255) { |
| 924 | com_add_lnotab(c, 255, 0); |
| 925 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 926 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 927 | while (incr_line > 255) { |
| 928 | com_add_lnotab(c, incr_addr, 255); |
| 929 | incr_line -=255; |
| 930 | incr_addr = 0; |
| 931 | } |
| 932 | if (incr_addr > 0 || incr_line > 0) |
| 933 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 934 | c->c_last_addr = c->c_nexti; |
| 935 | c->c_last_line = lineno; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 940 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 941 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 942 | int extended_arg = arg >> 16; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 943 | if (extended_arg){ |
| 944 | com_addbyte(c, EXTENDED_ARG); |
| 945 | com_addint(c, extended_arg); |
| 946 | arg &= 0xffff; |
| 947 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 948 | com_addbyte(c, op); |
| 949 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 953 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 954 | { |
| 955 | /* Compile a forward reference for backpatching */ |
| 956 | int here; |
| 957 | int anchor; |
| 958 | com_addbyte(c, op); |
| 959 | here = c->c_nexti; |
| 960 | anchor = *p_anchor; |
| 961 | *p_anchor = here; |
| 962 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 963 | } |
| 964 | |
| 965 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 966 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 967 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 968 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 969 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 970 | int dist; |
| 971 | int prev; |
| 972 | for (;;) { |
| 973 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 974 | prev = code[anchor] + (code[anchor+1] << 8); |
| 975 | dist = target - (anchor+2); |
| 976 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 977 | dist >>= 8; |
| 978 | code[anchor+1] = dist; |
| 979 | dist >>= 8; |
| 980 | if (dist) { |
| 981 | com_error(c, PyExc_SystemError, |
| 982 | "com_backpatch: offset too large"); |
| 983 | break; |
| 984 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 985 | if (!prev) |
| 986 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 987 | anchor -= prev; |
| 988 | } |
| 989 | } |
| 990 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 991 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 992 | |
| 993 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 994 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 995 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 996 | PyObject *w, *t, *np=NULL; |
| 997 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 998 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 999 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 1000 | if (t == NULL) |
| 1001 | goto fail; |
| 1002 | w = PyDict_GetItem(dict, t); |
| 1003 | if (w != NULL) { |
| 1004 | n = PyInt_AsLong(w); |
| 1005 | } else { |
| 1006 | n = PyList_Size(list); |
| 1007 | np = PyInt_FromLong(n); |
| 1008 | if (np == NULL) |
| 1009 | goto fail; |
| 1010 | if (PyList_Append(list, v) != 0) |
| 1011 | goto fail; |
| 1012 | if (PyDict_SetItem(dict, t, np) != 0) |
| 1013 | goto fail; |
| 1014 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1015 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1016 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1017 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1018 | fail: |
| 1019 | Py_XDECREF(np); |
| 1020 | Py_XDECREF(t); |
| 1021 | c->c_errors++; |
| 1022 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1026 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1027 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1028 | 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] | 1029 | } |
| 1030 | |
| 1031 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1032 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1033 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1034 | 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] | 1035 | } |
| 1036 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1037 | int |
| 1038 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1039 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1040 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1041 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1042 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1043 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 1044 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1045 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1046 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1047 | return 0; /* Don't mangle __extremely_long_names */ |
| 1048 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 1049 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1050 | /* Strip leading underscores from class name */ |
| 1051 | while (*p == '_') |
| 1052 | p++; |
| 1053 | if (*p == '\0') |
| 1054 | return 0; /* Don't mangle if class is just underscores */ |
| 1055 | plen = strlen(p); |
| 1056 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1057 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 1058 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1059 | buffer[0] = '_'; |
| 1060 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1061 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1062 | return 1; |
| 1063 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1064 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1065 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1066 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1067 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1068 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1069 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1070 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1071 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1072 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1073 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 1074 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1075 | c->c_errors++; |
| 1076 | i = 255; |
| 1077 | } |
| 1078 | else { |
| 1079 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1080 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1081 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1082 | com_addoparg(c, op, i); |
| 1083 | } |
| 1084 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1085 | #define NAME_LOCAL 0 |
| 1086 | #define NAME_GLOBAL 1 |
| 1087 | #define NAME_DEFAULT 2 |
| 1088 | #define NAME_CLOSURE 3 |
| 1089 | |
| 1090 | static int |
| 1091 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 1092 | { |
| 1093 | PyObject *v = PyDict_GetItem(dict, name); |
| 1094 | if (v == NULL) |
| 1095 | return -1; |
| 1096 | else |
| 1097 | return PyInt_AS_LONG(v); |
| 1098 | } |
| 1099 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1100 | static int |
| 1101 | none_assignment_check(struct compiling *c, char *name, int assigning) |
| 1102 | { |
| 1103 | if (name[0] == 'N' && strcmp(name, "None") == 0) { |
| 1104 | char *msg; |
| 1105 | if (assigning) |
Michael W. Hudson | 976249b | 2003-01-16 15:39:07 +0000 | [diff] [blame] | 1106 | msg = "assignment to None"; |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1107 | else |
| 1108 | msg = "deleting None"; |
| 1109 | if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) { |
| 1110 | c->c_errors++; |
| 1111 | return -1; |
| 1112 | } |
| 1113 | } |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1117 | static void |
| 1118 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 1119 | { |
| 1120 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1121 | int i, reftype; |
| 1122 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1123 | int op = STOP_CODE; |
| 1124 | char buffer[MANGLE_LEN]; |
| 1125 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1126 | if (kind != VAR_LOAD && |
| 1127 | none_assignment_check(c, name, kind == VAR_STORE)) |
| 1128 | { |
| 1129 | c->c_errors++; |
| 1130 | i = 255; |
| 1131 | goto done; |
| 1132 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1133 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1134 | name = buffer; |
| 1135 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1136 | c->c_errors++; |
| 1137 | i = 255; |
| 1138 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1139 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1140 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1141 | reftype = get_ref_type(c, name); |
| 1142 | switch (reftype) { |
| 1143 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1144 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1145 | scope = NAME_LOCAL; |
| 1146 | break; |
| 1147 | case GLOBAL_EXPLICIT: |
| 1148 | scope = NAME_GLOBAL; |
| 1149 | break; |
| 1150 | case GLOBAL_IMPLICIT: |
| 1151 | if (c->c_flags & CO_OPTIMIZED) |
| 1152 | scope = NAME_GLOBAL; |
| 1153 | break; |
| 1154 | case FREE: |
| 1155 | case CELL: |
| 1156 | scope = NAME_CLOSURE; |
| 1157 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1161 | if (scope == NAME_LOCAL) |
| 1162 | i = com_lookup_arg(c->c_locals, v); |
| 1163 | else if (reftype == FREE) |
| 1164 | i = com_lookup_arg(c->c_freevars, v); |
| 1165 | else if (reftype == CELL) |
| 1166 | i = com_lookup_arg(c->c_cellvars, v); |
| 1167 | if (i == -1) { |
| 1168 | c->c_errors++; /* XXX no exception set */ |
| 1169 | i = 255; |
| 1170 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1171 | } |
| 1172 | Py_DECREF(v); |
| 1173 | |
| 1174 | switch (kind) { |
| 1175 | case VAR_LOAD: |
| 1176 | switch (scope) { |
| 1177 | case NAME_LOCAL: |
| 1178 | op = LOAD_FAST; |
| 1179 | break; |
| 1180 | case NAME_GLOBAL: |
| 1181 | op = LOAD_GLOBAL; |
| 1182 | break; |
| 1183 | case NAME_DEFAULT: |
| 1184 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1185 | break; |
| 1186 | case NAME_CLOSURE: |
| 1187 | op = LOAD_DEREF; |
| 1188 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1189 | } |
| 1190 | break; |
| 1191 | case VAR_STORE: |
| 1192 | switch (scope) { |
| 1193 | case NAME_LOCAL: |
| 1194 | op = STORE_FAST; |
| 1195 | break; |
| 1196 | case NAME_GLOBAL: |
| 1197 | op = STORE_GLOBAL; |
| 1198 | break; |
| 1199 | case NAME_DEFAULT: |
| 1200 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1201 | break; |
| 1202 | case NAME_CLOSURE: |
| 1203 | op = STORE_DEREF; |
| 1204 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1205 | } |
| 1206 | break; |
| 1207 | case VAR_DELETE: |
| 1208 | switch (scope) { |
| 1209 | case NAME_LOCAL: |
| 1210 | op = DELETE_FAST; |
| 1211 | break; |
| 1212 | case NAME_GLOBAL: |
| 1213 | op = DELETE_GLOBAL; |
| 1214 | break; |
| 1215 | case NAME_DEFAULT: |
| 1216 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1217 | break; |
| 1218 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1219 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1220 | PyOS_snprintf(buf, sizeof(buf), |
| 1221 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1222 | com_error(c, PyExc_SyntaxError, buf); |
| 1223 | i = 255; |
| 1224 | break; |
| 1225 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1226 | } |
| 1227 | break; |
| 1228 | } |
| 1229 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1230 | com_addoparg(c, op, i); |
| 1231 | } |
| 1232 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1233 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1234 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1235 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1236 | char *name; |
| 1237 | char buffer[1000]; |
| 1238 | /* XXX it is possible to write this code without the 1000 |
| 1239 | chars on the total length of dotted names, I just can't be |
| 1240 | bothered right now */ |
| 1241 | if (TYPE(n) == STAR) |
| 1242 | name = "*"; |
| 1243 | else if (TYPE(n) == dotted_name) { |
| 1244 | char *p = buffer; |
| 1245 | int i; |
| 1246 | name = buffer; |
| 1247 | for (i = 0; i < NCH(n); i += 2) { |
| 1248 | char *s = STR(CHILD(n, i)); |
| 1249 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1250 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1251 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1252 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1253 | break; |
| 1254 | } |
| 1255 | if (p != buffer) |
| 1256 | *p++ = '.'; |
| 1257 | strcpy(p, s); |
| 1258 | p = strchr(p, '\0'); |
| 1259 | } |
| 1260 | } |
| 1261 | else { |
| 1262 | REQ(n, NAME); |
| 1263 | name = STR(n); |
| 1264 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1265 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1268 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1269 | parsenumber(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1270 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1271 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1272 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1273 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1274 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1275 | int imflag; |
| 1276 | #endif |
| 1277 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1278 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1279 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1280 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1281 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1282 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1283 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1284 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1285 | if (s[0] == '0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1286 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1287 | if (x < 0 && errno == 0) { |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1288 | if (PyErr_WarnExplicit( |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1289 | PyExc_FutureWarning, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1290 | "hex/oct constants > sys.maxint " |
| 1291 | "will return positive values " |
| 1292 | "in Python 2.4 and up", |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1293 | /* XXX: Give WarnExplicit |
| 1294 | a const char* argument. */ |
| 1295 | (char*)c->c_filename, |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1296 | c->c_lineno, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1297 | NULL, |
| 1298 | NULL) < 0) |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1299 | return NULL; |
Guido van Rossum | 3cb8e54 | 2002-08-11 14:06:15 +0000 | [diff] [blame] | 1300 | errno = 0; /* Might be changed by PyErr_Warn() */ |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1303 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1304 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1305 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1306 | if (errno != 0) |
| 1307 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1308 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1309 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1310 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1311 | #ifndef WITHOUT_COMPLEX |
| 1312 | if (imflag) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1313 | Py_complex z; |
| 1314 | z.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1315 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1316 | z.imag = atof(s); |
| 1317 | PyFPE_END_PROTECT(z) |
| 1318 | return PyComplex_FromCComplex(z); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1319 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1320 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1321 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1322 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1323 | PyFPE_START_PROTECT("atof", return 0) |
| 1324 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1325 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1326 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1327 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1330 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1331 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1332 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1333 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1334 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1335 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1336 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1337 | PyObject *u, *v; |
| 1338 | char *s, *t; |
| 1339 | t = s = *sPtr; |
| 1340 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1341 | while (s < end && (*s & 0x80)) s++; |
| 1342 | *sPtr = s; |
| 1343 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1344 | if (u == NULL) |
| 1345 | return NULL; |
| 1346 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1347 | Py_DECREF(u); |
| 1348 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1349 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 1352 | /* compiler.transformer.Transformer.decode_literal depends on what |
| 1353 | might seem like minor details of this function -- changes here |
| 1354 | must be reflected there. */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1355 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1356 | parsestr(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1357 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1358 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1359 | size_t len; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1360 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1361 | int rawmode = 0; |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1362 | char* encoding = ((c == NULL) ? NULL : c->c_encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1363 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1364 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1365 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1366 | if (isalpha(quote) || quote == '_') { |
| 1367 | if (quote == 'u' || quote == 'U') { |
| 1368 | quote = *++s; |
| 1369 | unicode = 1; |
| 1370 | } |
| 1371 | if (quote == 'r' || quote == 'R') { |
| 1372 | quote = *++s; |
| 1373 | rawmode = 1; |
| 1374 | } |
| 1375 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1376 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1377 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1378 | return NULL; |
| 1379 | } |
| 1380 | s++; |
| 1381 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1382 | if (len > INT_MAX) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1383 | com_error(c, PyExc_OverflowError, |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1384 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1385 | return NULL; |
| 1386 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1387 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1388 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1389 | return NULL; |
| 1390 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1391 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1392 | s += 2; |
| 1393 | len -= 2; |
| 1394 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1395 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1396 | return NULL; |
| 1397 | } |
| 1398 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1399 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1400 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1401 | PyObject *u, *w; |
Walter Dörwald | 4c6c765 | 2002-11-21 20:13:40 +0000 | [diff] [blame] | 1402 | char *buf; |
| 1403 | char *p; |
| 1404 | char *end; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1405 | if (encoding == NULL) { |
| 1406 | buf = s; |
| 1407 | u = NULL; |
| 1408 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1409 | buf = s; |
| 1410 | u = NULL; |
| 1411 | } else { |
| 1412 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1413 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1414 | if (u == NULL) |
| 1415 | return NULL; |
| 1416 | p = buf = PyString_AsString(u); |
| 1417 | end = s + len; |
| 1418 | while (s < end) { |
| 1419 | if (*s == '\\') { |
| 1420 | *p++ = *s++; |
| 1421 | if (*s & 0x80) { |
| 1422 | strcpy(p, "u005c"); |
| 1423 | p += 5; |
| 1424 | } |
| 1425 | } |
| 1426 | if (*s & 0x80) { /* XXX inefficient */ |
| 1427 | char *r; |
| 1428 | int rn, i; |
| 1429 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1430 | if (w == NULL) { |
| 1431 | Py_DECREF(u); |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | r = PyString_AsString(w); |
| 1435 | rn = PyString_Size(w); |
| 1436 | assert(rn % 2 == 0); |
| 1437 | for (i = 0; i < rn; i += 2) { |
| 1438 | sprintf(p, "\\u%02x%02x", |
| 1439 | r[i + 0] & 0xFF, |
| 1440 | r[i + 1] & 0xFF); |
| 1441 | p += 6; |
| 1442 | } |
| 1443 | Py_DECREF(w); |
| 1444 | } else { |
| 1445 | *p++ = *s++; |
| 1446 | } |
| 1447 | } |
| 1448 | len = p - buf; |
| 1449 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1450 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1451 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1452 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1453 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1454 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1455 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1456 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1457 | return v; |
| 1458 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1459 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1460 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1461 | need_encoding = (encoding != NULL && |
| 1462 | strcmp(encoding, "utf-8") != 0 && |
| 1463 | strcmp(encoding, "iso-8859-1") != 0); |
| 1464 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1465 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1466 | #ifndef Py_USING_UNICODE |
| 1467 | /* This should not happen - we never see any other |
| 1468 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1469 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1470 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1471 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1472 | if (u == NULL) |
| 1473 | return NULL; |
| 1474 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1475 | Py_DECREF(u); |
| 1476 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1477 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1478 | } else { |
| 1479 | return PyString_FromStringAndSize(s, len); |
| 1480 | } |
| 1481 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1482 | |
| 1483 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1484 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1485 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1486 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1487 | return v; |
| 1488 | } |
| 1489 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1490 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1491 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1492 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1493 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1494 | int i; |
| 1495 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1496 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1497 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1498 | for (i = 1; i < NCH(n); i++) { |
| 1499 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1500 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1501 | if (s == NULL) |
| 1502 | goto onError; |
| 1503 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1504 | PyString_ConcatAndDel(&v, s); |
| 1505 | if (v == NULL) |
| 1506 | goto onError; |
| 1507 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1508 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1509 | else { |
| 1510 | PyObject *temp; |
| 1511 | temp = PyUnicode_Concat(v, s); |
| 1512 | Py_DECREF(s); |
| 1513 | if (temp == NULL) |
| 1514 | goto onError; |
| 1515 | Py_DECREF(v); |
| 1516 | v = temp; |
| 1517 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1518 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1522 | |
| 1523 | onError: |
| 1524 | Py_XDECREF(v); |
| 1525 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1528 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1529 | 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] | 1530 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1531 | int anchor = 0; |
| 1532 | int save_begin = c->c_begin; |
| 1533 | |
| 1534 | /* list_iter: for v in expr [list_iter] */ |
| 1535 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1536 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1537 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1538 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1539 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1540 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1541 | c->c_loops++; |
| 1542 | com_list_iter(c, n, e, t); |
| 1543 | c->c_loops--; |
| 1544 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1545 | c->c_begin = save_begin; |
| 1546 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1547 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static void |
| 1551 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1552 | { |
| 1553 | int anchor = 0; |
| 1554 | int a = 0; |
| 1555 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1556 | com_node(c, CHILD(n, 1)); |
| 1557 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1558 | com_addbyte(c, POP_TOP); |
| 1559 | com_pop(c, 1); |
| 1560 | com_list_iter(c, n, e, t); |
| 1561 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1562 | com_backpatch(c, a); |
| 1563 | /* We jump here with an extra entry which we now pop */ |
| 1564 | com_addbyte(c, POP_TOP); |
| 1565 | com_backpatch(c, anchor); |
| 1566 | } |
| 1567 | |
| 1568 | static void |
| 1569 | com_list_iter(struct compiling *c, |
| 1570 | node *p, /* parent of list_iter node */ |
| 1571 | node *e, /* element expression node */ |
| 1572 | char *t /* name of result list temp local */) |
| 1573 | { |
| 1574 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1575 | node *n = CHILD(p, NCH(p)-1); |
| 1576 | if (TYPE(n) == list_iter) { |
| 1577 | n = CHILD(n, 0); |
| 1578 | switch (TYPE(n)) { |
| 1579 | case list_for: |
| 1580 | com_list_for(c, n, e, t); |
| 1581 | break; |
| 1582 | case list_if: |
| 1583 | com_list_if(c, n, e, t); |
| 1584 | break; |
| 1585 | default: |
| 1586 | com_error(c, PyExc_SystemError, |
| 1587 | "invalid list_iter node type"); |
| 1588 | } |
| 1589 | } |
| 1590 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1591 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1592 | com_push(c, 1); |
| 1593 | com_node(c, e); |
| 1594 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1595 | com_addbyte(c, POP_TOP); |
| 1596 | com_pop(c, 2); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | static void |
| 1601 | com_list_comprehension(struct compiling *c, node *n) |
| 1602 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1603 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1604 | char tmpname[30]; |
| 1605 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1606 | com_addoparg(c, BUILD_LIST, 0); |
| 1607 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1608 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1609 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1610 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1611 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1612 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1613 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1614 | --c->c_tmpname; |
| 1615 | } |
| 1616 | |
| 1617 | static void |
| 1618 | com_listmaker(struct compiling *c, node *n) |
| 1619 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1620 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1621 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1622 | com_list_comprehension(c, n); |
| 1623 | else { |
| 1624 | int len = 0; |
| 1625 | int i; |
| 1626 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1627 | com_node(c, CHILD(n, i)); |
| 1628 | com_addoparg(c, BUILD_LIST, len); |
| 1629 | com_pop(c, len-1); |
| 1630 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1634 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1635 | { |
| 1636 | int i; |
| 1637 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1638 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1639 | /* We must arrange things just right for STORE_SUBSCR. |
| 1640 | It wants the stack to look like (value) (dict) (key) */ |
| 1641 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1642 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1643 | com_node(c, CHILD(n, i)); /* key */ |
Gustavo Niemeyer | 78429a6 | 2002-12-16 13:54:02 +0000 | [diff] [blame] | 1644 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1645 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1646 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1647 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1652 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1653 | { |
| 1654 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1655 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1656 | int i; |
| 1657 | REQ(n, atom); |
| 1658 | ch = CHILD(n, 0); |
| 1659 | switch (TYPE(ch)) { |
| 1660 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1661 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1662 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1663 | com_push(c, 1); |
| 1664 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1665 | else |
| 1666 | com_node(c, CHILD(n, 1)); |
| 1667 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1668 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1669 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1670 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1671 | com_push(c, 1); |
| 1672 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1673 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1674 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1675 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1676 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1677 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1678 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1679 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1680 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1681 | break; |
| 1682 | case BACKQUOTE: |
| 1683 | com_node(c, CHILD(n, 1)); |
| 1684 | com_addbyte(c, UNARY_CONVERT); |
| 1685 | break; |
| 1686 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1687 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1688 | i = 255; |
| 1689 | } |
| 1690 | else { |
| 1691 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1692 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1693 | } |
| 1694 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1695 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1698 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1699 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1700 | c->c_errors++; |
| 1701 | i = 255; |
| 1702 | } |
| 1703 | else { |
| 1704 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1705 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1706 | } |
| 1707 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1708 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1709 | break; |
| 1710 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1711 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1712 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1715 | com_error(c, PyExc_SystemError, |
| 1716 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1721 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1722 | { |
| 1723 | if (NCH(n) == 1) { |
| 1724 | com_addbyte(c, op); |
| 1725 | } |
| 1726 | else if (NCH(n) == 2) { |
| 1727 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1728 | com_node(c, CHILD(n, 0)); |
| 1729 | com_addbyte(c, op+1); |
| 1730 | } |
| 1731 | else { |
| 1732 | com_node(c, CHILD(n, 1)); |
| 1733 | com_addbyte(c, op+2); |
| 1734 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1735 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1736 | } |
| 1737 | else { |
| 1738 | com_node(c, CHILD(n, 0)); |
| 1739 | com_node(c, CHILD(n, 2)); |
| 1740 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1741 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1745 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1746 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1747 | { |
| 1748 | if (NCH(n) == 1) { |
| 1749 | com_addbyte(c, DUP_TOP); |
| 1750 | com_push(c, 1); |
| 1751 | com_addbyte(c, SLICE); |
| 1752 | com_node(c, augn); |
| 1753 | com_addbyte(c, opcode); |
| 1754 | com_pop(c, 1); |
| 1755 | com_addbyte(c, ROT_TWO); |
| 1756 | com_addbyte(c, STORE_SLICE); |
| 1757 | com_pop(c, 2); |
| 1758 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1759 | com_node(c, CHILD(n, 0)); |
| 1760 | com_addoparg(c, DUP_TOPX, 2); |
| 1761 | com_push(c, 2); |
| 1762 | com_addbyte(c, SLICE+1); |
| 1763 | com_pop(c, 1); |
| 1764 | com_node(c, augn); |
| 1765 | com_addbyte(c, opcode); |
| 1766 | com_pop(c, 1); |
| 1767 | com_addbyte(c, ROT_THREE); |
| 1768 | com_addbyte(c, STORE_SLICE+1); |
| 1769 | com_pop(c, 3); |
| 1770 | } else if (NCH(n) == 2) { |
| 1771 | com_node(c, CHILD(n, 1)); |
| 1772 | com_addoparg(c, DUP_TOPX, 2); |
| 1773 | com_push(c, 2); |
| 1774 | com_addbyte(c, SLICE+2); |
| 1775 | com_pop(c, 1); |
| 1776 | com_node(c, augn); |
| 1777 | com_addbyte(c, opcode); |
| 1778 | com_pop(c, 1); |
| 1779 | com_addbyte(c, ROT_THREE); |
| 1780 | com_addbyte(c, STORE_SLICE+2); |
| 1781 | com_pop(c, 3); |
| 1782 | } else { |
| 1783 | com_node(c, CHILD(n, 0)); |
| 1784 | com_node(c, CHILD(n, 2)); |
| 1785 | com_addoparg(c, DUP_TOPX, 3); |
| 1786 | com_push(c, 3); |
| 1787 | com_addbyte(c, SLICE+3); |
| 1788 | com_pop(c, 2); |
| 1789 | com_node(c, augn); |
| 1790 | com_addbyte(c, opcode); |
| 1791 | com_pop(c, 1); |
| 1792 | com_addbyte(c, ROT_FOUR); |
| 1793 | com_addbyte(c, STORE_SLICE+3); |
| 1794 | com_pop(c, 4); |
| 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1799 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1800 | { |
| 1801 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1802 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1803 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1804 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1805 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1806 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1807 | } |
| 1808 | else { |
| 1809 | com_node(c, CHILD(n, 0)); |
| 1810 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1811 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1812 | } |
| 1813 | m = n; |
| 1814 | do { |
| 1815 | m = CHILD(m, 0); |
| 1816 | } while (NCH(m) == 1); |
| 1817 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1818 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1819 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1820 | * SF bug 132313 points out that complaining about a keyword |
| 1821 | * then is very confusing. |
| 1822 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1823 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1824 | TYPE(m) == lambdef ? |
| 1825 | "lambda cannot contain assignment" : |
| 1826 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1827 | } |
| 1828 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1829 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 63dd79a | 2002-08-16 02:24:56 +0000 | [diff] [blame] | 1830 | (void) none_assignment_check(c, STR(m), 1); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1831 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1832 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1833 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1834 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1835 | else if (*pkeywords == NULL) { |
| 1836 | c->c_errors++; |
| 1837 | Py_DECREF(v); |
| 1838 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1839 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1840 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1841 | "duplicate keyword argument"); |
| 1842 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1843 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1844 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1845 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1846 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1847 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1853 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1854 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1855 | { |
| 1856 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1857 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1858 | } |
| 1859 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1860 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1861 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1862 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1863 | int star_flag = 0; |
| 1864 | int starstar_flag = 0; |
| 1865 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1866 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1867 | na = 0; |
| 1868 | nk = 0; |
| 1869 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1870 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1871 | if (TYPE(ch) == STAR || |
| 1872 | TYPE(ch) == DOUBLESTAR) |
| 1873 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1874 | if (ch->n_lineno != lineno) { |
| 1875 | lineno = ch->n_lineno; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1876 | com_set_lineno(c, lineno); |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1877 | } |
| 1878 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1879 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1880 | na++; |
| 1881 | else |
| 1882 | nk++; |
| 1883 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1884 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1885 | while (i < NCH(n)) { |
| 1886 | node *tok = CHILD(n, i); |
| 1887 | node *ch = CHILD(n, i+1); |
| 1888 | i += 3; |
| 1889 | switch (TYPE(tok)) { |
| 1890 | case STAR: star_flag = 1; break; |
| 1891 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1892 | } |
| 1893 | com_node(c, ch); |
| 1894 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1895 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1896 | com_error(c, PyExc_SyntaxError, |
| 1897 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1898 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1899 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1900 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1901 | star_flag + (starstar_flag << 1); |
| 1902 | else |
| 1903 | opcode = CALL_FUNCTION; |
| 1904 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1905 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1910 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1911 | { |
| 1912 | com_addopname(c, LOAD_ATTR, n); |
| 1913 | } |
| 1914 | |
| 1915 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1916 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1917 | { |
| 1918 | int i=0; |
| 1919 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1920 | node *ch; |
| 1921 | |
| 1922 | /* first argument */ |
| 1923 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1924 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1925 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1926 | i++; |
| 1927 | } |
| 1928 | else { |
| 1929 | com_node(c, CHILD(n,i)); |
| 1930 | i++; |
| 1931 | REQ(CHILD(n,i),COLON); |
| 1932 | i++; |
| 1933 | } |
| 1934 | /* second argument */ |
| 1935 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1936 | com_node(c, CHILD(n,i)); |
| 1937 | i++; |
| 1938 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1939 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1940 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1941 | com_push(c, 1); |
| 1942 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1943 | /* remaining arguments */ |
| 1944 | for (; i < NCH(n); i++) { |
| 1945 | ns++; |
| 1946 | ch=CHILD(n,i); |
| 1947 | REQ(ch, sliceop); |
| 1948 | if (NCH(ch) == 1) { |
| 1949 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1950 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1951 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1952 | } |
| 1953 | else |
| 1954 | com_node(c, CHILD(ch,1)); |
| 1955 | } |
| 1956 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1957 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1961 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1962 | { |
| 1963 | node *ch; |
| 1964 | REQ(n, subscript); |
| 1965 | ch = CHILD(n,0); |
| 1966 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1967 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1968 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1969 | com_push(c, 1); |
| 1970 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1971 | else { |
| 1972 | /* check for slice */ |
| 1973 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1974 | com_sliceobj(c, n); |
| 1975 | else { |
| 1976 | REQ(ch, test); |
| 1977 | com_node(c, ch); |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1983 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1984 | { |
| 1985 | int i, op; |
| 1986 | REQ(n, subscriptlist); |
| 1987 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1988 | if (NCH(n) == 1) { |
| 1989 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1990 | /* 'Basic' slice, should have exactly one colon. */ |
| 1991 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1992 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1993 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1994 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1995 | switch (assigning) { |
| 1996 | case OP_DELETE: |
| 1997 | op = DELETE_SLICE; |
| 1998 | break; |
| 1999 | case OP_ASSIGN: |
| 2000 | op = STORE_SLICE; |
| 2001 | break; |
| 2002 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2003 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2004 | break; |
| 2005 | default: |
| 2006 | com_augassign_slice(c, sub, assigning, augn); |
| 2007 | return; |
| 2008 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2009 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2010 | if (op == STORE_SLICE) |
| 2011 | com_pop(c, 2); |
| 2012 | else if (op == DELETE_SLICE) |
| 2013 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
| 2016 | } |
| 2017 | /* Else normal subscriptlist. Compile each subscript. */ |
| 2018 | for (i = 0; i < NCH(n); i += 2) |
| 2019 | com_subscript(c, CHILD(n, i)); |
| 2020 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2021 | if (NCH(n) > 1) { |
| 2022 | i = (NCH(n)+1) / 2; |
| 2023 | com_addoparg(c, BUILD_TUPLE, i); |
| 2024 | com_pop(c, i-1); |
| 2025 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2026 | switch (assigning) { |
| 2027 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2028 | op = DELETE_SUBSCR; |
| 2029 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2030 | break; |
| 2031 | default: |
| 2032 | case OP_ASSIGN: |
| 2033 | op = STORE_SUBSCR; |
| 2034 | i = 3; |
| 2035 | break; |
| 2036 | case OP_APPLY: |
| 2037 | op = BINARY_SUBSCR; |
| 2038 | i = 1; |
| 2039 | break; |
| 2040 | } |
| 2041 | if (assigning > OP_APPLY) { |
| 2042 | com_addoparg(c, DUP_TOPX, 2); |
| 2043 | com_push(c, 2); |
| 2044 | com_addbyte(c, BINARY_SUBSCR); |
| 2045 | com_pop(c, 1); |
| 2046 | com_node(c, augn); |
| 2047 | com_addbyte(c, assigning); |
| 2048 | com_pop(c, 1); |
| 2049 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2050 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2051 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2052 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2056 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2057 | { |
| 2058 | REQ(n, trailer); |
| 2059 | switch (TYPE(CHILD(n, 0))) { |
| 2060 | case LPAR: |
| 2061 | com_call_function(c, CHILD(n, 1)); |
| 2062 | break; |
| 2063 | case DOT: |
| 2064 | com_select_member(c, CHILD(n, 1)); |
| 2065 | break; |
| 2066 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2067 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2068 | break; |
| 2069 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2070 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2071 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2076 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2077 | { |
| 2078 | int i; |
| 2079 | REQ(n, power); |
| 2080 | com_atom(c, CHILD(n, 0)); |
| 2081 | for (i = 1; i < NCH(n); i++) { |
| 2082 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 2083 | com_factor(c, CHILD(n, i+1)); |
| 2084 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2085 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2086 | break; |
| 2087 | } |
| 2088 | else |
| 2089 | com_apply_trailer(c, CHILD(n, i)); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2094 | com_invert_constant(struct compiling *c, node *n) |
| 2095 | { |
| 2096 | /* Compute the inverse of int and longs and use them directly, |
| 2097 | but be prepared to generate code for all other |
| 2098 | possibilities (invalid numbers, floats, complex). |
| 2099 | */ |
| 2100 | PyObject *num, *inv = NULL; |
| 2101 | int i; |
| 2102 | |
| 2103 | REQ(n, NUMBER); |
| 2104 | num = parsenumber(c, STR(n)); |
| 2105 | if (num == NULL) |
| 2106 | i = 255; |
| 2107 | else { |
| 2108 | inv = PyNumber_Invert(num); |
| 2109 | if (inv == NULL) { |
| 2110 | PyErr_Clear(); |
| 2111 | i = com_addconst(c, num); |
| 2112 | } else { |
| 2113 | i = com_addconst(c, inv); |
| 2114 | Py_DECREF(inv); |
| 2115 | } |
| 2116 | Py_DECREF(num); |
| 2117 | } |
| 2118 | com_addoparg(c, LOAD_CONST, i); |
| 2119 | com_push(c, 1); |
| 2120 | if (num != NULL && inv == NULL) |
| 2121 | com_addbyte(c, UNARY_INVERT); |
| 2122 | } |
| 2123 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2124 | static int |
| 2125 | is_float_zero(const char *p) |
| 2126 | { |
| 2127 | int found_radix_point = 0; |
| 2128 | int ch; |
| 2129 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2130 | switch (ch) { |
| 2131 | case '0': |
| 2132 | /* no reason to believe it's not 0 -- continue */ |
| 2133 | break; |
| 2134 | |
| 2135 | case 'e': case 'E': case 'j': case 'J': |
| 2136 | /* If this was a hex constant, we already would have |
| 2137 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2138 | must be an exponent marker, and we haven't yet |
| 2139 | seen a non-zero digit, and it doesn't matter what |
| 2140 | the exponent is then. For 'j' or 'J' similarly, |
| 2141 | except that this is an imaginary 0 then. */ |
| 2142 | return 1; |
| 2143 | |
| 2144 | case '.': |
| 2145 | found_radix_point = 1; |
| 2146 | break; |
| 2147 | |
| 2148 | default: |
| 2149 | return 0; |
| 2150 | } |
| 2151 | } |
| 2152 | return found_radix_point; |
| 2153 | } |
| 2154 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2155 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2156 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2157 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2158 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2159 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2160 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2161 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2162 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2163 | approriate value as a constant. If the value is negative, |
| 2164 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2165 | negative in the 0th position -- unless we're doing unary minus |
| 2166 | of a floating zero! In that case the sign is significant, but |
| 2167 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2168 | */ |
| 2169 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2170 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2171 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2172 | && NCH(pfactor) == 1 |
| 2173 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2174 | && NCH(ppower) == 1 |
| 2175 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2176 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
Guido van Rossum | 66b1259 | 2003-02-12 16:57:47 +0000 | [diff] [blame] | 2177 | && !(childtype == MINUS && |
| 2178 | (STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2179 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2180 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2181 | return; |
| 2182 | } |
| 2183 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2184 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2185 | if (s == NULL) { |
| 2186 | com_error(c, PyExc_MemoryError, ""); |
| 2187 | com_addbyte(c, 255); |
| 2188 | return; |
| 2189 | } |
| 2190 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2191 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2192 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2193 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2194 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2195 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2196 | } |
| 2197 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2198 | com_factor(c, CHILD(n, 1)); |
| 2199 | com_addbyte(c, UNARY_POSITIVE); |
| 2200 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2201 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2202 | com_factor(c, CHILD(n, 1)); |
| 2203 | com_addbyte(c, UNARY_NEGATIVE); |
| 2204 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2205 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2206 | com_factor(c, CHILD(n, 1)); |
| 2207 | com_addbyte(c, UNARY_INVERT); |
| 2208 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2209 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2210 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2215 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2216 | { |
| 2217 | int i; |
| 2218 | int op; |
| 2219 | REQ(n, term); |
| 2220 | com_factor(c, CHILD(n, 0)); |
| 2221 | for (i = 2; i < NCH(n); i += 2) { |
| 2222 | com_factor(c, CHILD(n, i)); |
| 2223 | switch (TYPE(CHILD(n, i-1))) { |
| 2224 | case STAR: |
| 2225 | op = BINARY_MULTIPLY; |
| 2226 | break; |
| 2227 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2228 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2229 | op = BINARY_TRUE_DIVIDE; |
| 2230 | else |
| 2231 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2232 | break; |
| 2233 | case PERCENT: |
| 2234 | op = BINARY_MODULO; |
| 2235 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2236 | case DOUBLESLASH: |
| 2237 | op = BINARY_FLOOR_DIVIDE; |
| 2238 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2239 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2240 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2241 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2242 | op = 255; |
| 2243 | } |
| 2244 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2245 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2250 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2251 | { |
| 2252 | int i; |
| 2253 | int op; |
| 2254 | REQ(n, arith_expr); |
| 2255 | com_term(c, CHILD(n, 0)); |
| 2256 | for (i = 2; i < NCH(n); i += 2) { |
| 2257 | com_term(c, CHILD(n, i)); |
| 2258 | switch (TYPE(CHILD(n, i-1))) { |
| 2259 | case PLUS: |
| 2260 | op = BINARY_ADD; |
| 2261 | break; |
| 2262 | case MINUS: |
| 2263 | op = BINARY_SUBTRACT; |
| 2264 | break; |
| 2265 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2266 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2267 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2268 | op = 255; |
| 2269 | } |
| 2270 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2271 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2276 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2277 | { |
| 2278 | int i; |
| 2279 | int op; |
| 2280 | REQ(n, shift_expr); |
| 2281 | com_arith_expr(c, CHILD(n, 0)); |
| 2282 | for (i = 2; i < NCH(n); i += 2) { |
| 2283 | com_arith_expr(c, CHILD(n, i)); |
| 2284 | switch (TYPE(CHILD(n, i-1))) { |
| 2285 | case LEFTSHIFT: |
| 2286 | op = BINARY_LSHIFT; |
| 2287 | break; |
| 2288 | case RIGHTSHIFT: |
| 2289 | op = BINARY_RSHIFT; |
| 2290 | break; |
| 2291 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2292 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2293 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2294 | op = 255; |
| 2295 | } |
| 2296 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2297 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2302 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2303 | { |
| 2304 | int i; |
| 2305 | int op; |
| 2306 | REQ(n, and_expr); |
| 2307 | com_shift_expr(c, CHILD(n, 0)); |
| 2308 | for (i = 2; i < NCH(n); i += 2) { |
| 2309 | com_shift_expr(c, CHILD(n, i)); |
| 2310 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2311 | op = BINARY_AND; |
| 2312 | } |
| 2313 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2314 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2315 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2316 | op = 255; |
| 2317 | } |
| 2318 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2319 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2324 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2325 | { |
| 2326 | int i; |
| 2327 | int op; |
| 2328 | REQ(n, xor_expr); |
| 2329 | com_and_expr(c, CHILD(n, 0)); |
| 2330 | for (i = 2; i < NCH(n); i += 2) { |
| 2331 | com_and_expr(c, CHILD(n, i)); |
| 2332 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2333 | op = BINARY_XOR; |
| 2334 | } |
| 2335 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2336 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2337 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2338 | op = 255; |
| 2339 | } |
| 2340 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2341 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2346 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2347 | { |
| 2348 | int i; |
| 2349 | int op; |
| 2350 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2351 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2352 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2353 | com_xor_expr(c, CHILD(n, i)); |
| 2354 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2355 | op = BINARY_OR; |
| 2356 | } |
| 2357 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2358 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2359 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2360 | op = 255; |
| 2361 | } |
| 2362 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2363 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2368 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2369 | { |
| 2370 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2371 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2372 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2373 | if (NCH(n) == 1) { |
| 2374 | n = CHILD(n, 0); |
| 2375 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2376 | case LESS: return PyCmp_LT; |
| 2377 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2378 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2379 | case EQUAL: return PyCmp_EQ; |
| 2380 | case LESSEQUAL: return PyCmp_LE; |
| 2381 | case GREATEREQUAL: return PyCmp_GE; |
| 2382 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2383 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2384 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2385 | } |
| 2386 | } |
| 2387 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2388 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2389 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2390 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2391 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2392 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2393 | } |
| 2394 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2395 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2399 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2400 | { |
| 2401 | int i; |
| 2402 | enum cmp_op op; |
| 2403 | int anchor; |
| 2404 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2405 | com_expr(c, CHILD(n, 0)); |
| 2406 | if (NCH(n) == 1) |
| 2407 | return; |
| 2408 | |
| 2409 | /**************************************************************** |
| 2410 | The following code is generated for all but the last |
| 2411 | comparison in a chain: |
| 2412 | |
| 2413 | label: on stack: opcode: jump to: |
| 2414 | |
| 2415 | a <code to load b> |
| 2416 | a, b DUP_TOP |
| 2417 | a, b, b ROT_THREE |
| 2418 | b, a, b COMPARE_OP |
| 2419 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2420 | b, 1 POP_TOP |
| 2421 | b |
| 2422 | |
| 2423 | We are now ready to repeat this sequence for the next |
| 2424 | comparison in the chain. |
| 2425 | |
| 2426 | For the last we generate: |
| 2427 | |
| 2428 | b <code to load c> |
| 2429 | b, c COMPARE_OP |
| 2430 | 0-or-1 |
| 2431 | |
| 2432 | If there were any jumps to L1 (i.e., there was more than one |
| 2433 | comparison), we generate: |
| 2434 | |
| 2435 | 0-or-1 JUMP_FORWARD L2 |
| 2436 | L1: b, 0 ROT_TWO |
| 2437 | 0, b POP_TOP |
| 2438 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2439 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2440 | ****************************************************************/ |
| 2441 | |
| 2442 | anchor = 0; |
| 2443 | |
| 2444 | for (i = 2; i < NCH(n); i += 2) { |
| 2445 | com_expr(c, CHILD(n, i)); |
| 2446 | if (i+2 < NCH(n)) { |
| 2447 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2448 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2449 | com_addbyte(c, ROT_THREE); |
| 2450 | } |
| 2451 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2452 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2453 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2454 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2455 | } |
| 2456 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2457 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2458 | if (i+2 < NCH(n)) { |
| 2459 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2460 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2461 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2462 | } |
| 2463 | } |
| 2464 | |
| 2465 | if (anchor) { |
| 2466 | int anchor2 = 0; |
| 2467 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2468 | com_backpatch(c, anchor); |
| 2469 | com_addbyte(c, ROT_TWO); |
| 2470 | com_addbyte(c, POP_TOP); |
| 2471 | com_backpatch(c, anchor2); |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2476 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2477 | { |
| 2478 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2479 | if (NCH(n) == 1) { |
| 2480 | com_comparison(c, CHILD(n, 0)); |
| 2481 | } |
| 2482 | else { |
| 2483 | com_not_test(c, CHILD(n, 1)); |
| 2484 | com_addbyte(c, UNARY_NOT); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2489 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2490 | { |
| 2491 | int i; |
| 2492 | int anchor; |
| 2493 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2494 | anchor = 0; |
| 2495 | i = 0; |
| 2496 | for (;;) { |
| 2497 | com_not_test(c, CHILD(n, i)); |
| 2498 | if ((i += 2) >= NCH(n)) |
| 2499 | break; |
| 2500 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2501 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2502 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2503 | } |
| 2504 | if (anchor) |
| 2505 | com_backpatch(c, anchor); |
| 2506 | } |
| 2507 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2508 | static int |
| 2509 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2510 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2511 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2512 | if (free == 0) |
| 2513 | return 0; |
| 2514 | for (i = 0; i < free; ++i) { |
| 2515 | /* Bypass com_addop_varname because it will generate |
| 2516 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2517 | */ |
| 2518 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2519 | int arg, reftype; |
| 2520 | |
| 2521 | /* Special case: If a class contains a method with a |
| 2522 | free variable that has the same name as a method, |
| 2523 | the name will be considered free *and* local in the |
| 2524 | class. It should be handled by the closure, as |
| 2525 | well as by the normal name loookup logic. |
| 2526 | */ |
| 2527 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2528 | if (reftype == CELL) |
| 2529 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2530 | else /* (reftype == FREE) */ |
| 2531 | arg = com_lookup_arg(c->c_freevars, name); |
| 2532 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2533 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2534 | "freevars of %s: %s\n", |
| 2535 | PyObject_REPR(name), |
| 2536 | c->c_name, |
| 2537 | reftype, arg, |
| 2538 | PyString_AS_STRING(co->co_name), |
| 2539 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2540 | Py_FatalError("com_make_closure()"); |
| 2541 | } |
| 2542 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2543 | |
| 2544 | } |
| 2545 | com_push(c, free); |
| 2546 | return 1; |
| 2547 | } |
| 2548 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2549 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2550 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2551 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2552 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2553 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2554 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2555 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2556 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2557 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2558 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2559 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2560 | if (co == NULL) { |
| 2561 | c->c_errors++; |
| 2562 | return; |
| 2563 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2564 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2565 | i = com_addconst(c, (PyObject *)co); |
| 2566 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2567 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2568 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2569 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2570 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2571 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2572 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2573 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2574 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2575 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2576 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2577 | else { |
| 2578 | int anchor = 0; |
| 2579 | int i = 0; |
| 2580 | for (;;) { |
| 2581 | com_and_test(c, CHILD(n, i)); |
| 2582 | if ((i += 2) >= NCH(n)) |
| 2583 | break; |
| 2584 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2585 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2586 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2587 | } |
| 2588 | if (anchor) |
| 2589 | com_backpatch(c, anchor); |
| 2590 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2594 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2595 | { |
| 2596 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2597 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2598 | com_node(c, CHILD(n, 0)); |
| 2599 | } |
| 2600 | else { |
| 2601 | int i; |
| 2602 | int len; |
| 2603 | len = (NCH(n) + 1) / 2; |
| 2604 | for (i = 0; i < NCH(n); i += 2) |
| 2605 | com_node(c, CHILD(n, i)); |
| 2606 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2607 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | |
| 2612 | /* Begin of assignment compilation */ |
| 2613 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2614 | |
| 2615 | static void |
| 2616 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2617 | { |
| 2618 | com_addbyte(c, DUP_TOP); |
| 2619 | com_push(c, 1); |
| 2620 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2621 | com_node(c, augn); |
| 2622 | com_addbyte(c, opcode); |
| 2623 | com_pop(c, 1); |
| 2624 | com_addbyte(c, ROT_TWO); |
| 2625 | com_addopname(c, STORE_ATTR, n); |
| 2626 | com_pop(c, 2); |
| 2627 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2628 | |
| 2629 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2630 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2631 | { |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 2632 | if (none_assignment_check(c, STR(n), assigning)) |
| 2633 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2634 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2635 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2639 | 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] | 2640 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | REQ(n, trailer); |
| 2642 | switch (TYPE(CHILD(n, 0))) { |
| 2643 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2644 | if (assigning == OP_DELETE) |
| 2645 | com_error(c, PyExc_SyntaxError, |
| 2646 | "can't delete function call"); |
| 2647 | else |
| 2648 | com_error(c, PyExc_SyntaxError, |
| 2649 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2650 | break; |
| 2651 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2652 | if (assigning > OP_APPLY) |
| 2653 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2654 | else |
| 2655 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2656 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2657 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2658 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2659 | break; |
| 2660 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2661 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2666 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2667 | { |
| 2668 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2669 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2670 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2671 | if (assigning) { |
| 2672 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2673 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2674 | com_push(c, i-1); |
| 2675 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2676 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2677 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2678 | } |
| 2679 | |
| 2680 | static void |
| 2681 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2682 | { |
| 2683 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2684 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2685 | com_push(c, 1); |
| 2686 | com_node(c, augn); |
| 2687 | com_addbyte(c, opcode); |
| 2688 | com_pop(c, 1); |
| 2689 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2693 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2694 | { |
| 2695 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2696 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2697 | if (assigning) |
| 2698 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2702 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2703 | { |
| 2704 | /* Loop to avoid trivial recursion */ |
| 2705 | for (;;) { |
| 2706 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2707 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2708 | case exprlist: |
| 2709 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2710 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2711 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2712 | if (assigning > OP_APPLY) { |
| 2713 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2714 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2717 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2718 | return; |
| 2719 | } |
| 2720 | n = CHILD(n, 0); |
| 2721 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2722 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2723 | case test: |
| 2724 | case and_test: |
| 2725 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2726 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2727 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2728 | case xor_expr: |
| 2729 | case and_expr: |
| 2730 | case shift_expr: |
| 2731 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2732 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2733 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2734 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2735 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2736 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2737 | return; |
| 2738 | } |
| 2739 | n = CHILD(n, 0); |
| 2740 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2741 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2742 | case power: /* atom trailer* ('**' power)* |
| 2743 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2744 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2745 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2746 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2747 | return; |
| 2748 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2749 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2750 | int i; |
| 2751 | com_node(c, CHILD(n, 0)); |
| 2752 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2753 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2754 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2755 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2756 | return; |
| 2757 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2758 | com_apply_trailer(c, CHILD(n, i)); |
| 2759 | } /* NB i is still alive */ |
| 2760 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2761 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2762 | return; |
| 2763 | } |
| 2764 | n = CHILD(n, 0); |
| 2765 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2766 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2767 | case atom: |
| 2768 | switch (TYPE(CHILD(n, 0))) { |
| 2769 | case LPAR: |
| 2770 | n = CHILD(n, 1); |
| 2771 | if (TYPE(n) == RPAR) { |
| 2772 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2773 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2774 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2775 | return; |
| 2776 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2777 | if (assigning > OP_APPLY) { |
| 2778 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2779 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2780 | return; |
| 2781 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2782 | break; |
| 2783 | case LSQB: |
| 2784 | n = CHILD(n, 1); |
| 2785 | if (TYPE(n) == RSQB) { |
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 []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2788 | return; |
| 2789 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2790 | if (assigning > OP_APPLY) { |
| 2791 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2792 | "augmented assign to list not possible"); |
| 2793 | return; |
| 2794 | } |
| 2795 | if (NCH(n) > 1 |
| 2796 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2797 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2798 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2799 | return; |
| 2800 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2801 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2804 | if (assigning > OP_APPLY) |
| 2805 | com_augassign_name(c, CHILD(n, 0), |
| 2806 | assigning, augn); |
| 2807 | else |
| 2808 | com_assign_name(c, CHILD(n, 0), |
| 2809 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2812 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2813 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2814 | return; |
| 2815 | } |
| 2816 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2817 | |
| 2818 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2819 | com_error(c, PyExc_SyntaxError, |
| 2820 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2821 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2822 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2823 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2824 | com_error(c, PyExc_SystemError, |
| 2825 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2826 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2827 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2831 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2832 | static void |
| 2833 | com_augassign(struct compiling *c, node *n) |
| 2834 | { |
| 2835 | int opcode; |
| 2836 | |
| 2837 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2838 | case '+': opcode = INPLACE_ADD; break; |
| 2839 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2840 | case '/': |
| 2841 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2842 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2843 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2844 | opcode = INPLACE_TRUE_DIVIDE; |
| 2845 | else |
| 2846 | opcode = INPLACE_DIVIDE; |
| 2847 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2848 | case '%': opcode = INPLACE_MODULO; break; |
| 2849 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2850 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2851 | case '&': opcode = INPLACE_AND; break; |
| 2852 | case '^': opcode = INPLACE_XOR; break; |
| 2853 | case '|': opcode = INPLACE_OR; break; |
| 2854 | case '*': |
| 2855 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2856 | opcode = INPLACE_POWER; |
| 2857 | else |
| 2858 | opcode = INPLACE_MULTIPLY; |
| 2859 | break; |
| 2860 | default: |
| 2861 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2862 | return; |
| 2863 | } |
| 2864 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2865 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2866 | |
| 2867 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2868 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2869 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2870 | REQ(n, expr_stmt); |
| 2871 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2872 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2873 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2874 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2875 | if (NCH(n) == 1) { |
| 2876 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2877 | if (c->c_interactive) |
| 2878 | com_addbyte(c, PRINT_EXPR); |
| 2879 | else |
| 2880 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2881 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2882 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2883 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2884 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2885 | else { |
| 2886 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2887 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2888 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2889 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2890 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2891 | com_push(c, 1); |
| 2892 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2893 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2894 | } |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2899 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2900 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2901 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2902 | int i; |
| 2903 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2904 | if (Py_OptimizeFlag) |
| 2905 | return; |
| 2906 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2907 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2908 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2909 | raise AssertionError [, <message>] |
| 2910 | |
| 2911 | where <message> is the second test, if present. |
| 2912 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2913 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2914 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2915 | com_addbyte(c, POP_TOP); |
| 2916 | com_pop(c, 1); |
| 2917 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2918 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2919 | com_push(c, 1); |
| 2920 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2921 | if (i > 1) |
| 2922 | com_node(c, CHILD(n, 3)); |
| 2923 | com_addoparg(c, RAISE_VARARGS, i); |
| 2924 | com_pop(c, i); |
| 2925 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2926 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2927 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2928 | com_addbyte(c, POP_TOP); |
| 2929 | } |
| 2930 | |
| 2931 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2932 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2933 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2934 | int i = 1; |
| 2935 | node* stream = NULL; |
| 2936 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2937 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2938 | |
| 2939 | /* are we using the extended print form? */ |
| 2940 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2941 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2942 | com_node(c, stream); |
| 2943 | /* stack: [...] => [... stream] */ |
| 2944 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2945 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2946 | i = 4; |
| 2947 | else |
| 2948 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2949 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2950 | for (; i < NCH(n); i += 2) { |
| 2951 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2952 | com_addbyte(c, DUP_TOP); |
| 2953 | /* stack: [stream] => [stream stream] */ |
| 2954 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2955 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2956 | /* stack: [stream stream] => [stream stream obj] */ |
| 2957 | com_addbyte(c, ROT_TWO); |
| 2958 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2959 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2960 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2961 | com_pop(c, 2); |
| 2962 | } |
| 2963 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2964 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2965 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2966 | com_addbyte(c, PRINT_ITEM); |
| 2967 | com_pop(c, 1); |
| 2968 | } |
| 2969 | } |
| 2970 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2971 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2972 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2973 | /* must pop the extra stream object off the stack */ |
| 2974 | com_addbyte(c, POP_TOP); |
| 2975 | /* stack: [... stream] => [...] */ |
| 2976 | com_pop(c, 1); |
| 2977 | } |
| 2978 | } |
| 2979 | else { |
| 2980 | if (stream != NULL) { |
| 2981 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2982 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2983 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2984 | com_pop(c, 1); |
| 2985 | } |
| 2986 | else |
| 2987 | com_addbyte(c, PRINT_NEWLINE); |
| 2988 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2992 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2994 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2995 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2996 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2997 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2998 | if (c->c_flags & CO_GENERATOR) { |
| 2999 | if (NCH(n) > 1) { |
| 3000 | com_error(c, PyExc_SyntaxError, |
| 3001 | "'return' with argument inside generator"); |
| 3002 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3003 | } |
| 3004 | if (NCH(n) < 2) { |
| 3005 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3006 | com_push(c, 1); |
| 3007 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3008 | else |
| 3009 | com_node(c, CHILD(n, 1)); |
| 3010 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3011 | com_pop(c, 1); |
| 3012 | } |
| 3013 | |
| 3014 | static void |
| 3015 | com_yield_stmt(struct compiling *c, node *n) |
| 3016 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 3017 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3018 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 3019 | if (!c->c_infunction) { |
| 3020 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 3021 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 3022 | |
| 3023 | for (i = 0; i < c->c_nblocks; ++i) { |
| 3024 | if (c->c_block[i] == SETUP_FINALLY) { |
| 3025 | com_error(c, PyExc_SyntaxError, |
| 3026 | "'yield' not allowed in a 'try' block " |
| 3027 | "with a 'finally' clause"); |
| 3028 | return; |
| 3029 | } |
| 3030 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3031 | com_node(c, CHILD(n, 1)); |
| 3032 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3033 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3037 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3038 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3039 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3040 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 3041 | if (NCH(n) > 1) { |
| 3042 | com_node(c, CHILD(n, 1)); |
| 3043 | if (NCH(n) > 3) { |
| 3044 | com_node(c, CHILD(n, 3)); |
| 3045 | if (NCH(n) > 5) |
| 3046 | com_node(c, CHILD(n, 5)); |
| 3047 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3048 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3049 | i = NCH(n)/2; |
| 3050 | com_addoparg(c, RAISE_VARARGS, i); |
| 3051 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3055 | com_from_import(struct compiling *c, node *n) |
| 3056 | { |
| 3057 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 3058 | com_push(c, 1); |
| 3059 | if (NCH(n) > 1) { |
| 3060 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 3061 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 3062 | return; |
| 3063 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3064 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3065 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3066 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3067 | com_pop(c, 1); |
| 3068 | } |
| 3069 | |
| 3070 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3071 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3072 | { |
| 3073 | int i; |
| 3074 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3075 | /* 'import' dotted_name (',' dotted_name)* | |
| 3076 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3077 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3078 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3079 | /* 'from' dotted_name 'import' ... */ |
| 3080 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3081 | |
| 3082 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 3083 | tup = Py_BuildValue("(s)", "*"); |
| 3084 | } else { |
| 3085 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 3086 | for (i = 3; i < NCH(n); i += 2) { |
| 3087 | PyTuple_SET_ITEM(tup, (i-3)/2, |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3088 | PyString_FromString(STR( |
| 3089 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3090 | } |
| 3091 | } |
| 3092 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3093 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3094 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3095 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3096 | if (TYPE(CHILD(n, 3)) == STAR) |
| 3097 | com_addbyte(c, IMPORT_STAR); |
| 3098 | else { |
| 3099 | for (i = 3; i < NCH(n); i += 2) |
| 3100 | com_from_import(c, CHILD(n, i)); |
| 3101 | com_addbyte(c, POP_TOP); |
| 3102 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3103 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3104 | } |
| 3105 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3106 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3107 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3108 | node *subn = CHILD(n, i); |
| 3109 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3110 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3111 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3112 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3113 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3114 | int j; |
| 3115 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3116 | com_error(c, PyExc_SyntaxError, |
| 3117 | "invalid syntax"); |
| 3118 | return; |
| 3119 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3120 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3121 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3122 | CHILD(CHILD(subn, 0), |
| 3123 | j)); |
| 3124 | com_addop_varname(c, VAR_STORE, |
| 3125 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3126 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3127 | com_addop_varname(c, VAR_STORE, |
| 3128 | STR(CHILD(CHILD(subn, 0), |
| 3129 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3130 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3136 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3137 | { |
| 3138 | REQ(n, exec_stmt); |
| 3139 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3140 | com_node(c, CHILD(n, 1)); |
| 3141 | if (NCH(n) >= 4) |
| 3142 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3143 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3144 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3145 | com_push(c, 1); |
| 3146 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3147 | if (NCH(n) >= 6) |
| 3148 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3149 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3150 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3151 | com_push(c, 1); |
| 3152 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3153 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3154 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3157 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3158 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3159 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3160 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3161 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3162 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3163 | |
| 3164 | /* Label to avoid tail recursion */ |
| 3165 | next: |
| 3166 | switch (TYPE(n)) { |
| 3167 | |
| 3168 | case suite: |
| 3169 | if (NCH(n) == 1) { |
| 3170 | n = CHILD(n, 0); |
| 3171 | goto next; |
| 3172 | } |
| 3173 | /* Fall through */ |
| 3174 | case file_input: |
| 3175 | for (i = 0; i < NCH(n); i++) { |
| 3176 | node *ch = CHILD(n, i); |
| 3177 | if (TYPE(ch) == stmt) { |
| 3178 | n = ch; |
| 3179 | goto next; |
| 3180 | } |
| 3181 | } |
| 3182 | break; |
| 3183 | |
| 3184 | case stmt: |
| 3185 | case simple_stmt: |
| 3186 | case small_stmt: |
| 3187 | n = CHILD(n, 0); |
| 3188 | goto next; |
| 3189 | |
| 3190 | case expr_stmt: |
| 3191 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3192 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3193 | case test: |
| 3194 | case and_test: |
| 3195 | case not_test: |
| 3196 | case comparison: |
| 3197 | case expr: |
| 3198 | case xor_expr: |
| 3199 | case and_expr: |
| 3200 | case shift_expr: |
| 3201 | case arith_expr: |
| 3202 | case term: |
| 3203 | case factor: |
| 3204 | case power: |
| 3205 | case atom: |
| 3206 | if (NCH(n) == 1) { |
| 3207 | n = CHILD(n, 0); |
| 3208 | goto next; |
| 3209 | } |
| 3210 | break; |
| 3211 | |
| 3212 | case NAME: |
| 3213 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3214 | return 1; |
| 3215 | break; |
| 3216 | |
| 3217 | case NUMBER: |
| 3218 | v = parsenumber(c, STR(n)); |
| 3219 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3220 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3221 | break; |
| 3222 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3223 | i = PyObject_IsTrue(v); |
| 3224 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3225 | return i == 0; |
| 3226 | |
| 3227 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3228 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3229 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3230 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3233 | i = PyObject_IsTrue(v); |
| 3234 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3235 | return i == 0; |
| 3236 | |
| 3237 | } |
| 3238 | return 0; |
| 3239 | } |
| 3240 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3241 | |
| 3242 | /* Look under n for a return stmt with an expression. |
| 3243 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3244 | * functions already known to be generators (as determined by the symtable |
| 3245 | * pass). |
| 3246 | * Return the offending return node if found, else NULL. |
| 3247 | */ |
| 3248 | static node * |
| 3249 | look_for_offending_return(node *n) |
| 3250 | { |
| 3251 | int i; |
| 3252 | |
| 3253 | for (i = 0; i < NCH(n); ++i) { |
| 3254 | node *kid = CHILD(n, i); |
| 3255 | |
| 3256 | switch (TYPE(kid)) { |
| 3257 | case classdef: |
| 3258 | case funcdef: |
| 3259 | case lambdef: |
| 3260 | /* Stuff in nested functions & classes doesn't |
| 3261 | affect the code block we started in. */ |
| 3262 | return NULL; |
| 3263 | |
| 3264 | case return_stmt: |
| 3265 | if (NCH(kid) > 1) |
| 3266 | return kid; |
| 3267 | break; |
| 3268 | |
| 3269 | default: { |
| 3270 | node *bad = look_for_offending_return(kid); |
| 3271 | if (bad != NULL) |
| 3272 | return bad; |
| 3273 | } |
| 3274 | } |
| 3275 | } |
| 3276 | |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3280 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3281 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3282 | { |
| 3283 | int i; |
| 3284 | int anchor = 0; |
| 3285 | REQ(n, if_stmt); |
| 3286 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3287 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3288 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3289 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3290 | if (is_constant_false(c, ch)) { |
| 3291 | /* We're going to skip this block. However, if this |
| 3292 | is a generator, we have to check the dead code |
| 3293 | anyway to make sure there aren't any return stmts |
| 3294 | with expressions, in the same scope. */ |
| 3295 | if (c->c_flags & CO_GENERATOR) { |
| 3296 | node *p = look_for_offending_return(n); |
| 3297 | if (p != NULL) { |
| 3298 | int savelineno = c->c_lineno; |
| 3299 | c->c_lineno = p->n_lineno; |
| 3300 | com_error(c, PyExc_SyntaxError, |
| 3301 | "'return' with argument " |
| 3302 | "inside generator"); |
| 3303 | c->c_lineno = savelineno; |
| 3304 | } |
| 3305 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3306 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3307 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3308 | if (i > 0) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3309 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3310 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3311 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3312 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3313 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3314 | com_node(c, CHILD(n, i+3)); |
| 3315 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3316 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3317 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3318 | com_addbyte(c, POP_TOP); |
| 3319 | } |
| 3320 | if (i+2 < NCH(n)) |
| 3321 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3322 | if (anchor) |
| 3323 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3327 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3328 | { |
| 3329 | int break_anchor = 0; |
| 3330 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3331 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3332 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3333 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3334 | block_push(c, SETUP_LOOP); |
| 3335 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3336 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3337 | com_node(c, CHILD(n, 1)); |
| 3338 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3339 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3340 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3341 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3342 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3343 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3344 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3345 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3346 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3347 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3348 | com_addbyte(c, POP_TOP); |
| 3349 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3350 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3351 | if (NCH(n) > 4) |
| 3352 | com_node(c, CHILD(n, 6)); |
| 3353 | com_backpatch(c, break_anchor); |
| 3354 | } |
| 3355 | |
| 3356 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3357 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3358 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3359 | int break_anchor = 0; |
| 3360 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3361 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3362 | REQ(n, for_stmt); |
| 3363 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3364 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3365 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3366 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3367 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3368 | c->c_begin = c->c_nexti; |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 3369 | com_set_lineno(c, c->c_last_line); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3370 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3371 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3372 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3373 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3374 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3375 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3376 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3377 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3378 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3379 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3380 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3381 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3382 | if (NCH(n) > 8) |
| 3383 | com_node(c, CHILD(n, 8)); |
| 3384 | com_backpatch(c, break_anchor); |
| 3385 | } |
| 3386 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3387 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3388 | |
| 3389 | SETUP_FINALLY L |
| 3390 | <code for S> |
| 3391 | POP_BLOCK |
| 3392 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3393 | L: <code for Sf> |
| 3394 | END_FINALLY |
| 3395 | |
| 3396 | The special instructions use the block stack. Each block |
| 3397 | stack entry contains the instruction that created it (here |
| 3398 | SETUP_FINALLY), the level of the value stack at the time the |
| 3399 | block stack entry was created, and a label (here L). |
| 3400 | |
| 3401 | SETUP_FINALLY: |
| 3402 | Pushes the current value stack level and the label |
| 3403 | onto the block stack. |
| 3404 | POP_BLOCK: |
| 3405 | Pops en entry from the block stack, and pops the value |
| 3406 | stack until its level is the same as indicated on the |
| 3407 | block stack. (The label is ignored.) |
| 3408 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3409 | Pops a variable number of entries from the *value* stack |
| 3410 | and re-raises the exception they specify. The number of |
| 3411 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3412 | |
| 3413 | The block stack is unwound when an exception is raised: |
| 3414 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3415 | onto the value stack (and the exception condition is cleared), |
| 3416 | and the interpreter jumps to the label gotten from the block |
| 3417 | stack. |
| 3418 | |
| 3419 | 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] | 3420 | (The contents of the value stack is shown in [], with the top |
| 3421 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3422 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3423 | |
| 3424 | Value stack Label Instruction Argument |
| 3425 | [] SETUP_EXCEPT L1 |
| 3426 | [] <code for S> |
| 3427 | [] POP_BLOCK |
| 3428 | [] JUMP_FORWARD L0 |
| 3429 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3430 | [tb, val, exc] L1: DUP ) |
| 3431 | [tb, val, exc, exc] <evaluate E1> ) |
| 3432 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3433 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3434 | [tb, val, exc, 1] POP ) |
| 3435 | [tb, val, exc] POP |
| 3436 | [tb, val] <assign to V1> (or POP if no V1) |
| 3437 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3438 | [] <code for S1> |
| 3439 | JUMP_FORWARD L0 |
| 3440 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3441 | [tb, val, exc, 0] L2: POP |
| 3442 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3443 | .............................etc....................... |
| 3444 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3445 | [tb, val, exc, 0] Ln+1: POP |
| 3446 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3447 | |
| 3448 | [] L0: <next statement> |
| 3449 | |
| 3450 | Of course, parts are not generated if Vi or Ei is not present. |
| 3451 | */ |
| 3452 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3453 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3454 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3455 | { |
| 3456 | int except_anchor = 0; |
| 3457 | int end_anchor = 0; |
| 3458 | int else_anchor = 0; |
| 3459 | int i; |
| 3460 | node *ch; |
| 3461 | |
| 3462 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3463 | block_push(c, SETUP_EXCEPT); |
| 3464 | com_node(c, CHILD(n, 2)); |
| 3465 | com_addbyte(c, POP_BLOCK); |
| 3466 | block_pop(c, SETUP_EXCEPT); |
| 3467 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3468 | com_backpatch(c, except_anchor); |
| 3469 | for (i = 3; |
| 3470 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3471 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3472 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3473 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3474 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3475 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | } |
| 3478 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3479 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3480 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3481 | if (NCH(ch) > 1) { |
| 3482 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3483 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3484 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3485 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3486 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3487 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3488 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3489 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3490 | } |
| 3491 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3492 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3493 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3494 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3495 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3496 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3497 | com_pop(c, 1); |
| 3498 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3499 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3500 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3501 | com_node(c, CHILD(n, i+2)); |
| 3502 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3503 | if (except_anchor) { |
| 3504 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3505 | /* We come in with [tb, val, exc, 0] on the |
| 3506 | stack; one pop and it's the same as |
| 3507 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3508 | com_addbyte(c, POP_TOP); |
| 3509 | } |
| 3510 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3511 | /* We actually come in here with [tb, val, exc] but the |
| 3512 | END_FINALLY will zap those and jump around. |
| 3513 | The c_stacklevel does not reflect them so we need not pop |
| 3514 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3515 | com_addbyte(c, END_FINALLY); |
| 3516 | com_backpatch(c, else_anchor); |
| 3517 | if (i < NCH(n)) |
| 3518 | com_node(c, CHILD(n, i+2)); |
| 3519 | com_backpatch(c, end_anchor); |
| 3520 | } |
| 3521 | |
| 3522 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3523 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3524 | { |
| 3525 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3526 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3527 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3528 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3529 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3530 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3531 | com_addbyte(c, POP_BLOCK); |
| 3532 | block_pop(c, SETUP_FINALLY); |
| 3533 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3534 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3535 | /* While the generated code pushes only one item, |
| 3536 | the try-finally handling can enter here with |
| 3537 | up to three items. OK, here are the details: |
| 3538 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3539 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3540 | com_backpatch(c, finally_anchor); |
| 3541 | ch = CHILD(n, NCH(n)-1); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3542 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3543 | com_node(c, ch); |
| 3544 | com_addbyte(c, END_FINALLY); |
| 3545 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3546 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3550 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3551 | { |
| 3552 | REQ(n, try_stmt); |
| 3553 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3554 | | 'try' ':' suite 'finally' ':' suite */ |
| 3555 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3556 | com_try_finally(c, n); |
| 3557 | else |
| 3558 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3561 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3562 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3563 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3564 | int i; |
| 3565 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3566 | /* Label to avoid tail recursion */ |
| 3567 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3568 | switch (TYPE(n)) { |
| 3569 | |
| 3570 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3571 | if (NCH(n) == 1) { |
| 3572 | n = CHILD(n, 0); |
| 3573 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3574 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3575 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3576 | case file_input: |
| 3577 | for (i = 0; i < NCH(n); i++) { |
| 3578 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3579 | if (TYPE(ch) == stmt) { |
| 3580 | n = ch; |
| 3581 | goto next; |
| 3582 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3583 | } |
| 3584 | break; |
| 3585 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3586 | case stmt: |
| 3587 | case simple_stmt: |
| 3588 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3589 | n = CHILD(n, 0); |
| 3590 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3591 | |
| 3592 | case expr_stmt: |
| 3593 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3594 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3595 | case test: |
| 3596 | case and_test: |
| 3597 | case not_test: |
| 3598 | case comparison: |
| 3599 | case expr: |
| 3600 | case xor_expr: |
| 3601 | case and_expr: |
| 3602 | case shift_expr: |
| 3603 | case arith_expr: |
| 3604 | case term: |
| 3605 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3606 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3607 | if (NCH(n) == 1) { |
| 3608 | n = CHILD(n, 0); |
| 3609 | goto next; |
| 3610 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3611 | break; |
| 3612 | |
| 3613 | case atom: |
| 3614 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3615 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3616 | break; |
| 3617 | |
| 3618 | } |
| 3619 | return NULL; |
| 3620 | } |
| 3621 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3622 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3623 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3624 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3625 | /* Don't generate doc-strings if run with -OO */ |
| 3626 | if (Py_OptimizeFlag > 1) |
| 3627 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3628 | n = get_rawdocstring(n); |
| 3629 | if (n == NULL) |
| 3630 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3631 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3634 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3635 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3636 | { |
| 3637 | REQ(n, suite); |
| 3638 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3639 | if (NCH(n) == 1) { |
| 3640 | com_node(c, CHILD(n, 0)); |
| 3641 | } |
| 3642 | else { |
| 3643 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3644 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3645 | node *ch = CHILD(n, i); |
| 3646 | if (TYPE(ch) == stmt) |
| 3647 | com_node(c, ch); |
| 3648 | } |
| 3649 | } |
| 3650 | } |
| 3651 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3652 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3653 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3654 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3655 | { |
| 3656 | int i = c->c_nblocks; |
| 3657 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3658 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3659 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3660 | else if (i <= 0) { |
| 3661 | /* at the outer level */ |
| 3662 | com_error(c, PyExc_SyntaxError, |
| 3663 | "'continue' not properly in loop"); |
| 3664 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3665 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3666 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3667 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3668 | if (c->c_block[j] == SETUP_LOOP) |
| 3669 | break; |
| 3670 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3671 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3672 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3673 | for (; i > j; --i) { |
| 3674 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3675 | c->c_block[i] == SETUP_FINALLY) { |
| 3676 | com_addoparg(c, CONTINUE_LOOP, |
| 3677 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3678 | return; |
| 3679 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3680 | if (c->c_block[i] == END_FINALLY) { |
| 3681 | com_error(c, PyExc_SyntaxError, |
| 3682 | "'continue' not supported inside 'finally' clause"); |
| 3683 | return; |
| 3684 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3685 | } |
| 3686 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3687 | com_error(c, PyExc_SyntaxError, |
| 3688 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3689 | } |
| 3690 | /* XXX Could allow it inside a 'finally' clause |
| 3691 | XXX if we could pop the exception still on the stack */ |
| 3692 | } |
| 3693 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3694 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3695 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3696 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3697 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3698 | if (TYPE(n) == lambdef) { |
| 3699 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3700 | n = CHILD(n, 1); |
| 3701 | } |
| 3702 | else { |
| 3703 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3704 | n = CHILD(n, 2); |
| 3705 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3706 | n = CHILD(n, 1); |
| 3707 | } |
| 3708 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3709 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3710 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3711 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3712 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3713 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3714 | nargs = 0; |
| 3715 | ndefs = 0; |
| 3716 | for (i = 0; i < nch; i++) { |
| 3717 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3718 | if (TYPE(CHILD(n, i)) == STAR || |
| 3719 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3720 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3721 | nargs++; |
| 3722 | i++; |
| 3723 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3724 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3725 | else |
| 3726 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3727 | if (t == EQUAL) { |
| 3728 | i++; |
| 3729 | ndefs++; |
| 3730 | com_node(c, CHILD(n, i)); |
| 3731 | i++; |
| 3732 | if (i >= nch) |
| 3733 | break; |
| 3734 | t = TYPE(CHILD(n, i)); |
| 3735 | } |
| 3736 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3737 | /* Treat "(a=1, b)" as an error */ |
| 3738 | if (ndefs) |
| 3739 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3740 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3741 | } |
| 3742 | if (t != COMMA) |
| 3743 | break; |
| 3744 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3745 | return ndefs; |
| 3746 | } |
| 3747 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3748 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3749 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3750 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3751 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3752 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3753 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3754 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3755 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3756 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3757 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3758 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3759 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3760 | c->c_errors++; |
| 3761 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3762 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3763 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3764 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3765 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3766 | if (closure) |
| 3767 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3768 | else |
| 3769 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3770 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3771 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3772 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3773 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3778 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3779 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3780 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3781 | REQ(n, testlist); |
| 3782 | /* testlist: test (',' test)* [','] */ |
| 3783 | for (i = 0; i < NCH(n); i += 2) |
| 3784 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3785 | i = (NCH(n)+1) / 2; |
| 3786 | com_addoparg(c, BUILD_TUPLE, i); |
| 3787 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3791 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3792 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3793 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3794 | PyObject *v; |
| 3795 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3796 | char *name; |
| 3797 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3798 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3799 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3800 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3801 | c->c_errors++; |
| 3802 | return; |
| 3803 | } |
| 3804 | /* Push the class name on the stack */ |
| 3805 | i = com_addconst(c, v); |
| 3806 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3807 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3808 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3809 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3810 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3811 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3812 | com_push(c, 1); |
| 3813 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3814 | else |
| 3815 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3816 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3817 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3818 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3819 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3820 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3821 | c->c_errors++; |
| 3822 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3823 | int closure = com_make_closure(c, co); |
| 3824 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3825 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3826 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3827 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3828 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3829 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3830 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3831 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3832 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3833 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3834 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3835 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3836 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3837 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3838 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3839 | } |
| 3840 | |
| 3841 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3842 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3843 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3844 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3845 | if (c->c_errors) |
| 3846 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3847 | switch (TYPE(n)) { |
| 3848 | |
| 3849 | /* Definition nodes */ |
| 3850 | |
| 3851 | case funcdef: |
| 3852 | com_funcdef(c, n); |
| 3853 | break; |
| 3854 | case classdef: |
| 3855 | com_classdef(c, n); |
| 3856 | break; |
| 3857 | |
| 3858 | /* Trivial parse tree nodes */ |
| 3859 | |
| 3860 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3861 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3862 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3863 | n = CHILD(n, 0); |
| 3864 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3865 | |
| 3866 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3867 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3868 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3869 | { |
| 3870 | int i; |
| 3871 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3872 | com_node(c, CHILD(n, i)); |
| 3873 | } |
| 3874 | break; |
| 3875 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3876 | case compound_stmt: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3877 | com_set_lineno(c, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3878 | n = CHILD(n, 0); |
| 3879 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3880 | |
| 3881 | /* Statement nodes */ |
| 3882 | |
| 3883 | case expr_stmt: |
| 3884 | com_expr_stmt(c, n); |
| 3885 | break; |
| 3886 | case print_stmt: |
| 3887 | com_print_stmt(c, n); |
| 3888 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3889 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3890 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3891 | break; |
| 3892 | case pass_stmt: |
| 3893 | break; |
| 3894 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3895 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3896 | com_error(c, PyExc_SyntaxError, |
| 3897 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3898 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3899 | com_addbyte(c, BREAK_LOOP); |
| 3900 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3901 | case continue_stmt: |
| 3902 | com_continue_stmt(c, n); |
| 3903 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3904 | case return_stmt: |
| 3905 | com_return_stmt(c, n); |
| 3906 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3907 | case yield_stmt: |
| 3908 | com_yield_stmt(c, n); |
| 3909 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3910 | case raise_stmt: |
| 3911 | com_raise_stmt(c, n); |
| 3912 | break; |
| 3913 | case import_stmt: |
| 3914 | com_import_stmt(c, n); |
| 3915 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3916 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3917 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3918 | case exec_stmt: |
| 3919 | com_exec_stmt(c, n); |
| 3920 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3921 | case assert_stmt: |
| 3922 | com_assert_stmt(c, n); |
| 3923 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3924 | case if_stmt: |
| 3925 | com_if_stmt(c, n); |
| 3926 | break; |
| 3927 | case while_stmt: |
| 3928 | com_while_stmt(c, n); |
| 3929 | break; |
| 3930 | case for_stmt: |
| 3931 | com_for_stmt(c, n); |
| 3932 | break; |
| 3933 | case try_stmt: |
| 3934 | com_try_stmt(c, n); |
| 3935 | break; |
| 3936 | case suite: |
| 3937 | com_suite(c, n); |
| 3938 | break; |
| 3939 | |
| 3940 | /* Expression nodes */ |
| 3941 | |
| 3942 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3943 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3944 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3945 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3946 | break; |
| 3947 | case test: |
| 3948 | com_test(c, n); |
| 3949 | break; |
| 3950 | case and_test: |
| 3951 | com_and_test(c, n); |
| 3952 | break; |
| 3953 | case not_test: |
| 3954 | com_not_test(c, n); |
| 3955 | break; |
| 3956 | case comparison: |
| 3957 | com_comparison(c, n); |
| 3958 | break; |
| 3959 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3960 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3961 | break; |
| 3962 | case expr: |
| 3963 | com_expr(c, n); |
| 3964 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3965 | case xor_expr: |
| 3966 | com_xor_expr(c, n); |
| 3967 | break; |
| 3968 | case and_expr: |
| 3969 | com_and_expr(c, n); |
| 3970 | break; |
| 3971 | case shift_expr: |
| 3972 | com_shift_expr(c, n); |
| 3973 | break; |
| 3974 | case arith_expr: |
| 3975 | com_arith_expr(c, n); |
| 3976 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3977 | case term: |
| 3978 | com_term(c, n); |
| 3979 | break; |
| 3980 | case factor: |
| 3981 | com_factor(c, n); |
| 3982 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3983 | case power: |
| 3984 | com_power(c, n); |
| 3985 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3986 | case atom: |
| 3987 | com_atom(c, n); |
| 3988 | break; |
| 3989 | |
| 3990 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3991 | com_error(c, PyExc_SystemError, |
| 3992 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3993 | } |
| 3994 | } |
| 3995 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3996 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3997 | |
| 3998 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3999 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4000 | { |
| 4001 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4002 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 4003 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4004 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4005 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4006 | com_pop(c, 1); |
| 4007 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4011 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4012 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 4013 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4014 | if (NCH(n) == 1) { |
| 4015 | com_fpdef(c, CHILD(n, 0)); |
| 4016 | } |
| 4017 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4018 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 4019 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4020 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4021 | for (i = 0; i < NCH(n); i += 2) |
| 4022 | com_fpdef(c, CHILD(n, i)); |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4027 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4028 | { |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4029 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4030 | int complex = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4031 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4032 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 4033 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4034 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4035 | nch = NCH(n); |
| 4036 | /* Enter all arguments in table of locals */ |
| 4037 | for (i = 0, narg = 0; i < nch; i++) { |
| 4038 | node *ch = CHILD(n, i); |
| 4039 | node *fp; |
| 4040 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4041 | break; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4042 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4043 | fp = CHILD(ch, 0); |
| 4044 | if (TYPE(fp) != NAME) { |
| 4045 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
| 4046 | complex = 1; |
Guido van Rossum | 9c8a086 | 2002-12-23 16:35:23 +0000 | [diff] [blame] | 4047 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4048 | narg++; |
| 4049 | /* all name updates handled by symtable */ |
| 4050 | if (++i >= nch) |
| 4051 | break; |
| 4052 | ch = CHILD(n, i); |
| 4053 | if (TYPE(ch) == EQUAL) |
| 4054 | i += 2; |
| 4055 | else |
| 4056 | REQ(ch, COMMA); |
| 4057 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4058 | if (complex) { |
| 4059 | /* Generate code for complex arguments only after |
| 4060 | having counted the simple arguments */ |
| 4061 | int ilocal = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4062 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4063 | node *ch = CHILD(n, i); |
| 4064 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 4065 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4066 | break; |
| 4067 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4068 | fp = CHILD(ch, 0); |
| 4069 | if (TYPE(fp) != NAME) { |
| 4070 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4071 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4072 | com_fpdef(c, ch); |
| 4073 | } |
| 4074 | ilocal++; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4075 | if (++i >= nch) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4076 | break; |
| 4077 | ch = CHILD(n, i); |
| 4078 | if (TYPE(ch) == EQUAL) |
| 4079 | i += 2; |
| 4080 | else |
| 4081 | REQ(ch, COMMA); |
| 4082 | } |
| 4083 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4087 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4088 | { |
| 4089 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4090 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4091 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4092 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4093 | if (doc != NULL) { |
| 4094 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4095 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4096 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4097 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4098 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4099 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4100 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4101 | for (i = 0; i < NCH(n); i++) { |
| 4102 | node *ch = CHILD(n, i); |
| 4103 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 4104 | com_node(c, ch); |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | /* Top-level compile-node interface */ |
| 4109 | |
| 4110 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4111 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4112 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4113 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4114 | node *ch; |
| 4115 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4116 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4117 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4118 | if (doc != NULL) { |
| 4119 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4120 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4121 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4122 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4123 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4124 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4125 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4126 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4127 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4128 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4129 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4130 | c->c_infunction = 0; |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4131 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4132 | com_push(c, 1); |
| 4133 | com_addbyte(c, RETURN_VALUE); |
| 4134 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4138 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4139 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4140 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4141 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4142 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4143 | |
| 4144 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4145 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4146 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4147 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4148 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4149 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4150 | else |
| 4151 | ch = CHILD(n, 2); |
| 4152 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4153 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4154 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4158 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4159 | { |
| 4160 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4161 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4162 | REQ(n, classdef); |
| 4163 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4164 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4165 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4166 | /* Initialize local __module__ from global __name__ */ |
| 4167 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4168 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4169 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4170 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4171 | if (doc != NULL) { |
| 4172 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4173 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4174 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4175 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4176 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4177 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4178 | } |
| 4179 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4180 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4181 | com_node(c, ch); |
| 4182 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4183 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4184 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4185 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4186 | } |
| 4187 | |
| 4188 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4189 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4190 | { |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4191 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4192 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4193 | switch (TYPE(n)) { |
| 4194 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4195 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4196 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4197 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4198 | n = CHILD(n, 0); |
| 4199 | if (TYPE(n) != NEWLINE) |
| 4200 | com_node(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4201 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4202 | com_push(c, 1); |
| 4203 | com_addbyte(c, RETURN_VALUE); |
| 4204 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4205 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4206 | break; |
| 4207 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4208 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4209 | com_file_input(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4210 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4211 | com_push(c, 1); |
| 4212 | com_addbyte(c, RETURN_VALUE); |
| 4213 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4214 | break; |
| 4215 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4216 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4217 | com_node(c, CHILD(n, 0)); |
| 4218 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4219 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4220 | break; |
| 4221 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4222 | case lambdef: /* anonymous function definition */ |
| 4223 | compile_lambdef(c, n); |
| 4224 | break; |
| 4225 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4226 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4227 | compile_funcdef(c, n); |
| 4228 | break; |
| 4229 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4230 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4231 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4232 | break; |
| 4233 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4234 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4235 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4236 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4240 | static PyObject * |
| 4241 | dict_keys_inorder(PyObject *dict, int offset) |
| 4242 | { |
| 4243 | PyObject *tuple, *k, *v; |
| 4244 | int i, pos = 0, size = PyDict_Size(dict); |
| 4245 | |
| 4246 | tuple = PyTuple_New(size); |
| 4247 | if (tuple == NULL) |
| 4248 | return NULL; |
| 4249 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4250 | i = PyInt_AS_LONG(v); |
| 4251 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4252 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4253 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4254 | } |
| 4255 | return tuple; |
| 4256 | } |
| 4257 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4258 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4259 | PyNode_Compile(node *n, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4260 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4261 | return PyNode_CompileFlags(n, filename, NULL); |
| 4262 | } |
| 4263 | |
| 4264 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4265 | PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4266 | { |
| 4267 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4270 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4271 | PyNode_CompileSymtable(node *n, const char *filename) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4272 | { |
| 4273 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4274 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4275 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4276 | ff = PyNode_Future(n, filename); |
| 4277 | if (ff == NULL) |
| 4278 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4279 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4280 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4281 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4282 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4283 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4284 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4285 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4286 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4287 | if (st->st_errors > 0) |
| 4288 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4289 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4290 | if (st->st_errors > 0) |
| 4291 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4292 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4293 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4294 | fail: |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4295 | PyObject_FREE((void *)ff); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4296 | st->st_future = NULL; |
| 4297 | PySymtable_Free(st); |
| 4298 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4301 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4302 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4303 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4304 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4307 | static PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4308 | jcompile(node *n, const char *filename, struct compiling *base, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4309 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4310 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4311 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4312 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4313 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4314 | return NULL; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4315 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { |
| 4316 | sc.c_encoding = "utf-8"; |
| 4317 | } else if (TYPE(n) == encoding_decl) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4318 | sc.c_encoding = STR(n); |
| 4319 | n = CHILD(n, 0); |
| 4320 | } else { |
| 4321 | sc.c_encoding = NULL; |
| 4322 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4323 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4324 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4325 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4326 | /* c_symtable still points to parent's symbols */ |
| 4327 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4328 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4329 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4330 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4331 | if (base->c_encoding != NULL) { |
| 4332 | assert(sc.c_encoding == NULL); |
| 4333 | sc.c_encoding = base->c_encoding; |
| 4334 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4335 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4336 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4337 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4338 | if (sc.c_future == NULL) { |
| 4339 | com_free(&sc); |
| 4340 | return NULL; |
| 4341 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4342 | if (flags) { |
| 4343 | int merged = sc.c_future->ff_features | |
| 4344 | flags->cf_flags; |
| 4345 | sc.c_future->ff_features = merged; |
| 4346 | flags->cf_flags = merged; |
| 4347 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4348 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4349 | com_free(&sc); |
| 4350 | return NULL; |
| 4351 | } |
| 4352 | } |
| 4353 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4354 | if (symtable_load_symbols(&sc) < 0) { |
| 4355 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4356 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4357 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4358 | compile_node(&sc, n); |
| 4359 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4360 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4361 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4362 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4363 | consts = PyList_AsTuple(sc.c_consts); |
| 4364 | names = PyList_AsTuple(sc.c_names); |
| 4365 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4366 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4367 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4368 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4369 | filename = PyString_InternFromString(sc.c_filename); |
| 4370 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4371 | if (!PyErr_Occurred()) |
| 4372 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4373 | sc.c_nlocals, |
| 4374 | sc.c_maxstacklevel, |
| 4375 | sc.c_flags, |
| 4376 | sc.c_code, |
| 4377 | consts, |
| 4378 | names, |
| 4379 | varnames, |
| 4380 | freevars, |
| 4381 | cellvars, |
| 4382 | filename, |
| 4383 | name, |
| 4384 | sc.c_firstlineno, |
| 4385 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4386 | Py_XDECREF(consts); |
| 4387 | Py_XDECREF(names); |
| 4388 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4389 | Py_XDECREF(freevars); |
| 4390 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4391 | Py_XDECREF(filename); |
| 4392 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4393 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4394 | else if (!PyErr_Occurred()) { |
| 4395 | /* This could happen if someone called PyErr_Clear() after an |
| 4396 | error was reported above. That's not supposed to happen, |
| 4397 | but I just plugged one case and I'm not sure there can't be |
| 4398 | others. In that case, raise SystemError so that at least |
| 4399 | it gets reported instead dumping core. */ |
| 4400 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4401 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4402 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4403 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4404 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4405 | sc.c_symtable = NULL; |
| 4406 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4407 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4408 | return co; |
| 4409 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4410 | |
| 4411 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4412 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4413 | { |
| 4414 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4415 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4416 | int line = co->co_firstlineno; |
| 4417 | int addr = 0; |
| 4418 | while (--size >= 0) { |
| 4419 | addr += *p++; |
| 4420 | if (addr > addrq) |
| 4421 | break; |
| 4422 | line += *p++; |
| 4423 | } |
| 4424 | return line; |
| 4425 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4426 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4427 | /* The test for LOCAL must come before the test for FREE in order to |
| 4428 | handle classes where name is both local and free. The local var is |
| 4429 | a method and the free var is a free var referenced within a method. |
| 4430 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4431 | |
| 4432 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4433 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4434 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4435 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4436 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4437 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4438 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4439 | return CELL; |
| 4440 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4441 | return LOCAL; |
| 4442 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4443 | return FREE; |
| 4444 | v = PyDict_GetItemString(c->c_globals, name); |
| 4445 | if (v) { |
| 4446 | if (v == Py_None) |
| 4447 | return GLOBAL_EXPLICIT; |
| 4448 | else { |
| 4449 | return GLOBAL_IMPLICIT; |
| 4450 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4451 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4452 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4453 | "unknown scope for %.100s in %.100s(%s) " |
| 4454 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4455 | name, c->c_name, |
| 4456 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4457 | c->c_filename, |
| 4458 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4459 | PyObject_REPR(c->c_locals), |
| 4460 | PyObject_REPR(c->c_globals) |
| 4461 | ); |
| 4462 | |
| 4463 | Py_FatalError(buf); |
| 4464 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4467 | /* Helper functions to issue warnings */ |
| 4468 | |
| 4469 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4470 | issue_warning(const char *msg, const char *filename, int lineno) |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4471 | { |
| 4472 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4473 | lineno, NULL, NULL) < 0) { |
| 4474 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4475 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4476 | PyErr_SyntaxLocation(filename, lineno); |
| 4477 | } |
| 4478 | return -1; |
| 4479 | } |
| 4480 | return 0; |
| 4481 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4482 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4483 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4484 | symtable_warn(struct symtable *st, char *msg) |
| 4485 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4486 | 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] | 4487 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4488 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4489 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4490 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4493 | /* Helper function for setting lineno and filename */ |
| 4494 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4495 | static int |
| 4496 | symtable_build(struct compiling *c, node *n) |
| 4497 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4498 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4499 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4500 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4501 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4502 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4503 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4504 | return -1; |
| 4505 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4506 | if (c->c_symtable->st_errors > 0) |
| 4507 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4508 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4509 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4510 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4511 | return 0; |
| 4512 | } |
| 4513 | |
| 4514 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4515 | symtable_init_compiling_symbols(struct compiling *c) |
| 4516 | { |
| 4517 | PyObject *varnames; |
| 4518 | |
| 4519 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4520 | if (varnames == NULL) { |
| 4521 | varnames = PyList_New(0); |
| 4522 | if (varnames == NULL) |
| 4523 | return -1; |
| 4524 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4525 | Py_INCREF(varnames); |
| 4526 | } else |
| 4527 | Py_INCREF(varnames); |
| 4528 | c->c_varnames = varnames; |
| 4529 | |
| 4530 | c->c_globals = PyDict_New(); |
| 4531 | if (c->c_globals == NULL) |
| 4532 | return -1; |
| 4533 | c->c_freevars = PyDict_New(); |
| 4534 | if (c->c_freevars == NULL) |
| 4535 | return -1; |
| 4536 | c->c_cellvars = PyDict_New(); |
| 4537 | if (c->c_cellvars == NULL) |
| 4538 | return -1; |
| 4539 | return 0; |
| 4540 | } |
| 4541 | |
| 4542 | struct symbol_info { |
| 4543 | int si_nlocals; |
| 4544 | int si_ncells; |
| 4545 | int si_nfrees; |
| 4546 | int si_nimplicit; |
| 4547 | }; |
| 4548 | |
| 4549 | static void |
| 4550 | symtable_init_info(struct symbol_info *si) |
| 4551 | { |
| 4552 | si->si_nlocals = 0; |
| 4553 | si->si_ncells = 0; |
| 4554 | si->si_nfrees = 0; |
| 4555 | si->si_nimplicit = 0; |
| 4556 | } |
| 4557 | |
| 4558 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4559 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4560 | struct symbol_info *si) |
| 4561 | { |
| 4562 | PyObject *dict, *v; |
| 4563 | |
| 4564 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4565 | it indicates a local that we must allocate storage for (a |
| 4566 | cell var). If it occurs in a class, then the class has a |
| 4567 | method and a free variable with the same name. |
| 4568 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4569 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4570 | /* If it isn't declared locally, it can't be a cell. */ |
| 4571 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4572 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4573 | v = PyInt_FromLong(si->si_ncells++); |
| 4574 | dict = c->c_cellvars; |
| 4575 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4576 | /* If it is free anyway, then there is no need to do |
| 4577 | anything here. |
| 4578 | */ |
| 4579 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4580 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4581 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4582 | v = PyInt_FromLong(si->si_nfrees++); |
| 4583 | dict = c->c_freevars; |
| 4584 | } |
| 4585 | if (v == NULL) |
| 4586 | return -1; |
| 4587 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4588 | Py_DECREF(v); |
| 4589 | return -1; |
| 4590 | } |
| 4591 | Py_DECREF(v); |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4595 | /* If a variable is a cell and an argument, make sure that appears in |
| 4596 | co_cellvars before any variable to its right in varnames. |
| 4597 | */ |
| 4598 | |
| 4599 | |
| 4600 | static int |
| 4601 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4602 | PyObject *varnames, int flags) |
| 4603 | { |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4604 | PyObject *v = NULL; |
| 4605 | PyObject *w, *d, *list = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4606 | int i, pos; |
| 4607 | |
| 4608 | if (flags & CO_VARARGS) |
| 4609 | argcount++; |
| 4610 | if (flags & CO_VARKEYWORDS) |
| 4611 | argcount++; |
| 4612 | for (i = argcount; --i >= 0; ) { |
| 4613 | v = PyList_GET_ITEM(varnames, i); |
| 4614 | if (PyDict_GetItem(*cellvars, v)) { |
| 4615 | if (list == NULL) { |
| 4616 | list = PyList_New(1); |
| 4617 | if (list == NULL) |
| 4618 | return -1; |
| 4619 | PyList_SET_ITEM(list, 0, v); |
| 4620 | Py_INCREF(v); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4621 | } else { |
| 4622 | if (PyList_Insert(list, 0, v) < 0) { |
| 4623 | Py_DECREF(list); |
| 4624 | return -1; |
| 4625 | } |
| 4626 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4629 | if (list == NULL) /* There used to be a check here for the size of */ |
| 4630 | return 0; /* the list being 0, which would have leaked the */ |
| 4631 | /* list if that condition was ever possible. JRH */ |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4632 | /* There are cellvars that are also arguments. Create a dict |
| 4633 | to replace cellvars and put the args at the front. |
| 4634 | */ |
| 4635 | d = PyDict_New(); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4636 | if (d == NULL) |
| 4637 | return -1; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4638 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4639 | v = PyInt_FromLong(i); |
| 4640 | if (v == NULL) |
| 4641 | goto fail; |
| 4642 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4643 | goto fail; |
| 4644 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4645 | goto fail; |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4646 | Py_DECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4647 | } |
| 4648 | pos = 0; |
| 4649 | i = PyList_GET_SIZE(list); |
| 4650 | Py_DECREF(list); |
| 4651 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4652 | 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] | 4653 | if (w == NULL) |
| 4654 | goto fail; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4655 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4656 | Py_DECREF(w); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4657 | v = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4658 | goto fail; |
| 4659 | } |
| 4660 | Py_DECREF(w); |
| 4661 | } |
| 4662 | Py_DECREF(*cellvars); |
| 4663 | *cellvars = d; |
| 4664 | return 1; |
| 4665 | fail: |
| 4666 | Py_DECREF(d); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4667 | Py_XDECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4668 | return -1; |
| 4669 | } |
| 4670 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4671 | static int |
| 4672 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4673 | { |
| 4674 | PyObject *name, *v; |
| 4675 | int pos; |
| 4676 | |
| 4677 | /* The cell vars are the first elements of the closure, |
| 4678 | followed by the free vars. Update the offsets in |
| 4679 | c_freevars to account for number of cellvars. */ |
| 4680 | pos = 0; |
| 4681 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4682 | int i = PyInt_AS_LONG(v) + offset; |
| 4683 | PyObject *o = PyInt_FromLong(i); |
| 4684 | if (o == NULL) |
| 4685 | return -1; |
| 4686 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4687 | Py_DECREF(o); |
| 4688 | return -1; |
| 4689 | } |
| 4690 | Py_DECREF(o); |
| 4691 | } |
| 4692 | return 0; |
| 4693 | } |
| 4694 | |
| 4695 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4696 | symtable_check_unoptimized(struct compiling *c, |
| 4697 | PySymtableEntryObject *ste, |
| 4698 | struct symbol_info *si) |
| 4699 | { |
| 4700 | char buf[300]; |
| 4701 | |
| 4702 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4703 | || (ste->ste_nested && si->si_nimplicit))) |
| 4704 | return 0; |
| 4705 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4706 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4707 | |
| 4708 | #define ILLEGAL_IS "is a nested function" |
| 4709 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4710 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4711 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4712 | |
| 4713 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4714 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4715 | |
| 4716 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4717 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4718 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4719 | |
| 4720 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4721 | should be stored so the exception can point to them. */ |
| 4722 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4723 | if (ste->ste_child_free) { |
| 4724 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4725 | PyOS_snprintf(buf, sizeof(buf), |
| 4726 | ILLEGAL_IMPORT_STAR, |
| 4727 | PyString_AS_STRING(ste->ste_name), |
| 4728 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4729 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4730 | PyOS_snprintf(buf, sizeof(buf), |
| 4731 | ILLEGAL_BARE_EXEC, |
| 4732 | PyString_AS_STRING(ste->ste_name), |
| 4733 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4734 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4735 | PyOS_snprintf(buf, sizeof(buf), |
| 4736 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4737 | PyString_AS_STRING(ste->ste_name), |
| 4738 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4739 | } |
| 4740 | } else { |
| 4741 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4742 | PyOS_snprintf(buf, sizeof(buf), |
| 4743 | ILLEGAL_IMPORT_STAR, |
| 4744 | PyString_AS_STRING(ste->ste_name), |
| 4745 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4746 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4747 | PyOS_snprintf(buf, sizeof(buf), |
| 4748 | ILLEGAL_BARE_EXEC, |
| 4749 | PyString_AS_STRING(ste->ste_name), |
| 4750 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4751 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4752 | PyOS_snprintf(buf, sizeof(buf), |
| 4753 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4754 | PyString_AS_STRING(ste->ste_name), |
| 4755 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4756 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4757 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4758 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4759 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4760 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4761 | ste->ste_opt_lineno); |
| 4762 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4766 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4767 | struct symbol_info *si) |
| 4768 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4769 | if (c->c_future) |
| 4770 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4771 | if (ste->ste_generator) |
| 4772 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4773 | if (ste->ste_type != TYPE_MODULE) |
| 4774 | c->c_flags |= CO_NEWLOCALS; |
| 4775 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4776 | c->c_nlocals = si->si_nlocals; |
| 4777 | if (ste->ste_optimized == 0) |
| 4778 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4779 | else if (ste->ste_optimized != OPT_EXEC) |
| 4780 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4781 | } |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
| 4785 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4786 | symtable_load_symbols(struct compiling *c) |
| 4787 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4788 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4789 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4790 | PyObject *name, *varnames, *v; |
| 4791 | int i, flags, pos; |
| 4792 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4793 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4794 | v = NULL; |
| 4795 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4796 | if (symtable_init_compiling_symbols(c) < 0) |
| 4797 | goto fail; |
| 4798 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4799 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4800 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4801 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4802 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4803 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4804 | v = PyInt_FromLong(i); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4805 | if (v == NULL) |
| 4806 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4807 | if (PyDict_SetItem(c->c_locals, |
| 4808 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4809 | goto fail; |
| 4810 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | /* XXX The cases below define the rules for whether a name is |
| 4814 | local or global. The logic could probably be clearer. */ |
| 4815 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4816 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4817 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4818 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4819 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4820 | /* undo the original DEF_FREE */ |
| 4821 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4822 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4823 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4824 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4825 | 2. Free variables in methods that are also class |
| 4826 | variables or declared global. |
| 4827 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4828 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4829 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4830 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4831 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4832 | c->c_argcount--; |
| 4833 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4834 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4835 | c->c_argcount--; |
| 4836 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4837 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4838 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4839 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4840 | if (flags & DEF_PARAM) { |
| 4841 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4842 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4843 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4844 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4845 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4846 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4847 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4848 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4849 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4850 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4851 | si.si_nimplicit++; |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4852 | if (PyDict_SetItem(c->c_globals, name, Py_True) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4853 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4854 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4855 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4856 | if (v == NULL) |
| 4857 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4858 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4859 | goto fail; |
| 4860 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4861 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4862 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4863 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4864 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4865 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4866 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4867 | if (v == NULL) |
| 4868 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4869 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4870 | goto fail; |
| 4871 | Py_DECREF(v); |
| 4872 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4873 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4874 | if (PyDict_SetItem(c->c_globals, name, |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4875 | Py_True) < 0) |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4876 | goto fail; |
| 4877 | if (st->st_nscopes != 1) { |
| 4878 | v = PyInt_FromLong(flags); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4879 | if (v == NULL) |
| 4880 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4881 | if (PyDict_SetItem(st->st_global, |
| 4882 | name, v)) |
| 4883 | goto fail; |
| 4884 | Py_DECREF(v); |
| 4885 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4886 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4887 | } |
| 4888 | } |
| 4889 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4890 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4891 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4892 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4893 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4894 | c->c_varnames, c->c_flags) < 0) |
| 4895 | return -1; |
| 4896 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4897 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4898 | return -1; |
| 4899 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4900 | fail: |
| 4901 | /* is this always the right thing to do? */ |
| 4902 | Py_XDECREF(v); |
| 4903 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4907 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4908 | { |
| 4909 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4910 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4911 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4912 | if (st == NULL) |
| 4913 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4914 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4915 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4916 | st->st_filename = NULL; |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4917 | st->st_symbols = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4918 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4919 | goto fail; |
| 4920 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4921 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4922 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4923 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4924 | st->st_errors = 0; |
| 4925 | st->st_tmpname = 0; |
| 4926 | st->st_private = NULL; |
| 4927 | return st; |
| 4928 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4929 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4930 | return NULL; |
| 4931 | } |
| 4932 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4933 | void |
| 4934 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4935 | { |
| 4936 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4937 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4938 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4939 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4942 | /* When the compiler exits a scope, it must should update the scope's |
| 4943 | free variable information with the list of free variables in its |
| 4944 | children. |
| 4945 | |
| 4946 | Variables that are free in children and defined in the current |
| 4947 | scope are cellvars. |
| 4948 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4949 | 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] | 4950 | false), free variables in children that are not defined here are |
| 4951 | implicit globals. |
| 4952 | |
| 4953 | */ |
| 4954 | |
| 4955 | static int |
| 4956 | symtable_update_free_vars(struct symtable *st) |
| 4957 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4958 | int i, j, def; |
| 4959 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4960 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4961 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4962 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4963 | def = DEF_FREE_CLASS; |
| 4964 | else |
| 4965 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4966 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4967 | int pos = 0; |
| 4968 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4969 | if (list) |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4970 | if (PyList_SetSlice(list, 0, |
| 4971 | ((PyVarObject*)list)->ob_size, 0) < 0) |
| 4972 | return -1; |
| 4973 | /* Yes, the above call CAN fail, even though it's reducing |
| 4974 | the size of the list. The current implementation will |
| 4975 | allocate temp memory equal to the size of the list: this |
| 4976 | is avoidable in this specific case, but probably not |
| 4977 | worth the effort of special-casing it. - JRH */ |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4978 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4979 | PyList_GET_ITEM(ste->ste_children, i); |
| 4980 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4981 | int flags = PyInt_AS_LONG(o); |
| 4982 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4983 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4984 | if (list == NULL) { |
| 4985 | list = PyList_New(0); |
| 4986 | if (list == NULL) |
| 4987 | return -1; |
| 4988 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4989 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4990 | if (PyList_Append(list, name) < 0) { |
| 4991 | Py_DECREF(list); |
| 4992 | return -1; |
| 4993 | } |
| 4994 | } |
| 4995 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4996 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4997 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4998 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4999 | /* If a name N is declared global in scope A and |
| 5000 | referenced in scope B contained (perhaps |
| 5001 | indirectly) in A and there are no scopes |
| 5002 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 5003 | is global in B. Unless A is a class scope, |
| 5004 | because class scopes are not considered for |
| 5005 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5006 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 5007 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5008 | int flags = PyInt_AS_LONG(v); |
| 5009 | if (flags & DEF_GLOBAL) { |
| 5010 | symtable_undo_free(st, child->ste_id, |
| 5011 | name); |
| 5012 | continue; |
| 5013 | } |
| 5014 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5015 | if (ste->ste_nested) { |
| 5016 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5017 | name, def) < 0) { |
| 5018 | Py_DECREF(list); |
| 5019 | return -1; |
| 5020 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5021 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5022 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5023 | name) < 0) { |
| 5024 | Py_DECREF(list); |
| 5025 | return -1; |
| 5026 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5027 | } |
| 5028 | } |
| 5029 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5030 | |
| 5031 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5032 | return 0; |
| 5033 | } |
| 5034 | |
| 5035 | /* If the current scope is a non-nested class or if name is not |
| 5036 | defined in the current, non-nested scope, then it is an implicit |
| 5037 | global in all nested scopes. |
| 5038 | */ |
| 5039 | |
| 5040 | static int |
| 5041 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 5042 | { |
| 5043 | PyObject *o; |
| 5044 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5045 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5046 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5047 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5048 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5049 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5050 | if (o == NULL) |
| 5051 | return symtable_undo_free(st, child, name); |
| 5052 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5053 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5054 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5055 | return symtable_undo_free(st, child, name); |
| 5056 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5057 | return symtable_add_def_o(st, ste->ste_symbols, |
| 5058 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | static int |
| 5062 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 5063 | PyObject *name) |
| 5064 | { |
| 5065 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5066 | PyObject *info; |
| 5067 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5068 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5069 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 5070 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5071 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5072 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5073 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5074 | if (info == NULL) |
| 5075 | return 0; |
| 5076 | v = PyInt_AS_LONG(info); |
| 5077 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5078 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5079 | DEF_FREE_GLOBAL) < 0) |
| 5080 | return -1; |
| 5081 | } else |
| 5082 | /* If the name is defined here or declared global, |
| 5083 | then the recursion stops. */ |
| 5084 | return 0; |
| 5085 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5086 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 5087 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5088 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5089 | PyList_GET_ITEM(ste->ste_children, i); |
| 5090 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5091 | if (x < 0) |
| 5092 | return x; |
| 5093 | } |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5097 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 5098 | This reference is released when the scope is exited, via the DECREF |
| 5099 | in symtable_exit_scope(). |
| 5100 | */ |
| 5101 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5102 | static int |
| 5103 | symtable_exit_scope(struct symtable *st) |
| 5104 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5105 | int end; |
| 5106 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5107 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5108 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5109 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5110 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5111 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 5112 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5113 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 5114 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5115 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5116 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5117 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5118 | static void |
| 5119 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 5120 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5121 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5122 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5123 | |
| 5124 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5125 | prev = st->st_cur; |
| 5126 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5127 | /* Py_DECREF(st->st_cur); */ |
| 5128 | /* I believe the previous line would lead to a |
| 5129 | double-DECREF when st is disposed - JRH */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5130 | st->st_errors++; |
| 5131 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5132 | } |
| 5133 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5134 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5135 | PySymtableEntry_New(st, name, type, lineno); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5136 | if (st->st_cur == NULL) { |
| 5137 | st->st_errors++; |
| 5138 | return; |
| 5139 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5140 | if (strcmp(name, TOP) == 0) |
| 5141 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5142 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5143 | if (PyList_Append(prev->ste_children, |
| 5144 | (PyObject *)st->st_cur) < 0) |
| 5145 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5146 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5150 | symtable_lookup(struct symtable *st, char *name) |
| 5151 | { |
| 5152 | char buffer[MANGLE_LEN]; |
| 5153 | PyObject *v; |
| 5154 | int flags; |
| 5155 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5156 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5157 | name = buffer; |
| 5158 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5159 | if (v == NULL) { |
| 5160 | if (PyErr_Occurred()) |
| 5161 | return -1; |
| 5162 | else |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | flags = PyInt_AS_LONG(v); |
| 5167 | return flags; |
| 5168 | } |
| 5169 | |
| 5170 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5171 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5172 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5173 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5174 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5175 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5176 | |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5177 | /* Warn about None, except inside a tuple (where the assignment |
| 5178 | code already issues a warning). */ |
| 5179 | if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) && |
| 5180 | *name == 'N' && strcmp(name, "None") == 0) |
| 5181 | { |
| 5182 | if (symtable_warn(st, "argument named None")) |
| 5183 | return -1; |
| 5184 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5185 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5186 | name = buffer; |
| 5187 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5188 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5189 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5190 | Py_DECREF(s); |
| 5191 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | /* Must only be called with mangled names */ |
| 5195 | |
| 5196 | static int |
| 5197 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5198 | PyObject *name, int flag) |
| 5199 | { |
| 5200 | PyObject *o; |
| 5201 | int val; |
| 5202 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5203 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5204 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5205 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5206 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5207 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5208 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5209 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5210 | return -1; |
| 5211 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5212 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5213 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5214 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5215 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5216 | if (o == NULL) |
| 5217 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5218 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5219 | Py_DECREF(o); |
| 5220 | return -1; |
| 5221 | } |
| 5222 | Py_DECREF(o); |
| 5223 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5224 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5225 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5226 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5227 | } else if (flag & DEF_GLOBAL) { |
| 5228 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5229 | perhaps only DEF_FREE_GLOBAL */ |
| 5230 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5231 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5232 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5233 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5234 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5235 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5236 | if (o == NULL) |
| 5237 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5238 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5239 | Py_DECREF(o); |
| 5240 | return -1; |
| 5241 | } |
| 5242 | Py_DECREF(o); |
| 5243 | } |
| 5244 | return 0; |
| 5245 | } |
| 5246 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5247 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5248 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5249 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5250 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5251 | ignored) in case those are the only places a yield occurs (so that this |
| 5252 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5253 | static int |
| 5254 | look_for_yield(node *n) |
| 5255 | { |
| 5256 | int i; |
| 5257 | |
| 5258 | for (i = 0; i < NCH(n); ++i) { |
| 5259 | node *kid = CHILD(n, i); |
| 5260 | |
| 5261 | switch (TYPE(kid)) { |
| 5262 | |
| 5263 | case classdef: |
| 5264 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5265 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5266 | /* Stuff in nested functions and classes can't make |
| 5267 | the parent a generator. */ |
| 5268 | return 0; |
| 5269 | |
| 5270 | case yield_stmt: |
| 5271 | return 1; |
| 5272 | |
| 5273 | default: |
| 5274 | if (look_for_yield(kid)) |
| 5275 | return 1; |
| 5276 | } |
| 5277 | } |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5281 | static void |
| 5282 | symtable_node(struct symtable *st, node *n) |
| 5283 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5284 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5285 | |
| 5286 | loop: |
| 5287 | switch (TYPE(n)) { |
| 5288 | case funcdef: { |
| 5289 | char *func_name = STR(CHILD(n, 1)); |
| 5290 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5291 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5292 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5293 | symtable_funcdef(st, n); |
| 5294 | symtable_exit_scope(st); |
| 5295 | break; |
| 5296 | } |
| 5297 | case lambdef: |
| 5298 | if (NCH(n) == 4) |
| 5299 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5300 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5301 | symtable_funcdef(st, n); |
| 5302 | symtable_exit_scope(st); |
| 5303 | break; |
| 5304 | case classdef: { |
| 5305 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5306 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5307 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5308 | node *bases = CHILD(n, 3); |
| 5309 | int i; |
| 5310 | for (i = 0; i < NCH(bases); i += 2) { |
| 5311 | symtable_node(st, CHILD(bases, i)); |
| 5312 | } |
| 5313 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5314 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5315 | tmp = st->st_private; |
| 5316 | st->st_private = class_name; |
| 5317 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5318 | st->st_private = tmp; |
| 5319 | symtable_exit_scope(st); |
| 5320 | break; |
| 5321 | } |
| 5322 | case if_stmt: |
| 5323 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5324 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5325 | if (st->st_cur->ste_generator == 0) |
| 5326 | st->st_cur->ste_generator = |
| 5327 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5328 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5329 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5330 | symtable_node(st, CHILD(n, i + 1)); |
| 5331 | symtable_node(st, CHILD(n, i + 3)); |
| 5332 | } |
| 5333 | if (i + 2 < NCH(n)) |
| 5334 | symtable_node(st, CHILD(n, i + 2)); |
| 5335 | break; |
| 5336 | case global_stmt: |
| 5337 | symtable_global(st, n); |
| 5338 | break; |
| 5339 | case import_stmt: |
| 5340 | symtable_import(st, n); |
| 5341 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5342 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5343 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5344 | symtable_node(st, CHILD(n, 1)); |
| 5345 | if (NCH(n) > 2) |
| 5346 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5347 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5348 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5349 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5350 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5351 | if (NCH(n) > 4) |
| 5352 | symtable_node(st, CHILD(n, 5)); |
| 5353 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5354 | |
| 5355 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5356 | case assert_stmt: |
| 5357 | if (Py_OptimizeFlag) |
| 5358 | return; |
| 5359 | if (NCH(n) == 2) { |
| 5360 | n = CHILD(n, 1); |
| 5361 | goto loop; |
| 5362 | } else { |
| 5363 | symtable_node(st, CHILD(n, 1)); |
| 5364 | n = CHILD(n, 3); |
| 5365 | goto loop; |
| 5366 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5367 | case except_clause: |
| 5368 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5369 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5370 | if (NCH(n) > 1) { |
| 5371 | n = CHILD(n, 1); |
| 5372 | goto loop; |
| 5373 | } |
| 5374 | break; |
| 5375 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5376 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5377 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5378 | case yield_stmt: |
| 5379 | st->st_cur->ste_generator = 1; |
| 5380 | n = CHILD(n, 1); |
| 5381 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5382 | case expr_stmt: |
| 5383 | if (NCH(n) == 1) |
| 5384 | n = CHILD(n, 0); |
| 5385 | else { |
| 5386 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5387 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5388 | symtable_node(st, CHILD(n, 2)); |
| 5389 | break; |
| 5390 | } else { |
| 5391 | int i; |
| 5392 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5393 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5394 | n = CHILD(n, NCH(n) - 1); |
| 5395 | } |
| 5396 | } |
| 5397 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5398 | case list_iter: |
| 5399 | n = CHILD(n, 0); |
| 5400 | if (TYPE(n) == list_for) { |
| 5401 | st->st_tmpname++; |
| 5402 | symtable_list_comprehension(st, n); |
| 5403 | st->st_tmpname--; |
| 5404 | } else { |
| 5405 | REQ(n, list_if); |
| 5406 | symtable_node(st, CHILD(n, 1)); |
| 5407 | if (NCH(n) == 3) { |
| 5408 | n = CHILD(n, 2); |
| 5409 | goto loop; |
| 5410 | } |
| 5411 | } |
| 5412 | break; |
| 5413 | case for_stmt: |
| 5414 | symtable_assign(st, CHILD(n, 1), 0); |
| 5415 | for (i = 3; i < NCH(n); ++i) |
| 5416 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5417 | symtable_node(st, CHILD(n, i)); |
| 5418 | break; |
| 5419 | /* The remaining cases fall through to default except in |
| 5420 | special circumstances. This requires the individual cases |
| 5421 | to be coded with great care, even though they look like |
| 5422 | rather innocuous. Each case must double-check TYPE(n). |
| 5423 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5424 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5425 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5426 | n = CHILD(n, 2); |
| 5427 | goto loop; |
| 5428 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5429 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5430 | case listmaker: |
| 5431 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5432 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5433 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5434 | symtable_node(st, CHILD(n, 0)); |
| 5435 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5436 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5437 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5438 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5439 | case atom: |
| 5440 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5441 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5442 | break; |
| 5443 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5444 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5445 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5446 | /* Walk over every non-token child with a special case |
| 5447 | for one child. |
| 5448 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5449 | if (NCH(n) == 1) { |
| 5450 | n = CHILD(n, 0); |
| 5451 | goto loop; |
| 5452 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5453 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5454 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5455 | symtable_node(st, CHILD(n, i)); |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | static void |
| 5460 | symtable_funcdef(struct symtable *st, node *n) |
| 5461 | { |
| 5462 | node *body; |
| 5463 | |
| 5464 | if (TYPE(n) == lambdef) { |
| 5465 | if (NCH(n) == 4) |
| 5466 | symtable_params(st, CHILD(n, 1)); |
| 5467 | } else |
| 5468 | symtable_params(st, CHILD(n, 2)); |
| 5469 | body = CHILD(n, NCH(n) - 1); |
| 5470 | symtable_node(st, body); |
| 5471 | } |
| 5472 | |
| 5473 | /* The next two functions parse the argument tuple. |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5474 | symtable_default_args() checks for names in the default arguments, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5475 | which are references in the defining scope. symtable_params() |
| 5476 | parses the parameter names, which are defined in the function's |
| 5477 | body. |
| 5478 | |
| 5479 | varargslist: |
| 5480 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5481 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5482 | */ |
| 5483 | |
| 5484 | static void |
| 5485 | symtable_default_args(struct symtable *st, node *n) |
| 5486 | { |
| 5487 | node *c; |
| 5488 | int i; |
| 5489 | |
| 5490 | if (TYPE(n) == parameters) { |
| 5491 | n = CHILD(n, 1); |
| 5492 | if (TYPE(n) == RPAR) |
| 5493 | return; |
| 5494 | } |
| 5495 | REQ(n, varargslist); |
| 5496 | for (i = 0; i < NCH(n); i += 2) { |
| 5497 | c = CHILD(n, i); |
| 5498 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5499 | break; |
| 5500 | } |
| 5501 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5502 | symtable_node(st, CHILD(n, i)); |
| 5503 | } |
| 5504 | } |
| 5505 | |
| 5506 | static void |
| 5507 | symtable_params(struct symtable *st, node *n) |
| 5508 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5509 | int i, complex = -1, ext = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5510 | node *c = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5511 | |
| 5512 | if (TYPE(n) == parameters) { |
| 5513 | n = CHILD(n, 1); |
| 5514 | if (TYPE(n) == RPAR) |
| 5515 | return; |
| 5516 | } |
| 5517 | REQ(n, varargslist); |
| 5518 | for (i = 0; i < NCH(n); i += 2) { |
| 5519 | c = CHILD(n, i); |
| 5520 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5521 | ext = 1; |
| 5522 | break; |
| 5523 | } |
| 5524 | if (TYPE(c) == test) { |
| 5525 | continue; |
| 5526 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5527 | if (TYPE(CHILD(c, 0)) == NAME) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5528 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5529 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5530 | char nbuf[30]; |
| 5531 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5532 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5533 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5534 | } |
| 5535 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5536 | if (ext) { |
| 5537 | c = CHILD(n, i); |
| 5538 | if (TYPE(c) == STAR) { |
| 5539 | i++; |
| 5540 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5541 | DEF_PARAM | DEF_STAR); |
| 5542 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5543 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5544 | c = NULL; |
| 5545 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5546 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5547 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5548 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5549 | i++; |
| 5550 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5551 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5552 | } |
| 5553 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5554 | if (complex >= 0) { |
| 5555 | int j; |
| 5556 | for (j = 0; j <= complex; j++) { |
| 5557 | c = CHILD(n, j); |
| 5558 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5559 | c = CHILD(n, ++j); |
| 5560 | else if (TYPE(c) == EQUAL) |
| 5561 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5562 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5563 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5564 | } |
| 5565 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | static void |
| 5569 | symtable_params_fplist(struct symtable *st, node *n) |
| 5570 | { |
| 5571 | int i; |
| 5572 | node *c; |
| 5573 | |
| 5574 | REQ(n, fplist); |
| 5575 | for (i = 0; i < NCH(n); i += 2) { |
| 5576 | c = CHILD(n, i); |
| 5577 | REQ(c, fpdef); |
| 5578 | if (NCH(c) == 1) |
| 5579 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5580 | DEF_PARAM | DEF_INTUPLE); |
| 5581 | else |
| 5582 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5583 | } |
| 5584 | |
| 5585 | } |
| 5586 | |
| 5587 | static void |
| 5588 | symtable_global(struct symtable *st, node *n) |
| 5589 | { |
| 5590 | int i; |
| 5591 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5592 | /* XXX It might be helpful to warn about module-level global |
| 5593 | statements, but it's hard to tell the difference between |
| 5594 | module-level and a string passed to exec. |
| 5595 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5596 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5597 | for (i = 1; i < NCH(n); i += 2) { |
| 5598 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5599 | int flags; |
| 5600 | |
| 5601 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5602 | if (flags < 0) |
| 5603 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5604 | if (flags && flags != DEF_GLOBAL) { |
| 5605 | char buf[500]; |
| 5606 | if (flags & DEF_PARAM) { |
| 5607 | PyErr_Format(PyExc_SyntaxError, |
| 5608 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5609 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5610 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5611 | st->st_cur->ste_lineno); |
| 5612 | st->st_errors++; |
| 5613 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5614 | } |
| 5615 | else { |
| 5616 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5617 | PyOS_snprintf(buf, sizeof(buf), |
| 5618 | GLOBAL_AFTER_ASSIGN, |
| 5619 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5620 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5621 | PyOS_snprintf(buf, sizeof(buf), |
| 5622 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5623 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5624 | } |
| 5625 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5626 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5627 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | static void |
| 5631 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5632 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5633 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5634 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5635 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5636 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5637 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5638 | symtable_node(st, CHILD(n, 3)); |
| 5639 | if (NCH(n) == 5) |
| 5640 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | static void |
| 5644 | symtable_import(struct symtable *st, node *n) |
| 5645 | { |
| 5646 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5647 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5648 | | 'from' dotted_name 'import' |
| 5649 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5650 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5651 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5652 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5653 | node *dotname = CHILD(n, 1); |
| 5654 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5655 | /* check for bogus imports */ |
| 5656 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5657 | PyErr_SetString(PyExc_SyntaxError, |
| 5658 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5659 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5660 | n->n_lineno); |
| 5661 | st->st_errors++; |
| 5662 | return; |
| 5663 | } |
| 5664 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5665 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5666 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5667 | if (symtable_warn(st, |
| 5668 | "import * only allowed at module level") < 0) |
| 5669 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5670 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5671 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5672 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5673 | } else { |
| 5674 | for (i = 3; i < NCH(n); i += 2) { |
| 5675 | node *c = CHILD(n, i); |
| 5676 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5677 | symtable_assign(st, CHILD(c, 2), |
| 5678 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5679 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5680 | symtable_assign(st, CHILD(c, 0), |
| 5681 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5682 | } |
| 5683 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5684 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5685 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5686 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5687 | } |
| 5688 | } |
| 5689 | } |
| 5690 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5691 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5692 | symtable_add_def() if it is eventually called. The flag is useful |
| 5693 | to specify the particular type of assignment that should be |
| 5694 | recorded, e.g. an assignment caused by import. |
| 5695 | */ |
| 5696 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5697 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5698 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5699 | { |
| 5700 | node *tmp; |
| 5701 | int i; |
| 5702 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5703 | loop: |
| 5704 | switch (TYPE(n)) { |
| 5705 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5706 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5707 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5708 | return; |
| 5709 | case power: |
| 5710 | if (NCH(n) > 2) { |
| 5711 | for (i = 2; i < NCH(n); ++i) |
| 5712 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5713 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5714 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5715 | if (NCH(n) > 1) { |
| 5716 | symtable_node(st, CHILD(n, 0)); |
| 5717 | symtable_node(st, CHILD(n, 1)); |
| 5718 | } else { |
| 5719 | n = CHILD(n, 0); |
| 5720 | goto loop; |
| 5721 | } |
| 5722 | return; |
| 5723 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5724 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5725 | /* XXX This is an error, but the next pass |
| 5726 | will catch it. */ |
| 5727 | return; |
| 5728 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5729 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5730 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5731 | } |
| 5732 | return; |
| 5733 | case exprlist: |
| 5734 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5735 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5736 | if (NCH(n) == 1) { |
| 5737 | n = CHILD(n, 0); |
| 5738 | goto loop; |
| 5739 | } |
| 5740 | else { |
| 5741 | int i; |
| 5742 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5743 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5744 | return; |
| 5745 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5746 | case atom: |
| 5747 | tmp = CHILD(n, 0); |
| 5748 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5749 | n = CHILD(n, 1); |
| 5750 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5751 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5752 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5753 | PyErr_SetString(PyExc_SyntaxError, |
| 5754 | ASSIGN_DEBUG); |
| 5755 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5756 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5757 | st->st_errors++; |
| 5758 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5759 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5760 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5761 | return; |
| 5762 | case dotted_as_name: |
| 5763 | if (NCH(n) == 3) |
| 5764 | symtable_add_def(st, STR(CHILD(n, 2)), |
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 | else |
| 5767 | symtable_add_def(st, |
| 5768 | STR(CHILD(CHILD(n, |
| 5769 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5770 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5771 | return; |
| 5772 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5773 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5774 | return; |
| 5775 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5776 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5777 | return; |
| 5778 | default: |
| 5779 | if (NCH(n) == 0) |
| 5780 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5781 | if (NCH(n) == 1) { |
| 5782 | n = CHILD(n, 0); |
| 5783 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5784 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5785 | /* Should only occur for errors like x + 1 = 1, |
| 5786 | which will be caught in the next pass. */ |
| 5787 | for (i = 0; i < NCH(n); ++i) |
| 5788 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5789 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5790 | } |
| 5791 | } |