OSDN Git Service

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