Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1 | /* Compile an expression node to intermediate code */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | /* XXX TO DO: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4 | XXX add __doc__ attribute == co_doc to code object attributes? |
| 5 | XXX (it's currently the first item of the co_const tuple) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 6 | XXX Generate simple jump for break/return outside 'try...finally' |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 7 | XXX Allow 'continue' inside finally clause of try-finally |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 8 | XXX New opcode for loading the initial index for a for loop |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 9 | XXX other JAR tricks? |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 14 | #include "node.h" |
| 15 | #include "token.h" |
| 16 | #include "graminit.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 17 | #include "compile.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 18 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 19 | #include "opcode.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | #include "structmember.h" |
| 21 | |
| 22 | #include <ctype.h> |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 24 | /* Three symbols from graminit.h are also defined in Python.h, with |
| 25 | Py_ prefixes to their names. Python.h can't include graminit.h |
| 26 | (which defines too many confusing symbols), but we can check here |
| 27 | that they haven't changed (which is very unlikely, but possible). */ |
| 28 | #if Py_single_input != single_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 29 | #error "single_input has changed -- update Py_single_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 30 | #endif |
| 31 | #if Py_file_input != file_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 32 | #error "file_input has changed -- update Py_file_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | #endif |
| 34 | #if Py_eval_input != eval_input |
Guido van Rossum | 2f75b29 | 1997-05-20 22:18:48 +0000 | [diff] [blame] | 35 | #error "eval_input has changed -- update Py_eval_input in Python.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 38 | int Py_OptimizeFlag = 0; |
| 39 | |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 40 | #define OP_DELETE 0 |
| 41 | #define OP_ASSIGN 1 |
| 42 | #define OP_APPLY 2 |
| 43 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 44 | #define VAR_LOAD 0 |
| 45 | #define VAR_STORE 1 |
| 46 | #define VAR_DELETE 2 |
| 47 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 48 | #define DEL_CLOSURE_ERROR \ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 49 | "can not delete variable '%.400s' referenced in nested scope" |
| 50 | |
| 51 | #define DUPLICATE_ARGUMENT \ |
| 52 | "duplicate argument '%s' in function definition" |
| 53 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 54 | #define ILLEGAL_DYNAMIC_SCOPE \ |
| 55 | "%.100s: exec or 'import *' makes names ambiguous in nested scope" |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 57 | #define GLOBAL_AFTER_ASSIGN \ |
| 58 | "name '%.400s' is assigned to before global declaration" |
| 59 | |
| 60 | #define GLOBAL_AFTER_USE \ |
| 61 | "name '%.400s' is used prior to global declaration" |
| 62 | |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 63 | #define PARAM_GLOBAL \ |
Neal Norwitz | 2a47c0f | 2002-01-29 00:53:41 +0000 | [diff] [blame] | 64 | "name '%.400s' is a function parameter and declared global" |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 65 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 66 | #define LATE_FUTURE \ |
| 67 | "from __future__ imports must occur at the beginning of the file" |
| 68 | |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 69 | #define ASSIGN_DEBUG \ |
| 70 | "can not assign to __debug__" |
| 71 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 72 | #define MANGLE_LEN 256 |
| 73 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | #define OFF(x) offsetof(PyCodeObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 76 | static PyMemberDef code_memberlist[] = { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 77 | {"co_argcount", T_INT, OFF(co_argcount), READONLY}, |
| 78 | {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 79 | {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 80 | {"co_flags", T_INT, OFF(co_flags), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 81 | {"co_code", T_OBJECT, OFF(co_code), READONLY}, |
| 82 | {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, |
| 83 | {"co_names", T_OBJECT, OFF(co_names), READONLY}, |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 84 | {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 85 | {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, |
| 86 | {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, |
Guido van Rossum | 39d942d | 1992-01-12 02:30:05 +0000 | [diff] [blame] | 87 | {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 88 | {"co_name", T_OBJECT, OFF(co_name), READONLY}, |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 89 | {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, |
| 90 | {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | {NULL} /* Sentinel */ |
| 92 | }; |
| 93 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(code_doc, |
| 95 | "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\ |
| 96 | varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\ |
| 97 | \n\ |
| 98 | Create a code object. Not for the faint of heart."); |
| 99 | |
| 100 | static PyObject * |
| 101 | code_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 102 | { |
| 103 | int argcount; |
| 104 | int nlocals; |
| 105 | int stacksize; |
| 106 | int flags; |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 107 | PyObject *co; |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 108 | PyObject *empty = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 109 | PyObject *code; |
| 110 | PyObject *consts; |
| 111 | PyObject *names; |
| 112 | PyObject *varnames; |
| 113 | PyObject *freevars = NULL; |
| 114 | PyObject *cellvars = NULL; |
| 115 | PyObject *filename; |
| 116 | PyObject *name; |
| 117 | int firstlineno; |
| 118 | PyObject *lnotab; |
| 119 | |
| 120 | if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code", |
| 121 | &argcount, &nlocals, &stacksize, &flags, |
| 122 | &code, |
| 123 | &PyTuple_Type, &consts, |
| 124 | &PyTuple_Type, &names, |
| 125 | &PyTuple_Type, &varnames, |
| 126 | &filename, &name, |
| 127 | &firstlineno, &lnotab, |
| 128 | &PyTuple_Type, &freevars, |
| 129 | &PyTuple_Type, &cellvars)) |
| 130 | return NULL; |
| 131 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 132 | if (!PyObject_CheckReadBuffer(code)) { |
| 133 | PyErr_SetString(PyExc_TypeError, |
| 134 | "bytecode object must be a single-segment read-only buffer"); |
| 135 | return NULL; |
| 136 | } |
| 137 | |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 138 | if (freevars == NULL || cellvars == NULL) { |
| 139 | empty = PyTuple_New(0); |
| 140 | if (empty == NULL) |
| 141 | return NULL; |
| 142 | if (freevars == NULL) |
| 143 | freevars = empty; |
| 144 | if (cellvars == NULL) |
| 145 | cellvars = empty; |
| 146 | } |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 147 | |
| 148 | co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 149 | code, consts, names, varnames, |
| 150 | freevars, cellvars, filename, name, |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 151 | firstlineno, lnotab); |
Raymond Hettinger | cc1798e | 2003-09-16 04:27:52 +0000 | [diff] [blame] | 152 | Py_XDECREF(empty); |
Raymond Hettinger | 37a724d | 2003-09-15 21:43:16 +0000 | [diff] [blame] | 153 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 156 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 157 | code_dealloc(PyCodeObject *co) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 158 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 159 | Py_XDECREF(co->co_code); |
| 160 | Py_XDECREF(co->co_consts); |
| 161 | Py_XDECREF(co->co_names); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 162 | Py_XDECREF(co->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 163 | Py_XDECREF(co->co_freevars); |
| 164 | Py_XDECREF(co->co_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 165 | Py_XDECREF(co->co_filename); |
| 166 | Py_XDECREF(co->co_name); |
Guido van Rossum | 275558c | 1997-07-25 20:13:49 +0000 | [diff] [blame] | 167 | Py_XDECREF(co->co_lnotab); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 168 | PyObject_DEL(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 171 | static PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 172 | code_repr(PyCodeObject *co) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 173 | { |
| 174 | char buf[500]; |
| 175 | int lineno = -1; |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 176 | char *filename = "???"; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 177 | char *name = "???"; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | a396a88 | 2000-04-07 01:21:36 +0000 | [diff] [blame] | 179 | if (co->co_firstlineno != 0) |
| 180 | lineno = co->co_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 181 | if (co->co_filename && PyString_Check(co->co_filename)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 182 | filename = PyString_AS_STRING(co->co_filename); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 183 | if (co->co_name && PyString_Check(co->co_name)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 184 | name = PyString_AS_STRING(co->co_name); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 185 | PyOS_snprintf(buf, sizeof(buf), |
| 186 | "<code object %.100s at %p, file \"%.300s\", line %d>", |
| 187 | name, co, filename, lineno); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 188 | return PyString_FromString(buf); |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 191 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 192 | code_compare(PyCodeObject *co, PyCodeObject *cp) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 193 | { |
| 194 | int cmp; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 195 | cmp = PyObject_Compare(co->co_name, cp->co_name); |
| 196 | if (cmp) return cmp; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 197 | cmp = co->co_argcount - cp->co_argcount; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 198 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 199 | cmp = co->co_nlocals - cp->co_nlocals; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 200 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | beef8aa | 1997-08-29 17:12:43 +0000 | [diff] [blame] | 201 | cmp = co->co_flags - cp->co_flags; |
Michael W. Hudson | 3ae3315 | 2002-10-03 09:50:47 +0000 | [diff] [blame] | 202 | if (cmp) return (cmp<0)?-1:1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 203 | cmp = PyObject_Compare(co->co_code, cp->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 204 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 205 | cmp = PyObject_Compare(co->co_consts, cp->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 206 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 207 | cmp = PyObject_Compare(co->co_names, cp->co_names); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 208 | if (cmp) return cmp; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 209 | cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 210 | if (cmp) return cmp; |
| 211 | cmp = PyObject_Compare(co->co_freevars, cp->co_freevars); |
| 212 | if (cmp) return cmp; |
| 213 | cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 214 | return cmp; |
| 215 | } |
| 216 | |
| 217 | static long |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 218 | code_hash(PyCodeObject *co) |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 219 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 220 | long h, h0, h1, h2, h3, h4, h5, h6; |
Guido van Rossum | 4467959 | 2000-04-10 16:20:31 +0000 | [diff] [blame] | 221 | h0 = PyObject_Hash(co->co_name); |
| 222 | if (h0 == -1) return -1; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 223 | h1 = PyObject_Hash(co->co_code); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 224 | if (h1 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | h2 = PyObject_Hash(co->co_consts); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 226 | if (h2 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 227 | h3 = PyObject_Hash(co->co_names); |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 228 | if (h3 == -1) return -1; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 229 | h4 = PyObject_Hash(co->co_varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 230 | if (h4 == -1) return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 231 | h5 = PyObject_Hash(co->co_freevars); |
| 232 | if (h5 == -1) return -1; |
| 233 | h6 = PyObject_Hash(co->co_cellvars); |
| 234 | if (h6 == -1) return -1; |
| 235 | h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 236 | co->co_argcount ^ co->co_nlocals ^ co->co_flags; |
Guido van Rossum | 2e8f8a3 | 1993-11-05 10:20:10 +0000 | [diff] [blame] | 237 | if (h == -1) h = -2; |
| 238 | return h; |
| 239 | } |
| 240 | |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 241 | /* XXX code objects need to participate in GC? */ |
| 242 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 243 | PyTypeObject PyCode_Type = { |
| 244 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 245 | 0, |
| 246 | "code", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 247 | sizeof(PyCodeObject), |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 248 | 0, |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 249 | (destructor)code_dealloc, /* tp_dealloc */ |
| 250 | 0, /* tp_print */ |
| 251 | 0, /* tp_getattr */ |
| 252 | 0, /* tp_setattr */ |
| 253 | (cmpfunc)code_compare, /* tp_compare */ |
| 254 | (reprfunc)code_repr, /* tp_repr */ |
| 255 | 0, /* tp_as_number */ |
| 256 | 0, /* tp_as_sequence */ |
| 257 | 0, /* tp_as_mapping */ |
| 258 | (hashfunc)code_hash, /* tp_hash */ |
| 259 | 0, /* tp_call */ |
| 260 | 0, /* tp_str */ |
| 261 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 262 | 0, /* tp_setattro */ |
| 263 | 0, /* tp_as_buffer */ |
| 264 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 265 | code_doc, /* tp_doc */ |
Jeremy Hylton | c785f48 | 2001-09-14 20:08:07 +0000 | [diff] [blame] | 266 | 0, /* tp_traverse */ |
| 267 | 0, /* tp_clear */ |
| 268 | 0, /* tp_richcompare */ |
| 269 | 0, /* tp_weaklistoffset */ |
| 270 | 0, /* tp_iter */ |
| 271 | 0, /* tp_iternext */ |
| 272 | 0, /* tp_methods */ |
| 273 | code_memberlist, /* tp_members */ |
| 274 | 0, /* tp_getset */ |
| 275 | 0, /* tp_base */ |
| 276 | 0, /* tp_dict */ |
| 277 | 0, /* tp_descr_get */ |
| 278 | 0, /* tp_descr_set */ |
| 279 | 0, /* tp_dictoffset */ |
| 280 | 0, /* tp_init */ |
| 281 | 0, /* tp_alloc */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 282 | code_new, /* tp_new */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
Guido van Rossum | 644a12b | 1997-04-09 19:24:53 +0000 | [diff] [blame] | 285 | #define NAME_CHARS \ |
| 286 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 287 | |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 288 | /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ |
| 289 | |
| 290 | static int |
| 291 | all_name_chars(unsigned char *s) |
| 292 | { |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 293 | static char ok_name_char[256]; |
| 294 | static unsigned char *name_chars = (unsigned char *)NAME_CHARS; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 295 | |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 296 | if (ok_name_char[*name_chars] == 0) { |
| 297 | unsigned char *p; |
| 298 | for (p = name_chars; *p; p++) |
| 299 | ok_name_char[*p] = 1; |
| 300 | } |
| 301 | while (*s) { |
| 302 | if (ok_name_char[*s++] == 0) |
| 303 | return 0; |
| 304 | } |
| 305 | return 1; |
Guido van Rossum | fc5ce61 | 2001-01-19 00:24:06 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 308 | static int |
| 309 | intern_strings(PyObject *tuple) |
| 310 | { |
| 311 | int i; |
| 312 | |
| 313 | for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { |
| 314 | PyObject *v = PyTuple_GET_ITEM(tuple, i); |
| 315 | if (v == NULL || !PyString_Check(v)) { |
| 316 | Py_FatalError("non-string found in code slot"); |
| 317 | PyErr_BadInternalCall(); |
| 318 | return -1; |
| 319 | } |
| 320 | PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 321 | } |
| 322 | return 0; |
| 323 | } |
| 324 | |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 325 | #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1])) |
| 326 | #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD) |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 327 | #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP) |
| 328 | #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3)) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 329 | #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255 |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 330 | #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1) |
| 331 | #define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1]) |
| 332 | |
| 333 | static unsigned int * |
| 334 | markblocks(unsigned char *code, int len) |
| 335 | { |
| 336 | unsigned int *blocks = PyMem_Malloc(len*sizeof(int)); |
| 337 | int i,j, opcode, oldblock, newblock, blockcnt = 0; |
| 338 | |
| 339 | if (blocks == NULL) |
| 340 | return NULL; |
| 341 | memset(blocks, 0, len*sizeof(int)); |
| 342 | for (i=0 ; i<len ; i+=CODESIZE(opcode)) { |
| 343 | opcode = code[i]; |
| 344 | switch (opcode) { |
| 345 | case FOR_ITER: |
| 346 | case JUMP_FORWARD: |
| 347 | case JUMP_IF_FALSE: |
| 348 | case JUMP_IF_TRUE: |
| 349 | case JUMP_ABSOLUTE: |
| 350 | case CONTINUE_LOOP: |
| 351 | case SETUP_LOOP: |
| 352 | case SETUP_EXCEPT: |
| 353 | case SETUP_FINALLY: |
| 354 | j = GETJUMPTGT(code, i); |
| 355 | oldblock = blocks[j]; |
| 356 | newblock = ++blockcnt; |
| 357 | for (; j<len ; j++) { |
| 358 | if (blocks[j] != (unsigned)oldblock) |
| 359 | break; |
| 360 | blocks[j] = newblock; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | return blocks; |
| 366 | } |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 367 | |
| 368 | static PyObject * |
| 369 | optimize_code(PyObject *code, PyObject* consts) |
| 370 | { |
| 371 | int i, j, codelen; |
| 372 | int tgt, tgttgt, opcode; |
| 373 | unsigned char *codestr; |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 374 | unsigned int *blocks; |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 375 | |
| 376 | /* Make a modifiable copy of the code string */ |
| 377 | if (!PyString_Check(code)) |
| 378 | goto exitUnchanged; |
| 379 | codelen = PyString_Size(code); |
| 380 | codestr = PyMem_Malloc(codelen); |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 381 | if (codestr == NULL) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 382 | goto exitUnchanged; |
| 383 | codestr = memcpy(codestr, PyString_AS_STRING(code), codelen); |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 384 | blocks = markblocks(codestr, codelen); |
| 385 | if (blocks == NULL) { |
| 386 | PyMem_Free(codestr); |
| 387 | goto exitUnchanged; |
| 388 | } |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 389 | assert(PyTuple_Check(consts)); |
| 390 | |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 391 | for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) { |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 392 | opcode = codestr[i]; |
| 393 | switch (opcode) { |
| 394 | |
| 395 | /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP. |
| 396 | Note, only the first opcode is changed, the others still |
| 397 | perform normally if they happen to be jump targets. */ |
| 398 | case LOAD_CONST: |
| 399 | j = GETARG(codestr, i); |
| 400 | if (codestr[i+3] != JUMP_IF_FALSE || |
| 401 | codestr[i+6] != POP_TOP || |
| 402 | !PyObject_IsTrue(PyTuple_GET_ITEM(consts, j))) |
| 403 | continue; |
| 404 | codestr[i] = JUMP_FORWARD; |
| 405 | SETARG(codestr, i, 4); |
| 406 | break; |
| 407 | |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 408 | /* Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2 JMP+2 NOP NOP. |
| 409 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2 JMP+1 NOP. */ |
| 410 | case BUILD_TUPLE: |
| 411 | case BUILD_LIST: |
| 412 | if (codestr[i+3] != UNPACK_SEQUENCE) |
| 413 | continue; |
| 414 | if (!ISBASICBLOCK(blocks,i,6)) |
| 415 | continue; |
| 416 | if (GETARG(codestr, i) == 2 && \ |
| 417 | GETARG(codestr, i+3) == 2) { |
| 418 | codestr[i] = ROT_TWO; |
| 419 | codestr[i+1] = JUMP_FORWARD; |
| 420 | SETARG(codestr, i+1, 2); |
| 421 | codestr[i+4] = DUP_TOP; /* Filler codes used as NOPs */ |
| 422 | codestr[i+5] = POP_TOP; |
| 423 | continue; |
| 424 | } |
| 425 | if (GETARG(codestr, i) == 3 && \ |
| 426 | GETARG(codestr, i+3) == 3) { |
| 427 | codestr[i] = ROT_THREE; |
| 428 | codestr[i+1] = ROT_TWO; |
| 429 | codestr[i+2] = JUMP_FORWARD; |
| 430 | SETARG(codestr, i+2, 1); |
| 431 | codestr[i+5] = DUP_TOP; |
| 432 | } |
| 433 | break; |
| 434 | |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 435 | /* Replace jumps to unconditional jumps */ |
Raymond Hettinger | 255a3d0 | 2003-04-15 10:35:07 +0000 | [diff] [blame] | 436 | case FOR_ITER: |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 437 | case JUMP_FORWARD: |
| 438 | case JUMP_IF_FALSE: |
| 439 | case JUMP_IF_TRUE: |
| 440 | case JUMP_ABSOLUTE: |
| 441 | case CONTINUE_LOOP: |
| 442 | case SETUP_LOOP: |
| 443 | case SETUP_EXCEPT: |
| 444 | case SETUP_FINALLY: |
| 445 | tgt = GETJUMPTGT(codestr, i); |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 446 | if (!UNCONDITIONAL_JUMP(codestr[tgt])) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 447 | continue; |
| 448 | tgttgt = GETJUMPTGT(codestr, tgt); |
| 449 | if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */ |
| 450 | opcode = JUMP_ABSOLUTE; |
Raymond Hettinger | 5b75c38 | 2003-03-28 12:05:00 +0000 | [diff] [blame] | 451 | if (!ABSOLUTE_JUMP(opcode)) |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 452 | tgttgt -= i + 3; /* Calc relative jump addr */ |
| 453 | if (tgttgt < 0) /* No backward relative jumps */ |
| 454 | continue; |
| 455 | codestr[i] = opcode; |
| 456 | SETARG(codestr, i, tgttgt); |
| 457 | break; |
| 458 | |
| 459 | case EXTENDED_ARG: |
| 460 | PyMem_Free(codestr); |
| 461 | goto exitUnchanged; |
| 462 | } |
| 463 | } |
Martin v. Löwis | a94568a | 2003-05-10 07:36:56 +0000 | [diff] [blame] | 464 | code = PyString_FromStringAndSize((char *)codestr, codelen); |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 465 | PyMem_Free(codestr); |
Raymond Hettinger | ff5bc50 | 2004-03-21 15:12:00 +0000 | [diff] [blame] | 466 | PyMem_Free(blocks); |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 467 | return code; |
| 468 | |
| 469 | exitUnchanged: |
| 470 | Py_INCREF(code); |
| 471 | return code; |
| 472 | } |
| 473 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 474 | PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 475 | PyCode_New(int argcount, int nlocals, int stacksize, int flags, |
| 476 | PyObject *code, PyObject *consts, PyObject *names, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 477 | PyObject *varnames, PyObject *freevars, PyObject *cellvars, |
| 478 | PyObject *filename, PyObject *name, int firstlineno, |
| 479 | PyObject *lnotab) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 480 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 481 | PyCodeObject *co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 482 | int i; |
| 483 | /* Check argument types */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 484 | if (argcount < 0 || nlocals < 0 || |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 485 | code == NULL || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 486 | consts == NULL || !PyTuple_Check(consts) || |
| 487 | names == NULL || !PyTuple_Check(names) || |
| 488 | varnames == NULL || !PyTuple_Check(varnames) || |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 489 | freevars == NULL || !PyTuple_Check(freevars) || |
| 490 | cellvars == NULL || !PyTuple_Check(cellvars) || |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 491 | name == NULL || !PyString_Check(name) || |
| 492 | filename == NULL || !PyString_Check(filename) || |
Jeremy Hylton | 9f64caa | 2001-11-09 22:02:48 +0000 | [diff] [blame] | 493 | lnotab == NULL || !PyString_Check(lnotab) || |
| 494 | !PyObject_CheckReadBuffer(code)) { |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 495 | PyErr_BadInternalCall(); |
| 496 | return NULL; |
| 497 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 498 | intern_strings(names); |
| 499 | intern_strings(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 500 | intern_strings(freevars); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 501 | intern_strings(cellvars); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 502 | /* Intern selected string constants */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 503 | for (i = PyTuple_Size(consts); --i >= 0; ) { |
| 504 | PyObject *v = PyTuple_GetItem(consts, i); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 505 | if (!PyString_Check(v)) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 506 | continue; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 507 | if (!all_name_chars((unsigned char *)PyString_AS_STRING(v))) |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 508 | continue; |
| 509 | PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 510 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 511 | co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 512 | if (co != NULL) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 513 | co->co_argcount = argcount; |
| 514 | co->co_nlocals = nlocals; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 515 | co->co_stacksize = stacksize; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 516 | co->co_flags = flags; |
Raymond Hettinger | f6f575a | 2003-03-26 01:07:54 +0000 | [diff] [blame] | 517 | co->co_code = optimize_code(code, consts); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 518 | Py_INCREF(consts); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 519 | co->co_consts = consts; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 520 | Py_INCREF(names); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 521 | co->co_names = names; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 522 | Py_INCREF(varnames); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 523 | co->co_varnames = varnames; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 524 | Py_INCREF(freevars); |
| 525 | co->co_freevars = freevars; |
| 526 | Py_INCREF(cellvars); |
| 527 | co->co_cellvars = cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 528 | Py_INCREF(filename); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 529 | co->co_filename = filename; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 530 | Py_INCREF(name); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 531 | co->co_name = name; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 532 | co->co_firstlineno = firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 533 | Py_INCREF(lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 534 | co->co_lnotab = lnotab; |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 535 | if (PyTuple_GET_SIZE(freevars) == 0 && |
| 536 | PyTuple_GET_SIZE(cellvars) == 0) |
| 537 | co->co_flags |= CO_NOFREE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 538 | } |
| 539 | return co; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /* Data structure used internally */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 544 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 545 | /* The compiler uses two passes to generate bytecodes. The first pass |
| 546 | builds the symbol table. The second pass generates the bytecode. |
| 547 | |
| 548 | The first pass uses a single symtable struct. The second pass uses |
| 549 | a compiling struct for each code block. The compiling structs |
| 550 | share a reference to the symtable. |
| 551 | |
| 552 | The two passes communicate via symtable_load_symbols() and via |
| 553 | is_local() and is_global(). The former initializes several slots |
| 554 | in the compiling struct: c_varnames, c_locals, c_nlocals, |
| 555 | c_argcount, c_globals, and c_flags. |
| 556 | */ |
| 557 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 558 | /* All about c_lnotab. |
| 559 | |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 560 | c_lnotab is an array of unsigned bytes disguised as a Python string. Since |
| 561 | version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are |
| 562 | mapped to source code line #s via c_lnotab instead. |
| 563 | |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 564 | The array is conceptually a list of |
| 565 | (bytecode offset increment, line number increment) |
| 566 | pairs. The details are important and delicate, best illustrated by example: |
| 567 | |
| 568 | byte code offset source code line number |
| 569 | 0 1 |
| 570 | 6 2 |
| 571 | 50 7 |
| 572 | 350 307 |
| 573 | 361 308 |
| 574 | |
| 575 | The first trick is that these numbers aren't stored, only the increments |
| 576 | from one row to the next (this doesn't really work, but it's a start): |
| 577 | |
| 578 | 0, 1, 6, 1, 44, 5, 300, 300, 11, 1 |
| 579 | |
| 580 | The second trick is that an unsigned byte can't hold negative values, or |
| 581 | values larger than 255, so (a) there's a deep assumption that byte code |
| 582 | offsets and their corresponding line #s both increase monotonically, and (b) |
| 583 | if at least one column jumps by more than 255 from one row to the next, more |
| 584 | than one pair is written to the table. In case #b, there's no way to know |
| 585 | from looking at the table later how many were written. That's the delicate |
| 586 | part. A user of c_lnotab desiring to find the source line number |
| 587 | corresponding to a bytecode address A should do something like this |
| 588 | |
| 589 | lineno = addr = 0 |
| 590 | for addr_incr, line_incr in c_lnotab: |
| 591 | addr += addr_incr |
| 592 | if addr > A: |
| 593 | return lineno |
| 594 | lineno += line_incr |
| 595 | |
| 596 | In order for this to work, when the addr field increments by more than 255, |
| 597 | the line # increment in each pair generated must be 0 until the remaining addr |
| 598 | increment is < 256. So, in the example above, com_set_lineno should not (as |
| 599 | was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to |
| 600 | 255, 0, 45, 255, 0, 45. |
| 601 | */ |
| 602 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 603 | struct compiling { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 604 | PyObject *c_code; /* string */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 605 | PyObject *c_consts; /* list of objects */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 606 | PyObject *c_const_dict; /* inverse of c_consts */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 607 | PyObject *c_names; /* list of strings (names) */ |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 608 | PyObject *c_name_dict; /* inverse of c_names */ |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 609 | PyObject *c_globals; /* dictionary (value=None or True) */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 610 | PyObject *c_locals; /* dictionary (value=localID) */ |
| 611 | PyObject *c_varnames; /* list (inverse of c_locals) */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 612 | PyObject *c_freevars; /* dictionary (value=None) */ |
| 613 | PyObject *c_cellvars; /* list */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 614 | int c_nlocals; /* index of next local */ |
| 615 | int c_argcount; /* number of top-level arguments */ |
| 616 | int c_flags; /* same as co_flags */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 617 | int c_nexti; /* index into c_code */ |
| 618 | int c_errors; /* counts errors occurred */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 619 | int c_infunction; /* set when compiling a function */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 620 | int c_interactive; /* generating code for interactive command */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 621 | int c_loops; /* counts nested loops */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 622 | int c_begin; /* begin of current loop, for 'continue' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 623 | int c_block[CO_MAXBLOCKS]; /* stack of block types */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 624 | int c_nblocks; /* current block stack level */ |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 625 | const char *c_filename; /* filename of current node */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 626 | char *c_name; /* name of object (e.g. function) */ |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 627 | int c_lineno; /* Current line number */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 628 | int c_stacklevel; /* Current stack level */ |
| 629 | int c_maxstacklevel; /* Maximum stack level */ |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 630 | int c_firstlineno; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 631 | PyObject *c_lnotab; /* Table mapping address to line number */ |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 632 | int c_last_addr; /* last op addr seen and recorded in lnotab */ |
| 633 | int c_last_line; /* last line seen and recorded in lnotab */ |
| 634 | int c_lnotab_next; /* current length of lnotab */ |
| 635 | int c_lnotab_last; /* start of last lnotab record added */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 636 | char *c_private; /* for private name mangling */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 637 | int c_tmpname; /* temporary local name counter */ |
Guido van Rossum | cd90c20 | 2001-02-09 15:06:42 +0000 | [diff] [blame] | 638 | int c_nested; /* Is block nested funcdef or lamdef? */ |
| 639 | int c_closure; /* Is nested w/freevars? */ |
| 640 | struct symtable *c_symtable; /* pointer to module symbol table */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 641 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 642 | char *c_encoding; /* source encoding (a borrowed reference) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 643 | }; |
| 644 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 645 | static int |
| 646 | is_free(int v) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 647 | { |
| 648 | if ((v & (USE | DEF_FREE)) |
| 649 | && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) |
| 650 | return 1; |
| 651 | if (v & DEF_FREE_CLASS) |
| 652 | return 1; |
| 653 | return 0; |
| 654 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 655 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 656 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 657 | com_error(struct compiling *c, PyObject *exc, char *msg) |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 658 | { |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 659 | PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL; |
| 660 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 661 | if (c == NULL) { |
| 662 | /* Error occurred via symtable call to |
| 663 | is_constant_false */ |
| 664 | PyErr_SetString(exc, msg); |
| 665 | return; |
| 666 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 667 | c->c_errors++; |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 668 | if (c->c_lineno < 1 || c->c_interactive) { |
| 669 | /* Unknown line number or interactive input */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 670 | PyErr_SetString(exc, msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 671 | return; |
| 672 | } |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 673 | v = PyString_FromString(msg); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 674 | if (v == NULL) |
| 675 | return; /* MemoryError, too bad */ |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 676 | |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 677 | line = PyErr_ProgramText(c->c_filename, c->c_lineno); |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 678 | if (line == NULL) { |
| 679 | Py_INCREF(Py_None); |
| 680 | line = Py_None; |
| 681 | } |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 682 | if (exc == PyExc_SyntaxError) { |
| 683 | t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno, |
| 684 | Py_None, line); |
| 685 | if (t == NULL) |
| 686 | goto exit; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 687 | w = PyTuple_Pack(2, v, t); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 688 | if (w == NULL) |
| 689 | goto exit; |
| 690 | PyErr_SetObject(exc, w); |
| 691 | } else { |
| 692 | /* Make sure additional exceptions are printed with |
| 693 | file and line, also. */ |
| 694 | PyErr_SetObject(exc, v); |
| 695 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
| 696 | } |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 697 | exit: |
| 698 | Py_XDECREF(t); |
| 699 | Py_XDECREF(v); |
| 700 | Py_XDECREF(w); |
| 701 | Py_XDECREF(line); |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 704 | /* Interface to the block stack */ |
| 705 | |
| 706 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 707 | block_push(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 708 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 709 | if (c->c_nblocks >= CO_MAXBLOCKS) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 710 | com_error(c, PyExc_SystemError, |
| 711 | "too many statically nested blocks"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 712 | } |
| 713 | else { |
| 714 | c->c_block[c->c_nblocks++] = type; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 719 | block_pop(struct compiling *c, int type) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 720 | { |
| 721 | if (c->c_nblocks > 0) |
| 722 | c->c_nblocks--; |
| 723 | 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] | 724 | com_error(c, PyExc_SystemError, "bad block pop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 728 | /* Prototype forward declarations */ |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 729 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 730 | static int issue_warning(const char *, const char *, int); |
| 731 | static int com_init(struct compiling *, const char *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 732 | static void com_free(struct compiling *); |
| 733 | static void com_push(struct compiling *, int); |
| 734 | static void com_pop(struct compiling *, int); |
| 735 | static void com_done(struct compiling *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 736 | static void com_node(struct compiling *, node *); |
| 737 | static void com_factor(struct compiling *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 738 | static void com_addbyte(struct compiling *, int); |
| 739 | static void com_addint(struct compiling *, int); |
| 740 | static void com_addoparg(struct compiling *, int, int); |
| 741 | static void com_addfwref(struct compiling *, int, int *); |
| 742 | static void com_backpatch(struct compiling *, int); |
| 743 | static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *); |
| 744 | static int com_addconst(struct compiling *, PyObject *); |
| 745 | static int com_addname(struct compiling *, PyObject *); |
| 746 | static void com_addopname(struct compiling *, int, node *); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 747 | static void com_test(struct compiling *c, node *n); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 748 | static void com_list(struct compiling *, node *, int); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 749 | static void com_list_iter(struct compiling *, node *, node *, char *); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 750 | static void com_gen_iter(struct compiling *, node *, node *); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 751 | static int com_argdefs(struct compiling *, node *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 752 | static void com_assign(struct compiling *, node *, int, node *); |
| 753 | static void com_assign_name(struct compiling *, node *, int); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 754 | static int com_make_closure(struct compiling *c, PyCodeObject *co); |
| 755 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 756 | static PyCodeObject *icompile(node *, struct compiling *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 757 | static PyCodeObject *jcompile(node *, const char *, struct compiling *, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 758 | PyCompilerFlags *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 759 | static PyObject *parsestrplus(struct compiling*, node *); |
| 760 | static PyObject *parsestr(struct compiling *, char *); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 761 | static node *get_rawdocstring(node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 762 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 763 | static int get_ref_type(struct compiling *, char *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 764 | |
| 765 | /* symtable operations */ |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 766 | static int symtable_lookup(struct symtable *st, char *name); |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 767 | static struct symtable *symtable_build(node *, PyFutureFeatures *, |
| 768 | const char *filename); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 769 | static int symtable_load_symbols(struct compiling *); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 770 | static struct symtable *symtable_init(void); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 771 | static void symtable_enter_scope(struct symtable *, char *, int, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 772 | static int symtable_exit_scope(struct symtable *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 773 | static int symtable_add_def(struct symtable *, char *, int); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 774 | static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 775 | |
| 776 | static void symtable_node(struct symtable *, node *); |
| 777 | static void symtable_funcdef(struct symtable *, node *); |
| 778 | static void symtable_default_args(struct symtable *, node *); |
| 779 | static void symtable_params(struct symtable *, node *); |
| 780 | static void symtable_params_fplist(struct symtable *, node *n); |
| 781 | static void symtable_global(struct symtable *, node *); |
| 782 | static void symtable_import(struct symtable *, node *); |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 783 | static void symtable_assign(struct symtable *, node *, int); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 784 | static void symtable_list_comprehension(struct symtable *, node *); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 785 | static void symtable_generator_expression(struct symtable *, node *); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 786 | static void symtable_list_for(struct symtable *, node *); |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 787 | static void symtable_gen_for(struct symtable *, node *, int); |
| 788 | static void symtable_gen_iter(struct symtable *, node *); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 789 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 790 | static int symtable_update_free_vars(struct symtable *); |
| 791 | static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); |
| 792 | static int symtable_check_global(struct symtable *, PyObject *, PyObject *); |
| 793 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 794 | /* helper */ |
| 795 | static void |
| 796 | do_pad(int pad) |
| 797 | { |
| 798 | int i; |
| 799 | for (i = 0; i < pad; ++i) |
| 800 | fprintf(stderr, " "); |
| 801 | } |
| 802 | |
| 803 | static void |
| 804 | dump(node *n, int pad, int depth) |
| 805 | { |
| 806 | int i; |
| 807 | if (depth == 0) |
| 808 | return; |
| 809 | do_pad(pad); |
| 810 | fprintf(stderr, "%d: %s\n", TYPE(n), STR(n)); |
| 811 | if (depth > 0) |
| 812 | depth--; |
| 813 | for (i = 0; i < NCH(n); ++i) |
| 814 | dump(CHILD(n, i), pad + 1, depth); |
| 815 | } |
| 816 | |
| 817 | #define DUMP(N) dump(N, 0, -1) |
| 818 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 819 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 820 | com_init(struct compiling *c, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 821 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 822 | memset((void *)c, '\0', sizeof(struct compiling)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 823 | if ((c->c_code = PyString_FromStringAndSize((char *)NULL, |
| 824 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 825 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 826 | if ((c->c_consts = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 827 | goto fail; |
| 828 | if ((c->c_const_dict = PyDict_New()) == NULL) |
| 829 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 830 | if ((c->c_names = PyList_New(0)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 831 | goto fail; |
| 832 | if ((c->c_name_dict = PyDict_New()) == NULL) |
| 833 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 834 | if ((c->c_locals = PyDict_New()) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 835 | goto fail; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 836 | if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, |
| 837 | 1000)) == NULL) |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 838 | goto fail; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 839 | c->c_globals = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 840 | c->c_varnames = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 841 | c->c_freevars = NULL; |
| 842 | c->c_cellvars = NULL; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 843 | c->c_nlocals = 0; |
| 844 | c->c_argcount = 0; |
| 845 | c->c_flags = 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 846 | c->c_nexti = 0; |
| 847 | c->c_errors = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 848 | c->c_infunction = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 849 | c->c_interactive = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 850 | c->c_loops = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 851 | c->c_begin = 0; |
| 852 | c->c_nblocks = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 853 | c->c_filename = filename; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 854 | c->c_name = "?"; |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 855 | c->c_lineno = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 856 | c->c_stacklevel = 0; |
| 857 | c->c_maxstacklevel = 0; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 858 | c->c_firstlineno = 0; |
| 859 | c->c_last_addr = 0; |
| 860 | c->c_last_line = 0; |
Barry Warsaw | 174e801 | 2001-01-22 04:35:57 +0000 | [diff] [blame] | 861 | c->c_lnotab_next = 0; |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 862 | c->c_lnotab_last = 0; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 863 | c->c_tmpname = 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 864 | c->c_nested = 0; |
| 865 | c->c_closure = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 866 | c->c_symtable = NULL; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 867 | return 1; |
| 868 | |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 869 | fail: |
| 870 | com_free(c); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 875 | com_free(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 876 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 877 | Py_XDECREF(c->c_code); |
| 878 | Py_XDECREF(c->c_consts); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 879 | Py_XDECREF(c->c_const_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 880 | Py_XDECREF(c->c_names); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 881 | Py_XDECREF(c->c_name_dict); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 882 | Py_XDECREF(c->c_globals); |
| 883 | Py_XDECREF(c->c_locals); |
| 884 | Py_XDECREF(c->c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 885 | Py_XDECREF(c->c_freevars); |
| 886 | Py_XDECREF(c->c_cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 887 | Py_XDECREF(c->c_lnotab); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 888 | if (c->c_future) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 889 | PyObject_FREE((void *)c->c_future); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 893 | com_push(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 894 | { |
| 895 | c->c_stacklevel += n; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 896 | if (c->c_stacklevel > c->c_maxstacklevel) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 897 | c->c_maxstacklevel = c->c_stacklevel; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 898 | /* |
| 899 | fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n", |
| 900 | c->c_filename, c->c_name, c->c_lineno, |
| 901 | c->c_nexti, c->c_stacklevel, n); |
| 902 | */ |
| 903 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 907 | com_pop(struct compiling *c, int n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 908 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 909 | if (c->c_stacklevel < n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 910 | c->c_stacklevel = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 911 | else |
| 912 | c->c_stacklevel -= n; |
| 913 | } |
| 914 | |
| 915 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 916 | com_done(struct compiling *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 917 | { |
| 918 | if (c->c_code != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 919 | _PyString_Resize(&c->c_code, c->c_nexti); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 920 | if (c->c_lnotab != NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 921 | _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 924 | static int |
| 925 | com_check_size(PyObject **s, int offset) |
| 926 | { |
| 927 | int len = PyString_GET_SIZE(*s); |
| 928 | if (offset >= len) |
| 929 | return _PyString_Resize(s, len * 2); |
| 930 | return 0; |
| 931 | } |
| 932 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 933 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 934 | com_addbyte(struct compiling *c, int byte) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 936 | /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 937 | assert(byte >= 0 && byte <= 255); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 938 | assert(c->c_code != 0); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 939 | if (com_check_size(&c->c_code, c->c_nexti)) { |
| 940 | c->c_errors++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 941 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 942 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 943 | PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 947 | com_addint(struct compiling *c, int x) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 948 | { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 949 | com_addbyte(c, x & 0xff); |
| 950 | com_addbyte(c, x >> 8); /* XXX x should be positive */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 954 | com_add_lnotab(struct compiling *c, int addr, int line) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 955 | { |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 956 | char *p; |
| 957 | if (c->c_lnotab == NULL) |
| 958 | return; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 959 | if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) { |
| 960 | c->c_errors++; |
| 961 | return; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 962 | } |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 963 | p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 964 | *p++ = addr; |
| 965 | *p++ = line; |
| 966 | c->c_lnotab_next += 2; |
| 967 | } |
| 968 | |
| 969 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 970 | com_set_lineno(struct compiling *c, int lineno) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 971 | { |
| 972 | c->c_lineno = lineno; |
| 973 | if (c->c_firstlineno == 0) { |
| 974 | c->c_firstlineno = c->c_last_line = lineno; |
| 975 | } |
| 976 | else { |
| 977 | int incr_addr = c->c_nexti - c->c_last_addr; |
| 978 | int incr_line = lineno - c->c_last_line; |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 979 | c->c_lnotab_last = c->c_lnotab_next; |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 980 | while (incr_addr > 255) { |
| 981 | com_add_lnotab(c, 255, 0); |
| 982 | incr_addr -= 255; |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 983 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 984 | while (incr_line > 255) { |
| 985 | com_add_lnotab(c, incr_addr, 255); |
| 986 | incr_line -=255; |
| 987 | incr_addr = 0; |
| 988 | } |
| 989 | if (incr_addr > 0 || incr_line > 0) |
| 990 | com_add_lnotab(c, incr_addr, incr_line); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 991 | c->c_last_addr = c->c_nexti; |
| 992 | c->c_last_line = lineno; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | static void |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 997 | com_strip_lnotab(struct compiling *c) |
| 998 | { |
| 999 | /* strip the last lnotab entry if no opcode were emitted. |
| 1000 | * This prevents a line number to be generated on a final |
| 1001 | * pass, like in the following example: |
| 1002 | * |
| 1003 | * if a: |
| 1004 | * print 5 |
| 1005 | * else: |
| 1006 | * pass |
| 1007 | * |
| 1008 | * Without the fix, a line trace event would be generated |
| 1009 | * on the pass even if a is true (because of the implicit |
| 1010 | * return). |
| 1011 | */ |
| 1012 | if (c->c_nexti == c->c_last_addr && c->c_lnotab_last > 0) { |
| 1013 | c->c_lnotab_next = c->c_lnotab_last; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1018 | com_addoparg(struct compiling *c, int op, int arg) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1019 | { |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1020 | int extended_arg = arg >> 16; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1021 | if (extended_arg){ |
| 1022 | com_addbyte(c, EXTENDED_ARG); |
| 1023 | com_addint(c, extended_arg); |
| 1024 | arg &= 0xffff; |
| 1025 | } |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 1026 | com_addbyte(c, op); |
| 1027 | com_addint(c, arg); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1031 | com_addfwref(struct compiling *c, int op, int *p_anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1032 | { |
| 1033 | /* Compile a forward reference for backpatching */ |
| 1034 | int here; |
| 1035 | int anchor; |
| 1036 | com_addbyte(c, op); |
| 1037 | here = c->c_nexti; |
| 1038 | anchor = *p_anchor; |
| 1039 | *p_anchor = here; |
| 1040 | com_addint(c, anchor == 0 ? 0 : here - anchor); |
| 1041 | } |
| 1042 | |
| 1043 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1044 | com_backpatch(struct compiling *c, int anchor) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1045 | { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 1046 | unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1047 | int target = c->c_nexti; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1048 | int dist; |
| 1049 | int prev; |
| 1050 | for (;;) { |
| 1051 | /* Make the JUMP instruction at anchor point to target */ |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1052 | prev = code[anchor] + (code[anchor+1] << 8); |
| 1053 | dist = target - (anchor+2); |
| 1054 | code[anchor] = dist & 0xff; |
Fred Drake | ef8ace3 | 2000-08-24 00:32:09 +0000 | [diff] [blame] | 1055 | dist >>= 8; |
| 1056 | code[anchor+1] = dist; |
| 1057 | dist >>= 8; |
| 1058 | if (dist) { |
| 1059 | com_error(c, PyExc_SystemError, |
| 1060 | "com_backpatch: offset too large"); |
| 1061 | break; |
| 1062 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1063 | if (!prev) |
| 1064 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1065 | anchor -= prev; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 1069 | /* Handle literals and names uniformly */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1070 | |
| 1071 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1072 | com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1073 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1074 | PyObject *w, *t, *np=NULL; |
| 1075 | long n; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1076 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1077 | t = PyTuple_Pack(2, v, v->ob_type); |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1078 | if (t == NULL) |
| 1079 | goto fail; |
| 1080 | w = PyDict_GetItem(dict, t); |
| 1081 | if (w != NULL) { |
| 1082 | n = PyInt_AsLong(w); |
| 1083 | } else { |
| 1084 | n = PyList_Size(list); |
| 1085 | np = PyInt_FromLong(n); |
| 1086 | if (np == NULL) |
| 1087 | goto fail; |
| 1088 | if (PyList_Append(list, v) != 0) |
| 1089 | goto fail; |
| 1090 | if (PyDict_SetItem(dict, t, np) != 0) |
| 1091 | goto fail; |
| 1092 | Py_DECREF(np); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1093 | } |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1094 | Py_DECREF(t); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 1095 | return n; |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1096 | fail: |
| 1097 | Py_XDECREF(np); |
| 1098 | Py_XDECREF(t); |
| 1099 | c->c_errors++; |
| 1100 | return 0; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1104 | com_addconst(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1105 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1106 | 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] | 1107 | } |
| 1108 | |
| 1109 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1110 | com_addname(struct compiling *c, PyObject *v) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1111 | { |
Guido van Rossum | a2ace6a | 2000-04-28 16:42:25 +0000 | [diff] [blame] | 1112 | 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] | 1113 | } |
| 1114 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1115 | int |
| 1116 | _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1117 | { |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1118 | /* Name mangling: __private becomes _classname__private. |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1119 | This is independent from how the name is used. */ |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1120 | size_t nlen, plen; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1121 | if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_') |
| 1122 | return 0; |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1123 | nlen = strlen(name); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1124 | if (nlen+2 >= maxlen) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1125 | return 0; /* Don't mangle __extremely_long_names */ |
| 1126 | if (name[nlen-1] == '_' && name[nlen-2] == '_') |
| 1127 | return 0; /* Don't mangle __whatever__ */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1128 | /* Strip leading underscores from class name */ |
| 1129 | while (*p == '_') |
| 1130 | p++; |
| 1131 | if (*p == '\0') |
| 1132 | return 0; /* Don't mangle if class is just underscores */ |
| 1133 | plen = strlen(p); |
| 1134 | if (plen + nlen >= maxlen) |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1135 | plen = maxlen-nlen-2; /* Truncate class name if too long */ |
| 1136 | /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1137 | buffer[0] = '_'; |
| 1138 | strncpy(buffer+1, p, plen); |
Guido van Rossum | d8a6d1c | 1996-08-24 07:54:27 +0000 | [diff] [blame] | 1139 | strcpy(buffer+1+plen, name); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1140 | return 1; |
| 1141 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1142 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1143 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1144 | com_addop_name(struct compiling *c, int op, char *name) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1145 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1146 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1147 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1148 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1149 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1150 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 1151 | name = buffer; |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 1152 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1153 | c->c_errors++; |
| 1154 | i = 255; |
| 1155 | } |
| 1156 | else { |
| 1157 | i = com_addname(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1158 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1159 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1160 | com_addoparg(c, op, i); |
| 1161 | } |
| 1162 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1163 | #define NAME_LOCAL 0 |
| 1164 | #define NAME_GLOBAL 1 |
| 1165 | #define NAME_DEFAULT 2 |
| 1166 | #define NAME_CLOSURE 3 |
| 1167 | |
| 1168 | static int |
| 1169 | com_lookup_arg(PyObject *dict, PyObject *name) |
| 1170 | { |
| 1171 | PyObject *v = PyDict_GetItem(dict, name); |
| 1172 | if (v == NULL) |
| 1173 | return -1; |
| 1174 | else |
| 1175 | return PyInt_AS_LONG(v); |
| 1176 | } |
| 1177 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1178 | static int |
| 1179 | none_assignment_check(struct compiling *c, char *name, int assigning) |
| 1180 | { |
| 1181 | if (name[0] == 'N' && strcmp(name, "None") == 0) { |
| 1182 | char *msg; |
| 1183 | if (assigning) |
Michael W. Hudson | 976249b | 2003-01-16 15:39:07 +0000 | [diff] [blame] | 1184 | msg = "assignment to None"; |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1185 | else |
| 1186 | msg = "deleting None"; |
| 1187 | if (issue_warning(msg, c->c_filename, c->c_lineno) < 0) { |
| 1188 | c->c_errors++; |
| 1189 | return -1; |
| 1190 | } |
| 1191 | } |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1195 | static void |
| 1196 | com_addop_varname(struct compiling *c, int kind, char *name) |
| 1197 | { |
| 1198 | PyObject *v; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1199 | int i, reftype; |
| 1200 | int scope = NAME_DEFAULT; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1201 | int op = STOP_CODE; |
| 1202 | char buffer[MANGLE_LEN]; |
| 1203 | |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 1204 | if (kind != VAR_LOAD && |
| 1205 | none_assignment_check(c, name, kind == VAR_STORE)) |
| 1206 | { |
| 1207 | c->c_errors++; |
| 1208 | i = 255; |
| 1209 | goto done; |
| 1210 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 1211 | if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1212 | name = buffer; |
| 1213 | if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { |
| 1214 | c->c_errors++; |
| 1215 | i = 255; |
| 1216 | goto done; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1217 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1218 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1219 | reftype = get_ref_type(c, name); |
| 1220 | switch (reftype) { |
| 1221 | case LOCAL: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1222 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1223 | scope = NAME_LOCAL; |
| 1224 | break; |
| 1225 | case GLOBAL_EXPLICIT: |
| 1226 | scope = NAME_GLOBAL; |
| 1227 | break; |
| 1228 | case GLOBAL_IMPLICIT: |
| 1229 | if (c->c_flags & CO_OPTIMIZED) |
| 1230 | scope = NAME_GLOBAL; |
| 1231 | break; |
| 1232 | case FREE: |
| 1233 | case CELL: |
| 1234 | scope = NAME_CLOSURE; |
| 1235 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | i = com_addname(c, v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1239 | if (scope == NAME_LOCAL) |
| 1240 | i = com_lookup_arg(c->c_locals, v); |
| 1241 | else if (reftype == FREE) |
| 1242 | i = com_lookup_arg(c->c_freevars, v); |
| 1243 | else if (reftype == CELL) |
| 1244 | i = com_lookup_arg(c->c_cellvars, v); |
| 1245 | if (i == -1) { |
| 1246 | c->c_errors++; /* XXX no exception set */ |
| 1247 | i = 255; |
| 1248 | goto done; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1249 | } |
| 1250 | Py_DECREF(v); |
| 1251 | |
| 1252 | switch (kind) { |
| 1253 | case VAR_LOAD: |
| 1254 | switch (scope) { |
| 1255 | case NAME_LOCAL: |
| 1256 | op = LOAD_FAST; |
| 1257 | break; |
| 1258 | case NAME_GLOBAL: |
| 1259 | op = LOAD_GLOBAL; |
| 1260 | break; |
| 1261 | case NAME_DEFAULT: |
| 1262 | op = LOAD_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | case NAME_CLOSURE: |
| 1265 | op = LOAD_DEREF; |
| 1266 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1267 | } |
| 1268 | break; |
| 1269 | case VAR_STORE: |
| 1270 | switch (scope) { |
| 1271 | case NAME_LOCAL: |
| 1272 | op = STORE_FAST; |
| 1273 | break; |
| 1274 | case NAME_GLOBAL: |
| 1275 | op = STORE_GLOBAL; |
| 1276 | break; |
| 1277 | case NAME_DEFAULT: |
| 1278 | op = STORE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | case NAME_CLOSURE: |
| 1281 | op = STORE_DEREF; |
| 1282 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1283 | } |
| 1284 | break; |
| 1285 | case VAR_DELETE: |
| 1286 | switch (scope) { |
| 1287 | case NAME_LOCAL: |
| 1288 | op = DELETE_FAST; |
| 1289 | break; |
| 1290 | case NAME_GLOBAL: |
| 1291 | op = DELETE_GLOBAL; |
| 1292 | break; |
| 1293 | case NAME_DEFAULT: |
| 1294 | op = DELETE_NAME; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1295 | break; |
| 1296 | case NAME_CLOSURE: { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 1297 | char buf[500]; |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1298 | PyOS_snprintf(buf, sizeof(buf), |
| 1299 | DEL_CLOSURE_ERROR, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 1300 | com_error(c, PyExc_SyntaxError, buf); |
| 1301 | i = 255; |
| 1302 | break; |
| 1303 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1304 | } |
| 1305 | break; |
| 1306 | } |
| 1307 | done: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1308 | com_addoparg(c, op, i); |
| 1309 | } |
| 1310 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1311 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1312 | com_addopname(struct compiling *c, int op, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1313 | { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1314 | char *name; |
| 1315 | char buffer[1000]; |
| 1316 | /* XXX it is possible to write this code without the 1000 |
| 1317 | chars on the total length of dotted names, I just can't be |
| 1318 | bothered right now */ |
| 1319 | if (TYPE(n) == STAR) |
| 1320 | name = "*"; |
| 1321 | else if (TYPE(n) == dotted_name) { |
| 1322 | char *p = buffer; |
| 1323 | int i; |
| 1324 | name = buffer; |
| 1325 | for (i = 0; i < NCH(n); i += 2) { |
| 1326 | char *s = STR(CHILD(n, i)); |
| 1327 | if (p + strlen(s) > buffer + (sizeof buffer) - 2) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1328 | com_error(c, PyExc_MemoryError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1329 | "dotted_name too long"); |
Guido van Rossum | d9dfaf5 | 1995-02-17 15:04:57 +0000 | [diff] [blame] | 1330 | name = NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | } |
| 1333 | if (p != buffer) |
| 1334 | *p++ = '.'; |
| 1335 | strcpy(p, s); |
| 1336 | p = strchr(p, '\0'); |
| 1337 | } |
| 1338 | } |
| 1339 | else { |
| 1340 | REQ(n, NAME); |
| 1341 | name = STR(n); |
| 1342 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1343 | com_addop_name(c, op, name); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1346 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1347 | parsenumber(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1348 | { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1349 | char *end; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1350 | long x; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1351 | double dx; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1352 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1353 | int imflag; |
| 1354 | #endif |
| 1355 | |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1356 | errno = 0; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 1357 | end = s + strlen(s) - 1; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1358 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 15ad9a6 | 1996-01-26 20:53:56 +0000 | [diff] [blame] | 1359 | imflag = *end == 'j' || *end == 'J'; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1360 | #endif |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 1361 | if (*end == 'l' || *end == 'L') |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1362 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1363 | if (s[0] == '0') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1364 | x = (long) PyOS_strtoul(s, &end, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1365 | if (x < 0 && errno == 0) { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 1366 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 1369 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1370 | x = PyOS_strtol(s, &end, 0); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1371 | if (*end == '\0') { |
Jeremy Hylton | 71b6af9 | 2001-08-27 19:45:25 +0000 | [diff] [blame] | 1372 | if (errno != 0) |
| 1373 | return PyLong_FromString(s, (char **)0, 0); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1374 | return PyInt_FromLong(x); |
Guido van Rossum | 282914b | 1991-04-04 10:42:56 +0000 | [diff] [blame] | 1375 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1376 | /* XXX Huge floats may silently fail */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1377 | #ifndef WITHOUT_COMPLEX |
| 1378 | if (imflag) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1379 | Py_complex z; |
| 1380 | z.real = 0.; |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1381 | PyFPE_START_PROTECT("atof", return 0) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1382 | z.imag = atof(s); |
| 1383 | PyFPE_END_PROTECT(z) |
| 1384 | return PyComplex_FromCComplex(z); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1385 | } |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1386 | else |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 1387 | #endif |
Guido van Rossum | ac1fc95 | 1997-10-08 15:23:55 +0000 | [diff] [blame] | 1388 | { |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1389 | PyFPE_START_PROTECT("atof", return 0) |
| 1390 | dx = atof(s); |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 1391 | PyFPE_END_PROTECT(dx) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1392 | return PyFloat_FromDouble(dx); |
Guido van Rossum | 0ae748d | 1997-02-14 22:58:07 +0000 | [diff] [blame] | 1393 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1396 | static PyObject * |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1397 | decode_utf8(char **sPtr, char *end, char* encoding) |
| 1398 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1399 | #ifndef Py_USING_UNICODE |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1400 | Py_FatalError("decode_utf8 should not be called in this build."); |
| 1401 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1402 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1403 | PyObject *u, *v; |
| 1404 | char *s, *t; |
| 1405 | t = s = *sPtr; |
| 1406 | /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
| 1407 | while (s < end && (*s & 0x80)) s++; |
| 1408 | *sPtr = s; |
| 1409 | u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
| 1410 | if (u == NULL) |
| 1411 | return NULL; |
| 1412 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1413 | Py_DECREF(u); |
| 1414 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1415 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 1418 | /* compiler.transformer.Transformer.decode_literal depends on what |
| 1419 | might seem like minor details of this function -- changes here |
| 1420 | must be reflected there. */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1421 | static PyObject * |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1422 | parsestr(struct compiling *c, char *s) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1423 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1424 | PyObject *v; |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1425 | size_t len; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1426 | int quote = *s; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1427 | int rawmode = 0; |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1428 | char* encoding = ((c == NULL) ? NULL : c->c_encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1429 | int need_encoding; |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1430 | int unicode = 0; |
Guido van Rossum | 05459c5 | 2002-05-28 18:47:29 +0000 | [diff] [blame] | 1431 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1432 | if (isalpha(quote) || quote == '_') { |
| 1433 | if (quote == 'u' || quote == 'U') { |
| 1434 | quote = *++s; |
| 1435 | unicode = 1; |
| 1436 | } |
| 1437 | if (quote == 'r' || quote == 'R') { |
| 1438 | quote = *++s; |
| 1439 | rawmode = 1; |
| 1440 | } |
| 1441 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1442 | if (quote != '\'' && quote != '\"') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1443 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1444 | return NULL; |
| 1445 | } |
| 1446 | s++; |
| 1447 | len = strlen(s); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1448 | if (len > INT_MAX) { |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1449 | com_error(c, PyExc_OverflowError, |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1450 | "string to parse is too long"); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 1451 | return NULL; |
| 1452 | } |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1453 | if (s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1454 | PyErr_BadInternalCall(); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1455 | return NULL; |
| 1456 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1457 | if (len >= 4 && s[0] == quote && s[1] == quote) { |
| 1458 | s += 2; |
| 1459 | len -= 2; |
| 1460 | if (s[--len] != quote || s[--len] != quote) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1461 | PyErr_BadInternalCall(); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | } |
| 1464 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1465 | #ifdef Py_USING_UNICODE |
Guido van Rossum | fdc8bdb | 2000-05-01 17:54:56 +0000 | [diff] [blame] | 1466 | if (unicode || Py_UnicodeFlag) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1467 | PyObject *u, *w; |
Walter Dörwald | 4c6c765 | 2002-11-21 20:13:40 +0000 | [diff] [blame] | 1468 | char *buf; |
| 1469 | char *p; |
| 1470 | char *end; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1471 | if (encoding == NULL) { |
| 1472 | buf = s; |
| 1473 | u = NULL; |
| 1474 | } else if (strcmp(encoding, "iso-8859-1") == 0) { |
| 1475 | buf = s; |
| 1476 | u = NULL; |
| 1477 | } else { |
| 1478 | /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
| 1479 | u = PyString_FromStringAndSize((char *)NULL, len * 4); |
| 1480 | if (u == NULL) |
| 1481 | return NULL; |
| 1482 | p = buf = PyString_AsString(u); |
| 1483 | end = s + len; |
| 1484 | while (s < end) { |
| 1485 | if (*s == '\\') { |
| 1486 | *p++ = *s++; |
| 1487 | if (*s & 0x80) { |
| 1488 | strcpy(p, "u005c"); |
| 1489 | p += 5; |
| 1490 | } |
| 1491 | } |
| 1492 | if (*s & 0x80) { /* XXX inefficient */ |
| 1493 | char *r; |
| 1494 | int rn, i; |
| 1495 | w = decode_utf8(&s, end, "utf-16-be"); |
| 1496 | if (w == NULL) { |
| 1497 | Py_DECREF(u); |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | r = PyString_AsString(w); |
| 1501 | rn = PyString_Size(w); |
| 1502 | assert(rn % 2 == 0); |
| 1503 | for (i = 0; i < rn; i += 2) { |
| 1504 | sprintf(p, "\\u%02x%02x", |
| 1505 | r[i + 0] & 0xFF, |
| 1506 | r[i + 1] & 0xFF); |
| 1507 | p += 6; |
| 1508 | } |
| 1509 | Py_DECREF(w); |
| 1510 | } else { |
| 1511 | *p++ = *s++; |
| 1512 | } |
| 1513 | } |
| 1514 | len = p - buf; |
| 1515 | } |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1516 | if (rawmode) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1517 | v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL); |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1518 | else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1519 | v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL); |
| 1520 | Py_XDECREF(u); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1521 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1522 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 1523 | return v; |
| 1524 | |
Guido van Rossum | 5aa88f0 | 2000-03-10 23:01:36 +0000 | [diff] [blame] | 1525 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1526 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1527 | need_encoding = (encoding != NULL && |
| 1528 | strcmp(encoding, "utf-8") != 0 && |
| 1529 | strcmp(encoding, "iso-8859-1") != 0); |
| 1530 | if (rawmode || strchr(s, '\\') == NULL) { |
| 1531 | if (need_encoding) { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1532 | #ifndef Py_USING_UNICODE |
| 1533 | /* This should not happen - we never see any other |
| 1534 | encoding. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 1535 | Py_FatalError("cannot deal with encodings in this build."); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1536 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1537 | PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL); |
| 1538 | if (u == NULL) |
| 1539 | return NULL; |
| 1540 | v = PyUnicode_AsEncodedString(u, encoding, NULL); |
| 1541 | Py_DECREF(u); |
| 1542 | return v; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 1543 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1544 | } else { |
| 1545 | return PyString_FromStringAndSize(s, len); |
| 1546 | } |
| 1547 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 1548 | |
| 1549 | v = PyString_DecodeEscape(s, len, NULL, unicode, |
| 1550 | need_encoding ? encoding : NULL); |
Fredrik Lundh | 1fa0b89 | 2000-09-02 20:11:27 +0000 | [diff] [blame] | 1551 | if (v == NULL) |
Guido van Rossum | b081e0c | 2002-08-16 01:57:32 +0000 | [diff] [blame] | 1552 | PyErr_SyntaxLocation(c->c_filename, c->c_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1553 | return v; |
| 1554 | } |
| 1555 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1556 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1557 | parsestrplus(struct compiling* c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1558 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1559 | PyObject *v; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1560 | int i; |
| 1561 | REQ(CHILD(n, 0), STRING); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1562 | if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) { |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1563 | /* String literal concatenation */ |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1564 | for (i = 1; i < NCH(n); i++) { |
| 1565 | PyObject *s; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1566 | s = parsestr(c, STR(CHILD(n, i))); |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1567 | if (s == NULL) |
| 1568 | goto onError; |
| 1569 | if (PyString_Check(v) && PyString_Check(s)) { |
| 1570 | PyString_ConcatAndDel(&v, s); |
| 1571 | if (v == NULL) |
| 1572 | goto onError; |
| 1573 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1574 | #ifdef Py_USING_UNICODE |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1575 | else { |
| 1576 | PyObject *temp; |
| 1577 | temp = PyUnicode_Concat(v, s); |
| 1578 | Py_DECREF(s); |
| 1579 | if (temp == NULL) |
| 1580 | goto onError; |
| 1581 | Py_DECREF(v); |
| 1582 | v = temp; |
| 1583 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1584 | #endif |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } |
| 1587 | return v; |
Fred Drake | 4e998bc | 2000-04-13 14:10:44 +0000 | [diff] [blame] | 1588 | |
| 1589 | onError: |
| 1590 | Py_XDECREF(v); |
| 1591 | return NULL; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1594 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1595 | 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] | 1596 | { |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1597 | int anchor = 0; |
| 1598 | int save_begin = c->c_begin; |
| 1599 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1600 | /* list_for: for v in expr [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1601 | com_node(c, CHILD(n, 3)); /* expr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1602 | com_addbyte(c, GET_ITER); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1603 | c->c_begin = c->c_nexti; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1604 | com_addfwref(c, FOR_ITER, &anchor); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1605 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1606 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1607 | c->c_loops++; |
| 1608 | com_list_iter(c, n, e, t); |
| 1609 | c->c_loops--; |
| 1610 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1611 | c->c_begin = save_begin; |
| 1612 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1613 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1617 | com_gen_for(struct compiling *c, node *n, node *t, int is_outmost) |
| 1618 | { |
| 1619 | int break_anchor = 0; |
| 1620 | int anchor = 0; |
| 1621 | int save_begin = c->c_begin; |
| 1622 | |
| 1623 | REQ(n, gen_for); |
| 1624 | /* gen_for: for v in test [gen_iter] */ |
| 1625 | |
| 1626 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
| 1627 | block_push(c, SETUP_LOOP); |
| 1628 | |
| 1629 | if (is_outmost) { |
| 1630 | com_addop_varname(c, VAR_LOAD, "[outmost-iterable]"); |
| 1631 | com_push(c, 1); |
| 1632 | } |
| 1633 | else { |
| 1634 | com_node(c, CHILD(n, 3)); |
| 1635 | com_addbyte(c, GET_ITER); |
| 1636 | } |
| 1637 | |
| 1638 | c->c_begin = c->c_nexti; |
| 1639 | com_set_lineno(c, c->c_last_line); |
| 1640 | com_addfwref(c, FOR_ITER, &anchor); |
| 1641 | com_push(c, 1); |
| 1642 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
| 1643 | |
| 1644 | if (NCH(n) == 5) |
| 1645 | com_gen_iter(c, CHILD(n, 4), t); |
| 1646 | else { |
| 1647 | com_test(c, t); |
| 1648 | com_addbyte(c, YIELD_VALUE); |
| 1649 | com_pop(c, 1); |
| 1650 | } |
| 1651 | |
| 1652 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 1653 | c->c_begin = save_begin; |
| 1654 | |
| 1655 | com_backpatch(c, anchor); |
| 1656 | com_pop(c, 1); /* FOR_ITER has popped this */ |
| 1657 | com_addbyte(c, POP_BLOCK); |
| 1658 | block_pop(c, SETUP_LOOP); |
| 1659 | com_backpatch(c, break_anchor); |
| 1660 | } |
| 1661 | |
| 1662 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1663 | com_list_if(struct compiling *c, node *n, node *e, char *t) |
| 1664 | { |
| 1665 | int anchor = 0; |
| 1666 | int a = 0; |
| 1667 | /* list_iter: 'if' test [list_iter] */ |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1668 | com_node(c, CHILD(n, 1)); |
| 1669 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1670 | com_addbyte(c, POP_TOP); |
| 1671 | com_pop(c, 1); |
| 1672 | com_list_iter(c, n, e, t); |
| 1673 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1674 | com_backpatch(c, a); |
| 1675 | /* We jump here with an extra entry which we now pop */ |
| 1676 | com_addbyte(c, POP_TOP); |
| 1677 | com_backpatch(c, anchor); |
| 1678 | } |
| 1679 | |
| 1680 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1681 | com_gen_if(struct compiling *c, node *n, node *t) |
| 1682 | { |
| 1683 | /* gen_if: 'if' test [gen_iter] */ |
| 1684 | int anchor = 0; |
| 1685 | int a=0; |
| 1686 | |
| 1687 | com_node(c, CHILD(n, 1)); |
| 1688 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 1689 | com_addbyte(c, POP_TOP); |
| 1690 | com_pop(c, 1); |
| 1691 | |
| 1692 | if (NCH(n) == 3) |
| 1693 | com_gen_iter(c, CHILD(n, 2), t); |
| 1694 | else { |
| 1695 | com_test(c, t); |
| 1696 | com_addbyte(c, YIELD_VALUE); |
| 1697 | com_pop(c, 1); |
| 1698 | } |
| 1699 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 1700 | com_backpatch(c, a); |
| 1701 | /* We jump here with an extra entry which we now pop */ |
| 1702 | com_addbyte(c, POP_TOP); |
| 1703 | com_backpatch(c, anchor); |
| 1704 | } |
| 1705 | |
| 1706 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1707 | com_list_iter(struct compiling *c, |
| 1708 | node *p, /* parent of list_iter node */ |
| 1709 | node *e, /* element expression node */ |
| 1710 | char *t /* name of result list temp local */) |
| 1711 | { |
| 1712 | /* list_iter is the last child in a listmaker, list_for, or list_if */ |
| 1713 | node *n = CHILD(p, NCH(p)-1); |
| 1714 | if (TYPE(n) == list_iter) { |
| 1715 | n = CHILD(n, 0); |
| 1716 | switch (TYPE(n)) { |
| 1717 | case list_for: |
| 1718 | com_list_for(c, n, e, t); |
| 1719 | break; |
| 1720 | case list_if: |
| 1721 | com_list_if(c, n, e, t); |
| 1722 | break; |
| 1723 | default: |
| 1724 | com_error(c, PyExc_SystemError, |
| 1725 | "invalid list_iter node type"); |
| 1726 | } |
| 1727 | } |
| 1728 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1729 | com_addop_varname(c, VAR_LOAD, t); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1730 | com_push(c, 1); |
| 1731 | com_node(c, e); |
Raymond Hettinger | dd80f76 | 2004-03-07 07:31:06 +0000 | [diff] [blame] | 1732 | com_addbyte(c, LIST_APPEND); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1733 | com_pop(c, 2); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1738 | com_gen_iter(struct compiling *c, node *n, node *t) |
| 1739 | { |
| 1740 | /* gen_iter: gen_for | gen_if */ |
| 1741 | node *ch; |
| 1742 | REQ(n, gen_iter); |
| 1743 | |
| 1744 | ch = CHILD(n, 0); |
| 1745 | |
| 1746 | switch (TYPE(ch)) { |
| 1747 | case gen_for: |
| 1748 | com_gen_for(c, ch, t, 0); |
| 1749 | break; |
| 1750 | case gen_if: |
| 1751 | com_gen_if(c, ch, t); |
| 1752 | break; |
| 1753 | default: |
| 1754 | com_error(c, PyExc_SystemError, |
| 1755 | "invalid gen_iter node type"); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | static void |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1760 | com_list_comprehension(struct compiling *c, node *n) |
| 1761 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1762 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1763 | char tmpname[30]; |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 1764 | |
| 1765 | REQ(n, listmaker); |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 1766 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1767 | com_addoparg(c, BUILD_LIST, 0); |
| 1768 | com_addbyte(c, DUP_TOP); /* leave the result on the stack */ |
| 1769 | com_push(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1770 | com_addop_varname(c, VAR_STORE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1771 | com_pop(c, 1); |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1772 | com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1773 | com_addop_varname(c, VAR_DELETE, tmpname); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1774 | --c->c_tmpname; |
| 1775 | } |
| 1776 | |
| 1777 | static void |
| 1778 | com_listmaker(struct compiling *c, node *n) |
| 1779 | { |
Skip Montanaro | 46dfa5f | 2000-08-22 02:43:07 +0000 | [diff] [blame] | 1780 | /* listmaker: test ( list_for | (',' test)* [','] ) */ |
| 1781 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1782 | com_list_comprehension(c, n); |
| 1783 | else { |
| 1784 | int len = 0; |
| 1785 | int i; |
| 1786 | for (i = 0; i < NCH(n); i += 2, len++) |
| 1787 | com_node(c, CHILD(n, i)); |
| 1788 | com_addoparg(c, BUILD_LIST, len); |
| 1789 | com_pop(c, len-1); |
| 1790 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1794 | com_generator_expression(struct compiling *c, node *n) |
| 1795 | { |
| 1796 | /* testlist_gexp: test gen_for */ |
| 1797 | /* argument: test gen_for */ |
| 1798 | PyCodeObject *co; |
| 1799 | |
| 1800 | REQ(CHILD(n, 0), test); |
| 1801 | REQ(CHILD(n, 1), gen_for); |
| 1802 | |
| 1803 | symtable_enter_scope(c->c_symtable, "<genexpr>", TYPE(n), |
| 1804 | n->n_lineno); |
| 1805 | co = icompile(n, c); |
| 1806 | symtable_exit_scope(c->c_symtable); |
| 1807 | |
| 1808 | if (co == NULL) |
| 1809 | c->c_errors++; |
| 1810 | else { |
| 1811 | int closure = com_make_closure(c, co); |
| 1812 | int i = com_addconst(c, (PyObject *)co); |
| 1813 | |
| 1814 | com_addoparg(c, LOAD_CONST, i); |
| 1815 | com_push(c, 1); |
| 1816 | if (closure) |
| 1817 | com_addoparg(c, MAKE_CLOSURE, 0); |
| 1818 | else |
| 1819 | com_addoparg(c, MAKE_FUNCTION, 0); |
| 1820 | |
| 1821 | com_test(c, CHILD(CHILD(n, 1), 3)); |
| 1822 | com_addbyte(c, GET_ITER); |
| 1823 | com_addoparg(c, CALL_FUNCTION, 1); |
| 1824 | com_pop(c, 1); |
| 1825 | |
| 1826 | Py_DECREF(co); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | static void |
| 1831 | com_testlist_gexp(struct compiling *c, node *n) |
| 1832 | { |
| 1833 | /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */ |
| 1834 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) |
| 1835 | com_generator_expression(c, n); |
| 1836 | else com_list(c, n, 0); |
| 1837 | } |
| 1838 | |
| 1839 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1840 | com_dictmaker(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1841 | { |
| 1842 | int i; |
| 1843 | /* dictmaker: test ':' test (',' test ':' value)* [','] */ |
| 1844 | for (i = 0; i+2 < NCH(n); i += 4) { |
| 1845 | /* We must arrange things just right for STORE_SUBSCR. |
| 1846 | It wants the stack to look like (value) (dict) (key) */ |
| 1847 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1848 | com_push(c, 1); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1849 | com_node(c, CHILD(n, i)); /* key */ |
Gustavo Niemeyer | 78429a6 | 2002-12-16 13:54:02 +0000 | [diff] [blame] | 1850 | com_node(c, CHILD(n, i+2)); /* value */ |
| 1851 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1852 | com_addbyte(c, STORE_SUBSCR); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1853 | com_pop(c, 3); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1858 | com_atom(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1859 | { |
| 1860 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1861 | PyObject *v; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1862 | int i; |
| 1863 | REQ(n, atom); |
| 1864 | ch = CHILD(n, 0); |
| 1865 | switch (TYPE(ch)) { |
| 1866 | case LPAR: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1867 | if (TYPE(CHILD(n, 1)) == RPAR) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1868 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1869 | com_push(c, 1); |
| 1870 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1871 | else |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1872 | com_testlist_gexp(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1873 | break; |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1874 | case LSQB: /* '[' [listmaker] ']' */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1875 | if (TYPE(CHILD(n, 1)) == RSQB) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1876 | com_addoparg(c, BUILD_LIST, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1877 | com_push(c, 1); |
| 1878 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1879 | else |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1880 | com_listmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1881 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1882 | case LBRACE: /* '{' [dictmaker] '}' */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1883 | com_addoparg(c, BUILD_MAP, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1884 | com_push(c, 1); |
Skip Montanaro | 803d6e5 | 2000-08-12 18:09:51 +0000 | [diff] [blame] | 1885 | if (TYPE(CHILD(n, 1)) == dictmaker) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 1886 | com_dictmaker(c, CHILD(n, 1)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1887 | break; |
| 1888 | case BACKQUOTE: |
| 1889 | com_node(c, CHILD(n, 1)); |
| 1890 | com_addbyte(c, UNARY_CONVERT); |
| 1891 | break; |
| 1892 | case NUMBER: |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 1893 | if ((v = parsenumber(c, STR(ch))) == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1894 | i = 255; |
| 1895 | } |
| 1896 | else { |
| 1897 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1898 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1899 | } |
| 1900 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1901 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1902 | break; |
| 1903 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1904 | v = parsestrplus(c, n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 1905 | if (v == NULL) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1906 | c->c_errors++; |
| 1907 | i = 255; |
| 1908 | } |
| 1909 | else { |
| 1910 | i = com_addconst(c, v); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1911 | Py_DECREF(v); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1912 | } |
| 1913 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1914 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1915 | break; |
| 1916 | case NAME: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1917 | com_addop_varname(c, VAR_LOAD, STR(ch)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1918 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1919 | break; |
| 1920 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1921 | com_error(c, PyExc_SystemError, |
| 1922 | "com_atom: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1927 | com_slice(struct compiling *c, node *n, int op) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1928 | { |
| 1929 | if (NCH(n) == 1) { |
| 1930 | com_addbyte(c, op); |
| 1931 | } |
| 1932 | else if (NCH(n) == 2) { |
| 1933 | if (TYPE(CHILD(n, 0)) != COLON) { |
| 1934 | com_node(c, CHILD(n, 0)); |
| 1935 | com_addbyte(c, op+1); |
| 1936 | } |
| 1937 | else { |
| 1938 | com_node(c, CHILD(n, 1)); |
| 1939 | com_addbyte(c, op+2); |
| 1940 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1941 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1942 | } |
| 1943 | else { |
| 1944 | com_node(c, CHILD(n, 0)); |
| 1945 | com_node(c, CHILD(n, 2)); |
| 1946 | com_addbyte(c, op+3); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 1947 | com_pop(c, 2); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 1951 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1952 | com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn) |
| 1953 | { |
| 1954 | if (NCH(n) == 1) { |
| 1955 | com_addbyte(c, DUP_TOP); |
| 1956 | com_push(c, 1); |
| 1957 | com_addbyte(c, SLICE); |
| 1958 | com_node(c, augn); |
| 1959 | com_addbyte(c, opcode); |
| 1960 | com_pop(c, 1); |
| 1961 | com_addbyte(c, ROT_TWO); |
| 1962 | com_addbyte(c, STORE_SLICE); |
| 1963 | com_pop(c, 2); |
| 1964 | } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) { |
| 1965 | com_node(c, CHILD(n, 0)); |
| 1966 | com_addoparg(c, DUP_TOPX, 2); |
| 1967 | com_push(c, 2); |
| 1968 | com_addbyte(c, SLICE+1); |
| 1969 | com_pop(c, 1); |
| 1970 | com_node(c, augn); |
| 1971 | com_addbyte(c, opcode); |
| 1972 | com_pop(c, 1); |
| 1973 | com_addbyte(c, ROT_THREE); |
| 1974 | com_addbyte(c, STORE_SLICE+1); |
| 1975 | com_pop(c, 3); |
| 1976 | } else if (NCH(n) == 2) { |
| 1977 | com_node(c, CHILD(n, 1)); |
| 1978 | com_addoparg(c, DUP_TOPX, 2); |
| 1979 | com_push(c, 2); |
| 1980 | com_addbyte(c, SLICE+2); |
| 1981 | com_pop(c, 1); |
| 1982 | com_node(c, augn); |
| 1983 | com_addbyte(c, opcode); |
| 1984 | com_pop(c, 1); |
| 1985 | com_addbyte(c, ROT_THREE); |
| 1986 | com_addbyte(c, STORE_SLICE+2); |
| 1987 | com_pop(c, 3); |
| 1988 | } else { |
| 1989 | com_node(c, CHILD(n, 0)); |
| 1990 | com_node(c, CHILD(n, 2)); |
| 1991 | com_addoparg(c, DUP_TOPX, 3); |
| 1992 | com_push(c, 3); |
| 1993 | com_addbyte(c, SLICE+3); |
| 1994 | com_pop(c, 2); |
| 1995 | com_node(c, augn); |
| 1996 | com_addbyte(c, opcode); |
| 1997 | com_pop(c, 1); |
| 1998 | com_addbyte(c, ROT_FOUR); |
| 1999 | com_addbyte(c, STORE_SLICE+3); |
| 2000 | com_pop(c, 4); |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2005 | com_argument(struct compiling *c, node *n, PyObject **pkeywords) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2006 | { |
| 2007 | node *m; |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2008 | REQ(n, argument); /* [test '='] test [gen_for]; really [keyword '='] test */ |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2009 | if (NCH(n) == 1) { |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2010 | if (*pkeywords != NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2011 | com_error(c, PyExc_SyntaxError, |
Fred Drake | dcf08e0 | 2000-08-15 15:49:44 +0000 | [diff] [blame] | 2012 | "non-keyword arg after keyword arg"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2013 | } |
| 2014 | else { |
| 2015 | com_node(c, CHILD(n, 0)); |
| 2016 | } |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2017 | return; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2018 | } |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2019 | if (NCH(n) == 2) { |
| 2020 | com_generator_expression(c, n); |
| 2021 | return; |
| 2022 | } |
| 2023 | |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2024 | m = n; |
| 2025 | do { |
| 2026 | m = CHILD(m, 0); |
| 2027 | } while (NCH(m) == 1); |
| 2028 | if (TYPE(m) != NAME) { |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 2029 | /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 2030 | * LHS test = lambda x: x[0], and RHS test = 3. |
| 2031 | * SF bug 132313 points out that complaining about a keyword |
| 2032 | * then is very confusing. |
| 2033 | */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2034 | com_error(c, PyExc_SyntaxError, |
Tim Peters | 4e30378 | 2001-02-18 04:45:10 +0000 | [diff] [blame] | 2035 | TYPE(m) == lambdef ? |
| 2036 | "lambda cannot contain assignment" : |
| 2037 | "keyword can't be an expression"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2038 | } |
| 2039 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2040 | PyObject *v = PyString_InternFromString(STR(m)); |
Guido van Rossum | 63dd79a | 2002-08-16 02:24:56 +0000 | [diff] [blame] | 2041 | (void) none_assignment_check(c, STR(m), 1); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2042 | if (v != NULL && *pkeywords == NULL) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2043 | *pkeywords = PyDict_New(); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 2044 | if (v == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2045 | c->c_errors++; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 2046 | else if (*pkeywords == NULL) { |
| 2047 | c->c_errors++; |
| 2048 | Py_DECREF(v); |
| 2049 | } else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2050 | if (PyDict_GetItem(*pkeywords, v) != NULL) |
| 2051 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2052 | "duplicate keyword argument"); |
| 2053 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2054 | if (PyDict_SetItem(*pkeywords, v, v) != 0) |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2055 | c->c_errors++; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2056 | com_addoparg(c, LOAD_CONST, com_addconst(c, v)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2057 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2058 | Py_DECREF(v); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2059 | } |
| 2060 | } |
| 2061 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2064 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2065 | com_call_function(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2066 | { |
| 2067 | if (TYPE(n) == RPAR) { |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2068 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2069 | } |
| 2070 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2071 | PyObject *keywords = NULL; |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2072 | int i, na, nk; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 2073 | int lineno = n->n_lineno; |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2074 | int star_flag = 0; |
| 2075 | int starstar_flag = 0; |
| 2076 | int opcode; |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2077 | REQ(n, arglist); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2078 | na = 0; |
| 2079 | nk = 0; |
| 2080 | for (i = 0; i < NCH(n); i += 2) { |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 2081 | node *ch = CHILD(n, i); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2082 | if (TYPE(ch) == STAR || |
| 2083 | TYPE(ch) == DOUBLESTAR) |
| 2084 | break; |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 2085 | if (ch->n_lineno != lineno) { |
| 2086 | lineno = ch->n_lineno; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 2087 | com_set_lineno(c, lineno); |
Guido van Rossum | ca90605 | 1998-12-10 16:56:22 +0000 | [diff] [blame] | 2088 | } |
| 2089 | com_argument(c, ch, &keywords); |
Guido van Rossum | 635abd2 | 1997-01-06 22:56:52 +0000 | [diff] [blame] | 2090 | if (keywords == NULL) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2091 | na++; |
| 2092 | else |
| 2093 | nk++; |
| 2094 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2095 | Py_XDECREF(keywords); |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2096 | while (i < NCH(n)) { |
| 2097 | node *tok = CHILD(n, i); |
| 2098 | node *ch = CHILD(n, i+1); |
| 2099 | i += 3; |
| 2100 | switch (TYPE(tok)) { |
| 2101 | case STAR: star_flag = 1; break; |
| 2102 | case DOUBLESTAR: starstar_flag = 1; break; |
| 2103 | } |
| 2104 | com_node(c, ch); |
| 2105 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2106 | if (na > 255 || nk > 255) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2107 | com_error(c, PyExc_SyntaxError, |
| 2108 | "more than 255 arguments"); |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 2109 | } |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2110 | if (star_flag || starstar_flag) |
Jeremy Hylton | e4fb958 | 2000-03-29 00:10:44 +0000 | [diff] [blame] | 2111 | opcode = CALL_FUNCTION_VAR - 1 + |
Jeremy Hylton | 7690151 | 2000-03-28 23:49:17 +0000 | [diff] [blame] | 2112 | star_flag + (starstar_flag << 1); |
| 2113 | else |
| 2114 | opcode = CALL_FUNCTION; |
| 2115 | com_addoparg(c, opcode, na | (nk << 8)); |
| 2116 | com_pop(c, na + 2*nk + star_flag + starstar_flag); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2121 | com_select_member(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2122 | { |
| 2123 | com_addopname(c, LOAD_ATTR, n); |
| 2124 | } |
| 2125 | |
| 2126 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2127 | com_sliceobj(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2128 | { |
| 2129 | int i=0; |
| 2130 | int ns=2; /* number of slice arguments */ |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2131 | node *ch; |
| 2132 | |
| 2133 | /* first argument */ |
| 2134 | if (TYPE(CHILD(n,i)) == COLON) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2135 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2136 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2137 | i++; |
| 2138 | } |
| 2139 | else { |
| 2140 | com_node(c, CHILD(n,i)); |
| 2141 | i++; |
| 2142 | REQ(CHILD(n,i),COLON); |
| 2143 | i++; |
| 2144 | } |
| 2145 | /* second argument */ |
| 2146 | if (i < NCH(n) && TYPE(CHILD(n,i)) == test) { |
| 2147 | com_node(c, CHILD(n,i)); |
| 2148 | i++; |
| 2149 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2150 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2151 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2152 | com_push(c, 1); |
| 2153 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2154 | /* remaining arguments */ |
| 2155 | for (; i < NCH(n); i++) { |
| 2156 | ns++; |
| 2157 | ch=CHILD(n,i); |
| 2158 | REQ(ch, sliceop); |
| 2159 | if (NCH(ch) == 1) { |
| 2160 | /* right argument of ':' missing */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2161 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2162 | com_push(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2163 | } |
| 2164 | else |
| 2165 | com_node(c, CHILD(ch,1)); |
| 2166 | } |
| 2167 | com_addoparg(c, BUILD_SLICE, ns); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2168 | com_pop(c, 1 + (ns == 3)); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2172 | com_subscript(struct compiling *c, node *n) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2173 | { |
| 2174 | node *ch; |
| 2175 | REQ(n, subscript); |
| 2176 | ch = CHILD(n,0); |
| 2177 | /* check for rubber index */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2178 | if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) { |
Guido van Rossum | e449af7 | 1996-10-11 16:25:41 +0000 | [diff] [blame] | 2179 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2180 | com_push(c, 1); |
| 2181 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2182 | else { |
| 2183 | /* check for slice */ |
| 2184 | if ((TYPE(ch) == COLON || NCH(n) > 1)) |
| 2185 | com_sliceobj(c, n); |
| 2186 | else { |
| 2187 | REQ(ch, test); |
| 2188 | com_node(c, ch); |
| 2189 | } |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2194 | com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2195 | { |
| 2196 | int i, op; |
| 2197 | REQ(n, subscriptlist); |
| 2198 | /* Check to make backward compatible slice behavior for '[i:j]' */ |
| 2199 | if (NCH(n) == 1) { |
| 2200 | node *sub = CHILD(n, 0); /* subscript */ |
Thomas Wouters | e8643c4 | 2000-08-05 21:37:50 +0000 | [diff] [blame] | 2201 | /* 'Basic' slice, should have exactly one colon. */ |
| 2202 | if ((TYPE(CHILD(sub, 0)) == COLON |
| 2203 | || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON)) |
| 2204 | && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop)) |
| 2205 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2206 | switch (assigning) { |
| 2207 | case OP_DELETE: |
| 2208 | op = DELETE_SLICE; |
| 2209 | break; |
| 2210 | case OP_ASSIGN: |
| 2211 | op = STORE_SLICE; |
| 2212 | break; |
| 2213 | case OP_APPLY: |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2214 | op = SLICE; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2215 | break; |
| 2216 | default: |
| 2217 | com_augassign_slice(c, sub, assigning, augn); |
| 2218 | return; |
| 2219 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2220 | com_slice(c, sub, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2221 | if (op == STORE_SLICE) |
| 2222 | com_pop(c, 2); |
| 2223 | else if (op == DELETE_SLICE) |
| 2224 | com_pop(c, 1); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2225 | return; |
| 2226 | } |
| 2227 | } |
| 2228 | /* Else normal subscriptlist. Compile each subscript. */ |
| 2229 | for (i = 0; i < NCH(n); i += 2) |
| 2230 | com_subscript(c, CHILD(n, i)); |
| 2231 | /* Put multiple subscripts into a tuple */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2232 | if (NCH(n) > 1) { |
| 2233 | i = (NCH(n)+1) / 2; |
| 2234 | com_addoparg(c, BUILD_TUPLE, i); |
| 2235 | com_pop(c, i-1); |
| 2236 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2237 | switch (assigning) { |
| 2238 | case OP_DELETE: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2239 | op = DELETE_SUBSCR; |
| 2240 | i = 2; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2241 | break; |
| 2242 | default: |
| 2243 | case OP_ASSIGN: |
| 2244 | op = STORE_SUBSCR; |
| 2245 | i = 3; |
| 2246 | break; |
| 2247 | case OP_APPLY: |
| 2248 | op = BINARY_SUBSCR; |
| 2249 | i = 1; |
| 2250 | break; |
| 2251 | } |
| 2252 | if (assigning > OP_APPLY) { |
| 2253 | com_addoparg(c, DUP_TOPX, 2); |
| 2254 | com_push(c, 2); |
| 2255 | com_addbyte(c, BINARY_SUBSCR); |
| 2256 | com_pop(c, 1); |
| 2257 | com_node(c, augn); |
| 2258 | com_addbyte(c, assigning); |
| 2259 | com_pop(c, 1); |
| 2260 | com_addbyte(c, ROT_THREE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2261 | } |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2262 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2263 | com_pop(c, i); |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2267 | com_apply_trailer(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2268 | { |
| 2269 | REQ(n, trailer); |
| 2270 | switch (TYPE(CHILD(n, 0))) { |
| 2271 | case LPAR: |
| 2272 | com_call_function(c, CHILD(n, 1)); |
| 2273 | break; |
| 2274 | case DOT: |
| 2275 | com_select_member(c, CHILD(n, 1)); |
| 2276 | break; |
| 2277 | case LSQB: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2278 | com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2281 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2282 | "com_apply_trailer: unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2287 | com_power(struct compiling *c, node *n) |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2288 | { |
| 2289 | int i; |
| 2290 | REQ(n, power); |
| 2291 | com_atom(c, CHILD(n, 0)); |
| 2292 | for (i = 1; i < NCH(n); i++) { |
| 2293 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
| 2294 | com_factor(c, CHILD(n, i+1)); |
| 2295 | com_addbyte(c, BINARY_POWER); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2296 | com_pop(c, 1); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2297 | break; |
| 2298 | } |
| 2299 | else |
| 2300 | com_apply_trailer(c, CHILD(n, i)); |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | static void |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2305 | com_invert_constant(struct compiling *c, node *n) |
| 2306 | { |
| 2307 | /* Compute the inverse of int and longs and use them directly, |
| 2308 | but be prepared to generate code for all other |
| 2309 | possibilities (invalid numbers, floats, complex). |
| 2310 | */ |
| 2311 | PyObject *num, *inv = NULL; |
| 2312 | int i; |
| 2313 | |
| 2314 | REQ(n, NUMBER); |
| 2315 | num = parsenumber(c, STR(n)); |
| 2316 | if (num == NULL) |
| 2317 | i = 255; |
| 2318 | else { |
| 2319 | inv = PyNumber_Invert(num); |
| 2320 | if (inv == NULL) { |
| 2321 | PyErr_Clear(); |
| 2322 | i = com_addconst(c, num); |
| 2323 | } else { |
| 2324 | i = com_addconst(c, inv); |
| 2325 | Py_DECREF(inv); |
| 2326 | } |
| 2327 | Py_DECREF(num); |
| 2328 | } |
| 2329 | com_addoparg(c, LOAD_CONST, i); |
| 2330 | com_push(c, 1); |
| 2331 | if (num != NULL && inv == NULL) |
| 2332 | com_addbyte(c, UNARY_INVERT); |
| 2333 | } |
| 2334 | |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2335 | static int |
| 2336 | is_float_zero(const char *p) |
| 2337 | { |
| 2338 | int found_radix_point = 0; |
| 2339 | int ch; |
| 2340 | while ((ch = Py_CHARMASK(*p++)) != '\0') { |
| 2341 | switch (ch) { |
| 2342 | case '0': |
| 2343 | /* no reason to believe it's not 0 -- continue */ |
| 2344 | break; |
| 2345 | |
| 2346 | case 'e': case 'E': case 'j': case 'J': |
| 2347 | /* If this was a hex constant, we already would have |
| 2348 | returned 0 due to the 'x' or 'X', so 'e' or 'E' |
| 2349 | must be an exponent marker, and we haven't yet |
| 2350 | seen a non-zero digit, and it doesn't matter what |
| 2351 | the exponent is then. For 'j' or 'J' similarly, |
| 2352 | except that this is an imaginary 0 then. */ |
| 2353 | return 1; |
| 2354 | |
| 2355 | case '.': |
| 2356 | found_radix_point = 1; |
| 2357 | break; |
| 2358 | |
| 2359 | default: |
| 2360 | return 0; |
| 2361 | } |
| 2362 | } |
| 2363 | return found_radix_point; |
| 2364 | } |
| 2365 | |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2366 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2367 | com_factor(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2368 | { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2369 | int childtype = TYPE(CHILD(n, 0)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2370 | node *pfactor, *ppower, *patom, *pnum; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2371 | REQ(n, factor); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2372 | /* If the unary +, -, or ~ operator is applied to a constant, |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2373 | don't generate a UNARY_xxx opcode. Just store the |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2374 | approriate value as a constant. If the value is negative, |
| 2375 | extend the string containing the constant and insert a |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2376 | negative in the 0th position -- unless we're doing unary minus |
| 2377 | of a floating zero! In that case the sign is significant, but |
| 2378 | the const dict can't distinguish +0.0 from -0.0. |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2379 | */ |
| 2380 | if ((childtype == PLUS || childtype == MINUS || childtype == TILDE) |
Fred Drake | 14ef244 | 2001-08-30 18:53:25 +0000 | [diff] [blame] | 2381 | && NCH(n) == 2 |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2382 | && TYPE((pfactor = CHILD(n, 1))) == factor |
| 2383 | && NCH(pfactor) == 1 |
| 2384 | && TYPE((ppower = CHILD(pfactor, 0))) == power |
| 2385 | && NCH(ppower) == 1 |
| 2386 | && TYPE((patom = CHILD(ppower, 0))) == atom |
| 2387 | && TYPE((pnum = CHILD(patom, 0))) == NUMBER |
Guido van Rossum | 66b1259 | 2003-02-12 16:57:47 +0000 | [diff] [blame] | 2388 | && !(childtype == MINUS && |
| 2389 | (STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) { |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2390 | if (childtype == TILDE) { |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2391 | com_invert_constant(c, pnum); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2392 | return; |
| 2393 | } |
| 2394 | if (childtype == MINUS) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2395 | char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2396 | if (s == NULL) { |
| 2397 | com_error(c, PyExc_MemoryError, ""); |
| 2398 | com_addbyte(c, 255); |
| 2399 | return; |
| 2400 | } |
| 2401 | s[0] = '-'; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2402 | strcpy(s + 1, STR(pnum)); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 2403 | PyObject_FREE(STR(pnum)); |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2404 | STR(pnum) = s; |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2405 | } |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 2406 | com_atom(c, patom); |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2407 | } |
| 2408 | else if (childtype == PLUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2409 | com_factor(c, CHILD(n, 1)); |
| 2410 | com_addbyte(c, UNARY_POSITIVE); |
| 2411 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2412 | else if (childtype == MINUS) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2413 | com_factor(c, CHILD(n, 1)); |
| 2414 | com_addbyte(c, UNARY_NEGATIVE); |
| 2415 | } |
Jeremy Hylton | d5e5a2a | 2001-08-12 01:54:38 +0000 | [diff] [blame] | 2416 | else if (childtype == TILDE) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2417 | com_factor(c, CHILD(n, 1)); |
| 2418 | com_addbyte(c, UNARY_INVERT); |
| 2419 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2420 | else { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2421 | com_power(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2426 | com_term(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2427 | { |
| 2428 | int i; |
| 2429 | int op; |
| 2430 | REQ(n, term); |
| 2431 | com_factor(c, CHILD(n, 0)); |
| 2432 | for (i = 2; i < NCH(n); i += 2) { |
| 2433 | com_factor(c, CHILD(n, i)); |
| 2434 | switch (TYPE(CHILD(n, i-1))) { |
| 2435 | case STAR: |
| 2436 | op = BINARY_MULTIPLY; |
| 2437 | break; |
| 2438 | case SLASH: |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2439 | if (c->c_flags & CO_FUTURE_DIVISION) |
| 2440 | op = BINARY_TRUE_DIVIDE; |
| 2441 | else |
| 2442 | op = BINARY_DIVIDE; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2443 | break; |
| 2444 | case PERCENT: |
| 2445 | op = BINARY_MODULO; |
| 2446 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2447 | case DOUBLESLASH: |
| 2448 | op = BINARY_FLOOR_DIVIDE; |
| 2449 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2450 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2451 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 2452 | "com_term: operator not *, /, // or %"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2453 | op = 255; |
| 2454 | } |
| 2455 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2456 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2461 | com_arith_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2462 | { |
| 2463 | int i; |
| 2464 | int op; |
| 2465 | REQ(n, arith_expr); |
| 2466 | com_term(c, CHILD(n, 0)); |
| 2467 | for (i = 2; i < NCH(n); i += 2) { |
| 2468 | com_term(c, CHILD(n, i)); |
| 2469 | switch (TYPE(CHILD(n, i-1))) { |
| 2470 | case PLUS: |
| 2471 | op = BINARY_ADD; |
| 2472 | break; |
| 2473 | case MINUS: |
| 2474 | op = BINARY_SUBTRACT; |
| 2475 | break; |
| 2476 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2477 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2478 | "com_arith_expr: operator not + or -"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2479 | op = 255; |
| 2480 | } |
| 2481 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2482 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2487 | com_shift_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2488 | { |
| 2489 | int i; |
| 2490 | int op; |
| 2491 | REQ(n, shift_expr); |
| 2492 | com_arith_expr(c, CHILD(n, 0)); |
| 2493 | for (i = 2; i < NCH(n); i += 2) { |
| 2494 | com_arith_expr(c, CHILD(n, i)); |
| 2495 | switch (TYPE(CHILD(n, i-1))) { |
| 2496 | case LEFTSHIFT: |
| 2497 | op = BINARY_LSHIFT; |
| 2498 | break; |
| 2499 | case RIGHTSHIFT: |
| 2500 | op = BINARY_RSHIFT; |
| 2501 | break; |
| 2502 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2503 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2504 | "com_shift_expr: operator not << or >>"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2505 | op = 255; |
| 2506 | } |
| 2507 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2508 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2513 | com_and_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2514 | { |
| 2515 | int i; |
| 2516 | int op; |
| 2517 | REQ(n, and_expr); |
| 2518 | com_shift_expr(c, CHILD(n, 0)); |
| 2519 | for (i = 2; i < NCH(n); i += 2) { |
| 2520 | com_shift_expr(c, CHILD(n, i)); |
| 2521 | if (TYPE(CHILD(n, i-1)) == AMPER) { |
| 2522 | op = BINARY_AND; |
| 2523 | } |
| 2524 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2525 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2526 | "com_and_expr: operator not &"); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2527 | op = 255; |
| 2528 | } |
| 2529 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2530 | com_pop(c, 1); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2531 | } |
| 2532 | } |
| 2533 | |
| 2534 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2535 | com_xor_expr(struct compiling *c, node *n) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2536 | { |
| 2537 | int i; |
| 2538 | int op; |
| 2539 | REQ(n, xor_expr); |
| 2540 | com_and_expr(c, CHILD(n, 0)); |
| 2541 | for (i = 2; i < NCH(n); i += 2) { |
| 2542 | com_and_expr(c, CHILD(n, i)); |
| 2543 | if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) { |
| 2544 | op = BINARY_XOR; |
| 2545 | } |
| 2546 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2547 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2548 | "com_xor_expr: operator not ^"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2549 | op = 255; |
| 2550 | } |
| 2551 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2552 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2557 | com_expr(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2558 | { |
| 2559 | int i; |
| 2560 | int op; |
| 2561 | REQ(n, expr); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2562 | com_xor_expr(c, CHILD(n, 0)); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2563 | for (i = 2; i < NCH(n); i += 2) { |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2564 | com_xor_expr(c, CHILD(n, i)); |
| 2565 | if (TYPE(CHILD(n, i-1)) == VBAR) { |
| 2566 | op = BINARY_OR; |
| 2567 | } |
| 2568 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2569 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2570 | "com_expr: expr operator not |"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2571 | op = 255; |
| 2572 | } |
| 2573 | com_addbyte(c, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2574 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | static enum cmp_op |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2579 | cmp_type(node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2580 | { |
| 2581 | REQ(n, comp_op); |
Tim Peters | 12d55a7 | 2003-05-12 19:16:52 +0000 | [diff] [blame] | 2582 | /* comp_op: '<' | '>' | '>=' | '<=' | '<>' | '!=' | '==' |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2583 | | 'in' | 'not' 'in' | 'is' | 'is' not' */ |
| 2584 | if (NCH(n) == 1) { |
| 2585 | n = CHILD(n, 0); |
| 2586 | switch (TYPE(n)) { |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2587 | case LESS: return PyCmp_LT; |
| 2588 | case GREATER: return PyCmp_GT; |
Tim Peters | 12d55a7 | 2003-05-12 19:16:52 +0000 | [diff] [blame] | 2589 | case EQEQUAL: return PyCmp_EQ; |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2590 | case LESSEQUAL: return PyCmp_LE; |
| 2591 | case GREATEREQUAL: return PyCmp_GE; |
| 2592 | case NOTEQUAL: return PyCmp_NE; /* <> or != */ |
| 2593 | case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN; |
| 2594 | if (strcmp(STR(n), "is") == 0) return PyCmp_IS; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2595 | } |
| 2596 | } |
| 2597 | else if (NCH(n) == 2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2598 | switch (TYPE(CHILD(n, 0))) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2599 | case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2600 | return PyCmp_NOT_IN; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2601 | if (strcmp(STR(CHILD(n, 0)), "is") == 0) |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2602 | return PyCmp_IS_NOT; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2605 | return PyCmp_BAD; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2609 | com_comparison(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2610 | { |
| 2611 | int i; |
| 2612 | enum cmp_op op; |
| 2613 | int anchor; |
| 2614 | REQ(n, comparison); /* comparison: expr (comp_op expr)* */ |
| 2615 | com_expr(c, CHILD(n, 0)); |
| 2616 | if (NCH(n) == 1) |
| 2617 | return; |
| 2618 | |
| 2619 | /**************************************************************** |
| 2620 | The following code is generated for all but the last |
| 2621 | comparison in a chain: |
| 2622 | |
| 2623 | label: on stack: opcode: jump to: |
| 2624 | |
| 2625 | a <code to load b> |
| 2626 | a, b DUP_TOP |
| 2627 | a, b, b ROT_THREE |
| 2628 | b, a, b COMPARE_OP |
| 2629 | b, 0-or-1 JUMP_IF_FALSE L1 |
| 2630 | b, 1 POP_TOP |
| 2631 | b |
| 2632 | |
| 2633 | We are now ready to repeat this sequence for the next |
| 2634 | comparison in the chain. |
| 2635 | |
| 2636 | For the last we generate: |
| 2637 | |
| 2638 | b <code to load c> |
| 2639 | b, c COMPARE_OP |
| 2640 | 0-or-1 |
| 2641 | |
| 2642 | If there were any jumps to L1 (i.e., there was more than one |
| 2643 | comparison), we generate: |
| 2644 | |
| 2645 | 0-or-1 JUMP_FORWARD L2 |
| 2646 | L1: b, 0 ROT_TWO |
| 2647 | 0, b POP_TOP |
| 2648 | 0 |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2649 | L2: 0-or-1 |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2650 | ****************************************************************/ |
| 2651 | |
| 2652 | anchor = 0; |
| 2653 | |
| 2654 | for (i = 2; i < NCH(n); i += 2) { |
| 2655 | com_expr(c, CHILD(n, i)); |
| 2656 | if (i+2 < NCH(n)) { |
| 2657 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2658 | com_push(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2659 | com_addbyte(c, ROT_THREE); |
| 2660 | } |
| 2661 | op = cmp_type(CHILD(n, i-1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 2662 | if (op == PyCmp_BAD) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2663 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2664 | "com_comparison: unknown comparison op"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2665 | } |
| 2666 | com_addoparg(c, COMPARE_OP, op); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2667 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2668 | if (i+2 < NCH(n)) { |
| 2669 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2670 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2671 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | if (anchor) { |
| 2676 | int anchor2 = 0; |
| 2677 | com_addfwref(c, JUMP_FORWARD, &anchor2); |
| 2678 | com_backpatch(c, anchor); |
| 2679 | com_addbyte(c, ROT_TWO); |
| 2680 | com_addbyte(c, POP_TOP); |
| 2681 | com_backpatch(c, anchor2); |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2686 | com_not_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2687 | { |
| 2688 | REQ(n, not_test); /* 'not' not_test | comparison */ |
| 2689 | if (NCH(n) == 1) { |
| 2690 | com_comparison(c, CHILD(n, 0)); |
| 2691 | } |
| 2692 | else { |
| 2693 | com_not_test(c, CHILD(n, 1)); |
| 2694 | com_addbyte(c, UNARY_NOT); |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2699 | com_and_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2700 | { |
| 2701 | int i; |
| 2702 | int anchor; |
| 2703 | REQ(n, and_test); /* not_test ('and' not_test)* */ |
| 2704 | anchor = 0; |
| 2705 | i = 0; |
| 2706 | for (;;) { |
| 2707 | com_not_test(c, CHILD(n, i)); |
| 2708 | if ((i += 2) >= NCH(n)) |
| 2709 | break; |
| 2710 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 2711 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2712 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2713 | } |
| 2714 | if (anchor) |
| 2715 | com_backpatch(c, anchor); |
| 2716 | } |
| 2717 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2718 | static int |
| 2719 | com_make_closure(struct compiling *c, PyCodeObject *co) |
| 2720 | { |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2721 | int i, free = PyCode_GetNumFree(co); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2722 | if (free == 0) |
| 2723 | return 0; |
| 2724 | for (i = 0; i < free; ++i) { |
| 2725 | /* Bypass com_addop_varname because it will generate |
| 2726 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 2727 | */ |
| 2728 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 2729 | int arg, reftype; |
| 2730 | |
| 2731 | /* Special case: If a class contains a method with a |
| 2732 | free variable that has the same name as a method, |
| 2733 | the name will be considered free *and* local in the |
| 2734 | class. It should be handled by the closure, as |
| 2735 | well as by the normal name loookup logic. |
| 2736 | */ |
| 2737 | reftype = get_ref_type(c, PyString_AS_STRING(name)); |
| 2738 | if (reftype == CELL) |
| 2739 | arg = com_lookup_arg(c->c_cellvars, name); |
| 2740 | else /* (reftype == FREE) */ |
| 2741 | arg = com_lookup_arg(c->c_freevars, name); |
| 2742 | if (arg == -1) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 2743 | fprintf(stderr, "lookup %s in %s %d %d\n" |
| 2744 | "freevars of %s: %s\n", |
| 2745 | PyObject_REPR(name), |
| 2746 | c->c_name, |
| 2747 | reftype, arg, |
| 2748 | PyString_AS_STRING(co->co_name), |
| 2749 | PyObject_REPR(co->co_freevars)); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2750 | Py_FatalError("com_make_closure()"); |
| 2751 | } |
| 2752 | com_addoparg(c, LOAD_CLOSURE, arg); |
| 2753 | |
| 2754 | } |
| 2755 | com_push(c, free); |
| 2756 | return 1; |
| 2757 | } |
| 2758 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2759 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2760 | com_test(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2761 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2762 | REQ(n, test); /* and_test ('or' and_test)* | lambdef */ |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2763 | if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2764 | PyCodeObject *co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2765 | int i, closure; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 2766 | int ndefs = com_argdefs(c, CHILD(n, 0)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 2767 | symtable_enter_scope(c->c_symtable, "lambda", lambdef, |
| 2768 | n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2769 | co = icompile(CHILD(n, 0), c); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2770 | if (co == NULL) { |
| 2771 | c->c_errors++; |
| 2772 | return; |
| 2773 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2774 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2775 | i = com_addconst(c, (PyObject *)co); |
| 2776 | closure = com_make_closure(c, co); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2777 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2778 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2779 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2780 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 2781 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2782 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 2783 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 2784 | Py_DECREF(co); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2785 | com_pop(c, ndefs); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2786 | } |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2787 | else { |
| 2788 | int anchor = 0; |
| 2789 | int i = 0; |
| 2790 | for (;;) { |
| 2791 | com_and_test(c, CHILD(n, i)); |
| 2792 | if ((i += 2) >= NCH(n)) |
| 2793 | break; |
| 2794 | com_addfwref(c, JUMP_IF_TRUE, &anchor); |
| 2795 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2796 | com_pop(c, 1); |
Guido van Rossum | 57531fe | 1993-11-30 14:57:42 +0000 | [diff] [blame] | 2797 | } |
| 2798 | if (anchor) |
| 2799 | com_backpatch(c, anchor); |
| 2800 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2804 | com_list(struct compiling *c, node *n, int toplevel) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2805 | { |
| 2806 | /* exprlist: expr (',' expr)* [',']; likewise for testlist */ |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 2807 | if (NCH(n) == 1 && !toplevel) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2808 | com_node(c, CHILD(n, 0)); |
| 2809 | } |
| 2810 | else { |
| 2811 | int i; |
| 2812 | int len; |
| 2813 | len = (NCH(n) + 1) / 2; |
| 2814 | for (i = 0; i < NCH(n); i += 2) |
| 2815 | com_node(c, CHILD(n, i)); |
| 2816 | com_addoparg(c, BUILD_TUPLE, len); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2817 | com_pop(c, len-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | |
| 2822 | /* Begin of assignment compilation */ |
| 2823 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2824 | |
| 2825 | static void |
| 2826 | com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn) |
| 2827 | { |
| 2828 | com_addbyte(c, DUP_TOP); |
| 2829 | com_push(c, 1); |
| 2830 | com_addopname(c, LOAD_ATTR, n); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2831 | com_node(c, augn); |
| 2832 | com_addbyte(c, opcode); |
| 2833 | com_pop(c, 1); |
| 2834 | com_addbyte(c, ROT_TWO); |
| 2835 | com_addopname(c, STORE_ATTR, n); |
| 2836 | com_pop(c, 2); |
| 2837 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2838 | |
| 2839 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2840 | com_assign_attr(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2841 | { |
Guido van Rossum | 3ac99d4 | 2002-08-16 02:13:49 +0000 | [diff] [blame] | 2842 | if (none_assignment_check(c, STR(n), assigning)) |
| 2843 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2844 | com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2845 | com_pop(c, assigning ? 2 : 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
| 2848 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2849 | 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] | 2850 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2851 | REQ(n, trailer); |
| 2852 | switch (TYPE(CHILD(n, 0))) { |
| 2853 | case LPAR: /* '(' [exprlist] ')' */ |
Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 2854 | if (assigning == OP_DELETE) |
| 2855 | com_error(c, PyExc_SyntaxError, |
| 2856 | "can't delete function call"); |
| 2857 | else |
| 2858 | com_error(c, PyExc_SyntaxError, |
| 2859 | "can't assign to function call"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2860 | break; |
| 2861 | case DOT: /* '.' NAME */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2862 | if (assigning > OP_APPLY) |
| 2863 | com_augassign_attr(c, CHILD(n, 1), assigning, augn); |
| 2864 | else |
| 2865 | com_assign_attr(c, CHILD(n, 1), assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2866 | break; |
Guido van Rossum | 8861b74 | 1996-07-30 16:49:37 +0000 | [diff] [blame] | 2867 | case LSQB: /* '[' subscriptlist ']' */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2868 | com_subscriptlist(c, CHILD(n, 1), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2869 | break; |
| 2870 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2871 | com_error(c, PyExc_SystemError, "unknown trailer type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | static void |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2876 | com_assign_sequence(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2877 | { |
| 2878 | int i; |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2879 | if (TYPE(n) != testlist && TYPE(n) != testlist_gexp && |
| 2880 | TYPE(n) != listmaker) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2881 | REQ(n, exprlist); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2882 | if (assigning) { |
| 2883 | i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2884 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2885 | com_push(c, i-1); |
| 2886 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2887 | for (i = 0; i < NCH(n); i += 2) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2888 | com_assign(c, CHILD(n, i), assigning, NULL); |
| 2889 | } |
| 2890 | |
| 2891 | static void |
| 2892 | com_augassign_name(struct compiling *c, node *n, int opcode, node *augn) |
| 2893 | { |
| 2894 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2895 | com_addop_varname(c, VAR_LOAD, STR(n)); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2896 | com_push(c, 1); |
| 2897 | com_node(c, augn); |
| 2898 | com_addbyte(c, opcode); |
| 2899 | com_pop(c, 1); |
| 2900 | com_assign_name(c, n, OP_ASSIGN); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 2904 | com_assign_name(struct compiling *c, node *n, int assigning) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2905 | { |
| 2906 | REQ(n, NAME); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 2907 | com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 2908 | if (assigning) |
| 2909 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | static void |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2913 | com_assign(struct compiling *c, node *n, int assigning, node *augn) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2914 | { |
| 2915 | /* Loop to avoid trivial recursion */ |
| 2916 | for (;;) { |
| 2917 | switch (TYPE(n)) { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2918 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2919 | case exprlist: |
| 2920 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 2921 | case testlist1: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2922 | case testlist_gexp: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2923 | if (NCH(n) > 1) { |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 2924 | if (TYPE(CHILD(n, 1)) == gen_for) { |
| 2925 | com_error(c, PyExc_SystemError, |
| 2926 | "assign to generator expression not possible"); |
| 2927 | return; |
| 2928 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2929 | if (assigning > OP_APPLY) { |
| 2930 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 2931 | "augmented assign to tuple not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2932 | return; |
| 2933 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 2934 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2935 | return; |
| 2936 | } |
| 2937 | n = CHILD(n, 0); |
| 2938 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2939 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2940 | case test: |
| 2941 | case and_test: |
| 2942 | case not_test: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2943 | case comparison: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2944 | case expr: |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 2945 | case xor_expr: |
| 2946 | case and_expr: |
| 2947 | case shift_expr: |
| 2948 | case arith_expr: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2949 | case term: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2950 | case factor: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2951 | if (NCH(n) > 1) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2952 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2953 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2954 | return; |
| 2955 | } |
| 2956 | n = CHILD(n, 0); |
| 2957 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2958 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 2959 | case power: /* atom trailer* ('**' power)* |
| 2960 | ('+'|'-'|'~') factor | atom trailer* */ |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2961 | if (TYPE(CHILD(n, 0)) != atom) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2962 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2963 | "can't assign to operator"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2964 | return; |
| 2965 | } |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2966 | if (NCH(n) > 1) { /* trailer or exponent present */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2967 | int i; |
| 2968 | com_node(c, CHILD(n, 0)); |
| 2969 | for (i = 1; i+1 < NCH(n); i++) { |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2970 | if (TYPE(CHILD(n, i)) == DOUBLESTAR) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2971 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2972 | "can't assign to operator"); |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 2973 | return; |
| 2974 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2975 | com_apply_trailer(c, CHILD(n, i)); |
| 2976 | } /* NB i is still alive */ |
| 2977 | com_assign_trailer(c, |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2978 | CHILD(n, i), assigning, augn); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2979 | return; |
| 2980 | } |
| 2981 | n = CHILD(n, 0); |
| 2982 | break; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 2983 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2984 | case atom: |
| 2985 | switch (TYPE(CHILD(n, 0))) { |
| 2986 | case LPAR: |
| 2987 | n = CHILD(n, 1); |
| 2988 | if (TYPE(n) == RPAR) { |
| 2989 | /* XXX Should allow () = () ??? */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 2990 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 2991 | "can't assign to ()"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2992 | return; |
| 2993 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2994 | if (assigning > OP_APPLY) { |
| 2995 | com_error(c, PyExc_SyntaxError, |
Neal Norwitz | a1d654e | 2003-05-22 22:00:04 +0000 | [diff] [blame] | 2996 | "augmented assign to tuple literal not possible"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 2997 | return; |
| 2998 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | case LSQB: |
| 3001 | n = CHILD(n, 1); |
| 3002 | if (TYPE(n) == RSQB) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3003 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3004 | "can't assign to []"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3005 | return; |
| 3006 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3007 | if (assigning > OP_APPLY) { |
| 3008 | com_error(c, PyExc_SyntaxError, |
Neal Norwitz | a1d654e | 2003-05-22 22:00:04 +0000 | [diff] [blame] | 3009 | "augmented assign to list literal not possible"); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3010 | return; |
| 3011 | } |
| 3012 | if (NCH(n) > 1 |
| 3013 | && TYPE(CHILD(n, 1)) == list_for) { |
| 3014 | com_error(c, PyExc_SyntaxError, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3015 | "can't assign to list comprehension"); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3016 | return; |
| 3017 | } |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 3018 | com_assign_sequence(c, n, assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3019 | return; |
| 3020 | case NAME: |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3021 | if (assigning > OP_APPLY) |
| 3022 | com_augassign_name(c, CHILD(n, 0), |
| 3023 | assigning, augn); |
| 3024 | else |
| 3025 | com_assign_name(c, CHILD(n, 0), |
| 3026 | assigning); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3027 | return; |
| 3028 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3029 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3030 | "can't assign to literal"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3031 | return; |
| 3032 | } |
| 3033 | break; |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 3034 | |
| 3035 | case lambdef: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3036 | com_error(c, PyExc_SyntaxError, |
| 3037 | "can't assign to lambda"); |
Guido van Rossum | 15cc9a0 | 1996-08-08 18:51:04 +0000 | [diff] [blame] | 3038 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3039 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3040 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3041 | com_error(c, PyExc_SystemError, |
| 3042 | "com_assign: bad node"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3043 | return; |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3044 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3048 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3049 | static void |
| 3050 | com_augassign(struct compiling *c, node *n) |
| 3051 | { |
| 3052 | int opcode; |
| 3053 | |
| 3054 | switch (STR(CHILD(CHILD(n, 1), 0))[0]) { |
| 3055 | case '+': opcode = INPLACE_ADD; break; |
| 3056 | case '-': opcode = INPLACE_SUBTRACT; break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 3057 | case '/': |
| 3058 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '/') |
| 3059 | opcode = INPLACE_FLOOR_DIVIDE; |
| 3060 | else if (c->c_flags & CO_FUTURE_DIVISION) |
| 3061 | opcode = INPLACE_TRUE_DIVIDE; |
| 3062 | else |
| 3063 | opcode = INPLACE_DIVIDE; |
| 3064 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3065 | case '%': opcode = INPLACE_MODULO; break; |
| 3066 | case '<': opcode = INPLACE_LSHIFT; break; |
| 3067 | case '>': opcode = INPLACE_RSHIFT; break; |
| 3068 | case '&': opcode = INPLACE_AND; break; |
| 3069 | case '^': opcode = INPLACE_XOR; break; |
| 3070 | case '|': opcode = INPLACE_OR; break; |
| 3071 | case '*': |
| 3072 | if (STR(CHILD(CHILD(n, 1), 0))[1] == '*') |
| 3073 | opcode = INPLACE_POWER; |
| 3074 | else |
| 3075 | opcode = INPLACE_MULTIPLY; |
| 3076 | break; |
| 3077 | default: |
| 3078 | com_error(c, PyExc_SystemError, "com_augassign: bad operator"); |
| 3079 | return; |
| 3080 | } |
| 3081 | com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2)); |
| 3082 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3083 | |
| 3084 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3085 | com_expr_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3086 | { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3087 | REQ(n, expr_stmt); |
| 3088 | /* testlist (('=' testlist)* | augassign testlist) */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3089 | /* Forget it if we have just a doc string here */ |
Guido van Rossum | 5f5e817 | 1997-04-06 03:41:40 +0000 | [diff] [blame] | 3090 | if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3091 | return; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3092 | if (NCH(n) == 1) { |
| 3093 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3094 | if (c->c_interactive) |
| 3095 | com_addbyte(c, PRINT_EXPR); |
| 3096 | else |
| 3097 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3098 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3099 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3100 | else if (TYPE(CHILD(n,1)) == augassign) |
| 3101 | com_augassign(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3102 | else { |
| 3103 | int i; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3104 | com_node(c, CHILD(n, NCH(n)-1)); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3105 | for (i = 0; i < NCH(n)-2; i+=2) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3106 | if (i+2 < NCH(n)-2) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3107 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3108 | com_push(c, 1); |
| 3109 | } |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3110 | com_assign(c, CHILD(n, i), OP_ASSIGN, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3111 | } |
| 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3116 | com_assert_stmt(struct compiling *c, node *n) |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3117 | { |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 3118 | int a = 0; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3119 | int i; |
| 3120 | REQ(n, assert_stmt); /* 'assert' test [',' test] */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 3121 | if (Py_OptimizeFlag) |
| 3122 | return; |
| 3123 | /* Generate code like |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3124 | |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 3125 | if not <test>: |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3126 | raise AssertionError [, <message>] |
| 3127 | |
| 3128 | where <message> is the second test, if present. |
| 3129 | */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3130 | com_node(c, CHILD(n, 1)); |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 3131 | com_addfwref(c, JUMP_IF_TRUE, &a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3132 | com_addbyte(c, POP_TOP); |
| 3133 | com_pop(c, 1); |
| 3134 | /* Raise that exception! */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3135 | com_addop_name(c, LOAD_GLOBAL, "AssertionError"); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3136 | com_push(c, 1); |
| 3137 | i = NCH(n)/2; /* Either 2 or 4 */ |
| 3138 | if (i > 1) |
| 3139 | com_node(c, CHILD(n, 3)); |
| 3140 | com_addoparg(c, RAISE_VARARGS, i); |
| 3141 | com_pop(c, i); |
| 3142 | /* The interpreter does not fall through */ |
Neil Schemenauer | 89e3ee0 | 2002-04-26 01:58:53 +0000 | [diff] [blame] | 3143 | /* Jump ends up here */ |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3144 | com_backpatch(c, a); |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 3145 | com_addbyte(c, POP_TOP); |
| 3146 | } |
| 3147 | |
| 3148 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3149 | com_print_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3150 | { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3151 | int i = 1; |
| 3152 | node* stream = NULL; |
| 3153 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3154 | REQ(n, print_stmt); /* 'print' (test ',')* [test] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3155 | |
| 3156 | /* are we using the extended print form? */ |
| 3157 | if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) { |
| 3158 | stream = CHILD(n, 2); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3159 | com_node(c, stream); |
| 3160 | /* stack: [...] => [... stream] */ |
| 3161 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3162 | if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA) |
| 3163 | i = 4; |
| 3164 | else |
| 3165 | i = 3; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3166 | } |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3167 | for (; i < NCH(n); i += 2) { |
| 3168 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3169 | com_addbyte(c, DUP_TOP); |
| 3170 | /* stack: [stream] => [stream stream] */ |
| 3171 | com_push(c, 1); |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3172 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3173 | /* stack: [stream stream] => [stream stream obj] */ |
| 3174 | com_addbyte(c, ROT_TWO); |
| 3175 | /* stack: [stream stream obj] => [stream obj stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3176 | com_addbyte(c, PRINT_ITEM_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3177 | /* stack: [stream obj stream] => [stream] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3178 | com_pop(c, 2); |
| 3179 | } |
| 3180 | else { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3181 | com_node(c, CHILD(n, i)); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3182 | /* stack: [...] => [... obj] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3183 | com_addbyte(c, PRINT_ITEM); |
| 3184 | com_pop(c, 1); |
| 3185 | } |
| 3186 | } |
| 3187 | /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */ |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3188 | if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) { |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3189 | if (stream != NULL) { |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3190 | /* must pop the extra stream object off the stack */ |
| 3191 | com_addbyte(c, POP_TOP); |
| 3192 | /* stack: [... stream] => [...] */ |
| 3193 | com_pop(c, 1); |
| 3194 | } |
| 3195 | } |
| 3196 | else { |
| 3197 | if (stream != NULL) { |
| 3198 | /* this consumes the last stream object on stack */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3199 | com_addbyte(c, PRINT_NEWLINE_TO); |
Barry Warsaw | 24703a0 | 2000-08-21 17:07:20 +0000 | [diff] [blame] | 3200 | /* stack: [... stream] => [...] */ |
Barry Warsaw | 29c574e | 2000-08-21 15:38:56 +0000 | [diff] [blame] | 3201 | com_pop(c, 1); |
| 3202 | } |
| 3203 | else |
| 3204 | com_addbyte(c, PRINT_NEWLINE); |
| 3205 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3209 | com_return_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3210 | { |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3211 | REQ(n, return_stmt); /* 'return' [testlist] */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3212 | if (!c->c_infunction) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3213 | com_error(c, PyExc_SyntaxError, "'return' outside function"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3214 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3215 | if (c->c_flags & CO_GENERATOR) { |
| 3216 | if (NCH(n) > 1) { |
| 3217 | com_error(c, PyExc_SyntaxError, |
| 3218 | "'return' with argument inside generator"); |
| 3219 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3220 | } |
| 3221 | if (NCH(n) < 2) { |
| 3222 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3223 | com_push(c, 1); |
| 3224 | } |
Tim Peters | ad1a18b | 2001-06-23 06:19:16 +0000 | [diff] [blame] | 3225 | else |
| 3226 | com_node(c, CHILD(n, 1)); |
| 3227 | com_addbyte(c, RETURN_VALUE); |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3228 | com_pop(c, 1); |
| 3229 | } |
| 3230 | |
| 3231 | static void |
| 3232 | com_yield_stmt(struct compiling *c, node *n) |
| 3233 | { |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 3234 | int i; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3235 | REQ(n, yield_stmt); /* 'yield' testlist */ |
| 3236 | if (!c->c_infunction) { |
| 3237 | com_error(c, PyExc_SyntaxError, "'yield' outside function"); |
| 3238 | } |
Tim Peters | 95c80f8 | 2001-06-23 02:07:08 +0000 | [diff] [blame] | 3239 | |
| 3240 | for (i = 0; i < c->c_nblocks; ++i) { |
| 3241 | if (c->c_block[i] == SETUP_FINALLY) { |
| 3242 | com_error(c, PyExc_SyntaxError, |
| 3243 | "'yield' not allowed in a 'try' block " |
| 3244 | "with a 'finally' clause"); |
| 3245 | return; |
| 3246 | } |
| 3247 | } |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 3248 | com_node(c, CHILD(n, 1)); |
| 3249 | com_addbyte(c, YIELD_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3250 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3254 | com_raise_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3255 | { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3256 | int i; |
Guido van Rossum | d295f12 | 1998-04-09 21:39:57 +0000 | [diff] [blame] | 3257 | REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */ |
| 3258 | if (NCH(n) > 1) { |
| 3259 | com_node(c, CHILD(n, 1)); |
| 3260 | if (NCH(n) > 3) { |
| 3261 | com_node(c, CHILD(n, 3)); |
| 3262 | if (NCH(n) > 5) |
| 3263 | com_node(c, CHILD(n, 5)); |
| 3264 | } |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3265 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3266 | i = NCH(n)/2; |
| 3267 | com_addoparg(c, RAISE_VARARGS, i); |
| 3268 | com_pop(c, i); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | static void |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3272 | com_from_import(struct compiling *c, node *n) |
| 3273 | { |
| 3274 | com_addopname(c, IMPORT_FROM, CHILD(n, 0)); |
| 3275 | com_push(c, 1); |
| 3276 | if (NCH(n) > 1) { |
| 3277 | if (strcmp(STR(CHILD(n, 1)), "as") != 0) { |
| 3278 | com_error(c, PyExc_SyntaxError, "invalid syntax"); |
| 3279 | return; |
| 3280 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3281 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3282 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3283 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3284 | com_pop(c, 1); |
| 3285 | } |
| 3286 | |
| 3287 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3288 | com_import_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3289 | { |
| 3290 | int i; |
| 3291 | REQ(n, import_stmt); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3292 | /* 'import' dotted_name (',' dotted_name)* | |
| 3293 | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*) */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3294 | if (STR(CHILD(n, 0))[0] == 'f') { |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3295 | PyObject *tup; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3296 | /* 'from' dotted_name 'import' ... */ |
| 3297 | REQ(CHILD(n, 1), dotted_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3298 | |
| 3299 | if (TYPE(CHILD(n, 3)) == STAR) { |
| 3300 | tup = Py_BuildValue("(s)", "*"); |
| 3301 | } else { |
| 3302 | tup = PyTuple_New((NCH(n) - 2)/2); |
| 3303 | for (i = 3; i < NCH(n); i += 2) { |
| 3304 | PyTuple_SET_ITEM(tup, (i-3)/2, |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 3305 | PyString_FromString(STR( |
| 3306 | CHILD(CHILD(n, i), 0)))); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3307 | } |
| 3308 | } |
| 3309 | com_addoparg(c, LOAD_CONST, com_addconst(c, tup)); |
Guido van Rossum | 83fb073 | 2000-11-27 22:22:36 +0000 | [diff] [blame] | 3310 | Py_DECREF(tup); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3311 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3312 | com_addopname(c, IMPORT_NAME, CHILD(n, 1)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3313 | if (TYPE(CHILD(n, 3)) == STAR) |
| 3314 | com_addbyte(c, IMPORT_STAR); |
| 3315 | else { |
| 3316 | for (i = 3; i < NCH(n); i += 2) |
| 3317 | com_from_import(c, CHILD(n, i)); |
| 3318 | com_addbyte(c, POP_TOP); |
| 3319 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3320 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3321 | } |
| 3322 | else { |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3323 | /* 'import' ... */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3324 | for (i = 1; i < NCH(n); i += 2) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3325 | node *subn = CHILD(n, i); |
| 3326 | REQ(subn, dotted_as_name); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3327 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3328 | com_push(c, 1); |
Thomas Wouters | dd13e4f | 2000-08-27 20:31:27 +0000 | [diff] [blame] | 3329 | com_addopname(c, IMPORT_NAME, CHILD(subn, 0)); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3330 | if (NCH(subn) > 1) { |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3331 | int j; |
| 3332 | if (strcmp(STR(CHILD(subn, 1)), "as") != 0) { |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3333 | com_error(c, PyExc_SyntaxError, |
| 3334 | "invalid syntax"); |
| 3335 | return; |
| 3336 | } |
Thomas Wouters | e753ef8 | 2000-08-27 20:16:32 +0000 | [diff] [blame] | 3337 | for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2) |
| 3338 | com_addopname(c, LOAD_ATTR, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3339 | CHILD(CHILD(subn, 0), |
| 3340 | j)); |
| 3341 | com_addop_varname(c, VAR_STORE, |
| 3342 | STR(CHILD(subn, 2))); |
Thomas Wouters | 5215225 | 2000-08-17 22:55:00 +0000 | [diff] [blame] | 3343 | } else |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3344 | com_addop_varname(c, VAR_STORE, |
| 3345 | STR(CHILD(CHILD(subn, 0), |
| 3346 | 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3347 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3353 | com_exec_stmt(struct compiling *c, node *n) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3354 | { |
| 3355 | REQ(n, exec_stmt); |
| 3356 | /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */ |
| 3357 | com_node(c, CHILD(n, 1)); |
| 3358 | if (NCH(n) >= 4) |
| 3359 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3360 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3361 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3362 | com_push(c, 1); |
| 3363 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3364 | if (NCH(n) >= 6) |
| 3365 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3366 | else { |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3367 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3368 | com_push(c, 1); |
| 3369 | } |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3370 | com_addbyte(c, EXEC_STMT); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3371 | com_pop(c, 3); |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3374 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3375 | is_constant_false(struct compiling *c, node *n) |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3376 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3377 | PyObject *v; |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3378 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3379 | /* argument c will be NULL when called from symtable_node() */ |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3380 | |
| 3381 | /* Label to avoid tail recursion */ |
| 3382 | next: |
| 3383 | switch (TYPE(n)) { |
| 3384 | |
| 3385 | case suite: |
| 3386 | if (NCH(n) == 1) { |
| 3387 | n = CHILD(n, 0); |
| 3388 | goto next; |
| 3389 | } |
| 3390 | /* Fall through */ |
| 3391 | case file_input: |
| 3392 | for (i = 0; i < NCH(n); i++) { |
| 3393 | node *ch = CHILD(n, i); |
| 3394 | if (TYPE(ch) == stmt) { |
| 3395 | n = ch; |
| 3396 | goto next; |
| 3397 | } |
| 3398 | } |
| 3399 | break; |
| 3400 | |
| 3401 | case stmt: |
| 3402 | case simple_stmt: |
| 3403 | case small_stmt: |
| 3404 | n = CHILD(n, 0); |
| 3405 | goto next; |
| 3406 | |
| 3407 | case expr_stmt: |
| 3408 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3409 | case testlist1: |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3410 | case test: |
| 3411 | case and_test: |
| 3412 | case not_test: |
| 3413 | case comparison: |
| 3414 | case expr: |
| 3415 | case xor_expr: |
| 3416 | case and_expr: |
| 3417 | case shift_expr: |
| 3418 | case arith_expr: |
| 3419 | case term: |
| 3420 | case factor: |
| 3421 | case power: |
| 3422 | case atom: |
| 3423 | if (NCH(n) == 1) { |
| 3424 | n = CHILD(n, 0); |
| 3425 | goto next; |
| 3426 | } |
| 3427 | break; |
| 3428 | |
| 3429 | case NAME: |
| 3430 | if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0) |
| 3431 | return 1; |
| 3432 | break; |
| 3433 | |
| 3434 | case NUMBER: |
| 3435 | v = parsenumber(c, STR(n)); |
| 3436 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3437 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3440 | i = PyObject_IsTrue(v); |
| 3441 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3442 | return i == 0; |
| 3443 | |
| 3444 | case STRING: |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3445 | v = parsestr(c, STR(n)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3446 | if (v == NULL) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3447 | PyErr_Clear(); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3448 | break; |
| 3449 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3450 | i = PyObject_IsTrue(v); |
| 3451 | Py_DECREF(v); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3452 | return i == 0; |
| 3453 | |
| 3454 | } |
| 3455 | return 0; |
| 3456 | } |
| 3457 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3458 | |
| 3459 | /* Look under n for a return stmt with an expression. |
| 3460 | * This hack is used to find illegal returns under "if 0:" blocks in |
| 3461 | * functions already known to be generators (as determined by the symtable |
| 3462 | * pass). |
| 3463 | * Return the offending return node if found, else NULL. |
| 3464 | */ |
| 3465 | static node * |
| 3466 | look_for_offending_return(node *n) |
| 3467 | { |
| 3468 | int i; |
| 3469 | |
| 3470 | for (i = 0; i < NCH(n); ++i) { |
| 3471 | node *kid = CHILD(n, i); |
| 3472 | |
| 3473 | switch (TYPE(kid)) { |
| 3474 | case classdef: |
| 3475 | case funcdef: |
| 3476 | case lambdef: |
| 3477 | /* Stuff in nested functions & classes doesn't |
| 3478 | affect the code block we started in. */ |
| 3479 | return NULL; |
| 3480 | |
| 3481 | case return_stmt: |
| 3482 | if (NCH(kid) > 1) |
| 3483 | return kid; |
| 3484 | break; |
| 3485 | |
| 3486 | default: { |
| 3487 | node *bad = look_for_offending_return(kid); |
| 3488 | if (bad != NULL) |
| 3489 | return bad; |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
| 3494 | return NULL; |
| 3495 | } |
| 3496 | |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 3497 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3498 | com_if_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3499 | { |
| 3500 | int i; |
| 3501 | int anchor = 0; |
| 3502 | REQ(n, if_stmt); |
| 3503 | /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */ |
| 3504 | for (i = 0; i+3 < NCH(n); i+=4) { |
| 3505 | int a = 0; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3506 | node *ch = CHILD(n, i+1); |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3507 | if (is_constant_false(c, ch)) { |
| 3508 | /* We're going to skip this block. However, if this |
| 3509 | is a generator, we have to check the dead code |
| 3510 | anyway to make sure there aren't any return stmts |
| 3511 | with expressions, in the same scope. */ |
| 3512 | if (c->c_flags & CO_GENERATOR) { |
| 3513 | node *p = look_for_offending_return(n); |
| 3514 | if (p != NULL) { |
| 3515 | int savelineno = c->c_lineno; |
| 3516 | c->c_lineno = p->n_lineno; |
| 3517 | com_error(c, PyExc_SyntaxError, |
| 3518 | "'return' with argument " |
| 3519 | "inside generator"); |
| 3520 | c->c_lineno = savelineno; |
| 3521 | } |
| 3522 | } |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3523 | continue; |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 3524 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3525 | if (i > 0) |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3526 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3527 | com_node(c, ch); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3528 | com_addfwref(c, JUMP_IF_FALSE, &a); |
| 3529 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3530 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3531 | com_node(c, CHILD(n, i+3)); |
| 3532 | com_addfwref(c, JUMP_FORWARD, &anchor); |
| 3533 | com_backpatch(c, a); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3534 | /* We jump here with an extra entry which we now pop */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3535 | com_addbyte(c, POP_TOP); |
| 3536 | } |
| 3537 | if (i+2 < NCH(n)) |
| 3538 | com_node(c, CHILD(n, i+2)); |
Guido van Rossum | 7c53111 | 1997-03-11 18:42:21 +0000 | [diff] [blame] | 3539 | if (anchor) |
| 3540 | com_backpatch(c, anchor); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3544 | com_while_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3545 | { |
| 3546 | int break_anchor = 0; |
| 3547 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3548 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3549 | REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */ |
| 3550 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3551 | block_push(c, SETUP_LOOP); |
| 3552 | c->c_begin = c->c_nexti; |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3553 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3554 | com_node(c, CHILD(n, 1)); |
| 3555 | com_addfwref(c, JUMP_IF_FALSE, &anchor); |
| 3556 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3557 | com_pop(c, 1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3558 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3559 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3560 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3561 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3562 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3563 | com_backpatch(c, anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3564 | /* We jump here with one entry more on the stack */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3565 | com_addbyte(c, POP_TOP); |
| 3566 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3567 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3568 | if (NCH(n) > 4) |
| 3569 | com_node(c, CHILD(n, 6)); |
| 3570 | com_backpatch(c, break_anchor); |
| 3571 | } |
| 3572 | |
| 3573 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3574 | com_for_stmt(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3575 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3576 | int break_anchor = 0; |
| 3577 | int anchor = 0; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3578 | int save_begin = c->c_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3579 | REQ(n, for_stmt); |
| 3580 | /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */ |
| 3581 | com_addfwref(c, SETUP_LOOP, &break_anchor); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3582 | block_push(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3583 | com_node(c, CHILD(n, 3)); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3584 | com_addbyte(c, GET_ITER); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3585 | c->c_begin = c->c_nexti; |
Michael W. Hudson | 26848a3 | 2003-04-29 17:07:36 +0000 | [diff] [blame] | 3586 | com_set_lineno(c, c->c_last_line); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3587 | com_addfwref(c, FOR_ITER, &anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3588 | com_push(c, 1); |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3589 | com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3590 | c->c_loops++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3591 | com_node(c, CHILD(n, 5)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3592 | c->c_loops--; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3593 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3594 | c->c_begin = save_begin; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3595 | com_backpatch(c, anchor); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3596 | com_pop(c, 1); /* FOR_ITER has popped this */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3597 | com_addbyte(c, POP_BLOCK); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3598 | block_pop(c, SETUP_LOOP); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3599 | if (NCH(n) > 8) |
| 3600 | com_node(c, CHILD(n, 8)); |
| 3601 | com_backpatch(c, break_anchor); |
| 3602 | } |
| 3603 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3604 | /* Code generated for "try: S finally: Sf" is as follows: |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3605 | |
| 3606 | SETUP_FINALLY L |
| 3607 | <code for S> |
| 3608 | POP_BLOCK |
| 3609 | LOAD_CONST <nil> |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3610 | L: <code for Sf> |
| 3611 | END_FINALLY |
| 3612 | |
| 3613 | The special instructions use the block stack. Each block |
| 3614 | stack entry contains the instruction that created it (here |
| 3615 | SETUP_FINALLY), the level of the value stack at the time the |
| 3616 | block stack entry was created, and a label (here L). |
| 3617 | |
| 3618 | SETUP_FINALLY: |
| 3619 | Pushes the current value stack level and the label |
| 3620 | onto the block stack. |
| 3621 | POP_BLOCK: |
| 3622 | Pops en entry from the block stack, and pops the value |
| 3623 | stack until its level is the same as indicated on the |
| 3624 | block stack. (The label is ignored.) |
| 3625 | END_FINALLY: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3626 | Pops a variable number of entries from the *value* stack |
| 3627 | and re-raises the exception they specify. The number of |
| 3628 | entries popped depends on the (pseudo) exception type. |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3629 | |
| 3630 | The block stack is unwound when an exception is raised: |
| 3631 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 3632 | onto the value stack (and the exception condition is cleared), |
| 3633 | and the interpreter jumps to the label gotten from the block |
| 3634 | stack. |
| 3635 | |
| 3636 | 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] | 3637 | (The contents of the value stack is shown in [], with the top |
| 3638 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3639 | associated value, and 'exc' the exception.) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3640 | |
| 3641 | Value stack Label Instruction Argument |
| 3642 | [] SETUP_EXCEPT L1 |
| 3643 | [] <code for S> |
| 3644 | [] POP_BLOCK |
| 3645 | [] JUMP_FORWARD L0 |
| 3646 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3647 | [tb, val, exc] L1: DUP ) |
| 3648 | [tb, val, exc, exc] <evaluate E1> ) |
| 3649 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 3650 | [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 ) |
| 3651 | [tb, val, exc, 1] POP ) |
| 3652 | [tb, val, exc] POP |
| 3653 | [tb, val] <assign to V1> (or POP if no V1) |
| 3654 | [tb] POP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3655 | [] <code for S1> |
| 3656 | JUMP_FORWARD L0 |
| 3657 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3658 | [tb, val, exc, 0] L2: POP |
| 3659 | [tb, val, exc] DUP |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3660 | .............................etc....................... |
| 3661 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3662 | [tb, val, exc, 0] Ln+1: POP |
| 3663 | [tb, val, exc] END_FINALLY # re-raise exception |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 3664 | |
| 3665 | [] L0: <next statement> |
| 3666 | |
| 3667 | Of course, parts are not generated if Vi or Ei is not present. |
| 3668 | */ |
| 3669 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3670 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3671 | com_try_except(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3672 | { |
| 3673 | int except_anchor = 0; |
| 3674 | int end_anchor = 0; |
| 3675 | int else_anchor = 0; |
| 3676 | int i; |
| 3677 | node *ch; |
| 3678 | |
| 3679 | com_addfwref(c, SETUP_EXCEPT, &except_anchor); |
| 3680 | block_push(c, SETUP_EXCEPT); |
| 3681 | com_node(c, CHILD(n, 2)); |
| 3682 | com_addbyte(c, POP_BLOCK); |
| 3683 | block_pop(c, SETUP_EXCEPT); |
| 3684 | com_addfwref(c, JUMP_FORWARD, &else_anchor); |
| 3685 | com_backpatch(c, except_anchor); |
| 3686 | for (i = 3; |
| 3687 | i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause; |
| 3688 | i += 3) { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3689 | /* except_clause: 'except' [expr [',' var]] */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3690 | if (except_anchor == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3691 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 3692 | "default 'except:' must be last"); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3693 | break; |
| 3694 | } |
| 3695 | except_anchor = 0; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3696 | com_push(c, 3); /* tb, val, exc pushed by exception */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3697 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3698 | if (NCH(ch) > 1) { |
| 3699 | com_addbyte(c, DUP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3700 | com_push(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3701 | com_node(c, CHILD(ch, 1)); |
Martin v. Löwis | 7198a52 | 2002-01-01 19:59:11 +0000 | [diff] [blame] | 3702 | com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3703 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3704 | com_addfwref(c, JUMP_IF_FALSE, &except_anchor); |
| 3705 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3706 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3707 | } |
| 3708 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3709 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3710 | if (NCH(ch) > 3) |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 3711 | com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3712 | else { |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3713 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3714 | com_pop(c, 1); |
| 3715 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3716 | com_addbyte(c, POP_TOP); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3717 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3718 | com_node(c, CHILD(n, i+2)); |
| 3719 | com_addfwref(c, JUMP_FORWARD, &end_anchor); |
| 3720 | if (except_anchor) { |
| 3721 | com_backpatch(c, except_anchor); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3722 | /* We come in with [tb, val, exc, 0] on the |
| 3723 | stack; one pop and it's the same as |
| 3724 | expected at the start of the loop */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3725 | com_addbyte(c, POP_TOP); |
| 3726 | } |
| 3727 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3728 | /* We actually come in here with [tb, val, exc] but the |
| 3729 | END_FINALLY will zap those and jump around. |
| 3730 | The c_stacklevel does not reflect them so we need not pop |
| 3731 | anything. */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3732 | com_addbyte(c, END_FINALLY); |
| 3733 | com_backpatch(c, else_anchor); |
| 3734 | if (i < NCH(n)) |
| 3735 | com_node(c, CHILD(n, i+2)); |
| 3736 | com_backpatch(c, end_anchor); |
| 3737 | } |
| 3738 | |
| 3739 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3740 | com_try_finally(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3741 | { |
| 3742 | int finally_anchor = 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3743 | node *ch; |
Guido van Rossum | 94fb82e | 1992-04-05 14:24:50 +0000 | [diff] [blame] | 3744 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3745 | com_addfwref(c, SETUP_FINALLY, &finally_anchor); |
| 3746 | block_push(c, SETUP_FINALLY); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3747 | com_node(c, CHILD(n, 2)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3748 | com_addbyte(c, POP_BLOCK); |
| 3749 | block_pop(c, SETUP_FINALLY); |
| 3750 | block_push(c, END_FINALLY); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3751 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3752 | /* While the generated code pushes only one item, |
| 3753 | the try-finally handling can enter here with |
| 3754 | up to three items. OK, here are the details: |
| 3755 | 3 for an exception, 2 for RETURN, 1 for BREAK. */ |
| 3756 | com_push(c, 3); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3757 | com_backpatch(c, finally_anchor); |
| 3758 | ch = CHILD(n, NCH(n)-1); |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 3759 | com_set_lineno(c, ch->n_lineno); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3760 | com_node(c, ch); |
| 3761 | com_addbyte(c, END_FINALLY); |
| 3762 | block_pop(c, END_FINALLY); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3763 | com_pop(c, 3); /* Matches the com_push above */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3767 | com_try_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3768 | { |
| 3769 | REQ(n, try_stmt); |
| 3770 | /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 3771 | | 'try' ':' suite 'finally' ':' suite */ |
| 3772 | if (TYPE(CHILD(n, 3)) != except_clause) |
| 3773 | com_try_finally(c, n); |
| 3774 | else |
| 3775 | com_try_except(c, n); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3778 | static node * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3779 | get_rawdocstring(node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3780 | { |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3781 | int i; |
| 3782 | |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3783 | /* Label to avoid tail recursion */ |
| 3784 | next: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3785 | switch (TYPE(n)) { |
| 3786 | |
| 3787 | case suite: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3788 | if (NCH(n) == 1) { |
| 3789 | n = CHILD(n, 0); |
| 3790 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3791 | } |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3792 | /* Fall through */ |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3793 | case file_input: |
| 3794 | for (i = 0; i < NCH(n); i++) { |
| 3795 | node *ch = CHILD(n, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3796 | if (TYPE(ch) == stmt) { |
| 3797 | n = ch; |
| 3798 | goto next; |
| 3799 | } |
Guido van Rossum | 164d4ff | 1995-01-26 00:40:09 +0000 | [diff] [blame] | 3800 | } |
| 3801 | break; |
| 3802 | |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3803 | case stmt: |
| 3804 | case simple_stmt: |
| 3805 | case small_stmt: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3806 | n = CHILD(n, 0); |
| 3807 | goto next; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3808 | |
| 3809 | case expr_stmt: |
| 3810 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 3811 | case testlist1: |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3812 | case test: |
| 3813 | case and_test: |
| 3814 | case not_test: |
| 3815 | case comparison: |
| 3816 | case expr: |
| 3817 | case xor_expr: |
| 3818 | case and_expr: |
| 3819 | case shift_expr: |
| 3820 | case arith_expr: |
| 3821 | case term: |
| 3822 | case factor: |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 3823 | case power: |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3824 | if (NCH(n) == 1) { |
| 3825 | n = CHILD(n, 0); |
| 3826 | goto next; |
| 3827 | } |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3828 | break; |
| 3829 | |
| 3830 | case atom: |
| 3831 | if (TYPE(CHILD(n, 0)) == STRING) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3832 | return n; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 3833 | break; |
| 3834 | |
| 3835 | } |
| 3836 | return NULL; |
| 3837 | } |
| 3838 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3839 | static PyObject * |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3840 | get_docstring(struct compiling *c, node *n) |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3841 | { |
Guido van Rossum | 541563e | 1999-01-28 15:08:09 +0000 | [diff] [blame] | 3842 | /* Don't generate doc-strings if run with -OO */ |
| 3843 | if (Py_OptimizeFlag > 1) |
| 3844 | return NULL; |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3845 | n = get_rawdocstring(n); |
| 3846 | if (n == NULL) |
| 3847 | return NULL; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3848 | return parsestrplus(c, n); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3849 | } |
| 3850 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3851 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3852 | com_suite(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3853 | { |
| 3854 | REQ(n, suite); |
| 3855 | /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */ |
| 3856 | if (NCH(n) == 1) { |
| 3857 | com_node(c, CHILD(n, 0)); |
| 3858 | } |
| 3859 | else { |
| 3860 | int i; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3861 | for (i = 0; i < NCH(n) && c->c_errors == 0; i++) { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3862 | node *ch = CHILD(n, i); |
| 3863 | if (TYPE(ch) == stmt) |
| 3864 | com_node(c, ch); |
| 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 3869 | /* ARGSUSED */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3870 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3871 | com_continue_stmt(struct compiling *c, node *n) |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3872 | { |
| 3873 | int i = c->c_nblocks; |
| 3874 | if (i-- > 0 && c->c_block[i] == SETUP_LOOP) { |
| 3875 | com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); |
| 3876 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3877 | else if (i <= 0) { |
| 3878 | /* at the outer level */ |
| 3879 | com_error(c, PyExc_SyntaxError, |
| 3880 | "'continue' not properly in loop"); |
| 3881 | } |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3882 | else { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3883 | int j; |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3884 | for (j = i-1; j >= 0; --j) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3885 | if (c->c_block[j] == SETUP_LOOP) |
| 3886 | break; |
| 3887 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3888 | if (j >= 0) { |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3889 | /* there is a loop, but something interferes */ |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3890 | for (; i > j; --i) { |
| 3891 | if (c->c_block[i] == SETUP_EXCEPT || |
| 3892 | c->c_block[i] == SETUP_FINALLY) { |
| 3893 | com_addoparg(c, CONTINUE_LOOP, |
| 3894 | c->c_begin); |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3895 | return; |
| 3896 | } |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 3897 | if (c->c_block[i] == END_FINALLY) { |
| 3898 | com_error(c, PyExc_SyntaxError, |
| 3899 | "'continue' not supported inside 'finally' clause"); |
| 3900 | return; |
| 3901 | } |
Fred Drake | fd1f1be | 2000-09-08 16:31:24 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3904 | com_error(c, PyExc_SyntaxError, |
| 3905 | "'continue' not properly in loop"); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3906 | } |
| 3907 | /* XXX Could allow it inside a 'finally' clause |
| 3908 | XXX if we could pop the exception still on the stack */ |
| 3909 | } |
| 3910 | |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3911 | /* Return the number of default values in the argument list. |
| 3912 | |
| 3913 | If a non-default argument follows a default argument, set an |
| 3914 | exception and return -1. |
| 3915 | */ |
| 3916 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3917 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3918 | com_argdefs(struct compiling *c, node *n) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3919 | { |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3920 | int i, nch, ndefs; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3921 | if (TYPE(n) == lambdef) { |
| 3922 | /* lambdef: 'lambda' [varargslist] ':' test */ |
| 3923 | n = CHILD(n, 1); |
| 3924 | } |
| 3925 | else { |
| 3926 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ... */ |
| 3927 | n = CHILD(n, 2); |
| 3928 | REQ(n, parameters); /* parameters: '(' [varargslist] ')' */ |
| 3929 | n = CHILD(n, 1); |
| 3930 | } |
| 3931 | if (TYPE(n) != varargslist) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3932 | return 0; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3933 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 3934 | (fpdef ['=' test] ',')* '*' ....... | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3935 | fpdef ['=' test] (',' fpdef ['=' test])* [','] */ |
| 3936 | nch = NCH(n); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3937 | ndefs = 0; |
| 3938 | for (i = 0; i < nch; i++) { |
| 3939 | int t; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 3940 | if (TYPE(CHILD(n, i)) == STAR || |
| 3941 | TYPE(CHILD(n, i)) == DOUBLESTAR) |
Guido van Rossum | f10570b | 1995-07-07 22:53:21 +0000 | [diff] [blame] | 3942 | break; |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3943 | i++; |
| 3944 | if (i >= nch) |
Guido van Rossum | a1e7e62 | 1995-09-18 21:44:04 +0000 | [diff] [blame] | 3945 | t = RPAR; /* Anything except EQUAL or COMMA */ |
| 3946 | else |
| 3947 | t = TYPE(CHILD(n, i)); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3948 | if (t == EQUAL) { |
| 3949 | i++; |
| 3950 | ndefs++; |
| 3951 | com_node(c, CHILD(n, i)); |
| 3952 | i++; |
| 3953 | if (i >= nch) |
| 3954 | break; |
| 3955 | t = TYPE(CHILD(n, i)); |
| 3956 | } |
| 3957 | else { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3958 | /* Treat "(a=1, b)" as an error */ |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3959 | if (ndefs) { |
Guido van Rossum | 29d38cd | 1998-10-02 13:41:54 +0000 | [diff] [blame] | 3960 | com_error(c, PyExc_SyntaxError, |
Guido van Rossum | dfede31 | 1998-10-02 14:06:56 +0000 | [diff] [blame] | 3961 | "non-default argument follows default argument"); |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3962 | return -1; |
| 3963 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3964 | } |
| 3965 | if (t != COMMA) |
| 3966 | break; |
| 3967 | } |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 3968 | return ndefs; |
| 3969 | } |
| 3970 | |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3971 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 3972 | com_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3973 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3974 | PyObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3975 | int ndefs; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3976 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3977 | ndefs = com_argdefs(c, n); |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 3978 | if (ndefs < 0) |
| 3979 | return; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 3980 | symtable_enter_scope(c->c_symtable, STR(CHILD(n, 1)), TYPE(n), |
| 3981 | n->n_lineno); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3982 | co = (PyObject *)icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3983 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3984 | if (co == NULL) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3985 | c->c_errors++; |
| 3986 | else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3987 | int closure = com_make_closure(c, (PyCodeObject *)co); |
| 3988 | int i = com_addconst(c, co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3989 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3990 | com_push(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3991 | if (closure) |
| 3992 | com_addoparg(c, MAKE_CLOSURE, ndefs); |
| 3993 | else |
| 3994 | com_addoparg(c, MAKE_FUNCTION, ndefs); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3995 | com_pop(c, ndefs); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 3996 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 3997 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3998 | Py_DECREF(co); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 3999 | } |
| 4000 | } |
| 4001 | |
| 4002 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4003 | com_bases(struct compiling *c, node *n) |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4004 | { |
Guido van Rossum | f1aeab7 | 1992-03-27 17:28:26 +0000 | [diff] [blame] | 4005 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4006 | REQ(n, testlist); |
| 4007 | /* testlist: test (',' test)* [','] */ |
| 4008 | for (i = 0; i < NCH(n); i += 2) |
| 4009 | com_node(c, CHILD(n, i)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4010 | i = (NCH(n)+1) / 2; |
| 4011 | com_addoparg(c, BUILD_TUPLE, i); |
| 4012 | com_pop(c, i-1); |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4016 | com_classdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4017 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4018 | int i; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4019 | PyObject *v; |
| 4020 | PyCodeObject *co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4021 | char *name; |
| 4022 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4023 | REQ(n, classdef); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4024 | /* classdef: class NAME ['(' testlist ')'] ':' suite */ |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4025 | if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4026 | c->c_errors++; |
| 4027 | return; |
| 4028 | } |
| 4029 | /* Push the class name on the stack */ |
| 4030 | i = com_addconst(c, v); |
| 4031 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4032 | com_push(c, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4033 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4034 | /* Push the tuple of base classes on the stack */ |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4035 | if (TYPE(CHILD(n, 2)) != LPAR) { |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4036 | com_addoparg(c, BUILD_TUPLE, 0); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4037 | com_push(c, 1); |
| 4038 | } |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 4039 | else |
| 4040 | com_bases(c, CHILD(n, 3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4041 | name = STR(CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4042 | symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4043 | co = icompile(n, c); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4044 | symtable_exit_scope(c->c_symtable); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4045 | if (co == NULL) |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4046 | c->c_errors++; |
| 4047 | else { |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4048 | int closure = com_make_closure(c, co); |
| 4049 | i = com_addconst(c, (PyObject *)co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4050 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4051 | com_push(c, 1); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4052 | if (closure) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4053 | com_addoparg(c, MAKE_CLOSURE, 0); |
Jeremy Hylton | 733c893 | 2001-12-13 19:51:56 +0000 | [diff] [blame] | 4054 | com_pop(c, PyCode_GetNumFree(co)); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4055 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4056 | com_addoparg(c, MAKE_FUNCTION, 0); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4057 | com_addoparg(c, CALL_FUNCTION, 0); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4058 | com_addbyte(c, BUILD_CLASS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4059 | com_pop(c, 2); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4060 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1))); |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4061 | com_pop(c, 1); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4062 | Py_DECREF(co); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4063 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4067 | com_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4068 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4069 | loop: |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4070 | if (c->c_errors) |
| 4071 | return; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4072 | switch (TYPE(n)) { |
| 4073 | |
| 4074 | /* Definition nodes */ |
| 4075 | |
| 4076 | case funcdef: |
| 4077 | com_funcdef(c, n); |
| 4078 | break; |
| 4079 | case classdef: |
| 4080 | com_classdef(c, n); |
| 4081 | break; |
| 4082 | |
| 4083 | /* Trivial parse tree nodes */ |
| 4084 | |
| 4085 | case stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 4086 | case small_stmt: |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4087 | case flow_stmt: |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4088 | n = CHILD(n, 0); |
| 4089 | goto loop; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4090 | |
| 4091 | case simple_stmt: |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 4092 | /* small_stmt (';' small_stmt)* [';'] NEWLINE */ |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4093 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 4094 | { |
| 4095 | int i; |
| 4096 | for (i = 0; i < NCH(n)-1; i += 2) |
| 4097 | com_node(c, CHILD(n, i)); |
| 4098 | } |
| 4099 | break; |
| 4100 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4101 | case compound_stmt: |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4102 | com_set_lineno(c, n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4103 | n = CHILD(n, 0); |
| 4104 | goto loop; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4105 | |
| 4106 | /* Statement nodes */ |
| 4107 | |
| 4108 | case expr_stmt: |
| 4109 | com_expr_stmt(c, n); |
| 4110 | break; |
| 4111 | case print_stmt: |
| 4112 | com_print_stmt(c, n); |
| 4113 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 4114 | case del_stmt: /* 'del' exprlist */ |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 4115 | com_assign(c, CHILD(n, 1), OP_DELETE, NULL); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4116 | break; |
| 4117 | case pass_stmt: |
| 4118 | break; |
| 4119 | case break_stmt: |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4120 | if (c->c_loops == 0) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4121 | com_error(c, PyExc_SyntaxError, |
| 4122 | "'break' outside loop"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4123 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4124 | com_addbyte(c, BREAK_LOOP); |
| 4125 | break; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 4126 | case continue_stmt: |
| 4127 | com_continue_stmt(c, n); |
| 4128 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4129 | case return_stmt: |
| 4130 | com_return_stmt(c, n); |
| 4131 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 4132 | case yield_stmt: |
| 4133 | com_yield_stmt(c, n); |
| 4134 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4135 | case raise_stmt: |
| 4136 | com_raise_stmt(c, n); |
| 4137 | break; |
| 4138 | case import_stmt: |
| 4139 | com_import_stmt(c, n); |
| 4140 | break; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4141 | case global_stmt: |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4142 | break; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 4143 | case exec_stmt: |
| 4144 | com_exec_stmt(c, n); |
| 4145 | break; |
Guido van Rossum | 228d7f3 | 1997-04-02 05:24:36 +0000 | [diff] [blame] | 4146 | case assert_stmt: |
| 4147 | com_assert_stmt(c, n); |
| 4148 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4149 | case if_stmt: |
| 4150 | com_if_stmt(c, n); |
| 4151 | break; |
| 4152 | case while_stmt: |
| 4153 | com_while_stmt(c, n); |
| 4154 | break; |
| 4155 | case for_stmt: |
| 4156 | com_for_stmt(c, n); |
| 4157 | break; |
| 4158 | case try_stmt: |
| 4159 | com_try_stmt(c, n); |
| 4160 | break; |
| 4161 | case suite: |
| 4162 | com_suite(c, n); |
| 4163 | break; |
| 4164 | |
| 4165 | /* Expression nodes */ |
| 4166 | |
| 4167 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 4168 | case testlist1: |
Guido van Rossum | 1c91707 | 2001-10-15 15:44:05 +0000 | [diff] [blame] | 4169 | case testlist_safe: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 4170 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4171 | break; |
| 4172 | case test: |
| 4173 | com_test(c, n); |
| 4174 | break; |
| 4175 | case and_test: |
| 4176 | com_and_test(c, n); |
| 4177 | break; |
| 4178 | case not_test: |
| 4179 | com_not_test(c, n); |
| 4180 | break; |
| 4181 | case comparison: |
| 4182 | com_comparison(c, n); |
| 4183 | break; |
| 4184 | case exprlist: |
Guido van Rossum | 288a60f | 1991-12-16 13:05:10 +0000 | [diff] [blame] | 4185 | com_list(c, n, 0); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4186 | break; |
| 4187 | case expr: |
| 4188 | com_expr(c, n); |
| 4189 | break; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 4190 | case xor_expr: |
| 4191 | com_xor_expr(c, n); |
| 4192 | break; |
| 4193 | case and_expr: |
| 4194 | com_and_expr(c, n); |
| 4195 | break; |
| 4196 | case shift_expr: |
| 4197 | com_shift_expr(c, n); |
| 4198 | break; |
| 4199 | case arith_expr: |
| 4200 | com_arith_expr(c, n); |
| 4201 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4202 | case term: |
| 4203 | com_term(c, n); |
| 4204 | break; |
| 4205 | case factor: |
| 4206 | com_factor(c, n); |
| 4207 | break; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 4208 | case power: |
| 4209 | com_power(c, n); |
| 4210 | break; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4211 | case atom: |
| 4212 | com_atom(c, n); |
| 4213 | break; |
| 4214 | |
| 4215 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4216 | com_error(c, PyExc_SystemError, |
| 4217 | "com_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4218 | } |
| 4219 | } |
| 4220 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 4221 | static void com_fplist(struct compiling *, node *); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4222 | |
| 4223 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4224 | com_fpdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4225 | { |
| 4226 | REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4227 | if (TYPE(CHILD(n, 0)) == LPAR) |
| 4228 | com_fplist(c, CHILD(n, 1)); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4229 | else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4230 | com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0))); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4231 | com_pop(c, 1); |
| 4232 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4236 | com_fplist(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4237 | { |
Guido van Rossum | a9df32a | 1991-12-31 13:13:35 +0000 | [diff] [blame] | 4238 | REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4239 | if (NCH(n) == 1) { |
| 4240 | com_fpdef(c, CHILD(n, 0)); |
| 4241 | } |
| 4242 | else { |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4243 | int i = (NCH(n)+1)/2; |
Thomas Wouters | 0be5aab | 2000-08-11 22:15:52 +0000 | [diff] [blame] | 4244 | com_addoparg(c, UNPACK_SEQUENCE, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4245 | com_push(c, i-1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4246 | for (i = 0; i < NCH(n); i += 2) |
| 4247 | com_fpdef(c, CHILD(n, i)); |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4252 | com_arglist(struct compiling *c, node *n) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4253 | { |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4254 | int nch, i, narg; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4255 | int complex = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4256 | char nbuf[30]; |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4257 | REQ(n, varargslist); |
Guido van Rossum | acbefef | 1992-01-19 16:33:51 +0000 | [diff] [blame] | 4258 | /* varargslist: |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4259 | (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */ |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4260 | nch = NCH(n); |
| 4261 | /* Enter all arguments in table of locals */ |
| 4262 | for (i = 0, narg = 0; i < nch; i++) { |
| 4263 | node *ch = CHILD(n, i); |
| 4264 | node *fp; |
| 4265 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4266 | break; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4267 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4268 | fp = CHILD(ch, 0); |
| 4269 | if (TYPE(fp) != NAME) { |
| 4270 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
| 4271 | complex = 1; |
Guido van Rossum | 9c8a086 | 2002-12-23 16:35:23 +0000 | [diff] [blame] | 4272 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4273 | narg++; |
| 4274 | /* all name updates handled by symtable */ |
| 4275 | if (++i >= nch) |
| 4276 | break; |
| 4277 | ch = CHILD(n, i); |
| 4278 | if (TYPE(ch) == EQUAL) |
| 4279 | i += 2; |
| 4280 | else |
| 4281 | REQ(ch, COMMA); |
| 4282 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4283 | if (complex) { |
| 4284 | /* Generate code for complex arguments only after |
| 4285 | having counted the simple arguments */ |
| 4286 | int ilocal = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4287 | for (i = 0; i < nch; i++) { |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4288 | node *ch = CHILD(n, i); |
| 4289 | node *fp; |
Guido van Rossum | 50564e8 | 1996-01-12 01:13:16 +0000 | [diff] [blame] | 4290 | if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4291 | break; |
| 4292 | REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */ |
| 4293 | fp = CHILD(ch, 0); |
| 4294 | if (TYPE(fp) != NAME) { |
| 4295 | com_addoparg(c, LOAD_FAST, ilocal); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4296 | com_push(c, 1); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4297 | com_fpdef(c, ch); |
| 4298 | } |
| 4299 | ilocal++; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 4300 | if (++i >= nch) |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4301 | break; |
| 4302 | ch = CHILD(n, i); |
| 4303 | if (TYPE(ch) == EQUAL) |
| 4304 | i += 2; |
| 4305 | else |
| 4306 | REQ(ch, COMMA); |
| 4307 | } |
| 4308 | } |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4312 | com_file_input(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4313 | { |
| 4314 | int i; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4315 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4316 | REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4317 | doc = get_docstring(c, n); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4318 | if (doc != NULL) { |
| 4319 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4320 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4321 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4322 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4323 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4324 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4325 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4326 | for (i = 0; i < NCH(n); i++) { |
| 4327 | node *ch = CHILD(n, i); |
| 4328 | if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE) |
| 4329 | com_node(c, ch); |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | /* Top-level compile-node interface */ |
| 4334 | |
| 4335 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4336 | compile_funcdef(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4337 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4338 | PyObject *doc; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4339 | node *ch; |
| 4340 | REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4341 | c->c_name = STR(CHILD(n, 1)); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4342 | doc = get_docstring(c, CHILD(n, 4)); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4343 | if (doc != NULL) { |
| 4344 | (void) com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4345 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4346 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4347 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4348 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4349 | ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ |
| 4350 | ch = CHILD(ch, 1); /* ')' | varargslist */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4351 | if (TYPE(ch) == varargslist) |
Guido van Rossum | 49d6dc4 | 1992-01-14 18:30:26 +0000 | [diff] [blame] | 4352 | com_arglist(c, ch); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4353 | c->c_infunction = 1; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4354 | com_node(c, CHILD(n, 4)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4355 | c->c_infunction = 0; |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 4356 | com_strip_lnotab(c); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4357 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4358 | com_push(c, 1); |
| 4359 | com_addbyte(c, RETURN_VALUE); |
| 4360 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4364 | compile_lambdef(struct compiling *c, node *n) |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4365 | { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4366 | node *ch; |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4367 | REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */ |
Guido van Rossum | 363ac7d | 1994-11-10 22:40:34 +0000 | [diff] [blame] | 4368 | c->c_name = "<lambda>"; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4369 | |
| 4370 | ch = CHILD(n, 1); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4371 | (void) com_addconst(c, Py_None); /* No docstring */ |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4372 | if (TYPE(ch) == varargslist) { |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4373 | com_arglist(c, ch); |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4374 | ch = CHILD(n, 3); |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4375 | } |
Guido van Rossum | 681d79a | 1995-07-18 14:51:37 +0000 | [diff] [blame] | 4376 | else |
| 4377 | ch = CHILD(n, 2); |
| 4378 | com_node(c, ch); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4379 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4380 | com_pop(c, 1); |
Guido van Rossum | 12d12c5 | 1993-10-26 17:58:25 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4384 | compile_classdef(struct compiling *c, node *n) |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4385 | { |
| 4386 | node *ch; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4387 | PyObject *doc; |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4388 | REQ(n, classdef); |
| 4389 | /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 4390 | c->c_name = STR(CHILD(n, 1)); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4391 | c->c_private = c->c_name; |
Guido van Rossum | 340cbe7 | 2002-01-15 21:06:07 +0000 | [diff] [blame] | 4392 | /* Initialize local __module__ from global __name__ */ |
| 4393 | com_addop_name(c, LOAD_GLOBAL, "__name__"); |
| 4394 | com_addop_name(c, STORE_NAME, "__module__"); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4395 | ch = CHILD(n, NCH(n)-1); /* The suite */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4396 | doc = get_docstring(c, ch); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4397 | if (doc != NULL) { |
| 4398 | int i = com_addconst(c, doc); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4399 | Py_DECREF(doc); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4400 | com_addoparg(c, LOAD_CONST, i); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4401 | com_push(c, 1); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4402 | com_addop_name(c, STORE_NAME, "__doc__"); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4403 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4404 | } |
| 4405 | else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4406 | (void) com_addconst(c, Py_None); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4407 | com_node(c, ch); |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 4408 | com_strip_lnotab(c); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4409 | com_addbyte(c, LOAD_LOCALS); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4410 | com_push(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4411 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4412 | com_pop(c, 1); |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 4416 | compile_generator_expression(struct compiling *c, node *n) |
| 4417 | { |
| 4418 | /* testlist_gexp: test gen_for */ |
| 4419 | /* argument: test gen_for */ |
| 4420 | REQ(CHILD(n, 0), test); |
| 4421 | REQ(CHILD(n, 1), gen_for); |
| 4422 | |
| 4423 | c->c_name = "<generator expression>"; |
| 4424 | com_gen_for(c, CHILD(n, 1), CHILD(n, 0), 1); |
| 4425 | |
| 4426 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4427 | com_push(c, 1); |
| 4428 | com_addbyte(c, RETURN_VALUE); |
| 4429 | com_pop(c, 1); |
| 4430 | } |
| 4431 | |
| 4432 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4433 | compile_node(struct compiling *c, node *n) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4434 | { |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 4435 | com_set_lineno(c, n->n_lineno); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4436 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4437 | switch (TYPE(n)) { |
| 4438 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4439 | case single_input: /* One interactive command */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4440 | /* NEWLINE | simple_stmt | compound_stmt NEWLINE */ |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4441 | c->c_interactive++; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4442 | n = CHILD(n, 0); |
| 4443 | if (TYPE(n) != NEWLINE) |
| 4444 | com_node(c, n); |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 4445 | com_strip_lnotab(c); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4446 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4447 | com_push(c, 1); |
| 4448 | com_addbyte(c, RETURN_VALUE); |
| 4449 | com_pop(c, 1); |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4450 | c->c_interactive--; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4451 | break; |
| 4452 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4453 | case file_input: /* A whole file, or built-in function exec() */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4454 | com_file_input(c, n); |
Armin Rigo | 80d937e | 2004-03-22 17:52:53 +0000 | [diff] [blame] | 4455 | com_strip_lnotab(c); |
Michael W. Hudson | 53d58bb | 2002-08-30 13:09:51 +0000 | [diff] [blame] | 4456 | com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); |
| 4457 | com_push(c, 1); |
| 4458 | com_addbyte(c, RETURN_VALUE); |
| 4459 | com_pop(c, 1); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4460 | break; |
| 4461 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4462 | case eval_input: /* Built-in function input() */ |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4463 | com_node(c, CHILD(n, 0)); |
| 4464 | com_addbyte(c, RETURN_VALUE); |
Guido van Rossum | 8b993a9 | 1997-01-17 21:04:03 +0000 | [diff] [blame] | 4465 | com_pop(c, 1); |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4466 | break; |
| 4467 | |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4468 | case lambdef: /* anonymous function definition */ |
| 4469 | compile_lambdef(c, n); |
| 4470 | break; |
| 4471 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4472 | case funcdef: /* A function definition */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4473 | compile_funcdef(c, n); |
| 4474 | break; |
| 4475 | |
Guido van Rossum | 4c41778 | 1991-01-21 16:09:22 +0000 | [diff] [blame] | 4476 | case classdef: /* A class definition */ |
Guido van Rossum | 1f4fa50 | 1995-01-07 12:41:23 +0000 | [diff] [blame] | 4477 | compile_classdef(c, n); |
Guido van Rossum | d6f3bc2 | 1990-11-18 17:35:03 +0000 | [diff] [blame] | 4478 | break; |
| 4479 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 4480 | case testlist_gexp: /* A generator expression */ |
| 4481 | case argument: /* A generator expression */ |
| 4482 | compile_generator_expression(c, n); |
| 4483 | break; |
| 4484 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4485 | default: |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4486 | com_error(c, PyExc_SystemError, |
Guido van Rossum | 452a983 | 1996-09-17 14:32:04 +0000 | [diff] [blame] | 4487 | "compile_node: unexpected node type"); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4491 | static PyObject * |
| 4492 | dict_keys_inorder(PyObject *dict, int offset) |
| 4493 | { |
| 4494 | PyObject *tuple, *k, *v; |
| 4495 | int i, pos = 0, size = PyDict_Size(dict); |
| 4496 | |
| 4497 | tuple = PyTuple_New(size); |
| 4498 | if (tuple == NULL) |
| 4499 | return NULL; |
| 4500 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4501 | i = PyInt_AS_LONG(v); |
| 4502 | Py_INCREF(k); |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 4503 | assert((i - offset) < size); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4504 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4505 | } |
| 4506 | return tuple; |
| 4507 | } |
| 4508 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4509 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4510 | PyNode_Compile(node *n, const char *filename) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4511 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4512 | return PyNode_CompileFlags(n, filename, NULL); |
| 4513 | } |
| 4514 | |
| 4515 | PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4516 | PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags) |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4517 | { |
| 4518 | return jcompile(n, filename, NULL, flags); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4521 | struct symtable * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4522 | PyNode_CompileSymtable(node *n, const char *filename) |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4523 | { |
| 4524 | struct symtable *st; |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4525 | PyFutureFeatures *ff; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4526 | |
Jeremy Hylton | ded4bd7 | 2001-03-21 19:01:33 +0000 | [diff] [blame] | 4527 | ff = PyNode_Future(n, filename); |
| 4528 | if (ff == NULL) |
| 4529 | return NULL; |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4530 | st = symtable_build(n, ff, filename); |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4531 | if (st == NULL) { |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 4532 | PyObject_FREE((void *)ff); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4533 | return NULL; |
Tim Peters | 8c5e415 | 2001-11-04 19:26:58 +0000 | [diff] [blame] | 4534 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4535 | return st; |
| 4536 | } |
| 4537 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4538 | static PyCodeObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4539 | icompile(node *n, struct compiling *base) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4540 | { |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4541 | return jcompile(n, base->c_filename, base, NULL); |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4542 | } |
| 4543 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4544 | static PyCodeObject * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4545 | jcompile(node *n, const char *filename, struct compiling *base, |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4546 | PyCompilerFlags *flags) |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4547 | { |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4548 | struct compiling sc; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4549 | PyCodeObject *co; |
Guido van Rossum | 590baa4 | 1993-11-30 13:40:46 +0000 | [diff] [blame] | 4550 | if (!com_init(&sc, filename)) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4551 | return NULL; |
Just van Rossum | 3aaf42c | 2003-02-10 08:21:10 +0000 | [diff] [blame] | 4552 | if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { |
| 4553 | sc.c_encoding = "utf-8"; |
| 4554 | } else if (TYPE(n) == encoding_decl) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4555 | sc.c_encoding = STR(n); |
| 4556 | n = CHILD(n, 0); |
| 4557 | } else { |
| 4558 | sc.c_encoding = NULL; |
| 4559 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4560 | if (base) { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4561 | sc.c_private = base->c_private; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4562 | sc.c_symtable = base->c_symtable; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4563 | /* c_symtable still points to parent's symbols */ |
| 4564 | if (base->c_nested |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4565 | || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4566 | sc.c_nested = 1; |
Jeremy Hylton | 93a569d | 2001-10-17 13:22:22 +0000 | [diff] [blame] | 4567 | sc.c_flags |= base->c_flags & PyCF_MASK; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 4568 | if (base->c_encoding != NULL) { |
| 4569 | assert(sc.c_encoding == NULL); |
| 4570 | sc.c_encoding = base->c_encoding; |
| 4571 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4572 | } else { |
Guido van Rossum | 8ff077b | 1996-08-24 06:21:31 +0000 | [diff] [blame] | 4573 | sc.c_private = NULL; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 4574 | sc.c_future = PyNode_Future(n, filename); |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 4575 | if (sc.c_future == NULL) { |
| 4576 | com_free(&sc); |
| 4577 | return NULL; |
| 4578 | } |
Jeremy Hylton | 481081e | 2001-08-14 20:01:59 +0000 | [diff] [blame] | 4579 | if (flags) { |
| 4580 | int merged = sc.c_future->ff_features | |
| 4581 | flags->cf_flags; |
| 4582 | sc.c_future->ff_features = merged; |
| 4583 | flags->cf_flags = merged; |
| 4584 | } |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4585 | sc.c_symtable = symtable_build(n, sc.c_future, sc.c_filename); |
| 4586 | if (sc.c_symtable == NULL) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4587 | com_free(&sc); |
| 4588 | return NULL; |
| 4589 | } |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4590 | /* reset symbol table for second pass */ |
| 4591 | sc.c_symtable->st_nscopes = 1; |
| 4592 | sc.c_symtable->st_pass = 2; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4593 | } |
| 4594 | co = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4595 | if (symtable_load_symbols(&sc) < 0) { |
| 4596 | sc.c_errors++; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4597 | goto exit; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 4598 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4599 | compile_node(&sc, n); |
| 4600 | com_done(&sc); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 4601 | if (sc.c_errors == 0) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4602 | PyObject *consts, *names, *varnames, *filename, *name, |
| 4603 | *freevars, *cellvars; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4604 | consts = PyList_AsTuple(sc.c_consts); |
| 4605 | names = PyList_AsTuple(sc.c_names); |
| 4606 | varnames = PyList_AsTuple(sc.c_varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4607 | cellvars = dict_keys_inorder(sc.c_cellvars, 0); |
| 4608 | freevars = dict_keys_inorder(sc.c_freevars, |
| 4609 | PyTuple_GET_SIZE(cellvars)); |
Guido van Rossum | 7b89b6a | 1997-01-18 08:02:57 +0000 | [diff] [blame] | 4610 | filename = PyString_InternFromString(sc.c_filename); |
| 4611 | name = PyString_InternFromString(sc.c_name); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4612 | if (!PyErr_Occurred()) |
| 4613 | co = PyCode_New(sc.c_argcount, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4614 | sc.c_nlocals, |
| 4615 | sc.c_maxstacklevel, |
| 4616 | sc.c_flags, |
| 4617 | sc.c_code, |
| 4618 | consts, |
| 4619 | names, |
| 4620 | varnames, |
| 4621 | freevars, |
| 4622 | cellvars, |
| 4623 | filename, |
| 4624 | name, |
| 4625 | sc.c_firstlineno, |
| 4626 | sc.c_lnotab); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4627 | Py_XDECREF(consts); |
| 4628 | Py_XDECREF(names); |
| 4629 | Py_XDECREF(varnames); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4630 | Py_XDECREF(freevars); |
| 4631 | Py_XDECREF(cellvars); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4632 | Py_XDECREF(filename); |
| 4633 | Py_XDECREF(name); |
Guido van Rossum | a082ce4 | 1991-06-04 19:41:56 +0000 | [diff] [blame] | 4634 | } |
Guido van Rossum | 6c2f0c7 | 2000-08-07 19:22:43 +0000 | [diff] [blame] | 4635 | else if (!PyErr_Occurred()) { |
| 4636 | /* This could happen if someone called PyErr_Clear() after an |
| 4637 | error was reported above. That's not supposed to happen, |
| 4638 | but I just plugged one case and I'm not sure there can't be |
| 4639 | others. In that case, raise SystemError so that at least |
| 4640 | it gets reported instead dumping core. */ |
| 4641 | PyErr_SetString(PyExc_SystemError, "lost syntax error"); |
| 4642 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4643 | exit: |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4644 | if (base == NULL) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 4645 | PySymtable_Free(sc.c_symtable); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 4646 | sc.c_symtable = NULL; |
| 4647 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4648 | com_free(&sc); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4649 | return co; |
| 4650 | } |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4651 | |
| 4652 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 4653 | PyCode_Addr2Line(PyCodeObject *co, int addrq) |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4654 | { |
| 4655 | int size = PyString_Size(co->co_lnotab) / 2; |
Guido van Rossum | 2174dcb | 1999-09-15 22:48:09 +0000 | [diff] [blame] | 4656 | unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab); |
Guido van Rossum | da4eb5c | 1997-01-24 03:43:35 +0000 | [diff] [blame] | 4657 | int line = co->co_firstlineno; |
| 4658 | int addr = 0; |
| 4659 | while (--size >= 0) { |
| 4660 | addr += *p++; |
| 4661 | if (addr > addrq) |
| 4662 | break; |
| 4663 | line += *p++; |
| 4664 | } |
| 4665 | return line; |
| 4666 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4667 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4668 | /* The test for LOCAL must come before the test for FREE in order to |
| 4669 | handle classes where name is both local and free. The local var is |
| 4670 | a method and the free var is a free var referenced within a method. |
| 4671 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4672 | |
| 4673 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4674 | get_ref_type(struct compiling *c, char *name) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4675 | { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4676 | char buf[350]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4677 | PyObject *v; |
Jeremy Hylton | 5125773 | 2001-03-01 00:42:55 +0000 | [diff] [blame] | 4678 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4679 | if (PyDict_GetItemString(c->c_cellvars, name) != NULL) |
| 4680 | return CELL; |
| 4681 | if (PyDict_GetItemString(c->c_locals, name) != NULL) |
| 4682 | return LOCAL; |
| 4683 | if (PyDict_GetItemString(c->c_freevars, name) != NULL) |
| 4684 | return FREE; |
| 4685 | v = PyDict_GetItemString(c->c_globals, name); |
| 4686 | if (v) { |
| 4687 | if (v == Py_None) |
| 4688 | return GLOBAL_EXPLICIT; |
| 4689 | else { |
| 4690 | return GLOBAL_IMPLICIT; |
| 4691 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4692 | } |
Marc-André Lemburg | d4c0a9c | 2001-11-28 11:47:00 +0000 | [diff] [blame] | 4693 | PyOS_snprintf(buf, sizeof(buf), |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 4694 | "unknown scope for %.100s in %.100s(%s) " |
| 4695 | "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n", |
| 4696 | name, c->c_name, |
| 4697 | PyObject_REPR(c->c_symtable->st_cur->ste_id), |
| 4698 | c->c_filename, |
| 4699 | PyObject_REPR(c->c_symtable->st_cur->ste_symbols), |
| 4700 | PyObject_REPR(c->c_locals), |
| 4701 | PyObject_REPR(c->c_globals) |
| 4702 | ); |
| 4703 | |
| 4704 | Py_FatalError(buf); |
| 4705 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4708 | /* Helper functions to issue warnings */ |
| 4709 | |
| 4710 | static int |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 4711 | issue_warning(const char *msg, const char *filename, int lineno) |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4712 | { |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4713 | if (PyErr_Occurred()) { |
| 4714 | /* This can happen because symtable_node continues |
| 4715 | processing even after raising a SyntaxError. |
| 4716 | Calling PyErr_WarnExplicit now would clobber the |
| 4717 | pending exception; instead we fail and let that |
| 4718 | exception propagate. |
| 4719 | */ |
| 4720 | return -1; |
| 4721 | } |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4722 | if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename, |
| 4723 | lineno, NULL, NULL) < 0) { |
| 4724 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
| 4725 | PyErr_SetString(PyExc_SyntaxError, msg); |
| 4726 | PyErr_SyntaxLocation(filename, lineno); |
| 4727 | } |
| 4728 | return -1; |
| 4729 | } |
| 4730 | return 0; |
| 4731 | } |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4732 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4733 | static int |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4734 | symtable_warn(struct symtable *st, char *msg) |
| 4735 | { |
Guido van Rossum | 207fda6 | 2001-03-02 03:30:41 +0000 | [diff] [blame] | 4736 | 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] | 4737 | st->st_errors++; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4738 | return -1; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4739 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4740 | return 0; |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Jeremy Hylton | 9f1b993 | 2001-02-28 07:07:43 +0000 | [diff] [blame] | 4743 | /* Helper function for setting lineno and filename */ |
| 4744 | |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4745 | static struct symtable * |
| 4746 | symtable_build(node *n, PyFutureFeatures *ff, const char *filename) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4747 | { |
Jeremy Hylton | 1955fcf | 2003-07-15 20:23:26 +0000 | [diff] [blame] | 4748 | struct symtable *st; |
| 4749 | |
| 4750 | st = symtable_init(); |
| 4751 | if (st == NULL) |
| 4752 | return NULL; |
| 4753 | st->st_future = ff; |
| 4754 | st->st_filename = filename; |
| 4755 | symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno); |
| 4756 | if (st->st_errors > 0) |
| 4757 | goto fail; |
| 4758 | symtable_node(st, n); |
| 4759 | if (st->st_errors > 0) |
| 4760 | goto fail; |
| 4761 | return st; |
| 4762 | fail: |
| 4763 | if (!PyErr_Occurred()) { |
| 4764 | /* This could happen because after a syntax error is |
| 4765 | detected, the symbol-table-building continues for |
| 4766 | a while, and PyErr_Clear() might erroneously be |
| 4767 | called during that process. One such case has been |
| 4768 | fixed, but there might be more (now or later). |
| 4769 | */ |
| 4770 | PyErr_SetString(PyExc_SystemError, "lost exception"); |
| 4771 | } |
| 4772 | st->st_future = NULL; |
| 4773 | st->st_filename = NULL; |
| 4774 | PySymtable_Free(st); |
| 4775 | return NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
| 4778 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4779 | symtable_init_compiling_symbols(struct compiling *c) |
| 4780 | { |
| 4781 | PyObject *varnames; |
| 4782 | |
| 4783 | varnames = c->c_symtable->st_cur->ste_varnames; |
| 4784 | if (varnames == NULL) { |
| 4785 | varnames = PyList_New(0); |
| 4786 | if (varnames == NULL) |
| 4787 | return -1; |
| 4788 | c->c_symtable->st_cur->ste_varnames = varnames; |
| 4789 | Py_INCREF(varnames); |
| 4790 | } else |
| 4791 | Py_INCREF(varnames); |
| 4792 | c->c_varnames = varnames; |
| 4793 | |
| 4794 | c->c_globals = PyDict_New(); |
| 4795 | if (c->c_globals == NULL) |
| 4796 | return -1; |
| 4797 | c->c_freevars = PyDict_New(); |
| 4798 | if (c->c_freevars == NULL) |
| 4799 | return -1; |
| 4800 | c->c_cellvars = PyDict_New(); |
| 4801 | if (c->c_cellvars == NULL) |
| 4802 | return -1; |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | struct symbol_info { |
| 4807 | int si_nlocals; |
| 4808 | int si_ncells; |
| 4809 | int si_nfrees; |
| 4810 | int si_nimplicit; |
| 4811 | }; |
| 4812 | |
| 4813 | static void |
| 4814 | symtable_init_info(struct symbol_info *si) |
| 4815 | { |
| 4816 | si->si_nlocals = 0; |
| 4817 | si->si_ncells = 0; |
| 4818 | si->si_nfrees = 0; |
| 4819 | si->si_nimplicit = 0; |
| 4820 | } |
| 4821 | |
| 4822 | static int |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4823 | symtable_resolve_free(struct compiling *c, PyObject *name, int flags, |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4824 | struct symbol_info *si) |
| 4825 | { |
| 4826 | PyObject *dict, *v; |
| 4827 | |
| 4828 | /* Seperate logic for DEF_FREE. If it occurs in a function, |
| 4829 | it indicates a local that we must allocate storage for (a |
| 4830 | cell var). If it occurs in a class, then the class has a |
| 4831 | method and a free variable with the same name. |
| 4832 | */ |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4833 | if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4834 | /* If it isn't declared locally, it can't be a cell. */ |
| 4835 | if (!(flags & (DEF_LOCAL | DEF_PARAM))) |
| 4836 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4837 | v = PyInt_FromLong(si->si_ncells++); |
| 4838 | dict = c->c_cellvars; |
| 4839 | } else { |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4840 | /* If it is free anyway, then there is no need to do |
| 4841 | anything here. |
| 4842 | */ |
| 4843 | if (is_free(flags ^ DEF_FREE_CLASS) |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 4844 | || (flags == DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 4845 | return 0; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4846 | v = PyInt_FromLong(si->si_nfrees++); |
| 4847 | dict = c->c_freevars; |
| 4848 | } |
| 4849 | if (v == NULL) |
| 4850 | return -1; |
| 4851 | if (PyDict_SetItem(dict, name, v) < 0) { |
| 4852 | Py_DECREF(v); |
| 4853 | return -1; |
| 4854 | } |
| 4855 | Py_DECREF(v); |
| 4856 | return 0; |
| 4857 | } |
| 4858 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4859 | /* If a variable is a cell and an argument, make sure that appears in |
| 4860 | co_cellvars before any variable to its right in varnames. |
| 4861 | */ |
| 4862 | |
| 4863 | |
| 4864 | static int |
| 4865 | symtable_cellvar_offsets(PyObject **cellvars, int argcount, |
| 4866 | PyObject *varnames, int flags) |
| 4867 | { |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4868 | PyObject *v = NULL; |
| 4869 | PyObject *w, *d, *list = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4870 | int i, pos; |
| 4871 | |
| 4872 | if (flags & CO_VARARGS) |
| 4873 | argcount++; |
| 4874 | if (flags & CO_VARKEYWORDS) |
| 4875 | argcount++; |
| 4876 | for (i = argcount; --i >= 0; ) { |
| 4877 | v = PyList_GET_ITEM(varnames, i); |
| 4878 | if (PyDict_GetItem(*cellvars, v)) { |
| 4879 | if (list == NULL) { |
| 4880 | list = PyList_New(1); |
| 4881 | if (list == NULL) |
| 4882 | return -1; |
| 4883 | PyList_SET_ITEM(list, 0, v); |
| 4884 | Py_INCREF(v); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4885 | } else { |
| 4886 | if (PyList_Insert(list, 0, v) < 0) { |
| 4887 | Py_DECREF(list); |
| 4888 | return -1; |
| 4889 | } |
| 4890 | } |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4891 | } |
| 4892 | } |
Jeremy Hylton | 521482d | 2003-05-22 15:47:02 +0000 | [diff] [blame] | 4893 | if (list == NULL) |
| 4894 | return 0; |
| 4895 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4896 | /* There are cellvars that are also arguments. Create a dict |
| 4897 | to replace cellvars and put the args at the front. |
| 4898 | */ |
| 4899 | d = PyDict_New(); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4900 | if (d == NULL) |
| 4901 | return -1; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4902 | for (i = PyList_GET_SIZE(list); --i >= 0; ) { |
| 4903 | v = PyInt_FromLong(i); |
| 4904 | if (v == NULL) |
| 4905 | goto fail; |
| 4906 | if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0) |
| 4907 | goto fail; |
| 4908 | if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0) |
| 4909 | goto fail; |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4910 | Py_DECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4911 | } |
| 4912 | pos = 0; |
| 4913 | i = PyList_GET_SIZE(list); |
| 4914 | Py_DECREF(list); |
| 4915 | while (PyDict_Next(*cellvars, &pos, &v, &w)) { |
| 4916 | w = PyInt_FromLong(i++); /* don't care about the old key */ |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 4917 | if (w == NULL) |
| 4918 | goto fail; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4919 | if (PyDict_SetItem(d, v, w) < 0) { |
| 4920 | Py_DECREF(w); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4921 | v = NULL; |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4922 | goto fail; |
| 4923 | } |
| 4924 | Py_DECREF(w); |
| 4925 | } |
| 4926 | Py_DECREF(*cellvars); |
| 4927 | *cellvars = d; |
| 4928 | return 1; |
| 4929 | fail: |
| 4930 | Py_DECREF(d); |
Tim Peters | b39903b | 2003-03-24 17:22:24 +0000 | [diff] [blame] | 4931 | Py_XDECREF(v); |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 4932 | return -1; |
| 4933 | } |
| 4934 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4935 | static int |
| 4936 | symtable_freevar_offsets(PyObject *freevars, int offset) |
| 4937 | { |
| 4938 | PyObject *name, *v; |
| 4939 | int pos; |
| 4940 | |
| 4941 | /* The cell vars are the first elements of the closure, |
| 4942 | followed by the free vars. Update the offsets in |
| 4943 | c_freevars to account for number of cellvars. */ |
| 4944 | pos = 0; |
| 4945 | while (PyDict_Next(freevars, &pos, &name, &v)) { |
| 4946 | int i = PyInt_AS_LONG(v) + offset; |
| 4947 | PyObject *o = PyInt_FromLong(i); |
| 4948 | if (o == NULL) |
| 4949 | return -1; |
| 4950 | if (PyDict_SetItem(freevars, name, o) < 0) { |
| 4951 | Py_DECREF(o); |
| 4952 | return -1; |
| 4953 | } |
| 4954 | Py_DECREF(o); |
| 4955 | } |
| 4956 | return 0; |
| 4957 | } |
| 4958 | |
| 4959 | static int |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4960 | symtable_check_unoptimized(struct compiling *c, |
| 4961 | PySymtableEntryObject *ste, |
| 4962 | struct symbol_info *si) |
| 4963 | { |
| 4964 | char buf[300]; |
| 4965 | |
| 4966 | if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free |
| 4967 | || (ste->ste_nested && si->si_nimplicit))) |
| 4968 | return 0; |
| 4969 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4970 | #define ILLEGAL_CONTAINS "contains a nested function with free variables" |
| 4971 | |
| 4972 | #define ILLEGAL_IS "is a nested function" |
| 4973 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4974 | #define ILLEGAL_IMPORT_STAR \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4975 | "import * is not allowed in function '%.100s' because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4976 | |
| 4977 | #define ILLEGAL_BARE_EXEC \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4978 | "unqualified exec is not allowed in function '%.100s' it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4979 | |
| 4980 | #define ILLEGAL_EXEC_AND_IMPORT_STAR \ |
Neal Norwitz | 150d09d | 2002-01-29 00:56:37 +0000 | [diff] [blame] | 4981 | "function '%.100s' uses import * and bare exec, which are illegal " \ |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4982 | "because it %s" |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 4983 | |
| 4984 | /* XXX perhaps the linenos for these opt-breaking statements |
| 4985 | should be stored so the exception can point to them. */ |
| 4986 | |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4987 | if (ste->ste_child_free) { |
| 4988 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4989 | PyOS_snprintf(buf, sizeof(buf), |
| 4990 | ILLEGAL_IMPORT_STAR, |
| 4991 | PyString_AS_STRING(ste->ste_name), |
| 4992 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4993 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4994 | PyOS_snprintf(buf, sizeof(buf), |
| 4995 | ILLEGAL_BARE_EXEC, |
| 4996 | PyString_AS_STRING(ste->ste_name), |
| 4997 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 4998 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 4999 | PyOS_snprintf(buf, sizeof(buf), |
| 5000 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 5001 | PyString_AS_STRING(ste->ste_name), |
| 5002 | ILLEGAL_CONTAINS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 5003 | } |
| 5004 | } else { |
| 5005 | if (ste->ste_optimized == OPT_IMPORT_STAR) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5006 | PyOS_snprintf(buf, sizeof(buf), |
| 5007 | ILLEGAL_IMPORT_STAR, |
| 5008 | PyString_AS_STRING(ste->ste_name), |
| 5009 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 5010 | else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC)) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5011 | PyOS_snprintf(buf, sizeof(buf), |
| 5012 | ILLEGAL_BARE_EXEC, |
| 5013 | PyString_AS_STRING(ste->ste_name), |
| 5014 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 5015 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5016 | PyOS_snprintf(buf, sizeof(buf), |
| 5017 | ILLEGAL_EXEC_AND_IMPORT_STAR, |
| 5018 | PyString_AS_STRING(ste->ste_name), |
| 5019 | ILLEGAL_IS); |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 5020 | } |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5021 | } |
Jeremy Hylton | 280e6bd | 2001-03-22 03:51:05 +0000 | [diff] [blame] | 5022 | |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5023 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 5024 | PyErr_SyntaxLocation(c->c_symtable->st_filename, |
| 5025 | ste->ste_opt_lineno); |
| 5026 | return -1; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5030 | symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste, |
| 5031 | struct symbol_info *si) |
| 5032 | { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 5033 | if (c->c_future) |
| 5034 | c->c_flags |= c->c_future->ff_features; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5035 | if (ste->ste_generator) |
| 5036 | c->c_flags |= CO_GENERATOR; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5037 | if (ste->ste_type != TYPE_MODULE) |
| 5038 | c->c_flags |= CO_NEWLOCALS; |
| 5039 | if (ste->ste_type == TYPE_FUNCTION) { |
| 5040 | c->c_nlocals = si->si_nlocals; |
| 5041 | if (ste->ste_optimized == 0) |
| 5042 | c->c_flags |= CO_OPTIMIZED; |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5043 | else if (ste->ste_optimized != OPT_EXEC) |
| 5044 | return symtable_check_unoptimized(c, ste, si); |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5045 | } |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
| 5049 | static int |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5050 | symtable_error(struct symtable *st, int lineno) |
| 5051 | { |
| 5052 | if (lineno == 0) |
| 5053 | lineno = st->st_cur->ste_lineno; |
| 5054 | PyErr_SyntaxLocation(st->st_filename, lineno); |
| 5055 | st->st_errors++; |
| 5056 | return -1; |
| 5057 | } |
| 5058 | |
| 5059 | static int |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5060 | symtable_load_symbols(struct compiling *c) |
| 5061 | { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5062 | struct symtable *st = c->c_symtable; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5063 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5064 | PyObject *name, *varnames, *v; |
| 5065 | int i, flags, pos; |
| 5066 | struct symbol_info si; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5067 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5068 | v = NULL; |
| 5069 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5070 | if (symtable_init_compiling_symbols(c) < 0) |
| 5071 | goto fail; |
| 5072 | symtable_init_info(&si); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5073 | varnames = st->st_cur->ste_varnames; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5074 | si.si_nlocals = PyList_GET_SIZE(varnames); |
| 5075 | c->c_argcount = si.si_nlocals; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5076 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5077 | for (i = 0; i < si.si_nlocals; ++i) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5078 | v = PyInt_FromLong(i); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5079 | if (v == NULL) |
| 5080 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5081 | if (PyDict_SetItem(c->c_locals, |
| 5082 | PyList_GET_ITEM(varnames, i), v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5083 | goto fail; |
| 5084 | Py_DECREF(v); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
| 5087 | /* XXX The cases below define the rules for whether a name is |
| 5088 | local or global. The logic could probably be clearer. */ |
| 5089 | pos = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5090 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 5091 | flags = PyInt_AS_LONG(v); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5092 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5093 | if (flags & DEF_FREE_GLOBAL) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5094 | /* undo the original DEF_FREE */ |
| 5095 | flags &= ~(DEF_FREE | DEF_FREE_CLASS); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5096 | |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 5097 | /* Deal with names that need two actions: |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5098 | 1. Cell variables that are also locals. |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 5099 | 2. Free variables in methods that are also class |
| 5100 | variables or declared global. |
| 5101 | */ |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5102 | if (flags & (DEF_FREE | DEF_FREE_CLASS)) |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 5103 | symtable_resolve_free(c, name, flags, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5104 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5105 | if (flags & DEF_STAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5106 | c->c_argcount--; |
| 5107 | c->c_flags |= CO_VARARGS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5108 | } else if (flags & DEF_DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5109 | c->c_argcount--; |
| 5110 | c->c_flags |= CO_VARKEYWORDS; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5111 | } else if (flags & DEF_INTUPLE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5112 | c->c_argcount--; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5113 | else if (flags & DEF_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5114 | if (flags & DEF_PARAM) { |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 5115 | PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL, |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 5116 | PyString_AS_STRING(name)); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5117 | symtable_error(st, 0); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5118 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5119 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5120 | if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) |
| 5121 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5122 | } else if (flags & DEF_FREE_GLOBAL) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5123 | si.si_nimplicit++; |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 5124 | if (PyDict_SetItem(c->c_globals, name, Py_True) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5125 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5126 | } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5127 | v = PyInt_FromLong(si.si_nlocals++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5128 | if (v == NULL) |
| 5129 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5130 | if (PyDict_SetItem(c->c_locals, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5131 | goto fail; |
| 5132 | Py_DECREF(v); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5133 | if (ste->ste_type != TYPE_CLASS) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5134 | if (PyList_Append(c->c_varnames, name) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5135 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5136 | } else if (is_free(flags)) { |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5137 | if (ste->ste_nested) { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5138 | v = PyInt_FromLong(si.si_nfrees++); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5139 | if (v == NULL) |
| 5140 | goto fail; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5141 | if (PyDict_SetItem(c->c_freevars, name, v) < 0) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5142 | goto fail; |
| 5143 | Py_DECREF(v); |
| 5144 | } else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5145 | si.si_nimplicit++; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5146 | if (PyDict_SetItem(c->c_globals, name, |
Neal Norwitz | 0698222 | 2002-12-18 01:18:44 +0000 | [diff] [blame] | 5147 | Py_True) < 0) |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5148 | goto fail; |
| 5149 | if (st->st_nscopes != 1) { |
| 5150 | v = PyInt_FromLong(flags); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5151 | if (v == NULL) |
| 5152 | goto fail; |
Jeremy Hylton | 1abf610 | 2001-08-11 21:51:24 +0000 | [diff] [blame] | 5153 | if (PyDict_SetItem(st->st_global, |
| 5154 | name, v)) |
| 5155 | goto fail; |
| 5156 | Py_DECREF(v); |
| 5157 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5158 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5159 | } |
| 5160 | } |
Jeremy Hylton | ddc4fd0 | 2001-04-27 02:29:40 +0000 | [diff] [blame] | 5161 | assert(PyDict_Size(c->c_freevars) == si.si_nfrees); |
| 5162 | |
Jeremy Hylton | 220ae7c | 2001-03-21 16:43:47 +0000 | [diff] [blame] | 5163 | if (si.si_ncells > 1) { /* one cell is always in order */ |
| 5164 | if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount, |
| 5165 | c->c_varnames, c->c_flags) < 0) |
| 5166 | return -1; |
| 5167 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5168 | if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0) |
| 5169 | return -1; |
| 5170 | return symtable_update_flags(c, ste, &si); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5171 | fail: |
| 5172 | /* is this always the right thing to do? */ |
| 5173 | Py_XDECREF(v); |
| 5174 | return -1; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | static struct symtable * |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5178 | symtable_init() |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5179 | { |
| 5180 | struct symtable *st; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5181 | |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 5182 | st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5183 | if (st == NULL) |
| 5184 | return NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5185 | st->st_pass = 1; |
Tim Peters | ff1f852 | 2001-08-11 01:06:35 +0000 | [diff] [blame] | 5186 | |
Jeremy Hylton | 5acc0c0 | 2001-02-02 20:01:10 +0000 | [diff] [blame] | 5187 | st->st_filename = NULL; |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5188 | st->st_symbols = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5189 | if ((st->st_stack = PyList_New(0)) == NULL) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5190 | goto fail; |
| 5191 | if ((st->st_symbols = PyDict_New()) == NULL) |
| 5192 | goto fail; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5193 | st->st_cur = NULL; |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5194 | st->st_nscopes = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5195 | st->st_errors = 0; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5196 | st->st_private = NULL; |
| 5197 | return st; |
| 5198 | fail: |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5199 | PySymtable_Free(st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5200 | return NULL; |
| 5201 | } |
| 5202 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5203 | void |
| 5204 | PySymtable_Free(struct symtable *st) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5205 | { |
| 5206 | Py_XDECREF(st->st_symbols); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5207 | Py_XDECREF(st->st_stack); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5208 | Py_XDECREF(st->st_cur); |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 5209 | PyObject_FREE((void *)st); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5212 | /* When the compiler exits a scope, it must should update the scope's |
| 5213 | free variable information with the list of free variables in its |
| 5214 | children. |
| 5215 | |
| 5216 | Variables that are free in children and defined in the current |
| 5217 | scope are cellvars. |
| 5218 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5219 | 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] | 5220 | false), free variables in children that are not defined here are |
| 5221 | implicit globals. |
| 5222 | |
| 5223 | */ |
| 5224 | |
| 5225 | static int |
| 5226 | symtable_update_free_vars(struct symtable *st) |
| 5227 | { |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5228 | int i, j, def; |
| 5229 | PyObject *o, *name, *list = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5230 | PySymtableEntryObject *child, *ste = st->st_cur; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5231 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5232 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5233 | def = DEF_FREE_CLASS; |
| 5234 | else |
| 5235 | def = DEF_FREE; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5236 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5237 | int pos = 0; |
| 5238 | |
Jeremy Hylton | f9415e6 | 2003-05-22 16:22:33 +0000 | [diff] [blame] | 5239 | if (list && PyList_SetSlice(list, 0, |
| 5240 | PyList_GET_SIZE(list), 0) < 0) |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5241 | return -1; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5242 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5243 | PyList_GET_ITEM(ste->ste_children, i); |
| 5244 | while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5245 | int flags = PyInt_AS_LONG(o); |
| 5246 | if (!(is_free(flags))) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5247 | continue; /* avoids indentation */ |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5248 | if (list == NULL) { |
| 5249 | list = PyList_New(0); |
| 5250 | if (list == NULL) |
| 5251 | return -1; |
| 5252 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5253 | ste->ste_child_free = 1; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5254 | if (PyList_Append(list, name) < 0) { |
| 5255 | Py_DECREF(list); |
| 5256 | return -1; |
| 5257 | } |
| 5258 | } |
| 5259 | for (j = 0; list && j < PyList_GET_SIZE(list); j++) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5260 | PyObject *v; |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5261 | name = PyList_GET_ITEM(list, j); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5262 | v = PyDict_GetItem(ste->ste_symbols, name); |
| 5263 | /* If a name N is declared global in scope A and |
| 5264 | referenced in scope B contained (perhaps |
| 5265 | indirectly) in A and there are no scopes |
| 5266 | with bindings for N between B and A, then N |
Jeremy Hylton | 9c90105 | 2001-05-08 04:12:34 +0000 | [diff] [blame] | 5267 | is global in B. Unless A is a class scope, |
| 5268 | because class scopes are not considered for |
| 5269 | nested scopes. |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5270 | */ |
Jeremy Hylton | ce7ef59 | 2001-03-20 00:25:43 +0000 | [diff] [blame] | 5271 | if (v && (ste->ste_type != TYPE_CLASS)) { |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5272 | int flags = PyInt_AS_LONG(v); |
| 5273 | if (flags & DEF_GLOBAL) { |
| 5274 | symtable_undo_free(st, child->ste_id, |
| 5275 | name); |
| 5276 | continue; |
| 5277 | } |
| 5278 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5279 | if (ste->ste_nested) { |
| 5280 | if (symtable_add_def_o(st, ste->ste_symbols, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5281 | name, def) < 0) { |
| 5282 | Py_DECREF(list); |
| 5283 | return -1; |
| 5284 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5285 | } else { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5286 | if (symtable_check_global(st, child->ste_id, |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5287 | name) < 0) { |
| 5288 | Py_DECREF(list); |
| 5289 | return -1; |
| 5290 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5291 | } |
| 5292 | } |
| 5293 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 5294 | |
| 5295 | Py_XDECREF(list); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5296 | return 0; |
| 5297 | } |
| 5298 | |
| 5299 | /* If the current scope is a non-nested class or if name is not |
| 5300 | defined in the current, non-nested scope, then it is an implicit |
| 5301 | global in all nested scopes. |
| 5302 | */ |
| 5303 | |
| 5304 | static int |
| 5305 | symtable_check_global(struct symtable *st, PyObject *child, PyObject *name) |
| 5306 | { |
| 5307 | PyObject *o; |
| 5308 | int v; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5309 | PySymtableEntryObject *ste = st->st_cur; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5310 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5311 | if (ste->ste_type == TYPE_CLASS) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5312 | return symtable_undo_free(st, child, name); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5313 | o = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5314 | if (o == NULL) |
| 5315 | return symtable_undo_free(st, child, name); |
| 5316 | v = PyInt_AS_LONG(o); |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5317 | |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5318 | if (is_free(v) || (v & DEF_GLOBAL)) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5319 | return symtable_undo_free(st, child, name); |
| 5320 | else |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5321 | return symtable_add_def_o(st, ste->ste_symbols, |
| 5322 | name, DEF_FREE); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
| 5325 | static int |
| 5326 | symtable_undo_free(struct symtable *st, PyObject *id, |
| 5327 | PyObject *name) |
| 5328 | { |
| 5329 | int i, v, x; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5330 | PyObject *info; |
| 5331 | PySymtableEntryObject *ste; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5332 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5333 | ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id); |
| 5334 | if (ste == NULL) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5335 | return -1; |
Jeremy Hylton | 7889107 | 2001-03-01 06:09:34 +0000 | [diff] [blame] | 5336 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5337 | info = PyDict_GetItem(ste->ste_symbols, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5338 | if (info == NULL) |
| 5339 | return 0; |
| 5340 | v = PyInt_AS_LONG(info); |
| 5341 | if (is_free(v)) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5342 | if (symtable_add_def_o(st, ste->ste_symbols, name, |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5343 | DEF_FREE_GLOBAL) < 0) |
| 5344 | return -1; |
| 5345 | } else |
| 5346 | /* If the name is defined here or declared global, |
| 5347 | then the recursion stops. */ |
| 5348 | return 0; |
| 5349 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5350 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 5351 | PySymtableEntryObject *child; |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5352 | child = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5353 | PyList_GET_ITEM(ste->ste_children, i); |
| 5354 | x = symtable_undo_free(st, child->ste_id, name); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5355 | if (x < 0) |
| 5356 | return x; |
| 5357 | } |
| 5358 | return 0; |
| 5359 | } |
| 5360 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5361 | /* symtable_enter_scope() gets a reference via PySymtableEntry_New(). |
| 5362 | This reference is released when the scope is exited, via the DECREF |
| 5363 | in symtable_exit_scope(). |
| 5364 | */ |
| 5365 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5366 | static int |
| 5367 | symtable_exit_scope(struct symtable *st) |
| 5368 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5369 | int end; |
| 5370 | |
Jeremy Hylton | 4419ac1 | 2001-02-28 22:54:51 +0000 | [diff] [blame] | 5371 | if (st->st_pass == 1) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5372 | symtable_update_free_vars(st); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5373 | Py_DECREF(st->st_cur); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5374 | end = PyList_GET_SIZE(st->st_stack) - 1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5375 | st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 5376 | end); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5377 | if (PySequence_DelItem(st->st_stack, end) < 0) |
| 5378 | return -1; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5379 | return 0; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5380 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5381 | |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5382 | static void |
| 5383 | symtable_enter_scope(struct symtable *st, char *name, int type, |
| 5384 | int lineno) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5385 | { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5386 | PySymtableEntryObject *prev = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5387 | |
| 5388 | if (st->st_cur) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5389 | prev = st->st_cur; |
| 5390 | if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5391 | st->st_errors++; |
| 5392 | return; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5393 | } |
| 5394 | } |
Barry Warsaw | 0372af7 | 2001-02-23 18:22:59 +0000 | [diff] [blame] | 5395 | st->st_cur = (PySymtableEntryObject *) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5396 | PySymtableEntry_New(st, name, type, lineno); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5397 | if (st->st_cur == NULL) { |
| 5398 | st->st_errors++; |
| 5399 | return; |
| 5400 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5401 | if (strcmp(name, TOP) == 0) |
| 5402 | st->st_global = st->st_cur->ste_symbols; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5403 | if (prev && st->st_pass == 1) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5404 | if (PyList_Append(prev->ste_children, |
| 5405 | (PyObject *)st->st_cur) < 0) |
| 5406 | st->st_errors++; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 5407 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | static int |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5411 | symtable_lookup(struct symtable *st, char *name) |
| 5412 | { |
| 5413 | char buffer[MANGLE_LEN]; |
| 5414 | PyObject *v; |
| 5415 | int flags; |
| 5416 | |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5417 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5418 | name = buffer; |
| 5419 | v = PyDict_GetItemString(st->st_cur->ste_symbols, name); |
| 5420 | if (v == NULL) { |
| 5421 | if (PyErr_Occurred()) |
| 5422 | return -1; |
| 5423 | else |
| 5424 | return 0; |
| 5425 | } |
| 5426 | |
| 5427 | flags = PyInt_AS_LONG(v); |
| 5428 | return flags; |
| 5429 | } |
| 5430 | |
| 5431 | static int |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5432 | symtable_add_def(struct symtable *st, char *name, int flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5433 | { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5434 | PyObject *s; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5435 | char buffer[MANGLE_LEN]; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5436 | int ret; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5437 | |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5438 | /* Warn about None, except inside a tuple (where the assignment |
| 5439 | code already issues a warning). */ |
| 5440 | if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) && |
| 5441 | *name == 'N' && strcmp(name, "None") == 0) |
| 5442 | { |
| 5443 | if (symtable_warn(st, "argument named None")) |
| 5444 | return -1; |
| 5445 | } |
Raymond Hettinger | 0ae0c07 | 2002-06-20 22:23:15 +0000 | [diff] [blame] | 5446 | if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer))) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5447 | name = buffer; |
| 5448 | if ((s = PyString_InternFromString(name)) == NULL) |
| 5449 | return -1; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5450 | ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag); |
| 5451 | Py_DECREF(s); |
| 5452 | return ret; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* Must only be called with mangled names */ |
| 5456 | |
| 5457 | static int |
| 5458 | symtable_add_def_o(struct symtable *st, PyObject *dict, |
| 5459 | PyObject *name, int flag) |
| 5460 | { |
| 5461 | PyObject *o; |
| 5462 | int val; |
| 5463 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5464 | if ((o = PyDict_GetItem(dict, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5465 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5466 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5467 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5468 | PyString_AsString(name)); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5469 | return symtable_error(st, 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5470 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5471 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5472 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5473 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5474 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5475 | if (o == NULL) |
| 5476 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5477 | if (PyDict_SetItem(dict, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5478 | Py_DECREF(o); |
| 5479 | return -1; |
| 5480 | } |
| 5481 | Py_DECREF(o); |
| 5482 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5483 | if (flag & DEF_PARAM) { |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 5484 | if (PyList_Append(st->st_cur->ste_varnames, name) < 0) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5485 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5486 | } else if (flag & DEF_GLOBAL) { |
| 5487 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 5488 | perhaps only DEF_FREE_GLOBAL */ |
| 5489 | if ((o = PyDict_GetItem(st->st_global, name))) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5490 | val = PyInt_AS_LONG(o); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5491 | val |= flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5492 | } else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5493 | val = flag; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5494 | o = PyInt_FromLong(val); |
Martin v. Löwis | cd12bfc | 2003-05-03 10:53:08 +0000 | [diff] [blame] | 5495 | if (o == NULL) |
| 5496 | return -1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5497 | if (PyDict_SetItem(st->st_global, name, o) < 0) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5498 | Py_DECREF(o); |
| 5499 | return -1; |
| 5500 | } |
| 5501 | Py_DECREF(o); |
| 5502 | } |
| 5503 | return 0; |
| 5504 | } |
| 5505 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5506 | #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5507 | |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5508 | /* Look for a yield stmt under n. Return 1 if found, else 0. |
| 5509 | This hack is used to look inside "if 0:" blocks (which are normally |
| 5510 | ignored) in case those are the only places a yield occurs (so that this |
| 5511 | function is a generator). */ |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5512 | static int |
| 5513 | look_for_yield(node *n) |
| 5514 | { |
| 5515 | int i; |
| 5516 | |
| 5517 | for (i = 0; i < NCH(n); ++i) { |
| 5518 | node *kid = CHILD(n, i); |
| 5519 | |
| 5520 | switch (TYPE(kid)) { |
| 5521 | |
| 5522 | case classdef: |
| 5523 | case funcdef: |
Tim Peters | 08a898f | 2001-06-28 01:52:22 +0000 | [diff] [blame] | 5524 | case lambdef: |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5525 | /* Stuff in nested functions and classes can't make |
| 5526 | the parent a generator. */ |
| 5527 | return 0; |
| 5528 | |
| 5529 | case yield_stmt: |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5530 | return GENERATOR; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5531 | |
| 5532 | default: |
| 5533 | if (look_for_yield(kid)) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5534 | return GENERATOR; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
| 5537 | return 0; |
| 5538 | } |
| 5539 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5540 | static void |
| 5541 | symtable_node(struct symtable *st, node *n) |
| 5542 | { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5543 | int i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5544 | |
| 5545 | loop: |
| 5546 | switch (TYPE(n)) { |
| 5547 | case funcdef: { |
| 5548 | char *func_name = STR(CHILD(n, 1)); |
| 5549 | symtable_add_def(st, func_name, DEF_LOCAL); |
| 5550 | symtable_default_args(st, CHILD(n, 2)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5551 | symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5552 | symtable_funcdef(st, n); |
| 5553 | symtable_exit_scope(st); |
| 5554 | break; |
| 5555 | } |
| 5556 | case lambdef: |
| 5557 | if (NCH(n) == 4) |
| 5558 | symtable_default_args(st, CHILD(n, 1)); |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5559 | symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5560 | symtable_funcdef(st, n); |
| 5561 | symtable_exit_scope(st); |
| 5562 | break; |
| 5563 | case classdef: { |
| 5564 | char *tmp, *class_name = STR(CHILD(n, 1)); |
| 5565 | symtable_add_def(st, class_name, DEF_LOCAL); |
| 5566 | if (TYPE(CHILD(n, 2)) == LPAR) { |
| 5567 | node *bases = CHILD(n, 3); |
| 5568 | int i; |
| 5569 | for (i = 0; i < NCH(bases); i += 2) { |
| 5570 | symtable_node(st, CHILD(bases, i)); |
| 5571 | } |
| 5572 | } |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 5573 | symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5574 | tmp = st->st_private; |
| 5575 | st->st_private = class_name; |
| 5576 | symtable_node(st, CHILD(n, NCH(n) - 1)); |
| 5577 | st->st_private = tmp; |
| 5578 | symtable_exit_scope(st); |
| 5579 | break; |
| 5580 | } |
| 5581 | case if_stmt: |
| 5582 | for (i = 0; i + 3 < NCH(n); i += 4) { |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5583 | if (is_constant_false(NULL, (CHILD(n, i + 1)))) { |
| 5584 | if (st->st_cur->ste_generator == 0) |
| 5585 | st->st_cur->ste_generator = |
| 5586 | look_for_yield(CHILD(n, i+3)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5587 | continue; |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5588 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5589 | symtable_node(st, CHILD(n, i + 1)); |
| 5590 | symtable_node(st, CHILD(n, i + 3)); |
| 5591 | } |
| 5592 | if (i + 2 < NCH(n)) |
| 5593 | symtable_node(st, CHILD(n, i + 2)); |
| 5594 | break; |
| 5595 | case global_stmt: |
| 5596 | symtable_global(st, n); |
| 5597 | break; |
| 5598 | case import_stmt: |
| 5599 | symtable_import(st, n); |
| 5600 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5601 | case exec_stmt: { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5602 | st->st_cur->ste_optimized |= OPT_EXEC; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5603 | symtable_node(st, CHILD(n, 1)); |
| 5604 | if (NCH(n) > 2) |
| 5605 | symtable_node(st, CHILD(n, 3)); |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5606 | else { |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5607 | st->st_cur->ste_optimized |= OPT_BARE_EXEC; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 5608 | st->st_cur->ste_opt_lineno = n->n_lineno; |
| 5609 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5610 | if (NCH(n) > 4) |
| 5611 | symtable_node(st, CHILD(n, 5)); |
| 5612 | break; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5613 | |
| 5614 | } |
Jeremy Hylton | 384639f | 2001-02-19 15:50:51 +0000 | [diff] [blame] | 5615 | case assert_stmt: |
| 5616 | if (Py_OptimizeFlag) |
| 5617 | return; |
| 5618 | if (NCH(n) == 2) { |
| 5619 | n = CHILD(n, 1); |
| 5620 | goto loop; |
| 5621 | } else { |
| 5622 | symtable_node(st, CHILD(n, 1)); |
| 5623 | n = CHILD(n, 3); |
| 5624 | goto loop; |
| 5625 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5626 | case except_clause: |
| 5627 | if (NCH(n) == 4) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5628 | symtable_assign(st, CHILD(n, 3), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5629 | if (NCH(n) > 1) { |
| 5630 | n = CHILD(n, 1); |
| 5631 | goto loop; |
| 5632 | } |
| 5633 | break; |
| 5634 | case del_stmt: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5635 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5636 | break; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 5637 | case yield_stmt: |
| 5638 | st->st_cur->ste_generator = 1; |
| 5639 | n = CHILD(n, 1); |
| 5640 | goto loop; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5641 | case expr_stmt: |
| 5642 | if (NCH(n) == 1) |
| 5643 | n = CHILD(n, 0); |
| 5644 | else { |
| 5645 | if (TYPE(CHILD(n, 1)) == augassign) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5646 | symtable_assign(st, CHILD(n, 0), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5647 | symtable_node(st, CHILD(n, 2)); |
| 5648 | break; |
| 5649 | } else { |
| 5650 | int i; |
| 5651 | for (i = 0; i < NCH(n) - 2; i += 2) |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5652 | symtable_assign(st, CHILD(n, i), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5653 | n = CHILD(n, NCH(n) - 1); |
| 5654 | } |
| 5655 | } |
| 5656 | goto loop; |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5657 | case list_iter: |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5658 | /* only occurs when there are multiple for loops |
| 5659 | in a list comprehension */ |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5660 | n = CHILD(n, 0); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5661 | if (TYPE(n) == list_for) |
| 5662 | symtable_list_for(st, n); |
| 5663 | else { |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5664 | REQ(n, list_if); |
| 5665 | symtable_node(st, CHILD(n, 1)); |
| 5666 | if (NCH(n) == 3) { |
| 5667 | n = CHILD(n, 2); |
| 5668 | goto loop; |
| 5669 | } |
| 5670 | } |
| 5671 | break; |
| 5672 | case for_stmt: |
| 5673 | symtable_assign(st, CHILD(n, 1), 0); |
| 5674 | for (i = 3; i < NCH(n); ++i) |
| 5675 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5676 | symtable_node(st, CHILD(n, i)); |
| 5677 | break; |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5678 | case arglist: |
| 5679 | if (NCH(n) > 1) |
| 5680 | for (i = 0; i < NCH(n); ++i) { |
| 5681 | node *ch = CHILD(n, i); |
| 5682 | if (TYPE(ch) == argument && NCH(ch) == 2 && |
| 5683 | TYPE(CHILD(ch, 1)) == gen_for) { |
| 5684 | PyErr_SetString(PyExc_SyntaxError, |
| 5685 | "invalid syntax"); |
| 5686 | symtable_error(st, n->n_lineno); |
| 5687 | return; |
| 5688 | } |
| 5689 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5690 | /* The remaining cases fall through to default except in |
| 5691 | special circumstances. This requires the individual cases |
| 5692 | to be coded with great care, even though they look like |
| 5693 | rather innocuous. Each case must double-check TYPE(n). |
| 5694 | */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5695 | case argument: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5696 | if (TYPE(n) == argument && NCH(n) == 3) { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5697 | n = CHILD(n, 2); |
| 5698 | goto loop; |
| 5699 | } |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5700 | else if (TYPE(n) == argument && NCH(n) == 2 && |
| 5701 | TYPE(CHILD(n, 1)) == gen_for) { |
| 5702 | symtable_generator_expression(st, n); |
| 5703 | break; |
| 5704 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5705 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5706 | case listmaker: |
| 5707 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5708 | symtable_list_comprehension(st, n); |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5709 | break; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5710 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5711 | /* fall through */ |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5712 | case testlist_gexp: |
| 5713 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) { |
| 5714 | symtable_generator_expression(st, n); |
| 5715 | break; |
| 5716 | } |
| 5717 | /* fall through */ |
| 5718 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5719 | case atom: |
| 5720 | if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) { |
| 5721 | symtable_add_use(st, STR(CHILD(n, 0))); |
| 5722 | break; |
| 5723 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5724 | /* fall through */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5725 | default: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5726 | /* Walk over every non-token child with a special case |
| 5727 | for one child. |
| 5728 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5729 | if (NCH(n) == 1) { |
| 5730 | n = CHILD(n, 0); |
| 5731 | goto loop; |
| 5732 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 5733 | for (i = 0; i < NCH(n); ++i) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5734 | if (TYPE(CHILD(n, i)) >= single_input) |
| 5735 | symtable_node(st, CHILD(n, i)); |
| 5736 | } |
| 5737 | } |
| 5738 | |
| 5739 | static void |
| 5740 | symtable_funcdef(struct symtable *st, node *n) |
| 5741 | { |
| 5742 | node *body; |
| 5743 | |
| 5744 | if (TYPE(n) == lambdef) { |
| 5745 | if (NCH(n) == 4) |
| 5746 | symtable_params(st, CHILD(n, 1)); |
| 5747 | } else |
| 5748 | symtable_params(st, CHILD(n, 2)); |
| 5749 | body = CHILD(n, NCH(n) - 1); |
| 5750 | symtable_node(st, body); |
| 5751 | } |
| 5752 | |
| 5753 | /* The next two functions parse the argument tuple. |
Guido van Rossum | b716462 | 2002-08-16 02:48:11 +0000 | [diff] [blame] | 5754 | symtable_default_args() checks for names in the default arguments, |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5755 | which are references in the defining scope. symtable_params() |
| 5756 | parses the parameter names, which are defined in the function's |
| 5757 | body. |
| 5758 | |
| 5759 | varargslist: |
| 5760 | (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 5761 | | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
| 5762 | */ |
| 5763 | |
| 5764 | static void |
| 5765 | symtable_default_args(struct symtable *st, node *n) |
| 5766 | { |
| 5767 | node *c; |
| 5768 | int i; |
| 5769 | |
| 5770 | if (TYPE(n) == parameters) { |
| 5771 | n = CHILD(n, 1); |
| 5772 | if (TYPE(n) == RPAR) |
| 5773 | return; |
| 5774 | } |
| 5775 | REQ(n, varargslist); |
| 5776 | for (i = 0; i < NCH(n); i += 2) { |
| 5777 | c = CHILD(n, i); |
| 5778 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5779 | break; |
| 5780 | } |
| 5781 | if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL)) |
| 5782 | symtable_node(st, CHILD(n, i)); |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | static void |
| 5787 | symtable_params(struct symtable *st, node *n) |
| 5788 | { |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5789 | int i, complex = -1, ext = 0; |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5790 | node *c = NULL; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5791 | |
| 5792 | if (TYPE(n) == parameters) { |
| 5793 | n = CHILD(n, 1); |
| 5794 | if (TYPE(n) == RPAR) |
| 5795 | return; |
| 5796 | } |
| 5797 | REQ(n, varargslist); |
| 5798 | for (i = 0; i < NCH(n); i += 2) { |
| 5799 | c = CHILD(n, i); |
| 5800 | if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) { |
| 5801 | ext = 1; |
| 5802 | break; |
| 5803 | } |
| 5804 | if (TYPE(c) == test) { |
| 5805 | continue; |
| 5806 | } |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5807 | if (TYPE(CHILD(c, 0)) == NAME) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5808 | symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM); |
Guido van Rossum | 633d90c | 2002-12-23 16:51:42 +0000 | [diff] [blame] | 5809 | else { |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5810 | char nbuf[30]; |
| 5811 | PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5812 | symtable_add_def(st, nbuf, DEF_PARAM); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5813 | complex = i; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5814 | } |
| 5815 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5816 | if (ext) { |
| 5817 | c = CHILD(n, i); |
| 5818 | if (TYPE(c) == STAR) { |
| 5819 | i++; |
| 5820 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5821 | DEF_PARAM | DEF_STAR); |
| 5822 | i += 2; |
Jeremy Hylton | 1113cfc | 2001-01-23 00:50:52 +0000 | [diff] [blame] | 5823 | if (i >= NCH(n)) |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5824 | c = NULL; |
| 5825 | else |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5826 | c = CHILD(n, i); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5827 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5828 | if (c && TYPE(c) == DOUBLESTAR) { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5829 | i++; |
| 5830 | symtable_add_def(st, STR(CHILD(n, i)), |
| 5831 | DEF_PARAM | DEF_DOUBLESTAR); |
| 5832 | } |
| 5833 | } |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5834 | if (complex >= 0) { |
| 5835 | int j; |
| 5836 | for (j = 0; j <= complex; j++) { |
| 5837 | c = CHILD(n, j); |
| 5838 | if (TYPE(c) == COMMA) |
Jeremy Hylton | 2b3f0ca | 2001-02-19 23:52:49 +0000 | [diff] [blame] | 5839 | c = CHILD(n, ++j); |
| 5840 | else if (TYPE(c) == EQUAL) |
| 5841 | c = CHILD(n, j += 3); |
Jeremy Hylton | a6ebc48 | 2001-01-25 17:01:49 +0000 | [diff] [blame] | 5842 | if (TYPE(CHILD(c, 0)) == LPAR) |
| 5843 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5844 | } |
| 5845 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | static void |
| 5849 | symtable_params_fplist(struct symtable *st, node *n) |
| 5850 | { |
| 5851 | int i; |
| 5852 | node *c; |
| 5853 | |
| 5854 | REQ(n, fplist); |
| 5855 | for (i = 0; i < NCH(n); i += 2) { |
| 5856 | c = CHILD(n, i); |
| 5857 | REQ(c, fpdef); |
| 5858 | if (NCH(c) == 1) |
| 5859 | symtable_add_def(st, STR(CHILD(c, 0)), |
| 5860 | DEF_PARAM | DEF_INTUPLE); |
| 5861 | else |
| 5862 | symtable_params_fplist(st, CHILD(c, 1)); |
| 5863 | } |
| 5864 | |
| 5865 | } |
| 5866 | |
| 5867 | static void |
| 5868 | symtable_global(struct symtable *st, node *n) |
| 5869 | { |
| 5870 | int i; |
| 5871 | |
Jeremy Hylton | 9f324e9 | 2001-03-01 22:59:14 +0000 | [diff] [blame] | 5872 | /* XXX It might be helpful to warn about module-level global |
| 5873 | statements, but it's hard to tell the difference between |
| 5874 | module-level and a string passed to exec. |
| 5875 | */ |
Jeremy Hylton | c176132 | 2001-02-28 23:44:45 +0000 | [diff] [blame] | 5876 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5877 | for (i = 1; i < NCH(n); i += 2) { |
| 5878 | char *name = STR(CHILD(n, i)); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5879 | int flags; |
| 5880 | |
| 5881 | flags = symtable_lookup(st, name); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5882 | if (flags < 0) |
| 5883 | continue; |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5884 | if (flags && flags != DEF_GLOBAL) { |
| 5885 | char buf[500]; |
| 5886 | if (flags & DEF_PARAM) { |
Martin v. Löwis | dd7eb14 | 2003-10-18 22:05:25 +0000 | [diff] [blame] | 5887 | PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL, |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5888 | name); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 5889 | symtable_error(st, 0); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5890 | return; |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5891 | } |
| 5892 | else { |
| 5893 | if (flags & DEF_LOCAL) |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5894 | PyOS_snprintf(buf, sizeof(buf), |
| 5895 | GLOBAL_AFTER_ASSIGN, |
| 5896 | name); |
Guido van Rossum | 0bba7f8 | 2001-02-28 21:55:38 +0000 | [diff] [blame] | 5897 | else |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5898 | PyOS_snprintf(buf, sizeof(buf), |
| 5899 | GLOBAL_AFTER_USE, name); |
Guido van Rossum | ee34ac1 | 2001-02-28 22:08:12 +0000 | [diff] [blame] | 5900 | symtable_warn(st, buf); |
Jeremy Hylton | be77cf7 | 2001-02-27 05:15:57 +0000 | [diff] [blame] | 5901 | } |
| 5902 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5903 | symtable_add_def(st, name, DEF_GLOBAL); |
| 5904 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | static void |
| 5908 | symtable_list_comprehension(struct symtable *st, node *n) |
| 5909 | { |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5910 | /* listmaker: test list_for */ |
Barry Warsaw | 8f6d868 | 2001-11-28 21:10:39 +0000 | [diff] [blame] | 5911 | char tmpname[30]; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5912 | |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5913 | REQ(n, listmaker); |
| 5914 | PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", |
| 5915 | ++st->st_cur->ste_tmpname); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5916 | symtable_add_def(st, tmpname, DEF_LOCAL); |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5917 | symtable_list_for(st, CHILD(n, 1)); |
| 5918 | symtable_node(st, CHILD(n, 0)); |
| 5919 | --st->st_cur->ste_tmpname; |
| 5920 | } |
| 5921 | |
| 5922 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5923 | symtable_generator_expression(struct symtable *st, node *n) |
| 5924 | { |
| 5925 | /* testlist_gexp: test gen_for */ |
| 5926 | REQ(CHILD(n, 0), test); |
| 5927 | REQ(CHILD(n, 1), gen_for); |
| 5928 | |
| 5929 | symtable_enter_scope(st, "<genexpr>", TYPE(n), n->n_lineno); |
| 5930 | st->st_cur->ste_generator = GENERATOR_EXPRESSION; |
| 5931 | |
| 5932 | symtable_add_def(st, "[outmost-iterable]", DEF_PARAM); |
| 5933 | |
| 5934 | symtable_gen_for(st, CHILD(n, 1), 1); |
| 5935 | symtable_node(st, CHILD(n, 0)); |
| 5936 | symtable_exit_scope(st); |
| 5937 | |
| 5938 | /* for outmost iterable precomputation */ |
| 5939 | symtable_node(st, CHILD(CHILD(n, 1), 3)); |
| 5940 | } |
| 5941 | |
| 5942 | static void |
Jeremy Hylton | 4d508ad | 2003-05-21 17:34:50 +0000 | [diff] [blame] | 5943 | symtable_list_for(struct symtable *st, node *n) |
| 5944 | { |
| 5945 | REQ(n, list_for); |
| 5946 | /* list_for: for v in expr [list_iter] */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 5947 | symtable_assign(st, CHILD(n, 1), 0); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5948 | symtable_node(st, CHILD(n, 3)); |
| 5949 | if (NCH(n) == 5) |
| 5950 | symtable_node(st, CHILD(n, 4)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | static void |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 5954 | symtable_gen_for(struct symtable *st, node *n, int is_outmost) |
| 5955 | { |
| 5956 | REQ(n, gen_for); |
| 5957 | |
| 5958 | /* gen_for: for v in test [gen_iter] */ |
| 5959 | symtable_assign(st, CHILD(n, 1), 0); |
| 5960 | if (is_outmost) |
| 5961 | symtable_add_use(st, "[outmost-iterable]"); |
| 5962 | else |
| 5963 | symtable_node(st, CHILD(n, 3)); |
| 5964 | |
| 5965 | if (NCH(n) == 5) |
| 5966 | symtable_gen_iter(st, CHILD(n, 4)); |
| 5967 | } |
| 5968 | |
| 5969 | static void |
| 5970 | symtable_gen_iter(struct symtable *st, node *n) |
| 5971 | { |
| 5972 | REQ(n, gen_iter); |
| 5973 | |
| 5974 | n = CHILD(n, 0); |
| 5975 | if (TYPE(n) == gen_for) |
| 5976 | symtable_gen_for(st, n, 0); |
| 5977 | else { |
| 5978 | REQ(n, gen_if); |
| 5979 | symtable_node(st, CHILD(n, 1)); |
| 5980 | |
| 5981 | if (NCH(n) == 3) |
| 5982 | symtable_gen_iter(st, CHILD(n, 2)); |
| 5983 | } |
| 5984 | } |
| 5985 | |
| 5986 | static void |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5987 | symtable_import(struct symtable *st, node *n) |
| 5988 | { |
| 5989 | int i; |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5990 | /* import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5991 | | 'from' dotted_name 'import' |
| 5992 | ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 5993 | import_as_name: NAME [NAME NAME] |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5994 | */ |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5995 | if (STR(CHILD(n, 0))[0] == 'f') { /* from */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 5996 | node *dotname = CHILD(n, 1); |
| 5997 | if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) { |
| 5998 | /* check for bogus imports */ |
| 5999 | if (n->n_lineno >= st->st_future->ff_last_lineno) { |
| 6000 | PyErr_SetString(PyExc_SyntaxError, |
| 6001 | LATE_FUTURE); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 6002 | symtable_error(st, n->n_lineno); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 6003 | return; |
| 6004 | } |
| 6005 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6006 | if (TYPE(CHILD(n, 3)) == STAR) { |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 6007 | if (st->st_cur->ste_type != TYPE_MODULE) { |
Jeremy Hylton | 6a53bd8 | 2001-08-06 20:34:25 +0000 | [diff] [blame] | 6008 | if (symtable_warn(st, |
| 6009 | "import * only allowed at module level") < 0) |
| 6010 | return; |
Jeremy Hylton | 8a6f295 | 2001-08-06 19:45:40 +0000 | [diff] [blame] | 6011 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6012 | st->st_cur->ste_optimized |= OPT_IMPORT_STAR; |
Jeremy Hylton | 2e2cded | 2001-03-22 03:57:58 +0000 | [diff] [blame] | 6013 | st->st_cur->ste_opt_lineno = n->n_lineno; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6014 | } else { |
| 6015 | for (i = 3; i < NCH(n); i += 2) { |
| 6016 | node *c = CHILD(n, i); |
| 6017 | if (NCH(c) > 1) /* import as */ |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 6018 | symtable_assign(st, CHILD(c, 2), |
| 6019 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6020 | else |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 6021 | symtable_assign(st, CHILD(c, 0), |
| 6022 | DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6023 | } |
| 6024 | } |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 6025 | } else { |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6026 | for (i = 1; i < NCH(n); i += 2) { |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 6027 | symtable_assign(st, CHILD(n, i), DEF_IMPORT); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6028 | } |
| 6029 | } |
| 6030 | } |
| 6031 | |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6032 | /* The third argument to symatble_assign() is a flag to be passed to |
| 6033 | symtable_add_def() if it is eventually called. The flag is useful |
| 6034 | to specify the particular type of assignment that should be |
| 6035 | recorded, e.g. an assignment caused by import. |
| 6036 | */ |
| 6037 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6038 | static void |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6039 | symtable_assign(struct symtable *st, node *n, int def_flag) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6040 | { |
| 6041 | node *tmp; |
| 6042 | int i; |
| 6043 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6044 | loop: |
| 6045 | switch (TYPE(n)) { |
| 6046 | case lambdef: |
Jeremy Hylton | eab156f | 2001-01-30 01:24:43 +0000 | [diff] [blame] | 6047 | /* invalid assignment, e.g. lambda x:x=2. The next |
| 6048 | pass will catch this error. */ |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6049 | return; |
| 6050 | case power: |
| 6051 | if (NCH(n) > 2) { |
| 6052 | for (i = 2; i < NCH(n); ++i) |
| 6053 | if (TYPE(CHILD(n, i)) != DOUBLESTAR) |
| 6054 | symtable_node(st, CHILD(n, i)); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6055 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6056 | if (NCH(n) > 1) { |
| 6057 | symtable_node(st, CHILD(n, 0)); |
| 6058 | symtable_node(st, CHILD(n, 1)); |
| 6059 | } else { |
| 6060 | n = CHILD(n, 0); |
| 6061 | goto loop; |
| 6062 | } |
| 6063 | return; |
| 6064 | case listmaker: |
Jeremy Hylton | 23b4227 | 2001-03-19 20:38:06 +0000 | [diff] [blame] | 6065 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) { |
| 6066 | /* XXX This is an error, but the next pass |
| 6067 | will catch it. */ |
| 6068 | return; |
| 6069 | } else { |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6070 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6071 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6072 | } |
| 6073 | return; |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 6074 | case testlist_gexp: |
| 6075 | if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) { |
| 6076 | /* XXX This is an error, but the next pass |
| 6077 | will catch it. */ |
| 6078 | return; |
| 6079 | } else { |
| 6080 | for (i = 0; i < NCH(n); i += 2) |
| 6081 | symtable_assign(st, CHILD(n, i), def_flag); |
| 6082 | } |
| 6083 | return; |
| 6084 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6085 | case exprlist: |
| 6086 | case testlist: |
Guido van Rossum | 2d3b986 | 2002-05-24 15:47:06 +0000 | [diff] [blame] | 6087 | case testlist1: |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6088 | if (NCH(n) == 1) { |
| 6089 | n = CHILD(n, 0); |
| 6090 | goto loop; |
| 6091 | } |
| 6092 | else { |
| 6093 | int i; |
| 6094 | for (i = 0; i < NCH(n); i += 2) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6095 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6096 | return; |
| 6097 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6098 | case atom: |
| 6099 | tmp = CHILD(n, 0); |
| 6100 | if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { |
| 6101 | n = CHILD(n, 1); |
| 6102 | goto loop; |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 6103 | } else if (TYPE(tmp) == NAME) { |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 6104 | if (strcmp(STR(tmp), "__debug__") == 0) { |
| 6105 | PyErr_SetString(PyExc_SyntaxError, |
| 6106 | ASSIGN_DEBUG); |
Jeremy Hylton | 9832613 | 2003-09-22 04:26:44 +0000 | [diff] [blame] | 6107 | symtable_error(st, n->n_lineno); |
| 6108 | return; |
Jeremy Hylton | 778e265 | 2001-11-09 19:50:08 +0000 | [diff] [blame] | 6109 | } |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6110 | symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag); |
Jeremy Hylton | 897b821 | 2001-03-23 14:08:38 +0000 | [diff] [blame] | 6111 | } |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6112 | return; |
| 6113 | case dotted_as_name: |
| 6114 | if (NCH(n) == 3) |
| 6115 | symtable_add_def(st, STR(CHILD(n, 2)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6116 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6117 | else |
| 6118 | symtable_add_def(st, |
| 6119 | STR(CHILD(CHILD(n, |
| 6120 | 0), 0)), |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6121 | DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6122 | return; |
| 6123 | case dotted_name: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6124 | symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6125 | return; |
| 6126 | case NAME: |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6127 | symtable_add_def(st, STR(n), DEF_LOCAL | def_flag); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6128 | return; |
| 6129 | default: |
| 6130 | if (NCH(n) == 0) |
| 6131 | return; |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 6132 | if (NCH(n) == 1) { |
| 6133 | n = CHILD(n, 0); |
| 6134 | goto loop; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6135 | } |
Jeremy Hylton | 17820c4 | 2001-02-19 15:33:10 +0000 | [diff] [blame] | 6136 | /* Should only occur for errors like x + 1 = 1, |
| 6137 | which will be caught in the next pass. */ |
| 6138 | for (i = 0; i < NCH(n); ++i) |
| 6139 | if (TYPE(CHILD(n, i)) >= single_input) |
Jeremy Hylton | 961dfe0 | 2001-10-18 16:15:10 +0000 | [diff] [blame] | 6140 | symtable_assign(st, CHILD(n, i), def_flag); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6141 | } |
| 6142 | } |