1
/* -----------------------------------------------------------------------------
4
* This file provides some general purpose functions for manipulating
7
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
9
* Copyright (C) 1999-2000. The University of Chicago
10
* See the file LICENSE for information on usage and redistribution.
11
* ----------------------------------------------------------------------------- */
17
static char cvsroot[] = "$Header: /cvs/projects/SWIG/Source/Swig/tree.c,v 1.6.4.8 2001/12/10 21:14:27 beazley Exp $";
19
/* -----------------------------------------------------------------------------
22
* Dump the tag structure of a parse tree to standard output
23
* ----------------------------------------------------------------------------- */
26
Swig_dump_tags(DOH *obj, DOH *root) {
30
if (!root) croot = NewString("");
34
Printf(stdout,"%s . %s (%s:%d)\n", croot, nodeType(obj), Getfile(obj), Getline(obj));
35
cobj = firstChild(obj);
37
newroot = NewStringf("%s . %s",croot,nodeType(obj));
38
Swig_dump_tags(cobj,newroot);
41
obj = nextSibling(obj);
48
/* -----------------------------------------------------------------------------
51
* Dump the tree structure of a parse tree to standard output
52
* ----------------------------------------------------------------------------- */
54
static int indent_level = 0;
56
static void print_indent(int l) {
58
for (i = 0; i < indent_level; i++) {
68
Swig_dump_tree(DOH *obj) {
74
Printf(stdout,"+++ %s ----------------------------------------\n", nodeType(obj));
78
if ((Cmp(k,"nodeType") == 0) || (Cmp(k,"firstChild") == 0) || (Cmp(k,"lastChild") == 0) ||
79
(Cmp(k,"parentNode") == 0) || (Cmp(k,"nextSibling") == 0) ||
80
(Cmp(k,"previousSibling") == 0) || (*(Char(k)) == '$')) {
82
} else if (Cmp(k,"parms") == 0) {
84
Printf(stdout,"%-12s - %s\n", k, ParmList_protostr(Getattr(obj,k)));
89
if (DohIsString(Getattr(obj,k))) {
90
o = Str(Getattr(obj,k));
94
Printf(stdout,"%-12s - \"%(escape)-0.40s%s\"\n", k, o, trunc);
97
Printf(stdout,"%-12s - 0x%x\n", k, Getattr(obj,k));
102
cobj = firstChild(obj);
106
Swig_dump_tree(cobj);
112
obj = nextSibling(obj);
116
/* -----------------------------------------------------------------------------
119
* Appends a new child to a node
120
* ----------------------------------------------------------------------------- */
123
appendChild(Node *node, Node *chd) {
128
lc = lastChild(node);
130
set_firstChild(node,chd);
132
set_nextSibling(lc,chd);
133
set_previousSibling(chd,lc);
137
set_parentNode(chd,node);
138
chd = nextSibling(chd);
140
set_lastChild(node,lc);
144
/* -----------------------------------------------------------------------------
146
* ----------------------------------------------------------------------------- */
148
#define MAX_SWIG_STACK 256
149
static Hash *attr_stack[MAX_SWIG_STACK];
150
static Node **nodeptr_stack[MAX_SWIG_STACK];
151
static Node *node_stack[MAX_SWIG_STACK];
152
static int stackp = 0;
153
static int stack_direction = 0;
155
static void set_direction(int n, int *x) {
160
stack_direction = -1; /* Stack grows down */
162
stack_direction = 1; /* Stack grows up */
168
Swig_require(Node **nptr, ...) {
175
name = va_arg(ap, char *);
182
} else if (*name == '?') {
187
obj = Getattr(n,name);
189
Printf(stderr,"%s:%d. Fatal error (Swig_require). Missing attribute '%s' in node '%s'.\n",
190
Getfile(n), Getline(n), name, nodeType(n));
193
if (!obj) obj = DohNone;
195
if (!attr_stack[stackp]) {
196
attr_stack[stackp]= NewHash();
198
frame = attr_stack[stackp];
199
if (Setattr(frame,name,obj)) {
200
Printf(stderr,"Swig_require('%s'): Warning, attribute '%s' was already saved.\n", nodeType(n), name);
203
name = va_arg(ap, char *);
207
/* This is a sanity check to make sure no one is saving data, but not restoring it */
210
if (!stack_direction) set_direction(1,0);
212
if (stack_direction < 0) {
213
if ((((char *) nptr) >= ((char *) nodeptr_stack[stackp-1])) && (n != node_stack[stackp-1])) e = 1;
215
if ((((char *) nptr) <= ((char *) nodeptr_stack[stackp-1])) && (n != node_stack[stackp-1])) e = 1;
219
"Swig_require('%s'): Fatal memory management error. If you are seeing this\n\
220
message. It means that the target language module is not managing its memory\n\
221
correctly. A handler for '%s' probably forgot to call Swig_restore().\n\
222
Please report this problem to swig-dev@cs.uchicago.edu.\n", nodeType(n), nodeType(node_stack[stackp-1]));
226
nodeptr_stack[stackp] = nptr;
227
node_stack[stackp] = n;
235
Swig_save(Node **nptr, ...) {
242
if ((stackp > 0) && (nodeptr_stack[stackp-1] == nptr)) {
243
frame = attr_stack[stackp-1];
247
if (!stack_direction) set_direction(1,0);
248
if (stack_direction < 0) {
249
if ((((char *) nptr) >= ((char *) nodeptr_stack[stackp-1])) && (n != node_stack[stackp-1])) e = 1;
251
if ((((char *) nptr) <= ((char *) nodeptr_stack[stackp-1])) && (n != node_stack[stackp-1])) e = 1;
255
"Swig_save('%s'): Fatal memory management error. If you are seeing this\n\
256
message. It means that the target language module is not managing its memory\n\
257
correctly. A handler for '%s' probably forgot to call Swig_restore().\n\
258
Please report this problem to swig-dev@cs.uchicago.edu.\n", nodeType(n), nodeType(node_stack[stackp-1]));
262
attr_stack[stackp] = NewHash();
263
nodeptr_stack[stackp] = nptr;
264
node_stack[stackp] = n;
265
frame = attr_stack[stackp];
269
name = va_arg(ap, char *);
273
} else if (*name == '?') {
276
obj = Getattr(n,name);
280
if (Setattr(frame,name,obj)) {
281
Printf(stderr,"Swig_save('%s'): Warning, attribute '%s' was already saved.\n", nodeType(n), name);
283
name = va_arg(ap, char *);
290
Swig_restore(Node **nptr) {
295
if (!(nptr==nodeptr_stack[stackp-1])) {
297
"Swig_restore('%s'): Fatal memory management error. If you are seeing this\n\
298
message. It means that the target language module is not managing its memory\n\
299
correctly. A handler for '%s' probably forgot to call Swig_restore().\n\
300
Please report this problem to swig-dev@cs.uchicago.edu.\n", nodeType(n), nodeType(node_stack[stackp-1]));
304
frame = attr_stack[stackp];
305
nodeptr_stack[stackp] = 0;
306
node_stack[stackp] = 0;
307
for (key = Firstkey(frame); key; key = Nextkey(frame)) {
308
DOH *obj = Getattr(frame,key);
309
if (obj != DohNone) {