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