blob: 25edc8bed0857d2f88e911a808a70b1451af623e [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Vladimir Marangozov58e64a82000-09-03 23:47:08 +00002/* Parse tree node implementation */
3
4#include "pgenheaders.h"
5#include "node.h"
6#include "errcode.h"
7
Fred Drakeef8ace32000-08-24 00:32:09 +00008#ifdef HAVE_LIMITS_H
Jeremy Hylton94988062000-06-20 19:10:44 +00009#include <limits.h>
Fred Drakeef8ace32000-08-24 00:32:09 +000010#endif
11#ifndef INT_MAX
12#define INT_MAX 2147483647
13#endif
Jeremy Hylton94988062000-06-20 19:10:44 +000014
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015node *
Thomas Wouters23c9e002000-07-22 19:20:54 +000016PyNode_New(int type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017{
Guido van Rossum86bea461997-04-29 21:03:06 +000018 node *n = PyMem_NEW(node, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019 if (n == NULL)
20 return NULL;
21 n->n_type = type;
22 n->n_str = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000023 n->n_lineno = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024 n->n_nchildren = 0;
25 n->n_child = NULL;
26 return n;
27}
28
29#define XXX 3 /* Node alignment factor to speed up realloc */
30#define XXXROUNDUP(n) ((n) == 1 ? 1 : ((n) + XXX - 1) / XXX * XXX)
31
Jeremy Hylton94988062000-06-20 19:10:44 +000032int
Thomas Wouters23c9e002000-07-22 19:20:54 +000033PyNode_AddChild(register node *n1, int type, char *str, int lineno)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034{
35 register int nch = n1->n_nchildren;
36 register int nch1 = nch+1;
37 register node *n;
Fred Drakeef8ace32000-08-24 00:32:09 +000038 if (nch == INT_MAX || nch < 0)
Jeremy Hylton94988062000-06-20 19:10:44 +000039 return E_OVERFLOW;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040 if (XXXROUNDUP(nch) < nch1) {
41 n = n1->n_child;
42 nch1 = XXXROUNDUP(nch1);
Guido van Rossum86bea461997-04-29 21:03:06 +000043 PyMem_RESIZE(n, node, nch1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 if (n == NULL)
Jeremy Hylton94988062000-06-20 19:10:44 +000045 return E_NOMEM;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 n1->n_child = n;
47 }
48 n = &n1->n_child[n1->n_nchildren++];
49 n->n_type = type;
50 n->n_str = str;
Guido van Rossum3f5da241990-12-20 15:06:42 +000051 n->n_lineno = lineno;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052 n->n_nchildren = 0;
53 n->n_child = NULL;
Jeremy Hylton94988062000-06-20 19:10:44 +000054 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055}
Guido van Rossum03a24cd1990-11-18 17:37:06 +000056
Guido van Rossum3f5da241990-12-20 15:06:42 +000057/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000058static void freechildren(node *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000059
60
61void
Thomas Wouters23c9e002000-07-22 19:20:54 +000062PyNode_Free(node *n)
Guido van Rossum3f5da241990-12-20 15:06:42 +000063{
64 if (n != NULL) {
65 freechildren(n);
Guido van Rossum86bea461997-04-29 21:03:06 +000066 PyMem_DEL(n);
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 }
68}
69
Guido van Rossum03a24cd1990-11-18 17:37:06 +000070static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000071freechildren(node *n)
Guido van Rossum03a24cd1990-11-18 17:37:06 +000072{
73 int i;
74 for (i = NCH(n); --i >= 0; )
75 freechildren(CHILD(n, i));
76 if (n->n_child != NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000077 PyMem_DEL(n->n_child);
Guido van Rossum03a24cd1990-11-18 17:37:06 +000078 if (STR(n) != NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000079 PyMem_DEL(STR(n));
Guido van Rossum03a24cd1990-11-18 17:37:06 +000080}