Debian Codegraph @

gcc/fortran/symbol.cc

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