]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/symbol.c
[multiple changes]
[gcc.git] / gcc / fortran / symbol.c
CommitLineData
6de9cd9a 1/* Maintain binary trees of symbols.
710a179f
TS
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
6de9cd9a
DN
4 Contributed by Andy Vaught
5
9fc4d79b 6This file is part of GCC.
6de9cd9a 7
9fc4d79b
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
6de9cd9a 12
9fc4d79b
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
9fc4d79b 19along with GCC; see the file COPYING. If not, write to the Free
ab57747b
KC
20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301, USA. */
6de9cd9a
DN
22
23
24#include "config.h"
d22e4895 25#include "system.h"
6690a9e0 26#include "flags.h"
6de9cd9a
DN
27#include "gfortran.h"
28#include "parse.h"
29
a8b3b0b6 30
6de9cd9a
DN
31/* Strings for all symbol attributes. We use these for dumping the
32 parse tree, in error messages, and also when reading and writing
33 modules. */
34
35const mstring flavors[] =
36{
37 minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
38 minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
39 minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
40 minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
41 minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
42 minit (NULL, -1)
43};
44
45const mstring procedures[] =
46{
47 minit ("UNKNOWN-PROC", PROC_UNKNOWN),
48 minit ("MODULE-PROC", PROC_MODULE),
49 minit ("INTERNAL-PROC", PROC_INTERNAL),
50 minit ("DUMMY-PROC", PROC_DUMMY),
51 minit ("INTRINSIC-PROC", PROC_INTRINSIC),
52 minit ("EXTERNAL-PROC", PROC_EXTERNAL),
53 minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
54 minit (NULL, -1)
55};
56
57const mstring intents[] =
58{
59 minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
60 minit ("IN", INTENT_IN),
61 minit ("OUT", INTENT_OUT),
62 minit ("INOUT", INTENT_INOUT),
63 minit (NULL, -1)
64};
65
66const mstring access_types[] =
67{
68 minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
69 minit ("PUBLIC", ACCESS_PUBLIC),
70 minit ("PRIVATE", ACCESS_PRIVATE),
71 minit (NULL, -1)
72};
73
74const mstring ifsrc_types[] =
75{
76 minit ("UNKNOWN", IFSRC_UNKNOWN),
77 minit ("DECL", IFSRC_DECL),
78 minit ("BODY", IFSRC_IFBODY),
79 minit ("USAGE", IFSRC_USAGE)
80};
81
82
83/* This is to make sure the backend generates setup code in the correct
84 order. */
85
86static int next_dummy_order = 1;
87
88
89gfc_namespace *gfc_current_ns;
90
c9543002
TS
91gfc_gsymbol *gfc_gsym_root = NULL;
92
6de9cd9a
DN
93static gfc_symbol *changed_syms = NULL;
94
7453378e
PT
95gfc_dt_list *gfc_derived_types;
96
6de9cd9a
DN
97
98/*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
99
1107b970
PB
100/* The following static variable indicates whether a particular element has
101 been explicitly set or not. */
6de9cd9a 102
6de9cd9a
DN
103static int new_flag[GFC_LETTERS];
104
105
106/* Handle a correctly parsed IMPLICIT NONE. */
107
108void
109gfc_set_implicit_none (void)
110{
111 int i;
112
438e1428
TS
113 if (gfc_current_ns->seen_implicit_none)
114 {
115 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
116 return;
117 }
118
119 gfc_current_ns->seen_implicit_none = 1;
120
1107b970 121 for (i = 0; i < GFC_LETTERS; i++)
6de9cd9a 122 {
1107b970
PB
123 gfc_clear_ts (&gfc_current_ns->default_type[i]);
124 gfc_current_ns->set_flag[i] = 1;
6de9cd9a
DN
125 }
126}
127
128
1107b970 129/* Reset the implicit range flags. */
6de9cd9a
DN
130
131void
1107b970 132gfc_clear_new_implicit (void)
6de9cd9a
DN
133{
134 int i;
135
136 for (i = 0; i < GFC_LETTERS; i++)
1107b970 137 new_flag[i] = 0;
6de9cd9a
DN
138}
139
140
1107b970 141/* Prepare for a new implicit range. Sets flags in new_flag[]. */
6de9cd9a 142
1107b970
PB
143try
144gfc_add_new_implicit_range (int c1, int c2)
6de9cd9a
DN
145{
146 int i;
147
148 c1 -= 'a';
149 c2 -= 'a';
150
151 for (i = c1; i <= c2; i++)
152 {
153 if (new_flag[i])
154 {
155 gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
156 i + 'A');
157 return FAILURE;
158 }
159
6de9cd9a
DN
160 new_flag[i] = 1;
161 }
162
163 return SUCCESS;
164}
165
166
1107b970
PB
167/* Add a matched implicit range for gfc_set_implicit(). Check if merging
168 the new implicit types back into the existing types will work. */
6de9cd9a
DN
169
170try
66e4ab31 171gfc_merge_new_implicit (gfc_typespec *ts)
6de9cd9a
DN
172{
173 int i;
174
438e1428
TS
175 if (gfc_current_ns->seen_implicit_none)
176 {
177 gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
178 return FAILURE;
179 }
180
6de9cd9a 181 for (i = 0; i < GFC_LETTERS; i++)
1107b970
PB
182 {
183 if (new_flag[i])
184 {
6de9cd9a 185
1107b970
PB
186 if (gfc_current_ns->set_flag[i])
187 {
188 gfc_error ("Letter %c already has an IMPLICIT type at %C",
189 i + 'A');
190 return FAILURE;
191 }
192 gfc_current_ns->default_type[i] = *ts;
193 gfc_current_ns->set_flag[i] = 1;
194 }
195 }
6de9cd9a
DN
196 return SUCCESS;
197}
198
199
eebc3ee0 200/* Given a symbol, return a pointer to the typespec for its default type. */
6de9cd9a
DN
201
202gfc_typespec *
66e4ab31 203gfc_get_default_type (gfc_symbol *sym, gfc_namespace *ns)
6de9cd9a
DN
204{
205 char letter;
206
207 letter = sym->name[0];
e6472bce
FXC
208
209 if (gfc_option.flag_allow_leading_underscore && letter == '_')
210 gfc_internal_error ("Option -fallow_leading_underscore is for use only by "
211 "gfortran developers, and should not be used for "
212 "implicitly typed variables");
213
6de9cd9a
DN
214 if (letter < 'a' || letter > 'z')
215 gfc_internal_error ("gfc_get_default_type(): Bad symbol");
216
217 if (ns == NULL)
218 ns = gfc_current_ns;
219
220 return &ns->default_type[letter - 'a'];
221}
222
223
224/* Given a pointer to a symbol, set its type according to the first
225 letter of its name. Fails if the letter in question has no default
226 type. */
227
228try
66e4ab31 229gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns)
6de9cd9a
DN
230{
231 gfc_typespec *ts;
232
233 if (sym->ts.type != BT_UNKNOWN)
234 gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
235
236 ts = gfc_get_default_type (sym, ns);
237
238 if (ts->type == BT_UNKNOWN)
239 {
d1303acd
TS
240 if (error_flag && !sym->attr.untyped)
241 {
242 gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
243 sym->name, &sym->declared_at);
244 sym->attr.untyped = 1; /* Ensure we only give an error once. */
245 }
6de9cd9a
DN
246
247 return FAILURE;
248 }
249
250 sym->ts = *ts;
251 sym->attr.implicit_type = 1;
252
a8b3b0b6
CR
253 if (sym->attr.is_bind_c == 1)
254 {
255 /* BIND(C) variables should not be implicitly declared. */
256 gfc_warning_now ("Implicitly declared BIND(C) variable '%s' at %L may "
257 "not be C interoperable", sym->name, &sym->declared_at);
258 sym->ts.f90_type = sym->ts.type;
259 }
260
261 if (sym->attr.dummy != 0)
262 {
263 if (sym->ns->proc_name != NULL
264 && (sym->ns->proc_name->attr.subroutine != 0
265 || sym->ns->proc_name->attr.function != 0)
266 && sym->ns->proc_name->attr.is_bind_c != 0)
267 {
268 /* Dummy args to a BIND(C) routine may not be interoperable if
269 they are implicitly typed. */
270 gfc_warning_now ("Implicity declared variable '%s' at %L may not "
271 "be C interoperable but it is a dummy argument to "
272 "the BIND(C) procedure '%s' at %L", sym->name,
273 &(sym->declared_at), sym->ns->proc_name->name,
274 &(sym->ns->proc_name->declared_at));
275 sym->ts.f90_type = sym->ts.type;
276 }
277 }
278
6de9cd9a
DN
279 return SUCCESS;
280}
281
282
e9bd9f7d
PT
283/* This function is called from parse.c(parse_progunit) to check the
284 type of the function is not implicitly typed in the host namespace
285 and to implicitly type the function result, if necessary. */
286
287void
288gfc_check_function_type (gfc_namespace *ns)
289{
290 gfc_symbol *proc = ns->proc_name;
291
292 if (!proc->attr.contained || proc->result->attr.implicit_type)
293 return;
294
295 if (proc->result->ts.type == BT_UNKNOWN)
296 {
297 if (gfc_set_default_type (proc->result, 0, gfc_current_ns)
298 == SUCCESS)
299 {
300 if (proc->result != proc)
c2de0c19
TB
301 {
302 proc->ts = proc->result->ts;
303 proc->as = gfc_copy_array_spec (proc->result->as);
304 proc->attr.dimension = proc->result->attr.dimension;
305 proc->attr.pointer = proc->result->attr.pointer;
306 proc->attr.allocatable = proc->result->attr.allocatable;
307 }
e9bd9f7d
PT
308 }
309 else
310 {
c2de0c19
TB
311 gfc_error ("Function result '%s' at %L has no IMPLICIT type",
312 proc->result->name, &proc->result->declared_at);
e9bd9f7d
PT
313 proc->result->attr.untyped = 1;
314 }
315 }
316}
317
318
6de9cd9a
DN
319/******************** Symbol attribute stuff *********************/
320
321/* This is a generic conflict-checker. We do this to avoid having a
322 single conflict in two places. */
323
324#define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
325#define conf2(a) if (attr->a) { a2 = a; goto conflict; }
aa08038d
EE
326#define conf_std(a, b, std) if (attr->a && attr->b)\
327 {\
328 a1 = a;\
329 a2 = b;\
330 standard = std;\
331 goto conflict_std;\
332 }
6de9cd9a
DN
333
334static try
66e4ab31 335check_conflict (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
336{
337 static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
338 *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
775e6c3a 339 *intent_in = "INTENT(IN)", *intrinsic = "INTRINSIC",
06469efd 340 *intent_out = "INTENT(OUT)", *intent_inout = "INTENT(INOUT)",
775e6c3a
TB
341 *allocatable = "ALLOCATABLE", *elemental = "ELEMENTAL",
342 *private = "PRIVATE", *recursive = "RECURSIVE",
6de9cd9a
DN
343 *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
344 *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
345 *function = "FUNCTION", *subroutine = "SUBROUTINE",
e8ec07e1 346 *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
83d890b9 347 *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
06469efd 348 *cray_pointee = "CRAY POINTEE", *data = "DATA", *value = "VALUE",
a8b3b0b6
CR
349 *volatile_ = "VOLATILE", *protected = "PROTECTED",
350 *is_bind_c = "BIND(C)";
6c7a4dfd 351 static const char *threadprivate = "THREADPRIVATE";
6de9cd9a
DN
352
353 const char *a1, *a2;
aa08038d 354 int standard;
6de9cd9a
DN
355
356 if (where == NULL)
63645982 357 where = &gfc_current_locus;
6de9cd9a
DN
358
359 if (attr->pointer && attr->intent != INTENT_UNKNOWN)
360 {
361 a1 = pointer;
362 a2 = intent;
f17facac
TB
363 standard = GFC_STD_F2003;
364 goto conflict_std;
6de9cd9a
DN
365 }
366
367 /* Check for attributes not allowed in a BLOCK DATA. */
368 if (gfc_current_state () == COMP_BLOCK_DATA)
369 {
370 a1 = NULL;
371
53096259
PT
372 if (attr->in_namelist)
373 a1 = in_namelist;
6de9cd9a
DN
374 if (attr->allocatable)
375 a1 = allocatable;
376 if (attr->external)
377 a1 = external;
378 if (attr->optional)
379 a1 = optional;
380 if (attr->access == ACCESS_PRIVATE)
381 a1 = private;
382 if (attr->access == ACCESS_PUBLIC)
383 a1 = public;
384 if (attr->intent != INTENT_UNKNOWN)
385 a1 = intent;
386
387 if (a1 != NULL)
388 {
389 gfc_error
66e4ab31
SK
390 ("%s attribute not allowed in BLOCK DATA program unit at %L",
391 a1, where);
6de9cd9a
DN
392 return FAILURE;
393 }
394 }
395
9c213349
TB
396 conf (dummy, entry);
397 conf (dummy, intrinsic);
6de9cd9a 398 conf (dummy, save);
6c7a4dfd 399 conf (dummy, threadprivate);
6de9cd9a 400 conf (pointer, target);
6de9cd9a 401 conf (pointer, intrinsic);
1902704e 402 conf (pointer, elemental);
8e119f1b 403 conf (allocatable, elemental);
1902704e 404
6de9cd9a
DN
405 conf (target, external);
406 conf (target, intrinsic);
407 conf (external, dimension); /* See Fortran 95's R504. */
408
409 conf (external, intrinsic);
1902704e
PT
410
411 if (attr->if_source || attr->contained)
412 {
413 conf (external, subroutine);
414 conf (external, function);
415 }
416
6de9cd9a 417 conf (allocatable, pointer);
aa08038d 418 conf_std (allocatable, dummy, GFC_STD_F2003);
8e119f1b
EE
419 conf_std (allocatable, function, GFC_STD_F2003);
420 conf_std (allocatable, result, GFC_STD_F2003);
6de9cd9a
DN
421 conf (elemental, recursive);
422
423 conf (in_common, dummy);
424 conf (in_common, allocatable);
425 conf (in_common, result);
96b95725
PT
426 conf (in_common, save);
427 conf (result, save);
428
6de9cd9a
DN
429 conf (dummy, result);
430
e8ec07e1
PT
431 conf (in_equivalence, use_assoc);
432 conf (in_equivalence, dummy);
433 conf (in_equivalence, target);
434 conf (in_equivalence, pointer);
435 conf (in_equivalence, function);
436 conf (in_equivalence, result);
437 conf (in_equivalence, entry);
438 conf (in_equivalence, allocatable);
6c7a4dfd 439 conf (in_equivalence, threadprivate);
e8ec07e1 440
6de9cd9a
DN
441 conf (in_namelist, pointer);
442 conf (in_namelist, allocatable);
443
444 conf (entry, result);
445
446 conf (function, subroutine);
447
a8b3b0b6
CR
448 if (!function && !subroutine)
449 conf (is_bind_c, dummy);
450
451 conf (is_bind_c, cray_pointer);
452 conf (is_bind_c, cray_pointee);
453 conf (is_bind_c, allocatable);
454
455 /* Need to also get volatile attr, according to 5.1 of F2003 draft.
456 Parameter conflict caught below. Also, value cannot be specified
457 for a dummy procedure. */
458
83d890b9
AL
459 /* Cray pointer/pointee conflicts. */
460 conf (cray_pointer, cray_pointee);
461 conf (cray_pointer, dimension);
462 conf (cray_pointer, pointer);
463 conf (cray_pointer, target);
464 conf (cray_pointer, allocatable);
465 conf (cray_pointer, external);
466 conf (cray_pointer, intrinsic);
467 conf (cray_pointer, in_namelist);
468 conf (cray_pointer, function);
469 conf (cray_pointer, subroutine);
470 conf (cray_pointer, entry);
471
472 conf (cray_pointee, allocatable);
473 conf (cray_pointee, intent);
474 conf (cray_pointee, optional);
475 conf (cray_pointee, dummy);
476 conf (cray_pointee, target);
83d890b9
AL
477 conf (cray_pointee, intrinsic);
478 conf (cray_pointee, pointer);
83d890b9 479 conf (cray_pointee, entry);
b122dc6a
JJ
480 conf (cray_pointee, in_common);
481 conf (cray_pointee, in_equivalence);
6c7a4dfd 482 conf (cray_pointee, threadprivate);
83d890b9 483
4075a94e
PT
484 conf (data, dummy);
485 conf (data, function);
486 conf (data, result);
487 conf (data, allocatable);
488 conf (data, use_assoc);
489
06469efd
PT
490 conf (value, pointer)
491 conf (value, allocatable)
492 conf (value, subroutine)
493 conf (value, function)
494 conf (value, volatile_)
495 conf (value, dimension)
496 conf (value, external)
497
66e4ab31
SK
498 if (attr->value
499 && (attr->intent == INTENT_OUT || attr->intent == INTENT_INOUT))
06469efd
PT
500 {
501 a1 = value;
502 a2 = attr->intent == INTENT_OUT ? intent_out : intent_inout;
503 goto conflict;
504 }
505
a8b3b0b6
CR
506 conf (protected, intrinsic)
507 conf (protected, external)
508 conf (protected, in_common)
509
775e6c3a
TB
510 conf (volatile_, intrinsic)
511 conf (volatile_, external)
512
513 if (attr->volatile_ && attr->intent == INTENT_IN)
514 {
515 a1 = volatile_;
516 a2 = intent_in;
517 goto conflict;
518 }
519
6de9cd9a
DN
520 a1 = gfc_code2string (flavors, attr->flavor);
521
522 if (attr->in_namelist
523 && attr->flavor != FL_VARIABLE
847b053d 524 && attr->flavor != FL_PROCEDURE
6de9cd9a
DN
525 && attr->flavor != FL_UNKNOWN)
526 {
6de9cd9a
DN
527 a2 = in_namelist;
528 goto conflict;
529 }
530
531 switch (attr->flavor)
532 {
533 case FL_PROGRAM:
534 case FL_BLOCK_DATA:
535 case FL_MODULE:
536 case FL_LABEL:
9c213349 537 conf2 (dimension);
6de9cd9a
DN
538 conf2 (dummy);
539 conf2 (save);
d7043acd 540 conf2 (volatile_);
6de9cd9a 541 conf2 (pointer);
ee7e677f 542 conf2 (protected);
6de9cd9a
DN
543 conf2 (target);
544 conf2 (external);
545 conf2 (intrinsic);
546 conf2 (allocatable);
547 conf2 (result);
548 conf2 (in_namelist);
549 conf2 (optional);
550 conf2 (function);
551 conf2 (subroutine);
6c7a4dfd 552 conf2 (threadprivate);
6de9cd9a
DN
553 break;
554
555 case FL_VARIABLE:
556 case FL_NAMELIST:
557 break;
558
559 case FL_PROCEDURE:
560 conf2 (intent);
66e4ab31 561 conf2 (save);
6de9cd9a
DN
562
563 if (attr->subroutine)
564 {
66e4ab31
SK
565 conf2 (pointer);
566 conf2 (target);
567 conf2 (allocatable);
568 conf2 (result);
569 conf2 (in_namelist);
570 conf2 (dimension);
571 conf2 (function);
572 conf2 (threadprivate);
6de9cd9a
DN
573 }
574
575 switch (attr->proc)
576 {
577 case PROC_ST_FUNCTION:
578 conf2 (in_common);
2bb02bf0 579 conf2 (dummy);
6de9cd9a
DN
580 break;
581
582 case PROC_MODULE:
583 conf2 (dummy);
584 break;
585
586 case PROC_DUMMY:
587 conf2 (result);
588 conf2 (in_common);
589 conf2 (save);
6c7a4dfd 590 conf2 (threadprivate);
6de9cd9a
DN
591 break;
592
593 default:
594 break;
595 }
596
597 break;
598
599 case FL_DERIVED:
600 conf2 (dummy);
601 conf2 (save);
602 conf2 (pointer);
603 conf2 (target);
604 conf2 (external);
605 conf2 (intrinsic);
606 conf2 (allocatable);
607 conf2 (optional);
608 conf2 (entry);
609 conf2 (function);
610 conf2 (subroutine);
6c7a4dfd 611 conf2 (threadprivate);
6de9cd9a
DN
612
613 if (attr->intent != INTENT_UNKNOWN)
614 {
615 a2 = intent;
616 goto conflict;
617 }
618 break;
619
620 case FL_PARAMETER:
621 conf2 (external);
622 conf2 (intrinsic);
623 conf2 (optional);
624 conf2 (allocatable);
625 conf2 (function);
626 conf2 (subroutine);
627 conf2 (entry);
628 conf2 (pointer);
ee7e677f 629 conf2 (protected);
6de9cd9a
DN
630 conf2 (target);
631 conf2 (dummy);
632 conf2 (in_common);
2a0abeaf 633 conf2 (save);
06469efd 634 conf2 (value);
775e6c3a 635 conf2 (volatile_);
6c7a4dfd 636 conf2 (threadprivate);
a8b3b0b6
CR
637 /* TODO: hmm, double check this. */
638 conf2 (value);
6de9cd9a
DN
639 break;
640
641 default:
642 break;
643 }
644
645 return SUCCESS;
646
647conflict:
231b2fcc
TS
648 if (name == NULL)
649 gfc_error ("%s attribute conflicts with %s attribute at %L",
650 a1, a2, where);
651 else
652 gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
653 a1, a2, name, where);
654
6de9cd9a 655 return FAILURE;
aa08038d
EE
656
657conflict_std:
658 if (name == NULL)
659 {
f17facac 660 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
ee167bcb 661 "with %s attribute at %L", a1, a2,
aa08038d
EE
662 where);
663 }
664 else
665 {
f17facac
TB
666 return gfc_notify_std (standard, "Fortran 2003: %s attribute "
667 "with %s attribute in '%s' at %L",
aa08038d
EE
668 a1, a2, name, where);
669 }
6de9cd9a
DN
670}
671
672#undef conf
673#undef conf2
aa08038d 674#undef conf_std
6de9cd9a
DN
675
676
677/* Mark a symbol as referenced. */
678
679void
66e4ab31 680gfc_set_sym_referenced (gfc_symbol *sym)
6de9cd9a 681{
66e4ab31 682
6de9cd9a
DN
683 if (sym->attr.referenced)
684 return;
685
686 sym->attr.referenced = 1;
687
688 /* Remember which order dummy variables are accessed in. */
689 if (sym->attr.dummy)
690 sym->dummy_order = next_dummy_order++;
691}
692
693
694/* Common subroutine called by attribute changing subroutines in order
695 to prevent them from changing a symbol that has been
696 use-associated. Returns zero if it is OK to change the symbol,
697 nonzero if not. */
698
699static int
66e4ab31 700check_used (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
701{
702
703 if (attr->use_assoc == 0)
704 return 0;
705
706 if (where == NULL)
63645982 707 where = &gfc_current_locus;
6de9cd9a 708
231b2fcc
TS
709 if (name == NULL)
710 gfc_error ("Cannot change attributes of USE-associated symbol at %L",
711 where);
712 else
713 gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
714 name, where);
6de9cd9a
DN
715
716 return 1;
717}
718
719
6de9cd9a
DN
720/* Generate an error because of a duplicate attribute. */
721
722static void
66e4ab31 723duplicate_attr (const char *attr, locus *where)
6de9cd9a
DN
724{
725
726 if (where == NULL)
63645982 727 where = &gfc_current_locus;
6de9cd9a
DN
728
729 gfc_error ("Duplicate %s attribute specified at %L", attr, where);
730}
731
66e4ab31
SK
732
733/* Called from decl.c (attr_decl1) to check attributes, when declared
734 separately. */
6de9cd9a 735
1902704e 736try
66e4ab31 737gfc_add_attribute (symbol_attribute *attr, locus *where)
1902704e 738{
66e4ab31 739
7114edca 740 if (check_used (attr, NULL, where))
1902704e
PT
741 return FAILURE;
742
743 return check_conflict (attr, NULL, where);
744}
745
6de9cd9a 746try
66e4ab31 747gfc_add_allocatable (symbol_attribute *attr, locus *where)
6de9cd9a
DN
748{
749
7114edca 750 if (check_used (attr, NULL, where))
6de9cd9a
DN
751 return FAILURE;
752
753 if (attr->allocatable)
754 {
755 duplicate_attr ("ALLOCATABLE", where);
756 return FAILURE;
757 }
758
759 attr->allocatable = 1;
231b2fcc 760 return check_conflict (attr, NULL, where);
6de9cd9a
DN
761}
762
763
764try
66e4ab31 765gfc_add_dimension (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
766{
767
7114edca 768 if (check_used (attr, name, where))
6de9cd9a
DN
769 return FAILURE;
770
771 if (attr->dimension)
772 {
773 duplicate_attr ("DIMENSION", where);
774 return FAILURE;
775 }
776
777 attr->dimension = 1;
231b2fcc 778 return check_conflict (attr, name, where);
6de9cd9a
DN
779}
780
781
782try
66e4ab31 783gfc_add_external (symbol_attribute *attr, locus *where)
6de9cd9a
DN
784{
785
7114edca 786 if (check_used (attr, NULL, where))
6de9cd9a
DN
787 return FAILURE;
788
789 if (attr->external)
790 {
791 duplicate_attr ("EXTERNAL", where);
792 return FAILURE;
793 }
794
795 attr->external = 1;
796
231b2fcc 797 return check_conflict (attr, NULL, where);
6de9cd9a
DN
798}
799
800
801try
66e4ab31 802gfc_add_intrinsic (symbol_attribute *attr, locus *where)
6de9cd9a
DN
803{
804
7114edca 805 if (check_used (attr, NULL, where))
6de9cd9a
DN
806 return FAILURE;
807
808 if (attr->intrinsic)
809 {
810 duplicate_attr ("INTRINSIC", where);
811 return FAILURE;
812 }
813
814 attr->intrinsic = 1;
815
231b2fcc 816 return check_conflict (attr, NULL, where);
6de9cd9a
DN
817}
818
819
820try
66e4ab31 821gfc_add_optional (symbol_attribute *attr, locus *where)
6de9cd9a
DN
822{
823
7114edca 824 if (check_used (attr, NULL, where))
6de9cd9a
DN
825 return FAILURE;
826
827 if (attr->optional)
828 {
829 duplicate_attr ("OPTIONAL", where);
830 return FAILURE;
831 }
832
833 attr->optional = 1;
231b2fcc 834 return check_conflict (attr, NULL, where);
6de9cd9a
DN
835}
836
837
838try
66e4ab31 839gfc_add_pointer (symbol_attribute *attr, locus *where)
6de9cd9a
DN
840{
841
7114edca 842 if (check_used (attr, NULL, where))
6de9cd9a
DN
843 return FAILURE;
844
845 attr->pointer = 1;
231b2fcc 846 return check_conflict (attr, NULL, where);
6de9cd9a
DN
847}
848
849
83d890b9 850try
66e4ab31 851gfc_add_cray_pointer (symbol_attribute *attr, locus *where)
83d890b9
AL
852{
853
7114edca 854 if (check_used (attr, NULL, where))
83d890b9
AL
855 return FAILURE;
856
857 attr->cray_pointer = 1;
858 return check_conflict (attr, NULL, where);
859}
860
861
862try
66e4ab31 863gfc_add_cray_pointee (symbol_attribute *attr, locus *where)
83d890b9
AL
864{
865
7114edca 866 if (check_used (attr, NULL, where))
83d890b9
AL
867 return FAILURE;
868
869 if (attr->cray_pointee)
870 {
871 gfc_error ("Cray Pointee at %L appears in multiple pointer()"
e25a0da3 872 " statements", where);
83d890b9
AL
873 return FAILURE;
874 }
875
876 attr->cray_pointee = 1;
877 return check_conflict (attr, NULL, where);
878}
879
66e4ab31 880
ee7e677f 881try
66e4ab31 882gfc_add_protected (symbol_attribute *attr, const char *name, locus *where)
ee7e677f
TB
883{
884 if (check_used (attr, name, where))
885 return FAILURE;
886
887 if (attr->protected)
888 {
889 if (gfc_notify_std (GFC_STD_LEGACY,
890 "Duplicate PROTECTED attribute specified at %L",
891 where)
892 == FAILURE)
893 return FAILURE;
894 }
895
896 attr->protected = 1;
897 return check_conflict (attr, name, where);
898}
83d890b9 899
66e4ab31 900
6de9cd9a 901try
66e4ab31 902gfc_add_result (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
903{
904
7114edca 905 if (check_used (attr, name, where))
6de9cd9a
DN
906 return FAILURE;
907
908 attr->result = 1;
231b2fcc 909 return check_conflict (attr, name, where);
6de9cd9a
DN
910}
911
912
913try
66e4ab31 914gfc_add_save (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
915{
916
231b2fcc 917 if (check_used (attr, name, where))
6de9cd9a
DN
918 return FAILURE;
919
920 if (gfc_pure (NULL))
921 {
922 gfc_error
923 ("SAVE attribute at %L cannot be specified in a PURE procedure",
924 where);
925 return FAILURE;
926 }
927
928 if (attr->save)
929 {
09e87839
AL
930 if (gfc_notify_std (GFC_STD_LEGACY,
931 "Duplicate SAVE attribute specified at %L",
932 where)
933 == FAILURE)
934 return FAILURE;
6de9cd9a
DN
935 }
936
937 attr->save = 1;
231b2fcc 938 return check_conflict (attr, name, where);
6de9cd9a
DN
939}
940
66e4ab31 941
06469efd 942try
66e4ab31 943gfc_add_value (symbol_attribute *attr, const char *name, locus *where)
06469efd
PT
944{
945
946 if (check_used (attr, name, where))
947 return FAILURE;
948
949 if (attr->value)
950 {
951 if (gfc_notify_std (GFC_STD_LEGACY,
952 "Duplicate VALUE attribute specified at %L",
953 where)
954 == FAILURE)
955 return FAILURE;
956 }
957
958 attr->value = 1;
959 return check_conflict (attr, name, where);
960}
961
66e4ab31 962
775e6c3a 963try
66e4ab31 964gfc_add_volatile (symbol_attribute *attr, const char *name, locus *where)
775e6c3a 965{
9bce3c1c
TB
966 /* No check_used needed as 11.2.1 of the F2003 standard allows
967 that the local identifier made accessible by a use statement can be
968 given a VOLATILE attribute. */
969
77bb16aa
TB
970 if (attr->volatile_ && attr->volatile_ns == gfc_current_ns)
971 if (gfc_notify_std (GFC_STD_LEGACY,
972 "Duplicate VOLATILE attribute specified at %L", where)
973 == FAILURE)
974 return FAILURE;
775e6c3a
TB
975
976 attr->volatile_ = 1;
77bb16aa 977 attr->volatile_ns = gfc_current_ns;
775e6c3a
TB
978 return check_conflict (attr, name, where);
979}
980
6de9cd9a 981
6c7a4dfd 982try
66e4ab31 983gfc_add_threadprivate (symbol_attribute *attr, const char *name, locus *where)
6c7a4dfd 984{
66e4ab31 985
6c7a4dfd
JJ
986 if (check_used (attr, name, where))
987 return FAILURE;
988
989 if (attr->threadprivate)
990 {
991 duplicate_attr ("THREADPRIVATE", where);
992 return FAILURE;
993 }
994
995 attr->threadprivate = 1;
996 return check_conflict (attr, name, where);
997}
998
999
6de9cd9a 1000try
66e4ab31 1001gfc_add_target (symbol_attribute *attr, locus *where)
6de9cd9a
DN
1002{
1003
7114edca 1004 if (check_used (attr, NULL, where))
6de9cd9a
DN
1005 return FAILURE;
1006
1007 if (attr->target)
1008 {
1009 duplicate_attr ("TARGET", where);
1010 return FAILURE;
1011 }
1012
1013 attr->target = 1;
231b2fcc 1014 return check_conflict (attr, NULL, where);
6de9cd9a
DN
1015}
1016
1017
1018try
66e4ab31 1019gfc_add_dummy (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1020{
1021
231b2fcc 1022 if (check_used (attr, name, where))
6de9cd9a
DN
1023 return FAILURE;
1024
eebc3ee0 1025 /* Duplicate dummy arguments are allowed due to ENTRY statements. */
6de9cd9a 1026 attr->dummy = 1;
231b2fcc 1027 return check_conflict (attr, name, where);
6de9cd9a
DN
1028}
1029
1030
6de9cd9a 1031try
66e4ab31 1032gfc_add_in_common (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1033{
1034
7114edca 1035 if (check_used (attr, name, where))
6de9cd9a
DN
1036 return FAILURE;
1037
1038 /* Duplicate attribute already checked for. */
1039 attr->in_common = 1;
231b2fcc 1040 if (check_conflict (attr, name, where) == FAILURE)
6de9cd9a
DN
1041 return FAILURE;
1042
1043 if (attr->flavor == FL_VARIABLE)
1044 return SUCCESS;
e8ec07e1
PT
1045
1046 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
1047}
1048
66e4ab31 1049
e8ec07e1 1050try
66e4ab31 1051gfc_add_in_equivalence (symbol_attribute *attr, const char *name, locus *where)
e8ec07e1
PT
1052{
1053
1054 /* Duplicate attribute already checked for. */
1055 attr->in_equivalence = 1;
1056 if (check_conflict (attr, name, where) == FAILURE)
1057 return FAILURE;
1058
1059 if (attr->flavor == FL_VARIABLE)
1060 return SUCCESS;
6de9cd9a 1061
231b2fcc 1062 return gfc_add_flavor (attr, FL_VARIABLE, name, where);
6de9cd9a
DN
1063}
1064
1065
9056bd70 1066try
231b2fcc 1067gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
9056bd70
TS
1068{
1069
231b2fcc 1070 if (check_used (attr, name, where))
9056bd70
TS
1071 return FAILURE;
1072
1073 attr->data = 1;
231b2fcc 1074 return check_conflict (attr, name, where);
9056bd70
TS
1075}
1076
1077
6de9cd9a 1078try
66e4ab31 1079gfc_add_in_namelist (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1080{
1081
1082 attr->in_namelist = 1;
231b2fcc 1083 return check_conflict (attr, name, where);
6de9cd9a
DN
1084}
1085
1086
1087try
66e4ab31 1088gfc_add_sequence (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1089{
1090
231b2fcc 1091 if (check_used (attr, name, where))
6de9cd9a
DN
1092 return FAILURE;
1093
1094 attr->sequence = 1;
231b2fcc 1095 return check_conflict (attr, name, where);
6de9cd9a
DN
1096}
1097
1098
1099try
66e4ab31 1100gfc_add_elemental (symbol_attribute *attr, locus *where)
6de9cd9a
DN
1101{
1102
7114edca 1103 if (check_used (attr, NULL, where))
6de9cd9a
DN
1104 return FAILURE;
1105
1106 attr->elemental = 1;
231b2fcc 1107 return check_conflict (attr, NULL, where);
6de9cd9a
DN
1108}
1109
1110
1111try
66e4ab31 1112gfc_add_pure (symbol_attribute *attr, locus *where)
6de9cd9a
DN
1113{
1114
7114edca 1115 if (check_used (attr, NULL, where))
6de9cd9a
DN
1116 return FAILURE;
1117
1118 attr->pure = 1;
231b2fcc 1119 return check_conflict (attr, NULL, where);
6de9cd9a
DN
1120}
1121
1122
1123try
66e4ab31 1124gfc_add_recursive (symbol_attribute *attr, locus *where)
6de9cd9a
DN
1125{
1126
7114edca 1127 if (check_used (attr, NULL, where))
6de9cd9a
DN
1128 return FAILURE;
1129
1130 attr->recursive = 1;
231b2fcc 1131 return check_conflict (attr, NULL, where);
6de9cd9a
DN
1132}
1133
1134
1135try
66e4ab31 1136gfc_add_entry (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1137{
1138
231b2fcc 1139 if (check_used (attr, name, where))
6de9cd9a
DN
1140 return FAILURE;
1141
1142 if (attr->entry)
1143 {
1144 duplicate_attr ("ENTRY", where);
1145 return FAILURE;
1146 }
1147
1148 attr->entry = 1;
231b2fcc 1149 return check_conflict (attr, name, where);
6de9cd9a
DN
1150}
1151
1152
1153try
66e4ab31 1154gfc_add_function (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1155{
1156
1157 if (attr->flavor != FL_PROCEDURE
231b2fcc 1158 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
6de9cd9a
DN
1159 return FAILURE;
1160
1161 attr->function = 1;
231b2fcc 1162 return check_conflict (attr, name, where);
6de9cd9a
DN
1163}
1164
1165
1166try
66e4ab31 1167gfc_add_subroutine (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1168{
1169
1170 if (attr->flavor != FL_PROCEDURE
231b2fcc 1171 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
6de9cd9a
DN
1172 return FAILURE;
1173
1174 attr->subroutine = 1;
231b2fcc 1175 return check_conflict (attr, name, where);
6de9cd9a
DN
1176}
1177
1178
1179try
66e4ab31 1180gfc_add_generic (symbol_attribute *attr, const char *name, locus *where)
6de9cd9a
DN
1181{
1182
1183 if (attr->flavor != FL_PROCEDURE
231b2fcc 1184 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
6de9cd9a
DN
1185 return FAILURE;
1186
1187 attr->generic = 1;
231b2fcc 1188 return check_conflict (attr, name, where);
6de9cd9a
DN
1189}
1190
1191
eebc3ee0 1192/* Flavors are special because some flavors are not what Fortran
6de9cd9a
DN
1193 considers attributes and can be reaffirmed multiple times. */
1194
1195try
66e4ab31
SK
1196gfc_add_flavor (symbol_attribute *attr, sym_flavor f, const char *name,
1197 locus *where)
6de9cd9a
DN
1198{
1199
1200 if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
1201 || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
231b2fcc 1202 || f == FL_NAMELIST) && check_used (attr, name, where))
6de9cd9a
DN
1203 return FAILURE;
1204
1205 if (attr->flavor == f && f == FL_VARIABLE)
1206 return SUCCESS;
1207
1208 if (attr->flavor != FL_UNKNOWN)
1209 {
1210 if (where == NULL)
63645982 1211 where = &gfc_current_locus;
6de9cd9a 1212
661051aa
DF
1213 if (name)
1214 gfc_error ("%s attribute of '%s' conflicts with %s attribute at %L",
1215 gfc_code2string (flavors, attr->flavor), name,
1216 gfc_code2string (flavors, f), where);
1217 else
1218 gfc_error ("%s attribute conflicts with %s attribute at %L",
1219 gfc_code2string (flavors, attr->flavor),
1220 gfc_code2string (flavors, f), where);
6de9cd9a
DN
1221
1222 return FAILURE;
1223 }
1224
1225 attr->flavor = f;
1226
231b2fcc 1227 return check_conflict (attr, name, where);
6de9cd9a
DN
1228}
1229
1230
1231try
66e4ab31
SK
1232gfc_add_procedure (symbol_attribute *attr, procedure_type t,
1233 const char *name, locus *where)
6de9cd9a
DN
1234{
1235
7114edca 1236 if (check_used (attr, name, where))
6de9cd9a
DN
1237 return FAILURE;
1238
1239 if (attr->flavor != FL_PROCEDURE
231b2fcc 1240 && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
6de9cd9a
DN
1241 return FAILURE;
1242
1243 if (where == NULL)
63645982 1244 where = &gfc_current_locus;
6de9cd9a
DN
1245
1246 if (attr->proc != PROC_UNKNOWN)
1247 {
31043f6c 1248 gfc_error ("%s procedure at %L is already declared as %s procedure",
6de9cd9a 1249 gfc_code2string (procedures, t), where,
6de9cd9a
DN
1250 gfc_code2string (procedures, attr->proc));
1251
1252 return FAILURE;
1253 }
1254
1255 attr->proc = t;
1256
1257 /* Statement functions are always scalar and functions. */
1258 if (t == PROC_ST_FUNCTION
231b2fcc 1259 && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
6de9cd9a
DN
1260 || attr->dimension))
1261 return FAILURE;
1262
231b2fcc 1263 return check_conflict (attr, name, where);
6de9cd9a
DN
1264}
1265
1266
1267try
66e4ab31 1268gfc_add_intent (symbol_attribute *attr, sym_intent intent, locus *where)
6de9cd9a
DN
1269{
1270
231b2fcc 1271 if (check_used (attr, NULL, where))
6de9cd9a
DN
1272 return FAILURE;
1273
1274 if (attr->intent == INTENT_UNKNOWN)
1275 {
1276 attr->intent = intent;
231b2fcc 1277 return check_conflict (attr, NULL, where);
6de9cd9a
DN
1278 }
1279
1280 if (where == NULL)
63645982 1281 where = &gfc_current_locus;
6de9cd9a
DN
1282
1283 gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1284 gfc_intent_string (attr->intent),
1285 gfc_intent_string (intent), where);
1286
1287 return FAILURE;
1288}
1289
1290
1291/* No checks for use-association in public and private statements. */
1292
1293try
66e4ab31
SK
1294gfc_add_access (symbol_attribute *attr, gfc_access access,
1295 const char *name, locus *where)
6de9cd9a
DN
1296{
1297
1298 if (attr->access == ACCESS_UNKNOWN)
1299 {
1300 attr->access = access;
231b2fcc 1301 return check_conflict (attr, name, where);
6de9cd9a
DN
1302 }
1303
1304 if (where == NULL)
63645982 1305 where = &gfc_current_locus;
6de9cd9a
DN
1306 gfc_error ("ACCESS specification at %L was already specified", where);
1307
1308 return FAILURE;
1309}
1310
1311
a8b3b0b6
CR
1312/* Set the is_bind_c field for the given symbol_attribute. */
1313
1314try
1315gfc_add_is_bind_c (symbol_attribute *attr, const char *name, locus *where,
1316 int is_proc_lang_bind_spec)
1317{
1318
1319 if (is_proc_lang_bind_spec == 0 && attr->flavor == FL_PROCEDURE)
1320 gfc_error_now ("BIND(C) attribute at %L can only be used for "
1321 "variables or common blocks", where);
1322 else if (attr->is_bind_c)
1323 gfc_error_now ("Duplicate BIND attribute specified at %L", where);
1324 else
1325 attr->is_bind_c = 1;
1326
1327 if (where == NULL)
1328 where = &gfc_current_locus;
1329
1330 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BIND(C) at %L", where)
1331 == FAILURE)
1332 return FAILURE;
1333
1334 return check_conflict (attr, name, where);
1335}
1336
1337
6de9cd9a 1338try
a8b3b0b6
CR
1339gfc_add_explicit_interface (gfc_symbol *sym, ifsrc source,
1340 gfc_formal_arglist * formal, locus *where)
6de9cd9a
DN
1341{
1342
231b2fcc 1343 if (check_used (&sym->attr, sym->name, where))
6de9cd9a
DN
1344 return FAILURE;
1345
1346 if (where == NULL)
63645982 1347 where = &gfc_current_locus;
6de9cd9a
DN
1348
1349 if (sym->attr.if_source != IFSRC_UNKNOWN
1350 && sym->attr.if_source != IFSRC_DECL)
1351 {
1352 gfc_error ("Symbol '%s' at %L already has an explicit interface",
1353 sym->name, where);
1354 return FAILURE;
1355 }
1356
1357 sym->formal = formal;
1358 sym->attr.if_source = source;
1359
1360 return SUCCESS;
1361}
1362
1363
1364/* Add a type to a symbol. */
1365
1366try
66e4ab31 1367gfc_add_type (gfc_symbol *sym, gfc_typespec *ts, locus *where)
6de9cd9a
DN
1368{
1369 sym_flavor flavor;
1370
6de9cd9a 1371 if (where == NULL)
63645982 1372 where = &gfc_current_locus;
6de9cd9a
DN
1373
1374 if (sym->ts.type != BT_UNKNOWN)
1375 {
6690a9e0
PT
1376 const char *msg = "Symbol '%s' at %L already has basic type of %s";
1377 if (!(sym->ts.type == ts->type
66e4ab31
SK
1378 && (sym->attr.flavor == FL_PROCEDURE || sym->attr.result))
1379 || gfc_notification_std (GFC_STD_GNU) == ERROR
1380 || pedantic)
6690a9e0
PT
1381 {
1382 gfc_error (msg, sym->name, where, gfc_basic_typename (sym->ts.type));
1383 return FAILURE;
1384 }
1385 else if (gfc_notify_std (GFC_STD_GNU, msg, sym->name, where,
1386 gfc_basic_typename (sym->ts.type)) == FAILURE)
66e4ab31 1387 return FAILURE;
6de9cd9a
DN
1388 }
1389
1390 flavor = sym->attr.flavor;
1391
1392 if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
66e4ab31
SK
1393 || flavor == FL_LABEL
1394 || (flavor == FL_PROCEDURE && sym->attr.subroutine)
6de9cd9a
DN
1395 || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1396 {
1397 gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1398 return FAILURE;
1399 }
1400
1401 sym->ts = *ts;
1402 return SUCCESS;
1403}
1404
1405
1406/* Clears all attributes. */
1407
1408void
66e4ab31 1409gfc_clear_attr (symbol_attribute *attr)
6de9cd9a 1410{
66e4ab31 1411 memset (attr, 0, sizeof (symbol_attribute));
6de9cd9a
DN
1412}
1413
1414
1415/* Check for missing attributes in the new symbol. Currently does
1416 nothing, but it's not clear that it is unnecessary yet. */
1417
1418try
66e4ab31
SK
1419gfc_missing_attr (symbol_attribute *attr ATTRIBUTE_UNUSED,
1420 locus *where ATTRIBUTE_UNUSED)
6de9cd9a
DN
1421{
1422
1423 return SUCCESS;
1424}
1425
1426
1427/* Copy an attribute to a symbol attribute, bit by bit. Some
1428 attributes have a lot of side-effects but cannot be present given
1429 where we are called from, so we ignore some bits. */
1430
1431try
a8b3b0b6 1432gfc_copy_attr (symbol_attribute *dest, symbol_attribute *src, locus *where)
6de9cd9a 1433{
a8b3b0b6
CR
1434 int is_proc_lang_bind_spec;
1435
6de9cd9a
DN
1436 if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1437 goto fail;
1438
231b2fcc 1439 if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
6de9cd9a
DN
1440 goto fail;
1441 if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1442 goto fail;
1443 if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1444 goto fail;
ee7e677f
TB
1445 if (src->protected && gfc_add_protected (dest, NULL, where) == FAILURE)
1446 goto fail;
231b2fcc 1447 if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
6de9cd9a 1448 goto fail;
06469efd
PT
1449 if (src->value && gfc_add_value (dest, NULL, where) == FAILURE)
1450 goto fail;
775e6c3a
TB
1451 if (src->volatile_ && gfc_add_volatile (dest, NULL, where) == FAILURE)
1452 goto fail;
66e4ab31
SK
1453 if (src->threadprivate
1454 && gfc_add_threadprivate (dest, NULL, where) == FAILURE)
6c7a4dfd 1455 goto fail;
6de9cd9a
DN
1456 if (src->target && gfc_add_target (dest, where) == FAILURE)
1457 goto fail;
231b2fcc 1458 if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
6de9cd9a 1459 goto fail;
231b2fcc 1460 if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
6de9cd9a
DN
1461 goto fail;
1462 if (src->entry)
1463 dest->entry = 1;
1464
231b2fcc 1465 if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
6de9cd9a
DN
1466 goto fail;
1467
231b2fcc 1468 if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
6de9cd9a 1469 goto fail;
6de9cd9a 1470
231b2fcc 1471 if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
6de9cd9a 1472 goto fail;
231b2fcc 1473 if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
6de9cd9a 1474 goto fail;
231b2fcc 1475 if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
6de9cd9a
DN
1476 goto fail;
1477
231b2fcc 1478 if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
6de9cd9a
DN
1479 goto fail;
1480 if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1481 goto fail;
1482 if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1483 goto fail;
1484 if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1485 goto fail;
1486
1487 if (src->flavor != FL_UNKNOWN
231b2fcc 1488 && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
6de9cd9a
DN
1489 goto fail;
1490
1491 if (src->intent != INTENT_UNKNOWN
1492 && gfc_add_intent (dest, src->intent, where) == FAILURE)
1493 goto fail;
1494
1495 if (src->access != ACCESS_UNKNOWN
231b2fcc 1496 && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
6de9cd9a
DN
1497 goto fail;
1498
1499 if (gfc_missing_attr (dest, where) == FAILURE)
1500 goto fail;
1501
83d890b9
AL
1502 if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1503 goto fail;
1504 if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1505 goto fail;
23bc73b5 1506
a8b3b0b6
CR
1507 is_proc_lang_bind_spec = (src->flavor == FL_PROCEDURE ? 1 : 0);
1508 if (src->is_bind_c
1509 && gfc_add_is_bind_c (dest, NULL, where, is_proc_lang_bind_spec)
1510 != SUCCESS)
1511 return FAILURE;
1512
1513 if (src->is_c_interop)
1514 dest->is_c_interop = 1;
1515 if (src->is_iso_c)
1516 dest->is_iso_c = 1;
1517
23bc73b5
DF
1518 if (src->external && gfc_add_external (dest, where) == FAILURE)
1519 goto fail;
1520 if (src->intrinsic && gfc_add_intrinsic (dest, where) == FAILURE)
1521 goto fail;
6de9cd9a
DN
1522
1523 return SUCCESS;
1524
1525fail:
1526 return FAILURE;
1527}
1528
1529
1530/************** Component name management ************/
1531
1532/* Component names of a derived type form their own little namespaces
1533 that are separate from all other spaces. The space is composed of
1534 a singly linked list of gfc_component structures whose head is
1535 located in the parent symbol. */
1536
1537
1538/* Add a component name to a symbol. The call fails if the name is
1539 already present. On success, the component pointer is modified to
1540 point to the additional component structure. */
1541
1542try
66e4ab31
SK
1543gfc_add_component (gfc_symbol *sym, const char *name,
1544 gfc_component **component)
6de9cd9a
DN
1545{
1546 gfc_component *p, *tail;
1547
1548 tail = NULL;
1549
1550 for (p = sym->components; p; p = p->next)
1551 {
1552 if (strcmp (p->name, name) == 0)
1553 {
1554 gfc_error ("Component '%s' at %C already declared at %L",
1555 name, &p->loc);
1556 return FAILURE;
1557 }
1558
1559 tail = p;
1560 }
1561
eebc3ee0 1562 /* Allocate a new component. */
6de9cd9a
DN
1563 p = gfc_get_component ();
1564
1565 if (tail == NULL)
1566 sym->components = p;
1567 else
1568 tail->next = p;
1569
cb9e4f55 1570 p->name = gfc_get_string (name);
63645982 1571 p->loc = gfc_current_locus;
6de9cd9a
DN
1572
1573 *component = p;
1574 return SUCCESS;
1575}
1576
1577
6b887797
PT
1578/* Recursive function to switch derived types of all symbol in a
1579 namespace. */
6de9cd9a
DN
1580
1581static void
66e4ab31 1582switch_types (gfc_symtree *st, gfc_symbol *from, gfc_symbol *to)
6de9cd9a
DN
1583{
1584 gfc_symbol *sym;
1585
1586 if (st == NULL)
1587 return;
1588
1589 sym = st->n.sym;
1590 if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1591 sym->ts.derived = to;
1592
1593 switch_types (st->left, from, to);
1594 switch_types (st->right, from, to);
1595}
1596
1597
1598/* This subroutine is called when a derived type is used in order to
1599 make the final determination about which version to use. The
1600 standard requires that a type be defined before it is 'used', but
1601 such types can appear in IMPLICIT statements before the actual
1602 definition. 'Using' in this context means declaring a variable to
1603 be that type or using the type constructor.
1604
1605 If a type is used and the components haven't been defined, then we
1606 have to have a derived type in a parent unit. We find the node in
1607 the other namespace and point the symtree node in this namespace to
1608 that node. Further reference to this name point to the correct
eebc3ee0 1609 node. If we can't find the node in a parent namespace, then we have
6de9cd9a
DN
1610 an error.
1611
1612 This subroutine takes a pointer to a symbol node and returns a
1613 pointer to the translated node or NULL for an error. Usually there
1614 is no translation and we return the node we were passed. */
1615
1e6283cb 1616gfc_symbol *
66e4ab31 1617gfc_use_derived (gfc_symbol *sym)
6de9cd9a 1618{
810306f2 1619 gfc_symbol *s;
6de9cd9a
DN
1620 gfc_typespec *t;
1621 gfc_symtree *st;
1622 int i;
1623
6b887797
PT
1624 if (sym->components != NULL)
1625 return sym; /* Already defined. */
3e978d30 1626
6b887797
PT
1627 if (sym->ns->parent == NULL)
1628 goto bad;
6de9cd9a
DN
1629
1630 if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1631 {
1632 gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1633 return NULL;
1634 }
1635
1636 if (s == NULL || s->attr.flavor != FL_DERIVED)
1637 goto bad;
1638
1639 /* Get rid of symbol sym, translating all references to s. */
1640 for (i = 0; i < GFC_LETTERS; i++)
1641 {
1642 t = &sym->ns->default_type[i];
1643 if (t->derived == sym)
1644 t->derived = s;
1645 }
1646
1647 st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1648 st->n.sym = s;
1649
1650 s->refs++;
1651
1652 /* Unlink from list of modified symbols. */
810306f2 1653 gfc_commit_symbol (sym);
6de9cd9a
DN
1654
1655 switch_types (sym->ns->sym_root, sym, s);
1656
1657 /* TODO: Also have to replace sym -> s in other lists like
1658 namelists, common lists and interface lists. */
1659 gfc_free_symbol (sym);
1660
1e6283cb 1661 return s;
6de9cd9a
DN
1662
1663bad:
1664 gfc_error ("Derived type '%s' at %C is being used before it is defined",
1665 sym->name);
1666 return NULL;
1667}
1668
1669
6de9cd9a
DN
1670/* Given a derived type node and a component name, try to locate the
1671 component structure. Returns the NULL pointer if the component is
1672 not found or the components are private. */
1673
1674gfc_component *
66e4ab31 1675gfc_find_component (gfc_symbol *sym, const char *name)
6de9cd9a
DN
1676{
1677 gfc_component *p;
1678
1679 if (name == NULL)
1680 return NULL;
1681
1682 sym = gfc_use_derived (sym);
1683
1684 if (sym == NULL)
1685 return NULL;
1686
1687 for (p = sym->components; p; p = p->next)
1688 if (strcmp (p->name, name) == 0)
1689 break;
1690
1691 if (p == NULL)
1692 gfc_error ("'%s' at %C is not a member of the '%s' structure",
1693 name, sym->name);
1694 else
1695 {
2eae3dc7
TB
1696 if (sym->attr.use_assoc && (sym->component_access == ACCESS_PRIVATE
1697 || p->access == ACCESS_PRIVATE))
6de9cd9a
DN
1698 {
1699 gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1700 name, sym->name);
1701 p = NULL;
1702 }
1703 }
1704
1705 return p;
1706}
1707
1708
1709/* Given a symbol, free all of the component structures and everything
1710 they point to. */
1711
1712static void
66e4ab31 1713free_components (gfc_component *p)
6de9cd9a
DN
1714{
1715 gfc_component *q;
1716
1717 for (; p; p = q)
1718 {
1719 q = p->next;
1720
1721 gfc_free_array_spec (p->as);
1722 gfc_free_expr (p->initializer);
1723
1724 gfc_free (p);
1725 }
1726}
1727
1728
66e4ab31 1729/* Set component attributes from a standard symbol attribute structure. */
6de9cd9a
DN
1730
1731void
66e4ab31 1732gfc_set_component_attr (gfc_component *c, symbol_attribute *attr)
6de9cd9a
DN
1733{
1734
1735 c->dimension = attr->dimension;
1736 c->pointer = attr->pointer;
5046aff5 1737 c->allocatable = attr->allocatable;
2eae3dc7 1738 c->access = attr->access;
6de9cd9a
DN
1739}
1740
1741
1742/* Get a standard symbol attribute structure given the component
1743 structure. */
1744
1745void
66e4ab31 1746gfc_get_component_attr (symbol_attribute *attr, gfc_component *c)
6de9cd9a
DN
1747{
1748
1749 gfc_clear_attr (attr);
1750 attr->dimension = c->dimension;
1751 attr->pointer = c->pointer;
5046aff5 1752 attr->allocatable = c->allocatable;
2eae3dc7 1753 attr->access = c->access;
6de9cd9a
DN
1754}
1755
1756
1757/******************** Statement label management ********************/
1758
5cf54585
TS
1759/* Comparison function for statement labels, used for managing the
1760 binary tree. */
1761
1762static int
66e4ab31 1763compare_st_labels (void *a1, void *b1)
5cf54585 1764{
66e4ab31
SK
1765 int a = ((gfc_st_label *) a1)->value;
1766 int b = ((gfc_st_label *) b1)->value;
5cf54585
TS
1767
1768 return (b - a);
1769}
1770
1771
1772/* Free a single gfc_st_label structure, making sure the tree is not
6de9cd9a
DN
1773 messed up. This function is called only when some parse error
1774 occurs. */
1775
1776void
66e4ab31 1777gfc_free_st_label (gfc_st_label *label)
6de9cd9a 1778{
66e4ab31 1779
b5cbe7ee 1780 if (label == NULL)
6de9cd9a
DN
1781 return;
1782
5cf54585 1783 gfc_delete_bbt (&gfc_current_ns->st_labels, label, compare_st_labels);
b5cbe7ee
SK
1784
1785 if (label->format != NULL)
1786 gfc_free_expr (label->format);
1787
1788 gfc_free (label);
6de9cd9a
DN
1789}
1790
66e4ab31 1791
5cf54585 1792/* Free a whole tree of gfc_st_label structures. */
6de9cd9a
DN
1793
1794static void
66e4ab31 1795free_st_labels (gfc_st_label *label)
6de9cd9a 1796{
66e4ab31 1797
5cf54585
TS
1798 if (label == NULL)
1799 return;
6de9cd9a 1800
5cf54585
TS
1801 free_st_labels (label->left);
1802 free_st_labels (label->right);
1803
1804 if (label->format != NULL)
1805 gfc_free_expr (label->format);
1806 gfc_free (label);
6de9cd9a
DN
1807}
1808
1809
1810/* Given a label number, search for and return a pointer to the label
1811 structure, creating it if it does not exist. */
1812
1813gfc_st_label *
1814gfc_get_st_label (int labelno)
1815{
1816 gfc_st_label *lp;
1817
1818 /* First see if the label is already in this namespace. */
5cf54585
TS
1819 lp = gfc_current_ns->st_labels;
1820 while (lp)
1821 {
1822 if (lp->value == labelno)
1823 return lp;
1824
1825 if (lp->value < labelno)
1826 lp = lp->left;
1827 else
1828 lp = lp->right;
1829 }
6de9cd9a
DN
1830
1831 lp = gfc_getmem (sizeof (gfc_st_label));
1832
1833 lp->value = labelno;
1834 lp->defined = ST_LABEL_UNKNOWN;
1835 lp->referenced = ST_LABEL_UNKNOWN;
1836
5cf54585 1837 gfc_insert_bbt (&gfc_current_ns->st_labels, lp, compare_st_labels);
6de9cd9a
DN
1838
1839 return lp;
1840}
1841
1842
1843/* Called when a statement with a statement label is about to be
1844 accepted. We add the label to the list of the current namespace,
1845 making sure it hasn't been defined previously and referenced
1846 correctly. */
1847
1848void
66e4ab31 1849gfc_define_st_label (gfc_st_label *lp, gfc_sl_type type, locus *label_locus)
6de9cd9a
DN
1850{
1851 int labelno;
1852
1853 labelno = lp->value;
1854
1855 if (lp->defined != ST_LABEL_UNKNOWN)
1856 gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1857 &lp->where, label_locus);
1858 else
1859 {
1860 lp->where = *label_locus;
1861
1862 switch (type)
1863 {
1864 case ST_LABEL_FORMAT:
1865 if (lp->referenced == ST_LABEL_TARGET)
1866 gfc_error ("Label %d at %C already referenced as branch target",
1867 labelno);
1868 else
1869 lp->defined = ST_LABEL_FORMAT;
1870
1871 break;
1872
1873 case ST_LABEL_TARGET:
1874 if (lp->referenced == ST_LABEL_FORMAT)
1875 gfc_error ("Label %d at %C already referenced as a format label",
1876 labelno);
1877 else
1878 lp->defined = ST_LABEL_TARGET;
1879
1880 break;
1881
1882 default:
1883 lp->defined = ST_LABEL_BAD_TARGET;
1884 lp->referenced = ST_LABEL_BAD_TARGET;
1885 }
1886 }
1887}
1888
1889
1890/* Reference a label. Given a label and its type, see if that
1891 reference is consistent with what is known about that label,
1892 updating the unknown state. Returns FAILURE if something goes
1893 wrong. */
1894
1895try
66e4ab31 1896gfc_reference_st_label (gfc_st_label *lp, gfc_sl_type type)
6de9cd9a
DN
1897{
1898 gfc_sl_type label_type;
1899 int labelno;
1900 try rc;
1901
1902 if (lp == NULL)
1903 return SUCCESS;
1904
1905 labelno = lp->value;
1906
1907 if (lp->defined != ST_LABEL_UNKNOWN)
1908 label_type = lp->defined;
1909 else
1910 {
1911 label_type = lp->referenced;
63645982 1912 lp->where = gfc_current_locus;
6de9cd9a
DN
1913 }
1914
1915 if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1916 {
1917 gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1918 rc = FAILURE;
1919 goto done;
1920 }
1921
1922 if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1923 && type == ST_LABEL_FORMAT)
1924 {
1925 gfc_error ("Label %d at %C previously used as branch target", labelno);
1926 rc = FAILURE;
1927 goto done;
1928 }
1929
1930 lp->referenced = type;
1931 rc = SUCCESS;
1932
1933done:
1934 return rc;
1935}
1936
1937
1938/************** Symbol table management subroutines ****************/
1939
1940/* Basic details: Fortran 95 requires a potentially unlimited number
1941 of distinct namespaces when compiling a program unit. This case
1942 occurs during a compilation of internal subprograms because all of
1943 the internal subprograms must be read before we can start
1944 generating code for the host.
1945
eebc3ee0 1946 Given the tricky nature of the Fortran grammar, we must be able to
6de9cd9a
DN
1947 undo changes made to a symbol table if the current interpretation
1948 of a statement is found to be incorrect. Whenever a symbol is
1949 looked up, we make a copy of it and link to it. All of these
1950 symbols are kept in a singly linked list so that we can commit or
1951 undo the changes at a later time.
1952
4f613946 1953 A symtree may point to a symbol node outside of its namespace. In
6de9cd9a
DN
1954 this case, that symbol has been used as a host associated variable
1955 at some previous time. */
1956
0366dfe9
TS
1957/* Allocate a new namespace structure. Copies the implicit types from
1958 PARENT if PARENT_TYPES is set. */
6de9cd9a
DN
1959
1960gfc_namespace *
66e4ab31 1961gfc_get_namespace (gfc_namespace *parent, int parent_types)
6de9cd9a
DN
1962{
1963 gfc_namespace *ns;
1964 gfc_typespec *ts;
1965 gfc_intrinsic_op in;
1966 int i;
1967
1968 ns = gfc_getmem (sizeof (gfc_namespace));
1969 ns->sym_root = NULL;
1970 ns->uop_root = NULL;
1971 ns->default_access = ACCESS_UNKNOWN;
1972 ns->parent = parent;
1973
1974 for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1975 ns->operator_access[in] = ACCESS_UNKNOWN;
1976
1977 /* Initialize default implicit types. */
1978 for (i = 'a'; i <= 'z'; i++)
1979 {
1980 ns->set_flag[i - 'a'] = 0;
1981 ts = &ns->default_type[i - 'a'];
1982
0366dfe9 1983 if (parent_types && ns->parent != NULL)
6de9cd9a 1984 {
66e4ab31 1985 /* Copy parent settings. */
6de9cd9a
DN
1986 *ts = ns->parent->default_type[i - 'a'];
1987 continue;
1988 }
1989
1990 if (gfc_option.flag_implicit_none != 0)
1991 {
1992 gfc_clear_ts (ts);
1993 continue;
1994 }
1995
1996 if ('i' <= i && i <= 'n')
1997 {
1998 ts->type = BT_INTEGER;
9d64df18 1999 ts->kind = gfc_default_integer_kind;
6de9cd9a
DN
2000 }
2001 else
2002 {
2003 ts->type = BT_REAL;
9d64df18 2004 ts->kind = gfc_default_real_kind;
6de9cd9a
DN
2005 }
2006 }
2007
3d79abbd
PB
2008 ns->refs = 1;
2009
6de9cd9a
DN
2010 return ns;
2011}
2012
2013
2014/* Comparison function for symtree nodes. */
2015
2016static int
66e4ab31 2017compare_symtree (void *_st1, void *_st2)
6de9cd9a
DN
2018{
2019 gfc_symtree *st1, *st2;
2020
2021 st1 = (gfc_symtree *) _st1;
2022 st2 = (gfc_symtree *) _st2;
2023
2024 return strcmp (st1->name, st2->name);
2025}
2026
2027
2028/* Allocate a new symtree node and associate it with the new symbol. */
2029
2030gfc_symtree *
66e4ab31 2031gfc_new_symtree (gfc_symtree **root, const char *name)
6de9cd9a
DN
2032{
2033 gfc_symtree *st;
2034
2035 st = gfc_getmem (sizeof (gfc_symtree));
cb9e4f55 2036 st->name = gfc_get_string (name);
6de9cd9a
DN
2037
2038 gfc_insert_bbt (root, st, compare_symtree);
2039 return st;
2040}
2041
2042
2043/* Delete a symbol from the tree. Does not free the symbol itself! */
2044
2045static void
66e4ab31 2046delete_symtree (gfc_symtree **root, const char *name)
6de9cd9a
DN
2047{
2048 gfc_symtree st, *st0;
2049
2050 st0 = gfc_find_symtree (*root, name);
2051
cb9e4f55 2052 st.name = gfc_get_string (name);
6de9cd9a
DN
2053 gfc_delete_bbt (root, &st, compare_symtree);
2054
2055 gfc_free (st0);
2056}
2057
2058
2059/* Given a root symtree node and a name, try to find the symbol within
2060 the namespace. Returns NULL if the symbol is not found. */
2061
2062gfc_symtree *
66e4ab31 2063gfc_find_symtree (gfc_symtree *st, const char *name)
6de9cd9a
DN
2064{
2065 int c;
2066
2067 while (st != NULL)
2068 {
2069 c = strcmp (name, st->name);
2070 if (c == 0)
2071 return st;
2072
2073 st = (c < 0) ? st->left : st->right;
2074 }
2075
2076 return NULL;
2077}
2078
2079
2080/* Given a name find a user operator node, creating it if it doesn't
2081 exist. These are much simpler than symbols because they can't be
2082 ambiguous with one another. */
2083
2084gfc_user_op *
2085gfc_get_uop (const char *name)
2086{
2087 gfc_user_op *uop;
2088 gfc_symtree *st;
2089
2090 st = gfc_find_symtree (gfc_current_ns->uop_root, name);
2091 if (st != NULL)
2092 return st->n.uop;
2093
2094 st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
2095
2096 uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
cb9e4f55 2097 uop->name = gfc_get_string (name);
6de9cd9a
DN
2098 uop->access = ACCESS_UNKNOWN;
2099 uop->ns = gfc_current_ns;
2100
2101 return uop;
2102}
2103
2104
2105/* Given a name find the user operator node. Returns NULL if it does
2106 not exist. */
2107
2108gfc_user_op *
66e4ab31 2109gfc_find_uop (const char *name, gfc_namespace *ns)
6de9cd9a
DN
2110{
2111 gfc_symtree *st;
2112
2113 if (ns == NULL)
2114 ns = gfc_current_ns;
2115
2116 st = gfc_find_symtree (ns->uop_root, name);
2117 return (st == NULL) ? NULL : st->n.uop;
2118}
2119
2120
2121/* Remove a gfc_symbol structure and everything it points to. */
2122
2123void
66e4ab31 2124gfc_free_symbol (gfc_symbol *sym)
6de9cd9a
DN
2125{
2126
2127 if (sym == NULL)
2128 return;
2129
2130 gfc_free_array_spec (sym->as);
2131
2132 free_components (sym->components);
2133
2134 gfc_free_expr (sym->value);
2135
2136 gfc_free_namelist (sym->namelist);
2137
2138 gfc_free_namespace (sym->formal_ns);
2139
1027275d
PT
2140 if (!sym->attr.generic_copy)
2141 gfc_free_interface (sym->generic);
6de9cd9a
DN
2142
2143 gfc_free_formal_arglist (sym->formal);
2144
2145 gfc_free (sym);
2146}
2147
2148
2149/* Allocate and initialize a new symbol node. */
2150
2151gfc_symbol *
66e4ab31 2152gfc_new_symbol (const char *name, gfc_namespace *ns)
6de9cd9a
DN
2153{
2154 gfc_symbol *p;
2155
2156 p = gfc_getmem (sizeof (gfc_symbol));
2157
2158 gfc_clear_ts (&p->ts);
2159 gfc_clear_attr (&p->attr);
2160 p->ns = ns;
2161
63645982 2162 p->declared_at = gfc_current_locus;
6de9cd9a
DN
2163
2164 if (strlen (name) > GFC_MAX_SYMBOL_LEN)
2165 gfc_internal_error ("new_symbol(): Symbol name too long");
2166
cb9e4f55 2167 p->name = gfc_get_string (name);
a8b3b0b6
CR
2168
2169 /* Make sure flags for symbol being C bound are clear initially. */
2170 p->attr.is_bind_c = 0;
2171 p->attr.is_iso_c = 0;
2172 /* Make sure the binding label field has a Nul char to start. */
2173 p->binding_label[0] = '\0';
2174
2175 /* Clear the ptrs we may need. */
2176 p->common_block = NULL;
2177
6de9cd9a
DN
2178 return p;
2179}
2180
2181
2182/* Generate an error if a symbol is ambiguous. */
2183
2184static void
66e4ab31 2185ambiguous_symbol (const char *name, gfc_symtree *st)
6de9cd9a
DN
2186{
2187
cb9e4f55 2188 if (st->n.sym->module)
6de9cd9a
DN
2189 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2190 "from module '%s'", name, st->n.sym->name, st->n.sym->module);
2191 else
2192 gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
2193 "from current program unit", name, st->n.sym->name);
2194}
2195
2196
294fbfc8 2197/* Search for a symtree starting in the current namespace, resorting to
6de9cd9a 2198 any parent namespaces if requested by a nonzero parent_flag.
294fbfc8 2199 Returns nonzero if the name is ambiguous. */
6de9cd9a
DN
2200
2201int
66e4ab31
SK
2202gfc_find_sym_tree (const char *name, gfc_namespace *ns, int parent_flag,
2203 gfc_symtree **result)
6de9cd9a
DN
2204{
2205 gfc_symtree *st;
2206
2207 if (ns == NULL)
2208 ns = gfc_current_ns;
2209
2210 do
2211 {
2212 st = gfc_find_symtree (ns->sym_root, name);
2213 if (st != NULL)
2214 {
2215 *result = st;
993ef28f
PT
2216 /* Ambiguous generic interfaces are permitted, as long
2217 as the specific interfaces are different. */
2218 if (st->ambiguous && !st->n.sym->attr.generic)
6de9cd9a
DN
2219 {
2220 ambiguous_symbol (name, st);
2221 return 1;
2222 }
2223
2224 return 0;
2225 }
2226
2227 if (!parent_flag)
2228 break;
2229
2230 ns = ns->parent;
2231 }
2232 while (ns != NULL);
2233
2234 *result = NULL;
2235 return 0;
2236}
2237
2238
294fbfc8
TS
2239/* Same, but returns the symbol instead. */
2240
6de9cd9a 2241int
66e4ab31
SK
2242gfc_find_symbol (const char *name, gfc_namespace *ns, int parent_flag,
2243 gfc_symbol **result)
6de9cd9a
DN
2244{
2245 gfc_symtree *st;
2246 int i;
2247
2248 i = gfc_find_sym_tree (name, ns, parent_flag, &st);
2249
2250 if (st == NULL)
2251 *result = NULL;
2252 else
2253 *result = st->n.sym;
2254
2255 return i;
2256}
2257
2258
2259/* Save symbol with the information necessary to back it out. */
2260
2261static void
66e4ab31 2262save_symbol_data (gfc_symbol *sym)
6de9cd9a
DN
2263{
2264
2265 if (sym->new || sym->old_symbol != NULL)
2266 return;
2267
2268 sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
2269 *(sym->old_symbol) = *sym;
2270
2271 sym->tlink = changed_syms;
2272 changed_syms = sym;
2273}
2274
2275
2276/* Given a name, find a symbol, or create it if it does not exist yet
2277 in the current namespace. If the symbol is found we make sure that
2278 it's OK.
2279
2280 The integer return code indicates
2281 0 All OK
2282 1 The symbol name was ambiguous
2283 2 The name meant to be established was already host associated.
2284
2285 So if the return value is nonzero, then an error was issued. */
2286
2287int
66e4ab31 2288gfc_get_sym_tree (const char *name, gfc_namespace *ns, gfc_symtree **result)
6de9cd9a
DN
2289{
2290 gfc_symtree *st;
2291 gfc_symbol *p;
2292
2293 /* This doesn't usually happen during resolution. */
2294 if (ns == NULL)
2295 ns = gfc_current_ns;
2296
2297 /* Try to find the symbol in ns. */
2298 st = gfc_find_symtree (ns->sym_root, name);
2299
2300 if (st == NULL)
2301 {
2302 /* If not there, create a new symbol. */
2303 p = gfc_new_symbol (name, ns);
2304
2305 /* Add to the list of tentative symbols. */
2306 p->old_symbol = NULL;
2307 p->tlink = changed_syms;
2308 p->mark = 1;
2309 p->new = 1;
2310 changed_syms = p;
2311
2312 st = gfc_new_symtree (&ns->sym_root, name);
2313 st->n.sym = p;
2314 p->refs++;
2315
2316 }
2317 else
2318 {
993ef28f
PT
2319 /* Make sure the existing symbol is OK. Ambiguous
2320 generic interfaces are permitted, as long as the
2321 specific interfaces are different. */
2322 if (st->ambiguous && !st->n.sym->attr.generic)
6de9cd9a
DN
2323 {
2324 ambiguous_symbol (name, st);
2325 return 1;
2326 }
2327
2328 p = st->n.sym;
2329
2330 if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2331 {
2332 /* Symbol is from another namespace. */
2333 gfc_error ("Symbol '%s' at %C has already been host associated",
2334 name);
2335 return 2;
2336 }
2337
2338 p->mark = 1;
2339
2340 /* Copy in case this symbol is changed. */
2341 save_symbol_data (p);
2342 }
2343
2344 *result = st;
2345 return 0;
2346}
2347
2348
2349int
66e4ab31 2350gfc_get_symbol (const char *name, gfc_namespace *ns, gfc_symbol **result)
6de9cd9a
DN
2351{
2352 gfc_symtree *st;
2353 int i;
2354
6de9cd9a
DN
2355 i = gfc_get_sym_tree (name, ns, &st);
2356 if (i != 0)
2357 return i;
2358
2359 if (st)
2360 *result = st->n.sym;
2361 else
2362 *result = NULL;
2363 return i;
2364}
2365
2366
2367/* Subroutine that searches for a symbol, creating it if it doesn't
2368 exist, but tries to host-associate the symbol if possible. */
2369
2370int
66e4ab31 2371gfc_get_ha_sym_tree (const char *name, gfc_symtree **result)
6de9cd9a
DN
2372{
2373 gfc_symtree *st;
2374 int i;
2375
2376 i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2377 if (st != NULL)
2378 {
2379 save_symbol_data (st->n.sym);
6de9cd9a
DN
2380 *result = st;
2381 return i;
2382 }
2383
2384 if (gfc_current_ns->parent != NULL)
2385 {
2386 i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2387 if (i)
2388 return i;
2389
2390 if (st != NULL)
2391 {
2392 *result = st;
2393 return 0;
2394 }
2395 }
2396
2397 return gfc_get_sym_tree (name, gfc_current_ns, result);
2398}
2399
2400
2401int
66e4ab31 2402gfc_get_ha_symbol (const char *name, gfc_symbol **result)
6de9cd9a
DN
2403{
2404 int i;
2405 gfc_symtree *st;
2406
2407 i = gfc_get_ha_sym_tree (name, &st);
2408
2409 if (st)
2410 *result = st->n.sym;
2411 else
2412 *result = NULL;
2413
2414 return i;
2415}
2416
2417/* Return true if both symbols could refer to the same data object. Does
2418 not take account of aliasing due to equivalence statements. */
2419
2420int
66e4ab31 2421gfc_symbols_could_alias (gfc_symbol *lsym, gfc_symbol *rsym)
6de9cd9a
DN
2422{
2423 /* Aliasing isn't possible if the symbols have different base types. */
2424 if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2425 return 0;
2426
2427 /* Pointers can point to other pointers, target objects and allocatable
2428 objects. Two allocatable objects cannot share the same storage. */
2429 if (lsym->attr.pointer
2430 && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2431 return 1;
2432 if (lsym->attr.target && rsym->attr.pointer)
2433 return 1;
2434 if (lsym->attr.allocatable && rsym->attr.pointer)
2435 return 1;
2436
2437 return 0;
2438}
2439
2440
2441/* Undoes all the changes made to symbols in the current statement.
2442 This subroutine is made simpler due to the fact that attributes are
2443 never removed once added. */
2444
2445void
2446gfc_undo_symbols (void)
2447{
2448 gfc_symbol *p, *q, *old;
2449
2450 for (p = changed_syms; p; p = q)
2451 {
2452 q = p->tlink;
2453
2454 if (p->new)
2455 {
2456 /* Symbol was new. */
2457 delete_symtree (&p->ns->sym_root, p->name);
2458
2459 p->refs--;
2460 if (p->refs < 0)
2461 gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2462 if (p->refs == 0)
2463 gfc_free_symbol (p);
2464 continue;
2465 }
2466
2467 /* Restore previous state of symbol. Just copy simple stuff. */
2468 p->mark = 0;
2469 old = p->old_symbol;
2470
2471 p->ts.type = old->ts.type;
2472 p->ts.kind = old->ts.kind;
2473
2474 p->attr = old->attr;
2475
2476 if (p->value != old->value)
2477 {
2478 gfc_free_expr (old->value);
2479 p->value = NULL;
2480 }
2481
2482 if (p->as != old->as)
2483 {
2484 if (p->as)
2485 gfc_free_array_spec (p->as);
2486 p->as = old->as;
2487 }
2488
2489 p->generic = old->generic;
2490 p->component_access = old->component_access;
2491
2492 if (p->namelist != NULL && old->namelist == NULL)
2493 {
2494 gfc_free_namelist (p->namelist);
2495 p->namelist = NULL;
2496 }
2497 else
2498 {
6de9cd9a
DN
2499 if (p->namelist_tail != old->namelist_tail)
2500 {
2501 gfc_free_namelist (old->namelist_tail);
2502 old->namelist_tail->next = NULL;
2503 }
2504 }
2505
2506 p->namelist_tail = old->namelist_tail;
2507
2508 if (p->formal != old->formal)
2509 {
2510 gfc_free_formal_arglist (p->formal);
2511 p->formal = old->formal;
2512 }
2513
2514 gfc_free (p->old_symbol);
2515 p->old_symbol = NULL;
2516 p->tlink = NULL;
2517 }
2518
2519 changed_syms = NULL;
2520}
2521
2522
091c9413
EE
2523/* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2524 components of old_symbol that might need deallocation are the "allocatables"
2525 that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2526 namelist_tail. In case these differ between old_symbol and sym, it's just
2527 because sym->namelist has gotten a few more items. */
810306f2
EE
2528
2529static void
66e4ab31 2530free_old_symbol (gfc_symbol *sym)
810306f2 2531{
66e4ab31 2532
810306f2
EE
2533 if (sym->old_symbol == NULL)
2534 return;
2535
2536 if (sym->old_symbol->as != sym->as)
2537 gfc_free_array_spec (sym->old_symbol->as);
2538
2539 if (sym->old_symbol->value != sym->value)
2540 gfc_free_expr (sym->old_symbol->value);
2541
091c9413
EE
2542 if (sym->old_symbol->formal != sym->formal)
2543 gfc_free_formal_arglist (sym->old_symbol->formal);
2544
810306f2
EE
2545 gfc_free (sym->old_symbol);
2546 sym->old_symbol = NULL;
2547}
2548
2549
6de9cd9a
DN
2550/* Makes the changes made in the current statement permanent-- gets
2551 rid of undo information. */
2552
2553void
2554gfc_commit_symbols (void)
2555{
2556 gfc_symbol *p, *q;
2557
2558 for (p = changed_syms; p; p = q)
2559 {
2560 q = p->tlink;
2561 p->tlink = NULL;
2562 p->mark = 0;
2563 p->new = 0;
810306f2 2564 free_old_symbol (p);
6de9cd9a 2565 }
6de9cd9a
DN
2566 changed_syms = NULL;
2567}
2568
2569
810306f2
EE
2570/* Makes the changes made in one symbol permanent -- gets rid of undo
2571 information. */
2572
2573void
66e4ab31 2574gfc_commit_symbol (gfc_symbol *sym)
810306f2
EE
2575{
2576 gfc_symbol *p;
2577
2578 if (changed_syms == sym)
2579 changed_syms = sym->tlink;
2580 else
2581 {
2582 for (p = changed_syms; p; p = p->tlink)
2583 if (p->tlink == sym)
2584 {
2585 p->tlink = sym->tlink;
2586 break;
2587 }
2588 }
2589
2590 sym->tlink = NULL;
2591 sym->mark = 0;
2592 sym->new = 0;
2593
2594 free_old_symbol (sym);
2595}
2596
2597
53814b8f
TS
2598/* Recursive function that deletes an entire tree and all the common
2599 head structures it points to. */
2600
2601static void
2602free_common_tree (gfc_symtree * common_tree)
2603{
2604 if (common_tree == NULL)
2605 return;
2606
2607 free_common_tree (common_tree->left);
2608 free_common_tree (common_tree->right);
2609
2610 gfc_free (common_tree);
2611}
2612
2613
6de9cd9a
DN
2614/* Recursive function that deletes an entire tree and all the user
2615 operator nodes that it contains. */
2616
2617static void
66e4ab31 2618free_uop_tree (gfc_symtree *uop_tree)
6de9cd9a
DN
2619{
2620
2621 if (uop_tree == NULL)
2622 return;
2623
2624 free_uop_tree (uop_tree->left);
2625 free_uop_tree (uop_tree->right);
2626
2627 gfc_free_interface (uop_tree->n.uop->operator);
2628
2629 gfc_free (uop_tree->n.uop);
2630 gfc_free (uop_tree);
2631}
2632
2633
2634/* Recursive function that deletes an entire tree and all the symbols
2635 that it contains. */
2636
2637static void
66e4ab31 2638free_sym_tree (gfc_symtree *sym_tree)
6de9cd9a
DN
2639{
2640 gfc_namespace *ns;
2641 gfc_symbol *sym;
2642
2643 if (sym_tree == NULL)
2644 return;
2645
2646 free_sym_tree (sym_tree->left);
2647 free_sym_tree (sym_tree->right);
2648
2649 sym = sym_tree->n.sym;
2650
2651 sym->refs--;
2652 if (sym->refs < 0)
2653 gfc_internal_error ("free_sym_tree(): Negative refs");
2654
2655 if (sym->formal_ns != NULL && sym->refs == 1)
2656 {
2657 /* As formal_ns contains a reference to sym, delete formal_ns just
2658 before the deletion of sym. */
2659 ns = sym->formal_ns;
2660 sym->formal_ns = NULL;
2661 gfc_free_namespace (ns);
2662 }
2663 else if (sym->refs == 0)
2664 {
2665 /* Go ahead and delete the symbol. */
2666 gfc_free_symbol (sym);
2667 }
2668
2669 gfc_free (sym_tree);
2670}
2671
2672
7453378e 2673/* Free the derived type list. */
6b887797
PT
2674
2675static void
7453378e 2676gfc_free_dt_list (void)
6b887797 2677{
7453378e 2678 gfc_dt_list *dt, *n;
6b887797 2679
7453378e 2680 for (dt = gfc_derived_types; dt; dt = n)
6b887797
PT
2681 {
2682 n = dt->next;
2683 gfc_free (dt);
2684 }
7453378e
PT
2685
2686 gfc_derived_types = NULL;
6b887797
PT
2687}
2688
2689
61321991
PT
2690/* Free the gfc_equiv_info's. */
2691
2692static void
66e4ab31 2693gfc_free_equiv_infos (gfc_equiv_info *s)
61321991
PT
2694{
2695 if (s == NULL)
2696 return;
2697 gfc_free_equiv_infos (s->next);
2698 gfc_free (s);
2699}
2700
2701
2702/* Free the gfc_equiv_lists. */
2703
2704static void
66e4ab31 2705gfc_free_equiv_lists (gfc_equiv_list *l)
61321991
PT
2706{
2707 if (l == NULL)
2708 return;
2709 gfc_free_equiv_lists (l->next);
2710 gfc_free_equiv_infos (l->equiv);
2711 gfc_free (l);
2712}
2713
2714
6de9cd9a
DN
2715/* Free a namespace structure and everything below it. Interface
2716 lists associated with intrinsic operators are not freed. These are
2717 taken care of when a specific name is freed. */
2718
2719void
66e4ab31 2720gfc_free_namespace (gfc_namespace *ns)
6de9cd9a
DN
2721{
2722 gfc_charlen *cl, *cl2;
2723 gfc_namespace *p, *q;
2724 gfc_intrinsic_op i;
2725
2726 if (ns == NULL)
2727 return;
2728
3d79abbd
PB
2729 ns->refs--;
2730 if (ns->refs > 0)
2731 return;
6e45f57b 2732 gcc_assert (ns->refs == 0);
3d79abbd 2733
6de9cd9a
DN
2734 gfc_free_statements (ns->code);
2735
2736 free_sym_tree (ns->sym_root);
2737 free_uop_tree (ns->uop_root);
53814b8f 2738 free_common_tree (ns->common_root);
6de9cd9a
DN
2739
2740 for (cl = ns->cl_list; cl; cl = cl2)
2741 {
2742 cl2 = cl->next;
2743 gfc_free_expr (cl->length);
2744 gfc_free (cl);
2745 }
2746
2747 free_st_labels (ns->st_labels);
2748
2749 gfc_free_equiv (ns->equiv);
61321991 2750 gfc_free_equiv_lists (ns->equiv_lists);
6de9cd9a
DN
2751
2752 for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2753 gfc_free_interface (ns->operator[i]);
2754
2755 gfc_free_data (ns->data);
2756 p = ns->contained;
2757 gfc_free (ns);
2758
2759 /* Recursively free any contained namespaces. */
2760 while (p != NULL)
2761 {
2762 q = p;
2763 p = p->sibling;
6de9cd9a
DN
2764 gfc_free_namespace (q);
2765 }
2766}
2767
2768
2769void
2770gfc_symbol_init_2 (void)
2771{
2772
0366dfe9 2773 gfc_current_ns = gfc_get_namespace (NULL, 0);
6de9cd9a
DN
2774}
2775
2776
2777void
2778gfc_symbol_done_2 (void)
2779{
2780
2781 gfc_free_namespace (gfc_current_ns);
2782 gfc_current_ns = NULL;
7453378e 2783 gfc_free_dt_list ();
6de9cd9a
DN
2784}
2785
2786
2787/* Clear mark bits from symbol nodes associated with a symtree node. */
2788
2789static void
66e4ab31 2790clear_sym_mark (gfc_symtree *st)
6de9cd9a
DN
2791{
2792
2793 st->n.sym->mark = 0;
2794}
2795
2796
2797/* Recursively traverse the symtree nodes. */
2798
9056bd70 2799void
66e4ab31 2800gfc_traverse_symtree (gfc_symtree *st, void (*func) (gfc_symtree *))
6de9cd9a 2801{
6de9cd9a
DN
2802 if (st != NULL)
2803 {
2804 (*func) (st);
2805
9056bd70
TS
2806 gfc_traverse_symtree (st->left, func);
2807 gfc_traverse_symtree (st->right, func);
6de9cd9a
DN
2808 }
2809}
2810
2811
6de9cd9a
DN
2812/* Recursive namespace traversal function. */
2813
2814static void
66e4ab31 2815traverse_ns (gfc_symtree *st, void (*func) (gfc_symbol *))
6de9cd9a
DN
2816{
2817
2818 if (st == NULL)
2819 return;
2820
2821 if (st->n.sym->mark == 0)
2822 (*func) (st->n.sym);
2823 st->n.sym->mark = 1;
2824
2825 traverse_ns (st->left, func);
2826 traverse_ns (st->right, func);
2827}
2828
2829
2830/* Call a given function for all symbols in the namespace. We take
2831 care that each gfc_symbol node is called exactly once. */
2832
2833void
66e4ab31 2834gfc_traverse_ns (gfc_namespace *ns, void (*func) (gfc_symbol *))
6de9cd9a
DN
2835{
2836
9056bd70 2837 gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
6de9cd9a
DN
2838
2839 traverse_ns (ns->sym_root, func);
2840}
2841
2842
bd83e614 2843/* Return TRUE if the symbol is an automatic variable. */
66e4ab31 2844
bd83e614 2845static bool
66e4ab31 2846gfc_is_var_automatic (gfc_symbol *sym)
bd83e614
PB
2847{
2848 /* Pointer and allocatable variables are never automatic. */
2849 if (sym->attr.pointer || sym->attr.allocatable)
2850 return false;
2851 /* Check for arrays with non-constant size. */
2852 if (sym->attr.dimension && sym->as
2853 && !gfc_is_compile_time_shape (sym->as))
2854 return true;
5189dd41 2855 /* Check for non-constant length character variables. */
bd83e614
PB
2856 if (sym->ts.type == BT_CHARACTER
2857 && sym->ts.cl
d05d9ac7 2858 && !gfc_is_constant_expr (sym->ts.cl->length))
bd83e614
PB
2859 return true;
2860 return false;
2861}
2862
6de9cd9a
DN
2863/* Given a symbol, mark it as SAVEd if it is allowed. */
2864
2865static void
66e4ab31 2866save_symbol (gfc_symbol *sym)
6de9cd9a
DN
2867{
2868
2869 if (sym->attr.use_assoc)
2870 return;
2871
6de9cd9a
DN
2872 if (sym->attr.in_common
2873 || sym->attr.dummy
2874 || sym->attr.flavor != FL_VARIABLE)
2875 return;
bd83e614
PB
2876 /* Automatic objects are not saved. */
2877 if (gfc_is_var_automatic (sym))
2878 return;
231b2fcc 2879 gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
6de9cd9a
DN
2880}
2881
2882
2883/* Mark those symbols which can be SAVEd as such. */
2884
2885void
66e4ab31 2886gfc_save_all (gfc_namespace *ns)
6de9cd9a
DN
2887{
2888
2889 gfc_traverse_ns (ns, save_symbol);
2890}
2891
2892
2893#ifdef GFC_DEBUG
2894/* Make sure that no changes to symbols are pending. */
2895
2896void
2897gfc_symbol_state(void) {
2898
2899 if (changed_syms != NULL)
2900 gfc_internal_error("Symbol changes still pending!");
2901}
2902#endif
2903
c9543002
TS
2904
2905/************** Global symbol handling ************/
2906
2907
2908/* Search a tree for the global symbol. */
2909
2910gfc_gsymbol *
cb9e4f55 2911gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
c9543002 2912{
1a549788 2913 int c;
c9543002
TS
2914
2915 if (symbol == NULL)
2916 return NULL;
c9543002 2917
1a549788
TS
2918 while (symbol)
2919 {
2920 c = strcmp (name, symbol->name);
2921 if (!c)
2922 return symbol;
c9543002 2923
1a549788
TS
2924 symbol = (c < 0) ? symbol->left : symbol->right;
2925 }
c9543002
TS
2926
2927 return NULL;
2928}
2929
2930
2931/* Compare two global symbols. Used for managing the BB tree. */
2932
2933static int
66e4ab31 2934gsym_compare (void *_s1, void *_s2)
c9543002
TS
2935{
2936 gfc_gsymbol *s1, *s2;
2937
66e4ab31
SK
2938 s1 = (gfc_gsymbol *) _s1;
2939 s2 = (gfc_gsymbol *) _s2;
2940 return strcmp (s1->name, s2->name);
c9543002
TS
2941}
2942
2943
2944/* Get a global symbol, creating it if it doesn't exist. */
2945
2946gfc_gsymbol *
cb9e4f55 2947gfc_get_gsymbol (const char *name)
c9543002
TS
2948{
2949 gfc_gsymbol *s;
2950
2951 s = gfc_find_gsymbol (gfc_gsym_root, name);
2952 if (s != NULL)
2953 return s;
2954
2955 s = gfc_getmem (sizeof (gfc_gsymbol));
2956 s->type = GSYM_UNKNOWN;
973a384d 2957 s->name = gfc_get_string (name);
c9543002
TS
2958
2959 gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2960
2961 return s;
2962}
a8b3b0b6
CR
2963
2964
2965static gfc_symbol *
2966get_iso_c_binding_dt (int sym_id)
2967{
2968 gfc_dt_list *dt_list;
2969
2970 dt_list = gfc_derived_types;
2971
2972 /* Loop through the derived types in the name list, searching for
2973 the desired symbol from iso_c_binding. Search the parent namespaces
2974 if necessary and requested to (parent_flag). */
2975 while (dt_list != NULL)
2976 {
2977 if (dt_list->derived->from_intmod != INTMOD_NONE
2978 && dt_list->derived->intmod_sym_id == sym_id)
2979 return dt_list->derived;
2980
2981 dt_list = dt_list->next;
2982 }
2983
2984 return NULL;
2985}
2986
2987
2988/* Verifies that the given derived type symbol, derived_sym, is interoperable
2989 with C. This is necessary for any derived type that is BIND(C) and for
2990 derived types that are parameters to functions that are BIND(C). All
2991 fields of the derived type are required to be interoperable, and are tested
2992 for such. If an error occurs, the errors are reported here, allowing for
2993 multiple errors to be handled for a single derived type. */
2994
2995try
2996verify_bind_c_derived_type (gfc_symbol *derived_sym)
2997{
2998 gfc_component *curr_comp = NULL;
2999 try is_c_interop = FAILURE;
3000 try retval = SUCCESS;
3001
3002 if (derived_sym == NULL)
3003 gfc_internal_error ("verify_bind_c_derived_type(): Given symbol is "
3004 "unexpectedly NULL");
3005
3006 /* If we've already looked at this derived symbol, do not look at it again
3007 so we don't repeat warnings/errors. */
3008 if (derived_sym->ts.is_c_interop)
3009 return SUCCESS;
3010
3011 /* The derived type must have the BIND attribute to be interoperable
3012 J3/04-007, Section 15.2.3. */
3013 if (derived_sym->attr.is_bind_c != 1)
3014 {
3015 derived_sym->ts.is_c_interop = 0;
3016 gfc_error_now ("Derived type '%s' declared at %L must have the BIND "
3017 "attribute to be C interoperable", derived_sym->name,
3018 &(derived_sym->declared_at));
3019 retval = FAILURE;
3020 }
3021
3022 curr_comp = derived_sym->components;
3023
3024 /* TODO: is this really an error? */
3025 if (curr_comp == NULL)
3026 {
3027 gfc_error ("Derived type '%s' at %L is empty",
3028 derived_sym->name, &(derived_sym->declared_at));
3029 return FAILURE;
3030 }
3031
3032 /* Initialize the derived type as being C interoperable.
3033 If we find an error in the components, this will be set false. */
3034 derived_sym->ts.is_c_interop = 1;
3035
3036 /* Loop through the list of components to verify that the kind of
3037 each is a C interoperable type. */
3038 do
3039 {
3040 /* The components cannot be pointers (fortran sense).
3041 J3/04-007, Section 15.2.3, C1505. */
3042 if (curr_comp->pointer != 0)
3043 {
3044 gfc_error ("Component '%s' at %L cannot have the "
3045 "POINTER attribute because it is a member "
3046 "of the BIND(C) derived type '%s' at %L",
3047 curr_comp->name, &(curr_comp->loc),
3048 derived_sym->name, &(derived_sym->declared_at));
3049 retval = FAILURE;
3050 }
3051
3052 /* The components cannot be allocatable.
3053 J3/04-007, Section 15.2.3, C1505. */
3054 if (curr_comp->allocatable != 0)
3055 {
3056 gfc_error ("Component '%s' at %L cannot have the "
3057 "ALLOCATABLE attribute because it is a member "
3058 "of the BIND(C) derived type '%s' at %L",
3059 curr_comp->name, &(curr_comp->loc),
3060 derived_sym->name, &(derived_sym->declared_at));
3061 retval = FAILURE;
3062 }
3063
3064 /* BIND(C) derived types must have interoperable components. */
3065 if (curr_comp->ts.type == BT_DERIVED
3066 && curr_comp->ts.derived->ts.is_iso_c != 1
3067 && curr_comp->ts.derived != derived_sym)
3068 {
3069 /* This should be allowed; the draft says a derived-type can not
3070 have type parameters if it is has the BIND attribute. Type
3071 parameters seem to be for making parameterized derived types.
3072 There's no need to verify the type if it is c_ptr/c_funptr. */
3073 retval = verify_bind_c_derived_type (curr_comp->ts.derived);
3074 }
3075 else
3076 {
3077 /* Grab the typespec for the given component and test the kind. */
3078 is_c_interop = verify_c_interop (&(curr_comp->ts), curr_comp->name,
3079 &(curr_comp->loc));
3080
3081 if (is_c_interop != SUCCESS)
3082 {
3083 /* Report warning and continue since not fatal. The
3084 draft does specify a constraint that requires all fields
3085 to interoperate, but if the user says real(4), etc., it
3086 may interoperate with *something* in C, but the compiler
3087 most likely won't know exactly what. Further, it may not
3088 interoperate with the same data type(s) in C if the user
3089 recompiles with different flags (e.g., -m32 and -m64 on
3090 x86_64 and using integer(4) to claim interop with a
3091 C_LONG). */
3092 if (derived_sym->attr.is_bind_c == 1)
3093 /* If the derived type is bind(c), all fields must be
3094 interop. */
3095 gfc_warning ("Component '%s' in derived type '%s' at %L "
3096 "may not be C interoperable, even though "
3097 "derived type '%s' is BIND(C)",
3098 curr_comp->name, derived_sym->name,
3099 &(curr_comp->loc), derived_sym->name);
3100 else
3101 /* If derived type is param to bind(c) routine, or to one
3102 of the iso_c_binding procs, it must be interoperable, so
3103 all fields must interop too. */
3104 gfc_warning ("Component '%s' in derived type '%s' at %L "
3105 "may not be C interoperable",
3106 curr_comp->name, derived_sym->name,
3107 &(curr_comp->loc));
3108 }
3109 }
3110
3111 curr_comp = curr_comp->next;
3112 } while (curr_comp != NULL);
3113
3114
3115 /* Make sure we don't have conflicts with the attributes. */
3116 if (derived_sym->attr.access == ACCESS_PRIVATE)
3117 {
3118 gfc_error ("Derived type '%s' at %L cannot be declared with both "
3119 "PRIVATE and BIND(C) attributes", derived_sym->name,
3120 &(derived_sym->declared_at));
3121 retval = FAILURE;
3122 }
3123
3124 if (derived_sym->attr.sequence != 0)
3125 {
3126 gfc_error ("Derived type '%s' at %L cannot have the SEQUENCE "
3127 "attribute because it is BIND(C)", derived_sym->name,
3128 &(derived_sym->declared_at));
3129 retval = FAILURE;
3130 }
3131
3132 /* Mark the derived type as not being C interoperable if we found an
3133 error. If there were only warnings, proceed with the assumption
3134 it's interoperable. */
3135 if (retval == FAILURE)
3136 derived_sym->ts.is_c_interop = 0;
3137
3138 return retval;
3139}
3140
3141
3142/* Generate symbols for the named constants c_null_ptr and c_null_funptr. */
3143
3144static try
3145gen_special_c_interop_ptr (int ptr_id, const char *ptr_name,
3146 const char *module_name)
3147{
3148 gfc_symtree *tmp_symtree;
3149 gfc_symbol *tmp_sym;
3150
3151 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, ptr_name);
3152
3153 if (tmp_symtree != NULL)
3154 tmp_sym = tmp_symtree->n.sym;
3155 else
3156 {
3157 tmp_sym = NULL;
3158 gfc_internal_error ("gen_special_c_interop_ptr(): Unable to "
3159 "create symbol for %s", ptr_name);
3160 }
3161
3162 /* Set up the symbol's important fields. Save attr required so we can
3163 initialize the ptr to NULL. */
3164 tmp_sym->attr.save = 1;
3165 tmp_sym->ts.is_c_interop = 1;
3166 tmp_sym->attr.is_c_interop = 1;
3167 tmp_sym->ts.is_iso_c = 1;
3168 tmp_sym->ts.type = BT_DERIVED;
3169
3170 /* The c_ptr and c_funptr derived types will provide the
3171 definition for c_null_ptr and c_null_funptr, respectively. */
3172 if (ptr_id == ISOCBINDING_NULL_PTR)
3173 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_PTR);
3174 else
3175 tmp_sym->ts.derived = get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3176 if (tmp_sym->ts.derived == NULL)
3177 {
3178 /* This can occur if the user forgot to declare c_ptr or
3179 c_funptr and they're trying to use one of the procedures
3180 that has arg(s) of the missing type. In this case, a
3181 regular version of the thing should have been put in the
3182 current ns. */
3183 generate_isocbinding_symbol (module_name, ptr_id == ISOCBINDING_NULL_PTR
3184 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR,
3185 (char *) (ptr_id == ISOCBINDING_NULL_PTR
3186 ? "_gfortran_iso_c_binding_c_ptr"
3187 : "_gfortran_iso_c_binding_c_funptr"));
3188
3189 tmp_sym->ts.derived =
3190 get_iso_c_binding_dt (ptr_id == ISOCBINDING_NULL_PTR
3191 ? ISOCBINDING_PTR : ISOCBINDING_FUNPTR);
3192 }
3193
3194 /* Module name is some mangled version of iso_c_binding. */
3195 tmp_sym->module = gfc_get_string (module_name);
3196
3197 /* Say it's from the iso_c_binding module. */
3198 tmp_sym->attr.is_iso_c = 1;
3199
3200 tmp_sym->attr.use_assoc = 1;
3201 tmp_sym->attr.is_bind_c = 1;
3202 /* Set the binding_label. */
3203 sprintf (tmp_sym->binding_label, "%s_%s", module_name, tmp_sym->name);
3204
3205 /* Set the c_address field of c_null_ptr and c_null_funptr to
3206 the value of NULL. */
3207 tmp_sym->value = gfc_get_expr ();
3208 tmp_sym->value->expr_type = EXPR_STRUCTURE;
3209 tmp_sym->value->ts.type = BT_DERIVED;
3210 tmp_sym->value->ts.derived = tmp_sym->ts.derived;
3211 tmp_sym->value->value.constructor = gfc_get_constructor ();
3212 /* This line will initialize the c_null_ptr/c_null_funptr
3213 c_address field to NULL. */
3214 tmp_sym->value->value.constructor->expr = gfc_int_expr (0);
3215 /* Must declare c_null_ptr and c_null_funptr as having the
3216 PARAMETER attribute so they can be used in init expressions. */
3217 tmp_sym->attr.flavor = FL_PARAMETER;
3218
3219 return SUCCESS;
3220}
3221
3222
3223/* Add a formal argument, gfc_formal_arglist, to the
3224 end of the given list of arguments. Set the reference to the
3225 provided symbol, param_sym, in the argument. */
3226
3227static void
3228add_formal_arg (gfc_formal_arglist **head,
3229 gfc_formal_arglist **tail,
3230 gfc_formal_arglist *formal_arg,
3231 gfc_symbol *param_sym)
3232{
3233 /* Put in list, either as first arg or at the tail (curr arg). */
3234 if (*head == NULL)
3235 *head = *tail = formal_arg;
3236 else
3237 {
3238 (*tail)->next = formal_arg;
3239 (*tail) = formal_arg;
3240 }
3241
3242 (*tail)->sym = param_sym;
3243 (*tail)->next = NULL;
3244
3245 return;
3246}
3247
3248
3249/* Generates a symbol representing the CPTR argument to an
3250 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3251 CPTR and add it to the provided argument list. */
3252
3253static void
3254gen_cptr_param (gfc_formal_arglist **head,
3255 gfc_formal_arglist **tail,
3256 const char *module_name,
3257 gfc_namespace *ns, const char *c_ptr_name)
3258{
3259 gfc_symbol *param_sym = NULL;
3260 gfc_symbol *c_ptr_sym = NULL;
3261 gfc_symtree *param_symtree = NULL;
3262 gfc_formal_arglist *formal_arg = NULL;
3263 const char *c_ptr_in;
3264 const char *c_ptr_type = "c_ptr";
3265
3266 if(c_ptr_name == NULL)
3267 c_ptr_in = "gfc_cptr__";
3268 else
3269 c_ptr_in = c_ptr_name;
3270 gfc_get_sym_tree (c_ptr_in, ns, &param_symtree);
3271 if (param_symtree != NULL)
3272 param_sym = param_symtree->n.sym;
3273 else
3274 gfc_internal_error ("gen_cptr_param(): Unable to "
3275 "create symbol for %s", c_ptr_in);
3276
3277 /* Set up the appropriate fields for the new c_ptr param sym. */
3278 param_sym->refs++;
3279 param_sym->attr.flavor = FL_DERIVED;
3280 param_sym->ts.type = BT_DERIVED;
3281 param_sym->attr.intent = INTENT_IN;
3282 param_sym->attr.dummy = 1;
3283
3284 /* This will pass the ptr to the iso_c routines as a (void *). */
3285 param_sym->attr.value = 1;
3286 param_sym->attr.use_assoc = 1;
3287
3288 /* Get the symbol for c_ptr, no matter what it's name is (user renamed). */
3289 c_ptr_sym = get_iso_c_binding_dt (ISOCBINDING_PTR);
3290 if (c_ptr_sym == NULL)
3291 {
3292 /* This can happen if the user did not define c_ptr but they are
3293 trying to use one of the iso_c_binding functions that need it. */
3294 gfc_error_now ("Type 'C_PTR' required for ISO_C_BINDING function at %C");
3295 generate_isocbinding_symbol (module_name, ISOCBINDING_PTR,
3296 (char *) "_gfortran_iso_c_binding_c_ptr");
3297
3298 gfc_get_ha_symbol (c_ptr_type, &(c_ptr_sym));
3299 }
3300
3301 param_sym->ts.derived = c_ptr_sym;
3302 param_sym->module = gfc_get_string (module_name);
3303
3304 /* Make new formal arg. */
3305 formal_arg = gfc_get_formal_arglist ();
3306 /* Add arg to list of formal args (the CPTR arg). */
3307 add_formal_arg (head, tail, formal_arg, param_sym);
3308}
3309
3310
3311/* Generates a symbol representing the FPTR argument to an
3312 iso_c_binding procedure. Also, create a gfc_formal_arglist for the
3313 FPTR and add it to the provided argument list. */
3314
3315static void
3316gen_fptr_param (gfc_formal_arglist **head,
3317 gfc_formal_arglist **tail,
3318 const char *module_name,
3319 gfc_namespace *ns, const char *f_ptr_name)
3320{
3321 gfc_symbol *param_sym = NULL;
3322 gfc_symtree *param_symtree = NULL;
3323 gfc_formal_arglist *formal_arg = NULL;
3324 const char *f_ptr_out = "gfc_fptr__";
3325
3326 if (f_ptr_name != NULL)
3327 f_ptr_out = f_ptr_name;
3328
3329 gfc_get_sym_tree (f_ptr_out, ns, &param_symtree);
3330 if (param_symtree != NULL)
3331 param_sym = param_symtree->n.sym;
3332 else
3333 gfc_internal_error ("generateFPtrParam(): Unable to "
3334 "create symbol for %s", f_ptr_out);
3335
3336 /* Set up the necessary fields for the fptr output param sym. */
3337 param_sym->refs++;
3338 param_sym->attr.pointer = 1;
3339 param_sym->attr.dummy = 1;
3340 param_sym->attr.use_assoc = 1;
3341
3342 /* ISO C Binding type to allow any pointer type as actual param. */
3343 param_sym->ts.type = BT_VOID;
3344 param_sym->module = gfc_get_string (module_name);
3345
3346 /* Make the arg. */
3347 formal_arg = gfc_get_formal_arglist ();
3348 /* Add arg to list of formal args. */
3349 add_formal_arg (head, tail, formal_arg, param_sym);
3350}
3351
3352
3353/* Generates a symbol representing the optional SHAPE argument for the
3354 iso_c_binding c_f_pointer() procedure. Also, create a
3355 gfc_formal_arglist for the SHAPE and add it to the provided
3356 argument list. */
3357
3358static void
3359gen_shape_param (gfc_formal_arglist **head,
3360 gfc_formal_arglist **tail,
3361 const char *module_name,
3362 gfc_namespace *ns, const char *shape_param_name)
3363{
3364 gfc_symbol *param_sym = NULL;
3365 gfc_symtree *param_symtree = NULL;
3366 gfc_formal_arglist *formal_arg = NULL;
3367 const char *shape_param = "gfc_shape_array__";
3368 int i;
3369
3370 if (shape_param_name != NULL)
3371 shape_param = shape_param_name;
3372
3373 gfc_get_sym_tree (shape_param, ns, &param_symtree);
3374 if (param_symtree != NULL)
3375 param_sym = param_symtree->n.sym;
3376 else
3377 gfc_internal_error ("generateShapeParam(): Unable to "
3378 "create symbol for %s", shape_param);
3379
3380 /* Set up the necessary fields for the shape input param sym. */
3381 param_sym->refs++;
3382 param_sym->attr.dummy = 1;
3383 param_sym->attr.use_assoc = 1;
3384
3385 /* Integer array, rank 1, describing the shape of the object. */
3386 param_sym->ts.type = BT_INTEGER;
3387 param_sym->ts.kind = gfc_default_integer_kind;
3388 param_sym->as = gfc_get_array_spec ();
3389
3390 /* Clear out the dimension info for the array. */
3391 for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
3392 {
3393 param_sym->as->lower[i] = NULL;
3394 param_sym->as->upper[i] = NULL;
3395 }
3396 param_sym->as->rank = 1;
3397 param_sym->as->lower[0] = gfc_int_expr (1);
3398
3399 /* The extent is unknown until we get it. The length give us
3400 the rank the incoming pointer. */
3401 param_sym->as->type = AS_ASSUMED_SHAPE;
3402
3403 /* The arg is also optional; it is required iff the second arg
3404 (fptr) is to an array, otherwise, it's ignored. */
3405 param_sym->attr.optional = 1;
3406 param_sym->attr.intent = INTENT_IN;
3407 param_sym->attr.dimension = 1;
3408 param_sym->module = gfc_get_string (module_name);
3409
3410 /* Make the arg. */
3411 formal_arg = gfc_get_formal_arglist ();
3412 /* Add arg to list of formal args. */
3413 add_formal_arg (head, tail, formal_arg, param_sym);
3414}
3415
3416/* Add a procedure interface to the given symbol (i.e., store a
3417 reference to the list of formal arguments). */
3418
3419static void
3420add_proc_interface (gfc_symbol *sym, ifsrc source,
3421 gfc_formal_arglist *formal)
3422{
3423
3424 sym->formal = formal;
3425 sym->attr.if_source = source;
3426}
3427
3428
3429/* Builds the parameter list for the iso_c_binding procedure
3430 c_f_pointer or c_f_procpointer. The old_sym typically refers to a
3431 generic version of either the c_f_pointer or c_f_procpointer
3432 functions. The new_proc_sym represents a "resolved" version of the
3433 symbol. The functions are resolved to match the types of their
3434 parameters; for example, c_f_pointer(cptr, fptr) would resolve to
3435 something similar to c_f_pointer_i4 if the type of data object fptr
3436 pointed to was a default integer. The actual name of the resolved
3437 procedure symbol is further mangled with the module name, etc., but
3438 the idea holds true. */
3439
3440static void
3441build_formal_args (gfc_symbol *new_proc_sym,
3442 gfc_symbol *old_sym, int add_optional_arg)
3443{
3444 gfc_formal_arglist *head = NULL, *tail = NULL;
3445 gfc_namespace *parent_ns = NULL;
3446
3447 parent_ns = gfc_current_ns;
3448 /* Create a new namespace, which will be the formal ns (namespace
3449 of the formal args). */
3450 gfc_current_ns = gfc_get_namespace(parent_ns, 0);
3451 gfc_current_ns->proc_name = new_proc_sym;
3452
3453 /* Generate the params. */
3454 if ((old_sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
3455 (old_sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
3456 {
3457 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3458 gfc_current_ns, "cptr");
3459 gen_fptr_param (&head, &tail, (const char *) new_proc_sym->module,
3460 gfc_current_ns, "fptr");
3461
3462 /* If we're dealing with c_f_pointer, it has an optional third arg. */
3463 if (old_sym->intmod_sym_id == ISOCBINDING_F_POINTER)
3464 {
3465 gen_shape_param (&head, &tail,
3466 (const char *) new_proc_sym->module,
3467 gfc_current_ns, "shape");
3468 }
3469 }
3470 else if (old_sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
3471 {
3472 /* c_associated has one required arg and one optional; both
3473 are c_ptrs. */
3474 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3475 gfc_current_ns, "c_ptr_1");
3476 if (add_optional_arg)
3477 {
3478 gen_cptr_param (&head, &tail, (const char *) new_proc_sym->module,
3479 gfc_current_ns, "c_ptr_2");
3480 /* The last param is optional so mark it as such. */
3481 tail->sym->attr.optional = 1;
3482 }
3483 }
3484
3485 /* Add the interface (store formal args to new_proc_sym). */
3486 add_proc_interface (new_proc_sym, IFSRC_DECL, head);
3487
3488 /* Set up the formal_ns pointer to the one created for the
3489 new procedure so it'll get cleaned up during gfc_free_symbol(). */
3490 new_proc_sym->formal_ns = gfc_current_ns;
3491
3492 gfc_current_ns = parent_ns;
3493}
3494
3495
3496/* Generate the given set of C interoperable kind objects, or all
3497 interoperable kinds. This function will only be given kind objects
3498 for valid iso_c_binding defined types because this is verified when
3499 the 'use' statement is parsed. If the user gives an 'only' clause,
3500 the specific kinds are looked up; if they don't exist, an error is
3501 reported. If the user does not give an 'only' clause, all
3502 iso_c_binding symbols are generated. If a list of specific kinds
3503 is given, it must have a NULL in the first empty spot to mark the
3504 end of the list. */
3505
3506
3507void
3508generate_isocbinding_symbol (const char *mod_name, iso_c_binding_symbol s,
3509 char *local_name)
3510{
3511 char *name = (local_name && local_name[0]) ? local_name
3512 : c_interop_kinds_table[s].name;
3513 gfc_symtree *tmp_symtree = NULL;
3514 gfc_symbol *tmp_sym = NULL;
3515 gfc_dt_list **dt_list_ptr = NULL;
3516 gfc_component *tmp_comp = NULL;
3517 char comp_name[(GFC_MAX_SYMBOL_LEN * 2) + 1];
3518 int index;
3519
3520 tmp_symtree = gfc_find_symtree (gfc_current_ns->sym_root, name);
3521
3522 /* Already exists in this scope so don't re-add it.
3523 TODO: we should probably check that it's really the same symbol. */
3524 if (tmp_symtree != NULL)
3525 return;
3526
3527 /* Create the sym tree in the current ns. */
3528 gfc_get_sym_tree (name, gfc_current_ns, &tmp_symtree);
3529 if (tmp_symtree)
3530 tmp_sym = tmp_symtree->n.sym;
3531 else
3532 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3533 "create symbol");
3534
3535 /* Say what module this symbol belongs to. */
3536 tmp_sym->module = gfc_get_string (mod_name);
3537 tmp_sym->from_intmod = INTMOD_ISO_C_BINDING;
3538 tmp_sym->intmod_sym_id = s;
3539
3540 switch (s)
3541 {
3542
3543#define NAMED_INTCST(a,b,c) case a :
3544#define NAMED_REALCST(a,b,c) case a :
3545#define NAMED_CMPXCST(a,b,c) case a :
3546#define NAMED_LOGCST(a,b,c) case a :
3547#define NAMED_CHARKNDCST(a,b,c) case a :
3548#include "iso-c-binding.def"
3549
3550 tmp_sym->value = gfc_int_expr (c_interop_kinds_table[s].value);
3551
3552 /* Initialize an integer constant expression node. */
3553 tmp_sym->attr.flavor = FL_PARAMETER;
3554 tmp_sym->ts.type = BT_INTEGER;
3555 tmp_sym->ts.kind = gfc_default_integer_kind;
3556
3557 /* Mark this type as a C interoperable one. */
3558 tmp_sym->ts.is_c_interop = 1;
3559 tmp_sym->ts.is_iso_c = 1;
3560 tmp_sym->value->ts.is_c_interop = 1;
3561 tmp_sym->value->ts.is_iso_c = 1;
3562 tmp_sym->attr.is_c_interop = 1;
3563
3564 /* Tell what f90 type this c interop kind is valid. */
3565 tmp_sym->ts.f90_type = c_interop_kinds_table[s].f90_type;
3566
3567 /* Say it's from the iso_c_binding module. */
3568 tmp_sym->attr.is_iso_c = 1;
3569
3570 /* Make it use associated. */
3571 tmp_sym->attr.use_assoc = 1;
3572 break;
3573
3574
3575#define NAMED_CHARCST(a,b,c) case a :
3576#include "iso-c-binding.def"
3577
3578 /* Initialize an integer constant expression node for the
3579 length of the character. */
3580 tmp_sym->value = gfc_get_expr ();
3581 tmp_sym->value->expr_type = EXPR_CONSTANT;
3582 tmp_sym->value->ts.type = BT_CHARACTER;
3583 tmp_sym->value->ts.kind = gfc_default_character_kind;
3584 tmp_sym->value->where = gfc_current_locus;
3585 tmp_sym->value->ts.is_c_interop = 1;
3586 tmp_sym->value->ts.is_iso_c = 1;
3587 tmp_sym->value->value.character.length = 1;
3588 tmp_sym->value->value.character.string = gfc_getmem (2);
3589 tmp_sym->value->value.character.string[0]
3590 = (char) c_interop_kinds_table[s].value;
3591 tmp_sym->value->value.character.string[1] = '\0';
3592
3593 /* May not need this in both attr and ts, but do need in
3594 attr for writing module file. */
3595 tmp_sym->attr.is_c_interop = 1;
3596
3597 tmp_sym->attr.flavor = FL_PARAMETER;
3598 tmp_sym->ts.type = BT_CHARACTER;
3599
3600 /* Need to set it to the C_CHAR kind. */
3601 tmp_sym->ts.kind = gfc_default_character_kind;
3602
3603 /* Mark this type as a C interoperable one. */
3604 tmp_sym->ts.is_c_interop = 1;
3605 tmp_sym->ts.is_iso_c = 1;
3606
3607 /* Tell what f90 type this c interop kind is valid. */
3608 tmp_sym->ts.f90_type = BT_CHARACTER;
3609
3610 /* Say it's from the iso_c_binding module. */
3611 tmp_sym->attr.is_iso_c = 1;
3612
3613 /* Make it use associated. */
3614 tmp_sym->attr.use_assoc = 1;
3615 break;
3616
3617 case ISOCBINDING_PTR:
3618 case ISOCBINDING_FUNPTR:
3619
3620 /* Initialize an integer constant expression node. */
3621 tmp_sym->attr.flavor = FL_DERIVED;
3622 tmp_sym->ts.is_c_interop = 1;
3623 tmp_sym->attr.is_c_interop = 1;
3624 tmp_sym->attr.is_iso_c = 1;
3625 tmp_sym->ts.is_iso_c = 1;
3626 tmp_sym->ts.type = BT_DERIVED;
3627
3628 /* A derived type must have the bind attribute to be
3629 interoperable (J3/04-007, Section 15.2.3), even though
3630 the binding label is not used. */
3631 tmp_sym->attr.is_bind_c = 1;
3632
3633 tmp_sym->attr.referenced = 1;
3634
3635 tmp_sym->ts.derived = tmp_sym;
3636
3637 /* Add the symbol created for the derived type to the current ns. */
3638 dt_list_ptr = &(gfc_derived_types);
3639 while (*dt_list_ptr != NULL && (*dt_list_ptr)->next != NULL)
3640 dt_list_ptr = &((*dt_list_ptr)->next);
3641
3642 /* There is already at least one derived type in the list, so append
3643 the one we're currently building for c_ptr or c_funptr. */
3644 if (*dt_list_ptr != NULL)
3645 dt_list_ptr = &((*dt_list_ptr)->next);
3646 (*dt_list_ptr) = gfc_get_dt_list ();
3647 (*dt_list_ptr)->derived = tmp_sym;
3648 (*dt_list_ptr)->next = NULL;
3649
3650 /* Set up the component of the derived type, which will be
3651 an integer with kind equal to c_ptr_size. Mangle the name of
3652 the field for the c_address to prevent the curious user from
3653 trying to access it from Fortran. */
3654 sprintf (comp_name, "__%s_%s", tmp_sym->name, "c_address");
3655 gfc_add_component (tmp_sym, comp_name, &tmp_comp);
3656 if (tmp_comp == NULL)
3657 gfc_internal_error ("generate_isocbinding_symbol(): Unable to "
3658 "create component for c_address");
3659
3660 tmp_comp->ts.type = BT_INTEGER;
3661
3662 /* Set this because the module will need to read/write this field. */
3663 tmp_comp->ts.f90_type = BT_INTEGER;
3664
3665 /* The kinds for c_ptr and c_funptr are the same. */
3666 index = get_c_kind ("c_ptr", c_interop_kinds_table);
3667 tmp_comp->ts.kind = c_interop_kinds_table[index].value;
3668
3669 tmp_comp->pointer = 0;
3670 tmp_comp->dimension = 0;
3671
3672 /* Mark the component as C interoperable. */
3673 tmp_comp->ts.is_c_interop = 1;
3674
3675 /* Make it use associated (iso_c_binding module). */
3676 tmp_sym->attr.use_assoc = 1;
3677 break;
3678
3679 case ISOCBINDING_NULL_PTR:
3680 case ISOCBINDING_NULL_FUNPTR:
3681 gen_special_c_interop_ptr (s, name, mod_name);
3682 break;
3683
3684 case ISOCBINDING_F_POINTER:
3685 case ISOCBINDING_ASSOCIATED:
3686 case ISOCBINDING_LOC:
3687 case ISOCBINDING_FUNLOC:
3688 case ISOCBINDING_F_PROCPOINTER:
3689
3690 tmp_sym->attr.proc = PROC_MODULE;
3691
3692 /* Use the procedure's name as it is in the iso_c_binding module for
3693 setting the binding label in case the user renamed the symbol. */
3694 sprintf (tmp_sym->binding_label, "%s_%s", mod_name,
3695 c_interop_kinds_table[s].name);
3696 tmp_sym->attr.is_iso_c = 1;
3697 if (s == ISOCBINDING_F_POINTER || s == ISOCBINDING_F_PROCPOINTER)
3698 tmp_sym->attr.subroutine = 1;
3699 else
3700 {
3701 /* TODO! This needs to be finished more for the expr of the
3702 function or something!
3703 This may not need to be here, because trying to do c_loc
3704 as an external. */
3705 if (s == ISOCBINDING_ASSOCIATED)
3706 {
3707 tmp_sym->attr.function = 1;
3708 tmp_sym->ts.type = BT_LOGICAL;
3709 tmp_sym->ts.kind = gfc_default_logical_kind;
3710 tmp_sym->result = tmp_sym;
3711 }
3712 else
3713 {
3714 /* Here, we're taking the simple approach. We're defining
3715 c_loc as an external identifier so the compiler will put
3716 what we expect on the stack for the address we want the
3717 C address of. */
3718 tmp_sym->ts.type = BT_DERIVED;
3719 if (s == ISOCBINDING_LOC)
3720 tmp_sym->ts.derived =
3721 get_iso_c_binding_dt (ISOCBINDING_PTR);
3722 else
3723 tmp_sym->ts.derived =
3724 get_iso_c_binding_dt (ISOCBINDING_FUNPTR);
3725
3726 if (tmp_sym->ts.derived == NULL)
3727 {
3728 /* Create the necessary derived type so we can continue
3729 processing the file. */
3730 generate_isocbinding_symbol
3731 (mod_name, s == ISOCBINDING_FUNLOC
3732 ? ISOCBINDING_FUNPTR : ISOCBINDING_FUNPTR,
3733 (char *)(s == ISOCBINDING_FUNLOC
3734 ? "_gfortran_iso_c_binding_c_funptr"
3735 : "_gfortran_iso_c_binding_c_ptr"));
3736 tmp_sym->ts.derived =
3737 get_iso_c_binding_dt (s == ISOCBINDING_FUNLOC
3738 ? ISOCBINDING_FUNPTR
3739 : ISOCBINDING_PTR);
3740 }
3741
3742 /* The function result is itself (no result clause). */
3743 tmp_sym->result = tmp_sym;
3744 tmp_sym->attr.external = 1;
3745 tmp_sym->attr.use_assoc = 0;
3746 tmp_sym->attr.if_source = IFSRC_UNKNOWN;
3747 tmp_sym->attr.proc = PROC_UNKNOWN;
3748 }
3749 }
3750
3751 tmp_sym->attr.flavor = FL_PROCEDURE;
3752 tmp_sym->attr.contained = 0;
3753
3754 /* Try using this builder routine, with the new and old symbols
3755 both being the generic iso_c proc sym being created. This
3756 will create the formal args (and the new namespace for them).
3757 Don't build an arg list for c_loc because we're going to treat
3758 c_loc as an external procedure. */
3759 if (s != ISOCBINDING_LOC && s != ISOCBINDING_FUNLOC)
3760 /* The 1 says to add any optional args, if applicable. */
3761 build_formal_args (tmp_sym, tmp_sym, 1);
3762
3763 /* Set this after setting up the symbol, to prevent error messages. */
3764 tmp_sym->attr.use_assoc = 1;
3765
3766 /* This symbol will not be referenced directly. It will be
3767 resolved to the implementation for the given f90 kind. */
3768 tmp_sym->attr.referenced = 0;
3769
3770 break;
3771
3772 default:
3773 gcc_unreachable ();
3774 }
3775}
3776
3777
3778/* Creates a new symbol based off of an old iso_c symbol, with a new
3779 binding label. This function can be used to create a new,
3780 resolved, version of a procedure symbol for c_f_pointer or
3781 c_f_procpointer that is based on the generic symbols. A new
3782 parameter list is created for the new symbol using
3783 build_formal_args(). The add_optional_flag specifies whether the
3784 to add the optional SHAPE argument. The new symbol is
3785 returned. */
3786
3787gfc_symbol *
3788get_iso_c_sym (gfc_symbol *old_sym, char *new_name,
3789 char *new_binding_label, int add_optional_arg)
3790{
3791 gfc_symtree *new_symtree = NULL;
3792
3793 /* See if we have a symbol by that name already available, looking
3794 through any parent namespaces. */
3795 gfc_find_sym_tree (new_name, gfc_current_ns, 1, &new_symtree);
3796 if (new_symtree != NULL)
3797 /* Return the existing symbol. */
3798 return new_symtree->n.sym;
3799
3800 /* Create the symtree/symbol, with attempted host association. */
3801 gfc_get_ha_sym_tree (new_name, &new_symtree);
3802 if (new_symtree == NULL)
3803 gfc_internal_error ("get_iso_c_sym(): Unable to create "
3804 "symtree for '%s'", new_name);
3805
3806 /* Now fill in the fields of the resolved symbol with the old sym. */
3807 strcpy (new_symtree->n.sym->binding_label, new_binding_label);
3808 new_symtree->n.sym->attr = old_sym->attr;
3809 new_symtree->n.sym->ts = old_sym->ts;
3810 new_symtree->n.sym->module = gfc_get_string (old_sym->module);
3811 /* Build the formal arg list. */
3812 build_formal_args (new_symtree->n.sym, old_sym, add_optional_arg);
3813
3814 gfc_commit_symbol (new_symtree->n.sym);
3815
3816 return new_symtree->n.sym;
3817}
3818
This page took 1.400794 seconds and 5 git commands to generate.