OSDN Git Service

2007-10-31 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / decl.c
1 /* Declaration statement matcher
2    Copyright (C) 2002, 2004, 2005, 2006, 2007
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27
28
29 /* Macros to access allocate memory for gfc_data_variable,
30    gfc_data_value and gfc_data.  */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
34
35
36 /* This flag is set if an old-style length selector is matched
37    during a type-declaration statement.  */
38
39 static int old_char_selector;
40
41 /* When variables acquire types and attributes from a declaration
42    statement, they get them from the following static variables.  The
43    first part of a declaration sets these variables and the second
44    part copies these into symbol structures.  */
45
46 static gfc_typespec current_ts;
47
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
51
52 /* The current binding label (if any).  */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55    line in case we're given the BIND(C) attribute with a NAME= specifier.  */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58    can supply a name if the curr_binding_label is nil and NAME= was not.  */
59 static int has_name_equals = 0;
60
61 /* Initializer of the previous enumerator.  */
62
63 static gfc_expr *last_initializer;
64
65 /* History of all the enumerators is maintained, so that
66    kind values of all the enumerators could be updated depending
67    upon the maximum initialized value.  */
68
69 typedef struct enumerator_history
70 {
71   gfc_symbol *sym;
72   gfc_expr *initializer;
73   struct enumerator_history *next;
74 }
75 enumerator_history;
76
77 /* Header of enum history chain.  */
78
79 static enumerator_history *enum_history = NULL;
80
81 /* Pointer of enum history node containing largest initializer.  */
82
83 static enumerator_history *max_enum = NULL;
84
85 /* gfc_new_block points to the symbol of a newly matched block.  */
86
87 gfc_symbol *gfc_new_block;
88
89 locus gfc_function_kind_locus;
90 locus gfc_function_type_locus;
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       gfc_free_expr (p->expr);
138       gfc_free (p);
139     }
140 }
141
142
143 /* Free a list of gfc_data structures.  */
144
145 void
146 gfc_free_data (gfc_data *p)
147 {
148   gfc_data *q;
149
150   for (; p; p = q)
151     {
152       q = p->next;
153       free_variable (p->var);
154       free_value (p->value);
155       gfc_free (p);
156     }
157 }
158
159
160 /* Free all data in a namespace.  */
161
162 static void
163 gfc_free_data_all (gfc_namespace *ns)
164 {
165   gfc_data *d;
166
167   for (;ns->data;)
168     {
169       d = ns->data->next;
170       gfc_free (ns->data);
171       ns->data = d;
172     }
173 }
174
175
176 static match var_element (gfc_data_variable *);
177
178 /* Match a list of variables terminated by an iterator and a right
179    parenthesis.  */
180
181 static match
182 var_list (gfc_data_variable *parent)
183 {
184   gfc_data_variable *tail, var;
185   match m;
186
187   m = var_element (&var);
188   if (m == MATCH_ERROR)
189     return MATCH_ERROR;
190   if (m == MATCH_NO)
191     goto syntax;
192
193   tail = gfc_get_data_variable ();
194   *tail = var;
195
196   parent->list = tail;
197
198   for (;;)
199     {
200       if (gfc_match_char (',') != MATCH_YES)
201         goto syntax;
202
203       m = gfc_match_iterator (&parent->iter, 1);
204       if (m == MATCH_YES)
205         break;
206       if (m == MATCH_ERROR)
207         return MATCH_ERROR;
208
209       m = var_element (&var);
210       if (m == MATCH_ERROR)
211         return MATCH_ERROR;
212       if (m == MATCH_NO)
213         goto syntax;
214
215       tail->next = gfc_get_data_variable ();
216       tail = tail->next;
217
218       *tail = var;
219     }
220
221   if (gfc_match_char (')') != MATCH_YES)
222     goto syntax;
223   return MATCH_YES;
224
225 syntax:
226   gfc_syntax_error (ST_DATA);
227   return MATCH_ERROR;
228 }
229
230
231 /* Match a single element in a data variable list, which can be a
232    variable-iterator list.  */
233
234 static match
235 var_element (gfc_data_variable *new)
236 {
237   match m;
238   gfc_symbol *sym;
239
240   memset (new, 0, sizeof (gfc_data_variable));
241
242   if (gfc_match_char ('(') == MATCH_YES)
243     return var_list (new);
244
245   m = gfc_match_variable (&new->expr, 0);
246   if (m != MATCH_YES)
247     return m;
248
249   sym = new->expr->symtree->n.sym;
250
251   if (!sym->attr.function && gfc_current_ns->parent
252       && gfc_current_ns->parent == sym->ns)
253     {
254       gfc_error ("Host associated variable '%s' may not be in the DATA "
255                  "statement at %C", sym->name);
256       return MATCH_ERROR;
257     }
258
259   if (gfc_current_state () != COMP_BLOCK_DATA
260       && sym->attr.in_common
261       && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
262                          "common block variable '%s' in DATA statement at %C",
263                          sym->name) == FAILURE)
264     return MATCH_ERROR;
265
266   if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
267     return MATCH_ERROR;
268
269   return MATCH_YES;
270 }
271
272
273 /* Match the top-level list of data variables.  */
274
275 static match
276 top_var_list (gfc_data *d)
277 {
278   gfc_data_variable var, *tail, *new;
279   match m;
280
281   tail = NULL;
282
283   for (;;)
284     {
285       m = var_element (&var);
286       if (m == MATCH_NO)
287         goto syntax;
288       if (m == MATCH_ERROR)
289         return MATCH_ERROR;
290
291       new = gfc_get_data_variable ();
292       *new = var;
293
294       if (tail == NULL)
295         d->var = new;
296       else
297         tail->next = new;
298
299       tail = new;
300
301       if (gfc_match_char ('/') == MATCH_YES)
302         break;
303       if (gfc_match_char (',') != MATCH_YES)
304         goto syntax;
305     }
306
307   return MATCH_YES;
308
309 syntax:
310   gfc_syntax_error (ST_DATA);
311   gfc_free_data_all (gfc_current_ns);
312   return MATCH_ERROR;
313 }
314
315
316 static match
317 match_data_constant (gfc_expr **result)
318 {
319   char name[GFC_MAX_SYMBOL_LEN + 1];
320   gfc_symbol *sym;
321   gfc_expr *expr;
322   match m;
323   locus old_loc;
324
325   m = gfc_match_literal_constant (&expr, 1);
326   if (m == MATCH_YES)
327     {
328       *result = expr;
329       return MATCH_YES;
330     }
331
332   if (m == MATCH_ERROR)
333     return MATCH_ERROR;
334
335   m = gfc_match_null (result);
336   if (m != MATCH_NO)
337     return m;
338
339   old_loc = gfc_current_locus;
340
341   /* Should this be a structure component, try to match it
342      before matching a name.  */
343   m = gfc_match_rvalue (result);
344   if (m == MATCH_ERROR)
345     return m;
346
347   if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
348     {
349       if (gfc_simplify_expr (*result, 0) == FAILURE)
350         m = MATCH_ERROR;
351       return m;
352     }
353
354   gfc_current_locus = old_loc;
355
356   m = gfc_match_name (name);
357   if (m != MATCH_YES)
358     return m;
359
360   if (gfc_find_symbol (name, NULL, 1, &sym))
361     return MATCH_ERROR;
362
363   if (sym == NULL
364       || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
365     {
366       gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
367                  name);
368       return MATCH_ERROR;
369     }
370   else if (sym->attr.flavor == FL_DERIVED)
371     return gfc_match_structure_constructor (sym, result);
372
373   *result = gfc_copy_expr (sym->value);
374   return MATCH_YES;
375 }
376
377
378 /* Match a list of values in a DATA statement.  The leading '/' has
379    already been seen at this point.  */
380
381 static match
382 top_val_list (gfc_data *data)
383 {
384   gfc_data_value *new, *tail;
385   gfc_expr *expr;
386   match m;
387
388   tail = NULL;
389
390   for (;;)
391     {
392       m = match_data_constant (&expr);
393       if (m == MATCH_NO)
394         goto syntax;
395       if (m == MATCH_ERROR)
396         return MATCH_ERROR;
397
398       new = gfc_get_data_value ();
399       mpz_init (new->repeat);
400
401       if (tail == NULL)
402         data->value = new;
403       else
404         tail->next = new;
405
406       tail = new;
407
408       if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
409         {
410           tail->expr = expr;
411           mpz_set_ui (tail->repeat, 1);
412         }
413       else
414         {
415           if (expr->ts.type == BT_INTEGER)
416             mpz_set (tail->repeat, expr->value.integer);
417           gfc_free_expr (expr);
418
419           m = match_data_constant (&tail->expr);
420           if (m == MATCH_NO)
421             goto syntax;
422           if (m == MATCH_ERROR)
423             return MATCH_ERROR;
424         }
425
426       if (gfc_match_char ('/') == MATCH_YES)
427         break;
428       if (gfc_match_char (',') == MATCH_NO)
429         goto syntax;
430     }
431
432   return MATCH_YES;
433
434 syntax:
435   gfc_syntax_error (ST_DATA);
436   gfc_free_data_all (gfc_current_ns);
437   return MATCH_ERROR;
438 }
439
440
441 /* Matches an old style initialization.  */
442
443 static match
444 match_old_style_init (const char *name)
445 {
446   match m;
447   gfc_symtree *st;
448   gfc_symbol *sym;
449   gfc_data *newdata;
450
451   /* Set up data structure to hold initializers.  */
452   gfc_find_sym_tree (name, NULL, 0, &st);
453   sym = st->n.sym;
454
455   newdata = gfc_get_data ();
456   newdata->var = gfc_get_data_variable ();
457   newdata->var->expr = gfc_get_variable_expr (st);
458   newdata->where = gfc_current_locus;
459
460   /* Match initial value list. This also eats the terminal '/'.  */
461   m = top_val_list (newdata);
462   if (m != MATCH_YES)
463     {
464       gfc_free (newdata);
465       return m;
466     }
467
468   if (gfc_pure (NULL))
469     {
470       gfc_error ("Initialization at %C is not allowed in a PURE procedure");
471       gfc_free (newdata);
472       return MATCH_ERROR;
473     }
474
475   /* Mark the variable as having appeared in a data statement.  */
476   if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
477     {
478       gfc_free (newdata);
479       return MATCH_ERROR;
480     }
481
482   /* Chain in namespace list of DATA initializers.  */
483   newdata->next = gfc_current_ns->data;
484   gfc_current_ns->data = newdata;
485
486   return m;
487 }
488
489
490 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
491    we are matching a DATA statement and are therefore issuing an error
492    if we encounter something unexpected, if not, we're trying to match
493    an old-style initialization expression of the form INTEGER I /2/.  */
494
495 match
496 gfc_match_data (void)
497 {
498   gfc_data *new;
499   match m;
500
501   set_in_match_data (true);
502
503   for (;;)
504     {
505       new = gfc_get_data ();
506       new->where = gfc_current_locus;
507
508       m = top_var_list (new);
509       if (m != MATCH_YES)
510         goto cleanup;
511
512       m = top_val_list (new);
513       if (m != MATCH_YES)
514         goto cleanup;
515
516       new->next = gfc_current_ns->data;
517       gfc_current_ns->data = new;
518
519       if (gfc_match_eos () == MATCH_YES)
520         break;
521
522       gfc_match_char (',');     /* Optional comma */
523     }
524
525   set_in_match_data (false);
526
527   if (gfc_pure (NULL))
528     {
529       gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
530       return MATCH_ERROR;
531     }
532
533   return MATCH_YES;
534
535 cleanup:
536   set_in_match_data (false);
537   gfc_free_data (new);
538   return MATCH_ERROR;
539 }
540
541
542 /************************ Declaration statements *********************/
543
544 /* Match an intent specification.  Since this can only happen after an
545    INTENT word, a legal intent-spec must follow.  */
546
547 static sym_intent
548 match_intent_spec (void)
549 {
550
551   if (gfc_match (" ( in out )") == MATCH_YES)
552     return INTENT_INOUT;
553   if (gfc_match (" ( in )") == MATCH_YES)
554     return INTENT_IN;
555   if (gfc_match (" ( out )") == MATCH_YES)
556     return INTENT_OUT;
557
558   gfc_error ("Bad INTENT specification at %C");
559   return INTENT_UNKNOWN;
560 }
561
562
563 /* Matches a character length specification, which is either a
564    specification expression or a '*'.  */
565
566 static match
567 char_len_param_value (gfc_expr **expr)
568 {
569   match m;
570
571   if (gfc_match_char ('*') == MATCH_YES)
572     {
573       *expr = NULL;
574       return MATCH_YES;
575     }
576
577   m = gfc_match_expr (expr);
578   if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
579     {
580       if ((*expr)->value.function.actual
581           && (*expr)->value.function.actual->expr->symtree)
582         {
583           gfc_expr *e;
584           e = (*expr)->value.function.actual->expr;
585           if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
586               && e->expr_type == EXPR_VARIABLE)
587             {
588               if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
589                 goto syntax;
590               if (e->symtree->n.sym->ts.type == BT_CHARACTER
591                   && e->symtree->n.sym->ts.cl
592                   && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
593                 goto syntax;
594             }
595         }
596     }
597   return m;
598
599 syntax:
600   gfc_error ("Conflict in attributes of function argument at %C");
601   return MATCH_ERROR;
602 }
603
604
605 /* A character length is a '*' followed by a literal integer or a
606    char_len_param_value in parenthesis.  */
607
608 static match
609 match_char_length (gfc_expr **expr)
610 {
611   int length;
612   match m;
613
614   m = gfc_match_char ('*');
615   if (m != MATCH_YES)
616     return m;
617
618   m = gfc_match_small_literal_int (&length, NULL);
619   if (m == MATCH_ERROR)
620     return m;
621
622   if (m == MATCH_YES)
623     {
624       *expr = gfc_int_expr (length);
625       return m;
626     }
627
628   if (gfc_match_char ('(') == MATCH_NO)
629     goto syntax;
630
631   m = char_len_param_value (expr);
632   if (m == MATCH_ERROR)
633     return m;
634   if (m == MATCH_NO)
635     goto syntax;
636
637   if (gfc_match_char (')') == MATCH_NO)
638     {
639       gfc_free_expr (*expr);
640       *expr = NULL;
641       goto syntax;
642     }
643
644   return MATCH_YES;
645
646 syntax:
647   gfc_error ("Syntax error in character length specification at %C");
648   return MATCH_ERROR;
649 }
650
651
652 /* Special subroutine for finding a symbol.  Check if the name is found
653    in the current name space.  If not, and we're compiling a function or
654    subroutine and the parent compilation unit is an interface, then check
655    to see if the name we've been given is the name of the interface
656    (located in another namespace).  */
657
658 static int
659 find_special (const char *name, gfc_symbol **result)
660 {
661   gfc_state_data *s;
662   int i;
663
664   i = gfc_get_symbol (name, NULL, result);
665   if (i == 0)
666     goto end;
667
668   if (gfc_current_state () != COMP_SUBROUTINE
669       && gfc_current_state () != COMP_FUNCTION)
670     goto end;
671
672   s = gfc_state_stack->previous;
673   if (s == NULL)
674     goto end;
675
676   if (s->state != COMP_INTERFACE)
677     goto end;
678   if (s->sym == NULL)
679     goto end;             /* Nameless interface.  */
680
681   if (strcmp (name, s->sym->name) == 0)
682     {
683       *result = s->sym;
684       return 0;
685     }
686
687 end:
688   return i;
689 }
690
691
692 /* Special subroutine for getting a symbol node associated with a
693    procedure name, used in SUBROUTINE and FUNCTION statements.  The
694    symbol is created in the parent using with symtree node in the
695    child unit pointing to the symbol.  If the current namespace has no
696    parent, then the symbol is just created in the current unit.  */
697
698 static int
699 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
700 {
701   gfc_symtree *st;
702   gfc_symbol *sym;
703   int rc = 0;
704
705   /* Module functions have to be left in their own namespace because
706      they have potentially (almost certainly!) already been referenced.
707      In this sense, they are rather like external functions.  This is
708      fixed up in resolve.c(resolve_entries), where the symbol name-
709      space is set to point to the master function, so that the fake
710      result mechanism can work.  */
711   if (module_fcn_entry)
712     {
713       /* Present if entry is declared to be a module procedure.  */
714       rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
715
716       if (*result == NULL)
717         rc = gfc_get_symbol (name, NULL, result);
718       else if (gfc_get_symbol (name, NULL, &sym) == 0
719                  && sym
720                  && sym->ts.type != BT_UNKNOWN
721                  && (*result)->ts.type == BT_UNKNOWN
722                  && sym->attr.flavor == FL_UNKNOWN)
723         /* Pick up the typespec for the entry, if declared in the function
724            body.  Note that this symbol is FL_UNKNOWN because it will
725            only have appeared in a type declaration.  The local symtree
726            is set to point to the module symbol and a unique symtree
727            to the local version.  This latter ensures a correct clearing
728            of the symbols.  */
729           {
730             (*result)->ts = sym->ts;
731             gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
732             st->n.sym = *result;
733             st = gfc_get_unique_symtree (gfc_current_ns);
734             st->n.sym = sym;
735           }
736     }
737   else
738     rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
739
740   if (rc)
741     return rc;
742
743   sym = *result;
744   gfc_current_ns->refs++;
745
746   if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
747     {
748       /* Trap another encompassed procedure with the same name.  All
749          these conditions are necessary to avoid picking up an entry
750          whose name clashes with that of the encompassing procedure;
751          this is handled using gsymbols to register unique,globally
752          accessible names.  */
753       if (sym->attr.flavor != 0
754           && sym->attr.proc != 0
755           && (sym->attr.subroutine || sym->attr.function)
756           && sym->attr.if_source != IFSRC_UNKNOWN)
757         gfc_error_now ("Procedure '%s' at %C is already defined at %L",
758                        name, &sym->declared_at);
759
760       /* Trap a procedure with a name the same as interface in the
761          encompassing scope.  */
762       if (sym->attr.generic != 0
763           && (sym->attr.subroutine || sym->attr.function)
764           && !sym->attr.mod_proc)
765         gfc_error_now ("Name '%s' at %C is already defined"
766                        " as a generic interface at %L",
767                        name, &sym->declared_at);
768
769       /* Trap declarations of attributes in encompassing scope.  The
770          signature for this is that ts.kind is set.  Legitimate
771          references only set ts.type.  */
772       if (sym->ts.kind != 0
773           && !sym->attr.implicit_type
774           && sym->attr.proc == 0
775           && gfc_current_ns->parent != NULL
776           && sym->attr.access == 0
777           && !module_fcn_entry)
778         gfc_error_now ("Procedure '%s' at %C has an explicit interface "
779                        "and must not have attributes declared at %L",
780                        name, &sym->declared_at);
781     }
782
783   if (gfc_current_ns->parent == NULL || *result == NULL)
784     return rc;
785
786   /* Module function entries will already have a symtree in
787      the current namespace but will need one at module level.  */
788   if (module_fcn_entry)
789     {
790       /* Present if entry is declared to be a module procedure.  */
791       rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
792       if (st == NULL)
793         st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
794     }
795   else
796     st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
797
798   st->n.sym = sym;
799   sym->refs++;
800
801   /* See if the procedure should be a module procedure.  */
802
803   if (((sym->ns->proc_name != NULL
804                 && sym->ns->proc_name->attr.flavor == FL_MODULE
805                 && sym->attr.proc != PROC_MODULE)
806             || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
807         && gfc_add_procedure (&sym->attr, PROC_MODULE,
808                               sym->name, NULL) == FAILURE)
809     rc = 2;
810
811   return rc;
812 }
813
814
815 /* Verify that the given symbol representing a parameter is C
816    interoperable, by checking to see if it was marked as such after
817    its declaration.  If the given symbol is not interoperable, a
818    warning is reported, thus removing the need to return the status to
819    the calling function.  The standard does not require the user use
820    one of the iso_c_binding named constants to declare an
821    interoperable parameter, but we can't be sure if the param is C
822    interop or not if the user doesn't.  For example, integer(4) may be
823    legal Fortran, but doesn't have meaning in C.  It may interop with
824    a number of the C types, which causes a problem because the
825    compiler can't know which one.  This code is almost certainly not
826    portable, and the user will get what they deserve if the C type
827    across platforms isn't always interoperable with integer(4).  If
828    the user had used something like integer(c_int) or integer(c_long),
829    the compiler could have automatically handled the varying sizes
830    across platforms.  */
831
832 try
833 verify_c_interop_param (gfc_symbol *sym)
834 {
835   int is_c_interop = 0;
836   try retval = SUCCESS;
837
838   /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
839      Don't repeat the checks here.  */
840   if (sym->attr.implicit_type)
841     return SUCCESS;
842   
843   /* For subroutines or functions that are passed to a BIND(C) procedure,
844      they're interoperable if they're BIND(C) and their params are all
845      interoperable.  */
846   if (sym->attr.flavor == FL_PROCEDURE)
847     {
848       if (sym->attr.is_bind_c == 0)
849         {
850           gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
851                          "attribute to be C interoperable", sym->name,
852                          &(sym->declared_at));
853                          
854           return FAILURE;
855         }
856       else
857         {
858           if (sym->attr.is_c_interop == 1)
859             /* We've already checked this procedure; don't check it again.  */
860             return SUCCESS;
861           else
862             return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
863                                       sym->common_block);
864         }
865     }
866   
867   /* See if we've stored a reference to a procedure that owns sym.  */
868   if (sym->ns != NULL && sym->ns->proc_name != NULL)
869     {
870       if (sym->ns->proc_name->attr.is_bind_c == 1)
871         {
872           is_c_interop =
873             (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
874              == SUCCESS ? 1 : 0);
875
876           if (is_c_interop != 1)
877             {
878               /* Make personalized messages to give better feedback.  */
879               if (sym->ts.type == BT_DERIVED)
880                 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
881                            " procedure '%s' but is not C interoperable "
882                            "because derived type '%s' is not C interoperable",
883                            sym->name, &(sym->declared_at),
884                            sym->ns->proc_name->name, 
885                            sym->ts.derived->name);
886               else
887                 gfc_warning ("Variable '%s' at %L is a parameter to the "
888                              "BIND(C) procedure '%s' but may not be C "
889                              "interoperable",
890                              sym->name, &(sym->declared_at),
891                              sym->ns->proc_name->name);
892             }
893
894           /* Character strings are only C interoperable if they have a
895              length of 1.  */
896           if (sym->ts.type == BT_CHARACTER)
897             {
898               gfc_charlen *cl = sym->ts.cl;
899               if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
900                   || mpz_cmp_si (cl->length->value.integer, 1) != 0)
901                 {
902                   gfc_error ("Character argument '%s' at %L "
903                              "must be length 1 because "
904                              "procedure '%s' is BIND(C)",
905                              sym->name, &sym->declared_at,
906                              sym->ns->proc_name->name);
907                   retval = FAILURE;
908                 }
909             }
910
911           /* We have to make sure that any param to a bind(c) routine does
912              not have the allocatable, pointer, or optional attributes,
913              according to J3/04-007, section 5.1.  */
914           if (sym->attr.allocatable == 1)
915             {
916               gfc_error ("Variable '%s' at %L cannot have the "
917                          "ALLOCATABLE attribute because procedure '%s'"
918                          " is BIND(C)", sym->name, &(sym->declared_at),
919                          sym->ns->proc_name->name);
920               retval = FAILURE;
921             }
922
923           if (sym->attr.pointer == 1)
924             {
925               gfc_error ("Variable '%s' at %L cannot have the "
926                          "POINTER attribute because procedure '%s'"
927                          " is BIND(C)", sym->name, &(sym->declared_at),
928                          sym->ns->proc_name->name);
929               retval = FAILURE;
930             }
931
932           if (sym->attr.optional == 1)
933             {
934               gfc_error ("Variable '%s' at %L cannot have the "
935                          "OPTIONAL attribute because procedure '%s'"
936                          " is BIND(C)", sym->name, &(sym->declared_at),
937                          sym->ns->proc_name->name);
938               retval = FAILURE;
939             }
940
941           /* Make sure that if it has the dimension attribute, that it is
942              either assumed size or explicit shape.  */
943           if (sym->as != NULL)
944             {
945               if (sym->as->type == AS_ASSUMED_SHAPE)
946                 {
947                   gfc_error ("Assumed-shape array '%s' at %L cannot be an "
948                              "argument to the procedure '%s' at %L because "
949                              "the procedure is BIND(C)", sym->name,
950                              &(sym->declared_at), sym->ns->proc_name->name,
951                              &(sym->ns->proc_name->declared_at));
952                   retval = FAILURE;
953                 }
954
955               if (sym->as->type == AS_DEFERRED)
956                 {
957                   gfc_error ("Deferred-shape array '%s' at %L cannot be an "
958                              "argument to the procedure '%s' at %L because "
959                              "the procedure is BIND(C)", sym->name,
960                              &(sym->declared_at), sym->ns->proc_name->name,
961                              &(sym->ns->proc_name->declared_at));
962                   retval = FAILURE;
963                 }
964           }
965         }
966     }
967
968   return retval;
969 }
970
971
972 /* Function called by variable_decl() that adds a name to the symbol table.  */
973
974 static try
975 build_sym (const char *name, gfc_charlen *cl,
976            gfc_array_spec **as, locus *var_locus)
977 {
978   symbol_attribute attr;
979   gfc_symbol *sym;
980
981   if (gfc_get_symbol (name, NULL, &sym))
982     return FAILURE;
983
984   /* Start updating the symbol table.  Add basic type attribute if present.  */
985   if (current_ts.type != BT_UNKNOWN
986       && (sym->attr.implicit_type == 0
987           || !gfc_compare_types (&sym->ts, &current_ts))
988       && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
989     return FAILURE;
990
991   if (sym->ts.type == BT_CHARACTER)
992     sym->ts.cl = cl;
993
994   /* Add dimension attribute if present.  */
995   if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
996     return FAILURE;
997   *as = NULL;
998
999   /* Add attribute to symbol.  The copy is so that we can reset the
1000      dimension attribute.  */
1001   attr = current_attr;
1002   attr.dimension = 0;
1003
1004   if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1005     return FAILURE;
1006
1007   /* Finish any work that may need to be done for the binding label,
1008      if it's a bind(c).  The bind(c) attr is found before the symbol
1009      is made, and before the symbol name (for data decls), so the
1010      current_ts is holding the binding label, or nothing if the
1011      name= attr wasn't given.  Therefore, test here if we're dealing
1012      with a bind(c) and make sure the binding label is set correctly.  */
1013   if (sym->attr.is_bind_c == 1)
1014     {
1015       if (sym->binding_label[0] == '\0')
1016         {
1017           /* Set the binding label and verify that if a NAME= was specified
1018              then only one identifier was in the entity-decl-list.  */
1019           if (set_binding_label (sym->binding_label, sym->name,
1020                                  num_idents_on_line) == FAILURE)
1021             return FAILURE;
1022         }
1023     }
1024
1025   /* See if we know we're in a common block, and if it's a bind(c)
1026      common then we need to make sure we're an interoperable type.  */
1027   if (sym->attr.in_common == 1)
1028     {
1029       /* Test the common block object.  */
1030       if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1031           && sym->ts.is_c_interop != 1)
1032         {
1033           gfc_error_now ("Variable '%s' in common block '%s' at %C "
1034                          "must be declared with a C interoperable "
1035                          "kind since common block '%s' is BIND(C)",
1036                          sym->name, sym->common_block->name,
1037                          sym->common_block->name);
1038           gfc_clear_error ();
1039         }
1040     }
1041
1042   sym->attr.implied_index = 0;
1043
1044   return SUCCESS;
1045 }
1046
1047
1048 /* Set character constant to the given length. The constant will be padded or
1049    truncated.  */
1050
1051 void
1052 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1053 {
1054   char *s;
1055   int slen;
1056
1057   gcc_assert (expr->expr_type == EXPR_CONSTANT);
1058   gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1059
1060   slen = expr->value.character.length;
1061   if (len != slen)
1062     {
1063       s = gfc_getmem (len + 1);
1064       memcpy (s, expr->value.character.string, MIN (len, slen));
1065       if (len > slen)
1066         memset (&s[slen], ' ', len - slen);
1067
1068       if (gfc_option.warn_character_truncation && slen > len)
1069         gfc_warning_now ("CHARACTER expression at %L is being truncated "
1070                          "(%d/%d)", &expr->where, slen, len);
1071
1072       /* Apply the standard by 'hand' otherwise it gets cleared for
1073          initializers.  */
1074       if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1075         gfc_error_now ("The CHARACTER elements of the array constructor "
1076                        "at %L must have the same length (%d/%d)",
1077                         &expr->where, slen, len);
1078
1079       s[len] = '\0';
1080       gfc_free (expr->value.character.string);
1081       expr->value.character.string = s;
1082       expr->value.character.length = len;
1083     }
1084 }
1085
1086
1087 /* Function to create and update the enumerator history
1088    using the information passed as arguments.
1089    Pointer "max_enum" is also updated, to point to
1090    enum history node containing largest initializer.
1091
1092    SYM points to the symbol node of enumerator.
1093    INIT points to its enumerator value.  */
1094
1095 static void
1096 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1097 {
1098   enumerator_history *new_enum_history;
1099   gcc_assert (sym != NULL && init != NULL);
1100
1101   new_enum_history = gfc_getmem (sizeof (enumerator_history));
1102
1103   new_enum_history->sym = sym;
1104   new_enum_history->initializer = init;
1105   new_enum_history->next = NULL;
1106
1107   if (enum_history == NULL)
1108     {
1109       enum_history = new_enum_history;
1110       max_enum = enum_history;
1111     }
1112   else
1113     {
1114       new_enum_history->next = enum_history;
1115       enum_history = new_enum_history;
1116
1117       if (mpz_cmp (max_enum->initializer->value.integer,
1118                    new_enum_history->initializer->value.integer) < 0)
1119         max_enum = new_enum_history;
1120     }
1121 }
1122
1123
1124 /* Function to free enum kind history.  */
1125
1126 void
1127 gfc_free_enum_history (void)
1128 {
1129   enumerator_history *current = enum_history;
1130   enumerator_history *next;
1131
1132   while (current != NULL)
1133     {
1134       next = current->next;
1135       gfc_free (current);
1136       current = next;
1137     }
1138   max_enum = NULL;
1139   enum_history = NULL;
1140 }
1141
1142
1143 /* Function called by variable_decl() that adds an initialization
1144    expression to a symbol.  */
1145
1146 static try
1147 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1148 {
1149   symbol_attribute attr;
1150   gfc_symbol *sym;
1151   gfc_expr *init;
1152
1153   init = *initp;
1154   if (find_special (name, &sym))
1155     return FAILURE;
1156
1157   attr = sym->attr;
1158
1159   /* If this symbol is confirming an implicit parameter type,
1160      then an initialization expression is not allowed.  */
1161   if (attr.flavor == FL_PARAMETER
1162       && sym->value != NULL
1163       && *initp != NULL)
1164     {
1165       gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1166                  sym->name);
1167       return FAILURE;
1168     }
1169
1170   if (attr.in_common
1171       && !attr.data
1172       && *initp != NULL)
1173     {
1174       gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1175                  sym->name);
1176       return FAILURE;
1177     }
1178
1179   if (init == NULL)
1180     {
1181       /* An initializer is required for PARAMETER declarations.  */
1182       if (attr.flavor == FL_PARAMETER)
1183         {
1184           gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1185           return FAILURE;
1186         }
1187     }
1188   else
1189     {
1190       /* If a variable appears in a DATA block, it cannot have an
1191          initializer.  */
1192       if (sym->attr.data)
1193         {
1194           gfc_error ("Variable '%s' at %C with an initializer already "
1195                      "appears in a DATA statement", sym->name);
1196           return FAILURE;
1197         }
1198
1199       /* Check if the assignment can happen. This has to be put off
1200          until later for a derived type variable.  */
1201       if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1202           && gfc_check_assign_symbol (sym, init) == FAILURE)
1203         return FAILURE;
1204
1205       if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1206         {
1207           /* Update symbol character length according initializer.  */
1208           if (sym->ts.cl->length == NULL)
1209             {
1210               int clen;
1211               /* If there are multiple CHARACTER variables declared on the
1212                  same line, we don't want them to share the same length.  */
1213               sym->ts.cl = gfc_get_charlen ();
1214               sym->ts.cl->next = gfc_current_ns->cl_list;
1215               gfc_current_ns->cl_list = sym->ts.cl;
1216
1217               if (sym->attr.flavor == FL_PARAMETER)
1218                 {
1219                   if (init->expr_type == EXPR_CONSTANT)
1220                     {
1221                       clen = init->value.character.length;
1222                       sym->ts.cl->length = gfc_int_expr (clen);
1223                     }
1224                   else if (init->expr_type == EXPR_ARRAY)
1225                     {
1226                       gfc_expr *p = init->value.constructor->expr;
1227                       clen = p->value.character.length;
1228                       sym->ts.cl->length = gfc_int_expr (clen);
1229                     }
1230                   else if (init->ts.cl && init->ts.cl->length)
1231                     sym->ts.cl->length =
1232                                 gfc_copy_expr (sym->value->ts.cl->length);
1233                 }
1234             }
1235           /* Update initializer character length according symbol.  */
1236           else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1237             {
1238               int len = mpz_get_si (sym->ts.cl->length->value.integer);
1239               gfc_constructor * p;
1240
1241               if (init->expr_type == EXPR_CONSTANT)
1242                 gfc_set_constant_character_len (len, init, false);
1243               else if (init->expr_type == EXPR_ARRAY)
1244                 {
1245                   /* Build a new charlen to prevent simplification from
1246                      deleting the length before it is resolved.  */
1247                   init->ts.cl = gfc_get_charlen ();
1248                   init->ts.cl->next = gfc_current_ns->cl_list;
1249                   gfc_current_ns->cl_list = sym->ts.cl;
1250                   init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1251
1252                   for (p = init->value.constructor; p; p = p->next)
1253                     gfc_set_constant_character_len (len, p->expr, false);
1254                 }
1255             }
1256         }
1257
1258       /* Need to check if the expression we initialized this
1259          to was one of the iso_c_binding named constants.  If so,
1260          and we're a parameter (constant), let it be iso_c.
1261          For example:
1262          integer(c_int), parameter :: my_int = c_int
1263          integer(my_int) :: my_int_2
1264          If we mark my_int as iso_c (since we can see it's value
1265          is equal to one of the named constants), then my_int_2
1266          will be considered C interoperable.  */
1267       if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1268         {
1269           sym->ts.is_iso_c |= init->ts.is_iso_c;
1270           sym->ts.is_c_interop |= init->ts.is_c_interop;
1271           /* attr bits needed for module files.  */
1272           sym->attr.is_iso_c |= init->ts.is_iso_c;
1273           sym->attr.is_c_interop |= init->ts.is_c_interop;
1274           if (init->ts.is_iso_c)
1275             sym->ts.f90_type = init->ts.f90_type;
1276         }
1277       
1278       /* Add initializer.  Make sure we keep the ranks sane.  */
1279       if (sym->attr.dimension && init->rank == 0)
1280         {
1281           mpz_t size;
1282           gfc_expr *array;
1283           gfc_constructor *c;
1284           int n;
1285           if (sym->attr.flavor == FL_PARAMETER
1286                 && init->expr_type == EXPR_CONSTANT
1287                 && spec_size (sym->as, &size) == SUCCESS
1288                 && mpz_cmp_si (size, 0) > 0)
1289             {
1290               array = gfc_start_constructor (init->ts.type, init->ts.kind,
1291                                              &init->where);
1292
1293               array->value.constructor = c = NULL;
1294               for (n = 0; n < (int)mpz_get_si (size); n++)
1295                 {
1296                   if (array->value.constructor == NULL)
1297                     {
1298                       array->value.constructor = c = gfc_get_constructor ();
1299                       c->expr = init;
1300                     }
1301                   else
1302                     {
1303                       c->next = gfc_get_constructor ();
1304                       c = c->next;
1305                       c->expr = gfc_copy_expr (init);
1306                     }
1307                 }
1308
1309               array->shape = gfc_get_shape (sym->as->rank);
1310               for (n = 0; n < sym->as->rank; n++)
1311                 spec_dimen_size (sym->as, n, &array->shape[n]);
1312
1313               init = array;
1314               mpz_clear (size);
1315             }
1316           init->rank = sym->as->rank;
1317         }
1318
1319       sym->value = init;
1320       if (sym->attr.save == SAVE_NONE)
1321         sym->attr.save = SAVE_IMPLICIT;
1322       *initp = NULL;
1323     }
1324
1325   return SUCCESS;
1326 }
1327
1328
1329 /* Function called by variable_decl() that adds a name to a structure
1330    being built.  */
1331
1332 static try
1333 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1334               gfc_array_spec **as)
1335 {
1336   gfc_component *c;
1337
1338   /* If the current symbol is of the same derived type that we're
1339      constructing, it must have the pointer attribute.  */
1340   if (current_ts.type == BT_DERIVED
1341       && current_ts.derived == gfc_current_block ()
1342       && current_attr.pointer == 0)
1343     {
1344       gfc_error ("Component at %C must have the POINTER attribute");
1345       return FAILURE;
1346     }
1347
1348   if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1349     {
1350       if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1351         {
1352           gfc_error ("Array component of structure at %C must have explicit "
1353                      "or deferred shape");
1354           return FAILURE;
1355         }
1356     }
1357
1358   if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1359     return FAILURE;
1360
1361   c->ts = current_ts;
1362   c->ts.cl = cl;
1363   gfc_set_component_attr (c, &current_attr);
1364
1365   c->initializer = *init;
1366   *init = NULL;
1367
1368   c->as = *as;
1369   if (c->as != NULL)
1370     c->dimension = 1;
1371   *as = NULL;
1372
1373   /* Check array components.  */
1374   if (!c->dimension)
1375     {
1376       if (c->allocatable)
1377         {
1378           gfc_error ("Allocatable component at %C must be an array");
1379           return FAILURE;
1380         }
1381       else
1382         return SUCCESS;
1383     }
1384
1385   if (c->pointer)
1386     {
1387       if (c->as->type != AS_DEFERRED)
1388         {
1389           gfc_error ("Pointer array component of structure at %C must have a "
1390                      "deferred shape");
1391           return FAILURE;
1392         }
1393     }
1394   else if (c->allocatable)
1395     {
1396       if (c->as->type != AS_DEFERRED)
1397         {
1398           gfc_error ("Allocatable component of structure at %C must have a "
1399                      "deferred shape");
1400           return FAILURE;
1401         }
1402     }
1403   else
1404     {
1405       if (c->as->type != AS_EXPLICIT)
1406         {
1407           gfc_error ("Array component of structure at %C must have an "
1408                      "explicit shape");
1409           return FAILURE;
1410         }
1411     }
1412
1413   return SUCCESS;
1414 }
1415
1416
1417 /* Match a 'NULL()', and possibly take care of some side effects.  */
1418
1419 match
1420 gfc_match_null (gfc_expr **result)
1421 {
1422   gfc_symbol *sym;
1423   gfc_expr *e;
1424   match m;
1425
1426   m = gfc_match (" null ( )");
1427   if (m != MATCH_YES)
1428     return m;
1429
1430   /* The NULL symbol now has to be/become an intrinsic function.  */
1431   if (gfc_get_symbol ("null", NULL, &sym))
1432     {
1433       gfc_error ("NULL() initialization at %C is ambiguous");
1434       return MATCH_ERROR;
1435     }
1436
1437   gfc_intrinsic_symbol (sym);
1438
1439   if (sym->attr.proc != PROC_INTRINSIC
1440       && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1441                              sym->name, NULL) == FAILURE
1442           || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1443     return MATCH_ERROR;
1444
1445   e = gfc_get_expr ();
1446   e->where = gfc_current_locus;
1447   e->expr_type = EXPR_NULL;
1448   e->ts.type = BT_UNKNOWN;
1449
1450   *result = e;
1451
1452   return MATCH_YES;
1453 }
1454
1455
1456 /* Match a variable name with an optional initializer.  When this
1457    subroutine is called, a variable is expected to be parsed next.
1458    Depending on what is happening at the moment, updates either the
1459    symbol table or the current interface.  */
1460
1461 static match
1462 variable_decl (int elem)
1463 {
1464   char name[GFC_MAX_SYMBOL_LEN + 1];
1465   gfc_expr *initializer, *char_len;
1466   gfc_array_spec *as;
1467   gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1468   gfc_charlen *cl;
1469   locus var_locus;
1470   match m;
1471   try t;
1472   gfc_symbol *sym;
1473   locus old_locus;
1474
1475   initializer = NULL;
1476   as = NULL;
1477   cp_as = NULL;
1478   old_locus = gfc_current_locus;
1479
1480   /* When we get here, we've just matched a list of attributes and
1481      maybe a type and a double colon.  The next thing we expect to see
1482      is the name of the symbol.  */
1483   m = gfc_match_name (name);
1484   if (m != MATCH_YES)
1485     goto cleanup;
1486
1487   var_locus = gfc_current_locus;
1488
1489   /* Now we could see the optional array spec. or character length.  */
1490   m = gfc_match_array_spec (&as);
1491   if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1492     cp_as = gfc_copy_array_spec (as);
1493   else if (m == MATCH_ERROR)
1494     goto cleanup;
1495
1496   if (m == MATCH_NO)
1497     as = gfc_copy_array_spec (current_as);
1498
1499   char_len = NULL;
1500   cl = NULL;
1501
1502   if (current_ts.type == BT_CHARACTER)
1503     {
1504       switch (match_char_length (&char_len))
1505         {
1506         case MATCH_YES:
1507           cl = gfc_get_charlen ();
1508           cl->next = gfc_current_ns->cl_list;
1509           gfc_current_ns->cl_list = cl;
1510
1511           cl->length = char_len;
1512           break;
1513
1514         /* Non-constant lengths need to be copied after the first
1515            element.  Also copy assumed lengths.  */
1516         case MATCH_NO:
1517           if (elem > 1
1518               && (current_ts.cl->length == NULL
1519                   || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1520             {
1521               cl = gfc_get_charlen ();
1522               cl->next = gfc_current_ns->cl_list;
1523               gfc_current_ns->cl_list = cl;
1524               cl->length = gfc_copy_expr (current_ts.cl->length);
1525             }
1526           else
1527             cl = current_ts.cl;
1528
1529           break;
1530
1531         case MATCH_ERROR:
1532           goto cleanup;
1533         }
1534     }
1535
1536   /*  If this symbol has already shown up in a Cray Pointer declaration,
1537       then we want to set the type & bail out.  */
1538   if (gfc_option.flag_cray_pointer)
1539     {
1540       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1541       if (sym != NULL && sym->attr.cray_pointee)
1542         {
1543           sym->ts.type = current_ts.type;
1544           sym->ts.kind = current_ts.kind;
1545           sym->ts.cl = cl;
1546           sym->ts.derived = current_ts.derived;
1547           sym->ts.is_c_interop = current_ts.is_c_interop;
1548           sym->ts.is_iso_c = current_ts.is_iso_c;
1549           m = MATCH_YES;
1550         
1551           /* Check to see if we have an array specification.  */
1552           if (cp_as != NULL)
1553             {
1554               if (sym->as != NULL)
1555                 {
1556                   gfc_error ("Duplicate array spec for Cray pointee at %C");
1557                   gfc_free_array_spec (cp_as);
1558                   m = MATCH_ERROR;
1559                   goto cleanup;
1560                 }
1561               else
1562                 {
1563                   if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1564                     gfc_internal_error ("Couldn't set pointee array spec.");
1565
1566                   /* Fix the array spec.  */
1567                   m = gfc_mod_pointee_as (sym->as);
1568                   if (m == MATCH_ERROR)
1569                     goto cleanup;
1570                 }
1571             }
1572           goto cleanup;
1573         }
1574       else
1575         {
1576           gfc_free_array_spec (cp_as);
1577         }
1578     }
1579
1580
1581   /* OK, we've successfully matched the declaration.  Now put the
1582      symbol in the current namespace, because it might be used in the
1583      optional initialization expression for this symbol, e.g. this is
1584      perfectly legal:
1585
1586      integer, parameter :: i = huge(i)
1587
1588      This is only true for parameters or variables of a basic type.
1589      For components of derived types, it is not true, so we don't
1590      create a symbol for those yet.  If we fail to create the symbol,
1591      bail out.  */
1592   if (gfc_current_state () != COMP_DERIVED
1593       && build_sym (name, cl, &as, &var_locus) == FAILURE)
1594     {
1595       m = MATCH_ERROR;
1596       goto cleanup;
1597     }
1598
1599   /* An interface body specifies all of the procedure's
1600      characteristics and these shall be consistent with those
1601      specified in the procedure definition, except that the interface
1602      may specify a procedure that is not pure if the procedure is
1603      defined to be pure(12.3.2).  */
1604   if (current_ts.type == BT_DERIVED
1605       && gfc_current_ns->proc_name
1606       && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1607       && current_ts.derived->ns != gfc_current_ns)
1608     {
1609       gfc_symtree *st;
1610       st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1611       if (!(current_ts.derived->attr.imported
1612                 && st != NULL
1613                 && st->n.sym == current_ts.derived)
1614             && !gfc_current_ns->has_import_set)
1615         {
1616             gfc_error ("the type of '%s' at %C has not been declared within the "
1617                        "interface", name);
1618             m = MATCH_ERROR;
1619             goto cleanup;
1620         }
1621     }
1622
1623   /* In functions that have a RESULT variable defined, the function
1624      name always refers to function calls.  Therefore, the name is
1625      not allowed to appear in specification statements.  */
1626   if (gfc_current_state () == COMP_FUNCTION
1627       && gfc_current_block () != NULL
1628       && gfc_current_block ()->result != NULL
1629       && gfc_current_block ()->result != gfc_current_block ()
1630       && strcmp (gfc_current_block ()->name, name) == 0)
1631     {
1632       gfc_error ("Function name '%s' not allowed at %C", name);
1633       m = MATCH_ERROR;
1634       goto cleanup;
1635     }
1636
1637   /* We allow old-style initializations of the form
1638        integer i /2/, j(4) /3*3, 1/
1639      (if no colon has been seen). These are different from data
1640      statements in that initializers are only allowed to apply to the
1641      variable immediately preceding, i.e.
1642        integer i, j /1, 2/
1643      is not allowed. Therefore we have to do some work manually, that
1644      could otherwise be left to the matchers for DATA statements.  */
1645
1646   if (!colon_seen && gfc_match (" /") == MATCH_YES)
1647     {
1648       if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1649                           "initialization at %C") == FAILURE)
1650         return MATCH_ERROR;
1651  
1652       return match_old_style_init (name);
1653     }
1654
1655   /* The double colon must be present in order to have initializers.
1656      Otherwise the statement is ambiguous with an assignment statement.  */
1657   if (colon_seen)
1658     {
1659       if (gfc_match (" =>") == MATCH_YES)
1660         {
1661           if (!current_attr.pointer)
1662             {
1663               gfc_error ("Initialization at %C isn't for a pointer variable");
1664               m = MATCH_ERROR;
1665               goto cleanup;
1666             }
1667
1668           m = gfc_match_null (&initializer);
1669           if (m == MATCH_NO)
1670             {
1671               gfc_error ("Pointer initialization requires a NULL() at %C");
1672               m = MATCH_ERROR;
1673             }
1674
1675           if (gfc_pure (NULL))
1676             {
1677               gfc_error ("Initialization of pointer at %C is not allowed in "
1678                          "a PURE procedure");
1679               m = MATCH_ERROR;
1680             }
1681
1682           if (m != MATCH_YES)
1683             goto cleanup;
1684
1685         }
1686       else if (gfc_match_char ('=') == MATCH_YES)
1687         {
1688           if (current_attr.pointer)
1689             {
1690               gfc_error ("Pointer initialization at %C requires '=>', "
1691                          "not '='");
1692               m = MATCH_ERROR;
1693               goto cleanup;
1694             }
1695
1696           m = gfc_match_init_expr (&initializer);
1697           if (m == MATCH_NO)
1698             {
1699               gfc_error ("Expected an initialization expression at %C");
1700               m = MATCH_ERROR;
1701             }
1702
1703           if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1704             {
1705               gfc_error ("Initialization of variable at %C is not allowed in "
1706                          "a PURE procedure");
1707               m = MATCH_ERROR;
1708             }
1709
1710           if (m != MATCH_YES)
1711             goto cleanup;
1712         }
1713     }
1714
1715   if (initializer != NULL && current_attr.allocatable
1716         && gfc_current_state () == COMP_DERIVED)
1717     {
1718       gfc_error ("Initialization of allocatable component at %C is not "
1719                  "allowed");
1720       m = MATCH_ERROR;
1721       goto cleanup;
1722     }
1723
1724   /* Add the initializer.  Note that it is fine if initializer is
1725      NULL here, because we sometimes also need to check if a
1726      declaration *must* have an initialization expression.  */
1727   if (gfc_current_state () != COMP_DERIVED)
1728     t = add_init_expr_to_sym (name, &initializer, &var_locus);
1729   else
1730     {
1731       if (current_ts.type == BT_DERIVED
1732           && !current_attr.pointer && !initializer)
1733         initializer = gfc_default_initializer (&current_ts);
1734       t = build_struct (name, cl, &initializer, &as);
1735     }
1736
1737   m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1738
1739 cleanup:
1740   /* Free stuff up and return.  */
1741   gfc_free_expr (initializer);
1742   gfc_free_array_spec (as);
1743
1744   return m;
1745 }
1746
1747
1748 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1749    This assumes that the byte size is equal to the kind number for
1750    non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
1751
1752 match
1753 gfc_match_old_kind_spec (gfc_typespec *ts)
1754 {
1755   match m;
1756   int original_kind;
1757
1758   if (gfc_match_char ('*') != MATCH_YES)
1759     return MATCH_NO;
1760
1761   m = gfc_match_small_literal_int (&ts->kind, NULL);
1762   if (m != MATCH_YES)
1763     return MATCH_ERROR;
1764
1765   original_kind = ts->kind;
1766
1767   /* Massage the kind numbers for complex types.  */
1768   if (ts->type == BT_COMPLEX)
1769     {
1770       if (ts->kind % 2)
1771         {
1772           gfc_error ("Old-style type declaration %s*%d not supported at %C",
1773                      gfc_basic_typename (ts->type), original_kind);
1774           return MATCH_ERROR;
1775         }
1776       ts->kind /= 2;
1777     }
1778
1779   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1780     {
1781       gfc_error ("Old-style type declaration %s*%d not supported at %C",
1782                  gfc_basic_typename (ts->type), original_kind);
1783       return MATCH_ERROR;
1784     }
1785
1786   if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1787                       gfc_basic_typename (ts->type), original_kind) == FAILURE)
1788     return MATCH_ERROR;
1789
1790   return MATCH_YES;
1791 }
1792
1793
1794 /* Match a kind specification.  Since kinds are generally optional, we
1795    usually return MATCH_NO if something goes wrong.  If a "kind="
1796    string is found, then we know we have an error.  */
1797
1798 match
1799 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1800 {
1801   locus where, loc;
1802   gfc_expr *e;
1803   match m, n;
1804   const char *msg;
1805
1806   m = MATCH_NO;
1807   n = MATCH_YES;
1808   e = NULL;
1809
1810   where = loc = gfc_current_locus;
1811
1812   if (kind_expr_only)
1813     goto kind_expr;
1814
1815   if (gfc_match_char ('(') == MATCH_NO)
1816     return MATCH_NO;
1817
1818   /* Also gobbles optional text.  */
1819   if (gfc_match (" kind = ") == MATCH_YES)
1820     m = MATCH_ERROR;
1821
1822   loc = gfc_current_locus;
1823
1824 kind_expr:
1825   n = gfc_match_init_expr (&e);
1826
1827   if (n != MATCH_YES)
1828     {
1829       if (gfc_current_state () == COMP_INTERFACE
1830             || gfc_current_state () == COMP_NONE
1831             || gfc_current_state () == COMP_CONTAINS)
1832         {
1833           /* Signal using kind = -1 that the expression might include
1834              use associated or imported parameters and try again after
1835              the specification expressions.....  */
1836           if (gfc_match_char (')') != MATCH_YES)
1837             {
1838               gfc_error ("Missing right parenthesis at %C");
1839               m = MATCH_ERROR;
1840               goto no_match;
1841             }
1842
1843           gfc_free_expr (e);
1844           ts->kind = -1;
1845           gfc_function_kind_locus = loc;
1846           gfc_undo_symbols ();
1847           return MATCH_YES;
1848         }
1849       else
1850         {
1851           /* ....or else, the match is real.  */
1852           if (n == MATCH_NO)
1853             gfc_error ("Expected initialization expression at %C");
1854           if (n != MATCH_YES)
1855             return MATCH_ERROR;
1856         }
1857     }
1858
1859   if (e->rank != 0)
1860     {
1861       gfc_error ("Expected scalar initialization expression at %C");
1862       m = MATCH_ERROR;
1863       goto no_match;
1864     }
1865
1866   msg = gfc_extract_int (e, &ts->kind);
1867   if (msg != NULL)
1868     {
1869       gfc_error (msg);
1870       m = MATCH_ERROR;
1871       goto no_match;
1872     }
1873
1874   /* Before throwing away the expression, let's see if we had a
1875      C interoperable kind (and store the fact).  */
1876   if (e->ts.is_c_interop == 1)
1877     {
1878       /* Mark this as c interoperable if being declared with one
1879          of the named constants from iso_c_binding.  */
1880       ts->is_c_interop = e->ts.is_iso_c;
1881       ts->f90_type = e->ts.f90_type;
1882     }
1883   
1884   gfc_free_expr (e);
1885   e = NULL;
1886
1887   /* Ignore errors to this point, if we've gotten here.  This means
1888      we ignore the m=MATCH_ERROR from above.  */
1889   if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1890     {
1891       gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1892                  gfc_basic_typename (ts->type));
1893       m = MATCH_ERROR;
1894     }
1895   else if (gfc_match_char (')') != MATCH_YES)
1896     {
1897       gfc_error ("Missing right parenthesis at %C");
1898       m = MATCH_ERROR;
1899     }
1900   else
1901      /* All tests passed.  */
1902      m = MATCH_YES;
1903
1904   if(m == MATCH_ERROR)
1905      gfc_current_locus = where;
1906   
1907   /* Return what we know from the test(s).  */
1908   return m;
1909
1910 no_match:
1911   gfc_free_expr (e);
1912   gfc_current_locus = where;
1913   return m;
1914 }
1915
1916
1917 static match
1918 match_char_kind (int * kind, int * is_iso_c)
1919 {
1920   locus where;
1921   gfc_expr *e;
1922   match m, n;
1923   const char *msg;
1924
1925   m = MATCH_NO;
1926   e = NULL;
1927   where = gfc_current_locus;
1928
1929   n = gfc_match_init_expr (&e);
1930   if (n == MATCH_NO)
1931     gfc_error ("Expected initialization expression at %C");
1932   if (n != MATCH_YES)
1933     return MATCH_ERROR;
1934
1935   if (e->rank != 0)
1936     {
1937       gfc_error ("Expected scalar initialization expression at %C");
1938       m = MATCH_ERROR;
1939       goto no_match;
1940     }
1941
1942   msg = gfc_extract_int (e, kind);
1943   *is_iso_c = e->ts.is_iso_c;
1944   if (msg != NULL)
1945     {
1946       gfc_error (msg);
1947       m = MATCH_ERROR;
1948       goto no_match;
1949     }
1950
1951   gfc_free_expr (e);
1952
1953   /* Ignore errors to this point, if we've gotten here.  This means
1954      we ignore the m=MATCH_ERROR from above.  */
1955   if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
1956     {
1957       gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
1958       m = MATCH_ERROR;
1959     }
1960   else
1961      /* All tests passed.  */
1962      m = MATCH_YES;
1963
1964   if (m == MATCH_ERROR)
1965      gfc_current_locus = where;
1966   
1967   /* Return what we know from the test(s).  */
1968   return m;
1969
1970 no_match:
1971   gfc_free_expr (e);
1972   gfc_current_locus = where;
1973   return m;
1974 }
1975
1976 /* Match the various kind/length specifications in a CHARACTER
1977    declaration.  We don't return MATCH_NO.  */
1978
1979 static match
1980 match_char_spec (gfc_typespec *ts)
1981 {
1982   int kind, seen_length, is_iso_c;
1983   gfc_charlen *cl;
1984   gfc_expr *len;
1985   match m;
1986
1987   len = NULL;
1988   seen_length = 0;
1989   kind = 0;
1990   is_iso_c = 0;
1991
1992   /* Try the old-style specification first.  */
1993   old_char_selector = 0;
1994
1995   m = match_char_length (&len);
1996   if (m != MATCH_NO)
1997     {
1998       if (m == MATCH_YES)
1999         old_char_selector = 1;
2000       seen_length = 1;
2001       goto done;
2002     }
2003
2004   m = gfc_match_char ('(');
2005   if (m != MATCH_YES)
2006     {
2007       m = MATCH_YES;    /* Character without length is a single char.  */
2008       goto done;
2009     }
2010
2011   /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
2012   if (gfc_match (" kind =") == MATCH_YES)
2013     {
2014       m = match_char_kind (&kind, &is_iso_c);
2015        
2016       if (m == MATCH_ERROR)
2017         goto done;
2018       if (m == MATCH_NO)
2019         goto syntax;
2020
2021       if (gfc_match (" , len =") == MATCH_NO)
2022         goto rparen;
2023
2024       m = char_len_param_value (&len);
2025       if (m == MATCH_NO)
2026         goto syntax;
2027       if (m == MATCH_ERROR)
2028         goto done;
2029       seen_length = 1;
2030
2031       goto rparen;
2032     }
2033
2034   /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
2035   if (gfc_match (" len =") == MATCH_YES)
2036     {
2037       m = char_len_param_value (&len);
2038       if (m == MATCH_NO)
2039         goto syntax;
2040       if (m == MATCH_ERROR)
2041         goto done;
2042       seen_length = 1;
2043
2044       if (gfc_match_char (')') == MATCH_YES)
2045         goto done;
2046
2047       if (gfc_match (" , kind =") != MATCH_YES)
2048         goto syntax;
2049
2050       if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2051         goto done;
2052
2053       goto rparen;
2054     }
2055
2056   /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
2057   m = char_len_param_value (&len);
2058   if (m == MATCH_NO)
2059     goto syntax;
2060   if (m == MATCH_ERROR)
2061     goto done;
2062   seen_length = 1;
2063
2064   m = gfc_match_char (')');
2065   if (m == MATCH_YES)
2066     goto done;
2067
2068   if (gfc_match_char (',') != MATCH_YES)
2069     goto syntax;
2070
2071   gfc_match (" kind =");        /* Gobble optional text.  */
2072
2073   m = match_char_kind (&kind, &is_iso_c);
2074   if (m == MATCH_ERROR)
2075     goto done;
2076   if (m == MATCH_NO)
2077     goto syntax;
2078
2079 rparen:
2080   /* Require a right-paren at this point.  */
2081   m = gfc_match_char (')');
2082   if (m == MATCH_YES)
2083     goto done;
2084
2085 syntax:
2086   gfc_error ("Syntax error in CHARACTER declaration at %C");
2087   m = MATCH_ERROR;
2088   gfc_free_expr (len);
2089   return m;
2090
2091 done:
2092   if (m != MATCH_YES)
2093     {
2094       gfc_free_expr (len);
2095       return m;
2096     }
2097
2098   /* Do some final massaging of the length values.  */
2099   cl = gfc_get_charlen ();
2100   cl->next = gfc_current_ns->cl_list;
2101   gfc_current_ns->cl_list = cl;
2102
2103   if (seen_length == 0)
2104     cl->length = gfc_int_expr (1);
2105   else
2106     cl->length = len;
2107
2108   ts->cl = cl;
2109   ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2110
2111   /* We have to know if it was a c interoperable kind so we can
2112      do accurate type checking of bind(c) procs, etc.  */
2113   if (kind != 0)
2114     /* Mark this as c interoperable if being declared with one
2115        of the named constants from iso_c_binding.  */
2116     ts->is_c_interop = is_iso_c;
2117   else if (len != NULL)
2118     /* Here, we might have parsed something such as: character(c_char)
2119        In this case, the parsing code above grabs the c_char when
2120        looking for the length (line 1690, roughly).  it's the last
2121        testcase for parsing the kind params of a character variable.
2122        However, it's not actually the length.    this seems like it
2123        could be an error.  
2124        To see if the user used a C interop kind, test the expr
2125        of the so called length, and see if it's C interoperable.  */
2126     ts->is_c_interop = len->ts.is_iso_c;
2127   
2128   return MATCH_YES;
2129 }
2130
2131
2132 /* Matches a type specification.  If successful, sets the ts structure
2133    to the matched specification.  This is necessary for FUNCTION and
2134    IMPLICIT statements.
2135
2136    If implicit_flag is nonzero, then we don't check for the optional
2137    kind specification.  Not doing so is needed for matching an IMPLICIT
2138    statement correctly.  */
2139
2140 match
2141 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2142 {
2143   char name[GFC_MAX_SYMBOL_LEN + 1];
2144   gfc_symbol *sym;
2145   match m;
2146   int c;
2147   locus loc = gfc_current_locus;
2148
2149   gfc_clear_ts (ts);
2150
2151   /* Clear the current binding label, in case one is given.  */
2152   curr_binding_label[0] = '\0';
2153
2154   if (gfc_match (" byte") == MATCH_YES)
2155     {
2156       if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2157           == FAILURE)
2158         return MATCH_ERROR;
2159
2160       if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2161         {
2162           gfc_error ("BYTE type used at %C "
2163                      "is not available on the target machine");
2164           return MATCH_ERROR;
2165         }
2166
2167       ts->type = BT_INTEGER;
2168       ts->kind = 1;
2169       return MATCH_YES;
2170     }
2171
2172   if (gfc_match (" integer") == MATCH_YES)
2173     {
2174       ts->type = BT_INTEGER;
2175       ts->kind = gfc_default_integer_kind;
2176       goto get_kind;
2177     }
2178
2179   if (gfc_match (" character") == MATCH_YES)
2180     {
2181       ts->type = BT_CHARACTER;
2182       if (implicit_flag == 0)
2183         return match_char_spec (ts);
2184       else
2185         return MATCH_YES;
2186     }
2187
2188   if (gfc_match (" real") == MATCH_YES)
2189     {
2190       ts->type = BT_REAL;
2191       ts->kind = gfc_default_real_kind;
2192       goto get_kind;
2193     }
2194
2195   if (gfc_match (" double precision") == MATCH_YES)
2196     {
2197       ts->type = BT_REAL;
2198       ts->kind = gfc_default_double_kind;
2199       return MATCH_YES;
2200     }
2201
2202   if (gfc_match (" complex") == MATCH_YES)
2203     {
2204       ts->type = BT_COMPLEX;
2205       ts->kind = gfc_default_complex_kind;
2206       goto get_kind;
2207     }
2208
2209   if (gfc_match (" double complex") == MATCH_YES)
2210     {
2211       if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2212                           "conform to the Fortran 95 standard") == FAILURE)
2213         return MATCH_ERROR;
2214
2215       ts->type = BT_COMPLEX;
2216       ts->kind = gfc_default_double_kind;
2217       return MATCH_YES;
2218     }
2219
2220   if (gfc_match (" logical") == MATCH_YES)
2221     {
2222       ts->type = BT_LOGICAL;
2223       ts->kind = gfc_default_logical_kind;
2224       goto get_kind;
2225     }
2226
2227   m = gfc_match (" type ( %n )", name);
2228   if (m != MATCH_YES)
2229     return m;
2230
2231   if (gfc_current_state () == COMP_INTERFACE
2232         || gfc_current_state () == COMP_NONE)
2233     {
2234       gfc_function_type_locus = loc;
2235       ts->type = BT_UNKNOWN;
2236       ts->kind = -1;
2237       return MATCH_YES;
2238     }
2239
2240   /* Search for the name but allow the components to be defined later.  If
2241      type = -1, this typespec has been seen in a function declaration but
2242      the type could not legally be accessed at that point.  */
2243   if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2244     {
2245       gfc_error ("Type name '%s' at %C is ambiguous", name);
2246       return MATCH_ERROR;
2247     }
2248   else if (ts->kind == -1)
2249     {
2250       if (gfc_find_symbol (name, NULL, 0, &sym))
2251         {       
2252           gfc_error ("Type name '%s' at %C is ambiguous", name);
2253           return MATCH_ERROR;
2254         }
2255
2256       if (sym == NULL)
2257         return MATCH_NO;
2258     }
2259
2260   if (sym->attr.flavor != FL_DERIVED
2261       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2262     return MATCH_ERROR;
2263
2264   ts->type = BT_DERIVED;
2265   ts->kind = 0;
2266   ts->derived = sym;
2267
2268   return MATCH_YES;
2269
2270 get_kind:
2271   /* For all types except double, derived and character, look for an
2272      optional kind specifier.  MATCH_NO is actually OK at this point.  */
2273   if (implicit_flag == 1)
2274     return MATCH_YES;
2275
2276   if (gfc_current_form == FORM_FREE)
2277     {
2278       c = gfc_peek_char();
2279       if (!gfc_is_whitespace(c) && c != '*' && c != '('
2280           && c != ':' && c != ',')
2281        return MATCH_NO;
2282     }
2283
2284   m = gfc_match_kind_spec (ts, false);
2285   if (m == MATCH_NO && ts->type != BT_CHARACTER)
2286     m = gfc_match_old_kind_spec (ts);
2287
2288   if (m == MATCH_NO)
2289     m = MATCH_YES;              /* No kind specifier found.  */
2290
2291   return m;
2292 }
2293
2294
2295 /* Match an IMPLICIT NONE statement.  Actually, this statement is
2296    already matched in parse.c, or we would not end up here in the
2297    first place.  So the only thing we need to check, is if there is
2298    trailing garbage.  If not, the match is successful.  */
2299
2300 match
2301 gfc_match_implicit_none (void)
2302 {
2303   return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2304 }
2305
2306
2307 /* Match the letter range(s) of an IMPLICIT statement.  */
2308
2309 static match
2310 match_implicit_range (void)
2311 {
2312   int c, c1, c2, inner;
2313   locus cur_loc;
2314
2315   cur_loc = gfc_current_locus;
2316
2317   gfc_gobble_whitespace ();
2318   c = gfc_next_char ();
2319   if (c != '(')
2320     {
2321       gfc_error ("Missing character range in IMPLICIT at %C");
2322       goto bad;
2323     }
2324
2325   inner = 1;
2326   while (inner)
2327     {
2328       gfc_gobble_whitespace ();
2329       c1 = gfc_next_char ();
2330       if (!ISALPHA (c1))
2331         goto bad;
2332
2333       gfc_gobble_whitespace ();
2334       c = gfc_next_char ();
2335
2336       switch (c)
2337         {
2338         case ')':
2339           inner = 0;            /* Fall through.  */
2340
2341         case ',':
2342           c2 = c1;
2343           break;
2344
2345         case '-':
2346           gfc_gobble_whitespace ();
2347           c2 = gfc_next_char ();
2348           if (!ISALPHA (c2))
2349             goto bad;
2350
2351           gfc_gobble_whitespace ();
2352           c = gfc_next_char ();
2353
2354           if ((c != ',') && (c != ')'))
2355             goto bad;
2356           if (c == ')')
2357             inner = 0;
2358
2359           break;
2360
2361         default:
2362           goto bad;
2363         }
2364
2365       if (c1 > c2)
2366         {
2367           gfc_error ("Letters must be in alphabetic order in "
2368                      "IMPLICIT statement at %C");
2369           goto bad;
2370         }
2371
2372       /* See if we can add the newly matched range to the pending
2373          implicits from this IMPLICIT statement.  We do not check for
2374          conflicts with whatever earlier IMPLICIT statements may have
2375          set.  This is done when we've successfully finished matching
2376          the current one.  */
2377       if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2378         goto bad;
2379     }
2380
2381   return MATCH_YES;
2382
2383 bad:
2384   gfc_syntax_error (ST_IMPLICIT);
2385
2386   gfc_current_locus = cur_loc;
2387   return MATCH_ERROR;
2388 }
2389
2390
2391 /* Match an IMPLICIT statement, storing the types for
2392    gfc_set_implicit() if the statement is accepted by the parser.
2393    There is a strange looking, but legal syntactic construction
2394    possible.  It looks like:
2395
2396      IMPLICIT INTEGER (a-b) (c-d)
2397
2398    This is legal if "a-b" is a constant expression that happens to
2399    equal one of the legal kinds for integers.  The real problem
2400    happens with an implicit specification that looks like:
2401
2402      IMPLICIT INTEGER (a-b)
2403
2404    In this case, a typespec matcher that is "greedy" (as most of the
2405    matchers are) gobbles the character range as a kindspec, leaving
2406    nothing left.  We therefore have to go a bit more slowly in the
2407    matching process by inhibiting the kindspec checking during
2408    typespec matching and checking for a kind later.  */
2409
2410 match
2411 gfc_match_implicit (void)
2412 {
2413   gfc_typespec ts;
2414   locus cur_loc;
2415   int c;
2416   match m;
2417
2418   /* We don't allow empty implicit statements.  */
2419   if (gfc_match_eos () == MATCH_YES)
2420     {
2421       gfc_error ("Empty IMPLICIT statement at %C");
2422       return MATCH_ERROR;
2423     }
2424
2425   do
2426     {
2427       /* First cleanup.  */
2428       gfc_clear_new_implicit ();
2429
2430       /* A basic type is mandatory here.  */
2431       m = gfc_match_type_spec (&ts, 1);
2432       if (m == MATCH_ERROR)
2433         goto error;
2434       if (m == MATCH_NO)
2435         goto syntax;
2436
2437       cur_loc = gfc_current_locus;
2438       m = match_implicit_range ();
2439
2440       if (m == MATCH_YES)
2441         {
2442           /* We may have <TYPE> (<RANGE>).  */
2443           gfc_gobble_whitespace ();
2444           c = gfc_next_char ();
2445           if ((c == '\n') || (c == ','))
2446             {
2447               /* Check for CHARACTER with no length parameter.  */
2448               if (ts.type == BT_CHARACTER && !ts.cl)
2449                 {
2450                   ts.kind = gfc_default_character_kind;
2451                   ts.cl = gfc_get_charlen ();
2452                   ts.cl->next = gfc_current_ns->cl_list;
2453                   gfc_current_ns->cl_list = ts.cl;
2454                   ts.cl->length = gfc_int_expr (1);
2455                 }
2456
2457               /* Record the Successful match.  */
2458               if (gfc_merge_new_implicit (&ts) != SUCCESS)
2459                 return MATCH_ERROR;
2460               continue;
2461             }
2462
2463           gfc_current_locus = cur_loc;
2464         }
2465
2466       /* Discard the (incorrectly) matched range.  */
2467       gfc_clear_new_implicit ();
2468
2469       /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
2470       if (ts.type == BT_CHARACTER)
2471         m = match_char_spec (&ts);
2472       else
2473         {
2474           m = gfc_match_kind_spec (&ts, false);
2475           if (m == MATCH_NO)
2476             {
2477               m = gfc_match_old_kind_spec (&ts);
2478               if (m == MATCH_ERROR)
2479                 goto error;
2480               if (m == MATCH_NO)
2481                 goto syntax;
2482             }
2483         }
2484       if (m == MATCH_ERROR)
2485         goto error;
2486
2487       m = match_implicit_range ();
2488       if (m == MATCH_ERROR)
2489         goto error;
2490       if (m == MATCH_NO)
2491         goto syntax;
2492
2493       gfc_gobble_whitespace ();
2494       c = gfc_next_char ();
2495       if ((c != '\n') && (c != ','))
2496         goto syntax;
2497
2498       if (gfc_merge_new_implicit (&ts) != SUCCESS)
2499         return MATCH_ERROR;
2500     }
2501   while (c == ',');
2502
2503   return MATCH_YES;
2504
2505 syntax:
2506   gfc_syntax_error (ST_IMPLICIT);
2507
2508 error:
2509   return MATCH_ERROR;
2510 }
2511
2512
2513 match
2514 gfc_match_import (void)
2515 {
2516   char name[GFC_MAX_SYMBOL_LEN + 1];
2517   match m;
2518   gfc_symbol *sym;
2519   gfc_symtree *st;
2520
2521   if (gfc_current_ns->proc_name == NULL
2522       || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2523     {
2524       gfc_error ("IMPORT statement at %C only permitted in "
2525                  "an INTERFACE body");
2526       return MATCH_ERROR;
2527     }
2528
2529   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2530       == FAILURE)
2531     return MATCH_ERROR;
2532
2533   if (gfc_match_eos () == MATCH_YES)
2534     {
2535       /* All host variables should be imported.  */
2536       gfc_current_ns->has_import_set = 1;
2537       return MATCH_YES;
2538     }
2539
2540   if (gfc_match (" ::") == MATCH_YES)
2541     {
2542       if (gfc_match_eos () == MATCH_YES)
2543         {
2544            gfc_error ("Expecting list of named entities at %C");
2545            return MATCH_ERROR;
2546         }
2547     }
2548
2549   for(;;)
2550     {
2551       m = gfc_match (" %n", name);
2552       switch (m)
2553         {
2554         case MATCH_YES:
2555           if (gfc_current_ns->parent !=  NULL
2556               && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2557             {
2558                gfc_error ("Type name '%s' at %C is ambiguous", name);
2559                return MATCH_ERROR;
2560             }
2561           else if (gfc_current_ns->proc_name->ns->parent !=  NULL
2562                    && gfc_find_symbol (name,
2563                                        gfc_current_ns->proc_name->ns->parent,
2564                                        1, &sym))
2565             {
2566                gfc_error ("Type name '%s' at %C is ambiguous", name);
2567                return MATCH_ERROR;
2568             }
2569
2570           if (sym == NULL)
2571             {
2572               gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2573                          "at %C - does not exist.", name);
2574               return MATCH_ERROR;
2575             }
2576
2577           if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2578             {
2579               gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2580                            "at %C.", name);
2581               goto next_item;
2582             }
2583
2584           st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2585           st->n.sym = sym;
2586           sym->refs++;
2587           sym->attr.imported = 1;
2588
2589           goto next_item;
2590
2591         case MATCH_NO:
2592           break;
2593
2594         case MATCH_ERROR:
2595           return MATCH_ERROR;
2596         }
2597
2598     next_item:
2599       if (gfc_match_eos () == MATCH_YES)
2600         break;
2601       if (gfc_match_char (',') != MATCH_YES)
2602         goto syntax;
2603     }
2604
2605   return MATCH_YES;
2606
2607 syntax:
2608   gfc_error ("Syntax error in IMPORT statement at %C");
2609   return MATCH_ERROR;
2610 }
2611
2612
2613 /* A minimal implementation of gfc_match without whitespace, escape
2614    characters or variable arguments.  Returns true if the next
2615    characters match the TARGET template exactly.  */
2616
2617 static bool
2618 match_string_p (const char *target)
2619 {
2620   const char *p;
2621
2622   for (p = target; *p; p++)
2623     if (gfc_next_char () != *p)
2624       return false;
2625   return true;
2626 }
2627
2628 /* Matches an attribute specification including array specs.  If
2629    successful, leaves the variables current_attr and current_as
2630    holding the specification.  Also sets the colon_seen variable for
2631    later use by matchers associated with initializations.
2632
2633    This subroutine is a little tricky in the sense that we don't know
2634    if we really have an attr-spec until we hit the double colon.
2635    Until that time, we can only return MATCH_NO.  This forces us to
2636    check for duplicate specification at this level.  */
2637
2638 static match
2639 match_attr_spec (void)
2640 {
2641   /* Modifiers that can exist in a type statement.  */
2642   typedef enum
2643   { GFC_DECL_BEGIN = 0,
2644     DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2645     DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2646     DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2647     DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2648     DECL_IS_BIND_C, DECL_NONE,
2649     GFC_DECL_END /* Sentinel */
2650   }
2651   decl_types;
2652
2653 /* GFC_DECL_END is the sentinel, index starts at 0.  */
2654 #define NUM_DECL GFC_DECL_END
2655
2656   locus start, seen_at[NUM_DECL];
2657   int seen[NUM_DECL];
2658   decl_types d;
2659   const char *attr;
2660   match m;
2661   try t;
2662
2663   gfc_clear_attr (&current_attr);
2664   start = gfc_current_locus;
2665
2666   current_as = NULL;
2667   colon_seen = 0;
2668
2669   /* See if we get all of the keywords up to the final double colon.  */
2670   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2671     seen[d] = 0;
2672
2673   for (;;)
2674     {
2675       int ch;
2676
2677       d = DECL_NONE;
2678       gfc_gobble_whitespace ();
2679
2680       ch = gfc_next_char ();
2681       if (ch == ':')
2682         {
2683           /* This is the successful exit condition for the loop.  */
2684           if (gfc_next_char () == ':')
2685             break;
2686         }
2687       else if (ch == ',')
2688         {
2689           gfc_gobble_whitespace ();
2690           switch (gfc_peek_char ())
2691             {
2692             case 'a':
2693               if (match_string_p ("allocatable"))
2694                 d = DECL_ALLOCATABLE;
2695               break;
2696
2697             case 'b':
2698               /* Try and match the bind(c).  */
2699               m = gfc_match_bind_c (NULL);
2700               if (m == MATCH_YES)
2701                 d = DECL_IS_BIND_C;
2702               else if (m == MATCH_ERROR)
2703                 goto cleanup;
2704               break;
2705
2706             case 'd':
2707               if (match_string_p ("dimension"))
2708                 d = DECL_DIMENSION;
2709               break;
2710
2711             case 'e':
2712               if (match_string_p ("external"))
2713                 d = DECL_EXTERNAL;
2714               break;
2715
2716             case 'i':
2717               if (match_string_p ("int"))
2718                 {
2719                   ch = gfc_next_char ();
2720                   if (ch == 'e')
2721                     {
2722                       if (match_string_p ("nt"))
2723                         {
2724                           /* Matched "intent".  */
2725                           /* TODO: Call match_intent_spec from here.  */
2726                           if (gfc_match (" ( in out )") == MATCH_YES)
2727                             d = DECL_INOUT;
2728                           else if (gfc_match (" ( in )") == MATCH_YES)
2729                             d = DECL_IN;
2730                           else if (gfc_match (" ( out )") == MATCH_YES)
2731                             d = DECL_OUT;
2732                         }
2733                     }
2734                   else if (ch == 'r')
2735                     {
2736                       if (match_string_p ("insic"))
2737                         {
2738                           /* Matched "intrinsic".  */
2739                           d = DECL_INTRINSIC;
2740                         }
2741                     }
2742                 }
2743               break;
2744
2745             case 'o':
2746               if (match_string_p ("optional"))
2747                 d = DECL_OPTIONAL;
2748               break;
2749
2750             case 'p':
2751               gfc_next_char ();
2752               switch (gfc_next_char ())
2753                 {
2754                 case 'a':
2755                   if (match_string_p ("rameter"))
2756                     {
2757                       /* Matched "parameter".  */
2758                       d = DECL_PARAMETER;
2759                     }
2760                   break;
2761
2762                 case 'o':
2763                   if (match_string_p ("inter"))
2764                     {
2765                       /* Matched "pointer".  */
2766                       d = DECL_POINTER;
2767                     }
2768                   break;
2769
2770                 case 'r':
2771                   ch = gfc_next_char ();
2772                   if (ch == 'i')
2773                     {
2774                       if (match_string_p ("vate"))
2775                         {
2776                           /* Matched "private".  */
2777                           d = DECL_PRIVATE;
2778                         }
2779                     }
2780                   else if (ch == 'o')
2781                     {
2782                       if (match_string_p ("tected"))
2783                         {
2784                           /* Matched "protected".  */
2785                           d = DECL_PROTECTED;
2786                         }
2787                     }
2788                   break;
2789
2790                 case 'u':
2791                   if (match_string_p ("blic"))
2792                     {
2793                       /* Matched "public".  */
2794                       d = DECL_PUBLIC;
2795                     }
2796                   break;
2797                 }
2798               break;
2799
2800             case 's':
2801               if (match_string_p ("save"))
2802                 d = DECL_SAVE;
2803               break;
2804
2805             case 't':
2806               if (match_string_p ("target"))
2807                 d = DECL_TARGET;
2808               break;
2809
2810             case 'v':
2811               gfc_next_char ();
2812               ch = gfc_next_char ();
2813               if (ch == 'a')
2814                 {
2815                   if (match_string_p ("lue"))
2816                     {
2817                       /* Matched "value".  */
2818                       d = DECL_VALUE;
2819                     }
2820                 }
2821               else if (ch == 'o')
2822                 {
2823                   if (match_string_p ("latile"))
2824                     {
2825                       /* Matched "volatile".  */
2826                       d = DECL_VOLATILE;
2827                     }
2828                 }
2829               break;
2830             }
2831         }
2832
2833       /* No double colon and no recognizable decl_type, so assume that
2834          we've been looking at something else the whole time.  */
2835       if (d == DECL_NONE)
2836         {
2837           m = MATCH_NO;
2838           goto cleanup;
2839         }
2840
2841       seen[d]++;
2842       seen_at[d] = gfc_current_locus;
2843
2844       if (d == DECL_DIMENSION)
2845         {
2846           m = gfc_match_array_spec (&current_as);
2847
2848           if (m == MATCH_NO)
2849             {
2850               gfc_error ("Missing dimension specification at %C");
2851               m = MATCH_ERROR;
2852             }
2853
2854           if (m == MATCH_ERROR)
2855             goto cleanup;
2856         }
2857     }
2858
2859   /* Since we've seen a double colon, we have to be looking at an
2860      attr-spec.  This means that we can now issue errors.  */
2861   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2862     if (seen[d] > 1)
2863       {
2864         switch (d)
2865           {
2866           case DECL_ALLOCATABLE:
2867             attr = "ALLOCATABLE";
2868             break;
2869           case DECL_DIMENSION:
2870             attr = "DIMENSION";
2871             break;
2872           case DECL_EXTERNAL:
2873             attr = "EXTERNAL";
2874             break;
2875           case DECL_IN:
2876             attr = "INTENT (IN)";
2877             break;
2878           case DECL_OUT:
2879             attr = "INTENT (OUT)";
2880             break;
2881           case DECL_INOUT:
2882             attr = "INTENT (IN OUT)";
2883             break;
2884           case DECL_INTRINSIC:
2885             attr = "INTRINSIC";
2886             break;
2887           case DECL_OPTIONAL:
2888             attr = "OPTIONAL";
2889             break;
2890           case DECL_PARAMETER:
2891             attr = "PARAMETER";
2892             break;
2893           case DECL_POINTER:
2894             attr = "POINTER";
2895             break;
2896           case DECL_PROTECTED:
2897             attr = "PROTECTED";
2898             break;
2899           case DECL_PRIVATE:
2900             attr = "PRIVATE";
2901             break;
2902           case DECL_PUBLIC:
2903             attr = "PUBLIC";
2904             break;
2905           case DECL_SAVE:
2906             attr = "SAVE";
2907             break;
2908           case DECL_TARGET:
2909             attr = "TARGET";
2910             break;
2911           case DECL_IS_BIND_C:
2912             attr = "IS_BIND_C";
2913             break;
2914           case DECL_VALUE:
2915             attr = "VALUE";
2916             break;
2917           case DECL_VOLATILE:
2918             attr = "VOLATILE";
2919             break;
2920           default:
2921             attr = NULL;        /* This shouldn't happen.  */
2922           }
2923
2924         gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2925         m = MATCH_ERROR;
2926         goto cleanup;
2927       }
2928
2929   /* Now that we've dealt with duplicate attributes, add the attributes
2930      to the current attribute.  */
2931   for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2932     {
2933       if (seen[d] == 0)
2934         continue;
2935
2936       if (gfc_current_state () == COMP_DERIVED
2937           && d != DECL_DIMENSION && d != DECL_POINTER
2938           && d != DECL_PRIVATE   && d != DECL_PUBLIC
2939           && d != DECL_NONE)
2940         {
2941           if (d == DECL_ALLOCATABLE)
2942             {
2943               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2944                                   "attribute at %C in a TYPE definition")
2945                   == FAILURE)
2946                 {
2947                   m = MATCH_ERROR;
2948                   goto cleanup;
2949                 }
2950             }
2951           else
2952             {
2953               gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2954                          &seen_at[d]);
2955               m = MATCH_ERROR;
2956               goto cleanup;
2957             }
2958         }
2959
2960       if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2961           && gfc_current_state () != COMP_MODULE)
2962         {
2963           if (d == DECL_PRIVATE)
2964             attr = "PRIVATE";
2965           else
2966             attr = "PUBLIC";
2967           if (gfc_current_state () == COMP_DERIVED
2968               && gfc_state_stack->previous
2969               && gfc_state_stack->previous->state == COMP_MODULE)
2970             {
2971               if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2972                                   "at %L in a TYPE definition", attr,
2973                                   &seen_at[d])
2974                   == FAILURE)
2975                 {
2976                   m = MATCH_ERROR;
2977                   goto cleanup;
2978                 }
2979             }
2980           else
2981             {
2982               gfc_error ("%s attribute at %L is not allowed outside of the "
2983                          "specification part of a module", attr, &seen_at[d]);
2984               m = MATCH_ERROR;
2985               goto cleanup;
2986             }
2987         }
2988
2989       switch (d)
2990         {
2991         case DECL_ALLOCATABLE:
2992           t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2993           break;
2994
2995         case DECL_DIMENSION:
2996           t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2997           break;
2998
2999         case DECL_EXTERNAL:
3000           t = gfc_add_external (&current_attr, &seen_at[d]);
3001           break;
3002
3003         case DECL_IN:
3004           t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3005           break;
3006
3007         case DECL_OUT:
3008           t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3009           break;
3010
3011         case DECL_INOUT:
3012           t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3013           break;
3014
3015         case DECL_INTRINSIC:
3016           t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3017           break;
3018
3019         case DECL_OPTIONAL:
3020           t = gfc_add_optional (&current_attr, &seen_at[d]);
3021           break;
3022
3023         case DECL_PARAMETER:
3024           t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3025           break;
3026
3027         case DECL_POINTER:
3028           t = gfc_add_pointer (&current_attr, &seen_at[d]);
3029           break;
3030
3031         case DECL_PROTECTED:
3032           if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3033             {
3034                gfc_error ("PROTECTED at %C only allowed in specification "
3035                           "part of a module");
3036                t = FAILURE;
3037                break;
3038             }
3039
3040           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3041                               "attribute at %C")
3042               == FAILURE)
3043             t = FAILURE;
3044           else
3045             t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3046           break;
3047
3048         case DECL_PRIVATE:
3049           t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3050                               &seen_at[d]);
3051           break;
3052
3053         case DECL_PUBLIC:
3054           t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3055                               &seen_at[d]);
3056           break;
3057
3058         case DECL_SAVE:
3059           t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
3060           break;
3061
3062         case DECL_TARGET:
3063           t = gfc_add_target (&current_attr, &seen_at[d]);
3064           break;
3065
3066         case DECL_IS_BIND_C:
3067            t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3068            break;
3069            
3070         case DECL_VALUE:
3071           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3072                               "at %C")
3073               == FAILURE)
3074             t = FAILURE;
3075           else
3076             t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3077           break;
3078
3079         case DECL_VOLATILE:
3080           if (gfc_notify_std (GFC_STD_F2003,
3081                               "Fortran 2003: VOLATILE attribute at %C")
3082               == FAILURE)
3083             t = FAILURE;
3084           else
3085             t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3086           break;
3087
3088         default:
3089           gfc_internal_error ("match_attr_spec(): Bad attribute");
3090         }
3091
3092       if (t == FAILURE)
3093         {
3094           m = MATCH_ERROR;
3095           goto cleanup;
3096         }
3097     }
3098
3099   colon_seen = 1;
3100   return MATCH_YES;
3101
3102 cleanup:
3103   gfc_current_locus = start;
3104   gfc_free_array_spec (current_as);
3105   current_as = NULL;
3106   return m;
3107 }
3108
3109
3110 /* Set the binding label, dest_label, either with the binding label
3111    stored in the given gfc_typespec, ts, or if none was provided, it
3112    will be the symbol name in all lower case, as required by the draft
3113    (J3/04-007, section 15.4.1).  If a binding label was given and
3114    there is more than one argument (num_idents), it is an error.  */
3115
3116 try
3117 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3118 {
3119   if (num_idents > 1 && has_name_equals)
3120     {
3121       gfc_error ("Multiple identifiers provided with "
3122                  "single NAME= specifier at %C");
3123       return FAILURE;
3124     }
3125
3126   if (curr_binding_label[0] != '\0')
3127     {
3128       /* Binding label given; store in temp holder til have sym.  */
3129       strncpy (dest_label, curr_binding_label,
3130                strlen (curr_binding_label) + 1);
3131     }
3132   else
3133     {
3134       /* No binding label given, and the NAME= specifier did not exist,
3135          which means there was no NAME="".  */
3136       if (sym_name != NULL && has_name_equals == 0)
3137         strncpy (dest_label, sym_name, strlen (sym_name) + 1);
3138     }
3139    
3140   return SUCCESS;
3141 }
3142
3143
3144 /* Set the status of the given common block as being BIND(C) or not,
3145    depending on the given parameter, is_bind_c.  */
3146
3147 void
3148 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3149 {
3150   com_block->is_bind_c = is_bind_c;
3151   return;
3152 }
3153
3154
3155 /* Verify that the given gfc_typespec is for a C interoperable type.  */
3156
3157 try
3158 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3159 {
3160   try t;
3161
3162   /* Make sure the kind used is appropriate for the type.
3163      The f90_type is unknown if an integer constant was
3164      used (e.g., real(4), bind(c) :: myFloat).  */
3165   if (ts->f90_type != BT_UNKNOWN)
3166     {
3167       t = gfc_validate_c_kind (ts);
3168       if (t != SUCCESS)
3169         {
3170           /* Print an error, but continue parsing line.  */
3171           gfc_error_now ("C kind parameter is for type %s but "
3172                          "symbol '%s' at %L is of type %s",
3173                          gfc_basic_typename (ts->f90_type),
3174                          name, where, 
3175                          gfc_basic_typename (ts->type));
3176         }
3177     }
3178
3179   /* Make sure the kind is C interoperable.  This does not care about the
3180      possible error above.  */
3181   if (ts->type == BT_DERIVED && ts->derived != NULL)
3182     return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3183   else if (ts->is_c_interop != 1)
3184     return FAILURE;
3185   
3186   return SUCCESS;
3187 }
3188
3189
3190 /* Verify that the variables of a given common block, which has been
3191    defined with the attribute specifier bind(c), to be of a C
3192    interoperable type.  Errors will be reported here, if
3193    encountered.  */
3194
3195 try
3196 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3197 {
3198   gfc_symbol *curr_sym = NULL;
3199   try retval = SUCCESS;
3200
3201   curr_sym = com_block->head;
3202   
3203   /* Make sure we have at least one symbol.  */
3204   if (curr_sym == NULL)
3205     return retval;
3206
3207   /* Here we know we have a symbol, so we'll execute this loop
3208      at least once.  */
3209   do
3210     {
3211       /* The second to last param, 1, says this is in a common block.  */
3212       retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3213       curr_sym = curr_sym->common_next;
3214     } while (curr_sym != NULL); 
3215
3216   return retval;
3217 }
3218
3219
3220 /* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
3221    an appropriate error message is reported.  */
3222
3223 try
3224 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3225                    int is_in_common, gfc_common_head *com_block)
3226 {
3227   try retval = SUCCESS;
3228
3229   if (tmp_sym->attr.function && tmp_sym->result != NULL)
3230     {
3231       tmp_sym = tmp_sym->result;
3232       /* Make sure it wasn't an implicitly typed result.  */
3233       if (tmp_sym->attr.implicit_type)
3234         {
3235           gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3236                        "%L may not be C interoperable", tmp_sym->name,
3237                        &tmp_sym->declared_at);
3238           tmp_sym->ts.f90_type = tmp_sym->ts.type;
3239           /* Mark it as C interoperable to prevent duplicate warnings.  */
3240           tmp_sym->ts.is_c_interop = 1;
3241           tmp_sym->attr.is_c_interop = 1;
3242         }
3243     }
3244   
3245   /* Here, we know we have the bind(c) attribute, so if we have
3246      enough type info, then verify that it's a C interop kind.
3247      The info could be in the symbol already, or possibly still in
3248      the given ts (current_ts), so look in both.  */
3249   if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN) 
3250     {
3251       if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3252                             &(tmp_sym->declared_at)) != SUCCESS)
3253         {
3254           /* See if we're dealing with a sym in a common block or not.  */
3255           if (is_in_common == 1)
3256             {
3257               gfc_warning ("Variable '%s' in common block '%s' at %L "
3258                            "may not be a C interoperable "
3259                            "kind though common block '%s' is BIND(C)",
3260                            tmp_sym->name, com_block->name,
3261                            &(tmp_sym->declared_at), com_block->name);
3262             }
3263           else
3264             {
3265               if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3266                 gfc_error ("Type declaration '%s' at %L is not C "
3267                            "interoperable but it is BIND(C)",
3268                            tmp_sym->name, &(tmp_sym->declared_at));
3269               else
3270                 gfc_warning ("Variable '%s' at %L "
3271                              "may not be a C interoperable "
3272                              "kind but it is bind(c)",
3273                              tmp_sym->name, &(tmp_sym->declared_at));
3274             }
3275         }
3276       
3277       /* Variables declared w/in a common block can't be bind(c)
3278          since there's no way for C to see these variables, so there's
3279          semantically no reason for the attribute.  */
3280       if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3281         {
3282           gfc_error ("Variable '%s' in common block '%s' at "
3283                      "%L cannot be declared with BIND(C) "
3284                      "since it is not a global",
3285                      tmp_sym->name, com_block->name,
3286                      &(tmp_sym->declared_at));
3287           retval = FAILURE;
3288         }
3289       
3290       /* Scalar variables that are bind(c) can not have the pointer
3291          or allocatable attributes.  */
3292       if (tmp_sym->attr.is_bind_c == 1)
3293         {
3294           if (tmp_sym->attr.pointer == 1)
3295             {
3296               gfc_error ("Variable '%s' at %L cannot have both the "
3297                          "POINTER and BIND(C) attributes",
3298                          tmp_sym->name, &(tmp_sym->declared_at));
3299               retval = FAILURE;
3300             }
3301
3302           if (tmp_sym->attr.allocatable == 1)
3303             {
3304               gfc_error ("Variable '%s' at %L cannot have both the "
3305                          "ALLOCATABLE and BIND(C) attributes",
3306                          tmp_sym->name, &(tmp_sym->declared_at));
3307               retval = FAILURE;
3308             }
3309
3310           /* If it is a BIND(C) function, make sure the return value is a
3311              scalar value.  The previous tests in this function made sure
3312              the type is interoperable.  */
3313           if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3314             gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3315                        "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3316
3317           /* BIND(C) functions can not return a character string.  */
3318           if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3319             if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3320                 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3321                 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3322               gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3323                          "be a character string", tmp_sym->name,
3324                          &(tmp_sym->declared_at));
3325         }
3326     }
3327
3328   /* See if the symbol has been marked as private.  If it has, make sure
3329      there is no binding label and warn the user if there is one.  */
3330   if (tmp_sym->attr.access == ACCESS_PRIVATE
3331       && tmp_sym->binding_label[0] != '\0')
3332       /* Use gfc_warning_now because we won't say that the symbol fails
3333          just because of this.  */
3334       gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3335                        "given the binding label '%s'", tmp_sym->name,
3336                        &(tmp_sym->declared_at), tmp_sym->binding_label);
3337
3338   return retval;
3339 }
3340
3341
3342 /* Set the appropriate fields for a symbol that's been declared as
3343    BIND(C) (the is_bind_c flag and the binding label), and verify that
3344    the type is C interoperable.  Errors are reported by the functions
3345    used to set/test these fields.  */
3346
3347 try
3348 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3349 {
3350   try retval = SUCCESS;
3351   
3352   /* TODO: Do we need to make sure the vars aren't marked private?  */
3353
3354   /* Set the is_bind_c bit in symbol_attribute.  */
3355   gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3356
3357   if (set_binding_label (tmp_sym->binding_label, tmp_sym->name, 
3358                          num_idents) != SUCCESS)
3359     return FAILURE;
3360
3361   return retval;
3362 }
3363
3364
3365 /* Set the fields marking the given common block as BIND(C), including
3366    a binding label, and report any errors encountered.  */
3367
3368 try
3369 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3370 {
3371   try retval = SUCCESS;
3372   
3373   /* destLabel, common name, typespec (which may have binding label).  */
3374   if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3375       != SUCCESS)
3376     return FAILURE;
3377
3378   /* Set the given common block (com_block) to being bind(c) (1).  */
3379   set_com_block_bind_c (com_block, 1);
3380
3381   return retval;
3382 }
3383
3384
3385 /* Retrieve the list of one or more identifiers that the given bind(c)
3386    attribute applies to.  */
3387
3388 try
3389 get_bind_c_idents (void)
3390 {
3391   char name[GFC_MAX_SYMBOL_LEN + 1];
3392   int num_idents = 0;
3393   gfc_symbol *tmp_sym = NULL;
3394   match found_id;
3395   gfc_common_head *com_block = NULL;
3396   
3397   if (gfc_match_name (name) == MATCH_YES)
3398     {
3399       found_id = MATCH_YES;
3400       gfc_get_ha_symbol (name, &tmp_sym);
3401     }
3402   else if (match_common_name (name) == MATCH_YES)
3403     {
3404       found_id = MATCH_YES;
3405       com_block = gfc_get_common (name, 0);
3406     }
3407   else
3408     {
3409       gfc_error ("Need either entity or common block name for "
3410                  "attribute specification statement at %C");
3411       return FAILURE;
3412     }
3413    
3414   /* Save the current identifier and look for more.  */
3415   do
3416     {
3417       /* Increment the number of identifiers found for this spec stmt.  */
3418       num_idents++;
3419
3420       /* Make sure we have a sym or com block, and verify that it can
3421          be bind(c).  Set the appropriate field(s) and look for more
3422          identifiers.  */
3423       if (tmp_sym != NULL || com_block != NULL)         
3424         {
3425           if (tmp_sym != NULL)
3426             {
3427               if (set_verify_bind_c_sym (tmp_sym, num_idents)
3428                   != SUCCESS)
3429                 return FAILURE;
3430             }
3431           else
3432             {
3433               if (set_verify_bind_c_com_block(com_block, num_idents)
3434                   != SUCCESS)
3435                 return FAILURE;
3436             }
3437          
3438           /* Look to see if we have another identifier.  */
3439           tmp_sym = NULL;
3440           if (gfc_match_eos () == MATCH_YES)
3441             found_id = MATCH_NO;
3442           else if (gfc_match_char (',') != MATCH_YES)
3443             found_id = MATCH_NO;
3444           else if (gfc_match_name (name) == MATCH_YES)
3445             {
3446               found_id = MATCH_YES;
3447               gfc_get_ha_symbol (name, &tmp_sym);
3448             }
3449           else if (match_common_name (name) == MATCH_YES)
3450             {
3451               found_id = MATCH_YES;
3452               com_block = gfc_get_common (name, 0);
3453             }
3454           else
3455             {
3456               gfc_error ("Missing entity or common block name for "
3457                          "attribute specification statement at %C");
3458               return FAILURE;
3459             }
3460         }
3461       else
3462         {
3463           gfc_internal_error ("Missing symbol");
3464         }
3465     } while (found_id == MATCH_YES);
3466
3467   /* if we get here we were successful */
3468   return SUCCESS;
3469 }
3470
3471
3472 /* Try and match a BIND(C) attribute specification statement.  */
3473    
3474 match
3475 gfc_match_bind_c_stmt (void)
3476 {
3477   match found_match = MATCH_NO;
3478   gfc_typespec *ts;
3479
3480   ts = &current_ts;
3481   
3482   /* This may not be necessary.  */
3483   gfc_clear_ts (ts);
3484   /* Clear the temporary binding label holder.  */
3485   curr_binding_label[0] = '\0';
3486
3487   /* Look for the bind(c).  */
3488   found_match = gfc_match_bind_c (NULL);
3489
3490   if (found_match == MATCH_YES)
3491     {
3492       /* Look for the :: now, but it is not required.  */
3493       gfc_match (" :: ");
3494
3495       /* Get the identifier(s) that needs to be updated.  This may need to
3496          change to hand the flag(s) for the attr specified so all identifiers
3497          found can have all appropriate parts updated (assuming that the same
3498          spec stmt can have multiple attrs, such as both bind(c) and
3499          allocatable...).  */
3500       if (get_bind_c_idents () != SUCCESS)
3501         /* Error message should have printed already.  */
3502         return MATCH_ERROR;
3503     }
3504
3505   return found_match;
3506 }
3507
3508
3509 /* Match a data declaration statement.  */
3510
3511 match
3512 gfc_match_data_decl (void)
3513 {
3514   gfc_symbol *sym;
3515   match m;
3516   int elem;
3517
3518   num_idents_on_line = 0;
3519   
3520   m = gfc_match_type_spec (&current_ts, 0);
3521   if (m != MATCH_YES)
3522     return m;
3523
3524   if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3525     {
3526       sym = gfc_use_derived (current_ts.derived);
3527
3528       if (sym == NULL)
3529         {
3530           m = MATCH_ERROR;
3531           goto cleanup;
3532         }
3533
3534       current_ts.derived = sym;
3535     }
3536
3537   m = match_attr_spec ();
3538   if (m == MATCH_ERROR)
3539     {
3540       m = MATCH_NO;
3541       goto cleanup;
3542     }
3543
3544   if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3545       && !current_ts.derived->attr.zero_comp)
3546     {
3547
3548       if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3549         goto ok;
3550
3551       gfc_find_symbol (current_ts.derived->name,
3552                        current_ts.derived->ns->parent, 1, &sym);
3553
3554       /* Any symbol that we find had better be a type definition
3555          which has its components defined.  */
3556       if (sym != NULL && sym->attr.flavor == FL_DERIVED
3557           && (current_ts.derived->components != NULL
3558               || current_ts.derived->attr.zero_comp))
3559         goto ok;
3560
3561       /* Now we have an error, which we signal, and then fix up
3562          because the knock-on is plain and simple confusing.  */
3563       gfc_error_now ("Derived type at %C has not been previously defined "
3564                      "and so cannot appear in a derived type definition");
3565       current_attr.pointer = 1;
3566       goto ok;
3567     }
3568
3569 ok:
3570   /* If we have an old-style character declaration, and no new-style
3571      attribute specifications, then there a comma is optional between
3572      the type specification and the variable list.  */
3573   if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3574     gfc_match_char (',');
3575
3576   /* Give the types/attributes to symbols that follow. Give the element
3577      a number so that repeat character length expressions can be copied.  */
3578   elem = 1;
3579   for (;;)
3580     {
3581       num_idents_on_line++;
3582       m = variable_decl (elem++);
3583       if (m == MATCH_ERROR)
3584         goto cleanup;
3585       if (m == MATCH_NO)
3586         break;
3587
3588       if (gfc_match_eos () == MATCH_YES)
3589         goto cleanup;
3590       if (gfc_match_char (',') != MATCH_YES)
3591         break;
3592     }
3593
3594   if (gfc_error_flag_test () == 0)
3595     gfc_error ("Syntax error in data declaration at %C");
3596   m = MATCH_ERROR;
3597
3598   gfc_free_data_all (gfc_current_ns);
3599
3600 cleanup:
3601   gfc_free_array_spec (current_as);
3602   current_as = NULL;
3603   return m;
3604 }
3605
3606
3607 /* Match a prefix associated with a function or subroutine
3608    declaration.  If the typespec pointer is nonnull, then a typespec
3609    can be matched.  Note that if nothing matches, MATCH_YES is
3610    returned (the null string was matched).  */
3611
3612 static match
3613 match_prefix (gfc_typespec *ts)
3614 {
3615   int seen_type;
3616
3617   gfc_clear_attr (&current_attr);
3618   seen_type = 0;
3619
3620 loop:
3621   if (!seen_type && ts != NULL
3622       && gfc_match_type_spec (ts, 0) == MATCH_YES
3623       && gfc_match_space () == MATCH_YES)
3624     {
3625
3626       seen_type = 1;
3627       goto loop;
3628     }
3629
3630   if (gfc_match ("elemental% ") == MATCH_YES)
3631     {
3632       if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3633         return MATCH_ERROR;
3634
3635       goto loop;
3636     }
3637
3638   if (gfc_match ("pure% ") == MATCH_YES)
3639     {
3640       if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3641         return MATCH_ERROR;
3642
3643       goto loop;
3644     }
3645
3646   if (gfc_match ("recursive% ") == MATCH_YES)
3647     {
3648       if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3649         return MATCH_ERROR;
3650
3651       goto loop;
3652     }
3653
3654   /* At this point, the next item is not a prefix.  */
3655   return MATCH_YES;
3656 }
3657
3658
3659 /* Copy attributes matched by match_prefix() to attributes on a symbol.  */
3660
3661 static try
3662 copy_prefix (symbol_attribute *dest, locus *where)
3663 {
3664   if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3665     return FAILURE;
3666
3667   if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3668     return FAILURE;
3669
3670   if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3671     return FAILURE;
3672
3673   return SUCCESS;
3674 }
3675
3676
3677 /* Match a formal argument list.  */
3678
3679 match
3680 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3681 {
3682   gfc_formal_arglist *head, *tail, *p, *q;
3683   char name[GFC_MAX_SYMBOL_LEN + 1];
3684   gfc_symbol *sym;
3685   match m;
3686
3687   head = tail = NULL;
3688
3689   if (gfc_match_char ('(') != MATCH_YES)
3690     {
3691       if (null_flag)
3692         goto ok;
3693       return MATCH_NO;
3694     }
3695
3696   if (gfc_match_char (')') == MATCH_YES)
3697     goto ok;
3698
3699   for (;;)
3700     {
3701       if (gfc_match_char ('*') == MATCH_YES)
3702         sym = NULL;
3703       else
3704         {
3705           m = gfc_match_name (name);
3706           if (m != MATCH_YES)
3707             goto cleanup;
3708
3709           if (gfc_get_symbol (name, NULL, &sym))
3710             goto cleanup;
3711         }
3712
3713       p = gfc_get_formal_arglist ();
3714
3715       if (head == NULL)
3716         head = tail = p;
3717       else
3718         {
3719           tail->next = p;
3720           tail = p;
3721         }
3722
3723       tail->sym = sym;
3724
3725       /* We don't add the VARIABLE flavor because the name could be a
3726          dummy procedure.  We don't apply these attributes to formal
3727          arguments of statement functions.  */
3728       if (sym != NULL && !st_flag
3729           && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3730               || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3731         {
3732           m = MATCH_ERROR;
3733           goto cleanup;
3734         }
3735
3736       /* The name of a program unit can be in a different namespace,
3737          so check for it explicitly.  After the statement is accepted,
3738          the name is checked for especially in gfc_get_symbol().  */
3739       if (gfc_new_block != NULL && sym != NULL
3740           && strcmp (sym->name, gfc_new_block->name) == 0)
3741         {
3742           gfc_error ("Name '%s' at %C is the name of the procedure",
3743                      sym->name);
3744           m = MATCH_ERROR;
3745           goto cleanup;
3746         }
3747
3748       if (gfc_match_char (')') == MATCH_YES)
3749         goto ok;
3750
3751       m = gfc_match_char (',');
3752       if (m != MATCH_YES)
3753         {
3754           gfc_error ("Unexpected junk in formal argument list at %C");
3755           goto cleanup;
3756         }
3757     }
3758
3759 ok:
3760   /* Check for duplicate symbols in the formal argument list.  */
3761   if (head != NULL)
3762     {
3763       for (p = head; p->next; p = p->next)
3764         {
3765           if (p->sym == NULL)
3766             continue;
3767
3768           for (q = p->next; q; q = q->next)
3769             if (p->sym == q->sym)
3770               {
3771                 gfc_error ("Duplicate symbol '%s' in formal argument list "
3772                            "at %C", p->sym->name);
3773
3774                 m = MATCH_ERROR;
3775                 goto cleanup;
3776               }
3777         }
3778     }
3779
3780   if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3781       == FAILURE)
3782     {
3783       m = MATCH_ERROR;
3784       goto cleanup;
3785     }
3786
3787   return MATCH_YES;
3788
3789 cleanup:
3790   gfc_free_formal_arglist (head);
3791   return m;
3792 }
3793
3794
3795 /* Match a RESULT specification following a function declaration or
3796    ENTRY statement.  Also matches the end-of-statement.  */
3797
3798 static match
3799 match_result (gfc_symbol *function, gfc_symbol **result)
3800 {
3801   char name[GFC_MAX_SYMBOL_LEN + 1];
3802   gfc_symbol *r;
3803   match m;
3804
3805   if (gfc_match (" result (") != MATCH_YES)
3806     return MATCH_NO;
3807
3808   m = gfc_match_name (name);
3809   if (m != MATCH_YES)
3810     return m;
3811
3812   /* Get the right paren, and that's it because there could be the
3813      bind(c) attribute after the result clause.  */
3814   if (gfc_match_char(')') != MATCH_YES)
3815     {
3816      /* TODO: should report the missing right paren here.  */
3817       return MATCH_ERROR;
3818     }
3819
3820   if (strcmp (function->name, name) == 0)
3821     {
3822       gfc_error ("RESULT variable at %C must be different than function name");
3823       return MATCH_ERROR;
3824     }
3825
3826   if (gfc_get_symbol (name, NULL, &r))
3827     return MATCH_ERROR;
3828
3829   if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3830       || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3831     return MATCH_ERROR;
3832
3833   *result = r;
3834
3835   return MATCH_YES;
3836 }
3837
3838
3839 /* Match a function suffix, which could be a combination of a result
3840    clause and BIND(C), either one, or neither.  The draft does not
3841    require them to come in a specific order.  */
3842
3843 match
3844 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3845 {
3846   match is_bind_c;   /* Found bind(c).  */
3847   match is_result;   /* Found result clause.  */
3848   match found_match; /* Status of whether we've found a good match.  */
3849   int peek_char;     /* Character we're going to peek at.  */
3850
3851   /* Initialize to having found nothing.  */
3852   found_match = MATCH_NO;
3853   is_bind_c = MATCH_NO; 
3854   is_result = MATCH_NO;
3855
3856   /* Get the next char to narrow between result and bind(c).  */
3857   gfc_gobble_whitespace ();
3858   peek_char = gfc_peek_char ();
3859
3860   switch (peek_char)
3861     {
3862     case 'r':
3863       /* Look for result clause.  */
3864       is_result = match_result (sym, result);
3865       if (is_result == MATCH_YES)
3866         {
3867           /* Now see if there is a bind(c) after it.  */
3868           is_bind_c = gfc_match_bind_c (sym);
3869           /* We've found the result clause and possibly bind(c).  */
3870           found_match = MATCH_YES;
3871         }
3872       else
3873         /* This should only be MATCH_ERROR.  */
3874         found_match = is_result; 
3875       break;
3876     case 'b':
3877       /* Look for bind(c) first.  */
3878       is_bind_c = gfc_match_bind_c (sym);
3879       if (is_bind_c == MATCH_YES)
3880         {
3881           /* Now see if a result clause followed it.  */
3882           is_result = match_result (sym, result);
3883           found_match = MATCH_YES;
3884         }
3885       else
3886         {
3887           /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
3888           found_match = MATCH_ERROR;
3889         }
3890       break;
3891     default:
3892       gfc_error ("Unexpected junk after function declaration at %C");
3893       found_match = MATCH_ERROR;
3894       break;
3895     }
3896
3897   if (is_bind_c == MATCH_YES)
3898     if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3899         == FAILURE)
3900       return MATCH_ERROR;
3901   
3902   return found_match;
3903 }
3904
3905
3906 /* Match a PROCEDURE declaration (R1211).  */
3907
3908 static match
3909 match_procedure_decl (void)
3910 {
3911   match m;
3912   locus old_loc, entry_loc;
3913   gfc_symbol *sym, *proc_if = NULL;
3914   int num;
3915
3916   old_loc = entry_loc = gfc_current_locus;
3917
3918   gfc_clear_ts (&current_ts);
3919
3920   if (gfc_match (" (") != MATCH_YES)
3921     {
3922       gfc_current_locus = entry_loc;
3923       return MATCH_NO;
3924     }
3925
3926   /* Get the type spec. for the procedure interface.  */
3927   old_loc = gfc_current_locus;
3928   m = gfc_match_type_spec (&current_ts, 0);
3929   if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3930     goto got_ts;
3931
3932   if (m == MATCH_ERROR)
3933     return m;
3934
3935   gfc_current_locus = old_loc;
3936
3937   /* Get the name of the procedure or abstract interface
3938   to inherit the interface from.  */
3939   m = gfc_match_symbol (&proc_if, 1);
3940
3941   if (m == MATCH_NO)
3942     goto syntax;
3943   else if (m == MATCH_ERROR)
3944     return m;
3945
3946   /* Various interface checks.  */
3947   if (proc_if)
3948     {
3949       if (proc_if->generic)
3950         {
3951           gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3952           return MATCH_ERROR;
3953         }
3954       if (proc_if->attr.proc == PROC_ST_FUNCTION)
3955         {
3956           gfc_error ("Interface '%s' at %C may not be a statement function",
3957                     proc_if->name);
3958           return MATCH_ERROR;
3959         }
3960       /* Handle intrinsic procedures.  */
3961       if (gfc_intrinsic_name (proc_if->name, 0)
3962           || gfc_intrinsic_name (proc_if->name, 1))
3963         proc_if->attr.intrinsic = 1;
3964       if (proc_if->attr.intrinsic
3965           && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3966         {
3967           gfc_error ("Intrinsic procedure '%s' not allowed "
3968                     "in PROCEDURE statement at %C", proc_if->name);
3969           return MATCH_ERROR;
3970         }
3971       /* TODO: Allow intrinsics with gfc_intrinsic_actual_ok
3972          (proc_if->name, 0) after PR33162 is fixed.  */
3973       if (proc_if->attr.intrinsic)
3974         {
3975           gfc_error ("Fortran 2003: Support for intrinsic procedure '%s' "
3976                      "in PROCEDURE statement at %C not yet implemented "
3977                      "in gfortran", proc_if->name);
3978           return MATCH_ERROR;
3979         }
3980     }
3981
3982 got_ts:
3983
3984   if (gfc_match (" )") != MATCH_YES)
3985     {
3986       gfc_current_locus = entry_loc;
3987       return MATCH_NO;
3988     }
3989
3990   /* Parse attributes.  */
3991   m = match_attr_spec();
3992   if (m == MATCH_ERROR)
3993     return MATCH_ERROR;
3994
3995   /* Get procedure symbols.  */
3996   for(num=1;;num++)
3997     {
3998
3999       m = gfc_match_symbol (&sym, 0);
4000       if (m == MATCH_NO)
4001         goto syntax;
4002       else if (m == MATCH_ERROR)
4003         return m;
4004
4005       /* Add current_attr to the symbol attributes.  */
4006       if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4007         return MATCH_ERROR;
4008
4009       if (sym->attr.is_bind_c)
4010         {
4011           /* Check for C1218.  */
4012           if (!proc_if || !proc_if->attr.is_bind_c)
4013             {
4014               gfc_error ("BIND(C) attribute at %C requires "
4015                         "an interface with BIND(C)");
4016               return MATCH_ERROR;
4017             }
4018           /* Check for C1217.  */
4019           if (has_name_equals && sym->attr.pointer)
4020             {
4021               gfc_error ("BIND(C) procedure with NAME may not have "
4022                         "POINTER attribute at %C");
4023               return MATCH_ERROR;
4024             }
4025           if (has_name_equals && sym->attr.dummy)
4026             {
4027               gfc_error ("Dummy procedure at %C may not have "
4028                         "BIND(C) attribute with NAME");
4029               return MATCH_ERROR;
4030             }
4031           /* Set binding label for BIND(C).  */
4032           if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4033             return MATCH_ERROR;
4034         }
4035
4036       if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4037         return MATCH_ERROR;
4038       if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4039         return MATCH_ERROR;
4040
4041       /* Set interface.  */
4042       if (proc_if != NULL)
4043         sym->interface = proc_if;
4044       else if (current_ts.type != BT_UNKNOWN)
4045         {
4046           sym->interface = gfc_new_symbol ("", gfc_current_ns);
4047           sym->interface->ts = current_ts;
4048           sym->interface->attr.function = 1;
4049           sym->ts = sym->interface->ts;
4050           sym->attr.function = sym->interface->attr.function;
4051         }
4052
4053       if (gfc_match_eos () == MATCH_YES)
4054         return MATCH_YES;
4055       if (gfc_match_char (',') != MATCH_YES)
4056         goto syntax;
4057     }
4058
4059 syntax:
4060   gfc_error ("Syntax error in PROCEDURE statement at %C");
4061   return MATCH_ERROR;
4062 }
4063
4064
4065 /* Match a PROCEDURE declaration inside an interface (R1206).  */
4066
4067 static match
4068 match_procedure_in_interface (void)
4069 {
4070   match m;
4071   gfc_symbol *sym;
4072   char name[GFC_MAX_SYMBOL_LEN + 1];
4073
4074   if (current_interface.type == INTERFACE_NAMELESS
4075       || current_interface.type == INTERFACE_ABSTRACT)
4076     {
4077       gfc_error ("PROCEDURE at %C must be in a generic interface");
4078       return MATCH_ERROR;
4079     }
4080
4081   for(;;)
4082     {
4083       m = gfc_match_name (name);
4084       if (m == MATCH_NO)
4085         goto syntax;
4086       else if (m == MATCH_ERROR)
4087         return m;
4088       if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4089         return MATCH_ERROR;
4090
4091       if (gfc_add_interface (sym) == FAILURE)
4092         return MATCH_ERROR;
4093
4094       sym->attr.procedure = 1;
4095
4096       if (gfc_match_eos () == MATCH_YES)
4097         break;
4098       if (gfc_match_char (',') != MATCH_YES)
4099         goto syntax;
4100     }
4101
4102   return MATCH_YES;
4103
4104 syntax:
4105   gfc_error ("Syntax error in PROCEDURE statement at %C");
4106   return MATCH_ERROR;
4107 }
4108
4109
4110 /* General matcher for PROCEDURE declarations.  */
4111
4112 match
4113 gfc_match_procedure (void)
4114 {
4115   match m;
4116
4117   switch (gfc_current_state ())
4118     {
4119     case COMP_NONE:
4120     case COMP_PROGRAM:
4121     case COMP_MODULE:
4122     case COMP_SUBROUTINE:
4123     case COMP_FUNCTION:
4124       m = match_procedure_decl ();
4125       break;
4126     case COMP_INTERFACE:
4127       m = match_procedure_in_interface ();
4128       break;
4129     case COMP_DERIVED:
4130       gfc_error ("Fortran 2003: Procedure components at %C are "
4131                 "not yet implemented in gfortran");
4132       return MATCH_ERROR;
4133     default:
4134       return MATCH_NO;
4135     }
4136
4137   if (m != MATCH_YES)
4138     return m;
4139
4140   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4141       == FAILURE)
4142     return MATCH_ERROR;
4143
4144   return m;
4145 }
4146
4147
4148 /* Match a function declaration.  */
4149
4150 match
4151 gfc_match_function_decl (void)
4152 {
4153   char name[GFC_MAX_SYMBOL_LEN + 1];
4154   gfc_symbol *sym, *result;
4155   locus old_loc;
4156   match m;
4157   match suffix_match;
4158   match found_match; /* Status returned by match func.  */  
4159
4160   if (gfc_current_state () != COMP_NONE
4161       && gfc_current_state () != COMP_INTERFACE
4162       && gfc_current_state () != COMP_CONTAINS)
4163     return MATCH_NO;
4164
4165   gfc_clear_ts (&current_ts);
4166
4167   old_loc = gfc_current_locus;
4168
4169   m = match_prefix (&current_ts);
4170   if (m != MATCH_YES)
4171     {
4172       gfc_current_locus = old_loc;
4173       return m;
4174     }
4175
4176   if (gfc_match ("function% %n", name) != MATCH_YES)
4177     {
4178       gfc_current_locus = old_loc;
4179       return MATCH_NO;
4180     }
4181   if (get_proc_name (name, &sym, false))
4182     return MATCH_ERROR;
4183   gfc_new_block = sym;
4184
4185   m = gfc_match_formal_arglist (sym, 0, 0);
4186   if (m == MATCH_NO)
4187     {
4188       gfc_error ("Expected formal argument list in function "
4189                  "definition at %C");
4190       m = MATCH_ERROR;
4191       goto cleanup;
4192     }
4193   else if (m == MATCH_ERROR)
4194     goto cleanup;
4195
4196   result = NULL;
4197
4198   /* According to the draft, the bind(c) and result clause can
4199      come in either order after the formal_arg_list (i.e., either
4200      can be first, both can exist together or by themselves or neither
4201      one).  Therefore, the match_result can't match the end of the
4202      string, and check for the bind(c) or result clause in either order.  */
4203   found_match = gfc_match_eos ();
4204
4205   /* Make sure that it isn't already declared as BIND(C).  If it is, it
4206      must have been marked BIND(C) with a BIND(C) attribute and that is
4207      not allowed for procedures.  */
4208   if (sym->attr.is_bind_c == 1)
4209     {
4210       sym->attr.is_bind_c = 0;
4211       if (sym->old_symbol != NULL)
4212         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4213                        "variables or common blocks",
4214                        &(sym->old_symbol->declared_at));
4215       else
4216         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4217                        "variables or common blocks", &gfc_current_locus);
4218     }
4219
4220   if (found_match != MATCH_YES)
4221     {
4222       /* If we haven't found the end-of-statement, look for a suffix.  */
4223       suffix_match = gfc_match_suffix (sym, &result);
4224       if (suffix_match == MATCH_YES)
4225         /* Need to get the eos now.  */
4226         found_match = gfc_match_eos ();
4227       else
4228         found_match = suffix_match;
4229     }
4230
4231   if(found_match != MATCH_YES)
4232     m = MATCH_ERROR;
4233   else
4234     {
4235       /* Make changes to the symbol.  */
4236       m = MATCH_ERROR;
4237       
4238       if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4239         goto cleanup;
4240       
4241       if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4242           || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4243         goto cleanup;
4244
4245       if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4246           && !sym->attr.implicit_type)
4247         {
4248           gfc_error ("Function '%s' at %C already has a type of %s", name,
4249                      gfc_basic_typename (sym->ts.type));
4250           goto cleanup;
4251         }
4252
4253       if (result == NULL)
4254         {
4255           sym->ts = current_ts;
4256           sym->result = sym;
4257         }
4258       else
4259         {
4260           result->ts = current_ts;
4261           sym->result = result;
4262         }
4263
4264       return MATCH_YES;
4265     }
4266
4267 cleanup:
4268   gfc_current_locus = old_loc;
4269   return m;
4270 }
4271
4272
4273 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4274    pass the name of the entry, rather than the gfc_current_block name, and
4275    to return false upon finding an existing global entry.  */
4276
4277 static bool
4278 add_global_entry (const char *name, int sub)
4279 {
4280   gfc_gsymbol *s;
4281
4282   s = gfc_get_gsymbol(name);
4283
4284   if (s->defined
4285       || (s->type != GSYM_UNKNOWN
4286           && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4287     gfc_global_used(s, NULL);
4288   else
4289     {
4290       s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4291       s->where = gfc_current_locus;
4292       s->defined = 1;
4293       return true;
4294     }
4295   return false;
4296 }
4297
4298
4299 /* Match an ENTRY statement.  */
4300
4301 match
4302 gfc_match_entry (void)
4303 {
4304   gfc_symbol *proc;
4305   gfc_symbol *result;
4306   gfc_symbol *entry;
4307   char name[GFC_MAX_SYMBOL_LEN + 1];
4308   gfc_compile_state state;
4309   match m;
4310   gfc_entry_list *el;
4311   locus old_loc;
4312   bool module_procedure;
4313
4314   m = gfc_match_name (name);
4315   if (m != MATCH_YES)
4316     return m;
4317
4318   state = gfc_current_state ();
4319   if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4320     {
4321       switch (state)
4322         {
4323           case COMP_PROGRAM:
4324             gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4325             break;
4326           case COMP_MODULE:
4327             gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4328             break;
4329           case COMP_BLOCK_DATA:
4330             gfc_error ("ENTRY statement at %C cannot appear within "
4331                        "a BLOCK DATA");
4332             break;
4333           case COMP_INTERFACE:
4334             gfc_error ("ENTRY statement at %C cannot appear within "
4335                        "an INTERFACE");
4336             break;
4337           case COMP_DERIVED:
4338             gfc_error ("ENTRY statement at %C cannot appear within "
4339                        "a DERIVED TYPE block");
4340             break;
4341           case COMP_IF:
4342             gfc_error ("ENTRY statement at %C cannot appear within "
4343                        "an IF-THEN block");
4344             break;
4345           case COMP_DO:
4346             gfc_error ("ENTRY statement at %C cannot appear within "
4347                        "a DO block");
4348             break;
4349           case COMP_SELECT:
4350             gfc_error ("ENTRY statement at %C cannot appear within "
4351                        "a SELECT block");
4352             break;
4353           case COMP_FORALL:
4354             gfc_error ("ENTRY statement at %C cannot appear within "
4355                        "a FORALL block");
4356             break;
4357           case COMP_WHERE:
4358             gfc_error ("ENTRY statement at %C cannot appear within "
4359                        "a WHERE block");
4360             break;
4361           case COMP_CONTAINS:
4362             gfc_error ("ENTRY statement at %C cannot appear within "
4363                        "a contained subprogram");
4364             break;
4365           default:
4366             gfc_internal_error ("gfc_match_entry(): Bad state");
4367         }
4368       return MATCH_ERROR;
4369     }
4370
4371   module_procedure = gfc_current_ns->parent != NULL
4372                    && gfc_current_ns->parent->proc_name
4373                    && gfc_current_ns->parent->proc_name->attr.flavor
4374                       == FL_MODULE;
4375
4376   if (gfc_current_ns->parent != NULL
4377       && gfc_current_ns->parent->proc_name
4378       && !module_procedure)
4379     {
4380       gfc_error("ENTRY statement at %C cannot appear in a "
4381                 "contained procedure");
4382       return MATCH_ERROR;
4383     }
4384
4385   /* Module function entries need special care in get_proc_name
4386      because previous references within the function will have
4387      created symbols attached to the current namespace.  */
4388   if (get_proc_name (name, &entry,
4389                      gfc_current_ns->parent != NULL
4390                      && module_procedure
4391                      && gfc_current_ns->proc_name->attr.function))
4392     return MATCH_ERROR;
4393
4394   proc = gfc_current_block ();
4395
4396   if (state == COMP_SUBROUTINE)
4397     {
4398       /* An entry in a subroutine.  */
4399       if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4400         return MATCH_ERROR;
4401
4402       m = gfc_match_formal_arglist (entry, 0, 1);
4403       if (m != MATCH_YES)
4404         return MATCH_ERROR;
4405
4406       if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4407           || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4408         return MATCH_ERROR;
4409     }
4410   else
4411     {
4412       /* An entry in a function.
4413          We need to take special care because writing
4414             ENTRY f()
4415          as
4416             ENTRY f
4417          is allowed, whereas
4418             ENTRY f() RESULT (r)
4419          can't be written as
4420             ENTRY f RESULT (r).  */
4421       if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4422         return MATCH_ERROR;
4423
4424       old_loc = gfc_current_locus;
4425       if (gfc_match_eos () == MATCH_YES)
4426         {
4427           gfc_current_locus = old_loc;
4428           /* Match the empty argument list, and add the interface to
4429              the symbol.  */
4430           m = gfc_match_formal_arglist (entry, 0, 1);
4431         }
4432       else
4433         m = gfc_match_formal_arglist (entry, 0, 0);
4434
4435       if (m != MATCH_YES)
4436         return MATCH_ERROR;
4437
4438       result = NULL;
4439
4440       if (gfc_match_eos () == MATCH_YES)
4441         {
4442           if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4443               || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4444             return MATCH_ERROR;
4445
4446           entry->result = entry;
4447         }
4448       else
4449         {
4450           m = match_result (proc, &result);
4451           if (m == MATCH_NO)
4452             gfc_syntax_error (ST_ENTRY);
4453           if (m != MATCH_YES)
4454             return MATCH_ERROR;
4455
4456           if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4457               || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4458               || gfc_add_function (&entry->attr, result->name, NULL)
4459                  == FAILURE)
4460             return MATCH_ERROR;
4461
4462           entry->result = result;
4463         }
4464     }
4465
4466   if (gfc_match_eos () != MATCH_YES)
4467     {
4468       gfc_syntax_error (ST_ENTRY);
4469       return MATCH_ERROR;
4470     }
4471
4472   entry->attr.recursive = proc->attr.recursive;
4473   entry->attr.elemental = proc->attr.elemental;
4474   entry->attr.pure = proc->attr.pure;
4475
4476   el = gfc_get_entry_list ();
4477   el->sym = entry;
4478   el->next = gfc_current_ns->entries;
4479   gfc_current_ns->entries = el;
4480   if (el->next)
4481     el->id = el->next->id + 1;
4482   else
4483     el->id = 1;
4484
4485   new_st.op = EXEC_ENTRY;
4486   new_st.ext.entry = el;
4487
4488   return MATCH_YES;
4489 }
4490
4491
4492 /* Match a subroutine statement, including optional prefixes.  */
4493
4494 match
4495 gfc_match_subroutine (void)
4496 {
4497   char name[GFC_MAX_SYMBOL_LEN + 1];
4498   gfc_symbol *sym;
4499   match m;
4500   match is_bind_c;
4501   char peek_char;
4502
4503   if (gfc_current_state () != COMP_NONE
4504       && gfc_current_state () != COMP_INTERFACE
4505       && gfc_current_state () != COMP_CONTAINS)
4506     return MATCH_NO;
4507
4508   m = match_prefix (NULL);
4509   if (m != MATCH_YES)
4510     return m;
4511
4512   m = gfc_match ("subroutine% %n", name);
4513   if (m != MATCH_YES)
4514     return m;
4515
4516   if (get_proc_name (name, &sym, false))
4517     return MATCH_ERROR;
4518   gfc_new_block = sym;
4519
4520   /* Check what next non-whitespace character is so we can tell if there
4521      where the required parens if we have a BIND(C).  */
4522   gfc_gobble_whitespace ();
4523   peek_char = gfc_peek_char ();
4524   
4525   if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4526     return MATCH_ERROR;
4527
4528   if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4529     return MATCH_ERROR;
4530
4531   /* Make sure that it isn't already declared as BIND(C).  If it is, it
4532      must have been marked BIND(C) with a BIND(C) attribute and that is
4533      not allowed for procedures.  */
4534   if (sym->attr.is_bind_c == 1)
4535     {
4536       sym->attr.is_bind_c = 0;
4537       if (sym->old_symbol != NULL)
4538         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4539                        "variables or common blocks",
4540                        &(sym->old_symbol->declared_at));
4541       else
4542         gfc_error_now ("BIND(C) attribute at %L can only be used for "
4543                        "variables or common blocks", &gfc_current_locus);
4544     }
4545   
4546   /* Here, we are just checking if it has the bind(c) attribute, and if
4547      so, then we need to make sure it's all correct.  If it doesn't,
4548      we still need to continue matching the rest of the subroutine line.  */
4549   is_bind_c = gfc_match_bind_c (sym);
4550   if (is_bind_c == MATCH_ERROR)
4551     {
4552       /* There was an attempt at the bind(c), but it was wrong.  An
4553          error message should have been printed w/in the gfc_match_bind_c
4554          so here we'll just return the MATCH_ERROR.  */
4555       return MATCH_ERROR;
4556     }
4557
4558   if (is_bind_c == MATCH_YES)
4559     {
4560       if (peek_char != '(')
4561         {
4562           gfc_error ("Missing required parentheses before BIND(C) at %C");
4563           return MATCH_ERROR;
4564         }
4565       if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4566           == FAILURE)
4567         return MATCH_ERROR;
4568     }
4569   
4570   if (gfc_match_eos () != MATCH_YES)
4571     {
4572       gfc_syntax_error (ST_SUBROUTINE);
4573       return MATCH_ERROR;
4574     }
4575
4576   if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4577     return MATCH_ERROR;
4578
4579   return MATCH_YES;
4580 }
4581
4582
4583 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4584    given, and set the binding label in either the given symbol (if not
4585    NULL), or in the current_ts.  The symbol may be NULL because we may
4586    encounter the BIND(C) before the declaration itself.  Return
4587    MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4588    MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4589    or MATCH_YES if the specifier was correct and the binding label and
4590    bind(c) fields were set correctly for the given symbol or the
4591    current_ts.  */
4592
4593 match
4594 gfc_match_bind_c (gfc_symbol *sym)
4595 {
4596   /* binding label, if exists */   
4597   char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4598   match double_quote;
4599   match single_quote;
4600
4601   /* Initialize the flag that specifies whether we encountered a NAME= 
4602      specifier or not.  */
4603   has_name_equals = 0;
4604
4605   /* Init the first char to nil so we can catch if we don't have
4606      the label (name attr) or the symbol name yet.  */
4607   binding_label[0] = '\0';
4608    
4609   /* This much we have to be able to match, in this order, if
4610      there is a bind(c) label.  */
4611   if (gfc_match (" bind ( c ") != MATCH_YES)
4612     return MATCH_NO;
4613
4614   /* Now see if there is a binding label, or if we've reached the
4615      end of the bind(c) attribute without one.  */
4616   if (gfc_match_char (',') == MATCH_YES)
4617     {
4618       if (gfc_match (" name = ") != MATCH_YES)
4619         {
4620           gfc_error ("Syntax error in NAME= specifier for binding label "
4621                      "at %C");
4622           /* should give an error message here */
4623           return MATCH_ERROR;
4624         }
4625
4626       has_name_equals = 1;
4627
4628       /* Get the opening quote.  */
4629       double_quote = MATCH_YES;
4630       single_quote = MATCH_YES;
4631       double_quote = gfc_match_char ('"');
4632       if (double_quote != MATCH_YES)
4633         single_quote = gfc_match_char ('\'');
4634       if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4635         {
4636           gfc_error ("Syntax error in NAME= specifier for binding label "
4637                      "at %C");
4638           return MATCH_ERROR;
4639         }
4640       
4641       /* Grab the binding label, using functions that will not lower
4642          case the names automatically.  */
4643       if (gfc_match_name_C (binding_label) != MATCH_YES)
4644          return MATCH_ERROR;
4645       
4646       /* Get the closing quotation.  */
4647       if (double_quote == MATCH_YES)
4648         {
4649           if (gfc_match_char ('"') != MATCH_YES)
4650             {
4651               gfc_error ("Missing closing quote '\"' for binding label at %C");
4652               /* User started string with '"' so looked to match it.  */
4653               return MATCH_ERROR;
4654             }
4655         }
4656       else
4657         {
4658           if (gfc_match_char ('\'') != MATCH_YES)
4659             {
4660               gfc_error ("Missing closing quote '\'' for binding label at %C");
4661               /* User started string with "'" char.  */
4662               return MATCH_ERROR;
4663             }
4664         }
4665    }
4666
4667   /* Get the required right paren.  */
4668   if (gfc_match_char (')') != MATCH_YES)
4669     {
4670       gfc_error ("Missing closing paren for binding label at %C");
4671       return MATCH_ERROR;
4672     }
4673
4674   /* Save the binding label to the symbol.  If sym is null, we're
4675      probably matching the typespec attributes of a declaration and
4676      haven't gotten the name yet, and therefore, no symbol yet.  */
4677   if (binding_label[0] != '\0')
4678     {
4679       if (sym != NULL)
4680       {
4681         strncpy (sym->binding_label, binding_label,
4682                  strlen (binding_label)+1);
4683       }
4684       else
4685         strncpy (curr_binding_label, binding_label,
4686                  strlen (binding_label) + 1);
4687     }
4688   else
4689     {
4690       /* No binding label, but if symbol isn't null, we
4691          can set the label for it here.  */
4692       /* TODO: If the name= was given and no binding label (name=""), we simply
4693          will let fortran mangle the symbol name as it usually would.
4694          However, this could still let C call it if the user looked up the
4695          symbol in the object file.  Should the name set during mangling in
4696          trans-decl.c be marked with characters that are invalid for C to
4697          prevent this?  */
4698       if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4699         strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4700     }
4701
4702   if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4703       && current_interface.type == INTERFACE_ABSTRACT)
4704     {
4705       gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4706       return MATCH_ERROR;
4707     }
4708
4709   return MATCH_YES;
4710 }
4711
4712
4713 /* Return nonzero if we're currently compiling a contained procedure.  */
4714
4715 static int
4716 contained_procedure (void)
4717 {
4718   gfc_state_data *s;
4719
4720   for (s=gfc_state_stack; s; s=s->previous)
4721     if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4722         && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4723       return 1;
4724
4725   return 0;
4726 }
4727
4728 /* Set the kind of each enumerator.  The kind is selected such that it is
4729    interoperable with the corresponding C enumeration type, making
4730    sure that -fshort-enums is honored.  */
4731
4732 static void
4733 set_enum_kind(void)
4734 {
4735   enumerator_history *current_history = NULL;
4736   int kind;
4737   int i;
4738
4739   if (max_enum == NULL || enum_history == NULL)
4740     return;
4741
4742   if (!gfc_option.fshort_enums)
4743     return;
4744
4745   i = 0;
4746   do
4747     {
4748       kind = gfc_integer_kinds[i++].kind;
4749     }
4750   while (kind < gfc_c_int_kind
4751          && gfc_check_integer_range (max_enum->initializer->value.integer,
4752                                      kind) != ARITH_OK);
4753
4754   current_history = enum_history;
4755   while (current_history != NULL)
4756     {
4757       current_history->sym->ts.kind = kind;
4758       current_history = current_history->next;
4759     }
4760 }
4761
4762
4763 /* Match any of the various end-block statements.  Returns the type of
4764    END to the caller.  The END INTERFACE, END IF, END DO and END
4765    SELECT statements cannot be replaced by a single END statement.  */
4766
4767 match
4768 gfc_match_end (gfc_statement *st)
4769 {
4770   char name[GFC_MAX_SYMBOL_LEN + 1];
4771   gfc_compile_state state;
4772   locus old_loc;
4773   const char *block_name;
4774   const char *target;
4775   int eos_ok;
4776   match m;
4777
4778   old_loc = gfc_current_locus;
4779   if (gfc_match ("end") != MATCH_YES)
4780     return MATCH_NO;
4781
4782   state = gfc_current_state ();
4783   block_name = gfc_current_block () == NULL
4784              ? NULL : gfc_current_block ()->name;
4785
4786   if (state == COMP_CONTAINS)
4787     {
4788       state = gfc_state_stack->previous->state;
4789       block_name = gfc_state_stack->previous->sym == NULL
4790                  ? NULL : gfc_state_stack->previous->sym->name;
4791     }
4792
4793   switch (state)
4794     {
4795     case COMP_NONE:
4796     case COMP_PROGRAM:
4797       *st = ST_END_PROGRAM;
4798       target = " program";
4799       eos_ok = 1;
4800       break;
4801
4802     case COMP_SUBROUTINE:
4803       *st = ST_END_SUBROUTINE;
4804       target = " subroutine";
4805       eos_ok = !contained_procedure ();
4806       break;
4807
4808     case COMP_FUNCTION:
4809       *st = ST_END_FUNCTION;
4810       target = " function";
4811       eos_ok = !contained_procedure ();
4812       break;
4813
4814     case COMP_BLOCK_DATA:
4815       *st = ST_END_BLOCK_DATA;
4816       target = " block data";
4817       eos_ok = 1;
4818       break;
4819
4820     case COMP_MODULE:
4821       *st = ST_END_MODULE;
4822       target = " module";
4823       eos_ok = 1;
4824       break;
4825
4826     case COMP_INTERFACE:
4827       *st = ST_END_INTERFACE;
4828       target = " interface";
4829       eos_ok = 0;
4830       break;
4831
4832     case COMP_DERIVED:
4833       *st = ST_END_TYPE;
4834       target = " type";
4835       eos_ok = 0;
4836       break;
4837
4838     case COMP_IF:
4839       *st = ST_ENDIF;
4840       target = " if";
4841       eos_ok = 0;
4842       break;
4843
4844     case COMP_DO:
4845       *st = ST_ENDDO;
4846       target = " do";
4847       eos_ok = 0;
4848       break;
4849
4850     case COMP_SELECT:
4851       *st = ST_END_SELECT;
4852       target = " select";
4853       eos_ok = 0;
4854       break;
4855
4856     case COMP_FORALL:
4857       *st = ST_END_FORALL;
4858       target = " forall";
4859       eos_ok = 0;
4860       break;
4861
4862     case COMP_WHERE:
4863       *st = ST_END_WHERE;
4864       target = " where";
4865       eos_ok = 0;
4866       break;
4867
4868     case COMP_ENUM:
4869       *st = ST_END_ENUM;
4870       target = " enum";
4871       eos_ok = 0;
4872       last_initializer = NULL;
4873       set_enum_kind ();
4874       gfc_free_enum_history ();
4875       break;
4876
4877     default:
4878       gfc_error ("Unexpected END statement at %C");
4879       goto cleanup;
4880     }
4881
4882   if (gfc_match_eos () == MATCH_YES)
4883     {
4884       if (!eos_ok)
4885         {
4886           /* We would have required END [something].  */
4887           gfc_error ("%s statement expected at %L",
4888                      gfc_ascii_statement (*st), &old_loc);
4889           goto cleanup;
4890         }
4891
4892       return MATCH_YES;
4893     }
4894
4895   /* Verify that we've got the sort of end-block that we're expecting.  */
4896   if (gfc_match (target) != MATCH_YES)
4897     {
4898       gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4899       goto cleanup;
4900     }
4901
4902   /* If we're at the end, make sure a block name wasn't required.  */
4903   if (gfc_match_eos () == MATCH_YES)
4904     {
4905
4906       if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4907           && *st != ST_END_FORALL && *st != ST_END_WHERE)
4908         return MATCH_YES;
4909
4910       if (gfc_current_block () == NULL)
4911         return MATCH_YES;
4912
4913       gfc_error ("Expected block name of '%s' in %s statement at %C",
4914                  block_name, gfc_ascii_statement (*st));
4915
4916       return MATCH_ERROR;
4917     }
4918
4919   /* END INTERFACE has a special handler for its several possible endings.  */
4920   if (*st == ST_END_INTERFACE)
4921     return gfc_match_end_interface ();
4922
4923   /* We haven't hit the end of statement, so what is left must be an
4924      end-name.  */
4925   m = gfc_match_space ();
4926   if (m == MATCH_YES)
4927     m = gfc_match_name (name);
4928
4929   if (m == MATCH_NO)
4930     gfc_error ("Expected terminating name at %C");
4931   if (m != MATCH_YES)
4932     goto cleanup;
4933
4934   if (block_name == NULL)
4935     goto syntax;
4936
4937   if (strcmp (name, block_name) != 0)
4938     {
4939       gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4940                  gfc_ascii_statement (*st));
4941       goto cleanup;
4942     }
4943
4944   if (gfc_match_eos () == MATCH_YES)
4945     return MATCH_YES;
4946
4947 syntax:
4948   gfc_syntax_error (*st);
4949
4950 cleanup:
4951   gfc_current_locus = old_loc;
4952   return MATCH_ERROR;
4953 }
4954
4955
4956
4957 /***************** Attribute declaration statements ****************/
4958
4959 /* Set the attribute of a single variable.  */
4960
4961 static match
4962 attr_decl1 (void)
4963 {
4964   char name[GFC_MAX_SYMBOL_LEN + 1];
4965   gfc_array_spec *as;
4966   gfc_symbol *sym;
4967   locus var_locus;
4968   match m;
4969
4970   as = NULL;
4971
4972   m = gfc_match_name (name);
4973   if (m != MATCH_YES)
4974     goto cleanup;
4975
4976   if (find_special (name, &sym))
4977     return MATCH_ERROR;
4978
4979   var_locus = gfc_current_locus;
4980
4981   /* Deal with possible array specification for certain attributes.  */
4982   if (current_attr.dimension
4983       || current_attr.allocatable
4984       || current_attr.pointer
4985       || current_attr.target)
4986     {
4987       m = gfc_match_array_spec (&as);
4988       if (m == MATCH_ERROR)
4989         goto cleanup;
4990
4991       if (current_attr.dimension && m == MATCH_NO)
4992         {
4993           gfc_error ("Missing array specification at %L in DIMENSION "
4994                      "statement", &var_locus);
4995           m = MATCH_ERROR;
4996           goto cleanup;
4997         }
4998
4999       if ((current_attr.allocatable || current_attr.pointer)
5000           && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5001         {
5002           gfc_error ("Array specification must be deferred at %L", &var_locus);
5003           m = MATCH_ERROR;
5004           goto cleanup;
5005         }
5006     }
5007
5008   /* Update symbol table.  DIMENSION attribute is set
5009      in gfc_set_array_spec().  */
5010   if (current_attr.dimension == 0
5011       && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
5012     {
5013       m = MATCH_ERROR;
5014       goto cleanup;
5015     }
5016
5017   if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5018     {
5019       m = MATCH_ERROR;
5020       goto cleanup;
5021     }
5022
5023   if (sym->attr.cray_pointee && sym->as != NULL)
5024     {
5025       /* Fix the array spec.  */
5026       m = gfc_mod_pointee_as (sym->as);         
5027       if (m == MATCH_ERROR)
5028         goto cleanup;
5029     }
5030
5031   if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5032     {
5033       m = MATCH_ERROR;
5034       goto cleanup;
5035     }
5036
5037   if ((current_attr.external || current_attr.intrinsic)
5038       && sym->attr.flavor != FL_PROCEDURE
5039       && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5040     {
5041       m = MATCH_ERROR;
5042       goto cleanup;
5043     }
5044
5045   return MATCH_YES;
5046
5047 cleanup:
5048   gfc_free_array_spec (as);
5049   return m;
5050 }
5051
5052
5053 /* Generic attribute declaration subroutine.  Used for attributes that
5054    just have a list of names.  */
5055
5056 static match
5057 attr_decl (void)
5058 {
5059   match m;
5060
5061   /* Gobble the optional double colon, by simply ignoring the result
5062      of gfc_match().  */
5063   gfc_match (" ::");
5064
5065   for (;;)
5066     {
5067       m = attr_decl1 ();
5068       if (m != MATCH_YES)
5069         break;
5070
5071       if (gfc_match_eos () == MATCH_YES)
5072         {
5073           m = MATCH_YES;
5074           break;
5075         }
5076
5077       if (gfc_match_char (',') != MATCH_YES)
5078         {
5079           gfc_error ("Unexpected character in variable list at %C");
5080           m = MATCH_ERROR;
5081           break;
5082         }
5083     }
5084
5085   return m;
5086 }
5087
5088
5089 /* This routine matches Cray Pointer declarations of the form:
5090    pointer ( <pointer>, <pointee> )
5091    or
5092    pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5093    The pointer, if already declared, should be an integer.  Otherwise, we
5094    set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
5095    be either a scalar, or an array declaration.  No space is allocated for
5096    the pointee.  For the statement
5097    pointer (ipt, ar(10))
5098    any subsequent uses of ar will be translated (in C-notation) as
5099    ar(i) => ((<type> *) ipt)(i)
5100    After gimplification, pointee variable will disappear in the code.  */
5101
5102 static match
5103 cray_pointer_decl (void)
5104 {
5105   match m;
5106   gfc_array_spec *as;
5107   gfc_symbol *cptr; /* Pointer symbol.  */
5108   gfc_symbol *cpte; /* Pointee symbol.  */
5109   locus var_locus;
5110   bool done = false;
5111
5112   while (!done)
5113     {
5114       if (gfc_match_char ('(') != MATCH_YES)
5115         {
5116           gfc_error ("Expected '(' at %C");
5117           return MATCH_ERROR;
5118         }
5119
5120       /* Match pointer.  */
5121       var_locus = gfc_current_locus;
5122       gfc_clear_attr (&current_attr);
5123       gfc_add_cray_pointer (&current_attr, &var_locus);
5124       current_ts.type = BT_INTEGER;
5125       current_ts.kind = gfc_index_integer_kind;
5126
5127       m = gfc_match_symbol (&cptr, 0);
5128       if (m != MATCH_YES)
5129         {
5130           gfc_error ("Expected variable name at %C");
5131           return m;
5132         }
5133
5134       if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5135         return MATCH_ERROR;
5136
5137       gfc_set_sym_referenced (cptr);
5138
5139       if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
5140         {
5141           cptr->ts.type = BT_INTEGER;
5142           cptr->ts.kind = gfc_index_integer_kind;
5143         }
5144       else if (cptr->ts.type != BT_INTEGER)
5145         {
5146           gfc_error ("Cray pointer at %C must be an integer");
5147           return MATCH_ERROR;
5148         }
5149       else if (cptr->ts.kind < gfc_index_integer_kind)
5150         gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5151                      " memory addresses require %d bytes",
5152                      cptr->ts.kind, gfc_index_integer_kind);
5153
5154       if (gfc_match_char (',') != MATCH_YES)
5155         {
5156           gfc_error ("Expected \",\" at %C");
5157           return MATCH_ERROR;
5158         }
5159
5160       /* Match Pointee.  */
5161       var_locus = gfc_current_locus;
5162       gfc_clear_attr (&current_attr);
5163       gfc_add_cray_pointee (&current_attr, &var_locus);
5164       current_ts.type = BT_UNKNOWN;
5165       current_ts.kind = 0;
5166
5167       m = gfc_match_symbol (&cpte, 0);
5168       if (m != MATCH_YES)
5169         {
5170           gfc_error ("Expected variable name at %C");
5171           return m;
5172         }
5173
5174       /* Check for an optional array spec.  */
5175       m = gfc_match_array_spec (&as);
5176       if (m == MATCH_ERROR)
5177         {
5178           gfc_free_array_spec (as);
5179           return m;
5180         }
5181       else if (m == MATCH_NO)
5182         {
5183           gfc_free_array_spec (as);
5184           as = NULL;
5185         }   
5186
5187       if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5188         return MATCH_ERROR;
5189
5190       gfc_set_sym_referenced (cpte);
5191
5192       if (cpte->as == NULL)
5193         {
5194           if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5195             gfc_internal_error ("Couldn't set Cray pointee array spec.");
5196         }
5197       else if (as != NULL)
5198         {
5199           gfc_error ("Duplicate array spec for Cray pointee at %C");
5200           gfc_free_array_spec (as);
5201           return MATCH_ERROR;
5202         }
5203       
5204       as = NULL;
5205     
5206       if (cpte->as != NULL)
5207         {
5208           /* Fix array spec.  */
5209           m = gfc_mod_pointee_as (cpte->as);
5210           if (m == MATCH_ERROR)
5211             return m;
5212         } 
5213    
5214       /* Point the Pointee at the Pointer.  */
5215       cpte->cp_pointer = cptr;
5216
5217       if (gfc_match_char (')') != MATCH_YES)
5218         {
5219           gfc_error ("Expected \")\" at %C");
5220           return MATCH_ERROR;    
5221         }
5222       m = gfc_match_char (',');
5223       if (m != MATCH_YES)
5224         done = true; /* Stop searching for more declarations.  */
5225
5226     }
5227   
5228   if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
5229       || gfc_match_eos () != MATCH_YES)
5230     {
5231       gfc_error ("Expected \",\" or end of statement at %C");
5232       return MATCH_ERROR;
5233     }
5234   return MATCH_YES;
5235 }
5236
5237
5238 match
5239 gfc_match_external (void)
5240 {
5241
5242   gfc_clear_attr (&current_attr);
5243   current_attr.external = 1;
5244
5245   return attr_decl ();
5246 }
5247
5248
5249 match
5250 gfc_match_intent (void)
5251 {
5252   sym_intent intent;
5253
5254   intent = match_intent_spec ();
5255   if (intent == INTENT_UNKNOWN)
5256     return MATCH_ERROR;
5257
5258   gfc_clear_attr (&current_attr);
5259   current_attr.intent = intent;
5260
5261   return attr_decl ();
5262 }
5263
5264
5265 match
5266 gfc_match_intrinsic (void)
5267 {
5268
5269   gfc_clear_attr (&current_attr);
5270   current_attr.intrinsic = 1;
5271
5272   return attr_decl ();
5273 }
5274
5275
5276 match
5277 gfc_match_optional (void)
5278 {
5279
5280   gfc_clear_attr (&current_attr);
5281   current_attr.optional = 1;
5282
5283   return attr_decl ();
5284 }
5285
5286
5287 match
5288 gfc_match_pointer (void)
5289 {
5290   gfc_gobble_whitespace ();
5291   if (gfc_peek_char () == '(')
5292     {
5293       if (!gfc_option.flag_cray_pointer)
5294         {
5295           gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5296                      "flag");
5297           return MATCH_ERROR;
5298         }
5299       return cray_pointer_decl ();
5300     }
5301   else
5302     {
5303       gfc_clear_attr (&current_attr);
5304       current_attr.pointer = 1;
5305     
5306       return attr_decl ();
5307     }
5308 }
5309
5310
5311 match
5312 gfc_match_allocatable (void)
5313 {
5314   gfc_clear_attr (&current_attr);
5315   current_attr.allocatable = 1;
5316
5317   return attr_decl ();
5318 }
5319
5320
5321 match
5322 gfc_match_dimension (void)
5323 {
5324   gfc_clear_attr (&current_attr);
5325   current_attr.dimension = 1;
5326
5327   return attr_decl ();
5328 }
5329
5330
5331 match
5332 gfc_match_target (void)
5333 {
5334   gfc_clear_attr (&current_attr);
5335   current_attr.target = 1;
5336
5337   return attr_decl ();
5338 }
5339
5340
5341 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5342    statement.  */
5343
5344 static match
5345 access_attr_decl (gfc_statement st)
5346 {
5347   char name[GFC_MAX_SYMBOL_LEN + 1];
5348   interface_type type;
5349   gfc_user_op *uop;
5350   gfc_symbol *sym;
5351   gfc_intrinsic_op operator;
5352   match m;
5353
5354   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5355     goto done;
5356
5357   for (;;)
5358     {
5359       m = gfc_match_generic_spec (&type, name, &operator);
5360       if (m == MATCH_NO)
5361         goto syntax;
5362       if (m == MATCH_ERROR)
5363         return MATCH_ERROR;
5364
5365       switch (type)
5366         {
5367         case INTERFACE_NAMELESS:
5368         case INTERFACE_ABSTRACT:
5369           goto syntax;
5370
5371         case INTERFACE_GENERIC:
5372           if (gfc_get_symbol (name, NULL, &sym))
5373             goto done;
5374
5375           if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5376                                           ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5377                               sym->name, NULL) == FAILURE)
5378             return MATCH_ERROR;
5379
5380           break;
5381
5382         case INTERFACE_INTRINSIC_OP:
5383           if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5384             {
5385               gfc_current_ns->operator_access[operator] =
5386                 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5387             }
5388           else
5389             {
5390               gfc_error ("Access specification of the %s operator at %C has "
5391                          "already been specified", gfc_op2string (operator));
5392               goto done;
5393             }
5394
5395           break;
5396
5397         case INTERFACE_USER_OP:
5398           uop = gfc_get_uop (name);
5399
5400           if (uop->access == ACCESS_UNKNOWN)
5401             {
5402               uop->access = (st == ST_PUBLIC)
5403                           ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5404             }
5405           else
5406             {
5407               gfc_error ("Access specification of the .%s. operator at %C "
5408                          "has already been specified", sym->name);
5409               goto done;
5410             }
5411
5412           break;
5413         }
5414
5415       if (gfc_match_char (',') == MATCH_NO)
5416         break;
5417     }
5418
5419   if (gfc_match_eos () != MATCH_YES)
5420     goto syntax;
5421   return MATCH_YES;
5422
5423 syntax:
5424   gfc_syntax_error (st);
5425
5426 done:
5427   return MATCH_ERROR;
5428 }
5429
5430
5431 match
5432 gfc_match_protected (void)
5433 {
5434   gfc_symbol *sym;
5435   match m;
5436
5437   if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5438     {
5439        gfc_error ("PROTECTED at %C only allowed in specification "
5440                   "part of a module");
5441        return MATCH_ERROR;
5442
5443     }
5444
5445   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5446       == FAILURE)
5447     return MATCH_ERROR;
5448
5449   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5450     {
5451       return MATCH_ERROR;
5452     }
5453
5454   if (gfc_match_eos () == MATCH_YES)
5455     goto syntax;
5456
5457   for(;;)
5458     {
5459       m = gfc_match_symbol (&sym, 0);
5460       switch (m)
5461         {
5462         case MATCH_YES:
5463           if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5464               == FAILURE)
5465             return MATCH_ERROR;
5466           goto next_item;
5467
5468         case MATCH_NO:
5469           break;
5470
5471         case MATCH_ERROR:
5472           return MATCH_ERROR;
5473         }
5474
5475     next_item:
5476       if (gfc_match_eos () == MATCH_YES)
5477         break;
5478       if (gfc_match_char (',') != MATCH_YES)
5479         goto syntax;
5480     }
5481
5482   return MATCH_YES;
5483
5484 syntax:
5485   gfc_error ("Syntax error in PROTECTED statement at %C");
5486   return MATCH_ERROR;
5487 }
5488
5489
5490 /* The PRIVATE statement is a bit weird in that it can be an attribute
5491    declaration, but also works as a standlone statement inside of a
5492    type declaration or a module.  */
5493
5494 match
5495 gfc_match_private (gfc_statement *st)
5496 {
5497
5498   if (gfc_match ("private") != MATCH_YES)
5499     return MATCH_NO;
5500
5501   if (gfc_current_state () != COMP_MODULE
5502       && (gfc_current_state () != COMP_DERIVED
5503           || !gfc_state_stack->previous
5504           || gfc_state_stack->previous->state != COMP_MODULE))
5505     {
5506       gfc_error ("PRIVATE statement at %C is only allowed in the "
5507                  "specification part of a module");
5508       return MATCH_ERROR;
5509     }
5510
5511   if (gfc_current_state () == COMP_DERIVED)
5512     {
5513       if (gfc_match_eos () == MATCH_YES)
5514         {
5515           *st = ST_PRIVATE;
5516           return MATCH_YES;
5517         }
5518
5519       gfc_syntax_error (ST_PRIVATE);
5520       return MATCH_ERROR;
5521     }
5522
5523   if (gfc_match_eos () == MATCH_YES)
5524     {
5525       *st = ST_PRIVATE;
5526       return MATCH_YES;
5527     }
5528
5529   *st = ST_ATTR_DECL;
5530   return access_attr_decl (ST_PRIVATE);
5531 }
5532
5533
5534 match
5535 gfc_match_public (gfc_statement *st)
5536 {
5537
5538   if (gfc_match ("public") != MATCH_YES)
5539     return MATCH_NO;
5540
5541   if (gfc_current_state () != COMP_MODULE)
5542     {
5543       gfc_error ("PUBLIC statement at %C is only allowed in the "
5544                  "specification part of a module");
5545       return MATCH_ERROR;
5546     }
5547
5548   if (gfc_match_eos () == MATCH_YES)
5549     {
5550       *st = ST_PUBLIC;
5551       return MATCH_YES;
5552     }
5553
5554   *st = ST_ATTR_DECL;
5555   return access_attr_decl (ST_PUBLIC);
5556 }
5557
5558
5559 /* Workhorse for gfc_match_parameter.  */
5560
5561 static match
5562 do_parm (void)
5563 {
5564   gfc_symbol *sym;
5565   gfc_expr *init;
5566   match m;
5567
5568   m = gfc_match_symbol (&sym, 0);
5569   if (m == MATCH_NO)
5570     gfc_error ("Expected variable name at %C in PARAMETER statement");
5571
5572   if (m != MATCH_YES)
5573     return m;
5574
5575   if (gfc_match_char ('=') == MATCH_NO)
5576     {
5577       gfc_error ("Expected = sign in PARAMETER statement at %C");
5578       return MATCH_ERROR;
5579     }
5580
5581   m = gfc_match_init_expr (&init);
5582   if (m == MATCH_NO)
5583     gfc_error ("Expected expression at %C in PARAMETER statement");
5584   if (m != MATCH_YES)
5585     return m;
5586
5587   if (sym->ts.type == BT_UNKNOWN
5588       && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5589     {
5590       m = MATCH_ERROR;
5591       goto cleanup;
5592     }
5593
5594   if (gfc_check_assign_symbol (sym, init) == FAILURE
5595       || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5596     {
5597       m = MATCH_ERROR;
5598       goto cleanup;
5599     }
5600
5601   if (sym->ts.type == BT_CHARACTER
5602       && sym->ts.cl != NULL
5603       && sym->ts.cl->length != NULL
5604       && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5605       && init->expr_type == EXPR_CONSTANT
5606       && init->ts.type == BT_CHARACTER
5607       && init->ts.kind == 1)
5608     gfc_set_constant_character_len (
5609       mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5610
5611   sym->value = init;
5612   return MATCH_YES;
5613
5614 cleanup:
5615   gfc_free_expr (init);
5616   return m;
5617 }
5618
5619
5620 /* Match a parameter statement, with the weird syntax that these have.  */
5621
5622 match
5623 gfc_match_parameter (void)
5624 {
5625   match m;
5626
5627   if (gfc_match_char ('(') == MATCH_NO)
5628     return MATCH_NO;
5629
5630   for (;;)
5631     {
5632       m = do_parm ();
5633       if (m != MATCH_YES)
5634         break;
5635
5636       if (gfc_match (" )%t") == MATCH_YES)
5637         break;
5638
5639       if (gfc_match_char (',') != MATCH_YES)
5640         {
5641           gfc_error ("Unexpected characters in PARAMETER statement at %C");
5642           m = MATCH_ERROR;
5643           break;
5644         }
5645     }
5646
5647   return m;
5648 }
5649
5650
5651 /* Save statements have a special syntax.  */
5652
5653 match
5654 gfc_match_save (void)
5655 {
5656   char n[GFC_MAX_SYMBOL_LEN+1];
5657   gfc_common_head *c;
5658   gfc_symbol *sym;
5659   match m;
5660
5661   if (gfc_match_eos () == MATCH_YES)
5662     {
5663       if (gfc_current_ns->seen_save)
5664         {
5665           if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5666                               "follows previous SAVE statement")
5667               == FAILURE)
5668             return MATCH_ERROR;
5669         }
5670
5671       gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5672       return MATCH_YES;
5673     }
5674
5675   if (gfc_current_ns->save_all)
5676     {
5677       if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5678                           "blanket SAVE statement")
5679           == FAILURE)
5680         return MATCH_ERROR;
5681     }
5682
5683   gfc_match (" ::");
5684
5685   for (;;)
5686     {
5687       m = gfc_match_symbol (&sym, 0);
5688       switch (m)
5689         {
5690         case MATCH_YES:
5691           if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5692               == FAILURE)
5693             return MATCH_ERROR;
5694           goto next_item;
5695
5696         case MATCH_NO:
5697           break;
5698
5699         case MATCH_ERROR:
5700           return MATCH_ERROR;
5701         }
5702
5703       m = gfc_match (" / %n /", &n);
5704       if (m == MATCH_ERROR)
5705         return MATCH_ERROR;
5706       if (m == MATCH_NO)
5707         goto syntax;
5708
5709       c = gfc_get_common (n, 0);
5710       c->saved = 1;
5711
5712       gfc_current_ns->seen_save = 1;
5713
5714     next_item:
5715       if (gfc_match_eos () == MATCH_YES)
5716         break;
5717       if (gfc_match_char (',') != MATCH_YES)
5718         goto syntax;
5719     }
5720
5721   return MATCH_YES;
5722
5723 syntax:
5724   gfc_error ("Syntax error in SAVE statement at %C");
5725   return MATCH_ERROR;
5726 }
5727
5728
5729 match
5730 gfc_match_value (void)
5731 {
5732   gfc_symbol *sym;
5733   match m;
5734
5735   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5736       == FAILURE)
5737     return MATCH_ERROR;
5738
5739   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5740     {
5741       return MATCH_ERROR;
5742     }
5743
5744   if (gfc_match_eos () == MATCH_YES)
5745     goto syntax;
5746
5747   for(;;)
5748     {
5749       m = gfc_match_symbol (&sym, 0);
5750       switch (m)
5751         {
5752         case MATCH_YES:
5753           if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5754               == FAILURE)
5755             return MATCH_ERROR;
5756           goto next_item;
5757
5758         case MATCH_NO:
5759           break;
5760
5761         case MATCH_ERROR:
5762           return MATCH_ERROR;
5763         }
5764
5765     next_item:
5766       if (gfc_match_eos () == MATCH_YES)
5767         break;
5768       if (gfc_match_char (',') != MATCH_YES)
5769         goto syntax;
5770     }
5771
5772   return MATCH_YES;
5773
5774 syntax:
5775   gfc_error ("Syntax error in VALUE statement at %C");
5776   return MATCH_ERROR;
5777 }
5778
5779
5780 match
5781 gfc_match_volatile (void)
5782 {
5783   gfc_symbol *sym;
5784   match m;
5785
5786   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5787       == FAILURE)
5788     return MATCH_ERROR;
5789
5790   if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5791     {
5792       return MATCH_ERROR;
5793     }
5794
5795   if (gfc_match_eos () == MATCH_YES)
5796     goto syntax;
5797
5798   for(;;)
5799     {
5800       /* VOLATILE is special because it can be added to host-associated 
5801          symbols locally.  */
5802       m = gfc_match_symbol (&sym, 1);
5803       switch (m)
5804         {
5805         case MATCH_YES:
5806           if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5807               == FAILURE)
5808             return MATCH_ERROR;
5809           goto next_item;
5810
5811         case MATCH_NO:
5812           break;
5813
5814         case MATCH_ERROR:
5815           return MATCH_ERROR;
5816         }
5817
5818     next_item:
5819       if (gfc_match_eos () == MATCH_YES)
5820         break;
5821       if (gfc_match_char (',') != MATCH_YES)
5822         goto syntax;
5823     }
5824
5825   return MATCH_YES;
5826
5827 syntax:
5828   gfc_error ("Syntax error in VOLATILE statement at %C");
5829   return MATCH_ERROR;
5830 }
5831
5832
5833 /* Match a module procedure statement.  Note that we have to modify
5834    symbols in the parent's namespace because the current one was there
5835    to receive symbols that are in an interface's formal argument list.  */
5836
5837 match
5838 gfc_match_modproc (void)
5839 {
5840   char name[GFC_MAX_SYMBOL_LEN + 1];
5841   gfc_symbol *sym;
5842   match m;
5843   gfc_namespace *module_ns;
5844
5845   if (gfc_state_stack->state != COMP_INTERFACE
5846       || gfc_state_stack->previous == NULL
5847       || current_interface.type == INTERFACE_NAMELESS
5848       || current_interface.type == INTERFACE_ABSTRACT)
5849     {
5850       gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5851                  "interface");
5852       return MATCH_ERROR;
5853     }
5854
5855   module_ns = gfc_current_ns->parent;
5856   for (; module_ns; module_ns = module_ns->parent)
5857     if (module_ns->proc_name->attr.flavor == FL_MODULE)
5858       break;
5859
5860   if (module_ns == NULL)
5861     return MATCH_ERROR;
5862
5863   for (;;)
5864     {
5865       m = gfc_match_name (name);
5866       if (m == MATCH_NO)
5867         goto syntax;
5868       if (m != MATCH_YES)
5869         return MATCH_ERROR;
5870
5871       if (gfc_get_symbol (name, module_ns, &sym))
5872         return MATCH_ERROR;
5873
5874       if (sym->attr.proc != PROC_MODULE
5875           && gfc_add_procedure (&sym->attr, PROC_MODULE,
5876                                 sym->name, NULL) == FAILURE)
5877         return MATCH_ERROR;
5878
5879       if (gfc_add_interface (sym) == FAILURE)
5880         return MATCH_ERROR;
5881
5882       sym->attr.mod_proc = 1;
5883
5884       if (gfc_match_eos () == MATCH_YES)
5885         break;
5886       if (gfc_match_char (',') != MATCH_YES)
5887         goto syntax;
5888     }
5889
5890   return MATCH_YES;
5891
5892 syntax:
5893   gfc_syntax_error (ST_MODULE_PROC);
5894   return MATCH_ERROR;
5895 }
5896
5897
5898 /* Match the optional attribute specifiers for a type declaration.
5899    Return MATCH_ERROR if an error is encountered in one of the handled
5900    attributes (public, private, bind(c)), MATCH_NO if what's found is
5901    not a handled attribute, and MATCH_YES otherwise.  TODO: More error
5902    checking on attribute conflicts needs to be done.  */
5903
5904 match
5905 gfc_get_type_attr_spec (symbol_attribute *attr)
5906 {
5907   /* See if the derived type is marked as private.  */
5908   if (gfc_match (" , private") == MATCH_YES)
5909     {
5910       if (gfc_current_state () != COMP_MODULE)
5911         {
5912           gfc_error ("Derived type at %C can only be PRIVATE in the "
5913                      "specification part of a module");
5914           return MATCH_ERROR;
5915         }
5916
5917       if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5918         return MATCH_ERROR;
5919     }
5920   else if (gfc_match (" , public") == MATCH_YES)
5921     {
5922       if (gfc_current_state () != COMP_MODULE)
5923         {
5924           gfc_error ("Derived type at %C can only be PUBLIC in the "
5925                      "specification part of a module");
5926           return MATCH_ERROR;
5927         }
5928
5929       if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5930         return MATCH_ERROR;
5931     }
5932   else if (gfc_match(" , bind ( c )") == MATCH_YES)
5933     {
5934       /* If the type is defined to be bind(c) it then needs to make
5935          sure that all fields are interoperable.  This will
5936          need to be a semantic check on the finished derived type.
5937          See 15.2.3 (lines 9-12) of F2003 draft.  */
5938       if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5939         return MATCH_ERROR;
5940
5941       /* TODO: attr conflicts need to be checked, probably in symbol.c.  */
5942     }
5943   else
5944     return MATCH_NO;
5945
5946   /* If we get here, something matched.  */
5947   return MATCH_YES;
5948 }
5949
5950
5951 /* Match the beginning of a derived type declaration.  If a type name
5952    was the result of a function, then it is possible to have a symbol
5953    already to be known as a derived type yet have no components.  */
5954
5955 match
5956 gfc_match_derived_decl (void)
5957 {
5958   char name[GFC_MAX_SYMBOL_LEN + 1];
5959   symbol_attribute attr;
5960   gfc_symbol *sym;
5961   match m;
5962   match is_type_attr_spec = MATCH_NO;
5963   bool seen_attr = false;
5964
5965   if (gfc_current_state () == COMP_DERIVED)
5966     return MATCH_NO;
5967
5968   gfc_clear_attr (&attr);
5969
5970   do
5971     {
5972       is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5973       if (is_type_attr_spec == MATCH_ERROR)
5974         return MATCH_ERROR;
5975       if (is_type_attr_spec == MATCH_YES)
5976         seen_attr = true;
5977     } while (is_type_attr_spec == MATCH_YES);
5978
5979   if (gfc_match (" ::") != MATCH_YES && seen_attr)
5980     {
5981       gfc_error ("Expected :: in TYPE definition at %C");
5982       return MATCH_ERROR;
5983     }
5984
5985   m = gfc_match (" %n%t", name);
5986   if (m != MATCH_YES)
5987     return m;
5988
5989   /* Make sure the name is not the name of an intrinsic type.  */
5990   if (gfc_is_intrinsic_typename (name))
5991     {
5992       gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5993                  "type", name);
5994       return MATCH_ERROR;
5995     }
5996
5997   if (gfc_get_symbol (name, NULL, &sym))
5998     return MATCH_ERROR;
5999
6000   if (sym->ts.type != BT_UNKNOWN)
6001     {
6002       gfc_error ("Derived type name '%s' at %C already has a basic type "
6003                  "of %s", sym->name, gfc_typename (&sym->ts));
6004       return MATCH_ERROR;
6005     }
6006
6007   /* The symbol may already have the derived attribute without the
6008      components.  The ways this can happen is via a function
6009      definition, an INTRINSIC statement or a subtype in another
6010      derived type that is a pointer.  The first part of the AND clause
6011      is true if a the symbol is not the return value of a function.  */
6012   if (sym->attr.flavor != FL_DERIVED
6013       && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6014     return MATCH_ERROR;
6015
6016   if (sym->components != NULL || sym->attr.zero_comp)
6017     {
6018       gfc_error ("Derived type definition of '%s' at %C has already been "
6019                  "defined", sym->name);
6020       return MATCH_ERROR;
6021     }
6022
6023   if (attr.access != ACCESS_UNKNOWN
6024       && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6025     return MATCH_ERROR;
6026
6027   /* See if the derived type was labeled as bind(c).  */
6028   if (attr.is_bind_c != 0)
6029     sym->attr.is_bind_c = attr.is_bind_c;
6030
6031   gfc_new_block = sym;
6032
6033   return MATCH_YES;
6034 }
6035
6036
6037 /* Cray Pointees can be declared as: 
6038       pointer (ipt, a (n,m,...,*)) 
6039    By default, this is treated as an AS_ASSUMED_SIZE array.  We'll
6040    cheat and set a constant bound of 1 for the last dimension, if this
6041    is the case. Since there is no bounds-checking for Cray Pointees,
6042    this will be okay.  */
6043
6044 try
6045 gfc_mod_pointee_as (gfc_array_spec *as)
6046 {
6047   as->cray_pointee = true; /* This will be useful to know later.  */
6048   if (as->type == AS_ASSUMED_SIZE)
6049     {
6050       as->type = AS_EXPLICIT;
6051       as->upper[as->rank - 1] = gfc_int_expr (1);
6052       as->cp_was_assumed = true;
6053     }
6054   else if (as->type == AS_ASSUMED_SHAPE)
6055     {
6056       gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6057       return MATCH_ERROR;
6058     }
6059   return MATCH_YES;
6060 }
6061
6062
6063 /* Match the enum definition statement, here we are trying to match 
6064    the first line of enum definition statement.  
6065    Returns MATCH_YES if match is found.  */
6066
6067 match
6068 gfc_match_enum (void)
6069 {
6070   match m;
6071   
6072   m = gfc_match_eos ();
6073   if (m != MATCH_YES)
6074     return m;
6075
6076   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6077       == FAILURE)
6078     return MATCH_ERROR;
6079
6080   return MATCH_YES;
6081 }
6082
6083
6084 /* Match a variable name with an optional initializer.  When this
6085    subroutine is called, a variable is expected to be parsed next.
6086    Depending on what is happening at the moment, updates either the
6087    symbol table or the current interface.  */
6088
6089 static match
6090 enumerator_decl (void)
6091 {
6092   char name[GFC_MAX_SYMBOL_LEN + 1];
6093   gfc_expr *initializer;
6094   gfc_array_spec *as = NULL;
6095   gfc_symbol *sym;
6096   locus var_locus;
6097   match m;
6098   try t;
6099   locus old_locus;
6100
6101   initializer = NULL;
6102   old_locus = gfc_current_locus;
6103
6104   /* When we get here, we've just matched a list of attributes and
6105      maybe a type and a double colon.  The next thing we expect to see
6106      is the name of the symbol.  */
6107   m = gfc_match_name (name);
6108   if (m != MATCH_YES)
6109     goto cleanup;
6110
6111   var_locus = gfc_current_locus;
6112
6113   /* OK, we've successfully matched the declaration.  Now put the
6114      symbol in the current namespace. If we fail to create the symbol,
6115      bail out.  */
6116   if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6117     {
6118       m = MATCH_ERROR;
6119       goto cleanup;
6120     }
6121
6122   /* The double colon must be present in order to have initializers.
6123      Otherwise the statement is ambiguous with an assignment statement.  */
6124   if (colon_seen)
6125     {
6126       if (gfc_match_char ('=') == MATCH_YES)
6127         {
6128           m = gfc_match_init_expr (&initializer);
6129           if (m == MATCH_NO)
6130             {
6131               gfc_error ("Expected an initialization expression at %C");
6132               m = MATCH_ERROR;
6133             }
6134
6135           if (m != MATCH_YES)
6136             goto cleanup;
6137         }
6138     }
6139
6140   /* If we do not have an initializer, the initialization value of the
6141      previous enumerator (stored in last_initializer) is incremented
6142      by 1 and is used to initialize the current enumerator.  */
6143   if (initializer == NULL)
6144     initializer = gfc_enum_initializer (last_initializer, old_locus);
6145
6146   if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6147     {
6148       gfc_error("ENUMERATOR %L not initialized with integer expression",
6149                 &var_locus);
6150       m = MATCH_ERROR;
6151       gfc_free_enum_history ();
6152       goto cleanup;
6153     }
6154
6155   /* Store this current initializer, for the next enumerator variable
6156      to be parsed.  add_init_expr_to_sym() zeros initializer, so we
6157      use last_initializer below.  */
6158   last_initializer = initializer;
6159   t = add_init_expr_to_sym (name, &initializer, &var_locus);
6160
6161   /* Maintain enumerator history.  */
6162   gfc_find_symbol (name, NULL, 0, &sym);
6163   create_enum_history (sym, last_initializer);
6164
6165   return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6166
6167 cleanup:
6168   /* Free stuff up and return.  */
6169   gfc_free_expr (initializer);
6170
6171   return m;
6172 }
6173
6174
6175 /* Match the enumerator definition statement.  */
6176
6177 match
6178 gfc_match_enumerator_def (void)
6179 {
6180   match m;
6181   try t;
6182
6183   gfc_clear_ts (&current_ts);
6184
6185   m = gfc_match (" enumerator");
6186   if (m != MATCH_YES)
6187     return m;
6188
6189   m = gfc_match (" :: ");
6190   if (m == MATCH_ERROR)
6191     return m;
6192
6193   colon_seen = (m == MATCH_YES);
6194
6195   if (gfc_current_state () != COMP_ENUM)
6196     {
6197       gfc_error ("ENUM definition statement expected before %C");
6198       gfc_free_enum_history ();
6199       return MATCH_ERROR;
6200     }
6201
6202   (&current_ts)->type = BT_INTEGER;
6203   (&current_ts)->kind = gfc_c_int_kind;
6204
6205   gfc_clear_attr (&current_attr);
6206   t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6207   if (t == FAILURE)
6208     {
6209       m = MATCH_ERROR;
6210       goto cleanup;
6211     }
6212
6213   for (;;)
6214     {
6215       m = enumerator_decl ();
6216       if (m == MATCH_ERROR)
6217         goto cleanup;
6218       if (m == MATCH_NO)
6219         break;
6220
6221       if (gfc_match_eos () == MATCH_YES)
6222         goto cleanup;
6223       if (gfc_match_char (',') != MATCH_YES)
6224         break;
6225     }
6226
6227   if (gfc_current_state () == COMP_ENUM)
6228     {
6229       gfc_free_enum_history ();
6230       gfc_error ("Syntax error in ENUMERATOR definition at %C");
6231       m = MATCH_ERROR;
6232     }
6233
6234 cleanup:
6235   gfc_free_array_spec (current_as);
6236   current_as = NULL;
6237   return m;
6238
6239 }
6240