OSDN Git Service

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