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