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