blob: f1b70e0f6815be206c96b18b56baa6b6a0bf4a9f [file] [log] [blame]
Vladimir Marangozov58e64a82000-09-03 23:47:08 +00001/* Parse tree node implementation */
2
Tim Peters1d6a7292000-09-26 06:11:54 +00003#include "Python.h"
Vladimir Marangozov58e64a82000-09-03 23:47:08 +00004#include "node.h"
5#include "errcode.h"
6
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007node *
Thomas Wouters23c9e002000-07-22 19:20:54 +00008PyNode_New(int type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000010 node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
11 if (n == NULL)
12 return NULL;
13 n->n_type = type;
14 n->n_str = NULL;
15 n->n_lineno = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000016 n->n_end_lineno = 0;
17 n->n_end_col_offset = -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 n->n_nchildren = 0;
19 n->n_child = NULL;
20 return n;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021}
22
Tim Peters623fdb92002-07-08 19:11:07 +000023/* See comments at XXXROUNDUP below. Returns -1 on overflow. */
Tim Peters755ebea2002-07-08 06:32:09 +000024static int
25fancy_roundup(int n)
26{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 /* Round up to the closest power of 2 >= n. */
28 int result = 256;
29 assert(n > 128);
30 while (result < n) {
31 result <<= 1;
32 if (result <= 0)
33 return -1;
34 }
35 return result;
Tim Peters755ebea2002-07-08 06:32:09 +000036}
37
38/* A gimmick to make massive numbers of reallocs quicker. The result is
Tim Peterse561dc22002-07-15 17:58:03 +000039 * a number >= the input. In PyNode_AddChild, it's used like so, when
40 * we're about to add child number current_size + 1:
41 *
42 * if XXXROUNDUP(current_size) < XXXROUNDUP(current_size + 1):
43 * allocate space for XXXROUNDUP(current_size + 1) total children
44 * else:
45 * we already have enough space
46 *
47 * Since a node starts out empty, we must have
48 *
49 * XXXROUNDUP(0) < XXXROUNDUP(1)
50 *
51 * so that we allocate space for the first child. One-child nodes are very
52 * common (presumably that would change if we used a more abstract form
53 * of syntax tree), so to avoid wasting memory it's desirable that
54 * XXXROUNDUP(1) == 1. That in turn forces XXXROUNDUP(0) == 0.
55 *
56 * Else for 2 <= n <= 128, we round up to the closest multiple of 4. Why 4?
57 * Rounding up to a multiple of an exact power of 2 is very efficient, and
58 * most nodes with more than one child have <= 4 kids.
59 *
60 * Else we call fancy_roundup() to grow proportionately to n. We've got an
Tim Peters755ebea2002-07-08 06:32:09 +000061 * extreme case then (like test_longexp.py), and on many platforms doing
62 * anything less than proportional growth leads to exorbitant runtime
63 * (e.g., MacPython), or extreme fragmentation of user address space (e.g.,
64 * Win98).
Tim Peterse561dc22002-07-15 17:58:03 +000065 *
66 * In a run of compileall across the 2.3a0 Lib directory, Andrew MacIntyre
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067 * reported that, with this scheme, 89% of PyObject_REALLOC calls in
Tim Peterse561dc22002-07-15 17:58:03 +000068 * PyNode_AddChild passed 1 for the size, and 9% passed 4. So this usually
69 * wastes very little memory, but is very effective at sidestepping
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000070 * platform-realloc disasters on vulnerable platforms.
Tim Peterse561dc22002-07-15 17:58:03 +000071 *
72 * Note that this would be straightforward if a node stored its current
73 * capacity. The code is tricky to avoid that.
Tim Peters755ebea2002-07-08 06:32:09 +000074 */
Serhiy Storchaka67c719b2014-09-05 10:10:23 +030075#define XXXROUNDUP(n) ((n) <= 1 ? (n) : \
76 (n) <= 128 ? (int)_Py_SIZE_ROUND_UP((n), 4) : \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 fancy_roundup(n))
Tim Peters755ebea2002-07-08 06:32:09 +000078
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000079
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000080void
81_PyNode_FinalizeEndPos(node *n)
82{
83 int nch = NCH(n);
84 node *last;
85 if (nch == 0) {
86 return;
87 }
88 last = CHILD(n, nch - 1);
89 _PyNode_FinalizeEndPos(last);
90 n->n_end_lineno = last->n_end_lineno;
91 n->n_end_col_offset = last->n_end_col_offset;
92}
93
Jeremy Hylton94988062000-06-20 19:10:44 +000094int
Ivan Levkivskyi9932a222019-01-22 11:18:22 +000095PyNode_AddChild(node *n1, int type, char *str, int lineno, int col_offset,
96 int end_lineno, int end_col_offset)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 const int nch = n1->n_nchildren;
99 int current_capacity;
100 int required_capacity;
101 node *n;
Tim Peters755ebea2002-07-08 06:32:09 +0000102
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000103 // finalize end position of previous node (if any)
104 if (nch > 0) {
105 _PyNode_FinalizeEndPos(CHILD(n1, nch - 1));
106 }
107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (nch == INT_MAX || nch < 0)
109 return E_OVERFLOW;
Tim Peters755ebea2002-07-08 06:32:09 +0000110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 current_capacity = XXXROUNDUP(nch);
112 required_capacity = XXXROUNDUP(nch + 1);
113 if (current_capacity < 0 || required_capacity < 0)
114 return E_OVERFLOW;
115 if (current_capacity < required_capacity) {
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700116 if ((size_t)required_capacity > SIZE_MAX / sizeof(node)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 return E_NOMEM;
118 }
119 n = n1->n_child;
120 n = (node *) PyObject_REALLOC(n,
121 required_capacity * sizeof(node));
122 if (n == NULL)
123 return E_NOMEM;
124 n1->n_child = n;
125 }
Tim Peters755ebea2002-07-08 06:32:09 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 n = &n1->n_child[n1->n_nchildren++];
128 n->n_type = type;
129 n->n_str = str;
130 n->n_lineno = lineno;
131 n->n_col_offset = col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000132 n->n_end_lineno = end_lineno; // this and below will be updates after all children are added.
133 n->n_end_col_offset = end_col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 n->n_nchildren = 0;
135 n->n_child = NULL;
136 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
Guido van Rossum03a24cd1990-11-18 17:37:06 +0000138
Guido van Rossum3f5da241990-12-20 15:06:42 +0000139/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +0000140static void freechildren(node *);
Jesus Ceae9c53182012-08-03 14:28:37 +0200141static Py_ssize_t sizeofchildren(node *n);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000142
143
144void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000145PyNode_Free(node *n)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 if (n != NULL) {
148 freechildren(n);
149 PyObject_FREE(n);
150 }
Guido van Rossum3f5da241990-12-20 15:06:42 +0000151}
152
Jesus Ceae9c53182012-08-03 14:28:37 +0200153Py_ssize_t
154_PyNode_SizeOf(node *n)
155{
156 Py_ssize_t res = 0;
157
158 if (n != NULL)
159 res = sizeof(node) + sizeofchildren(n);
160 return res;
161}
162
Guido van Rossum03a24cd1990-11-18 17:37:06 +0000163static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000164freechildren(node *n)
Guido van Rossum03a24cd1990-11-18 17:37:06 +0000165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 int i;
167 for (i = NCH(n); --i >= 0; )
168 freechildren(CHILD(n, i));
169 if (n->n_child != NULL)
170 PyObject_FREE(n->n_child);
171 if (STR(n) != NULL)
172 PyObject_FREE(STR(n));
Guido van Rossum03a24cd1990-11-18 17:37:06 +0000173}
Jesus Ceae9c53182012-08-03 14:28:37 +0200174
175static Py_ssize_t
176sizeofchildren(node *n)
177{
178 Py_ssize_t res = 0;
179 int i;
180 for (i = NCH(n); --i >= 0; )
181 res += sizeofchildren(CHILD(n, i));
182 if (n->n_child != NULL)
183 /* allocated size of n->n_child array */
184 res += XXXROUNDUP(NCH(n)) * sizeof(node);
185 if (STR(n) != NULL)
186 res += strlen(STR(n)) + 1;
187 return res;
188}