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 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 326 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 327 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 328 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 329 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 330 | PyObject *filename, PyObject *name, int firstlineno, |
| 331 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 332 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 333 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 334 | int i; |
| 335 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 336 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 337 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 338 | consts == NULL || !PyTuple_Check(consts) || |
| 339 | names == NULL || !PyTuple_Check(names) || |
| 340 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 341 | freevars == NULL || !PyTuple_Check(freevars) || |
| 342 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 343 | name == NULL || !PyString_Check(name) || |
| 344 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 345 | lnotab == NULL || !PyString_Check(lnotab) || |
| 346 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 347 | PyErr_BadInternalCall(); |
| 348 | return NULL; |
| 349 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 350 | intern_strings(names); |
| 351 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 352 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 353 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 354 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 355 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 356 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 357 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 358 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 359 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 360 | continue; |
| 361 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 362 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 363 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 364 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 365 | co->co_argcount = argcount; |
| 366 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 367 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 368 | co->co_flags = flags; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 369 | Py_INCREF(code); |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 370 | co->co_code = code; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 371 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 372 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 373 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 374 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 375 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 376 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 377 | Py_INCREF(freevars); |
| 378 | co->co_freevars = freevars; |
| 379 | Py_INCREF(cellvars); |
| 380 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 382 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 383 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 384 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 385 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 386 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 387 | co->co_lnotab = lnotab; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 388 | } |
| 389 | return co; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 394 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 395 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 396 | builds the symbol table. The second pass generates the bytecode. |
| 397 | |
| 398 | The first pass uses a single symtable struct. The second pass uses |
| 399 | a compiling struct for each code block. The compiling structs |
| 400 | share a reference to the symtable. |
| 401 | |
| 402 | The two passes communicate via symtable_load_symbols() and via |
| 403 | is_local() and is_global(). The former initializes several slots |
| 404 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 405 | c_argcount, c_globals, and c_flags. |
| 406 | */ |
| 407 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 408 | /* All about c_lnotab. |
| 409 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 410 | c_lnotab is an array of unsigned bytes disguised as a Python string. Since |
| 411 | version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are |
| 412 | mapped to source code line #s via c_lnotab instead. |
| 413 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 414 | The array is conceptually a list of |
| 415 | (bytecode offset increment, line number increment) |
| 416 | pairs. The details are important and delicate, best illustrated by example: |
| 417 | |
| 418 | byte code offset source code line number |
| 419 | 0 1 |
| 420 | 6 2 |
| 421 | 50 7 |
| 422 | 350 307 |
| 423 | 361 308 |
| 424 | |
| 425 | The first trick is that these numbers aren't stored, only the increments |
| 426 | from one row to the next (this doesn't really work, but it's a start): |
| 427 | |
| 428 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 429 | |
| 430 | The second trick is that an unsigned byte can't hold negative values, or |
| 431 | values larger than 255, so (a) there's a deep assumption that byte code |
| 432 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 433 | if at least one column jumps by more than 255 from one row to the next, more |
| 434 | than one pair is written to the table. In case #b, there's no way to know |
| 435 | from looking at the table later how many were written. That's the delicate |
| 436 | part. A user of c_lnotab desiring to find the source line number |
| 437 | corresponding to a bytecode address A should do something like this |
| 438 | |
| 439 | lineno = addr = 0 |
| 440 | for addr_incr, line_incr in c_lnotab: |
| 441 | addr += addr_incr |
| 442 | if addr > A: |
| 443 | return lineno |
| 444 | lineno += line_incr |
| 445 | |
| 446 | In order for this to work, when the addr field increments by more than 255, |
| 447 | the line # increment in each pair generated must be 0 until the remaining addr |
| 448 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 449 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 450 | 255, 0, 45, 255, 0, 45. |
| 451 | */ |
| 452 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 453 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 454 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 455 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 456 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 457 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 458 | PyObject *c_name_dict; /* inverse of c_names */ |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 459 | PyObject *c_globals; /* dictionary (value=None or True) */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 460 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 461 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 462 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 463 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 464 | int c_nlocals; /* index of next local */ |
| 465 | int c_argcount; /* number of top-level arguments */ |
| 466 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 467 | int c_nexti; /* index into c_code */ |
| 468 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 469 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 470 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 471 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 472 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 473 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 474 | int c_nblocks; /* current block stack level */ |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 475 | const char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 476 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 477 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 478 | int c_stacklevel; /* Current stack level */ |
| 479 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 480 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 481 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 482 | int c_last_addr, c_last_line, c_lnotab_next; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 483 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 484 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 485 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 486 | int c_closure; /* Is nested w/freevars? */ |
| 487 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 488 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 489 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 490 | }; |
| 491 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 492 | static int |
| 493 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 494 | { |
| 495 | if ((v & (USE | DEF_FREE)) |
| 496 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 497 | return 1; |
| 498 | if (v & DEF_FREE_CLASS) |
| 499 | return 1; |
| 500 | return 0; |
| 501 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 502 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 503 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 504 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 505 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 506 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 507 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 508 | if (c == NULL) { |
| 509 | /* Error occurred via symtable call to |
| 510 | is_constant_false */ |
| 511 | PyErr_SetString(exc, msg); |
| 512 | return; |
| 513 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 514 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 515 | if (c->c_lineno < 1 || c->c_interactive) { |
| 516 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 517 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 518 | return; |
| 519 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 520 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 521 | if (v == NULL) |
| 522 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 523 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 524 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 525 | if (line == NULL) { |
| 526 | Py_INCREF(Py_None); |
| 527 | line = Py_None; |
| 528 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 529 | if (exc == PyExc_SyntaxError) { |
| 530 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 531 | Py_None, line); |
| 532 | if (t == NULL) |
| 533 | goto exit; |
| 534 | w = Py_BuildValue("(OO)", v, t); |
| 535 | if (w == NULL) |
| 536 | goto exit; |
| 537 | PyErr_SetObject(exc, w); |
| 538 | } else { |
| 539 | /* Make sure additional exceptions are printed with |
| 540 | file and line, also. */ |
| 541 | PyErr_SetObject(exc, v); |
| 542 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 543 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 544 | exit: |
| 545 | Py_XDECREF(t); |
| 546 | Py_XDECREF(v); |
| 547 | Py_XDECREF(w); |
| 548 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 551 | /* Interface to the block stack */ |
| 552 | |
| 553 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 554 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 555 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 556 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 557 | com_error(c, PyExc_SystemError, |
| 558 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 559 | } |
| 560 | else { |
| 561 | c->c_block[c->c_nblocks++] = type; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 566 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 567 | { |
| 568 | if (c->c_nblocks > 0) |
| 569 | c->c_nblocks--; |
| 570 | 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] | 571 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 575 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 576 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 577 | static int issue_warning(const char *, const char *, int); |
| 578 | static int com_init(struct compiling *, const char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 579 | static void com_free(struct compiling *); |
| 580 | static void com_push(struct compiling *, int); |
| 581 | static void com_pop(struct compiling *, int); |
| 582 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 583 | static void com_node(struct compiling *, node *); |
| 584 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 585 | static void com_addbyte(struct compiling *, int); |
| 586 | static void com_addint(struct compiling *, int); |
| 587 | static void com_addoparg(struct compiling *, int, int); |
| 588 | static void com_addfwref(struct compiling *, int, int *); |
| 589 | static void com_backpatch(struct compiling *, int); |
| 590 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 591 | static int com_addconst(struct compiling *, PyObject *); |
| 592 | static int com_addname(struct compiling *, PyObject *); |
| 593 | static void com_addopname(struct compiling *, int, node *); |
| 594 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 595 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 596 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 597 | static void com_assign(struct compiling *, node *, int, node *); |
| 598 | static void com_assign_name(struct compiling *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 599 | static PyCodeObject *icompile(node *, struct compiling *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 600 | static PyCodeObject *jcompile(node *, const char *, struct compiling *, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 601 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 602 | static PyObject *parsestrplus(struct compiling*, node *); |
| 603 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 604 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 605 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 606 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 607 | |
| 608 | /* symtable operations */ |
| 609 | static int symtable_build(struct compiling *, node *); |
| 610 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 611 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 612 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 613 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 614 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 615 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 616 | |
| 617 | static void symtable_node(struct symtable *, node *); |
| 618 | static void symtable_funcdef(struct symtable *, node *); |
| 619 | static void symtable_default_args(struct symtable *, node *); |
| 620 | static void symtable_params(struct symtable *, node *); |
| 621 | static void symtable_params_fplist(struct symtable *, node *n); |
| 622 | static void symtable_global(struct symtable *, node *); |
| 623 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 624 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 625 | static void symtable_list_comprehension(struct symtable *, node *); |
| 626 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 627 | static int symtable_update_free_vars(struct symtable *); |
| 628 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 629 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 630 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 631 | /* helper */ |
| 632 | static void |
| 633 | do_pad(int pad) |
| 634 | { |
| 635 | int i; |
| 636 | for (i = 0; i < pad; ++i) |
| 637 | fprintf(stderr, " "); |
| 638 | } |
| 639 | |
| 640 | static void |
| 641 | dump(node *n, int pad, int depth) |
| 642 | { |
| 643 | int i; |
| 644 | if (depth == 0) |
| 645 | return; |
| 646 | do_pad(pad); |
| 647 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 648 | if (depth > 0) |
| 649 | depth--; |
| 650 | for (i = 0; i < NCH(n); ++i) |
| 651 | dump(CHILD(n, i), pad + 1, depth); |
| 652 | } |
| 653 | |
| 654 | #define DUMP(N) dump(N, 0, -1) |
| 655 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 656 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 657 | com_init(struct compiling *c, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 658 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 659 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 660 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 661 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 662 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 663 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 664 | goto fail; |
| 665 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 666 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 667 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 668 | goto fail; |
| 669 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 670 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 671 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 672 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 673 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 674 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 675 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 676 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 677 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 678 | c->c_freevars = NULL; |
| 679 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 680 | c->c_nlocals = 0; |
| 681 | c->c_argcount = 0; |
| 682 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 683 | c->c_nexti = 0; |
| 684 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 685 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 686 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 687 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 688 | c->c_begin = 0; |
| 689 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 690 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 691 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 692 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 693 | c->c_stacklevel = 0; |
| 694 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 695 | c->c_firstlineno = 0; |
| 696 | c->c_last_addr = 0; |
| 697 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 698 | c->c_lnotab_next = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 699 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 700 | c->c_nested = 0; |
| 701 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 702 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 703 | return 1; |
| 704 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 705 | fail: |
| 706 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 711 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 712 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 713 | Py_XDECREF(c->c_code); |
| 714 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 715 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 716 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 717 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 718 | Py_XDECREF(c->c_globals); |
| 719 | Py_XDECREF(c->c_locals); |
| 720 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 721 | Py_XDECREF(c->c_freevars); |
| 722 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 723 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 724 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 725 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 729 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 730 | { |
| 731 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 732 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 733 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 734 | /* |
| 735 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 736 | c->c_filename, c->c_name, c->c_lineno, |
| 737 | c->c_nexti, c->c_stacklevel, n); |
| 738 | */ |
| 739 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 743 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 744 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 745 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 746 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 747 | else |
| 748 | c->c_stacklevel -= n; |
| 749 | } |
| 750 | |
| 751 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 752 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 753 | { |
| 754 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 755 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 756 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 757 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 760 | static int |
| 761 | com_check_size(PyObject **s, int offset) |
| 762 | { |
| 763 | int len = PyString_GET_SIZE(*s); |
| 764 | if (offset >= len) |
| 765 | return _PyString_Resize(s, len * 2); |
| 766 | return 0; |
| 767 | } |
| 768 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 769 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 770 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 771 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 772 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 773 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 774 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 775 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 776 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 777 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 778 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 779 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 783 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 784 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 785 | com_addbyte(c, x & 0xff); |
| 786 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 790 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 791 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 792 | char *p; |
| 793 | if (c->c_lnotab == NULL) |
| 794 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 795 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 796 | c->c_errors++; |
| 797 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 798 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 799 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 800 | *p++ = addr; |
| 801 | *p++ = line; |
| 802 | c->c_lnotab_next += 2; |
| 803 | } |
| 804 | |
| 805 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 806 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 807 | { |
| 808 | c->c_lineno = lineno; |
| 809 | if (c->c_firstlineno == 0) { |
| 810 | c->c_firstlineno = c->c_last_line = lineno; |
| 811 | } |
| 812 | else { |
| 813 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 814 | int incr_line = lineno - c->c_last_line; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 815 | while (incr_addr > 255) { |
| 816 | com_add_lnotab(c, 255, 0); |
| 817 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 818 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 819 | while (incr_line > 255) { |
| 820 | com_add_lnotab(c, incr_addr, 255); |
| 821 | incr_line -=255; |
| 822 | incr_addr = 0; |
| 823 | } |
| 824 | if (incr_addr > 0 || incr_line > 0) |
| 825 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 826 | c->c_last_addr = c->c_nexti; |
| 827 | c->c_last_line = lineno; |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 832 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 833 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 834 | int extended_arg = arg >> 16; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 835 | if (extended_arg){ |
| 836 | com_addbyte(c, EXTENDED_ARG); |
| 837 | com_addint(c, extended_arg); |
| 838 | arg &= 0xffff; |
| 839 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 840 | com_addbyte(c, op); |
| 841 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 845 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 846 | { |
| 847 | /* Compile a forward reference for backpatching */ |
| 848 | int here; |
| 849 | int anchor; |
| 850 | com_addbyte(c, op); |
| 851 | here = c->c_nexti; |
| 852 | anchor = *p_anchor; |
| 853 | *p_anchor = here; |
| 854 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 855 | } |
| 856 | |
| 857 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 858 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 859 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 860 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 861 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 862 | int dist; |
| 863 | int prev; |
| 864 | for (;;) { |
| 865 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 866 | prev = code[anchor] + (code[anchor+1] << 8); |
| 867 | dist = target - (anchor+2); |
| 868 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 869 | dist >>= 8; |
| 870 | code[anchor+1] = dist; |
| 871 | dist >>= 8; |
| 872 | if (dist) { |
| 873 | com_error(c, PyExc_SystemError, |
| 874 | "com_backpatch: offset too large"); |
| 875 | break; |
| 876 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 877 | if (!prev) |
| 878 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 879 | anchor -= prev; |
| 880 | } |
| 881 | } |
| 882 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 883 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 884 | |
| 885 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 886 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 887 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 888 | PyObject *w, *t, *np=NULL; |
| 889 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 890 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 891 | t = Py_BuildValue("(OO)", v, v->ob_type); |
| 892 | if (t == NULL) |
| 893 | goto fail; |
| 894 | w = PyDict_GetItem(dict, t); |
| 895 | if (w != NULL) { |
| 896 | n = PyInt_AsLong(w); |
| 897 | } else { |
| 898 | n = PyList_Size(list); |
| 899 | np = PyInt_FromLong(n); |
| 900 | if (np == NULL) |
| 901 | goto fail; |
| 902 | if (PyList_Append(list, v) != 0) |
| 903 | goto fail; |
| 904 | if (PyDict_SetItem(dict, t, np) != 0) |
| 905 | goto fail; |
| 906 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 907 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 908 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 909 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 910 | fail: |
| 911 | Py_XDECREF(np); |
| 912 | Py_XDECREF(t); |
| 913 | c->c_errors++; |
| 914 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 918 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 919 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 920 | 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] | 921 | } |
| 922 | |
| 923 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 924 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 925 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 926 | 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] | 927 | } |
| 928 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 929 | int |
| 930 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 931 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 932 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 933 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 934 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 935 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 936 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 937 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 938 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 939 | return 0; /* Don't mangle __extremely_long_names */ |
| 940 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 941 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 942 | /* Strip leading underscores from class name */ |
| 943 | while (*p == '_') |
| 944 | p++; |
| 945 | if (*p == '\0') |
| 946 | return 0; /* Don't mangle if class is just underscores */ |
| 947 | plen = strlen(p); |
| 948 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 949 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 950 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 951 | buffer[0] = '_'; |
| 952 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 953 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 954 | return 1; |
| 955 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 957 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 958 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 959 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 960 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 961 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 962 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 963 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 964 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 965 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 966 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 967 | c->c_errors++; |
| 968 | i = 255; |
| 969 | } |
| 970 | else { |
| 971 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 972 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 973 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 974 | com_addoparg(c, op, i); |
| 975 | } |
| 976 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 977 | #define NAME_LOCAL 0 |
| 978 | #define NAME_GLOBAL 1 |
| 979 | #define NAME_DEFAULT 2 |
| 980 | #define NAME_CLOSURE 3 |
| 981 | |
| 982 | static int |
| 983 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 984 | { |
| 985 | PyObject *v = PyDict_GetItem(dict, name); |
| 986 | if (v == NULL) |
| 987 | return -1; |
| 988 | else |
| 989 | return PyInt_AS_LONG(v); |
| 990 | } |
| 991 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 992 | static int |
| 993 | none_assignment_check(struct compiling *c, char *name, int assigning) |
| 994 | { |
| 995 | if (name[0] == 'N' && strcmp(name, "None") == 0) { |
| 996 | char *msg; |
| 997 | if (assigning) |
| 998 | msg = "assigment to None"; |
| 999 | else |
| 1000 | msg = "deleting None"; |
| 1001 | if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) { |
| 1002 | c->c_errors++; |
| 1003 | return -1; |
| 1004 | } |
| 1005 | } |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1009 | static void |
| 1010 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 1011 | { |
| 1012 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1013 | int i, reftype; |
| 1014 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1015 | int op = STOP_CODE; |
| 1016 | char buffer[MANGLE_LEN]; |
| 1017 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1018 | if (kind != VAR_LOAD && |
| 1019 | none_assignment_check(c, name, kind == VAR_STORE)) |
| 1020 | { |
| 1021 | c->c_errors++; |
| 1022 | i = 255; |
| 1023 | goto done; |
| 1024 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1025 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1026 | name = buffer; |
| 1027 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1028 | c->c_errors++; |
| 1029 | i = 255; |
| 1030 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1031 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1032 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1033 | reftype = get_ref_type(c, name); |
| 1034 | switch (reftype) { |
| 1035 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1036 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1037 | scope = NAME_LOCAL; |
| 1038 | break; |
| 1039 | case GLOBAL_EXPLICIT: |
| 1040 | scope = NAME_GLOBAL; |
| 1041 | break; |
| 1042 | case GLOBAL_IMPLICIT: |
| 1043 | if (c->c_flags & CO_OPTIMIZED) |
| 1044 | scope = NAME_GLOBAL; |
| 1045 | break; |
| 1046 | case FREE: |
| 1047 | case CELL: |
| 1048 | scope = NAME_CLOSURE; |
| 1049 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1053 | if (scope == NAME_LOCAL) |
| 1054 | i = com_lookup_arg(c->c_locals, v); |
| 1055 | else if (reftype == FREE) |
| 1056 | i = com_lookup_arg(c->c_freevars, v); |
| 1057 | else if (reftype == CELL) |
| 1058 | i = com_lookup_arg(c->c_cellvars, v); |
| 1059 | if (i == -1) { |
| 1060 | c->c_errors++; /* XXX no exception set */ |
| 1061 | i = 255; |
| 1062 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1063 | } |
| 1064 | Py_DECREF(v); |
| 1065 | |
| 1066 | switch (kind) { |
| 1067 | case VAR_LOAD: |
| 1068 | switch (scope) { |
| 1069 | case NAME_LOCAL: |
| 1070 | op = LOAD_FAST; |
| 1071 | break; |
| 1072 | case NAME_GLOBAL: |
| 1073 | op = LOAD_GLOBAL; |
| 1074 | break; |
| 1075 | case NAME_DEFAULT: |
| 1076 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1077 | break; |
| 1078 | case NAME_CLOSURE: |
| 1079 | op = LOAD_DEREF; |
| 1080 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1081 | } |
| 1082 | break; |
| 1083 | case VAR_STORE: |
| 1084 | switch (scope) { |
| 1085 | case NAME_LOCAL: |
| 1086 | op = STORE_FAST; |
| 1087 | break; |
| 1088 | case NAME_GLOBAL: |
| 1089 | op = STORE_GLOBAL; |
| 1090 | break; |
| 1091 | case NAME_DEFAULT: |
| 1092 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | case NAME_CLOSURE: |
| 1095 | op = STORE_DEREF; |
| 1096 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1097 | } |
| 1098 | break; |
| 1099 | case VAR_DELETE: |
| 1100 | switch (scope) { |
| 1101 | case NAME_LOCAL: |
| 1102 | op = DELETE_FAST; |
| 1103 | break; |
| 1104 | case NAME_GLOBAL: |
| 1105 | op = DELETE_GLOBAL; |
| 1106 | break; |
| 1107 | case NAME_DEFAULT: |
| 1108 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1111 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1112 | PyOS_snprintf(buf, sizeof(buf), |
| 1113 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1114 | com_error(c, PyExc_SyntaxError, buf); |
| 1115 | i = 255; |
| 1116 | break; |
| 1117 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1118 | } |
| 1119 | break; |
| 1120 | } |
| 1121 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1122 | com_addoparg(c, op, i); |
| 1123 | } |
| 1124 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1125 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1126 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1127 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1128 | char *name; |
| 1129 | char buffer[1000]; |
| 1130 | /* XXX it is possible to write this code without the 1000 |
| 1131 | chars on the total length of dotted names, I just can't be |
| 1132 | bothered right now */ |
| 1133 | if (TYPE(n) == STAR) |
| 1134 | name = "*"; |
| 1135 | else if (TYPE(n) == dotted_name) { |
| 1136 | char *p = buffer; |
| 1137 | int i; |
| 1138 | name = buffer; |
| 1139 | for (i = 0; i < NCH(n); i += 2) { |
| 1140 | char *s = STR(CHILD(n, i)); |
| 1141 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1142 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1143 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1144 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | } |
| 1147 | if (p != buffer) |
| 1148 | *p++ = '.'; |
| 1149 | strcpy(p, s); |
| 1150 | p = strchr(p, '\0'); |
| 1151 | } |
| 1152 | } |
| 1153 | else { |
| 1154 | REQ(n, NAME); |
| 1155 | name = STR(n); |
| 1156 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1157 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1160 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1161 | parsenumber(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1162 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1163 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1164 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1165 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1166 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1167 | int imflag; |
| 1168 | #endif |
| 1169 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1170 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1171 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1172 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1173 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1174 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1175 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1176 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1177 | if (s[0] == '0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1178 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1179 | if (x < 0 && errno == 0) { |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1180 | if (PyErr_WarnExplicit( |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1181 | PyExc_FutureWarning, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1182 | "hex/oct constants > sys.maxint " |
| 1183 | "will return positive values " |
| 1184 | "in Python 2.4 and up", |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 1185 | /* XXX: Give WarnExplicit |
| 1186 | a const char* argument. */ |
| 1187 | (char*)c->c_filename, |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1188 | c->c_lineno, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1189 | NULL, |
| 1190 | NULL) < 0) |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1191 | return NULL; |
Guido van Rossum | 3cb8e54 | 2002-08-11 14:06:15 +0000 | [diff] [blame] | 1192 | errno = 0; /* Might be changed by PyErr_Warn() */ |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1195 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1196 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1197 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1198 | if (errno != 0) |
| 1199 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1200 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1201 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1202 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1203 | #ifndef WITHOUT_COMPLEX |
| 1204 | if (imflag) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1205 | Py_complex z; |
| 1206 | z.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1207 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1208 | z.imag = atof(s); |
| 1209 | PyFPE_END_PROTECT(z) |
| 1210 | return PyComplex_FromCComplex(z); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1211 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1212 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1213 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1214 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1215 | PyFPE_START_PROTECT("atof", return 0) |
| 1216 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1217 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1218 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1219 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1222 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1223 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1224 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1225 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1226 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1227 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1228 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1229 | PyObject *u, *v; |
| 1230 | char *s, *t; |
| 1231 | t = s = *sPtr; |
| 1232 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1233 | while (s < end && (*s & 0x80)) s++; |
| 1234 | *sPtr = s; |
| 1235 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1236 | if (u == NULL) |
| 1237 | return NULL; |
| 1238 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1239 | Py_DECREF(u); |
| 1240 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1241 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 1244 | /* compiler.transformer.Transformer.decode_literal depends on what |
| 1245 | might seem like minor details of this function -- changes here |
| 1246 | must be reflected there. */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1247 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1248 | parsestr(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1249 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1250 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1251 | size_t len; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1252 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1253 | int rawmode = 0; |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1254 | char* encoding = ((c == NULL) ? NULL : c->c_encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1255 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1256 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1257 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1258 | if (isalpha(quote) || quote == '_') { |
| 1259 | if (quote == 'u' || quote == 'U') { |
| 1260 | quote = *++s; |
| 1261 | unicode = 1; |
| 1262 | } |
| 1263 | if (quote == 'r' || quote == 'R') { |
| 1264 | quote = *++s; |
| 1265 | rawmode = 1; |
| 1266 | } |
| 1267 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1268 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1269 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1270 | return NULL; |
| 1271 | } |
| 1272 | s++; |
| 1273 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1274 | if (len > INT_MAX) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1275 | com_error(c, PyExc_OverflowError, |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1276 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1277 | return NULL; |
| 1278 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1279 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1280 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1281 | return NULL; |
| 1282 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1283 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1284 | s += 2; |
| 1285 | len -= 2; |
| 1286 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1287 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1288 | return NULL; |
| 1289 | } |
| 1290 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1291 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1292 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1293 | PyObject *u, *w; |
Walter Dörwald | 4c6c765 | 2002-11-21 20:13:40 +0000 | [diff] [blame] | 1294 | char *buf; |
| 1295 | char *p; |
| 1296 | char *end; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1297 | if (encoding == NULL) { |
| 1298 | buf = s; |
| 1299 | u = NULL; |
| 1300 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1301 | buf = s; |
| 1302 | u = NULL; |
| 1303 | } else { |
| 1304 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1305 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1306 | if (u == NULL) |
| 1307 | return NULL; |
| 1308 | p = buf = PyString_AsString(u); |
| 1309 | end = s + len; |
| 1310 | while (s < end) { |
| 1311 | if (*s == '\\') { |
| 1312 | *p++ = *s++; |
| 1313 | if (*s & 0x80) { |
| 1314 | strcpy(p, "u005c"); |
| 1315 | p += 5; |
| 1316 | } |
| 1317 | } |
| 1318 | if (*s & 0x80) { /* XXX inefficient */ |
| 1319 | char *r; |
| 1320 | int rn, i; |
| 1321 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1322 | if (w == NULL) { |
| 1323 | Py_DECREF(u); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | r = PyString_AsString(w); |
| 1327 | rn = PyString_Size(w); |
| 1328 | assert(rn % 2 == 0); |
| 1329 | for (i = 0; i < rn; i += 2) { |
| 1330 | sprintf(p, "\\u%02x%02x", |
| 1331 | r[i + 0] & 0xFF, |
| 1332 | r[i + 1] & 0xFF); |
| 1333 | p += 6; |
| 1334 | } |
| 1335 | Py_DECREF(w); |
| 1336 | } else { |
| 1337 | *p++ = *s++; |
| 1338 | } |
| 1339 | } |
| 1340 | len = p - buf; |
| 1341 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1342 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1343 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1344 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1345 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1346 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1347 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1348 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1349 | return v; |
| 1350 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1351 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1352 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1353 | need_encoding = (encoding != NULL && |
| 1354 | strcmp(encoding, "utf-8") != 0 && |
| 1355 | strcmp(encoding, "iso-8859-1") != 0); |
| 1356 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1357 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1358 | #ifndef Py_USING_UNICODE |
| 1359 | /* This should not happen - we never see any other |
| 1360 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1361 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1362 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1363 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1364 | if (u == NULL) |
| 1365 | return NULL; |
| 1366 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1367 | Py_DECREF(u); |
| 1368 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1369 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1370 | } else { |
| 1371 | return PyString_FromStringAndSize(s, len); |
| 1372 | } |
| 1373 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1374 | |
| 1375 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1376 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1377 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1378 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1379 | return v; |
| 1380 | } |
| 1381 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1382 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1383 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1384 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1385 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1386 | int i; |
| 1387 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1388 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1389 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1390 | for (i = 1; i < NCH(n); i++) { |
| 1391 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1392 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1393 | if (s == NULL) |
| 1394 | goto onError; |
| 1395 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1396 | PyString_ConcatAndDel(&v, s); |
| 1397 | if (v == NULL) |
| 1398 | goto onError; |
| 1399 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1400 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1401 | else { |
| 1402 | PyObject *temp; |
| 1403 | temp = PyUnicode_Concat(v, s); |
| 1404 | Py_DECREF(s); |
| 1405 | if (temp == NULL) |
| 1406 | goto onError; |
| 1407 | Py_DECREF(v); |
| 1408 | v = temp; |
| 1409 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1410 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1414 | |
| 1415 | onError: |
| 1416 | Py_XDECREF(v); |
| 1417 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1420 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1421 | 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] | 1422 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1423 | int anchor = 0; |
| 1424 | int save_begin = c->c_begin; |
| 1425 | |
| 1426 | /* list_iter: for v in expr [list_iter] */ |
| 1427 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1428 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1429 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1430 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1431 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1432 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1433 | c->c_loops++; |
| 1434 | com_list_iter(c, n, e, t); |
| 1435 | c->c_loops--; |
| 1436 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1437 | c->c_begin = save_begin; |
| 1438 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1439 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | static void |
| 1443 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1444 | { |
| 1445 | int anchor = 0; |
| 1446 | int a = 0; |
| 1447 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1448 | com_node(c, CHILD(n, 1)); |
| 1449 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1450 | com_addbyte(c, POP_TOP); |
| 1451 | com_pop(c, 1); |
| 1452 | com_list_iter(c, n, e, t); |
| 1453 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1454 | com_backpatch(c, a); |
| 1455 | /* We jump here with an extra entry which we now pop */ |
| 1456 | com_addbyte(c, POP_TOP); |
| 1457 | com_backpatch(c, anchor); |
| 1458 | } |
| 1459 | |
| 1460 | static void |
| 1461 | com_list_iter(struct compiling *c, |
| 1462 | node *p, /* parent of list_iter node */ |
| 1463 | node *e, /* element expression node */ |
| 1464 | char *t /* name of result list temp local */) |
| 1465 | { |
| 1466 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1467 | node *n = CHILD(p, NCH(p)-1); |
| 1468 | if (TYPE(n) == list_iter) { |
| 1469 | n = CHILD(n, 0); |
| 1470 | switch (TYPE(n)) { |
| 1471 | case list_for: |
| 1472 | com_list_for(c, n, e, t); |
| 1473 | break; |
| 1474 | case list_if: |
| 1475 | com_list_if(c, n, e, t); |
| 1476 | break; |
| 1477 | default: |
| 1478 | com_error(c, PyExc_SystemError, |
| 1479 | "invalid list_iter node type"); |
| 1480 | } |
| 1481 | } |
| 1482 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1483 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1484 | com_push(c, 1); |
| 1485 | com_node(c, e); |
| 1486 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1487 | com_addbyte(c, POP_TOP); |
| 1488 | com_pop(c, 2); |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | static void |
| 1493 | com_list_comprehension(struct compiling *c, node *n) |
| 1494 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1495 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1496 | char tmpname[30]; |
| 1497 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1498 | com_addoparg(c, BUILD_LIST, 0); |
| 1499 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1500 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1501 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1502 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1503 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1504 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1505 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1506 | --c->c_tmpname; |
| 1507 | } |
| 1508 | |
| 1509 | static void |
| 1510 | com_listmaker(struct compiling *c, node *n) |
| 1511 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1512 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1513 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1514 | com_list_comprehension(c, n); |
| 1515 | else { |
| 1516 | int len = 0; |
| 1517 | int i; |
| 1518 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1519 | com_node(c, CHILD(n, i)); |
| 1520 | com_addoparg(c, BUILD_LIST, len); |
| 1521 | com_pop(c, len-1); |
| 1522 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1526 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1527 | { |
| 1528 | int i; |
| 1529 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1530 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1531 | /* We must arrange things just right for STORE_SUBSCR. |
| 1532 | It wants the stack to look like (value) (dict) (key) */ |
| 1533 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1534 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1535 | com_node(c, CHILD(n, i)); /* key */ |
Gustavo Niemeyer | 78429a6 | 2002-12-16 13:54:02 +0000 | [diff] [blame] | 1536 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1537 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1538 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1539 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1544 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1545 | { |
| 1546 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1547 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1548 | int i; |
| 1549 | REQ(n, atom); |
| 1550 | ch = CHILD(n, 0); |
| 1551 | switch (TYPE(ch)) { |
| 1552 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1553 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1554 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1555 | com_push(c, 1); |
| 1556 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1557 | else |
| 1558 | com_node(c, CHILD(n, 1)); |
| 1559 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1560 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1561 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1562 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1563 | com_push(c, 1); |
| 1564 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1565 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1566 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1567 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1568 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1569 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1570 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1571 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1572 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | case BACKQUOTE: |
| 1575 | com_node(c, CHILD(n, 1)); |
| 1576 | com_addbyte(c, UNARY_CONVERT); |
| 1577 | break; |
| 1578 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1579 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1580 | i = 255; |
| 1581 | } |
| 1582 | else { |
| 1583 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1584 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1585 | } |
| 1586 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1587 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1588 | break; |
| 1589 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1590 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1591 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1592 | c->c_errors++; |
| 1593 | i = 255; |
| 1594 | } |
| 1595 | else { |
| 1596 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1597 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1598 | } |
| 1599 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1600 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1603 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1604 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1605 | break; |
| 1606 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1607 | com_error(c, PyExc_SystemError, |
| 1608 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1613 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1614 | { |
| 1615 | if (NCH(n) == 1) { |
| 1616 | com_addbyte(c, op); |
| 1617 | } |
| 1618 | else if (NCH(n) == 2) { |
| 1619 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1620 | com_node(c, CHILD(n, 0)); |
| 1621 | com_addbyte(c, op+1); |
| 1622 | } |
| 1623 | else { |
| 1624 | com_node(c, CHILD(n, 1)); |
| 1625 | com_addbyte(c, op+2); |
| 1626 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1627 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1628 | } |
| 1629 | else { |
| 1630 | com_node(c, CHILD(n, 0)); |
| 1631 | com_node(c, CHILD(n, 2)); |
| 1632 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1633 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1637 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1638 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1639 | { |
| 1640 | if (NCH(n) == 1) { |
| 1641 | com_addbyte(c, DUP_TOP); |
| 1642 | com_push(c, 1); |
| 1643 | com_addbyte(c, SLICE); |
| 1644 | com_node(c, augn); |
| 1645 | com_addbyte(c, opcode); |
| 1646 | com_pop(c, 1); |
| 1647 | com_addbyte(c, ROT_TWO); |
| 1648 | com_addbyte(c, STORE_SLICE); |
| 1649 | com_pop(c, 2); |
| 1650 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1651 | com_node(c, CHILD(n, 0)); |
| 1652 | com_addoparg(c, DUP_TOPX, 2); |
| 1653 | com_push(c, 2); |
| 1654 | com_addbyte(c, SLICE+1); |
| 1655 | com_pop(c, 1); |
| 1656 | com_node(c, augn); |
| 1657 | com_addbyte(c, opcode); |
| 1658 | com_pop(c, 1); |
| 1659 | com_addbyte(c, ROT_THREE); |
| 1660 | com_addbyte(c, STORE_SLICE+1); |
| 1661 | com_pop(c, 3); |
| 1662 | } else if (NCH(n) == 2) { |
| 1663 | com_node(c, CHILD(n, 1)); |
| 1664 | com_addoparg(c, DUP_TOPX, 2); |
| 1665 | com_push(c, 2); |
| 1666 | com_addbyte(c, SLICE+2); |
| 1667 | com_pop(c, 1); |
| 1668 | com_node(c, augn); |
| 1669 | com_addbyte(c, opcode); |
| 1670 | com_pop(c, 1); |
| 1671 | com_addbyte(c, ROT_THREE); |
| 1672 | com_addbyte(c, STORE_SLICE+2); |
| 1673 | com_pop(c, 3); |
| 1674 | } else { |
| 1675 | com_node(c, CHILD(n, 0)); |
| 1676 | com_node(c, CHILD(n, 2)); |
| 1677 | com_addoparg(c, DUP_TOPX, 3); |
| 1678 | com_push(c, 3); |
| 1679 | com_addbyte(c, SLICE+3); |
| 1680 | com_pop(c, 2); |
| 1681 | com_node(c, augn); |
| 1682 | com_addbyte(c, opcode); |
| 1683 | com_pop(c, 1); |
| 1684 | com_addbyte(c, ROT_FOUR); |
| 1685 | com_addbyte(c, STORE_SLICE+3); |
| 1686 | com_pop(c, 4); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1691 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1692 | { |
| 1693 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1694 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1695 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1696 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1697 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1698 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1699 | } |
| 1700 | else { |
| 1701 | com_node(c, CHILD(n, 0)); |
| 1702 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1703 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1704 | } |
| 1705 | m = n; |
| 1706 | do { |
| 1707 | m = CHILD(m, 0); |
| 1708 | } while (NCH(m) == 1); |
| 1709 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1710 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1711 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1712 | * SF bug 132313 points out that complaining about a keyword |
| 1713 | * then is very confusing. |
| 1714 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1715 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1716 | TYPE(m) == lambdef ? |
| 1717 | "lambda cannot contain assignment" : |
| 1718 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1719 | } |
| 1720 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1721 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 63dd79a | 2002-08-16 02:24:56 +0000 | [diff] [blame] | 1722 | (void) none_assignment_check(c, STR(m), 1); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1723 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1724 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1725 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1726 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1727 | else if (*pkeywords == NULL) { |
| 1728 | c->c_errors++; |
| 1729 | Py_DECREF(v); |
| 1730 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1731 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1732 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1733 | "duplicate keyword argument"); |
| 1734 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1735 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1736 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1737 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1738 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1739 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1740 | } |
| 1741 | } |
| 1742 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1745 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1746 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1747 | { |
| 1748 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1749 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1750 | } |
| 1751 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1752 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1753 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1754 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1755 | int star_flag = 0; |
| 1756 | int starstar_flag = 0; |
| 1757 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1758 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1759 | na = 0; |
| 1760 | nk = 0; |
| 1761 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1762 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1763 | if (TYPE(ch) == STAR || |
| 1764 | TYPE(ch) == DOUBLESTAR) |
| 1765 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1766 | if (ch->n_lineno != lineno) { |
| 1767 | lineno = ch->n_lineno; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1768 | com_set_lineno(c, lineno); |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1769 | } |
| 1770 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1771 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1772 | na++; |
| 1773 | else |
| 1774 | nk++; |
| 1775 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1776 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1777 | while (i < NCH(n)) { |
| 1778 | node *tok = CHILD(n, i); |
| 1779 | node *ch = CHILD(n, i+1); |
| 1780 | i += 3; |
| 1781 | switch (TYPE(tok)) { |
| 1782 | case STAR: star_flag = 1; break; |
| 1783 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1784 | } |
| 1785 | com_node(c, ch); |
| 1786 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1787 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1788 | com_error(c, PyExc_SyntaxError, |
| 1789 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1790 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1791 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1792 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1793 | star_flag + (starstar_flag << 1); |
| 1794 | else |
| 1795 | opcode = CALL_FUNCTION; |
| 1796 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1797 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1802 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1803 | { |
| 1804 | com_addopname(c, LOAD_ATTR, n); |
| 1805 | } |
| 1806 | |
| 1807 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1808 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1809 | { |
| 1810 | int i=0; |
| 1811 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1812 | node *ch; |
| 1813 | |
| 1814 | /* first argument */ |
| 1815 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1816 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1817 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1818 | i++; |
| 1819 | } |
| 1820 | else { |
| 1821 | com_node(c, CHILD(n,i)); |
| 1822 | i++; |
| 1823 | REQ(CHILD(n,i),COLON); |
| 1824 | i++; |
| 1825 | } |
| 1826 | /* second argument */ |
| 1827 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1828 | com_node(c, CHILD(n,i)); |
| 1829 | i++; |
| 1830 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1831 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1832 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1833 | com_push(c, 1); |
| 1834 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1835 | /* remaining arguments */ |
| 1836 | for (; i < NCH(n); i++) { |
| 1837 | ns++; |
| 1838 | ch=CHILD(n,i); |
| 1839 | REQ(ch, sliceop); |
| 1840 | if (NCH(ch) == 1) { |
| 1841 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1842 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1843 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1844 | } |
| 1845 | else |
| 1846 | com_node(c, CHILD(ch,1)); |
| 1847 | } |
| 1848 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1849 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1853 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1854 | { |
| 1855 | node *ch; |
| 1856 | REQ(n, subscript); |
| 1857 | ch = CHILD(n,0); |
| 1858 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1859 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1860 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1861 | com_push(c, 1); |
| 1862 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1863 | else { |
| 1864 | /* check for slice */ |
| 1865 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1866 | com_sliceobj(c, n); |
| 1867 | else { |
| 1868 | REQ(ch, test); |
| 1869 | com_node(c, ch); |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1875 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1876 | { |
| 1877 | int i, op; |
| 1878 | REQ(n, subscriptlist); |
| 1879 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1880 | if (NCH(n) == 1) { |
| 1881 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1882 | /* 'Basic' slice, should have exactly one colon. */ |
| 1883 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1884 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1885 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1886 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1887 | switch (assigning) { |
| 1888 | case OP_DELETE: |
| 1889 | op = DELETE_SLICE; |
| 1890 | break; |
| 1891 | case OP_ASSIGN: |
| 1892 | op = STORE_SLICE; |
| 1893 | break; |
| 1894 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1895 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1896 | break; |
| 1897 | default: |
| 1898 | com_augassign_slice(c, sub, assigning, augn); |
| 1899 | return; |
| 1900 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1901 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1902 | if (op == STORE_SLICE) |
| 1903 | com_pop(c, 2); |
| 1904 | else if (op == DELETE_SLICE) |
| 1905 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1906 | return; |
| 1907 | } |
| 1908 | } |
| 1909 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1910 | for (i = 0; i < NCH(n); i += 2) |
| 1911 | com_subscript(c, CHILD(n, i)); |
| 1912 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1913 | if (NCH(n) > 1) { |
| 1914 | i = (NCH(n)+1) / 2; |
| 1915 | com_addoparg(c, BUILD_TUPLE, i); |
| 1916 | com_pop(c, i-1); |
| 1917 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1918 | switch (assigning) { |
| 1919 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1920 | op = DELETE_SUBSCR; |
| 1921 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1922 | break; |
| 1923 | default: |
| 1924 | case OP_ASSIGN: |
| 1925 | op = STORE_SUBSCR; |
| 1926 | i = 3; |
| 1927 | break; |
| 1928 | case OP_APPLY: |
| 1929 | op = BINARY_SUBSCR; |
| 1930 | i = 1; |
| 1931 | break; |
| 1932 | } |
| 1933 | if (assigning > OP_APPLY) { |
| 1934 | com_addoparg(c, DUP_TOPX, 2); |
| 1935 | com_push(c, 2); |
| 1936 | com_addbyte(c, BINARY_SUBSCR); |
| 1937 | com_pop(c, 1); |
| 1938 | com_node(c, augn); |
| 1939 | com_addbyte(c, assigning); |
| 1940 | com_pop(c, 1); |
| 1941 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1942 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1943 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1944 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1948 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1949 | { |
| 1950 | REQ(n, trailer); |
| 1951 | switch (TYPE(CHILD(n, 0))) { |
| 1952 | case LPAR: |
| 1953 | com_call_function(c, CHILD(n, 1)); |
| 1954 | break; |
| 1955 | case DOT: |
| 1956 | com_select_member(c, CHILD(n, 1)); |
| 1957 | break; |
| 1958 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1959 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1962 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1963 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1968 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1969 | { |
| 1970 | int i; |
| 1971 | REQ(n, power); |
| 1972 | com_atom(c, CHILD(n, 0)); |
| 1973 | for (i = 1; i < NCH(n); i++) { |
| 1974 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1975 | com_factor(c, CHILD(n, i+1)); |
| 1976 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1977 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1978 | break; |
| 1979 | } |
| 1980 | else |
| 1981 | com_apply_trailer(c, CHILD(n, i)); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1986 | com_invert_constant(struct compiling *c, node *n) |
| 1987 | { |
| 1988 | /* Compute the inverse of int and longs and use them directly, |
| 1989 | but be prepared to generate code for all other |
| 1990 | possibilities (invalid numbers, floats, complex). |
| 1991 | */ |
| 1992 | PyObject *num, *inv = NULL; |
| 1993 | int i; |
| 1994 | |
| 1995 | REQ(n, NUMBER); |
| 1996 | num = parsenumber(c, STR(n)); |
| 1997 | if (num == NULL) |
| 1998 | i = 255; |
| 1999 | else { |
| 2000 | inv = PyNumber_Invert(num); |
| 2001 | if (inv == NULL) { |
| 2002 | PyErr_Clear(); |
| 2003 | i = com_addconst(c, num); |
| 2004 | } else { |
| 2005 | i = com_addconst(c, inv); |
| 2006 | Py_DECREF(inv); |
| 2007 | } |
| 2008 | Py_DECREF(num); |
| 2009 | } |
| 2010 | com_addoparg(c, LOAD_CONST, i); |
| 2011 | com_push(c, 1); |
| 2012 | if (num != NULL && inv == NULL) |
| 2013 | com_addbyte(c, UNARY_INVERT); |
| 2014 | } |
| 2015 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2016 | static int |
| 2017 | is_float_zero(const char *p) |
| 2018 | { |
| 2019 | int found_radix_point = 0; |
| 2020 | int ch; |
| 2021 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2022 | switch (ch) { |
| 2023 | case '0': |
| 2024 | /* no reason to believe it's not 0 -- continue */ |
| 2025 | break; |
| 2026 | |
| 2027 | case 'e': case 'E': case 'j': case 'J': |
| 2028 | /* If this was a hex constant, we already would have |
| 2029 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2030 | must be an exponent marker, and we haven't yet |
| 2031 | seen a non-zero digit, and it doesn't matter what |
| 2032 | the exponent is then. For 'j' or 'J' similarly, |
| 2033 | except that this is an imaginary 0 then. */ |
| 2034 | return 1; |
| 2035 | |
| 2036 | case '.': |
| 2037 | found_radix_point = 1; |
| 2038 | break; |
| 2039 | |
| 2040 | default: |
| 2041 | return 0; |
| 2042 | } |
| 2043 | } |
| 2044 | return found_radix_point; |
| 2045 | } |
| 2046 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2047 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2048 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2049 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2050 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2051 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2052 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2053 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2054 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2055 | approriate value as a constant. If the value is negative, |
| 2056 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2057 | negative in the 0th position -- unless we're doing unary minus |
| 2058 | of a floating zero! In that case the sign is significant, but |
| 2059 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2060 | */ |
| 2061 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2062 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2063 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2064 | && NCH(pfactor) == 1 |
| 2065 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2066 | && NCH(ppower) == 1 |
| 2067 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2068 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 2069 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2070 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2071 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2072 | return; |
| 2073 | } |
| 2074 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2075 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2076 | if (s == NULL) { |
| 2077 | com_error(c, PyExc_MemoryError, ""); |
| 2078 | com_addbyte(c, 255); |
| 2079 | return; |
| 2080 | } |
| 2081 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2082 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2083 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2084 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2085 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2086 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2087 | } |
| 2088 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2089 | com_factor(c, CHILD(n, 1)); |
| 2090 | com_addbyte(c, UNARY_POSITIVE); |
| 2091 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2092 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2093 | com_factor(c, CHILD(n, 1)); |
| 2094 | com_addbyte(c, UNARY_NEGATIVE); |
| 2095 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2096 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2097 | com_factor(c, CHILD(n, 1)); |
| 2098 | com_addbyte(c, UNARY_INVERT); |
| 2099 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2100 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2101 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2106 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2107 | { |
| 2108 | int i; |
| 2109 | int op; |
| 2110 | REQ(n, term); |
| 2111 | com_factor(c, CHILD(n, 0)); |
| 2112 | for (i = 2; i < NCH(n); i += 2) { |
| 2113 | com_factor(c, CHILD(n, i)); |
| 2114 | switch (TYPE(CHILD(n, i-1))) { |
| 2115 | case STAR: |
| 2116 | op = BINARY_MULTIPLY; |
| 2117 | break; |
| 2118 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2119 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2120 | op = BINARY_TRUE_DIVIDE; |
| 2121 | else |
| 2122 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2123 | break; |
| 2124 | case PERCENT: |
| 2125 | op = BINARY_MODULO; |
| 2126 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2127 | case DOUBLESLASH: |
| 2128 | op = BINARY_FLOOR_DIVIDE; |
| 2129 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2130 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2131 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2132 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2133 | op = 255; |
| 2134 | } |
| 2135 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2136 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2141 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2142 | { |
| 2143 | int i; |
| 2144 | int op; |
| 2145 | REQ(n, arith_expr); |
| 2146 | com_term(c, CHILD(n, 0)); |
| 2147 | for (i = 2; i < NCH(n); i += 2) { |
| 2148 | com_term(c, CHILD(n, i)); |
| 2149 | switch (TYPE(CHILD(n, i-1))) { |
| 2150 | case PLUS: |
| 2151 | op = BINARY_ADD; |
| 2152 | break; |
| 2153 | case MINUS: |
| 2154 | op = BINARY_SUBTRACT; |
| 2155 | break; |
| 2156 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2157 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2158 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2159 | op = 255; |
| 2160 | } |
| 2161 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2162 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2167 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2168 | { |
| 2169 | int i; |
| 2170 | int op; |
| 2171 | REQ(n, shift_expr); |
| 2172 | com_arith_expr(c, CHILD(n, 0)); |
| 2173 | for (i = 2; i < NCH(n); i += 2) { |
| 2174 | com_arith_expr(c, CHILD(n, i)); |
| 2175 | switch (TYPE(CHILD(n, i-1))) { |
| 2176 | case LEFTSHIFT: |
| 2177 | op = BINARY_LSHIFT; |
| 2178 | break; |
| 2179 | case RIGHTSHIFT: |
| 2180 | op = BINARY_RSHIFT; |
| 2181 | break; |
| 2182 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2183 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2184 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2185 | op = 255; |
| 2186 | } |
| 2187 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2188 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2193 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2194 | { |
| 2195 | int i; |
| 2196 | int op; |
| 2197 | REQ(n, and_expr); |
| 2198 | com_shift_expr(c, CHILD(n, 0)); |
| 2199 | for (i = 2; i < NCH(n); i += 2) { |
| 2200 | com_shift_expr(c, CHILD(n, i)); |
| 2201 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2202 | op = BINARY_AND; |
| 2203 | } |
| 2204 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2205 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2206 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2207 | op = 255; |
| 2208 | } |
| 2209 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2210 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2211 | } |
| 2212 | } |
| 2213 | |
| 2214 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2215 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2216 | { |
| 2217 | int i; |
| 2218 | int op; |
| 2219 | REQ(n, xor_expr); |
| 2220 | com_and_expr(c, CHILD(n, 0)); |
| 2221 | for (i = 2; i < NCH(n); i += 2) { |
| 2222 | com_and_expr(c, CHILD(n, i)); |
| 2223 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2224 | op = BINARY_XOR; |
| 2225 | } |
| 2226 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2227 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2228 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2229 | op = 255; |
| 2230 | } |
| 2231 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2232 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2237 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2238 | { |
| 2239 | int i; |
| 2240 | int op; |
| 2241 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2242 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2243 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2244 | com_xor_expr(c, CHILD(n, i)); |
| 2245 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2246 | op = BINARY_OR; |
| 2247 | } |
| 2248 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2249 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2250 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2251 | op = 255; |
| 2252 | } |
| 2253 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2254 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2259 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2260 | { |
| 2261 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2262 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2263 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2264 | if (NCH(n) == 1) { |
| 2265 | n = CHILD(n, 0); |
| 2266 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2267 | case LESS: return PyCmp_LT; |
| 2268 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2269 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2270 | case EQUAL: return PyCmp_EQ; |
| 2271 | case LESSEQUAL: return PyCmp_LE; |
| 2272 | case GREATEREQUAL: return PyCmp_GE; |
| 2273 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2274 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2275 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2276 | } |
| 2277 | } |
| 2278 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2279 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2280 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2281 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2282 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2283 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2284 | } |
| 2285 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2286 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2290 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2291 | { |
| 2292 | int i; |
| 2293 | enum cmp_op op; |
| 2294 | int anchor; |
| 2295 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2296 | com_expr(c, CHILD(n, 0)); |
| 2297 | if (NCH(n) == 1) |
| 2298 | return; |
| 2299 | |
| 2300 | /**************************************************************** |
| 2301 | The following code is generated for all but the last |
| 2302 | comparison in a chain: |
| 2303 | |
| 2304 | label: on stack: opcode: jump to: |
| 2305 | |
| 2306 | a <code to load b> |
| 2307 | a, b DUP_TOP |
| 2308 | a, b, b ROT_THREE |
| 2309 | b, a, b COMPARE_OP |
| 2310 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2311 | b, 1 POP_TOP |
| 2312 | b |
| 2313 | |
| 2314 | We are now ready to repeat this sequence for the next |
| 2315 | comparison in the chain. |
| 2316 | |
| 2317 | For the last we generate: |
| 2318 | |
| 2319 | b <code to load c> |
| 2320 | b, c COMPARE_OP |
| 2321 | 0-or-1 |
| 2322 | |
| 2323 | If there were any jumps to L1 (i.e., there was more than one |
| 2324 | comparison), we generate: |
| 2325 | |
| 2326 | 0-or-1 JUMP_FORWARD L2 |
| 2327 | L1: b, 0 ROT_TWO |
| 2328 | 0, b POP_TOP |
| 2329 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2330 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2331 | ****************************************************************/ |
| 2332 | |
| 2333 | anchor = 0; |
| 2334 | |
| 2335 | for (i = 2; i < NCH(n); i += 2) { |
| 2336 | com_expr(c, CHILD(n, i)); |
| 2337 | if (i+2 < NCH(n)) { |
| 2338 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2339 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2340 | com_addbyte(c, ROT_THREE); |
| 2341 | } |
| 2342 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2343 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2344 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2345 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2346 | } |
| 2347 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2348 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2349 | if (i+2 < NCH(n)) { |
| 2350 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2351 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2352 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | if (anchor) { |
| 2357 | int anchor2 = 0; |
| 2358 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2359 | com_backpatch(c, anchor); |
| 2360 | com_addbyte(c, ROT_TWO); |
| 2361 | com_addbyte(c, POP_TOP); |
| 2362 | com_backpatch(c, anchor2); |
| 2363 | } |
| 2364 | } |
| 2365 | |
| 2366 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2367 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2368 | { |
| 2369 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2370 | if (NCH(n) == 1) { |
| 2371 | com_comparison(c, CHILD(n, 0)); |
| 2372 | } |
| 2373 | else { |
| 2374 | com_not_test(c, CHILD(n, 1)); |
| 2375 | com_addbyte(c, UNARY_NOT); |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2380 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2381 | { |
| 2382 | int i; |
| 2383 | int anchor; |
| 2384 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2385 | anchor = 0; |
| 2386 | i = 0; |
| 2387 | for (;;) { |
| 2388 | com_not_test(c, CHILD(n, i)); |
| 2389 | if ((i += 2) >= NCH(n)) |
| 2390 | break; |
| 2391 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2392 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2393 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2394 | } |
| 2395 | if (anchor) |
| 2396 | com_backpatch(c, anchor); |
| 2397 | } |
| 2398 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2399 | static int |
| 2400 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2401 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2402 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2403 | if (free == 0) |
| 2404 | return 0; |
| 2405 | for (i = 0; i < free; ++i) { |
| 2406 | /* Bypass com_addop_varname because it will generate |
| 2407 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2408 | */ |
| 2409 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2410 | int arg, reftype; |
| 2411 | |
| 2412 | /* Special case: If a class contains a method with a |
| 2413 | free variable that has the same name as a method, |
| 2414 | the name will be considered free *and* local in the |
| 2415 | class. It should be handled by the closure, as |
| 2416 | well as by the normal name loookup logic. |
| 2417 | */ |
| 2418 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2419 | if (reftype == CELL) |
| 2420 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2421 | else /* (reftype == FREE) */ |
| 2422 | arg = com_lookup_arg(c->c_freevars, name); |
| 2423 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2424 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2425 | "freevars of %s: %s\n", |
| 2426 | PyObject_REPR(name), |
| 2427 | c->c_name, |
| 2428 | reftype, arg, |
| 2429 | PyString_AS_STRING(co->co_name), |
| 2430 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2431 | Py_FatalError("com_make_closure()"); |
| 2432 | } |
| 2433 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2434 | |
| 2435 | } |
| 2436 | com_push(c, free); |
| 2437 | return 1; |
| 2438 | } |
| 2439 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2440 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2441 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2442 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2443 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2444 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2445 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2446 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2447 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2448 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2449 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2450 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2451 | if (co == NULL) { |
| 2452 | c->c_errors++; |
| 2453 | return; |
| 2454 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2455 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2456 | i = com_addconst(c, (PyObject *)co); |
| 2457 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2458 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2459 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2460 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2461 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2462 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2463 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2464 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2465 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2466 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2467 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2468 | else { |
| 2469 | int anchor = 0; |
| 2470 | int i = 0; |
| 2471 | for (;;) { |
| 2472 | com_and_test(c, CHILD(n, i)); |
| 2473 | if ((i += 2) >= NCH(n)) |
| 2474 | break; |
| 2475 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2476 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2477 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2478 | } |
| 2479 | if (anchor) |
| 2480 | com_backpatch(c, anchor); |
| 2481 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2485 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2486 | { |
| 2487 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2488 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2489 | com_node(c, CHILD(n, 0)); |
| 2490 | } |
| 2491 | else { |
| 2492 | int i; |
| 2493 | int len; |
| 2494 | len = (NCH(n) + 1) / 2; |
| 2495 | for (i = 0; i < NCH(n); i += 2) |
| 2496 | com_node(c, CHILD(n, i)); |
| 2497 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2498 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | |
| 2503 | /* Begin of assignment compilation */ |
| 2504 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2505 | |
| 2506 | static void |
| 2507 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2508 | { |
| 2509 | com_addbyte(c, DUP_TOP); |
| 2510 | com_push(c, 1); |
| 2511 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2512 | com_node(c, augn); |
| 2513 | com_addbyte(c, opcode); |
| 2514 | com_pop(c, 1); |
| 2515 | com_addbyte(c, ROT_TWO); |
| 2516 | com_addopname(c, STORE_ATTR, n); |
| 2517 | com_pop(c, 2); |
| 2518 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2519 | |
| 2520 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2521 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2522 | { |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 2523 | if (none_assignment_check(c, STR(n), assigning)) |
| 2524 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2525 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2526 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2530 | 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] | 2531 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2532 | REQ(n, trailer); |
| 2533 | switch (TYPE(CHILD(n, 0))) { |
| 2534 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2535 | if (assigning == OP_DELETE) |
| 2536 | com_error(c, PyExc_SyntaxError, |
| 2537 | "can't delete function call"); |
| 2538 | else |
| 2539 | com_error(c, PyExc_SyntaxError, |
| 2540 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2541 | break; |
| 2542 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2543 | if (assigning > OP_APPLY) |
| 2544 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2545 | else |
| 2546 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2547 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2548 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2549 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2550 | break; |
| 2551 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2552 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2557 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2558 | { |
| 2559 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2560 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2561 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2562 | if (assigning) { |
| 2563 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2564 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2565 | com_push(c, i-1); |
| 2566 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2567 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2568 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2569 | } |
| 2570 | |
| 2571 | static void |
| 2572 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2573 | { |
| 2574 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2575 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2576 | com_push(c, 1); |
| 2577 | com_node(c, augn); |
| 2578 | com_addbyte(c, opcode); |
| 2579 | com_pop(c, 1); |
| 2580 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2584 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2585 | { |
| 2586 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2587 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2588 | if (assigning) |
| 2589 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2593 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2594 | { |
| 2595 | /* Loop to avoid trivial recursion */ |
| 2596 | for (;;) { |
| 2597 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2598 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2599 | case exprlist: |
| 2600 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2601 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2602 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2603 | if (assigning > OP_APPLY) { |
| 2604 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2605 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2608 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2609 | return; |
| 2610 | } |
| 2611 | n = CHILD(n, 0); |
| 2612 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2613 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2614 | case test: |
| 2615 | case and_test: |
| 2616 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2617 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2618 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2619 | case xor_expr: |
| 2620 | case and_expr: |
| 2621 | case shift_expr: |
| 2622 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2623 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2624 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2625 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2626 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2627 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | } |
| 2630 | n = CHILD(n, 0); |
| 2631 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2632 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2633 | case power: /* atom trailer* ('**' power)* |
| 2634 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2635 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2636 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2637 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2638 | return; |
| 2639 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2640 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2641 | int i; |
| 2642 | com_node(c, CHILD(n, 0)); |
| 2643 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2644 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2645 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2646 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2647 | return; |
| 2648 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2649 | com_apply_trailer(c, CHILD(n, i)); |
| 2650 | } /* NB i is still alive */ |
| 2651 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2652 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2653 | return; |
| 2654 | } |
| 2655 | n = CHILD(n, 0); |
| 2656 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2657 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2658 | case atom: |
| 2659 | switch (TYPE(CHILD(n, 0))) { |
| 2660 | case LPAR: |
| 2661 | n = CHILD(n, 1); |
| 2662 | if (TYPE(n) == RPAR) { |
| 2663 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2664 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2665 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2668 | if (assigning > OP_APPLY) { |
| 2669 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2670 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2671 | return; |
| 2672 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2673 | break; |
| 2674 | case LSQB: |
| 2675 | n = CHILD(n, 1); |
| 2676 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2677 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2678 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2679 | return; |
| 2680 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2681 | if (assigning > OP_APPLY) { |
| 2682 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2683 | "augmented assign to list not possible"); |
| 2684 | return; |
| 2685 | } |
| 2686 | if (NCH(n) > 1 |
| 2687 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2688 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2689 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2690 | return; |
| 2691 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2692 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2693 | return; |
| 2694 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2695 | if (assigning > OP_APPLY) |
| 2696 | com_augassign_name(c, CHILD(n, 0), |
| 2697 | assigning, augn); |
| 2698 | else |
| 2699 | com_assign_name(c, CHILD(n, 0), |
| 2700 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2701 | return; |
| 2702 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2703 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2704 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
| 2707 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2708 | |
| 2709 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2710 | com_error(c, PyExc_SyntaxError, |
| 2711 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2712 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2713 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2714 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2715 | com_error(c, PyExc_SystemError, |
| 2716 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2717 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2718 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2722 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2723 | static void |
| 2724 | com_augassign(struct compiling *c, node *n) |
| 2725 | { |
| 2726 | int opcode; |
| 2727 | |
| 2728 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2729 | case '+': opcode = INPLACE_ADD; break; |
| 2730 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2731 | case '/': |
| 2732 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2733 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2734 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2735 | opcode = INPLACE_TRUE_DIVIDE; |
| 2736 | else |
| 2737 | opcode = INPLACE_DIVIDE; |
| 2738 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2739 | case '%': opcode = INPLACE_MODULO; break; |
| 2740 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2741 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2742 | case '&': opcode = INPLACE_AND; break; |
| 2743 | case '^': opcode = INPLACE_XOR; break; |
| 2744 | case '|': opcode = INPLACE_OR; break; |
| 2745 | case '*': |
| 2746 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2747 | opcode = INPLACE_POWER; |
| 2748 | else |
| 2749 | opcode = INPLACE_MULTIPLY; |
| 2750 | break; |
| 2751 | default: |
| 2752 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2753 | return; |
| 2754 | } |
| 2755 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2756 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2757 | |
| 2758 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2759 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2760 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2761 | REQ(n, expr_stmt); |
| 2762 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2763 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2764 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2765 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2766 | if (NCH(n) == 1) { |
| 2767 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2768 | if (c->c_interactive) |
| 2769 | com_addbyte(c, PRINT_EXPR); |
| 2770 | else |
| 2771 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2772 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2773 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2774 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2775 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2776 | else { |
| 2777 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2778 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2779 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2780 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2781 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2782 | com_push(c, 1); |
| 2783 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2784 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2790 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2791 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2792 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2793 | int i; |
| 2794 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2795 | if (Py_OptimizeFlag) |
| 2796 | return; |
| 2797 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2798 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2799 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2800 | raise AssertionError [, <message>] |
| 2801 | |
| 2802 | where <message> is the second test, if present. |
| 2803 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2804 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2805 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2806 | com_addbyte(c, POP_TOP); |
| 2807 | com_pop(c, 1); |
| 2808 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2809 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2810 | com_push(c, 1); |
| 2811 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2812 | if (i > 1) |
| 2813 | com_node(c, CHILD(n, 3)); |
| 2814 | com_addoparg(c, RAISE_VARARGS, i); |
| 2815 | com_pop(c, i); |
| 2816 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2817 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2818 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2819 | com_addbyte(c, POP_TOP); |
| 2820 | } |
| 2821 | |
| 2822 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2823 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2824 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2825 | int i = 1; |
| 2826 | node* stream = NULL; |
| 2827 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2828 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2829 | |
| 2830 | /* are we using the extended print form? */ |
| 2831 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2832 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2833 | com_node(c, stream); |
| 2834 | /* stack: [...] => [... stream] */ |
| 2835 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2836 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2837 | i = 4; |
| 2838 | else |
| 2839 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2840 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2841 | for (; i < NCH(n); i += 2) { |
| 2842 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2843 | com_addbyte(c, DUP_TOP); |
| 2844 | /* stack: [stream] => [stream stream] */ |
| 2845 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2846 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2847 | /* stack: [stream stream] => [stream stream obj] */ |
| 2848 | com_addbyte(c, ROT_TWO); |
| 2849 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2850 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2851 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2852 | com_pop(c, 2); |
| 2853 | } |
| 2854 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2855 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2856 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2857 | com_addbyte(c, PRINT_ITEM); |
| 2858 | com_pop(c, 1); |
| 2859 | } |
| 2860 | } |
| 2861 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2862 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2863 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2864 | /* must pop the extra stream object off the stack */ |
| 2865 | com_addbyte(c, POP_TOP); |
| 2866 | /* stack: [... stream] => [...] */ |
| 2867 | com_pop(c, 1); |
| 2868 | } |
| 2869 | } |
| 2870 | else { |
| 2871 | if (stream != NULL) { |
| 2872 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2873 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2874 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2875 | com_pop(c, 1); |
| 2876 | } |
| 2877 | else |
| 2878 | com_addbyte(c, PRINT_NEWLINE); |
| 2879 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2883 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2884 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2885 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2886 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2887 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2888 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2889 | if (c->c_flags & CO_GENERATOR) { |
| 2890 | if (NCH(n) > 1) { |
| 2891 | com_error(c, PyExc_SyntaxError, |
| 2892 | "'return' with argument inside generator"); |
| 2893 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2894 | } |
| 2895 | if (NCH(n) < 2) { |
| 2896 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2897 | com_push(c, 1); |
| 2898 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2899 | else |
| 2900 | com_node(c, CHILD(n, 1)); |
| 2901 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2902 | com_pop(c, 1); |
| 2903 | } |
| 2904 | |
| 2905 | static void |
| 2906 | com_yield_stmt(struct compiling *c, node *n) |
| 2907 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2908 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2909 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2910 | if (!c->c_infunction) { |
| 2911 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2912 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2913 | |
| 2914 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2915 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2916 | com_error(c, PyExc_SyntaxError, |
| 2917 | "'yield' not allowed in a 'try' block " |
| 2918 | "with a 'finally' clause"); |
| 2919 | return; |
| 2920 | } |
| 2921 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2922 | com_node(c, CHILD(n, 1)); |
| 2923 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2924 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2928 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2929 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2930 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2931 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2932 | if (NCH(n) > 1) { |
| 2933 | com_node(c, CHILD(n, 1)); |
| 2934 | if (NCH(n) > 3) { |
| 2935 | com_node(c, CHILD(n, 3)); |
| 2936 | if (NCH(n) > 5) |
| 2937 | com_node(c, CHILD(n, 5)); |
| 2938 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2939 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2940 | i = NCH(n)/2; |
| 2941 | com_addoparg(c, RAISE_VARARGS, i); |
| 2942 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2946 | com_from_import(struct compiling *c, node *n) |
| 2947 | { |
| 2948 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2949 | com_push(c, 1); |
| 2950 | if (NCH(n) > 1) { |
| 2951 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2952 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2953 | return; |
| 2954 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2955 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2956 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2957 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2958 | com_pop(c, 1); |
| 2959 | } |
| 2960 | |
| 2961 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2962 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2963 | { |
| 2964 | int i; |
| 2965 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2966 | /* 'import' dotted_name (',' dotted_name)* | |
| 2967 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2968 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2969 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2970 | /* 'from' dotted_name 'import' ... */ |
| 2971 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2972 | |
| 2973 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2974 | tup = Py_BuildValue("(s)", "*"); |
| 2975 | } else { |
| 2976 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2977 | for (i = 3; i < NCH(n); i += 2) { |
| 2978 | PyTuple_SET_ITEM(tup, (i-3)/2, |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 2979 | PyString_FromString(STR( |
| 2980 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2984 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2985 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2986 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2987 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2988 | com_addbyte(c, IMPORT_STAR); |
| 2989 | else { |
| 2990 | for (i = 3; i < NCH(n); i += 2) |
| 2991 | com_from_import(c, CHILD(n, i)); |
| 2992 | com_addbyte(c, POP_TOP); |
| 2993 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2994 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2995 | } |
| 2996 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2997 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2998 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2999 | node *subn = CHILD(n, i); |
| 3000 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3001 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3002 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3003 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3004 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3005 | int j; |
| 3006 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3007 | com_error(c, PyExc_SyntaxError, |
| 3008 | "invalid syntax"); |
| 3009 | return; |
| 3010 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3011 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3012 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3013 | CHILD(CHILD(subn, 0), |
| 3014 | j)); |
| 3015 | com_addop_varname(c, VAR_STORE, |
| 3016 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3017 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3018 | com_addop_varname(c, VAR_STORE, |
| 3019 | STR(CHILD(CHILD(subn, 0), |
| 3020 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3021 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3027 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3028 | { |
| 3029 | REQ(n, exec_stmt); |
| 3030 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3031 | com_node(c, CHILD(n, 1)); |
| 3032 | if (NCH(n) >= 4) |
| 3033 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3034 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3035 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3036 | com_push(c, 1); |
| 3037 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3038 | if (NCH(n) >= 6) |
| 3039 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3040 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3041 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3042 | com_push(c, 1); |
| 3043 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3044 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3045 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3048 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3049 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3050 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3051 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3052 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3053 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3054 | |
| 3055 | /* Label to avoid tail recursion */ |
| 3056 | next: |
| 3057 | switch (TYPE(n)) { |
| 3058 | |
| 3059 | case suite: |
| 3060 | if (NCH(n) == 1) { |
| 3061 | n = CHILD(n, 0); |
| 3062 | goto next; |
| 3063 | } |
| 3064 | /* Fall through */ |
| 3065 | case file_input: |
| 3066 | for (i = 0; i < NCH(n); i++) { |
| 3067 | node *ch = CHILD(n, i); |
| 3068 | if (TYPE(ch) == stmt) { |
| 3069 | n = ch; |
| 3070 | goto next; |
| 3071 | } |
| 3072 | } |
| 3073 | break; |
| 3074 | |
| 3075 | case stmt: |
| 3076 | case simple_stmt: |
| 3077 | case small_stmt: |
| 3078 | n = CHILD(n, 0); |
| 3079 | goto next; |
| 3080 | |
| 3081 | case expr_stmt: |
| 3082 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3083 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3084 | case test: |
| 3085 | case and_test: |
| 3086 | case not_test: |
| 3087 | case comparison: |
| 3088 | case expr: |
| 3089 | case xor_expr: |
| 3090 | case and_expr: |
| 3091 | case shift_expr: |
| 3092 | case arith_expr: |
| 3093 | case term: |
| 3094 | case factor: |
| 3095 | case power: |
| 3096 | case atom: |
| 3097 | if (NCH(n) == 1) { |
| 3098 | n = CHILD(n, 0); |
| 3099 | goto next; |
| 3100 | } |
| 3101 | break; |
| 3102 | |
| 3103 | case NAME: |
| 3104 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3105 | return 1; |
| 3106 | break; |
| 3107 | |
| 3108 | case NUMBER: |
| 3109 | v = parsenumber(c, STR(n)); |
| 3110 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3111 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3112 | break; |
| 3113 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3114 | i = PyObject_IsTrue(v); |
| 3115 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3116 | return i == 0; |
| 3117 | |
| 3118 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3119 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3120 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3121 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3122 | break; |
| 3123 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3124 | i = PyObject_IsTrue(v); |
| 3125 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3126 | return i == 0; |
| 3127 | |
| 3128 | } |
| 3129 | return 0; |
| 3130 | } |
| 3131 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3132 | |
| 3133 | /* Look under n for a return stmt with an expression. |
| 3134 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3135 | * functions already known to be generators (as determined by the symtable |
| 3136 | * pass). |
| 3137 | * Return the offending return node if found, else NULL. |
| 3138 | */ |
| 3139 | static node * |
| 3140 | look_for_offending_return(node *n) |
| 3141 | { |
| 3142 | int i; |
| 3143 | |
| 3144 | for (i = 0; i < NCH(n); ++i) { |
| 3145 | node *kid = CHILD(n, i); |
| 3146 | |
| 3147 | switch (TYPE(kid)) { |
| 3148 | case classdef: |
| 3149 | case funcdef: |
| 3150 | case lambdef: |
| 3151 | /* Stuff in nested functions & classes doesn't |
| 3152 | affect the code block we started in. */ |
| 3153 | return NULL; |
| 3154 | |
| 3155 | case return_stmt: |
| 3156 | if (NCH(kid) > 1) |
| 3157 | return kid; |
| 3158 | break; |
| 3159 | |
| 3160 | default: { |
| 3161 | node *bad = look_for_offending_return(kid); |
| 3162 | if (bad != NULL) |
| 3163 | return bad; |
| 3164 | } |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | return NULL; |
| 3169 | } |
| 3170 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3171 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3172 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3173 | { |
| 3174 | int i; |
| 3175 | int anchor = 0; |
| 3176 | REQ(n, if_stmt); |
| 3177 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3178 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3179 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3180 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3181 | if (is_constant_false(c, ch)) { |
| 3182 | /* We're going to skip this block. However, if this |
| 3183 | is a generator, we have to check the dead code |
| 3184 | anyway to make sure there aren't any return stmts |
| 3185 | with expressions, in the same scope. */ |
| 3186 | if (c->c_flags & CO_GENERATOR) { |
| 3187 | node *p = look_for_offending_return(n); |
| 3188 | if (p != NULL) { |
| 3189 | int savelineno = c->c_lineno; |
| 3190 | c->c_lineno = p->n_lineno; |
| 3191 | com_error(c, PyExc_SyntaxError, |
| 3192 | "'return' with argument " |
| 3193 | "inside generator"); |
| 3194 | c->c_lineno = savelineno; |
| 3195 | } |
| 3196 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3197 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3198 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3199 | if (i > 0) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3200 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3201 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3202 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3203 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3204 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3205 | com_node(c, CHILD(n, i+3)); |
| 3206 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3207 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3208 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3209 | com_addbyte(c, POP_TOP); |
| 3210 | } |
| 3211 | if (i+2 < NCH(n)) |
| 3212 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3213 | if (anchor) |
| 3214 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3218 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3219 | { |
| 3220 | int break_anchor = 0; |
| 3221 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3222 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3223 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3224 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3225 | block_push(c, SETUP_LOOP); |
| 3226 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3227 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3228 | com_node(c, CHILD(n, 1)); |
| 3229 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3230 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3231 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3232 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3233 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3234 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3235 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3236 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3237 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3238 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3239 | com_addbyte(c, POP_TOP); |
| 3240 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3241 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3242 | if (NCH(n) > 4) |
| 3243 | com_node(c, CHILD(n, 6)); |
| 3244 | com_backpatch(c, break_anchor); |
| 3245 | } |
| 3246 | |
| 3247 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3248 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3249 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3250 | int break_anchor = 0; |
| 3251 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3252 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3253 | REQ(n, for_stmt); |
| 3254 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3255 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3256 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3257 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3258 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3259 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3260 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3261 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3262 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3263 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3264 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3265 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3266 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3267 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3268 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3269 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3270 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3271 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3272 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3273 | if (NCH(n) > 8) |
| 3274 | com_node(c, CHILD(n, 8)); |
| 3275 | com_backpatch(c, break_anchor); |
| 3276 | } |
| 3277 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3278 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3279 | |
| 3280 | SETUP_FINALLY L |
| 3281 | <code for S> |
| 3282 | POP_BLOCK |
| 3283 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3284 | L: <code for Sf> |
| 3285 | END_FINALLY |
| 3286 | |
| 3287 | The special instructions use the block stack. Each block |
| 3288 | stack entry contains the instruction that created it (here |
| 3289 | SETUP_FINALLY), the level of the value stack at the time the |
| 3290 | block stack entry was created, and a label (here L). |
| 3291 | |
| 3292 | SETUP_FINALLY: |
| 3293 | Pushes the current value stack level and the label |
| 3294 | onto the block stack. |
| 3295 | POP_BLOCK: |
| 3296 | Pops en entry from the block stack, and pops the value |
| 3297 | stack until its level is the same as indicated on the |
| 3298 | block stack. (The label is ignored.) |
| 3299 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3300 | Pops a variable number of entries from the *value* stack |
| 3301 | and re-raises the exception they specify. The number of |
| 3302 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3303 | |
| 3304 | The block stack is unwound when an exception is raised: |
| 3305 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3306 | onto the value stack (and the exception condition is cleared), |
| 3307 | and the interpreter jumps to the label gotten from the block |
| 3308 | stack. |
| 3309 | |
| 3310 | 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] | 3311 | (The contents of the value stack is shown in [], with the top |
| 3312 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3313 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3314 | |
| 3315 | Value stack Label Instruction Argument |
| 3316 | [] SETUP_EXCEPT L1 |
| 3317 | [] <code for S> |
| 3318 | [] POP_BLOCK |
| 3319 | [] JUMP_FORWARD L0 |
| 3320 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3321 | [tb, val, exc] L1: DUP ) |
| 3322 | [tb, val, exc, exc] <evaluate E1> ) |
| 3323 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3324 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3325 | [tb, val, exc, 1] POP ) |
| 3326 | [tb, val, exc] POP |
| 3327 | [tb, val] <assign to V1> (or POP if no V1) |
| 3328 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3329 | [] <code for S1> |
| 3330 | JUMP_FORWARD L0 |
| 3331 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3332 | [tb, val, exc, 0] L2: POP |
| 3333 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3334 | .............................etc....................... |
| 3335 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3336 | [tb, val, exc, 0] Ln+1: POP |
| 3337 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3338 | |
| 3339 | [] L0: <next statement> |
| 3340 | |
| 3341 | Of course, parts are not generated if Vi or Ei is not present. |
| 3342 | */ |
| 3343 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3344 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3345 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3346 | { |
| 3347 | int except_anchor = 0; |
| 3348 | int end_anchor = 0; |
| 3349 | int else_anchor = 0; |
| 3350 | int i; |
| 3351 | node *ch; |
| 3352 | |
| 3353 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3354 | block_push(c, SETUP_EXCEPT); |
| 3355 | com_node(c, CHILD(n, 2)); |
| 3356 | com_addbyte(c, POP_BLOCK); |
| 3357 | block_pop(c, SETUP_EXCEPT); |
| 3358 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3359 | com_backpatch(c, except_anchor); |
| 3360 | for (i = 3; |
| 3361 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3362 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3363 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3364 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3365 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3366 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3367 | break; |
| 3368 | } |
| 3369 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3370 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3371 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3372 | if (NCH(ch) > 1) { |
| 3373 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3374 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3375 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3376 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3377 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3378 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3379 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3380 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3381 | } |
| 3382 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3383 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3384 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3385 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3386 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3387 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3388 | com_pop(c, 1); |
| 3389 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3390 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3391 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3392 | com_node(c, CHILD(n, i+2)); |
| 3393 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3394 | if (except_anchor) { |
| 3395 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3396 | /* We come in with [tb, val, exc, 0] on the |
| 3397 | stack; one pop and it's the same as |
| 3398 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3399 | com_addbyte(c, POP_TOP); |
| 3400 | } |
| 3401 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3402 | /* We actually come in here with [tb, val, exc] but the |
| 3403 | END_FINALLY will zap those and jump around. |
| 3404 | The c_stacklevel does not reflect them so we need not pop |
| 3405 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3406 | com_addbyte(c, END_FINALLY); |
| 3407 | com_backpatch(c, else_anchor); |
| 3408 | if (i < NCH(n)) |
| 3409 | com_node(c, CHILD(n, i+2)); |
| 3410 | com_backpatch(c, end_anchor); |
| 3411 | } |
| 3412 | |
| 3413 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3414 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3415 | { |
| 3416 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3417 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3418 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3419 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3420 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3421 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3422 | com_addbyte(c, POP_BLOCK); |
| 3423 | block_pop(c, SETUP_FINALLY); |
| 3424 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3425 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3426 | /* While the generated code pushes only one item, |
| 3427 | the try-finally handling can enter here with |
| 3428 | up to three items. OK, here are the details: |
| 3429 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3430 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3431 | com_backpatch(c, finally_anchor); |
| 3432 | ch = CHILD(n, NCH(n)-1); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3433 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3434 | com_node(c, ch); |
| 3435 | com_addbyte(c, END_FINALLY); |
| 3436 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3437 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3441 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3442 | { |
| 3443 | REQ(n, try_stmt); |
| 3444 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3445 | | 'try' ':' suite 'finally' ':' suite */ |
| 3446 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3447 | com_try_finally(c, n); |
| 3448 | else |
| 3449 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3452 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3453 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3454 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3455 | int i; |
| 3456 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3457 | /* Label to avoid tail recursion */ |
| 3458 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3459 | switch (TYPE(n)) { |
| 3460 | |
| 3461 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3462 | if (NCH(n) == 1) { |
| 3463 | n = CHILD(n, 0); |
| 3464 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3465 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3466 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3467 | case file_input: |
| 3468 | for (i = 0; i < NCH(n); i++) { |
| 3469 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3470 | if (TYPE(ch) == stmt) { |
| 3471 | n = ch; |
| 3472 | goto next; |
| 3473 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3474 | } |
| 3475 | break; |
| 3476 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3477 | case stmt: |
| 3478 | case simple_stmt: |
| 3479 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3480 | n = CHILD(n, 0); |
| 3481 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3482 | |
| 3483 | case expr_stmt: |
| 3484 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3485 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3486 | case test: |
| 3487 | case and_test: |
| 3488 | case not_test: |
| 3489 | case comparison: |
| 3490 | case expr: |
| 3491 | case xor_expr: |
| 3492 | case and_expr: |
| 3493 | case shift_expr: |
| 3494 | case arith_expr: |
| 3495 | case term: |
| 3496 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3497 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3498 | if (NCH(n) == 1) { |
| 3499 | n = CHILD(n, 0); |
| 3500 | goto next; |
| 3501 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | |
| 3504 | case atom: |
| 3505 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3506 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3507 | break; |
| 3508 | |
| 3509 | } |
| 3510 | return NULL; |
| 3511 | } |
| 3512 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3513 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3514 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3515 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3516 | /* Don't generate doc-strings if run with -OO */ |
| 3517 | if (Py_OptimizeFlag > 1) |
| 3518 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3519 | n = get_rawdocstring(n); |
| 3520 | if (n == NULL) |
| 3521 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3522 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3525 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3526 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3527 | { |
| 3528 | REQ(n, suite); |
| 3529 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3530 | if (NCH(n) == 1) { |
| 3531 | com_node(c, CHILD(n, 0)); |
| 3532 | } |
| 3533 | else { |
| 3534 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3535 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3536 | node *ch = CHILD(n, i); |
| 3537 | if (TYPE(ch) == stmt) |
| 3538 | com_node(c, ch); |
| 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3543 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3544 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3545 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3546 | { |
| 3547 | int i = c->c_nblocks; |
| 3548 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3549 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3550 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3551 | else if (i <= 0) { |
| 3552 | /* at the outer level */ |
| 3553 | com_error(c, PyExc_SyntaxError, |
| 3554 | "'continue' not properly in loop"); |
| 3555 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3556 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3557 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3558 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3559 | if (c->c_block[j] == SETUP_LOOP) |
| 3560 | break; |
| 3561 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3562 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3563 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3564 | for (; i > j; --i) { |
| 3565 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3566 | c->c_block[i] == SETUP_FINALLY) { |
| 3567 | com_addoparg(c, CONTINUE_LOOP, |
| 3568 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3571 | if (c->c_block[i] == END_FINALLY) { |
| 3572 | com_error(c, PyExc_SyntaxError, |
| 3573 | "'continue' not supported inside 'finally' clause"); |
| 3574 | return; |
| 3575 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3576 | } |
| 3577 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3578 | com_error(c, PyExc_SyntaxError, |
| 3579 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3580 | } |
| 3581 | /* XXX Could allow it inside a 'finally' clause |
| 3582 | XXX if we could pop the exception still on the stack */ |
| 3583 | } |
| 3584 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3585 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3586 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3587 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3588 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3589 | if (TYPE(n) == lambdef) { |
| 3590 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3591 | n = CHILD(n, 1); |
| 3592 | } |
| 3593 | else { |
| 3594 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3595 | n = CHILD(n, 2); |
| 3596 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3597 | n = CHILD(n, 1); |
| 3598 | } |
| 3599 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3600 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3601 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3602 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3603 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3604 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3605 | nargs = 0; |
| 3606 | ndefs = 0; |
| 3607 | for (i = 0; i < nch; i++) { |
| 3608 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3609 | if (TYPE(CHILD(n, i)) == STAR || |
| 3610 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3611 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3612 | nargs++; |
| 3613 | i++; |
| 3614 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3615 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3616 | else |
| 3617 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3618 | if (t == EQUAL) { |
| 3619 | i++; |
| 3620 | ndefs++; |
| 3621 | com_node(c, CHILD(n, i)); |
| 3622 | i++; |
| 3623 | if (i >= nch) |
| 3624 | break; |
| 3625 | t = TYPE(CHILD(n, i)); |
| 3626 | } |
| 3627 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3628 | /* Treat "(a=1, b)" as an error */ |
| 3629 | if (ndefs) |
| 3630 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3631 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3632 | } |
| 3633 | if (t != COMMA) |
| 3634 | break; |
| 3635 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3636 | return ndefs; |
| 3637 | } |
| 3638 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3639 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3640 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3641 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3642 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3643 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3644 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3645 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3646 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3647 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3648 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3649 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3650 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3651 | c->c_errors++; |
| 3652 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3653 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3654 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3655 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3656 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3657 | if (closure) |
| 3658 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3659 | else |
| 3660 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3661 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3662 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3663 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3664 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3669 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3670 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3671 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3672 | REQ(n, testlist); |
| 3673 | /* testlist: test (',' test)* [','] */ |
| 3674 | for (i = 0; i < NCH(n); i += 2) |
| 3675 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3676 | i = (NCH(n)+1) / 2; |
| 3677 | com_addoparg(c, BUILD_TUPLE, i); |
| 3678 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3682 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3683 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3684 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3685 | PyObject *v; |
| 3686 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3687 | char *name; |
| 3688 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3689 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3690 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3691 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3692 | c->c_errors++; |
| 3693 | return; |
| 3694 | } |
| 3695 | /* Push the class name on the stack */ |
| 3696 | i = com_addconst(c, v); |
| 3697 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3698 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3699 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3700 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3701 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3702 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3703 | com_push(c, 1); |
| 3704 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3705 | else |
| 3706 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3707 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3708 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3709 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3710 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3711 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3712 | c->c_errors++; |
| 3713 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3714 | int closure = com_make_closure(c, co); |
| 3715 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3716 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3717 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3718 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3719 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3720 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3721 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3722 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3723 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3724 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3725 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3726 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3727 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3728 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3729 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
| 3732 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3733 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3734 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3735 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3736 | if (c->c_errors) |
| 3737 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3738 | switch (TYPE(n)) { |
| 3739 | |
| 3740 | /* Definition nodes */ |
| 3741 | |
| 3742 | case funcdef: |
| 3743 | com_funcdef(c, n); |
| 3744 | break; |
| 3745 | case classdef: |
| 3746 | com_classdef(c, n); |
| 3747 | break; |
| 3748 | |
| 3749 | /* Trivial parse tree nodes */ |
| 3750 | |
| 3751 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3752 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3753 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3754 | n = CHILD(n, 0); |
| 3755 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3756 | |
| 3757 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3758 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3759 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3760 | { |
| 3761 | int i; |
| 3762 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3763 | com_node(c, CHILD(n, i)); |
| 3764 | } |
| 3765 | break; |
| 3766 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3767 | case compound_stmt: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3768 | com_set_lineno(c, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3769 | n = CHILD(n, 0); |
| 3770 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3771 | |
| 3772 | /* Statement nodes */ |
| 3773 | |
| 3774 | case expr_stmt: |
| 3775 | com_expr_stmt(c, n); |
| 3776 | break; |
| 3777 | case print_stmt: |
| 3778 | com_print_stmt(c, n); |
| 3779 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3780 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3781 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3782 | break; |
| 3783 | case pass_stmt: |
| 3784 | break; |
| 3785 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3786 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3787 | com_error(c, PyExc_SyntaxError, |
| 3788 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3789 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3790 | com_addbyte(c, BREAK_LOOP); |
| 3791 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3792 | case continue_stmt: |
| 3793 | com_continue_stmt(c, n); |
| 3794 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3795 | case return_stmt: |
| 3796 | com_return_stmt(c, n); |
| 3797 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3798 | case yield_stmt: |
| 3799 | com_yield_stmt(c, n); |
| 3800 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3801 | case raise_stmt: |
| 3802 | com_raise_stmt(c, n); |
| 3803 | break; |
| 3804 | case import_stmt: |
| 3805 | com_import_stmt(c, n); |
| 3806 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3807 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3808 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3809 | case exec_stmt: |
| 3810 | com_exec_stmt(c, n); |
| 3811 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3812 | case assert_stmt: |
| 3813 | com_assert_stmt(c, n); |
| 3814 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3815 | case if_stmt: |
| 3816 | com_if_stmt(c, n); |
| 3817 | break; |
| 3818 | case while_stmt: |
| 3819 | com_while_stmt(c, n); |
| 3820 | break; |
| 3821 | case for_stmt: |
| 3822 | com_for_stmt(c, n); |
| 3823 | break; |
| 3824 | case try_stmt: |
| 3825 | com_try_stmt(c, n); |
| 3826 | break; |
| 3827 | case suite: |
| 3828 | com_suite(c, n); |
| 3829 | break; |
| 3830 | |
| 3831 | /* Expression nodes */ |
| 3832 | |
| 3833 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3834 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3835 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3836 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3837 | break; |
| 3838 | case test: |
| 3839 | com_test(c, n); |
| 3840 | break; |
| 3841 | case and_test: |
| 3842 | com_and_test(c, n); |
| 3843 | break; |
| 3844 | case not_test: |
| 3845 | com_not_test(c, n); |
| 3846 | break; |
| 3847 | case comparison: |
| 3848 | com_comparison(c, n); |
| 3849 | break; |
| 3850 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3851 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3852 | break; |
| 3853 | case expr: |
| 3854 | com_expr(c, n); |
| 3855 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3856 | case xor_expr: |
| 3857 | com_xor_expr(c, n); |
| 3858 | break; |
| 3859 | case and_expr: |
| 3860 | com_and_expr(c, n); |
| 3861 | break; |
| 3862 | case shift_expr: |
| 3863 | com_shift_expr(c, n); |
| 3864 | break; |
| 3865 | case arith_expr: |
| 3866 | com_arith_expr(c, n); |
| 3867 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3868 | case term: |
| 3869 | com_term(c, n); |
| 3870 | break; |
| 3871 | case factor: |
| 3872 | com_factor(c, n); |
| 3873 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3874 | case power: |
| 3875 | com_power(c, n); |
| 3876 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3877 | case atom: |
| 3878 | com_atom(c, n); |
| 3879 | break; |
| 3880 | |
| 3881 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3882 | com_error(c, PyExc_SystemError, |
| 3883 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3887 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3888 | |
| 3889 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3890 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3891 | { |
| 3892 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3893 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3894 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3895 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3896 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3897 | com_pop(c, 1); |
| 3898 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3902 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3903 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3904 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3905 | if (NCH(n) == 1) { |
| 3906 | com_fpdef(c, CHILD(n, 0)); |
| 3907 | } |
| 3908 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3909 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3910 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3911 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3912 | for (i = 0; i < NCH(n); i += 2) |
| 3913 | com_fpdef(c, CHILD(n, i)); |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3918 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3919 | { |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3920 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3921 | int complex = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3922 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3923 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3924 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3925 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3926 | nch = NCH(n); |
| 3927 | /* Enter all arguments in table of locals */ |
| 3928 | for (i = 0, narg = 0; i < nch; i++) { |
| 3929 | node *ch = CHILD(n, i); |
| 3930 | node *fp; |
| 3931 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3932 | break; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3933 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3934 | fp = CHILD(ch, 0); |
| 3935 | if (TYPE(fp) != NAME) { |
| 3936 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
| 3937 | complex = 1; |
Guido van Rossum | 9c8a086 | 2002-12-23 16:35:23 +0000 | [diff] [blame] | 3938 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3939 | narg++; |
| 3940 | /* all name updates handled by symtable */ |
| 3941 | if (++i >= nch) |
| 3942 | break; |
| 3943 | ch = CHILD(n, i); |
| 3944 | if (TYPE(ch) == EQUAL) |
| 3945 | i += 2; |
| 3946 | else |
| 3947 | REQ(ch, COMMA); |
| 3948 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3949 | if (complex) { |
| 3950 | /* Generate code for complex arguments only after |
| 3951 | having counted the simple arguments */ |
| 3952 | int ilocal = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3953 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3954 | node *ch = CHILD(n, i); |
| 3955 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3956 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3957 | break; |
| 3958 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3959 | fp = CHILD(ch, 0); |
| 3960 | if (TYPE(fp) != NAME) { |
| 3961 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3962 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3963 | com_fpdef(c, ch); |
| 3964 | } |
| 3965 | ilocal++; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3966 | if (++i >= nch) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3967 | break; |
| 3968 | ch = CHILD(n, i); |
| 3969 | if (TYPE(ch) == EQUAL) |
| 3970 | i += 2; |
| 3971 | else |
| 3972 | REQ(ch, COMMA); |
| 3973 | } |
| 3974 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3978 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3979 | { |
| 3980 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3981 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3982 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3983 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3984 | if (doc != NULL) { |
| 3985 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3986 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3987 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3988 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3989 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3990 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3991 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3992 | for (i = 0; i < NCH(n); i++) { |
| 3993 | node *ch = CHILD(n, i); |
| 3994 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3995 | com_node(c, ch); |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | /* Top-level compile-node interface */ |
| 4000 | |
| 4001 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4002 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4003 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4004 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4005 | node *ch; |
| 4006 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4007 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4008 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4009 | if (doc != NULL) { |
| 4010 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4011 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4012 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4013 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4014 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4015 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4016 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4017 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4018 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4019 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4020 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4021 | c->c_infunction = 0; |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4022 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4023 | com_push(c, 1); |
| 4024 | com_addbyte(c, RETURN_VALUE); |
| 4025 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4029 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4030 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4031 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4032 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4033 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4034 | |
| 4035 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4036 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4037 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4038 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4039 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4040 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4041 | else |
| 4042 | ch = CHILD(n, 2); |
| 4043 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4044 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4045 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4049 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4050 | { |
| 4051 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4052 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4053 | REQ(n, classdef); |
| 4054 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4055 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4056 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4057 | /* Initialize local __module__ from global __name__ */ |
| 4058 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4059 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4060 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4061 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4062 | if (doc != NULL) { |
| 4063 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4064 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4065 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4066 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4067 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4068 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4069 | } |
| 4070 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4071 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4072 | com_node(c, ch); |
| 4073 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4074 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4075 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4076 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4080 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4081 | { |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4082 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4083 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4084 | switch (TYPE(n)) { |
| 4085 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4086 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4087 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4088 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4089 | n = CHILD(n, 0); |
| 4090 | if (TYPE(n) != NEWLINE) |
| 4091 | com_node(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4092 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4093 | com_push(c, 1); |
| 4094 | com_addbyte(c, RETURN_VALUE); |
| 4095 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4096 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4097 | break; |
| 4098 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4099 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4100 | com_file_input(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4101 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4102 | com_push(c, 1); |
| 4103 | com_addbyte(c, RETURN_VALUE); |
| 4104 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4105 | break; |
| 4106 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4107 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4108 | com_node(c, CHILD(n, 0)); |
| 4109 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4110 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4111 | break; |
| 4112 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4113 | case lambdef: /* anonymous function definition */ |
| 4114 | compile_lambdef(c, n); |
| 4115 | break; |
| 4116 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4117 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4118 | compile_funcdef(c, n); |
| 4119 | break; |
| 4120 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4121 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4122 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4123 | break; |
| 4124 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4125 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4126 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4127 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4128 | } |
| 4129 | } |
| 4130 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4131 | static PyObject * |
| 4132 | dict_keys_inorder(PyObject *dict, int offset) |
| 4133 | { |
| 4134 | PyObject *tuple, *k, *v; |
| 4135 | int i, pos = 0, size = PyDict_Size(dict); |
| 4136 | |
| 4137 | tuple = PyTuple_New(size); |
| 4138 | if (tuple == NULL) |
| 4139 | return NULL; |
| 4140 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4141 | i = PyInt_AS_LONG(v); |
| 4142 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4143 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4144 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4145 | } |
| 4146 | return tuple; |
| 4147 | } |
| 4148 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4149 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4150 | PyNode_Compile(node *n, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4151 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4152 | return PyNode_CompileFlags(n, filename, NULL); |
| 4153 | } |
| 4154 | |
| 4155 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4156 | PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4157 | { |
| 4158 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4161 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4162 | PyNode_CompileSymtable(node *n, const char *filename) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4163 | { |
| 4164 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4165 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4166 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4167 | ff = PyNode_Future(n, filename); |
| 4168 | if (ff == NULL) |
| 4169 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4170 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4171 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4172 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4173 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4174 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4175 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4176 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4177 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4178 | if (st->st_errors > 0) |
| 4179 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4180 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4181 | if (st->st_errors > 0) |
| 4182 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4183 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4184 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4185 | fail: |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4186 | PyObject_FREE((void *)ff); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4187 | st->st_future = NULL; |
| 4188 | PySymtable_Free(st); |
| 4189 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4190 | } |
| 4191 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4192 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4193 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4194 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4195 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4198 | static PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4199 | jcompile(node *n, const char *filename, struct compiling *base, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4200 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4201 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4202 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4203 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4204 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4205 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4206 | if (TYPE(n) == encoding_decl) { |
| 4207 | sc.c_encoding = STR(n); |
| 4208 | n = CHILD(n, 0); |
| 4209 | } else { |
| 4210 | sc.c_encoding = NULL; |
| 4211 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4212 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4213 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4214 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4215 | /* c_symtable still points to parent's symbols */ |
| 4216 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4217 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4218 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4219 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4220 | if (base->c_encoding != NULL) { |
| 4221 | assert(sc.c_encoding == NULL); |
| 4222 | sc.c_encoding = base->c_encoding; |
| 4223 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4224 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4225 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4226 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4227 | if (sc.c_future == NULL) { |
| 4228 | com_free(&sc); |
| 4229 | return NULL; |
| 4230 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4231 | if (flags) { |
| 4232 | int merged = sc.c_future->ff_features | |
| 4233 | flags->cf_flags; |
| 4234 | sc.c_future->ff_features = merged; |
| 4235 | flags->cf_flags = merged; |
| 4236 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4237 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4238 | com_free(&sc); |
| 4239 | return NULL; |
| 4240 | } |
| 4241 | } |
| 4242 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4243 | if (symtable_load_symbols(&sc) < 0) { |
| 4244 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4245 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4246 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4247 | compile_node(&sc, n); |
| 4248 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4249 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4250 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4251 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4252 | consts = PyList_AsTuple(sc.c_consts); |
| 4253 | names = PyList_AsTuple(sc.c_names); |
| 4254 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4255 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4256 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4257 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4258 | filename = PyString_InternFromString(sc.c_filename); |
| 4259 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4260 | if (!PyErr_Occurred()) |
| 4261 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4262 | sc.c_nlocals, |
| 4263 | sc.c_maxstacklevel, |
| 4264 | sc.c_flags, |
| 4265 | sc.c_code, |
| 4266 | consts, |
| 4267 | names, |
| 4268 | varnames, |
| 4269 | freevars, |
| 4270 | cellvars, |
| 4271 | filename, |
| 4272 | name, |
| 4273 | sc.c_firstlineno, |
| 4274 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4275 | Py_XDECREF(consts); |
| 4276 | Py_XDECREF(names); |
| 4277 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4278 | Py_XDECREF(freevars); |
| 4279 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4280 | Py_XDECREF(filename); |
| 4281 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4282 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4283 | else if (!PyErr_Occurred()) { |
| 4284 | /* This could happen if someone called PyErr_Clear() after an |
| 4285 | error was reported above. That's not supposed to happen, |
| 4286 | but I just plugged one case and I'm not sure there can't be |
| 4287 | others. In that case, raise SystemError so that at least |
| 4288 | it gets reported instead dumping core. */ |
| 4289 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4290 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4291 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4292 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4293 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4294 | sc.c_symtable = NULL; |
| 4295 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4296 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4297 | return co; |
| 4298 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4299 | |
| 4300 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4301 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4302 | { |
| 4303 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4304 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4305 | int line = co->co_firstlineno; |
| 4306 | int addr = 0; |
| 4307 | while (--size >= 0) { |
| 4308 | addr += *p++; |
| 4309 | if (addr > addrq) |
| 4310 | break; |
| 4311 | line += *p++; |
| 4312 | } |
| 4313 | return line; |
| 4314 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4315 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4316 | /* The test for LOCAL must come before the test for FREE in order to |
| 4317 | handle classes where name is both local and free. The local var is |
| 4318 | a method and the free var is a free var referenced within a method. |
| 4319 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4320 | |
| 4321 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4322 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4323 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4324 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4325 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4326 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4327 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4328 | return CELL; |
| 4329 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4330 | return LOCAL; |
| 4331 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4332 | return FREE; |
| 4333 | v = PyDict_GetItemString(c->c_globals, name); |
| 4334 | if (v) { |
| 4335 | if (v == Py_None) |
| 4336 | return GLOBAL_EXPLICIT; |
| 4337 | else { |
| 4338 | return GLOBAL_IMPLICIT; |
| 4339 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4340 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4341 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4342 | "unknown scope for %.100s in %.100s(%s) " |
| 4343 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4344 | name, c->c_name, |
| 4345 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4346 | c->c_filename, |
| 4347 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4348 | PyObject_REPR(c->c_locals), |
| 4349 | PyObject_REPR(c->c_globals) |
| 4350 | ); |
| 4351 | |
| 4352 | Py_FatalError(buf); |
| 4353 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4356 | /* Helper functions to issue warnings */ |
| 4357 | |
| 4358 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4359 | issue_warning(const char *msg, const char *filename, int lineno) |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4360 | { |
| 4361 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4362 | lineno, NULL, NULL) < 0) { |
| 4363 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4364 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4365 | PyErr_SyntaxLocation(filename, lineno); |
| 4366 | } |
| 4367 | return -1; |
| 4368 | } |
| 4369 | return 0; |
| 4370 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4371 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4372 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4373 | symtable_warn(struct symtable *st, char *msg) |
| 4374 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4375 | 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] | 4376 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4377 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4378 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4379 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4382 | /* Helper function for setting lineno and filename */ |
| 4383 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4384 | static int |
| 4385 | symtable_build(struct compiling *c, node *n) |
| 4386 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4387 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4388 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4389 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4390 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4391 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4392 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4393 | return -1; |
| 4394 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4395 | if (c->c_symtable->st_errors > 0) |
| 4396 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4397 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4398 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4399 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4400 | return 0; |
| 4401 | } |
| 4402 | |
| 4403 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4404 | symtable_init_compiling_symbols(struct compiling *c) |
| 4405 | { |
| 4406 | PyObject *varnames; |
| 4407 | |
| 4408 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4409 | if (varnames == NULL) { |
| 4410 | varnames = PyList_New(0); |
| 4411 | if (varnames == NULL) |
| 4412 | return -1; |
| 4413 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4414 | Py_INCREF(varnames); |
| 4415 | } else |
| 4416 | Py_INCREF(varnames); |
| 4417 | c->c_varnames = varnames; |
| 4418 | |
| 4419 | c->c_globals = PyDict_New(); |
| 4420 | if (c->c_globals == NULL) |
| 4421 | return -1; |
| 4422 | c->c_freevars = PyDict_New(); |
| 4423 | if (c->c_freevars == NULL) |
| 4424 | return -1; |
| 4425 | c->c_cellvars = PyDict_New(); |
| 4426 | if (c->c_cellvars == NULL) |
| 4427 | return -1; |
| 4428 | return 0; |
| 4429 | } |
| 4430 | |
| 4431 | struct symbol_info { |
| 4432 | int si_nlocals; |
| 4433 | int si_ncells; |
| 4434 | int si_nfrees; |
| 4435 | int si_nimplicit; |
| 4436 | }; |
| 4437 | |
| 4438 | static void |
| 4439 | symtable_init_info(struct symbol_info *si) |
| 4440 | { |
| 4441 | si->si_nlocals = 0; |
| 4442 | si->si_ncells = 0; |
| 4443 | si->si_nfrees = 0; |
| 4444 | si->si_nimplicit = 0; |
| 4445 | } |
| 4446 | |
| 4447 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4448 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4449 | struct symbol_info *si) |
| 4450 | { |
| 4451 | PyObject *dict, *v; |
| 4452 | |
| 4453 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4454 | it indicates a local that we must allocate storage for (a |
| 4455 | cell var). If it occurs in a class, then the class has a |
| 4456 | method and a free variable with the same name. |
| 4457 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4458 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4459 | /* If it isn't declared locally, it can't be a cell. */ |
| 4460 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4461 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4462 | v = PyInt_FromLong(si->si_ncells++); |
| 4463 | dict = c->c_cellvars; |
| 4464 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4465 | /* If it is free anyway, then there is no need to do |
| 4466 | anything here. |
| 4467 | */ |
| 4468 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4469 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4470 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4471 | v = PyInt_FromLong(si->si_nfrees++); |
| 4472 | dict = c->c_freevars; |
| 4473 | } |
| 4474 | if (v == NULL) |
| 4475 | return -1; |
| 4476 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4477 | Py_DECREF(v); |
| 4478 | return -1; |
| 4479 | } |
| 4480 | Py_DECREF(v); |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4484 | /* If a variable is a cell and an argument, make sure that appears in |
| 4485 | co_cellvars before any variable to its right in varnames. |
| 4486 | */ |
| 4487 | |
| 4488 | |
| 4489 | static int |
| 4490 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4491 | PyObject *varnames, int flags) |
| 4492 | { |
| 4493 | PyObject *v, *w, *d, *list = NULL; |
| 4494 | int i, pos; |
| 4495 | |
| 4496 | if (flags & CO_VARARGS) |
| 4497 | argcount++; |
| 4498 | if (flags & CO_VARKEYWORDS) |
| 4499 | argcount++; |
| 4500 | for (i = argcount; --i >= 0; ) { |
| 4501 | v = PyList_GET_ITEM(varnames, i); |
| 4502 | if (PyDict_GetItem(*cellvars, v)) { |
| 4503 | if (list == NULL) { |
| 4504 | list = PyList_New(1); |
| 4505 | if (list == NULL) |
| 4506 | return -1; |
| 4507 | PyList_SET_ITEM(list, 0, v); |
| 4508 | Py_INCREF(v); |
| 4509 | } else |
| 4510 | PyList_Insert(list, 0, v); |
| 4511 | } |
| 4512 | } |
| 4513 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4514 | return 0; |
| 4515 | /* There are cellvars that are also arguments. Create a dict |
| 4516 | to replace cellvars and put the args at the front. |
| 4517 | */ |
| 4518 | d = PyDict_New(); |
| 4519 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4520 | v = PyInt_FromLong(i); |
| 4521 | if (v == NULL) |
| 4522 | goto fail; |
| 4523 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4524 | goto fail; |
| 4525 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4526 | goto fail; |
| 4527 | } |
| 4528 | pos = 0; |
| 4529 | i = PyList_GET_SIZE(list); |
| 4530 | Py_DECREF(list); |
| 4531 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4532 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4533 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4534 | Py_DECREF(w); |
| 4535 | goto fail; |
| 4536 | } |
| 4537 | Py_DECREF(w); |
| 4538 | } |
| 4539 | Py_DECREF(*cellvars); |
| 4540 | *cellvars = d; |
| 4541 | return 1; |
| 4542 | fail: |
| 4543 | Py_DECREF(d); |
| 4544 | return -1; |
| 4545 | } |
| 4546 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4547 | static int |
| 4548 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4549 | { |
| 4550 | PyObject *name, *v; |
| 4551 | int pos; |
| 4552 | |
| 4553 | /* The cell vars are the first elements of the closure, |
| 4554 | followed by the free vars. Update the offsets in |
| 4555 | c_freevars to account for number of cellvars. */ |
| 4556 | pos = 0; |
| 4557 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4558 | int i = PyInt_AS_LONG(v) + offset; |
| 4559 | PyObject *o = PyInt_FromLong(i); |
| 4560 | if (o == NULL) |
| 4561 | return -1; |
| 4562 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4563 | Py_DECREF(o); |
| 4564 | return -1; |
| 4565 | } |
| 4566 | Py_DECREF(o); |
| 4567 | } |
| 4568 | return 0; |
| 4569 | } |
| 4570 | |
| 4571 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4572 | symtable_check_unoptimized(struct compiling *c, |
| 4573 | PySymtableEntryObject *ste, |
| 4574 | struct symbol_info *si) |
| 4575 | { |
| 4576 | char buf[300]; |
| 4577 | |
| 4578 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4579 | || (ste->ste_nested && si->si_nimplicit))) |
| 4580 | return 0; |
| 4581 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4582 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4583 | |
| 4584 | #define ILLEGAL_IS "is a nested function" |
| 4585 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4586 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4587 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4588 | |
| 4589 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4590 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4591 | |
| 4592 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4593 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4594 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4595 | |
| 4596 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4597 | should be stored so the exception can point to them. */ |
| 4598 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4599 | if (ste->ste_child_free) { |
| 4600 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4601 | PyOS_snprintf(buf, sizeof(buf), |
| 4602 | ILLEGAL_IMPORT_STAR, |
| 4603 | PyString_AS_STRING(ste->ste_name), |
| 4604 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4605 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4606 | PyOS_snprintf(buf, sizeof(buf), |
| 4607 | ILLEGAL_BARE_EXEC, |
| 4608 | PyString_AS_STRING(ste->ste_name), |
| 4609 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4610 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4611 | PyOS_snprintf(buf, sizeof(buf), |
| 4612 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4613 | PyString_AS_STRING(ste->ste_name), |
| 4614 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4615 | } |
| 4616 | } else { |
| 4617 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4618 | PyOS_snprintf(buf, sizeof(buf), |
| 4619 | ILLEGAL_IMPORT_STAR, |
| 4620 | PyString_AS_STRING(ste->ste_name), |
| 4621 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4622 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4623 | PyOS_snprintf(buf, sizeof(buf), |
| 4624 | ILLEGAL_BARE_EXEC, |
| 4625 | PyString_AS_STRING(ste->ste_name), |
| 4626 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4627 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4628 | PyOS_snprintf(buf, sizeof(buf), |
| 4629 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4630 | PyString_AS_STRING(ste->ste_name), |
| 4631 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4632 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4633 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4634 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4635 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4636 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4637 | ste->ste_opt_lineno); |
| 4638 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4642 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4643 | struct symbol_info *si) |
| 4644 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4645 | if (c->c_future) |
| 4646 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4647 | if (ste->ste_generator) |
| 4648 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4649 | if (ste->ste_type != TYPE_MODULE) |
| 4650 | c->c_flags |= CO_NEWLOCALS; |
| 4651 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4652 | c->c_nlocals = si->si_nlocals; |
| 4653 | if (ste->ste_optimized == 0) |
| 4654 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4655 | else if (ste->ste_optimized != OPT_EXEC) |
| 4656 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4657 | } |
| 4658 | return 0; |
| 4659 | } |
| 4660 | |
| 4661 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4662 | symtable_load_symbols(struct compiling *c) |
| 4663 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4664 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4665 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4666 | PyObject *name, *varnames, *v; |
| 4667 | int i, flags, pos; |
| 4668 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4669 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4670 | v = NULL; |
| 4671 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4672 | if (symtable_init_compiling_symbols(c) < 0) |
| 4673 | goto fail; |
| 4674 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4675 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4676 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4677 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4678 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4679 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4680 | v = PyInt_FromLong(i); |
| 4681 | if (PyDict_SetItem(c->c_locals, |
| 4682 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4683 | goto fail; |
| 4684 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | /* XXX The cases below define the rules for whether a name is |
| 4688 | local or global. The logic could probably be clearer. */ |
| 4689 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4690 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4691 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4692 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4693 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4694 | /* undo the original DEF_FREE */ |
| 4695 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4696 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4697 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4698 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4699 | 2. Free variables in methods that are also class |
| 4700 | variables or declared global. |
| 4701 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4702 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4703 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4704 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4705 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4706 | c->c_argcount--; |
| 4707 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4708 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4709 | c->c_argcount--; |
| 4710 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4711 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4712 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4713 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4714 | if (flags & DEF_PARAM) { |
| 4715 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4716 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4717 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4718 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4719 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4720 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4721 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4722 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4723 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4724 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4725 | si.si_nimplicit++; |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4726 | if (PyDict_SetItem(c->c_globals, name, Py_True) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4727 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4728 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4729 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4730 | if (v == NULL) |
| 4731 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4732 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4733 | goto fail; |
| 4734 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4735 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4736 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4737 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4738 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4739 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4740 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4741 | if (v == NULL) |
| 4742 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4743 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4744 | goto fail; |
| 4745 | Py_DECREF(v); |
| 4746 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4747 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4748 | if (PyDict_SetItem(c->c_globals, name, |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 4749 | Py_True) < 0) |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4750 | goto fail; |
| 4751 | if (st->st_nscopes != 1) { |
| 4752 | v = PyInt_FromLong(flags); |
| 4753 | if (PyDict_SetItem(st->st_global, |
| 4754 | name, v)) |
| 4755 | goto fail; |
| 4756 | Py_DECREF(v); |
| 4757 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4758 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
| 4761 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4762 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4763 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4764 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4765 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4766 | c->c_varnames, c->c_flags) < 0) |
| 4767 | return -1; |
| 4768 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4769 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4770 | return -1; |
| 4771 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4772 | fail: |
| 4773 | /* is this always the right thing to do? */ |
| 4774 | Py_XDECREF(v); |
| 4775 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4779 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4780 | { |
| 4781 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4782 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4783 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4784 | if (st == NULL) |
| 4785 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4786 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4787 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4788 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4789 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4790 | goto fail; |
| 4791 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4792 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4793 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4794 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4795 | st->st_errors = 0; |
| 4796 | st->st_tmpname = 0; |
| 4797 | st->st_private = NULL; |
| 4798 | return st; |
| 4799 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4800 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4801 | return NULL; |
| 4802 | } |
| 4803 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4804 | void |
| 4805 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4806 | { |
| 4807 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4808 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4809 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4810 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4813 | /* When the compiler exits a scope, it must should update the scope's |
| 4814 | free variable information with the list of free variables in its |
| 4815 | children. |
| 4816 | |
| 4817 | Variables that are free in children and defined in the current |
| 4818 | scope are cellvars. |
| 4819 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4820 | 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] | 4821 | false), free variables in children that are not defined here are |
| 4822 | implicit globals. |
| 4823 | |
| 4824 | */ |
| 4825 | |
| 4826 | static int |
| 4827 | symtable_update_free_vars(struct symtable *st) |
| 4828 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4829 | int i, j, def; |
| 4830 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4831 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4832 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4833 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4834 | def = DEF_FREE_CLASS; |
| 4835 | else |
| 4836 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4837 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4838 | int pos = 0; |
| 4839 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4840 | if (list) |
| 4841 | PyList_SetSlice(list, 0, |
| 4842 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4843 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4844 | PyList_GET_ITEM(ste->ste_children, i); |
| 4845 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4846 | int flags = PyInt_AS_LONG(o); |
| 4847 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4848 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4849 | if (list == NULL) { |
| 4850 | list = PyList_New(0); |
| 4851 | if (list == NULL) |
| 4852 | return -1; |
| 4853 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4854 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4855 | if (PyList_Append(list, name) < 0) { |
| 4856 | Py_DECREF(list); |
| 4857 | return -1; |
| 4858 | } |
| 4859 | } |
| 4860 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4861 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4862 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4863 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4864 | /* If a name N is declared global in scope A and |
| 4865 | referenced in scope B contained (perhaps |
| 4866 | indirectly) in A and there are no scopes |
| 4867 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4868 | is global in B. Unless A is a class scope, |
| 4869 | because class scopes are not considered for |
| 4870 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4871 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4872 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4873 | int flags = PyInt_AS_LONG(v); |
| 4874 | if (flags & DEF_GLOBAL) { |
| 4875 | symtable_undo_free(st, child->ste_id, |
| 4876 | name); |
| 4877 | continue; |
| 4878 | } |
| 4879 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4880 | if (ste->ste_nested) { |
| 4881 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4882 | name, def) < 0) { |
| 4883 | Py_DECREF(list); |
| 4884 | return -1; |
| 4885 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4886 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4887 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4888 | name) < 0) { |
| 4889 | Py_DECREF(list); |
| 4890 | return -1; |
| 4891 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4895 | |
| 4896 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4897 | return 0; |
| 4898 | } |
| 4899 | |
| 4900 | /* If the current scope is a non-nested class or if name is not |
| 4901 | defined in the current, non-nested scope, then it is an implicit |
| 4902 | global in all nested scopes. |
| 4903 | */ |
| 4904 | |
| 4905 | static int |
| 4906 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4907 | { |
| 4908 | PyObject *o; |
| 4909 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4910 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4911 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4912 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4913 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4914 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4915 | if (o == NULL) |
| 4916 | return symtable_undo_free(st, child, name); |
| 4917 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4918 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4919 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4920 | return symtable_undo_free(st, child, name); |
| 4921 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4922 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4923 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
| 4926 | static int |
| 4927 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4928 | PyObject *name) |
| 4929 | { |
| 4930 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4931 | PyObject *info; |
| 4932 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4933 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4934 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4935 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4936 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4937 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4938 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4939 | if (info == NULL) |
| 4940 | return 0; |
| 4941 | v = PyInt_AS_LONG(info); |
| 4942 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4943 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4944 | DEF_FREE_GLOBAL) < 0) |
| 4945 | return -1; |
| 4946 | } else |
| 4947 | /* If the name is defined here or declared global, |
| 4948 | then the recursion stops. */ |
| 4949 | return 0; |
| 4950 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4951 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4952 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4953 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4954 | PyList_GET_ITEM(ste->ste_children, i); |
| 4955 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4956 | if (x < 0) |
| 4957 | return x; |
| 4958 | } |
| 4959 | return 0; |
| 4960 | } |
| 4961 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4962 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4963 | This reference is released when the scope is exited, via the DECREF |
| 4964 | in symtable_exit_scope(). |
| 4965 | */ |
| 4966 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4967 | static int |
| 4968 | symtable_exit_scope(struct symtable *st) |
| 4969 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4970 | int end; |
| 4971 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4972 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4973 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4974 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4975 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4976 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4977 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4978 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4979 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4980 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4981 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4982 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4983 | static void |
| 4984 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4985 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4986 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4987 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4988 | |
| 4989 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4990 | prev = st->st_cur; |
| 4991 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4992 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4993 | st->st_errors++; |
| 4994 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4995 | } |
| 4996 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4997 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4998 | PySymtableEntry_New(st, name, type, lineno); |
| 4999 | if (strcmp(name, TOP) == 0) |
| 5000 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5001 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5002 | if (PyList_Append(prev->ste_children, |
| 5003 | (PyObject *)st->st_cur) < 0) |
| 5004 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5005 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5009 | symtable_lookup(struct symtable *st, char *name) |
| 5010 | { |
| 5011 | char buffer[MANGLE_LEN]; |
| 5012 | PyObject *v; |
| 5013 | int flags; |
| 5014 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5015 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5016 | name = buffer; |
| 5017 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5018 | if (v == NULL) { |
| 5019 | if (PyErr_Occurred()) |
| 5020 | return -1; |
| 5021 | else |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
| 5025 | flags = PyInt_AS_LONG(v); |
| 5026 | return flags; |
| 5027 | } |
| 5028 | |
| 5029 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5030 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5031 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5032 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5033 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5034 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5035 | |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5036 | /* Warn about None, except inside a tuple (where the assignment |
| 5037 | code already issues a warning). */ |
| 5038 | if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) && |
| 5039 | *name == 'N' && strcmp(name, "None") == 0) |
| 5040 | { |
| 5041 | if (symtable_warn(st, "argument named None")) |
| 5042 | return -1; |
| 5043 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5044 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5045 | name = buffer; |
| 5046 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5047 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5048 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5049 | Py_DECREF(s); |
| 5050 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5051 | } |
| 5052 | |
| 5053 | /* Must only be called with mangled names */ |
| 5054 | |
| 5055 | static int |
| 5056 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5057 | PyObject *name, int flag) |
| 5058 | { |
| 5059 | PyObject *o; |
| 5060 | int val; |
| 5061 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5062 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5063 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5064 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5065 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5066 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5067 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5068 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5069 | return -1; |
| 5070 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5071 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5072 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5073 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5074 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5075 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5076 | Py_DECREF(o); |
| 5077 | return -1; |
| 5078 | } |
| 5079 | Py_DECREF(o); |
| 5080 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5081 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5082 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5083 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5084 | } else if (flag & DEF_GLOBAL) { |
| 5085 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5086 | perhaps only DEF_FREE_GLOBAL */ |
| 5087 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5088 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5089 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5090 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5091 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5092 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5093 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5094 | Py_DECREF(o); |
| 5095 | return -1; |
| 5096 | } |
| 5097 | Py_DECREF(o); |
| 5098 | } |
| 5099 | return 0; |
| 5100 | } |
| 5101 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5102 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5103 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5104 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5105 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5106 | ignored) in case those are the only places a yield occurs (so that this |
| 5107 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5108 | static int |
| 5109 | look_for_yield(node *n) |
| 5110 | { |
| 5111 | int i; |
| 5112 | |
| 5113 | for (i = 0; i < NCH(n); ++i) { |
| 5114 | node *kid = CHILD(n, i); |
| 5115 | |
| 5116 | switch (TYPE(kid)) { |
| 5117 | |
| 5118 | case classdef: |
| 5119 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5120 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5121 | /* Stuff in nested functions and classes can't make |
| 5122 | the parent a generator. */ |
| 5123 | return 0; |
| 5124 | |
| 5125 | case yield_stmt: |
| 5126 | return 1; |
| 5127 | |
| 5128 | default: |
| 5129 | if (look_for_yield(kid)) |
| 5130 | return 1; |
| 5131 | } |
| 5132 | } |
| 5133 | return 0; |
| 5134 | } |
| 5135 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5136 | static void |
| 5137 | symtable_node(struct symtable *st, node *n) |
| 5138 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5139 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5140 | |
| 5141 | loop: |
| 5142 | switch (TYPE(n)) { |
| 5143 | case funcdef: { |
| 5144 | char *func_name = STR(CHILD(n, 1)); |
| 5145 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5146 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5147 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5148 | symtable_funcdef(st, n); |
| 5149 | symtable_exit_scope(st); |
| 5150 | break; |
| 5151 | } |
| 5152 | case lambdef: |
| 5153 | if (NCH(n) == 4) |
| 5154 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5155 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5156 | symtable_funcdef(st, n); |
| 5157 | symtable_exit_scope(st); |
| 5158 | break; |
| 5159 | case classdef: { |
| 5160 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5161 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5162 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5163 | node *bases = CHILD(n, 3); |
| 5164 | int i; |
| 5165 | for (i = 0; i < NCH(bases); i += 2) { |
| 5166 | symtable_node(st, CHILD(bases, i)); |
| 5167 | } |
| 5168 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5169 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5170 | tmp = st->st_private; |
| 5171 | st->st_private = class_name; |
| 5172 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5173 | st->st_private = tmp; |
| 5174 | symtable_exit_scope(st); |
| 5175 | break; |
| 5176 | } |
| 5177 | case if_stmt: |
| 5178 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5179 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5180 | if (st->st_cur->ste_generator == 0) |
| 5181 | st->st_cur->ste_generator = |
| 5182 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5183 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5184 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5185 | symtable_node(st, CHILD(n, i + 1)); |
| 5186 | symtable_node(st, CHILD(n, i + 3)); |
| 5187 | } |
| 5188 | if (i + 2 < NCH(n)) |
| 5189 | symtable_node(st, CHILD(n, i + 2)); |
| 5190 | break; |
| 5191 | case global_stmt: |
| 5192 | symtable_global(st, n); |
| 5193 | break; |
| 5194 | case import_stmt: |
| 5195 | symtable_import(st, n); |
| 5196 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5197 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5198 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5199 | symtable_node(st, CHILD(n, 1)); |
| 5200 | if (NCH(n) > 2) |
| 5201 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5202 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5203 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5204 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5205 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5206 | if (NCH(n) > 4) |
| 5207 | symtable_node(st, CHILD(n, 5)); |
| 5208 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5209 | |
| 5210 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5211 | case assert_stmt: |
| 5212 | if (Py_OptimizeFlag) |
| 5213 | return; |
| 5214 | if (NCH(n) == 2) { |
| 5215 | n = CHILD(n, 1); |
| 5216 | goto loop; |
| 5217 | } else { |
| 5218 | symtable_node(st, CHILD(n, 1)); |
| 5219 | n = CHILD(n, 3); |
| 5220 | goto loop; |
| 5221 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5222 | case except_clause: |
| 5223 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5224 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5225 | if (NCH(n) > 1) { |
| 5226 | n = CHILD(n, 1); |
| 5227 | goto loop; |
| 5228 | } |
| 5229 | break; |
| 5230 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5231 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5232 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5233 | case yield_stmt: |
| 5234 | st->st_cur->ste_generator = 1; |
| 5235 | n = CHILD(n, 1); |
| 5236 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5237 | case expr_stmt: |
| 5238 | if (NCH(n) == 1) |
| 5239 | n = CHILD(n, 0); |
| 5240 | else { |
| 5241 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5242 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5243 | symtable_node(st, CHILD(n, 2)); |
| 5244 | break; |
| 5245 | } else { |
| 5246 | int i; |
| 5247 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5248 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5249 | n = CHILD(n, NCH(n) - 1); |
| 5250 | } |
| 5251 | } |
| 5252 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5253 | case list_iter: |
| 5254 | n = CHILD(n, 0); |
| 5255 | if (TYPE(n) == list_for) { |
| 5256 | st->st_tmpname++; |
| 5257 | symtable_list_comprehension(st, n); |
| 5258 | st->st_tmpname--; |
| 5259 | } else { |
| 5260 | REQ(n, list_if); |
| 5261 | symtable_node(st, CHILD(n, 1)); |
| 5262 | if (NCH(n) == 3) { |
| 5263 | n = CHILD(n, 2); |
| 5264 | goto loop; |
| 5265 | } |
| 5266 | } |
| 5267 | break; |
| 5268 | case for_stmt: |
| 5269 | symtable_assign(st, CHILD(n, 1), 0); |
| 5270 | for (i = 3; i < NCH(n); ++i) |
| 5271 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5272 | symtable_node(st, CHILD(n, i)); |
| 5273 | break; |
| 5274 | /* The remaining cases fall through to default except in |
| 5275 | special circumstances. This requires the individual cases |
| 5276 | to be coded with great care, even though they look like |
| 5277 | rather innocuous. Each case must double-check TYPE(n). |
| 5278 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5279 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5280 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5281 | n = CHILD(n, 2); |
| 5282 | goto loop; |
| 5283 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5284 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5285 | case listmaker: |
| 5286 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5287 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5288 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5289 | symtable_node(st, CHILD(n, 0)); |
| 5290 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5291 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5292 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5293 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5294 | case atom: |
| 5295 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5296 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5297 | break; |
| 5298 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5299 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5300 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5301 | /* Walk over every non-token child with a special case |
| 5302 | for one child. |
| 5303 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5304 | if (NCH(n) == 1) { |
| 5305 | n = CHILD(n, 0); |
| 5306 | goto loop; |
| 5307 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5308 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5309 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5310 | symtable_node(st, CHILD(n, i)); |
| 5311 | } |
| 5312 | } |
| 5313 | |
| 5314 | static void |
| 5315 | symtable_funcdef(struct symtable *st, node *n) |
| 5316 | { |
| 5317 | node *body; |
| 5318 | |
| 5319 | if (TYPE(n) == lambdef) { |
| 5320 | if (NCH(n) == 4) |
| 5321 | symtable_params(st, CHILD(n, 1)); |
| 5322 | } else |
| 5323 | symtable_params(st, CHILD(n, 2)); |
| 5324 | body = CHILD(n, NCH(n) - 1); |
| 5325 | symtable_node(st, body); |
| 5326 | } |
| 5327 | |
| 5328 | /* The next two functions parse the argument tuple. |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5329 | symtable_default_args() checks for names in the default arguments, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5330 | which are references in the defining scope. symtable_params() |
| 5331 | parses the parameter names, which are defined in the function's |
| 5332 | body. |
| 5333 | |
| 5334 | varargslist: |
| 5335 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5336 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5337 | */ |
| 5338 | |
| 5339 | static void |
| 5340 | symtable_default_args(struct symtable *st, node *n) |
| 5341 | { |
| 5342 | node *c; |
| 5343 | int i; |
| 5344 | |
| 5345 | if (TYPE(n) == parameters) { |
| 5346 | n = CHILD(n, 1); |
| 5347 | if (TYPE(n) == RPAR) |
| 5348 | return; |
| 5349 | } |
| 5350 | REQ(n, varargslist); |
| 5351 | for (i = 0; i < NCH(n); i += 2) { |
| 5352 | c = CHILD(n, i); |
| 5353 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5354 | break; |
| 5355 | } |
| 5356 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5357 | symtable_node(st, CHILD(n, i)); |
| 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | static void |
| 5362 | symtable_params(struct symtable *st, node *n) |
| 5363 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5364 | int i, complex = -1, ext = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5365 | node *c = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5366 | |
| 5367 | if (TYPE(n) == parameters) { |
| 5368 | n = CHILD(n, 1); |
| 5369 | if (TYPE(n) == RPAR) |
| 5370 | return; |
| 5371 | } |
| 5372 | REQ(n, varargslist); |
| 5373 | for (i = 0; i < NCH(n); i += 2) { |
| 5374 | c = CHILD(n, i); |
| 5375 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5376 | ext = 1; |
| 5377 | break; |
| 5378 | } |
| 5379 | if (TYPE(c) == test) { |
| 5380 | continue; |
| 5381 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5382 | if (TYPE(CHILD(c, 0)) == NAME) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5383 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5384 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5385 | char nbuf[30]; |
| 5386 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5387 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5388 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5389 | } |
| 5390 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5391 | if (ext) { |
| 5392 | c = CHILD(n, i); |
| 5393 | if (TYPE(c) == STAR) { |
| 5394 | i++; |
| 5395 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5396 | DEF_PARAM | DEF_STAR); |
| 5397 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5398 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5399 | c = NULL; |
| 5400 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5401 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5402 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5403 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5404 | i++; |
| 5405 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5406 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5407 | } |
| 5408 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5409 | if (complex >= 0) { |
| 5410 | int j; |
| 5411 | for (j = 0; j <= complex; j++) { |
| 5412 | c = CHILD(n, j); |
| 5413 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5414 | c = CHILD(n, ++j); |
| 5415 | else if (TYPE(c) == EQUAL) |
| 5416 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5417 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5418 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5419 | } |
| 5420 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | static void |
| 5424 | symtable_params_fplist(struct symtable *st, node *n) |
| 5425 | { |
| 5426 | int i; |
| 5427 | node *c; |
| 5428 | |
| 5429 | REQ(n, fplist); |
| 5430 | for (i = 0; i < NCH(n); i += 2) { |
| 5431 | c = CHILD(n, i); |
| 5432 | REQ(c, fpdef); |
| 5433 | if (NCH(c) == 1) |
| 5434 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5435 | DEF_PARAM | DEF_INTUPLE); |
| 5436 | else |
| 5437 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5438 | } |
| 5439 | |
| 5440 | } |
| 5441 | |
| 5442 | static void |
| 5443 | symtable_global(struct symtable *st, node *n) |
| 5444 | { |
| 5445 | int i; |
| 5446 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5447 | /* XXX It might be helpful to warn about module-level global |
| 5448 | statements, but it's hard to tell the difference between |
| 5449 | module-level and a string passed to exec. |
| 5450 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5451 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5452 | for (i = 1; i < NCH(n); i += 2) { |
| 5453 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5454 | int flags; |
| 5455 | |
| 5456 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5457 | if (flags < 0) |
| 5458 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5459 | if (flags && flags != DEF_GLOBAL) { |
| 5460 | char buf[500]; |
| 5461 | if (flags & DEF_PARAM) { |
| 5462 | PyErr_Format(PyExc_SyntaxError, |
| 5463 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5464 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5465 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5466 | st->st_cur->ste_lineno); |
| 5467 | st->st_errors++; |
| 5468 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5469 | } |
| 5470 | else { |
| 5471 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5472 | PyOS_snprintf(buf, sizeof(buf), |
| 5473 | GLOBAL_AFTER_ASSIGN, |
| 5474 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5475 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5476 | PyOS_snprintf(buf, sizeof(buf), |
| 5477 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5478 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5479 | } |
| 5480 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5481 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5482 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | static void |
| 5486 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5487 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5488 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5489 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5490 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5491 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5492 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5493 | symtable_node(st, CHILD(n, 3)); |
| 5494 | if (NCH(n) == 5) |
| 5495 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | static void |
| 5499 | symtable_import(struct symtable *st, node *n) |
| 5500 | { |
| 5501 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5502 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5503 | | 'from' dotted_name 'import' |
| 5504 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5505 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5506 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5507 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5508 | node *dotname = CHILD(n, 1); |
| 5509 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5510 | /* check for bogus imports */ |
| 5511 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5512 | PyErr_SetString(PyExc_SyntaxError, |
| 5513 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5514 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5515 | n->n_lineno); |
| 5516 | st->st_errors++; |
| 5517 | return; |
| 5518 | } |
| 5519 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5520 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5521 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5522 | if (symtable_warn(st, |
| 5523 | "import * only allowed at module level") < 0) |
| 5524 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5525 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5526 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5527 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5528 | } else { |
| 5529 | for (i = 3; i < NCH(n); i += 2) { |
| 5530 | node *c = CHILD(n, i); |
| 5531 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5532 | symtable_assign(st, CHILD(c, 2), |
| 5533 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5534 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5535 | symtable_assign(st, CHILD(c, 0), |
| 5536 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5537 | } |
| 5538 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5539 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5540 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5541 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5542 | } |
| 5543 | } |
| 5544 | } |
| 5545 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5546 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5547 | symtable_add_def() if it is eventually called. The flag is useful |
| 5548 | to specify the particular type of assignment that should be |
| 5549 | recorded, e.g. an assignment caused by import. |
| 5550 | */ |
| 5551 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5552 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5553 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5554 | { |
| 5555 | node *tmp; |
| 5556 | int i; |
| 5557 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5558 | loop: |
| 5559 | switch (TYPE(n)) { |
| 5560 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5561 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5562 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5563 | return; |
| 5564 | case power: |
| 5565 | if (NCH(n) > 2) { |
| 5566 | for (i = 2; i < NCH(n); ++i) |
| 5567 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5568 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5569 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5570 | if (NCH(n) > 1) { |
| 5571 | symtable_node(st, CHILD(n, 0)); |
| 5572 | symtable_node(st, CHILD(n, 1)); |
| 5573 | } else { |
| 5574 | n = CHILD(n, 0); |
| 5575 | goto loop; |
| 5576 | } |
| 5577 | return; |
| 5578 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5579 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5580 | /* XXX This is an error, but the next pass |
| 5581 | will catch it. */ |
| 5582 | return; |
| 5583 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5584 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5585 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5586 | } |
| 5587 | return; |
| 5588 | case exprlist: |
| 5589 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5590 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5591 | if (NCH(n) == 1) { |
| 5592 | n = CHILD(n, 0); |
| 5593 | goto loop; |
| 5594 | } |
| 5595 | else { |
| 5596 | int i; |
| 5597 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5598 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5599 | return; |
| 5600 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5601 | case atom: |
| 5602 | tmp = CHILD(n, 0); |
| 5603 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5604 | n = CHILD(n, 1); |
| 5605 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5606 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5607 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5608 | PyErr_SetString(PyExc_SyntaxError, |
| 5609 | ASSIGN_DEBUG); |
| 5610 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5611 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5612 | st->st_errors++; |
| 5613 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5614 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5615 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5616 | return; |
| 5617 | case dotted_as_name: |
| 5618 | if (NCH(n) == 3) |
| 5619 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5620 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5621 | else |
| 5622 | symtable_add_def(st, |
| 5623 | STR(CHILD(CHILD(n, |
| 5624 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5625 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5626 | return; |
| 5627 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5628 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5629 | return; |
| 5630 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5631 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5632 | return; |
| 5633 | default: |
| 5634 | if (NCH(n) == 0) |
| 5635 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5636 | if (NCH(n) == 1) { |
| 5637 | n = CHILD(n, 0); |
| 5638 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5639 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5640 | /* Should only occur for errors like x + 1 = 1, |
| 5641 | which will be caught in the next pass. */ |
| 5642 | for (i = 0; i < NCH(n); ++i) |
| 5643 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5644 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |