OSDN Git Service

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