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