OSDN Git Service

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