OSDN Git Service

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