Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 2 | #include "compile.h" |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 3 | #include "symtable.h" |
| 4 | #include "graminit.h" |
| 5 | #include "structmember.h" |
| 6 | |
| 7 | PyObject * |
| 8 | PySymtableEntry_New(struct symtable *st, char *name, int type, int lineno) |
| 9 | { |
| 10 | PySymtableEntryObject *ste = NULL; |
| 11 | PyObject *k, *v; |
| 12 | |
| 13 | k = PyInt_FromLong(st->st_nscopes++); |
| 14 | if (k == NULL) |
| 15 | goto fail; |
| 16 | v = PyDict_GetItem(st->st_symbols, k); |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 17 | if (v) /* XXX could check that name, type, lineno match */ { |
| 18 | Py_INCREF(v); |
| 19 | return v; |
| 20 | } |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 21 | |
| 22 | ste = (PySymtableEntryObject *)PyObject_New(PySymtableEntryObject, |
| 23 | &PySymtableEntry_Type); |
| 24 | ste->ste_table = st; |
| 25 | ste->ste_id = k; |
| 26 | |
| 27 | v = PyString_FromString(name); |
| 28 | if (v == NULL) |
| 29 | goto fail; |
| 30 | ste->ste_name = v; |
| 31 | |
| 32 | v = PyDict_New(); |
| 33 | if (v == NULL) |
| 34 | goto fail; |
| 35 | ste->ste_symbols = v; |
| 36 | |
| 37 | v = PyList_New(0); |
| 38 | if (v == NULL) |
| 39 | goto fail; |
| 40 | ste->ste_varnames = v; |
| 41 | |
| 42 | v = PyList_New(0); |
| 43 | if (v == NULL) |
| 44 | goto fail; |
| 45 | ste->ste_children = v; |
| 46 | |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 47 | ste->ste_optimized = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 48 | ste->ste_lineno = lineno; |
| 49 | switch (type) { |
| 50 | case funcdef: |
| 51 | case lambdef: |
| 52 | ste->ste_type = TYPE_FUNCTION; |
| 53 | break; |
| 54 | case classdef: |
| 55 | ste->ste_type = TYPE_CLASS; |
| 56 | break; |
| 57 | case single_input: |
| 58 | case eval_input: |
| 59 | case file_input: |
| 60 | ste->ste_type = TYPE_MODULE; |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | if (st->st_cur == NULL) |
| 65 | ste->ste_nested = 0; |
| 66 | else if (st->st_cur->ste_nested |
| 67 | || st->st_cur->ste_type == TYPE_FUNCTION) |
| 68 | ste->ste_nested = 1; |
| 69 | else |
| 70 | ste->ste_nested = 0; |
| 71 | ste->ste_child_free = 0; |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 72 | ste->ste_generator = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 73 | |
| 74 | if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) |
| 75 | goto fail; |
Jeremy Hylton | 74b3bc4 | 2001-02-23 17:55:27 +0000 | [diff] [blame] | 76 | |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 77 | return (PyObject *)ste; |
| 78 | fail: |
| 79 | Py_XDECREF(ste); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | static PyObject * |
| 84 | ste_repr(PySymtableEntryObject *ste) |
| 85 | { |
| 86 | char buf[256]; |
| 87 | |
| 88 | sprintf(buf, "<symtable entry %.100s(%ld), line %d>", |
| 89 | PyString_AS_STRING(ste->ste_name), |
| 90 | PyInt_AS_LONG(ste->ste_id), |
| 91 | ste->ste_lineno); |
| 92 | return PyString_FromString(buf); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | ste_dealloc(PySymtableEntryObject *ste) |
| 97 | { |
| 98 | ste->ste_table = NULL; |
| 99 | Py_XDECREF(ste->ste_id); |
| 100 | Py_XDECREF(ste->ste_name); |
| 101 | Py_XDECREF(ste->ste_symbols); |
| 102 | Py_XDECREF(ste->ste_varnames); |
| 103 | Py_XDECREF(ste->ste_children); |
| 104 | PyObject_Del(ste); |
| 105 | } |
| 106 | |
| 107 | #define OFF(x) offsetof(PySymtableEntryObject, x) |
| 108 | |
| 109 | static struct memberlist ste_memberlist[] = { |
| 110 | {"id", T_OBJECT, OFF(ste_id), READONLY}, |
| 111 | {"name", T_OBJECT, OFF(ste_name), READONLY}, |
| 112 | {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, |
| 113 | {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, |
| 114 | {"children", T_OBJECT, OFF(ste_children), READONLY}, |
| 115 | {"type", T_INT, OFF(ste_type), READONLY}, |
| 116 | {"lineno", T_INT, OFF(ste_lineno), READONLY}, |
| 117 | {"optimized",T_INT, OFF(ste_optimized), READONLY}, |
| 118 | {"nested", T_INT, OFF(ste_nested), READONLY}, |
| 119 | {NULL} |
| 120 | }; |
| 121 | |
| 122 | static PyObject * |
| 123 | ste_getattr(PySymtableEntryObject *ste, char *name) |
| 124 | { |
| 125 | return PyMember_Get((char *)ste, ste_memberlist, name); |
| 126 | } |
| 127 | |
| 128 | PyTypeObject PySymtableEntry_Type = { |
| 129 | PyObject_HEAD_INIT(&PyType_Type) |
| 130 | 0, |
| 131 | "symtable entry", |
| 132 | sizeof(PySymtableEntryObject), |
| 133 | 0, |
| 134 | (destructor)ste_dealloc, /* tp_dealloc */ |
| 135 | 0, /* tp_print */ |
| 136 | (getattrfunc)ste_getattr, /* tp_getattr */ |
| 137 | 0, /* tp_setattr */ |
| 138 | 0, /* tp_compare */ |
| 139 | (reprfunc)ste_repr, /* tp_repr */ |
| 140 | 0, /* tp_as_number */ |
| 141 | 0, /* tp_as_sequence */ |
| 142 | 0, /* tp_as_mapping */ |
| 143 | 0, /* tp_hash */ |
| 144 | 0, /* tp_call */ |
| 145 | 0, /* tp_str */ |
| 146 | 0, /* tp_getattro */ |
| 147 | 0, /* tp_setattro */ |
| 148 | 0, /* tp_as_buffer */ |
| 149 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 150 | 0, /* tp_doc */ |
| 151 | }; |