xref: /netbsd-src/external/gpl3/gcc/dist/gcc/fortran/st.cc (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1*b1e83836Smrg /* Build executable statement trees.
2*b1e83836Smrg    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3*b1e83836Smrg    Contributed by Andy Vaught
4*b1e83836Smrg 
5*b1e83836Smrg This file is part of GCC.
6*b1e83836Smrg 
7*b1e83836Smrg GCC is free software; you can redistribute it and/or modify it under
8*b1e83836Smrg the terms of the GNU General Public License as published by the Free
9*b1e83836Smrg Software Foundation; either version 3, or (at your option) any later
10*b1e83836Smrg version.
11*b1e83836Smrg 
12*b1e83836Smrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*b1e83836Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*b1e83836Smrg FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*b1e83836Smrg for more details.
16*b1e83836Smrg 
17*b1e83836Smrg You should have received a copy of the GNU General Public License
18*b1e83836Smrg along with GCC; see the file COPYING3.  If not see
19*b1e83836Smrg <http://www.gnu.org/licenses/>.  */
20*b1e83836Smrg 
21*b1e83836Smrg /* Executable statements are strung together into a singly linked list
22*b1e83836Smrg    of code structures.  These structures are later translated into GCC
23*b1e83836Smrg    GENERIC tree structures and from there to executable code for a
24*b1e83836Smrg    target.  */
25*b1e83836Smrg 
26*b1e83836Smrg #include "config.h"
27*b1e83836Smrg #include "system.h"
28*b1e83836Smrg #include "coretypes.h"
29*b1e83836Smrg #include "gfortran.h"
30*b1e83836Smrg 
31*b1e83836Smrg gfc_code new_st;
32*b1e83836Smrg 
33*b1e83836Smrg 
34*b1e83836Smrg /* Zeroes out the new_st structure.  */
35*b1e83836Smrg 
36*b1e83836Smrg void
gfc_clear_new_st(void)37*b1e83836Smrg gfc_clear_new_st (void)
38*b1e83836Smrg {
39*b1e83836Smrg   memset (&new_st, '\0', sizeof (new_st));
40*b1e83836Smrg   new_st.op = EXEC_NOP;
41*b1e83836Smrg }
42*b1e83836Smrg 
43*b1e83836Smrg 
44*b1e83836Smrg /* Get a gfc_code structure, initialized with the current locus
45*b1e83836Smrg    and a statement code 'op'.  */
46*b1e83836Smrg 
47*b1e83836Smrg gfc_code *
gfc_get_code(gfc_exec_op op)48*b1e83836Smrg gfc_get_code (gfc_exec_op op)
49*b1e83836Smrg {
50*b1e83836Smrg   gfc_code *c;
51*b1e83836Smrg 
52*b1e83836Smrg   c = XCNEW (gfc_code);
53*b1e83836Smrg   c->op = op;
54*b1e83836Smrg   c->loc = gfc_current_locus;
55*b1e83836Smrg   return c;
56*b1e83836Smrg }
57*b1e83836Smrg 
58*b1e83836Smrg 
59*b1e83836Smrg /* Given some part of a gfc_code structure, append a set of code to
60*b1e83836Smrg    its tail, returning a pointer to the new tail.  */
61*b1e83836Smrg 
62*b1e83836Smrg gfc_code *
gfc_append_code(gfc_code * tail,gfc_code * new_code)63*b1e83836Smrg gfc_append_code (gfc_code *tail, gfc_code *new_code)
64*b1e83836Smrg {
65*b1e83836Smrg   if (tail != NULL)
66*b1e83836Smrg     {
67*b1e83836Smrg       while (tail->next != NULL)
68*b1e83836Smrg 	tail = tail->next;
69*b1e83836Smrg 
70*b1e83836Smrg       tail->next = new_code;
71*b1e83836Smrg     }
72*b1e83836Smrg 
73*b1e83836Smrg   while (new_code->next != NULL)
74*b1e83836Smrg     new_code = new_code->next;
75*b1e83836Smrg 
76*b1e83836Smrg   return new_code;
77*b1e83836Smrg }
78*b1e83836Smrg 
79*b1e83836Smrg 
80*b1e83836Smrg /* Free a single code structure, but not the actual structure itself.  */
81*b1e83836Smrg 
82*b1e83836Smrg void
gfc_free_statement(gfc_code * p)83*b1e83836Smrg gfc_free_statement (gfc_code *p)
84*b1e83836Smrg {
85*b1e83836Smrg   if (p->expr1)
86*b1e83836Smrg     gfc_free_expr (p->expr1);
87*b1e83836Smrg   if (p->expr2)
88*b1e83836Smrg     gfc_free_expr (p->expr2);
89*b1e83836Smrg 
90*b1e83836Smrg   switch (p->op)
91*b1e83836Smrg     {
92*b1e83836Smrg     case EXEC_NOP:
93*b1e83836Smrg     case EXEC_END_BLOCK:
94*b1e83836Smrg     case EXEC_END_NESTED_BLOCK:
95*b1e83836Smrg     case EXEC_ASSIGN:
96*b1e83836Smrg     case EXEC_INIT_ASSIGN:
97*b1e83836Smrg     case EXEC_GOTO:
98*b1e83836Smrg     case EXEC_CYCLE:
99*b1e83836Smrg     case EXEC_RETURN:
100*b1e83836Smrg     case EXEC_END_PROCEDURE:
101*b1e83836Smrg     case EXEC_IF:
102*b1e83836Smrg     case EXEC_PAUSE:
103*b1e83836Smrg     case EXEC_STOP:
104*b1e83836Smrg     case EXEC_ERROR_STOP:
105*b1e83836Smrg     case EXEC_EXIT:
106*b1e83836Smrg     case EXEC_WHERE:
107*b1e83836Smrg     case EXEC_IOLENGTH:
108*b1e83836Smrg     case EXEC_POINTER_ASSIGN:
109*b1e83836Smrg     case EXEC_DO_WHILE:
110*b1e83836Smrg     case EXEC_CONTINUE:
111*b1e83836Smrg     case EXEC_TRANSFER:
112*b1e83836Smrg     case EXEC_LABEL_ASSIGN:
113*b1e83836Smrg     case EXEC_ENTRY:
114*b1e83836Smrg     case EXEC_ARITHMETIC_IF:
115*b1e83836Smrg     case EXEC_CRITICAL:
116*b1e83836Smrg     case EXEC_SYNC_ALL:
117*b1e83836Smrg     case EXEC_SYNC_IMAGES:
118*b1e83836Smrg     case EXEC_SYNC_MEMORY:
119*b1e83836Smrg     case EXEC_LOCK:
120*b1e83836Smrg     case EXEC_UNLOCK:
121*b1e83836Smrg     case EXEC_EVENT_POST:
122*b1e83836Smrg     case EXEC_EVENT_WAIT:
123*b1e83836Smrg     case EXEC_FAIL_IMAGE:
124*b1e83836Smrg     case EXEC_CHANGE_TEAM:
125*b1e83836Smrg     case EXEC_END_TEAM:
126*b1e83836Smrg     case EXEC_FORM_TEAM:
127*b1e83836Smrg     case EXEC_SYNC_TEAM:
128*b1e83836Smrg       break;
129*b1e83836Smrg 
130*b1e83836Smrg     case EXEC_BLOCK:
131*b1e83836Smrg       gfc_free_namespace (p->ext.block.ns);
132*b1e83836Smrg       gfc_free_association_list (p->ext.block.assoc);
133*b1e83836Smrg       break;
134*b1e83836Smrg 
135*b1e83836Smrg     case EXEC_COMPCALL:
136*b1e83836Smrg     case EXEC_CALL_PPC:
137*b1e83836Smrg     case EXEC_CALL:
138*b1e83836Smrg     case EXEC_ASSIGN_CALL:
139*b1e83836Smrg       gfc_free_actual_arglist (p->ext.actual);
140*b1e83836Smrg       break;
141*b1e83836Smrg 
142*b1e83836Smrg     case EXEC_SELECT:
143*b1e83836Smrg     case EXEC_SELECT_TYPE:
144*b1e83836Smrg     case EXEC_SELECT_RANK:
145*b1e83836Smrg       if (p->ext.block.case_list)
146*b1e83836Smrg 	gfc_free_case_list (p->ext.block.case_list);
147*b1e83836Smrg       break;
148*b1e83836Smrg 
149*b1e83836Smrg     case EXEC_DO:
150*b1e83836Smrg       gfc_free_iterator (p->ext.iterator, 1);
151*b1e83836Smrg       break;
152*b1e83836Smrg 
153*b1e83836Smrg     case EXEC_ALLOCATE:
154*b1e83836Smrg     case EXEC_DEALLOCATE:
155*b1e83836Smrg       gfc_free_alloc_list (p->ext.alloc.list);
156*b1e83836Smrg       break;
157*b1e83836Smrg 
158*b1e83836Smrg     case EXEC_OPEN:
159*b1e83836Smrg       gfc_free_open (p->ext.open);
160*b1e83836Smrg       break;
161*b1e83836Smrg 
162*b1e83836Smrg     case EXEC_CLOSE:
163*b1e83836Smrg       gfc_free_close (p->ext.close);
164*b1e83836Smrg       break;
165*b1e83836Smrg 
166*b1e83836Smrg     case EXEC_BACKSPACE:
167*b1e83836Smrg     case EXEC_ENDFILE:
168*b1e83836Smrg     case EXEC_REWIND:
169*b1e83836Smrg     case EXEC_FLUSH:
170*b1e83836Smrg       gfc_free_filepos (p->ext.filepos);
171*b1e83836Smrg       break;
172*b1e83836Smrg 
173*b1e83836Smrg     case EXEC_INQUIRE:
174*b1e83836Smrg       gfc_free_inquire (p->ext.inquire);
175*b1e83836Smrg       break;
176*b1e83836Smrg 
177*b1e83836Smrg     case EXEC_WAIT:
178*b1e83836Smrg       gfc_free_wait (p->ext.wait);
179*b1e83836Smrg       break;
180*b1e83836Smrg 
181*b1e83836Smrg     case EXEC_READ:
182*b1e83836Smrg     case EXEC_WRITE:
183*b1e83836Smrg       gfc_free_dt (p->ext.dt);
184*b1e83836Smrg       break;
185*b1e83836Smrg 
186*b1e83836Smrg     case EXEC_DT_END:
187*b1e83836Smrg       /* The ext.dt member is a duplicate pointer and doesn't need to
188*b1e83836Smrg 	 be freed.  */
189*b1e83836Smrg       break;
190*b1e83836Smrg 
191*b1e83836Smrg     case EXEC_DO_CONCURRENT:
192*b1e83836Smrg     case EXEC_FORALL:
193*b1e83836Smrg       gfc_free_forall_iterator (p->ext.forall_iterator);
194*b1e83836Smrg       break;
195*b1e83836Smrg 
196*b1e83836Smrg     case EXEC_OACC_DECLARE:
197*b1e83836Smrg       if (p->ext.oacc_declare)
198*b1e83836Smrg 	gfc_free_oacc_declare_clauses (p->ext.oacc_declare);
199*b1e83836Smrg       break;
200*b1e83836Smrg 
201*b1e83836Smrg     case EXEC_OACC_ATOMIC:
202*b1e83836Smrg     case EXEC_OACC_PARALLEL_LOOP:
203*b1e83836Smrg     case EXEC_OACC_PARALLEL:
204*b1e83836Smrg     case EXEC_OACC_KERNELS_LOOP:
205*b1e83836Smrg     case EXEC_OACC_KERNELS:
206*b1e83836Smrg     case EXEC_OACC_SERIAL_LOOP:
207*b1e83836Smrg     case EXEC_OACC_SERIAL:
208*b1e83836Smrg     case EXEC_OACC_DATA:
209*b1e83836Smrg     case EXEC_OACC_HOST_DATA:
210*b1e83836Smrg     case EXEC_OACC_LOOP:
211*b1e83836Smrg     case EXEC_OACC_UPDATE:
212*b1e83836Smrg     case EXEC_OACC_WAIT:
213*b1e83836Smrg     case EXEC_OACC_CACHE:
214*b1e83836Smrg     case EXEC_OACC_ENTER_DATA:
215*b1e83836Smrg     case EXEC_OACC_EXIT_DATA:
216*b1e83836Smrg     case EXEC_OACC_ROUTINE:
217*b1e83836Smrg     case EXEC_OMP_ATOMIC:
218*b1e83836Smrg     case EXEC_OMP_CANCEL:
219*b1e83836Smrg     case EXEC_OMP_CANCELLATION_POINT:
220*b1e83836Smrg     case EXEC_OMP_CRITICAL:
221*b1e83836Smrg     case EXEC_OMP_DEPOBJ:
222*b1e83836Smrg     case EXEC_OMP_DISTRIBUTE:
223*b1e83836Smrg     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
224*b1e83836Smrg     case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
225*b1e83836Smrg     case EXEC_OMP_DISTRIBUTE_SIMD:
226*b1e83836Smrg     case EXEC_OMP_DO:
227*b1e83836Smrg     case EXEC_OMP_DO_SIMD:
228*b1e83836Smrg     case EXEC_OMP_ERROR:
229*b1e83836Smrg     case EXEC_OMP_LOOP:
230*b1e83836Smrg     case EXEC_OMP_END_SINGLE:
231*b1e83836Smrg     case EXEC_OMP_MASKED_TASKLOOP:
232*b1e83836Smrg     case EXEC_OMP_MASKED_TASKLOOP_SIMD:
233*b1e83836Smrg     case EXEC_OMP_MASTER_TASKLOOP:
234*b1e83836Smrg     case EXEC_OMP_MASTER_TASKLOOP_SIMD:
235*b1e83836Smrg     case EXEC_OMP_ORDERED:
236*b1e83836Smrg     case EXEC_OMP_MASKED:
237*b1e83836Smrg     case EXEC_OMP_PARALLEL:
238*b1e83836Smrg     case EXEC_OMP_PARALLEL_DO:
239*b1e83836Smrg     case EXEC_OMP_PARALLEL_DO_SIMD:
240*b1e83836Smrg     case EXEC_OMP_PARALLEL_LOOP:
241*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASKED:
242*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
243*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
244*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASTER:
245*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
246*b1e83836Smrg     case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
247*b1e83836Smrg     case EXEC_OMP_PARALLEL_SECTIONS:
248*b1e83836Smrg     case EXEC_OMP_PARALLEL_WORKSHARE:
249*b1e83836Smrg     case EXEC_OMP_SCAN:
250*b1e83836Smrg     case EXEC_OMP_SCOPE:
251*b1e83836Smrg     case EXEC_OMP_SECTIONS:
252*b1e83836Smrg     case EXEC_OMP_SIMD:
253*b1e83836Smrg     case EXEC_OMP_SINGLE:
254*b1e83836Smrg     case EXEC_OMP_TARGET:
255*b1e83836Smrg     case EXEC_OMP_TARGET_DATA:
256*b1e83836Smrg     case EXEC_OMP_TARGET_ENTER_DATA:
257*b1e83836Smrg     case EXEC_OMP_TARGET_EXIT_DATA:
258*b1e83836Smrg     case EXEC_OMP_TARGET_PARALLEL:
259*b1e83836Smrg     case EXEC_OMP_TARGET_PARALLEL_DO:
260*b1e83836Smrg     case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
261*b1e83836Smrg     case EXEC_OMP_TARGET_PARALLEL_LOOP:
262*b1e83836Smrg     case EXEC_OMP_TARGET_SIMD:
263*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS:
264*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
265*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
266*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
267*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
268*b1e83836Smrg     case EXEC_OMP_TARGET_TEAMS_LOOP:
269*b1e83836Smrg     case EXEC_OMP_TARGET_UPDATE:
270*b1e83836Smrg     case EXEC_OMP_TASK:
271*b1e83836Smrg     case EXEC_OMP_TASKLOOP:
272*b1e83836Smrg     case EXEC_OMP_TASKLOOP_SIMD:
273*b1e83836Smrg     case EXEC_OMP_TEAMS:
274*b1e83836Smrg     case EXEC_OMP_TEAMS_DISTRIBUTE:
275*b1e83836Smrg     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
276*b1e83836Smrg     case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
277*b1e83836Smrg     case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
278*b1e83836Smrg     case EXEC_OMP_TEAMS_LOOP:
279*b1e83836Smrg     case EXEC_OMP_WORKSHARE:
280*b1e83836Smrg       gfc_free_omp_clauses (p->ext.omp_clauses);
281*b1e83836Smrg       break;
282*b1e83836Smrg 
283*b1e83836Smrg     case EXEC_OMP_END_CRITICAL:
284*b1e83836Smrg       free (CONST_CAST (char *, p->ext.omp_name));
285*b1e83836Smrg       break;
286*b1e83836Smrg 
287*b1e83836Smrg     case EXEC_OMP_FLUSH:
288*b1e83836Smrg       gfc_free_omp_namelist (p->ext.omp_namelist, false);
289*b1e83836Smrg       break;
290*b1e83836Smrg 
291*b1e83836Smrg     case EXEC_OMP_BARRIER:
292*b1e83836Smrg     case EXEC_OMP_MASTER:
293*b1e83836Smrg     case EXEC_OMP_END_NOWAIT:
294*b1e83836Smrg     case EXEC_OMP_TASKGROUP:
295*b1e83836Smrg     case EXEC_OMP_TASKWAIT:
296*b1e83836Smrg     case EXEC_OMP_TASKYIELD:
297*b1e83836Smrg       break;
298*b1e83836Smrg 
299*b1e83836Smrg     default:
300*b1e83836Smrg       gfc_internal_error ("gfc_free_statement(): Bad statement");
301*b1e83836Smrg     }
302*b1e83836Smrg }
303*b1e83836Smrg 
304*b1e83836Smrg 
305*b1e83836Smrg /* Free a code statement and all other code structures linked to it.  */
306*b1e83836Smrg 
307*b1e83836Smrg void
gfc_free_statements(gfc_code * p)308*b1e83836Smrg gfc_free_statements (gfc_code *p)
309*b1e83836Smrg {
310*b1e83836Smrg   gfc_code *q;
311*b1e83836Smrg 
312*b1e83836Smrg   for (; p; p = q)
313*b1e83836Smrg     {
314*b1e83836Smrg       q = p->next;
315*b1e83836Smrg 
316*b1e83836Smrg       if (p->block)
317*b1e83836Smrg 	gfc_free_statements (p->block);
318*b1e83836Smrg       gfc_free_statement (p);
319*b1e83836Smrg       free (p);
320*b1e83836Smrg     }
321*b1e83836Smrg }
322*b1e83836Smrg 
323*b1e83836Smrg 
324*b1e83836Smrg /* Free an association list (of an ASSOCIATE statement).  */
325*b1e83836Smrg 
326*b1e83836Smrg void
gfc_free_association_list(gfc_association_list * assoc)327*b1e83836Smrg gfc_free_association_list (gfc_association_list* assoc)
328*b1e83836Smrg {
329*b1e83836Smrg   if (!assoc)
330*b1e83836Smrg     return;
331*b1e83836Smrg 
332*b1e83836Smrg   gfc_free_association_list (assoc->next);
333*b1e83836Smrg   free (assoc);
334*b1e83836Smrg }
335