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 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 459 | PyObject *c_globals; /* dictionary (value=None) */ |
| 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 */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 475 | 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 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 577 | static int issue_warning(char *, char *, int); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 578 | static int com_init(struct compiling *, char *); |
| 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 *); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 600 | static PyCodeObject *jcompile(node *, char *, struct compiling *, |
| 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 |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 657 | com_init(struct compiling *c, 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", |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1185 | c->c_filename, |
| 1186 | c->c_lineno, |
Guido van Rossum | 715eca9 | 2002-08-12 21:54:46 +0000 | [diff] [blame] | 1187 | NULL, |
| 1188 | NULL) < 0) |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1189 | return NULL; |
Guido van Rossum | 3cb8e54 | 2002-08-11 14:06:15 +0000 | [diff] [blame] | 1190 | errno = 0; /* Might be changed by PyErr_Warn() */ |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1191 | } |
| 1192 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1193 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1194 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1195 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1196 | if (errno != 0) |
| 1197 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1198 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1199 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1200 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1201 | #ifndef WITHOUT_COMPLEX |
| 1202 | if (imflag) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1203 | Py_complex z; |
| 1204 | z.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1205 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1206 | z.imag = atof(s); |
| 1207 | PyFPE_END_PROTECT(z) |
| 1208 | return PyComplex_FromCComplex(z); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1209 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1210 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1211 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1212 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1213 | PyFPE_START_PROTECT("atof", return 0) |
| 1214 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1215 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1216 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1217 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1220 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1221 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1222 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1223 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1224 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1225 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1226 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1227 | PyObject *u, *v; |
| 1228 | char *s, *t; |
| 1229 | t = s = *sPtr; |
| 1230 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1231 | while (s < end && (*s & 0x80)) s++; |
| 1232 | *sPtr = s; |
| 1233 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1234 | if (u == NULL) |
| 1235 | return NULL; |
| 1236 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1237 | Py_DECREF(u); |
| 1238 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1239 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1243 | parsestr(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1244 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1245 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1246 | size_t len; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1247 | char *buf; |
| 1248 | char *p; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1249 | char *end; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1250 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1251 | int rawmode = 0; |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1252 | char* encoding = ((c == NULL) ? NULL : c->c_encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1253 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1254 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1255 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1256 | if (isalpha(quote) || quote == '_') { |
| 1257 | if (quote == 'u' || quote == 'U') { |
| 1258 | quote = *++s; |
| 1259 | unicode = 1; |
| 1260 | } |
| 1261 | if (quote == 'r' || quote == 'R') { |
| 1262 | quote = *++s; |
| 1263 | rawmode = 1; |
| 1264 | } |
| 1265 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1266 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1267 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1268 | return NULL; |
| 1269 | } |
| 1270 | s++; |
| 1271 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1272 | if (len > INT_MAX) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1273 | com_error(c, PyExc_OverflowError, |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1274 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1275 | return NULL; |
| 1276 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1277 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1278 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1279 | return NULL; |
| 1280 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1281 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1282 | s += 2; |
| 1283 | len -= 2; |
| 1284 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1285 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1286 | return NULL; |
| 1287 | } |
| 1288 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1289 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1290 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1291 | PyObject *u, *w; |
| 1292 | if (encoding == NULL) { |
| 1293 | buf = s; |
| 1294 | u = NULL; |
| 1295 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1296 | buf = s; |
| 1297 | u = NULL; |
| 1298 | } else { |
| 1299 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1300 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1301 | if (u == NULL) |
| 1302 | return NULL; |
| 1303 | p = buf = PyString_AsString(u); |
| 1304 | end = s + len; |
| 1305 | while (s < end) { |
| 1306 | if (*s == '\\') { |
| 1307 | *p++ = *s++; |
| 1308 | if (*s & 0x80) { |
| 1309 | strcpy(p, "u005c"); |
| 1310 | p += 5; |
| 1311 | } |
| 1312 | } |
| 1313 | if (*s & 0x80) { /* XXX inefficient */ |
| 1314 | char *r; |
| 1315 | int rn, i; |
| 1316 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1317 | if (w == NULL) { |
| 1318 | Py_DECREF(u); |
| 1319 | return NULL; |
| 1320 | } |
| 1321 | r = PyString_AsString(w); |
| 1322 | rn = PyString_Size(w); |
| 1323 | assert(rn % 2 == 0); |
| 1324 | for (i = 0; i < rn; i += 2) { |
| 1325 | sprintf(p, "\\u%02x%02x", |
| 1326 | r[i + 0] & 0xFF, |
| 1327 | r[i + 1] & 0xFF); |
| 1328 | p += 6; |
| 1329 | } |
| 1330 | Py_DECREF(w); |
| 1331 | } else { |
| 1332 | *p++ = *s++; |
| 1333 | } |
| 1334 | } |
| 1335 | len = p - buf; |
| 1336 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1337 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1338 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1339 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1340 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1341 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1342 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1343 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1344 | return v; |
| 1345 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1346 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1347 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1348 | need_encoding = (encoding != NULL && |
| 1349 | strcmp(encoding, "utf-8") != 0 && |
| 1350 | strcmp(encoding, "iso-8859-1") != 0); |
| 1351 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1352 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1353 | #ifndef Py_USING_UNICODE |
| 1354 | /* This should not happen - we never see any other |
| 1355 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1356 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1357 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1358 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1359 | if (u == NULL) |
| 1360 | return NULL; |
| 1361 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1362 | Py_DECREF(u); |
| 1363 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1364 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1365 | } else { |
| 1366 | return PyString_FromStringAndSize(s, len); |
| 1367 | } |
| 1368 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1369 | |
| 1370 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1371 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1372 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1373 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1374 | return v; |
| 1375 | } |
| 1376 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1377 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1378 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1379 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1380 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1381 | int i; |
| 1382 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1383 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1384 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1385 | for (i = 1; i < NCH(n); i++) { |
| 1386 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1387 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1388 | if (s == NULL) |
| 1389 | goto onError; |
| 1390 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1391 | PyString_ConcatAndDel(&v, s); |
| 1392 | if (v == NULL) |
| 1393 | goto onError; |
| 1394 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1395 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1396 | else { |
| 1397 | PyObject *temp; |
| 1398 | temp = PyUnicode_Concat(v, s); |
| 1399 | Py_DECREF(s); |
| 1400 | if (temp == NULL) |
| 1401 | goto onError; |
| 1402 | Py_DECREF(v); |
| 1403 | v = temp; |
| 1404 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1405 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1406 | } |
| 1407 | } |
| 1408 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1409 | |
| 1410 | onError: |
| 1411 | Py_XDECREF(v); |
| 1412 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1415 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1416 | 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] | 1417 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1418 | int anchor = 0; |
| 1419 | int save_begin = c->c_begin; |
| 1420 | |
| 1421 | /* list_iter: for v in expr [list_iter] */ |
| 1422 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1423 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1424 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1425 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1426 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1427 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1428 | c->c_loops++; |
| 1429 | com_list_iter(c, n, e, t); |
| 1430 | c->c_loops--; |
| 1431 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1432 | c->c_begin = save_begin; |
| 1433 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1434 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | static void |
| 1438 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1439 | { |
| 1440 | int anchor = 0; |
| 1441 | int a = 0; |
| 1442 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1443 | com_node(c, CHILD(n, 1)); |
| 1444 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1445 | com_addbyte(c, POP_TOP); |
| 1446 | com_pop(c, 1); |
| 1447 | com_list_iter(c, n, e, t); |
| 1448 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1449 | com_backpatch(c, a); |
| 1450 | /* We jump here with an extra entry which we now pop */ |
| 1451 | com_addbyte(c, POP_TOP); |
| 1452 | com_backpatch(c, anchor); |
| 1453 | } |
| 1454 | |
| 1455 | static void |
| 1456 | com_list_iter(struct compiling *c, |
| 1457 | node *p, /* parent of list_iter node */ |
| 1458 | node *e, /* element expression node */ |
| 1459 | char *t /* name of result list temp local */) |
| 1460 | { |
| 1461 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1462 | node *n = CHILD(p, NCH(p)-1); |
| 1463 | if (TYPE(n) == list_iter) { |
| 1464 | n = CHILD(n, 0); |
| 1465 | switch (TYPE(n)) { |
| 1466 | case list_for: |
| 1467 | com_list_for(c, n, e, t); |
| 1468 | break; |
| 1469 | case list_if: |
| 1470 | com_list_if(c, n, e, t); |
| 1471 | break; |
| 1472 | default: |
| 1473 | com_error(c, PyExc_SystemError, |
| 1474 | "invalid list_iter node type"); |
| 1475 | } |
| 1476 | } |
| 1477 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1478 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1479 | com_push(c, 1); |
| 1480 | com_node(c, e); |
| 1481 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1482 | com_addbyte(c, POP_TOP); |
| 1483 | com_pop(c, 2); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | static void |
| 1488 | com_list_comprehension(struct compiling *c, node *n) |
| 1489 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1490 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1491 | char tmpname[30]; |
| 1492 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1493 | com_addoparg(c, BUILD_LIST, 0); |
| 1494 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1495 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1496 | com_addop_name(c, LOAD_ATTR, "append"); |
| 1497 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1498 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1499 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1500 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1501 | --c->c_tmpname; |
| 1502 | } |
| 1503 | |
| 1504 | static void |
| 1505 | com_listmaker(struct compiling *c, node *n) |
| 1506 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1507 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1508 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1509 | com_list_comprehension(c, n); |
| 1510 | else { |
| 1511 | int len = 0; |
| 1512 | int i; |
| 1513 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1514 | com_node(c, CHILD(n, i)); |
| 1515 | com_addoparg(c, BUILD_LIST, len); |
| 1516 | com_pop(c, len-1); |
| 1517 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1521 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1522 | { |
| 1523 | int i; |
| 1524 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1525 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1526 | /* We must arrange things just right for STORE_SUBSCR. |
| 1527 | It wants the stack to look like (value) (dict) (key) */ |
| 1528 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1529 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1530 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1531 | com_addbyte(c, ROT_TWO); |
| 1532 | com_node(c, CHILD(n, i)); /* key */ |
| 1533 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1534 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1539 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1540 | { |
| 1541 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1542 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1543 | int i; |
| 1544 | REQ(n, atom); |
| 1545 | ch = CHILD(n, 0); |
| 1546 | switch (TYPE(ch)) { |
| 1547 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1548 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1549 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1550 | com_push(c, 1); |
| 1551 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1552 | else |
| 1553 | com_node(c, CHILD(n, 1)); |
| 1554 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1555 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1556 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1557 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1558 | com_push(c, 1); |
| 1559 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1560 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1561 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1562 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1563 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1564 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1565 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1566 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1567 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1568 | break; |
| 1569 | case BACKQUOTE: |
| 1570 | com_node(c, CHILD(n, 1)); |
| 1571 | com_addbyte(c, UNARY_CONVERT); |
| 1572 | break; |
| 1573 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1574 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1575 | i = 255; |
| 1576 | } |
| 1577 | else { |
| 1578 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1579 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1580 | } |
| 1581 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1582 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1585 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1586 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1587 | c->c_errors++; |
| 1588 | i = 255; |
| 1589 | } |
| 1590 | else { |
| 1591 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1592 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1593 | } |
| 1594 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1595 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1598 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1599 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1600 | break; |
| 1601 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1602 | com_error(c, PyExc_SystemError, |
| 1603 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1608 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1609 | { |
| 1610 | if (NCH(n) == 1) { |
| 1611 | com_addbyte(c, op); |
| 1612 | } |
| 1613 | else if (NCH(n) == 2) { |
| 1614 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1615 | com_node(c, CHILD(n, 0)); |
| 1616 | com_addbyte(c, op+1); |
| 1617 | } |
| 1618 | else { |
| 1619 | com_node(c, CHILD(n, 1)); |
| 1620 | com_addbyte(c, op+2); |
| 1621 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1622 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1623 | } |
| 1624 | else { |
| 1625 | com_node(c, CHILD(n, 0)); |
| 1626 | com_node(c, CHILD(n, 2)); |
| 1627 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1628 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1629 | } |
| 1630 | } |
| 1631 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1632 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1633 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1634 | { |
| 1635 | if (NCH(n) == 1) { |
| 1636 | com_addbyte(c, DUP_TOP); |
| 1637 | com_push(c, 1); |
| 1638 | com_addbyte(c, SLICE); |
| 1639 | com_node(c, augn); |
| 1640 | com_addbyte(c, opcode); |
| 1641 | com_pop(c, 1); |
| 1642 | com_addbyte(c, ROT_TWO); |
| 1643 | com_addbyte(c, STORE_SLICE); |
| 1644 | com_pop(c, 2); |
| 1645 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1646 | com_node(c, CHILD(n, 0)); |
| 1647 | com_addoparg(c, DUP_TOPX, 2); |
| 1648 | com_push(c, 2); |
| 1649 | com_addbyte(c, SLICE+1); |
| 1650 | com_pop(c, 1); |
| 1651 | com_node(c, augn); |
| 1652 | com_addbyte(c, opcode); |
| 1653 | com_pop(c, 1); |
| 1654 | com_addbyte(c, ROT_THREE); |
| 1655 | com_addbyte(c, STORE_SLICE+1); |
| 1656 | com_pop(c, 3); |
| 1657 | } else if (NCH(n) == 2) { |
| 1658 | com_node(c, CHILD(n, 1)); |
| 1659 | com_addoparg(c, DUP_TOPX, 2); |
| 1660 | com_push(c, 2); |
| 1661 | com_addbyte(c, SLICE+2); |
| 1662 | com_pop(c, 1); |
| 1663 | com_node(c, augn); |
| 1664 | com_addbyte(c, opcode); |
| 1665 | com_pop(c, 1); |
| 1666 | com_addbyte(c, ROT_THREE); |
| 1667 | com_addbyte(c, STORE_SLICE+2); |
| 1668 | com_pop(c, 3); |
| 1669 | } else { |
| 1670 | com_node(c, CHILD(n, 0)); |
| 1671 | com_node(c, CHILD(n, 2)); |
| 1672 | com_addoparg(c, DUP_TOPX, 3); |
| 1673 | com_push(c, 3); |
| 1674 | com_addbyte(c, SLICE+3); |
| 1675 | com_pop(c, 2); |
| 1676 | com_node(c, augn); |
| 1677 | com_addbyte(c, opcode); |
| 1678 | com_pop(c, 1); |
| 1679 | com_addbyte(c, ROT_FOUR); |
| 1680 | com_addbyte(c, STORE_SLICE+3); |
| 1681 | com_pop(c, 4); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1686 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1687 | { |
| 1688 | node *m; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1689 | REQ(n, argument); /* [test '='] test; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1690 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1691 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1692 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 1693 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1694 | } |
| 1695 | else { |
| 1696 | com_node(c, CHILD(n, 0)); |
| 1697 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1698 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1699 | } |
| 1700 | m = n; |
| 1701 | do { |
| 1702 | m = CHILD(m, 0); |
| 1703 | } while (NCH(m) == 1); |
| 1704 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1705 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1706 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 1707 | * SF bug 132313 points out that complaining about a keyword |
| 1708 | * then is very confusing. |
| 1709 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1710 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 1711 | TYPE(m) == lambdef ? |
| 1712 | "lambda cannot contain assignment" : |
| 1713 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1714 | } |
| 1715 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1716 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 63dd79a | 2002-08-16 02:24:56 +0000 | [diff] [blame] | 1717 | (void) none_assignment_check(c, STR(m), 1); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1718 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1719 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1720 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1721 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 1722 | else if (*pkeywords == NULL) { |
| 1723 | c->c_errors++; |
| 1724 | Py_DECREF(v); |
| 1725 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1726 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 1727 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1728 | "duplicate keyword argument"); |
| 1729 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1730 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1731 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1732 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1733 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1734 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1735 | } |
| 1736 | } |
| 1737 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1740 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1741 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1742 | { |
| 1743 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1744 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1745 | } |
| 1746 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1747 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1748 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1749 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1750 | int star_flag = 0; |
| 1751 | int starstar_flag = 0; |
| 1752 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1753 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1754 | na = 0; |
| 1755 | nk = 0; |
| 1756 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1757 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1758 | if (TYPE(ch) == STAR || |
| 1759 | TYPE(ch) == DOUBLESTAR) |
| 1760 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1761 | if (ch->n_lineno != lineno) { |
| 1762 | lineno = ch->n_lineno; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1763 | com_set_lineno(c, lineno); |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 1764 | } |
| 1765 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1766 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1767 | na++; |
| 1768 | else |
| 1769 | nk++; |
| 1770 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1771 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1772 | while (i < NCH(n)) { |
| 1773 | node *tok = CHILD(n, i); |
| 1774 | node *ch = CHILD(n, i+1); |
| 1775 | i += 3; |
| 1776 | switch (TYPE(tok)) { |
| 1777 | case STAR: star_flag = 1; break; |
| 1778 | case DOUBLESTAR: starstar_flag = 1; break; |
| 1779 | } |
| 1780 | com_node(c, ch); |
| 1781 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1782 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1783 | com_error(c, PyExc_SyntaxError, |
| 1784 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 1785 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1786 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 1787 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 1788 | star_flag + (starstar_flag << 1); |
| 1789 | else |
| 1790 | opcode = CALL_FUNCTION; |
| 1791 | com_addoparg(c, opcode, na | (nk << 8)); |
| 1792 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1797 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1798 | { |
| 1799 | com_addopname(c, LOAD_ATTR, n); |
| 1800 | } |
| 1801 | |
| 1802 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1803 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1804 | { |
| 1805 | int i=0; |
| 1806 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1807 | node *ch; |
| 1808 | |
| 1809 | /* first argument */ |
| 1810 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1811 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1812 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1813 | i++; |
| 1814 | } |
| 1815 | else { |
| 1816 | com_node(c, CHILD(n,i)); |
| 1817 | i++; |
| 1818 | REQ(CHILD(n,i),COLON); |
| 1819 | i++; |
| 1820 | } |
| 1821 | /* second argument */ |
| 1822 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 1823 | com_node(c, CHILD(n,i)); |
| 1824 | i++; |
| 1825 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1826 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1827 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1828 | com_push(c, 1); |
| 1829 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1830 | /* remaining arguments */ |
| 1831 | for (; i < NCH(n); i++) { |
| 1832 | ns++; |
| 1833 | ch=CHILD(n,i); |
| 1834 | REQ(ch, sliceop); |
| 1835 | if (NCH(ch) == 1) { |
| 1836 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1837 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1838 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1839 | } |
| 1840 | else |
| 1841 | com_node(c, CHILD(ch,1)); |
| 1842 | } |
| 1843 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1844 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1848 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1849 | { |
| 1850 | node *ch; |
| 1851 | REQ(n, subscript); |
| 1852 | ch = CHILD(n,0); |
| 1853 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1854 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 1855 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1856 | com_push(c, 1); |
| 1857 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1858 | else { |
| 1859 | /* check for slice */ |
| 1860 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 1861 | com_sliceobj(c, n); |
| 1862 | else { |
| 1863 | REQ(ch, test); |
| 1864 | com_node(c, ch); |
| 1865 | } |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1870 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1871 | { |
| 1872 | int i, op; |
| 1873 | REQ(n, subscriptlist); |
| 1874 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 1875 | if (NCH(n) == 1) { |
| 1876 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 1877 | /* 'Basic' slice, should have exactly one colon. */ |
| 1878 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 1879 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 1880 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 1881 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1882 | switch (assigning) { |
| 1883 | case OP_DELETE: |
| 1884 | op = DELETE_SLICE; |
| 1885 | break; |
| 1886 | case OP_ASSIGN: |
| 1887 | op = STORE_SLICE; |
| 1888 | break; |
| 1889 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1890 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1891 | break; |
| 1892 | default: |
| 1893 | com_augassign_slice(c, sub, assigning, augn); |
| 1894 | return; |
| 1895 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1896 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1897 | if (op == STORE_SLICE) |
| 1898 | com_pop(c, 2); |
| 1899 | else if (op == DELETE_SLICE) |
| 1900 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1901 | return; |
| 1902 | } |
| 1903 | } |
| 1904 | /* Else normal subscriptlist. Compile each subscript. */ |
| 1905 | for (i = 0; i < NCH(n); i += 2) |
| 1906 | com_subscript(c, CHILD(n, i)); |
| 1907 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1908 | if (NCH(n) > 1) { |
| 1909 | i = (NCH(n)+1) / 2; |
| 1910 | com_addoparg(c, BUILD_TUPLE, i); |
| 1911 | com_pop(c, i-1); |
| 1912 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1913 | switch (assigning) { |
| 1914 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1915 | op = DELETE_SUBSCR; |
| 1916 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1917 | break; |
| 1918 | default: |
| 1919 | case OP_ASSIGN: |
| 1920 | op = STORE_SUBSCR; |
| 1921 | i = 3; |
| 1922 | break; |
| 1923 | case OP_APPLY: |
| 1924 | op = BINARY_SUBSCR; |
| 1925 | i = 1; |
| 1926 | break; |
| 1927 | } |
| 1928 | if (assigning > OP_APPLY) { |
| 1929 | com_addoparg(c, DUP_TOPX, 2); |
| 1930 | com_push(c, 2); |
| 1931 | com_addbyte(c, BINARY_SUBSCR); |
| 1932 | com_pop(c, 1); |
| 1933 | com_node(c, augn); |
| 1934 | com_addbyte(c, assigning); |
| 1935 | com_pop(c, 1); |
| 1936 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1937 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1938 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1939 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1943 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1944 | { |
| 1945 | REQ(n, trailer); |
| 1946 | switch (TYPE(CHILD(n, 0))) { |
| 1947 | case LPAR: |
| 1948 | com_call_function(c, CHILD(n, 1)); |
| 1949 | break; |
| 1950 | case DOT: |
| 1951 | com_select_member(c, CHILD(n, 1)); |
| 1952 | break; |
| 1953 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1954 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1955 | break; |
| 1956 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1957 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1958 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1959 | } |
| 1960 | } |
| 1961 | |
| 1962 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1963 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1964 | { |
| 1965 | int i; |
| 1966 | REQ(n, power); |
| 1967 | com_atom(c, CHILD(n, 0)); |
| 1968 | for (i = 1; i < NCH(n); i++) { |
| 1969 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 1970 | com_factor(c, CHILD(n, i+1)); |
| 1971 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1972 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1973 | break; |
| 1974 | } |
| 1975 | else |
| 1976 | com_apply_trailer(c, CHILD(n, i)); |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 1981 | com_invert_constant(struct compiling *c, node *n) |
| 1982 | { |
| 1983 | /* Compute the inverse of int and longs and use them directly, |
| 1984 | but be prepared to generate code for all other |
| 1985 | possibilities (invalid numbers, floats, complex). |
| 1986 | */ |
| 1987 | PyObject *num, *inv = NULL; |
| 1988 | int i; |
| 1989 | |
| 1990 | REQ(n, NUMBER); |
| 1991 | num = parsenumber(c, STR(n)); |
| 1992 | if (num == NULL) |
| 1993 | i = 255; |
| 1994 | else { |
| 1995 | inv = PyNumber_Invert(num); |
| 1996 | if (inv == NULL) { |
| 1997 | PyErr_Clear(); |
| 1998 | i = com_addconst(c, num); |
| 1999 | } else { |
| 2000 | i = com_addconst(c, inv); |
| 2001 | Py_DECREF(inv); |
| 2002 | } |
| 2003 | Py_DECREF(num); |
| 2004 | } |
| 2005 | com_addoparg(c, LOAD_CONST, i); |
| 2006 | com_push(c, 1); |
| 2007 | if (num != NULL && inv == NULL) |
| 2008 | com_addbyte(c, UNARY_INVERT); |
| 2009 | } |
| 2010 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2011 | static int |
| 2012 | is_float_zero(const char *p) |
| 2013 | { |
| 2014 | int found_radix_point = 0; |
| 2015 | int ch; |
| 2016 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2017 | switch (ch) { |
| 2018 | case '0': |
| 2019 | /* no reason to believe it's not 0 -- continue */ |
| 2020 | break; |
| 2021 | |
| 2022 | case 'e': case 'E': case 'j': case 'J': |
| 2023 | /* If this was a hex constant, we already would have |
| 2024 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2025 | must be an exponent marker, and we haven't yet |
| 2026 | seen a non-zero digit, and it doesn't matter what |
| 2027 | the exponent is then. For 'j' or 'J' similarly, |
| 2028 | except that this is an imaginary 0 then. */ |
| 2029 | return 1; |
| 2030 | |
| 2031 | case '.': |
| 2032 | found_radix_point = 1; |
| 2033 | break; |
| 2034 | |
| 2035 | default: |
| 2036 | return 0; |
| 2037 | } |
| 2038 | } |
| 2039 | return found_radix_point; |
| 2040 | } |
| 2041 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2042 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2043 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2044 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2045 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2046 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2047 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2048 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2049 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2050 | approriate value as a constant. If the value is negative, |
| 2051 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2052 | negative in the 0th position -- unless we're doing unary minus |
| 2053 | of a floating zero! In that case the sign is significant, but |
| 2054 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2055 | */ |
| 2056 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2057 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2058 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2059 | && NCH(pfactor) == 1 |
| 2060 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2061 | && NCH(ppower) == 1 |
| 2062 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2063 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
| 2064 | && !(childtype == MINUS && is_float_zero(STR(pnum)))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2065 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2066 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2067 | return; |
| 2068 | } |
| 2069 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2070 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2071 | if (s == NULL) { |
| 2072 | com_error(c, PyExc_MemoryError, ""); |
| 2073 | com_addbyte(c, 255); |
| 2074 | return; |
| 2075 | } |
| 2076 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2077 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2078 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2079 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2080 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2081 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2082 | } |
| 2083 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2084 | com_factor(c, CHILD(n, 1)); |
| 2085 | com_addbyte(c, UNARY_POSITIVE); |
| 2086 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2087 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2088 | com_factor(c, CHILD(n, 1)); |
| 2089 | com_addbyte(c, UNARY_NEGATIVE); |
| 2090 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2091 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2092 | com_factor(c, CHILD(n, 1)); |
| 2093 | com_addbyte(c, UNARY_INVERT); |
| 2094 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2095 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2096 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2101 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2102 | { |
| 2103 | int i; |
| 2104 | int op; |
| 2105 | REQ(n, term); |
| 2106 | com_factor(c, CHILD(n, 0)); |
| 2107 | for (i = 2; i < NCH(n); i += 2) { |
| 2108 | com_factor(c, CHILD(n, i)); |
| 2109 | switch (TYPE(CHILD(n, i-1))) { |
| 2110 | case STAR: |
| 2111 | op = BINARY_MULTIPLY; |
| 2112 | break; |
| 2113 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2114 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2115 | op = BINARY_TRUE_DIVIDE; |
| 2116 | else |
| 2117 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2118 | break; |
| 2119 | case PERCENT: |
| 2120 | op = BINARY_MODULO; |
| 2121 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2122 | case DOUBLESLASH: |
| 2123 | op = BINARY_FLOOR_DIVIDE; |
| 2124 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2125 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2126 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2127 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2128 | op = 255; |
| 2129 | } |
| 2130 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2131 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2136 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2137 | { |
| 2138 | int i; |
| 2139 | int op; |
| 2140 | REQ(n, arith_expr); |
| 2141 | com_term(c, CHILD(n, 0)); |
| 2142 | for (i = 2; i < NCH(n); i += 2) { |
| 2143 | com_term(c, CHILD(n, i)); |
| 2144 | switch (TYPE(CHILD(n, i-1))) { |
| 2145 | case PLUS: |
| 2146 | op = BINARY_ADD; |
| 2147 | break; |
| 2148 | case MINUS: |
| 2149 | op = BINARY_SUBTRACT; |
| 2150 | break; |
| 2151 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2152 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2153 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2154 | op = 255; |
| 2155 | } |
| 2156 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2157 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2162 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2163 | { |
| 2164 | int i; |
| 2165 | int op; |
| 2166 | REQ(n, shift_expr); |
| 2167 | com_arith_expr(c, CHILD(n, 0)); |
| 2168 | for (i = 2; i < NCH(n); i += 2) { |
| 2169 | com_arith_expr(c, CHILD(n, i)); |
| 2170 | switch (TYPE(CHILD(n, i-1))) { |
| 2171 | case LEFTSHIFT: |
| 2172 | op = BINARY_LSHIFT; |
| 2173 | break; |
| 2174 | case RIGHTSHIFT: |
| 2175 | op = BINARY_RSHIFT; |
| 2176 | break; |
| 2177 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2178 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2179 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2180 | op = 255; |
| 2181 | } |
| 2182 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2183 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2188 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2189 | { |
| 2190 | int i; |
| 2191 | int op; |
| 2192 | REQ(n, and_expr); |
| 2193 | com_shift_expr(c, CHILD(n, 0)); |
| 2194 | for (i = 2; i < NCH(n); i += 2) { |
| 2195 | com_shift_expr(c, CHILD(n, i)); |
| 2196 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2197 | op = BINARY_AND; |
| 2198 | } |
| 2199 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2200 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2201 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2202 | op = 255; |
| 2203 | } |
| 2204 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2205 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2210 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2211 | { |
| 2212 | int i; |
| 2213 | int op; |
| 2214 | REQ(n, xor_expr); |
| 2215 | com_and_expr(c, CHILD(n, 0)); |
| 2216 | for (i = 2; i < NCH(n); i += 2) { |
| 2217 | com_and_expr(c, CHILD(n, i)); |
| 2218 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2219 | op = BINARY_XOR; |
| 2220 | } |
| 2221 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2222 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2223 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2224 | op = 255; |
| 2225 | } |
| 2226 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2227 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2232 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2233 | { |
| 2234 | int i; |
| 2235 | int op; |
| 2236 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2237 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2238 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2239 | com_xor_expr(c, CHILD(n, i)); |
| 2240 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2241 | op = BINARY_OR; |
| 2242 | } |
| 2243 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2244 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2245 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2246 | op = 255; |
| 2247 | } |
| 2248 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2249 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2254 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2255 | { |
| 2256 | REQ(n, comp_op); |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2257 | /* comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2258 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2259 | if (NCH(n) == 1) { |
| 2260 | n = CHILD(n, 0); |
| 2261 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2262 | case LESS: return PyCmp_LT; |
| 2263 | case GREATER: return PyCmp_GT; |
Guido van Rossum | 01cfd44 | 1991-10-20 20:12:38 +0000 | [diff] [blame] | 2264 | case EQEQUAL: /* == */ |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2265 | case EQUAL: return PyCmp_EQ; |
| 2266 | case LESSEQUAL: return PyCmp_LE; |
| 2267 | case GREATEREQUAL: return PyCmp_GE; |
| 2268 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2269 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2270 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2274 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2275 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2276 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2277 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2278 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2279 | } |
| 2280 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2281 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2285 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2286 | { |
| 2287 | int i; |
| 2288 | enum cmp_op op; |
| 2289 | int anchor; |
| 2290 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2291 | com_expr(c, CHILD(n, 0)); |
| 2292 | if (NCH(n) == 1) |
| 2293 | return; |
| 2294 | |
| 2295 | /**************************************************************** |
| 2296 | The following code is generated for all but the last |
| 2297 | comparison in a chain: |
| 2298 | |
| 2299 | label: on stack: opcode: jump to: |
| 2300 | |
| 2301 | a <code to load b> |
| 2302 | a, b DUP_TOP |
| 2303 | a, b, b ROT_THREE |
| 2304 | b, a, b COMPARE_OP |
| 2305 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2306 | b, 1 POP_TOP |
| 2307 | b |
| 2308 | |
| 2309 | We are now ready to repeat this sequence for the next |
| 2310 | comparison in the chain. |
| 2311 | |
| 2312 | For the last we generate: |
| 2313 | |
| 2314 | b <code to load c> |
| 2315 | b, c COMPARE_OP |
| 2316 | 0-or-1 |
| 2317 | |
| 2318 | If there were any jumps to L1 (i.e., there was more than one |
| 2319 | comparison), we generate: |
| 2320 | |
| 2321 | 0-or-1 JUMP_FORWARD L2 |
| 2322 | L1: b, 0 ROT_TWO |
| 2323 | 0, b POP_TOP |
| 2324 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2325 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2326 | ****************************************************************/ |
| 2327 | |
| 2328 | anchor = 0; |
| 2329 | |
| 2330 | for (i = 2; i < NCH(n); i += 2) { |
| 2331 | com_expr(c, CHILD(n, i)); |
| 2332 | if (i+2 < NCH(n)) { |
| 2333 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2334 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2335 | com_addbyte(c, ROT_THREE); |
| 2336 | } |
| 2337 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2338 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2339 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2340 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2341 | } |
| 2342 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2343 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2344 | if (i+2 < NCH(n)) { |
| 2345 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2346 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2347 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
| 2350 | |
| 2351 | if (anchor) { |
| 2352 | int anchor2 = 0; |
| 2353 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2354 | com_backpatch(c, anchor); |
| 2355 | com_addbyte(c, ROT_TWO); |
| 2356 | com_addbyte(c, POP_TOP); |
| 2357 | com_backpatch(c, anchor2); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2362 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2363 | { |
| 2364 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2365 | if (NCH(n) == 1) { |
| 2366 | com_comparison(c, CHILD(n, 0)); |
| 2367 | } |
| 2368 | else { |
| 2369 | com_not_test(c, CHILD(n, 1)); |
| 2370 | com_addbyte(c, UNARY_NOT); |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2375 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2376 | { |
| 2377 | int i; |
| 2378 | int anchor; |
| 2379 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2380 | anchor = 0; |
| 2381 | i = 0; |
| 2382 | for (;;) { |
| 2383 | com_not_test(c, CHILD(n, i)); |
| 2384 | if ((i += 2) >= NCH(n)) |
| 2385 | break; |
| 2386 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2387 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2388 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2389 | } |
| 2390 | if (anchor) |
| 2391 | com_backpatch(c, anchor); |
| 2392 | } |
| 2393 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2394 | static int |
| 2395 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2396 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2397 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2398 | if (free == 0) |
| 2399 | return 0; |
| 2400 | for (i = 0; i < free; ++i) { |
| 2401 | /* Bypass com_addop_varname because it will generate |
| 2402 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2403 | */ |
| 2404 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2405 | int arg, reftype; |
| 2406 | |
| 2407 | /* Special case: If a class contains a method with a |
| 2408 | free variable that has the same name as a method, |
| 2409 | the name will be considered free *and* local in the |
| 2410 | class. It should be handled by the closure, as |
| 2411 | well as by the normal name loookup logic. |
| 2412 | */ |
| 2413 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2414 | if (reftype == CELL) |
| 2415 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2416 | else /* (reftype == FREE) */ |
| 2417 | arg = com_lookup_arg(c->c_freevars, name); |
| 2418 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2419 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2420 | "freevars of %s: %s\n", |
| 2421 | PyObject_REPR(name), |
| 2422 | c->c_name, |
| 2423 | reftype, arg, |
| 2424 | PyString_AS_STRING(co->co_name), |
| 2425 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2426 | Py_FatalError("com_make_closure()"); |
| 2427 | } |
| 2428 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2429 | |
| 2430 | } |
| 2431 | com_push(c, free); |
| 2432 | return 1; |
| 2433 | } |
| 2434 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2435 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2436 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2437 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2438 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2439 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2440 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2441 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2442 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2443 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2444 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2445 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2446 | if (co == NULL) { |
| 2447 | c->c_errors++; |
| 2448 | return; |
| 2449 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2450 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2451 | i = com_addconst(c, (PyObject *)co); |
| 2452 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2453 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2454 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2455 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2456 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2457 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2458 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2459 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2460 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2461 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2462 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2463 | else { |
| 2464 | int anchor = 0; |
| 2465 | int i = 0; |
| 2466 | for (;;) { |
| 2467 | com_and_test(c, CHILD(n, i)); |
| 2468 | if ((i += 2) >= NCH(n)) |
| 2469 | break; |
| 2470 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2471 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2472 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2473 | } |
| 2474 | if (anchor) |
| 2475 | com_backpatch(c, anchor); |
| 2476 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2480 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2481 | { |
| 2482 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2483 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2484 | com_node(c, CHILD(n, 0)); |
| 2485 | } |
| 2486 | else { |
| 2487 | int i; |
| 2488 | int len; |
| 2489 | len = (NCH(n) + 1) / 2; |
| 2490 | for (i = 0; i < NCH(n); i += 2) |
| 2491 | com_node(c, CHILD(n, i)); |
| 2492 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2493 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | /* Begin of assignment compilation */ |
| 2499 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2500 | |
| 2501 | static void |
| 2502 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2503 | { |
| 2504 | com_addbyte(c, DUP_TOP); |
| 2505 | com_push(c, 1); |
| 2506 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2507 | com_node(c, augn); |
| 2508 | com_addbyte(c, opcode); |
| 2509 | com_pop(c, 1); |
| 2510 | com_addbyte(c, ROT_TWO); |
| 2511 | com_addopname(c, STORE_ATTR, n); |
| 2512 | com_pop(c, 2); |
| 2513 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2514 | |
| 2515 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2516 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2517 | { |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 2518 | if (none_assignment_check(c, STR(n), assigning)) |
| 2519 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2520 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2521 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
| 2524 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2525 | 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] | 2526 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2527 | REQ(n, trailer); |
| 2528 | switch (TYPE(CHILD(n, 0))) { |
| 2529 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2530 | if (assigning == OP_DELETE) |
| 2531 | com_error(c, PyExc_SyntaxError, |
| 2532 | "can't delete function call"); |
| 2533 | else |
| 2534 | com_error(c, PyExc_SyntaxError, |
| 2535 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2536 | break; |
| 2537 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2538 | if (assigning > OP_APPLY) |
| 2539 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2540 | else |
| 2541 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2542 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2543 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2544 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2547 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2552 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2553 | { |
| 2554 | int i; |
Thomas Wouters | b59290f | 2000-08-25 05:41:11 +0000 | [diff] [blame] | 2555 | if (TYPE(n) != testlist && TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2556 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2557 | if (assigning) { |
| 2558 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2559 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2560 | com_push(c, i-1); |
| 2561 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2562 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2563 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2564 | } |
| 2565 | |
| 2566 | static void |
| 2567 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2568 | { |
| 2569 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2570 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2571 | com_push(c, 1); |
| 2572 | com_node(c, augn); |
| 2573 | com_addbyte(c, opcode); |
| 2574 | com_pop(c, 1); |
| 2575 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2579 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2580 | { |
| 2581 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2582 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2583 | if (assigning) |
| 2584 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2588 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2589 | { |
| 2590 | /* Loop to avoid trivial recursion */ |
| 2591 | for (;;) { |
| 2592 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2593 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2594 | case exprlist: |
| 2595 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2596 | case testlist1: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2597 | if (NCH(n) > 1) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2598 | if (assigning > OP_APPLY) { |
| 2599 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2600 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2601 | return; |
| 2602 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2603 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
| 2606 | n = CHILD(n, 0); |
| 2607 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2608 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2609 | case test: |
| 2610 | case and_test: |
| 2611 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2612 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2613 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2614 | case xor_expr: |
| 2615 | case and_expr: |
| 2616 | case shift_expr: |
| 2617 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2618 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2619 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2620 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2621 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2622 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2623 | return; |
| 2624 | } |
| 2625 | n = CHILD(n, 0); |
| 2626 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2627 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2628 | case power: /* atom trailer* ('**' power)* |
| 2629 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2630 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2631 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2632 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2633 | return; |
| 2634 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2635 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2636 | int i; |
| 2637 | com_node(c, CHILD(n, 0)); |
| 2638 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2639 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2640 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2641 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2644 | com_apply_trailer(c, CHILD(n, i)); |
| 2645 | } /* NB i is still alive */ |
| 2646 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2647 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2648 | return; |
| 2649 | } |
| 2650 | n = CHILD(n, 0); |
| 2651 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2652 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2653 | case atom: |
| 2654 | switch (TYPE(CHILD(n, 0))) { |
| 2655 | case LPAR: |
| 2656 | n = CHILD(n, 1); |
| 2657 | if (TYPE(n) == RPAR) { |
| 2658 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2659 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2660 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2663 | if (assigning > OP_APPLY) { |
| 2664 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2665 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | case LSQB: |
| 2670 | n = CHILD(n, 1); |
| 2671 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2672 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2673 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2676 | if (assigning > OP_APPLY) { |
| 2677 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2678 | "augmented assign to list not possible"); |
| 2679 | return; |
| 2680 | } |
| 2681 | if (NCH(n) > 1 |
| 2682 | && TYPE(CHILD(n, 1)) == list_for) { |
| 2683 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2684 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2685 | return; |
| 2686 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2687 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2688 | return; |
| 2689 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2690 | if (assigning > OP_APPLY) |
| 2691 | com_augassign_name(c, CHILD(n, 0), |
| 2692 | assigning, augn); |
| 2693 | else |
| 2694 | com_assign_name(c, CHILD(n, 0), |
| 2695 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2696 | return; |
| 2697 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2698 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2699 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2700 | return; |
| 2701 | } |
| 2702 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2703 | |
| 2704 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2705 | com_error(c, PyExc_SyntaxError, |
| 2706 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 2707 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2708 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2709 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2710 | com_error(c, PyExc_SystemError, |
| 2711 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +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 | } |
| 2715 | } |
| 2716 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 2717 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2718 | static void |
| 2719 | com_augassign(struct compiling *c, node *n) |
| 2720 | { |
| 2721 | int opcode; |
| 2722 | |
| 2723 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 2724 | case '+': opcode = INPLACE_ADD; break; |
| 2725 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2726 | case '/': |
| 2727 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 2728 | opcode = INPLACE_FLOOR_DIVIDE; |
| 2729 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 2730 | opcode = INPLACE_TRUE_DIVIDE; |
| 2731 | else |
| 2732 | opcode = INPLACE_DIVIDE; |
| 2733 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2734 | case '%': opcode = INPLACE_MODULO; break; |
| 2735 | case '<': opcode = INPLACE_LSHIFT; break; |
| 2736 | case '>': opcode = INPLACE_RSHIFT; break; |
| 2737 | case '&': opcode = INPLACE_AND; break; |
| 2738 | case '^': opcode = INPLACE_XOR; break; |
| 2739 | case '|': opcode = INPLACE_OR; break; |
| 2740 | case '*': |
| 2741 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 2742 | opcode = INPLACE_POWER; |
| 2743 | else |
| 2744 | opcode = INPLACE_MULTIPLY; |
| 2745 | break; |
| 2746 | default: |
| 2747 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 2748 | return; |
| 2749 | } |
| 2750 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 2751 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2752 | |
| 2753 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2754 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2755 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2756 | REQ(n, expr_stmt); |
| 2757 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2758 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 2759 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2760 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2761 | if (NCH(n) == 1) { |
| 2762 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 2763 | if (c->c_interactive) |
| 2764 | com_addbyte(c, PRINT_EXPR); |
| 2765 | else |
| 2766 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2767 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2768 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2769 | else if (TYPE(CHILD(n,1)) == augassign) |
| 2770 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2771 | else { |
| 2772 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2773 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2774 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2775 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2776 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2777 | com_push(c, 1); |
| 2778 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2779 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2785 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2786 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2787 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2788 | int i; |
| 2789 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2790 | if (Py_OptimizeFlag) |
| 2791 | return; |
| 2792 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2793 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2794 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2795 | raise AssertionError [, <message>] |
| 2796 | |
| 2797 | where <message> is the second test, if present. |
| 2798 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2799 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2800 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2801 | com_addbyte(c, POP_TOP); |
| 2802 | com_pop(c, 1); |
| 2803 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2804 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2805 | com_push(c, 1); |
| 2806 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 2807 | if (i > 1) |
| 2808 | com_node(c, CHILD(n, 3)); |
| 2809 | com_addoparg(c, RAISE_VARARGS, i); |
| 2810 | com_pop(c, i); |
| 2811 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 2812 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2813 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 2814 | com_addbyte(c, POP_TOP); |
| 2815 | } |
| 2816 | |
| 2817 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2818 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2819 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2820 | int i = 1; |
| 2821 | node* stream = NULL; |
| 2822 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2823 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2824 | |
| 2825 | /* are we using the extended print form? */ |
| 2826 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 2827 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2828 | com_node(c, stream); |
| 2829 | /* stack: [...] => [... stream] */ |
| 2830 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2831 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 2832 | i = 4; |
| 2833 | else |
| 2834 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2835 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2836 | for (; i < NCH(n); i += 2) { |
| 2837 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2838 | com_addbyte(c, DUP_TOP); |
| 2839 | /* stack: [stream] => [stream stream] */ |
| 2840 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2841 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2842 | /* stack: [stream stream] => [stream stream obj] */ |
| 2843 | com_addbyte(c, ROT_TWO); |
| 2844 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2845 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2846 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2847 | com_pop(c, 2); |
| 2848 | } |
| 2849 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2850 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2851 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2852 | com_addbyte(c, PRINT_ITEM); |
| 2853 | com_pop(c, 1); |
| 2854 | } |
| 2855 | } |
| 2856 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2857 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2858 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2859 | /* must pop the extra stream object off the stack */ |
| 2860 | com_addbyte(c, POP_TOP); |
| 2861 | /* stack: [... stream] => [...] */ |
| 2862 | com_pop(c, 1); |
| 2863 | } |
| 2864 | } |
| 2865 | else { |
| 2866 | if (stream != NULL) { |
| 2867 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2868 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 2869 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 2870 | com_pop(c, 1); |
| 2871 | } |
| 2872 | else |
| 2873 | com_addbyte(c, PRINT_NEWLINE); |
| 2874 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2878 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2879 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 2880 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2881 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2882 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2883 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2884 | if (c->c_flags & CO_GENERATOR) { |
| 2885 | if (NCH(n) > 1) { |
| 2886 | com_error(c, PyExc_SyntaxError, |
| 2887 | "'return' with argument inside generator"); |
| 2888 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2889 | } |
| 2890 | if (NCH(n) < 2) { |
| 2891 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2892 | com_push(c, 1); |
| 2893 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 2894 | else |
| 2895 | com_node(c, CHILD(n, 1)); |
| 2896 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2897 | com_pop(c, 1); |
| 2898 | } |
| 2899 | |
| 2900 | static void |
| 2901 | com_yield_stmt(struct compiling *c, node *n) |
| 2902 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2903 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2904 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 2905 | if (!c->c_infunction) { |
| 2906 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 2907 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 2908 | |
| 2909 | for (i = 0; i < c->c_nblocks; ++i) { |
| 2910 | if (c->c_block[i] == SETUP_FINALLY) { |
| 2911 | com_error(c, PyExc_SyntaxError, |
| 2912 | "'yield' not allowed in a 'try' block " |
| 2913 | "with a 'finally' clause"); |
| 2914 | return; |
| 2915 | } |
| 2916 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 2917 | com_node(c, CHILD(n, 1)); |
| 2918 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2919 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2923 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2924 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2925 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 2926 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 2927 | if (NCH(n) > 1) { |
| 2928 | com_node(c, CHILD(n, 1)); |
| 2929 | if (NCH(n) > 3) { |
| 2930 | com_node(c, CHILD(n, 3)); |
| 2931 | if (NCH(n) > 5) |
| 2932 | com_node(c, CHILD(n, 5)); |
| 2933 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2934 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2935 | i = NCH(n)/2; |
| 2936 | com_addoparg(c, RAISE_VARARGS, i); |
| 2937 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2941 | com_from_import(struct compiling *c, node *n) |
| 2942 | { |
| 2943 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 2944 | com_push(c, 1); |
| 2945 | if (NCH(n) > 1) { |
| 2946 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 2947 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 2948 | return; |
| 2949 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2950 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2951 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2952 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2953 | com_pop(c, 1); |
| 2954 | } |
| 2955 | |
| 2956 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2957 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2958 | { |
| 2959 | int i; |
| 2960 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2961 | /* 'import' dotted_name (',' dotted_name)* | |
| 2962 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2963 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2964 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 2965 | /* 'from' dotted_name 'import' ... */ |
| 2966 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2967 | |
| 2968 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 2969 | tup = Py_BuildValue("(s)", "*"); |
| 2970 | } else { |
| 2971 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 2972 | for (i = 3; i < NCH(n); i += 2) { |
| 2973 | PyTuple_SET_ITEM(tup, (i-3)/2, |
| 2974 | PyString_FromString(STR( |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 2975 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2976 | } |
| 2977 | } |
| 2978 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 2979 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2980 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2981 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2982 | if (TYPE(CHILD(n, 3)) == STAR) |
| 2983 | com_addbyte(c, IMPORT_STAR); |
| 2984 | else { |
| 2985 | for (i = 3; i < NCH(n); i += 2) |
| 2986 | com_from_import(c, CHILD(n, i)); |
| 2987 | com_addbyte(c, POP_TOP); |
| 2988 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2989 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2990 | } |
| 2991 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2992 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2993 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2994 | node *subn = CHILD(n, i); |
| 2995 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2996 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2997 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 2998 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 2999 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3000 | int j; |
| 3001 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3002 | com_error(c, PyExc_SyntaxError, |
| 3003 | "invalid syntax"); |
| 3004 | return; |
| 3005 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3006 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3007 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3008 | CHILD(CHILD(subn, 0), |
| 3009 | j)); |
| 3010 | com_addop_varname(c, VAR_STORE, |
| 3011 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3012 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3013 | com_addop_varname(c, VAR_STORE, |
| 3014 | STR(CHILD(CHILD(subn, 0), |
| 3015 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3016 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3017 | } |
| 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3022 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3023 | { |
| 3024 | REQ(n, exec_stmt); |
| 3025 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3026 | com_node(c, CHILD(n, 1)); |
| 3027 | if (NCH(n) >= 4) |
| 3028 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3029 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3030 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3031 | com_push(c, 1); |
| 3032 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3033 | if (NCH(n) >= 6) |
| 3034 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3035 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3036 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3037 | com_push(c, 1); |
| 3038 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3039 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3040 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3043 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3044 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3045 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3046 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3047 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3048 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3049 | |
| 3050 | /* Label to avoid tail recursion */ |
| 3051 | next: |
| 3052 | switch (TYPE(n)) { |
| 3053 | |
| 3054 | case suite: |
| 3055 | if (NCH(n) == 1) { |
| 3056 | n = CHILD(n, 0); |
| 3057 | goto next; |
| 3058 | } |
| 3059 | /* Fall through */ |
| 3060 | case file_input: |
| 3061 | for (i = 0; i < NCH(n); i++) { |
| 3062 | node *ch = CHILD(n, i); |
| 3063 | if (TYPE(ch) == stmt) { |
| 3064 | n = ch; |
| 3065 | goto next; |
| 3066 | } |
| 3067 | } |
| 3068 | break; |
| 3069 | |
| 3070 | case stmt: |
| 3071 | case simple_stmt: |
| 3072 | case small_stmt: |
| 3073 | n = CHILD(n, 0); |
| 3074 | goto next; |
| 3075 | |
| 3076 | case expr_stmt: |
| 3077 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3078 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3079 | case test: |
| 3080 | case and_test: |
| 3081 | case not_test: |
| 3082 | case comparison: |
| 3083 | case expr: |
| 3084 | case xor_expr: |
| 3085 | case and_expr: |
| 3086 | case shift_expr: |
| 3087 | case arith_expr: |
| 3088 | case term: |
| 3089 | case factor: |
| 3090 | case power: |
| 3091 | case atom: |
| 3092 | if (NCH(n) == 1) { |
| 3093 | n = CHILD(n, 0); |
| 3094 | goto next; |
| 3095 | } |
| 3096 | break; |
| 3097 | |
| 3098 | case NAME: |
| 3099 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3100 | return 1; |
| 3101 | break; |
| 3102 | |
| 3103 | case NUMBER: |
| 3104 | v = parsenumber(c, STR(n)); |
| 3105 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3106 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3107 | break; |
| 3108 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3109 | i = PyObject_IsTrue(v); |
| 3110 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3111 | return i == 0; |
| 3112 | |
| 3113 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3114 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3115 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3116 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3117 | break; |
| 3118 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3119 | i = PyObject_IsTrue(v); |
| 3120 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3121 | return i == 0; |
| 3122 | |
| 3123 | } |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3127 | |
| 3128 | /* Look under n for a return stmt with an expression. |
| 3129 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3130 | * functions already known to be generators (as determined by the symtable |
| 3131 | * pass). |
| 3132 | * Return the offending return node if found, else NULL. |
| 3133 | */ |
| 3134 | static node * |
| 3135 | look_for_offending_return(node *n) |
| 3136 | { |
| 3137 | int i; |
| 3138 | |
| 3139 | for (i = 0; i < NCH(n); ++i) { |
| 3140 | node *kid = CHILD(n, i); |
| 3141 | |
| 3142 | switch (TYPE(kid)) { |
| 3143 | case classdef: |
| 3144 | case funcdef: |
| 3145 | case lambdef: |
| 3146 | /* Stuff in nested functions & classes doesn't |
| 3147 | affect the code block we started in. */ |
| 3148 | return NULL; |
| 3149 | |
| 3150 | case return_stmt: |
| 3151 | if (NCH(kid) > 1) |
| 3152 | return kid; |
| 3153 | break; |
| 3154 | |
| 3155 | default: { |
| 3156 | node *bad = look_for_offending_return(kid); |
| 3157 | if (bad != NULL) |
| 3158 | return bad; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | return NULL; |
| 3164 | } |
| 3165 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3166 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3167 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3168 | { |
| 3169 | int i; |
| 3170 | int anchor = 0; |
| 3171 | REQ(n, if_stmt); |
| 3172 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3173 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3174 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3175 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3176 | if (is_constant_false(c, ch)) { |
| 3177 | /* We're going to skip this block. However, if this |
| 3178 | is a generator, we have to check the dead code |
| 3179 | anyway to make sure there aren't any return stmts |
| 3180 | with expressions, in the same scope. */ |
| 3181 | if (c->c_flags & CO_GENERATOR) { |
| 3182 | node *p = look_for_offending_return(n); |
| 3183 | if (p != NULL) { |
| 3184 | int savelineno = c->c_lineno; |
| 3185 | c->c_lineno = p->n_lineno; |
| 3186 | com_error(c, PyExc_SyntaxError, |
| 3187 | "'return' with argument " |
| 3188 | "inside generator"); |
| 3189 | c->c_lineno = savelineno; |
| 3190 | } |
| 3191 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3192 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3193 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3194 | if (i > 0) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3195 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3196 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3197 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3198 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3199 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3200 | com_node(c, CHILD(n, i+3)); |
| 3201 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3202 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3203 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3204 | com_addbyte(c, POP_TOP); |
| 3205 | } |
| 3206 | if (i+2 < NCH(n)) |
| 3207 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3208 | if (anchor) |
| 3209 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3213 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3214 | { |
| 3215 | int break_anchor = 0; |
| 3216 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3217 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3218 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3219 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3220 | block_push(c, SETUP_LOOP); |
| 3221 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3222 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3223 | com_node(c, CHILD(n, 1)); |
| 3224 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3225 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3226 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3227 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3228 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3229 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3230 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3231 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3232 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3233 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3234 | com_addbyte(c, POP_TOP); |
| 3235 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3236 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3237 | if (NCH(n) > 4) |
| 3238 | com_node(c, CHILD(n, 6)); |
| 3239 | com_backpatch(c, break_anchor); |
| 3240 | } |
| 3241 | |
| 3242 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3243 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3244 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3245 | int break_anchor = 0; |
| 3246 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3247 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3248 | REQ(n, for_stmt); |
| 3249 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3250 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3251 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3252 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3253 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3254 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3255 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3256 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3257 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3258 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3259 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3260 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3261 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3262 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3263 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3264 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3265 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3266 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3267 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3268 | if (NCH(n) > 8) |
| 3269 | com_node(c, CHILD(n, 8)); |
| 3270 | com_backpatch(c, break_anchor); |
| 3271 | } |
| 3272 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3273 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3274 | |
| 3275 | SETUP_FINALLY L |
| 3276 | <code for S> |
| 3277 | POP_BLOCK |
| 3278 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3279 | L: <code for Sf> |
| 3280 | END_FINALLY |
| 3281 | |
| 3282 | The special instructions use the block stack. Each block |
| 3283 | stack entry contains the instruction that created it (here |
| 3284 | SETUP_FINALLY), the level of the value stack at the time the |
| 3285 | block stack entry was created, and a label (here L). |
| 3286 | |
| 3287 | SETUP_FINALLY: |
| 3288 | Pushes the current value stack level and the label |
| 3289 | onto the block stack. |
| 3290 | POP_BLOCK: |
| 3291 | Pops en entry from the block stack, and pops the value |
| 3292 | stack until its level is the same as indicated on the |
| 3293 | block stack. (The label is ignored.) |
| 3294 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3295 | Pops a variable number of entries from the *value* stack |
| 3296 | and re-raises the exception they specify. The number of |
| 3297 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3298 | |
| 3299 | The block stack is unwound when an exception is raised: |
| 3300 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3301 | onto the value stack (and the exception condition is cleared), |
| 3302 | and the interpreter jumps to the label gotten from the block |
| 3303 | stack. |
| 3304 | |
| 3305 | 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] | 3306 | (The contents of the value stack is shown in [], with the top |
| 3307 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3308 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3309 | |
| 3310 | Value stack Label Instruction Argument |
| 3311 | [] SETUP_EXCEPT L1 |
| 3312 | [] <code for S> |
| 3313 | [] POP_BLOCK |
| 3314 | [] JUMP_FORWARD L0 |
| 3315 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3316 | [tb, val, exc] L1: DUP ) |
| 3317 | [tb, val, exc, exc] <evaluate E1> ) |
| 3318 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3319 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3320 | [tb, val, exc, 1] POP ) |
| 3321 | [tb, val, exc] POP |
| 3322 | [tb, val] <assign to V1> (or POP if no V1) |
| 3323 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3324 | [] <code for S1> |
| 3325 | JUMP_FORWARD L0 |
| 3326 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3327 | [tb, val, exc, 0] L2: POP |
| 3328 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3329 | .............................etc....................... |
| 3330 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3331 | [tb, val, exc, 0] Ln+1: POP |
| 3332 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3333 | |
| 3334 | [] L0: <next statement> |
| 3335 | |
| 3336 | Of course, parts are not generated if Vi or Ei is not present. |
| 3337 | */ |
| 3338 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3339 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3340 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3341 | { |
| 3342 | int except_anchor = 0; |
| 3343 | int end_anchor = 0; |
| 3344 | int else_anchor = 0; |
| 3345 | int i; |
| 3346 | node *ch; |
| 3347 | |
| 3348 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3349 | block_push(c, SETUP_EXCEPT); |
| 3350 | com_node(c, CHILD(n, 2)); |
| 3351 | com_addbyte(c, POP_BLOCK); |
| 3352 | block_pop(c, SETUP_EXCEPT); |
| 3353 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3354 | com_backpatch(c, except_anchor); |
| 3355 | for (i = 3; |
| 3356 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3357 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3358 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3359 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3360 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3361 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3362 | break; |
| 3363 | } |
| 3364 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3365 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3366 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3367 | if (NCH(ch) > 1) { |
| 3368 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3369 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3370 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3371 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3372 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3373 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3374 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3375 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3376 | } |
| 3377 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3378 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3379 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3380 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3381 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3382 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3383 | com_pop(c, 1); |
| 3384 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3385 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3386 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3387 | com_node(c, CHILD(n, i+2)); |
| 3388 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3389 | if (except_anchor) { |
| 3390 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3391 | /* We come in with [tb, val, exc, 0] on the |
| 3392 | stack; one pop and it's the same as |
| 3393 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3394 | com_addbyte(c, POP_TOP); |
| 3395 | } |
| 3396 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3397 | /* We actually come in here with [tb, val, exc] but the |
| 3398 | END_FINALLY will zap those and jump around. |
| 3399 | The c_stacklevel does not reflect them so we need not pop |
| 3400 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3401 | com_addbyte(c, END_FINALLY); |
| 3402 | com_backpatch(c, else_anchor); |
| 3403 | if (i < NCH(n)) |
| 3404 | com_node(c, CHILD(n, i+2)); |
| 3405 | com_backpatch(c, end_anchor); |
| 3406 | } |
| 3407 | |
| 3408 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3409 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3410 | { |
| 3411 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3412 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3413 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3414 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3415 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3416 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3417 | com_addbyte(c, POP_BLOCK); |
| 3418 | block_pop(c, SETUP_FINALLY); |
| 3419 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3420 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3421 | /* While the generated code pushes only one item, |
| 3422 | the try-finally handling can enter here with |
| 3423 | up to three items. OK, here are the details: |
| 3424 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3425 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3426 | com_backpatch(c, finally_anchor); |
| 3427 | ch = CHILD(n, NCH(n)-1); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3428 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3429 | com_node(c, ch); |
| 3430 | com_addbyte(c, END_FINALLY); |
| 3431 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3432 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3436 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3437 | { |
| 3438 | REQ(n, try_stmt); |
| 3439 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3440 | | 'try' ':' suite 'finally' ':' suite */ |
| 3441 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3442 | com_try_finally(c, n); |
| 3443 | else |
| 3444 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3447 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3448 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3449 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3450 | int i; |
| 3451 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3452 | /* Label to avoid tail recursion */ |
| 3453 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3454 | switch (TYPE(n)) { |
| 3455 | |
| 3456 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3457 | if (NCH(n) == 1) { |
| 3458 | n = CHILD(n, 0); |
| 3459 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3460 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3461 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3462 | case file_input: |
| 3463 | for (i = 0; i < NCH(n); i++) { |
| 3464 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3465 | if (TYPE(ch) == stmt) { |
| 3466 | n = ch; |
| 3467 | goto next; |
| 3468 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3469 | } |
| 3470 | break; |
| 3471 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3472 | case stmt: |
| 3473 | case simple_stmt: |
| 3474 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3475 | n = CHILD(n, 0); |
| 3476 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3477 | |
| 3478 | case expr_stmt: |
| 3479 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3480 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3481 | case test: |
| 3482 | case and_test: |
| 3483 | case not_test: |
| 3484 | case comparison: |
| 3485 | case expr: |
| 3486 | case xor_expr: |
| 3487 | case and_expr: |
| 3488 | case shift_expr: |
| 3489 | case arith_expr: |
| 3490 | case term: |
| 3491 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3492 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3493 | if (NCH(n) == 1) { |
| 3494 | n = CHILD(n, 0); |
| 3495 | goto next; |
| 3496 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | |
| 3499 | case atom: |
| 3500 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3501 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | |
| 3504 | } |
| 3505 | return NULL; |
| 3506 | } |
| 3507 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3508 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3509 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3510 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3511 | /* Don't generate doc-strings if run with -OO */ |
| 3512 | if (Py_OptimizeFlag > 1) |
| 3513 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3514 | n = get_rawdocstring(n); |
| 3515 | if (n == NULL) |
| 3516 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3517 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3520 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3521 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3522 | { |
| 3523 | REQ(n, suite); |
| 3524 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3525 | if (NCH(n) == 1) { |
| 3526 | com_node(c, CHILD(n, 0)); |
| 3527 | } |
| 3528 | else { |
| 3529 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3530 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3531 | node *ch = CHILD(n, i); |
| 3532 | if (TYPE(ch) == stmt) |
| 3533 | com_node(c, ch); |
| 3534 | } |
| 3535 | } |
| 3536 | } |
| 3537 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3538 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3539 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3540 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3541 | { |
| 3542 | int i = c->c_nblocks; |
| 3543 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3544 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3545 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3546 | else if (i <= 0) { |
| 3547 | /* at the outer level */ |
| 3548 | com_error(c, PyExc_SyntaxError, |
| 3549 | "'continue' not properly in loop"); |
| 3550 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3551 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3552 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3553 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3554 | if (c->c_block[j] == SETUP_LOOP) |
| 3555 | break; |
| 3556 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3557 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3558 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3559 | for (; i > j; --i) { |
| 3560 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3561 | c->c_block[i] == SETUP_FINALLY) { |
| 3562 | com_addoparg(c, CONTINUE_LOOP, |
| 3563 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3564 | return; |
| 3565 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3566 | if (c->c_block[i] == END_FINALLY) { |
| 3567 | com_error(c, PyExc_SyntaxError, |
| 3568 | "'continue' not supported inside 'finally' clause"); |
| 3569 | return; |
| 3570 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3571 | } |
| 3572 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3573 | com_error(c, PyExc_SyntaxError, |
| 3574 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3575 | } |
| 3576 | /* XXX Could allow it inside a 'finally' clause |
| 3577 | XXX if we could pop the exception still on the stack */ |
| 3578 | } |
| 3579 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3580 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3581 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3582 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3583 | int i, nch, nargs, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3584 | if (TYPE(n) == lambdef) { |
| 3585 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3586 | n = CHILD(n, 1); |
| 3587 | } |
| 3588 | else { |
| 3589 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3590 | n = CHILD(n, 2); |
| 3591 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3592 | n = CHILD(n, 1); |
| 3593 | } |
| 3594 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3595 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3596 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3597 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3598 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3599 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3600 | nargs = 0; |
| 3601 | ndefs = 0; |
| 3602 | for (i = 0; i < nch; i++) { |
| 3603 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3604 | if (TYPE(CHILD(n, i)) == STAR || |
| 3605 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3606 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3607 | nargs++; |
| 3608 | i++; |
| 3609 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3610 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3611 | else |
| 3612 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3613 | if (t == EQUAL) { |
| 3614 | i++; |
| 3615 | ndefs++; |
| 3616 | com_node(c, CHILD(n, i)); |
| 3617 | i++; |
| 3618 | if (i >= nch) |
| 3619 | break; |
| 3620 | t = TYPE(CHILD(n, i)); |
| 3621 | } |
| 3622 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3623 | /* Treat "(a=1, b)" as an error */ |
| 3624 | if (ndefs) |
| 3625 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3626 | "non-default argument follows default argument"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3627 | } |
| 3628 | if (t != COMMA) |
| 3629 | break; |
| 3630 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3631 | return ndefs; |
| 3632 | } |
| 3633 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3634 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3635 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3636 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3637 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3638 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3639 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3640 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3641 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3642 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3643 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3644 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3645 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3646 | c->c_errors++; |
| 3647 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3648 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3649 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3650 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3651 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3652 | if (closure) |
| 3653 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3654 | else |
| 3655 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3656 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3657 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3658 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3659 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3664 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3665 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3666 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3667 | REQ(n, testlist); |
| 3668 | /* testlist: test (',' test)* [','] */ |
| 3669 | for (i = 0; i < NCH(n); i += 2) |
| 3670 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3671 | i = (NCH(n)+1) / 2; |
| 3672 | com_addoparg(c, BUILD_TUPLE, i); |
| 3673 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3677 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3678 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3679 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3680 | PyObject *v; |
| 3681 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3682 | char *name; |
| 3683 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3684 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3685 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 3686 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3687 | c->c_errors++; |
| 3688 | return; |
| 3689 | } |
| 3690 | /* Push the class name on the stack */ |
| 3691 | i = com_addconst(c, v); |
| 3692 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3693 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3694 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3695 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3696 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3697 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3698 | com_push(c, 1); |
| 3699 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 3700 | else |
| 3701 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3702 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3703 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3704 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3705 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3706 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3707 | c->c_errors++; |
| 3708 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3709 | int closure = com_make_closure(c, co); |
| 3710 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3711 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3712 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3713 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3714 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 3715 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3716 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3717 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3718 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3719 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3720 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3721 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 3722 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3723 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3724 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3728 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3729 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3730 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3731 | if (c->c_errors) |
| 3732 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3733 | switch (TYPE(n)) { |
| 3734 | |
| 3735 | /* Definition nodes */ |
| 3736 | |
| 3737 | case funcdef: |
| 3738 | com_funcdef(c, n); |
| 3739 | break; |
| 3740 | case classdef: |
| 3741 | com_classdef(c, n); |
| 3742 | break; |
| 3743 | |
| 3744 | /* Trivial parse tree nodes */ |
| 3745 | |
| 3746 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3747 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3748 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3749 | n = CHILD(n, 0); |
| 3750 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3751 | |
| 3752 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3753 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3754 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3755 | { |
| 3756 | int i; |
| 3757 | for (i = 0; i < NCH(n)-1; i += 2) |
| 3758 | com_node(c, CHILD(n, i)); |
| 3759 | } |
| 3760 | break; |
| 3761 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3762 | case compound_stmt: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3763 | com_set_lineno(c, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3764 | n = CHILD(n, 0); |
| 3765 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3766 | |
| 3767 | /* Statement nodes */ |
| 3768 | |
| 3769 | case expr_stmt: |
| 3770 | com_expr_stmt(c, n); |
| 3771 | break; |
| 3772 | case print_stmt: |
| 3773 | com_print_stmt(c, n); |
| 3774 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3775 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3776 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3777 | break; |
| 3778 | case pass_stmt: |
| 3779 | break; |
| 3780 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3781 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3782 | com_error(c, PyExc_SyntaxError, |
| 3783 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3784 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3785 | com_addbyte(c, BREAK_LOOP); |
| 3786 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3787 | case continue_stmt: |
| 3788 | com_continue_stmt(c, n); |
| 3789 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3790 | case return_stmt: |
| 3791 | com_return_stmt(c, n); |
| 3792 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3793 | case yield_stmt: |
| 3794 | com_yield_stmt(c, n); |
| 3795 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3796 | case raise_stmt: |
| 3797 | com_raise_stmt(c, n); |
| 3798 | break; |
| 3799 | case import_stmt: |
| 3800 | com_import_stmt(c, n); |
| 3801 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3802 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 3803 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3804 | case exec_stmt: |
| 3805 | com_exec_stmt(c, n); |
| 3806 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3807 | case assert_stmt: |
| 3808 | com_assert_stmt(c, n); |
| 3809 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3810 | case if_stmt: |
| 3811 | com_if_stmt(c, n); |
| 3812 | break; |
| 3813 | case while_stmt: |
| 3814 | com_while_stmt(c, n); |
| 3815 | break; |
| 3816 | case for_stmt: |
| 3817 | com_for_stmt(c, n); |
| 3818 | break; |
| 3819 | case try_stmt: |
| 3820 | com_try_stmt(c, n); |
| 3821 | break; |
| 3822 | case suite: |
| 3823 | com_suite(c, n); |
| 3824 | break; |
| 3825 | |
| 3826 | /* Expression nodes */ |
| 3827 | |
| 3828 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3829 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 3830 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3831 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3832 | break; |
| 3833 | case test: |
| 3834 | com_test(c, n); |
| 3835 | break; |
| 3836 | case and_test: |
| 3837 | com_and_test(c, n); |
| 3838 | break; |
| 3839 | case not_test: |
| 3840 | com_not_test(c, n); |
| 3841 | break; |
| 3842 | case comparison: |
| 3843 | com_comparison(c, n); |
| 3844 | break; |
| 3845 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 3846 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3847 | break; |
| 3848 | case expr: |
| 3849 | com_expr(c, n); |
| 3850 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 3851 | case xor_expr: |
| 3852 | com_xor_expr(c, n); |
| 3853 | break; |
| 3854 | case and_expr: |
| 3855 | com_and_expr(c, n); |
| 3856 | break; |
| 3857 | case shift_expr: |
| 3858 | com_shift_expr(c, n); |
| 3859 | break; |
| 3860 | case arith_expr: |
| 3861 | com_arith_expr(c, n); |
| 3862 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3863 | case term: |
| 3864 | com_term(c, n); |
| 3865 | break; |
| 3866 | case factor: |
| 3867 | com_factor(c, n); |
| 3868 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3869 | case power: |
| 3870 | com_power(c, n); |
| 3871 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3872 | case atom: |
| 3873 | com_atom(c, n); |
| 3874 | break; |
| 3875 | |
| 3876 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3877 | com_error(c, PyExc_SystemError, |
| 3878 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3879 | } |
| 3880 | } |
| 3881 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 3882 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3883 | |
| 3884 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3885 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3886 | { |
| 3887 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3888 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 3889 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3890 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3891 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3892 | com_pop(c, 1); |
| 3893 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3897 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3898 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 3899 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3900 | if (NCH(n) == 1) { |
| 3901 | com_fpdef(c, CHILD(n, 0)); |
| 3902 | } |
| 3903 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3904 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3905 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3906 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3907 | for (i = 0; i < NCH(n); i += 2) |
| 3908 | com_fpdef(c, CHILD(n, i)); |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3913 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3914 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3915 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3916 | int complex = 0; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3917 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3918 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 3919 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3920 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3921 | nch = NCH(n); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3922 | /* Enter all arguments in table of locals */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3923 | for (i = 0, narg = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3924 | node *ch = CHILD(n, i); |
| 3925 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3926 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3927 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3928 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3929 | fp = CHILD(ch, 0); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 3930 | if (TYPE(fp) != NAME) { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 3931 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 3932 | complex = 1; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3933 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3934 | narg++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3935 | /* all name updates handled by symtable */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3936 | if (++i >= nch) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3937 | break; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3938 | ch = CHILD(n, i); |
| 3939 | if (TYPE(ch) == EQUAL) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3940 | i += 2; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3941 | else |
| 3942 | REQ(ch, COMMA); |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3943 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3944 | if (complex) { |
| 3945 | /* Generate code for complex arguments only after |
| 3946 | having counted the simple arguments */ |
| 3947 | int ilocal = 0; |
| 3948 | for (i = 0; i < nch; i++) { |
| 3949 | node *ch = CHILD(n, i); |
| 3950 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3951 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3952 | break; |
| 3953 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 3954 | fp = CHILD(ch, 0); |
| 3955 | if (TYPE(fp) != NAME) { |
| 3956 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3957 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3958 | com_fpdef(c, ch); |
| 3959 | } |
| 3960 | ilocal++; |
| 3961 | if (++i >= nch) |
| 3962 | break; |
| 3963 | ch = CHILD(n, i); |
| 3964 | if (TYPE(ch) == EQUAL) |
| 3965 | i += 2; |
| 3966 | else |
| 3967 | REQ(ch, COMMA); |
| 3968 | } |
| 3969 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3973 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3974 | { |
| 3975 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3976 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3977 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3978 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3979 | if (doc != NULL) { |
| 3980 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3981 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3982 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3983 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3984 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3985 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3986 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3987 | for (i = 0; i < NCH(n); i++) { |
| 3988 | node *ch = CHILD(n, i); |
| 3989 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 3990 | com_node(c, ch); |
| 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | /* Top-level compile-node interface */ |
| 3995 | |
| 3996 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3997 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3998 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3999 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4000 | node *ch; |
| 4001 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4002 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4003 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4004 | if (doc != NULL) { |
| 4005 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4006 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4007 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4008 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4009 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4010 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4011 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4012 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4013 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4014 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4015 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4016 | c->c_infunction = 0; |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4017 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4018 | com_push(c, 1); |
| 4019 | com_addbyte(c, RETURN_VALUE); |
| 4020 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
| 4023 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4024 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4025 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4026 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4027 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4028 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4029 | |
| 4030 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4031 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4032 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4033 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4034 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4035 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4036 | else |
| 4037 | ch = CHILD(n, 2); |
| 4038 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4039 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4040 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4044 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4045 | { |
| 4046 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4047 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4048 | REQ(n, classdef); |
| 4049 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4050 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4051 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4052 | /* Initialize local __module__ from global __name__ */ |
| 4053 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4054 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4055 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4056 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4057 | if (doc != NULL) { |
| 4058 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4059 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4060 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4061 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4062 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4063 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4064 | } |
| 4065 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4066 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4067 | com_node(c, ch); |
| 4068 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4069 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4070 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4071 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4075 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4076 | { |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4077 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4078 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4079 | switch (TYPE(n)) { |
| 4080 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4081 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4082 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4083 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4084 | n = CHILD(n, 0); |
| 4085 | if (TYPE(n) != NEWLINE) |
| 4086 | com_node(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4087 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4088 | com_push(c, 1); |
| 4089 | com_addbyte(c, RETURN_VALUE); |
| 4090 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4091 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4092 | break; |
| 4093 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4094 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4095 | com_file_input(c, n); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4096 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4097 | com_push(c, 1); |
| 4098 | com_addbyte(c, RETURN_VALUE); |
| 4099 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4100 | break; |
| 4101 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4102 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4103 | com_node(c, CHILD(n, 0)); |
| 4104 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4105 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4106 | break; |
| 4107 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4108 | case lambdef: /* anonymous function definition */ |
| 4109 | compile_lambdef(c, n); |
| 4110 | break; |
| 4111 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4112 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4113 | compile_funcdef(c, n); |
| 4114 | break; |
| 4115 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4116 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4117 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4118 | break; |
| 4119 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4120 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4121 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4122 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4123 | } |
| 4124 | } |
| 4125 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4126 | static PyObject * |
| 4127 | dict_keys_inorder(PyObject *dict, int offset) |
| 4128 | { |
| 4129 | PyObject *tuple, *k, *v; |
| 4130 | int i, pos = 0, size = PyDict_Size(dict); |
| 4131 | |
| 4132 | tuple = PyTuple_New(size); |
| 4133 | if (tuple == NULL) |
| 4134 | return NULL; |
| 4135 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4136 | i = PyInt_AS_LONG(v); |
| 4137 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4138 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4139 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4140 | } |
| 4141 | return tuple; |
| 4142 | } |
| 4143 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4144 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4145 | PyNode_Compile(node *n, char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4146 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4147 | return PyNode_CompileFlags(n, filename, NULL); |
| 4148 | } |
| 4149 | |
| 4150 | PyCodeObject * |
| 4151 | PyNode_CompileFlags(node *n, char *filename, PyCompilerFlags *flags) |
| 4152 | { |
| 4153 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4154 | } |
| 4155 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4156 | struct symtable * |
| 4157 | PyNode_CompileSymtable(node *n, char *filename) |
| 4158 | { |
| 4159 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4160 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4161 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4162 | ff = PyNode_Future(n, filename); |
| 4163 | if (ff == NULL) |
| 4164 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4165 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4166 | st = symtable_init(); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4167 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4168 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4169 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4170 | } |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4171 | st->st_future = ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4172 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4173 | if (st->st_errors > 0) |
| 4174 | goto fail; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4175 | symtable_node(st, n); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4176 | if (st->st_errors > 0) |
| 4177 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4178 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4179 | return st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4180 | fail: |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4181 | PyObject_FREE((void *)ff); |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4182 | st->st_future = NULL; |
| 4183 | PySymtable_Free(st); |
| 4184 | return NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4187 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4188 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4189 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4190 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4193 | static PyCodeObject * |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4194 | jcompile(node *n, char *filename, struct compiling *base, |
| 4195 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4196 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4197 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4198 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4199 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4200 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4201 | if (TYPE(n) == encoding_decl) { |
| 4202 | sc.c_encoding = STR(n); |
| 4203 | n = CHILD(n, 0); |
| 4204 | } else { |
| 4205 | sc.c_encoding = NULL; |
| 4206 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4207 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4208 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4209 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4210 | /* c_symtable still points to parent's symbols */ |
| 4211 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4212 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4213 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4214 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4215 | if (base->c_encoding != NULL) { |
| 4216 | assert(sc.c_encoding == NULL); |
| 4217 | sc.c_encoding = base->c_encoding; |
| 4218 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4219 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4220 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4221 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4222 | if (sc.c_future == NULL) { |
| 4223 | com_free(&sc); |
| 4224 | return NULL; |
| 4225 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4226 | if (flags) { |
| 4227 | int merged = sc.c_future->ff_features | |
| 4228 | flags->cf_flags; |
| 4229 | sc.c_future->ff_features = merged; |
| 4230 | flags->cf_flags = merged; |
| 4231 | } |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4232 | if (symtable_build(&sc, n) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4233 | com_free(&sc); |
| 4234 | return NULL; |
| 4235 | } |
| 4236 | } |
| 4237 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4238 | if (symtable_load_symbols(&sc) < 0) { |
| 4239 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4240 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4241 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4242 | compile_node(&sc, n); |
| 4243 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4244 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4245 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4246 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4247 | consts = PyList_AsTuple(sc.c_consts); |
| 4248 | names = PyList_AsTuple(sc.c_names); |
| 4249 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4250 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4251 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4252 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4253 | filename = PyString_InternFromString(sc.c_filename); |
| 4254 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4255 | if (!PyErr_Occurred()) |
| 4256 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4257 | sc.c_nlocals, |
| 4258 | sc.c_maxstacklevel, |
| 4259 | sc.c_flags, |
| 4260 | sc.c_code, |
| 4261 | consts, |
| 4262 | names, |
| 4263 | varnames, |
| 4264 | freevars, |
| 4265 | cellvars, |
| 4266 | filename, |
| 4267 | name, |
| 4268 | sc.c_firstlineno, |
| 4269 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4270 | Py_XDECREF(consts); |
| 4271 | Py_XDECREF(names); |
| 4272 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4273 | Py_XDECREF(freevars); |
| 4274 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4275 | Py_XDECREF(filename); |
| 4276 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4277 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4278 | else if (!PyErr_Occurred()) { |
| 4279 | /* This could happen if someone called PyErr_Clear() after an |
| 4280 | error was reported above. That's not supposed to happen, |
| 4281 | but I just plugged one case and I'm not sure there can't be |
| 4282 | others. In that case, raise SystemError so that at least |
| 4283 | it gets reported instead dumping core. */ |
| 4284 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4285 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4286 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4287 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4288 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4289 | sc.c_symtable = NULL; |
| 4290 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4291 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4292 | return co; |
| 4293 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4294 | |
| 4295 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4296 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4297 | { |
| 4298 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4299 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4300 | int line = co->co_firstlineno; |
| 4301 | int addr = 0; |
| 4302 | while (--size >= 0) { |
| 4303 | addr += *p++; |
| 4304 | if (addr > addrq) |
| 4305 | break; |
| 4306 | line += *p++; |
| 4307 | } |
| 4308 | return line; |
| 4309 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4310 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4311 | /* The test for LOCAL must come before the test for FREE in order to |
| 4312 | handle classes where name is both local and free. The local var is |
| 4313 | a method and the free var is a free var referenced within a method. |
| 4314 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4315 | |
| 4316 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4317 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4318 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4319 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4320 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4321 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4322 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4323 | return CELL; |
| 4324 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4325 | return LOCAL; |
| 4326 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4327 | return FREE; |
| 4328 | v = PyDict_GetItemString(c->c_globals, name); |
| 4329 | if (v) { |
| 4330 | if (v == Py_None) |
| 4331 | return GLOBAL_EXPLICIT; |
| 4332 | else { |
| 4333 | return GLOBAL_IMPLICIT; |
| 4334 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4335 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4336 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4337 | "unknown scope for %.100s in %.100s(%s) " |
| 4338 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4339 | name, c->c_name, |
| 4340 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4341 | c->c_filename, |
| 4342 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4343 | PyObject_REPR(c->c_locals), |
| 4344 | PyObject_REPR(c->c_globals) |
| 4345 | ); |
| 4346 | |
| 4347 | Py_FatalError(buf); |
| 4348 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4351 | /* Helper functions to issue warnings */ |
| 4352 | |
| 4353 | static int |
| 4354 | issue_warning(char *msg, char *filename, int lineno) |
| 4355 | { |
| 4356 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4357 | lineno, NULL, NULL) < 0) { |
| 4358 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4359 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4360 | PyErr_SyntaxLocation(filename, lineno); |
| 4361 | } |
| 4362 | return -1; |
| 4363 | } |
| 4364 | return 0; |
| 4365 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4366 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4367 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4368 | symtable_warn(struct symtable *st, char *msg) |
| 4369 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4370 | 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] | 4371 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4372 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4373 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4374 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4377 | /* Helper function for setting lineno and filename */ |
| 4378 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4379 | static int |
| 4380 | symtable_build(struct compiling *c, node *n) |
| 4381 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4382 | if ((c->c_symtable = symtable_init()) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4383 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 4384 | c->c_symtable->st_future = c->c_future; |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4385 | c->c_symtable->st_filename = c->c_filename; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4386 | symtable_enter_scope(c->c_symtable, TOP, TYPE(n), n->n_lineno); |
| 4387 | if (c->c_symtable->st_errors > 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4388 | return -1; |
| 4389 | symtable_node(c->c_symtable, n); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4390 | if (c->c_symtable->st_errors > 0) |
| 4391 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4392 | /* reset for second pass */ |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4393 | c->c_symtable->st_nscopes = 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4394 | c->c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4395 | return 0; |
| 4396 | } |
| 4397 | |
| 4398 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4399 | symtable_init_compiling_symbols(struct compiling *c) |
| 4400 | { |
| 4401 | PyObject *varnames; |
| 4402 | |
| 4403 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4404 | if (varnames == NULL) { |
| 4405 | varnames = PyList_New(0); |
| 4406 | if (varnames == NULL) |
| 4407 | return -1; |
| 4408 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4409 | Py_INCREF(varnames); |
| 4410 | } else |
| 4411 | Py_INCREF(varnames); |
| 4412 | c->c_varnames = varnames; |
| 4413 | |
| 4414 | c->c_globals = PyDict_New(); |
| 4415 | if (c->c_globals == NULL) |
| 4416 | return -1; |
| 4417 | c->c_freevars = PyDict_New(); |
| 4418 | if (c->c_freevars == NULL) |
| 4419 | return -1; |
| 4420 | c->c_cellvars = PyDict_New(); |
| 4421 | if (c->c_cellvars == NULL) |
| 4422 | return -1; |
| 4423 | return 0; |
| 4424 | } |
| 4425 | |
| 4426 | struct symbol_info { |
| 4427 | int si_nlocals; |
| 4428 | int si_ncells; |
| 4429 | int si_nfrees; |
| 4430 | int si_nimplicit; |
| 4431 | }; |
| 4432 | |
| 4433 | static void |
| 4434 | symtable_init_info(struct symbol_info *si) |
| 4435 | { |
| 4436 | si->si_nlocals = 0; |
| 4437 | si->si_ncells = 0; |
| 4438 | si->si_nfrees = 0; |
| 4439 | si->si_nimplicit = 0; |
| 4440 | } |
| 4441 | |
| 4442 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4443 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4444 | struct symbol_info *si) |
| 4445 | { |
| 4446 | PyObject *dict, *v; |
| 4447 | |
| 4448 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4449 | it indicates a local that we must allocate storage for (a |
| 4450 | cell var). If it occurs in a class, then the class has a |
| 4451 | method and a free variable with the same name. |
| 4452 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4453 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4454 | /* If it isn't declared locally, it can't be a cell. */ |
| 4455 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4456 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4457 | v = PyInt_FromLong(si->si_ncells++); |
| 4458 | dict = c->c_cellvars; |
| 4459 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4460 | /* If it is free anyway, then there is no need to do |
| 4461 | anything here. |
| 4462 | */ |
| 4463 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4464 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4465 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4466 | v = PyInt_FromLong(si->si_nfrees++); |
| 4467 | dict = c->c_freevars; |
| 4468 | } |
| 4469 | if (v == NULL) |
| 4470 | return -1; |
| 4471 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4472 | Py_DECREF(v); |
| 4473 | return -1; |
| 4474 | } |
| 4475 | Py_DECREF(v); |
| 4476 | return 0; |
| 4477 | } |
| 4478 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4479 | /* If a variable is a cell and an argument, make sure that appears in |
| 4480 | co_cellvars before any variable to its right in varnames. |
| 4481 | */ |
| 4482 | |
| 4483 | |
| 4484 | static int |
| 4485 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4486 | PyObject *varnames, int flags) |
| 4487 | { |
| 4488 | PyObject *v, *w, *d, *list = NULL; |
| 4489 | int i, pos; |
| 4490 | |
| 4491 | if (flags & CO_VARARGS) |
| 4492 | argcount++; |
| 4493 | if (flags & CO_VARKEYWORDS) |
| 4494 | argcount++; |
| 4495 | for (i = argcount; --i >= 0; ) { |
| 4496 | v = PyList_GET_ITEM(varnames, i); |
| 4497 | if (PyDict_GetItem(*cellvars, v)) { |
| 4498 | if (list == NULL) { |
| 4499 | list = PyList_New(1); |
| 4500 | if (list == NULL) |
| 4501 | return -1; |
| 4502 | PyList_SET_ITEM(list, 0, v); |
| 4503 | Py_INCREF(v); |
| 4504 | } else |
| 4505 | PyList_Insert(list, 0, v); |
| 4506 | } |
| 4507 | } |
| 4508 | if (list == NULL || PyList_GET_SIZE(list) == 0) |
| 4509 | return 0; |
| 4510 | /* There are cellvars that are also arguments. Create a dict |
| 4511 | to replace cellvars and put the args at the front. |
| 4512 | */ |
| 4513 | d = PyDict_New(); |
| 4514 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4515 | v = PyInt_FromLong(i); |
| 4516 | if (v == NULL) |
| 4517 | goto fail; |
| 4518 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4519 | goto fail; |
| 4520 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4521 | goto fail; |
| 4522 | } |
| 4523 | pos = 0; |
| 4524 | i = PyList_GET_SIZE(list); |
| 4525 | Py_DECREF(list); |
| 4526 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4527 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
| 4528 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4529 | Py_DECREF(w); |
| 4530 | goto fail; |
| 4531 | } |
| 4532 | Py_DECREF(w); |
| 4533 | } |
| 4534 | Py_DECREF(*cellvars); |
| 4535 | *cellvars = d; |
| 4536 | return 1; |
| 4537 | fail: |
| 4538 | Py_DECREF(d); |
| 4539 | return -1; |
| 4540 | } |
| 4541 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4542 | static int |
| 4543 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4544 | { |
| 4545 | PyObject *name, *v; |
| 4546 | int pos; |
| 4547 | |
| 4548 | /* The cell vars are the first elements of the closure, |
| 4549 | followed by the free vars. Update the offsets in |
| 4550 | c_freevars to account for number of cellvars. */ |
| 4551 | pos = 0; |
| 4552 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4553 | int i = PyInt_AS_LONG(v) + offset; |
| 4554 | PyObject *o = PyInt_FromLong(i); |
| 4555 | if (o == NULL) |
| 4556 | return -1; |
| 4557 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4558 | Py_DECREF(o); |
| 4559 | return -1; |
| 4560 | } |
| 4561 | Py_DECREF(o); |
| 4562 | } |
| 4563 | return 0; |
| 4564 | } |
| 4565 | |
| 4566 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4567 | symtable_check_unoptimized(struct compiling *c, |
| 4568 | PySymtableEntryObject *ste, |
| 4569 | struct symbol_info *si) |
| 4570 | { |
| 4571 | char buf[300]; |
| 4572 | |
| 4573 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4574 | || (ste->ste_nested && si->si_nimplicit))) |
| 4575 | return 0; |
| 4576 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4577 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4578 | |
| 4579 | #define ILLEGAL_IS "is a nested function" |
| 4580 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4581 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4582 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4583 | |
| 4584 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4585 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4586 | |
| 4587 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4588 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4589 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4590 | |
| 4591 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4592 | should be stored so the exception can point to them. */ |
| 4593 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4594 | if (ste->ste_child_free) { |
| 4595 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4596 | PyOS_snprintf(buf, sizeof(buf), |
| 4597 | ILLEGAL_IMPORT_STAR, |
| 4598 | PyString_AS_STRING(ste->ste_name), |
| 4599 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4600 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4601 | PyOS_snprintf(buf, sizeof(buf), |
| 4602 | ILLEGAL_BARE_EXEC, |
| 4603 | PyString_AS_STRING(ste->ste_name), |
| 4604 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4605 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4606 | PyOS_snprintf(buf, sizeof(buf), |
| 4607 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4608 | PyString_AS_STRING(ste->ste_name), |
| 4609 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4610 | } |
| 4611 | } else { |
| 4612 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4613 | PyOS_snprintf(buf, sizeof(buf), |
| 4614 | ILLEGAL_IMPORT_STAR, |
| 4615 | PyString_AS_STRING(ste->ste_name), |
| 4616 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4617 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4618 | PyOS_snprintf(buf, sizeof(buf), |
| 4619 | ILLEGAL_BARE_EXEC, |
| 4620 | PyString_AS_STRING(ste->ste_name), |
| 4621 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4622 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4623 | PyOS_snprintf(buf, sizeof(buf), |
| 4624 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 4625 | PyString_AS_STRING(ste->ste_name), |
| 4626 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4627 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4628 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4629 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4630 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 4631 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 4632 | ste->ste_opt_lineno); |
| 4633 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4637 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 4638 | struct symbol_info *si) |
| 4639 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 4640 | if (c->c_future) |
| 4641 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4642 | if (ste->ste_generator) |
| 4643 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4644 | if (ste->ste_type != TYPE_MODULE) |
| 4645 | c->c_flags |= CO_NEWLOCALS; |
| 4646 | if (ste->ste_type == TYPE_FUNCTION) { |
| 4647 | c->c_nlocals = si->si_nlocals; |
| 4648 | if (ste->ste_optimized == 0) |
| 4649 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4650 | else if (ste->ste_optimized != OPT_EXEC) |
| 4651 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4652 | } |
| 4653 | return 0; |
| 4654 | } |
| 4655 | |
| 4656 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4657 | symtable_load_symbols(struct compiling *c) |
| 4658 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4659 | static PyObject *implicit = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4660 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4661 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4662 | PyObject *name, *varnames, *v; |
| 4663 | int i, flags, pos; |
| 4664 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4665 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4666 | if (implicit == NULL) { |
| 4667 | implicit = PyInt_FromLong(1); |
| 4668 | if (implicit == NULL) |
| 4669 | return -1; |
| 4670 | } |
| 4671 | v = NULL; |
| 4672 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4673 | if (symtable_init_compiling_symbols(c) < 0) |
| 4674 | goto fail; |
| 4675 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4676 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4677 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 4678 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4679 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4680 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4681 | v = PyInt_FromLong(i); |
| 4682 | if (PyDict_SetItem(c->c_locals, |
| 4683 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4684 | goto fail; |
| 4685 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | /* XXX The cases below define the rules for whether a name is |
| 4689 | local or global. The logic could probably be clearer. */ |
| 4690 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4691 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 4692 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4693 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4694 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4695 | /* undo the original DEF_FREE */ |
| 4696 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4697 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4698 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4699 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4700 | 2. Free variables in methods that are also class |
| 4701 | variables or declared global. |
| 4702 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4703 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4704 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4705 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4706 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4707 | c->c_argcount--; |
| 4708 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4709 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4710 | c->c_argcount--; |
| 4711 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4712 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4713 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4714 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4715 | if (flags & DEF_PARAM) { |
| 4716 | PyErr_Format(PyExc_SyntaxError, LOCAL_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4717 | PyString_AS_STRING(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 4718 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4719 | ste->ste_lineno); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4720 | st->st_errors++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4721 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4722 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4723 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 4724 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4725 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4726 | si.si_nimplicit++; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4727 | if (PyDict_SetItem(c->c_globals, name, implicit) < 0) |
| 4728 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4729 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4730 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4731 | if (v == NULL) |
| 4732 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4733 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4734 | goto fail; |
| 4735 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4736 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4737 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4738 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4739 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4740 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4741 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4742 | if (v == NULL) |
| 4743 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4744 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4745 | goto fail; |
| 4746 | Py_DECREF(v); |
| 4747 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4748 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4749 | if (PyDict_SetItem(c->c_globals, name, |
| 4750 | implicit) < 0) |
| 4751 | goto fail; |
| 4752 | if (st->st_nscopes != 1) { |
| 4753 | v = PyInt_FromLong(flags); |
| 4754 | if (PyDict_SetItem(st->st_global, |
| 4755 | name, v)) |
| 4756 | goto fail; |
| 4757 | Py_DECREF(v); |
| 4758 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4759 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4760 | } |
| 4761 | } |
| 4762 | |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4763 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 4764 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4765 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 4766 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 4767 | c->c_varnames, c->c_flags) < 0) |
| 4768 | return -1; |
| 4769 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4770 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 4771 | return -1; |
| 4772 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4773 | fail: |
| 4774 | /* is this always the right thing to do? */ |
| 4775 | Py_XDECREF(v); |
| 4776 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
| 4779 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4780 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4781 | { |
| 4782 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4783 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4784 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4785 | if (st == NULL) |
| 4786 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4787 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 4788 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 4789 | st->st_filename = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4790 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4791 | goto fail; |
| 4792 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 4793 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4794 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4795 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4796 | st->st_errors = 0; |
| 4797 | st->st_tmpname = 0; |
| 4798 | st->st_private = NULL; |
| 4799 | return st; |
| 4800 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4801 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4802 | return NULL; |
| 4803 | } |
| 4804 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4805 | void |
| 4806 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4807 | { |
| 4808 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4809 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4810 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4811 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4814 | /* When the compiler exits a scope, it must should update the scope's |
| 4815 | free variable information with the list of free variables in its |
| 4816 | children. |
| 4817 | |
| 4818 | Variables that are free in children and defined in the current |
| 4819 | scope are cellvars. |
| 4820 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4821 | 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] | 4822 | false), free variables in children that are not defined here are |
| 4823 | implicit globals. |
| 4824 | |
| 4825 | */ |
| 4826 | |
| 4827 | static int |
| 4828 | symtable_update_free_vars(struct symtable *st) |
| 4829 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4830 | int i, j, def; |
| 4831 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4832 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4833 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4834 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4835 | def = DEF_FREE_CLASS; |
| 4836 | else |
| 4837 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4838 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4839 | int pos = 0; |
| 4840 | |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4841 | if (list) |
| 4842 | PyList_SetSlice(list, 0, |
| 4843 | ((PyVarObject*)list)->ob_size, 0); |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4844 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4845 | PyList_GET_ITEM(ste->ste_children, i); |
| 4846 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4847 | int flags = PyInt_AS_LONG(o); |
| 4848 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4849 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4850 | if (list == NULL) { |
| 4851 | list = PyList_New(0); |
| 4852 | if (list == NULL) |
| 4853 | return -1; |
| 4854 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4855 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4856 | if (PyList_Append(list, name) < 0) { |
| 4857 | Py_DECREF(list); |
| 4858 | return -1; |
| 4859 | } |
| 4860 | } |
| 4861 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4862 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4863 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4864 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 4865 | /* If a name N is declared global in scope A and |
| 4866 | referenced in scope B contained (perhaps |
| 4867 | indirectly) in A and there are no scopes |
| 4868 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4869 | is global in B. Unless A is a class scope, |
| 4870 | because class scopes are not considered for |
| 4871 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4872 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4873 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4874 | int flags = PyInt_AS_LONG(v); |
| 4875 | if (flags & DEF_GLOBAL) { |
| 4876 | symtable_undo_free(st, child->ste_id, |
| 4877 | name); |
| 4878 | continue; |
| 4879 | } |
| 4880 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4881 | if (ste->ste_nested) { |
| 4882 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4883 | name, def) < 0) { |
| 4884 | Py_DECREF(list); |
| 4885 | return -1; |
| 4886 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4887 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4888 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4889 | name) < 0) { |
| 4890 | Py_DECREF(list); |
| 4891 | return -1; |
| 4892 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4896 | |
| 4897 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4898 | return 0; |
| 4899 | } |
| 4900 | |
| 4901 | /* If the current scope is a non-nested class or if name is not |
| 4902 | defined in the current, non-nested scope, then it is an implicit |
| 4903 | global in all nested scopes. |
| 4904 | */ |
| 4905 | |
| 4906 | static int |
| 4907 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 4908 | { |
| 4909 | PyObject *o; |
| 4910 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4911 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4912 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4913 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4914 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4915 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4916 | if (o == NULL) |
| 4917 | return symtable_undo_free(st, child, name); |
| 4918 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4919 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 4920 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4921 | return symtable_undo_free(st, child, name); |
| 4922 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4923 | return symtable_add_def_o(st, ste->ste_symbols, |
| 4924 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
| 4927 | static int |
| 4928 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 4929 | PyObject *name) |
| 4930 | { |
| 4931 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4932 | PyObject *info; |
| 4933 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4934 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4935 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 4936 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4937 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 4938 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4939 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4940 | if (info == NULL) |
| 4941 | return 0; |
| 4942 | v = PyInt_AS_LONG(info); |
| 4943 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4944 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4945 | DEF_FREE_GLOBAL) < 0) |
| 4946 | return -1; |
| 4947 | } else |
| 4948 | /* If the name is defined here or declared global, |
| 4949 | then the recursion stops. */ |
| 4950 | return 0; |
| 4951 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4952 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 4953 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4954 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4955 | PyList_GET_ITEM(ste->ste_children, i); |
| 4956 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4957 | if (x < 0) |
| 4958 | return x; |
| 4959 | } |
| 4960 | return 0; |
| 4961 | } |
| 4962 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4963 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 4964 | This reference is released when the scope is exited, via the DECREF |
| 4965 | in symtable_exit_scope(). |
| 4966 | */ |
| 4967 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4968 | static int |
| 4969 | symtable_exit_scope(struct symtable *st) |
| 4970 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4971 | int end; |
| 4972 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4973 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4974 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4975 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4976 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4977 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 4978 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4979 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 4980 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4981 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4982 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4983 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4984 | static void |
| 4985 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 4986 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4987 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4988 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4989 | |
| 4990 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4991 | prev = st->st_cur; |
| 4992 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 4993 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4994 | st->st_errors++; |
| 4995 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4996 | } |
| 4997 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 4998 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4999 | PySymtableEntry_New(st, name, type, lineno); |
| 5000 | if (strcmp(name, TOP) == 0) |
| 5001 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5002 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5003 | if (PyList_Append(prev->ste_children, |
| 5004 | (PyObject *)st->st_cur) < 0) |
| 5005 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5006 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5007 | } |
| 5008 | |
| 5009 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5010 | symtable_lookup(struct symtable *st, char *name) |
| 5011 | { |
| 5012 | char buffer[MANGLE_LEN]; |
| 5013 | PyObject *v; |
| 5014 | int flags; |
| 5015 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5016 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5017 | name = buffer; |
| 5018 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5019 | if (v == NULL) { |
| 5020 | if (PyErr_Occurred()) |
| 5021 | return -1; |
| 5022 | else |
| 5023 | return 0; |
| 5024 | } |
| 5025 | |
| 5026 | flags = PyInt_AS_LONG(v); |
| 5027 | return flags; |
| 5028 | } |
| 5029 | |
| 5030 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5031 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5032 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5033 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5034 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5035 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5036 | |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5037 | /* Warn about None, except inside a tuple (where the assignment |
| 5038 | code already issues a warning). */ |
| 5039 | if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) && |
| 5040 | *name == 'N' && strcmp(name, "None") == 0) |
| 5041 | { |
| 5042 | if (symtable_warn(st, "argument named None")) |
| 5043 | return -1; |
| 5044 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5045 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5046 | name = buffer; |
| 5047 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5048 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5049 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5050 | Py_DECREF(s); |
| 5051 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | /* Must only be called with mangled names */ |
| 5055 | |
| 5056 | static int |
| 5057 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5058 | PyObject *name, int flag) |
| 5059 | { |
| 5060 | PyObject *o; |
| 5061 | int val; |
| 5062 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5063 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5064 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5065 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5066 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5067 | PyString_AsString(name)); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5068 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5069 | st->st_cur->ste_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5070 | return -1; |
| 5071 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5072 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5073 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5074 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5075 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5076 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5077 | Py_DECREF(o); |
| 5078 | return -1; |
| 5079 | } |
| 5080 | Py_DECREF(o); |
| 5081 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5082 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5083 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5084 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5085 | } else if (flag & DEF_GLOBAL) { |
| 5086 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5087 | perhaps only DEF_FREE_GLOBAL */ |
| 5088 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5089 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5090 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5091 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5092 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5093 | o = PyInt_FromLong(val); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5094 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5095 | Py_DECREF(o); |
| 5096 | return -1; |
| 5097 | } |
| 5098 | Py_DECREF(o); |
| 5099 | } |
| 5100 | return 0; |
| 5101 | } |
| 5102 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5103 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5104 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5105 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5106 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5107 | ignored) in case those are the only places a yield occurs (so that this |
| 5108 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5109 | static int |
| 5110 | look_for_yield(node *n) |
| 5111 | { |
| 5112 | int i; |
| 5113 | |
| 5114 | for (i = 0; i < NCH(n); ++i) { |
| 5115 | node *kid = CHILD(n, i); |
| 5116 | |
| 5117 | switch (TYPE(kid)) { |
| 5118 | |
| 5119 | case classdef: |
| 5120 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5121 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5122 | /* Stuff in nested functions and classes can't make |
| 5123 | the parent a generator. */ |
| 5124 | return 0; |
| 5125 | |
| 5126 | case yield_stmt: |
| 5127 | return 1; |
| 5128 | |
| 5129 | default: |
| 5130 | if (look_for_yield(kid)) |
| 5131 | return 1; |
| 5132 | } |
| 5133 | } |
| 5134 | return 0; |
| 5135 | } |
| 5136 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5137 | static void |
| 5138 | symtable_node(struct symtable *st, node *n) |
| 5139 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5140 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5141 | |
| 5142 | loop: |
| 5143 | switch (TYPE(n)) { |
| 5144 | case funcdef: { |
| 5145 | char *func_name = STR(CHILD(n, 1)); |
| 5146 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5147 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5148 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5149 | symtable_funcdef(st, n); |
| 5150 | symtable_exit_scope(st); |
| 5151 | break; |
| 5152 | } |
| 5153 | case lambdef: |
| 5154 | if (NCH(n) == 4) |
| 5155 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5156 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5157 | symtable_funcdef(st, n); |
| 5158 | symtable_exit_scope(st); |
| 5159 | break; |
| 5160 | case classdef: { |
| 5161 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5162 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5163 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5164 | node *bases = CHILD(n, 3); |
| 5165 | int i; |
| 5166 | for (i = 0; i < NCH(bases); i += 2) { |
| 5167 | symtable_node(st, CHILD(bases, i)); |
| 5168 | } |
| 5169 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5170 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5171 | tmp = st->st_private; |
| 5172 | st->st_private = class_name; |
| 5173 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5174 | st->st_private = tmp; |
| 5175 | symtable_exit_scope(st); |
| 5176 | break; |
| 5177 | } |
| 5178 | case if_stmt: |
| 5179 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5180 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5181 | if (st->st_cur->ste_generator == 0) |
| 5182 | st->st_cur->ste_generator = |
| 5183 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5184 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5185 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5186 | symtable_node(st, CHILD(n, i + 1)); |
| 5187 | symtable_node(st, CHILD(n, i + 3)); |
| 5188 | } |
| 5189 | if (i + 2 < NCH(n)) |
| 5190 | symtable_node(st, CHILD(n, i + 2)); |
| 5191 | break; |
| 5192 | case global_stmt: |
| 5193 | symtable_global(st, n); |
| 5194 | break; |
| 5195 | case import_stmt: |
| 5196 | symtable_import(st, n); |
| 5197 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5198 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5199 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5200 | symtable_node(st, CHILD(n, 1)); |
| 5201 | if (NCH(n) > 2) |
| 5202 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5203 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5204 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5205 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5206 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5207 | if (NCH(n) > 4) |
| 5208 | symtable_node(st, CHILD(n, 5)); |
| 5209 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5210 | |
| 5211 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5212 | case assert_stmt: |
| 5213 | if (Py_OptimizeFlag) |
| 5214 | return; |
| 5215 | if (NCH(n) == 2) { |
| 5216 | n = CHILD(n, 1); |
| 5217 | goto loop; |
| 5218 | } else { |
| 5219 | symtable_node(st, CHILD(n, 1)); |
| 5220 | n = CHILD(n, 3); |
| 5221 | goto loop; |
| 5222 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5223 | case except_clause: |
| 5224 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5225 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5226 | if (NCH(n) > 1) { |
| 5227 | n = CHILD(n, 1); |
| 5228 | goto loop; |
| 5229 | } |
| 5230 | break; |
| 5231 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5232 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5233 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5234 | case yield_stmt: |
| 5235 | st->st_cur->ste_generator = 1; |
| 5236 | n = CHILD(n, 1); |
| 5237 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5238 | case expr_stmt: |
| 5239 | if (NCH(n) == 1) |
| 5240 | n = CHILD(n, 0); |
| 5241 | else { |
| 5242 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5243 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5244 | symtable_node(st, CHILD(n, 2)); |
| 5245 | break; |
| 5246 | } else { |
| 5247 | int i; |
| 5248 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5249 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5250 | n = CHILD(n, NCH(n) - 1); |
| 5251 | } |
| 5252 | } |
| 5253 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5254 | case list_iter: |
| 5255 | n = CHILD(n, 0); |
| 5256 | if (TYPE(n) == list_for) { |
| 5257 | st->st_tmpname++; |
| 5258 | symtable_list_comprehension(st, n); |
| 5259 | st->st_tmpname--; |
| 5260 | } else { |
| 5261 | REQ(n, list_if); |
| 5262 | symtable_node(st, CHILD(n, 1)); |
| 5263 | if (NCH(n) == 3) { |
| 5264 | n = CHILD(n, 2); |
| 5265 | goto loop; |
| 5266 | } |
| 5267 | } |
| 5268 | break; |
| 5269 | case for_stmt: |
| 5270 | symtable_assign(st, CHILD(n, 1), 0); |
| 5271 | for (i = 3; i < NCH(n); ++i) |
| 5272 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5273 | symtable_node(st, CHILD(n, i)); |
| 5274 | break; |
| 5275 | /* The remaining cases fall through to default except in |
| 5276 | special circumstances. This requires the individual cases |
| 5277 | to be coded with great care, even though they look like |
| 5278 | rather innocuous. Each case must double-check TYPE(n). |
| 5279 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5280 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5281 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5282 | n = CHILD(n, 2); |
| 5283 | goto loop; |
| 5284 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5285 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5286 | case listmaker: |
| 5287 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5288 | st->st_tmpname++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5289 | symtable_list_comprehension(st, CHILD(n, 1)); |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5290 | symtable_node(st, CHILD(n, 0)); |
| 5291 | st->st_tmpname--; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5292 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5293 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5294 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5295 | case atom: |
| 5296 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5297 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5298 | break; |
| 5299 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5300 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5301 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5302 | /* Walk over every non-token child with a special case |
| 5303 | for one child. |
| 5304 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5305 | if (NCH(n) == 1) { |
| 5306 | n = CHILD(n, 0); |
| 5307 | goto loop; |
| 5308 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5309 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5310 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5311 | symtable_node(st, CHILD(n, i)); |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | static void |
| 5316 | symtable_funcdef(struct symtable *st, node *n) |
| 5317 | { |
| 5318 | node *body; |
| 5319 | |
| 5320 | if (TYPE(n) == lambdef) { |
| 5321 | if (NCH(n) == 4) |
| 5322 | symtable_params(st, CHILD(n, 1)); |
| 5323 | } else |
| 5324 | symtable_params(st, CHILD(n, 2)); |
| 5325 | body = CHILD(n, NCH(n) - 1); |
| 5326 | symtable_node(st, body); |
| 5327 | } |
| 5328 | |
| 5329 | /* The next two functions parse the argument tuple. |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5330 | symtable_default_args() checks for names in the default arguments, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5331 | which are references in the defining scope. symtable_params() |
| 5332 | parses the parameter names, which are defined in the function's |
| 5333 | body. |
| 5334 | |
| 5335 | varargslist: |
| 5336 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5337 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5338 | */ |
| 5339 | |
| 5340 | static void |
| 5341 | symtable_default_args(struct symtable *st, node *n) |
| 5342 | { |
| 5343 | node *c; |
| 5344 | int i; |
| 5345 | |
| 5346 | if (TYPE(n) == parameters) { |
| 5347 | n = CHILD(n, 1); |
| 5348 | if (TYPE(n) == RPAR) |
| 5349 | return; |
| 5350 | } |
| 5351 | REQ(n, varargslist); |
| 5352 | for (i = 0; i < NCH(n); i += 2) { |
| 5353 | c = CHILD(n, i); |
| 5354 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5355 | break; |
| 5356 | } |
| 5357 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5358 | symtable_node(st, CHILD(n, i)); |
| 5359 | } |
| 5360 | } |
| 5361 | |
| 5362 | static void |
| 5363 | symtable_params(struct symtable *st, node *n) |
| 5364 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5365 | int i, complex = -1, ext = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5366 | node *c = NULL; |
| 5367 | |
| 5368 | if (TYPE(n) == parameters) { |
| 5369 | n = CHILD(n, 1); |
| 5370 | if (TYPE(n) == RPAR) |
| 5371 | return; |
| 5372 | } |
| 5373 | REQ(n, varargslist); |
| 5374 | for (i = 0; i < NCH(n); i += 2) { |
| 5375 | c = CHILD(n, i); |
| 5376 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5377 | ext = 1; |
| 5378 | break; |
| 5379 | } |
| 5380 | if (TYPE(c) == test) { |
| 5381 | continue; |
| 5382 | } |
| 5383 | if (TYPE(CHILD(c, 0)) == NAME) |
| 5384 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
| 5385 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5386 | char nbuf[30]; |
| 5387 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5388 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5389 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5390 | } |
| 5391 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5392 | if (ext) { |
| 5393 | c = CHILD(n, i); |
| 5394 | if (TYPE(c) == STAR) { |
| 5395 | i++; |
| 5396 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5397 | DEF_PARAM | DEF_STAR); |
| 5398 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5399 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5400 | c = NULL; |
| 5401 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5402 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5403 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5404 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5405 | i++; |
| 5406 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5407 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5408 | } |
| 5409 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5410 | if (complex >= 0) { |
| 5411 | int j; |
| 5412 | for (j = 0; j <= complex; j++) { |
| 5413 | c = CHILD(n, j); |
| 5414 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5415 | c = CHILD(n, ++j); |
| 5416 | else if (TYPE(c) == EQUAL) |
| 5417 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5418 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5419 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5420 | } |
| 5421 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | static void |
| 5425 | symtable_params_fplist(struct symtable *st, node *n) |
| 5426 | { |
| 5427 | int i; |
| 5428 | node *c; |
| 5429 | |
| 5430 | REQ(n, fplist); |
| 5431 | for (i = 0; i < NCH(n); i += 2) { |
| 5432 | c = CHILD(n, i); |
| 5433 | REQ(c, fpdef); |
| 5434 | if (NCH(c) == 1) |
| 5435 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5436 | DEF_PARAM | DEF_INTUPLE); |
| 5437 | else |
| 5438 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5439 | } |
| 5440 | |
| 5441 | } |
| 5442 | |
| 5443 | static void |
| 5444 | symtable_global(struct symtable *st, node *n) |
| 5445 | { |
| 5446 | int i; |
| 5447 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5448 | /* XXX It might be helpful to warn about module-level global |
| 5449 | statements, but it's hard to tell the difference between |
| 5450 | module-level and a string passed to exec. |
| 5451 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5452 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5453 | for (i = 1; i < NCH(n); i += 2) { |
| 5454 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5455 | int flags; |
| 5456 | |
| 5457 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5458 | if (flags < 0) |
| 5459 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5460 | if (flags && flags != DEF_GLOBAL) { |
| 5461 | char buf[500]; |
| 5462 | if (flags & DEF_PARAM) { |
| 5463 | PyErr_Format(PyExc_SyntaxError, |
| 5464 | "name '%.400s' is local and global", |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5465 | name); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5466 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5467 | st->st_cur->ste_lineno); |
| 5468 | st->st_errors++; |
| 5469 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5470 | } |
| 5471 | else { |
| 5472 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5473 | PyOS_snprintf(buf, sizeof(buf), |
| 5474 | GLOBAL_AFTER_ASSIGN, |
| 5475 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5476 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5477 | PyOS_snprintf(buf, sizeof(buf), |
| 5478 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5479 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5480 | } |
| 5481 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5482 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5483 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | static void |
| 5487 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5488 | { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5489 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5490 | |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5491 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", st->st_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5492 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5493 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5494 | symtable_node(st, CHILD(n, 3)); |
| 5495 | if (NCH(n) == 5) |
| 5496 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
| 5499 | static void |
| 5500 | symtable_import(struct symtable *st, node *n) |
| 5501 | { |
| 5502 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5503 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5504 | | 'from' dotted_name 'import' |
| 5505 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5506 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5507 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5508 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5509 | node *dotname = CHILD(n, 1); |
| 5510 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5511 | /* check for bogus imports */ |
| 5512 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 5513 | PyErr_SetString(PyExc_SyntaxError, |
| 5514 | LATE_FUTURE); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 5515 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5516 | n->n_lineno); |
| 5517 | st->st_errors++; |
| 5518 | return; |
| 5519 | } |
| 5520 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5521 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5522 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 5523 | if (symtable_warn(st, |
| 5524 | "import * only allowed at module level") < 0) |
| 5525 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 5526 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5527 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5528 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5529 | } else { |
| 5530 | for (i = 3; i < NCH(n); i += 2) { |
| 5531 | node *c = CHILD(n, i); |
| 5532 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5533 | symtable_assign(st, CHILD(c, 2), |
| 5534 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5535 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5536 | symtable_assign(st, CHILD(c, 0), |
| 5537 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5538 | } |
| 5539 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5540 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5541 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5542 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5547 | /* The third argument to symatble_assign() is a flag to be passed to |
| 5548 | symtable_add_def() if it is eventually called. The flag is useful |
| 5549 | to specify the particular type of assignment that should be |
| 5550 | recorded, e.g. an assignment caused by import. |
| 5551 | */ |
| 5552 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5553 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5554 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5555 | { |
| 5556 | node *tmp; |
| 5557 | int i; |
| 5558 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5559 | loop: |
| 5560 | switch (TYPE(n)) { |
| 5561 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5562 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 5563 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5564 | return; |
| 5565 | case power: |
| 5566 | if (NCH(n) > 2) { |
| 5567 | for (i = 2; i < NCH(n); ++i) |
| 5568 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 5569 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5570 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5571 | if (NCH(n) > 1) { |
| 5572 | symtable_node(st, CHILD(n, 0)); |
| 5573 | symtable_node(st, CHILD(n, 1)); |
| 5574 | } else { |
| 5575 | n = CHILD(n, 0); |
| 5576 | goto loop; |
| 5577 | } |
| 5578 | return; |
| 5579 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 5580 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 5581 | /* XXX This is an error, but the next pass |
| 5582 | will catch it. */ |
| 5583 | return; |
| 5584 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5585 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5586 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5587 | } |
| 5588 | return; |
| 5589 | case exprlist: |
| 5590 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 5591 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5592 | if (NCH(n) == 1) { |
| 5593 | n = CHILD(n, 0); |
| 5594 | goto loop; |
| 5595 | } |
| 5596 | else { |
| 5597 | int i; |
| 5598 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5599 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5600 | return; |
| 5601 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5602 | case atom: |
| 5603 | tmp = CHILD(n, 0); |
| 5604 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 5605 | n = CHILD(n, 1); |
| 5606 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5607 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5608 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 5609 | PyErr_SetString(PyExc_SyntaxError, |
| 5610 | ASSIGN_DEBUG); |
| 5611 | PyErr_SyntaxLocation(st->st_filename, |
Jeremy Hylton | 86424e3 | 2001-12-04 02:41:46 +0000 | [diff] [blame] | 5612 | n->n_lineno); |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 5613 | st->st_errors++; |
| 5614 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5615 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 5616 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5617 | return; |
| 5618 | case dotted_as_name: |
| 5619 | if (NCH(n) == 3) |
| 5620 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5621 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5622 | else |
| 5623 | symtable_add_def(st, |
| 5624 | STR(CHILD(CHILD(n, |
| 5625 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5626 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5627 | return; |
| 5628 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5629 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5630 | return; |
| 5631 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5632 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5633 | return; |
| 5634 | default: |
| 5635 | if (NCH(n) == 0) |
| 5636 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5637 | if (NCH(n) == 1) { |
| 5638 | n = CHILD(n, 0); |
| 5639 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5640 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 5641 | /* Should only occur for errors like x + 1 = 1, |
| 5642 | which will be caught in the next pass. */ |
| 5643 | for (i = 0; i < NCH(n); ++i) |
| 5644 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5645 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5646 | } |
| 5647 | } |