OSDN Git Service

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