Branch data Line data Source code
1 : : /* Maintain binary trees of symbols.
2 : : Copyright (C) 2000-2025 Free Software Foundation, Inc.
3 : : Contributed by Andy Vaught
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "options.h"
26 : : #include "gfortran.h"
27 : : #include "parse.h"
28 : : #include "match.h"
29 : : #include "constructor.h"
30 : :
31 : :
32 : : /* Strings for all symbol attributes. We use these for dumping the
33 : : parse tree, in error messages, and also when reading and writing
34 : : modules. */
35 : :
36 : : const mstring flavors[] =
37 : : {
38 : : minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
39 : : minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
40 : : minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
41 : : minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
42 : : minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
43 : : minit ("UNION", FL_UNION), minit ("STRUCTURE", FL_STRUCT),
44 : : minit (NULL, -1)
45 : : };
46 : :
47 : : const mstring procedures[] =
48 : : {
49 : : minit ("UNKNOWN-PROC", PROC_UNKNOWN),
50 : : minit ("MODULE-PROC", PROC_MODULE),
51 : : minit ("INTERNAL-PROC", PROC_INTERNAL),
52 : : minit ("DUMMY-PROC", PROC_DUMMY),
53 : : minit ("INTRINSIC-PROC", PROC_INTRINSIC),
54 : : minit ("EXTERNAL-PROC", PROC_EXTERNAL),
55 : : minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
56 : : minit (NULL, -1)
57 : : };
58 : :
59 : : const mstring intents[] =
60 : : {
61 : : minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
62 : : minit ("IN", INTENT_IN),
63 : : minit ("OUT", INTENT_OUT),
64 : : minit ("INOUT", INTENT_INOUT),
65 : : minit (NULL, -1)
66 : : };
67 : :
68 : : const mstring access_types[] =
69 : : {
70 : : minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
71 : : minit ("PUBLIC", ACCESS_PUBLIC),
72 : : minit ("PRIVATE", ACCESS_PRIVATE),
73 : : minit (NULL, -1)
74 : : };
75 : :
76 : : const mstring ifsrc_types[] =
77 : : {
78 : : minit ("UNKNOWN", IFSRC_UNKNOWN),
79 : : minit ("DECL", IFSRC_DECL),
80 : : minit ("BODY", IFSRC_IFBODY)
81 : : };
82 : :
83 : : const mstring save_status[] =
84 : : {
85 : : minit ("UNKNOWN", SAVE_NONE),
86 : : minit ("EXPLICIT-SAVE", SAVE_EXPLICIT),
87 : : minit ("IMPLICIT-SAVE", SAVE_IMPLICIT),
88 : : };
89 : :
90 : : /* Set the mstrings for DTIO procedure names. */
91 : : const mstring dtio_procs[] =
92 : : {
93 : : minit ("_dtio_formatted_read", DTIO_RF),
94 : : minit ("_dtio_formatted_write", DTIO_WF),
95 : : minit ("_dtio_unformatted_read", DTIO_RUF),
96 : : minit ("_dtio_unformatted_write", DTIO_WUF),
97 : : };
98 : :
99 : : /* This is to make sure the backend generates setup code in the correct
100 : : order. */
101 : : static int next_decl_order = 1;
102 : :
103 : : gfc_namespace *gfc_current_ns;
104 : : gfc_namespace *gfc_global_ns_list;
105 : :
106 : : gfc_gsymbol *gfc_gsym_root = NULL;
107 : :
108 : : gfc_symbol *gfc_derived_types;
109 : :
110 : : static gfc_undo_change_set default_undo_chgset_var = { vNULL, vNULL, NULL };
111 : : static gfc_undo_change_set *latest_undo_chgset = &default_undo_chgset_var;
112 : :
113 : :
114 : : /*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
115 : :
116 : : /* The following static variable indicates whether a particular element has
117 : : been explicitly set or not. */
118 : :
119 : : static int new_flag[GFC_LETTERS];
120 : :
121 : :
122 : : /* Handle a correctly parsed IMPLICIT NONE. */
123 : :
124 : : void
125 : 22971 : gfc_set_implicit_none (bool type, bool external, locus *loc)
126 : : {
127 : 22971 : int i;
128 : :
129 : 22971 : if (external)
130 : 1056 : gfc_current_ns->has_implicit_none_export = 1;
131 : :
132 : 22971 : if (type)
133 : : {
134 : 22960 : gfc_current_ns->seen_implicit_none = 1;
135 : 619869 : for (i = 0; i < GFC_LETTERS; i++)
136 : : {
137 : 596911 : if (gfc_current_ns->set_flag[i])
138 : : {
139 : 2 : gfc_error_now ("IMPLICIT NONE (type) statement at %L following an "
140 : : "IMPLICIT statement", loc);
141 : 2 : return;
142 : : }
143 : 596909 : gfc_clear_ts (&gfc_current_ns->default_type[i]);
144 : 596909 : gfc_current_ns->set_flag[i] = 1;
145 : : }
146 : : }
147 : : }
148 : :
149 : :
150 : : /* Reset the implicit range flags. */
151 : :
152 : : void
153 : 23581 : gfc_clear_new_implicit (void)
154 : : {
155 : 23581 : int i;
156 : :
157 : 636687 : for (i = 0; i < GFC_LETTERS; i++)
158 : 613106 : new_flag[i] = 0;
159 : 23581 : }
160 : :
161 : :
162 : : /* Prepare for a new implicit range. Sets flags in new_flag[]. */
163 : :
164 : : bool
165 : 654 : gfc_add_new_implicit_range (int c1, int c2)
166 : : {
167 : 654 : int i;
168 : :
169 : 654 : c1 -= 'a';
170 : 654 : c2 -= 'a';
171 : :
172 : 5723 : for (i = c1; i <= c2; i++)
173 : : {
174 : 5069 : if (new_flag[i])
175 : : {
176 : 0 : gfc_error ("Letter %qc already set in IMPLICIT statement at %C",
177 : : i + 'A');
178 : 0 : return false;
179 : : }
180 : :
181 : 5069 : new_flag[i] = 1;
182 : : }
183 : :
184 : : return true;
185 : : }
186 : :
187 : :
188 : : /* Add a matched implicit range for gfc_set_implicit(). Check if merging
189 : : the new implicit types back into the existing types will work. */
190 : :
191 : : bool
192 : 446 : gfc_merge_new_implicit (gfc_typespec *ts)
193 : : {
194 : 446 : int i;
195 : :
196 : 446 : if (gfc_current_ns->seen_implicit_none)
197 : : {
198 : 0 : gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
199 : 0 : return false;
200 : : }
201 : :
202 : 11996 : for (i = 0; i < GFC_LETTERS; i++)
203 : : {
204 : 11552 : if (new_flag[i])
205 : : {
206 : 5031 : if (gfc_current_ns->set_flag[i])
207 : : {
208 : 2 : gfc_error ("Letter %qc already has an IMPLICIT type at %C",
209 : : i + 'A');
210 : 2 : return false;
211 : : }
212 : :
213 : 5029 : gfc_current_ns->default_type[i] = *ts;
214 : 5029 : gfc_current_ns->implicit_loc[i] = gfc_current_locus;
215 : 5029 : gfc_current_ns->set_flag[i] = 1;
216 : : }
217 : : }
218 : : return true;
219 : : }
220 : :
221 : :
222 : : /* Given a symbol, return a pointer to the typespec for its default type. */
223 : :
224 : : gfc_typespec *
225 : 2899390 : gfc_get_default_type (const char *name, gfc_namespace *ns)
226 : : {
227 : 2899390 : char letter;
228 : :
229 : 2899390 : letter = name[0];
230 : :
231 : 2899390 : if (flag_allow_leading_underscore && letter == '_')
232 : 0 : gfc_fatal_error ("Option %<-fallow-leading-underscore%> is for use only by "
233 : : "gfortran developers, and should not be used for "
234 : : "implicitly typed variables");
235 : :
236 : 2899390 : if (letter < 'a' || letter > 'z')
237 : 0 : gfc_internal_error ("gfc_get_default_type(): Bad symbol %qs", name);
238 : :
239 : 2899390 : if (ns == NULL)
240 : 270973 : ns = gfc_current_ns;
241 : :
242 : 2899390 : return &ns->default_type[letter - 'a'];
243 : : }
244 : :
245 : :
246 : : /* Recursively append candidate SYM to CANDIDATES. Store the number of
247 : : candidates in CANDIDATES_LEN. */
248 : :
249 : : static void
250 : 529 : lookup_symbol_fuzzy_find_candidates (gfc_symtree *sym,
251 : : char **&candidates,
252 : : size_t &candidates_len)
253 : : {
254 : 917 : gfc_symtree *p;
255 : :
256 : 917 : if (sym == NULL)
257 : : return;
258 : :
259 : 917 : if (sym->n.sym->ts.type != BT_UNKNOWN && sym->n.sym->ts.type != BT_PROCEDURE)
260 : 500 : vec_push (candidates, candidates_len, sym->name);
261 : 917 : p = sym->left;
262 : 917 : if (p)
263 : 400 : lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
264 : :
265 : 917 : p = sym->right;
266 : 917 : if (p)
267 : : lookup_symbol_fuzzy_find_candidates (p, candidates, candidates_len);
268 : : }
269 : :
270 : :
271 : : /* Lookup symbol SYM_NAME fuzzily, taking names in SYMBOL into account. */
272 : :
273 : : static const char*
274 : 129 : lookup_symbol_fuzzy (const char *sym_name, gfc_symbol *symbol)
275 : : {
276 : 129 : char **candidates = NULL;
277 : 129 : size_t candidates_len = 0;
278 : 129 : lookup_symbol_fuzzy_find_candidates (symbol->ns->sym_root, candidates,
279 : : candidates_len);
280 : 129 : return gfc_closest_fuzzy_match (sym_name, candidates);
281 : : }
282 : :
283 : :
284 : : /* Given a pointer to a symbol, set its type according to the first
285 : : letter of its name. Fails if the letter in question has no default
286 : : type. */
287 : :
288 : : bool
289 : 112734 : gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
290 : : {
291 : 112734 : gfc_typespec *ts;
292 : 112734 : gfc_expr *e;
293 : :
294 : : /* Check to see if a function selector of unknown type can be resolved. */
295 : 112734 : if (sym->assoc
296 : 14 : && (e = sym->assoc->target)
297 : 112748 : && e->expr_type == EXPR_FUNCTION)
298 : : {
299 : 1 : if (e->ts.type == BT_UNKNOWN)
300 : 1 : gfc_resolve_expr (e);
301 : 1 : sym->ts = e->ts;
302 : 1 : if (sym->ts.type != BT_UNKNOWN)
303 : : return true;
304 : : }
305 : :
306 : 112734 : if (sym->ts.type != BT_UNKNOWN)
307 : 0 : gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
308 : :
309 : 112734 : ts = gfc_get_default_type (sym->name, ns);
310 : :
311 : 112734 : if (ts->type == BT_UNKNOWN)
312 : : {
313 : 58560 : if (error_flag && !sym->attr.untyped && !gfc_query_suppress_errors ())
314 : : {
315 : 129 : const char *guessed = lookup_symbol_fuzzy (sym->name, sym);
316 : 129 : if (guessed)
317 : 21 : gfc_error ("Symbol %qs at %L has no IMPLICIT type"
318 : : "; did you mean %qs?",
319 : : sym->name, &sym->declared_at, guessed);
320 : : else
321 : 108 : gfc_error ("Symbol %qs at %L has no IMPLICIT type",
322 : : sym->name, &sym->declared_at);
323 : 129 : sym->attr.untyped = 1; /* Ensure we only give an error once. */
324 : : }
325 : :
326 : 58560 : return false;
327 : : }
328 : :
329 : 54174 : sym->ts = *ts;
330 : 54174 : sym->attr.implicit_type = 1;
331 : :
332 : 54174 : if (ts->type == BT_CHARACTER && ts->u.cl)
333 : 457 : sym->ts.u.cl = gfc_new_charlen (sym->ns, ts->u.cl);
334 : 53717 : else if (ts->type == BT_CLASS
335 : 53717 : && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
336 : : return false;
337 : :
338 : 54174 : if (sym->attr.is_bind_c == 1 && warn_c_binding_type)
339 : : {
340 : : /* BIND(C) variables should not be implicitly declared. */
341 : 1 : gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared BIND(C) "
342 : : "variable %qs at %L may not be C interoperable",
343 : : sym->name, &sym->declared_at);
344 : 1 : sym->ts.f90_type = sym->ts.type;
345 : : }
346 : :
347 : 54174 : if (sym->attr.dummy != 0)
348 : : {
349 : 4343 : if (sym->ns->proc_name != NULL
350 : 4342 : && (sym->ns->proc_name->attr.subroutine != 0
351 : 398 : || sym->ns->proc_name->attr.function != 0)
352 : 4342 : && sym->ns->proc_name->attr.is_bind_c != 0
353 : 56 : && warn_c_binding_type)
354 : : {
355 : : /* Dummy args to a BIND(C) routine may not be interoperable if
356 : : they are implicitly typed. */
357 : 1 : gfc_warning_now (OPT_Wc_binding_type, "Implicitly declared variable "
358 : : "%qs at %L may not be C interoperable but it is a "
359 : : "dummy argument to the BIND(C) procedure %qs at %L",
360 : : sym->name, &(sym->declared_at),
361 : : sym->ns->proc_name->name,
362 : : &(sym->ns->proc_name->declared_at));
363 : 1 : sym->ts.f90_type = sym->ts.type;
364 : : }
365 : : }
366 : :
367 : : return true;
368 : : }
369 : :
370 : :
371 : : /* This function is called from parse.cc(parse_progunit) to check the
372 : : type of the function is not implicitly typed in the host namespace
373 : : and to implicitly type the function result, if necessary. */
374 : :
375 : : void
376 : 12506 : gfc_check_function_type (gfc_namespace *ns)
377 : : {
378 : 12506 : gfc_symbol *proc = ns->proc_name;
379 : :
380 : 12506 : if (!proc->attr.contained || proc->result->attr.implicit_type)
381 : : return;
382 : :
383 : 9775 : if (proc->result->ts.type == BT_UNKNOWN && proc->result->ts.interface == NULL)
384 : : {
385 : 100 : if (gfc_set_default_type (proc->result, 0, gfc_current_ns))
386 : : {
387 : 80 : if (proc->result != proc)
388 : : {
389 : 16 : proc->ts = proc->result->ts;
390 : 16 : proc->as = gfc_copy_array_spec (proc->result->as);
391 : 16 : proc->attr.dimension = proc->result->attr.dimension;
392 : 16 : proc->attr.pointer = proc->result->attr.pointer;
393 : 16 : proc->attr.allocatable = proc->result->attr.allocatable;
394 : : }
395 : : }
396 : 20 : else if (!proc->result->attr.proc_pointer)
397 : : {
398 : 2 : gfc_error ("Function result %qs at %L has no IMPLICIT type",
399 : : proc->result->name, &proc->result->declared_at);
400 : 2 : proc->result->attr.untyped = 1;
401 : : }
402 : : }
403 : : }
404 : :
405 : :
406 : : /******************** Symbol attribute stuff *********************/
407 : :
408 : : /* Older standards produced conflicts for some attributes that are allowed
409 : : in newer standards. Check for the conflict and issue an error depending
410 : : on the standard in play. */
411 : :
412 : : static bool
413 : 17033 : conflict_std (int standard, const char *a1, const char *a2, const char *name,
414 : : locus *where)
415 : : {
416 : 17033 : if (name == NULL)
417 : : {
418 : 10185 : return gfc_notify_std (standard, "%s attribute conflicts "
419 : : "with %s attribute at %L", a1, a2,
420 : 10185 : where);
421 : : }
422 : : else
423 : : {
424 : 6848 : return gfc_notify_std (standard, "%s attribute conflicts "
425 : : "with %s attribute in %qs at %L",
426 : 6848 : a1, a2, name, where);
427 : : }
428 : : }
429 : :
430 : : /* This is a generic conflict-checker. We do this to avoid having a
431 : : single conflict in two places. */
432 : :
433 : : #define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
434 : : #define conf2(a) if (attr->a) { a2 = a; goto conflict; }
435 : : #define conf_std(a, b, std) if (attr->a && attr->b \
436 : : && !conflict_std (std, a, b, name, where)) \
437 : : return false;
438 : :
439 : : bool
440 : 6816467 : gfc_check_conflict (symbol_attribute *attr, const char *name, locus *where)
441 : : {
442 : 6816467 : static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
443 : : *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
444 : : *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
445 : : *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
446 : : *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
447 : : *privat = "PRIVATE", *recursive = "RECURSIVE",
448 : : *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
449 : : *publik = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
450 : : *function = "FUNCTION", *subroutine = "SUBROUTINE",
451 : : *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
452 : : *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
453 : : *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
454 : : *volatile_ = "VOLATILE", *is_protected = "PROTECTED",
455 : : *is_bind_c = "BIND(C)", *procedure = "PROCEDURE",
456 : : *proc_pointer = "PROCEDURE POINTER", *abstract = "ABSTRACT",
457 : : *asynchronous = "ASYNCHRONOUS", *codimension = "CODIMENSION",
458 : : *contiguous = "CONTIGUOUS", *generic = "GENERIC", *automatic = "AUTOMATIC",
459 : : *pdt_len = "LEN", *pdt_kind = "KIND";
460 : 6816467 : static const char *threadprivate = "THREADPRIVATE";
461 : 6816467 : static const char *omp_declare_target = "OMP DECLARE TARGET";
462 : 6816467 : static const char *omp_declare_target_link = "OMP DECLARE TARGET LINK";
463 : 6816467 : static const char *oacc_declare_copyin = "OACC DECLARE COPYIN";
464 : 6816467 : static const char *oacc_declare_create = "OACC DECLARE CREATE";
465 : 6816467 : static const char *oacc_declare_deviceptr = "OACC DECLARE DEVICEPTR";
466 : 6816467 : static const char *oacc_declare_device_resident =
467 : : "OACC DECLARE DEVICE_RESIDENT";
468 : :
469 : 6816467 : const char *a1, *a2;
470 : :
471 : 6816467 : if (attr->artificial)
472 : : return true;
473 : :
474 : 6816449 : if (where == NULL)
475 : 4472284 : where = &gfc_current_locus;
476 : :
477 : 6816449 : if (attr->pointer && attr->intent != INTENT_UNKNOWN)
478 : 4331 : conf_std (pointer, intent, GFC_STD_F2003);
479 : :
480 : 6816448 : conf_std (in_namelist, allocatable, GFC_STD_F2003);
481 : 6816448 : conf_std (in_namelist, pointer, GFC_STD_F2003);
482 : :
483 : : /* Check for attributes not allowed in a BLOCK DATA. */
484 : 6816447 : if (gfc_current_state () == COMP_BLOCK_DATA)
485 : : {
486 : 3413 : a1 = NULL;
487 : :
488 : 3413 : if (attr->in_namelist)
489 : 1 : a1 = in_namelist;
490 : 3413 : if (attr->allocatable)
491 : 0 : a1 = allocatable;
492 : 3413 : if (attr->external)
493 : 0 : a1 = external;
494 : 3413 : if (attr->optional)
495 : 0 : a1 = optional;
496 : 3413 : if (attr->access == ACCESS_PRIVATE)
497 : 0 : a1 = privat;
498 : 3413 : if (attr->access == ACCESS_PUBLIC)
499 : 0 : a1 = publik;
500 : 3413 : if (attr->intent != INTENT_UNKNOWN)
501 : 0 : a1 = intent;
502 : :
503 : 3413 : if (a1 != NULL)
504 : : {
505 : 1 : gfc_error
506 : 1 : ("%s attribute not allowed in BLOCK DATA program unit at %L",
507 : : a1, where);
508 : 1 : return false;
509 : : }
510 : : }
511 : :
512 : 6816446 : if (attr->save == SAVE_EXPLICIT)
513 : : {
514 : 6459 : conf (dummy, save);
515 : 6457 : conf (in_common, save);
516 : 6449 : conf (result, save);
517 : 6446 : conf (automatic, save);
518 : :
519 : 6444 : switch (attr->flavor)
520 : : {
521 : 2 : case FL_PROGRAM:
522 : 2 : case FL_BLOCK_DATA:
523 : 2 : case FL_MODULE:
524 : 2 : case FL_LABEL:
525 : 2 : case_fl_struct:
526 : 2 : case FL_PARAMETER:
527 : 2 : a1 = gfc_code2string (flavors, attr->flavor);
528 : 2 : a2 = save;
529 : 2 : goto conflict;
530 : 2 : case FL_NAMELIST:
531 : 2 : gfc_error ("Namelist group name at %L cannot have the "
532 : : "SAVE attribute", where);
533 : 2 : return false;
534 : : case FL_PROCEDURE:
535 : : /* Conflicts between SAVE and PROCEDURE will be checked at
536 : : resolution stage, see "resolve_fl_procedure". */
537 : : case FL_VARIABLE:
538 : : default:
539 : : break;
540 : : }
541 : : }
542 : :
543 : : /* The copying of procedure dummy arguments for module procedures in
544 : : a submodule occur whilst the current state is COMP_CONTAINS. It
545 : : is necessary, therefore, to let this through. */
546 : 6816427 : if (name && attr->dummy
547 : 254189 : && (attr->function || attr->subroutine)
548 : 1653 : && gfc_current_state () == COMP_CONTAINS
549 : 13 : && !(gfc_new_block && gfc_new_block->abr_modproc_decl))
550 : 3 : gfc_error_now ("internal procedure %qs at %L conflicts with "
551 : : "DUMMY argument", name, where);
552 : :
553 : 6816427 : conf (dummy, entry);
554 : 6816425 : conf (dummy, intrinsic);
555 : 6816424 : conf (dummy, threadprivate);
556 : 6816424 : conf (dummy, omp_declare_target);
557 : 6816424 : conf (dummy, omp_declare_target_link);
558 : 6816424 : conf (pointer, target);
559 : 6816424 : conf (pointer, intrinsic);
560 : 6816424 : conf (pointer, elemental);
561 : 6816422 : conf (pointer, codimension);
562 : 6816391 : conf (allocatable, elemental);
563 : :
564 : 6816390 : conf (in_common, automatic);
565 : 6816384 : conf (result, automatic);
566 : 6816382 : conf (use_assoc, automatic);
567 : 6816382 : conf (dummy, automatic);
568 : :
569 : 6816380 : conf (target, external);
570 : 6816380 : conf (target, intrinsic);
571 : :
572 : 6816380 : if (!attr->if_source)
573 : 6715256 : conf (external, dimension); /* See Fortran 95's R504. */
574 : :
575 : 6816380 : conf (external, intrinsic);
576 : 6816378 : conf (entry, intrinsic);
577 : 6816377 : conf (abstract, intrinsic);
578 : :
579 : 6816374 : if ((attr->if_source == IFSRC_DECL && !attr->procedure) || attr->contained)
580 : 85168 : conf (external, subroutine);
581 : :
582 : 6816372 : if (attr->proc_pointer && !gfc_notify_std (GFC_STD_F2003,
583 : : "Procedure pointer at %C"))
584 : : return false;
585 : :
586 : 6816366 : conf (allocatable, pointer);
587 : 6816366 : conf_std (allocatable, dummy, GFC_STD_F2003);
588 : 6816366 : conf_std (allocatable, function, GFC_STD_F2003);
589 : 6816366 : conf_std (allocatable, result, GFC_STD_F2003);
590 : 6816366 : conf_std (elemental, recursive, GFC_STD_F2018);
591 : :
592 : 6816366 : conf (in_common, dummy);
593 : 6816366 : conf (in_common, allocatable);
594 : 6816366 : conf (in_common, codimension);
595 : 6816366 : conf (in_common, result);
596 : :
597 : 6816366 : conf (in_equivalence, use_assoc);
598 : 6816365 : conf (in_equivalence, codimension);
599 : 6816365 : conf (in_equivalence, dummy);
600 : 6816364 : conf (in_equivalence, target);
601 : 6816363 : conf (in_equivalence, pointer);
602 : 6816362 : conf (in_equivalence, function);
603 : 6816362 : conf (in_equivalence, result);
604 : 6816362 : conf (in_equivalence, entry);
605 : 6816362 : conf (in_equivalence, allocatable);
606 : 6816359 : conf (in_equivalence, threadprivate);
607 : 6816359 : conf (in_equivalence, omp_declare_target);
608 : 6816359 : conf (in_equivalence, omp_declare_target_link);
609 : 6816359 : conf (in_equivalence, oacc_declare_create);
610 : 6816359 : conf (in_equivalence, oacc_declare_copyin);
611 : 6816359 : conf (in_equivalence, oacc_declare_deviceptr);
612 : 6816359 : conf (in_equivalence, oacc_declare_device_resident);
613 : 6816359 : conf (in_equivalence, is_bind_c);
614 : :
615 : 6816358 : conf (dummy, result);
616 : 6816358 : conf (entry, result);
617 : 6816357 : conf (generic, result);
618 : 6816354 : conf (generic, omp_declare_target);
619 : 6816354 : conf (generic, omp_declare_target_link);
620 : :
621 : 6816354 : conf (function, subroutine);
622 : :
623 : 6816294 : if (!function && !subroutine)
624 : 0 : conf (is_bind_c, dummy);
625 : :
626 : 6816294 : conf (is_bind_c, cray_pointer);
627 : 6816294 : conf (is_bind_c, cray_pointee);
628 : 6816294 : conf (is_bind_c, codimension);
629 : 6816293 : conf (is_bind_c, allocatable);
630 : 6816292 : conf (is_bind_c, elemental);
631 : :
632 : : /* Need to also get volatile attr, according to 5.1 of F2003 draft.
633 : : Parameter conflict caught below. Also, value cannot be specified
634 : : for a dummy procedure. */
635 : :
636 : : /* Cray pointer/pointee conflicts. */
637 : 6816290 : conf (cray_pointer, cray_pointee);
638 : 6816289 : conf (cray_pointer, dimension);
639 : 6816288 : conf (cray_pointer, codimension);
640 : 6816288 : conf (cray_pointer, contiguous);
641 : 6816288 : conf (cray_pointer, pointer);
642 : 6816287 : conf (cray_pointer, target);
643 : 6816286 : conf (cray_pointer, allocatable);
644 : 6816286 : conf (cray_pointer, external);
645 : 6816286 : conf (cray_pointer, intrinsic);
646 : 6816286 : conf (cray_pointer, in_namelist);
647 : 6816286 : conf (cray_pointer, function);
648 : 6816286 : conf (cray_pointer, subroutine);
649 : 6816286 : conf (cray_pointer, entry);
650 : :
651 : 6816286 : conf (cray_pointee, allocatable);
652 : 6816286 : conf (cray_pointee, contiguous);
653 : 6816286 : conf (cray_pointee, codimension);
654 : 6816286 : conf (cray_pointee, intent);
655 : 6816286 : conf (cray_pointee, optional);
656 : 6816286 : conf (cray_pointee, dummy);
657 : 6816285 : conf (cray_pointee, target);
658 : 6816284 : conf (cray_pointee, intrinsic);
659 : 6816284 : conf (cray_pointee, pointer);
660 : 6816283 : conf (cray_pointee, entry);
661 : 6816283 : conf (cray_pointee, in_common);
662 : 6816280 : conf (cray_pointee, in_equivalence);
663 : 6816278 : conf (cray_pointee, threadprivate);
664 : 6816277 : conf (cray_pointee, omp_declare_target);
665 : 6816277 : conf (cray_pointee, omp_declare_target_link);
666 : 6816277 : conf (cray_pointee, oacc_declare_create);
667 : 6816277 : conf (cray_pointee, oacc_declare_copyin);
668 : 6816277 : conf (cray_pointee, oacc_declare_deviceptr);
669 : 6816277 : conf (cray_pointee, oacc_declare_device_resident);
670 : :
671 : 6816277 : conf (data, dummy);
672 : 6816274 : conf (data, function);
673 : 6816273 : conf (data, result);
674 : 6816272 : conf (data, allocatable);
675 : :
676 : 6816271 : conf (value, pointer)
677 : 6816270 : conf (value, allocatable)
678 : 6816270 : conf (value, subroutine)
679 : 6816270 : conf (value, function)
680 : 6816269 : conf (value, volatile_)
681 : 6816269 : conf (value, dimension)
682 : 6816265 : conf (value, codimension)
683 : 6816265 : conf (value, external)
684 : :
685 : 6816264 : conf (codimension, result)
686 : :
687 : 6816261 : if (attr->value
688 : 41103 : && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
689 : : {
690 : 4 : a1 = value;
691 : 4 : a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
692 : 4 : goto conflict;
693 : : }
694 : :
695 : 6816257 : conf (is_protected, intrinsic)
696 : 6816257 : conf (is_protected, in_common)
697 : :
698 : 6816253 : conf (asynchronous, intrinsic)
699 : 6816253 : conf (asynchronous, external)
700 : :
701 : 6816253 : conf (volatile_, intrinsic)
702 : 6816252 : conf (volatile_, external)
703 : :
704 : 6816251 : if (attr->volatile_ && attr->intent == INTENT_IN)
705 : : {
706 : 1 : a1 = volatile_;
707 : 1 : a2 = intent_in;
708 : 1 : goto conflict;
709 : : }
710 : :
711 : 6816250 : conf (procedure, allocatable)
712 : 6816249 : conf (procedure, dimension)
713 : 6816249 : conf (procedure, codimension)
714 : 6816249 : conf (procedure, intrinsic)
715 : 6816249 : conf (procedure, target)
716 : 6816249 : conf (procedure, value)
717 : 6816249 : conf (procedure, volatile_)
718 : 6816249 : conf (procedure, asynchronous)
719 : 6816249 : conf (procedure, entry)
720 : :
721 : 6816248 : conf (proc_pointer, abstract)
722 : 6816246 : conf (proc_pointer, omp_declare_target)
723 : 6816246 : conf (proc_pointer, omp_declare_target_link)
724 : :
725 : 6816246 : conf (entry, omp_declare_target)
726 : 6816246 : conf (entry, omp_declare_target_link)
727 : 6816246 : conf (entry, oacc_declare_create)
728 : 6816246 : conf (entry, oacc_declare_copyin)
729 : 6816246 : conf (entry, oacc_declare_deviceptr)
730 : 6816246 : conf (entry, oacc_declare_device_resident)
731 : :
732 : 6816246 : conf (pdt_kind, allocatable)
733 : 6816245 : conf (pdt_kind, pointer)
734 : 6816244 : conf (pdt_kind, dimension)
735 : 6816243 : conf (pdt_kind, codimension)
736 : :
737 : 6816243 : conf (pdt_len, allocatable)
738 : 6816242 : conf (pdt_len, pointer)
739 : 6816241 : conf (pdt_len, dimension)
740 : 6816240 : conf (pdt_len, codimension)
741 : 6816240 : conf (pdt_len, pdt_kind)
742 : :
743 : 6816238 : if (attr->access == ACCESS_PRIVATE)
744 : : {
745 : 2130 : a1 = privat;
746 : 2130 : conf2 (pdt_kind);
747 : 2129 : conf2 (pdt_len);
748 : : }
749 : :
750 : 6816236 : a1 = gfc_code2string (flavors, attr->flavor);
751 : :
752 : 6816236 : if (attr->in_namelist
753 : 4392 : && attr->flavor != FL_VARIABLE
754 : 1945 : && attr->flavor != FL_PROCEDURE
755 : 1936 : && attr->flavor != FL_UNKNOWN)
756 : : {
757 : 0 : a2 = in_namelist;
758 : 0 : goto conflict;
759 : : }
760 : :
761 : 6816236 : switch (attr->flavor)
762 : : {
763 : 161630 : case FL_PROGRAM:
764 : 161630 : case FL_BLOCK_DATA:
765 : 161630 : case FL_MODULE:
766 : 161630 : case FL_LABEL:
767 : 161630 : conf2 (codimension);
768 : 161630 : conf2 (dimension);
769 : 161629 : conf2 (dummy);
770 : 161629 : conf2 (volatile_);
771 : 161627 : conf2 (asynchronous);
772 : 161626 : conf2 (contiguous);
773 : 161626 : conf2 (pointer);
774 : 161626 : conf2 (is_protected);
775 : 161625 : conf2 (target);
776 : 161625 : conf2 (external);
777 : 161624 : conf2 (intrinsic);
778 : 161624 : conf2 (allocatable);
779 : 161624 : conf2 (result);
780 : 161624 : conf2 (in_namelist);
781 : 161624 : conf2 (optional);
782 : 161624 : conf2 (function);
783 : 161624 : conf2 (subroutine);
784 : 161623 : conf2 (threadprivate);
785 : 161623 : conf2 (omp_declare_target);
786 : 161623 : conf2 (omp_declare_target_link);
787 : 161623 : conf2 (oacc_declare_create);
788 : 161623 : conf2 (oacc_declare_copyin);
789 : 161623 : conf2 (oacc_declare_deviceptr);
790 : 161623 : conf2 (oacc_declare_device_resident);
791 : :
792 : 161623 : if (attr->access == ACCESS_PUBLIC || attr->access == ACCESS_PRIVATE)
793 : : {
794 : 2 : a2 = attr->access == ACCESS_PUBLIC ? publik : privat;
795 : 2 : gfc_error ("%s attribute applied to %s %s at %L", a2, a1,
796 : : name, where);
797 : 2 : return false;
798 : : }
799 : :
800 : 161621 : if (attr->is_bind_c)
801 : : {
802 : 2 : gfc_error_now ("BIND(C) applied to %s %s at %L", a1, name, where);
803 : 2 : return false;
804 : : }
805 : :
806 : : break;
807 : :
808 : : case FL_VARIABLE:
809 : : break;
810 : :
811 : 758 : case FL_NAMELIST:
812 : 758 : conf2 (result);
813 : : break;
814 : :
815 : 4242807 : case FL_PROCEDURE:
816 : : /* Conflicts with INTENT, SAVE and RESULT will be checked
817 : : at resolution stage, see "resolve_fl_procedure". */
818 : :
819 : 4242807 : if (attr->subroutine)
820 : : {
821 : 110429 : a1 = subroutine;
822 : 110429 : conf2 (target);
823 : 110429 : conf2 (allocatable);
824 : 110429 : conf2 (volatile_);
825 : 110428 : conf2 (asynchronous);
826 : 110427 : conf2 (in_namelist);
827 : 110427 : conf2 (codimension);
828 : 110427 : conf2 (dimension);
829 : 110426 : conf2 (function);
830 : 110426 : if (!attr->proc_pointer)
831 : 110244 : conf2 (threadprivate);
832 : : }
833 : :
834 : : /* Procedure pointers in COMMON blocks are allowed in F03,
835 : : * but forbidden per F08:C5100. */
836 : 4242804 : if (!attr->proc_pointer || (gfc_option.allow_std & GFC_STD_F2008))
837 : 4242634 : conf2 (in_common);
838 : :
839 : 4242800 : conf2 (omp_declare_target_link);
840 : :
841 : 4242798 : switch (attr->proc)
842 : : {
843 : 811357 : case PROC_ST_FUNCTION:
844 : 811357 : conf2 (dummy);
845 : 811356 : conf2 (target);
846 : : break;
847 : :
848 : 51163 : case PROC_MODULE:
849 : 51163 : conf2 (dummy);
850 : : break;
851 : :
852 : 0 : case PROC_DUMMY:
853 : 0 : conf2 (result);
854 : 0 : conf2 (threadprivate);
855 : : break;
856 : :
857 : : default:
858 : : break;
859 : : }
860 : :
861 : : break;
862 : :
863 : 35037 : case_fl_struct:
864 : 35037 : conf2 (dummy);
865 : 35037 : conf2 (pointer);
866 : 35037 : conf2 (target);
867 : 35037 : conf2 (external);
868 : 35037 : conf2 (intrinsic);
869 : 35037 : conf2 (allocatable);
870 : 35037 : conf2 (optional);
871 : 35037 : conf2 (entry);
872 : 35037 : conf2 (function);
873 : 35037 : conf2 (subroutine);
874 : 35037 : conf2 (threadprivate);
875 : 35037 : conf2 (result);
876 : 35037 : conf2 (omp_declare_target);
877 : 35037 : conf2 (omp_declare_target_link);
878 : 35037 : conf2 (oacc_declare_create);
879 : 35037 : conf2 (oacc_declare_copyin);
880 : 35037 : conf2 (oacc_declare_deviceptr);
881 : 35037 : conf2 (oacc_declare_device_resident);
882 : :
883 : 35037 : if (attr->intent != INTENT_UNKNOWN)
884 : : {
885 : 0 : a2 = intent;
886 : 0 : goto conflict;
887 : : }
888 : : break;
889 : :
890 : 38494 : case FL_PARAMETER:
891 : 38494 : conf2 (external);
892 : 38494 : conf2 (intrinsic);
893 : 38494 : conf2 (optional);
894 : 38494 : conf2 (allocatable);
895 : 38494 : conf2 (function);
896 : 38494 : conf2 (subroutine);
897 : 38494 : conf2 (entry);
898 : 38494 : conf2 (contiguous);
899 : 38494 : conf2 (pointer);
900 : 38494 : conf2 (is_protected);
901 : 38494 : conf2 (target);
902 : 38494 : conf2 (dummy);
903 : 38494 : conf2 (in_common);
904 : 38494 : conf2 (value);
905 : 38493 : conf2 (volatile_);
906 : 38492 : conf2 (asynchronous);
907 : 38492 : conf2 (threadprivate);
908 : 38492 : conf2 (value);
909 : 38492 : conf2 (codimension);
910 : 38491 : conf2 (result);
911 : 38490 : if (!attr->is_iso_c)
912 : 38470 : conf2 (is_bind_c);
913 : : break;
914 : :
915 : : default:
916 : : break;
917 : : }
918 : :
919 : : return true;
920 : :
921 : 227 : conflict:
922 : 227 : if (name == NULL)
923 : 58 : gfc_error ("%s attribute conflicts with %s attribute at %L",
924 : : a1, a2, where);
925 : : else
926 : 169 : gfc_error ("%s attribute conflicts with %s attribute in %qs at %L",
927 : : a1, a2, name, where);
928 : :
929 : : return false;
930 : : }
931 : :
932 : : #undef conf
933 : : #undef conf2
934 : : #undef conf_std
935 : :
936 : :
937 : : /* Mark a symbol as referenced. */
938 : :
939 : : void
940 : 8148313 : gfc_set_sym_referenced (gfc_symbol *sym)
941 : : {
942 : 8148313 : if (sym->attr.referenced)
943 : : return;
944 : :
945 : 4069130 : sym->attr.referenced = 1;
946 : :
947 : : /* Remember the declaration order. */
948 : 4069130 : sym->decl_order = next_decl_order++;
949 : : }
950 : :
951 : :
952 : : /* Common subroutine called by attribute changing subroutines in order
953 : : to prevent them from changing a symbol that has been
954 : : use-associated. Returns zero if it is OK to change the symbol,
955 : : nonzero if not. */
956 : :
957 : : static int
958 : 2276097 : check_used (symbol_attribute *attr, const char *name, locus *where)
959 : : {
960 : :
961 : 2276097 : if (attr->use_assoc == 0)
962 : : return 0;
963 : :
964 : 59 : if (where == NULL)
965 : 32 : where = &gfc_current_locus;
966 : :
967 : 59 : if (name == NULL)
968 : 3 : gfc_error ("Cannot change attributes of USE-associated symbol at %L",
969 : : where);
970 : : else
971 : 56 : gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
972 : : name, where);
973 : :
974 : : return 1;
975 : : }
976 : :
977 : :
978 : : /* Generate an error because of a duplicate attribute. */
979 : :
980 : : static void
981 : 21 : duplicate_attr (const char *attr, locus *where)
982 : : {
983 : :
984 : 0 : if (where == NULL)
985 : 7 : where = &gfc_current_locus;
986 : :
987 : 0 : gfc_error ("Duplicate %s attribute specified at %L", attr, where);
988 : 0 : }
989 : :
990 : :
991 : : bool
992 : 2994 : gfc_add_ext_attribute (symbol_attribute *attr, ext_attr_id_t ext_attr,
993 : : locus *where ATTRIBUTE_UNUSED)
994 : : {
995 : 2994 : attr->ext_attr |= 1 << ext_attr;
996 : 2994 : return true;
997 : : }
998 : :
999 : :
1000 : : /* Called from decl.cc (attr_decl1) to check attributes, when declared
1001 : : separately. */
1002 : :
1003 : : bool
1004 : 10181 : gfc_add_attribute (symbol_attribute *attr, locus *where)
1005 : : {
1006 : 10181 : if (check_used (attr, NULL, where))
1007 : : return false;
1008 : :
1009 : 10181 : return gfc_check_conflict (attr, NULL, where);
1010 : : }
1011 : :
1012 : :
1013 : : bool
1014 : 35519 : gfc_add_allocatable (symbol_attribute *attr, locus *where)
1015 : : {
1016 : :
1017 : 35519 : if (check_used (attr, NULL, where))
1018 : : return false;
1019 : :
1020 : 35519 : if (attr->allocatable && ! gfc_submodule_procedure(attr))
1021 : : {
1022 : 1 : duplicate_attr ("ALLOCATABLE", where);
1023 : 1 : return false;
1024 : : }
1025 : :
1026 : 573 : if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1027 : 35607 : && !gfc_find_state (COMP_INTERFACE))
1028 : : {
1029 : 1 : gfc_error ("ALLOCATABLE specified outside of INTERFACE body at %L",
1030 : : where);
1031 : 1 : return false;
1032 : : }
1033 : :
1034 : 35517 : attr->allocatable = 1;
1035 : 35517 : return gfc_check_conflict (attr, NULL, where);
1036 : : }
1037 : :
1038 : :
1039 : : bool
1040 : 77 : gfc_add_automatic (symbol_attribute *attr, const char *name, locus *where)
1041 : : {
1042 : 77 : if (check_used (attr, name, where))
1043 : : return false;
1044 : :
1045 : 77 : if (attr->automatic && !gfc_notify_std (GFC_STD_LEGACY,
1046 : : "Duplicate AUTOMATIC attribute specified at %L", where))
1047 : : return false;
1048 : :
1049 : 77 : attr->automatic = 1;
1050 : 77 : return gfc_check_conflict (attr, name, where);
1051 : : }
1052 : :
1053 : :
1054 : : bool
1055 : 1383 : gfc_add_codimension (symbol_attribute *attr, const char *name, locus *where)
1056 : : {
1057 : :
1058 : 1383 : if (check_used (attr, name, where))
1059 : : return false;
1060 : :
1061 : 1383 : if (attr->codimension)
1062 : : {
1063 : 2 : duplicate_attr ("CODIMENSION", where);
1064 : 2 : return false;
1065 : : }
1066 : :
1067 : 6 : if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1068 : 1382 : && !gfc_find_state (COMP_INTERFACE))
1069 : : {
1070 : 0 : gfc_error ("CODIMENSION specified for %qs outside its INTERFACE body "
1071 : : "at %L", name, where);
1072 : 0 : return false;
1073 : : }
1074 : :
1075 : 1381 : attr->codimension = 1;
1076 : 1381 : return gfc_check_conflict (attr, name, where);
1077 : : }
1078 : :
1079 : :
1080 : : bool
1081 : 99992 : gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
1082 : : {
1083 : :
1084 : 99992 : if (check_used (attr, name, where))
1085 : : return false;
1086 : :
1087 : 99992 : if (attr->dimension && ! gfc_submodule_procedure(attr))
1088 : : {
1089 : 2 : duplicate_attr ("DIMENSION", where);
1090 : 2 : return false;
1091 : : }
1092 : :
1093 : 1240 : if (attr->flavor == FL_PROCEDURE && attr->if_source == IFSRC_IFBODY
1094 : 100229 : && !gfc_find_state (COMP_INTERFACE))
1095 : : {
1096 : 1 : gfc_error ("DIMENSION specified for %qs outside its INTERFACE body "
1097 : : "at %L", name, where);
1098 : 1 : return false;
1099 : : }
1100 : :
1101 : 99989 : attr->dimension = 1;
1102 : 99989 : return gfc_check_conflict (attr, name, where);
1103 : : }
1104 : :
1105 : :
1106 : : bool
1107 : 4321 : gfc_add_contiguous (symbol_attribute *attr, const char *name, locus *where)
1108 : : {
1109 : :
1110 : 4321 : if (check_used (attr, name, where))
1111 : : return false;
1112 : :
1113 : 4321 : if (attr->contiguous)
1114 : : {
1115 : 2 : duplicate_attr ("CONTIGUOUS", where);
1116 : 2 : return false;
1117 : : }
1118 : :
1119 : 4319 : attr->contiguous = 1;
1120 : 4319 : return gfc_check_conflict (attr, name, where);
1121 : : }
1122 : :
1123 : :
1124 : : bool
1125 : 19739 : gfc_add_external (symbol_attribute *attr, locus *where)
1126 : : {
1127 : :
1128 : 19739 : if (check_used (attr, NULL, where))
1129 : : return false;
1130 : :
1131 : 19736 : if (attr->external)
1132 : : {
1133 : 4 : duplicate_attr ("EXTERNAL", where);
1134 : 4 : return false;
1135 : : }
1136 : :
1137 : 19732 : if (attr->pointer && attr->if_source != IFSRC_IFBODY)
1138 : : {
1139 : 799 : attr->pointer = 0;
1140 : 799 : attr->proc_pointer = 1;
1141 : : }
1142 : :
1143 : 19732 : attr->external = 1;
1144 : :
1145 : 19732 : return gfc_check_conflict (attr, NULL, where);
1146 : : }
1147 : :
1148 : :
1149 : : bool
1150 : 1690 : gfc_add_intrinsic (symbol_attribute *attr, locus *where)
1151 : : {
1152 : :
1153 : 1690 : if (check_used (attr, NULL, where))
1154 : : return false;
1155 : :
1156 : 1690 : if (attr->intrinsic)
1157 : : {
1158 : 0 : duplicate_attr ("INTRINSIC", where);
1159 : 0 : return false;
1160 : : }
1161 : :
1162 : 1690 : attr->intrinsic = 1;
1163 : :
1164 : 1690 : return gfc_check_conflict (attr, NULL, where);
1165 : : }
1166 : :
1167 : :
1168 : : bool
1169 : 11751 : gfc_add_optional (symbol_attribute *attr, locus *where)
1170 : : {
1171 : :
1172 : 11751 : if (check_used (attr, NULL, where))
1173 : : return false;
1174 : :
1175 : 11751 : if (attr->optional)
1176 : : {
1177 : 1 : duplicate_attr ("OPTIONAL", where);
1178 : 1 : return false;
1179 : : }
1180 : :
1181 : 11750 : attr->optional = 1;
1182 : 11750 : return gfc_check_conflict (attr, NULL, where);
1183 : : }
1184 : :
1185 : : bool
1186 : 215 : gfc_add_kind (symbol_attribute *attr, locus *where)
1187 : : {
1188 : 215 : if (attr->pdt_kind)
1189 : : {
1190 : 0 : duplicate_attr ("KIND", where);
1191 : 0 : return false;
1192 : : }
1193 : :
1194 : 215 : attr->pdt_kind = 1;
1195 : 215 : return gfc_check_conflict (attr, NULL, where);
1196 : : }
1197 : :
1198 : : bool
1199 : 254 : gfc_add_len (symbol_attribute *attr, locus *where)
1200 : : {
1201 : 254 : if (attr->pdt_len)
1202 : : {
1203 : 0 : duplicate_attr ("LEN", where);
1204 : 0 : return false;
1205 : : }
1206 : :
1207 : 254 : attr->pdt_len = 1;
1208 : 254 : return gfc_check_conflict (attr, NULL, where);
1209 : : }
1210 : :
1211 : :
1212 : : bool
1213 : 26336 : gfc_add_pointer (symbol_attribute *attr, locus *where)
1214 : : {
1215 : :
1216 : 26336 : if (check_used (attr, NULL, where))
1217 : : return false;
1218 : :
1219 : 3 : if (attr->pointer && !(attr->if_source == IFSRC_IFBODY
1220 : 1 : && !gfc_find_state (COMP_INTERFACE))
1221 : 26337 : && ! gfc_submodule_procedure(attr))
1222 : : {
1223 : 1 : duplicate_attr ("POINTER", where);
1224 : 1 : return false;
1225 : : }
1226 : :
1227 : 26327 : if (attr->procedure || (attr->external && attr->if_source != IFSRC_IFBODY)
1228 : 52641 : || (attr->if_source == IFSRC_IFBODY
1229 : 489 : && !gfc_find_state (COMP_INTERFACE)))
1230 : 36 : attr->proc_pointer = 1;
1231 : : else
1232 : 26299 : attr->pointer = 1;
1233 : :
1234 : 26335 : return gfc_check_conflict (attr, NULL, where);
1235 : : }
1236 : :
1237 : :
1238 : : bool
1239 : 690 : gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
1240 : : {
1241 : :
1242 : 690 : if (check_used (attr, NULL, where))
1243 : : return false;
1244 : :
1245 : 690 : attr->cray_pointer = 1;
1246 : 690 : return gfc_check_conflict (attr, NULL, where);
1247 : : }
1248 : :
1249 : :
1250 : : bool
1251 : 674 : gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
1252 : : {
1253 : :
1254 : 674 : if (check_used (attr, NULL, where))
1255 : : return false;
1256 : :
1257 : 674 : if (attr->cray_pointee)
1258 : : {
1259 : 1 : gfc_error ("Cray Pointee at %L appears in multiple pointer()"
1260 : : " statements", where);
1261 : 1 : return false;
1262 : : }
1263 : :
1264 : 673 : attr->cray_pointee = 1;
1265 : 673 : return gfc_check_conflict (attr, NULL, where);
1266 : : }
1267 : :
1268 : :
1269 : : bool
1270 : 114 : gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
1271 : : {
1272 : 114 : if (check_used (attr, name, where))
1273 : : return false;
1274 : :
1275 : 114 : if (attr->is_protected)
1276 : : {
1277 : 0 : if (!gfc_notify_std (GFC_STD_LEGACY,
1278 : : "Duplicate PROTECTED attribute specified at %L",
1279 : : where))
1280 : : return false;
1281 : : }
1282 : :
1283 : 114 : attr->is_protected = 1;
1284 : 114 : return gfc_check_conflict (attr, name, where);
1285 : : }
1286 : :
1287 : :
1288 : : bool
1289 : 8226 : gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
1290 : : {
1291 : :
1292 : 8226 : if (check_used (attr, name, where))
1293 : : return false;
1294 : :
1295 : 8226 : attr->result = 1;
1296 : 8226 : return gfc_check_conflict (attr, name, where);
1297 : : }
1298 : :
1299 : :
1300 : : bool
1301 : 10005 : gfc_add_save (symbol_attribute *attr, save_state s, const char *name,
1302 : : locus *where)
1303 : : {
1304 : :
1305 : 10005 : if (check_used (attr, name, where))
1306 : : return false;
1307 : :
1308 : 10005 : if (s == SAVE_EXPLICIT && gfc_pure (NULL))
1309 : : {
1310 : 2 : gfc_error ("SAVE attribute at %L cannot be specified in a PURE "
1311 : : "procedure", where);
1312 : 2 : return false;
1313 : : }
1314 : :
1315 : 10003 : if (s == SAVE_EXPLICIT)
1316 : 3637 : gfc_unset_implicit_pure (NULL);
1317 : :
1318 : 3637 : if (s == SAVE_EXPLICIT && attr->save == SAVE_EXPLICIT
1319 : 53 : && (flag_automatic || pedantic))
1320 : : {
1321 : 21 : if (!where)
1322 : : {
1323 : 1 : gfc_error ("Duplicate SAVE attribute specified near %C");
1324 : 1 : return false;
1325 : : }
1326 : :
1327 : 20 : if (!gfc_notify_std (GFC_STD_LEGACY, "Duplicate SAVE attribute "
1328 : : "specified at %L", where))
1329 : : return false;
1330 : : }
1331 : :
1332 : 10000 : attr->save = s;
1333 : 10000 : return gfc_check_conflict (attr, name, where);
1334 : : }
1335 : :
1336 : :
1337 : : bool
1338 : 23206 : gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
1339 : : {
1340 : :
1341 : 23206 : if (check_used (attr, name, where))
1342 : : return false;
1343 : :
1344 : 23206 : if (attr->value)
1345 : : {
1346 : 0 : if (!gfc_notify_std (GFC_STD_LEGACY,
1347 : : "Duplicate VALUE attribute specified at %L",
1348 : : where))
1349 : : return false;
1350 : : }
1351 : :
1352 : 23206 : attr->value = 1;
1353 : 23206 : return gfc_check_conflict (attr, name, where);
1354 : : }
1355 : :
1356 : :
1357 : : bool
1358 : 1223 : gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
1359 : : {
1360 : : /* No check_used needed as 11.2.1 of the F2003 standard allows
1361 : : that the local identifier made accessible by a use statement can be
1362 : : given a VOLATILE attribute - unless it is a coarray (F2008, C560). */
1363 : :
1364 : 1223 : if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
1365 : 1 : if (!gfc_notify_std (GFC_STD_LEGACY,
1366 : : "Duplicate VOLATILE attribute specified at %L",
1367 : : where))
1368 : : return false;
1369 : :
1370 : : /* F2008: C1282 A designator of a variable with the VOLATILE attribute
1371 : : shall not appear in a pure subprogram.
1372 : :
1373 : : F2018: C1588 A local variable of a pure subprogram, or of a BLOCK
1374 : : construct within a pure subprogram, shall not have the SAVE or
1375 : : VOLATILE attribute. */
1376 : 1223 : if (gfc_pure (NULL))
1377 : : {
1378 : 2 : gfc_error ("VOLATILE attribute at %L cannot be specified in a "
1379 : : "PURE procedure", where);
1380 : 2 : return false;
1381 : : }
1382 : :
1383 : :
1384 : 1221 : attr->volatile_ = 1;
1385 : 1221 : attr->volatile_ns = gfc_current_ns;
1386 : 1221 : return gfc_check_conflict (attr, name, where);
1387 : : }
1388 : :
1389 : :
1390 : : bool
1391 : 59 : gfc_add_asynchronous (symbol_attribute *attr, const char *name, locus *where)
1392 : : {
1393 : : /* No check_used needed as 11.2.1 of the F2003 standard allows
1394 : : that the local identifier made accessible by a use statement can be
1395 : : given a ASYNCHRONOUS attribute. */
1396 : :
1397 : 59 : if (attr->asynchronous && attr->asynchronous_ns == gfc_current_ns)
1398 : 0 : if (!gfc_notify_std (GFC_STD_LEGACY,
1399 : : "Duplicate ASYNCHRONOUS attribute specified at %L",
1400 : : where))
1401 : : return false;
1402 : :
1403 : 59 : attr->asynchronous = 1;
1404 : 59 : attr->asynchronous_ns = gfc_current_ns;
1405 : 59 : return gfc_check_conflict (attr, name, where);
1406 : : }
1407 : :
1408 : :
1409 : : bool
1410 : 283 : gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
1411 : : {
1412 : :
1413 : 283 : if (check_used (attr, name, where))
1414 : : return false;
1415 : :
1416 : 283 : if (attr->threadprivate)
1417 : : {
1418 : 0 : duplicate_attr ("THREADPRIVATE", where);
1419 : 0 : return false;
1420 : : }
1421 : :
1422 : 283 : attr->threadprivate = 1;
1423 : 283 : return gfc_check_conflict (attr, name, where);
1424 : : }
1425 : :
1426 : :
1427 : : bool
1428 : 1147 : gfc_add_omp_declare_target (symbol_attribute *attr, const char *name,
1429 : : locus *where)
1430 : : {
1431 : :
1432 : 1147 : if (check_used (attr, name, where))
1433 : : return false;
1434 : :
1435 : 1121 : if (attr->omp_declare_target)
1436 : : return true;
1437 : :
1438 : 1060 : attr->omp_declare_target = 1;
1439 : 1060 : return gfc_check_conflict (attr, name, where);
1440 : : }
1441 : :
1442 : :
1443 : : bool
1444 : 46 : gfc_add_omp_declare_target_link (symbol_attribute *attr, const char *name,
1445 : : locus *where)
1446 : : {
1447 : :
1448 : 46 : if (check_used (attr, name, where))
1449 : : return false;
1450 : :
1451 : 46 : if (attr->omp_declare_target_link)
1452 : : return true;
1453 : :
1454 : 32 : attr->omp_declare_target_link = 1;
1455 : 32 : return gfc_check_conflict (attr, name, where);
1456 : : }
1457 : :
1458 : :
1459 : : bool
1460 : 0 : gfc_add_oacc_declare_create (symbol_attribute *attr, const char *name,
1461 : : locus *where)
1462 : : {
1463 : 0 : if (check_used (attr, name, where))
1464 : : return false;
1465 : :
1466 : 0 : if (attr->oacc_declare_create)
1467 : : return true;
1468 : :
1469 : 0 : attr->oacc_declare_create = 1;
1470 : 0 : return gfc_check_conflict (attr, name, where);
1471 : : }
1472 : :
1473 : :
1474 : : bool
1475 : 0 : gfc_add_oacc_declare_copyin (symbol_attribute *attr, const char *name,
1476 : : locus *where)
1477 : : {
1478 : 0 : if (check_used (attr, name, where))
1479 : : return false;
1480 : :
1481 : 0 : if (attr->oacc_declare_copyin)
1482 : : return true;
1483 : :
1484 : 0 : attr->oacc_declare_copyin = 1;
1485 : 0 : return gfc_check_conflict (attr, name, where);
1486 : : }
1487 : :
1488 : :
1489 : : bool
1490 : 0 : gfc_add_oacc_declare_deviceptr (symbol_attribute *attr, const char *name,
1491 : : locus *where)
1492 : : {
1493 : 0 : if (check_used (attr, name, where))
1494 : : return false;
1495 : :
1496 : 0 : if (attr->oacc_declare_deviceptr)
1497 : : return true;
1498 : :
1499 : 0 : attr->oacc_declare_deviceptr = 1;
1500 : 0 : return gfc_check_conflict (attr, name, where);
1501 : : }
1502 : :
1503 : :
1504 : : bool
1505 : 0 : gfc_add_oacc_declare_device_resident (symbol_attribute *attr, const char *name,
1506 : : locus *where)
1507 : : {
1508 : 0 : if (check_used (attr, name, where))
1509 : : return false;
1510 : :
1511 : 0 : if (attr->oacc_declare_device_resident)
1512 : : return true;
1513 : :
1514 : 0 : attr->oacc_declare_device_resident = 1;
1515 : 0 : return gfc_check_conflict (attr, name, where);
1516 : : }
1517 : :
1518 : :
1519 : : bool
1520 : 11995 : gfc_add_target (symbol_attribute *attr, locus *where)
1521 : : {
1522 : :
1523 : 11995 : if (check_used (attr, NULL, where))
1524 : : return false;
1525 : :
1526 : 11995 : if (attr->target)
1527 : : {
1528 : 1 : duplicate_attr ("TARGET", where);
1529 : 1 : return false;
1530 : : }
1531 : :
1532 : 11994 : attr->target = 1;
1533 : 11994 : return gfc_check_conflict (attr, NULL, where);
1534 : : }
1535 : :
1536 : :
1537 : : bool
1538 : 98420 : gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
1539 : : {
1540 : :
1541 : 98420 : if (check_used (attr, name, where))
1542 : : return false;
1543 : :
1544 : : /* Duplicate dummy arguments are allowed due to ENTRY statements. */
1545 : 98420 : attr->dummy = 1;
1546 : 98420 : return gfc_check_conflict (attr, name, where);
1547 : : }
1548 : :
1549 : :
1550 : : bool
1551 : 11478 : gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
1552 : : {
1553 : :
1554 : 11478 : if (check_used (attr, name, where))
1555 : : return false;
1556 : :
1557 : : /* Duplicate attribute already checked for. */
1558 : 11478 : attr->in_common = 1;
1559 : 11478 : return gfc_check_conflict (attr, name, where);
1560 : : }
1561 : :
1562 : :
1563 : : bool
1564 : 2947 : gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
1565 : : {
1566 : :
1567 : : /* Duplicate attribute already checked for. */
1568 : 2947 : attr->in_equivalence = 1;
1569 : 2947 : if (!gfc_check_conflict (attr, name, where))
1570 : : return false;
1571 : :
1572 : 2938 : if (attr->flavor == FL_VARIABLE)
1573 : : return true;
1574 : :
1575 : 109 : return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1576 : : }
1577 : :
1578 : :
1579 : : bool
1580 : 2931 : gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
1581 : : {
1582 : :
1583 : 2931 : if (check_used (attr, name, where))
1584 : : return false;
1585 : :
1586 : 2930 : attr->data = 1;
1587 : 2930 : return gfc_check_conflict (attr, name, where);
1588 : : }
1589 : :
1590 : :
1591 : : bool
1592 : 1987 : gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
1593 : : {
1594 : :
1595 : 1987 : attr->in_namelist = 1;
1596 : 1987 : return gfc_check_conflict (attr, name, where);
1597 : : }
1598 : :
1599 : :
1600 : : bool
1601 : 953 : gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
1602 : : {
1603 : :
1604 : 953 : if (check_used (attr, name, where))
1605 : : return false;
1606 : :
1607 : 953 : attr->sequence = 1;
1608 : 953 : return gfc_check_conflict (attr, name, where);
1609 : : }
1610 : :
1611 : :
1612 : : bool
1613 : 8325 : gfc_add_elemental (symbol_attribute *attr, locus *where)
1614 : : {
1615 : :
1616 : 8325 : if (check_used (attr, NULL, where))
1617 : : return false;
1618 : :
1619 : 8325 : if (attr->elemental)
1620 : : {
1621 : 2 : duplicate_attr ("ELEMENTAL", where);
1622 : 2 : return false;
1623 : : }
1624 : :
1625 : 8323 : attr->elemental = 1;
1626 : 8323 : return gfc_check_conflict (attr, NULL, where);
1627 : : }
1628 : :
1629 : :
1630 : : bool
1631 : 11159 : gfc_add_pure (symbol_attribute *attr, locus *where)
1632 : : {
1633 : :
1634 : 11159 : if (check_used (attr, NULL, where))
1635 : : return false;
1636 : :
1637 : 11159 : if (attr->pure)
1638 : : {
1639 : 2 : duplicate_attr ("PURE", where);
1640 : 2 : return false;
1641 : : }
1642 : :
1643 : 11157 : attr->pure = 1;
1644 : 11157 : return gfc_check_conflict (attr, NULL, where);
1645 : : }
1646 : :
1647 : :
1648 : : bool
1649 : 757 : gfc_add_recursive (symbol_attribute *attr, locus *where)
1650 : : {
1651 : :
1652 : 757 : if (check_used (attr, NULL, where))
1653 : : return false;
1654 : :
1655 : 757 : if (attr->recursive)
1656 : : {
1657 : 2 : duplicate_attr ("RECURSIVE", where);
1658 : 2 : return false;
1659 : : }
1660 : :
1661 : 755 : attr->recursive = 1;
1662 : 755 : return gfc_check_conflict (attr, NULL, where);
1663 : : }
1664 : :
1665 : :
1666 : : bool
1667 : 759 : gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
1668 : : {
1669 : :
1670 : 759 : if (check_used (attr, name, where))
1671 : : return false;
1672 : :
1673 : 759 : if (attr->entry)
1674 : : {
1675 : 0 : duplicate_attr ("ENTRY", where);
1676 : 0 : return false;
1677 : : }
1678 : :
1679 : 759 : attr->entry = 1;
1680 : 759 : return gfc_check_conflict (attr, name, where);
1681 : : }
1682 : :
1683 : :
1684 : : bool
1685 : 1005227 : gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
1686 : : {
1687 : :
1688 : 1005227 : if (attr->flavor != FL_PROCEDURE
1689 : 1005227 : && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1690 : : return false;
1691 : :
1692 : 1005227 : attr->function = 1;
1693 : 1005227 : return gfc_check_conflict (attr, name, where);
1694 : : }
1695 : :
1696 : :
1697 : : bool
1698 : 82979 : gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
1699 : : {
1700 : :
1701 : 82979 : if (attr->flavor != FL_PROCEDURE
1702 : 82979 : && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1703 : : return false;
1704 : :
1705 : 82976 : attr->subroutine = 1;
1706 : :
1707 : : /* If we are looking at a BLOCK DATA statement and we encounter a
1708 : : name with a leading underscore (which must be
1709 : : compiler-generated), do not check. See PR 84394. */
1710 : :
1711 : 82976 : if (name && *name != '_' && gfc_current_state () != COMP_BLOCK_DATA)
1712 : 81450 : return gfc_check_conflict (attr, name, where);
1713 : : else
1714 : : return true;
1715 : : }
1716 : :
1717 : :
1718 : : bool
1719 : 25116 : gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
1720 : : {
1721 : :
1722 : 25116 : if (attr->flavor != FL_PROCEDURE
1723 : 25116 : && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1724 : : return false;
1725 : :
1726 : 25114 : attr->generic = 1;
1727 : 25114 : return gfc_check_conflict (attr, name, where);
1728 : : }
1729 : :
1730 : :
1731 : : bool
1732 : 1609 : gfc_add_proc (symbol_attribute *attr, const char *name, locus *where)
1733 : : {
1734 : :
1735 : 1609 : if (check_used (attr, NULL, where))
1736 : : return false;
1737 : :
1738 : 1609 : if (attr->flavor != FL_PROCEDURE
1739 : 1609 : && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1740 : : return false;
1741 : :
1742 : 1609 : if (attr->procedure)
1743 : : {
1744 : 0 : duplicate_attr ("PROCEDURE", where);
1745 : 0 : return false;
1746 : : }
1747 : :
1748 : 1609 : attr->procedure = 1;
1749 : :
1750 : 1609 : return gfc_check_conflict (attr, NULL, where);
1751 : : }
1752 : :
1753 : :
1754 : : bool
1755 : 789 : gfc_add_abstract (symbol_attribute* attr, locus* where)
1756 : : {
1757 : 789 : if (attr->abstract)
1758 : : {
1759 : 1 : duplicate_attr ("ABSTRACT", where);
1760 : 1 : return false;
1761 : : }
1762 : :
1763 : 788 : attr->abstract = 1;
1764 : :
1765 : 788 : return gfc_check_conflict (attr, NULL, where);
1766 : : }
1767 : :
1768 : :
1769 : : /* Flavors are special because some flavors are not what Fortran
1770 : : considers attributes and can be reaffirmed multiple times. */
1771 : :
1772 : : bool
1773 : 3769064 : gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1774 : : locus *where)
1775 : : {
1776 : :
1777 : 3769064 : if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1778 : 3769064 : || f == FL_PARAMETER || f == FL_LABEL || gfc_fl_struct(f)
1779 : 233501 : || f == FL_NAMELIST) && check_used (attr, name, where))
1780 : : return false;
1781 : :
1782 : 3769064 : if (attr->flavor == f && f == FL_VARIABLE)
1783 : : return true;
1784 : :
1785 : : /* Copying a procedure dummy argument for a module procedure in a
1786 : : submodule results in the flavor being copied and would result in
1787 : : an error without this. */
1788 : 3769062 : if (attr->flavor == f && f == FL_PROCEDURE
1789 : 554 : && gfc_new_block && gfc_new_block->abr_modproc_decl)
1790 : : return true;
1791 : :
1792 : 3769052 : if (attr->flavor != FL_UNKNOWN)
1793 : : {
1794 : 608 : if (where == NULL)
1795 : 496 : where = &gfc_current_locus;
1796 : :
1797 : 608 : if (name)
1798 : 352 : gfc_error ("%s attribute of %qs conflicts with %s attribute at %L",
1799 : 176 : gfc_code2string (flavors, attr->flavor), name,
1800 : : gfc_code2string (flavors, f), where);
1801 : : else
1802 : 864 : gfc_error ("%s attribute conflicts with %s attribute at %L",
1803 : 432 : gfc_code2string (flavors, attr->flavor),
1804 : : gfc_code2string (flavors, f), where);
1805 : :
1806 : 608 : return false;
1807 : : }
1808 : :
1809 : 3768444 : attr->flavor = f;
1810 : :
1811 : 3768444 : return gfc_check_conflict (attr, name, where);
1812 : : }
1813 : :
1814 : :
1815 : : bool
1816 : 1432661 : gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1817 : : const char *name, locus *where)
1818 : : {
1819 : :
1820 : 1432661 : if (check_used (attr, name, where))
1821 : : return false;
1822 : :
1823 : 1432632 : if (attr->flavor != FL_PROCEDURE
1824 : 1432632 : && !gfc_add_flavor (attr, FL_PROCEDURE, name, where))
1825 : : return false;
1826 : :
1827 : 1432582 : if (where == NULL)
1828 : 1413527 : where = &gfc_current_locus;
1829 : :
1830 : 1432582 : if (attr->proc != PROC_UNKNOWN && !attr->module_procedure
1831 : 277 : && attr->access == ACCESS_UNKNOWN)
1832 : : {
1833 : 0 : if (attr->proc == PROC_ST_FUNCTION && t == PROC_INTERNAL
1834 : 275 : && !gfc_notification_std (GFC_STD_F2008))
1835 : 0 : gfc_error ("%s procedure at %L is already declared as %s "
1836 : : "procedure. \nF2008: A pointer function assignment "
1837 : : "is ambiguous if it is the first executable statement "
1838 : : "after the specification block. Please add any other "
1839 : : "kind of executable statement before it. FIXME",
1840 : : gfc_code2string (procedures, t), where,
1841 : 0 : gfc_code2string (procedures, attr->proc));
1842 : : else
1843 : 275 : gfc_error ("%s procedure at %L is already declared as %s "
1844 : : "procedure", gfc_code2string (procedures, t), where,
1845 : 275 : gfc_code2string (procedures, attr->proc));
1846 : :
1847 : 275 : return false;
1848 : : }
1849 : :
1850 : 1432307 : attr->proc = t;
1851 : :
1852 : : /* Statement functions are always scalar and functions. */
1853 : 1432307 : if (t == PROC_ST_FUNCTION
1854 : 1432307 : && ((!attr->function && !gfc_add_function (attr, name, where))
1855 : 405695 : || attr->dimension))
1856 : 68 : return false;
1857 : :
1858 : 1432239 : return gfc_check_conflict (attr, name, where);
1859 : : }
1860 : :
1861 : :
1862 : : bool
1863 : 56965 : gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
1864 : : {
1865 : :
1866 : 56965 : if (check_used (attr, NULL, where))
1867 : : return false;
1868 : :
1869 : 56965 : if (attr->intent == INTENT_UNKNOWN)
1870 : : {
1871 : 56965 : attr->intent = intent;
1872 : 56965 : return gfc_check_conflict (attr, NULL, where);
1873 : : }
1874 : :
1875 : 0 : if (where == NULL)
1876 : 0 : where = &gfc_current_locus;
1877 : :
1878 : 0 : gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1879 : 0 : gfc_intent_string (attr->intent),
1880 : : gfc_intent_string (intent), where);
1881 : :
1882 : 0 : return false;
1883 : : }
1884 : :
1885 : :
1886 : : /* No checks for use-association in public and private statements. */
1887 : :
1888 : : bool
1889 : 5669 : gfc_add_access (symbol_attribute *attr, gfc_access access,
1890 : : const char *name, locus *where)
1891 : : {
1892 : :
1893 : 5669 : if (attr->access == ACCESS_UNKNOWN
1894 : 5 : || (attr->use_assoc && attr->access != ACCESS_PRIVATE))
1895 : : {
1896 : 5665 : attr->access = access;
1897 : 5665 : return gfc_check_conflict (attr, name, where);
1898 : : }
1899 : :
1900 : 4 : if (where == NULL)
1901 : 3 : where = &gfc_current_locus;
1902 : 4 : gfc_error ("ACCESS specification at %L was already specified", where);
1903 : :
1904 : 4 : return false;
1905 : : }
1906 : :
1907 : :
1908 : : /* Set the is_bind_c field for the given symbol_attribute. */
1909 : :
1910 : : bool
1911 : 7352 : gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1912 : : int is_proc_lang_bind_spec)
1913 : : {
1914 : :
1915 : 7352 : if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1916 : 5 : gfc_error_now ("BIND(C) attribute at %L can only be used for "
1917 : : "variables or common blocks", where);
1918 : 7347 : else if (attr->is_bind_c)
1919 : 1 : gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1920 : : else
1921 : 7346 : attr->is_bind_c = 1;
1922 : :
1923 : 7352 : if (where == NULL)
1924 : 54 : where = &gfc_current_locus;
1925 : :
1926 : 7352 : if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) at %L", where))
1927 : : return false;
1928 : :
1929 : 7352 : return gfc_check_conflict (attr, name, where);
1930 : : }
1931 : :
1932 : :
1933 : : /* Set the extension field for the given symbol_attribute. */
1934 : :
1935 : : bool
1936 : 1444 : gfc_add_extension (symbol_attribute *attr, locus *where)
1937 : : {
1938 : 1444 : if (where == NULL)
1939 : 0 : where = &gfc_current_locus;
1940 : :
1941 : 1444 : if (attr->extension)
1942 : 0 : gfc_error_now ("Duplicate EXTENDS attribute specified at %L", where);
1943 : : else
1944 : 1444 : attr->extension = 1;
1945 : :
1946 : 1444 : if (!gfc_notify_std (GFC_STD_F2003, "EXTENDS at %L", where))
1947 : : return false;
1948 : :
1949 : : return true;
1950 : : }
1951 : :
1952 : :
1953 : : bool
1954 : 149204 : gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1955 : : gfc_formal_arglist * formal, locus *where)
1956 : : {
1957 : 149204 : if (check_used (&sym->attr, sym->name, where))
1958 : : return false;
1959 : :
1960 : : /* Skip the following checks in the case of a module_procedures in a
1961 : : submodule since they will manifestly fail. */
1962 : 149204 : if (sym->attr.module_procedure == 1
1963 : 1357 : && source == IFSRC_DECL)
1964 : 899 : goto finish;
1965 : :
1966 : 148305 : if (where == NULL)
1967 : 148305 : where = &gfc_current_locus;
1968 : :
1969 : 148305 : if (sym->attr.if_source != IFSRC_UNKNOWN
1970 : 148305 : && sym->attr.if_source != IFSRC_DECL)
1971 : : {
1972 : 0 : gfc_error ("Symbol %qs at %L already has an explicit interface",
1973 : : sym->name, where);
1974 : 0 : return false;
1975 : : }
1976 : :
1977 : 148305 : if (source == IFSRC_IFBODY && (sym->attr.dimension || sym->attr.allocatable))
1978 : : {
1979 : 2 : gfc_error ("%qs at %L has attributes specified outside its INTERFACE "
1980 : : "body", sym->name, where);
1981 : 2 : return false;
1982 : : }
1983 : :
1984 : 148303 : finish:
1985 : 149202 : sym->formal = formal;
1986 : 149202 : sym->attr.if_source = source;
1987 : :
1988 : 149202 : return true;
1989 : : }
1990 : :
1991 : :
1992 : : /* Add a type to a symbol. */
1993 : :
1994 : : bool
1995 : 267566 : gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
1996 : : {
1997 : 267566 : sym_flavor flavor;
1998 : 267566 : bt type;
1999 : :
2000 : 267566 : if (where == NULL)
2001 : 5450 : where = &gfc_current_locus;
2002 : :
2003 : 267566 : if (sym->result)
2004 : 8130 : type = sym->result->ts.type;
2005 : : else
2006 : 259436 : type = sym->ts.type;
2007 : :
2008 : 267566 : if (sym->attr.result && type == BT_UNKNOWN && sym->ns->proc_name)
2009 : 4241 : type = sym->ns->proc_name->ts.type;
2010 : :
2011 : 267566 : if (type != BT_UNKNOWN && !(sym->attr.function && sym->attr.implicit_type)
2012 : 93 : && !(gfc_state_stack->previous && gfc_state_stack->previous->previous
2013 : 75 : && gfc_state_stack->previous->previous->state == COMP_SUBMODULE)
2014 : 52 : && !sym->attr.module_procedure)
2015 : : {
2016 : 26 : if (sym->attr.use_assoc)
2017 : 2 : gfc_error ("Symbol %qs at %L conflicts with symbol from module %qs, "
2018 : : "use-associated at %L", sym->name, where, sym->module,
2019 : : &sym->declared_at);
2020 : 24 : else if (sym->attr.function && sym->attr.result)
2021 : 1 : gfc_error ("Symbol %qs at %L already has basic type of %s",
2022 : 1 : sym->ns->proc_name->name, where, gfc_basic_typename (type));
2023 : : else
2024 : 23 : gfc_error ("Symbol %qs at %L already has basic type of %s", sym->name,
2025 : : where, gfc_basic_typename (type));
2026 : 26 : return false;
2027 : : }
2028 : :
2029 : 267540 : if (sym->attr.procedure && sym->ts.interface)
2030 : : {
2031 : 1 : gfc_error ("Procedure %qs at %L may not have basic type of %s",
2032 : : sym->name, where, gfc_basic_typename (ts->type));
2033 : 1 : return false;
2034 : : }
2035 : :
2036 : 267539 : flavor = sym->attr.flavor;
2037 : :
2038 : 267539 : if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
2039 : 267539 : || flavor == FL_LABEL
2040 : 267537 : || (flavor == FL_PROCEDURE && sym->attr.subroutine)
2041 : 267535 : || flavor == FL_DERIVED || flavor == FL_NAMELIST)
2042 : : {
2043 : 4 : gfc_error ("Symbol %qs at %L cannot have a type",
2044 : 4 : sym->ns->proc_name ? sym->ns->proc_name->name : sym->name,
2045 : : where);
2046 : 4 : return false;
2047 : : }
2048 : :
2049 : 267535 : sym->ts = *ts;
2050 : 267535 : return true;
2051 : : }
2052 : :
2053 : :
2054 : : /* Clears all attributes. */
2055 : :
2056 : : void
2057 : 7320437 : gfc_clear_attr (symbol_attribute *attr)
2058 : : {
2059 : 7320437 : memset (attr, 0, sizeof (symbol_attribute));
2060 : 7320437 : }
2061 : :
2062 : :
2063 : : /* Check for missing attributes in the new symbol. Currently does
2064 : : nothing, but it's not clear that it is unnecessary yet. */
2065 : :
2066 : : bool
2067 : 382400 : gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
2068 : : locus *where ATTRIBUTE_UNUSED)
2069 : : {
2070 : :
2071 : 382400 : return true;
2072 : : }
2073 : :
2074 : :
2075 : : /* Copy an attribute to a symbol attribute, bit by bit. Some
2076 : : attributes have a lot of side-effects but cannot be present given
2077 : : where we are called from, so we ignore some bits. */
2078 : :
2079 : : bool
2080 : 265916 : gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
2081 : : {
2082 : 265916 : int is_proc_lang_bind_spec;
2083 : :
2084 : : /* In line with the other attributes, we only add bits but do not remove
2085 : : them; cf. also PR 41034. */
2086 : 265916 : dest->ext_attr |= src->ext_attr;
2087 : :
2088 : 265916 : if (src->allocatable && !gfc_add_allocatable (dest, where))
2089 : 4 : goto fail;
2090 : :
2091 : 265912 : if (src->automatic && !gfc_add_automatic (dest, NULL, where))
2092 : 2 : goto fail;
2093 : 265910 : if (src->dimension && !gfc_add_dimension (dest, NULL, where))
2094 : 0 : goto fail;
2095 : 265910 : if (src->codimension && !gfc_add_codimension (dest, NULL, where))
2096 : 0 : goto fail;
2097 : 265910 : if (src->contiguous && !gfc_add_contiguous (dest, NULL, where))
2098 : 2 : goto fail;
2099 : 265908 : if (src->optional && !gfc_add_optional (dest, where))
2100 : 1 : goto fail;
2101 : 265907 : if (src->pointer && !gfc_add_pointer (dest, where))
2102 : 8 : goto fail;
2103 : 265899 : if (src->is_protected && !gfc_add_protected (dest, NULL, where))
2104 : 0 : goto fail;
2105 : 265899 : if (src->save && !gfc_add_save (dest, src->save, NULL, where))
2106 : 4 : goto fail;
2107 : 265895 : if (src->value && !gfc_add_value (dest, NULL, where))
2108 : 2 : goto fail;
2109 : 265893 : if (src->volatile_ && !gfc_add_volatile (dest, NULL, where))
2110 : 0 : goto fail;
2111 : 265893 : if (src->asynchronous && !gfc_add_asynchronous (dest, NULL, where))
2112 : 0 : goto fail;
2113 : 265893 : if (src->threadprivate
2114 : 265893 : && !gfc_add_threadprivate (dest, NULL, where))
2115 : 0 : goto fail;
2116 : 265893 : if (src->omp_declare_target
2117 : 265893 : && !gfc_add_omp_declare_target (dest, NULL, where))
2118 : 0 : goto fail;
2119 : 265893 : if (src->omp_declare_target_link
2120 : 265893 : && !gfc_add_omp_declare_target_link (dest, NULL, where))
2121 : 0 : goto fail;
2122 : 265893 : if (src->oacc_declare_create
2123 : 265893 : && !gfc_add_oacc_declare_create (dest, NULL, where))
2124 : 0 : goto fail;
2125 : 265893 : if (src->oacc_declare_copyin
2126 : 265893 : && !gfc_add_oacc_declare_copyin (dest, NULL, where))
2127 : 0 : goto fail;
2128 : 265893 : if (src->oacc_declare_deviceptr
2129 : 265893 : && !gfc_add_oacc_declare_deviceptr (dest, NULL, where))
2130 : 0 : goto fail;
2131 : 265893 : if (src->oacc_declare_device_resident
2132 : 265893 : && !gfc_add_oacc_declare_device_resident (dest, NULL, where))
2133 : 0 : goto fail;
2134 : 265893 : if (src->target && !gfc_add_target (dest, where))
2135 : 2 : goto fail;
2136 : 265891 : if (src->dummy && !gfc_add_dummy (dest, NULL, where))
2137 : 0 : goto fail;
2138 : 265891 : if (src->result && !gfc_add_result (dest, NULL, where))
2139 : 0 : goto fail;
2140 : 265891 : if (src->entry)
2141 : 0 : dest->entry = 1;
2142 : :
2143 : 265891 : if (src->in_namelist && !gfc_add_in_namelist (dest, NULL, where))
2144 : 0 : goto fail;
2145 : :
2146 : 265891 : if (src->in_common && !gfc_add_in_common (dest, NULL, where))
2147 : 0 : goto fail;
2148 : :
2149 : 265891 : if (src->generic && !gfc_add_generic (dest, NULL, where))
2150 : 0 : goto fail;
2151 : 265891 : if (src->function && !gfc_add_function (dest, NULL, where))
2152 : 0 : goto fail;
2153 : 265891 : if (src->subroutine && !gfc_add_subroutine (dest, NULL, where))
2154 : 0 : goto fail;
2155 : :
2156 : 265891 : if (src->sequence && !gfc_add_sequence (dest, NULL, where))
2157 : 0 : goto fail;
2158 : 265891 : if (src->elemental && !gfc_add_elemental (dest, where))
2159 : 0 : goto fail;
2160 : 265891 : if (src->pure && !gfc_add_pure (dest, where))
2161 : 0 : goto fail;
2162 : 265891 : if (src->recursive && !gfc_add_recursive (dest, where))
2163 : 0 : goto fail;
2164 : :
2165 : 265891 : if (src->flavor != FL_UNKNOWN
2166 : 265891 : && !gfc_add_flavor (dest, src->flavor, NULL, where))
2167 : 434 : goto fail;
2168 : :
2169 : 265457 : if (src->intent != INTENT_UNKNOWN
2170 : 265457 : && !gfc_add_intent (dest, src->intent, where))
2171 : 0 : goto fail;
2172 : :
2173 : 265457 : if (src->access != ACCESS_UNKNOWN
2174 : 265457 : && !gfc_add_access (dest, src->access, NULL, where))
2175 : 1 : goto fail;
2176 : :
2177 : 265456 : if (!gfc_missing_attr (dest, where))
2178 : 0 : goto fail;
2179 : :
2180 : 265456 : if (src->cray_pointer && !gfc_add_cray_pointer (dest, where))
2181 : 0 : goto fail;
2182 : 265456 : if (src->cray_pointee && !gfc_add_cray_pointee (dest, where))
2183 : 0 : goto fail;
2184 : :
2185 : 265456 : is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
2186 : 265456 : if (src->is_bind_c
2187 : 265456 : && !gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec))
2188 : : return false;
2189 : :
2190 : 265455 : if (src->is_c_interop)
2191 : 0 : dest->is_c_interop = 1;
2192 : 265455 : if (src->is_iso_c)
2193 : 0 : dest->is_iso_c = 1;
2194 : :
2195 : 265455 : if (src->external && !gfc_add_external (dest, where))
2196 : 5 : goto fail;
2197 : 265450 : if (src->intrinsic && !gfc_add_intrinsic (dest, where))
2198 : 4 : goto fail;
2199 : 265446 : if (src->proc_pointer)
2200 : 429 : dest->proc_pointer = 1;
2201 : :
2202 : : return true;
2203 : :
2204 : : fail:
2205 : : return false;
2206 : : }
2207 : :
2208 : :
2209 : : /* A function to generate a dummy argument symbol using that from the
2210 : : interface declaration. Can be used for the result symbol as well if
2211 : : the flag is set. */
2212 : :
2213 : : int
2214 : 351 : gfc_copy_dummy_sym (gfc_symbol **dsym, gfc_symbol *sym, int result)
2215 : : {
2216 : 351 : int rc;
2217 : :
2218 : 351 : rc = gfc_get_symbol (sym->name, NULL, dsym);
2219 : 351 : if (rc)
2220 : : return rc;
2221 : :
2222 : 351 : if (!gfc_add_type (*dsym, &(sym->ts), &gfc_current_locus))
2223 : : return 1;
2224 : :
2225 : 351 : if (!gfc_copy_attr (&(*dsym)->attr, &(sym->attr),
2226 : : &gfc_current_locus))
2227 : : return 1;
2228 : :
2229 : 351 : if ((*dsym)->attr.dimension)
2230 : 62 : (*dsym)->as = gfc_copy_array_spec (sym->as);
2231 : :
2232 : 351 : (*dsym)->attr.class_ok = sym->attr.class_ok;
2233 : :
2234 : 351 : if ((*dsym) != NULL && !result
2235 : 306 : && (!gfc_add_dummy(&(*dsym)->attr, (*dsym)->name, NULL)
2236 : 306 : || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2237 : 0 : return 1;
2238 : 351 : else if ((*dsym) != NULL && result
2239 : 396 : && (!gfc_add_result(&(*dsym)->attr, (*dsym)->name, NULL)
2240 : 45 : || !gfc_missing_attr (&(*dsym)->attr, NULL)))
2241 : 0 : return 1;
2242 : :
2243 : : return 0;
2244 : : }
2245 : :
2246 : :
2247 : : /************** Component name management ************/
2248 : :
2249 : : /* Component names of a derived type form their own little namespaces
2250 : : that are separate from all other spaces. The space is composed of
2251 : : a singly linked list of gfc_component structures whose head is
2252 : : located in the parent symbol. */
2253 : :
2254 : :
2255 : : /* Add a component name to a symbol. The call fails if the name is
2256 : : already present. On success, the component pointer is modified to
2257 : : point to the additional component structure. */
2258 : :
2259 : : bool
2260 : 125530 : gfc_add_component (gfc_symbol *sym, const char *name,
2261 : : gfc_component **component)
2262 : : {
2263 : 125530 : gfc_component *p, *tail;
2264 : :
2265 : : /* Check for existing components with the same name, but not for union
2266 : : components or containers. Unions and maps are anonymous so they have
2267 : : unique internal names which will never conflict.
2268 : : Don't use gfc_find_component here because it calls gfc_use_derived,
2269 : : but the derived type may not be fully defined yet. */
2270 : 125530 : tail = NULL;
2271 : :
2272 : 406489 : for (p = sym->components; p; p = p->next)
2273 : : {
2274 : 280963 : if (strcmp (p->name, name) == 0)
2275 : : {
2276 : 4 : gfc_error ("Component %qs at %C already declared at %L",
2277 : : name, &p->loc);
2278 : 4 : return false;
2279 : : }
2280 : :
2281 : 280959 : tail = p;
2282 : : }
2283 : :
2284 : 125526 : if (sym->attr.extension
2285 : 125526 : && gfc_find_component (sym->components->ts.u.derived,
2286 : : name, true, true, NULL))
2287 : : {
2288 : 2 : gfc_error ("Component %qs at %C already in the parent type "
2289 : 2 : "at %L", name, &sym->components->ts.u.derived->declared_at);
2290 : 2 : return false;
2291 : : }
2292 : :
2293 : : /* Allocate a new component. */
2294 : 125524 : p = gfc_get_component ();
2295 : :
2296 : 125524 : if (tail == NULL)
2297 : 39408 : sym->components = p;
2298 : : else
2299 : 86116 : tail->next = p;
2300 : :
2301 : 125524 : p->name = gfc_get_string ("%s", name);
2302 : 125524 : p->loc = gfc_current_locus;
2303 : 125524 : p->ts.type = BT_UNKNOWN;
2304 : :
2305 : 125524 : *component = p;
2306 : 125524 : return true;
2307 : : }
2308 : :
2309 : :
2310 : : /* Recursive function to switch derived types of all symbol in a
2311 : : namespace. */
2312 : :
2313 : : static void
2314 : 0 : switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
2315 : : {
2316 : 0 : gfc_symbol *sym;
2317 : :
2318 : 0 : if (st == NULL)
2319 : 0 : return;
2320 : :
2321 : 0 : sym = st->n.sym;
2322 : 0 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived == from)
2323 : 0 : sym->ts.u.derived = to;
2324 : :
2325 : 0 : switch_types (st->left, from, to);
2326 : 0 : switch_types (st->right, from, to);
2327 : : }
2328 : :
2329 : :
2330 : : /* This subroutine is called when a derived type is used in order to
2331 : : make the final determination about which version to use. The
2332 : : standard requires that a type be defined before it is 'used', but
2333 : : such types can appear in IMPLICIT statements before the actual
2334 : : definition. 'Using' in this context means declaring a variable to
2335 : : be that type or using the type constructor.
2336 : :
2337 : : If a type is used and the components haven't been defined, then we
2338 : : have to have a derived type in a parent unit. We find the node in
2339 : : the other namespace and point the symtree node in this namespace to
2340 : : that node. Further reference to this name point to the correct
2341 : : node. If we can't find the node in a parent namespace, then we have
2342 : : an error.
2343 : :
2344 : : This subroutine takes a pointer to a symbol node and returns a
2345 : : pointer to the translated node or NULL for an error. Usually there
2346 : : is no translation and we return the node we were passed. */
2347 : :
2348 : : gfc_symbol *
2349 : 356517 : gfc_use_derived (gfc_symbol *sym)
2350 : : {
2351 : 356517 : gfc_symbol *s;
2352 : 356517 : gfc_typespec *t;
2353 : 356517 : gfc_symtree *st;
2354 : 356517 : int i;
2355 : :
2356 : 356517 : if (!sym)
2357 : : return NULL;
2358 : :
2359 : 356513 : if (sym->attr.unlimited_polymorphic)
2360 : : return sym;
2361 : :
2362 : 354854 : if (sym->attr.generic)
2363 : 0 : sym = gfc_find_dt_in_generic (sym);
2364 : :
2365 : 354854 : if (sym->components != NULL || sym->attr.zero_comp)
2366 : : return sym; /* Already defined. */
2367 : :
2368 : 17 : if (sym->ns->parent == NULL)
2369 : 8 : goto bad;
2370 : :
2371 : 9 : if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
2372 : : {
2373 : 0 : gfc_error ("Symbol %qs at %C is ambiguous", sym->name);
2374 : 0 : return NULL;
2375 : : }
2376 : :
2377 : 9 : if (s == NULL || !gfc_fl_struct (s->attr.flavor))
2378 : 9 : goto bad;
2379 : :
2380 : : /* Get rid of symbol sym, translating all references to s. */
2381 : 0 : for (i = 0; i < GFC_LETTERS; i++)
2382 : : {
2383 : 0 : t = &sym->ns->default_type[i];
2384 : 0 : if (t->u.derived == sym)
2385 : 0 : t->u.derived = s;
2386 : : }
2387 : :
2388 : 0 : st = gfc_find_symtree (sym->ns->sym_root, sym->name);
2389 : 0 : st->n.sym = s;
2390 : :
2391 : 0 : s->refs++;
2392 : :
2393 : : /* Unlink from list of modified symbols. */
2394 : 0 : gfc_commit_symbol (sym);
2395 : :
2396 : 0 : switch_types (sym->ns->sym_root, sym, s);
2397 : :
2398 : : /* TODO: Also have to replace sym -> s in other lists like
2399 : : namelists, common lists and interface lists. */
2400 : 0 : gfc_free_symbol (sym);
2401 : :
2402 : 0 : return s;
2403 : :
2404 : 17 : bad:
2405 : 17 : gfc_error ("Derived type %qs at %C is being used before it is defined",
2406 : : sym->name);
2407 : 17 : return NULL;
2408 : : }
2409 : :
2410 : :
2411 : : /* Find all derived types in the uppermost namespace that have a component
2412 : : a component called name and stash them in the assoc field of an
2413 : : associate name variable.
2414 : : This is used to infer the derived type of an associate name, whose selector
2415 : : is a sibling derived type function that has not yet been parsed. Either
2416 : : the derived type is use associated in both contained and sibling procedures
2417 : : or it appears in the uppermost namespace. */
2418 : :
2419 : : static int cts = 0;
2420 : : static void
2421 : 12466 : find_derived_types (gfc_symbol *sym, gfc_symtree *st, const char *name,
2422 : : bool contained, bool stash)
2423 : : {
2424 : 12466 : if (st->n.sym && st->n.sym->attr.flavor == FL_DERIVED
2425 : 2116 : && !st->n.sym->attr.is_class
2426 : 1622 : && ((contained && st->n.sym->attr.use_assoc) || !contained)
2427 : 14072 : && gfc_find_component (st->n.sym, name, true, true, NULL))
2428 : : {
2429 : : /* Do the stashing, if required. */
2430 : 810 : cts++;
2431 : 810 : if (stash)
2432 : : {
2433 : 738 : if (sym->assoc->derived_types)
2434 : 294 : st->n.sym->dt_next = sym->assoc->derived_types;
2435 : 738 : sym->assoc->derived_types = st->n.sym;
2436 : : }
2437 : : }
2438 : :
2439 : 12466 : if (st->left)
2440 : 4860 : find_derived_types (sym, st->left, name, contained, stash);
2441 : :
2442 : 12466 : if (st->right)
2443 : 5658 : find_derived_types (sym, st->right, name, contained, stash);
2444 : 12466 : }
2445 : :
2446 : : int
2447 : 1002 : gfc_find_derived_types (gfc_symbol *sym, gfc_namespace *ns,
2448 : : const char *name, bool stash)
2449 : : {
2450 : 1002 : gfc_namespace *encompassing = NULL;
2451 : 1002 : gcc_assert (sym->assoc);
2452 : :
2453 : 1002 : cts = 0;
2454 : 3060 : while (ns->parent)
2455 : : {
2456 : 2058 : if (!ns->parent->parent && ns->proc_name
2457 : 1002 : && (ns->proc_name->attr.function || ns->proc_name->attr.subroutine))
2458 : 2058 : encompassing = ns;
2459 : : ns = ns->parent;
2460 : : }
2461 : :
2462 : : /* Search the top level namespace first. */
2463 : 1002 : find_derived_types (sym, ns->sym_root, name, false, stash);
2464 : :
2465 : : /* Then the encompassing namespace. */
2466 : 1002 : if (encompassing && encompassing != ns)
2467 : 946 : find_derived_types (sym, encompassing->sym_root, name, true, stash);
2468 : :
2469 : 1002 : return cts;
2470 : : }
2471 : :
2472 : : /* Find the component with the given name in the union type symbol.
2473 : : If ref is not NULL it will be set to the chain of components through which
2474 : : the component can actually be accessed. This is necessary for unions because
2475 : : intermediate structures may be maps, nested structures, or other unions,
2476 : : all of which may (or must) be 'anonymous' to user code. */
2477 : :
2478 : : static gfc_component *
2479 : 2192 : find_union_component (gfc_symbol *un, const char *name,
2480 : : bool noaccess, gfc_ref **ref)
2481 : : {
2482 : 2192 : gfc_component *m, *check;
2483 : 2192 : gfc_ref *sref, *tmp;
2484 : :
2485 : 3983 : for (m = un->components; m; m = m->next)
2486 : : {
2487 : 3483 : check = gfc_find_component (m->ts.u.derived, name, noaccess, true, &tmp);
2488 : 3483 : if (check == NULL)
2489 : 1791 : continue;
2490 : :
2491 : : /* Found component somewhere in m; chain the refs together. */
2492 : 1692 : if (ref)
2493 : : {
2494 : : /* Map ref. */
2495 : 1692 : sref = gfc_get_ref ();
2496 : 1692 : sref->type = REF_COMPONENT;
2497 : 1692 : sref->u.c.component = m;
2498 : 1692 : sref->u.c.sym = m->ts.u.derived;
2499 : 1692 : sref->next = tmp;
2500 : :
2501 : 1692 : *ref = sref;
2502 : : }
2503 : : /* Other checks (such as access) were done in the recursive calls. */
2504 : : return check;
2505 : : }
2506 : : return NULL;
2507 : : }
2508 : :
2509 : :
2510 : : /* Recursively append candidate COMPONENT structures to CANDIDATES. Store
2511 : : the number of total candidates in CANDIDATES_LEN. */
2512 : :
2513 : : static void
2514 : 34 : lookup_component_fuzzy_find_candidates (gfc_component *component,
2515 : : char **&candidates,
2516 : : size_t &candidates_len)
2517 : : {
2518 : 81 : for (gfc_component *p = component; p; p = p->next)
2519 : 47 : vec_push (candidates, candidates_len, p->name);
2520 : 34 : }
2521 : :
2522 : :
2523 : : /* Lookup component MEMBER fuzzily, taking names in COMPONENT into account. */
2524 : :
2525 : : static const char*
2526 : 34 : lookup_component_fuzzy (const char *member, gfc_component *component)
2527 : : {
2528 : 34 : char **candidates = NULL;
2529 : 34 : size_t candidates_len = 0;
2530 : 34 : lookup_component_fuzzy_find_candidates (component, candidates,
2531 : : candidates_len);
2532 : 34 : return gfc_closest_fuzzy_match (member, candidates);
2533 : : }
2534 : :
2535 : :
2536 : : /* Given a derived type node and a component name, try to locate the
2537 : : component structure. Returns the NULL pointer if the component is
2538 : : not found or the components are private. If noaccess is set, no access
2539 : : checks are done. If silent is set, an error will not be generated if
2540 : : the component cannot be found or accessed.
2541 : :
2542 : : If ref is not NULL, *ref is set to represent the chain of components
2543 : : required to get to the ultimate component.
2544 : :
2545 : : If the component is simply a direct subcomponent, or is inherited from a
2546 : : parent derived type in the given derived type, this is a single ref with its
2547 : : component set to the returned component.
2548 : :
2549 : : Otherwise, *ref is constructed as a chain of subcomponents. This occurs
2550 : : when the component is found through an implicit chain of nested union and
2551 : : map components. Unions and maps are "anonymous" substructures in FORTRAN
2552 : : which cannot be explicitly referenced, but the reference chain must be
2553 : : considered as in C for backend translation to correctly compute layouts.
2554 : : (For example, x.a may refer to x->(UNION)->(MAP)->(UNION)->(MAP)->a). */
2555 : :
2556 : : gfc_component *
2557 : 330784 : gfc_find_component (gfc_symbol *sym, const char *name,
2558 : : bool noaccess, bool silent, gfc_ref **ref)
2559 : : {
2560 : 330784 : gfc_component *p, *check;
2561 : 330784 : gfc_ref *sref = NULL, *tmp = NULL;
2562 : :
2563 : 330784 : if (name == NULL || sym == NULL)
2564 : : return NULL;
2565 : :
2566 : 325790 : if (sym->attr.flavor == FL_DERIVED)
2567 : 317027 : sym = gfc_use_derived (sym);
2568 : : else
2569 : 8763 : gcc_assert (gfc_fl_struct (sym->attr.flavor));
2570 : :
2571 : 317027 : if (sym == NULL)
2572 : : return NULL;
2573 : :
2574 : : /* Handle UNIONs specially - mutually recursive with gfc_find_component. */
2575 : 325788 : if (sym->attr.flavor == FL_UNION)
2576 : 500 : return find_union_component (sym, name, noaccess, ref);
2577 : :
2578 : 325288 : if (ref) *ref = NULL;
2579 : 710195 : for (p = sym->components; p; p = p->next)
2580 : : {
2581 : : /* Nest search into union's maps. */
2582 : 675177 : if (p->ts.type == BT_UNION)
2583 : : {
2584 : 1692 : check = find_union_component (p->ts.u.derived, name, noaccess, &tmp);
2585 : 1692 : if (check != NULL)
2586 : : {
2587 : : /* Union ref. */
2588 : 1692 : if (ref)
2589 : : {
2590 : 1252 : sref = gfc_get_ref ();
2591 : 1252 : sref->type = REF_COMPONENT;
2592 : 1252 : sref->u.c.component = p;
2593 : 1252 : sref->u.c.sym = p->ts.u.derived;
2594 : 1252 : sref->next = tmp;
2595 : 1252 : *ref = sref;
2596 : : }
2597 : 1692 : return check;
2598 : : }
2599 : : }
2600 : 673485 : else if (strcmp (p->name, name) == 0)
2601 : : break;
2602 : :
2603 : 384907 : continue;
2604 : : }
2605 : :
2606 : 323596 : if (p && sym->attr.use_assoc && !noaccess)
2607 : : {
2608 : 46455 : bool is_parent_comp = sym->attr.extension && (p == sym->components);
2609 : 46455 : if (p->attr.access == ACCESS_PRIVATE ||
2610 : : (p->attr.access != ACCESS_PUBLIC
2611 : 45600 : && sym->component_access == ACCESS_PRIVATE
2612 : 8 : && !is_parent_comp))
2613 : : {
2614 : 14 : if (!silent)
2615 : 14 : gfc_error ("Component %qs at %C is a PRIVATE component of %qs",
2616 : : name, sym->name);
2617 : 14 : return NULL;
2618 : : }
2619 : : }
2620 : :
2621 : : if (p == NULL
2622 : 35018 : && sym->attr.extension
2623 : 24011 : && sym->components->ts.type == BT_DERIVED)
2624 : : {
2625 : 24011 : p = gfc_find_component (sym->components->ts.u.derived, name,
2626 : : noaccess, silent, ref);
2627 : : /* Do not overwrite the error. */
2628 : 24011 : if (p == NULL)
2629 : : return p;
2630 : : }
2631 : :
2632 : 323165 : if (p == NULL && !silent)
2633 : : {
2634 : 34 : const char *guessed = lookup_component_fuzzy (name, sym->components);
2635 : 34 : if (guessed)
2636 : 10 : gfc_error ("%qs at %C is not a member of the %qs structure"
2637 : : "; did you mean %qs?",
2638 : : name, sym->name, guessed);
2639 : : else
2640 : 24 : gfc_error ("%qs at %C is not a member of the %qs structure",
2641 : : name, sym->name);
2642 : : }
2643 : :
2644 : : /* Component was found; build the ultimate component reference. */
2645 : 323165 : if (p != NULL && ref)
2646 : : {
2647 : 256148 : tmp = gfc_get_ref ();
2648 : 256148 : tmp->type = REF_COMPONENT;
2649 : 256148 : tmp->u.c.component = p;
2650 : 256148 : tmp->u.c.sym = sym;
2651 : : /* Link the final component ref to the end of the chain of subrefs. */
2652 : 256148 : if (sref)
2653 : : {
2654 : : *ref = sref;
2655 : : for (; sref->next; sref = sref->next)
2656 : : ;
2657 : : sref->next = tmp;
2658 : : }
2659 : : else
2660 : 256148 : *ref = tmp;
2661 : : }
2662 : :
2663 : : return p;
2664 : 384907 : }
2665 : :
2666 : :
2667 : : /* Given a symbol, free all of the component structures and everything
2668 : : they point to. */
2669 : :
2670 : : static void
2671 : 6024507 : free_components (gfc_component *p)
2672 : : {
2673 : 6024507 : gfc_component *q;
2674 : :
2675 : 6293004 : for (; p; p = q)
2676 : : {
2677 : 268497 : q = p->next;
2678 : :
2679 : 268497 : gfc_free_array_spec (p->as);
2680 : 268497 : gfc_free_expr (p->initializer);
2681 : 268497 : if (p->kind_expr)
2682 : 204 : gfc_free_expr (p->kind_expr);
2683 : 268497 : if (p->param_list)
2684 : 130 : gfc_free_actual_arglist (p->param_list);
2685 : 268497 : free (p->tb);
2686 : 268497 : p->tb = NULL;
2687 : 268497 : free (p);
2688 : : }
2689 : 6024507 : }
2690 : :
2691 : :
2692 : : /******************** Statement label management ********************/
2693 : :
2694 : : /* Comparison function for statement labels, used for managing the
2695 : : binary tree. */
2696 : :
2697 : : static int
2698 : 7733 : compare_st_labels (void *a1, void *b1)
2699 : : {
2700 : 7733 : gfc_st_label *a = (gfc_st_label *) a1;
2701 : 7733 : gfc_st_label *b = (gfc_st_label *) b1;
2702 : :
2703 : 7733 : if (a->omp_region == b->omp_region)
2704 : 7670 : return b->value - a->value;
2705 : : else
2706 : 63 : return b->omp_region - a->omp_region;
2707 : : }
2708 : :
2709 : :
2710 : : /* Free a single gfc_st_label structure, making sure the tree is not
2711 : : messed up. This function is called only when some parse error
2712 : : occurs. */
2713 : :
2714 : : void
2715 : 3 : gfc_free_st_label (gfc_st_label *label)
2716 : : {
2717 : :
2718 : 3 : if (label == NULL)
2719 : : return;
2720 : :
2721 : 3 : gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
2722 : :
2723 : 3 : if (label->format != NULL)
2724 : 0 : gfc_free_expr (label->format);
2725 : :
2726 : 3 : free (label);
2727 : : }
2728 : :
2729 : :
2730 : : /* Free a whole tree of gfc_st_label structures. */
2731 : :
2732 : : static void
2733 : 516452 : free_st_labels (gfc_st_label *label)
2734 : : {
2735 : :
2736 : 516452 : if (label == NULL)
2737 : : return;
2738 : :
2739 : 4698 : free_st_labels (label->left);
2740 : 4698 : free_st_labels (label->right);
2741 : :
2742 : 4698 : if (label->format != NULL)
2743 : 1014 : gfc_free_expr (label->format);
2744 : 4698 : free (label);
2745 : : }
2746 : :
2747 : :
2748 : : /* Given a label number, search for and return a pointer to the label
2749 : : structure, creating it if it does not exist. */
2750 : :
2751 : : gfc_st_label *
2752 : 13566 : gfc_get_st_label (int labelno)
2753 : : {
2754 : 13566 : gfc_st_label *lp;
2755 : 13566 : gfc_namespace *ns;
2756 : 13566 : int omp_region = gfc_omp_metadirective_region_stack.last ();
2757 : :
2758 : 13566 : if (gfc_current_state () == COMP_DERIVED)
2759 : 3 : ns = gfc_current_block ()->f2k_derived;
2760 : : else
2761 : : {
2762 : : /* Find the namespace of the scoping unit:
2763 : : If we're in a BLOCK construct, jump to the parent namespace. */
2764 : 13563 : ns = gfc_current_ns;
2765 : 13574 : while (ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL)
2766 : 11 : ns = ns->parent;
2767 : : }
2768 : :
2769 : : /* First see if the label is already in this namespace. */
2770 : 13566 : gcc_checking_assert (gfc_omp_metadirective_region_stack.length () > 0);
2771 : 18343 : for (int omp_region_idx = gfc_omp_metadirective_region_stack.length () - 1;
2772 : 18343 : omp_region_idx >= 0; omp_region_idx--)
2773 : : {
2774 : 13642 : int omp_region2 = gfc_omp_metadirective_region_stack[omp_region_idx];
2775 : 13642 : lp = ns->st_labels;
2776 : 31781 : while (lp)
2777 : : {
2778 : 27004 : if (lp->omp_region == omp_region2)
2779 : : {
2780 : 26746 : if (lp->value == labelno)
2781 : : return lp;
2782 : 17881 : if (lp->value < labelno)
2783 : 13080 : lp = lp->left;
2784 : : else
2785 : 4801 : lp = lp->right;
2786 : : }
2787 : 258 : else if (lp->omp_region < omp_region2)
2788 : 177 : lp = lp->left;
2789 : : else
2790 : 81 : lp = lp->right;
2791 : : }
2792 : : }
2793 : :
2794 : 4701 : lp = XCNEW (gfc_st_label);
2795 : :
2796 : 4701 : lp->value = labelno;
2797 : 4701 : lp->defined = ST_LABEL_UNKNOWN;
2798 : 4701 : lp->referenced = ST_LABEL_UNKNOWN;
2799 : 4701 : lp->ns = ns;
2800 : 4701 : lp->omp_region = omp_region;
2801 : :
2802 : 4701 : gfc_insert_bbt (&ns->st_labels, lp, compare_st_labels);
2803 : :
2804 : 4701 : return lp;
2805 : : }
2806 : :
2807 : : /* Rebind a statement label to a new OpenMP region. If a label with the same
2808 : : value already exists in the new region, update it and return it. Otherwise,
2809 : : move the label to the new region. */
2810 : :
2811 : : gfc_st_label *
2812 : 44 : gfc_rebind_label (gfc_st_label *label, int new_omp_region)
2813 : : {
2814 : 44 : gfc_st_label *lp = label->ns->st_labels;
2815 : 44 : int labelno = label->value;
2816 : :
2817 : 106 : while (lp)
2818 : : {
2819 : 97 : if (lp->omp_region == new_omp_region)
2820 : : {
2821 : 38 : if (lp->value == labelno)
2822 : : {
2823 : 35 : if (lp == label)
2824 : : return label;
2825 : 0 : if (lp->defined == ST_LABEL_UNKNOWN
2826 : 0 : && label->defined != ST_LABEL_UNKNOWN)
2827 : 0 : lp->defined = label->defined;
2828 : 0 : if (lp->referenced == ST_LABEL_UNKNOWN
2829 : 0 : && label->referenced != ST_LABEL_UNKNOWN)
2830 : 0 : lp->referenced = label->referenced;
2831 : 0 : if (lp->format == NULL && label->format != NULL)
2832 : 0 : lp->format = label->format;
2833 : 0 : gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
2834 : 0 : return lp;
2835 : : }
2836 : 3 : if (lp->value < labelno)
2837 : 2 : lp = lp->left;
2838 : : else
2839 : 1 : lp = lp->right;
2840 : : }
2841 : 59 : else if (lp->omp_region < new_omp_region)
2842 : 29 : lp = lp->left;
2843 : : else
2844 : 30 : lp = lp->right;
2845 : : }
2846 : :
2847 : 9 : gfc_delete_bbt (&label->ns->st_labels, label, compare_st_labels);
2848 : 9 : label->left = nullptr;
2849 : 9 : label->right = nullptr;
2850 : 9 : label->omp_region = new_omp_region;
2851 : 9 : gfc_insert_bbt (&label->ns->st_labels, label, compare_st_labels);
2852 : 9 : return label;
2853 : : }
2854 : :
2855 : : /* Called when a statement with a statement label is about to be
2856 : : accepted. We add the label to the list of the current namespace,
2857 : : making sure it hasn't been defined previously and referenced
2858 : : correctly. */
2859 : :
2860 : : void
2861 : 4685 : gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
2862 : : {
2863 : 4685 : int labelno;
2864 : :
2865 : 4685 : labelno = lp->value;
2866 : :
2867 : 4685 : if (lp->defined != ST_LABEL_UNKNOWN && !gfc_in_omp_metadirective_body)
2868 : 2 : gfc_error ("Duplicate statement label %d at %L and %L", labelno,
2869 : : &lp->where, label_locus);
2870 : : else
2871 : : {
2872 : 4683 : lp->where = *label_locus;
2873 : :
2874 : 4683 : switch (type)
2875 : : {
2876 : 1017 : case ST_LABEL_FORMAT:
2877 : 1017 : if (lp->referenced == ST_LABEL_TARGET
2878 : 1017 : || lp->referenced == ST_LABEL_DO_TARGET)
2879 : 0 : gfc_error ("Label %d at %C already referenced as branch target",
2880 : : labelno);
2881 : : else
2882 : 1017 : lp->defined = ST_LABEL_FORMAT;
2883 : :
2884 : : break;
2885 : :
2886 : 3659 : case ST_LABEL_TARGET:
2887 : 3659 : case ST_LABEL_DO_TARGET:
2888 : 3659 : if (lp->referenced == ST_LABEL_FORMAT)
2889 : 2 : gfc_error ("Label %d at %C already referenced as a format label",
2890 : : labelno);
2891 : : else
2892 : 3657 : lp->defined = type;
2893 : :
2894 : 1720 : if (lp->referenced == ST_LABEL_DO_TARGET && type != ST_LABEL_DO_TARGET
2895 : 3791 : && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
2896 : : "DO termination statement which is not END DO"
2897 : : " or CONTINUE with label %d at %C", labelno))
2898 : : return;
2899 : : break;
2900 : :
2901 : 7 : default:
2902 : 7 : lp->defined = ST_LABEL_BAD_TARGET;
2903 : 7 : lp->referenced = ST_LABEL_BAD_TARGET;
2904 : : }
2905 : : }
2906 : : }
2907 : :
2908 : :
2909 : : /* Reference a label. Given a label and its type, see if that
2910 : : reference is consistent with what is known about that label,
2911 : : updating the unknown state. Returns false if something goes
2912 : : wrong. */
2913 : :
2914 : : bool
2915 : 17923 : gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
2916 : : {
2917 : 17923 : gfc_sl_type label_type;
2918 : 17923 : int labelno;
2919 : 17923 : bool rc;
2920 : :
2921 : 17923 : if (lp == NULL)
2922 : : return true;
2923 : :
2924 : 7628 : labelno = lp->value;
2925 : :
2926 : 7628 : if (lp->defined != ST_LABEL_UNKNOWN)
2927 : : label_type = lp->defined;
2928 : : else
2929 : : {
2930 : 5968 : label_type = lp->referenced;
2931 : 5968 : lp->where = gfc_current_locus;
2932 : : }
2933 : :
2934 : 7628 : if (label_type == ST_LABEL_FORMAT
2935 : 1127 : && (type == ST_LABEL_TARGET || type == ST_LABEL_DO_TARGET))
2936 : : {
2937 : 0 : gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
2938 : 0 : rc = false;
2939 : 0 : goto done;
2940 : : }
2941 : :
2942 : 7628 : if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_DO_TARGET
2943 : 7628 : || label_type == ST_LABEL_BAD_TARGET)
2944 : 2440 : && type == ST_LABEL_FORMAT)
2945 : : {
2946 : 5 : gfc_error ("Label %d at %C previously used as branch target", labelno);
2947 : 5 : rc = false;
2948 : 5 : goto done;
2949 : : }
2950 : :
2951 : 622 : if (lp->referenced == ST_LABEL_DO_TARGET && type == ST_LABEL_DO_TARGET
2952 : 543 : && !gfc_in_omp_metadirective_body
2953 : 8164 : && !gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
2954 : : "Shared DO termination label %d at %C", labelno))
2955 : : return false;
2956 : :
2957 : 7623 : if (type == ST_LABEL_DO_TARGET
2958 : 7623 : && !gfc_notify_std (GFC_STD_F2018_OBS, "Labeled DO statement "
2959 : : "at %L", &gfc_current_locus))
2960 : : return false;
2961 : :
2962 : 7623 : if (lp->referenced != ST_LABEL_DO_TARGET)
2963 : 7001 : lp->referenced = type;
2964 : : rc = true;
2965 : :
2966 : : done:
2967 : : return rc;
2968 : : }
2969 : :
2970 : :
2971 : : /************** Symbol table management subroutines ****************/
2972 : :
2973 : : /* Basic details: Fortran 95 requires a potentially unlimited number
2974 : : of distinct namespaces when compiling a program unit. This case
2975 : : occurs during a compilation of internal subprograms because all of
2976 : : the internal subprograms must be read before we can start
2977 : : generating code for the host.
2978 : :
2979 : : Given the tricky nature of the Fortran grammar, we must be able to
2980 : : undo changes made to a symbol table if the current interpretation
2981 : : of a statement is found to be incorrect. Whenever a symbol is
2982 : : looked up, we make a copy of it and link to it. All of these
2983 : : symbols are kept in a vector so that we can commit or
2984 : : undo the changes at a later time.
2985 : :
2986 : : A symtree may point to a symbol node outside of its namespace. In
2987 : : this case, that symbol has been used as a host associated variable
2988 : : at some previous time. */
2989 : :
2990 : : /* Allocate a new namespace structure. Copies the implicit types from
2991 : : PARENT if PARENT_TYPES is set. */
2992 : :
2993 : : gfc_namespace *
2994 : 533276 : gfc_get_namespace (gfc_namespace *parent, int parent_types)
2995 : : {
2996 : 533276 : gfc_namespace *ns;
2997 : 533276 : gfc_typespec *ts;
2998 : 533276 : int in;
2999 : 533276 : int i;
3000 : :
3001 : 533276 : ns = XCNEW (gfc_namespace);
3002 : 533276 : ns->sym_root = NULL;
3003 : 533276 : ns->uop_root = NULL;
3004 : 533276 : ns->tb_sym_root = NULL;
3005 : 533276 : ns->finalizers = NULL;
3006 : 533276 : ns->default_access = ACCESS_UNKNOWN;
3007 : 533276 : ns->parent = parent;
3008 : :
3009 : 15465004 : for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
3010 : : {
3011 : 14931728 : ns->operator_access[in] = ACCESS_UNKNOWN;
3012 : 14931728 : ns->tb_op[in] = NULL;
3013 : : }
3014 : :
3015 : : /* Initialize default implicit types. */
3016 : 14398452 : for (i = 'a'; i <= 'z'; i++)
3017 : : {
3018 : 13865176 : ns->set_flag[i - 'a'] = 0;
3019 : 13865176 : ts = &ns->default_type[i - 'a'];
3020 : :
3021 : 13865176 : if (parent_types && ns->parent != NULL)
3022 : : {
3023 : : /* Copy parent settings. */
3024 : 1699568 : *ts = ns->parent->default_type[i - 'a'];
3025 : 1699568 : continue;
3026 : : }
3027 : :
3028 : 12165608 : if (flag_implicit_none != 0)
3029 : : {
3030 : 108550 : gfc_clear_ts (ts);
3031 : 108550 : continue;
3032 : : }
3033 : :
3034 : 12057058 : if ('i' <= i && i <= 'n')
3035 : : {
3036 : 2782398 : ts->type = BT_INTEGER;
3037 : 2782398 : ts->kind = gfc_default_integer_kind;
3038 : : }
3039 : : else
3040 : : {
3041 : 9274660 : ts->type = BT_REAL;
3042 : 9274660 : ts->kind = gfc_default_real_kind;
3043 : : }
3044 : : }
3045 : :
3046 : 533276 : ns->refs = 1;
3047 : :
3048 : 533276 : return ns;
3049 : : }
3050 : :
3051 : :
3052 : : /* Comparison function for symtree nodes. */
3053 : :
3054 : : static int
3055 : 33901169 : compare_symtree (void *_st1, void *_st2)
3056 : : {
3057 : 33901169 : gfc_symtree *st1, *st2;
3058 : :
3059 : 33901169 : st1 = (gfc_symtree *) _st1;
3060 : 33901169 : st2 = (gfc_symtree *) _st2;
3061 : :
3062 : 33901169 : return strcmp (st1->name, st2->name);
3063 : : }
3064 : :
3065 : :
3066 : : /* Allocate a new symtree node and associate it with the new symbol. */
3067 : :
3068 : : gfc_symtree *
3069 : 6200105 : gfc_new_symtree (gfc_symtree **root, const char *name)
3070 : : {
3071 : 6200105 : gfc_symtree *st;
3072 : :
3073 : 6200105 : st = XCNEW (gfc_symtree);
3074 : 6200105 : st->name = gfc_get_string ("%s", name);
3075 : :
3076 : 6200105 : gfc_insert_bbt (root, st, compare_symtree);
3077 : 6200105 : return st;
3078 : : }
3079 : :
3080 : :
3081 : : /* Delete a symbol from the tree. Does not free the symbol itself! */
3082 : :
3083 : : static void
3084 : 4078530 : gfc_delete_symtree (gfc_symtree **root, const char *name)
3085 : : {
3086 : 4078530 : gfc_symtree st, *st0;
3087 : 4078530 : const char *p;
3088 : :
3089 : : /* Submodules are marked as mod.submod. When freeing a submodule
3090 : : symbol, the symtree only has "submod", so adjust that here. */
3091 : :
3092 : 4078530 : p = strrchr(name, '.');
3093 : 4078530 : if (p)
3094 : 0 : p++;
3095 : : else
3096 : : p = name;
3097 : :
3098 : 4078530 : st.name = gfc_get_string ("%s", p);
3099 : 4078530 : st0 = (gfc_symtree *) gfc_delete_bbt (root, &st, compare_symtree);
3100 : :
3101 : 4078530 : free (st0);
3102 : 4078530 : }
3103 : :
3104 : :
3105 : : /* Given a root symtree node and a name, try to find the symbol within
3106 : : the namespace. Returns NULL if the symbol is not found. */
3107 : :
3108 : : gfc_symtree *
3109 : 29500195 : gfc_find_symtree (gfc_symtree *st, const char *name)
3110 : : {
3111 : 29500195 : int c;
3112 : :
3113 : 127162791 : while (st != NULL)
3114 : : {
3115 : 109445409 : c = strcmp (name, st->name);
3116 : 109445409 : if (c == 0)
3117 : : return st;
3118 : :
3119 : 97662596 : st = (c < 0) ? st->left : st->right;
3120 : : }
3121 : :
3122 : : return NULL;
3123 : : }
3124 : :
3125 : :
3126 : : /* Return a symtree node with a name that is guaranteed to be unique
3127 : : within the namespace and corresponds to an illegal fortran name. */
3128 : :
3129 : : gfc_symtree *
3130 : 635107 : gfc_get_unique_symtree (gfc_namespace *ns)
3131 : : {
3132 : 635107 : char name[GFC_MAX_SYMBOL_LEN + 1];
3133 : 635107 : static int serial = 0;
3134 : :
3135 : 635107 : sprintf (name, "@%d", serial++);
3136 : 635107 : return gfc_new_symtree (&ns->sym_root, name);
3137 : : }
3138 : :
3139 : :
3140 : : /* Given a name find a user operator node, creating it if it doesn't
3141 : : exist. These are much simpler than symbols because they can't be
3142 : : ambiguous with one another. */
3143 : :
3144 : : gfc_user_op *
3145 : 972 : gfc_get_uop (const char *name)
3146 : : {
3147 : 972 : gfc_user_op *uop;
3148 : 972 : gfc_symtree *st;
3149 : 972 : gfc_namespace *ns = gfc_current_ns;
3150 : :
3151 : 972 : if (ns->omp_udr_ns)
3152 : 35 : ns = ns->parent;
3153 : 972 : st = gfc_find_symtree (ns->uop_root, name);
3154 : 972 : if (st != NULL)
3155 : 594 : return st->n.uop;
3156 : :
3157 : 378 : st = gfc_new_symtree (&ns->uop_root, name);
3158 : :
3159 : 378 : uop = st->n.uop = XCNEW (gfc_user_op);
3160 : 378 : uop->name = gfc_get_string ("%s", name);
3161 : 378 : uop->access = ACCESS_UNKNOWN;
3162 : 378 : uop->ns = ns;
3163 : :
3164 : 378 : return uop;
3165 : : }
3166 : :
3167 : :
3168 : : /* Given a name find the user operator node. Returns NULL if it does
3169 : : not exist. */
3170 : :
3171 : : gfc_user_op *
3172 : 6892 : gfc_find_uop (const char *name, gfc_namespace *ns)
3173 : : {
3174 : 6892 : gfc_symtree *st;
3175 : :
3176 : 6892 : if (ns == NULL)
3177 : 18 : ns = gfc_current_ns;
3178 : :
3179 : 6892 : st = gfc_find_symtree (ns->uop_root, name);
3180 : 6892 : return (st == NULL) ? NULL : st->n.uop;
3181 : : }
3182 : :
3183 : :
3184 : : /* Update a symbol's common_block field, and take care of the associated
3185 : : memory management. */
3186 : :
3187 : : static void
3188 : 7447674 : set_symbol_common_block (gfc_symbol *sym, gfc_common_head *common_block)
3189 : : {
3190 : 7447674 : if (sym->common_block == common_block)
3191 : : return;
3192 : :
3193 : 5774 : if (sym->common_block && sym->common_block->name[0] != '\0')
3194 : : {
3195 : 5489 : sym->common_block->refs--;
3196 : 5489 : if (sym->common_block->refs == 0)
3197 : 1722 : free (sym->common_block);
3198 : : }
3199 : 5774 : sym->common_block = common_block;
3200 : : }
3201 : :
3202 : :
3203 : : /* Remove a gfc_symbol structure and everything it points to. */
3204 : :
3205 : : void
3206 : 6176223 : gfc_free_symbol (gfc_symbol *&sym)
3207 : : {
3208 : :
3209 : 6176223 : if (sym == NULL)
3210 : : return;
3211 : :
3212 : 6024507 : gfc_free_array_spec (sym->as);
3213 : :
3214 : 6024507 : free_components (sym->components);
3215 : :
3216 : 6024507 : gfc_free_expr (sym->value);
3217 : :
3218 : 6024507 : gfc_free_namelist (sym->namelist);
3219 : :
3220 : 6024507 : if (sym->ns != sym->formal_ns)
3221 : 5973435 : gfc_free_namespace (sym->formal_ns);
3222 : :
3223 : 6024507 : if (!sym->attr.generic_copy)
3224 : 6024507 : gfc_free_interface (sym->generic);
3225 : :
3226 : 6024507 : gfc_free_formal_arglist (sym->formal);
3227 : :
3228 : 6024507 : gfc_free_namespace (sym->f2k_derived);
3229 : :
3230 : 6024507 : set_symbol_common_block (sym, NULL);
3231 : :
3232 : 6024507 : if (sym->param_list)
3233 : 1008 : gfc_free_actual_arglist (sym->param_list);
3234 : :
3235 : 6024507 : free (sym);
3236 : 6024507 : sym = NULL;
3237 : : }
3238 : :
3239 : :
3240 : : /* Returns true if the symbol SYM has, through its FORMAL_NS field, a reference
3241 : : to itself which should be eliminated for the symbol memory to be released
3242 : : via normal reference counting.
3243 : :
3244 : : The implementation is crucial as it controls the proper release of symbols,
3245 : : especially (contained) procedure symbols, which can represent a lot of memory
3246 : : through the namespace of their body.
3247 : :
3248 : : We try to avoid freeing too much memory (causing dangling pointers), to not
3249 : : leak too much (wasting memory), and to avoid expensive walks of the symbol
3250 : : tree (which would be the correct way to check for a cycle). */
3251 : :
3252 : : bool
3253 : 6083280 : cyclic_reference_break_needed (gfc_symbol *sym)
3254 : : {
3255 : : /* Normal symbols don't reference themselves. */
3256 : 6083280 : if (sym->formal_ns == nullptr)
3257 : : return false;
3258 : :
3259 : : /* Procedures at the root of the file do have a self reference, but they don't
3260 : : have a reference in a parent namespace preventing the release of the
3261 : : procedure namespace, so they can use the normal reference counting. */
3262 : 294424 : if (sym->formal_ns == sym->ns)
3263 : : return false;
3264 : :
3265 : : /* If sym->refs == 1, we can use normal reference counting. If sym->refs > 2,
3266 : : the symbol won't be freed anyway, with or without cyclic reference. */
3267 : 285905 : if (sym->refs != 2)
3268 : : return false;
3269 : :
3270 : : /* Procedure symbols host-associated from a module in submodules are special,
3271 : : because the namespace of the procedure block in the submodule is different
3272 : : from the FORMAL_NS namespace generated by host-association. So there are
3273 : : two different namespaces representing the same procedure namespace. As
3274 : : FORMAL_NS comes from host-association, which only imports symbols visible
3275 : : from the outside (dummy arguments basically), we can assume there is no
3276 : : self reference through FORMAL_NS in that case. */
3277 : 46451 : if (sym->attr.host_assoc && sym->attr.used_in_submodule)
3278 : 347 : return false;
3279 : :
3280 : : /* We can assume that contained procedures have cyclic references, because
3281 : : the symbol of the procedure itself is accessible in the procedure body
3282 : : namespace. So we assume that symbols with a formal namespace different
3283 : : from the declaration namespace and two references, one of which is about
3284 : : to be removed, are procedures with just the self reference left. At this
3285 : : point, the symbol SYM matches that pattern, so we return true here to
3286 : : permit the release of SYM. */
3287 : : return true;
3288 : : }
3289 : :
3290 : :
3291 : : /* Decrease the reference counter and free memory when we reach zero.
3292 : : Returns true if the symbol has been freed, false otherwise. */
3293 : :
3294 : : bool
3295 : 6083837 : gfc_release_symbol (gfc_symbol *&sym)
3296 : : {
3297 : 6083837 : if (sym == NULL)
3298 : : return false;
3299 : :
3300 : 6083280 : if (cyclic_reference_break_needed (sym))
3301 : : {
3302 : : /* As formal_ns contains a reference to sym, delete formal_ns just
3303 : : before the deletion of sym. */
3304 : 46104 : gfc_namespace *ns = sym->formal_ns;
3305 : 46104 : sym->formal_ns = NULL;
3306 : 46104 : gfc_free_namespace (ns);
3307 : : }
3308 : :
3309 : 6083280 : sym->refs--;
3310 : 6083280 : if (sym->refs > 0)
3311 : : return false;
3312 : :
3313 : 5970454 : gcc_assert (sym->refs == 0);
3314 : 5970454 : gfc_free_symbol (sym);
3315 : 5970454 : return true;
3316 : : }
3317 : :
3318 : :
3319 : : /* Allocate and initialize a new symbol node. */
3320 : :
3321 : : gfc_symbol *
3322 : 6100184 : gfc_new_symbol (const char *name, gfc_namespace *ns, locus *where)
3323 : : {
3324 : 6100184 : gfc_symbol *p;
3325 : :
3326 : 6100184 : p = XCNEW (gfc_symbol);
3327 : :
3328 : 6100184 : gfc_clear_ts (&p->ts);
3329 : 6100184 : gfc_clear_attr (&p->attr);
3330 : 6100184 : p->ns = ns;
3331 : 6100184 : p->declared_at = where ? *where : gfc_current_locus;
3332 : 6100184 : p->name = gfc_get_string ("%s", name);
3333 : :
3334 : 6100184 : return p;
3335 : : }
3336 : :
3337 : :
3338 : : /* Generate an error if a symbol is ambiguous, and set the error flag
3339 : : on it. */
3340 : :
3341 : : static void
3342 : 40 : ambiguous_symbol (const char *name, gfc_symtree *st)
3343 : : {
3344 : :
3345 : 40 : if (st->n.sym->error)
3346 : : return;
3347 : :
3348 : 20 : if (st->n.sym->module)
3349 : 17 : gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
3350 : : "from module %qs", name, st->n.sym->name, st->n.sym->module);
3351 : : else
3352 : 3 : gfc_error ("Name %qs at %C is an ambiguous reference to %qs "
3353 : : "from current program unit", name, st->n.sym->name);
3354 : :
3355 : 20 : st->n.sym->error = 1;
3356 : : }
3357 : :
3358 : :
3359 : : /* If we're in a SELECT TYPE block, check if the variable 'st' matches any
3360 : : selector on the stack. If yes, replace it by the corresponding temporary. */
3361 : :
3362 : : static void
3363 : 10490758 : select_type_insert_tmp (gfc_symtree **st)
3364 : : {
3365 : 10540617 : gfc_select_type_stack *stack = select_type_stack;
3366 : 10714397 : for (; stack; stack = stack->prev)
3367 : 223639 : if ((*st)->n.sym == stack->selector && stack->tmp)
3368 : : {
3369 : 49859 : *st = stack->tmp;
3370 : 49859 : select_type_insert_tmp (st);
3371 : 49859 : return;
3372 : : }
3373 : : }
3374 : :
3375 : :
3376 : : /* Look for a symtree in the current procedure -- that is, go up to
3377 : : parent namespaces but only if inside a BLOCK. Returns NULL if not found. */
3378 : :
3379 : : gfc_symtree*
3380 : 240 : gfc_find_symtree_in_proc (const char* name, gfc_namespace* ns)
3381 : : {
3382 : 289 : while (ns)
3383 : : {
3384 : 289 : gfc_symtree* st = gfc_find_symtree (ns->sym_root, name);
3385 : 289 : if (st)
3386 : : return st;
3387 : :
3388 : 51 : if (!ns->construct_entities)
3389 : : break;
3390 : 49 : ns = ns->parent;
3391 : : }
3392 : :
3393 : : return NULL;
3394 : : }
3395 : :
3396 : :
3397 : : /* Search for a symtree starting in the current namespace, resorting to
3398 : : any parent namespaces if requested by a nonzero parent_flag.
3399 : : Returns true if the name is ambiguous. */
3400 : :
3401 : : bool
3402 : 18754651 : gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
3403 : : gfc_symtree **result)
3404 : : {
3405 : 18754651 : gfc_symtree *st;
3406 : :
3407 : 18754651 : if (ns == NULL)
3408 : 7638612 : ns = gfc_current_ns;
3409 : :
3410 : 21328749 : do
3411 : : {
3412 : 21328749 : st = gfc_find_symtree (ns->sym_root, name);
3413 : 21328749 : if (st != NULL)
3414 : : {
3415 : 10490758 : select_type_insert_tmp (&st);
3416 : :
3417 : 10490758 : *result = st;
3418 : : /* Ambiguous generic interfaces are permitted, as long
3419 : : as the specific interfaces are different. */
3420 : 10490758 : if (st->ambiguous && !st->n.sym->attr.generic)
3421 : : {
3422 : 36 : ambiguous_symbol (name, st);
3423 : 36 : return true;
3424 : : }
3425 : :
3426 : : return false;
3427 : : }
3428 : :
3429 : 10837991 : if (!parent_flag)
3430 : : break;
3431 : :
3432 : : /* Don't escape an interface block. */
3433 : 8006345 : if (ns && !ns->has_import_set
3434 : 7996627 : && ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY)
3435 : : break;
3436 : :
3437 : 7812234 : ns = ns->parent;
3438 : : }
3439 : 7812234 : while (ns != NULL);
3440 : :
3441 : 8263893 : if (gfc_current_state() == COMP_DERIVED
3442 : 182228 : && gfc_current_block ()->attr.pdt_template)
3443 : : {
3444 : : gfc_symbol *der = gfc_current_block ();
3445 : 18247 : for (; der; der = gfc_get_derived_super_type (der))
3446 : : {
3447 : 10241 : if (der->f2k_derived && der->f2k_derived->sym_root)
3448 : : {
3449 : 10167 : st = gfc_find_symtree (der->f2k_derived->sym_root, name);
3450 : 10167 : if (st)
3451 : : break;
3452 : : }
3453 : : }
3454 : 9775 : *result = st;
3455 : 9775 : return false;
3456 : : }
3457 : :
3458 : 8254118 : *result = NULL;
3459 : :
3460 : 8254118 : return false;
3461 : : }
3462 : :
3463 : :
3464 : : /* Same, but returns the symbol instead. */
3465 : :
3466 : : int
3467 : 2266481 : gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
3468 : : gfc_symbol **result)
3469 : : {
3470 : 2266481 : gfc_symtree *st;
3471 : 2266481 : int i;
3472 : :
3473 : 2266481 : i = gfc_find_sym_tree (name, ns, parent_flag, &st);
3474 : :
3475 : 2266481 : if (st == NULL)
3476 : 1712517 : *result = NULL;
3477 : : else
3478 : 553964 : *result = st->n.sym;
3479 : :
3480 : 2266481 : return i;
3481 : : }
3482 : :
3483 : :
3484 : : /* Tells whether there is only one set of changes in the stack. */
3485 : :
3486 : : static bool
3487 : 40148723 : single_undo_checkpoint_p (void)
3488 : : {
3489 : 40148723 : if (latest_undo_chgset == &default_undo_chgset_var)
3490 : : {
3491 : 40148723 : gcc_assert (latest_undo_chgset->previous == NULL);
3492 : : return true;
3493 : : }
3494 : : else
3495 : : {
3496 : 0 : gcc_assert (latest_undo_chgset->previous != NULL);
3497 : : return false;
3498 : : }
3499 : : }
3500 : :
3501 : : /* Save symbol with the information necessary to back it out. */
3502 : :
3503 : : void
3504 : 6045711 : gfc_save_symbol_data (gfc_symbol *sym)
3505 : : {
3506 : 6045711 : gfc_symbol *s;
3507 : 6045711 : unsigned i;
3508 : :
3509 : 6045711 : if (!single_undo_checkpoint_p ())
3510 : : {
3511 : : /* If there is more than one change set, look for the symbol in the
3512 : : current one. If it is found there, we can reuse it. */
3513 : 0 : FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3514 : 0 : if (s == sym)
3515 : : {
3516 : 0 : gcc_assert (sym->gfc_new || sym->old_symbol != NULL);
3517 : 6045711 : return;
3518 : : }
3519 : : }
3520 : 6045711 : else if (sym->gfc_new || sym->old_symbol != NULL)
3521 : : return;
3522 : :
3523 : 3079485 : s = XCNEW (gfc_symbol);
3524 : 3079485 : *s = *sym;
3525 : 3079485 : sym->old_symbol = s;
3526 : 3079485 : sym->gfc_new = 0;
3527 : :
3528 : 3079485 : latest_undo_chgset->syms.safe_push (sym);
3529 : : }
3530 : :
3531 : :
3532 : : /* Given a name, find a symbol, or create it if it does not exist yet
3533 : : in the current namespace. If the symbol is found we make sure that
3534 : : it's OK.
3535 : :
3536 : : The integer return code indicates
3537 : : 0 All OK
3538 : : 1 The symbol name was ambiguous
3539 : : 2 The name meant to be established was already host associated.
3540 : :
3541 : : So if the return value is nonzero, then an error was issued. */
3542 : :
3543 : : int
3544 : 5918675 : gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result,
3545 : : bool allow_subroutine, locus *where)
3546 : : {
3547 : 5918675 : gfc_symtree *st;
3548 : 5918675 : gfc_symbol *p;
3549 : :
3550 : : /* This doesn't usually happen during resolution. */
3551 : 5918675 : if (ns == NULL)
3552 : 2917164 : ns = gfc_current_ns;
3553 : :
3554 : : /* Try to find the symbol in ns. */
3555 : 5918675 : st = gfc_find_symtree (ns->sym_root, name);
3556 : :
3557 : 5918675 : if (st == NULL && ns->omp_udr_ns)
3558 : : {
3559 : 319 : ns = ns->parent;
3560 : 319 : st = gfc_find_symtree (ns->sym_root, name);
3561 : : }
3562 : :
3563 : 5059892 : if (st == NULL)
3564 : : {
3565 : : /* If not there, create a new symbol. */
3566 : 5059762 : p = gfc_new_symbol (name, ns, where);
3567 : :
3568 : : /* Add to the list of tentative symbols. */
3569 : 5059762 : p->old_symbol = NULL;
3570 : 5059762 : p->mark = 1;
3571 : 5059762 : p->gfc_new = 1;
3572 : 5059762 : latest_undo_chgset->syms.safe_push (p);
3573 : :
3574 : 5059762 : st = gfc_new_symtree (&ns->sym_root, name);
3575 : 5059762 : st->n.sym = p;
3576 : 5059762 : p->refs++;
3577 : :
3578 : : }
3579 : : else
3580 : : {
3581 : : /* Make sure the existing symbol is OK. Ambiguous
3582 : : generic interfaces are permitted, as long as the
3583 : : specific interfaces are different. */
3584 : 858913 : if (st->ambiguous && !st->n.sym->attr.generic)
3585 : : {
3586 : 4 : ambiguous_symbol (name, st);
3587 : 4 : return 1;
3588 : : }
3589 : :
3590 : 858909 : p = st->n.sym;
3591 : 858909 : if (p->ns != ns && (!p->attr.function || ns->proc_name != p)
3592 : 10350 : && !(allow_subroutine && p->attr.subroutine)
3593 : 10342 : && !(ns->proc_name && ns->proc_name->attr.if_source == IFSRC_IFBODY
3594 : 10300 : && (ns->has_import_set || p->attr.imported)))
3595 : : {
3596 : : /* Symbol is from another namespace. */
3597 : 43 : gfc_error ("Symbol %qs at %C has already been host associated",
3598 : : name);
3599 : 43 : return 2;
3600 : : }
3601 : :
3602 : 858866 : p->mark = 1;
3603 : :
3604 : : /* Copy in case this symbol is changed. */
3605 : 858866 : gfc_save_symbol_data (p);
3606 : : }
3607 : :
3608 : 5918628 : *result = st;
3609 : 5918628 : return 0;
3610 : : }
3611 : :
3612 : :
3613 : : int
3614 : 986521 : gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result,
3615 : : locus *where)
3616 : : {
3617 : 986521 : gfc_symtree *st;
3618 : 986521 : int i;
3619 : :
3620 : 986521 : i = gfc_get_sym_tree (name, ns, &st, false, where);
3621 : 986521 : if (i != 0)
3622 : : return i;
3623 : :
3624 : 986504 : if (st)
3625 : 986504 : *result = st->n.sym;
3626 : : else
3627 : 0 : *result = NULL;
3628 : : return i;
3629 : : }
3630 : :
3631 : :
3632 : : /* Subroutine that searches for a symbol, creating it if it doesn't
3633 : : exist, but tries to host-associate the symbol if possible. */
3634 : :
3635 : : int
3636 : 7813828 : gfc_get_ha_sym_tree (const char *name, gfc_symtree **result, locus *where)
3637 : : {
3638 : 7813828 : gfc_symtree *st;
3639 : 7813828 : int i;
3640 : :
3641 : 7813828 : i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
3642 : :
3643 : 7813828 : if (st != NULL)
3644 : : {
3645 : 5122927 : gfc_save_symbol_data (st->n.sym);
3646 : 5122927 : *result = st;
3647 : 5122927 : return i;
3648 : : }
3649 : :
3650 : 2690901 : i = gfc_find_sym_tree (name, gfc_current_ns, 1, &st);
3651 : 2690901 : if (i)
3652 : : return i;
3653 : :
3654 : 2690901 : if (st != NULL)
3655 : : {
3656 : 266652 : *result = st;
3657 : 266652 : return 0;
3658 : : }
3659 : :
3660 : 2424249 : return gfc_get_sym_tree (name, gfc_current_ns, result, false, where);
3661 : : }
3662 : :
3663 : :
3664 : : int
3665 : 31564 : gfc_get_ha_symbol (const char *name, gfc_symbol **result, locus *where)
3666 : : {
3667 : 31564 : int i;
3668 : 31564 : gfc_symtree *st = NULL;
3669 : :
3670 : 31564 : i = gfc_get_ha_sym_tree (name, &st, where);
3671 : :
3672 : 31564 : if (st)
3673 : 31564 : *result = st->n.sym;
3674 : : else
3675 : 0 : *result = NULL;
3676 : :
3677 : 31564 : return i;
3678 : : }
3679 : :
3680 : :
3681 : : /* Search for the symtree belonging to a gfc_common_head; we cannot use
3682 : : head->name as the common_root symtree's name might be mangled. */
3683 : :
3684 : : static gfc_symtree *
3685 : 17 : find_common_symtree (gfc_symtree *st, gfc_common_head *head)
3686 : : {
3687 : :
3688 : 20 : gfc_symtree *result;
3689 : :
3690 : 20 : if (st == NULL)
3691 : : return NULL;
3692 : :
3693 : 14 : if (st->n.common == head)
3694 : : return st;
3695 : :
3696 : 3 : result = find_common_symtree (st->left, head);
3697 : 3 : if (!result)
3698 : 3 : result = find_common_symtree (st->right, head);
3699 : :
3700 : : return result;
3701 : : }
3702 : :
3703 : :
3704 : : /* Restore previous state of symbol. Just copy simple stuff. */
3705 : :
3706 : : static void
3707 : 1423167 : restore_old_symbol (gfc_symbol *p)
3708 : : {
3709 : 1423167 : gfc_symbol *old;
3710 : :
3711 : 1423167 : p->mark = 0;
3712 : 1423167 : old = p->old_symbol;
3713 : :
3714 : 1423167 : p->ts.type = old->ts.type;
3715 : 1423167 : p->ts.kind = old->ts.kind;
3716 : :
3717 : 1423167 : p->attr = old->attr;
3718 : :
3719 : 1423167 : if (p->value != old->value)
3720 : : {
3721 : 1 : gcc_checking_assert (old->value == NULL);
3722 : 1 : gfc_free_expr (p->value);
3723 : 1 : p->value = NULL;
3724 : : }
3725 : :
3726 : 1423167 : if (p->as != old->as)
3727 : : {
3728 : 7 : if (p->as)
3729 : 7 : gfc_free_array_spec (p->as);
3730 : 7 : p->as = old->as;
3731 : : }
3732 : :
3733 : 1423167 : p->generic = old->generic;
3734 : 1423167 : p->component_access = old->component_access;
3735 : :
3736 : 1423167 : if (p->namelist != NULL && old->namelist == NULL)
3737 : : {
3738 : 0 : gfc_free_namelist (p->namelist);
3739 : 0 : p->namelist = NULL;
3740 : : }
3741 : : else
3742 : : {
3743 : 1423167 : if (p->namelist_tail != old->namelist_tail)
3744 : : {
3745 : 1 : gfc_free_namelist (old->namelist_tail->next);
3746 : 1 : old->namelist_tail->next = NULL;
3747 : : }
3748 : : }
3749 : :
3750 : 1423167 : p->namelist_tail = old->namelist_tail;
3751 : :
3752 : 1423167 : if (p->formal != old->formal)
3753 : : {
3754 : 23 : gfc_free_formal_arglist (p->formal);
3755 : 23 : p->formal = old->formal;
3756 : : }
3757 : :
3758 : 1423167 : set_symbol_common_block (p, old->common_block);
3759 : 1423167 : p->common_head = old->common_head;
3760 : :
3761 : 1423167 : p->old_symbol = old->old_symbol;
3762 : 1423167 : free (old);
3763 : 1423167 : }
3764 : :
3765 : :
3766 : : /* Frees the internal data of a gfc_undo_change_set structure. Doesn't free
3767 : : the structure itself. */
3768 : :
3769 : : static void
3770 : 79379 : free_undo_change_set_data (gfc_undo_change_set &cs)
3771 : : {
3772 : 0 : cs.syms.release ();
3773 : 79379 : cs.tbps.release ();
3774 : 0 : }
3775 : :
3776 : :
3777 : : /* Given a change set pointer, free its target's contents and update it with
3778 : : the address of the previous change set. Note that only the contents are
3779 : : freed, not the target itself (the contents' container). It is not a problem
3780 : : as the latter will be a local variable usually. */
3781 : :
3782 : : static void
3783 : 0 : pop_undo_change_set (gfc_undo_change_set *&cs)
3784 : : {
3785 : 0 : free_undo_change_set_data (*cs);
3786 : 0 : cs = cs->previous;
3787 : 0 : }
3788 : :
3789 : :
3790 : : static void free_old_symbol (gfc_symbol *sym);
3791 : :
3792 : :
3793 : : /* Merges the current change set into the previous one. The changes themselves
3794 : : are left untouched; only one checkpoint is forgotten. */
3795 : :
3796 : : void
3797 : 0 : gfc_drop_last_undo_checkpoint (void)
3798 : : {
3799 : 0 : gfc_symbol *s, *t;
3800 : 0 : unsigned i, j;
3801 : :
3802 : 0 : FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, s)
3803 : : {
3804 : : /* No need to loop in this case. */
3805 : 0 : if (s->old_symbol == NULL)
3806 : 0 : continue;
3807 : :
3808 : : /* Remove the duplicate symbols. */
3809 : 0 : FOR_EACH_VEC_ELT (latest_undo_chgset->previous->syms, j, t)
3810 : 0 : if (t == s)
3811 : : {
3812 : 0 : latest_undo_chgset->previous->syms.unordered_remove (j);
3813 : :
3814 : : /* S->OLD_SYMBOL is the backup symbol for S as it was at the
3815 : : last checkpoint. We drop that checkpoint, so S->OLD_SYMBOL
3816 : : shall contain from now on the backup symbol for S as it was
3817 : : at the checkpoint before. */
3818 : 0 : if (s->old_symbol->gfc_new)
3819 : : {
3820 : 0 : gcc_assert (s->old_symbol->old_symbol == NULL);
3821 : 0 : s->gfc_new = s->old_symbol->gfc_new;
3822 : 0 : free_old_symbol (s);
3823 : : }
3824 : : else
3825 : 0 : restore_old_symbol (s->old_symbol);
3826 : : break;
3827 : : }
3828 : : }
3829 : :
3830 : 0 : latest_undo_chgset->previous->syms.safe_splice (latest_undo_chgset->syms);
3831 : 0 : latest_undo_chgset->previous->tbps.safe_splice (latest_undo_chgset->tbps);
3832 : :
3833 : 0 : pop_undo_change_set (latest_undo_chgset);
3834 : 0 : }
3835 : :
3836 : :
3837 : : /* Remove the reference to the symbol SYM in the symbol tree held by NS
3838 : : and free SYM if the last reference to it has been removed.
3839 : : Returns whether the symbol has been freed. */
3840 : :
3841 : : static bool
3842 : 4078568 : delete_symbol_from_ns (gfc_symbol *sym, gfc_namespace *ns)
3843 : : {
3844 : 4078568 : if (ns == nullptr)
3845 : : return false;
3846 : :
3847 : : /* The derived type is saved in the symtree with the first
3848 : : letter capitalized; the all lower-case version to the
3849 : : derived type contains its associated generic function. */
3850 : 4078530 : const char *sym_name = gfc_fl_struct (sym->attr.flavor)
3851 : 36 : ? gfc_dt_upper_string (sym->name)
3852 : 4078530 : : sym->name;
3853 : :
3854 : 4078530 : gfc_delete_symtree (&ns->sym_root, sym_name);
3855 : :
3856 : 4078530 : return gfc_release_symbol (sym);
3857 : : }
3858 : :
3859 : :
3860 : : /* Undoes all the changes made to symbols since the previous checkpoint.
3861 : : This subroutine is made simpler due to the fact that attributes are
3862 : : never removed once added. */
3863 : :
3864 : : void
3865 : 12866399 : gfc_restore_last_undo_checkpoint (void)
3866 : : {
3867 : 12866399 : gfc_symbol *p;
3868 : 12866399 : unsigned i;
3869 : :
3870 : 31204651 : FOR_EACH_VEC_ELT_REVERSE (latest_undo_chgset->syms, i, p)
3871 : : {
3872 : : /* Symbol in a common block was new. Or was old and just put in common */
3873 : 5501669 : if (p->common_block
3874 : 3734 : && (p->gfc_new || !p->old_symbol->common_block))
3875 : : {
3876 : : /* If the symbol was added to any common block, it
3877 : : needs to be removed to stop the resolver looking
3878 : : for a (possibly) dead symbol. */
3879 : 80 : if (p->common_block->head == p && !p->common_next)
3880 : : {
3881 : 14 : gfc_symtree st, *st0;
3882 : 14 : st0 = find_common_symtree (p->ns->common_root,
3883 : : p->common_block);
3884 : 14 : if (st0)
3885 : : {
3886 : 11 : st.name = st0->name;
3887 : 11 : gfc_delete_bbt (&p->ns->common_root, &st, compare_symtree);
3888 : 11 : free (st0);
3889 : : }
3890 : : }
3891 : :
3892 : 80 : if (p->common_block->head == p)
3893 : 14 : p->common_block->head = p->common_next;
3894 : : else
3895 : : {
3896 : 66 : gfc_symbol *cparent, *csym;
3897 : :
3898 : 66 : cparent = p->common_block->head;
3899 : 66 : csym = cparent->common_next;
3900 : :
3901 : 290 : while (csym != p)
3902 : : {
3903 : 224 : cparent = csym;
3904 : 224 : csym = csym->common_next;
3905 : : }
3906 : :
3907 : 66 : gcc_assert(cparent->common_next == p);
3908 : 66 : cparent->common_next = csym->common_next;
3909 : : }
3910 : 80 : p->common_next = NULL;
3911 : : }
3912 : 5501669 : if (p->gfc_new)
3913 : : {
3914 : 4078502 : bool freed = delete_symbol_from_ns (p, p->ns);
3915 : :
3916 : : /* If the symbol is a procedure (function or subroutine), remove
3917 : : it from the procedure body namespace as well as from the outer
3918 : : namespace. */
3919 : 4078502 : if (!freed
3920 : 38 : && p->formal_ns != p->ns)
3921 : 38 : freed = delete_symbol_from_ns (p, p->formal_ns);
3922 : :
3923 : : /* If the formal_ns field has not been set yet, the previous
3924 : : conditional does nothing. In that case, we can assume that
3925 : : gfc_current_ns is the procedure body namespace, and remove the
3926 : : symbol from there. */
3927 : 38 : if (!freed
3928 : 38 : && gfc_current_ns != p->ns
3929 : 28 : && gfc_current_ns != p->formal_ns)
3930 : 28 : freed = delete_symbol_from_ns (p, gfc_current_ns);
3931 : : }
3932 : : else
3933 : 1423167 : restore_old_symbol (p);
3934 : : }
3935 : :
3936 : 12866399 : latest_undo_chgset->syms.truncate (0);
3937 : 12866399 : latest_undo_chgset->tbps.truncate (0);
3938 : :
3939 : 12866399 : if (!single_undo_checkpoint_p ())
3940 : 0 : pop_undo_change_set (latest_undo_chgset);
3941 : 12866399 : }
3942 : :
3943 : :
3944 : : /* Makes sure that there is only one set of changes; in other words we haven't
3945 : : forgotten to pair a call to gfc_new_checkpoint with a call to either
3946 : : gfc_drop_last_undo_checkpoint or gfc_restore_last_undo_checkpoint. */
3947 : :
3948 : : static void
3949 : 21236613 : enforce_single_undo_checkpoint (void)
3950 : : {
3951 : 21236613 : gcc_checking_assert (single_undo_checkpoint_p ());
3952 : 21236613 : }
3953 : :
3954 : :
3955 : : /* Undoes all the changes made to symbols in the current statement. */
3956 : :
3957 : : void
3958 : 12866399 : gfc_undo_symbols (void)
3959 : : {
3960 : 12866399 : enforce_single_undo_checkpoint ();
3961 : 12866399 : gfc_restore_last_undo_checkpoint ();
3962 : 12866399 : }
3963 : :
3964 : :
3965 : : /* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
3966 : : components of old_symbol that might need deallocation are the "allocatables"
3967 : : that are restored in gfc_undo_symbols(), with two exceptions: namelist and
3968 : : namelist_tail. In case these differ between old_symbol and sym, it's just
3969 : : because sym->namelist has gotten a few more items. */
3970 : :
3971 : : static void
3972 : 2716626 : free_old_symbol (gfc_symbol *sym)
3973 : : {
3974 : :
3975 : 2716626 : if (sym->old_symbol == NULL)
3976 : : return;
3977 : :
3978 : 1656317 : if (sym->old_symbol->as != NULL
3979 : 270401 : && sym->old_symbol->as != sym->as
3980 : 2 : && !(sym->ts.type == BT_CLASS
3981 : 2 : && sym->ts.u.derived->attr.is_class
3982 : 2 : && sym->old_symbol->as == CLASS_DATA (sym)->as))
3983 : 0 : gfc_free_array_spec (sym->old_symbol->as);
3984 : :
3985 : 1656317 : if (sym->old_symbol->value != sym->value)
3986 : 7274 : gfc_free_expr (sym->old_symbol->value);
3987 : :
3988 : 1656317 : if (sym->old_symbol->formal != sym->formal)
3989 : 16557 : gfc_free_formal_arglist (sym->old_symbol->formal);
3990 : :
3991 : 1656317 : free (sym->old_symbol);
3992 : 1656317 : sym->old_symbol = NULL;
3993 : : }
3994 : :
3995 : :
3996 : : /* Makes the changes made in the current statement permanent-- gets
3997 : : rid of undo information. */
3998 : :
3999 : : void
4000 : 1538077 : gfc_commit_symbols (void)
4001 : : {
4002 : 1538077 : gfc_symbol *p;
4003 : 1538077 : gfc_typebound_proc *tbp;
4004 : 1538077 : unsigned i;
4005 : :
4006 : 1538077 : enforce_single_undo_checkpoint ();
4007 : :
4008 : 5175134 : FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
4009 : : {
4010 : 2098980 : p->mark = 0;
4011 : 2098980 : p->gfc_new = 0;
4012 : 2098980 : free_old_symbol (p);
4013 : : }
4014 : 1538077 : latest_undo_chgset->syms.truncate (0);
4015 : :
4016 : 3133273 : FOR_EACH_VEC_ELT (latest_undo_chgset->tbps, i, tbp)
4017 : 57119 : tbp->error = 0;
4018 : 1538077 : latest_undo_chgset->tbps.truncate (0);
4019 : 1538077 : }
4020 : :
4021 : :
4022 : : /* Makes the changes made in one symbol permanent -- gets rid of undo
4023 : : information. */
4024 : :
4025 : : void
4026 : 617646 : gfc_commit_symbol (gfc_symbol *sym)
4027 : : {
4028 : 617646 : gfc_symbol *p;
4029 : 617646 : unsigned i;
4030 : :
4031 : 617646 : enforce_single_undo_checkpoint ();
4032 : :
4033 : 2197559 : FOR_EACH_VEC_ELT (latest_undo_chgset->syms, i, p)
4034 : 1499423 : if (p == sym)
4035 : : {
4036 : 537156 : latest_undo_chgset->syms.unordered_remove (i);
4037 : 537156 : break;
4038 : : }
4039 : :
4040 : 617646 : sym->mark = 0;
4041 : 617646 : sym->gfc_new = 0;
4042 : :
4043 : 617646 : free_old_symbol (sym);
4044 : 617646 : }
4045 : :
4046 : :
4047 : : /* Recursively free trees containing type-bound procedures. */
4048 : :
4049 : : static void
4050 : 1028238 : free_tb_tree (gfc_symtree *t)
4051 : : {
4052 : 1028238 : if (t == NULL)
4053 : : return;
4054 : :
4055 : 7063 : free_tb_tree (t->left);
4056 : 7063 : free_tb_tree (t->right);
4057 : :
4058 : : /* TODO: Free type-bound procedure u.generic */
4059 : 7063 : free (t->n.tb);
4060 : 7063 : t->n.tb = NULL;
4061 : 7063 : free (t);
4062 : : }
4063 : :
4064 : :
4065 : : /* Recursive function that deletes an entire tree and all the common
4066 : : head structures it points to. */
4067 : :
4068 : : static void
4069 : 510852 : free_common_tree (gfc_symtree * common_tree)
4070 : : {
4071 : 510852 : if (common_tree == NULL)
4072 : : return;
4073 : :
4074 : 1898 : free_common_tree (common_tree->left);
4075 : 1898 : free_common_tree (common_tree->right);
4076 : :
4077 : 1898 : free (common_tree);
4078 : : }
4079 : :
4080 : :
4081 : : /* Recursive function that deletes an entire tree and all the common
4082 : : head structures it points to. */
4083 : :
4084 : : static void
4085 : 508064 : free_omp_udr_tree (gfc_symtree * omp_udr_tree)
4086 : : {
4087 : 508064 : if (omp_udr_tree == NULL)
4088 : : return;
4089 : :
4090 : 504 : free_omp_udr_tree (omp_udr_tree->left);
4091 : 504 : free_omp_udr_tree (omp_udr_tree->right);
4092 : :
4093 : 504 : gfc_free_omp_udr (omp_udr_tree->n.omp_udr);
4094 : 504 : free (omp_udr_tree);
4095 : : }
4096 : :
4097 : :
4098 : : /* Recursive function that deletes an entire tree and all the user
4099 : : operator nodes that it contains. */
4100 : :
4101 : : static void
4102 : 507812 : free_uop_tree (gfc_symtree *uop_tree)
4103 : : {
4104 : 507812 : if (uop_tree == NULL)
4105 : : return;
4106 : :
4107 : 378 : free_uop_tree (uop_tree->left);
4108 : 378 : free_uop_tree (uop_tree->right);
4109 : :
4110 : 378 : gfc_free_interface (uop_tree->n.uop->op);
4111 : 378 : free (uop_tree->n.uop);
4112 : 378 : free (uop_tree);
4113 : : }
4114 : :
4115 : :
4116 : : /* Recursive function that deletes an entire tree and all the symbols
4117 : : that it contains. */
4118 : :
4119 : : static void
4120 : 4507638 : free_sym_tree (gfc_symtree *sym_tree)
4121 : : {
4122 : 4507638 : if (sym_tree == NULL)
4123 : : return;
4124 : :
4125 : 2000291 : free_sym_tree (sym_tree->left);
4126 : 2000291 : free_sym_tree (sym_tree->right);
4127 : :
4128 : 2000291 : gfc_release_symbol (sym_tree->n.sym);
4129 : 2000291 : free (sym_tree);
4130 : : }
4131 : :
4132 : :
4133 : : /* Free the gfc_equiv_info's. */
4134 : :
4135 : : static void
4136 : 14655 : gfc_free_equiv_infos (gfc_equiv_info *s)
4137 : : {
4138 : 14655 : if (s == NULL)
4139 : : return;
4140 : 8108 : gfc_free_equiv_infos (s->next);
4141 : 8108 : free (s);
4142 : : }
4143 : :
4144 : :
4145 : : /* Free the gfc_equiv_lists. */
4146 : :
4147 : : static void
4148 : 513603 : gfc_free_equiv_lists (gfc_equiv_list *l)
4149 : : {
4150 : 513603 : if (l == NULL)
4151 : : return;
4152 : 6547 : gfc_free_equiv_lists (l->next);
4153 : 6547 : gfc_free_equiv_infos (l->equiv);
4154 : 6547 : free (l);
4155 : : }
4156 : :
4157 : :
4158 : : /* Free a finalizer procedure list. */
4159 : :
4160 : : void
4161 : 962 : gfc_free_finalizer (gfc_finalizer* el)
4162 : : {
4163 : 962 : if (el)
4164 : : {
4165 : 962 : gfc_release_symbol (el->proc_sym);
4166 : 962 : free (el);
4167 : : }
4168 : 962 : }
4169 : :
4170 : : static void
4171 : 507056 : gfc_free_finalizer_list (gfc_finalizer* list)
4172 : : {
4173 : 508004 : while (list)
4174 : : {
4175 : 948 : gfc_finalizer* current = list;
4176 : 948 : list = list->next;
4177 : 948 : gfc_free_finalizer (current);
4178 : : }
4179 : 507056 : }
4180 : :
4181 : :
4182 : : /* Create a new gfc_charlen structure and add it to a namespace.
4183 : : If 'old_cl' is given, the newly created charlen will be a copy of it. */
4184 : :
4185 : : gfc_charlen*
4186 : 291609 : gfc_new_charlen (gfc_namespace *ns, gfc_charlen *old_cl)
4187 : : {
4188 : 291609 : gfc_charlen *cl;
4189 : :
4190 : 291609 : cl = gfc_get_charlen ();
4191 : :
4192 : : /* Copy old_cl. */
4193 : 291609 : if (old_cl)
4194 : : {
4195 : 14688 : cl->length = gfc_copy_expr (old_cl->length);
4196 : 14688 : cl->length_from_typespec = old_cl->length_from_typespec;
4197 : 14688 : cl->backend_decl = old_cl->backend_decl;
4198 : 14688 : cl->passed_length = old_cl->passed_length;
4199 : 14688 : cl->resolved = old_cl->resolved;
4200 : : }
4201 : :
4202 : : /* Put into namespace. */
4203 : 291609 : cl->next = ns->cl_list;
4204 : 291609 : ns->cl_list = cl;
4205 : :
4206 : 291609 : return cl;
4207 : : }
4208 : :
4209 : :
4210 : : /* Free the charlen list from cl to end (end is not freed).
4211 : : Free the whole list if end is NULL. */
4212 : :
4213 : : static void
4214 : 507056 : gfc_free_charlen (gfc_charlen *cl, gfc_charlen *end)
4215 : : {
4216 : 507056 : gfc_charlen *cl2;
4217 : :
4218 : 798389 : for (; cl != end; cl = cl2)
4219 : : {
4220 : 291333 : gcc_assert (cl);
4221 : :
4222 : 291333 : cl2 = cl->next;
4223 : 291333 : gfc_free_expr (cl->length);
4224 : 291333 : free (cl);
4225 : : }
4226 : 507056 : }
4227 : :
4228 : :
4229 : : /* Free entry list structs. */
4230 : :
4231 : : static void
4232 : 0 : free_entry_list (gfc_entry_list *el)
4233 : : {
4234 : 508476 : gfc_entry_list *next;
4235 : :
4236 : 508476 : if (el == NULL)
4237 : 0 : return;
4238 : :
4239 : 1420 : next = el->next;
4240 : 1420 : free (el);
4241 : 1420 : free_entry_list (next);
4242 : : }
4243 : :
4244 : :
4245 : : /* Free a namespace structure and everything below it. Interface
4246 : : lists associated with intrinsic operators are not freed. These are
4247 : : taken care of when a specific name is freed. */
4248 : :
4249 : : void
4250 : 12251494 : gfc_free_namespace (gfc_namespace *&ns)
4251 : : {
4252 : 12251494 : gfc_namespace *p, *q;
4253 : 12251494 : int i;
4254 : 12251494 : gfc_was_finalized *f;
4255 : :
4256 : 12251494 : if (ns == NULL)
4257 : 11744438 : return;
4258 : :
4259 : 532878 : ns->refs--;
4260 : 532878 : if (ns->refs > 0)
4261 : : return;
4262 : :
4263 : 507056 : gcc_assert (ns->refs == 0);
4264 : :
4265 : 507056 : gfc_free_statements (ns->code);
4266 : :
4267 : 507056 : free_sym_tree (ns->sym_root);
4268 : 507056 : free_uop_tree (ns->uop_root);
4269 : 507056 : free_common_tree (ns->common_root);
4270 : 507056 : free_omp_udr_tree (ns->omp_udr_root);
4271 : 507056 : free_tb_tree (ns->tb_sym_root);
4272 : 507056 : free_tb_tree (ns->tb_uop_root);
4273 : 507056 : gfc_free_finalizer_list (ns->finalizers);
4274 : 507056 : gfc_free_omp_declare_simd_list (ns->omp_declare_simd);
4275 : 507056 : gfc_free_omp_declare_variant_list (ns->omp_declare_variant);
4276 : 507056 : gfc_free_charlen (ns->cl_list, NULL);
4277 : 507056 : free_st_labels (ns->st_labels);
4278 : :
4279 : 507056 : free_entry_list (ns->entries);
4280 : 507056 : gfc_free_equiv (ns->equiv);
4281 : 507056 : gfc_free_equiv_lists (ns->equiv_lists);
4282 : 507056 : gfc_free_use_stmts (ns->use_stmts);
4283 : :
4284 : 15211680 : for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
4285 : 14197568 : gfc_free_interface (ns->op[i]);
4286 : :
4287 : 507056 : gfc_free_data (ns->data);
4288 : :
4289 : : /* Free all the expr + component combinations that have been
4290 : : finalized. */
4291 : 507056 : f = ns->was_finalized;
4292 : 509427 : while (f)
4293 : : {
4294 : 2371 : gfc_was_finalized* current = f;
4295 : 2371 : f = f->next;
4296 : 2371 : free (current);
4297 : : }
4298 : 507056 : if (ns->omp_assumes)
4299 : : {
4300 : 19 : free (ns->omp_assumes->absent);
4301 : 19 : free (ns->omp_assumes->contains);
4302 : 19 : gfc_free_expr_list (ns->omp_assumes->holds);
4303 : 19 : free (ns->omp_assumes);
4304 : : }
4305 : 507056 : p = ns->contained;
4306 : 507056 : free (ns);
4307 : 507056 : ns = NULL;
4308 : :
4309 : : /* Recursively free any contained namespaces. */
4310 : 556145 : while (p != NULL)
4311 : : {
4312 : 49089 : q = p;
4313 : 49089 : p = p->sibling;
4314 : 49089 : gfc_free_namespace (q);
4315 : : }
4316 : : }
4317 : :
4318 : :
4319 : : void
4320 : 79051 : gfc_symbol_init_2 (void)
4321 : : {
4322 : :
4323 : 79051 : gfc_current_ns = gfc_get_namespace (NULL, 0);
4324 : 79051 : }
4325 : :
4326 : :
4327 : : void
4328 : 79379 : gfc_symbol_done_2 (void)
4329 : : {
4330 : 79379 : if (gfc_current_ns != NULL)
4331 : : {
4332 : : /* free everything from the root. */
4333 : 79393 : while (gfc_current_ns->parent != NULL)
4334 : 14 : gfc_current_ns = gfc_current_ns->parent;
4335 : 79379 : gfc_free_namespace (gfc_current_ns);
4336 : 79379 : gfc_current_ns = NULL;
4337 : : }
4338 : 79379 : gfc_derived_types = NULL;
4339 : :
4340 : 79379 : enforce_single_undo_checkpoint ();
4341 : 79379 : free_undo_change_set_data (*latest_undo_chgset);
4342 : 79379 : }
4343 : :
4344 : :
4345 : : /* Count how many nodes a symtree has. */
4346 : :
4347 : : static unsigned
4348 : 25379738 : count_st_nodes (const gfc_symtree *st)
4349 : : {
4350 : 47388747 : unsigned nodes;
4351 : 47388747 : if (!st)
4352 : 25379738 : return 0;
4353 : :
4354 : 22009009 : nodes = count_st_nodes (st->left);
4355 : 22009009 : nodes++;
4356 : 22009009 : nodes += count_st_nodes (st->right);
4357 : :
4358 : 22009009 : return nodes;
4359 : : }
4360 : :
4361 : :
4362 : : /* Convert symtree tree into symtree vector. */
4363 : :
4364 : : static unsigned
4365 : 25379738 : fill_st_vector (gfc_symtree *st, gfc_symtree **st_vec, unsigned node_cntr)
4366 : : {
4367 : 47388747 : if (!st)
4368 : 25379738 : return node_cntr;
4369 : :
4370 : 22009009 : node_cntr = fill_st_vector (st->left, st_vec, node_cntr);
4371 : 22009009 : st_vec[node_cntr++] = st;
4372 : 22009009 : node_cntr = fill_st_vector (st->right, st_vec, node_cntr);
4373 : :
4374 : 22009009 : return node_cntr;
4375 : : }
4376 : :
4377 : :
4378 : : /* Traverse namespace. As the functions might modify the symtree, we store the
4379 : : symtree as a vector and operate on this vector. Note: We assume that
4380 : : sym_func or st_func never deletes nodes from the symtree - only adding is
4381 : : allowed. Additionally, newly added nodes are not traversed. */
4382 : :
4383 : : static void
4384 : 3370729 : do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *),
4385 : : void (*sym_func) (gfc_symbol *))
4386 : : {
4387 : 3370729 : gfc_symtree **st_vec;
4388 : 3370729 : unsigned nodes, i, node_cntr;
4389 : :
4390 : 3370729 : gcc_assert ((st_func && !sym_func) || (!st_func && sym_func));
4391 : 3370729 : nodes = count_st_nodes (st);
4392 : 3370729 : st_vec = XALLOCAVEC (gfc_symtree *, nodes);
4393 : 3370729 : node_cntr = 0;
4394 : 3370729 : fill_st_vector (st, st_vec, node_cntr);
4395 : :
4396 : 3370729 : if (sym_func)
4397 : : {
4398 : : /* Clear marks. */
4399 : 25083891 : for (i = 0; i < nodes; i++)
4400 : 21844141 : st_vec[i]->n.sym->mark = 0;
4401 : 25083891 : for (i = 0; i < nodes; i++)
4402 : 21844141 : if (!st_vec[i]->n.sym->mark)
4403 : : {
4404 : 21290339 : (*sym_func) (st_vec[i]->n.sym);
4405 : 21290339 : st_vec[i]->n.sym->mark = 1;
4406 : : }
4407 : : }
4408 : : else
4409 : 295847 : for (i = 0; i < nodes; i++)
4410 : 164868 : (*st_func) (st_vec[i]);
4411 : 3370729 : }
4412 : :
4413 : :
4414 : : /* Recursively traverse the symtree nodes. */
4415 : :
4416 : : void
4417 : 130979 : gfc_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *))
4418 : : {
4419 : 130979 : do_traverse_symtree (st, st_func, NULL);
4420 : 130979 : }
4421 : :
4422 : :
4423 : : /* Call a given function for all symbols in the namespace. We take
4424 : : care that each gfc_symbol node is called exactly once. */
4425 : :
4426 : : void
4427 : 3239750 : gfc_traverse_ns (gfc_namespace *ns, void (*sym_func) (gfc_symbol *))
4428 : : {
4429 : 3239750 : do_traverse_symtree (ns->sym_root, NULL, sym_func);
4430 : 3239750 : }
4431 : :
4432 : :
4433 : : /* Return TRUE when name is the name of an intrinsic type. */
4434 : :
4435 : : bool
4436 : 13039 : gfc_is_intrinsic_typename (const char *name)
4437 : : {
4438 : 13039 : if (strcmp (name, "integer") == 0
4439 : 13036 : || strcmp (name, "real") == 0
4440 : 13033 : || strcmp (name, "character") == 0
4441 : 13031 : || strcmp (name, "logical") == 0
4442 : 13029 : || strcmp (name, "complex") == 0
4443 : 13025 : || strcmp (name, "doubleprecision") == 0
4444 : 13022 : || strcmp (name, "doublecomplex") == 0)
4445 : : return true;
4446 : : else
4447 : 13019 : return false;
4448 : : }
4449 : :
4450 : :
4451 : : /* Return TRUE if the symbol is an automatic variable. */
4452 : :
4453 : : static bool
4454 : 819 : gfc_is_var_automatic (gfc_symbol *sym)
4455 : : {
4456 : : /* Pointer and allocatable variables are never automatic. */
4457 : 819 : if (sym->attr.pointer || sym->attr.allocatable)
4458 : : return false;
4459 : : /* Check for arrays with non-constant size. */
4460 : 70 : if (sym->attr.dimension && sym->as
4461 : 815 : && !gfc_is_compile_time_shape (sym->as))
4462 : : return true;
4463 : : /* Check for non-constant length character variables. */
4464 : 735 : if (sym->ts.type == BT_CHARACTER
4465 : 62 : && sym->ts.u.cl
4466 : 797 : && !gfc_is_constant_expr (sym->ts.u.cl->length))
4467 : : return true;
4468 : : /* Variables with explicit AUTOMATIC attribute. */
4469 : 727 : if (sym->attr.automatic)
4470 : : return true;
4471 : :
4472 : : return false;
4473 : : }
4474 : :
4475 : : /* Given a symbol, mark it as SAVEd if it is allowed. */
4476 : :
4477 : : static void
4478 : 2680 : save_symbol (gfc_symbol *sym)
4479 : : {
4480 : :
4481 : 2680 : if (sym->attr.use_assoc)
4482 : : return;
4483 : :
4484 : 2234 : if (sym->attr.in_common
4485 : 2218 : || sym->attr.in_equivalence
4486 : 2060 : || sym->attr.dummy
4487 : 1835 : || sym->attr.result
4488 : 1827 : || sym->attr.flavor != FL_VARIABLE)
4489 : : return;
4490 : : /* Automatic objects are not saved. */
4491 : 819 : if (gfc_is_var_automatic (sym))
4492 : : return;
4493 : 788 : gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name, &sym->declared_at);
4494 : : }
4495 : :
4496 : :
4497 : : /* Mark those symbols which can be SAVEd as such. */
4498 : :
4499 : : void
4500 : 297 : gfc_save_all (gfc_namespace *ns)
4501 : : {
4502 : 297 : gfc_traverse_ns (ns, save_symbol);
4503 : 297 : }
4504 : :
4505 : :
4506 : : /* Make sure that no changes to symbols are pending. */
4507 : :
4508 : : void
4509 : 6135112 : gfc_enforce_clean_symbol_state(void)
4510 : : {
4511 : 6135112 : enforce_single_undo_checkpoint ();
4512 : 6135112 : gcc_assert (latest_undo_chgset->syms.is_empty ());
4513 : 6135112 : }
4514 : :
4515 : :
4516 : : /************** Global symbol handling ************/
4517 : :
4518 : :
4519 : : /* Search a tree for the global symbol. */
4520 : :
4521 : : gfc_gsymbol *
4522 : 384165 : gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
4523 : : {
4524 : 384165 : int c;
4525 : :
4526 : 384165 : if (symbol == NULL)
4527 : : return NULL;
4528 : :
4529 : 1287525 : while (symbol)
4530 : : {
4531 : 1071250 : c = strcmp (name, symbol->name);
4532 : 1071250 : if (!c)
4533 : : return symbol;
4534 : :
4535 : 944167 : symbol = (c < 0) ? symbol->left : symbol->right;
4536 : : }
4537 : :
4538 : : return NULL;
4539 : : }
4540 : :
4541 : :
4542 : : /* Case insensitive search a tree for the global symbol. */
4543 : :
4544 : : gfc_gsymbol *
4545 : 33049 : gfc_find_case_gsymbol (gfc_gsymbol *symbol, const char *name)
4546 : : {
4547 : 33049 : int c;
4548 : :
4549 : 33049 : if (symbol == NULL)
4550 : : return NULL;
4551 : :
4552 : 132548 : while (symbol)
4553 : : {
4554 : 111177 : c = strcasecmp (name, symbol->name);
4555 : 111177 : if (!c)
4556 : : return symbol;
4557 : :
4558 : 99854 : symbol = (c < 0) ? symbol->left : symbol->right;
4559 : : }
4560 : :
4561 : : return NULL;
4562 : : }
4563 : :
4564 : :
4565 : : /* Compare two global symbols. Used for managing the BB tree. */
4566 : :
4567 : : static int
4568 : 159084 : gsym_compare (void *_s1, void *_s2)
4569 : : {
4570 : 159084 : gfc_gsymbol *s1, *s2;
4571 : :
4572 : 159084 : s1 = (gfc_gsymbol *) _s1;
4573 : 159084 : s2 = (gfc_gsymbol *) _s2;
4574 : 159084 : return strcmp (s1->name, s2->name);
4575 : : }
4576 : :
4577 : :
4578 : : /* Get a global symbol, creating it if it doesn't exist. */
4579 : :
4580 : : gfc_gsymbol *
4581 : 110856 : gfc_get_gsymbol (const char *name, bool bind_c)
4582 : : {
4583 : 110856 : gfc_gsymbol *s;
4584 : :
4585 : 110856 : s = gfc_find_gsymbol (gfc_gsym_root, name);
4586 : 110856 : if (s != NULL)
4587 : : return s;
4588 : :
4589 : 85610 : s = XCNEW (gfc_gsymbol);
4590 : 85610 : s->type = GSYM_UNKNOWN;
4591 : 85610 : s->name = gfc_get_string ("%s", name);
4592 : 85610 : s->bind_c = bind_c;
4593 : :
4594 : 85610 : gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
4595 : :
4596 : 85610 : return s;
4597 : : }
4598 : :
4599 : : void
4600 : 0 : gfc_traverse_gsymbol (gfc_gsymbol *gsym,
4601 : : void (*do_something) (gfc_gsymbol *, void *),
4602 : : void *data)
4603 : : {
4604 : 0 : if (gsym->left)
4605 : 0 : gfc_traverse_gsymbol (gsym->left, do_something, data);
4606 : :
4607 : 0 : (*do_something) (gsym, data);
4608 : :
4609 : 0 : if (gsym->right)
4610 : : gfc_traverse_gsymbol (gsym->right, do_something, data);
4611 : 0 : }
4612 : :
4613 : : static gfc_symbol *
4614 : 52 : get_iso_c_binding_dt (int sym_id)
4615 : : {
4616 : 52 : gfc_symbol *dt_list = gfc_derived_types;
4617 : :
4618 : : /* Loop through the derived types in the name list, searching for
4619 : : the desired symbol from iso_c_binding. Search the parent namespaces
4620 : : if necessary and requested to (parent_flag). */
4621 : 52 : if (dt_list)
4622 : : {
4623 : 25 : while (dt_list->dt_next != gfc_derived_types)
4624 : : {
4625 : 0 : if (dt_list->from_intmod != INTMOD_NONE
4626 : 0 : && dt_list->intmod_sym_id == sym_id)
4627 : : return dt_list;
4628 : :
4629 : : dt_list = dt_list->dt_next;
4630 : : }
4631 : : }
4632 : :
4633 : : return NULL;
4634 : : }
4635 : :
4636 : :
4637 : : /* Verifies that the given derived type symbol, derived_sym, is interoperable
4638 : : with C. This is necessary for any derived type that is BIND(C) and for
4639 : : derived types that are parameters to functions that are BIND(C). All
4640 : : fields of the derived type are required to be interoperable, and are tested
4641 : : for such. If an error occurs, the errors are reported here, allowing for
4642 : : multiple errors to be handled for a single derived type. */
4643 : :
4644 : : bool
4645 : 26431 : verify_bind_c_derived_type (gfc_symbol *derived_sym)
4646 : : {
4647 : 26431 : gfc_component *curr_comp = NULL;
4648 : 26431 : bool is_c_interop = false;
4649 : 26431 : bool retval = true;
4650 : :
4651 : 26431 : if (derived_sym == NULL)
4652 : 0 : gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
4653 : : "unexpectedly NULL");
4654 : :
4655 : : /* If we've already looked at this derived symbol, do not look at it again
4656 : : so we don't repeat warnings/errors. */
4657 : 26431 : if (derived_sym->ts.is_c_interop)
4658 : : return true;
4659 : :
4660 : : /* The derived type must have the BIND attribute to be interoperable
4661 : : J3/04-007, Section 15.2.3. */
4662 : 406 : if (derived_sym->attr.is_bind_c != 1)
4663 : : {
4664 : 2 : derived_sym->ts.is_c_interop = 0;
4665 : 2 : gfc_error_now ("Derived type %qs declared at %L must have the BIND "
4666 : : "attribute to be C interoperable", derived_sym->name,
4667 : : &(derived_sym->declared_at));
4668 : 2 : retval = false;
4669 : : }
4670 : :
4671 : 406 : curr_comp = derived_sym->components;
4672 : :
4673 : : /* Fortran 2003 allows an empty derived type. C99 appears to disallow an
4674 : : empty struct. Section 15.2 in Fortran 2003 states: "The following
4675 : : subclauses define the conditions under which a Fortran entity is
4676 : : interoperable. If a Fortran entity is interoperable, an equivalent
4677 : : entity may be defined by means of C and the Fortran entity is said
4678 : : to be interoperable with the C entity. There does not have to be such
4679 : : an interoperating C entity."
4680 : :
4681 : : However, later discussion on the J3 mailing list
4682 : : (https://mailman.j3-fortran.org/pipermail/j3/2021-July/013190.html)
4683 : : found this to be a defect, and Fortran 2018 added in section 18.3.4
4684 : : the following constraint:
4685 : : "C1805: A derived type with the BIND attribute shall have at least one
4686 : : component."
4687 : :
4688 : : We thus allow empty derived types only as GNU extension while giving a
4689 : : warning by default, or reject empty types in standard conformance mode.
4690 : : */
4691 : 406 : if (curr_comp == NULL)
4692 : : {
4693 : 2 : if (!gfc_notify_std (GFC_STD_GNU, "Derived type %qs with BIND(C) "
4694 : : "attribute at %L has no components",
4695 : : derived_sym->name, &(derived_sym->declared_at)))
4696 : : return false;
4697 : 1 : else if (!pedantic)
4698 : : /* Generally emit warning, but not twice if -pedantic is given. */
4699 : 1 : gfc_warning (0, "Derived type %qs with BIND(C) attribute at %L "
4700 : : "is empty, and may be inaccessible by the C "
4701 : : "companion processor",
4702 : : derived_sym->name, &(derived_sym->declared_at));
4703 : 1 : derived_sym->ts.is_c_interop = 1;
4704 : 1 : derived_sym->attr.is_bind_c = 1;
4705 : 1 : return true;
4706 : : }
4707 : :
4708 : :
4709 : : /* Initialize the derived type as being C interoperable.
4710 : : If we find an error in the components, this will be set false. */
4711 : 404 : derived_sym->ts.is_c_interop = 1;
4712 : :
4713 : : /* Loop through the list of components to verify that the kind of
4714 : : each is a C interoperable type. */
4715 : 853 : do
4716 : : {
4717 : : /* The components cannot be pointers (fortran sense).
4718 : : J3/04-007, Section 15.2.3, C1505. */
4719 : 853 : if (curr_comp->attr.pointer != 0)
4720 : : {
4721 : 3 : gfc_error ("Component %qs at %L cannot have the "
4722 : : "POINTER attribute because it is a member "
4723 : : "of the BIND(C) derived type %qs at %L",
4724 : : curr_comp->name, &(curr_comp->loc),
4725 : : derived_sym->name, &(derived_sym->declared_at));
4726 : 3 : retval = false;
4727 : : }
4728 : :
4729 : 853 : if (curr_comp->attr.proc_pointer != 0)
4730 : : {
4731 : 1 : gfc_error ("Procedure pointer component %qs at %L cannot be a member"
4732 : : " of the BIND(C) derived type %qs at %L", curr_comp->name,
4733 : : &curr_comp->loc, derived_sym->name,
4734 : : &derived_sym->declared_at);
4735 : 1 : retval = false;
4736 : : }
4737 : :
4738 : : /* The components cannot be allocatable.
4739 : : J3/04-007, Section 15.2.3, C1505. */
4740 : 853 : if (curr_comp->attr.allocatable != 0)
4741 : : {
4742 : 3 : gfc_error ("Component %qs at %L cannot have the "
4743 : : "ALLOCATABLE attribute because it is a member "
4744 : : "of the BIND(C) derived type %qs at %L",
4745 : : curr_comp->name, &(curr_comp->loc),
4746 : : derived_sym->name, &(derived_sym->declared_at));
4747 : 3 : retval = false;
4748 : : }
4749 : :
4750 : : /* BIND(C) derived types must have interoperable components. */
4751 : 853 : if (curr_comp->ts.type == BT_DERIVED
4752 : 71 : && curr_comp->ts.u.derived->ts.is_iso_c != 1
4753 : 17 : && curr_comp->ts.u.derived != derived_sym)
4754 : : {
4755 : : /* This should be allowed; the draft says a derived-type cannot
4756 : : have type parameters if it is has the BIND attribute. Type
4757 : : parameters seem to be for making parameterized derived types.
4758 : : There's no need to verify the type if it is c_ptr/c_funptr. */
4759 : 16 : retval = verify_bind_c_derived_type (curr_comp->ts.u.derived);
4760 : : }
4761 : : else
4762 : : {
4763 : : /* Grab the typespec for the given component and test the kind. */
4764 : 837 : is_c_interop = gfc_verify_c_interop (&(curr_comp->ts));
4765 : :
4766 : 837 : if (!is_c_interop)
4767 : : {
4768 : : /* Report warning and continue since not fatal. The
4769 : : draft does specify a constraint that requires all fields
4770 : : to interoperate, but if the user says real(4), etc., it
4771 : : may interoperate with *something* in C, but the compiler
4772 : : most likely won't know exactly what. Further, it may not
4773 : : interoperate with the same data type(s) in C if the user
4774 : : recompiles with different flags (e.g., -m32 and -m64 on
4775 : : x86_64 and using integer(4) to claim interop with a
4776 : : C_LONG). */
4777 : 34 : if (derived_sym->attr.is_bind_c == 1 && warn_c_binding_type)
4778 : : /* If the derived type is bind(c), all fields must be
4779 : : interop. */
4780 : 1 : gfc_warning (OPT_Wc_binding_type,
4781 : : "Component %qs in derived type %qs at %L "
4782 : : "may not be C interoperable, even though "
4783 : : "derived type %qs is BIND(C)",
4784 : : curr_comp->name, derived_sym->name,
4785 : : &(curr_comp->loc), derived_sym->name);
4786 : 33 : else if (warn_c_binding_type)
4787 : : /* If derived type is param to bind(c) routine, or to one
4788 : : of the iso_c_binding procs, it must be interoperable, so
4789 : : all fields must interop too. */
4790 : 0 : gfc_warning (OPT_Wc_binding_type,
4791 : : "Component %qs in derived type %qs at %L "
4792 : : "may not be C interoperable",
4793 : : curr_comp->name, derived_sym->name,
4794 : : &(curr_comp->loc));
4795 : : }
4796 : : }
4797 : :
4798 : 853 : curr_comp = curr_comp->next;
4799 : 853 : } while (curr_comp != NULL);
4800 : :
4801 : 404 : if (derived_sym->attr.sequence != 0)
4802 : : {
4803 : 0 : gfc_error ("Derived type %qs at %L cannot have the SEQUENCE "
4804 : : "attribute because it is BIND(C)", derived_sym->name,
4805 : : &(derived_sym->declared_at));
4806 : 0 : retval = false;
4807 : : }
4808 : :
4809 : : /* Mark the derived type as not being C interoperable if we found an
4810 : : error. If there were only warnings, proceed with the assumption
4811 : : it's interoperable. */
4812 : 404 : if (!retval)
4813 : 8 : derived_sym->ts.is_c_interop = 0;
4814 : :
4815 : : return retval;
4816 : : }
4817 : :
4818 : :
4819 : : /* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
4820 : :
4821 : : static bool
4822 : 6326 : gen_special_c_interop_ptr (gfc_symbol *tmp_sym, gfc_symtree *dt_symtree)
4823 : : {
4824 : 6326 : gfc_constructor *c;
4825 : :
4826 : 6326 : gcc_assert (tmp_sym && dt_symtree && dt_symtree->n.sym);
4827 : 6326 : dt_symtree->n.sym->attr.referenced = 1;
4828 : :
4829 : 6326 : tmp_sym->attr.is_c_interop = 1;
4830 : 6326 : tmp_sym->attr.is_bind_c = 1;
4831 : 6326 : tmp_sym->ts.is_c_interop = 1;
4832 : 6326 : tmp_sym->ts.is_iso_c = 1;
4833 : 6326 : tmp_sym->ts.type = BT_DERIVED;
4834 : 6326 : tmp_sym->ts.f90_type = BT_VOID;
4835 : 6326 : tmp_sym->attr.flavor = FL_PARAMETER;
4836 : 6326 : tmp_sym->ts.u.derived = dt_symtree->n.sym;
4837 : :
4838 : : /* Set the c_address field of c_null_ptr and c_null_funptr to
4839 : : the value of NULL. */
4840 : 6326 : tmp_sym->value = gfc_get_expr ();
4841 : 6326 : tmp_sym->value->expr_type = EXPR_STRUCTURE;
4842 : 6326 : tmp_sym->value->ts.type = BT_DERIVED;
4843 : 6326 : tmp_sym->value->ts.f90_type = BT_VOID;
4844 : 6326 : tmp_sym->value->ts.u.derived = tmp_sym->ts.u.derived;
4845 : 6326 : gfc_constructor_append_expr (&tmp_sym->value->value.constructor, NULL, NULL);
4846 : 6326 : c = gfc_constructor_first (tmp_sym->value->value.constructor);
4847 : 6326 : c->expr = gfc_get_int_expr (gfc_index_integer_kind, NULL, 0);
4848 : 6326 : c->expr->ts.is_iso_c = 1;
4849 : :
4850 : 6326 : return true;
4851 : : }
4852 : :
4853 : :
4854 : : /* Add a formal argument, gfc_formal_arglist, to the
4855 : : end of the given list of arguments. Set the reference to the
4856 : : provided symbol, param_sym, in the argument. */
4857 : :
4858 : : static void
4859 : 93819 : add_formal_arg (gfc_formal_arglist **head,
4860 : : gfc_formal_arglist **tail,
4861 : : gfc_formal_arglist *formal_arg,
4862 : : gfc_symbol *param_sym)
4863 : : {
4864 : : /* Put in list, either as first arg or at the tail (curr arg). */
4865 : 0 : if (*head == NULL)
4866 : 0 : *head = *tail = formal_arg;
4867 : : else
4868 : : {
4869 : 57387 : (*tail)->next = formal_arg;
4870 : 57387 : (*tail) = formal_arg;
4871 : : }
4872 : :
4873 : 93819 : (*tail)->sym = param_sym;
4874 : 93819 : (*tail)->next = NULL;
4875 : :
4876 : 93819 : return;
4877 : : }
4878 : :
4879 : :
4880 : : /* Add a procedure interface to the given symbol (i.e., store a
4881 : : reference to the list of formal arguments). */
4882 : :
4883 : : static void
4884 : 37134 : add_proc_interface (gfc_symbol *sym, ifsrc source, gfc_formal_arglist *formal)
4885 : : {
4886 : :
4887 : 37134 : sym->formal = formal;
4888 : 37134 : sym->attr.if_source = source;
4889 : 0 : }
4890 : :
4891 : :
4892 : : /* Copy the formal args from an existing symbol, src, into a new
4893 : : symbol, dest. New formal args are created, and the description of
4894 : : each arg is set according to the existing ones. This function is
4895 : : used when creating procedure declaration variables from a procedure
4896 : : declaration statement (see match_proc_decl()) to create the formal
4897 : : args based on the args of a given named interface.
4898 : :
4899 : : When an actual argument list is provided, skip the absent arguments
4900 : : unless copy_type is true.
4901 : : To be used together with gfc_se->ignore_optional. */
4902 : :
4903 : : void
4904 : 37134 : gfc_copy_formal_args_intr (gfc_symbol *dest, gfc_intrinsic_sym *src,
4905 : : gfc_actual_arglist *actual, bool copy_type)
4906 : : {
4907 : 37134 : gfc_formal_arglist *head = NULL;
4908 : 37134 : gfc_formal_arglist *tail = NULL;
4909 : 37134 : gfc_formal_arglist *formal_arg = NULL;
4910 : 37134 : gfc_intrinsic_arg *curr_arg = NULL;
4911 : 37134 : gfc_formal_arglist *formal_prev = NULL;
4912 : 37134 : gfc_actual_arglist *act_arg = actual;
4913 : : /* Save current namespace so we can change it for formal args. */
4914 : 37134 : gfc_namespace *parent_ns = gfc_current_ns;
4915 : :
4916 : : /* Create a new namespace, which will be the formal ns (namespace
4917 : : of the formal args). */
4918 : 37134 : gfc_current_ns = gfc_get_namespace (parent_ns, 0);
4919 : 37134 : gfc_current_ns->proc_name = dest;
4920 : :
4921 : 133827 : for (curr_arg = src->formal; curr_arg; curr_arg = curr_arg->next)
4922 : : {
4923 : : /* Skip absent arguments. */
4924 : 96693 : if (actual)
4925 : : {
4926 : 14490 : gcc_assert (act_arg != NULL);
4927 : 14490 : if (act_arg->expr == NULL)
4928 : : {
4929 : 2874 : act_arg = act_arg->next;
4930 : 2874 : continue;
4931 : : }
4932 : : }
4933 : 93819 : formal_arg = gfc_get_formal_arglist ();
4934 : 93819 : gfc_get_symbol (curr_arg->name, gfc_current_ns, &(formal_arg->sym));
4935 : :
4936 : : /* May need to copy more info for the symbol. */
4937 : 93819 : if (copy_type && act_arg->expr != NULL)
4938 : : {
4939 : 5720 : formal_arg->sym->ts = act_arg->expr->ts;
4940 : 5720 : if (act_arg->expr->rank > 0)
4941 : : {
4942 : 2575 : formal_arg->sym->attr.dimension = 1;
4943 : 2575 : formal_arg->sym->as = gfc_get_array_spec();
4944 : 2575 : formal_arg->sym->as->rank = -1;
4945 : 2575 : formal_arg->sym->as->type = AS_ASSUMED_RANK;
4946 : : }
4947 : 5720 : if (act_arg->name && strcmp (act_arg->name, "%VAL") == 0)
4948 : 1300 : formal_arg->sym->pass_as_value = 1;
4949 : : }
4950 : : else
4951 : 88099 : formal_arg->sym->ts = curr_arg->ts;
4952 : :
4953 : 93819 : formal_arg->sym->attr.optional = curr_arg->optional;
4954 : 93819 : formal_arg->sym->attr.value = curr_arg->value;
4955 : 93819 : formal_arg->sym->attr.intent = curr_arg->intent;
4956 : 93819 : formal_arg->sym->attr.flavor = FL_VARIABLE;
4957 : 93819 : formal_arg->sym->attr.dummy = 1;
4958 : :
4959 : : /* Do not treat an actual deferred-length character argument wrongly
4960 : : as template for the formal argument. */
4961 : 93819 : if (formal_arg->sym->ts.type == BT_CHARACTER
4962 : 7873 : && !(formal_arg->sym->attr.allocatable
4963 : 7873 : || formal_arg->sym->attr.pointer))
4964 : 7873 : formal_arg->sym->ts.deferred = false;
4965 : :
4966 : 93819 : if (formal_arg->sym->ts.type == BT_CHARACTER)
4967 : 7873 : formal_arg->sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
4968 : :
4969 : : /* If this isn't the first arg, set up the next ptr. For the
4970 : : last arg built, the formal_arg->next will never get set to
4971 : : anything other than NULL. */
4972 : 93819 : if (formal_prev != NULL)
4973 : 57387 : formal_prev->next = formal_arg;
4974 : : else
4975 : : formal_arg->next = NULL;
4976 : :
4977 : 93819 : formal_prev = formal_arg;
4978 : :
4979 : : /* Add arg to list of formal args. */
4980 : 93819 : add_formal_arg (&head, &tail, formal_arg, formal_arg->sym);
4981 : :
4982 : : /* Validate changes. */
4983 : 93819 : gfc_commit_symbol (formal_arg->sym);
4984 : 93819 : if (actual)
4985 : 11616 : act_arg = act_arg->next;
4986 : : }
4987 : :
4988 : : /* Add the interface to the symbol. */
4989 : 37134 : add_proc_interface (dest, IFSRC_DECL, head);
4990 : :
4991 : : /* Store the formal namespace information. */
4992 : 37134 : if (dest->formal != NULL)
4993 : : /* The current ns should be that for the dest proc. */
4994 : 36432 : dest->formal_ns = gfc_current_ns;
4995 : : else
4996 : 702 : gfc_free_namespace (gfc_current_ns);
4997 : : /* Restore the current namespace to what it was on entry. */
4998 : 37134 : gfc_current_ns = parent_ns;
4999 : 37134 : }
5000 : :
5001 : :
5002 : : static int
5003 : 153264 : std_for_isocbinding_symbol (int id)
5004 : : {
5005 : 153264 : switch (id)
5006 : : {
5007 : : #define NAMED_INTCST(a,b,c,d) \
5008 : : case a:\
5009 : : return d;
5010 : : #include "iso-c-binding.def"
5011 : : #undef NAMED_INTCST
5012 : :
5013 : : #define NAMED_UINTCST(a,b,c,d) \
5014 : : case a:\
5015 : : return d;
5016 : : #include "iso-c-binding.def"
5017 : : #undef NAMED_UINTCST
5018 : :
5019 : : #define NAMED_FUNCTION(a,b,c,d) \
5020 : : case a:\
5021 : : return d;
5022 : : #define NAMED_SUBROUTINE(a,b,c,d) \
5023 : : case a:\
5024 : : return d;
5025 : : #include "iso-c-binding.def"
5026 : : #undef NAMED_FUNCTION
5027 : : #undef NAMED_SUBROUTINE
5028 : :
5029 : : default:
5030 : : return GFC_STD_F2003;
5031 : : }
5032 : : }
5033 : :
5034 : : /* Generate the given set of C interoperable kind objects, or all
5035 : : interoperable kinds. This function will only be given kind objects
5036 : : for valid iso_c_binding defined types because this is verified when
5037 : : the 'use' statement is parsed. If the user gives an 'only' clause,
5038 : : the specific kinds are looked up; if they don't exist, an error is
5039 : : reported. If the user does not give an 'only' clause, all
5040 : : iso_c_binding symbols are generated. If a list of specific kinds
5041 : : is given, it must have a NULL in the first empty spot to mark the
5042 : : end of the list. For C_null_(fun)ptr, dt_symtree has to be set and
5043 : : point to the symtree for c_(fun)ptr. */
5044 : :
5045 : : gfc_symtree *
5046 : 153264 : generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
5047 : : const char *local_name, gfc_symtree *dt_symtree,
5048 : : bool hidden)
5049 : : {
5050 : 153264 : const char *const name = (local_name && local_name[0])
5051 : 153264 : ? local_name : c_interop_kinds_table[s].name;
5052 : 153264 : gfc_symtree *tmp_symtree;
5053 : 153264 : gfc_symbol *tmp_sym = NULL;
5054 : 153264 : int index;
5055 : :
5056 : 153264 : if (gfc_notification_std (std_for_isocbinding_symbol (s)) == ERROR)
5057 : : return NULL;
5058 : :
5059 : 153264 : tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
5060 : 153264 : if (hidden
5061 : 48 : && (!tmp_symtree || !tmp_symtree->n.sym
5062 : 14 : || tmp_symtree->n.sym->from_intmod != INTMOD_ISO_C_BINDING
5063 : 14 : || tmp_symtree->n.sym->intmod_sym_id != s))
5064 : 34 : tmp_symtree = NULL;
5065 : :
5066 : : /* Already exists in this scope so don't re-add it. */
5067 : 318 : if (tmp_symtree != NULL && (tmp_sym = tmp_symtree->n.sym) != NULL
5068 : 318 : && (!tmp_sym->attr.generic
5069 : 52 : || (tmp_sym = gfc_find_dt_in_generic (tmp_sym)) != NULL)
5070 : 153582 : && tmp_sym->from_intmod == INTMOD_ISO_C_BINDING)
5071 : : {
5072 : 318 : if (tmp_sym->attr.flavor == FL_DERIVED
5073 : 318 : && !get_iso_c_binding_dt (tmp_sym->intmod_sym_id))
5074 : : {
5075 : 52 : if (gfc_derived_types)
5076 : : {
5077 : 25 : tmp_sym->dt_next = gfc_derived_types->dt_next;
5078 : 25 : gfc_derived_types->dt_next = tmp_sym;
5079 : : }
5080 : : else
5081 : : {
5082 : 27 : tmp_sym->dt_next = tmp_sym;
5083 : : }
5084 : 52 : gfc_derived_types = tmp_sym;
5085 : : }
5086 : :
5087 : 318 : return tmp_symtree;
5088 : : }
5089 : :
5090 : : /* Create the sym tree in the current ns. */
5091 : 152946 : if (hidden)
5092 : : {
5093 : 34 : tmp_symtree = gfc_get_unique_symtree (gfc_current_ns);
5094 : 34 : tmp_sym = gfc_new_symbol (name, gfc_current_ns);
5095 : :
5096 : : /* Add to the list of tentative symbols. */
5097 : 34 : latest_undo_chgset->syms.safe_push (tmp_sym);
5098 : 34 : tmp_sym->old_symbol = NULL;
5099 : 34 : tmp_sym->mark = 1;
5100 : 34 : tmp_sym->gfc_new = 1;
5101 : :
5102 : 34 : tmp_symtree->n.sym = tmp_sym;
5103 : 34 : tmp_sym->refs++;
5104 : : }
5105 : : else
5106 : : {
5107 : 152912 : gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree, false);
5108 : 152912 : gcc_assert (tmp_symtree);
5109 : 152912 : tmp_sym = tmp_symtree->n.sym;
5110 : : }
5111 : :
5112 : : /* Say what module this symbol belongs to. */
5113 : 152946 : tmp_sym->module = gfc_get_string ("%s", mod_name);
5114 : 152946 : tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
5115 : 152946 : tmp_sym->intmod_sym_id = s;
5116 : 152946 : tmp_sym->attr.is_iso_c = 1;
5117 : 152946 : tmp_sym->attr.use_assoc = 1;
5118 : :
5119 : 152946 : gcc_assert (dt_symtree == NULL || s == ISOCBINDING_NULL_FUNPTR
5120 : : || s == ISOCBINDING_NULL_PTR);
5121 : :
5122 : 149757 : switch (s)
5123 : : {
5124 : :
5125 : : #define NAMED_INTCST(a,b,c,d) case a :
5126 : : #define NAMED_UINTCST(a,b,c,d) case a :
5127 : : #define NAMED_REALCST(a,b,c,d) case a :
5128 : : #define NAMED_CMPXCST(a,b,c,d) case a :
5129 : : #define NAMED_LOGCST(a,b,c) case a :
5130 : : #define NAMED_CHARKNDCST(a,b,c) case a :
5131 : : #include "iso-c-binding.def"
5132 : :
5133 : 225946 : tmp_sym->value = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5134 : 112973 : c_interop_kinds_table[s].value);
5135 : :
5136 : : /* Initialize an integer constant expression node. */
5137 : 112973 : tmp_sym->attr.flavor = FL_PARAMETER;
5138 : 112973 : tmp_sym->ts.type = BT_INTEGER;
5139 : 112973 : tmp_sym->ts.kind = gfc_default_integer_kind;
5140 : :
5141 : : /* Mark this type as a C interoperable one. */
5142 : 112973 : tmp_sym->ts.is_c_interop = 1;
5143 : 112973 : tmp_sym->ts.is_iso_c = 1;
5144 : 112973 : tmp_sym->value->ts.is_c_interop = 1;
5145 : 112973 : tmp_sym->value->ts.is_iso_c = 1;
5146 : 112973 : tmp_sym->attr.is_c_interop = 1;
5147 : :
5148 : : /* Tell what f90 type this c interop kind is valid. */
5149 : 112973 : tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
5150 : :
5151 : 112973 : break;
5152 : :
5153 : :
5154 : : #define NAMED_CHARCST(a,b,c) case a :
5155 : : #include "iso-c-binding.def"
5156 : :
5157 : : /* Initialize an integer constant expression node for the
5158 : : length of the character. */
5159 : 25060 : tmp_sym->value = gfc_get_character_expr (gfc_default_character_kind,
5160 : : &gfc_current_locus, NULL, 1);
5161 : 25060 : tmp_sym->value->ts.is_c_interop = 1;
5162 : 25060 : tmp_sym->value->ts.is_iso_c = 1;
5163 : 25060 : tmp_sym->value->value.character.length = 1;
5164 : 25060 : tmp_sym->value->value.character.string[0]
5165 : 25060 : = (gfc_char_t) c_interop_kinds_table[s].value;
5166 : 25060 : tmp_sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5167 : 25060 : tmp_sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
5168 : : NULL, 1);
5169 : :
5170 : : /* May not need this in both attr and ts, but do need in
5171 : : attr for writing module file. */
5172 : 25060 : tmp_sym->attr.is_c_interop = 1;
5173 : :
5174 : 25060 : tmp_sym->attr.flavor = FL_PARAMETER;
5175 : 25060 : tmp_sym->ts.type = BT_CHARACTER;
5176 : :
5177 : : /* Need to set it to the C_CHAR kind. */
5178 : 25060 : tmp_sym->ts.kind = gfc_default_character_kind;
5179 : :
5180 : : /* Mark this type as a C interoperable one. */
5181 : 25060 : tmp_sym->ts.is_c_interop = 1;
5182 : 25060 : tmp_sym->ts.is_iso_c = 1;
5183 : :
5184 : : /* Tell what f90 type this c interop kind is valid. */
5185 : 25060 : tmp_sym->ts.f90_type = BT_CHARACTER;
5186 : :
5187 : 25060 : break;
5188 : :
5189 : 8587 : case ISOCBINDING_PTR:
5190 : 8587 : case ISOCBINDING_FUNPTR:
5191 : 8587 : {
5192 : 8587 : gfc_symbol *dt_sym;
5193 : 8587 : gfc_component *tmp_comp = NULL;
5194 : :
5195 : : /* Generate real derived type. */
5196 : 8587 : if (hidden)
5197 : : dt_sym = tmp_sym;
5198 : : else
5199 : : {
5200 : 8553 : const char *hidden_name;
5201 : 8553 : gfc_interface *intr, *head;
5202 : :
5203 : 8553 : hidden_name = gfc_dt_upper_string (tmp_sym->name);
5204 : 8553 : tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
5205 : : hidden_name);
5206 : 8553 : gcc_assert (tmp_symtree == NULL);
5207 : 8553 : gfc_get_sym_tree (hidden_name, gfc_current_ns, &tmp_symtree, false);
5208 : 8553 : dt_sym = tmp_symtree->n.sym;
5209 : 11781 : dt_sym->name = gfc_get_string (s == ISOCBINDING_PTR
5210 : : ? "c_ptr" : "c_funptr");
5211 : :
5212 : : /* Generate an artificial generic function. */
5213 : 8553 : head = tmp_sym->generic;
5214 : 8553 : intr = gfc_get_interface ();
5215 : 8553 : intr->sym = dt_sym;
5216 : 8553 : intr->where = gfc_current_locus;
5217 : 8553 : intr->next = head;
5218 : 8553 : tmp_sym->generic = intr;
5219 : :
5220 : 8553 : if (!tmp_sym->attr.generic
5221 : 8553 : && !gfc_add_generic (&tmp_sym->attr, tmp_sym->name, NULL))
5222 : 0 : return NULL;
5223 : :
5224 : 8553 : if (!tmp_sym->attr.function
5225 : 8553 : && !gfc_add_function (&tmp_sym->attr, tmp_sym->name, NULL))
5226 : : return NULL;
5227 : : }
5228 : :
5229 : : /* Say what module this symbol belongs to. */
5230 : 8587 : dt_sym->module = gfc_get_string ("%s", mod_name);
5231 : 8587 : dt_sym->from_intmod = INTMOD_ISO_C_BINDING;
5232 : 8587 : dt_sym->intmod_sym_id = s;
5233 : 8587 : dt_sym->attr.use_assoc = 1;
5234 : :
5235 : : /* Initialize an integer constant expression node. */
5236 : 8587 : dt_sym->attr.flavor = FL_DERIVED;
5237 : 8587 : dt_sym->ts.is_c_interop = 1;
5238 : 8587 : dt_sym->attr.is_c_interop = 1;
5239 : 8587 : dt_sym->attr.private_comp = 1;
5240 : 8587 : dt_sym->component_access = ACCESS_PRIVATE;
5241 : 8587 : dt_sym->ts.is_iso_c = 1;
5242 : 8587 : dt_sym->ts.type = BT_DERIVED;
5243 : 8587 : dt_sym->ts.f90_type = BT_VOID;
5244 : :
5245 : : /* A derived type must have the bind attribute to be
5246 : : interoperable (J3/04-007, Section 15.2.3), even though
5247 : : the binding label is not used. */
5248 : 8587 : dt_sym->attr.is_bind_c = 1;
5249 : :
5250 : 8587 : dt_sym->attr.referenced = 1;
5251 : 8587 : dt_sym->ts.u.derived = dt_sym;
5252 : :
5253 : : /* Add the symbol created for the derived type to the current ns. */
5254 : 8587 : if (gfc_derived_types)
5255 : : {
5256 : 6593 : dt_sym->dt_next = gfc_derived_types->dt_next;
5257 : 6593 : gfc_derived_types->dt_next = dt_sym;
5258 : : }
5259 : : else
5260 : : {
5261 : 1994 : dt_sym->dt_next = dt_sym;
5262 : : }
5263 : 8587 : gfc_derived_types = dt_sym;
5264 : :
5265 : 8587 : gfc_add_component (dt_sym, "c_address", &tmp_comp);
5266 : 8587 : if (tmp_comp == NULL)
5267 : 0 : gcc_unreachable ();
5268 : :
5269 : 8587 : tmp_comp->ts.type = BT_INTEGER;
5270 : :
5271 : : /* Set this because the module will need to read/write this field. */
5272 : 8587 : tmp_comp->ts.f90_type = BT_INTEGER;
5273 : :
5274 : : /* The kinds for c_ptr and c_funptr are the same. */
5275 : 8587 : index = get_c_kind ("c_ptr", c_interop_kinds_table);
5276 : 8587 : tmp_comp->ts.kind = c_interop_kinds_table[index].value;
5277 : 8587 : tmp_comp->attr.access = ACCESS_PRIVATE;
5278 : :
5279 : : /* Mark the component as C interoperable. */
5280 : 8587 : tmp_comp->ts.is_c_interop = 1;
5281 : : }
5282 : :
5283 : 8587 : break;
5284 : :
5285 : 6326 : case ISOCBINDING_NULL_PTR:
5286 : 6326 : case ISOCBINDING_NULL_FUNPTR:
5287 : 6326 : gen_special_c_interop_ptr (tmp_sym, dt_symtree);
5288 : 6326 : break;
5289 : :
5290 : 0 : default:
5291 : 0 : gcc_unreachable ();
5292 : : }
5293 : 152946 : gfc_commit_symbol (tmp_sym);
5294 : 152946 : return tmp_symtree;
5295 : : }
5296 : :
5297 : :
5298 : : /* Check that a symbol is already typed. If strict is not set, an untyped
5299 : : symbol is acceptable for non-standard-conforming mode. */
5300 : :
5301 : : bool
5302 : 13738 : gfc_check_symbol_typed (gfc_symbol* sym, gfc_namespace* ns,
5303 : : bool strict, locus where)
5304 : : {
5305 : 13738 : gcc_assert (sym);
5306 : :
5307 : 13738 : if (gfc_matching_prefix)
5308 : : return true;
5309 : :
5310 : : /* Check for the type and try to give it an implicit one. */
5311 : 13695 : if (sym->ts.type == BT_UNKNOWN
5312 : 13695 : && !gfc_set_default_type (sym, 0, ns))
5313 : : {
5314 : 451 : if (strict)
5315 : : {
5316 : 11 : gfc_error ("Symbol %qs is used before it is typed at %L",
5317 : : sym->name, &where);
5318 : 11 : return false;
5319 : : }
5320 : :
5321 : 440 : if (!gfc_notify_std (GFC_STD_GNU, "Symbol %qs is used before"
5322 : : " it is typed at %L", sym->name, &where))
5323 : : return false;
5324 : : }
5325 : :
5326 : : /* Everything is ok. */
5327 : : return true;
5328 : : }
5329 : :
5330 : :
5331 : : /* Construct a typebound-procedure structure. Those are stored in a tentative
5332 : : list and marked `error' until symbols are committed. */
5333 : :
5334 : : gfc_typebound_proc*
5335 : 57133 : gfc_get_typebound_proc (gfc_typebound_proc *tb0)
5336 : : {
5337 : 57133 : gfc_typebound_proc *result;
5338 : :
5339 : 57133 : result = XCNEW (gfc_typebound_proc);
5340 : 57133 : if (tb0)
5341 : 3085 : *result = *tb0;
5342 : 57133 : result->error = 1;
5343 : :
5344 : 57133 : latest_undo_chgset->tbps.safe_push (result);
5345 : :
5346 : 57133 : return result;
5347 : : }
5348 : :
5349 : :
5350 : : /* Get the super-type of a given derived type. */
5351 : :
5352 : : gfc_symbol*
5353 : 645568 : gfc_get_derived_super_type (gfc_symbol* derived)
5354 : : {
5355 : 645568 : gcc_assert (derived);
5356 : :
5357 : 645568 : if (derived->attr.generic)
5358 : 2 : derived = gfc_find_dt_in_generic (derived);
5359 : :
5360 : 645568 : if (!derived->attr.extension)
5361 : : return NULL;
5362 : :
5363 : 121139 : gcc_assert (derived->components);
5364 : 121139 : gcc_assert (derived->components->ts.type == BT_DERIVED);
5365 : 121139 : gcc_assert (derived->components->ts.u.derived);
5366 : :
5367 : 121139 : if (derived->components->ts.u.derived->attr.generic)
5368 : 0 : return gfc_find_dt_in_generic (derived->components->ts.u.derived);
5369 : :
5370 : : return derived->components->ts.u.derived;
5371 : : }
5372 : :
5373 : :
5374 : : /* Check if a derived type t2 is an extension of (or equal to) a type t1. */
5375 : :
5376 : : bool
5377 : 29294 : gfc_type_is_extension_of (gfc_symbol *t1, gfc_symbol *t2)
5378 : : {
5379 : 33293 : while (!gfc_compare_derived_types (t1, t2) && t2->attr.extension)
5380 : 3999 : t2 = gfc_get_derived_super_type (t2);
5381 : 29294 : return gfc_compare_derived_types (t1, t2);
5382 : : }
5383 : :
5384 : : /* Check if parameterized derived type t2 is an instance of pdt template t1
5385 : :
5386 : : gfc_symbol *t1 -> pdt template to verify t2 against.
5387 : : gfc_symbol *t2 -> pdt instance to be verified.
5388 : :
5389 : : In decl.cc, gfc_get_pdt_instance, a pdt instance is given a 3 character
5390 : : prefix "Pdt", followed by an underscore list of the kind parameters,
5391 : : up to a maximum of 8 kind parameters. To verify if a PDT Type corresponds
5392 : : to the template, this functions extracts t2's derive_type name,
5393 : : and compares it to the derive_type name of t1 for compatibility.
5394 : :
5395 : : For example:
5396 : :
5397 : : t2->name = Pdtf_2_2; extract out the 'f' and compare with t1->name. */
5398 : :
5399 : : bool
5400 : 34 : gfc_pdt_is_instance_of (gfc_symbol *t1, gfc_symbol *t2)
5401 : : {
5402 : 34 : if ( !t1->attr.pdt_template || !t2->attr.pdt_type )
5403 : : return false;
5404 : :
5405 : : /* Limit comparison to length of t1->name to ignore new kind params. */
5406 : 34 : if ( !(strncmp (&(t2->name[3]), t1->name, strlen (t1->name)) == 0) )
5407 : 0 : return false;
5408 : :
5409 : : return true;
5410 : : }
5411 : :
5412 : : /* Check if two typespecs are type compatible (F03:5.1.1.2):
5413 : : If ts1 is nonpolymorphic, ts2 must be the same type.
5414 : : If ts1 is polymorphic (CLASS), ts2 must be an extension of ts1. */
5415 : :
5416 : : bool
5417 : 267589 : gfc_type_compatible (gfc_typespec *ts1, gfc_typespec *ts2)
5418 : : {
5419 : 267589 : bool is_class1 = (ts1->type == BT_CLASS);
5420 : 267589 : bool is_class2 = (ts2->type == BT_CLASS);
5421 : 267589 : bool is_derived1 = (ts1->type == BT_DERIVED);
5422 : 267589 : bool is_derived2 = (ts2->type == BT_DERIVED);
5423 : 267589 : bool is_union1 = (ts1->type == BT_UNION);
5424 : 267589 : bool is_union2 = (ts2->type == BT_UNION);
5425 : :
5426 : : /* A boz-literal-constant has no type. */
5427 : 267589 : if (ts1->type == BT_BOZ || ts2->type == BT_BOZ)
5428 : : return false;
5429 : :
5430 : 267587 : if (is_class1
5431 : 27930 : && ts1->u.derived->components
5432 : 27770 : && ((ts1->u.derived->attr.is_class
5433 : 27763 : && ts1->u.derived->components->ts.u.derived->attr
5434 : 27763 : .unlimited_polymorphic)
5435 : 26967 : || ts1->u.derived->attr.unlimited_polymorphic))
5436 : : return 1;
5437 : :
5438 : 266784 : if (!is_derived1 && !is_derived2 && !is_class1 && !is_class2
5439 : 2317 : && !is_union1 && !is_union2)
5440 : 2317 : return (ts1->type == ts2->type);
5441 : :
5442 : 264467 : if ((is_derived1 && is_derived2) || (is_union1 && is_union2))
5443 : 236327 : return gfc_compare_derived_types (ts1->u.derived, ts2->u.derived);
5444 : :
5445 : 28140 : if (is_derived1 && is_class2)
5446 : 1009 : return gfc_compare_derived_types (ts1->u.derived,
5447 : 1009 : ts2->u.derived->attr.is_class ?
5448 : 1006 : ts2->u.derived->components->ts.u.derived
5449 : 1009 : : ts2->u.derived);
5450 : 27131 : if (is_class1 && is_derived2)
5451 : 9166 : return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
5452 : 9165 : ts1->u.derived->components->ts.u.derived
5453 : : : ts1->u.derived,
5454 : 18332 : ts2->u.derived);
5455 : 17965 : else if (is_class1 && is_class2)
5456 : 35756 : return gfc_type_is_extension_of (ts1->u.derived->attr.is_class ?
5457 : 17795 : ts1->u.derived->components->ts.u.derived
5458 : : : ts1->u.derived,
5459 : 17961 : ts2->u.derived->attr.is_class ?
5460 : 17796 : ts2->u.derived->components->ts.u.derived
5461 : 17961 : : ts2->u.derived);
5462 : : else
5463 : : return 0;
5464 : : }
5465 : :
5466 : :
5467 : : /* Find the parent-namespace of the current function. If we're inside
5468 : : BLOCK constructs, it may not be the current one. */
5469 : :
5470 : : gfc_namespace*
5471 : 62331 : gfc_find_proc_namespace (gfc_namespace* ns)
5472 : : {
5473 : 62873 : while (ns->construct_entities)
5474 : : {
5475 : 542 : ns = ns->parent;
5476 : 542 : gcc_assert (ns);
5477 : : }
5478 : :
5479 : 62331 : return ns;
5480 : : }
5481 : :
5482 : :
5483 : : /* Check if an associate-variable should be translated as an `implicit' pointer
5484 : : internally (if it is associated to a variable and not an array with
5485 : : descriptor). */
5486 : :
5487 : : bool
5488 : 473777 : gfc_is_associate_pointer (gfc_symbol* sym)
5489 : : {
5490 : 473777 : if (!sym->assoc)
5491 : : return false;
5492 : :
5493 : 11635 : if (sym->ts.type == BT_CLASS)
5494 : : return true;
5495 : :
5496 : 6430 : if (sym->ts.type == BT_CHARACTER
5497 : 1260 : && sym->ts.deferred
5498 : 56 : && sym->assoc->target
5499 : 56 : && sym->assoc->target->expr_type == EXPR_FUNCTION)
5500 : : return true;
5501 : :
5502 : 6424 : if (!sym->assoc->variable)
5503 : : return false;
5504 : :
5505 : 5656 : if ((sym->attr.dimension || sym->attr.codimension)
5506 : 0 : && sym->as->type != AS_EXPLICIT)
5507 : 0 : return false;
5508 : :
5509 : : return true;
5510 : : }
5511 : :
5512 : :
5513 : : gfc_symbol *
5514 : 32775 : gfc_find_dt_in_generic (gfc_symbol *sym)
5515 : : {
5516 : 32775 : gfc_interface *intr = NULL;
5517 : :
5518 : 32775 : if (!sym || gfc_fl_struct (sym->attr.flavor))
5519 : : return sym;
5520 : :
5521 : 32775 : if (sym->attr.generic)
5522 : 34385 : for (intr = sym->generic; intr; intr = intr->next)
5523 : 21909 : if (gfc_fl_struct (intr->sym->attr.flavor))
5524 : : break;
5525 : 32773 : return intr ? intr->sym : NULL;
5526 : : }
5527 : :
5528 : :
5529 : : /* Get the dummy arguments from a procedure symbol. If it has been declared
5530 : : via a PROCEDURE statement with a named interface, ts.interface will be set
5531 : : and the arguments need to be taken from there. */
5532 : :
5533 : : gfc_formal_arglist *
5534 : 3654705 : gfc_sym_get_dummy_args (gfc_symbol *sym)
5535 : : {
5536 : 3654705 : gfc_formal_arglist *dummies;
5537 : :
5538 : 3654705 : if (sym == NULL)
5539 : : return NULL;
5540 : :
5541 : 3654704 : dummies = sym->formal;
5542 : 3654704 : if (dummies == NULL && sym->ts.interface != NULL)
5543 : 6676 : dummies = sym->ts.interface->formal;
5544 : :
5545 : : return dummies;
5546 : : }
5547 : :
5548 : :
5549 : : /* Given a procedure, returns the associated namespace.
5550 : : The resulting NS should match the condition NS->PROC_NAME == SYM. */
5551 : :
5552 : : gfc_namespace *
5553 : 733991 : gfc_get_procedure_ns (gfc_symbol *sym)
5554 : : {
5555 : 733991 : if (sym->formal_ns
5556 : 557481 : && sym->formal_ns->proc_name == sym
5557 : : /* For module procedures used in submodules, there are two namespaces.
5558 : : The one generated by the host association of the module is directly
5559 : : accessible through SYM->FORMAL_NS but doesn't have any parent set.
5560 : : The one generated by the parser is only accessible by walking the
5561 : : contained namespace but has its parent set. Prefer the one generated
5562 : : by the parser below. */
5563 : 557061 : && !(sym->attr.used_in_submodule
5564 : 954 : && sym->attr.contained
5565 : 393 : && sym->formal_ns->parent == nullptr))
5566 : : return sym->formal_ns;
5567 : :
5568 : : /* The above should have worked in most cases. If it hasn't, try some other
5569 : : heuristics, eventually returning SYM->NS. */
5570 : 177321 : if (gfc_current_ns->proc_name == sym)
5571 : : return gfc_current_ns;
5572 : :
5573 : : /* For contained procedures, the symbol's NS field is the
5574 : : hosting namespace, not the procedure namespace. */
5575 : 153139 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.contained)
5576 : 175965 : for (gfc_namespace *ns = sym->ns->contained; ns; ns = ns->sibling)
5577 : 175613 : if (ns->proc_name == sym)
5578 : : return ns;
5579 : :
5580 : 112082 : if (sym->formal_ns
5581 : 420 : && sym->formal_ns->proc_name == sym)
5582 : : return sym->formal_ns;
5583 : :
5584 : 112082 : if (sym->formal)
5585 : 3912 : for (gfc_formal_arglist *f = sym->formal; f != nullptr; f = f->next)
5586 : 2270 : if (f->sym)
5587 : : {
5588 : 2223 : gfc_namespace *ns = f->sym->ns;
5589 : 2223 : if (ns && ns->proc_name == sym)
5590 : : return ns;
5591 : : }
5592 : :
5593 : 112082 : return sym->ns;
5594 : : }
5595 : :
5596 : :
5597 : : /* Given a symbol, returns the namespace in which the symbol is specified.
5598 : : In most cases, it is the namespace hosting the symbol. This is the case
5599 : : for variables. For functions, however, it is the function namespace
5600 : : itself. This specification namespace is used to check conformance of
5601 : : array spec bound expressions. */
5602 : :
5603 : : gfc_namespace *
5604 : 1662602 : gfc_get_spec_ns (gfc_symbol *sym)
5605 : : {
5606 : 1662602 : if (sym->attr.flavor == FL_PROCEDURE
5607 : 465542 : && sym->attr.function)
5608 : : {
5609 : 311834 : if (sym->result == sym)
5610 : 224744 : return gfc_get_procedure_ns (sym);
5611 : : /* Generic and intrinsic functions can have a null result. */
5612 : 87090 : else if (sym->result != nullptr)
5613 : 37066 : return sym->result->ns;
5614 : : }
5615 : :
5616 : 1400792 : return sym->ns;
5617 : : }
|