OSDN Git Service

2010-08-15 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2    Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "flags.h"
28 #include "constructor.h"
29
30 /* Macros to access allocate memory for gfc_data_variable,
31    gfc_data_value and gfc_data.  */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
35
36
37 /* This flag is set if an old-style length selector is matched
38    during a type-declaration statement.  */
39
40 static int old_char_selector;
41
42 /* When variables acquire types and attributes from a declaration
43    statement, they get them from the following static variables.  The
44    first part of a declaration sets these variables and the second
45    part copies these into symbol structures.  */
46
47 static gfc_typespec current_ts;
48
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
52
53 /* The current binding label (if any).  */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56    line in case we're given the BIND(C) attribute with a NAME= specifier.  */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59    can supply a name if the curr_binding_label is nil and NAME= was not.  */
60 static int has_name_equals = 0;
61
62 /* Initializer of the previous enumerator.  */
63
64 static gfc_expr *last_initializer;
65
66 /* History of all the enumerators is maintained, so that
67    kind values of all the enumerators could be updated depending
68    upon the maximum initialized value.  */
69
70 typedef struct enumerator_history
71 {
72   gfc_symbol *sym;
73   gfc_expr *initializer;
74   struct enumerator_history *next;
75 }
76 enumerator_history;
77
78 /* Header of enum history chain.  */
79
80 static enumerator_history *enum_history = NULL;
81
82 /* Pointer of enum history node containing largest initializer.  */
83
84 static enumerator_history *max_enum = NULL;
85
86 /* gfc_new_block points to the symbol of a newly matched block.  */
87
88 gfc_symbol *gfc_new_block;
89
90 bool gfc_matching_function;
91
92
93 /********************* DATA statement subroutines *********************/
94
95 static bool in_match_data = false;
96
97 bool
98 gfc_in_match_data (void)
99 {
100   return in_match_data;
101 }
102
103 static void
104 set_in_match_data (bool set_value)
105 {
106   in_match_data = set_value;
107 }
108
109 /* Free a gfc_data_variable structure and everything beneath it.  */
110
111 static void
112 free_variable (gfc_data_variable *p)
113 {
114   gfc_data_variable *q;
115
116   for (; p; p = q)
117     {
118       q = p->next;
119       gfc_free_expr (p->expr);
120       gfc_free_iterator (&p->iter, 0);
121       free_variable (p->list);
122       gfc_free (p);
123     }
124 }
125
126
127 /* Free a gfc_data_value structure and everything beneath it.  */
128
129 static void
130 free_value (gfc_data_value *p)
131 {
132   gfc_data_value *q;
133
134   for (; p; p = q)
135     {
136       q = p->next;
137       mpz_clear (p->repeat);
138       gfc_free_expr (p->expr);
139       gfc_free (p);
140     }
141 }
142
143
144 /* Free a list of gfc_data structures.  */
145
146 void
147 gfc_free_data (gfc_data *p)
148 {
149   gfc_data *q;
150
151   for (; p; p = q)
152     {
153       q = p->next;
154       free_variable (p->var);
155       free_value (p->value);
156       gfc_free (p);
157     }
158 }
159
160
161 /* Free all data in a namespace.  */
162
163 static void
164 gfc_free_data_all (gfc_namespace *ns)
165 {
166   gfc_data *d;
167
168   for (;ns->data;)
169     {
170       d = ns->data->next;
171       gfc_free (ns->data);
172       ns->data = d;
173     }
174 }
175
176
177 static match var_element (gfc_data_variable *);
178
179 /* Match a list of variables terminated by an iterator and a right
180    parenthesis.  */
181
182 static match
183 var_list (gfc_data_variable *parent)
184 {
185   gfc_data_variable *tail, var;
186   match m;
187
188   m = var_element (&var);
189   if (m == MATCH_ERROR)
190     return MATCH_ERROR;
191   if (m == MATCH_NO)
192     goto syntax;
193
194   tail = gfc_get_data_variable ();
195   *tail = var;
196
197   parent->list = tail;
198
199   for (;;)
200     {
201       if (gfc_match_char (',') != MATCH_YES)
202         goto syntax;
203
204       m = gfc_match_iterator (&parent->iter, 1);
205       if (m == MATCH_YES)
206         break;
207       if (m == MATCH_ERROR)
208         return MATCH_ERROR;
209
210       m = var_element (&var);
211       if (m == MATCH_ERROR)
212         return MATCH_ERROR;
213       if (m == MATCH_NO)
214         goto syntax;
215
216       tail->next = gfc_get_data_variable ();
217       tail = tail->next;
218
219       *tail = var;
220     }
221
222   if (gfc_match_char (')') != MATCH_YES)
223     goto syntax;
224   return MATCH_YES;
225
226 syntax:
227   gfc_syntax_error (ST_DATA);
228   return MATCH_ERROR;
229 }
230
231
232 /* Match a single element in a data variable list, which can be a
233    variable-iterator list.  */
234
235 static match
236 var_element (gfc_data_variable *new_var)
237 {
238   match m;
239   gfc_symbol *sym;
240
241   memset (new_var, 0, sizeof (gfc_data_variable));
242
243   if (gfc_match_char ('(') == MATCH_YES)
244     return var_list (new_var);
245
246   m = gfc_match_variable (&new_var->expr, 0);
247   if (m != MATCH_YES)
248     return m;
249
250   sym = new_var->expr->symtree->n.sym;
251
252   /* Symbol should already have an associated type.  */
253   if (gfc_check_symbol_typed (sym, gfc_current_ns,
254                               false, gfc_current_locus) == FAILURE)
255     return MATCH_ERROR;
256
257   if (!sym->attr.function && gfc_current_ns->parent
258       && gfc_current_ns->parent == sym->ns)
259     {
260       gfc_error ("Host associated variable '%s' may not be in the DATA "
261                  "statement at %C", sym->name);
262       return MATCH_ERROR;
263     }
264
265   if (gfc_current_state () != COMP_BLOCK_DATA
266       && sym->attr.in_common
267       && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
268                          "common block variable '%s' in DATA statement at %C",
269                          sym->name) == FAILURE)
270     return MATCH_ERROR;
271
272   if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
273     return MATCH_ERROR;
274
275   return MATCH_YES;
276 }
277
278
279 /* Match the top-level list of data variables.  */
280
281 static match
282 top_var_list (gfc_data *d)
283 {
284   gfc_data_variable var, *tail, *new_var;
285   match m;
286
287   tail = NULL;
288
289   for (;;)
290     {
291       m = var_element (&var);
292       if (m == MATCH_NO)
293         goto syntax;
294       if (m == MATCH_ERROR)
295         return MATCH_ERROR;
296
297       new_var = gfc_get_data_variable ();
298       *new_var = var;
299
300       if (tail == NULL)
301         d->var = new_var;
302       else
303         tail->next = new_var;
304
305       tail = new_var;
306
307       if (gfc_match_char ('/') == MATCH_YES)
308         break;
309       if (gfc_match_char (',') != MATCH_YES)
310         goto syntax;
311     }
312
313   return MATCH_YES;
314
315 syntax:
316   gfc_syntax_error (ST_DATA);
317   gfc_free_data_all (gfc_current_ns);
318   return MATCH_ERROR;
319 }
320
321
322 static match
323 match_data_constant (gfc_expr **result)
324 {
325   char name[GFC_MAX_SYMBOL_LEN + 1];
326   gfc_symbol *sym;
327   gfc_expr *expr;
328   match m;
329   locus old_loc;
330
331   m = gfc_match_literal_constant (&expr, 1);
332   if (m == MATCH_YES)
333     {
334       *result = expr;
335       return MATCH_YES;
336     }
337
338   if (m == MATCH_ERROR)
339     return MATCH_ERROR;
340
341   m = gfc_match_null (result);
342   if (m != MATCH_NO)
343     return m;
344
345   old_loc = gfc_current_locus;
346
347   /* Should this be a structure component, try to match it
348      before matching a name.  */
349   m = gfc_match_rvalue (result);
350   if (m == MATCH_ERROR)
351     return m;
352
353   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
354     {
355       if (gfc_simplify_expr (*result, 0) == FAILURE)
356         m = MATCH_ERROR;
357       return m;
358     }
359
360   gfc_current_locus = old_loc;
361
362   m = gfc_match_name (name);
363   if (m != MATCH_YES)
364     return m;
365
366   if (gfc_find_symbol (name, NULL, 1, &sym))
367     return MATCH_ERROR;
368
369   if (sym == NULL
370       || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
371     {
372       gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
373                  name);
374       return MATCH_ERROR;
375     }
376   else if (sym->attr.flavor == FL_DERIVED)
377     return gfc_match_structure_constructor (sym, result, false);
378
379   /* Check to see if the value is an initialization array expression.  */
380   if (sym->value->expr_type == EXPR_ARRAY)
381     {
382       gfc_current_locus = old_loc;
383
384       m = gfc_match_init_expr (result);
385       if (m == MATCH_ERROR)
386         return m;
387
388       if (m == MATCH_YES)
389         {
390           if (gfc_simplify_expr (*result, 0) == FAILURE)
391             m = MATCH_ERROR;
392
393           if ((*result)->expr_type == EXPR_CONSTANT)
394             return m;
395           else
396             {
397               gfc_error ("Invalid initializer %s in Data statement at %C", name);
398               return MATCH_ERROR;
399             }
400         }
401     }
402
403   *result = gfc_copy_expr (sym->value);
404   return MATCH_YES;
405 }
406
407
408 /* Match a list of values in a DATA statement.  The leading '/' has
409    already been seen at this point.  */
410
411 static match
412 top_val_list (gfc_data *data)
413 {
414   gfc_data_value *new_val, *tail;
415   gfc_expr *expr;
416   match m;
417
418   tail = NULL;
419
420   for (;;)
421     {
422       m = match_data_constant (&expr);
423       if (m == MATCH_NO)
424         goto syntax;
425       if (m == MATCH_ERROR)
426         return MATCH_ERROR;
427
428       new_val = gfc_get_data_value ();
429       mpz_init (new_val->repeat);
430
431       if (tail == NULL)
432         data->value = new_val;
433       else
434         tail->next = new_val;
435
436       tail = new_val;
437
438       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
439         {
440           tail->expr = expr;
441           mpz_set_ui (tail->repeat, 1);
442         }
443       else
444         {
445           if (expr->ts.type == BT_INTEGER)
446             mpz_set (tail->repeat, expr->value.integer);
447           gfc_free_expr (expr);
448
449           m = match_data_constant (&tail->expr);
450           if (m == MATCH_NO)
451             goto syntax;
452           if (m == MATCH_ERROR)
453             return MATCH_ERROR;
454         }
455
456       if (gfc_match_char ('/') == MATCH_YES)
457         break;
458       if (gfc_match_char (',') == MATCH_NO)
459         goto syntax;
460     }
461
462   return MATCH_YES;
463
464 syntax:
465   gfc_syntax_error (ST_DATA);
466   gfc_free_data_all (gfc_current_ns);
467   return MATCH_ERROR;
468 }
469
470
471 /* Matches an old style initialization.  */
472
473 static match
474 match_old_style_init (const char *name)
475 {
476   match m;
477   gfc_symtree *st;
478   gfc_symbol *sym;
479   gfc_data *newdata;
480
481   /* Set up data structure to hold initializers.  */
482   gfc_find_sym_tree (name, NULL, 0, &st);
483   sym = st->n.sym;
484
485   newdata = gfc_get_data ();
486   newdata->var = gfc_get_data_variable ();
487   newdata->var->expr = gfc_get_variable_expr (st);
488   newdata->where = gfc_current_locus;
489
490   /* Match initial value list. This also eats the terminal '/'.  */
491   m = top_val_list (newdata);
492   if (m != MATCH_YES)
493     {
494       gfc_free (newdata);
495       return m;
496     }
497
498   if (gfc_pure (NULL))
499     {
500       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
501       gfc_free (newdata);
502       return MATCH_ERROR;
503     }
504
505   /* Mark the variable as having appeared in a data statement.  */
506   if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
507     {
508       gfc_free (newdata);
509       return MATCH_ERROR;
510     }
511
512   /* Chain in namespace list of DATA initializers.  */
513   newdata->next = gfc_current_ns->data;
514   gfc_current_ns->data = newdata;
515
516   return m;
517 }
518
519
520 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
521    we are matching a DATA statement and are therefore issuing an error
522    if we encounter something unexpected, if not, we're trying to match
523    an old-style initialization expression of the form INTEGER I /2/.  */
524
525 match
526 gfc_match_data (void)
527 {
528   gfc_data *new_data;
529   match m;
530
531   set_in_match_data (true);
532
533   for (;;)
534     {
535       new_data = gfc_get_data ();
536       new_data->where = gfc_current_locus;
537
538       m = top_var_list (new_data);
539       if (m != MATCH_YES)
540         goto cleanup;
541
542       m = top_val_list (new_data);
543       if (m != MATCH_YES)
544         goto cleanup;
545
546       new_data->next = gfc_current_ns->data;
547       gfc_current_ns->data = new_data;
548
549       if (gfc_match_eos () == MATCH_YES)
550         break;
551
552       gfc_match_char (',');     /* Optional comma */
553     }
554
555   set_in_match_data (false);
556
557   if (gfc_pure (NULL))
558     {
559       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
560       return MATCH_ERROR;
561     }
562
563   return MATCH_YES;
564
565 cleanup:
566   set_in_match_data (false);
567   gfc_free_data (new_data);
568   return MATCH_ERROR;
569 }
570
571
572 /************************ Declaration statements *********************/
573
574
575 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs.  */
576
577 static void
578 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
579 {
580   int i;
581
582   if (to->rank == 0 && from->rank > 0)
583     {
584       to->rank = from->rank;
585       to->type = from->type;
586       to->cray_pointee = from->cray_pointee;
587       to->cp_was_assumed = from->cp_was_assumed;
588
589       for (i = 0; i < to->corank; i++)
590         {
591           to->lower[from->rank + i] = to->lower[i];
592           to->upper[from->rank + i] = to->upper[i];
593         }
594       for (i = 0; i < from->rank; i++)
595         {
596           if (copy)
597             {
598               to->lower[i] = gfc_copy_expr (from->lower[i]);
599               to->upper[i] = gfc_copy_expr (from->upper[i]);
600             }
601           else
602             {
603               to->lower[i] = from->lower[i];
604               to->upper[i] = from->upper[i];
605             }
606         }
607     }
608   else if (to->corank == 0 && from->corank > 0)
609     {
610       to->corank = from->corank;
611       to->cotype = from->cotype;
612
613       for (i = 0; i < from->corank; i++)
614         {
615           if (copy)
616             {
617               to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
618               to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
619             }
620           else
621             {
622               to->lower[to->rank + i] = from->lower[i];
623               to->upper[to->rank + i] = from->upper[i];
624             }
625         }
626     }
627 }
628
629
630 /* Match an intent specification.  Since this can only happen after an
631    INTENT word, a legal intent-spec must follow.  */
632
633 static sym_intent
634 match_intent_spec (void)
635 {
636
637   if (gfc_match (" ( in out )") == MATCH_YES)
638     return INTENT_INOUT;
639   if (gfc_match (" ( in )") == MATCH_YES)
640     return INTENT_IN;
641   if (gfc_match (" ( out )") == MATCH_YES)
642     return INTENT_OUT;
643
644   gfc_error ("Bad INTENT specification at %C");
645   return INTENT_UNKNOWN;
646 }
647
648
649 /* Matches a character length specification, which is either a
650    specification expression or a '*'.  */
651
652 static match
653 char_len_param_value (gfc_expr **expr)
654 {
655   match m;
656
657   if (gfc_match_char ('*') == MATCH_YES)
658     {
659       *expr = NULL;
660       return MATCH_YES;
661     }
662
663   m = gfc_match_expr (expr);
664
665   if (m == MATCH_YES
666       && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
667     return MATCH_ERROR;
668
669   if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
670     {
671       if ((*expr)->value.function.actual
672           && (*expr)->value.function.actual->expr->symtree)
673         {
674           gfc_expr *e;
675           e = (*expr)->value.function.actual->expr;
676           if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
677               && e->expr_type == EXPR_VARIABLE)
678             {
679               if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
680                 goto syntax;
681               if (e->symtree->n.sym->ts.type == BT_CHARACTER
682                   && e->symtree->n.sym->ts.u.cl
683                   && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
684                 goto syntax;
685             }
686         }
687     }
688   return m;
689
690 syntax:
691   gfc_error ("Conflict in attributes of function argument at %C");
692   return MATCH_ERROR;
693 }
694
695
696 /* A character length is a '*' followed by a literal integer or a
697    char_len_param_value in parenthesis.  */
698
699 static match
700 match_char_length (gfc_expr **expr)
701 {
702   int length;
703   match m;
704
705   m = gfc_match_char ('*');
706   if (m != MATCH_YES)
707     return m;
708
709   m = gfc_match_small_literal_int (&length, NULL);
710   if (m == MATCH_ERROR)
711     return m;
712
713   if (m == MATCH_YES)
714     {
715       if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
716                           "Old-style character length at %C") == FAILURE)
717         return MATCH_ERROR;
718       *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
719       return m;
720     }
721
722   if (gfc_match_char ('(') == MATCH_NO)
723     goto syntax;
724
725   m = char_len_param_value (expr);
726   if (m != MATCH_YES && gfc_matching_function)
727     {
728       gfc_undo_symbols ();
729       m = MATCH_YES;
730     }
731
732   if (m == MATCH_ERROR)
733     return m;
734   if (m == MATCH_NO)
735     goto syntax;
736
737   if (gfc_match_char (')') == MATCH_NO)
738     {
739       gfc_free_expr (*expr);
740       *expr = NULL;
741       goto syntax;
742     }
743
744   return MATCH_YES;
745
746 syntax:
747   gfc_error ("Syntax error in character length specification at %C");
748   return MATCH_ERROR;
749 }
750
751
752 /* Special subroutine for finding a symbol.  Check if the name is found
753    in the current name space.  If not, and we're compiling a function or
754    subroutine and the parent compilation unit is an interface, then check
755    to see if the name we've been given is the name of the interface
756    (located in another namespace).  */
757
758 static int
759 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
760 {
761   gfc_state_data *s;
762   gfc_symtree *st;
763   int i;
764
765   i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
766   if (i == 0)
767     {
768       *result = st ? st->n.sym : NULL;
769       goto end;
770     }
771
772   if (gfc_current_state () != COMP_SUBROUTINE
773       && gfc_current_state () != COMP_FUNCTION)
774     goto end;
775
776   s = gfc_state_stack->previous;
777   if (s == NULL)
778     goto end;
779
780   if (s->state != COMP_INTERFACE)
781     goto end;
782   if (s->sym == NULL)
783     goto end;             /* Nameless interface.  */
784
785   if (strcmp (name, s->sym->name) == 0)
786     {
787       *result = s->sym;
788       return 0;
789     }
790
791 end:
792   return i;
793 }
794
795
796 /* Special subroutine for getting a symbol node associated with a
797    procedure name, used in SUBROUTINE and FUNCTION statements.  The
798    symbol is created in the parent using with symtree node in the
799    child unit pointing to the symbol.  If the current namespace has no
800    parent, then the symbol is just created in the current unit.  */
801
802 static int
803 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
804 {
805   gfc_symtree *st;
806   gfc_symbol *sym;
807   int rc = 0;
808
809   /* Module functions have to be left in their own namespace because
810      they have potentially (almost certainly!) already been referenced.
811      In this sense, they are rather like external functions.  This is
812      fixed up in resolve.c(resolve_entries), where the symbol name-
813      space is set to point to the master function, so that the fake
814      result mechanism can work.  */
815   if (module_fcn_entry)
816     {
817       /* Present if entry is declared to be a module procedure.  */
818       rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
819
820       if (*result == NULL)
821         rc = gfc_get_symbol (name, NULL, result);
822       else if (!gfc_get_symbol (name, NULL, &sym) && sym
823                  && (*result)->ts.type == BT_UNKNOWN
824                  && sym->attr.flavor == FL_UNKNOWN)
825         /* Pick up the typespec for the entry, if declared in the function
826            body.  Note that this symbol is FL_UNKNOWN because it will
827            only have appeared in a type declaration.  The local symtree
828            is set to point to the module symbol and a unique symtree
829            to the local version.  This latter ensures a correct clearing
830            of the symbols.  */
831         {
832           /* If the ENTRY proceeds its specification, we need to ensure
833              that this does not raise a "has no IMPLICIT type" error.  */
834           if (sym->ts.type == BT_UNKNOWN)
835             sym->attr.untyped = 1;
836
837           (*result)->ts = sym->ts;
838
839           /* Put the symbol in the procedure namespace so that, should
840              the ENTRY precede its specification, the specification
841              can be applied.  */
842           (*result)->ns = gfc_current_ns;
843
844           gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
845           st->n.sym = *result;
846           st = gfc_get_unique_symtree (gfc_current_ns);
847           st->n.sym = sym;
848         }
849     }
850   else
851     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
852
853   if (rc)
854     return rc;
855
856   sym = *result;
857   gfc_current_ns->refs++;
858
859   if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
860     {
861       /* Trap another encompassed procedure with the same name.  All
862          these conditions are necessary to avoid picking up an entry
863          whose name clashes with that of the encompassing procedure;
864          this is handled using gsymbols to register unique,globally
865          accessible names.  */
866       if (sym->attr.flavor != 0
867           && sym->attr.proc != 0
868           && (sym->attr.subroutine || sym->attr.function)
869           && sym->attr.if_source != IFSRC_UNKNOWN)
870         gfc_error_now ("Procedure '%s' at %C is already defined at %L",
871                        name, &sym->declared_at);
872
873       /* Trap a procedure with a name the same as interface in the
874          encompassing scope.  */
875       if (sym->attr.generic != 0
876           && (sym->attr.subroutine || sym->attr.function)
877           && !sym->attr.mod_proc)
878         gfc_error_now ("Name '%s' at %C is already defined"
879                        " as a generic interface at %L",
880                        name, &sym->declared_at);
881
882       /* Trap declarations of attributes in encompassing scope.  The
883          signature for this is that ts.kind is set.  Legitimate
884          references only set ts.type.  */
885       if (sym->ts.kind != 0
886           && !sym->attr.implicit_type
887           && sym->attr.proc == 0
888           && gfc_current_ns->parent != NULL
889           && sym->attr.access == 0
890           && !module_fcn_entry)
891         gfc_error_now ("Procedure '%s' at %C has an explicit interface "
892                        "and must not have attributes declared at %L",
893                        name, &sym->declared_at);
894     }
895
896   if (gfc_current_ns->parent == NULL || *result == NULL)
897     return rc;
898
899   /* Module function entries will already have a symtree in
900      the current namespace but will need one at module level.  */
901   if (module_fcn_entry)
902     {
903       /* Present if entry is declared to be a module procedure.  */
904       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
905       if (st == NULL)
906         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
907     }
908   else
909     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
910
911   st->n.sym = sym;
912   sym->refs++;
913
914   /* See if the procedure should be a module procedure.  */
915
916   if (((sym->ns->proc_name != NULL
917                 && sym->ns->proc_name->attr.flavor == FL_MODULE
918                 && sym->attr.proc != PROC_MODULE)
919             || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
920         && gfc_add_procedure (&sym->attr, PROC_MODULE,
921                               sym->name, NULL) == FAILURE)
922     rc = 2;
923
924   return rc;
925 }
926
927
928 /* Verify that the given symbol representing a parameter is C
929    interoperable, by checking to see if it was marked as such after
930    its declaration.  If the given symbol is not interoperable, a
931    warning is reported, thus removing the need to return the status to
932    the calling function.  The standard does not require the user use
933    one of the iso_c_binding named constants to declare an
934    interoperable parameter, but we can't be sure if the param is C
935    interop or not if the user doesn't.  For example, integer(4) may be
936    legal Fortran, but doesn't have meaning in C.  It may interop with
937    a number of the C types, which causes a problem because the
938    compiler can't know which one.  This code is almost certainly not
939    portable, and the user will get what they deserve if the C type
940    across platforms isn't always interoperable with integer(4).  If
941    the user had used something like integer(c_int) or integer(c_long),
942    the compiler could have automatically handled the varying sizes
943    across platforms.  */
944
945 gfc_try
946 verify_c_interop_param (gfc_symbol *sym)
947 {
948   int is_c_interop = 0;
949   gfc_try retval = SUCCESS;
950
951   /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
952      Don't repeat the checks here.  */
953   if (sym->attr.implicit_type)
954     return SUCCESS;
955   
956   /* For subroutines or functions that are passed to a BIND(C) procedure,
957      they're interoperable if they're BIND(C) and their params are all
958      interoperable.  */
959   if (sym->attr.flavor == FL_PROCEDURE)
960     {
961       if (sym->attr.is_bind_c == 0)
962         {
963           gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
964                          "attribute to be C interoperable", sym->name,
965                          &(sym->declared_at));
966                          
967           return FAILURE;
968         }
969       else
970         {
971           if (sym->attr.is_c_interop == 1)
972             /* We've already checked this procedure; don't check it again.  */
973             return SUCCESS;
974           else
975             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
976                                       sym->common_block);
977         }
978     }
979   
980   /* See if we've stored a reference to a procedure that owns sym.  */
981   if (sym->ns != NULL && sym->ns->proc_name != NULL)
982     {
983       if (sym->ns->proc_name->attr.is_bind_c == 1)
984         {
985           is_c_interop =
986             (verify_c_interop (&(sym->ts))
987              == SUCCESS ? 1 : 0);
988
989           if (is_c_interop != 1)
990             {
991               /* Make personalized messages to give better feedback.  */
992               if (sym->ts.type == BT_DERIVED)
993                 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
994                            "procedure '%s' but is not C interoperable "
995                            "because derived type '%s' is not C interoperable",
996                            sym->name, &(sym->declared_at),
997                            sym->ns->proc_name->name, 
998                            sym->ts.u.derived->name);
999               else
1000                 gfc_warning ("Variable '%s' at %L is a parameter to the "
1001                              "BIND(C) procedure '%s' but may not be C "
1002                              "interoperable",
1003                              sym->name, &(sym->declared_at),
1004                              sym->ns->proc_name->name);
1005             }
1006
1007           /* Character strings are only C interoperable if they have a
1008              length of 1.  */
1009           if (sym->ts.type == BT_CHARACTER)
1010             {
1011               gfc_charlen *cl = sym->ts.u.cl;
1012               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1013                   || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1014                 {
1015                   gfc_error ("Character argument '%s' at %L "
1016                              "must be length 1 because "
1017                              "procedure '%s' is BIND(C)",
1018                              sym->name, &sym->declared_at,
1019                              sym->ns->proc_name->name);
1020                   retval = FAILURE;
1021                 }
1022             }
1023
1024           /* We have to make sure that any param to a bind(c) routine does
1025              not have the allocatable, pointer, or optional attributes,
1026              according to J3/04-007, section 5.1.  */
1027           if (sym->attr.allocatable == 1)
1028             {
1029               gfc_error ("Variable '%s' at %L cannot have the "
1030                          "ALLOCATABLE attribute because procedure '%s'"
1031                          " is BIND(C)", sym->name, &(sym->declared_at),
1032                          sym->ns->proc_name->name);
1033               retval = FAILURE;
1034             }
1035
1036           if (sym->attr.pointer == 1)
1037             {
1038               gfc_error ("Variable '%s' at %L cannot have the "
1039                          "POINTER attribute because procedure '%s'"
1040                          " is BIND(C)", sym->name, &(sym->declared_at),
1041                          sym->ns->proc_name->name);
1042               retval = FAILURE;
1043             }
1044
1045           if (sym->attr.optional == 1)
1046             {
1047               gfc_error ("Variable '%s' at %L cannot have the "
1048                          "OPTIONAL attribute because procedure '%s'"
1049                          " is BIND(C)", sym->name, &(sym->declared_at),
1050                          sym->ns->proc_name->name);
1051               retval = FAILURE;
1052             }
1053
1054           /* Make sure that if it has the dimension attribute, that it is
1055              either assumed size or explicit shape.  */
1056           if (sym->as != NULL)
1057             {
1058               if (sym->as->type == AS_ASSUMED_SHAPE)
1059                 {
1060                   gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1061                              "argument to the procedure '%s' at %L because "
1062                              "the procedure is BIND(C)", sym->name,
1063                              &(sym->declared_at), sym->ns->proc_name->name,
1064                              &(sym->ns->proc_name->declared_at));
1065                   retval = FAILURE;
1066                 }
1067
1068               if (sym->as->type == AS_DEFERRED)
1069                 {
1070                   gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1071                              "argument to the procedure '%s' at %L because "
1072                              "the procedure is BIND(C)", sym->name,
1073                              &(sym->declared_at), sym->ns->proc_name->name,
1074                              &(sym->ns->proc_name->declared_at));
1075                   retval = FAILURE;
1076                 }
1077           }
1078         }
1079     }
1080
1081   return retval;
1082 }
1083
1084
1085
1086 /* Function called by variable_decl() that adds a name to the symbol table.  */
1087
1088 static gfc_try
1089 build_sym (const char *name, gfc_charlen *cl,
1090            gfc_array_spec **as, locus *var_locus)
1091 {
1092   symbol_attribute attr;
1093   gfc_symbol *sym;
1094
1095   if (gfc_get_symbol (name, NULL, &sym))
1096     return FAILURE;
1097
1098   /* Start updating the symbol table.  Add basic type attribute if present.  */
1099   if (current_ts.type != BT_UNKNOWN
1100       && (sym->attr.implicit_type == 0
1101           || !gfc_compare_types (&sym->ts, &current_ts))
1102       && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1103     return FAILURE;
1104
1105   if (sym->ts.type == BT_CHARACTER)
1106     sym->ts.u.cl = cl;
1107
1108   /* Add dimension attribute if present.  */
1109   if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1110     return FAILURE;
1111   *as = NULL;
1112
1113   /* Add attribute to symbol.  The copy is so that we can reset the
1114      dimension attribute.  */
1115   attr = current_attr;
1116   attr.dimension = 0;
1117   attr.codimension = 0;
1118
1119   if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1120     return FAILURE;
1121
1122   /* Finish any work that may need to be done for the binding label,
1123      if it's a bind(c).  The bind(c) attr is found before the symbol
1124      is made, and before the symbol name (for data decls), so the
1125      current_ts is holding the binding label, or nothing if the
1126      name= attr wasn't given.  Therefore, test here if we're dealing
1127      with a bind(c) and make sure the binding label is set correctly.  */
1128   if (sym->attr.is_bind_c == 1)
1129     {
1130       if (sym->binding_label[0] == '\0')
1131         {
1132           /* Set the binding label and verify that if a NAME= was specified
1133              then only one identifier was in the entity-decl-list.  */
1134           if (set_binding_label (sym->binding_label, sym->name,
1135                                  num_idents_on_line) == FAILURE)
1136             return FAILURE;
1137         }
1138     }
1139
1140   /* See if we know we're in a common block, and if it's a bind(c)
1141      common then we need to make sure we're an interoperable type.  */
1142   if (sym->attr.in_common == 1)
1143     {
1144       /* Test the common block object.  */
1145       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1146           && sym->ts.is_c_interop != 1)
1147         {
1148           gfc_error_now ("Variable '%s' in common block '%s' at %C "
1149                          "must be declared with a C interoperable "
1150                          "kind since common block '%s' is BIND(C)",
1151                          sym->name, sym->common_block->name,
1152                          sym->common_block->name);
1153           gfc_clear_error ();
1154         }
1155     }
1156
1157   sym->attr.implied_index = 0;
1158
1159   if (sym->ts.type == BT_CLASS
1160       && (sym->attr.class_ok = sym->attr.dummy || sym->attr.pointer
1161                                || sym->attr.allocatable))
1162     gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1163
1164   return SUCCESS;
1165 }
1166
1167
1168 /* Set character constant to the given length. The constant will be padded or
1169    truncated.  If we're inside an array constructor without a typespec, we
1170    additionally check that all elements have the same length; check_len -1
1171    means no checking.  */
1172
1173 void
1174 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1175 {
1176   gfc_char_t *s;
1177   int slen;
1178
1179   gcc_assert (expr->expr_type == EXPR_CONSTANT);
1180   gcc_assert (expr->ts.type == BT_CHARACTER);
1181
1182   slen = expr->value.character.length;
1183   if (len != slen)
1184     {
1185       s = gfc_get_wide_string (len + 1);
1186       memcpy (s, expr->value.character.string,
1187               MIN (len, slen) * sizeof (gfc_char_t));
1188       if (len > slen)
1189         gfc_wide_memset (&s[slen], ' ', len - slen);
1190
1191       if (gfc_option.warn_character_truncation && slen > len)
1192         gfc_warning_now ("CHARACTER expression at %L is being truncated "
1193                          "(%d/%d)", &expr->where, slen, len);
1194
1195       /* Apply the standard by 'hand' otherwise it gets cleared for
1196          initializers.  */
1197       if (check_len != -1 && slen != check_len
1198           && !(gfc_option.allow_std & GFC_STD_GNU))
1199         gfc_error_now ("The CHARACTER elements of the array constructor "
1200                        "at %L must have the same length (%d/%d)",
1201                         &expr->where, slen, check_len);
1202
1203       s[len] = '\0';
1204       gfc_free (expr->value.character.string);
1205       expr->value.character.string = s;
1206       expr->value.character.length = len;
1207     }
1208 }
1209
1210
1211 /* Function to create and update the enumerator history
1212    using the information passed as arguments.
1213    Pointer "max_enum" is also updated, to point to
1214    enum history node containing largest initializer.
1215
1216    SYM points to the symbol node of enumerator.
1217    INIT points to its enumerator value.  */
1218
1219 static void
1220 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1221 {
1222   enumerator_history *new_enum_history;
1223   gcc_assert (sym != NULL && init != NULL);
1224
1225   new_enum_history = XCNEW (enumerator_history);
1226
1227   new_enum_history->sym = sym;
1228   new_enum_history->initializer = init;
1229   new_enum_history->next = NULL;
1230
1231   if (enum_history == NULL)
1232     {
1233       enum_history = new_enum_history;
1234       max_enum = enum_history;
1235     }
1236   else
1237     {
1238       new_enum_history->next = enum_history;
1239       enum_history = new_enum_history;
1240
1241       if (mpz_cmp (max_enum->initializer->value.integer,
1242                    new_enum_history->initializer->value.integer) < 0)
1243         max_enum = new_enum_history;
1244     }
1245 }
1246
1247
1248 /* Function to free enum kind history.  */
1249
1250 void
1251 gfc_free_enum_history (void)
1252 {
1253   enumerator_history *current = enum_history;
1254   enumerator_history *next;
1255
1256   while (current != NULL)
1257     {
1258       next = current->next;
1259       gfc_free (current);
1260       current = next;
1261     }
1262   max_enum = NULL;
1263   enum_history = NULL;
1264 }
1265
1266
1267 /* Function called by variable_decl() that adds an initialization
1268    expression to a symbol.  */
1269
1270 static gfc_try
1271 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1272 {
1273   symbol_attribute attr;
1274   gfc_symbol *sym;
1275   gfc_expr *init;
1276
1277   init = *initp;
1278   if (find_special (name, &sym, false))
1279     return FAILURE;
1280
1281   attr = sym->attr;
1282
1283   /* If this symbol is confirming an implicit parameter type,
1284      then an initialization expression is not allowed.  */
1285   if (attr.flavor == FL_PARAMETER
1286       && sym->value != NULL
1287       && *initp != NULL)
1288     {
1289       gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1290                  sym->name);
1291       return FAILURE;
1292     }
1293
1294   if (init == NULL)
1295     {
1296       /* An initializer is required for PARAMETER declarations.  */
1297       if (attr.flavor == FL_PARAMETER)
1298         {
1299           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1300           return FAILURE;
1301         }
1302     }
1303   else
1304     {
1305       /* If a variable appears in a DATA block, it cannot have an
1306          initializer.  */
1307       if (sym->attr.data)
1308         {
1309           gfc_error ("Variable '%s' at %C with an initializer already "
1310                      "appears in a DATA statement", sym->name);
1311           return FAILURE;
1312         }
1313
1314       /* Check if the assignment can happen. This has to be put off
1315          until later for a derived type variable.  */
1316       if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1317           && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1318           && gfc_check_assign_symbol (sym, init) == FAILURE)
1319         return FAILURE;
1320
1321       if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1322             && init->ts.type == BT_CHARACTER)
1323         {
1324           /* Update symbol character length according initializer.  */
1325           if (gfc_check_assign_symbol (sym, init) == FAILURE)
1326             return FAILURE;
1327
1328           if (sym->ts.u.cl->length == NULL)
1329             {
1330               int clen;
1331               /* If there are multiple CHARACTER variables declared on the
1332                  same line, we don't want them to share the same length.  */
1333               sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1334
1335               if (sym->attr.flavor == FL_PARAMETER)
1336                 {
1337                   if (init->expr_type == EXPR_CONSTANT)
1338                     {
1339                       clen = init->value.character.length;
1340                       sym->ts.u.cl->length
1341                                 = gfc_get_int_expr (gfc_default_integer_kind,
1342                                                     NULL, clen);
1343                     }
1344                   else if (init->expr_type == EXPR_ARRAY)
1345                     {
1346                       gfc_constructor *c;
1347                       c = gfc_constructor_first (init->value.constructor);
1348                       clen = c->expr->value.character.length;
1349                       sym->ts.u.cl->length
1350                                 = gfc_get_int_expr (gfc_default_integer_kind,
1351                                                     NULL, clen);
1352                     }
1353                   else if (init->ts.u.cl && init->ts.u.cl->length)
1354                     sym->ts.u.cl->length =
1355                                 gfc_copy_expr (sym->value->ts.u.cl->length);
1356                 }
1357             }
1358           /* Update initializer character length according symbol.  */
1359           else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1360             {
1361               int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1362
1363               if (init->expr_type == EXPR_CONSTANT)
1364                 gfc_set_constant_character_len (len, init, -1);
1365               else if (init->expr_type == EXPR_ARRAY)
1366                 {
1367                   gfc_constructor *c;
1368
1369                   /* Build a new charlen to prevent simplification from
1370                      deleting the length before it is resolved.  */
1371                   init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1372                   init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1373
1374                   for (c = gfc_constructor_first (init->value.constructor);
1375                        c; c = gfc_constructor_next (c))
1376                     gfc_set_constant_character_len (len, c->expr, -1);
1377                 }
1378             }
1379         }
1380
1381       /* If sym is implied-shape, set its upper bounds from init.  */
1382       if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1383           && sym->as->type == AS_IMPLIED_SHAPE)
1384         {
1385           int dim;
1386
1387           if (init->rank == 0)
1388             {
1389               gfc_error ("Can't initialize implied-shape array at %L"
1390                          " with scalar", &sym->declared_at);
1391               return FAILURE;
1392             }
1393           gcc_assert (sym->as->rank == init->rank);
1394
1395           /* Shape should be present, we get an initialization expression.  */
1396           gcc_assert (init->shape);
1397
1398           for (dim = 0; dim < sym->as->rank; ++dim)
1399             {
1400               int k;
1401               gfc_expr* lower;
1402               gfc_expr* e;
1403               
1404               lower = sym->as->lower[dim];
1405               if (lower->expr_type != EXPR_CONSTANT)
1406                 {
1407                   gfc_error ("Non-constant lower bound in implied-shape"
1408                              " declaration at %L", &lower->where);
1409                   return FAILURE;
1410                 }
1411
1412               /* All dimensions must be without upper bound.  */
1413               gcc_assert (!sym->as->upper[dim]);
1414
1415               k = lower->ts.kind;
1416               e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1417               mpz_add (e->value.integer,
1418                        lower->value.integer, init->shape[dim]);
1419               mpz_sub_ui (e->value.integer, e->value.integer, 1);
1420               sym->as->upper[dim] = e;
1421             }
1422
1423           sym->as->type = AS_EXPLICIT;
1424         }
1425
1426       /* Need to check if the expression we initialized this
1427          to was one of the iso_c_binding named constants.  If so,
1428          and we're a parameter (constant), let it be iso_c.
1429          For example:
1430          integer(c_int), parameter :: my_int = c_int
1431          integer(my_int) :: my_int_2
1432          If we mark my_int as iso_c (since we can see it's value
1433          is equal to one of the named constants), then my_int_2
1434          will be considered C interoperable.  */
1435       if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1436         {
1437           sym->ts.is_iso_c |= init->ts.is_iso_c;
1438           sym->ts.is_c_interop |= init->ts.is_c_interop;
1439           /* attr bits needed for module files.  */
1440           sym->attr.is_iso_c |= init->ts.is_iso_c;
1441           sym->attr.is_c_interop |= init->ts.is_c_interop;
1442           if (init->ts.is_iso_c)
1443             sym->ts.f90_type = init->ts.f90_type;
1444         }
1445
1446       /* Add initializer.  Make sure we keep the ranks sane.  */
1447       if (sym->attr.dimension && init->rank == 0)
1448         {
1449           mpz_t size;
1450           gfc_expr *array;
1451           int n;
1452           if (sym->attr.flavor == FL_PARAMETER
1453                 && init->expr_type == EXPR_CONSTANT
1454                 && spec_size (sym->as, &size) == SUCCESS
1455                 && mpz_cmp_si (size, 0) > 0)
1456             {
1457               array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1458                                           &init->where);
1459               for (n = 0; n < (int)mpz_get_si (size); n++)
1460                 gfc_constructor_append_expr (&array->value.constructor,
1461                                              n == 0
1462                                                 ? init
1463                                                 : gfc_copy_expr (init),
1464                                              &init->where);
1465                 
1466               array->shape = gfc_get_shape (sym->as->rank);
1467               for (n = 0; n < sym->as->rank; n++)
1468                 spec_dimen_size (sym->as, n, &array->shape[n]);
1469
1470               init = array;
1471               mpz_clear (size);
1472             }
1473           init->rank = sym->as->rank;
1474         }
1475
1476       sym->value = init;
1477       if (sym->attr.save == SAVE_NONE)
1478         sym->attr.save = SAVE_IMPLICIT;
1479       *initp = NULL;
1480     }
1481
1482   return SUCCESS;
1483 }
1484
1485
1486 /* Function called by variable_decl() that adds a name to a structure
1487    being built.  */
1488
1489 static gfc_try
1490 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1491               gfc_array_spec **as)
1492 {
1493   gfc_component *c;
1494   gfc_try t = SUCCESS;
1495
1496   /* F03:C438/C439. If the current symbol is of the same derived type that we're
1497      constructing, it must have the pointer attribute.  */
1498   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1499       && current_ts.u.derived == gfc_current_block ()
1500       && current_attr.pointer == 0)
1501     {
1502       gfc_error ("Component at %C must have the POINTER attribute");
1503       return FAILURE;
1504     }
1505
1506   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1507     {
1508       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1509         {
1510           gfc_error ("Array component of structure at %C must have explicit "
1511                      "or deferred shape");
1512           return FAILURE;
1513         }
1514     }
1515
1516   if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1517     return FAILURE;
1518
1519   c->ts = current_ts;
1520   if (c->ts.type == BT_CHARACTER)
1521     c->ts.u.cl = cl;
1522   c->attr = current_attr;
1523
1524   c->initializer = *init;
1525   *init = NULL;
1526
1527   c->as = *as;
1528   if (c->as != NULL)
1529     {
1530       if (c->as->corank)
1531         c->attr.codimension = 1;
1532       if (c->as->rank)
1533         c->attr.dimension = 1;
1534     }
1535   *as = NULL;
1536
1537   /* Should this ever get more complicated, combine with similar section
1538      in add_init_expr_to_sym into a separate function.  */
1539   if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1540       && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1541     {
1542       int len;
1543
1544       gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1545       gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1546       gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1547
1548       len = mpz_get_si (c->ts.u.cl->length->value.integer);
1549
1550       if (c->initializer->expr_type == EXPR_CONSTANT)
1551         gfc_set_constant_character_len (len, c->initializer, -1);
1552       else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1553                         c->initializer->ts.u.cl->length->value.integer))
1554         {
1555           gfc_constructor *ctor;
1556           ctor = gfc_constructor_first (c->initializer->value.constructor);
1557
1558           if (ctor)
1559             {
1560               int first_len;
1561               bool has_ts = (c->initializer->ts.u.cl
1562                              && c->initializer->ts.u.cl->length_from_typespec);
1563
1564               /* Remember the length of the first element for checking
1565                  that all elements *in the constructor* have the same
1566                  length.  This need not be the length of the LHS!  */
1567               gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1568               gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1569               first_len = ctor->expr->value.character.length;
1570
1571               for ( ; ctor; ctor = gfc_constructor_next (ctor))
1572                 if (ctor->expr->expr_type == EXPR_CONSTANT)
1573                 {
1574                   gfc_set_constant_character_len (len, ctor->expr,
1575                                                   has_ts ? -1 : first_len);
1576                   ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1577                 }
1578             }
1579         }
1580     }
1581
1582   /* Check array components.  */
1583   if (!c->attr.dimension)
1584     goto scalar;
1585
1586   if (c->attr.pointer)
1587     {
1588       if (c->as->type != AS_DEFERRED)
1589         {
1590           gfc_error ("Pointer array component of structure at %C must have a "
1591                      "deferred shape");
1592           t = FAILURE;
1593         }
1594     }
1595   else if (c->attr.allocatable)
1596     {
1597       if (c->as->type != AS_DEFERRED)
1598         {
1599           gfc_error ("Allocatable component of structure at %C must have a "
1600                      "deferred shape");
1601           t = FAILURE;
1602         }
1603     }
1604   else
1605     {
1606       if (c->as->type != AS_EXPLICIT)
1607         {
1608           gfc_error ("Array component of structure at %C must have an "
1609                      "explicit shape");
1610           t = FAILURE;
1611         }
1612     }
1613
1614 scalar:
1615   if (c->ts.type == BT_CLASS)
1616     gfc_build_class_symbol (&c->ts, &c->attr, &c->as, true);
1617
1618   return t;
1619 }
1620
1621
1622 /* Match a 'NULL()', and possibly take care of some side effects.  */
1623
1624 match
1625 gfc_match_null (gfc_expr **result)
1626 {
1627   gfc_symbol *sym;
1628   match m;
1629
1630   m = gfc_match (" null ( )");
1631   if (m != MATCH_YES)
1632     return m;
1633
1634   /* The NULL symbol now has to be/become an intrinsic function.  */
1635   if (gfc_get_symbol ("null", NULL, &sym))
1636     {
1637       gfc_error ("NULL() initialization at %C is ambiguous");
1638       return MATCH_ERROR;
1639     }
1640
1641   gfc_intrinsic_symbol (sym);
1642
1643   if (sym->attr.proc != PROC_INTRINSIC
1644       && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1645                              sym->name, NULL) == FAILURE
1646           || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1647     return MATCH_ERROR;
1648
1649   *result = gfc_get_null_expr (&gfc_current_locus);
1650
1651   return MATCH_YES;
1652 }
1653
1654
1655 /* Match a variable name with an optional initializer.  When this
1656    subroutine is called, a variable is expected to be parsed next.
1657    Depending on what is happening at the moment, updates either the
1658    symbol table or the current interface.  */
1659
1660 static match
1661 variable_decl (int elem)
1662 {
1663   char name[GFC_MAX_SYMBOL_LEN + 1];
1664   gfc_expr *initializer, *char_len;
1665   gfc_array_spec *as;
1666   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1667   gfc_charlen *cl;
1668   locus var_locus;
1669   match m;
1670   gfc_try t;
1671   gfc_symbol *sym;
1672
1673   initializer = NULL;
1674   as = NULL;
1675   cp_as = NULL;
1676
1677   /* When we get here, we've just matched a list of attributes and
1678      maybe a type and a double colon.  The next thing we expect to see
1679      is the name of the symbol.  */
1680   m = gfc_match_name (name);
1681   if (m != MATCH_YES)
1682     goto cleanup;
1683
1684   var_locus = gfc_current_locus;
1685
1686   /* Now we could see the optional array spec. or character length.  */
1687   m = gfc_match_array_spec (&as, true, true);
1688   if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1689     cp_as = gfc_copy_array_spec (as);
1690   else if (m == MATCH_ERROR)
1691     goto cleanup;
1692
1693   if (m == MATCH_NO)
1694     as = gfc_copy_array_spec (current_as);
1695   else if (current_as)
1696     merge_array_spec (current_as, as, true);
1697
1698   /* At this point, we know for sure if the symbol is PARAMETER and can thus
1699      determine (and check) whether it can be implied-shape.  If it
1700      was parsed as assumed-size, change it because PARAMETERs can not
1701      be assumed-size.  */
1702   if (as)
1703     {
1704       if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1705         {
1706           m = MATCH_ERROR;
1707           gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1708                      name, &var_locus);
1709           goto cleanup;
1710         }
1711
1712       if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1713           && current_attr.flavor == FL_PARAMETER)
1714         as->type = AS_IMPLIED_SHAPE;
1715
1716       if (as->type == AS_IMPLIED_SHAPE
1717           && gfc_notify_std (GFC_STD_F2008,
1718                              "Fortran 2008: Implied-shape array at %L",
1719                              &var_locus) == FAILURE)
1720         {
1721           m = MATCH_ERROR;
1722           goto cleanup;
1723         }
1724     }
1725
1726   char_len = NULL;
1727   cl = NULL;
1728
1729   if (current_ts.type == BT_CHARACTER)
1730     {
1731       switch (match_char_length (&char_len))
1732         {
1733         case MATCH_YES:
1734           cl = gfc_new_charlen (gfc_current_ns, NULL);
1735
1736           cl->length = char_len;
1737           break;
1738
1739         /* Non-constant lengths need to be copied after the first
1740            element.  Also copy assumed lengths.  */
1741         case MATCH_NO:
1742           if (elem > 1
1743               && (current_ts.u.cl->length == NULL
1744                   || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1745             {
1746               cl = gfc_new_charlen (gfc_current_ns, NULL);
1747               cl->length = gfc_copy_expr (current_ts.u.cl->length);
1748             }
1749           else
1750             cl = current_ts.u.cl;
1751
1752           break;
1753
1754         case MATCH_ERROR:
1755           goto cleanup;
1756         }
1757     }
1758
1759   /*  If this symbol has already shown up in a Cray Pointer declaration,
1760       then we want to set the type & bail out.  */
1761   if (gfc_option.flag_cray_pointer)
1762     {
1763       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1764       if (sym != NULL && sym->attr.cray_pointee)
1765         {
1766           sym->ts.type = current_ts.type;
1767           sym->ts.kind = current_ts.kind;
1768           sym->ts.u.cl = cl;
1769           sym->ts.u.derived = current_ts.u.derived;
1770           sym->ts.is_c_interop = current_ts.is_c_interop;
1771           sym->ts.is_iso_c = current_ts.is_iso_c;
1772           m = MATCH_YES;
1773         
1774           /* Check to see if we have an array specification.  */
1775           if (cp_as != NULL)
1776             {
1777               if (sym->as != NULL)
1778                 {
1779                   gfc_error ("Duplicate array spec for Cray pointee at %C");
1780                   gfc_free_array_spec (cp_as);
1781                   m = MATCH_ERROR;
1782                   goto cleanup;
1783                 }
1784               else
1785                 {
1786                   if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1787                     gfc_internal_error ("Couldn't set pointee array spec.");
1788
1789                   /* Fix the array spec.  */
1790                   m = gfc_mod_pointee_as (sym->as);
1791                   if (m == MATCH_ERROR)
1792                     goto cleanup;
1793                 }
1794             }
1795           goto cleanup;
1796         }
1797       else
1798         {
1799           gfc_free_array_spec (cp_as);
1800         }
1801     }
1802
1803   /* Procedure pointer as function result.  */
1804   if (gfc_current_state () == COMP_FUNCTION
1805       && strcmp ("ppr@", gfc_current_block ()->name) == 0
1806       && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1807     strcpy (name, "ppr@");
1808
1809   if (gfc_current_state () == COMP_FUNCTION
1810       && strcmp (name, gfc_current_block ()->name) == 0
1811       && gfc_current_block ()->result
1812       && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1813     strcpy (name, "ppr@");
1814
1815   /* OK, we've successfully matched the declaration.  Now put the
1816      symbol in the current namespace, because it might be used in the
1817      optional initialization expression for this symbol, e.g. this is
1818      perfectly legal:
1819
1820      integer, parameter :: i = huge(i)
1821
1822      This is only true for parameters or variables of a basic type.
1823      For components of derived types, it is not true, so we don't
1824      create a symbol for those yet.  If we fail to create the symbol,
1825      bail out.  */
1826   if (gfc_current_state () != COMP_DERIVED
1827       && build_sym (name, cl, &as, &var_locus) == FAILURE)
1828     {
1829       m = MATCH_ERROR;
1830       goto cleanup;
1831     }
1832
1833   /* An interface body specifies all of the procedure's
1834      characteristics and these shall be consistent with those
1835      specified in the procedure definition, except that the interface
1836      may specify a procedure that is not pure if the procedure is
1837      defined to be pure(12.3.2).  */
1838   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1839       && gfc_current_ns->proc_name
1840       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1841       && current_ts.u.derived->ns != gfc_current_ns)
1842     {
1843       gfc_symtree *st;
1844       st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1845       if (!(current_ts.u.derived->attr.imported
1846                 && st != NULL
1847                 && st->n.sym == current_ts.u.derived)
1848             && !gfc_current_ns->has_import_set)
1849         {
1850             gfc_error ("the type of '%s' at %C has not been declared within the "
1851                        "interface", name);
1852             m = MATCH_ERROR;
1853             goto cleanup;
1854         }
1855     }
1856
1857   /* In functions that have a RESULT variable defined, the function
1858      name always refers to function calls.  Therefore, the name is
1859      not allowed to appear in specification statements.  */
1860   if (gfc_current_state () == COMP_FUNCTION
1861       && gfc_current_block () != NULL
1862       && gfc_current_block ()->result != NULL
1863       && gfc_current_block ()->result != gfc_current_block ()
1864       && strcmp (gfc_current_block ()->name, name) == 0)
1865     {
1866       gfc_error ("Function name '%s' not allowed at %C", name);
1867       m = MATCH_ERROR;
1868       goto cleanup;
1869     }
1870
1871   /* We allow old-style initializations of the form
1872        integer i /2/, j(4) /3*3, 1/
1873      (if no colon has been seen). These are different from data
1874      statements in that initializers are only allowed to apply to the
1875      variable immediately preceding, i.e.
1876        integer i, j /1, 2/
1877      is not allowed. Therefore we have to do some work manually, that
1878      could otherwise be left to the matchers for DATA statements.  */
1879
1880   if (!colon_seen && gfc_match (" /") == MATCH_YES)
1881     {
1882       if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1883                           "initialization at %C") == FAILURE)
1884         return MATCH_ERROR;
1885  
1886       return match_old_style_init (name);
1887     }
1888
1889   /* The double colon must be present in order to have initializers.
1890      Otherwise the statement is ambiguous with an assignment statement.  */
1891   if (colon_seen)
1892     {
1893       if (gfc_match (" =>") == MATCH_YES)
1894         {
1895           if (!current_attr.pointer)
1896             {
1897               gfc_error ("Initialization at %C isn't for a pointer variable");
1898               m = MATCH_ERROR;
1899               goto cleanup;
1900             }
1901
1902           m = gfc_match_null (&initializer);
1903           if (m == MATCH_NO)
1904             {
1905               gfc_error ("Pointer initialization requires a NULL() at %C");
1906               m = MATCH_ERROR;
1907             }
1908
1909           if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1910             {
1911               gfc_error ("Initialization of pointer at %C is not allowed in "
1912                          "a PURE procedure");
1913               m = MATCH_ERROR;
1914             }
1915
1916           if (m != MATCH_YES)
1917             goto cleanup;
1918
1919         }
1920       else if (gfc_match_char ('=') == MATCH_YES)
1921         {
1922           if (current_attr.pointer)
1923             {
1924               gfc_error ("Pointer initialization at %C requires '=>', "
1925                          "not '='");
1926               m = MATCH_ERROR;
1927               goto cleanup;
1928             }
1929
1930           m = gfc_match_init_expr (&initializer);
1931           if (m == MATCH_NO)
1932             {
1933               gfc_error ("Expected an initialization expression at %C");
1934               m = MATCH_ERROR;
1935             }
1936
1937           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
1938               && gfc_state_stack->state != COMP_DERIVED)
1939             {
1940               gfc_error ("Initialization of variable at %C is not allowed in "
1941                          "a PURE procedure");
1942               m = MATCH_ERROR;
1943             }
1944
1945           if (m != MATCH_YES)
1946             goto cleanup;
1947         }
1948     }
1949
1950   if (initializer != NULL && current_attr.allocatable
1951         && gfc_current_state () == COMP_DERIVED)
1952     {
1953       gfc_error ("Initialization of allocatable component at %C is not "
1954                  "allowed");
1955       m = MATCH_ERROR;
1956       goto cleanup;
1957     }
1958
1959   /* Add the initializer.  Note that it is fine if initializer is
1960      NULL here, because we sometimes also need to check if a
1961      declaration *must* have an initialization expression.  */
1962   if (gfc_current_state () != COMP_DERIVED)
1963     t = add_init_expr_to_sym (name, &initializer, &var_locus);
1964   else
1965     {
1966       if (current_ts.type == BT_DERIVED
1967           && !current_attr.pointer && !initializer)
1968         initializer = gfc_default_initializer (&current_ts);
1969       t = build_struct (name, cl, &initializer, &as);
1970     }
1971
1972   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1973
1974 cleanup:
1975   /* Free stuff up and return.  */
1976   gfc_free_expr (initializer);
1977   gfc_free_array_spec (as);
1978
1979   return m;
1980 }
1981
1982
1983 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1984    This assumes that the byte size is equal to the kind number for
1985    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
1986
1987 match
1988 gfc_match_old_kind_spec (gfc_typespec *ts)
1989 {
1990   match m;
1991   int original_kind;
1992
1993   if (gfc_match_char ('*') != MATCH_YES)
1994     return MATCH_NO;
1995
1996   m = gfc_match_small_literal_int (&ts->kind, NULL);
1997   if (m != MATCH_YES)
1998     return MATCH_ERROR;
1999
2000   original_kind = ts->kind;
2001
2002   /* Massage the kind numbers for complex types.  */
2003   if (ts->type == BT_COMPLEX)
2004     {
2005       if (ts->kind % 2)
2006         {
2007           gfc_error ("Old-style type declaration %s*%d not supported at %C",
2008                      gfc_basic_typename (ts->type), original_kind);
2009           return MATCH_ERROR;
2010         }
2011       ts->kind /= 2;
2012     }
2013
2014   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2015     {
2016       gfc_error ("Old-style type declaration %s*%d not supported at %C",
2017                  gfc_basic_typename (ts->type), original_kind);
2018       return MATCH_ERROR;
2019     }
2020
2021   if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2022                       gfc_basic_typename (ts->type), original_kind) == FAILURE)
2023     return MATCH_ERROR;
2024
2025   return MATCH_YES;
2026 }
2027
2028
2029 /* Match a kind specification.  Since kinds are generally optional, we
2030    usually return MATCH_NO if something goes wrong.  If a "kind="
2031    string is found, then we know we have an error.  */
2032
2033 match
2034 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2035 {
2036   locus where, loc;
2037   gfc_expr *e;
2038   match m, n;
2039   char c;
2040   const char *msg;
2041
2042   m = MATCH_NO;
2043   n = MATCH_YES;
2044   e = NULL;
2045
2046   where = loc = gfc_current_locus;
2047
2048   if (kind_expr_only)
2049     goto kind_expr;
2050
2051   if (gfc_match_char ('(') == MATCH_NO)
2052     return MATCH_NO;
2053
2054   /* Also gobbles optional text.  */
2055   if (gfc_match (" kind = ") == MATCH_YES)
2056     m = MATCH_ERROR;
2057
2058   loc = gfc_current_locus;
2059
2060 kind_expr:
2061   n = gfc_match_init_expr (&e);
2062
2063   if (n != MATCH_YES)
2064     {
2065       if (gfc_matching_function)
2066         {
2067           /* The function kind expression might include use associated or 
2068              imported parameters and try again after the specification
2069              expressions.....  */
2070           if (gfc_match_char (')') != MATCH_YES)
2071             {
2072               gfc_error ("Missing right parenthesis at %C");
2073               m = MATCH_ERROR;
2074               goto no_match;
2075             }
2076
2077           gfc_free_expr (e);
2078           gfc_undo_symbols ();
2079           return MATCH_YES;
2080         }
2081       else
2082         {
2083           /* ....or else, the match is real.  */
2084           if (n == MATCH_NO)
2085             gfc_error ("Expected initialization expression at %C");
2086           if (n != MATCH_YES)
2087             return MATCH_ERROR;
2088         }
2089     }
2090
2091   if (e->rank != 0)
2092     {
2093       gfc_error ("Expected scalar initialization expression at %C");
2094       m = MATCH_ERROR;
2095       goto no_match;
2096     }
2097
2098   msg = gfc_extract_int (e, &ts->kind);
2099
2100   if (msg != NULL)
2101     {
2102       gfc_error (msg);
2103       m = MATCH_ERROR;
2104       goto no_match;
2105     }
2106
2107   /* Before throwing away the expression, let's see if we had a
2108      C interoperable kind (and store the fact).  */
2109   if (e->ts.is_c_interop == 1)
2110     {
2111       /* Mark this as c interoperable if being declared with one
2112          of the named constants from iso_c_binding.  */
2113       ts->is_c_interop = e->ts.is_iso_c;
2114       ts->f90_type = e->ts.f90_type;
2115     }
2116   
2117   gfc_free_expr (e);
2118   e = NULL;
2119
2120   /* Ignore errors to this point, if we've gotten here.  This means
2121      we ignore the m=MATCH_ERROR from above.  */
2122   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2123     {
2124       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2125                  gfc_basic_typename (ts->type));
2126       gfc_current_locus = where;
2127       return MATCH_ERROR;
2128     }
2129
2130   /* Warn if, e.g., c_int is used for a REAL variable, but not
2131      if, e.g., c_double is used for COMPLEX as the standard
2132      explicitly says that the kind type parameter for complex and real
2133      variable is the same, i.e. c_float == c_float_complex.  */
2134   if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2135       && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2136            || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2137     gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2138                      "is %s", gfc_basic_typename (ts->f90_type), &where,
2139                      gfc_basic_typename (ts->type));
2140
2141   gfc_gobble_whitespace ();
2142   if ((c = gfc_next_ascii_char ()) != ')'
2143       && (ts->type != BT_CHARACTER || c != ','))
2144     {
2145       if (ts->type == BT_CHARACTER)
2146         gfc_error ("Missing right parenthesis or comma at %C");
2147       else
2148         gfc_error ("Missing right parenthesis at %C");
2149       m = MATCH_ERROR;
2150     }
2151   else
2152      /* All tests passed.  */
2153      m = MATCH_YES;
2154
2155   if(m == MATCH_ERROR)
2156      gfc_current_locus = where;
2157   
2158   /* Return what we know from the test(s).  */
2159   return m;
2160
2161 no_match:
2162   gfc_free_expr (e);
2163   gfc_current_locus = where;
2164   return m;
2165 }
2166
2167
2168 static match
2169 match_char_kind (int * kind, int * is_iso_c)
2170 {
2171   locus where;
2172   gfc_expr *e;
2173   match m, n;
2174   const char *msg;
2175
2176   m = MATCH_NO;
2177   e = NULL;
2178   where = gfc_current_locus;
2179
2180   n = gfc_match_init_expr (&e);
2181
2182   if (n != MATCH_YES && gfc_matching_function)
2183     {
2184       /* The expression might include use-associated or imported
2185          parameters and try again after the specification 
2186          expressions.  */
2187       gfc_free_expr (e);
2188       gfc_undo_symbols ();
2189       return MATCH_YES;
2190     }
2191
2192   if (n == MATCH_NO)
2193     gfc_error ("Expected initialization expression at %C");
2194   if (n != MATCH_YES)
2195     return MATCH_ERROR;
2196
2197   if (e->rank != 0)
2198     {
2199       gfc_error ("Expected scalar initialization expression at %C");
2200       m = MATCH_ERROR;
2201       goto no_match;
2202     }
2203
2204   msg = gfc_extract_int (e, kind);
2205   *is_iso_c = e->ts.is_iso_c;
2206   if (msg != NULL)
2207     {
2208       gfc_error (msg);
2209       m = MATCH_ERROR;
2210       goto no_match;
2211     }
2212
2213   gfc_free_expr (e);
2214
2215   /* Ignore errors to this point, if we've gotten here.  This means
2216      we ignore the m=MATCH_ERROR from above.  */
2217   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2218     {
2219       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2220       m = MATCH_ERROR;
2221     }
2222   else
2223      /* All tests passed.  */
2224      m = MATCH_YES;
2225
2226   if (m == MATCH_ERROR)
2227      gfc_current_locus = where;
2228   
2229   /* Return what we know from the test(s).  */
2230   return m;
2231
2232 no_match:
2233   gfc_free_expr (e);
2234   gfc_current_locus = where;
2235   return m;
2236 }
2237
2238
2239 /* Match the various kind/length specifications in a CHARACTER
2240    declaration.  We don't return MATCH_NO.  */
2241
2242 match
2243 gfc_match_char_spec (gfc_typespec *ts)
2244 {
2245   int kind, seen_length, is_iso_c;
2246   gfc_charlen *cl;
2247   gfc_expr *len;
2248   match m;
2249
2250   len = NULL;
2251   seen_length = 0;
2252   kind = 0;
2253   is_iso_c = 0;
2254
2255   /* Try the old-style specification first.  */
2256   old_char_selector = 0;
2257
2258   m = match_char_length (&len);
2259   if (m != MATCH_NO)
2260     {
2261       if (m == MATCH_YES)
2262         old_char_selector = 1;
2263       seen_length = 1;
2264       goto done;
2265     }
2266
2267   m = gfc_match_char ('(');
2268   if (m != MATCH_YES)
2269     {
2270       m = MATCH_YES;    /* Character without length is a single char.  */
2271       goto done;
2272     }
2273
2274   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
2275   if (gfc_match (" kind =") == MATCH_YES)
2276     {
2277       m = match_char_kind (&kind, &is_iso_c);
2278        
2279       if (m == MATCH_ERROR)
2280         goto done;
2281       if (m == MATCH_NO)
2282         goto syntax;
2283
2284       if (gfc_match (" , len =") == MATCH_NO)
2285         goto rparen;
2286
2287       m = char_len_param_value (&len);
2288       if (m == MATCH_NO)
2289         goto syntax;
2290       if (m == MATCH_ERROR)
2291         goto done;
2292       seen_length = 1;
2293
2294       goto rparen;
2295     }
2296
2297   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
2298   if (gfc_match (" len =") == MATCH_YES)
2299     {
2300       m = char_len_param_value (&len);
2301       if (m == MATCH_NO)
2302         goto syntax;
2303       if (m == MATCH_ERROR)
2304         goto done;
2305       seen_length = 1;
2306
2307       if (gfc_match_char (')') == MATCH_YES)
2308         goto done;
2309
2310       if (gfc_match (" , kind =") != MATCH_YES)
2311         goto syntax;
2312
2313       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2314         goto done;
2315
2316       goto rparen;
2317     }
2318
2319   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
2320   m = char_len_param_value (&len);
2321   if (m == MATCH_NO)
2322     goto syntax;
2323   if (m == MATCH_ERROR)
2324     goto done;
2325   seen_length = 1;
2326
2327   m = gfc_match_char (')');
2328   if (m == MATCH_YES)
2329     goto done;
2330
2331   if (gfc_match_char (',') != MATCH_YES)
2332     goto syntax;
2333
2334   gfc_match (" kind =");        /* Gobble optional text.  */
2335
2336   m = match_char_kind (&kind, &is_iso_c);
2337   if (m == MATCH_ERROR)
2338     goto done;
2339   if (m == MATCH_NO)
2340     goto syntax;
2341
2342 rparen:
2343   /* Require a right-paren at this point.  */
2344   m = gfc_match_char (')');
2345   if (m == MATCH_YES)
2346     goto done;
2347
2348 syntax:
2349   gfc_error ("Syntax error in CHARACTER declaration at %C");
2350   m = MATCH_ERROR;
2351   gfc_free_expr (len);
2352   return m;
2353
2354 done:
2355   /* Deal with character functions after USE and IMPORT statements.  */
2356   if (gfc_matching_function)
2357     {
2358       gfc_free_expr (len);
2359       gfc_undo_symbols ();
2360       return MATCH_YES;
2361     }
2362
2363   if (m != MATCH_YES)
2364     {
2365       gfc_free_expr (len);
2366       return m;
2367     }
2368
2369   /* Do some final massaging of the length values.  */
2370   cl = gfc_new_charlen (gfc_current_ns, NULL);
2371
2372   if (seen_length == 0)
2373     cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2374   else
2375     cl->length = len;
2376
2377   ts->u.cl = cl;
2378   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2379
2380   /* We have to know if it was a c interoperable kind so we can
2381      do accurate type checking of bind(c) procs, etc.  */
2382   if (kind != 0)
2383     /* Mark this as c interoperable if being declared with one
2384        of the named constants from iso_c_binding.  */
2385     ts->is_c_interop = is_iso_c;
2386   else if (len != NULL)
2387     /* Here, we might have parsed something such as: character(c_char)
2388        In this case, the parsing code above grabs the c_char when
2389        looking for the length (line 1690, roughly).  it's the last
2390        testcase for parsing the kind params of a character variable.
2391        However, it's not actually the length.    this seems like it
2392        could be an error.  
2393        To see if the user used a C interop kind, test the expr
2394        of the so called length, and see if it's C interoperable.  */
2395     ts->is_c_interop = len->ts.is_iso_c;
2396   
2397   return MATCH_YES;
2398 }
2399
2400
2401 /* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
2402    structure to the matched specification.  This is necessary for FUNCTION and
2403    IMPLICIT statements.
2404
2405    If implicit_flag is nonzero, then we don't check for the optional
2406    kind specification.  Not doing so is needed for matching an IMPLICIT
2407    statement correctly.  */
2408
2409 match
2410 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2411 {
2412   char name[GFC_MAX_SYMBOL_LEN + 1];
2413   gfc_symbol *sym;
2414   match m;
2415   char c;
2416   bool seen_deferred_kind, matched_type;
2417
2418   /* A belt and braces check that the typespec is correctly being treated
2419      as a deferred characteristic association.  */
2420   seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2421                           && (gfc_current_block ()->result->ts.kind == -1)
2422                           && (ts->kind == -1);
2423   gfc_clear_ts (ts);
2424   if (seen_deferred_kind)
2425     ts->kind = -1;
2426
2427   /* Clear the current binding label, in case one is given.  */
2428   curr_binding_label[0] = '\0';
2429
2430   if (gfc_match (" byte") == MATCH_YES)
2431     {
2432       if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2433           == FAILURE)
2434         return MATCH_ERROR;
2435
2436       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2437         {
2438           gfc_error ("BYTE type used at %C "
2439                      "is not available on the target machine");
2440           return MATCH_ERROR;
2441         }
2442
2443       ts->type = BT_INTEGER;
2444       ts->kind = 1;
2445       return MATCH_YES;
2446     }
2447
2448
2449   m = gfc_match (" type ( %n", name);
2450   matched_type = (m == MATCH_YES);
2451   
2452   if ((matched_type && strcmp ("integer", name) == 0)
2453       || (!matched_type && gfc_match (" integer") == MATCH_YES))
2454     {
2455       ts->type = BT_INTEGER;
2456       ts->kind = gfc_default_integer_kind;
2457       goto get_kind;
2458     }
2459
2460   if ((matched_type && strcmp ("character", name) == 0)
2461       || (!matched_type && gfc_match (" character") == MATCH_YES))
2462     {
2463       if (matched_type
2464           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2465                           "intrinsic-type-spec at %C") == FAILURE)
2466         return MATCH_ERROR;
2467
2468       ts->type = BT_CHARACTER;
2469       if (implicit_flag == 0)
2470         m = gfc_match_char_spec (ts);
2471       else
2472         m = MATCH_YES;
2473
2474       if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2475         m = MATCH_ERROR;
2476
2477       return m;
2478     }
2479
2480   if ((matched_type && strcmp ("real", name) == 0)
2481       || (!matched_type && gfc_match (" real") == MATCH_YES))
2482     {
2483       ts->type = BT_REAL;
2484       ts->kind = gfc_default_real_kind;
2485       goto get_kind;
2486     }
2487
2488   if ((matched_type
2489        && (strcmp ("doubleprecision", name) == 0
2490            || (strcmp ("double", name) == 0
2491                && gfc_match (" precision") == MATCH_YES)))
2492       || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2493     {
2494       if (matched_type
2495           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2496                           "intrinsic-type-spec at %C") == FAILURE)
2497         return MATCH_ERROR;
2498       if (matched_type && gfc_match_char (')') != MATCH_YES)
2499         return MATCH_ERROR;
2500
2501       ts->type = BT_REAL;
2502       ts->kind = gfc_default_double_kind;
2503       return MATCH_YES;
2504     }
2505
2506   if ((matched_type && strcmp ("complex", name) == 0)
2507       || (!matched_type && gfc_match (" complex") == MATCH_YES))
2508     {
2509       ts->type = BT_COMPLEX;
2510       ts->kind = gfc_default_complex_kind;
2511       goto get_kind;
2512     }
2513
2514   if ((matched_type
2515        && (strcmp ("doublecomplex", name) == 0
2516            || (strcmp ("double", name) == 0
2517                && gfc_match (" complex") == MATCH_YES)))
2518       || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2519     {
2520       if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2521           == FAILURE)
2522         return MATCH_ERROR;
2523
2524       if (matched_type
2525           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2526                           "intrinsic-type-spec at %C") == FAILURE)
2527         return MATCH_ERROR;
2528
2529       if (matched_type && gfc_match_char (')') != MATCH_YES)
2530         return MATCH_ERROR;
2531
2532       ts->type = BT_COMPLEX;
2533       ts->kind = gfc_default_double_kind;
2534       return MATCH_YES;
2535     }
2536
2537   if ((matched_type && strcmp ("logical", name) == 0)
2538       || (!matched_type && gfc_match (" logical") == MATCH_YES))
2539     {
2540       ts->type = BT_LOGICAL;
2541       ts->kind = gfc_default_logical_kind;
2542       goto get_kind;
2543     }
2544
2545   if (matched_type)
2546     m = gfc_match_char (')');
2547
2548   if (m == MATCH_YES)
2549     ts->type = BT_DERIVED;
2550   else
2551     {
2552       m = gfc_match (" class ( %n )", name);
2553       if (m != MATCH_YES)
2554         return m;
2555       ts->type = BT_CLASS;
2556
2557       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2558                           == FAILURE)
2559         return MATCH_ERROR;
2560     }
2561
2562   /* Defer association of the derived type until the end of the
2563      specification block.  However, if the derived type can be
2564      found, add it to the typespec.  */  
2565   if (gfc_matching_function)
2566     {
2567       ts->u.derived = NULL;
2568       if (gfc_current_state () != COMP_INTERFACE
2569             && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2570         ts->u.derived = sym;
2571       return MATCH_YES;
2572     }
2573
2574   /* Search for the name but allow the components to be defined later.  If
2575      type = -1, this typespec has been seen in a function declaration but
2576      the type could not be accessed at that point.  */
2577   sym = NULL;
2578   if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2579     {
2580       gfc_error ("Type name '%s' at %C is ambiguous", name);
2581       return MATCH_ERROR;
2582     }
2583   else if (ts->kind == -1)
2584     {
2585       int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2586                     || gfc_current_ns->has_import_set;
2587       if (gfc_find_symbol (name, NULL, iface, &sym))
2588         {       
2589           gfc_error ("Type name '%s' at %C is ambiguous", name);
2590           return MATCH_ERROR;
2591         }
2592
2593       ts->kind = 0;
2594       if (sym == NULL)
2595         return MATCH_NO;
2596     }
2597
2598   if (sym->attr.flavor != FL_DERIVED
2599       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2600     return MATCH_ERROR;
2601
2602   gfc_set_sym_referenced (sym);
2603   ts->u.derived = sym;
2604
2605   return MATCH_YES;
2606
2607 get_kind:
2608   if (matched_type
2609       && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2610                          "intrinsic-type-spec at %C") == FAILURE)
2611     return MATCH_ERROR;
2612
2613   /* For all types except double, derived and character, look for an
2614      optional kind specifier.  MATCH_NO is actually OK at this point.  */
2615   if (implicit_flag == 1)
2616     {
2617         if (matched_type && gfc_match_char (')') != MATCH_YES)
2618           return MATCH_ERROR;
2619
2620         return MATCH_YES;
2621     }
2622
2623   if (gfc_current_form == FORM_FREE)
2624     {
2625       c = gfc_peek_ascii_char ();
2626       if (!gfc_is_whitespace (c) && c != '*' && c != '('
2627           && c != ':' && c != ',')
2628         {
2629           if (matched_type && c == ')')
2630             {
2631               gfc_next_ascii_char ();
2632               return MATCH_YES;
2633             }
2634           return MATCH_NO;
2635         }
2636     }
2637
2638   m = gfc_match_kind_spec (ts, false);
2639   if (m == MATCH_NO && ts->type != BT_CHARACTER)
2640     m = gfc_match_old_kind_spec (ts);
2641
2642   if (matched_type && gfc_match_char (')') != MATCH_YES)
2643     return MATCH_ERROR;
2644
2645   /* Defer association of the KIND expression of function results
2646      until after USE and IMPORT statements.  */
2647   if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2648          || gfc_matching_function)
2649     return MATCH_YES;
2650
2651   if (m == MATCH_NO)
2652     m = MATCH_YES;              /* No kind specifier found.  */
2653
2654   return m;
2655 }
2656
2657
2658 /* Match an IMPLICIT NONE statement.  Actually, this statement is
2659    already matched in parse.c, or we would not end up here in the
2660    first place.  So the only thing we need to check, is if there is
2661    trailing garbage.  If not, the match is successful.  */
2662
2663 match
2664 gfc_match_implicit_none (void)
2665 {
2666   return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2667 }
2668
2669
2670 /* Match the letter range(s) of an IMPLICIT statement.  */
2671
2672 static match
2673 match_implicit_range (void)
2674 {
2675   char c, c1, c2;
2676   int inner;
2677   locus cur_loc;
2678
2679   cur_loc = gfc_current_locus;
2680
2681   gfc_gobble_whitespace ();
2682   c = gfc_next_ascii_char ();
2683   if (c != '(')
2684     {
2685       gfc_error ("Missing character range in IMPLICIT at %C");
2686       goto bad;
2687     }
2688
2689   inner = 1;
2690   while (inner)
2691     {
2692       gfc_gobble_whitespace ();
2693       c1 = gfc_next_ascii_char ();
2694       if (!ISALPHA (c1))
2695         goto bad;
2696
2697       gfc_gobble_whitespace ();
2698       c = gfc_next_ascii_char ();
2699
2700       switch (c)
2701         {
2702         case ')':
2703           inner = 0;            /* Fall through.  */
2704
2705         case ',':
2706           c2 = c1;
2707           break;
2708
2709         case '-':
2710           gfc_gobble_whitespace ();
2711           c2 = gfc_next_ascii_char ();
2712           if (!ISALPHA (c2))
2713             goto bad;
2714
2715           gfc_gobble_whitespace ();
2716           c = gfc_next_ascii_char ();
2717
2718           if ((c != ',') && (c != ')'))
2719             goto bad;
2720           if (c == ')')
2721             inner = 0;
2722
2723           break;
2724
2725         default:
2726           goto bad;
2727         }
2728
2729       if (c1 > c2)
2730         {
2731           gfc_error ("Letters must be in alphabetic order in "
2732                      "IMPLICIT statement at %C");
2733           goto bad;
2734         }
2735
2736       /* See if we can add the newly matched range to the pending
2737          implicits from this IMPLICIT statement.  We do not check for
2738          conflicts with whatever earlier IMPLICIT statements may have
2739          set.  This is done when we've successfully finished matching
2740          the current one.  */
2741       if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2742         goto bad;
2743     }
2744
2745   return MATCH_YES;
2746
2747 bad:
2748   gfc_syntax_error (ST_IMPLICIT);
2749
2750   gfc_current_locus = cur_loc;
2751   return MATCH_ERROR;
2752 }
2753
2754
2755 /* Match an IMPLICIT statement, storing the types for
2756    gfc_set_implicit() if the statement is accepted by the parser.
2757    There is a strange looking, but legal syntactic construction
2758    possible.  It looks like:
2759
2760      IMPLICIT INTEGER (a-b) (c-d)
2761
2762    This is legal if "a-b" is a constant expression that happens to
2763    equal one of the legal kinds for integers.  The real problem
2764    happens with an implicit specification that looks like:
2765
2766      IMPLICIT INTEGER (a-b)
2767
2768    In this case, a typespec matcher that is "greedy" (as most of the
2769    matchers are) gobbles the character range as a kindspec, leaving
2770    nothing left.  We therefore have to go a bit more slowly in the
2771    matching process by inhibiting the kindspec checking during
2772    typespec matching and checking for a kind later.  */
2773
2774 match
2775 gfc_match_implicit (void)
2776 {
2777   gfc_typespec ts;
2778   locus cur_loc;
2779   char c;
2780   match m;
2781
2782   gfc_clear_ts (&ts);
2783
2784   /* We don't allow empty implicit statements.  */
2785   if (gfc_match_eos () == MATCH_YES)
2786     {
2787       gfc_error ("Empty IMPLICIT statement at %C");
2788       return MATCH_ERROR;
2789     }
2790
2791   do
2792     {
2793       /* First cleanup.  */
2794       gfc_clear_new_implicit ();
2795
2796       /* A basic type is mandatory here.  */
2797       m = gfc_match_decl_type_spec (&ts, 1);
2798       if (m == MATCH_ERROR)
2799         goto error;
2800       if (m == MATCH_NO)
2801         goto syntax;
2802
2803       cur_loc = gfc_current_locus;
2804       m = match_implicit_range ();
2805
2806       if (m == MATCH_YES)
2807         {
2808           /* We may have <TYPE> (<RANGE>).  */
2809           gfc_gobble_whitespace ();
2810           c = gfc_next_ascii_char ();
2811           if ((c == '\n') || (c == ','))
2812             {
2813               /* Check for CHARACTER with no length parameter.  */
2814               if (ts.type == BT_CHARACTER && !ts.u.cl)
2815                 {
2816                   ts.kind = gfc_default_character_kind;
2817                   ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2818                   ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2819                                                       NULL, 1);
2820                 }
2821
2822               /* Record the Successful match.  */
2823               if (gfc_merge_new_implicit (&ts) != SUCCESS)
2824                 return MATCH_ERROR;
2825               continue;
2826             }
2827
2828           gfc_current_locus = cur_loc;
2829         }
2830
2831       /* Discard the (incorrectly) matched range.  */
2832       gfc_clear_new_implicit ();
2833
2834       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
2835       if (ts.type == BT_CHARACTER)
2836         m = gfc_match_char_spec (&ts);
2837       else
2838         {
2839           m = gfc_match_kind_spec (&ts, false);
2840           if (m == MATCH_NO)
2841             {
2842               m = gfc_match_old_kind_spec (&ts);
2843               if (m == MATCH_ERROR)
2844                 goto error;
2845               if (m == MATCH_NO)
2846                 goto syntax;
2847             }
2848         }
2849       if (m == MATCH_ERROR)
2850         goto error;
2851
2852       m = match_implicit_range ();
2853       if (m == MATCH_ERROR)
2854         goto error;
2855       if (m == MATCH_NO)
2856         goto syntax;
2857
2858       gfc_gobble_whitespace ();
2859       c = gfc_next_ascii_char ();
2860       if ((c != '\n') && (c != ','))
2861         goto syntax;
2862
2863       if (gfc_merge_new_implicit (&ts) != SUCCESS)
2864         return MATCH_ERROR;
2865     }
2866   while (c == ',');
2867
2868   return MATCH_YES;
2869
2870 syntax:
2871   gfc_syntax_error (ST_IMPLICIT);
2872
2873 error:
2874   return MATCH_ERROR;
2875 }
2876
2877
2878 match
2879 gfc_match_import (void)
2880 {
2881   char name[GFC_MAX_SYMBOL_LEN + 1];
2882   match m;
2883   gfc_symbol *sym;
2884   gfc_symtree *st;
2885
2886   if (gfc_current_ns->proc_name == NULL
2887       || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2888     {
2889       gfc_error ("IMPORT statement at %C only permitted in "
2890                  "an INTERFACE body");
2891       return MATCH_ERROR;
2892     }
2893
2894   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2895       == FAILURE)
2896     return MATCH_ERROR;
2897
2898   if (gfc_match_eos () == MATCH_YES)
2899     {
2900       /* All host variables should be imported.  */
2901       gfc_current_ns->has_import_set = 1;
2902       return MATCH_YES;
2903     }
2904
2905   if (gfc_match (" ::") == MATCH_YES)
2906     {
2907       if (gfc_match_eos () == MATCH_YES)
2908         {
2909            gfc_error ("Expecting list of named entities at %C");
2910            return MATCH_ERROR;
2911         }
2912     }
2913
2914   for(;;)
2915     {
2916       m = gfc_match (" %n", name);
2917       switch (m)
2918         {
2919         case MATCH_YES:
2920           if (gfc_current_ns->parent !=  NULL
2921               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2922             {
2923                gfc_error ("Type name '%s' at %C is ambiguous", name);
2924                return MATCH_ERROR;
2925             }
2926           else if (gfc_current_ns->proc_name->ns->parent !=  NULL
2927                    && gfc_find_symbol (name,
2928                                        gfc_current_ns->proc_name->ns->parent,
2929                                        1, &sym))
2930             {
2931                gfc_error ("Type name '%s' at %C is ambiguous", name);
2932                return MATCH_ERROR;
2933             }
2934
2935           if (sym == NULL)
2936             {
2937               gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2938                          "at %C - does not exist.", name);
2939               return MATCH_ERROR;
2940             }
2941
2942           if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2943             {
2944               gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2945                            "at %C.", name);
2946               goto next_item;
2947             }
2948
2949           st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2950           st->n.sym = sym;
2951           sym->refs++;
2952           sym->attr.imported = 1;
2953
2954           goto next_item;
2955
2956         case MATCH_NO:
2957           break;
2958
2959         case MATCH_ERROR:
2960           return MATCH_ERROR;
2961         }
2962
2963     next_item:
2964       if (gfc_match_eos () == MATCH_YES)
2965         break;
2966       if (gfc_match_char (',') != MATCH_YES)
2967         goto syntax;
2968     }
2969
2970   return MATCH_YES;
2971
2972 syntax:
2973   gfc_error ("Syntax error in IMPORT statement at %C");
2974   return MATCH_ERROR;
2975 }
2976
2977
2978 /* A minimal implementation of gfc_match without whitespace, escape
2979    characters or variable arguments.  Returns true if the next
2980    characters match the TARGET template exactly.  */
2981
2982 static bool
2983 match_string_p (const char *target)
2984 {
2985   const char *p;
2986
2987   for (p = target; *p; p++)
2988     if ((char) gfc_next_ascii_char () != *p)
2989       return false;
2990   return true;
2991 }
2992
2993 /* Matches an attribute specification including array specs.  If
2994    successful, leaves the variables current_attr and current_as
2995    holding the specification.  Also sets the colon_seen variable for
2996    later use by matchers associated with initializations.
2997
2998    This subroutine is a little tricky in the sense that we don't know
2999    if we really have an attr-spec until we hit the double colon.
3000    Until that time, we can only return MATCH_NO.  This forces us to
3001    check for duplicate specification at this level.  */
3002
3003 static match
3004 match_attr_spec (void)
3005 {
3006   /* Modifiers that can exist in a type statement.  */
3007   typedef enum
3008   { GFC_DECL_BEGIN = 0,
3009     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3010     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3011     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3012     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3013     DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3014     DECL_NONE, GFC_DECL_END /* Sentinel */
3015   }
3016   decl_types;
3017
3018 /* GFC_DECL_END is the sentinel, index starts at 0.  */
3019 #define NUM_DECL GFC_DECL_END
3020
3021   locus start, seen_at[NUM_DECL];
3022   int seen[NUM_DECL];
3023   unsigned int d;
3024   const char *attr;
3025   match m;
3026   gfc_try t;
3027
3028   gfc_clear_attr (&current_attr);
3029   start = gfc_current_locus;
3030
3031   current_as = NULL;
3032   colon_seen = 0;
3033
3034   /* See if we get all of the keywords up to the final double colon.  */
3035   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3036     seen[d] = 0;
3037
3038   for (;;)
3039     {
3040       char ch;
3041
3042       d = DECL_NONE;
3043       gfc_gobble_whitespace ();
3044
3045       ch = gfc_next_ascii_char ();
3046       if (ch == ':')
3047         {
3048           /* This is the successful exit condition for the loop.  */
3049           if (gfc_next_ascii_char () == ':')
3050             break;
3051         }
3052       else if (ch == ',')
3053         {
3054           gfc_gobble_whitespace ();
3055           switch (gfc_peek_ascii_char ())
3056             {
3057             case 'a':
3058               gfc_next_ascii_char ();
3059               switch (gfc_next_ascii_char ())
3060                 {
3061                 case 'l':
3062                   if (match_string_p ("locatable"))
3063                     {
3064                       /* Matched "allocatable".  */
3065                       d = DECL_ALLOCATABLE;
3066                     }
3067                   break;
3068
3069                 case 's':
3070                   if (match_string_p ("ynchronous"))
3071                     {
3072                       /* Matched "asynchronous".  */
3073                       d = DECL_ASYNCHRONOUS;
3074                     }
3075                   break;
3076                 }
3077               break;
3078
3079             case 'b':
3080               /* Try and match the bind(c).  */
3081               m = gfc_match_bind_c (NULL, true);
3082               if (m == MATCH_YES)
3083                 d = DECL_IS_BIND_C;
3084               else if (m == MATCH_ERROR)
3085                 goto cleanup;
3086               break;
3087
3088             case 'c':
3089               gfc_next_ascii_char ();
3090               if ('o' != gfc_next_ascii_char ())
3091                 break;
3092               switch (gfc_next_ascii_char ())
3093                 {
3094                 case 'd':
3095                   if (match_string_p ("imension"))
3096                     {
3097                       d = DECL_CODIMENSION;
3098                       break;
3099                     }
3100                 case 'n':
3101                   if (match_string_p ("tiguous"))
3102                     {
3103                       d = DECL_CONTIGUOUS;
3104                       break;
3105                     }
3106                 }
3107               break;
3108
3109             case 'd':
3110               if (match_string_p ("dimension"))
3111                 d = DECL_DIMENSION;
3112               break;
3113
3114             case 'e':
3115               if (match_string_p ("external"))
3116                 d = DECL_EXTERNAL;
3117               break;
3118
3119             case 'i':
3120               if (match_string_p ("int"))
3121                 {
3122                   ch = gfc_next_ascii_char ();
3123                   if (ch == 'e')
3124                     {
3125                       if (match_string_p ("nt"))
3126                         {
3127                           /* Matched "intent".  */
3128                           /* TODO: Call match_intent_spec from here.  */
3129                           if (gfc_match (" ( in out )") == MATCH_YES)
3130                             d = DECL_INOUT;
3131                           else if (gfc_match (" ( in )") == MATCH_YES)
3132                             d = DECL_IN;
3133                           else if (gfc_match (" ( out )") == MATCH_YES)
3134                             d = DECL_OUT;
3135                         }
3136                     }
3137                   else if (ch == 'r')
3138                     {
3139                       if (match_string_p ("insic"))
3140                         {
3141                           /* Matched "intrinsic".  */
3142                           d = DECL_INTRINSIC;
3143                         }
3144                     }
3145                 }
3146               break;
3147
3148             case 'o':
3149               if (match_string_p ("optional"))
3150                 d = DECL_OPTIONAL;
3151               break;
3152
3153             case 'p':
3154               gfc_next_ascii_char ();
3155               switch (gfc_next_ascii_char ())
3156                 {
3157                 case 'a':
3158                   if (match_string_p ("rameter"))
3159                     {
3160                       /* Matched "parameter".  */
3161                       d = DECL_PARAMETER;
3162                     }
3163                   break;
3164
3165                 case 'o':
3166                   if (match_string_p ("inter"))
3167                     {
3168                       /* Matched "pointer".  */
3169                       d = DECL_POINTER;
3170                     }
3171                   break;
3172
3173                 case 'r':
3174                   ch = gfc_next_ascii_char ();
3175                   if (ch == 'i')
3176                     {
3177                       if (match_string_p ("vate"))
3178                         {
3179                           /* Matched "private".  */
3180                           d = DECL_PRIVATE;
3181                         }
3182                     }
3183                   else if (ch == 'o')
3184                     {
3185                       if (match_string_p ("tected"))
3186                         {
3187                           /* Matched "protected".  */
3188                           d = DECL_PROTECTED;
3189                         }
3190                     }
3191                   break;
3192
3193                 case 'u':
3194                   if (match_string_p ("blic"))
3195                     {
3196                       /* Matched "public".  */
3197                       d = DECL_PUBLIC;
3198                     }
3199                   break;
3200                 }
3201               break;
3202
3203             case 's':
3204               if (match_string_p ("save"))
3205                 d = DECL_SAVE;
3206               break;
3207
3208             case 't':
3209               if (match_string_p ("target"))
3210                 d = DECL_TARGET;
3211               break;
3212
3213             case 'v':
3214               gfc_next_ascii_char ();
3215               ch = gfc_next_ascii_char ();
3216               if (ch == 'a')
3217                 {
3218                   if (match_string_p ("lue"))
3219                     {
3220                       /* Matched "value".  */
3221                       d = DECL_VALUE;
3222                     }
3223                 }
3224               else if (ch == 'o')
3225                 {
3226                   if (match_string_p ("latile"))
3227                     {
3228                       /* Matched "volatile".  */
3229                       d = DECL_VOLATILE;
3230                     }
3231                 }
3232               break;
3233             }
3234         }
3235
3236       /* No double colon and no recognizable decl_type, so assume that
3237          we've been looking at something else the whole time.  */
3238       if (d == DECL_NONE)
3239         {
3240           m = MATCH_NO;
3241           goto cleanup;
3242         }
3243
3244       /* Check to make sure any parens are paired up correctly.  */
3245       if (gfc_match_parens () == MATCH_ERROR)
3246         {
3247           m = MATCH_ERROR;
3248           goto cleanup;
3249         }
3250
3251       seen[d]++;
3252       seen_at[d] = gfc_current_locus;
3253
3254       if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3255         {
3256           gfc_array_spec *as = NULL;
3257
3258           m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3259                                     d == DECL_CODIMENSION);
3260
3261           if (current_as == NULL)
3262             current_as = as;
3263           else if (m == MATCH_YES)
3264             {
3265               merge_array_spec (as, current_as, false);
3266               gfc_free (as);
3267             }
3268
3269           if (m == MATCH_NO)
3270             {
3271               if (d == DECL_CODIMENSION)
3272                 gfc_error ("Missing codimension specification at %C");
3273               else
3274                 gfc_error ("Missing dimension specification at %C");
3275               m = MATCH_ERROR;
3276             }
3277
3278           if (m == MATCH_ERROR)
3279             goto cleanup;
3280         }
3281     }
3282
3283   /* Since we've seen a double colon, we have to be looking at an
3284      attr-spec.  This means that we can now issue errors.  */
3285   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3286     if (seen[d] > 1)
3287       {
3288         switch (d)
3289           {
3290           case DECL_ALLOCATABLE:
3291             attr = "ALLOCATABLE";
3292             break;
3293           case DECL_ASYNCHRONOUS:
3294             attr = "ASYNCHRONOUS";
3295             break;
3296           case DECL_CODIMENSION:
3297             attr = "CODIMENSION";
3298             break;
3299           case DECL_CONTIGUOUS:
3300             attr = "CONTIGUOUS";
3301             break;
3302           case DECL_DIMENSION:
3303             attr = "DIMENSION";
3304             break;
3305           case DECL_EXTERNAL:
3306             attr = "EXTERNAL";
3307             break;
3308           case DECL_IN:
3309             attr = "INTENT (IN)";
3310             break;
3311           case DECL_OUT:
3312             attr = "INTENT (OUT)";
3313             break;
3314           case DECL_INOUT:
3315             attr = "INTENT (IN OUT)";
3316             break;
3317           case DECL_INTRINSIC:
3318             attr = "INTRINSIC";
3319             break;
3320           case DECL_OPTIONAL:
3321             attr = "OPTIONAL";
3322             break;
3323           case DECL_PARAMETER:
3324             attr = "PARAMETER";
3325             break;
3326           case DECL_POINTER:
3327             attr = "POINTER";
3328             break;
3329           case DECL_PROTECTED:
3330             attr = "PROTECTED";
3331             break;
3332           case DECL_PRIVATE:
3333             attr = "PRIVATE";
3334             break;
3335           case DECL_PUBLIC:
3336             attr = "PUBLIC";
3337             break;
3338           case DECL_SAVE:
3339             attr = "SAVE";
3340             break;
3341           case DECL_TARGET:
3342             attr = "TARGET";
3343             break;
3344           case DECL_IS_BIND_C:
3345             attr = "IS_BIND_C";
3346             break;
3347           case DECL_VALUE:
3348             attr = "VALUE";
3349             break;
3350           case DECL_VOLATILE:
3351             attr = "VOLATILE";
3352             break;
3353           default:
3354             attr = NULL;        /* This shouldn't happen.  */
3355           }
3356
3357         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3358         m = MATCH_ERROR;
3359         goto cleanup;
3360       }
3361
3362   /* Now that we've dealt with duplicate attributes, add the attributes
3363      to the current attribute.  */
3364   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3365     {
3366       if (seen[d] == 0)
3367         continue;
3368
3369       if (gfc_current_state () == COMP_DERIVED
3370           && d != DECL_DIMENSION && d != DECL_CODIMENSION
3371           && d != DECL_POINTER   && d != DECL_PRIVATE
3372           && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3373         {
3374           if (d == DECL_ALLOCATABLE)
3375             {
3376               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3377                                   "attribute at %C in a TYPE definition")
3378                   == FAILURE)
3379                 {
3380                   m = MATCH_ERROR;
3381                   goto cleanup;
3382                 }
3383             }
3384           else
3385             {
3386               gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3387                          &seen_at[d]);
3388               m = MATCH_ERROR;
3389               goto cleanup;
3390             }
3391         }
3392
3393       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3394           && gfc_current_state () != COMP_MODULE)
3395         {
3396           if (d == DECL_PRIVATE)
3397             attr = "PRIVATE";
3398           else
3399             attr = "PUBLIC";
3400           if (gfc_current_state () == COMP_DERIVED
3401               && gfc_state_stack->previous
3402               && gfc_state_stack->previous->state == COMP_MODULE)
3403             {
3404               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3405                                   "at %L in a TYPE definition", attr,
3406                                   &seen_at[d])
3407                   == FAILURE)
3408                 {
3409                   m = MATCH_ERROR;
3410                   goto cleanup;
3411                 }
3412             }
3413           else
3414             {
3415               gfc_error ("%s attribute at %L is not allowed outside of the "
3416                          "specification part of a module", attr, &seen_at[d]);
3417               m = MATCH_ERROR;
3418               goto cleanup;
3419             }
3420         }
3421
3422       switch (d)
3423         {
3424         case DECL_ALLOCATABLE:
3425           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3426           break;
3427
3428         case DECL_ASYNCHRONOUS:
3429           if (gfc_notify_std (GFC_STD_F2003,
3430                               "Fortran 2003: ASYNCHRONOUS attribute at %C")
3431               == FAILURE)
3432             t = FAILURE;
3433           else
3434             t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3435           break;
3436
3437         case DECL_CODIMENSION:
3438           t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3439           break;
3440
3441         case DECL_CONTIGUOUS:
3442           if (gfc_notify_std (GFC_STD_F2008,
3443                               "Fortran 2008: CONTIGUOUS attribute at %C")
3444               == FAILURE)
3445             t = FAILURE;
3446           else
3447             t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3448           break;
3449
3450         case DECL_DIMENSION:
3451           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3452           break;
3453
3454         case DECL_EXTERNAL:
3455           t = gfc_add_external (&current_attr, &seen_at[d]);
3456           break;
3457
3458         case DECL_IN:
3459           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3460           break;
3461
3462         case DECL_OUT:
3463           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3464           break;
3465
3466         case DECL_INOUT:
3467           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3468           break;
3469
3470         case DECL_INTRINSIC:
3471           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3472           break;
3473
3474         case DECL_OPTIONAL:
3475           t = gfc_add_optional (&current_attr, &seen_at[d]);
3476           break;
3477
3478         case DECL_PARAMETER:
3479           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3480           break;
3481
3482         case DECL_POINTER:
3483           t = gfc_add_pointer (&current_attr, &seen_at[d]);
3484           break;
3485
3486         case DECL_PROTECTED:
3487           if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3488             {
3489                gfc_error ("PROTECTED at %C only allowed in specification "
3490                           "part of a module");
3491                t = FAILURE;
3492                break;
3493             }
3494
3495           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3496                               "attribute at %C")
3497               == FAILURE)
3498             t = FAILURE;
3499           else
3500             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3501           break;
3502
3503         case DECL_PRIVATE:
3504           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3505                               &seen_at[d]);
3506           break;
3507
3508         case DECL_PUBLIC:
3509           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3510                               &seen_at[d]);
3511           break;
3512
3513         case DECL_SAVE:
3514           t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
3515           break;
3516
3517         case DECL_TARGET:
3518           t = gfc_add_target (&current_attr, &seen_at[d]);
3519           break;
3520
3521         case DECL_IS_BIND_C:
3522            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3523            break;
3524            
3525         case DECL_VALUE:
3526           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3527                               "at %C")
3528               == FAILURE)
3529             t = FAILURE;
3530           else
3531             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3532           break;
3533
3534         case DECL_VOLATILE:
3535           if (gfc_notify_std (GFC_STD_F2003,
3536                               "Fortran 2003: VOLATILE attribute at %C")
3537               == FAILURE)
3538             t = FAILURE;
3539           else
3540             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3541           break;
3542
3543         default:
3544           gfc_internal_error ("match_attr_spec(): Bad attribute");
3545         }
3546
3547       if (t == FAILURE)
3548         {
3549           m = MATCH_ERROR;
3550           goto cleanup;
3551         }
3552     }
3553
3554   colon_seen = 1;
3555   return MATCH_YES;
3556
3557 cleanup:
3558   gfc_current_locus = start;
3559   gfc_free_array_spec (current_as);
3560   current_as = NULL;
3561   return m;
3562 }
3563
3564
3565 /* Set the binding label, dest_label, either with the binding label
3566    stored in the given gfc_typespec, ts, or if none was provided, it
3567    will be the symbol name in all lower case, as required by the draft
3568    (J3/04-007, section 15.4.1).  If a binding label was given and
3569    there is more than one argument (num_idents), it is an error.  */
3570
3571 gfc_try
3572 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3573 {
3574   if (num_idents > 1 && has_name_equals)
3575     {
3576       gfc_error ("Multiple identifiers provided with "
3577                  "single NAME= specifier at %C");
3578       return FAILURE;
3579     }
3580
3581   if (curr_binding_label[0] != '\0')
3582     {
3583       /* Binding label given; store in temp holder til have sym.  */
3584       strcpy (dest_label, curr_binding_label);
3585     }
3586   else
3587     {
3588       /* No binding label given, and the NAME= specifier did not exist,
3589          which means there was no NAME="".  */
3590       if (sym_name != NULL && has_name_equals == 0)
3591         strcpy (dest_label, sym_name);
3592     }
3593    
3594   return SUCCESS;
3595 }
3596
3597
3598 /* Set the status of the given common block as being BIND(C) or not,
3599    depending on the given parameter, is_bind_c.  */
3600
3601 void
3602 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3603 {
3604   com_block->is_bind_c = is_bind_c;
3605   return;
3606 }
3607
3608
3609 /* Verify that the given gfc_typespec is for a C interoperable type.  */
3610
3611 gfc_try
3612 verify_c_interop (gfc_typespec *ts)
3613 {
3614   if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3615     return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3616            ? SUCCESS : FAILURE;
3617   else if (ts->is_c_interop != 1)
3618     return FAILURE;
3619   
3620   return SUCCESS;
3621 }
3622
3623
3624 /* Verify that the variables of a given common block, which has been
3625    defined with the attribute specifier bind(c), to be of a C
3626    interoperable type.  Errors will be reported here, if
3627    encountered.  */
3628
3629 gfc_try
3630 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3631 {
3632   gfc_symbol *curr_sym = NULL;
3633   gfc_try retval = SUCCESS;
3634
3635   curr_sym = com_block->head;
3636   
3637   /* Make sure we have at least one symbol.  */
3638   if (curr_sym == NULL)
3639     return retval;
3640
3641   /* Here we know we have a symbol, so we'll execute this loop
3642      at least once.  */
3643   do
3644     {
3645       /* The second to last param, 1, says this is in a common block.  */
3646       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3647       curr_sym = curr_sym->common_next;
3648     } while (curr_sym != NULL); 
3649
3650   return retval;
3651 }
3652
3653
3654 /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
3655    an appropriate error message is reported.  */
3656
3657 gfc_try
3658 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3659                    int is_in_common, gfc_common_head *com_block)
3660 {
3661   bool bind_c_function = false;
3662   gfc_try retval = SUCCESS;
3663
3664   if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3665     bind_c_function = true;
3666
3667   if (tmp_sym->attr.function && tmp_sym->result != NULL)
3668     {
3669       tmp_sym = tmp_sym->result;
3670       /* Make sure it wasn't an implicitly typed result.  */
3671       if (tmp_sym->attr.implicit_type)
3672         {
3673           gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3674                        "%L may not be C interoperable", tmp_sym->name,
3675                        &tmp_sym->declared_at);
3676           tmp_sym->ts.f90_type = tmp_sym->ts.type;
3677           /* Mark it as C interoperable to prevent duplicate warnings.  */
3678           tmp_sym->ts.is_c_interop = 1;
3679           tmp_sym->attr.is_c_interop = 1;
3680         }
3681     }
3682
3683   /* Here, we know we have the bind(c) attribute, so if we have
3684      enough type info, then verify that it's a C interop kind.
3685      The info could be in the symbol already, or possibly still in
3686      the given ts (current_ts), so look in both.  */
3687   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN) 
3688     {
3689       if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3690         {
3691           /* See if we're dealing with a sym in a common block or not.  */
3692           if (is_in_common == 1)
3693             {
3694               gfc_warning ("Variable '%s' in common block '%s' at %L "
3695                            "may not be a C interoperable "
3696                            "kind though common block '%s' is BIND(C)",
3697                            tmp_sym->name, com_block->name,
3698                            &(tmp_sym->declared_at), com_block->name);
3699             }
3700           else
3701             {
3702               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3703                 gfc_error ("Type declaration '%s' at %L is not C "
3704                            "interoperable but it is BIND(C)",
3705                            tmp_sym->name, &(tmp_sym->declared_at));
3706               else
3707                 gfc_warning ("Variable '%s' at %L "
3708                              "may not be a C interoperable "
3709                              "kind but it is bind(c)",
3710                              tmp_sym->name, &(tmp_sym->declared_at));
3711             }
3712         }
3713       
3714       /* Variables declared w/in a common block can't be bind(c)
3715          since there's no way for C to see these variables, so there's
3716          semantically no reason for the attribute.  */
3717       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3718         {
3719           gfc_error ("Variable '%s' in common block '%s' at "
3720                      "%L cannot be declared with BIND(C) "
3721                      "since it is not a global",
3722                      tmp_sym->name, com_block->name,
3723                      &(tmp_sym->declared_at));
3724           retval = FAILURE;
3725         }
3726       
3727       /* Scalar variables that are bind(c) can not have the pointer
3728          or allocatable attributes.  */
3729       if (tmp_sym->attr.is_bind_c == 1)
3730         {
3731           if (tmp_sym->attr.pointer == 1)
3732             {
3733               gfc_error ("Variable '%s' at %L cannot have both the "
3734                          "POINTER and BIND(C) attributes",
3735                          tmp_sym->name, &(tmp_sym->declared_at));
3736               retval = FAILURE;
3737             }
3738
3739           if (tmp_sym->attr.allocatable == 1)
3740             {
3741               gfc_error ("Variable '%s' at %L cannot have both the "
3742                          "ALLOCATABLE and BIND(C) attributes",
3743                          tmp_sym->name, &(tmp_sym->declared_at));
3744               retval = FAILURE;
3745             }
3746
3747         }
3748
3749       /* If it is a BIND(C) function, make sure the return value is a
3750          scalar value.  The previous tests in this function made sure
3751          the type is interoperable.  */
3752       if (bind_c_function && tmp_sym->as != NULL)
3753         gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3754                    "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3755
3756       /* BIND(C) functions can not return a character string.  */
3757       if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3758         if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3759             || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3760             || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3761           gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3762                          "be a character string", tmp_sym->name,
3763                          &(tmp_sym->declared_at));
3764     }
3765
3766   /* See if the symbol has been marked as private.  If it has, make sure
3767      there is no binding label and warn the user if there is one.  */
3768   if (tmp_sym->attr.access == ACCESS_PRIVATE
3769       && tmp_sym->binding_label[0] != '\0')
3770       /* Use gfc_warning_now because we won't say that the symbol fails
3771          just because of this.  */
3772       gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3773                        "given the binding label '%s'", tmp_sym->name,
3774                        &(tmp_sym->declared_at), tmp_sym->binding_label);
3775
3776   return retval;
3777 }
3778
3779
3780 /* Set the appropriate fields for a symbol that's been declared as
3781    BIND(C) (the is_bind_c flag and the binding label), and verify that
3782    the type is C interoperable.  Errors are reported by the functions
3783    used to set/test these fields.  */
3784
3785 gfc_try
3786 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3787 {
3788   gfc_try retval = SUCCESS;
3789   
3790   /* TODO: Do we need to make sure the vars aren't marked private?  */
3791
3792   /* Set the is_bind_c bit in symbol_attribute.  */
3793   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3794
3795   if (set_binding_label (tmp_sym->binding_label, tmp_sym->name, 
3796                          num_idents) != SUCCESS)
3797     return FAILURE;
3798
3799   return retval;
3800 }
3801
3802
3803 /* Set the fields marking the given common block as BIND(C), including
3804    a binding label, and report any errors encountered.  */
3805
3806 gfc_try
3807 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3808 {
3809   gfc_try retval = SUCCESS;
3810   
3811   /* destLabel, common name, typespec (which may have binding label).  */
3812   if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3813       != SUCCESS)
3814     return FAILURE;
3815
3816   /* Set the given common block (com_block) to being bind(c) (1).  */
3817   set_com_block_bind_c (com_block, 1);
3818
3819   return retval;
3820 }
3821
3822
3823 /* Retrieve the list of one or more identifiers that the given bind(c)
3824    attribute applies to.  */
3825
3826 gfc_try
3827 get_bind_c_idents (void)
3828 {
3829   char name[GFC_MAX_SYMBOL_LEN + 1];
3830   int num_idents = 0;
3831   gfc_symbol *tmp_sym = NULL;
3832   match found_id;
3833   gfc_common_head *com_block = NULL;
3834   
3835   if (gfc_match_name (name) == MATCH_YES)
3836     {
3837       found_id = MATCH_YES;
3838       gfc_get_ha_symbol (name, &tmp_sym);
3839     }
3840   else if (match_common_name (name) == MATCH_YES)
3841     {
3842       found_id = MATCH_YES;
3843       com_block = gfc_get_common (name, 0);
3844     }
3845   else
3846     {
3847       gfc_error ("Need either entity or common block name for "
3848                  "attribute specification statement at %C");
3849       return FAILURE;
3850     }
3851    
3852   /* Save the current identifier and look for more.  */
3853   do
3854     {
3855       /* Increment the number of identifiers found for this spec stmt.  */
3856       num_idents++;
3857
3858       /* Make sure we have a sym or com block, and verify that it can
3859          be bind(c).  Set the appropriate field(s) and look for more
3860          identifiers.  */
3861       if (tmp_sym != NULL || com_block != NULL)         
3862         {
3863           if (tmp_sym != NULL)
3864             {
3865               if (set_verify_bind_c_sym (tmp_sym, num_idents)
3866                   != SUCCESS)
3867                 return FAILURE;
3868             }
3869           else
3870             {
3871               if (set_verify_bind_c_com_block(com_block, num_idents)
3872                   != SUCCESS)
3873                 return FAILURE;
3874             }
3875          
3876           /* Look to see if we have another identifier.  */
3877           tmp_sym = NULL;
3878           if (gfc_match_eos () == MATCH_YES)
3879             found_id = MATCH_NO;
3880           else if (gfc_match_char (',') != MATCH_YES)
3881             found_id = MATCH_NO;
3882           else if (gfc_match_name (name) == MATCH_YES)
3883             {
3884               found_id = MATCH_YES;
3885               gfc_get_ha_symbol (name, &tmp_sym);
3886             }
3887           else if (match_common_name (name) == MATCH_YES)
3888             {
3889               found_id = MATCH_YES;
3890               com_block = gfc_get_common (name, 0);
3891             }
3892           else
3893             {
3894               gfc_error ("Missing entity or common block name for "
3895                          "attribute specification statement at %C");
3896               return FAILURE;
3897             }
3898         }
3899       else
3900         {
3901           gfc_internal_error ("Missing symbol");
3902         }
3903     } while (found_id == MATCH_YES);
3904
3905   /* if we get here we were successful */
3906   return SUCCESS;
3907 }
3908
3909
3910 /* Try and match a BIND(C) attribute specification statement.  */
3911    
3912 match
3913 gfc_match_bind_c_stmt (void)
3914 {
3915   match found_match = MATCH_NO;
3916   gfc_typespec *ts;
3917
3918   ts = &current_ts;
3919   
3920   /* This may not be necessary.  */
3921   gfc_clear_ts (ts);
3922   /* Clear the temporary binding label holder.  */
3923   curr_binding_label[0] = '\0';
3924
3925   /* Look for the bind(c).  */
3926   found_match = gfc_match_bind_c (NULL, true);
3927
3928   if (found_match == MATCH_YES)
3929     {
3930       /* Look for the :: now, but it is not required.  */
3931       gfc_match (" :: ");
3932
3933       /* Get the identifier(s) that needs to be updated.  This may need to
3934          change to hand the flag(s) for the attr specified so all identifiers
3935          found can have all appropriate parts updated (assuming that the same
3936          spec stmt can have multiple attrs, such as both bind(c) and
3937          allocatable...).  */
3938       if (get_bind_c_idents () != SUCCESS)
3939         /* Error message should have printed already.  */
3940         return MATCH_ERROR;
3941     }
3942
3943   return found_match;
3944 }
3945
3946
3947 /* Match a data declaration statement.  */
3948
3949 match
3950 gfc_match_data_decl (void)
3951 {
3952   gfc_symbol *sym;
3953   match m;
3954   int elem;
3955
3956   num_idents_on_line = 0;
3957   
3958   m = gfc_match_decl_type_spec (&current_ts, 0);
3959   if (m != MATCH_YES)
3960     return m;
3961
3962   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3963         && gfc_current_state () != COMP_DERIVED)
3964     {
3965       sym = gfc_use_derived (current_ts.u.derived);
3966
3967       if (sym == NULL)
3968         {
3969           m = MATCH_ERROR;
3970           goto cleanup;
3971         }
3972
3973       current_ts.u.derived = sym;
3974     }
3975
3976   m = match_attr_spec ();
3977   if (m == MATCH_ERROR)
3978     {
3979       m = MATCH_NO;
3980       goto cleanup;
3981     }
3982
3983   if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3984       && current_ts.u.derived->components == NULL
3985       && !current_ts.u.derived->attr.zero_comp)
3986     {
3987
3988       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3989         goto ok;
3990
3991       gfc_find_symbol (current_ts.u.derived->name,
3992                        current_ts.u.derived->ns->parent, 1, &sym);
3993
3994       /* Any symbol that we find had better be a type definition
3995          which has its components defined.  */
3996       if (sym != NULL && sym->attr.flavor == FL_DERIVED
3997           && (current_ts.u.derived->components != NULL
3998               || current_ts.u.derived->attr.zero_comp))
3999         goto ok;
4000
4001       /* Now we have an error, which we signal, and then fix up
4002          because the knock-on is plain and simple confusing.  */
4003       gfc_error_now ("Derived type at %C has not been previously defined "
4004                      "and so cannot appear in a derived type definition");
4005       current_attr.pointer = 1;
4006       goto ok;
4007     }
4008
4009 ok:
4010   /* If we have an old-style character declaration, and no new-style
4011      attribute specifications, then there a comma is optional between
4012      the type specification and the variable list.  */
4013   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4014     gfc_match_char (',');
4015
4016   /* Give the types/attributes to symbols that follow. Give the element
4017      a number so that repeat character length expressions can be copied.  */
4018   elem = 1;
4019   for (;;)
4020     {
4021       num_idents_on_line++;
4022       m = variable_decl (elem++);
4023       if (m == MATCH_ERROR)
4024         goto cleanup;
4025       if (m == MATCH_NO)
4026         break;
4027
4028       if (gfc_match_eos () == MATCH_YES)
4029         goto cleanup;
4030       if (gfc_match_char (',') != MATCH_YES)
4031         break;
4032     }
4033
4034   if (gfc_error_flag_test () == 0)
4035     gfc_error ("Syntax error in data declaration at %C");
4036   m = MATCH_ERROR;
4037
4038   gfc_free_data_all (gfc_current_ns);
4039
4040 cleanup:
4041   gfc_free_array_spec (current_as);
4042   current_as = NULL;
4043   return m;
4044 }
4045
4046
4047 /* Match a prefix associated with a function or subroutine
4048    declaration.  If the typespec pointer is nonnull, then a typespec
4049    can be matched.  Note that if nothing matches, MATCH_YES is
4050    returned (the null string was matched).  */
4051
4052 match
4053 gfc_match_prefix (gfc_typespec *ts)
4054 {
4055   bool seen_type;
4056   bool seen_impure;
4057   bool found_prefix;
4058
4059   gfc_clear_attr (&current_attr);
4060   seen_type = false;
4061   seen_impure = false;
4062
4063   gcc_assert (!gfc_matching_prefix);
4064   gfc_matching_prefix = true;
4065
4066   do
4067     {
4068       found_prefix = false;
4069
4070       if (!seen_type && ts != NULL
4071           && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4072           && gfc_match_space () == MATCH_YES)
4073         {
4074
4075           seen_type = true;
4076           found_prefix = true;
4077         }
4078
4079       if (gfc_match ("elemental% ") == MATCH_YES)
4080         {
4081           if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4082             goto error;
4083
4084           found_prefix = true;
4085         }
4086
4087       if (gfc_match ("pure% ") == MATCH_YES)
4088         {
4089           if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4090             goto error;
4091
4092           found_prefix = true;
4093         }
4094
4095       if (gfc_match ("recursive% ") == MATCH_YES)
4096         {
4097           if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4098             goto error;
4099
4100           found_prefix = true;
4101         }
4102
4103       /* IMPURE is a somewhat special case, as it needs not set an actual
4104          attribute but rather only prevents ELEMENTAL routines from being
4105          automatically PURE.  */
4106       if (gfc_match ("impure% ") == MATCH_YES)
4107         {
4108           if (gfc_notify_std (GFC_STD_F2008,
4109                               "Fortran 2008: IMPURE procedure at %C")
4110                 == FAILURE)
4111             goto error;
4112
4113           seen_impure = true;
4114           found_prefix = true;
4115         }
4116     }
4117   while (found_prefix);
4118
4119   /* IMPURE and PURE must not both appear, of course.  */
4120   if (seen_impure && current_attr.pure)
4121     {
4122       gfc_error ("PURE and IMPURE must not appear both at %C");
4123       goto error;
4124     }
4125
4126   /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
4127   if (!seen_impure && current_attr.elemental && !current_attr.pure)
4128     {
4129       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4130         goto error;
4131     }
4132
4133   /* At this point, the next item is not a prefix.  */
4134   gcc_assert (gfc_matching_prefix);
4135   gfc_matching_prefix = false;
4136   return MATCH_YES;
4137
4138 error:
4139   gcc_assert (gfc_matching_prefix);
4140   gfc_matching_prefix = false;
4141   return MATCH_ERROR;
4142 }
4143
4144
4145 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
4146
4147 static gfc_try
4148 copy_prefix (symbol_attribute *dest, locus *where)
4149 {
4150   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4151     return FAILURE;
4152
4153   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4154     return FAILURE;
4155
4156   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4157     return FAILURE;
4158
4159   return SUCCESS;
4160 }
4161
4162
4163 /* Match a formal argument list.  */
4164
4165 match
4166 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4167 {
4168   gfc_formal_arglist *head, *tail, *p, *q;
4169   char name[GFC_MAX_SYMBOL_LEN + 1];
4170   gfc_symbol *sym;
4171   match m;
4172
4173   head = tail = NULL;
4174
4175   if (gfc_match_char ('(') != MATCH_YES)
4176     {
4177       if (null_flag)
4178         goto ok;
4179       return MATCH_NO;
4180     }
4181
4182   if (gfc_match_char (')') == MATCH_YES)
4183     goto ok;
4184
4185   for (;;)
4186     {
4187       if (gfc_match_char ('*') == MATCH_YES)
4188         sym = NULL;
4189       else
4190         {
4191           m = gfc_match_name (name);
4192           if (m != MATCH_YES)
4193             goto cleanup;
4194
4195           if (gfc_get_symbol (name, NULL, &sym))
4196             goto cleanup;
4197         }
4198
4199       p = gfc_get_formal_arglist ();
4200
4201       if (head == NULL)
4202         head = tail = p;
4203       else
4204         {
4205           tail->next = p;
4206           tail = p;
4207         }
4208
4209       tail->sym = sym;
4210
4211       /* We don't add the VARIABLE flavor because the name could be a
4212          dummy procedure.  We don't apply these attributes to formal
4213          arguments of statement functions.  */
4214       if (sym != NULL && !st_flag
4215           && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4216               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4217         {
4218           m = MATCH_ERROR;
4219           goto cleanup;
4220         }
4221
4222       /* The name of a program unit can be in a different namespace,
4223          so check for it explicitly.  After the statement is accepted,
4224          the name is checked for especially in gfc_get_symbol().  */
4225       if (gfc_new_block != NULL && sym != NULL
4226           && strcmp (sym->name, gfc_new_block->name) == 0)
4227         {
4228           gfc_error ("Name '%s' at %C is the name of the procedure",
4229                      sym->name);
4230           m = MATCH_ERROR;
4231           goto cleanup;
4232         }
4233
4234       if (gfc_match_char (')') == MATCH_YES)
4235         goto ok;
4236
4237       m = gfc_match_char (',');
4238       if (m != MATCH_YES)
4239         {
4240           gfc_error ("Unexpected junk in formal argument list at %C");
4241           goto cleanup;
4242         }
4243     }
4244
4245 ok:
4246   /* Check for duplicate symbols in the formal argument list.  */
4247   if (head != NULL)
4248     {
4249       for (p = head; p->next; p = p->next)
4250         {
4251           if (p->sym == NULL)
4252             continue;
4253
4254           for (q = p->next; q; q = q->next)
4255             if (p->sym == q->sym)
4256               {
4257                 gfc_error ("Duplicate symbol '%s' in formal argument list "
4258                            "at %C", p->sym->name);
4259
4260                 m = MATCH_ERROR;
4261                 goto cleanup;
4262               }
4263         }
4264     }
4265
4266   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4267       == FAILURE)
4268     {
4269       m = MATCH_ERROR;
4270       goto cleanup;
4271     }
4272
4273   return MATCH_YES;
4274
4275 cleanup:
4276   gfc_free_formal_arglist (head);
4277   return m;
4278 }
4279
4280
4281 /* Match a RESULT specification following a function declaration or
4282    ENTRY statement.  Also matches the end-of-statement.  */
4283
4284 static match
4285 match_result (gfc_symbol *function, gfc_symbol **result)
4286 {
4287   char name[GFC_MAX_SYMBOL_LEN + 1];
4288   gfc_symbol *r;
4289   match m;
4290
4291   if (gfc_match (" result (") != MATCH_YES)
4292     return MATCH_NO;
4293
4294   m = gfc_match_name (name);
4295   if (m != MATCH_YES)
4296     return m;
4297
4298   /* Get the right paren, and that's it because there could be the
4299      bind(c) attribute after the result clause.  */
4300   if (gfc_match_char(')') != MATCH_YES)
4301     {
4302      /* TODO: should report the missing right paren here.  */
4303       return MATCH_ERROR;
4304     }
4305
4306   if (strcmp (function->name, name) == 0)
4307     {
4308       gfc_error ("RESULT variable at %C must be different than function name");
4309       return MATCH_ERROR;
4310     }
4311
4312   if (gfc_get_symbol (name, NULL, &r))
4313     return MATCH_ERROR;
4314
4315   if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4316     return MATCH_ERROR;
4317
4318   *result = r;
4319
4320   return MATCH_YES;
4321 }
4322
4323
4324 /* Match a function suffix, which could be a combination of a result
4325    clause and BIND(C), either one, or neither.  The draft does not
4326    require them to come in a specific order.  */
4327
4328 match
4329 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4330 {
4331   match is_bind_c;   /* Found bind(c).  */
4332   match is_result;   /* Found result clause.  */
4333   match found_match; /* Status of whether we've found a good match.  */
4334   char peek_char;    /* Character we're going to peek at.  */
4335   bool allow_binding_name;
4336
4337   /* Initialize to having found nothing.  */
4338   found_match = MATCH_NO;
4339   is_bind_c = MATCH_NO; 
4340   is_result = MATCH_NO;
4341
4342   /* Get the next char to narrow between result and bind(c).  */
4343   gfc_gobble_whitespace ();
4344   peek_char = gfc_peek_ascii_char ();
4345
4346   /* C binding names are not allowed for internal procedures.  */
4347   if (gfc_current_state () == COMP_CONTAINS
4348       && sym->ns->proc_name->attr.flavor != FL_MODULE)
4349     allow_binding_name = false;
4350   else
4351     allow_binding_name = true;
4352
4353   switch (peek_char)
4354     {
4355     case 'r':
4356       /* Look for result clause.  */
4357       is_result = match_result (sym, result);
4358       if (is_result == MATCH_YES)
4359         {
4360           /* Now see if there is a bind(c) after it.  */
4361           is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4362           /* We've found the result clause and possibly bind(c).  */
4363           found_match = MATCH_YES;
4364         }
4365       else
4366         /* This should only be MATCH_ERROR.  */
4367         found_match = is_result; 
4368       break;
4369     case 'b':
4370       /* Look for bind(c) first.  */
4371       is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4372       if (is_bind_c == MATCH_YES)
4373         {
4374           /* Now see if a result clause followed it.  */
4375           is_result = match_result (sym, result);
4376           found_match = MATCH_YES;
4377         }
4378       else
4379         {
4380           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
4381           found_match = MATCH_ERROR;
4382         }
4383       break;
4384     default:
4385       gfc_error ("Unexpected junk after function declaration at %C");
4386       found_match = MATCH_ERROR;
4387       break;
4388     }
4389
4390   if (is_bind_c == MATCH_YES)
4391     {
4392       /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
4393       if (gfc_current_state () == COMP_CONTAINS
4394           && sym->ns->proc_name->attr.flavor != FL_MODULE
4395           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4396                              "at %L may not be specified for an internal "
4397                              "procedure", &gfc_current_locus)
4398              == FAILURE)
4399         return MATCH_ERROR;
4400
4401       if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4402           == FAILURE)
4403         return MATCH_ERROR;
4404     }
4405   
4406   return found_match;
4407 }
4408
4409
4410 /* Procedure pointer return value without RESULT statement:
4411    Add "hidden" result variable named "ppr@".  */
4412
4413 static gfc_try
4414 add_hidden_procptr_result (gfc_symbol *sym)
4415 {
4416   bool case1,case2;
4417
4418   if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4419     return FAILURE;
4420
4421   /* First usage case: PROCEDURE and EXTERNAL statements.  */
4422   case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4423           && strcmp (gfc_current_block ()->name, sym->name) == 0
4424           && sym->attr.external;
4425   /* Second usage case: INTERFACE statements.  */
4426   case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4427           && gfc_state_stack->previous->state == COMP_FUNCTION
4428           && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4429
4430   if (case1 || case2)
4431     {
4432       gfc_symtree *stree;
4433       if (case1)
4434         gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4435       else if (case2)
4436         {
4437           gfc_symtree *st2;
4438           gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4439           st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4440           st2->n.sym = stree->n.sym;
4441         }
4442       sym->result = stree->n.sym;
4443
4444       sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4445       sym->result->attr.pointer = sym->attr.pointer;
4446       sym->result->attr.external = sym->attr.external;
4447       sym->result->attr.referenced = sym->attr.referenced;
4448       sym->result->ts = sym->ts;
4449       sym->attr.proc_pointer = 0;
4450       sym->attr.pointer = 0;
4451       sym->attr.external = 0;
4452       if (sym->result->attr.external && sym->result->attr.pointer)
4453         {
4454           sym->result->attr.pointer = 0;
4455           sym->result->attr.proc_pointer = 1;
4456         }
4457
4458       return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4459     }
4460   /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
4461   else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4462            && sym->result && sym->result != sym && sym->result->attr.external
4463            && sym == gfc_current_ns->proc_name
4464            && sym == sym->result->ns->proc_name
4465            && strcmp ("ppr@", sym->result->name) == 0)
4466     {
4467       sym->result->attr.proc_pointer = 1;
4468       sym->attr.pointer = 0;
4469       return SUCCESS;
4470     }
4471   else
4472     return FAILURE;
4473 }
4474
4475
4476 /* Match the interface for a PROCEDURE declaration,
4477    including brackets (R1212).  */
4478
4479 static match
4480 match_procedure_interface (gfc_symbol **proc_if)
4481 {
4482   match m;
4483   gfc_symtree *st;
4484   locus old_loc, entry_loc;
4485   gfc_namespace *old_ns = gfc_current_ns;
4486   char name[GFC_MAX_SYMBOL_LEN + 1];
4487
4488   old_loc = entry_loc = gfc_current_locus;
4489   gfc_clear_ts (&current_ts);
4490
4491   if (gfc_match (" (") != MATCH_YES)
4492     {
4493       gfc_current_locus = entry_loc;
4494       return MATCH_NO;
4495     }
4496
4497   /* Get the type spec. for the procedure interface.  */
4498   old_loc = gfc_current_locus;
4499   m = gfc_match_decl_type_spec (&current_ts, 0);
4500   gfc_gobble_whitespace ();
4501   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4502     goto got_ts;
4503
4504   if (m == MATCH_ERROR)
4505     return m;
4506
4507   /* Procedure interface is itself a procedure.  */
4508   gfc_current_locus = old_loc;
4509   m = gfc_match_name (name);
4510
4511   /* First look to see if it is already accessible in the current
4512      namespace because it is use associated or contained.  */
4513   st = NULL;
4514   if (gfc_find_sym_tree (name, NULL, 0, &st))
4515     return MATCH_ERROR;
4516
4517   /* If it is still not found, then try the parent namespace, if it
4518      exists and create the symbol there if it is still not found.  */
4519   if (gfc_current_ns->parent)
4520     gfc_current_ns = gfc_current_ns->parent;
4521   if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4522     return MATCH_ERROR;
4523
4524   gfc_current_ns = old_ns;
4525   *proc_if = st->n.sym;
4526
4527   /* Various interface checks.  */
4528   if (*proc_if)
4529     {
4530       (*proc_if)->refs++;
4531       /* Resolve interface if possible. That way, attr.procedure is only set
4532          if it is declared by a later procedure-declaration-stmt, which is
4533          invalid per C1212.  */
4534       while ((*proc_if)->ts.interface)
4535         *proc_if = (*proc_if)->ts.interface;
4536
4537       if ((*proc_if)->generic)
4538         {
4539           gfc_error ("Interface '%s' at %C may not be generic",
4540                      (*proc_if)->name);
4541           return MATCH_ERROR;
4542         }
4543       if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4544         {
4545           gfc_error ("Interface '%s' at %C may not be a statement function",
4546                      (*proc_if)->name);
4547           return MATCH_ERROR;
4548         }
4549       /* Handle intrinsic procedures.  */
4550       if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4551             || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4552           && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4553               || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4554         (*proc_if)->attr.intrinsic = 1;
4555       if ((*proc_if)->attr.intrinsic
4556           && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4557         {
4558           gfc_error ("Intrinsic procedure '%s' not allowed "
4559                     "in PROCEDURE statement at %C", (*proc_if)->name);
4560           return MATCH_ERROR;
4561         }
4562     }
4563
4564 got_ts:
4565   if (gfc_match (" )") != MATCH_YES)
4566     {
4567       gfc_current_locus = entry_loc;
4568       return MATCH_NO;
4569     }
4570
4571   return MATCH_YES;
4572 }
4573
4574
4575 /* Match a PROCEDURE declaration (R1211).  */
4576
4577 static match
4578 match_procedure_decl (void)
4579 {
4580   match m;
4581   gfc_symbol *sym, *proc_if = NULL;
4582   int num;
4583   gfc_expr *initializer = NULL;
4584
4585   /* Parse interface (with brackets). */
4586   m = match_procedure_interface (&proc_if);
4587   if (m != MATCH_YES)
4588     return m;
4589
4590   /* Parse attributes (with colons).  */
4591   m = match_attr_spec();
4592   if (m == MATCH_ERROR)
4593     return MATCH_ERROR;
4594
4595   /* Get procedure symbols.  */
4596   for(num=1;;num++)
4597     {
4598       m = gfc_match_symbol (&sym, 0);
4599       if (m == MATCH_NO)
4600         goto syntax;
4601       else if (m == MATCH_ERROR)
4602         return m;
4603
4604       /* Add current_attr to the symbol attributes.  */
4605       if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4606         return MATCH_ERROR;
4607
4608       if (sym->attr.is_bind_c)
4609         {
4610           /* Check for C1218.  */
4611           if (!proc_if || !proc_if->attr.is_bind_c)
4612             {
4613               gfc_error ("BIND(C) attribute at %C requires "
4614                         "an interface with BIND(C)");
4615               return MATCH_ERROR;
4616             }
4617           /* Check for C1217.  */
4618           if (has_name_equals && sym->attr.pointer)
4619             {
4620               gfc_error ("BIND(C) procedure with NAME may not have "
4621                         "POINTER attribute at %C");
4622               return MATCH_ERROR;
4623             }
4624           if (has_name_equals && sym->attr.dummy)
4625             {
4626               gfc_error ("Dummy procedure at %C may not have "
4627                         "BIND(C) attribute with NAME");
4628               return MATCH_ERROR;
4629             }
4630           /* Set binding label for BIND(C).  */
4631           if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4632             return MATCH_ERROR;
4633         }
4634
4635       if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4636         return MATCH_ERROR;
4637
4638       if (add_hidden_procptr_result (sym) == SUCCESS)
4639         sym = sym->result;
4640
4641       if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4642         return MATCH_ERROR;
4643
4644       /* Set interface.  */
4645       if (proc_if != NULL)
4646         {
4647           if (sym->ts.type != BT_UNKNOWN)
4648             {
4649               gfc_error ("Procedure '%s' at %L already has basic type of %s",
4650                          sym->name, &gfc_current_locus,
4651                          gfc_basic_typename (sym->ts.type));
4652               return MATCH_ERROR;
4653             }
4654           sym->ts.interface = proc_if;
4655           sym->attr.untyped = 1;
4656           sym->attr.if_source = IFSRC_IFBODY;
4657         }
4658       else if (current_ts.type != BT_UNKNOWN)
4659         {
4660           if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4661             return MATCH_ERROR;
4662           sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4663           sym->ts.interface->ts = current_ts;
4664           sym->ts.interface->attr.function = 1;
4665           sym->attr.function = sym->ts.interface->attr.function;
4666           sym->attr.if_source = IFSRC_UNKNOWN;
4667         }
4668
4669       if (gfc_match (" =>") == MATCH_YES)
4670         {
4671           if (!current_attr.pointer)
4672             {
4673               gfc_error ("Initialization at %C isn't for a pointer variable");
4674               m = MATCH_ERROR;
4675               goto cleanup;
4676             }
4677
4678           m = gfc_match_null (&initializer);
4679           if (m == MATCH_NO)
4680             {
4681               gfc_error ("Pointer initialization requires a NULL() at %C");
4682               m = MATCH_ERROR;
4683             }
4684
4685           if (gfc_pure (NULL))
4686             {
4687               gfc_error ("Initialization of pointer at %C is not allowed in "
4688                          "a PURE procedure");
4689               m = MATCH_ERROR;
4690             }
4691
4692           if (m != MATCH_YES)
4693             goto cleanup;
4694
4695           if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4696               != SUCCESS)
4697             goto cleanup;
4698
4699         }
4700
4701       gfc_set_sym_referenced (sym);
4702
4703       if (gfc_match_eos () == MATCH_YES)
4704         return MATCH_YES;
4705       if (gfc_match_char (',') != MATCH_YES)
4706         goto syntax;
4707     }
4708
4709 syntax:
4710   gfc_error ("Syntax error in PROCEDURE statement at %C");
4711   return MATCH_ERROR;
4712
4713 cleanup:
4714   /* Free stuff up and return.  */
4715   gfc_free_expr (initializer);
4716   return m;
4717 }
4718
4719
4720 static match
4721 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4722
4723
4724 /* Match a procedure pointer component declaration (R445).  */
4725
4726 static match
4727 match_ppc_decl (void)
4728 {
4729   match m;
4730   gfc_symbol *proc_if = NULL;
4731   gfc_typespec ts;
4732   int num;
4733   gfc_component *c;
4734   gfc_expr *initializer = NULL;
4735   gfc_typebound_proc* tb;
4736   char name[GFC_MAX_SYMBOL_LEN + 1];
4737
4738   /* Parse interface (with brackets).  */
4739   m = match_procedure_interface (&proc_if);
4740   if (m != MATCH_YES)
4741     goto syntax;
4742
4743   /* Parse attributes.  */
4744   tb = XCNEW (gfc_typebound_proc);
4745   tb->where = gfc_current_locus;
4746   m = match_binding_attributes (tb, false, true);
4747   if (m == MATCH_ERROR)
4748     return m;
4749
4750   gfc_clear_attr (&current_attr);
4751   current_attr.procedure = 1;
4752   current_attr.proc_pointer = 1;
4753   current_attr.access = tb->access;
4754   current_attr.flavor = FL_PROCEDURE;
4755
4756   /* Match the colons (required).  */
4757   if (gfc_match (" ::") != MATCH_YES)
4758     {
4759       gfc_error ("Expected '::' after binding-attributes at %C");
4760       return MATCH_ERROR;
4761     }
4762
4763   /* Check for C450.  */
4764   if (!tb->nopass && proc_if == NULL)
4765     {
4766       gfc_error("NOPASS or explicit interface required at %C");
4767       return MATCH_ERROR;
4768     }
4769
4770   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4771                      "component at %C") == FAILURE)
4772     return MATCH_ERROR;
4773
4774   /* Match PPC names.  */
4775   ts = current_ts;
4776   for(num=1;;num++)
4777     {
4778       m = gfc_match_name (name);
4779       if (m == MATCH_NO)
4780         goto syntax;
4781       else if (m == MATCH_ERROR)
4782         return m;
4783
4784       if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4785         return MATCH_ERROR;
4786
4787       /* Add current_attr to the symbol attributes.  */
4788       if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4789         return MATCH_ERROR;
4790
4791       if (gfc_add_external (&c->attr, NULL) == FAILURE)
4792         return MATCH_ERROR;
4793
4794       if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4795         return MATCH_ERROR;
4796
4797       c->tb = tb;
4798
4799       /* Set interface.  */
4800       if (proc_if != NULL)
4801         {
4802           c->ts.interface = proc_if;
4803           c->attr.untyped = 1;
4804           c->attr.if_source = IFSRC_IFBODY;
4805         }
4806       else if (ts.type != BT_UNKNOWN)
4807         {
4808           c->ts = ts;
4809           c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4810           c->ts.interface->ts = ts;
4811           c->ts.interface->attr.function = 1;
4812           c->attr.function = c->ts.interface->attr.function;
4813           c->attr.if_source = IFSRC_UNKNOWN;
4814         }
4815
4816       if (gfc_match (" =>") == MATCH_YES)
4817         {
4818           m = gfc_match_null (&initializer);
4819           if (m == MATCH_NO)
4820             {
4821               gfc_error ("Pointer initialization requires a NULL() at %C");
4822               m = MATCH_ERROR;
4823             }
4824           if (gfc_pure (NULL))
4825             {
4826               gfc_error ("Initialization of pointer at %C is not allowed in "
4827                          "a PURE procedure");
4828               m = MATCH_ERROR;
4829             }
4830           if (m != MATCH_YES)
4831             {
4832               gfc_free_expr (initializer);
4833               return m;
4834             }
4835           c->initializer = initializer;
4836         }
4837
4838       if (gfc_match_eos () == MATCH_YES)
4839         return MATCH_YES;
4840       if (gfc_match_char (',') != MATCH_YES)
4841         goto syntax;
4842     }
4843
4844 syntax:
4845   gfc_error ("Syntax error in procedure pointer component at %C");
4846   return MATCH_ERROR;
4847 }
4848
4849
4850 /* Match a PROCEDURE declaration inside an interface (R1206).  */
4851
4852 static match
4853 match_procedure_in_interface (void)
4854 {
4855   match m;
4856   gfc_symbol *sym;
4857   char name[GFC_MAX_SYMBOL_LEN + 1];
4858
4859   if (current_interface.type == INTERFACE_NAMELESS
4860       || current_interface.type == INTERFACE_ABSTRACT)
4861     {
4862       gfc_error ("PROCEDURE at %C must be in a generic interface");
4863       return MATCH_ERROR;
4864     }
4865
4866   for(;;)
4867     {
4868       m = gfc_match_name (name);
4869       if (m == MATCH_NO)
4870         goto syntax;
4871       else if (m == MATCH_ERROR)
4872         return m;
4873       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4874         return MATCH_ERROR;
4875
4876       if (gfc_add_interface (sym) == FAILURE)
4877         return MATCH_ERROR;
4878
4879       if (gfc_match_eos () == MATCH_YES)
4880         break;
4881       if (gfc_match_char (',') != MATCH_YES)
4882         goto syntax;
4883     }
4884
4885   return MATCH_YES;
4886
4887 syntax:
4888   gfc_error ("Syntax error in PROCEDURE statement at %C");
4889   return MATCH_ERROR;
4890 }
4891
4892
4893 /* General matcher for PROCEDURE declarations.  */
4894
4895 static match match_procedure_in_type (void);
4896
4897 match
4898 gfc_match_procedure (void)
4899 {
4900   match m;
4901
4902   switch (gfc_current_state ())
4903     {
4904     case COMP_NONE:
4905     case COMP_PROGRAM:
4906     case COMP_MODULE:
4907     case COMP_SUBROUTINE:
4908     case COMP_FUNCTION:
4909       m = match_procedure_decl ();
4910       break;
4911     case COMP_INTERFACE:
4912       m = match_procedure_in_interface ();
4913       break;
4914     case COMP_DERIVED:
4915       m = match_ppc_decl ();
4916       break;
4917     case COMP_DERIVED_CONTAINS:
4918       m = match_procedure_in_type ();
4919       break;
4920     default:
4921       return MATCH_NO;
4922     }
4923
4924   if (m != MATCH_YES)
4925     return m;
4926
4927   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4928       == FAILURE)
4929     return MATCH_ERROR;
4930
4931   return m;
4932 }
4933
4934
4935 /* Warn if a matched procedure has the same name as an intrinsic; this is
4936    simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4937    parser-state-stack to find out whether we're in a module.  */
4938
4939 static void
4940 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4941 {
4942   bool in_module;
4943
4944   in_module = (gfc_state_stack->previous
4945                && gfc_state_stack->previous->state == COMP_MODULE);
4946
4947   gfc_warn_intrinsic_shadow (sym, in_module, func);
4948 }
4949
4950
4951 /* Match a function declaration.  */
4952
4953 match
4954 gfc_match_function_decl (void)
4955 {
4956   char name[GFC_MAX_SYMBOL_LEN + 1];
4957   gfc_symbol *sym, *result;
4958   locus old_loc;
4959   match m;
4960   match suffix_match;
4961   match found_match; /* Status returned by match func.  */  
4962
4963   if (gfc_current_state () != COMP_NONE
4964       && gfc_current_state () != COMP_INTERFACE
4965       && gfc_current_state () != COMP_CONTAINS)
4966     return MATCH_NO;
4967
4968   gfc_clear_ts (&current_ts);
4969
4970   old_loc = gfc_current_locus;
4971
4972   m = gfc_match_prefix (&current_ts);
4973   if (m != MATCH_YES)
4974     {
4975       gfc_current_locus = old_loc;
4976       return m;
4977     }
4978
4979   if (gfc_match ("function% %n", name) != MATCH_YES)
4980     {
4981       gfc_current_locus = old_loc;
4982       return MATCH_NO;
4983     }
4984   if (get_proc_name (name, &sym, false))
4985     return MATCH_ERROR;
4986
4987   if (add_hidden_procptr_result (sym) == SUCCESS)
4988     sym = sym->result;
4989
4990   gfc_new_block = sym;
4991
4992   m = gfc_match_formal_arglist (sym, 0, 0);
4993   if (m == MATCH_NO)
4994     {
4995       gfc_error ("Expected formal argument list in function "
4996                  "definition at %C");
4997       m = MATCH_ERROR;
4998       goto cleanup;
4999     }
5000   else if (m == MATCH_ERROR)
5001     goto cleanup;
5002
5003   result = NULL;
5004
5005   /* According to the draft, the bind(c) and result clause can
5006      come in either order after the formal_arg_list (i.e., either
5007      can be first, both can exist together or by themselves or neither
5008      one).  Therefore, the match_result can't match the end of the
5009      string, and check for the bind(c) or result clause in either order.  */
5010   found_match = gfc_match_eos ();
5011
5012   /* Make sure that it isn't already declared as BIND(C).  If it is, it
5013      must have been marked BIND(C) with a BIND(C) attribute and that is
5014      not allowed for procedures.  */
5015   if (sym->attr.is_bind_c == 1)
5016     {
5017       sym->attr.is_bind_c = 0;
5018       if (sym->old_symbol != NULL)
5019         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5020                        "variables or common blocks",
5021                        &(sym->old_symbol->declared_at));
5022       else
5023         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5024                        "variables or common blocks", &gfc_current_locus);
5025     }
5026
5027   if (found_match != MATCH_YES)
5028     {
5029       /* If we haven't found the end-of-statement, look for a suffix.  */
5030       suffix_match = gfc_match_suffix (sym, &result);
5031       if (suffix_match == MATCH_YES)
5032         /* Need to get the eos now.  */
5033         found_match = gfc_match_eos ();
5034       else
5035         found_match = suffix_match;
5036     }
5037
5038   if(found_match != MATCH_YES)
5039     m = MATCH_ERROR;
5040   else
5041     {
5042       /* Make changes to the symbol.  */
5043       m = MATCH_ERROR;
5044       
5045       if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5046         goto cleanup;
5047       
5048       if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5049           || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5050         goto cleanup;
5051
5052       /* Delay matching the function characteristics until after the
5053          specification block by signalling kind=-1.  */
5054       sym->declared_at = old_loc;
5055       if (current_ts.type != BT_UNKNOWN)
5056         current_ts.kind = -1;
5057       else
5058         current_ts.kind = 0;
5059
5060       if (result == NULL)
5061         {
5062           if (current_ts.type != BT_UNKNOWN
5063               && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5064             goto cleanup;
5065           sym->result = sym;
5066         }
5067       else
5068         {
5069           if (current_ts.type != BT_UNKNOWN
5070               && gfc_add_type (result, &current_ts, &gfc_current_locus)
5071                  == FAILURE)
5072             goto cleanup;
5073           sym->result = result;
5074         }
5075
5076       /* Warn if this procedure has the same name as an intrinsic.  */
5077       warn_intrinsic_shadow (sym, true);
5078
5079       return MATCH_YES;
5080     }
5081
5082 cleanup:
5083   gfc_current_locus = old_loc;
5084   return m;
5085 }
5086
5087
5088 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5089    pass the name of the entry, rather than the gfc_current_block name, and
5090    to return false upon finding an existing global entry.  */
5091
5092 static bool
5093 add_global_entry (const char *name, int sub)
5094 {
5095   gfc_gsymbol *s;
5096   enum gfc_symbol_type type;
5097
5098   s = gfc_get_gsymbol(name);
5099   type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5100
5101   if (s->defined
5102       || (s->type != GSYM_UNKNOWN
5103           && s->type != type))
5104     gfc_global_used(s, NULL);
5105   else
5106     {
5107       s->type = type;
5108       s->where = gfc_current_locus;
5109       s->defined = 1;
5110       s->ns = gfc_current_ns;
5111       return true;
5112     }
5113   return false;
5114 }
5115
5116
5117 /* Match an ENTRY statement.  */
5118
5119 match
5120 gfc_match_entry (void)
5121 {
5122   gfc_symbol *proc;
5123   gfc_symbol *result;
5124   gfc_symbol *entry;
5125   char name[GFC_MAX_SYMBOL_LEN + 1];
5126   gfc_compile_state state;
5127   match m;
5128   gfc_entry_list *el;
5129   locus old_loc;
5130   bool module_procedure;
5131   char peek_char;
5132   match is_bind_c;
5133
5134   m = gfc_match_name (name);
5135   if (m != MATCH_YES)
5136     return m;
5137
5138   if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5139                       "ENTRY statement at %C") == FAILURE)
5140     return MATCH_ERROR;
5141
5142   state = gfc_current_state ();
5143   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5144     {
5145       switch (state)
5146         {
5147           case COMP_PROGRAM:
5148             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5149             break;
5150           case COMP_MODULE:
5151             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5152             break;
5153           case COMP_BLOCK_DATA:
5154             gfc_error ("ENTRY statement at %C cannot appear within "
5155                        "a BLOCK DATA");
5156             break;
5157           case COMP_INTERFACE:
5158             gfc_error ("ENTRY statement at %C cannot appear within "
5159                        "an INTERFACE");
5160             break;
5161           case COMP_DERIVED:
5162             gfc_error ("ENTRY statement at %C cannot appear within "
5163                        "a DERIVED TYPE block");
5164             break;
5165           case COMP_IF:
5166             gfc_error ("ENTRY statement at %C cannot appear within "
5167                        "an IF-THEN block");
5168             break;
5169           case COMP_DO:
5170             gfc_error ("ENTRY statement at %C cannot appear within "
5171                        "a DO block");
5172             break;
5173           case COMP_SELECT:
5174             gfc_error ("ENTRY statement at %C cannot appear within "
5175                        "a SELECT block");
5176             break;
5177           case COMP_FORALL:
5178             gfc_error ("ENTRY statement at %C cannot appear within "
5179                        "a FORALL block");
5180             break;
5181           case COMP_WHERE:
5182             gfc_error ("ENTRY statement at %C cannot appear within "
5183                        "a WHERE block");
5184             break;
5185           case COMP_CONTAINS:
5186             gfc_error ("ENTRY statement at %C cannot appear within "
5187                        "a contained subprogram");
5188             break;
5189           default:
5190             gfc_internal_error ("gfc_match_entry(): Bad state");
5191         }
5192       return MATCH_ERROR;
5193     }
5194
5195   module_procedure = gfc_current_ns->parent != NULL
5196                    && gfc_current_ns->parent->proc_name
5197                    && gfc_current_ns->parent->proc_name->attr.flavor
5198                       == FL_MODULE;
5199
5200   if (gfc_current_ns->parent != NULL
5201       && gfc_current_ns->parent->proc_name
5202       && !module_procedure)
5203     {
5204       gfc_error("ENTRY statement at %C cannot appear in a "
5205                 "contained procedure");
5206       return MATCH_ERROR;
5207     }
5208
5209   /* Module function entries need special care in get_proc_name
5210      because previous references within the function will have
5211      created symbols attached to the current namespace.  */
5212   if (get_proc_name (name, &entry,
5213                      gfc_current_ns->parent != NULL
5214                      && module_procedure))
5215     return MATCH_ERROR;
5216
5217   proc = gfc_current_block ();
5218
5219   /* Make sure that it isn't already declared as BIND(C).  If it is, it
5220      must have been marked BIND(C) with a BIND(C) attribute and that is
5221      not allowed for procedures.  */
5222   if (entry->attr.is_bind_c == 1)
5223     {
5224       entry->attr.is_bind_c = 0;
5225       if (entry->old_symbol != NULL)
5226         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5227                        "variables or common blocks",
5228                        &(entry->old_symbol->declared_at));
5229       else
5230         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5231                        "variables or common blocks", &gfc_current_locus);
5232     }
5233   
5234   /* Check what next non-whitespace character is so we can tell if there
5235      is the required parens if we have a BIND(C).  */
5236   gfc_gobble_whitespace ();
5237   peek_char = gfc_peek_ascii_char ();
5238
5239   if (state == COMP_SUBROUTINE)
5240     {
5241       /* An entry in a subroutine.  */
5242       if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5243         return MATCH_ERROR;
5244
5245       m = gfc_match_formal_arglist (entry, 0, 1);
5246       if (m != MATCH_YES)
5247         return MATCH_ERROR;
5248
5249       /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5250          never be an internal procedure.  */
5251       is_bind_c = gfc_match_bind_c (entry, true);
5252       if (is_bind_c == MATCH_ERROR)
5253         return MATCH_ERROR;
5254       if (is_bind_c == MATCH_YES)
5255         {
5256           if (peek_char != '(')
5257             {
5258               gfc_error ("Missing required parentheses before BIND(C) at %C");
5259               return MATCH_ERROR;
5260             }
5261             if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5262                 == FAILURE)
5263               return MATCH_ERROR;
5264         }
5265
5266       if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5267           || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5268         return MATCH_ERROR;
5269     }
5270   else
5271     {
5272       /* An entry in a function.
5273          We need to take special care because writing
5274             ENTRY f()
5275          as
5276             ENTRY f
5277          is allowed, whereas
5278             ENTRY f() RESULT (r)
5279          can't be written as
5280             ENTRY f RESULT (r).  */
5281       if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5282         return MATCH_ERROR;
5283
5284       old_loc = gfc_current_locus;
5285       if (gfc_match_eos () == MATCH_YES)
5286         {
5287           gfc_current_locus = old_loc;
5288           /* Match the empty argument list, and add the interface to
5289              the symbol.  */
5290           m = gfc_match_formal_arglist (entry, 0, 1);
5291         }
5292       else
5293         m = gfc_match_formal_arglist (entry, 0, 0);
5294
5295       if (m != MATCH_YES)
5296         return MATCH_ERROR;
5297
5298       result = NULL;
5299
5300       if (gfc_match_eos () == MATCH_YES)
5301         {
5302           if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5303               || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5304             return MATCH_ERROR;
5305
5306           entry->result = entry;
5307         }
5308       else
5309         {
5310           m = gfc_match_suffix (entry, &result);
5311           if (m == MATCH_NO)
5312             gfc_syntax_error (ST_ENTRY);
5313           if (m != MATCH_YES)
5314             return MATCH_ERROR;
5315
5316           if (result)
5317             {
5318               if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5319                   || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5320                   || gfc_add_function (&entry->attr, result->name, NULL)
5321                   == FAILURE)
5322                 return MATCH_ERROR;
5323               entry->result = result;
5324             }
5325           else
5326             {
5327               if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5328                   || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5329                 return MATCH_ERROR;
5330               entry->result = entry;
5331             }
5332         }
5333     }
5334
5335   if (gfc_match_eos () != MATCH_YES)
5336     {
5337       gfc_syntax_error (ST_ENTRY);
5338       return MATCH_ERROR;
5339     }
5340
5341   entry->attr.recursive = proc->attr.recursive;
5342   entry->attr.elemental = proc->attr.elemental;
5343   entry->attr.pure = proc->attr.pure;
5344
5345   el = gfc_get_entry_list ();
5346   el->sym = entry;
5347   el->next = gfc_current_ns->entries;
5348   gfc_current_ns->entries = el;
5349   if (el->next)
5350     el->id = el->next->id + 1;
5351   else
5352     el->id = 1;
5353
5354   new_st.op = EXEC_ENTRY;
5355   new_st.ext.entry = el;
5356
5357   return MATCH_YES;
5358 }
5359
5360
5361 /* Match a subroutine statement, including optional prefixes.  */
5362
5363 match
5364 gfc_match_subroutine (void)
5365 {
5366   char name[GFC_MAX_SYMBOL_LEN + 1];
5367   gfc_symbol *sym;
5368   match m;
5369   match is_bind_c;
5370   char peek_char;
5371   bool allow_binding_name;
5372
5373   if (gfc_current_state () != COMP_NONE
5374       && gfc_current_state () != COMP_INTERFACE
5375       && gfc_current_state () != COMP_CONTAINS)
5376     return MATCH_NO;
5377
5378   m = gfc_match_prefix (NULL);
5379   if (m != MATCH_YES)
5380     return m;
5381
5382   m = gfc_match ("subroutine% %n", name);
5383   if (m != MATCH_YES)
5384     return m;
5385
5386   if (get_proc_name (name, &sym, false))
5387     return MATCH_ERROR;
5388
5389   /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5390      the symbol existed before. */
5391   sym->declared_at = gfc_current_locus;
5392
5393   if (add_hidden_procptr_result (sym) == SUCCESS)
5394     sym = sym->result;
5395
5396   gfc_new_block = sym;
5397
5398   /* Check what next non-whitespace character is so we can tell if there
5399      is the required parens if we have a BIND(C).  */
5400   gfc_gobble_whitespace ();
5401   peek_char = gfc_peek_ascii_char ();
5402   
5403   if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5404     return MATCH_ERROR;
5405
5406   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5407     return MATCH_ERROR;
5408
5409   /* Make sure that it isn't already declared as BIND(C).  If it is, it
5410      must have been marked BIND(C) with a BIND(C) attribute and that is
5411      not allowed for procedures.  */
5412   if (sym->attr.is_bind_c == 1)
5413     {
5414       sym->attr.is_bind_c = 0;
5415       if (sym->old_symbol != NULL)
5416         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5417                        "variables or common blocks",
5418                        &(sym->old_symbol->declared_at));
5419       else
5420         gfc_error_now ("BIND(C) attribute at %L can only be used for "
5421                        "variables or common blocks", &gfc_current_locus);
5422     }
5423
5424   /* C binding names are not allowed for internal procedures.  */
5425   if (gfc_current_state () == COMP_CONTAINS
5426       && sym->ns->proc_name->attr.flavor != FL_MODULE)
5427     allow_binding_name = false;
5428   else
5429     allow_binding_name = true;
5430
5431   /* Here, we are just checking if it has the bind(c) attribute, and if
5432      so, then we need to make sure it's all correct.  If it doesn't,
5433      we still need to continue matching the rest of the subroutine line.  */
5434   is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5435   if (is_bind_c == MATCH_ERROR)
5436     {
5437       /* There was an attempt at the bind(c), but it was wrong.  An
5438          error message should have been printed w/in the gfc_match_bind_c
5439          so here we'll just return the MATCH_ERROR.  */
5440       return MATCH_ERROR;
5441     }
5442
5443   if (is_bind_c == MATCH_YES)
5444     {
5445       /* The following is allowed in the Fortran 2008 draft.  */
5446       if (gfc_current_state () == COMP_CONTAINS
5447           && sym->ns->proc_name->attr.flavor != FL_MODULE
5448           && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5449                              "at %L may not be specified for an internal "
5450                              "procedure", &gfc_current_locus)
5451              == FAILURE)
5452         return MATCH_ERROR;
5453
5454       if (peek_char != '(')
5455         {
5456           gfc_error ("Missing required parentheses before BIND(C) at %C");
5457           return MATCH_ERROR;
5458         }
5459       if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5460           == FAILURE)
5461         return MATCH_ERROR;
5462     }
5463   
5464   if (gfc_match_eos () != MATCH_YES)
5465     {
5466       gfc_syntax_error (ST_SUBROUTINE);
5467       return MATCH_ERROR;
5468     }
5469
5470   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5471     return MATCH_ERROR;
5472
5473   /* Warn if it has the same name as an intrinsic.  */
5474   warn_intrinsic_shadow (sym, false);
5475
5476   return MATCH_YES;
5477 }
5478
5479
5480 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5481    given, and set the binding label in either the given symbol (if not
5482    NULL), or in the current_ts.  The symbol may be NULL because we may
5483    encounter the BIND(C) before the declaration itself.  Return
5484    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5485    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5486    or MATCH_YES if the specifier was correct and the binding label and
5487    bind(c) fields were set correctly for the given symbol or the
5488    current_ts. If allow_binding_name is false, no binding name may be
5489    given.  */
5490
5491 match
5492 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5493 {
5494   /* binding label, if exists */   
5495   char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5496   match double_quote;
5497   match single_quote;
5498
5499   /* Initialize the flag that specifies whether we encountered a NAME= 
5500      specifier or not.  */
5501   has_name_equals = 0;
5502
5503   /* Init the first char to nil so we can catch if we don't have
5504      the label (name attr) or the symbol name yet.  */
5505   binding_label[0] = '\0';
5506    
5507   /* This much we have to be able to match, in this order, if
5508      there is a bind(c) label.  */
5509   if (gfc_match (" bind ( c ") != MATCH_YES)
5510     return MATCH_NO;
5511
5512   /* Now see if there is a binding label, or if we've reached the
5513      end of the bind(c) attribute without one.  */
5514   if (gfc_match_char (',') == MATCH_YES)
5515     {
5516       if (gfc_match (" name = ") != MATCH_YES)
5517         {
5518           gfc_error ("Syntax error in NAME= specifier for binding label "
5519                      "at %C");
5520           /* should give an error message here */
5521           return MATCH_ERROR;
5522         }
5523
5524       has_name_equals = 1;
5525
5526       /* Get the opening quote.  */
5527       double_quote = MATCH_YES;
5528       single_quote = MATCH_YES;
5529       double_quote = gfc_match_char ('"');
5530       if (double_quote != MATCH_YES)
5531         single_quote = gfc_match_char ('\'');
5532       if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5533         {
5534           gfc_error ("Syntax error in NAME= specifier for binding label "
5535                      "at %C");
5536           return MATCH_ERROR;
5537         }
5538       
5539       /* Grab the binding label, using functions that will not lower
5540          case the names automatically.  */
5541       if (gfc_match_name_C (binding_label) != MATCH_YES)
5542          return MATCH_ERROR;
5543       
5544       /* Get the closing quotation.  */
5545       if (double_quote == MATCH_YES)
5546         {
5547           if (gfc_match_char ('"') != MATCH_YES)
5548             {
5549               gfc_error ("Missing closing quote '\"' for binding label at %C");
5550               /* User started string with '"' so looked to match it.  */
5551               return MATCH_ERROR;
5552             }
5553         }
5554       else
5555         {
5556           if (gfc_match_char ('\'') != MATCH_YES)
5557             {
5558               gfc_error ("Missing closing quote '\'' for binding label at %C");
5559               /* User started string with "'" char.  */
5560               return MATCH_ERROR;
5561             }
5562         }
5563    }
5564
5565   /* Get the required right paren.  */
5566   if (gfc_match_char (')') != MATCH_YES)
5567     {
5568       gfc_error ("Missing closing paren for binding label at %C");
5569       return MATCH_ERROR;
5570     }
5571
5572   if (has_name_equals && !allow_binding_name)
5573     {
5574       gfc_error ("No binding name is allowed in BIND(C) at %C");
5575       return MATCH_ERROR;
5576     }
5577
5578   if (has_name_equals && sym != NULL && sym->attr.dummy)
5579     {
5580       gfc_error ("For dummy procedure %s, no binding name is "
5581                  "allowed in BIND(C) at %C", sym->name);
5582       return MATCH_ERROR;
5583     }
5584
5585
5586   /* Save the binding label to the symbol.  If sym is null, we're
5587      probably matching the typespec attributes of a declaration and
5588      haven't gotten the name yet, and therefore, no symbol yet.  */
5589   if (binding_label[0] != '\0')
5590     {
5591       if (sym != NULL)
5592       {
5593         strcpy (sym->binding_label, binding_label);
5594       }
5595       else
5596         strcpy (curr_binding_label, binding_label);
5597     }
5598   else if (allow_binding_name)
5599     {
5600       /* No binding label, but if symbol isn't null, we
5601          can set the label for it here.
5602          If name="" or allow_binding_name is false, no C binding name is
5603          created. */
5604       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5605         strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5606     }
5607
5608   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5609       && current_interface.type == INTERFACE_ABSTRACT)
5610     {
5611       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5612       return MATCH_ERROR;
5613     }
5614
5615   return MATCH_YES;
5616 }
5617
5618
5619 /* Return nonzero if we're currently compiling a contained procedure.  */
5620
5621 static int
5622 contained_procedure (void)
5623 {
5624   gfc_state_data *s = gfc_state_stack;
5625
5626   if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5627       && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5628     return 1;
5629
5630   return 0;
5631 }
5632
5633 /* Set the kind of each enumerator.  The kind is selected such that it is
5634    interoperable with the corresponding C enumeration type, making
5635    sure that -fshort-enums is honored.  */
5636
5637 static void
5638 set_enum_kind(void)
5639 {
5640   enumerator_history *current_history = NULL;
5641   int kind;
5642   int i;
5643
5644   if (max_enum == NULL || enum_history == NULL)
5645     return;
5646
5647   if (!flag_short_enums)
5648     return;
5649
5650   i = 0;
5651   do
5652     {
5653       kind = gfc_integer_kinds[i++].kind;
5654     }
5655   while (kind < gfc_c_int_kind
5656          && gfc_check_integer_range (max_enum->initializer->value.integer,
5657                                      kind) != ARITH_OK);
5658
5659   current_history = enum_history;
5660   while (current_history != NULL)
5661     {
5662       current_history->sym->ts.kind = kind;
5663       current_history = current_history->next;
5664     }
5665 }
5666
5667
5668 /* Match any of the various end-block statements.  Returns the type of
5669    END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
5670    and END BLOCK statements cannot be replaced by a single END statement.  */
5671
5672 match
5673 gfc_match_end (gfc_statement *st)
5674 {
5675   char name[GFC_MAX_SYMBOL_LEN + 1];
5676   gfc_compile_state state;
5677   locus old_loc;
5678   const char *block_name;
5679   const char *target;
5680   int eos_ok;
5681   match m;
5682
5683   old_loc = gfc_current_locus;
5684   if (gfc_match ("end") != MATCH_YES)
5685     return MATCH_NO;
5686
5687   state = gfc_current_state ();
5688   block_name = gfc_current_block () == NULL
5689              ? NULL : gfc_current_block ()->name;
5690
5691   switch (state)
5692     {
5693     case COMP_ASSOCIATE:
5694     case COMP_BLOCK:
5695       if (!strcmp (block_name, "block@"))
5696         block_name = NULL;
5697       break;
5698
5699     case COMP_CONTAINS:
5700     case COMP_DERIVED_CONTAINS:
5701       state = gfc_state_stack->previous->state;
5702       block_name = gfc_state_stack->previous->sym == NULL
5703                  ? NULL : gfc_state_stack->previous->sym->name;
5704       break;
5705
5706     default:
5707       break;
5708     }
5709
5710   switch (state)
5711     {
5712     case COMP_NONE:
5713     case COMP_PROGRAM:
5714       *st = ST_END_PROGRAM;
5715       target = " program";
5716       eos_ok = 1;
5717       break;
5718
5719     case COMP_SUBROUTINE:
5720       *st = ST_END_SUBROUTINE;
5721       target = " subroutine";
5722       eos_ok = !contained_procedure ();
5723       break;
5724
5725     case COMP_FUNCTION:
5726       *st = ST_END_FUNCTION;
5727       target = " function";
5728       eos_ok = !contained_procedure ();
5729       break;
5730
5731     case COMP_BLOCK_DATA:
5732       *st = ST_END_BLOCK_DATA;
5733       target = " block data";
5734       eos_ok = 1;
5735       break;
5736
5737     case COMP_MODULE:
5738       *st = ST_END_MODULE;
5739       target = " module";
5740       eos_ok = 1;
5741       break;
5742
5743     case COMP_INTERFACE:
5744       *st = ST_END_INTERFACE;
5745       target = " interface";
5746       eos_ok = 0;
5747       break;
5748
5749     case COMP_DERIVED:
5750     case COMP_DERIVED_CONTAINS:
5751       *st = ST_END_TYPE;
5752       target = " type";
5753       eos_ok = 0;
5754       break;
5755
5756     case COMP_ASSOCIATE:
5757       *st = ST_END_ASSOCIATE;
5758       target = " associate";
5759       eos_ok = 0;
5760       break;
5761
5762     case COMP_BLOCK:
5763       *st = ST_END_BLOCK;
5764       target = " block";
5765       eos_ok = 0;
5766       break;
5767
5768     case COMP_IF:
5769       *st = ST_ENDIF;
5770       target = " if";
5771       eos_ok = 0;
5772       break;
5773
5774     case COMP_DO:
5775       *st = ST_ENDDO;
5776       target = " do";
5777       eos_ok = 0;
5778       break;
5779
5780     case COMP_CRITICAL:
5781       *st = ST_END_CRITICAL;
5782       target = " critical";
5783       eos_ok = 0;
5784       break;
5785
5786     case COMP_SELECT:
5787     case COMP_SELECT_TYPE:
5788       *st = ST_END_SELECT;
5789       target = " select";
5790       eos_ok = 0;
5791       break;
5792
5793     case COMP_FORALL:
5794       *st = ST_END_FORALL;
5795       target = " forall";
5796       eos_ok = 0;
5797       break;
5798
5799     case COMP_WHERE:
5800       *st = ST_END_WHERE;
5801       target = " where";
5802       eos_ok = 0;
5803       break;
5804
5805     case COMP_ENUM:
5806       *st = ST_END_ENUM;
5807       target = " enum";
5808       eos_ok = 0;
5809       last_initializer = NULL;
5810       set_enum_kind ();
5811       gfc_free_enum_history ();
5812       break;
5813
5814     default:
5815       gfc_error ("Unexpected END statement at %C");
5816       goto cleanup;
5817     }
5818
5819   if (gfc_match_eos () == MATCH_YES)
5820     {
5821       if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5822         {
5823           if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5824                               "instead of %s statement at %L",
5825                               gfc_ascii_statement (*st), &old_loc) == FAILURE)
5826             goto cleanup;
5827         }
5828       else if (!eos_ok)
5829         {
5830           /* We would have required END [something].  */
5831           gfc_error ("%s statement expected at %L",
5832                      gfc_ascii_statement (*st), &old_loc);
5833           goto cleanup;
5834         }
5835
5836       return MATCH_YES;
5837     }
5838
5839   /* Verify that we've got the sort of end-block that we're expecting.  */
5840   if (gfc_match (target) != MATCH_YES)
5841     {
5842       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5843       goto cleanup;
5844     }
5845
5846   /* If we're at the end, make sure a block name wasn't required.  */
5847   if (gfc_match_eos () == MATCH_YES)
5848     {
5849
5850       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5851           && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5852           && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5853         return MATCH_YES;
5854
5855       if (!block_name)
5856         return MATCH_YES;
5857
5858       gfc_error ("Expected block name of '%s' in %s statement at %C",
5859                  block_name, gfc_ascii_statement (*st));
5860
5861       return MATCH_ERROR;
5862     }
5863
5864   /* END INTERFACE has a special handler for its several possible endings.  */
5865   if (*st == ST_END_INTERFACE)
5866     return gfc_match_end_interface ();
5867
5868   /* We haven't hit the end of statement, so what is left must be an
5869      end-name.  */
5870   m = gfc_match_space ();
5871   if (m == MATCH_YES)
5872     m = gfc_match_name (name);
5873
5874   if (m == MATCH_NO)
5875     gfc_error ("Expected terminating name at %C");
5876   if (m != MATCH_YES)
5877     goto cleanup;
5878
5879   if (block_name == NULL)
5880     goto syntax;
5881
5882   if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5883     {
5884       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5885                  gfc_ascii_statement (*st));
5886       goto cleanup;
5887     }
5888   /* Procedure pointer as function result.  */
5889   else if (strcmp (block_name, "ppr@") == 0
5890            && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5891     {
5892       gfc_error ("Expected label '%s' for %s statement at %C",
5893                  gfc_current_block ()->ns->proc_name->name,
5894                  gfc_ascii_statement (*st));
5895       goto cleanup;
5896     }
5897
5898   if (gfc_match_eos () == MATCH_YES)
5899     return MATCH_YES;
5900
5901 syntax:
5902   gfc_syntax_error (*st);
5903
5904 cleanup:
5905   gfc_current_locus = old_loc;
5906   return MATCH_ERROR;
5907 }
5908
5909
5910
5911 /***************** Attribute declaration statements ****************/
5912
5913 /* Set the attribute of a single variable.  */
5914
5915 static match
5916 attr_decl1 (void)
5917 {
5918   char name[GFC_MAX_SYMBOL_LEN + 1];
5919   gfc_array_spec *as;
5920   gfc_symbol *sym;
5921   locus var_locus;
5922   match m;
5923
5924   as = NULL;
5925
5926   m = gfc_match_name (name);
5927   if (m != MATCH_YES)
5928     goto cleanup;
5929
5930   if (find_special (name, &sym, false))
5931     return MATCH_ERROR;
5932
5933   var_locus = gfc_current_locus;
5934
5935   /* Deal with possible array specification for certain attributes.  */
5936   if (current_attr.dimension
5937       || current_attr.codimension
5938       || current_attr.allocatable
5939       || current_attr.pointer
5940       || current_attr.target)
5941     {
5942       m = gfc_match_array_spec (&as, !current_attr.codimension,
5943                                 !current_attr.dimension
5944                                 && !current_attr.pointer
5945                                 && !current_attr.target);
5946       if (m == MATCH_ERROR)
5947         goto cleanup;
5948
5949       if (current_attr.dimension && m == MATCH_NO)
5950         {
5951           gfc_error ("Missing array specification at %L in DIMENSION "
5952                      "statement", &var_locus);
5953           m = MATCH_ERROR;
5954           goto cleanup;
5955         }
5956
5957       if (current_attr.dimension && sym->value)
5958         {
5959           gfc_error ("Dimensions specified for %s at %L after its "
5960                      "initialisation", sym->name, &var_locus);
5961           m = MATCH_ERROR;
5962           goto cleanup;
5963         }
5964
5965       if (current_attr.codimension && m == MATCH_NO)
5966         {
5967           gfc_error ("Missing array specification at %L in CODIMENSION "
5968                      "statement", &var_locus);
5969           m = MATCH_ERROR;
5970           goto cleanup;
5971         }
5972
5973       if ((current_attr.allocatable || current_attr.pointer)
5974           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5975         {
5976           gfc_error ("Array specification must be deferred at %L", &var_locus);
5977           m = MATCH_ERROR;
5978           goto cleanup;
5979         }
5980     }
5981
5982   /* Update symbol table.  DIMENSION attribute is set in
5983      gfc_set_array_spec().  For CLASS variables, this must be applied
5984      to the first component, or '$data' field.  */
5985   if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
5986     {
5987       if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr,&var_locus)
5988           == FAILURE)
5989         {
5990           m = MATCH_ERROR;
5991           goto cleanup;
5992         }
5993     }
5994   else
5995     {
5996       if (current_attr.dimension == 0 && current_attr.codimension == 0
5997           && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
5998         {
5999           m = MATCH_ERROR;
6000           goto cleanup;
6001         }
6002     }
6003     
6004   if (sym->ts.type == BT_CLASS && !sym->attr.class_ok
6005       && (sym->attr.class_ok = sym->attr.class_ok || current_attr.allocatable
6006                                || current_attr.pointer))
6007     gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
6008
6009   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6010     {
6011       m = MATCH_ERROR;
6012       goto cleanup;
6013     }
6014
6015   if (sym->attr.cray_pointee && sym->as != NULL)
6016     {
6017       /* Fix the array spec.  */
6018       m = gfc_mod_pointee_as (sym->as);         
6019       if (m == MATCH_ERROR)
6020         goto cleanup;
6021     }
6022
6023   if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6024     {
6025       m = MATCH_ERROR;
6026       goto cleanup;
6027     }
6028
6029   if ((current_attr.external || current_attr.intrinsic)
6030       && sym->attr.flavor != FL_PROCEDURE
6031       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6032     {
6033       m = MATCH_ERROR;
6034       goto cleanup;
6035     }
6036
6037   add_hidden_procptr_result (sym);
6038
6039   return MATCH_YES;
6040
6041 cleanup:
6042   gfc_free_array_spec (as);
6043   return m;
6044 }
6045
6046
6047 /* Generic attribute declaration subroutine.  Used for attributes that
6048    just have a list of names.  */
6049
6050 static match
6051 attr_decl (void)
6052 {
6053   match m;
6054
6055   /* Gobble the optional double colon, by simply ignoring the result
6056      of gfc_match().  */
6057   gfc_match (" ::");
6058
6059   for (;;)
6060     {
6061       m = attr_decl1 ();
6062       if (m != MATCH_YES)
6063         break;
6064
6065       if (gfc_match_eos () == MATCH_YES)
6066         {
6067           m = MATCH_YES;
6068           break;
6069         }
6070
6071       if (gfc_match_char (',') != MATCH_YES)
6072         {
6073           gfc_error ("Unexpected character in variable list at %C");
6074           m = MATCH_ERROR;
6075           break;
6076         }
6077     }
6078
6079   return m;
6080 }
6081
6082
6083 /* This routine matches Cray Pointer declarations of the form:
6084    pointer ( <pointer>, <pointee> )
6085    or
6086    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6087    The pointer, if already declared, should be an integer.  Otherwise, we
6088    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
6089    be either a scalar, or an array declaration.  No space is allocated for
6090    the pointee.  For the statement
6091    pointer (ipt, ar(10))
6092    any subsequent uses of ar will be translated (in C-notation) as
6093    ar(i) => ((<type> *) ipt)(i)
6094    After gimplification, pointee variable will disappear in the code.  */
6095
6096 static match
6097 cray_pointer_decl (void)
6098 {
6099   match m;
6100   gfc_array_spec *as = NULL;
6101   gfc_symbol *cptr; /* Pointer symbol.  */
6102   gfc_symbol *cpte; /* Pointee symbol.  */
6103   locus var_locus;
6104   bool done = false;
6105
6106   while (!done)
6107     {
6108       if (gfc_match_char ('(') != MATCH_YES)
6109         {
6110           gfc_error ("Expected '(' at %C");
6111           return MATCH_ERROR;
6112         }
6113
6114       /* Match pointer.  */
6115       var_locus = gfc_current_locus;
6116       gfc_clear_attr (&current_attr);
6117       gfc_add_cray_pointer (&current_attr, &var_locus);
6118       current_ts.type = BT_INTEGER;
6119       current_ts.kind = gfc_index_integer_kind;
6120
6121       m = gfc_match_symbol (&cptr, 0);
6122       if (m != MATCH_YES)
6123         {
6124           gfc_error ("Expected variable name at %C");
6125           return m;
6126         }
6127
6128       if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6129         return MATCH_ERROR;
6130
6131       gfc_set_sym_referenced (cptr);
6132
6133       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
6134         {
6135           cptr->ts.type = BT_INTEGER;
6136           cptr->ts.kind = gfc_index_integer_kind;
6137         }
6138       else if (cptr->ts.type != BT_INTEGER)
6139         {
6140           gfc_error ("Cray pointer at %C must be an integer");
6141           return MATCH_ERROR;
6142         }
6143       else if (cptr->ts.kind < gfc_index_integer_kind)
6144         gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6145                      " memory addresses require %d bytes",
6146                      cptr->ts.kind, gfc_index_integer_kind);
6147
6148       if (gfc_match_char (',') != MATCH_YES)
6149         {
6150           gfc_error ("Expected \",\" at %C");
6151           return MATCH_ERROR;
6152         }
6153
6154       /* Match Pointee.  */
6155       var_locus = gfc_current_locus;
6156       gfc_clear_attr (&current_attr);
6157       gfc_add_cray_pointee (&current_attr, &var_locus);
6158       current_ts.type = BT_UNKNOWN;
6159       current_ts.kind = 0;
6160
6161       m = gfc_match_symbol (&cpte, 0);
6162       if (m != MATCH_YES)
6163         {
6164           gfc_error ("Expected variable name at %C");
6165           return m;
6166         }
6167
6168       /* Check for an optional array spec.  */
6169       m = gfc_match_array_spec (&as, true, false);
6170       if (m == MATCH_ERROR)
6171         {
6172           gfc_free_array_spec (as);
6173           return m;
6174         }
6175       else if (m == MATCH_NO)
6176         {
6177           gfc_free_array_spec (as);
6178           as = NULL;
6179         }   
6180
6181       if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6182         return MATCH_ERROR;
6183
6184       gfc_set_sym_referenced (cpte);
6185
6186       if (cpte->as == NULL)
6187         {
6188           if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6189             gfc_internal_error ("Couldn't set Cray pointee array spec.");
6190         }
6191       else if (as != NULL)
6192         {
6193           gfc_error ("Duplicate array spec for Cray pointee at %C");
6194           gfc_free_array_spec (as);
6195           return MATCH_ERROR;
6196         }
6197       
6198       as = NULL;
6199     
6200       if (cpte->as != NULL)
6201         {
6202           /* Fix array spec.  */
6203           m = gfc_mod_pointee_as (cpte->as);
6204           if (m == MATCH_ERROR)
6205             return m;
6206         } 
6207    
6208       /* Point the Pointee at the Pointer.  */
6209       cpte->cp_pointer = cptr;
6210
6211       if (gfc_match_char (')') != MATCH_YES)
6212         {
6213           gfc_error ("Expected \")\" at %C");
6214           return MATCH_ERROR;    
6215         }
6216       m = gfc_match_char (',');
6217       if (m != MATCH_YES)
6218         done = true; /* Stop searching for more declarations.  */
6219
6220     }
6221   
6222   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
6223       || gfc_match_eos () != MATCH_YES)
6224     {
6225       gfc_error ("Expected \",\" or end of statement at %C");
6226       return MATCH_ERROR;
6227     }
6228   return MATCH_YES;
6229 }
6230
6231
6232 match
6233 gfc_match_external (void)
6234 {
6235
6236   gfc_clear_attr (&current_attr);
6237   current_attr.external = 1;
6238
6239   return attr_decl ();
6240 }
6241
6242
6243 match
6244 gfc_match_intent (void)
6245 {
6246   sym_intent intent;
6247
6248   /* This is not allowed within a BLOCK construct!  */
6249   if (gfc_current_state () == COMP_BLOCK)
6250     {
6251       gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6252       return MATCH_ERROR;
6253     }
6254
6255   intent = match_intent_spec ();
6256   if (intent == INTENT_UNKNOWN)
6257     return MATCH_ERROR;
6258
6259   gfc_clear_attr (&current_attr);
6260   current_attr.intent = intent;
6261
6262   return attr_decl ();
6263 }
6264
6265
6266 match
6267 gfc_match_intrinsic (void)
6268 {
6269
6270   gfc_clear_attr (&current_attr);
6271   current_attr.intrinsic = 1;
6272
6273   return attr_decl ();
6274 }
6275
6276
6277 match
6278 gfc_match_optional (void)
6279 {
6280   /* This is not allowed within a BLOCK construct!  */
6281   if (gfc_current_state () == COMP_BLOCK)
6282     {
6283       gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6284       return MATCH_ERROR;
6285     }
6286
6287   gfc_clear_attr (&current_attr);
6288   current_attr.optional = 1;
6289
6290   return attr_decl ();
6291 }
6292
6293
6294 match
6295 gfc_match_pointer (void)
6296 {
6297   gfc_gobble_whitespace ();
6298   if (gfc_peek_ascii_char () == '(')
6299     {
6300       if (!gfc_option.flag_cray_pointer)
6301         {
6302           gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6303                      "flag");
6304           return MATCH_ERROR;
6305         }
6306       return cray_pointer_decl ();
6307     }
6308   else
6309     {
6310       gfc_clear_attr (&current_attr);
6311       current_attr.pointer = 1;
6312     
6313       return attr_decl ();
6314     }
6315 }
6316
6317
6318 match
6319 gfc_match_allocatable (void)
6320 {
6321   gfc_clear_attr (&current_attr);
6322   current_attr.allocatable = 1;
6323
6324   return attr_decl ();
6325 }
6326
6327
6328 match
6329 gfc_match_codimension (void)
6330 {
6331   gfc_clear_attr (&current_attr);
6332   current_attr.codimension = 1;
6333
6334   return attr_decl ();
6335 }
6336
6337
6338 match
6339 gfc_match_contiguous (void)
6340 {
6341   if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6342       == FAILURE)
6343     return MATCH_ERROR;
6344
6345   gfc_clear_attr (&current_attr);
6346   current_attr.contiguous = 1;
6347
6348   return attr_decl ();
6349 }
6350
6351
6352 match
6353 gfc_match_dimension (void)
6354 {
6355   gfc_clear_attr (&current_attr);
6356   current_attr.dimension = 1;
6357
6358   return attr_decl ();
6359 }
6360
6361
6362 match
6363 gfc_match_target (void)
6364 {
6365   gfc_clear_attr (&current_attr);
6366   current_attr.target = 1;
6367
6368   return attr_decl ();
6369 }
6370
6371
6372 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6373    statement.  */
6374
6375 static match
6376 access_attr_decl (gfc_statement st)
6377 {
6378   char name[GFC_MAX_SYMBOL_LEN + 1];
6379   interface_type type;
6380   gfc_user_op *uop;
6381   gfc_symbol *sym;
6382   gfc_intrinsic_op op;
6383   match m;
6384
6385   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6386     goto done;
6387
6388   for (;;)
6389     {
6390       m = gfc_match_generic_spec (&type, name, &op);
6391       if (m == MATCH_NO)
6392         goto syntax;
6393       if (m == MATCH_ERROR)
6394         return MATCH_ERROR;
6395
6396       switch (type)
6397         {
6398         case INTERFACE_NAMELESS:
6399         case INTERFACE_ABSTRACT:
6400           goto syntax;
6401
6402         case INTERFACE_GENERIC:
6403           if (gfc_get_symbol (name, NULL, &sym))
6404             goto done;
6405
6406           if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6407                                           ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6408                               sym->name, NULL) == FAILURE)
6409             return MATCH_ERROR;
6410
6411           break;
6412
6413         case INTERFACE_INTRINSIC_OP:
6414           if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6415             {
6416               gfc_current_ns->operator_access[op] =
6417                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6418             }
6419           else
6420             {
6421               gfc_error ("Access specification of the %s operator at %C has "
6422                          "already been specified", gfc_op2string (op));
6423               goto done;
6424             }
6425
6426           break;
6427
6428         case INTERFACE_USER_OP:
6429           uop = gfc_get_uop (name);
6430
6431           if (uop->access == ACCESS_UNKNOWN)
6432             {
6433               uop->access = (st == ST_PUBLIC)
6434                           ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6435             }
6436           else
6437             {
6438               gfc_error ("Access specification of the .%s. operator at %C "
6439                          "has already been specified", sym->name);
6440               goto done;
6441             }
6442
6443           break;
6444         }
6445
6446       if (gfc_match_char (',') == MATCH_NO)
6447         break;
6448     }
6449
6450   if (gfc_match_eos () != MATCH_YES)
6451     goto syntax;
6452   return MATCH_YES;
6453
6454 syntax:
6455   gfc_syntax_error (st);
6456
6457 done:
6458   return MATCH_ERROR;
6459 }
6460
6461
6462 match
6463 gfc_match_protected (void)
6464 {
6465   gfc_symbol *sym;
6466   match m;
6467
6468   if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6469     {
6470        gfc_error ("PROTECTED at %C only allowed in specification "
6471                   "part of a module");
6472        return MATCH_ERROR;
6473
6474     }
6475
6476   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6477       == FAILURE)
6478     return MATCH_ERROR;
6479
6480   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6481     {
6482       return MATCH_ERROR;
6483     }
6484
6485   if (gfc_match_eos () == MATCH_YES)
6486     goto syntax;
6487
6488   for(;;)
6489     {
6490       m = gfc_match_symbol (&sym, 0);
6491       switch (m)
6492         {
6493         case MATCH_YES:
6494           if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6495               == FAILURE)
6496             return MATCH_ERROR;
6497           goto next_item;
6498
6499         case MATCH_NO:
6500           break;
6501
6502         case MATCH_ERROR:
6503           return MATCH_ERROR;
6504         }
6505
6506     next_item:
6507       if (gfc_match_eos () == MATCH_YES)
6508         break;
6509       if (gfc_match_char (',') != MATCH_YES)
6510         goto syntax;
6511     }
6512
6513   return MATCH_YES;
6514
6515 syntax:
6516   gfc_error ("Syntax error in PROTECTED statement at %C");
6517   return MATCH_ERROR;
6518 }
6519
6520
6521 /* The PRIVATE statement is a bit weird in that it can be an attribute
6522    declaration, but also works as a standalone statement inside of a
6523    type declaration or a module.  */
6524
6525 match
6526 gfc_match_private (gfc_statement *st)
6527 {
6528
6529   if (gfc_match ("private") != MATCH_YES)
6530     return MATCH_NO;
6531
6532   if (gfc_current_state () != COMP_MODULE
6533       && !(gfc_current_state () == COMP_DERIVED
6534            && gfc_state_stack->previous
6535            && gfc_state_stack->previous->state == COMP_MODULE)
6536       && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6537            && gfc_state_stack->previous && gfc_state_stack->previous->previous
6538            && gfc_state_stack->previous->previous->state == COMP_MODULE))
6539     {
6540       gfc_error ("PRIVATE statement at %C is only allowed in the "
6541                  "specification part of a module");
6542       return MATCH_ERROR;
6543     }
6544
6545   if (gfc_current_state () == COMP_DERIVED)
6546     {
6547       if (gfc_match_eos () == MATCH_YES)
6548         {
6549           *st = ST_PRIVATE;
6550           return MATCH_YES;
6551         }
6552
6553       gfc_syntax_error (ST_PRIVATE);
6554       return MATCH_ERROR;
6555     }
6556
6557   if (gfc_match_eos () == MATCH_YES)
6558     {
6559       *st = ST_PRIVATE;
6560       return MATCH_YES;
6561     }
6562
6563   *st = ST_ATTR_DECL;
6564   return access_attr_decl (ST_PRIVATE);
6565 }
6566
6567
6568 match
6569 gfc_match_public (gfc_statement *st)
6570 {
6571
6572   if (gfc_match ("public") != MATCH_YES)
6573     return MATCH_NO;
6574
6575   if (gfc_current_state () != COMP_MODULE)
6576     {
6577       gfc_error ("PUBLIC statement at %C is only allowed in the "
6578                  "specification part of a module");
6579       return MATCH_ERROR;
6580     }
6581
6582   if (gfc_match_eos () == MATCH_YES)
6583     {
6584       *st = ST_PUBLIC;
6585       return MATCH_YES;
6586     }
6587
6588   *st = ST_ATTR_DECL;
6589   return access_attr_decl (ST_PUBLIC);
6590 }
6591
6592
6593 /* Workhorse for gfc_match_parameter.  */
6594
6595 static match
6596 do_parm (void)
6597 {
6598   gfc_symbol *sym;
6599   gfc_expr *init;
6600   match m;
6601   gfc_try t;
6602
6603   m = gfc_match_symbol (&sym, 0);
6604   if (m == MATCH_NO)
6605     gfc_error ("Expected variable name at %C in PARAMETER statement");
6606
6607   if (m != MATCH_YES)
6608     return m;
6609
6610   if (gfc_match_char ('=') == MATCH_NO)
6611     {
6612       gfc_error ("Expected = sign in PARAMETER statement at %C");
6613       return MATCH_ERROR;
6614     }
6615
6616   m = gfc_match_init_expr (&init);
6617   if (m == MATCH_NO)
6618     gfc_error ("Expected expression at %C in PARAMETER statement");
6619   if (m != MATCH_YES)
6620     return m;
6621
6622   if (sym->ts.type == BT_UNKNOWN
6623       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6624     {
6625       m = MATCH_ERROR;
6626       goto cleanup;
6627     }
6628
6629   if (gfc_check_assign_symbol (sym, init) == FAILURE
6630       || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6631     {
6632       m = MATCH_ERROR;
6633       goto cleanup;
6634     }
6635
6636   if (sym->value)
6637     {
6638       gfc_error ("Initializing already initialized variable at %C");
6639       m = MATCH_ERROR;
6640       goto cleanup;
6641     }
6642
6643   t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6644   return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6645
6646 cleanup:
6647   gfc_free_expr (init);
6648   return m;
6649 }
6650
6651
6652 /* Match a parameter statement, with the weird syntax that these have.  */
6653
6654 match
6655 gfc_match_parameter (void)
6656 {
6657   match m;
6658
6659   if (gfc_match_char ('(') == MATCH_NO)
6660     return MATCH_NO;
6661
6662   for (;;)
6663     {
6664       m = do_parm ();
6665       if (m != MATCH_YES)
6666         break;
6667
6668       if (gfc_match (" )%t") == MATCH_YES)
6669         break;
6670
6671       if (gfc_match_char (',') != MATCH_YES)
6672         {
6673           gfc_error ("Unexpected characters in PARAMETER statement at %C");
6674           m = MATCH_ERROR;
6675           break;
6676         }
6677     }
6678
6679   return m;
6680 }
6681
6682
6683 /* Save statements have a special syntax.  */
6684
6685 match
6686 gfc_match_save (void)
6687 {
6688   char n[GFC_MAX_SYMBOL_LEN+1];
6689   gfc_common_head *c;
6690   gfc_symbol *sym;
6691   match m;
6692
6693   if (gfc_match_eos () == MATCH_YES)
6694     {
6695       if (gfc_current_ns->seen_save)
6696         {
6697           if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6698                               "follows previous SAVE statement")
6699               == FAILURE)
6700             return MATCH_ERROR;
6701         }
6702
6703       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6704       return MATCH_YES;
6705     }
6706
6707   if (gfc_current_ns->save_all)
6708     {
6709       if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6710                           "blanket SAVE statement")
6711           == FAILURE)
6712         return MATCH_ERROR;
6713     }
6714
6715   gfc_match (" ::");
6716
6717   for (;;)
6718     {
6719       m = gfc_match_symbol (&sym, 0);
6720       switch (m)
6721         {
6722         case MATCH_YES:
6723           if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6724               == FAILURE)
6725             return MATCH_ERROR;
6726           goto next_item;
6727
6728         case MATCH_NO:
6729           break;
6730
6731         case MATCH_ERROR:
6732           return MATCH_ERROR;
6733         }
6734
6735       m = gfc_match (" / %n /", &n);
6736       if (m == MATCH_ERROR)
6737         return MATCH_ERROR;
6738       if (m == MATCH_NO)
6739         goto syntax;
6740
6741       c = gfc_get_common (n, 0);
6742       c->saved = 1;
6743
6744       gfc_current_ns->seen_save = 1;
6745
6746     next_item:
6747       if (gfc_match_eos () == MATCH_YES)
6748         break;
6749       if (gfc_match_char (',') != MATCH_YES)
6750         goto syntax;
6751     }
6752
6753   return MATCH_YES;
6754
6755 syntax:
6756   gfc_error ("Syntax error in SAVE statement at %C");
6757   return MATCH_ERROR;
6758 }
6759
6760
6761 match
6762 gfc_match_value (void)
6763 {
6764   gfc_symbol *sym;
6765   match m;
6766
6767   /* This is not allowed within a BLOCK construct!  */
6768   if (gfc_current_state () == COMP_BLOCK)
6769     {
6770       gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6771       return MATCH_ERROR;
6772     }
6773
6774   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6775       == FAILURE)
6776     return MATCH_ERROR;
6777
6778   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6779     {
6780       return MATCH_ERROR;
6781     }
6782
6783   if (gfc_match_eos () == MATCH_YES)
6784     goto syntax;
6785
6786   for(;;)
6787     {
6788       m = gfc_match_symbol (&sym, 0);
6789       switch (m)
6790         {
6791         case MATCH_YES:
6792           if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6793               == FAILURE)
6794             return MATCH_ERROR;
6795           goto next_item;
6796
6797         case MATCH_NO:
6798           break;
6799
6800         case MATCH_ERROR:
6801           return MATCH_ERROR;
6802         }
6803
6804     next_item:
6805       if (gfc_match_eos () == MATCH_YES)
6806         break;
6807       if (gfc_match_char (',') != MATCH_YES)
6808         goto syntax;
6809     }
6810
6811   return MATCH_YES;
6812
6813 syntax:
6814   gfc_error ("Syntax error in VALUE statement at %C");
6815   return MATCH_ERROR;
6816 }
6817
6818
6819 match
6820 gfc_match_volatile (void)
6821 {
6822   gfc_symbol *sym;
6823   match m;
6824
6825   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6826       == FAILURE)
6827     return MATCH_ERROR;
6828
6829   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6830     {
6831       return MATCH_ERROR;
6832     }
6833
6834   if (gfc_match_eos () == MATCH_YES)
6835     goto syntax;
6836
6837   for(;;)
6838     {
6839       /* VOLATILE is special because it can be added to host-associated 
6840          symbols locally. Except for coarrays. */
6841       m = gfc_match_symbol (&sym, 1);
6842       switch (m)
6843         {
6844         case MATCH_YES:
6845           /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6846              for variable in a BLOCK which is defined outside of the BLOCK.  */
6847           if (sym->ns != gfc_current_ns && sym->attr.codimension)
6848             {
6849               gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6850                          "%C, which is use-/host-associated", sym->name);
6851               return MATCH_ERROR;
6852             }
6853           if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6854               == FAILURE)
6855             return MATCH_ERROR;
6856           goto next_item;
6857
6858         case MATCH_NO:
6859           break;
6860
6861         case MATCH_ERROR:
6862           return MATCH_ERROR;
6863         }
6864
6865     next_item:
6866       if (gfc_match_eos () == MATCH_YES)
6867         break;
6868       if (gfc_match_char (',') != MATCH_YES)
6869         goto syntax;
6870     }
6871
6872   return MATCH_YES;
6873
6874 syntax:
6875   gfc_error ("Syntax error in VOLATILE statement at %C");
6876   return MATCH_ERROR;
6877 }
6878
6879
6880 match
6881 gfc_match_asynchronous (void)
6882 {
6883   gfc_symbol *sym;
6884   match m;
6885
6886   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6887       == FAILURE)
6888     return MATCH_ERROR;
6889
6890   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6891     {
6892       return MATCH_ERROR;
6893     }
6894
6895   if (gfc_match_eos () == MATCH_YES)
6896     goto syntax;
6897
6898   for(;;)
6899     {
6900       /* ASYNCHRONOUS is special because it can be added to host-associated 
6901          symbols locally.  */
6902       m = gfc_match_symbol (&sym, 1);
6903       switch (m)
6904         {
6905         case MATCH_YES:
6906           if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6907               == FAILURE)
6908             return MATCH_ERROR;
6909           goto next_item;
6910
6911         case MATCH_NO:
6912           break;
6913
6914         case MATCH_ERROR:
6915           return MATCH_ERROR;
6916         }
6917
6918     next_item:
6919       if (gfc_match_eos () == MATCH_YES)
6920         break;
6921       if (gfc_match_char (',') != MATCH_YES)
6922         goto syntax;
6923     }
6924
6925   return MATCH_YES;
6926
6927 syntax:
6928   gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6929   return MATCH_ERROR;
6930 }
6931
6932
6933 /* Match a module procedure statement.  Note that we have to modify
6934    symbols in the parent's namespace because the current one was there
6935    to receive symbols that are in an interface's formal argument list.  */
6936
6937 match
6938 gfc_match_modproc (void)
6939 {
6940   char name[GFC_MAX_SYMBOL_LEN + 1];
6941   gfc_symbol *sym;
6942   match m;
6943   gfc_namespace *module_ns;
6944   gfc_interface *old_interface_head, *interface;
6945
6946   if (gfc_state_stack->state != COMP_INTERFACE
6947       || gfc_state_stack->previous == NULL
6948       || current_interface.type == INTERFACE_NAMELESS
6949       || current_interface.type == INTERFACE_ABSTRACT)
6950     {
6951       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6952                  "interface");
6953       return MATCH_ERROR;
6954     }
6955
6956   module_ns = gfc_current_ns->parent;
6957   for (; module_ns; module_ns = module_ns->parent)
6958     if (module_ns->proc_name->attr.flavor == FL_MODULE
6959         || module_ns->proc_name->attr.flavor == FL_PROGRAM
6960         || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
6961             && !module_ns->proc_name->attr.contained))
6962       break;
6963
6964   if (module_ns == NULL)
6965     return MATCH_ERROR;
6966
6967   /* Store the current state of the interface. We will need it if we
6968      end up with a syntax error and need to recover.  */
6969   old_interface_head = gfc_current_interface_head ();
6970
6971   for (;;)
6972     {
6973       locus old_locus = gfc_current_locus;
6974       bool last = false;
6975
6976       m = gfc_match_name (name);
6977       if (m == MATCH_NO)
6978         goto syntax;
6979       if (m != MATCH_YES)
6980         return MATCH_ERROR;
6981
6982       /* Check for syntax error before starting to add symbols to the
6983          current namespace.  */
6984       if (gfc_match_eos () == MATCH_YES)
6985         last = true;
6986       if (!last && gfc_match_char (',') != MATCH_YES)
6987         goto syntax;
6988
6989       /* Now we're sure the syntax is valid, we process this item
6990          further.  */
6991       if (gfc_get_symbol (name, module_ns, &sym))
6992         return MATCH_ERROR;
6993
6994       if (sym->attr.intrinsic)
6995         {
6996           gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
6997                      "PROCEDURE", &old_locus);
6998           return MATCH_ERROR;
6999         }
7000
7001       if (sym->attr.proc != PROC_MODULE
7002           && gfc_add_procedure (&sym->attr, PROC_MODULE,
7003                                 sym->name, NULL) == FAILURE)
7004         return MATCH_ERROR;
7005
7006       if (gfc_add_interface (sym) == FAILURE)
7007         return MATCH_ERROR;
7008
7009       sym->attr.mod_proc = 1;
7010       sym->declared_at = old_locus;
7011
7012       if (last)
7013         break;
7014     }
7015
7016   return MATCH_YES;
7017
7018 syntax:
7019   /* Restore the previous state of the interface.  */
7020   interface = gfc_current_interface_head ();
7021   gfc_set_current_interface_head (old_interface_head);
7022
7023   /* Free the new interfaces.  */
7024   while (interface != old_interface_head)
7025   {
7026     gfc_interface *i = interface->next;
7027     gfc_free (interface);
7028     interface = i;
7029   }
7030
7031   /* And issue a syntax error.  */
7032   gfc_syntax_error (ST_MODULE_PROC);
7033   return MATCH_ERROR;
7034 }
7035
7036
7037 /* Check a derived type that is being extended.  */
7038 static gfc_symbol*
7039 check_extended_derived_type (char *name)
7040 {
7041   gfc_symbol *extended;
7042
7043   if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7044     {
7045       gfc_error ("Ambiguous symbol in TYPE definition at %C");
7046       return NULL;
7047     }
7048
7049   if (!extended)
7050     {
7051       gfc_error ("No such symbol in TYPE definition at %C");
7052       return NULL;
7053     }
7054
7055   if (extended->attr.flavor != FL_DERIVED)
7056     {
7057       gfc_error ("'%s' in EXTENDS expression at %C is not a "
7058                  "derived type", name);
7059       return NULL;
7060     }
7061
7062   if (extended->attr.is_bind_c)
7063     {
7064       gfc_error ("'%s' cannot be extended at %C because it "
7065                  "is BIND(C)", extended->name);
7066       return NULL;
7067     }
7068
7069   if (extended->attr.sequence)
7070     {
7071       gfc_error ("'%s' cannot be extended at %C because it "
7072                  "is a SEQUENCE type", extended->name);
7073       return NULL;
7074     }
7075
7076   return extended;
7077 }
7078
7079
7080 /* Match the optional attribute specifiers for a type declaration.
7081    Return MATCH_ERROR if an error is encountered in one of the handled
7082    attributes (public, private, bind(c)), MATCH_NO if what's found is
7083    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
7084    checking on attribute conflicts needs to be done.  */
7085
7086 match
7087 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7088 {
7089   /* See if the derived type is marked as private.  */
7090   if (gfc_match (" , private") == MATCH_YES)
7091     {
7092       if (gfc_current_state () != COMP_MODULE)
7093         {
7094           gfc_error ("Derived type at %C can only be PRIVATE in the "
7095                      "specification part of a module");
7096           return MATCH_ERROR;
7097         }
7098
7099       if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7100         return MATCH_ERROR;
7101     }
7102   else if (gfc_match (" , public") == MATCH_YES)
7103     {
7104       if (gfc_current_state () != COMP_MODULE)
7105         {
7106           gfc_error ("Derived type at %C can only be PUBLIC in the "
7107                      "specification part of a module");
7108           return MATCH_ERROR;
7109         }
7110
7111       if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7112         return MATCH_ERROR;
7113     }
7114   else if (gfc_match (" , bind ( c )") == MATCH_YES)
7115     {
7116       /* If the type is defined to be bind(c) it then needs to make
7117          sure that all fields are interoperable.  This will
7118          need to be a semantic check on the finished derived type.
7119          See 15.2.3 (lines 9-12) of F2003 draft.  */
7120       if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7121         return MATCH_ERROR;
7122
7123       /* TODO: attr conflicts need to be checked, probably in symbol.c.  */
7124     }
7125   else if (gfc_match (" , abstract") == MATCH_YES)
7126     {
7127       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7128             == FAILURE)
7129         return MATCH_ERROR;
7130
7131       if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7132         return MATCH_ERROR;
7133     }
7134   else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7135     {
7136       if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7137         return MATCH_ERROR;
7138     }
7139   else
7140     return MATCH_NO;
7141
7142   /* If we get here, something matched.  */
7143   return MATCH_YES;
7144 }
7145
7146
7147 /* Assign a hash value for a derived type. The algorithm is that of
7148    SDBM. The hashed string is '[module_name #] derived_name'.  */
7149 static unsigned int
7150 hash_value (gfc_symbol *sym)
7151 {
7152   unsigned int hash = 0;
7153   const char *c;
7154   int i, len;
7155
7156   /* Hash of the module or procedure name.  */
7157   if (sym->module != NULL)
7158     c = sym->module;
7159   else if (sym->ns && sym->ns->proc_name
7160              && sym->ns->proc_name->attr.flavor == FL_MODULE)
7161     c = sym->ns->proc_name->name;
7162   else
7163     c = NULL;
7164
7165   if (c)
7166     { 
7167       len = strlen (c);
7168       for (i = 0; i < len; i++, c++)
7169         hash =  (hash << 6) + (hash << 16) - hash + (*c);
7170
7171       /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'.  */ 
7172       hash =  (hash << 6) + (hash << 16) - hash + '#';
7173     }
7174
7175   /* Hash of the derived type name.  */
7176   len = strlen (sym->name);
7177   c = sym->name;
7178   for (i = 0; i < len; i++, c++)
7179     hash = (hash << 6) + (hash << 16) - hash + (*c);
7180
7181   /* Return the hash but take the modulus for the sake of module read,
7182      even though this slightly increases the chance of collision.  */
7183   return (hash % 100000000);
7184 }
7185
7186
7187 /* Match the beginning of a derived type declaration.  If a type name
7188    was the result of a function, then it is possible to have a symbol
7189    already to be known as a derived type yet have no components.  */
7190
7191 match
7192 gfc_match_derived_decl (void)
7193 {
7194   char name[GFC_MAX_SYMBOL_LEN + 1];
7195   char parent[GFC_MAX_SYMBOL_LEN + 1];
7196   symbol_attribute attr;
7197   gfc_symbol *sym;
7198   gfc_symbol *extended;
7199   match m;
7200   match is_type_attr_spec = MATCH_NO;
7201   bool seen_attr = false;
7202
7203   if (gfc_current_state () == COMP_DERIVED)
7204     return MATCH_NO;
7205
7206   name[0] = '\0';
7207   parent[0] = '\0';
7208   gfc_clear_attr (&attr);
7209   extended = NULL;
7210
7211   do
7212     {
7213       is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7214       if (is_type_attr_spec == MATCH_ERROR)
7215         return MATCH_ERROR;
7216       if (is_type_attr_spec == MATCH_YES)
7217         seen_attr = true;
7218     } while (is_type_attr_spec == MATCH_YES);
7219
7220   /* Deal with derived type extensions.  The extension attribute has
7221      been added to 'attr' but now the parent type must be found and
7222      checked.  */
7223   if (parent[0])
7224     extended = check_extended_derived_type (parent);
7225
7226   if (parent[0] && !extended)
7227     return MATCH_ERROR;
7228
7229   if (gfc_match (" ::") != MATCH_YES && seen_attr)
7230     {
7231       gfc_error ("Expected :: in TYPE definition at %C");
7232       return MATCH_ERROR;
7233     }
7234
7235   m = gfc_match (" %n%t", name);
7236   if (m != MATCH_YES)
7237     return m;
7238
7239   /* Make sure the name is not the name of an intrinsic type.  */
7240   if (gfc_is_intrinsic_typename (name))
7241     {
7242       gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7243                  "type", name);
7244       return MATCH_ERROR;
7245     }
7246
7247   if (gfc_get_symbol (name, NULL, &sym))
7248     return MATCH_ERROR;
7249
7250   if (sym->ts.type != BT_UNKNOWN)
7251     {
7252       gfc_error ("Derived type name '%s' at %C already has a basic type "
7253                  "of %s", sym->name, gfc_typename (&sym->ts));
7254       return MATCH_ERROR;
7255     }
7256
7257   /* The symbol may already have the derived attribute without the
7258      components.  The ways this can happen is via a function
7259      definition, an INTRINSIC statement or a subtype in another
7260      derived type that is a pointer.  The first part of the AND clause
7261      is true if the symbol is not the return value of a function.  */
7262   if (sym->attr.flavor != FL_DERIVED
7263       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7264     return MATCH_ERROR;
7265
7266   if (sym->components != NULL || sym->attr.zero_comp)
7267     {
7268       gfc_error ("Derived type definition of '%s' at %C has already been "
7269                  "defined", sym->name);
7270       return MATCH_ERROR;
7271     }
7272
7273   if (attr.access != ACCESS_UNKNOWN
7274       && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7275     return MATCH_ERROR;
7276
7277   /* See if the derived type was labeled as bind(c).  */
7278   if (attr.is_bind_c != 0)
7279     sym->attr.is_bind_c = attr.is_bind_c;
7280
7281   /* Construct the f2k_derived namespace if it is not yet there.  */
7282   if (!sym->f2k_derived)
7283     sym->f2k_derived = gfc_get_namespace (NULL, 0);
7284   
7285   if (extended && !sym->components)
7286     {
7287       gfc_component *p;
7288       gfc_symtree *st;
7289
7290       /* Add the extended derived type as the first component.  */
7291       gfc_add_component (sym, parent, &p);
7292       extended->refs++;
7293       gfc_set_sym_referenced (extended);
7294
7295       p->ts.type = BT_DERIVED;
7296       p->ts.u.derived = extended;
7297       p->initializer = gfc_default_initializer (&p->ts);
7298       
7299       /* Set extension level.  */
7300       if (extended->attr.extension == 255)
7301         {
7302           /* Since the extension field is 8 bit wide, we can only have
7303              up to 255 extension levels.  */
7304           gfc_error ("Maximum extension level reached with type '%s' at %L",
7305                      extended->name, &extended->declared_at);
7306           return MATCH_ERROR;
7307         }
7308       sym->attr.extension = extended->attr.extension + 1;
7309
7310       /* Provide the links between the extended type and its extension.  */
7311       if (!extended->f2k_derived)
7312         extended->f2k_derived = gfc_get_namespace (NULL, 0);
7313       st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7314       st->n.sym = sym;
7315     }
7316
7317   if (!sym->hash_value)
7318     /* Set the hash for the compound name for this type.  */
7319     sym->hash_value = hash_value (sym);
7320
7321   /* Take over the ABSTRACT attribute.  */
7322   sym->attr.abstract = attr.abstract;
7323
7324   gfc_new_block = sym;
7325
7326   return MATCH_YES;
7327 }
7328
7329
7330 /* Cray Pointees can be declared as: 
7331       pointer (ipt, a (n,m,...,*))  */
7332
7333 match
7334 gfc_mod_pointee_as (gfc_array_spec *as)
7335 {
7336   as->cray_pointee = true; /* This will be useful to know later.  */
7337   if (as->type == AS_ASSUMED_SIZE)
7338     as->cp_was_assumed = true;
7339   else if (as->type == AS_ASSUMED_SHAPE)
7340     {
7341       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7342       return MATCH_ERROR;
7343     }
7344   return MATCH_YES;
7345 }
7346
7347
7348 /* Match the enum definition statement, here we are trying to match 
7349    the first line of enum definition statement.  
7350    Returns MATCH_YES if match is found.  */
7351
7352 match
7353 gfc_match_enum (void)
7354 {
7355   match m;
7356   
7357   m = gfc_match_eos ();
7358   if (m != MATCH_YES)
7359     return m;
7360
7361   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7362       == FAILURE)
7363     return MATCH_ERROR;
7364
7365   return MATCH_YES;
7366 }
7367
7368
7369 /* Returns an initializer whose value is one higher than the value of the
7370    LAST_INITIALIZER argument.  If the argument is NULL, the
7371    initializers value will be set to zero.  The initializer's kind
7372    will be set to gfc_c_int_kind.
7373
7374    If -fshort-enums is given, the appropriate kind will be selected
7375    later after all enumerators have been parsed.  A warning is issued
7376    here if an initializer exceeds gfc_c_int_kind.  */
7377
7378 static gfc_expr *
7379 enum_initializer (gfc_expr *last_initializer, locus where)
7380 {
7381   gfc_expr *result;
7382   result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7383
7384   mpz_init (result->value.integer);
7385
7386   if (last_initializer != NULL)
7387     {
7388       mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7389       result->where = last_initializer->where;
7390
7391       if (gfc_check_integer_range (result->value.integer,
7392              gfc_c_int_kind) != ARITH_OK)
7393         {
7394           gfc_error ("Enumerator exceeds the C integer type at %C");
7395           return NULL;
7396         }
7397     }
7398   else
7399     {
7400       /* Control comes here, if it's the very first enumerator and no
7401          initializer has been given.  It will be initialized to zero.  */
7402       mpz_set_si (result->value.integer, 0);
7403     }
7404
7405   return result;
7406 }
7407
7408
7409 /* Match a variable name with an optional initializer.  When this
7410    subroutine is called, a variable is expected to be parsed next.
7411    Depending on what is happening at the moment, updates either the
7412    symbol table or the current interface.  */
7413
7414 static match
7415 enumerator_decl (void)
7416 {
7417   char name[GFC_MAX_SYMBOL_LEN + 1];
7418   gfc_expr *initializer;
7419   gfc_array_spec *as = NULL;
7420   gfc_symbol *sym;
7421   locus var_locus;
7422   match m;
7423   gfc_try t;
7424   locus old_locus;
7425
7426   initializer = NULL;
7427   old_locus = gfc_current_locus;
7428
7429   /* When we get here, we've just matched a list of attributes and
7430      maybe a type and a double colon.  The next thing we expect to see
7431      is the name of the symbol.  */
7432   m = gfc_match_name (name);
7433   if (m != MATCH_YES)
7434     goto cleanup;
7435
7436   var_locus = gfc_current_locus;
7437
7438   /* OK, we've successfully matched the declaration.  Now put the
7439      symbol in the current namespace. If we fail to create the symbol,
7440      bail out.  */
7441   if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
7442     {
7443       m = MATCH_ERROR;
7444       goto cleanup;
7445     }
7446
7447   /* The double colon must be present in order to have initializers.
7448      Otherwise the statement is ambiguous with an assignment statement.  */
7449   if (colon_seen)
7450     {
7451       if (gfc_match_char ('=') == MATCH_YES)
7452         {
7453           m = gfc_match_init_expr (&initializer);
7454           if (m == MATCH_NO)
7455             {
7456               gfc_error ("Expected an initialization expression at %C");
7457               m = MATCH_ERROR;
7458             }
7459
7460           if (m != MATCH_YES)
7461             goto cleanup;
7462         }
7463     }
7464
7465   /* If we do not have an initializer, the initialization value of the
7466      previous enumerator (stored in last_initializer) is incremented
7467      by 1 and is used to initialize the current enumerator.  */
7468   if (initializer == NULL)
7469     initializer = enum_initializer (last_initializer, old_locus);
7470
7471   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7472     {
7473       gfc_error ("ENUMERATOR %L not initialized with integer expression",
7474                  &var_locus);
7475       m = MATCH_ERROR;
7476       goto cleanup;
7477     }
7478
7479   /* Store this current initializer, for the next enumerator variable
7480      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
7481      use last_initializer below.  */
7482   last_initializer = initializer;
7483   t = add_init_expr_to_sym (name, &initializer, &var_locus);
7484
7485   /* Maintain enumerator history.  */
7486   gfc_find_symbol (name, NULL, 0, &sym);
7487   create_enum_history (sym, last_initializer);
7488
7489   return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7490
7491 cleanup:
7492   /* Free stuff up and return.  */
7493   gfc_free_expr (initializer);
7494
7495   return m;
7496 }
7497
7498
7499 /* Match the enumerator definition statement.  */
7500
7501 match
7502 gfc_match_enumerator_def (void)
7503 {
7504   match m;
7505   gfc_try t;
7506
7507   gfc_clear_ts (&current_ts);
7508
7509   m = gfc_match (" enumerator");
7510   if (m != MATCH_YES)
7511     return m;
7512
7513   m = gfc_match (" :: ");
7514   if (m == MATCH_ERROR)
7515     return m;
7516
7517   colon_seen = (m == MATCH_YES);
7518
7519   if (gfc_current_state () != COMP_ENUM)
7520     {
7521       gfc_error ("ENUM definition statement expected before %C");
7522       gfc_free_enum_history ();
7523       return MATCH_ERROR;
7524     }
7525
7526   (&current_ts)->type = BT_INTEGER;
7527   (&current_ts)->kind = gfc_c_int_kind;
7528
7529   gfc_clear_attr (&current_attr);
7530   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7531   if (t == FAILURE)
7532     {
7533       m = MATCH_ERROR;
7534       goto cleanup;
7535     }
7536
7537   for (;;)
7538     {
7539       m = enumerator_decl ();
7540       if (m == MATCH_ERROR)
7541         {
7542           gfc_free_enum_history ();
7543           goto cleanup;
7544         }
7545       if (m == MATCH_NO)
7546         break;
7547
7548       if (gfc_match_eos () == MATCH_YES)
7549         goto cleanup;
7550       if (gfc_match_char (',') != MATCH_YES)
7551         break;
7552     }
7553
7554   if (gfc_current_state () == COMP_ENUM)
7555     {
7556       gfc_free_enum_history ();
7557       gfc_error ("Syntax error in ENUMERATOR definition at %C");
7558       m = MATCH_ERROR;
7559     }
7560
7561 cleanup:
7562   gfc_free_array_spec (current_as);
7563   current_as = NULL;
7564   return m;
7565
7566 }
7567
7568
7569 /* Match binding attributes.  */
7570
7571 static match
7572 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7573 {
7574   bool found_passing = false;
7575   bool seen_ptr = false;
7576   match m = MATCH_YES;
7577
7578   /* Intialize to defaults.  Do so even before the MATCH_NO check so that in
7579      this case the defaults are in there.  */
7580   ba->access = ACCESS_UNKNOWN;
7581   ba->pass_arg = NULL;
7582   ba->pass_arg_num = 0;
7583   ba->nopass = 0;
7584   ba->non_overridable = 0;
7585   ba->deferred = 0;
7586   ba->ppc = ppc;
7587
7588   /* If we find a comma, we believe there are binding attributes.  */
7589   m = gfc_match_char (',');
7590   if (m == MATCH_NO)
7591     goto done;
7592
7593   do
7594     {
7595       /* Access specifier.  */
7596
7597       m = gfc_match (" public");
7598       if (m == MATCH_ERROR)
7599         goto error;
7600       if (m == MATCH_YES)
7601         {
7602           if (ba->access != ACCESS_UNKNOWN)
7603             {
7604               gfc_error ("Duplicate access-specifier at %C");
7605               goto error;
7606             }
7607
7608           ba->access = ACCESS_PUBLIC;
7609           continue;
7610         }
7611
7612       m = gfc_match (" private");
7613       if (m == MATCH_ERROR)
7614         goto error;
7615       if (m == MATCH_YES)
7616         {
7617           if (ba->access != ACCESS_UNKNOWN)
7618             {
7619               gfc_error ("Duplicate access-specifier at %C");
7620               goto error;
7621             }
7622
7623           ba->access = ACCESS_PRIVATE;
7624           continue;
7625         }
7626
7627       /* If inside GENERIC, the following is not allowed.  */
7628       if (!generic)
7629         {
7630
7631           /* NOPASS flag.  */
7632           m = gfc_match (" nopass");
7633           if (m == MATCH_ERROR)
7634             goto error;
7635           if (m == MATCH_YES)
7636             {
7637               if (found_passing)
7638                 {
7639                   gfc_error ("Binding attributes already specify passing,"
7640                              " illegal NOPASS at %C");
7641                   goto error;
7642                 }
7643
7644               found_passing = true;
7645               ba->nopass = 1;
7646               continue;
7647             }
7648
7649           /* PASS possibly including argument.  */
7650           m = gfc_match (" pass");
7651           if (m == MATCH_ERROR)
7652             goto error;
7653           if (m == MATCH_YES)
7654             {
7655               char arg[GFC_MAX_SYMBOL_LEN + 1];
7656
7657               if (found_passing)
7658                 {
7659                   gfc_error ("Binding attributes already specify passing,"
7660                              " illegal PASS at %C");
7661                   goto error;
7662                 }
7663
7664               m = gfc_match (" ( %n )", arg);
7665               if (m == MATCH_ERROR)
7666                 goto error;
7667               if (m == MATCH_YES)
7668                 ba->pass_arg = gfc_get_string (arg);
7669               gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7670
7671               found_passing = true;
7672               ba->nopass = 0;
7673               continue;
7674             }
7675
7676           if (ppc)
7677             {
7678               /* POINTER flag.  */
7679               m = gfc_match (" pointer");
7680               if (m == MATCH_ERROR)
7681                 goto error;
7682               if (m == MATCH_YES)
7683                 {
7684                   if (seen_ptr)
7685                     {
7686                       gfc_error ("Duplicate POINTER attribute at %C");
7687                       goto error;
7688                     }
7689
7690                   seen_ptr = true;
7691                   continue;
7692                 }
7693             }
7694           else
7695             {
7696               /* NON_OVERRIDABLE flag.  */
7697               m = gfc_match (" non_overridable");
7698               if (m == MATCH_ERROR)
7699                 goto error;
7700               if (m == MATCH_YES)
7701                 {
7702                   if (ba->non_overridable)
7703                     {
7704                       gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7705                       goto error;
7706                     }
7707
7708                   ba->non_overridable = 1;
7709                   continue;
7710                 }
7711
7712               /* DEFERRED flag.  */
7713               m = gfc_match (" deferred");
7714               if (m == MATCH_ERROR)
7715                 goto error;
7716               if (m == MATCH_YES)
7717                 {
7718                   if (ba->deferred)
7719                     {
7720                       gfc_error ("Duplicate DEFERRED at %C");
7721                       goto error;
7722                     }
7723
7724                   ba->deferred = 1;
7725                   continue;
7726                 }
7727             }
7728
7729         }
7730
7731       /* Nothing matching found.  */
7732       if (generic)
7733         gfc_error ("Expected access-specifier at %C");
7734       else
7735         gfc_error ("Expected binding attribute at %C");
7736       goto error;
7737     }
7738   while (gfc_match_char (',') == MATCH_YES);
7739
7740   /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
7741   if (ba->non_overridable && ba->deferred)
7742     {
7743       gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7744       goto error;
7745     }
7746
7747   m = MATCH_YES;
7748
7749 done:
7750   if (ba->access == ACCESS_UNKNOWN)
7751     ba->access = gfc_typebound_default_access;
7752
7753   if (ppc && !seen_ptr)
7754     {
7755       gfc_error ("POINTER attribute is required for procedure pointer component"
7756                  " at %C");
7757       goto error;
7758     }
7759
7760   return m;
7761
7762 error:
7763   return MATCH_ERROR;
7764 }
7765
7766
7767 /* Match a PROCEDURE specific binding inside a derived type.  */
7768
7769 static match
7770 match_procedure_in_type (void)
7771 {
7772   char name[GFC_MAX_SYMBOL_LEN + 1];
7773   char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7774   char* target = NULL, *ifc = NULL;
7775   gfc_typebound_proc tb;
7776   bool seen_colons;
7777   bool seen_attrs;
7778   match m;
7779   gfc_symtree* stree;
7780   gfc_namespace* ns;
7781   gfc_symbol* block;
7782   int num;
7783
7784   /* Check current state.  */
7785   gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7786   block = gfc_state_stack->previous->sym;
7787   gcc_assert (block);
7788
7789   /* Try to match PROCEDURE(interface).  */
7790   if (gfc_match (" (") == MATCH_YES)
7791     {
7792       m = gfc_match_name (target_buf);
7793       if (m == MATCH_ERROR)
7794         return m;
7795       if (m != MATCH_YES)
7796         {
7797           gfc_error ("Interface-name expected after '(' at %C");
7798           return MATCH_ERROR;
7799         }
7800
7801       if (gfc_match (" )") != MATCH_YES)
7802         {
7803           gfc_error ("')' expected at %C");
7804           return MATCH_ERROR;
7805         }
7806
7807       ifc = target_buf;
7808     }
7809
7810   /* Construct the data structure.  */
7811   memset (&tb, 0, sizeof (tb));
7812   tb.where = gfc_current_locus;
7813
7814   /* Match binding attributes.  */
7815   m = match_binding_attributes (&tb, false, false);
7816   if (m == MATCH_ERROR)
7817     return m;
7818   seen_attrs = (m == MATCH_YES);
7819
7820   /* Check that attribute DEFERRED is given if an interface is specified.  */
7821   if (tb.deferred && !ifc)
7822     {
7823       gfc_error ("Interface must be specified for DEFERRED binding at %C");
7824       return MATCH_ERROR;
7825     }
7826   if (ifc && !tb.deferred)
7827     {
7828       gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7829       return MATCH_ERROR;
7830     }
7831
7832   /* Match the colons.  */
7833   m = gfc_match (" ::");
7834   if (m == MATCH_ERROR)
7835     return m;
7836   seen_colons = (m == MATCH_YES);
7837   if (seen_attrs && !seen_colons)
7838     {
7839       gfc_error ("Expected '::' after binding-attributes at %C");
7840       return MATCH_ERROR;
7841     }
7842
7843   /* Match the binding names.  */ 
7844   for(num=1;;num++)
7845     {
7846       m = gfc_match_name (name);
7847       if (m == MATCH_ERROR)
7848         return m;
7849       if (m == MATCH_NO)
7850         {
7851           gfc_error ("Expected binding name at %C");
7852           return MATCH_ERROR;
7853         }
7854
7855       if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7856                                    " at %C") == FAILURE)
7857         return MATCH_ERROR;
7858
7859       /* Try to match the '=> target', if it's there.  */
7860       target = ifc;
7861       m = gfc_match (" =>");
7862       if (m == MATCH_ERROR)
7863         return m;
7864       if (m == MATCH_YES)
7865         {
7866           if (tb.deferred)
7867             {
7868               gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7869               return MATCH_ERROR;
7870             }
7871
7872           if (!seen_colons)
7873             {
7874               gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7875                          " at %C");
7876               return MATCH_ERROR;
7877             }
7878
7879           m = gfc_match_name (target_buf);
7880           if (m == MATCH_ERROR)
7881             return m;
7882           if (m == MATCH_NO)
7883             {
7884               gfc_error ("Expected binding target after '=>' at %C");
7885               return MATCH_ERROR;
7886             }
7887           target = target_buf;
7888         }
7889
7890       /* If no target was found, it has the same name as the binding.  */
7891       if (!target)
7892         target = name;
7893
7894       /* Get the namespace to insert the symbols into.  */
7895       ns = block->f2k_derived;
7896       gcc_assert (ns);
7897
7898       /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
7899       if (tb.deferred && !block->attr.abstract)
7900         {
7901           gfc_error ("Type '%s' containing DEFERRED binding at %C "
7902                      "is not ABSTRACT", block->name);
7903           return MATCH_ERROR;
7904         }
7905
7906       /* See if we already have a binding with this name in the symtree which
7907          would be an error.  If a GENERIC already targetted this binding, it may
7908          be already there but then typebound is still NULL.  */
7909       stree = gfc_find_symtree (ns->tb_sym_root, name);
7910       if (stree && stree->n.tb)
7911         {
7912           gfc_error ("There is already a procedure with binding name '%s' for "
7913                      "the derived type '%s' at %C", name, block->name);
7914           return MATCH_ERROR;
7915         }
7916
7917       /* Insert it and set attributes.  */
7918
7919       if (!stree)
7920         {
7921           stree = gfc_new_symtree (&ns->tb_sym_root, name);
7922           gcc_assert (stree);
7923         }
7924       stree->n.tb = gfc_get_typebound_proc (&tb);
7925
7926       if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7927                             false))
7928         return MATCH_ERROR;
7929       gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7930   
7931       if (gfc_match_eos () == MATCH_YES)
7932         return MATCH_YES;
7933       if (gfc_match_char (',') != MATCH_YES)
7934         goto syntax;
7935     }
7936
7937 syntax:
7938   gfc_error ("Syntax error in PROCEDURE statement at %C");
7939   return MATCH_ERROR;
7940 }
7941
7942
7943 /* Match a GENERIC procedure binding inside a derived type.  */
7944
7945 match
7946 gfc_match_generic (void)
7947 {
7948   char name[GFC_MAX_SYMBOL_LEN + 1];
7949   char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
7950   gfc_symbol* block;
7951   gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
7952   gfc_typebound_proc* tb;
7953   gfc_namespace* ns;
7954   interface_type op_type;
7955   gfc_intrinsic_op op;
7956   match m;
7957
7958   /* Check current state.  */
7959   if (gfc_current_state () == COMP_DERIVED)
7960     {
7961       gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7962       return MATCH_ERROR;
7963     }
7964   if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7965     return MATCH_NO;
7966   block = gfc_state_stack->previous->sym;
7967   ns = block->f2k_derived;
7968   gcc_assert (block && ns);
7969
7970   memset (&tbattr, 0, sizeof (tbattr));
7971   tbattr.where = gfc_current_locus;
7972
7973   /* See if we get an access-specifier.  */
7974   m = match_binding_attributes (&tbattr, true, false);
7975   if (m == MATCH_ERROR)
7976     goto error;
7977
7978   /* Now the colons, those are required.  */
7979   if (gfc_match (" ::") != MATCH_YES)
7980     {
7981       gfc_error ("Expected '::' at %C");
7982       goto error;
7983     }
7984
7985   /* Match the binding name; depending on type (operator / generic) format
7986      it for future error messages into bind_name.  */
7987  
7988   m = gfc_match_generic_spec (&op_type, name, &op);
7989   if (m == MATCH_ERROR)
7990     return MATCH_ERROR;
7991   if (m == MATCH_NO)
7992     {
7993       gfc_error ("Expected generic name or operator descriptor at %C");
7994       goto error;
7995     }
7996
7997   switch (op_type)
7998     {
7999     case INTERFACE_GENERIC:
8000       snprintf (bind_name, sizeof (bind_name), "%s", name);
8001       break;
8002  
8003     case INTERFACE_USER_OP:
8004       snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8005       break;
8006  
8007     case INTERFACE_INTRINSIC_OP:
8008       snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8009                 gfc_op2string (op));
8010       break;
8011
8012     default:
8013       gcc_unreachable ();
8014     }
8015
8016   /* Match the required =>.  */
8017   if (gfc_match (" =>") != MATCH_YES)
8018     {
8019       gfc_error ("Expected '=>' at %C");
8020       goto error;
8021     }
8022   
8023   /* Try to find existing GENERIC binding with this name / for this operator;
8024      if there is something, check that it is another GENERIC and then extend
8025      it rather than building a new node.  Otherwise, create it and put it
8026      at the right position.  */
8027
8028   switch (op_type)
8029     {
8030     case INTERFACE_USER_OP:
8031     case INTERFACE_GENERIC:
8032       {
8033         const bool is_op = (op_type == INTERFACE_USER_OP);
8034         gfc_symtree* st;
8035
8036         st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8037         if (st)
8038           {
8039             tb = st->n.tb;
8040             gcc_assert (tb);
8041           }
8042         else
8043           tb = NULL;
8044
8045         break;
8046       }
8047
8048     case INTERFACE_INTRINSIC_OP:
8049       tb = ns->tb_op[op];
8050       break;
8051
8052     default:
8053       gcc_unreachable ();
8054     }
8055
8056   if (tb)
8057     {
8058       if (!tb->is_generic)
8059         {
8060           gcc_assert (op_type == INTERFACE_GENERIC);
8061           gfc_error ("There's already a non-generic procedure with binding name"
8062                      " '%s' for the derived type '%s' at %C",
8063                      bind_name, block->name);
8064           goto error;
8065         }
8066
8067       if (tb->access != tbattr.access)
8068         {
8069           gfc_error ("Binding at %C must have the same access as already"
8070                      " defined binding '%s'", bind_name);
8071           goto error;
8072         }
8073     }
8074   else
8075     {
8076       tb = gfc_get_typebound_proc (NULL);
8077       tb->where = gfc_current_locus;
8078       tb->access = tbattr.access;
8079       tb->is_generic = 1;
8080       tb->u.generic = NULL;
8081
8082       switch (op_type)
8083         {
8084         case INTERFACE_GENERIC:
8085         case INTERFACE_USER_OP:
8086           {
8087             const bool is_op = (op_type == INTERFACE_USER_OP);
8088             gfc_symtree* st;
8089
8090             st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8091                                   name);
8092             gcc_assert (st);
8093             st->n.tb = tb;
8094
8095             break;
8096           }
8097           
8098         case INTERFACE_INTRINSIC_OP:
8099           ns->tb_op[op] = tb;
8100           break;
8101
8102         default:
8103           gcc_unreachable ();
8104         }
8105     }
8106
8107   /* Now, match all following names as specific targets.  */
8108   do
8109     {
8110       gfc_symtree* target_st;
8111       gfc_tbp_generic* target;
8112
8113       m = gfc_match_name (name);
8114       if (m == MATCH_ERROR)
8115         goto error;
8116       if (m == MATCH_NO)
8117         {
8118           gfc_error ("Expected specific binding name at %C");
8119           goto error;
8120         }
8121
8122       target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8123
8124       /* See if this is a duplicate specification.  */
8125       for (target = tb->u.generic; target; target = target->next)
8126         if (target_st == target->specific_st)
8127           {
8128             gfc_error ("'%s' already defined as specific binding for the"
8129                        " generic '%s' at %C", name, bind_name);
8130             goto error;
8131           }
8132
8133       target = gfc_get_tbp_generic ();
8134       target->specific_st = target_st;
8135       target->specific = NULL;
8136       target->next = tb->u.generic;
8137       tb->u.generic = target;
8138     }
8139   while (gfc_match (" ,") == MATCH_YES);
8140
8141   /* Here should be the end.  */
8142   if (gfc_match_eos () != MATCH_YES)
8143     {
8144       gfc_error ("Junk after GENERIC binding at %C");
8145       goto error;
8146     }
8147
8148   return MATCH_YES;
8149
8150 error:
8151   return MATCH_ERROR;
8152 }
8153
8154
8155 /* Match a FINAL declaration inside a derived type.  */
8156
8157 match
8158 gfc_match_final_decl (void)
8159 {
8160   char name[GFC_MAX_SYMBOL_LEN + 1];
8161   gfc_symbol* sym;
8162   match m;
8163   gfc_namespace* module_ns;
8164   bool first, last;
8165   gfc_symbol* block;
8166
8167   if (gfc_current_form == FORM_FREE)
8168     {
8169       char c = gfc_peek_ascii_char ();
8170       if (!gfc_is_whitespace (c) && c != ':')
8171         return MATCH_NO;
8172     }
8173   
8174   if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8175     {
8176       if (gfc_current_form == FORM_FIXED)
8177         return MATCH_NO;
8178
8179       gfc_error ("FINAL declaration at %C must be inside a derived type "
8180                  "CONTAINS section");
8181       return MATCH_ERROR;
8182     }
8183
8184   block = gfc_state_stack->previous->sym;
8185   gcc_assert (block);
8186
8187   if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8188       || gfc_state_stack->previous->previous->state != COMP_MODULE)
8189     {
8190       gfc_error ("Derived type declaration with FINAL at %C must be in the"
8191                  " specification part of a MODULE");
8192       return MATCH_ERROR;
8193     }
8194
8195   module_ns = gfc_current_ns;
8196   gcc_assert (module_ns);
8197   gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8198
8199   /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
8200   if (gfc_match (" ::") == MATCH_ERROR)
8201     return MATCH_ERROR;
8202
8203   /* Match the sequence of procedure names.  */
8204   first = true;
8205   last = false;
8206   do
8207     {
8208       gfc_finalizer* f;
8209
8210       if (first && gfc_match_eos () == MATCH_YES)
8211         {
8212           gfc_error ("Empty FINAL at %C");
8213           return MATCH_ERROR;
8214         }
8215
8216       m = gfc_match_name (name);
8217       if (m == MATCH_NO)
8218         {
8219           gfc_error ("Expected module procedure name at %C");
8220           return MATCH_ERROR;
8221         }
8222       else if (m != MATCH_YES)
8223         return MATCH_ERROR;
8224
8225       if (gfc_match_eos () == MATCH_YES)
8226         last = true;
8227       if (!last && gfc_match_char (',') != MATCH_YES)
8228         {
8229           gfc_error ("Expected ',' at %C");
8230           return MATCH_ERROR;
8231         }
8232
8233       if (gfc_get_symbol (name, module_ns, &sym))
8234         {
8235           gfc_error ("Unknown procedure name \"%s\" at %C", name);
8236           return MATCH_ERROR;
8237         }
8238
8239       /* Mark the symbol as module procedure.  */
8240       if (sym->attr.proc != PROC_MODULE
8241           && gfc_add_procedure (&sym->attr, PROC_MODULE,
8242                                 sym->name, NULL) == FAILURE)
8243         return MATCH_ERROR;
8244
8245       /* Check if we already have this symbol in the list, this is an error.  */
8246       for (f = block->f2k_derived->finalizers; f; f = f->next)
8247         if (f->proc_sym == sym)
8248           {
8249             gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8250                        name);
8251             return MATCH_ERROR;
8252           }
8253
8254       /* Add this symbol to the list of finalizers.  */
8255       gcc_assert (block->f2k_derived);
8256       ++sym->refs;
8257       f = XCNEW (gfc_finalizer);
8258       f->proc_sym = sym;
8259       f->proc_tree = NULL;
8260       f->where = gfc_current_locus;
8261       f->next = block->f2k_derived->finalizers;
8262       block->f2k_derived->finalizers = f;
8263
8264       first = false;
8265     }
8266   while (!last);
8267
8268   return MATCH_YES;
8269 }
8270
8271
8272 const ext_attr_t ext_attr_list[] = {
8273   { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8274   { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8275   { "cdecl",     EXT_ATTR_CDECL,     "cdecl"     },
8276   { "stdcall",   EXT_ATTR_STDCALL,   "stdcall"   },
8277   { "fastcall",  EXT_ATTR_FASTCALL,  "fastcall"  },
8278   { NULL,        EXT_ATTR_LAST,      NULL        }
8279 };
8280
8281 /* Match a !GCC$ ATTRIBUTES statement of the form:
8282       !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8283    When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8284
8285    TODO: We should support all GCC attributes using the same syntax for
8286    the attribute list, i.e. the list in C
8287       __attributes(( attribute-list ))
8288    matches then
8289       !GCC$ ATTRIBUTES attribute-list ::
8290    Cf. c-parser.c's c_parser_attributes; the data can then directly be
8291    saved into a TREE.
8292
8293    As there is absolutely no risk of confusion, we should never return
8294    MATCH_NO.  */
8295 match
8296 gfc_match_gcc_attributes (void)
8297
8298   symbol_attribute attr;
8299   char name[GFC_MAX_SYMBOL_LEN + 1];
8300   unsigned id;
8301   gfc_symbol *sym;
8302   match m;
8303
8304   gfc_clear_attr (&attr);
8305   for(;;)
8306     {
8307       char ch;
8308
8309       if (gfc_match_name (name) != MATCH_YES)
8310         return MATCH_ERROR;
8311
8312       for (id = 0; id < EXT_ATTR_LAST; id++)
8313         if (strcmp (name, ext_attr_list[id].name) == 0)
8314           break;
8315
8316       if (id == EXT_ATTR_LAST)
8317         {
8318           gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8319           return MATCH_ERROR;
8320         }
8321
8322       if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8323           == FAILURE)
8324         return MATCH_ERROR;
8325
8326       gfc_gobble_whitespace ();
8327       ch = gfc_next_ascii_char ();
8328       if (ch == ':')
8329         {
8330           /* This is the successful exit condition for the loop.  */
8331           if (gfc_next_ascii_char () == ':')
8332             break;
8333         }
8334
8335       if (ch == ',')
8336         continue;
8337
8338       goto syntax;
8339     }
8340
8341   if (gfc_match_eos () == MATCH_YES)
8342     goto syntax;
8343
8344   for(;;)
8345     {
8346       m = gfc_match_name (name);
8347       if (m != MATCH_YES)
8348         return m;
8349
8350       if (find_special (name, &sym, true))
8351         return MATCH_ERROR;
8352       
8353       sym->attr.ext_attr |= attr.ext_attr;
8354
8355       if (gfc_match_eos () == MATCH_YES)
8356         break;
8357
8358       if (gfc_match_char (',') != MATCH_YES)
8359         goto syntax;
8360     }
8361
8362   return MATCH_YES;
8363
8364 syntax:
8365   gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8366   return MATCH_ERROR;
8367 }