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