OSDN Git Service

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