ada: Rename Is_Constr_Subt_For_UN_Aliased flag
[official-gcc.git] / gcc / fortran / constructor.cc
blob674d8b1c77b2670eb641e7bd69f21a3ab5511662
1 /* Array and structure constructors
2 Copyright (C) 2009-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "gfortran.h"
24 #include "constructor.h"
27 static void
28 node_free (splay_tree_value value)
30 gfc_constructor *c = (gfc_constructor*)value;
32 if (c->expr)
33 gfc_free_expr (c->expr);
35 if (c->iterator)
36 gfc_free_iterator (c->iterator, 1);
38 mpz_clear (c->offset);
39 mpz_clear (c->repeat);
41 free (c);
45 static gfc_constructor *
46 node_copy (splay_tree_node node, void *base)
48 gfc_constructor *c, *src = (gfc_constructor*)node->value;
50 c = XCNEW (gfc_constructor);
51 c->base = (gfc_constructor_base)base;
52 c->expr = gfc_copy_expr (src->expr);
53 c->iterator = gfc_copy_iterator (src->iterator);
54 c->where = src->where;
55 c->n.component = src->n.component;
57 mpz_init_set (c->offset, src->offset);
58 mpz_init_set (c->repeat, src->repeat);
60 return c;
64 static int
65 node_copy_and_insert (splay_tree_node node, void *base)
67 int n = mpz_get_si (((gfc_constructor*)node->value)->offset);
68 gfc_constructor_insert ((gfc_constructor_base*)base,
69 node_copy (node, base), n);
70 return 0;
74 gfc_constructor *
75 gfc_constructor_get (void)
77 gfc_constructor *c = XCNEW (gfc_constructor);
78 c->base = NULL;
79 c->expr = NULL;
80 c->iterator = NULL;
82 mpz_init_set_si (c->offset, 0);
83 mpz_init_set_si (c->repeat, 1);
85 return c;
88 static gfc_constructor_base
89 gfc_constructor_get_base (void)
91 return splay_tree_new (splay_tree_compare_ints, NULL, node_free);
95 gfc_constructor_base
96 gfc_constructor_copy (gfc_constructor_base base)
98 gfc_constructor_base new_base;
100 if (!base)
101 return NULL;
103 new_base = gfc_constructor_get_base ();
104 splay_tree_foreach (base, node_copy_and_insert, &new_base);
106 return new_base;
110 void
111 gfc_constructor_free (gfc_constructor_base base)
113 if (base)
114 splay_tree_delete (base);
118 gfc_constructor *
119 gfc_constructor_append (gfc_constructor_base *base, gfc_constructor *c)
121 int offset = 0;
122 if (*base)
123 offset = (int)(splay_tree_max (*base)->key) + 1;
125 return gfc_constructor_insert (base, c, offset);
129 gfc_constructor *
130 gfc_constructor_append_expr (gfc_constructor_base *base,
131 gfc_expr *e, locus *where)
133 gfc_constructor *c = gfc_constructor_get ();
134 c->expr = e;
135 if (where)
136 c->where = *where;
138 return gfc_constructor_append (base, c);
142 gfc_constructor *
143 gfc_constructor_insert (gfc_constructor_base *base, gfc_constructor *c, int n)
145 splay_tree_node node;
147 if (*base == NULL)
148 *base = splay_tree_new (splay_tree_compare_ints, NULL, node_free);
150 c->base = *base;
151 mpz_set_si (c->offset, n);
153 node = splay_tree_insert (*base, (splay_tree_key) n, (splay_tree_value) c);
154 gcc_assert (node);
156 return (gfc_constructor*)node->value;
160 gfc_constructor *
161 gfc_constructor_insert_expr (gfc_constructor_base *base,
162 gfc_expr *e, locus *where, int n)
164 gfc_constructor *c = gfc_constructor_get ();
165 c->expr = e;
166 if (where)
167 c->where = *where;
169 return gfc_constructor_insert (base, c, n);
173 gfc_constructor *
174 gfc_constructor_lookup (gfc_constructor_base base, int offset)
176 gfc_constructor *c;
177 splay_tree_node node;
179 if (!base)
180 return NULL;
182 node = splay_tree_lookup (base, (splay_tree_key) offset);
183 if (node)
184 return (gfc_constructor *) node->value;
186 /* Check if the previous node has a repeat count big enough to
187 cover the offset looked for. */
188 node = splay_tree_predecessor (base, (splay_tree_key) offset);
189 if (!node)
190 return NULL;
192 c = (gfc_constructor *) node->value;
193 if (mpz_cmp_si (c->repeat, 1) > 0)
195 if (mpz_get_si (c->offset) + mpz_get_si (c->repeat) <= offset)
196 c = NULL;
198 else
199 c = NULL;
201 return c;
205 gfc_expr *
206 gfc_constructor_lookup_expr (gfc_constructor_base base, int offset)
208 gfc_constructor *c = gfc_constructor_lookup (base, offset);
209 return c ? c->expr : NULL;
213 gfc_constructor *
214 gfc_constructor_first (gfc_constructor_base base)
216 if (base)
218 splay_tree_node node = splay_tree_min (base);
219 return node ? (gfc_constructor*) node->value : NULL;
221 else
222 return NULL;
226 gfc_constructor *
227 gfc_constructor_next (gfc_constructor *ctor)
229 if (ctor)
231 splay_tree_node node = splay_tree_successor (ctor->base,
232 mpz_get_si (ctor->offset));
233 return node ? (gfc_constructor*) node->value : NULL;
235 else
236 return NULL;
240 void
241 gfc_constructor_remove (gfc_constructor *ctor)
243 if (ctor)
244 splay_tree_remove (ctor->base, mpz_get_si (ctor->offset));
248 gfc_constructor *
249 gfc_constructor_lookup_next (gfc_constructor_base base, int offset)
251 splay_tree_node node;
253 if (!base)
254 return NULL;
256 node = splay_tree_successor (base, (splay_tree_key) offset);
257 if (!node)
258 return NULL;
260 return (gfc_constructor *) node->value;