OSDN Git Service

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