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